openGPMP
Open Source Mathematics Package
rc5.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file. As this is an Open Source effort, all implementations
25  * must be of the same methodology.
26  *
27  *
28  *
29  * This software is distributed on an AS IS basis, WITHOUT
30  * WARRANTY OF ANY KIND, either express or implied.
31  *
32  ************************************************************************/
33 #include <cstdint>
34 #include <openGPMP/nt/rc5.hpp>
35 
36 gpmp::nt::RC5::RC5(uint32_t rounds, uint32_t wordSize, uint32_t blockSize)
37  : w(wordSize), r(rounds), b(blockSize) {
38  init();
39 }
40 
42  S.resize(2 * r + 2);
43  S[0] = P;
44  for (uint32_t i = 1; i < 2 * r + 2; ++i) {
45  S[i] = S[i - 1] + Q;
46  }
47 }
48 
49 void gpmp::nt::RC5::encrypt(uint32_t &A, uint32_t &B) {
50  A += S[0];
51  B += S[1];
52  for (uint32_t i = 1; i <= r; ++i) {
53  A = rotl((A ^ B), B) + S[2 * i];
54  B = rotl((B ^ A), A) + S[2 * i + 1];
55  }
56 }
57 
58 void gpmp::nt::RC5::decrypt(uint32_t &A, uint32_t &B) {
59  for (uint32_t i = r; i > 0; --i) {
60  B = rotr(B - S[2 * i + 1], A) ^ A;
61  A = rotr(A - S[2 * i], B) ^ B;
62  }
63  B -= S[1];
64  A -= S[0];
65 }
66 
67 uint32_t gpmp::nt::RC5::rotl(uint32_t value, uint32_t shift) {
68  return (value << shift) | (value >> (w - shift));
69 }
70 
71 uint32_t gpmp::nt::RC5::rotr(uint32_t value, uint32_t shift) {
72  return (value >> shift) | (value << (w - shift));
73 }
uint32_t rotr(uint32_t value, uint32_t shift)
Definition: rc5.cpp:71
void decrypt(uint32_t &A, uint32_t &B)
Decrypts the given block.
Definition: rc5.cpp:58
void encrypt(uint32_t &A, uint32_t &B)
Encrypts the given block.
Definition: rc5.cpp:49
void init()
Definition: rc5.cpp:41
RC5(uint32_t rounds=12, uint32_t wordSize=32, uint32_t blockSize=16)
Constructs an RC5 object.
Definition: rc5.cpp:36
uint32_t rotl(uint32_t value, uint32_t shift)
Definition: rc5.cpp:67
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23