Mercurial > dropbear
comparison libtomcrypt/src/ciphers/safer/saferp.c @ 1511:5916af64acd4 fuzz
merge from main
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 17 Feb 2018 19:29:51 +0800 |
parents | 6dba84798cd5 |
children |
comparison
equal
deleted
inserted
replaced
1457:32f990cc96b1 | 1511:5916af64acd4 |
---|---|
3 * LibTomCrypt is a library that provides various cryptographic | 3 * LibTomCrypt is a library that provides various cryptographic |
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | |
9 * Tom St Denis, [email protected], http://libtom.org | |
10 */ | 8 */ |
11 | 9 |
12 /** | 10 /** |
13 @file saferp.c | 11 @file saferp.c |
14 LTC_SAFER+ Implementation by Tom St Denis | 12 LTC_SAFER+ Implementation by Tom St Denis |
15 */ | 13 */ |
16 #include "tomcrypt.h" | 14 #include "tomcrypt.h" |
17 | 15 |
18 #ifdef LTC_SAFERP | 16 #ifdef LTC_SAFERP |
17 | |
18 #define __LTC_SAFER_TAB_C__ | |
19 #include "safer_tab.c" | |
19 | 20 |
20 const struct ltc_cipher_descriptor saferp_desc = | 21 const struct ltc_cipher_descriptor saferp_desc = |
21 { | 22 { |
22 "safer+", | 23 "safer+", |
23 4, | 24 4, |
26 &saferp_ecb_encrypt, | 27 &saferp_ecb_encrypt, |
27 &saferp_ecb_decrypt, | 28 &saferp_ecb_decrypt, |
28 &saferp_test, | 29 &saferp_test, |
29 &saferp_done, | 30 &saferp_done, |
30 &saferp_keysize, | 31 &saferp_keysize, |
31 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | 32 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
32 }; | 33 }; |
33 | 34 |
34 /* ROUND(b,i) | 35 /* ROUND(b,i) |
35 * | 36 * |
36 * This is one forward key application. Note the basic form is | 37 * This is one forward key application. Note the basic form is |
37 * key addition, substitution, key addition. The safer_ebox and safer_lbox | 38 * key addition, substitution, key addition. The safer_ebox and safer_lbox |
38 * are the exponentiation box and logarithm boxes respectively. | 39 * are the exponentiation box and logarithm boxes respectively. |
39 * The value of 'i' is the current round number which allows this | 40 * The value of 'i' is the current round number which allows this |
40 * function to be unrolled massively. Most of LTC_SAFER+'s speed | 41 * function to be unrolled massively. Most of LTC_SAFER+'s speed |
41 * comes from not having to compute indirect accesses into the | 42 * comes from not having to compute indirect accesses into the |
42 * array of 16 bytes b[0..15] which is the block of data | 43 * array of 16 bytes b[0..15] which is the block of data |
43 */ | 44 */ |
44 | 45 |
45 extern const unsigned char safer_ebox[], safer_lbox[]; | 46 #define ROUND(b, i) do { \ |
46 | |
47 #define ROUND(b, i) \ | |
48 b[0] = (safer_ebox[(b[0] ^ skey->saferp.K[i][0]) & 255] + skey->saferp.K[i+1][0]) & 255; \ | 47 b[0] = (safer_ebox[(b[0] ^ skey->saferp.K[i][0]) & 255] + skey->saferp.K[i+1][0]) & 255; \ |
49 b[1] = safer_lbox[(b[1] + skey->saferp.K[i][1]) & 255] ^ skey->saferp.K[i+1][1]; \ | 48 b[1] = safer_lbox[(b[1] + skey->saferp.K[i][1]) & 255] ^ skey->saferp.K[i+1][1]; \ |
50 b[2] = safer_lbox[(b[2] + skey->saferp.K[i][2]) & 255] ^ skey->saferp.K[i+1][2]; \ | 49 b[2] = safer_lbox[(b[2] + skey->saferp.K[i][2]) & 255] ^ skey->saferp.K[i+1][2]; \ |
51 b[3] = (safer_ebox[(b[3] ^ skey->saferp.K[i][3]) & 255] + skey->saferp.K[i+1][3]) & 255; \ | 50 b[3] = (safer_ebox[(b[3] ^ skey->saferp.K[i][3]) & 255] + skey->saferp.K[i+1][3]) & 255; \ |
52 b[4] = (safer_ebox[(b[4] ^ skey->saferp.K[i][4]) & 255] + skey->saferp.K[i+1][4]) & 255; \ | 51 b[4] = (safer_ebox[(b[4] ^ skey->saferp.K[i][4]) & 255] + skey->saferp.K[i+1][4]) & 255; \ |
58 b[10] = safer_lbox[(b[10] + skey->saferp.K[i][10]) & 255] ^ skey->saferp.K[i+1][10]; \ | 57 b[10] = safer_lbox[(b[10] + skey->saferp.K[i][10]) & 255] ^ skey->saferp.K[i+1][10]; \ |
59 b[11] = (safer_ebox[(b[11] ^ skey->saferp.K[i][11]) & 255] + skey->saferp.K[i+1][11]) & 255; \ | 58 b[11] = (safer_ebox[(b[11] ^ skey->saferp.K[i][11]) & 255] + skey->saferp.K[i+1][11]) & 255; \ |
60 b[12] = (safer_ebox[(b[12] ^ skey->saferp.K[i][12]) & 255] + skey->saferp.K[i+1][12]) & 255; \ | 59 b[12] = (safer_ebox[(b[12] ^ skey->saferp.K[i][12]) & 255] + skey->saferp.K[i+1][12]) & 255; \ |
61 b[13] = safer_lbox[(b[13] + skey->saferp.K[i][13]) & 255] ^ skey->saferp.K[i+1][13]; \ | 60 b[13] = safer_lbox[(b[13] + skey->saferp.K[i][13]) & 255] ^ skey->saferp.K[i+1][13]; \ |
62 b[14] = safer_lbox[(b[14] + skey->saferp.K[i][14]) & 255] ^ skey->saferp.K[i+1][14]; \ | 61 b[14] = safer_lbox[(b[14] + skey->saferp.K[i][14]) & 255] ^ skey->saferp.K[i+1][14]; \ |
63 b[15] = (safer_ebox[(b[15] ^ skey->saferp.K[i][15]) & 255] + skey->saferp.K[i+1][15]) & 255; | 62 b[15] = (safer_ebox[(b[15] ^ skey->saferp.K[i][15]) & 255] + skey->saferp.K[i+1][15]) & 255; \ |
63 } while (0) | |
64 | 64 |
65 /* This is one inverse key application */ | 65 /* This is one inverse key application */ |
66 #define iROUND(b, i) \ | 66 #define iROUND(b, i) do { \ |
67 b[0] = safer_lbox[(b[0] - skey->saferp.K[i+1][0]) & 255] ^ skey->saferp.K[i][0]; \ | 67 b[0] = safer_lbox[(b[0] - skey->saferp.K[i+1][0]) & 255] ^ skey->saferp.K[i][0]; \ |
68 b[1] = (safer_ebox[(b[1] ^ skey->saferp.K[i+1][1]) & 255] - skey->saferp.K[i][1]) & 255; \ | 68 b[1] = (safer_ebox[(b[1] ^ skey->saferp.K[i+1][1]) & 255] - skey->saferp.K[i][1]) & 255; \ |
69 b[2] = (safer_ebox[(b[2] ^ skey->saferp.K[i+1][2]) & 255] - skey->saferp.K[i][2]) & 255; \ | 69 b[2] = (safer_ebox[(b[2] ^ skey->saferp.K[i+1][2]) & 255] - skey->saferp.K[i][2]) & 255; \ |
70 b[3] = safer_lbox[(b[3] - skey->saferp.K[i+1][3]) & 255] ^ skey->saferp.K[i][3]; \ | 70 b[3] = safer_lbox[(b[3] - skey->saferp.K[i+1][3]) & 255] ^ skey->saferp.K[i][3]; \ |
71 b[4] = safer_lbox[(b[4] - skey->saferp.K[i+1][4]) & 255] ^ skey->saferp.K[i][4]; \ | 71 b[4] = safer_lbox[(b[4] - skey->saferp.K[i+1][4]) & 255] ^ skey->saferp.K[i][4]; \ |
77 b[10] = (safer_ebox[(b[10] ^ skey->saferp.K[i+1][10]) & 255] - skey->saferp.K[i][10]) & 255; \ | 77 b[10] = (safer_ebox[(b[10] ^ skey->saferp.K[i+1][10]) & 255] - skey->saferp.K[i][10]) & 255; \ |
78 b[11] = safer_lbox[(b[11] - skey->saferp.K[i+1][11]) & 255] ^ skey->saferp.K[i][11]; \ | 78 b[11] = safer_lbox[(b[11] - skey->saferp.K[i+1][11]) & 255] ^ skey->saferp.K[i][11]; \ |
79 b[12] = safer_lbox[(b[12] - skey->saferp.K[i+1][12]) & 255] ^ skey->saferp.K[i][12]; \ | 79 b[12] = safer_lbox[(b[12] - skey->saferp.K[i+1][12]) & 255] ^ skey->saferp.K[i][12]; \ |
80 b[13] = (safer_ebox[(b[13] ^ skey->saferp.K[i+1][13]) & 255] - skey->saferp.K[i][13]) & 255; \ | 80 b[13] = (safer_ebox[(b[13] ^ skey->saferp.K[i+1][13]) & 255] - skey->saferp.K[i][13]) & 255; \ |
81 b[14] = (safer_ebox[(b[14] ^ skey->saferp.K[i+1][14]) & 255] - skey->saferp.K[i][14]) & 255; \ | 81 b[14] = (safer_ebox[(b[14] ^ skey->saferp.K[i+1][14]) & 255] - skey->saferp.K[i][14]) & 255; \ |
82 b[15] = safer_lbox[(b[15] - skey->saferp.K[i+1][15]) & 255] ^ skey->saferp.K[i][15]; | 82 b[15] = safer_lbox[(b[15] - skey->saferp.K[i+1][15]) & 255] ^ skey->saferp.K[i][15]; \ |
83 } while (0) | |
83 | 84 |
84 /* This is a forward single layer PHT transform. */ | 85 /* This is a forward single layer PHT transform. */ |
85 #define PHT(b) \ | 86 #define PHT(b) do { \ |
86 b[0] = (b[0] + (b[1] = (b[0] + b[1]) & 255)) & 255; \ | 87 b[0] = (b[0] + (b[1] = (b[0] + b[1]) & 255)) & 255; \ |
87 b[2] = (b[2] + (b[3] = (b[3] + b[2]) & 255)) & 255; \ | 88 b[2] = (b[2] + (b[3] = (b[3] + b[2]) & 255)) & 255; \ |
88 b[4] = (b[4] + (b[5] = (b[5] + b[4]) & 255)) & 255; \ | 89 b[4] = (b[4] + (b[5] = (b[5] + b[4]) & 255)) & 255; \ |
89 b[6] = (b[6] + (b[7] = (b[7] + b[6]) & 255)) & 255; \ | 90 b[6] = (b[6] + (b[7] = (b[7] + b[6]) & 255)) & 255; \ |
90 b[8] = (b[8] + (b[9] = (b[9] + b[8]) & 255)) & 255; \ | 91 b[8] = (b[8] + (b[9] = (b[9] + b[8]) & 255)) & 255; \ |
91 b[10] = (b[10] + (b[11] = (b[11] + b[10]) & 255)) & 255; \ | 92 b[10] = (b[10] + (b[11] = (b[11] + b[10]) & 255)) & 255; \ |
92 b[12] = (b[12] + (b[13] = (b[13] + b[12]) & 255)) & 255; \ | 93 b[12] = (b[12] + (b[13] = (b[13] + b[12]) & 255)) & 255; \ |
93 b[14] = (b[14] + (b[15] = (b[15] + b[14]) & 255)) & 255; | 94 b[14] = (b[14] + (b[15] = (b[15] + b[14]) & 255)) & 255; \ |
95 } while (0) | |
94 | 96 |
95 /* This is an inverse single layer PHT transform */ | 97 /* This is an inverse single layer PHT transform */ |
96 #define iPHT(b) \ | 98 #define iPHT(b) do { \ |
97 b[15] = (b[15] - (b[14] = (b[14] - b[15]) & 255)) & 255; \ | 99 b[15] = (b[15] - (b[14] = (b[14] - b[15]) & 255)) & 255; \ |
98 b[13] = (b[13] - (b[12] = (b[12] - b[13]) & 255)) & 255; \ | 100 b[13] = (b[13] - (b[12] = (b[12] - b[13]) & 255)) & 255; \ |
99 b[11] = (b[11] - (b[10] = (b[10] - b[11]) & 255)) & 255; \ | 101 b[11] = (b[11] - (b[10] = (b[10] - b[11]) & 255)) & 255; \ |
100 b[9] = (b[9] - (b[8] = (b[8] - b[9]) & 255)) & 255; \ | 102 b[9] = (b[9] - (b[8] = (b[8] - b[9]) & 255)) & 255; \ |
101 b[7] = (b[7] - (b[6] = (b[6] - b[7]) & 255)) & 255; \ | 103 b[7] = (b[7] - (b[6] = (b[6] - b[7]) & 255)) & 255; \ |
102 b[5] = (b[5] - (b[4] = (b[4] - b[5]) & 255)) & 255; \ | 104 b[5] = (b[5] - (b[4] = (b[4] - b[5]) & 255)) & 255; \ |
103 b[3] = (b[3] - (b[2] = (b[2] - b[3]) & 255)) & 255; \ | 105 b[3] = (b[3] - (b[2] = (b[2] - b[3]) & 255)) & 255; \ |
104 b[1] = (b[1] - (b[0] = (b[0] - b[1]) & 255)) & 255; \ | 106 b[1] = (b[1] - (b[0] = (b[0] - b[1]) & 255)) & 255; \ |
107 } while (0) | |
105 | 108 |
106 /* This is the "Armenian" Shuffle. It takes the input from b and stores it in b2 */ | 109 /* This is the "Armenian" Shuffle. It takes the input from b and stores it in b2 */ |
107 #define SHUF(b, b2) \ | 110 #define SHUF(b, b2) do { \ |
108 b2[0] = b[8]; b2[1] = b[11]; b2[2] = b[12]; b2[3] = b[15]; \ | 111 b2[0] = b[8]; b2[1] = b[11]; b2[2] = b[12]; b2[3] = b[15]; \ |
109 b2[4] = b[2]; b2[5] = b[1]; b2[6] = b[6]; b2[7] = b[5]; \ | 112 b2[4] = b[2]; b2[5] = b[1]; b2[6] = b[6]; b2[7] = b[5]; \ |
110 b2[8] = b[10]; b2[9] = b[9]; b2[10] = b[14]; b2[11] = b[13]; \ | 113 b2[8] = b[10]; b2[9] = b[9]; b2[10] = b[14]; b2[11] = b[13]; \ |
111 b2[12] = b[0]; b2[13] = b[7]; b2[14] = b[4]; b2[15] = b[3]; | 114 b2[12] = b[0]; b2[13] = b[7]; b2[14] = b[4]; b2[15] = b[3]; \ |
115 } while (0) | |
112 | 116 |
113 /* This is the inverse shuffle. It takes from b and gives to b2 */ | 117 /* This is the inverse shuffle. It takes from b and gives to b2 */ |
114 #define iSHUF(b, b2) \ | 118 #define iSHUF(b, b2) do { \ |
115 b2[0] = b[12]; b2[1] = b[5]; b2[2] = b[4]; b2[3] = b[15]; \ | 119 b2[0] = b[12]; b2[1] = b[5]; b2[2] = b[4]; b2[3] = b[15]; \ |
116 b2[4] = b[14]; b2[5] = b[7]; b2[6] = b[6]; b2[7] = b[13]; \ | 120 b2[4] = b[14]; b2[5] = b[7]; b2[6] = b[6]; b2[7] = b[13]; \ |
117 b2[8] = b[0]; b2[9] = b[9]; b2[10] = b[8]; b2[11] = b[1]; \ | 121 b2[8] = b[0]; b2[9] = b[9]; b2[10] = b[8]; b2[11] = b[1]; \ |
118 b2[12] = b[2]; b2[13] = b[11]; b2[14] = b[10]; b2[15] = b[3]; | 122 b2[12] = b[2]; b2[13] = b[11]; b2[14] = b[10]; b2[15] = b[3]; \ |
119 | 123 } while (0) |
120 /* The complete forward Linear Transform layer. | 124 |
121 * Note that alternating usage of b and b2. | 125 /* The complete forward Linear Transform layer. |
122 * Each round of LT starts in 'b' and ends in 'b2'. | 126 * Note that alternating usage of b and b2. |
127 * Each round of LT starts in 'b' and ends in 'b2'. | |
123 */ | 128 */ |
124 #define LT(b, b2) \ | 129 #define LT(b, b2) do { \ |
125 PHT(b); SHUF(b, b2); \ | 130 PHT(b); SHUF(b, b2); \ |
126 PHT(b2); SHUF(b2, b); \ | 131 PHT(b2); SHUF(b2, b); \ |
127 PHT(b); SHUF(b, b2); \ | 132 PHT(b); SHUF(b, b2); \ |
128 PHT(b2); | 133 PHT(b2); \ |
134 } while (0) | |
129 | 135 |
130 /* This is the inverse linear transform layer. */ | 136 /* This is the inverse linear transform layer. */ |
131 #define iLT(b, b2) \ | 137 #define iLT(b, b2) do { \ |
132 iPHT(b); \ | 138 iPHT(b); \ |
133 iSHUF(b, b2); iPHT(b2); \ | 139 iSHUF(b, b2); iPHT(b2); \ |
134 iSHUF(b2, b); iPHT(b); \ | 140 iSHUF(b2, b); iPHT(b); \ |
135 iSHUF(b, b2); iPHT(b2); | 141 iSHUF(b, b2); iPHT(b2); \ |
136 | 142 } while (0) |
137 #ifdef LTC_SMALL_CODE | 143 |
138 | 144 #ifdef LTC_SMALL_CODE |
139 static void _round(unsigned char *b, int i, symmetric_key *skey) | 145 |
146 static void _round(unsigned char *b, int i, symmetric_key *skey) | |
140 { | 147 { |
141 ROUND(b, i); | 148 ROUND(b, i); |
142 } | 149 } |
143 | 150 |
144 static void _iround(unsigned char *b, int i, symmetric_key *skey) | 151 static void _iround(unsigned char *b, int i, symmetric_key *skey) |
152 } | 159 } |
153 | 160 |
154 static void _ilt(unsigned char *b, unsigned char *b2) | 161 static void _ilt(unsigned char *b, unsigned char *b2) |
155 { | 162 { |
156 iLT(b, b2); | 163 iLT(b, b2); |
157 } | 164 } |
158 | 165 |
159 #undef ROUND | 166 #undef ROUND |
160 #define ROUND(b, i) _round(b, i, skey) | 167 #define ROUND(b, i) _round(b, i, skey) |
161 | 168 |
162 #undef iROUND | 169 #undef iROUND |
226 if (keylen != 16 && keylen != 24 && keylen != 32) { | 233 if (keylen != 16 && keylen != 24 && keylen != 32) { |
227 return CRYPT_INVALID_KEYSIZE; | 234 return CRYPT_INVALID_KEYSIZE; |
228 } | 235 } |
229 | 236 |
230 /* Is the number of rounds valid? Either use zero for default or | 237 /* Is the number of rounds valid? Either use zero for default or |
231 * 8,12,16 rounds for 16,24,32 byte keys | 238 * 8,12,16 rounds for 16,24,32 byte keys |
232 */ | 239 */ |
233 if (num_rounds != 0 && num_rounds != rounds[(keylen/8)-2]) { | 240 if (num_rounds != 0 && num_rounds != rounds[(keylen/8)-2]) { |
234 return CRYPT_INVALID_ROUNDS; | 241 return CRYPT_INVALID_ROUNDS; |
235 } | 242 } |
236 | 243 |
237 /* 128 bit key version */ | 244 /* 128 bit key version */ |
238 if (keylen == 16) { | 245 if (keylen == 16) { |
239 /* copy key into t */ | 246 /* copy key into t */ |
240 for (x = y = 0; x < 16; x++) { | 247 for (x = y = 0; x < 16; x++) { |
241 t[x] = key[x]; | 248 t[x] = key[x]; |
242 y ^= key[x]; | 249 y ^= key[x]; |
243 } | 250 } |
244 t[16] = y; | 251 t[16] = y; |
245 | 252 |
246 /* make round keys */ | 253 /* make round keys */ |
247 for (x = 0; x < 16; x++) { | 254 for (x = 0; x < 16; x++) { |
263 } | 270 } |
264 } | 271 } |
265 skey->saferp.rounds = 8; | 272 skey->saferp.rounds = 8; |
266 } else if (keylen == 24) { | 273 } else if (keylen == 24) { |
267 /* copy key into t */ | 274 /* copy key into t */ |
268 for (x = y = 0; x < 24; x++) { | 275 for (x = y = 0; x < 24; x++) { |
269 t[x] = key[x]; | 276 t[x] = key[x]; |
270 y ^= key[x]; | 277 y ^= key[x]; |
271 } | 278 } |
272 t[24] = y; | 279 t[24] = y; |
273 | 280 |
274 /* make round keys */ | 281 /* make round keys */ |
275 for (x = 0; x < 16; x++) { | 282 for (x = 0; x < 16; x++) { |
282 t[y] = ((t[y]<<3)|(t[y]>>5)) & 255; | 289 t[y] = ((t[y]<<3)|(t[y]>>5)) & 255; |
283 } | 290 } |
284 | 291 |
285 /* select and add */ | 292 /* select and add */ |
286 z = x; | 293 z = x; |
287 for (y = 0; y < 16; y++) { | 294 for (y = 0; y < 16; y++) { |
288 skey->saferp.K[x][y] = (t[z] + safer_bias[x-1][y]) & 255; | 295 skey->saferp.K[x][y] = (t[z] + safer_bias[x-1][y]) & 255; |
289 if (++z == 25) { z = 0; } | 296 if (++z == 25) { z = 0; } |
290 } | 297 } |
291 } | 298 } |
292 skey->saferp.rounds = 12; | 299 skey->saferp.rounds = 12; |
293 } else { | 300 } else { |
294 /* copy key into t */ | 301 /* copy key into t */ |
295 for (x = y = 0; x < 32; x++) { | 302 for (x = y = 0; x < 32; x++) { |
296 t[x] = key[x]; | 303 t[x] = key[x]; |
297 y ^= key[x]; | 304 y ^= key[x]; |
298 } | 305 } |
299 t[32] = y; | 306 t[32] = y; |
300 | 307 |
301 /* make round keys */ | 308 /* make round keys */ |
302 for (x = 0; x < 16; x++) { | 309 for (x = 0; x < 16; x++) { |
303 skey->saferp.K[0][x] = t[x]; | 310 skey->saferp.K[0][x] = t[x]; |
304 } | 311 } |
305 | 312 |
306 for (x = 1; x < 33; x++) { | 313 for (x = 1; x < 33; x++) { |
307 /* rotate 3 bits each */ | 314 /* rotate 3 bits each */ |
308 for (y = 0; y < 33; y++) { | 315 for (y = 0; y < 33; y++) { |
309 t[y] = ((t[y]<<3)|(t[y]>>5)) & 255; | 316 t[y] = ((t[y]<<3)|(t[y]>>5)) & 255; |
310 } | 317 } |
311 | 318 |
312 /* select and add */ | 319 /* select and add */ |
313 z = x; | 320 z = x; |
314 for (y = 0; y < 16; y++) { | 321 for (y = 0; y < 16; y++) { |
315 skey->saferp.K[x][y] = (t[z] + safer_bias[x-1][y]) & 255; | 322 skey->saferp.K[x][y] = (t[z] + safer_bias[x-1][y]) & 255; |
316 if (++z == 33) { z = 0; } | 323 if (++z == 33) { z = 0; } |
390 | 397 |
391 /** | 398 /** |
392 Decrypts a block of text with LTC_SAFER+ | 399 Decrypts a block of text with LTC_SAFER+ |
393 @param ct The input ciphertext (16 bytes) | 400 @param ct The input ciphertext (16 bytes) |
394 @param pt The output plaintext (16 bytes) | 401 @param pt The output plaintext (16 bytes) |
395 @param skey The key as scheduled | 402 @param skey The key as scheduled |
396 @return CRYPT_OK if successful | 403 @return CRYPT_OK if successful |
397 */ | 404 */ |
398 int saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | 405 int saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) |
399 { | 406 { |
400 unsigned char b[16]; | 407 unsigned char b[16]; |
458 */ | 465 */ |
459 int saferp_test(void) | 466 int saferp_test(void) |
460 { | 467 { |
461 #ifndef LTC_TEST | 468 #ifndef LTC_TEST |
462 return CRYPT_NOP; | 469 return CRYPT_NOP; |
463 #else | 470 #else |
464 static const struct { | 471 static const struct { |
465 int keylen; | 472 int keylen; |
466 unsigned char key[32], pt[16], ct[16]; | 473 unsigned char key[32], pt[16], ct[16]; |
467 } tests[] = { | 474 } tests[] = { |
468 { | 475 { |
469 16, | 476 16, |
470 { 41, 35, 190, 132, 225, 108, 214, 174, | 477 { 41, 35, 190, 132, 225, 108, 214, 174, |
471 82, 144, 73, 241, 241, 187, 233, 235 }, | 478 82, 144, 73, 241, 241, 187, 233, 235 }, |
472 { 179, 166, 219, 60, 135, 12, 62, 153, | 479 { 179, 166, 219, 60, 135, 12, 62, 153, |
473 36, 94, 13, 28, 6, 183, 71, 222 }, | 480 36, 94, 13, 28, 6, 183, 71, 222 }, |
474 { 224, 31, 182, 10, 12, 255, 84, 70, | 481 { 224, 31, 182, 10, 12, 255, 84, 70, |
475 127, 13, 89, 249, 9, 57, 165, 220 } | 482 127, 13, 89, 249, 9, 57, 165, 220 } |
476 }, { | 483 }, { |
477 24, | 484 24, |
478 { 72, 211, 143, 117, 230, 217, 29, 42, | 485 { 72, 211, 143, 117, 230, 217, 29, 42, |
479 229, 192, 247, 43, 120, 129, 135, 68, | 486 229, 192, 247, 43, 120, 129, 135, 68, |
480 14, 95, 80, 0, 212, 97, 141, 190 }, | 487 14, 95, 80, 0, 212, 97, 141, 190 }, |
481 { 123, 5, 21, 7, 59, 51, 130, 31, | 488 { 123, 5, 21, 7, 59, 51, 130, 31, |
482 24, 112, 146, 218, 100, 84, 206, 177 }, | 489 24, 112, 146, 218, 100, 84, 206, 177 }, |
483 { 92, 136, 4, 63, 57, 95, 100, 0, | 490 { 92, 136, 4, 63, 57, 95, 100, 0, |
484 150, 130, 130, 16, 193, 111, 219, 133 } | 491 150, 130, 130, 16, 193, 111, 219, 133 } |
485 }, { | 492 }, { |
486 32, | 493 32, |
487 { 243, 168, 141, 254, 190, 242, 235, 113, | 494 { 243, 168, 141, 254, 190, 242, 235, 113, |
488 255, 160, 208, 59, 117, 6, 140, 126, | 495 255, 160, 208, 59, 117, 6, 140, 126, |
489 135, 120, 115, 77, 208, 190, 130, 190, | 496 135, 120, 115, 77, 208, 190, 130, 190, |
490 219, 194, 70, 65, 43, 140, 250, 48 }, | 497 219, 194, 70, 65, 43, 140, 250, 48 }, |
491 { 127, 112, 240, 167, 84, 134, 50, 149, | 498 { 127, 112, 240, 167, 84, 134, 50, 149, |
492 170, 91, 104, 19, 11, 230, 252, 245 }, | 499 170, 91, 104, 19, 11, 230, 252, 245 }, |
493 { 88, 11, 25, 36, 172, 229, 202, 213, | 500 { 88, 11, 25, 36, 172, 229, 202, 213, |
494 170, 65, 105, 153, 220, 104, 153, 138 } | 501 170, 65, 105, 153, 220, 104, 153, 138 } |
495 } | 502 } |
496 }; | 503 }; |
497 | 504 |
498 unsigned char tmp[2][16]; | 505 unsigned char tmp[2][16]; |
499 symmetric_key skey; | 506 symmetric_key skey; |
500 int err, i, y; | 507 int err, i, y; |
501 | 508 |
505 } | 512 } |
506 saferp_ecb_encrypt(tests[i].pt, tmp[0], &skey); | 513 saferp_ecb_encrypt(tests[i].pt, tmp[0], &skey); |
507 saferp_ecb_decrypt(tmp[0], tmp[1], &skey); | 514 saferp_ecb_decrypt(tmp[0], tmp[1], &skey); |
508 | 515 |
509 /* compare */ | 516 /* compare */ |
510 if (XMEMCMP(tmp[0], tests[i].ct, 16) || XMEMCMP(tmp[1], tests[i].pt, 16)) { | 517 if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "Safer+ Encrypt", i) || |
518 compare_testvector(tmp[1], 16, tests[i].pt, 16, "Safer+ Decrypt", i)) { | |
511 return CRYPT_FAIL_TESTVECTOR; | 519 return CRYPT_FAIL_TESTVECTOR; |
512 } | 520 } |
513 | 521 |
514 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | 522 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ |
515 for (y = 0; y < 16; y++) tmp[0][y] = 0; | 523 for (y = 0; y < 16; y++) tmp[0][y] = 0; |
520 | 528 |
521 return CRYPT_OK; | 529 return CRYPT_OK; |
522 #endif | 530 #endif |
523 } | 531 } |
524 | 532 |
525 /** Terminate the context | 533 /** Terminate the context |
526 @param skey The scheduled key | 534 @param skey The scheduled key |
527 */ | 535 */ |
528 void saferp_done(symmetric_key *skey) | 536 void saferp_done(symmetric_key *skey) |
529 { | 537 { |
538 LTC_UNUSED_PARAM(skey); | |
530 } | 539 } |
531 | 540 |
532 /** | 541 /** |
533 Gets suitable key size | 542 Gets suitable key size |
534 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. | 543 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. |
535 @return CRYPT_OK if the input key size is acceptable. | 544 @return CRYPT_OK if the input key size is acceptable. |
536 */ | 545 */ |
537 int saferp_keysize(int *keysize) | 546 int saferp_keysize(int *keysize) |
538 { | 547 { |
539 LTC_ARGCHK(keysize != NULL); | 548 LTC_ARGCHK(keysize != NULL); |
540 | 549 |
541 if (*keysize < 16) | 550 if (*keysize < 16) |
542 return CRYPT_INVALID_KEYSIZE; | 551 return CRYPT_INVALID_KEYSIZE; |
543 if (*keysize < 24) { | 552 if (*keysize < 24) { |
544 *keysize = 16; | 553 *keysize = 16; |
545 } else if (*keysize < 32) { | 554 } else if (*keysize < 32) { |
552 | 561 |
553 #endif | 562 #endif |
554 | 563 |
555 | 564 |
556 | 565 |
557 /* $Source$ */ | 566 /* ref: $Format:%D$ */ |
558 /* $Revision$ */ | 567 /* git commit: $Format:%H$ */ |
559 /* $Date$ */ | 568 /* commit time: $Format:%ai$ */ |