Botan  1.10.9
rc5.cpp
Go to the documentation of this file.
1 /*
2 * RC5
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/rc5.h>
9 #include <botan/loadstor.h>
10 #include <botan/rotate.h>
11 #include <botan/parsing.h>
12 #include <algorithm>
13 
14 namespace Botan {
15 
16 /*
17 * RC5 Encryption
18 */
19 void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const
20  {
21  const size_t rounds = (S.size() - 2) / 2;
22 
23  for(size_t i = 0; i != blocks; ++i)
24  {
25  u32bit A = load_le<u32bit>(in, 0);
26  u32bit B = load_le<u32bit>(in, 1);
27 
28  A += S[0]; B += S[1];
29  for(size_t j = 0; j != rounds; j += 4)
30  {
31  A = rotate_left(A ^ B, B % 32) + S[2*j+2];
32  B = rotate_left(B ^ A, A % 32) + S[2*j+3];
33 
34  A = rotate_left(A ^ B, B % 32) + S[2*j+4];
35  B = rotate_left(B ^ A, A % 32) + S[2*j+5];
36 
37  A = rotate_left(A ^ B, B % 32) + S[2*j+6];
38  B = rotate_left(B ^ A, A % 32) + S[2*j+7];
39 
40  A = rotate_left(A ^ B, B % 32) + S[2*j+8];
41  B = rotate_left(B ^ A, A % 32) + S[2*j+9];
42  }
43 
44  store_le(out, A, B);
45 
46  in += BLOCK_SIZE;
47  out += BLOCK_SIZE;
48  }
49  }
50 
51 /*
52 * RC5 Decryption
53 */
54 void RC5::decrypt_n(const byte in[], byte out[], size_t blocks) const
55  {
56  const size_t rounds = (S.size() - 2) / 2;
57 
58  for(size_t i = 0; i != blocks; ++i)
59  {
60  u32bit A = load_le<u32bit>(in, 0);
61  u32bit B = load_le<u32bit>(in, 1);
62 
63  for(size_t j = rounds; j != 0; j -= 4)
64  {
65  B = rotate_right(B - S[2*j+1], A % 32) ^ A;
66  A = rotate_right(A - S[2*j ], B % 32) ^ B;
67 
68  B = rotate_right(B - S[2*j-1], A % 32) ^ A;
69  A = rotate_right(A - S[2*j-2], B % 32) ^ B;
70 
71  B = rotate_right(B - S[2*j-3], A % 32) ^ A;
72  A = rotate_right(A - S[2*j-4], B % 32) ^ B;
73 
74  B = rotate_right(B - S[2*j-5], A % 32) ^ A;
75  A = rotate_right(A - S[2*j-6], B % 32) ^ B;
76  }
77  B -= S[1]; A -= S[0];
78 
79  store_le(out, A, B);
80 
81  in += BLOCK_SIZE;
82  out += BLOCK_SIZE;
83  }
84  }
85 
86 /*
87 * RC5 Key Schedule
88 */
89 void RC5::key_schedule(const byte key[], size_t length)
90  {
91  const size_t WORD_KEYLENGTH = (((length - 1) / 4) + 1);
92  const size_t MIX_ROUNDS = 3 * std::max(WORD_KEYLENGTH, S.size());
93 
94  S[0] = 0xB7E15163;
95  for(size_t i = 1; i != S.size(); ++i)
96  S[i] = S[i-1] + 0x9E3779B9;
97 
99 
100  for(s32bit i = length-1; i >= 0; --i)
101  K[i/4] = (K[i/4] << 8) + key[i];
102 
103  u32bit A = 0, B = 0;
104 
105  for(size_t i = 0; i != MIX_ROUNDS; ++i)
106  {
107  A = rotate_left(S[i % S.size()] + A + B, 3);
108  B = rotate_left(K[i % WORD_KEYLENGTH] + A + B, (A + B) % 32);
109  S[i % S.size()] = A;
110  K[i % WORD_KEYLENGTH] = B;
111  }
112  }
113 
114 /*
115 * Return the name of this type
116 */
117 std::string RC5::name() const
118  {
119  return "RC5(" + to_string(get_rounds()) + ")";
120  }
121 
122 /*
123 * RC5 Constructor
124 */
125 RC5::RC5(size_t rounds)
126  {
127  if(rounds < 8 || rounds > 32 || (rounds % 4 != 0))
128  throw Invalid_Argument("RC5: Invalid number of rounds " +
129  to_string(rounds));
130 
131  S.resize(2*rounds + 2);
132  }
133 
134 }
void store_le(u16bit in, byte out[2])
Definition: loadstor.h:427
T rotate_left(T input, size_t rot)
Definition: rotate.h:21
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
signed int s32bit
Definition: types.h:37
unsigned char byte
Definition: types.h:22
void decrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: rc5.cpp:54
T rotate_right(T input, size_t rot)
Definition: rotate.h:34
u32bit load_le< u32bit >(const byte in[], size_t off)
Definition: loadstor.h:183
RC5(size_t rounds)
Definition: rc5.cpp:125
size_t size() const
Definition: secmem.h:29
void encrypt_n(const byte in[], byte out[], size_t blocks) const
Definition: rc5.cpp:19
std::string name() const
Definition: rc5.cpp:117
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
unsigned int u32bit
Definition: types.h:32