2
* The contents of this file are subject to the Mozilla Public
3
* License Version 1.1 (the "License"); you may not use this file
4
* except in compliance with the License. You may obtain a copy of
5
* the License at http://www.mozilla.org/MPL/
7
* Software distributed under the License is distributed on an "AS
8
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
* implied. See the License for the specific language governing
10
* rights and limitations under the License.
12
* The Original Code is the Netscape security libraries.
14
* The Initial Developer of the Original Code is Netscape
15
* Communications Corporation. Portions created by Netscape are
16
* Copyright (C) 1994-2000 Netscape Communications Corporation. All
21
* Alternatively, the contents of this file may be used under the
22
* terms of the GNU General Public License Version 2 or later (the
23
* "GPL"), in which case the provisions of the GPL are applicable
24
* instead of those above. If you wish to allow use of your
25
* version of this file only under the terms of the GPL and not to
26
* allow others to use your version of this file under the MPL,
27
* indicate your decision by deleting the provisions above and
28
* replace them with the notice and other provisions required by
29
* the GPL. If you do not delete the provisions above, a recipient
30
* may use your version of this file under either the MPL or the
34
* Software implementation of FORTEZZA skipjack primatives
41
* Xor the IV into the plaintext buffer either just before encryption, or
42
* just after decryption.
45
fort_XorIV(unsigned char *obuffer, unsigned char *buffer, unsigned char *iv) {
48
if ((buffer & 0x3) == 0) && ((iv & 0x3) == 0)) {
49
int32 *ibuffer = (int32 *)buffer;
50
int32 *iobuffer = (int32 *)obuffer;
51
int32 *iiv = (int32 *)iv;
53
iobuffer[0] = ibuffer[0] ^ iiv[0];
54
iobuffer[1] = ibuffer[1] ^ iiv[1];
59
for (i=0; i < SKIPJACK_BLOCK_SIZE; i++) {
60
obuffer[i] = buffer[i] ^ iv[i];
65
/* the F-table for Skipjack */
66
unsigned char F[256] = {
67
0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4,
68
0xb3, 0x21, 0x15, 0x78, 0x99, 0xb1, 0xaf, 0xf9,
69
0xe7, 0x2d, 0x4d, 0x8a, 0xce, 0x4c, 0xca, 0x2e,
70
0x52, 0x95, 0xd9, 0x1e, 0x4e, 0x38, 0x44, 0x28,
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0x0c, 0xef, 0xbc, 0x72, 0x75, 0x6f, 0x37, 0xa1,
94
0xec, 0xd3, 0x8e, 0x62, 0x8b, 0x86, 0x10, 0xe8,
95
0x08, 0x77, 0x11, 0xbe, 0x92, 0x4f, 0x24, 0xc5,
96
0x32, 0x36, 0x9d, 0xcf, 0xf3, 0xa6, 0xbb, 0xac,
97
0x5e, 0x6c, 0xa9, 0x13, 0x57, 0x25, 0xb5, 0xe3,
98
0xbd, 0xa8, 0x3a, 0x01, 0x05, 0x59, 0x2a, 0x46
101
typedef unsigned char fort_keysched[32*4];
103
/* do the key schedule work once for efficency */
105
fort_skipKeySchedule(FORTSkipjackKeyPtr key,fort_keysched keysched)
107
unsigned char *keyptr = key;
108
unsigned char *first = keyptr +sizeof(FORTSkipjackKey)-1;
113
for (i=0; i < (32*4); i++) {
114
keysched[i] = *keyptr--;
115
if (keyptr < key) keyptr = first;
121
fort_clearShedule(fort_keysched keysched)
123
PORT_Memset(keysched, 0, sizeof(keysched));
128
G(fort_keysched cv, int k, unsigned int wordIn)
130
unsigned char g1, g2, g3, g4, g5, g6;
132
g1 = (unsigned char) (wordIn >> 8) & 0xff;
133
g2 = (unsigned char) wordIn & 0xff;
135
g3 = F[g2^cv[4*k]]^g1;
136
g4 = F[g3^cv[4*k+1]]^g2;
137
g5 = F[g4^cv[4*k+2]]^g3;
138
g6 = F[g5^cv[4*k+3]]^g4;
144
G1(fort_keysched cv, int k, unsigned int wordIn)
146
unsigned char g1, g2, g3, g4, g5, g6;
148
g5 = (unsigned char) (wordIn >> 8) & 0xff;
149
g6 = (unsigned char) wordIn & 0xff;
151
g4 = F[g5^cv[4*k+3]]^g6;
152
g3 = F[g4^cv[4*k+2]]^g5;
153
g2 = F[g3^cv[4*k+1]]^g4;
154
g1 = F[g2^cv[4*k]]^g3;
160
ruleA(fort_keysched cv,int round,unsigned int *w)
173
w[1] = G(cv,k,w[1]) ^ w4 ^ counter;
179
ruleB(fort_keysched cv,int round,unsigned int *w)
185
int k = round*16+i+8;
190
w[3] = w[1] ^ w[2] ^ counter;
198
ruleA1(fort_keysched cv,int round,unsigned int *w)
203
for(i=7; i>=0; i--) {
208
w[4] = w[1] ^ w[2] ^ counter;
209
w[1] = G1(cv,k,w[2]);
217
ruleB1(fort_keysched cv,int round,unsigned int *w)
222
for(i=7; i>=0; i--) {
223
int k = round*16+i+8;
228
w[1] = G1(cv,k,w[2]);
229
w[2] = G1(cv,k,w[2]) ^ w[3] ^ counter;
237
fort_doskipD(fort_keysched cv,unsigned char *cipherIn,
238
unsigned char *plainOut) {
239
unsigned int w[5]; /* ignore w[0] so the code matches the doc */
241
/* initial byte swap */
242
w[1]=(cipherIn[7]<<8)+cipherIn[6];
243
w[2]=(cipherIn[5]<<8)+cipherIn[4];
244
w[3]=(cipherIn[3]<<8)+cipherIn[2];
245
w[4]=(cipherIn[1]<<8)+cipherIn[0];
252
/* final byte swap */
253
plainOut[0] = w[4] & 0xff;
254
plainOut[1] = (w[4] >> 8) & 0xff;
255
plainOut[2] = w[3] & 0xff;
256
plainOut[3] = (w[3] >> 8) & 0xff;
257
plainOut[4] = w[2] & 0xff;
258
plainOut[5] = (w[2] >> 8) & 0xff;
259
plainOut[6] = w[1] & 0xff;
260
plainOut[7] = (w[1] >> 8) & 0xff;
265
fort_doskipE(fort_keysched cv,unsigned char *cipherIn,
266
unsigned char *plainOut) {
267
unsigned int w[5]; /* ignore w[0] so the code matches the doc */
269
/* initial byte swap */
270
w[1]=(cipherIn[7]<<8)+cipherIn[6];
271
w[2]=(cipherIn[5]<<8)+cipherIn[4];
272
w[3]=(cipherIn[3]<<8)+cipherIn[2];
273
w[4]=(cipherIn[1]<<8)+cipherIn[0];
280
/* final byte swap */
281
plainOut[0] = w[4] & 0xff;
282
plainOut[1] = (w[4] >> 8) & 0xff;
283
plainOut[2] = w[3] & 0xff;
284
plainOut[3] = (w[3] >> 8) & 0xff;
285
plainOut[4] = w[2] & 0xff;
286
plainOut[5] = (w[2] >> 8) & 0xff;
287
plainOut[6] = w[1] & 0xff;
288
plainOut[7] = (w[1] >> 8) & 0xff;
292
/* Checksums are calculated by encrypted a fixed string with the key, then
293
* taking 16 bytes of the result from the block */
295
fort_CalcKeyChecksum(FORTSkipjackKeyPtr key, unsigned char *sum) {
296
unsigned char ckdata[8] = {
297
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
298
unsigned char ckres[8];
299
fort_keysched keysched;
302
fort_skipKeySchedule(key,keysched);
304
fort_doskipE(keysched,ckdata,ckres);
305
fort_clearShedule(keysched);
306
PORT_Memcpy(sum,&ckres[1],2);
310
/* These function actually implements skipjack CBC Decrypt */
312
fort_skipjackDecrypt(FORTSkipjackKeyPtr key, unsigned char *iv,
313
unsigned long size, unsigned char *cipherIn,
314
unsigned char *plainOut) {
315
unsigned char ivdata1[SKIPJACK_BLOCK_SIZE];
316
unsigned char ivdata2[SKIPJACK_BLOCK_SIZE];
317
unsigned char *lastiv, *nextiv, *tmpiv;
318
fort_keysched keysched;
320
/* do the key schedule work once for efficency */
321
fort_skipKeySchedule(key,keysched);
323
/* As we decrypt, we need to save the last block so that we can
324
* Xor it out of decrypted text to get the real plain text. We actually
325
* have to save it because cipherIn and plainOut may point to the same
329
PORT_Memcpy(lastiv,iv,SKIPJACK_BLOCK_SIZE);
330
while (size >= SKIPJACK_BLOCK_SIZE) {
331
/* save the IV for the next block */
332
PORT_Memcpy(nextiv,cipherIn,SKIPJACK_BLOCK_SIZE);
333
fort_doskipD(keysched,cipherIn,plainOut);
334
/* xor out the last IV */
335
fort_XorIV(plainOut,plainOut,lastiv);
337
/* swap the IV buffers */
342
/* increment the loop pointers... be sure to get the input, output,
343
* and size (decrement) each fortdoskipD operates on an entire block*/
344
cipherIn += SKIPJACK_BLOCK_SIZE;
345
plainOut += SKIPJACK_BLOCK_SIZE;
346
size -= SKIPJACK_BLOCK_SIZE;
348
fort_clearShedule(keysched); /* don't leave the key lying around the stack*/
349
if (size != 0) return CI_INV_SIZE;
353
/* These function actually implements skipjack CBC Encrypt */
355
fort_skipjackEncrypt(FORTSkipjackKeyPtr key, unsigned char *iv,
356
unsigned long size, unsigned char *plainIn,
357
unsigned char *cipherOut) {
358
unsigned char *tmpiv;
359
fort_keysched keysched;
360
unsigned char plain[SKIPJACK_BLOCK_SIZE];
362
fort_skipKeySchedule(key,keysched);
364
while (size >= SKIPJACK_BLOCK_SIZE) {
365
/* We Xor into a temp buffer because we don't want to modify plainIn,
366
* doing so may make the caller very unhappy:). */
367
fort_XorIV(plain,plainIn,tmpiv);
368
fort_doskipE(keysched,plain,cipherOut);
370
cipherOut += SKIPJACK_BLOCK_SIZE;
371
plainIn += SKIPJACK_BLOCK_SIZE;
372
size -= SKIPJACK_BLOCK_SIZE;
374
fort_clearShedule(keysched); /* don't leave the key lying around the stack*/
375
if (size != 0) return CI_INV_SIZE;
382
* unwrap is used for key generation and mixing
385
fort_skipjackUnwrap(FORTSkipjackKeyPtr key,unsigned long len,
386
unsigned char *cipherIn, unsigned char *plainOut) {
387
unsigned char low[10];
388
fort_keysched keysched;
391
/* unwrap can only unwrap 80 bit symetric keys and 160 private keys
392
* sometimes these values have checksums. When they do, we should verify
393
* those checksums. */
395
case 20: /* private key */
396
case 24: /* private key with checksum */
397
ret = fort_skipjackUnwrap(key,len/2,cipherIn,plainOut);
398
if (ret != CI_OK) return ret;
399
ret = fort_skipjackUnwrap(key,len/2,&cipherIn[len/2],low);
401
/* unmunge the low word */
402
for (i=0; i < 10; i++) {
403
low[i] = low[i] ^ plainOut[i];
406
/* the unwrap will fail above because the checkword is on
407
* low, not low ^ high.
409
if (ret == CI_CHECKWORD_FAIL) {
410
unsigned char checksum[2];
412
ret = fort_CalcKeyChecksum(low,checksum);
413
if (ret != CI_OK) return ret;
414
if (PORT_Memcmp(checksum,&cipherIn[len-2],2) != 0) {
415
return CI_CHECKWORD_FAIL;
418
if (ret != CI_OK) return ret;
420
/* re-order the low word */
421
PORT_Memcpy(&plainOut[10],&low[8],2);
422
PORT_Memcpy(&plainOut[12],&low[0],8);
424
case 10: /* 80 bit skipjack key */
425
case 12: /* 80 bit skipjack key with checksum */
426
fort_skipKeySchedule(key,keysched);
427
fort_doskipD(keysched,cipherIn,plainOut);
428
plainOut[8] = cipherIn[8] ^ plainOut[0];
429
plainOut[9] = cipherIn[9] ^ plainOut[1];
430
fort_doskipD(keysched,plainOut,plainOut);
431
fort_clearShedule(keysched);
432
/* if we have a checkum, verify it */
434
unsigned char checksum[2];
436
ret = fort_CalcKeyChecksum(plainOut,checksum);
437
if (ret != CI_OK) return ret;
438
if (PORT_Memcmp(checksum,&cipherIn[10],2) != 0) {
439
return CI_CHECKWORD_FAIL;
450
* unwrap is used for key generation and mixing
453
fort_skipjackWrap(FORTSkipjackKeyPtr key,unsigned long len,
454
unsigned char *plainIn, unsigned char *cipherOut) {
455
unsigned char low[10];
456
unsigned char checksum[2];
457
fort_keysched keysched;
461
/* NOTE: length refers to the target in the case of wrap */
462
/* Wrap can only Wrap 80 bit symetric keys and 160 private keys
463
* sometimes these values have checksums. When they do, we should verify
464
* those checksums. */
466
case 20: /* private key */
467
case 24: /* private key with checksum */
468
/* re-order the low word */
469
PORT_Memcpy(&low[8],&plainIn[10],2);
470
PORT_Memcpy(&low[0],&plainIn[12],8);
472
ret = fort_CalcKeyChecksum(low,checksum);
473
if (ret != CI_OK) return ret;
475
/* munge the low word */
476
for (i=0; i < 10; i++) {
477
low[i] = low[i] ^ plainIn[i];
479
ret = fort_skipjackWrap(key,len/2,plainIn,cipherOut);
480
ret = fort_skipjackWrap(key,len/2,low,&cipherOut[len/2]);
482
PORT_Memcpy(&cipherOut[len - 2], checksum, sizeof(checksum));
486
case 10: /* 80 bit skipjack key */
487
case 12: /* 80 bit skipjack key with checksum */
489
fort_skipKeySchedule(key,keysched);
490
fort_doskipE(keysched,plainIn,cipherOut);
491
cipherOut[8] = plainIn[8] ^ cipherOut[0];
492
cipherOut[9] = plainIn[9] ^ cipherOut[1];
493
fort_doskipE(keysched,cipherOut,cipherOut);
494
fort_clearShedule(keysched);
495
/* if we need a checkum, get it */
497
ret = fort_CalcKeyChecksum(plainIn,&cipherOut[10]);
498
if (ret != CI_OK) return ret;