関数 | |
static void | clearmem (char *start, int cnt) |
static void | ufc_init_des (void) |
static void | shuffle_sb (long32 *k, ufc_long saltbits) |
static void | shuffle_sb (long64 *k, ufc_long saltbits) |
static void | setup_salt (const char *s1) |
static void | ufc_mk_keytab (char *key) |
ufc_long * | _ufc_dofinalperm (ufc_long l1, ufc_long l2, ufc_long r1, ufc_long r2) |
static char * | output_conversion (ufc_long v1, ufc_long v2, const char *salt) |
static ufc_long * | _ufc_doit (ufc_long, ufc_long, ufc_long, ufc_long, ufc_long) |
char * | ufc_crypt (const char *key, const char *salt) |
int | ufc_dummy_procedure (void) |
変数 | |
static int | pc1 [56] |
static int | rots [16] |
static int | pc2 [48] |
static int | esel [48] |
static int | e_inverse [64] |
static int | perm32 [32] |
static int | sbox [8][4][16] |
static int | final_perm [64] |
long32 | _ufc_keytab [16][2] |
long32 | _ufc_sb0 [8192] |
long32 | _ufc_sb1 [8192] |
long32 | _ufc_sb2 [8192] |
long32 | _ufc_sb3 [8192] |
static long32 * | sb [4] = {_ufc_sb0, _ufc_sb1, _ufc_sb2, _ufc_sb3} |
static long64 * | sb [4] = {_ufc_sb0, _ufc_sb1, _ufc_sb2, _ufc_sb3} |
static ufc_long | eperm32tab [4][256][2] |
static ufc_long | do_pc1 [8][2][128] |
static ufc_long | do_pc2 [8][128] |
static ufc_long | efp [16][64][2] |
static unsigned char | bytemask [8] |
static ufc_long | longmask [32] |
static int | initialized = 0 |
static unsigned char | current_salt [3] = "&&" |
static ufc_long | current_saltbits = 0 |
static int | direction = 0 |
long32 | _ufc_keytab [16][2] |
static void clearmem | ( | char * | start, | |
int | cnt | |||
) | [static] |
static void ufc_init_des | ( | void | ) | [static] |
参照先 bytemask・clearmem()・do_pc1・do_pc2・e_inverse・efp・eperm32tab・esel・final_perm・initialized・longmask・pc1・pc2・perm32・sb.
参照元 setup_salt().
00302 { int comes_from_bit; 00303 int bit, sg; 00304 ufc_long j; 00305 ufc_long mask1, mask2; 00306 00307 /* 00308 * Create the do_pc1 table used 00309 * to affect pc1 permutation 00310 * when generating keys 00311 */ 00312 for(bit = 0; bit < 56; bit++) { 00313 comes_from_bit = pc1[bit] - 1; 00314 mask1 = bytemask[comes_from_bit % 8 + 1]; 00315 mask2 = longmask[bit % 28 + 4]; 00316 for(j = 0; j < 128; j++) { 00317 if(j & mask1) 00318 do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2; 00319 } 00320 } 00321 00322 /* 00323 * Create the do_pc2 table used 00324 * to affect pc2 permutation when 00325 * generating keys 00326 */ 00327 for(bit = 0; bit < 48; bit++) { 00328 comes_from_bit = pc2[bit] - 1; 00329 mask1 = bytemask[comes_from_bit % 7 + 1]; 00330 mask2 = BITMASK(bit % 24); 00331 for(j = 0; j < 128; j++) { 00332 if(j & mask1) 00333 do_pc2[comes_from_bit / 7][j] |= mask2; 00334 } 00335 } 00336 00337 /* 00338 * Now generate the table used to do combined 00339 * 32 bit permutation and e expansion 00340 * 00341 * We use it because we have to permute 16384 32 bit 00342 * longs into 48 bit in order to initialize sb. 00343 * 00344 * Looping 48 rounds per permutation becomes 00345 * just too slow... 00346 * 00347 */ 00348 00349 clearmem((char*)eperm32tab, sizeof(eperm32tab)); 00350 00351 for(bit = 0; bit < 48; bit++) { 00352 ufc_long inner_mask1,comes_from; 00353 00354 comes_from = perm32[esel[bit]-1]-1; 00355 inner_mask1 = bytemask[comes_from % 8]; 00356 00357 for(j = 256; j--;) { 00358 if(j & inner_mask1) 00359 eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK(bit % 24); 00360 } 00361 } 00362 00363 /* 00364 * Create the sb tables: 00365 * 00366 * For each 12 bit segment of an 48 bit intermediate 00367 * result, the sb table precomputes the two 4 bit 00368 * values of the sbox lookups done with the two 6 00369 * bit halves, shifts them to their proper place, 00370 * sends them through perm32 and finally E expands 00371 * them so that they are ready for the next 00372 * DES round. 00373 * 00374 */ 00375 for(sg = 0; sg < 4; sg++) { 00376 int j1, j2; 00377 int s1, s2; 00378 00379 for(j1 = 0; j1 < 64; j1++) { 00380 s1 = s_lookup(2 * sg, j1); 00381 for(j2 = 0; j2 < 64; j2++) { 00382 ufc_long to_permute, inx; 00383 00384 s2 = s_lookup(2 * sg + 1, j2); 00385 to_permute = ((s1 << 4) | s2) << (24 - 8 * sg); 00386 00387 #ifdef _UFC_32_ 00388 inx = ((j1 << 6) | j2) << 1; 00389 sb[sg][inx ] = eperm32tab[0][(to_permute >> 24) & 0xff][0]; 00390 sb[sg][inx+1] = eperm32tab[0][(to_permute >> 24) & 0xff][1]; 00391 sb[sg][inx ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0]; 00392 sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1]; 00393 sb[sg][inx ] |= eperm32tab[2][(to_permute >> 8) & 0xff][0]; 00394 sb[sg][inx+1] |= eperm32tab[2][(to_permute >> 8) & 0xff][1]; 00395 sb[sg][inx ] |= eperm32tab[3][(to_permute) & 0xff][0]; 00396 sb[sg][inx+1] |= eperm32tab[3][(to_permute) & 0xff][1]; 00397 #endif 00398 #ifdef _UFC_64_ 00399 inx = ((j1 << 6) | j2); 00400 sb[sg][inx] = 00401 ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) | 00402 (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1]; 00403 sb[sg][inx] |= 00404 ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) | 00405 (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1]; 00406 sb[sg][inx] |= 00407 ((long64)eperm32tab[2][(to_permute >> 8) & 0xff][0] << 32) | 00408 (long64)eperm32tab[2][(to_permute >> 8) & 0xff][1]; 00409 sb[sg][inx] |= 00410 ((long64)eperm32tab[3][(to_permute) & 0xff][0] << 32) | 00411 (long64)eperm32tab[3][(to_permute) & 0xff][1]; 00412 #endif 00413 } 00414 } 00415 } 00416 00417 /* 00418 * Create an inverse matrix for esel telling 00419 * where to plug out bits if undoing it 00420 */ 00421 for(bit=48; bit--;) { 00422 e_inverse[esel[bit] - 1 ] = bit; 00423 e_inverse[esel[bit] - 1 + 32] = bit + 48; 00424 } 00425 00426 /* 00427 * create efp: the matrix used to 00428 * undo the E expansion and effect final permutation 00429 */ 00430 clearmem((char*)efp, sizeof efp); 00431 for(bit = 0; bit < 64; bit++) { 00432 int o_bit, o_long; 00433 ufc_long word_value, inner_mask1, inner_mask2; 00434 int comes_from_f_bit, comes_from_e_bit; 00435 int comes_from_word, bit_within_word; 00436 00437 /* See where bit i belongs in the two 32 bit long's */ 00438 o_long = bit / 32; /* 0..1 */ 00439 o_bit = bit % 32; /* 0..31 */ 00440 00441 /* 00442 * And find a bit in the e permutated value setting this bit. 00443 * 00444 * Note: the e selection may have selected the same bit several 00445 * times. By the initialization of e_inverse, we only look 00446 * for one specific instance. 00447 */ 00448 comes_from_f_bit = final_perm[bit] - 1; /* 0..63 */ 00449 comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */ 00450 comes_from_word = comes_from_e_bit / 6; /* 0..15 */ 00451 bit_within_word = comes_from_e_bit % 6; /* 0..5 */ 00452 00453 inner_mask1 = longmask[bit_within_word + 26]; 00454 inner_mask2 = longmask[o_bit]; 00455 00456 for(word_value = 64; word_value--;) { 00457 if(word_value & inner_mask1) 00458 efp[comes_from_word][word_value][o_long] |= inner_mask2; 00459 } 00460 } 00461 initialized++; 00462 }
static void shuffle_sb | ( | long32 * | k, | |
ufc_long | saltbits | |||
) | [static] |
参照元 setup_salt().
00471 { ufc_long j; 00472 long32 x; 00473 for(j=4096; j--;) { 00474 x = (k[0] ^ k[1]) & (long32)saltbits; 00475 *k++ ^= x; 00476 *k++ ^= x; 00477 } 00478 }
static void shuffle_sb | ( | long64 * | k, | |
ufc_long | saltbits | |||
) | [static] |
static void setup_salt | ( | const char * | s1 | ) | [static] |
参照先 _ufc_sb0・_ufc_sb1・_ufc_sb2・_ufc_sb3・c・current_salt・current_saltbits・initialized・shuffle_sb()・ufc_init_des().
参照元 ufc_crypt().
00502 { ufc_long i, j, saltbits; 00503 const unsigned char *s2 = (const unsigned char *)s1; 00504 00505 if(!initialized) 00506 ufc_init_des(); 00507 00508 if(s2[0] == current_salt[0] && s2[1] == current_salt[1]) 00509 return; 00510 current_salt[0] = s2[0]; current_salt[1] = s2[1]; 00511 00512 /* 00513 * This is the only crypt change to DES: 00514 * entries are swapped in the expansion table 00515 * according to the bits set in the salt. 00516 */ 00517 saltbits = 0; 00518 for(i = 0; i < 2; i++) { 00519 long c=ascii_to_bin(s2[i]); 00520 if(c < 0 || c > 63) 00521 c = 0; 00522 for(j = 0; j < 6; j++) { 00523 if((c >> j) & 0x1) 00524 saltbits |= BITMASK(6 * i + j); 00525 } 00526 } 00527 00528 /* 00529 * Permute the sb table values 00530 * to reflect the changed e 00531 * selection table 00532 */ 00533 shuffle_sb(_ufc_sb0, current_saltbits ^ saltbits); 00534 shuffle_sb(_ufc_sb1, current_saltbits ^ saltbits); 00535 shuffle_sb(_ufc_sb2, current_saltbits ^ saltbits); 00536 shuffle_sb(_ufc_sb3, current_saltbits ^ saltbits); 00537 00538 current_saltbits = saltbits; 00539 }
static void ufc_mk_keytab | ( | char * | key | ) | [static] |
参照先 _ufc_keytab・direction・do_pc1・do_pc2・rots.
参照元 ufc_crypt().
00542 { ufc_long v1, v2, *k1; 00543 int i; 00544 #ifdef _UFC_32_ 00545 long32 v, *k2 = &_ufc_keytab[0][0]; 00546 #endif 00547 #ifdef _UFC_64_ 00548 long64 v, *k2 = &_ufc_keytab[0]; 00549 #endif 00550 00551 v1 = v2 = 0; k1 = &do_pc1[0][0][0]; 00552 for(i = 8; i--;) { 00553 v1 |= k1[*key & 0x7f]; k1 += 128; 00554 v2 |= k1[*key++ & 0x7f]; k1 += 128; 00555 } 00556 00557 for(i = 0; i < 16; i++) { 00558 k1 = &do_pc2[0][0]; 00559 00560 v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i])); 00561 v = k1[(v1 >> 21) & 0x7f]; k1 += 128; 00562 v |= k1[(v1 >> 14) & 0x7f]; k1 += 128; 00563 v |= k1[(v1 >> 7) & 0x7f]; k1 += 128; 00564 v |= k1[(v1 ) & 0x7f]; k1 += 128; 00565 00566 #ifdef _UFC_32_ 00567 *k2++ = v; 00568 v = 0; 00569 #endif 00570 #ifdef _UFC_64_ 00571 v <<= 32; 00572 #endif 00573 00574 v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i])); 00575 v |= k1[(v2 >> 21) & 0x7f]; k1 += 128; 00576 v |= k1[(v2 >> 14) & 0x7f]; k1 += 128; 00577 v |= k1[(v2 >> 7) & 0x7f]; k1 += 128; 00578 v |= k1[(v2 ) & 0x7f]; 00579 00580 *k2++ = v; 00581 } 00582 00583 direction = 0; 00584 }
ufc_long* _ufc_dofinalperm | ( | ufc_long | l1, | |
ufc_long | l2, | |||
ufc_long | r1, | |||
ufc_long | r2 | |||
) |
参照先 current_saltbits・efp.
参照元 _ufc_doit().
00591 { ufc_long v1, v2, x; 00592 static ufc_long ary[2]; 00593 00594 x = (l1 ^ l2) & current_saltbits; l1 ^= x; l2 ^= x; 00595 x = (r1 ^ r2) & current_saltbits; r1 ^= x; r2 ^= x; 00596 00597 v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3; 00598 00599 v1 |= efp[15][ r2 & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1]; 00600 v1 |= efp[14][(r2 >>= 6) & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1]; 00601 v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1]; 00602 v1 |= efp[12][(r2 >>= 6) & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1]; 00603 00604 v1 |= efp[11][ r1 & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1]; 00605 v1 |= efp[10][(r1 >>= 6) & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1]; 00606 v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1]; 00607 v1 |= efp[ 8][(r1 >>= 6) & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1]; 00608 00609 v1 |= efp[ 7][ l2 & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1]; 00610 v1 |= efp[ 6][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1]; 00611 v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1]; 00612 v1 |= efp[ 4][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1]; 00613 00614 v1 |= efp[ 3][ l1 & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1]; 00615 v1 |= efp[ 2][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1]; 00616 v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1]; 00617 v1 |= efp[ 0][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1]; 00618 00619 ary[0] = v1; ary[1] = v2; 00620 return ary; 00621 }
static char* output_conversion | ( | ufc_long | v1, | |
ufc_long | v2, | |||
const char * | salt | |||
) | [static] |
参照元 ufc_crypt().
00629 { static char outbuf[14]; 00630 int i, s; 00631 00632 outbuf[0] = salt[0]; 00633 outbuf[1] = salt[1] ? salt[1] : salt[0]; 00634 00635 for(i = 0; i < 5; i++) 00636 outbuf[i + 2] = bin_to_ascii((v1 >> (26 - 6 * i)) & 0x3f); 00637 00638 s = (v2 & 0xf) << 2; 00639 v2 = (v2 >> 2) | ((v1 & 0x3) << 30); 00640 00641 for(i = 5; i < 10; i++) 00642 outbuf[i + 2] = bin_to_ascii((v2 >> (56 - 6 * i)) & 0x3f); 00643 00644 outbuf[12] = bin_to_ascii(s); 00645 outbuf[13] = 0; 00646 00647 return outbuf; 00648 }
static ufc_long * _ufc_doit | ( | ufc_long | , | |
ufc_long | , | |||
ufc_long | , | |||
ufc_long | , | |||
ufc_long | ||||
) | [static] |
参照先 _ufc_dofinalperm()・_ufc_keytab・_ufc_sb0・_ufc_sb1・_ufc_sb2・_ufc_sb3.
参照元 ufc_crypt().
00697 { int i; 00698 long32 s, *k; 00699 00700 while(itr--) { 00701 k = &_ufc_keytab[0][0]; 00702 for(i=8; i--; ) { 00703 s = *k++ ^ r1; 00704 l1 ^= SBA(_ufc_sb1, s & 0xffff); l2 ^= SBA(_ufc_sb1, (s & 0xffff)+4); 00705 l1 ^= SBA(_ufc_sb0, s >>= 16); l2 ^= SBA(_ufc_sb0, (s) +4); 00706 s = *k++ ^ r2; 00707 l1 ^= SBA(_ufc_sb3, s & 0xffff); l2 ^= SBA(_ufc_sb3, (s & 0xffff)+4); 00708 l1 ^= SBA(_ufc_sb2, s >>= 16); l2 ^= SBA(_ufc_sb2, (s) +4); 00709 00710 s = *k++ ^ l1; 00711 r1 ^= SBA(_ufc_sb1, s & 0xffff); r2 ^= SBA(_ufc_sb1, (s & 0xffff)+4); 00712 r1 ^= SBA(_ufc_sb0, s >>= 16); r2 ^= SBA(_ufc_sb0, (s) +4); 00713 s = *k++ ^ l2; 00714 r1 ^= SBA(_ufc_sb3, s & 0xffff); r2 ^= SBA(_ufc_sb3, (s & 0xffff)+4); 00715 r1 ^= SBA(_ufc_sb2, s >>= 16); r2 ^= SBA(_ufc_sb2, (s) +4); 00716 } 00717 s=l1; l1=r1; r1=s; s=l2; l2=r2; r2=s; 00718 } 00719 return _ufc_dofinalperm(l1, l2, r1, r2); 00720 }
char* ufc_crypt | ( | const char * | key, | |
const char * | salt | |||
) |
参照先 _ufc_doit()・clearmem()・output_conversion()・setup_salt()・ufc_mk_keytab().
00657 { ufc_long *s; 00658 char ktab[9]; 00659 00660 /* 00661 * Hack DES tables according to salt 00662 */ 00663 setup_salt(salt); 00664 00665 /* 00666 * Setup key schedule 00667 */ 00668 clearmem(ktab, sizeof ktab); 00669 StrnCpy(ktab, key, 8); 00670 ufc_mk_keytab(ktab); 00671 00672 /* 00673 * Go for the 25 DES encryptions 00674 */ 00675 s = _ufc_doit((ufc_long)0, (ufc_long)0, 00676 (ufc_long)0, (ufc_long)0, (ufc_long)25); 00677 00678 /* 00679 * And convert back to 6 bit ASCII 00680 */ 00681 return output_conversion(s[0], s[1], salt); 00682 }
int pc1[56] [static] |
初期値:
{ 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }
参照元 ufc_init_des().
int rots[16] [static] |
int pc2[48] [static] |
初期値:
{ 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }
参照元 ufc_init_des().
int esel[48] [static] |
初期値:
{ 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }
参照元 ufc_init_des().
int e_inverse[64] [static] |
int perm32[32] [static] |
初期値:
{ 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 }
参照元 ufc_init_des().
int final_perm[64] [static] |
初期値:
{ 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }
参照元 ufc_init_des().
long64 _ufc_keytab |
long64 _ufc_sb0 |
long64 _ufc_sb1 |
long64 _ufc_sb2 |
long64 _ufc_sb3 |
ufc_long eperm32tab[4][256][2] [static] |
ufc_long do_pc1[8][2][128] [static] |
ufc_long do_pc2[8][128] [static] |
ufc_long efp[16][64][2] [static] |
unsigned char bytemask[8] [static] |
ufc_long longmask[32] [static] |
初期値:
{ 0x80000000, 0x40000000, 0x20000000, 0x10000000, 0x08000000, 0x04000000, 0x02000000, 0x01000000, 0x00800000, 0x00400000, 0x00200000, 0x00100000, 0x00080000, 0x00040000, 0x00020000, 0x00010000, 0x00008000, 0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200, 0x00000100, 0x00000080, 0x00000040, 0x00000020, 0x00000010, 0x00000008, 0x00000004, 0x00000002, 0x00000001 }
参照元 ufc_init_des().
int initialized = 0 [static] |
unsigned char current_salt[3] = "&&" [static] |
ufc_long current_saltbits = 0 [static] |
int direction = 0 [static] |
long32 _ufc_keytab[16][2] |