43 #if SPH_SMALL_FOOTPRINT && !defined SPH_SMALL_FOOTPRINT_SKEIN 44 #define SPH_SMALL_FOOTPRINT_SKEIN 1 48 #pragma warning (disable: 4146) 373 #define XCAT(x, y) XCAT_(x, y) 374 #define XCAT_(x, y) x ## y 378 #define SKSI(k, s, i) XCAT(k, XCAT(XCAT(XCAT(M5_, s), _), i)) 379 #define SKST(t, s, v) XCAT(t, XCAT(XCAT(XCAT(M3_, s), _), v)) 382 #define SKBI(k, s, i) XCAT(k, XCAT(XCAT(XCAT(M9_, s), _), i)) 383 #define SKBT(t, s, v) XCAT(t, XCAT(XCAT(XCAT(M3_, s), _), v)) 387 #define TFSMALL_KINIT(k0, k1, k2, k3, k4, t0, t1, t2) do { \ 388 k4 = (k0 ^ k1) ^ (k2 ^ k3) ^ SPH_C64(0x1BD11BDAA9FC1A22); \ 393 #define TFBIG_KINIT(k0, k1, k2, k3, k4, k5, k6, k7, k8, t0, t1, t2) do { \ 394 k8 = ((k0 ^ k1) ^ (k2 ^ k3)) ^ ((k4 ^ k5) ^ (k6 ^ k7)) \ 395 ^ SPH_C64(0x1BD11BDAA9FC1A22); \ 401 #define TFSMALL_ADDKEY(w0, w1, w2, w3, k, t, s) do { \ 402 w0 = SPH_T64(w0 + SKSI(k, s, 0)); \ 403 w1 = SPH_T64(w1 + SKSI(k, s, 1) + SKST(t, s, 0)); \ 404 w2 = SPH_T64(w2 + SKSI(k, s, 2) + SKST(t, s, 1)); \ 405 w3 = SPH_T64(w3 + SKSI(k, s, 3) + (sph_u64)s); \ 409 #if SPH_SMALL_FOOTPRINT_SKEIN 411 #define TFBIG_ADDKEY(s, tt0, tt1) do { \ 412 p0 = SPH_T64(p0 + h[s + 0]); \ 413 p1 = SPH_T64(p1 + h[s + 1]); \ 414 p2 = SPH_T64(p2 + h[s + 2]); \ 415 p3 = SPH_T64(p3 + h[s + 3]); \ 416 p4 = SPH_T64(p4 + h[s + 4]); \ 417 p5 = SPH_T64(p5 + h[s + 5] + tt0); \ 418 p6 = SPH_T64(p6 + h[s + 6] + tt1); \ 419 p7 = SPH_T64(p7 + h[s + 7] + (sph_u64)s); \ 424 #define TFBIG_ADDKEY(w0, w1, w2, w3, w4, w5, w6, w7, k, t, s) do { \ 425 w0 = SPH_T64(w0 + SKBI(k, s, 0)); \ 426 w1 = SPH_T64(w1 + SKBI(k, s, 1)); \ 427 w2 = SPH_T64(w2 + SKBI(k, s, 2)); \ 428 w3 = SPH_T64(w3 + SKBI(k, s, 3)); \ 429 w4 = SPH_T64(w4 + SKBI(k, s, 4)); \ 430 w5 = SPH_T64(w5 + SKBI(k, s, 5) + SKBT(t, s, 0)); \ 431 w6 = SPH_T64(w6 + SKBI(k, s, 6) + SKBT(t, s, 1)); \ 432 w7 = SPH_T64(w7 + SKBI(k, s, 7) + (sph_u64)s); \ 439 #define TFSMALL_MIX(x0, x1, rc) do { \ 440 x0 = SPH_T64(x0 + x1); \ 441 x1 = SPH_ROTL64(x1, rc) ^ x0; \ 445 #define TFBIG_MIX(x0, x1, rc) do { \ 446 x0 = SPH_T64(x0 + x1); \ 447 x1 = SPH_ROTL64(x1, rc) ^ x0; \ 452 #define TFSMALL_MIX4(w0, w1, w2, w3, rc0, rc1) do { \ 453 TFSMALL_MIX(w0, w1, rc0); \ 454 TFSMALL_MIX(w2, w3, rc1); \ 458 #define TFBIG_MIX8(w0, w1, w2, w3, w4, w5, w6, w7, rc0, rc1, rc2, rc3) do { \ 459 TFBIG_MIX(w0, w1, rc0); \ 460 TFBIG_MIX(w2, w3, rc1); \ 461 TFBIG_MIX(w4, w5, rc2); \ 462 TFBIG_MIX(w6, w7, rc3); \ 467 #define TFSMALL_4e(s) do { \ 468 TFSMALL_ADDKEY(p0, p1, p2, p3, h, t, s); \ 469 TFSMALL_MIX4(p0, p1, p2, p3, 14, 16); \ 470 TFSMALL_MIX4(p0, p3, p2, p1, 52, 57); \ 471 TFSMALL_MIX4(p0, p1, p2, p3, 23, 40); \ 472 TFSMALL_MIX4(p0, p3, p2, p1, 5, 37); \ 475 #define TFSMALL_4o(s) do { \ 476 TFSMALL_ADDKEY(p0, p1, p2, p3, h, t, s); \ 477 TFSMALL_MIX4(p0, p1, p2, p3, 25, 33); \ 478 TFSMALL_MIX4(p0, p3, p2, p1, 46, 12); \ 479 TFSMALL_MIX4(p0, p1, p2, p3, 58, 22); \ 480 TFSMALL_MIX4(p0, p3, p2, p1, 32, 32); \ 484 #if SPH_SMALL_FOOTPRINT_SKEIN 486 #define TFBIG_4e(s) do { \ 487 TFBIG_ADDKEY(s, t0, t1); \ 488 TFBIG_MIX8(p0, p1, p2, p3, p4, p5, p6, p7, 46, 36, 19, 37); \ 489 TFBIG_MIX8(p2, p1, p4, p7, p6, p5, p0, p3, 33, 27, 14, 42); \ 490 TFBIG_MIX8(p4, p1, p6, p3, p0, p5, p2, p7, 17, 49, 36, 39); \ 491 TFBIG_MIX8(p6, p1, p0, p7, p2, p5, p4, p3, 44, 9, 54, 56); \ 494 #define TFBIG_4o(s) do { \ 495 TFBIG_ADDKEY(s, t1, t2); \ 496 TFBIG_MIX8(p0, p1, p2, p3, p4, p5, p6, p7, 39, 30, 34, 24); \ 497 TFBIG_MIX8(p2, p1, p4, p7, p6, p5, p0, p3, 13, 50, 10, 17); \ 498 TFBIG_MIX8(p4, p1, p6, p3, p0, p5, p2, p7, 25, 29, 39, 43); \ 499 TFBIG_MIX8(p6, p1, p0, p7, p2, p5, p4, p3, 8, 35, 56, 22); \ 504 #define TFBIG_4e(s) do { \ 505 TFBIG_ADDKEY(p0, p1, p2, p3, p4, p5, p6, p7, h, t, s); \ 506 TFBIG_MIX8(p0, p1, p2, p3, p4, p5, p6, p7, 46, 36, 19, 37); \ 507 TFBIG_MIX8(p2, p1, p4, p7, p6, p5, p0, p3, 33, 27, 14, 42); \ 508 TFBIG_MIX8(p4, p1, p6, p3, p0, p5, p2, p7, 17, 49, 36, 39); \ 509 TFBIG_MIX8(p6, p1, p0, p7, p2, p5, p4, p3, 44, 9, 54, 56); \ 512 #define TFBIG_4o(s) do { \ 513 TFBIG_ADDKEY(p0, p1, p2, p3, p4, p5, p6, p7, h, t, s); \ 514 TFBIG_MIX8(p0, p1, p2, p3, p4, p5, p6, p7, 39, 30, 34, 24); \ 515 TFBIG_MIX8(p2, p1, p4, p7, p6, p5, p0, p3, 13, 50, 10, 17); \ 516 TFBIG_MIX8(p4, p1, p6, p3, p0, p5, p2, p7, 25, 29, 39, 43); \ 517 TFBIG_MIX8(p6, p1, p0, p7, p2, p5, p4, p3, 8, 35, 56, 22); \ 524 #define UBI_SMALL(etype, extra) do { \ 525 sph_u64 h4, t0, t1, t2; \ 526 sph_u64 m0 = sph_dec64le(buf + 0); \ 527 sph_u64 m1 = sph_dec64le(buf + 8); \ 528 sph_u64 m2 = sph_dec64le(buf + 16); \ 529 sph_u64 m3 = sph_dec64le(buf + 24); \ 534 t0 = SPH_T64(bcount << 5) + (sph_u64)(extra); \ 535 t1 = (bcount >> 59) + ((sph_u64)(etype) << 55); \ 536 TFSMALL_KINIT(h0, h1, h2, h3, h4, t0, t1, t2); \ 555 TFSMALL_ADDKEY(p0, p1, p2, p3, h, t, 18); \ 563 #if SPH_SMALL_FOOTPRINT_SKEIN 565 #define UBI_BIG(etype, extra) do { \ 566 sph_u64 t0, t1, t2; \ 568 sph_u64 m0 = sph_dec64le_aligned(buf + 0); \ 569 sph_u64 m1 = sph_dec64le_aligned(buf + 8); \ 570 sph_u64 m2 = sph_dec64le_aligned(buf + 16); \ 571 sph_u64 m3 = sph_dec64le_aligned(buf + 24); \ 572 sph_u64 m4 = sph_dec64le_aligned(buf + 32); \ 573 sph_u64 m5 = sph_dec64le_aligned(buf + 40); \ 574 sph_u64 m6 = sph_dec64le_aligned(buf + 48); \ 575 sph_u64 m7 = sph_dec64le_aligned(buf + 56); \ 584 t0 = SPH_T64(bcount << 6) + (sph_u64)(extra); \ 585 t1 = (bcount >> 58) + ((sph_u64)(etype) << 55); \ 586 TFBIG_KINIT(h[0], h[1], h[2], h[3], h[4], h[5], \ 587 h[6], h[7], h[8], t0, t1, t2); \ 588 for (u = 0; u <= 15; u += 3) { \ 589 h[u + 9] = h[u + 0]; \ 590 h[u + 10] = h[u + 1]; \ 591 h[u + 11] = h[u + 2]; \ 593 for (u = 0; u < 9; u ++) { \ 594 sph_u64 s = u << 1; \ 603 TFBIG_ADDKEY(18, t0, t1); \ 616 #define UBI_BIG(etype, extra) do { \ 617 sph_u64 h8, t0, t1, t2; \ 618 sph_u64 m0 = sph_dec64le_aligned(buf + 0); \ 619 sph_u64 m1 = sph_dec64le_aligned(buf + 8); \ 620 sph_u64 m2 = sph_dec64le_aligned(buf + 16); \ 621 sph_u64 m3 = sph_dec64le_aligned(buf + 24); \ 622 sph_u64 m4 = sph_dec64le_aligned(buf + 32); \ 623 sph_u64 m5 = sph_dec64le_aligned(buf + 40); \ 624 sph_u64 m6 = sph_dec64le_aligned(buf + 48); \ 625 sph_u64 m7 = sph_dec64le_aligned(buf + 56); \ 634 t0 = SPH_T64(bcount << 6) + (sph_u64)(extra); \ 635 t1 = (bcount >> 58) + ((sph_u64)(etype) << 55); \ 636 TFBIG_KINIT(h0, h1, h2, h3, h4, h5, h6, h7, h8, t0, t1, t2); \ 655 TFBIG_ADDKEY(p0, p1, p2, p3, p4, p5, p6, p7, h, t, 18); \ 670 #define DECL_STATE_SMALL \ 671 sph_u64 h0, h1, h2, h3; \ 674 #define READ_STATE_SMALL(sc) do { \ 679 bcount = sc->bcount; \ 682 #define WRITE_STATE_SMALL(sc) do { \ 687 sc->bcount = bcount; \ 691 #if SPH_SMALL_FOOTPRINT_SKEIN 693 #define DECL_STATE_BIG \ 697 #define READ_STATE_BIG(sc) do { \ 706 bcount = sc->bcount; \ 709 #define WRITE_STATE_BIG(sc) do { \ 718 sc->bcount = bcount; \ 723 #define DECL_STATE_BIG \ 724 sph_u64 h0, h1, h2, h3, h4, h5, h6, h7; \ 727 #define READ_STATE_BIG(sc) do { \ 736 bcount = sc->bcount; \ 739 #define WRITE_STATE_BIG(sc) do { \ 748 sc->bcount = bcount; \ 756 skein_small_init(sph_skein_small_context *sc,
const sph_u64 *iv)
768 skein_big_init(sph_skein_big_context *sc,
const sph_u64 *iv)
785 skein_small_core(sph_skein_small_context *sc,
const void *data,
size_t len)
794 clen = (
sizeof sc->buf) - ptr;
796 memcpy(buf + ptr, data, len);
801 memcpy(buf + ptr, data, clen);
802 data = (
const unsigned char *)data + clen;
806 #if SPH_SMALL_FOOTPRINT_SKEIN 809 first = (bcount == 0) << 7;
812 UBI_SMALL(96 + first, 0);
813 if (len <= sizeof sc->buf)
816 memcpy(buf, data,
sizeof sc->buf);
817 data = (
const unsigned char *)data +
sizeof sc->buf;
818 len -=
sizeof sc->buf;
831 first = (bcount == 0) << 7;
834 UBI_SMALL(96 + first, 0);
835 if (len <= sizeof sc->buf)
837 buf = (
unsigned char *)data;
840 if (len <= 2 * sizeof sc->buf) {
841 data = buf +
sizeof sc->buf;
842 len -=
sizeof sc->buf;
845 buf +=
sizeof sc->buf;
846 data = buf +
sizeof sc->buf;
848 len -= 2 *
sizeof sc->buf;
852 memcpy(sc->buf, data, len);
859 skein_big_core(sph_skein_big_context *sc,
const void *data,
size_t len)
878 if (len <= (
sizeof sc->buf) - ptr) {
879 memcpy(buf + ptr, data, len);
886 first = (bcount == 0) << 7;
890 if (ptr ==
sizeof sc->buf) {
892 UBI_BIG(96 + first, 0);
896 clen = (
sizeof sc->buf) - ptr;
899 memcpy(buf + ptr, data, clen);
901 data = (
const unsigned char *)data + clen;
911 skein_small_close(sph_skein_small_context *sc,
unsigned ub,
unsigned n,
912 void *dst,
size_t out_len)
925 x = ((ub & -z) | z) & 0xFF;
926 skein_small_core(sc, &x, 1);
932 memset(buf + ptr, 0, (
sizeof sc->buf) - ptr);
933 et = 352 + ((bcount == 0) << 7) + (n != 0);
934 for (i = 0; i < 2; i ++) {
937 memset(buf, 0,
sizeof sc->buf);
944 sph_enc64le_aligned(buf + 0, h0);
945 sph_enc64le_aligned(buf + 8, h1);
946 sph_enc64le_aligned(buf + 16, h2);
947 sph_enc64le_aligned(buf + 24, h3);
948 memcpy(dst, buf, out_len);
953 skein_big_close(sph_skein_big_context *sc,
unsigned ub,
unsigned n,
954 void *dst,
size_t out_len)
960 #if SPH_SMALL_FOOTPRINT_SKEIN 973 x = ((ub & -z) | z) & 0xFF;
974 skein_big_core(sc, &x, 1);
993 memset(buf + ptr, 0, (
sizeof sc->buf) - ptr);
994 et = 352 + ((bcount == 0) << 7) + (n != 0);
995 for (i = 0; i < 2; i ++) {
998 memset(buf, 0,
sizeof sc->buf);
1005 #if SPH_SMALL_FOOTPRINT_SKEIN 1012 for (u = 0; u < out_len; u += 8)
1013 sph_enc64le_aligned(buf + u, h[u >> 3]);
1014 memcpy(dst, buf, out_len);
1018 sph_enc64le_aligned(buf + 0, h0);
1019 sph_enc64le_aligned(buf + 8, h1);
1020 sph_enc64le_aligned(buf + 16, h2);
1021 sph_enc64le_aligned(buf + 24, h3);
1022 sph_enc64le_aligned(buf + 32, h4);
1023 sph_enc64le_aligned(buf + 40, h5);
1024 sph_enc64le_aligned(buf + 48, h6);
1025 sph_enc64le_aligned(buf + 56, h7);
1026 memcpy(dst, buf, out_len);
1033 static const sph_u64
IV224[] = {
1034 SPH_C64(0xC6098A8C9AE5EA0B), SPH_C64(0x876D568608C5191C),
1035 SPH_C64(0x99CB88D7D7F53884), SPH_C64(0x384BDDB1AEDDB5DE)
1038 static const sph_u64
IV256[] = {
1039 SPH_C64(0xFC9DA860D048B449), SPH_C64(0x2FCA66479FA7D833),
1040 SPH_C64(0xB33BC3896656840F), SPH_C64(0x6A54E920FDE8DA69)
1044 static const sph_u64
IV224[] = {
1045 SPH_C64(0xCCD0616248677224), SPH_C64(0xCBA65CF3A92339EF),
1046 SPH_C64(0x8CCD69D652FF4B64), SPH_C64(0x398AED7B3AB890B4),
1047 SPH_C64(0x0F59D1B1457D2BD0), SPH_C64(0x6776FE6575D4EB3D),
1048 SPH_C64(0x99FBC70E997413E9), SPH_C64(0x9E2CFCCFE1C41EF7)
1051 static const sph_u64
IV256[] = {
1052 SPH_C64(0xCCD044A12FDB3E13), SPH_C64(0xE83590301A79A9EB),
1053 SPH_C64(0x55AEA0614F816E6F), SPH_C64(0x2A2767A4AE9B94DB),
1054 SPH_C64(0xEC06025E74DD7683), SPH_C64(0xE7A436CDC4746251),
1055 SPH_C64(0xC36FBAF9393AD185), SPH_C64(0x3EEDBA1833EDFC13)
1058 static const sph_u64
IV384[] = {
1059 SPH_C64(0xA3F6C6BF3A75EF5F), SPH_C64(0xB0FEF9CCFD84FAA4),
1060 SPH_C64(0x9D77DD663D770CFE), SPH_C64(0xD798CBF3B468FDDA),
1061 SPH_C64(0x1BC4A6668A0E4465), SPH_C64(0x7ED7D434E5807407),
1062 SPH_C64(0x548FC1ACD4EC44D6), SPH_C64(0x266E17546AA18FF8)
1065 static const sph_u64
IV512[] = {
1066 SPH_C64(0x4903ADFF749C51CE), SPH_C64(0x0D95DE399746DF03),
1067 SPH_C64(0x8FD1934127C79BCE), SPH_C64(0x9A255629FF352CB1),
1068 SPH_C64(0x5DB62599DF6CA7B0), SPH_C64(0xEABE394CA9D5C3F4),
1069 SPH_C64(0x991112C71A75B523), SPH_C64(0xAE18A40B660FCC33)
1076 sph_skein224_init(
void *cc)
1078 skein_small_init(cc,
IV224);
1083 sph_skein224(
void *cc,
const void *data,
size_t len)
1085 skein_small_core(cc, data, len);
1090 sph_skein224_close(
void *cc,
void *dst)
1092 sph_skein224_addbits_and_close(cc, 0, 0, dst);
1097 sph_skein224_addbits_and_close(
void *cc,
unsigned ub,
unsigned n,
void *dst)
1099 skein_small_close(cc, ub, n, dst, 28);
1100 sph_skein224_init(cc);
1105 sph_skein256_init(
void *cc)
1107 skein_small_init(cc,
IV256);
1112 sph_skein256(
void *cc,
const void *data,
size_t len)
1114 skein_small_core(cc, data, len);
1119 sph_skein256_close(
void *cc,
void *dst)
1121 sph_skein256_addbits_and_close(cc, 0, 0, dst);
1126 sph_skein256_addbits_and_close(
void *cc,
unsigned ub,
unsigned n,
void *dst)
1128 skein_small_close(cc, ub, n, dst, 32);
1129 sph_skein256_init(cc);
1135 sph_skein224_init(
void *cc)
1137 skein_big_init(cc,
IV224);
1142 sph_skein224(
void *cc,
const void *data,
size_t len)
1144 skein_big_core(cc, data, len);
1149 sph_skein224_close(
void *cc,
void *dst)
1151 sph_skein224_addbits_and_close(cc, 0, 0, dst);
1156 sph_skein224_addbits_and_close(
void *cc,
unsigned ub,
unsigned n,
void *dst)
1158 skein_big_close(cc, ub, n, dst, 28);
1159 sph_skein224_init(cc);
1164 sph_skein256_init(
void *cc)
1166 skein_big_init(cc,
IV256);
1171 sph_skein256(
void *cc,
const void *data,
size_t len)
1173 skein_big_core(cc, data, len);
1178 sph_skein256_close(
void *cc,
void *dst)
1180 sph_skein256_addbits_and_close(cc, 0, 0, dst);
1185 sph_skein256_addbits_and_close(
void *cc,
unsigned ub,
unsigned n,
void *dst)
1187 skein_big_close(cc, ub, n, dst, 32);
1188 sph_skein256_init(cc);
1193 sph_skein384_init(
void *cc)
1195 skein_big_init(cc,
IV384);
1200 sph_skein384(
void *cc,
const void *data,
size_t len)
1202 skein_big_core(cc, data, len);
1207 sph_skein384_close(
void *cc,
void *dst)
1209 sph_skein384_addbits_and_close(cc, 0, 0, dst);
1214 sph_skein384_addbits_and_close(
void *cc,
unsigned ub,
unsigned n,
void *dst)
1216 skein_big_close(cc, ub, n, dst, 48);
1217 sph_skein384_init(cc);
1222 sph_skein512_init(
void *cc)
1224 skein_big_init(cc,
IV512);
1229 sph_skein512(
void *cc,
const void *data,
size_t len)
1231 skein_big_core(cc, data, len);
1236 sph_skein512_close(
void *cc,
void *dst)
1238 sph_skein512_addbits_and_close(cc, 0, 0, dst);
1243 sph_skein512_addbits_and_close(
void *cc,
unsigned ub,
unsigned n,
void *dst)
1245 skein_big_close(cc, ub, n, dst, 64);
1246 sph_skein512_init(cc);
#define READ_STATE_BIG(sc)
static const sph_u32 IV224[8]
static const sph_u32 IV384[]
#define WRITE_STATE_BIG(sc)
static const sph_u32 IV256[8]
static const sph_u32 IV512[]
void * memcpy(void *a, const void *b, size_t c)
#define READ_STATE_SMALL(sc)
#define WRITE_STATE_SMALL(sc)