comparison rc2.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 7faae8f46238
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
31 &rc2_ecb_decrypt, 31 &rc2_ecb_decrypt,
32 &rc2_test, 32 &rc2_test,
33 &rc2_keysize 33 &rc2_keysize
34 }; 34 };
35 35
36 36 /* 256-entry permutation table, probably derived somehow from pi */
37 /**********************************************************************\ 37 static const unsigned char permute[256] = {
38 * Expand a variable-length user key (between 1 and 128 bytes) to a *
39 * 64-short working rc2 key, of at most "bits" effective key bits. *
40 * The effective key bits parameter looks like an export control hack. *
41 * For normal use, it should always be set to 1024. For convenience, *
42 * zero is accepted as an alias for 1024. *
43 \**********************************************************************/
44
45 /* 256-entry permutation table, probably derived somehow from pi */
46 static const unsigned char permute[256] = {
47 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157, 38 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
48 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162, 39 198,126, 55,131, 43,118, 83,142, 98, 76,100,136, 68,139,251,162,
49 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50, 40 23,154, 89,245,135,179, 79, 19, 97, 69,109,141, 9,129,125, 50,
50 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130, 41 189,143, 64,235,134,183,123, 11,240,149, 33, 34, 92,107, 78,130,
51 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220, 42 84,214,101,147,206, 96,178, 28,115, 86,192, 20,167,140,241,220,
58 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49, 49 153,124, 58,133, 35,184,180,122,252, 2, 54, 91, 37, 85,151, 49,
59 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201, 50 45, 93,250,152,227,138,146,174, 5,223, 41, 16,103,108,186,201,
60 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169, 51 211, 0,230,207,225,158,168, 44, 99, 22, 1, 63, 88,226,137,169,
61 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46, 52 13, 56, 52, 27,171, 51,255,176,187, 72, 12, 95,185,177,205, 46,
62 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173 53 197,243,219, 71,229,165,156,119, 10,166, 32,104,254,127,193,173
63 }; 54 };
64 55
65 int rc2_setup(const unsigned char *key, int keylen, int rounds, symmetric_key *skey) 56 int rc2_setup(const unsigned char *key, int keylen, int rounds, symmetric_key *skey)
66 { 57 {
67 unsigned *xkey = skey->rc2.xkey; 58 unsigned *xkey = skey->rc2.xkey;
68 unsigned char tmp[128]; 59 unsigned char tmp[128];
85 } 76 }
86 77
87 /* Phase 1: Expand input key to 128 bytes */ 78 /* Phase 1: Expand input key to 128 bytes */
88 if (keylen < 128) { 79 if (keylen < 128) {
89 for (i = keylen; i < 128; i++) { 80 for (i = keylen; i < 128; i++) {
90 tmp[i] = permute[(int)((tmp[i - 1] + tmp[i - keylen]) & 255)]; 81 tmp[i] = permute[(tmp[i - 1] + tmp[i - keylen]) & 255];
91 } 82 }
92 } 83 }
93 84
94 /* Phase 2 - reduce effective key size to "bits" */ 85 /* Phase 2 - reduce effective key size to "bits" */
95 bits = keylen*8; 86 bits = keylen<<3;
96 T8 = (unsigned)(bits+7)>>3; 87 T8 = (unsigned)(bits+7)>>3;
97 TM = (255 >> (unsigned)(7 & -bits)); 88 TM = (255 >> (unsigned)(7 & -bits));
98 tmp[128 - T8] = permute[(int)(tmp[128 - T8] & TM)]; 89 tmp[128 - T8] = permute[tmp[128 - T8] & TM];
99 for (i = 127 - T8; i >= 0; i--) { 90 for (i = 127 - T8; i >= 0; i--) {
100 tmp[i] = permute[(int)(tmp[i + 1] ^ tmp[i + T8])]; 91 tmp[i] = permute[tmp[i + 1] ^ tmp[i + T8]];
101 } 92 }
102 93
103 /* Phase 3 - copy to xkey in little-endian order */ 94 /* Phase 3 - copy to xkey in little-endian order */
104 i = 63; 95 for (i = 0; i < 64; i++) {
105 do {
106 xkey[i] = (unsigned)tmp[2*i] + ((unsigned)tmp[2*i+1] << 8); 96 xkey[i] = (unsigned)tmp[2*i] + ((unsigned)tmp[2*i+1] << 8);
107 } while (i-- > 0); 97 }
108 98
109 #ifdef CLEAN_STACK 99 #ifdef CLEAN_STACK
110 zeromem(tmp, sizeof(tmp)); 100 zeromem(tmp, sizeof(tmp));
111 #endif 101 #endif
112 102
127 #endif 117 #endif
128 { 118 {
129 unsigned *xkey; 119 unsigned *xkey;
130 unsigned x76, x54, x32, x10, i; 120 unsigned x76, x54, x32, x10, i;
131 121
132 _ARGCHK(plain != NULL); 122 _ARGCHK(plain != NULL);
133 _ARGCHK(cipher != NULL); 123 _ARGCHK(cipher != NULL);
134 _ARGCHK(skey != NULL); 124 _ARGCHK(skey != NULL);
135 125
136 xkey = skey->rc2.xkey; 126 xkey = skey->rc2.xkey;
137 127
138 x76 = ((unsigned)plain[7] << 8) + (unsigned)plain[6]; 128 x76 = ((unsigned)plain[7] << 8) + (unsigned)plain[6];
139 x54 = ((unsigned)plain[5] << 8) + (unsigned)plain[4]; 129 x54 = ((unsigned)plain[5] << 8) + (unsigned)plain[4];
140 x32 = ((unsigned)plain[3] << 8) + (unsigned)plain[2]; 130 x32 = ((unsigned)plain[3] << 8) + (unsigned)plain[2];
141 x10 = ((unsigned)plain[1] << 8) + (unsigned)plain[0]; 131 x10 = ((unsigned)plain[1] << 8) + (unsigned)plain[0];
142 132
143 for (i = 0; i < 16; i++) { 133 for (i = 0; i < 16; i++) {
144 x10 = (x10 + (x32 & ~x76) + (x54 & x76) + xkey[4*i+0]) & 0xFFFF; 134 x10 = (x10 + (x32 & ~x76) + (x54 & x76) + xkey[4*i+0]) & 0xFFFF;
145 x10 = ((x10 << 1) | (x10 >> 15)) & 0xFFFF; 135 x10 = ((x10 << 1) | (x10 >> 15));
146 136
147 x32 = (x32 + (x54 & ~x10) + (x76 & x10) + xkey[4*i+1]) & 0xFFFF; 137 x32 = (x32 + (x54 & ~x10) + (x76 & x10) + xkey[4*i+1]) & 0xFFFF;
148 x32 = ((x32 << 2) | (x32 >> 14)) & 0xFFFF; 138 x32 = ((x32 << 2) | (x32 >> 14));
149 139
150 x54 = (x54 + (x76 & ~x32) + (x10 & x32) + xkey[4*i+2]) & 0xFFFF; 140 x54 = (x54 + (x76 & ~x32) + (x10 & x32) + xkey[4*i+2]) & 0xFFFF;
151 x54 = ((x54 << 3) | (x54 >> 13)) & 0xFFFF; 141 x54 = ((x54 << 3) | (x54 >> 13));
152 142
153 x76 = (x76 + (x10 & ~x54) + (x32 & x54) + xkey[4*i+3]) & 0xFFFF; 143 x76 = (x76 + (x10 & ~x54) + (x32 & x54) + xkey[4*i+3]) & 0xFFFF;
154 x76 = ((x76 << 5) | (x76 >> 11)) & 0xFFFF; 144 x76 = ((x76 << 5) | (x76 >> 11));
155 145
156 if (i == 4 || i == 10) { 146 if (i == 4 || i == 10) {
157 x10 = (x10 + xkey[x76 & 63]) & 0xFFFF; 147 x10 = (x10 + xkey[x76 & 63]) & 0xFFFF;
158 x32 = (x32 + xkey[x10 & 63]) & 0xFFFF; 148 x32 = (x32 + xkey[x10 & 63]) & 0xFFFF;
159 x54 = (x54 + xkey[x32 & 63]) & 0xFFFF; 149 x54 = (x54 + xkey[x32 & 63]) & 0xFFFF;
197 { 187 {
198 unsigned x76, x54, x32, x10; 188 unsigned x76, x54, x32, x10;
199 unsigned *xkey; 189 unsigned *xkey;
200 int i; 190 int i;
201 191
202 _ARGCHK(plain != NULL); 192 _ARGCHK(plain != NULL);
203 _ARGCHK(cipher != NULL); 193 _ARGCHK(cipher != NULL);
204 _ARGCHK(skey != NULL); 194 _ARGCHK(skey != NULL);
205 195
206 xkey = skey->rc2.xkey; 196 xkey = skey->rc2.xkey;
207 197
208 x76 = ((unsigned)cipher[7] << 8) + (unsigned)cipher[6]; 198 x76 = ((unsigned)cipher[7] << 8) + (unsigned)cipher[6];
209 x54 = ((unsigned)cipher[5] << 8) + (unsigned)cipher[4]; 199 x54 = ((unsigned)cipher[5] << 8) + (unsigned)cipher[4];
216 x54 = (x54 - xkey[x32 & 63]) & 0xFFFF; 206 x54 = (x54 - xkey[x32 & 63]) & 0xFFFF;
217 x32 = (x32 - xkey[x10 & 63]) & 0xFFFF; 207 x32 = (x32 - xkey[x10 & 63]) & 0xFFFF;
218 x10 = (x10 - xkey[x76 & 63]) & 0xFFFF; 208 x10 = (x10 - xkey[x76 & 63]) & 0xFFFF;
219 } 209 }
220 210
221 x76 = ((x76 << 11) | (x76 >> 5)) & 0xFFFF; 211 x76 = ((x76 << 11) | (x76 >> 5));
222 x76 = (x76 - ((x10 & ~x54) + (x32 & x54) + xkey[4*i+3])) & 0xFFFF; 212 x76 = (x76 - ((x10 & ~x54) + (x32 & x54) + xkey[4*i+3])) & 0xFFFF;
223 213
224 x54 = ((x54 << 13) | (x54 >> 3)) & 0xFFFF; 214 x54 = ((x54 << 13) | (x54 >> 3));
225 x54 = (x54 - ((x76 & ~x32) + (x10 & x32) + xkey[4*i+2])) & 0xFFFF; 215 x54 = (x54 - ((x76 & ~x32) + (x10 & x32) + xkey[4*i+2])) & 0xFFFF;
226 216
227 x32 = ((x32 << 14) | (x32 >> 2)) & 0xFFFF; 217 x32 = ((x32 << 14) | (x32 >> 2));
228 x32 = (x32 - ((x54 & ~x10) + (x76 & x10) + xkey[4*i+1])) & 0xFFFF; 218 x32 = (x32 - ((x54 & ~x10) + (x76 & x10) + xkey[4*i+1])) & 0xFFFF;
229 219
230 x10 = ((x10 << 15) | (x10 >> 1)) & 0xFFFF; 220 x10 = ((x10 << 15) | (x10 >> 1));
231 x10 = (x10 - ((x32 & ~x76) + (x54 & x76) + xkey[4*i+0])) & 0xFFFF; 221 x10 = (x10 - ((x32 & ~x76) + (x54 & x76) + xkey[4*i+0])) & 0xFFFF;
232 } 222 }
233 223
234 plain[0] = (unsigned char)x10; 224 plain[0] = (unsigned char)x10;
235 plain[1] = (unsigned char)(x10 >> 8); 225 plain[1] = (unsigned char)(x10 >> 8);