11 #pragma warning (disable: 4146) 14 static const sph_u32 IV224[] = {
20 static const sph_u32 IV256[] = {
26 static const sph_u32 IV384[] = {
33 static const sph_u32 IV512[] = {
42 static const sph_u32 mixtab0[] = {
131 static const sph_u32 mixtab1[] = {
220 static const sph_u32 mixtab2[] = {
309 static const sph_u32 mixtab3[] = {
398 #define TIX2(q, x00, x01, x08, x10, x24) do { \ 405 #define TIX3(q, x00, x01, x04, x08, x16, x27, x30) do { \ 413 #define TIX4(q, x00, x01, x04, x07, x08, x22, x24, x27, x30) do { \ 422 #define CMIX30(x00, x01, x02, x04, x05, x06, x15, x16, x17) do { \ 431 #define CMIX36(x00, x01, x02, x04, x05, x06, x18, x19, x20) do { \ 440 #define SMIX(x0, x1, x2, x3) do { \ 450 tmp = mixtab0[x0 >> 24]; \ 452 tmp = mixtab1[(x0 >> 16) & 0xFF]; \ 455 tmp = mixtab2[(x0 >> 8) & 0xFF]; \ 458 tmp = mixtab3[x0 & 0xFF]; \ 461 tmp = mixtab0[x1 >> 24]; \ 464 tmp = mixtab1[(x1 >> 16) & 0xFF]; \ 466 tmp = mixtab2[(x1 >> 8) & 0xFF]; \ 469 tmp = mixtab3[x1 & 0xFF]; \ 472 tmp = mixtab0[x2 >> 24]; \ 475 tmp = mixtab1[(x2 >> 16) & 0xFF]; \ 478 tmp = mixtab2[(x2 >> 8) & 0xFF]; \ 480 tmp = mixtab3[x2 & 0xFF]; \ 483 tmp = mixtab0[x3 >> 24]; \ 486 tmp = mixtab1[(x3 >> 16) & 0xFF]; \ 489 tmp = mixtab2[(x3 >> 8) & 0xFF]; \ 492 tmp = mixtab3[x3 & 0xFF]; \ 494 x0 = ((c0 ^ r0) & SPH_C32(0xFF000000)) \ 495 | ((c1 ^ r1) & SPH_C32(0x00FF0000)) \ 496 | ((c2 ^ r2) & SPH_C32(0x0000FF00)) \ 497 | ((c3 ^ r3) & SPH_C32(0x000000FF)); \ 498 x1 = ((c1 ^ (r0 << 8)) & SPH_C32(0xFF000000)) \ 499 | ((c2 ^ (r1 << 8)) & SPH_C32(0x00FF0000)) \ 500 | ((c3 ^ (r2 << 8)) & SPH_C32(0x0000FF00)) \ 501 | ((c0 ^ (r3 >> 24)) & SPH_C32(0x000000FF)); \ 502 x2 = ((c2 ^ (r0 << 16)) & SPH_C32(0xFF000000)) \ 503 | ((c3 ^ (r1 << 16)) & SPH_C32(0x00FF0000)) \ 504 | ((c0 ^ (r2 >> 16)) & SPH_C32(0x0000FF00)) \ 505 | ((c1 ^ (r3 >> 16)) & SPH_C32(0x000000FF)); \ 506 x3 = ((c3 ^ (r0 << 24)) & SPH_C32(0xFF000000)) \ 507 | ((c0 ^ (r1 >> 8)) & SPH_C32(0x00FF0000)) \ 508 | ((c1 ^ (r2 >> 8)) & SPH_C32(0x0000FF00)) \ 509 | ((c2 ^ (r3 >> 8)) & SPH_C32(0x000000FF)); \ 515 #define DECL_STATE_SMALL 516 #define READ_STATE_SMALL(state) 517 #define WRITE_STATE_SMALL(state) 518 #define DECL_STATE_BIG 519 #define READ_STATE_BIG(state) 520 #define WRITE_STATE_BIG(state) 522 #define S00 ((sc)->S[ 0]) 523 #define S01 ((sc)->S[ 1]) 524 #define S02 ((sc)->S[ 2]) 525 #define S03 ((sc)->S[ 3]) 526 #define S04 ((sc)->S[ 4]) 527 #define S05 ((sc)->S[ 5]) 528 #define S06 ((sc)->S[ 6]) 529 #define S07 ((sc)->S[ 7]) 530 #define S08 ((sc)->S[ 8]) 531 #define S09 ((sc)->S[ 9]) 532 #define S10 ((sc)->S[10]) 533 #define S11 ((sc)->S[11]) 534 #define S12 ((sc)->S[12]) 535 #define S13 ((sc)->S[13]) 536 #define S14 ((sc)->S[14]) 537 #define S15 ((sc)->S[15]) 538 #define S16 ((sc)->S[16]) 539 #define S17 ((sc)->S[17]) 540 #define S18 ((sc)->S[18]) 541 #define S19 ((sc)->S[19]) 542 #define S20 ((sc)->S[20]) 543 #define S21 ((sc)->S[21]) 544 #define S22 ((sc)->S[22]) 545 #define S23 ((sc)->S[23]) 546 #define S24 ((sc)->S[24]) 547 #define S25 ((sc)->S[25]) 548 #define S26 ((sc)->S[26]) 549 #define S27 ((sc)->S[27]) 550 #define S28 ((sc)->S[28]) 551 #define S29 ((sc)->S[29]) 552 #define S30 ((sc)->S[30]) 553 #define S31 ((sc)->S[31]) 554 #define S32 ((sc)->S[32]) 555 #define S33 ((sc)->S[33]) 556 #define S34 ((sc)->S[34]) 557 #define S35 ((sc)->S[35]) 561 #define DECL_STATE_SMALL \ 562 sph_u32 S00, S01, S02, S03, S04, S05, S06, S07, S08, S09; \ 563 sph_u32 S10, S11, S12, S13, S14, S15, S16, S17, S18, S19; \ 564 sph_u32 S20, S21, S22, S23, S24, S25, S26, S27, S28, S29; 566 #define DECL_STATE_BIG \ 568 sph_u32 S30, S31, S32, S33, S34, S35; 570 #define READ_STATE_SMALL(state) do { \ 571 S00 = (state)->S[ 0]; \ 572 S01 = (state)->S[ 1]; \ 573 S02 = (state)->S[ 2]; \ 574 S03 = (state)->S[ 3]; \ 575 S04 = (state)->S[ 4]; \ 576 S05 = (state)->S[ 5]; \ 577 S06 = (state)->S[ 6]; \ 578 S07 = (state)->S[ 7]; \ 579 S08 = (state)->S[ 8]; \ 580 S09 = (state)->S[ 9]; \ 581 S10 = (state)->S[10]; \ 582 S11 = (state)->S[11]; \ 583 S12 = (state)->S[12]; \ 584 S13 = (state)->S[13]; \ 585 S14 = (state)->S[14]; \ 586 S15 = (state)->S[15]; \ 587 S16 = (state)->S[16]; \ 588 S17 = (state)->S[17]; \ 589 S18 = (state)->S[18]; \ 590 S19 = (state)->S[19]; \ 591 S20 = (state)->S[20]; \ 592 S21 = (state)->S[21]; \ 593 S22 = (state)->S[22]; \ 594 S23 = (state)->S[23]; \ 595 S24 = (state)->S[24]; \ 596 S25 = (state)->S[25]; \ 597 S26 = (state)->S[26]; \ 598 S27 = (state)->S[27]; \ 599 S28 = (state)->S[28]; \ 600 S29 = (state)->S[29]; \ 603 #define READ_STATE_BIG(state) do { \ 604 READ_STATE_SMALL(state); \ 605 S30 = (state)->S[30]; \ 606 S31 = (state)->S[31]; \ 607 S32 = (state)->S[32]; \ 608 S33 = (state)->S[33]; \ 609 S34 = (state)->S[34]; \ 610 S35 = (state)->S[35]; \ 613 #define WRITE_STATE_SMALL(state) do { \ 614 (state)->S[ 0] = S00; \ 615 (state)->S[ 1] = S01; \ 616 (state)->S[ 2] = S02; \ 617 (state)->S[ 3] = S03; \ 618 (state)->S[ 4] = S04; \ 619 (state)->S[ 5] = S05; \ 620 (state)->S[ 6] = S06; \ 621 (state)->S[ 7] = S07; \ 622 (state)->S[ 8] = S08; \ 623 (state)->S[ 9] = S09; \ 624 (state)->S[10] = S10; \ 625 (state)->S[11] = S11; \ 626 (state)->S[12] = S12; \ 627 (state)->S[13] = S13; \ 628 (state)->S[14] = S14; \ 629 (state)->S[15] = S15; \ 630 (state)->S[16] = S16; \ 631 (state)->S[17] = S17; \ 632 (state)->S[18] = S18; \ 633 (state)->S[19] = S19; \ 634 (state)->S[20] = S20; \ 635 (state)->S[21] = S21; \ 636 (state)->S[22] = S22; \ 637 (state)->S[23] = S23; \ 638 (state)->S[24] = S24; \ 639 (state)->S[25] = S25; \ 640 (state)->S[26] = S26; \ 641 (state)->S[27] = S27; \ 642 (state)->S[28] = S28; \ 643 (state)->S[29] = S29; \ 646 #define WRITE_STATE_BIG(state) do { \ 647 WRITE_STATE_SMALL(state); \ 648 (state)->S[30] = S30; \ 649 (state)->S[31] = S31; \ 650 (state)->S[32] = S32; \ 651 (state)->S[33] = S33; \ 652 (state)->S[34] = S34; \ 653 (state)->S[35] = S35; \ 660 const sph_u32 *iv,
size_t iv_len)
664 for (u = 0; u < z_len; u ++)
666 memcpy(&sc->
S[z_len], iv, iv_len *
sizeof *iv);
680 #define INCR_COUNTER do { \ 681 sc->bit_count += (sph_u64)len << 3; \ 686 #define INCR_COUNTER do { \ 687 sph_u32 tmp = SPH_T32((sph_u32)len << 3); \ 688 sc->bit_count_low = SPH_T32(sc->bit_count_low + tmp); \ 689 if (sc->bit_count_low < tmp) \ 690 sc->bit_count_high ++; \ 691 sc->bit_count_high = SPH_T32(sc->bit_count_high \ 692 + ((sph_u32)len >> 29)); \ 699 unsigned plen, rshift; \ 702 plen = sc->partial_len; \ 704 unsigned count = 4 - plen; \ 708 while (count -- > 0) { \ 709 p = (p << 8) | *(const unsigned char *)data; \ 710 data = (const unsigned char *)data + 1; \ 715 sc->partial_len = plen; \ 722 sc->partial_len = (unsigned)len; \ 723 while (len -- > 0) { \ 724 p = (p << 8) | *(const unsigned char *)data; \ 725 data = (const unsigned char *)data + 1; \ 728 sc->round_shift = rshift; 738 p = sph_dec32be(data); \ 739 data = (const unsigned char *)data + 4; \ 755 TIX2(q, S00, S01, S08, S10, S24);
756 CMIX30(S27, S28, S29, S01, S02, S03, S12, S13, S14);
757 SMIX(S27, S28, S29, S00);
758 CMIX30(S24, S25, S26, S28, S29, S00, S09, S10, S11);
759 SMIX(S24, S25, S26, S27);
764 TIX2(q, S24, S25, S02, S04, S18);
765 CMIX30(S21, S22, S23, S25, S26, S27, S06, S07, S08);
766 SMIX(S21, S22, S23, S24);
767 CMIX30(S18, S19, S20, S22, S23, S24, S03, S04, S05);
768 SMIX(S18, S19, S20, S21);
773 TIX2(q, S18, S19, S26, S28, S12);
774 CMIX30(S15, S16, S17, S19, S20, S21, S00, S01, S02);
775 SMIX(S15, S16, S17, S18);
776 CMIX30(S12, S13, S14, S16, S17, S18, S27, S28, S29);
777 SMIX(S12, S13, S14, S15);
782 TIX2(q, S12, S13, S20, S22, S06);
783 CMIX30(S09, S10, S11, S13, S14, S15, S24, S25, S26);
784 SMIX(S09, S10, S11, S12);
785 CMIX30(S06, S07, S08, S10, S11, S12, S21, S22, S23);
786 SMIX(S06, S07, S08, S09);
791 TIX2(q, S06, S07, S14, S16, S00);
792 CMIX30(S03, S04, S05, S07, S08, S09, S18, S19, S20);
793 SMIX(S03, S04, S05, S06);
794 CMIX30(S00, S01, S02, S04, S05, S06, S15, S16, S17);
795 SMIX(S00, S01, S02, S03);
816 TIX3(q, S00, S01, S04, S08, S16, S27, S30);
817 CMIX36(S33, S34, S35, S01, S02, S03, S15, S16, S17);
818 SMIX(S33, S34, S35, S00);
819 CMIX36(S30, S31, S32, S34, S35, S00, S12, S13, S14);
820 SMIX(S30, S31, S32, S33);
821 CMIX36(S27, S28, S29, S31, S32, S33, S09, S10, S11);
822 SMIX(S27, S28, S29, S30);
827 TIX3(q, S27, S28, S31, S35, S07, S18, S21);
828 CMIX36(S24, S25, S26, S28, S29, S30, S06, S07, S08);
829 SMIX(S24, S25, S26, S27);
830 CMIX36(S21, S22, S23, S25, S26, S27, S03, S04, S05);
831 SMIX(S21, S22, S23, S24);
832 CMIX36(S18, S19, S20, S22, S23, S24, S00, S01, S02);
833 SMIX(S18, S19, S20, S21);
838 TIX3(q, S18, S19, S22, S26, S34, S09, S12);
839 CMIX36(S15, S16, S17, S19, S20, S21, S33, S34, S35);
840 SMIX(S15, S16, S17, S18);
841 CMIX36(S12, S13, S14, S16, S17, S18, S30, S31, S32);
842 SMIX(S12, S13, S14, S15);
843 CMIX36(S09, S10, S11, S13, S14, S15, S27, S28, S29);
844 SMIX(S09, S10, S11, S12);
849 TIX3(q, S09, S10, S13, S17, S25, S00, S03);
850 CMIX36(S06, S07, S08, S10, S11, S12, S24, S25, S26);
851 SMIX(S06, S07, S08, S09);
852 CMIX36(S03, S04, S05, S07, S08, S09, S21, S22, S23);
853 SMIX(S03, S04, S05, S06);
854 CMIX36(S00, S01, S02, S04, S05, S06, S18, S19, S20);
855 SMIX(S00, S01, S02, S03);
876 TIX4(q, S00, S01, S04, S07, S08, S22, S24, S27, S30);
877 CMIX36(S33, S34, S35, S01, S02, S03, S15, S16, S17);
878 SMIX(S33, S34, S35, S00);
879 CMIX36(S30, S31, S32, S34, S35, S00, S12, S13, S14);
880 SMIX(S30, S31, S32, S33);
881 CMIX36(S27, S28, S29, S31, S32, S33, S09, S10, S11);
882 SMIX(S27, S28, S29, S30);
883 CMIX36(S24, S25, S26, S28, S29, S30, S06, S07, S08);
884 SMIX(S24, S25, S26, S27);
889 TIX4(q, S24, S25, S28, S31, S32, S10, S12, S15, S18);
890 CMIX36(S21, S22, S23, S25, S26, S27, S03, S04, S05);
891 SMIX(S21, S22, S23, S24);
892 CMIX36(S18, S19, S20, S22, S23, S24, S00, S01, S02);
893 SMIX(S18, S19, S20, S21);
894 CMIX36(S15, S16, S17, S19, S20, S21, S33, S34, S35);
895 SMIX(S15, S16, S17, S18);
896 CMIX36(S12, S13, S14, S16, S17, S18, S30, S31, S32);
897 SMIX(S12, S13, S14, S15);
902 TIX4(q, S12, S13, S16, S19, S20, S34, S00, S03, S06);
903 CMIX36(S09, S10, S11, S13, S14, S15, S27, S28, S29);
904 SMIX(S09, S10, S11, S12);
905 CMIX36(S06, S07, S08, S10, S11, S12, S24, S25, S26);
906 SMIX(S06, S07, S08, S09);
907 CMIX36(S03, S04, S05, S07, S08, S09, S21, S22, S23);
908 SMIX(S03, S04, S05, S06);
909 CMIX36(S00, S01, S02, S04, S05, S06, S18, S19, S20);
910 SMIX(S00, S01, S02, S03);
920 #define WRITE_COUNTER do { \ 921 sph_enc64be(buf + 4, sc->bit_count + n); \ 926 #define WRITE_COUNTER do { \ 927 sph_enc32be(buf + 4, sc->bit_count_high); \ 928 sph_enc32be(buf + 8, sc->bit_count_low + n); \ 933 #define CLOSE_ENTRY(s, rcm, core) \ 934 unsigned char buf[16]; \ 935 unsigned plen, rms; \ 936 unsigned char *out; \ 938 plen = sc->partial_len; \ 940 if (plen == 0 && n == 0) { \ 942 } else if (plen < 4 || n != 0) { \ 947 buf[plen] = ub & ~(0xFFU >> n); \ 948 for (u = plen + 1; u < 4; u ++) \ 951 core(sc, buf + plen, (sizeof buf) - plen); \ 952 rms = sc->round_shift * (rcm); \ 953 memcpy(S, sc->S + (s) - rms, rms * sizeof(sph_u32)); \ 954 memcpy(S + rms, sc->S, ((s) - rms) * sizeof(sph_u32)); 956 #define ROR(n, s) do { \ 958 memcpy(tmp, S + ((s) - (n)), (n) * sizeof(sph_u32)); \ 959 memmove(S + (n), S, ((s) - (n)) * sizeof(sph_u32)); \ 960 memcpy(S, tmp, (n) * sizeof(sph_u32)); \ 965 void *dst,
size_t out_size_w32)
970 for (i = 0; i < 10; i ++) {
975 for (i = 0; i < 13; i ++) {
988 sph_enc32be(out + 0,
S[ 1]);
989 sph_enc32be(out + 4,
S[ 2]);
990 sph_enc32be(out + 8,
S[ 3]);
991 sph_enc32be(out + 12,
S[ 4]);
992 sph_enc32be(out + 16,
S[15]);
993 sph_enc32be(out + 20,
S[16]);
994 sph_enc32be(out + 24,
S[17]);
995 if (out_size_w32 == 8) {
996 sph_enc32be(out + 28,
S[18]);
1009 for (i = 0; i < 18; i ++) {
1014 for (i = 0; i < 13; i ++) {
1035 sph_enc32be(out + 0,
S[ 1]);
1036 sph_enc32be(out + 4,
S[ 2]);
1037 sph_enc32be(out + 8,
S[ 3]);
1038 sph_enc32be(out + 12,
S[ 4]);
1039 sph_enc32be(out + 16,
S[12]);
1040 sph_enc32be(out + 20,
S[13]);
1041 sph_enc32be(out + 24,
S[14]);
1042 sph_enc32be(out + 28,
S[15]);
1043 sph_enc32be(out + 32,
S[24]);
1044 sph_enc32be(out + 36,
S[25]);
1045 sph_enc32be(out + 40,
S[26]);
1046 sph_enc32be(out + 44,
S[27]);
1056 for (i = 0; i < 32; i ++) {
1061 for (i = 0; i < 13; i ++) {
1092 sph_enc32be(out + 0,
S[ 1]);
1093 sph_enc32be(out + 4,
S[ 2]);
1094 sph_enc32be(out + 8,
S[ 3]);
1095 sph_enc32be(out + 12,
S[ 4]);
1096 sph_enc32be(out + 16,
S[ 9]);
1097 sph_enc32be(out + 20,
S[10]);
1098 sph_enc32be(out + 24,
S[11]);
1099 sph_enc32be(out + 28,
S[12]);
1100 sph_enc32be(out + 32,
S[18]);
1101 sph_enc32be(out + 36,
S[19]);
1102 sph_enc32be(out + 40,
S[20]);
1103 sph_enc32be(out + 44,
S[21]);
1104 sph_enc32be(out + 48,
S[27]);
1105 sph_enc32be(out + 52,
S[28]);
1106 sph_enc32be(out + 56,
S[29]);
1107 sph_enc32be(out + 60,
S[30]);
1114 fugue_init(cc, 23, IV224, 7);
1120 fugue2_core(cc, data, len);
1126 fugue2_close(cc, 0, 0, dst, 7);
1132 fugue2_close(cc, ub, n, dst, 7);
1138 fugue_init(cc, 22, IV256, 8);
1144 fugue2_core(cc, data, len);
1150 fugue2_close(cc, 0, 0, dst, 8);
1156 fugue2_close(cc, ub, n, dst, 8);
1162 fugue_init(cc, 24, IV384, 12);
1168 fugue3_core(cc, data, len);
1174 fugue3_close(cc, 0, 0, dst);
1180 fugue3_close(cc, ub, n, dst);
1186 fugue_init(cc, 20, IV512, 16);
1192 fugue4_core(cc, data, len);
1198 fugue4_close(cc, 0, 0, dst);
1204 fugue4_close(cc, ub, n, dst);
void sph_fugue224(void *cc, const void *data, size_t len)
void sph_fugue512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
#define CMIX36(x00, x01, x02, x04, x05, x06, x18, x19, x20)
void sph_fugue512(void *cc, const void *data, size_t len)
void sph_fugue256_init(void *cc)
void sph_fugue224_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
void sph_fugue384_close(void *cc, void *dst)
#define CMIX30(x00, x01, x02, x04, x05, x06, x15, x16, x17)
void sph_fugue256(void *cc, const void *data, size_t len)
void sph_fugue256_close(void *cc, void *dst)
void sph_fugue224_close(void *cc, void *dst)
void sph_fugue384(void *cc, const void *data, size_t len)
#define WRITE_STATE_SMALL(state)
#define TIX2(q, x00, x01, x08, x10, x24)
void sph_fugue384_init(void *cc)
#define TIX3(q, x00, x01, x04, x08, x16, x27, x30)
#define S(x0, x1, x2, x3, cb, r)
#define WRITE_STATE_BIG(state)
#define CLOSE_ENTRY(s, rcm, core)
void sph_fugue384_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
#define READ_STATE_SMALL(state)
void sph_fugue256_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)
#define TIX4(q, x00, x01, x04, x07, x08, x22, x24, x27, x30)
void * memcpy(void *a, const void *b, size_t c)
void sph_fugue512_close(void *cc, void *dst)
#define SMIX(x0, x1, x2, x3)
#define READ_STATE_BIG(state)
void sph_fugue224_init(void *cc)
void sph_fugue512_init(void *cc)