OLD | NEW |
1 /* | 1 /* |
2 * sha512.c - implementation of SHA224, SHA256, SHA384 and SHA512 | 2 * sha512.c - implementation of SHA224, SHA256, SHA384 and SHA512 |
3 * | 3 * |
4 * This Source Code Form is subject to the terms of the Mozilla Public | 4 * This Source Code Form is subject to the terms of the Mozilla Public |
5 * License, v. 2.0. If a copy of the MPL was not distributed with this | 5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
7 | 7 |
8 #ifdef FREEBL_NO_DEPEND | 8 #ifdef FREEBL_NO_DEPEND |
9 #include "stubs.h" | 9 #include "stubs.h" |
10 #endif | 10 #endif |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, | 60 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
61 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 | 61 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
62 }; | 62 }; |
63 | 63 |
64 /* SHA-256 initial hash values */ | 64 /* SHA-256 initial hash values */ |
65 static const PRUint32 H256[8] = { | 65 static const PRUint32 H256[8] = { |
66 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, | 66 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, |
67 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 | 67 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 |
68 }; | 68 }; |
69 | 69 |
| 70 #if defined(IS_LITTLE_ENDIAN) |
70 #if (_MSC_VER >= 1300) | 71 #if (_MSC_VER >= 1300) |
71 #include <stdlib.h> | 72 #include <stdlib.h> |
72 #pragma intrinsic(_byteswap_ulong) | 73 #pragma intrinsic(_byteswap_ulong) |
73 #define SHA_HTONL(x) _byteswap_ulong(x) | 74 #define SHA_HTONL(x) _byteswap_ulong(x) |
74 #define BYTESWAP4(x) x = SHA_HTONL(x) | |
75 #elif defined(_MSC_VER) && defined(NSS_X86_OR_X64) | 75 #elif defined(_MSC_VER) && defined(NSS_X86_OR_X64) |
76 #ifndef FORCEINLINE | 76 #ifndef FORCEINLINE |
77 #if (_MSC_VER >= 1200) | 77 #if (_MSC_VER >= 1200) |
78 #define FORCEINLINE __forceinline | 78 #define FORCEINLINE __forceinline |
79 #else | 79 #else |
80 #define FORCEINLINE __inline | 80 #define FORCEINLINE __inline |
81 #endif | 81 #endif |
82 #endif | 82 #endif |
83 #define FASTCALL __fastcall | 83 #define FASTCALL __fastcall |
84 | 84 |
85 static FORCEINLINE PRUint32 FASTCALL | 85 static FORCEINLINE PRUint32 FASTCALL |
86 swap4b(PRUint32 dwd) | 86 swap4b(PRUint32 dwd) |
87 { | 87 { |
88 __asm { | 88 __asm { |
89 mov eax,dwd | 89 mov eax,dwd |
90 bswap eax | 90 bswap eax |
91 } | 91 } |
92 } | 92 } |
93 | 93 |
94 #define SHA_HTONL(x) swap4b(x) | 94 #define SHA_HTONL(x) swap4b(x) |
95 #define BYTESWAP4(x) x = SHA_HTONL(x) | |
96 | 95 |
97 #elif defined(__GNUC__) && defined(NSS_X86_OR_X64) | 96 #elif defined(__GNUC__) && defined(NSS_X86_OR_X64) |
98 static __inline__ PRUint32 swap4b(PRUint32 value) | 97 static __inline__ PRUint32 swap4b(PRUint32 value) |
99 { | 98 { |
100 __asm__("bswap %0" : "+r" (value)); | 99 __asm__("bswap %0" : "+r" (value)); |
101 return (value); | 100 return (value); |
102 } | 101 } |
103 #define SHA_HTONL(x) swap4b(x) | 102 #define SHA_HTONL(x) swap4b(x) |
104 #define BYTESWAP4(x) x = SHA_HTONL(x) | |
105 | 103 |
106 #elif defined(__GNUC__) && (defined(__thumb2__) || \ | 104 #elif defined(__GNUC__) && (defined(__thumb2__) || \ |
107 (!defined(__thumb__) && \ | 105 (!defined(__thumb__) && \ |
108 (defined(__ARM_ARCH_6__) || \ | 106 (defined(__ARM_ARCH_6__) || \ |
109 defined(__ARM_ARCH_6J__) || \ | 107 defined(__ARM_ARCH_6J__) || \ |
110 defined(__ARM_ARCH_6K__) || \ | 108 defined(__ARM_ARCH_6K__) || \ |
111 defined(__ARM_ARCH_6Z__) || \ | 109 defined(__ARM_ARCH_6Z__) || \ |
112 defined(__ARM_ARCH_6ZK__) || \ | 110 defined(__ARM_ARCH_6ZK__) || \ |
113 defined(__ARM_ARCH_6T2__) || \ | 111 defined(__ARM_ARCH_6T2__) || \ |
114 defined(__ARM_ARCH_7__) || \ | 112 defined(__ARM_ARCH_7__) || \ |
115 defined(__ARM_ARCH_7A__) || \ | 113 defined(__ARM_ARCH_7A__) || \ |
116 defined(__ARM_ARCH_7R__)))) | 114 defined(__ARM_ARCH_7R__)))) |
117 static __inline__ PRUint32 swap4b(PRUint32 value) | 115 static __inline__ PRUint32 swap4b(PRUint32 value) |
118 { | 116 { |
119 PRUint32 ret; | 117 PRUint32 ret; |
120 __asm__("rev %0, %1" : "=r" (ret) : "r"(value)); | 118 __asm__("rev %0, %1" : "=r" (ret) : "r"(value)); |
121 return ret; | 119 return ret; |
122 } | 120 } |
123 #define SHA_HTONL(x) swap4b(x) | 121 #define SHA_HTONL(x) swap4b(x) |
124 #define BYTESWAP4(x) x = SHA_HTONL(x) | |
125 | 122 |
126 #else | 123 #else |
127 #define SWAP4MASK 0x00FF00FF | 124 #define SWAP4MASK 0x00FF00FF |
128 #define SHA_HTONL(x) (t1 = (x), t1 = (t1 << 16) | (t1 >> 16), \ | 125 static PRUint32 swap4b(PRUint32 value) |
129 ((t1 & SWAP4MASK) << 8) | ((t1 >> 8) & SWAP4MASK)) | 126 { |
130 #define BYTESWAP4(x) x = SHA_HTONL(x) | 127 PRUint32 t1 = (value << 16) | (value >> 16); |
| 128 return ((t1 & SWAP4MASK) << 8) | ((t1 >> 8) & SWAP4MASK); |
| 129 } |
| 130 #define SHA_HTONL(x) swap4b(x) |
131 #endif | 131 #endif |
| 132 #define BYTESWAP4(x) x = SHA_HTONL(x) |
| 133 #endif /* defined(IS_LITTLE_ENDIAN) */ |
132 | 134 |
133 #if defined(_MSC_VER) | 135 #if defined(_MSC_VER) |
134 #pragma intrinsic (_lrotr, _lrotl) | 136 #pragma intrinsic (_lrotr, _lrotl) |
135 #define ROTR32(x,n) _lrotr(x,n) | 137 #define ROTR32(x,n) _lrotr(x,n) |
136 #define ROTL32(x,n) _lrotl(x,n) | 138 #define ROTL32(x,n) _lrotl(x,n) |
137 #else | 139 #else |
138 #define ROTR32(x,n) ((x >> n) | (x << ((8 * sizeof x) - n))) | 140 #define ROTR32(x,n) ((x >> n) | (x << ((8 * sizeof x) - n))) |
139 #define ROTL32(x,n) ((x << n) | (x >> ((8 * sizeof x) - n))) | 141 #define ROTL32(x,n) ((x << n) | (x >> ((8 * sizeof x) - n))) |
140 #endif | 142 #endif |
141 | 143 |
142 /* Capitol Sigma and lower case sigma functions */ | 144 /* Capitol Sigma and lower case sigma functions */ |
143 #define S0(x) (ROTR32(x, 2) ^ ROTR32(x,13) ^ ROTR32(x,22)) | 145 #define S0(x) (ROTR32(x, 2) ^ ROTR32(x,13) ^ ROTR32(x,22)) |
144 #define S1(x) (ROTR32(x, 6) ^ ROTR32(x,11) ^ ROTR32(x,25)) | 146 #define S1(x) (ROTR32(x, 6) ^ ROTR32(x,11) ^ ROTR32(x,25)) |
145 #define s0(x) (t1 = x, ROTR32(t1, 7) ^ ROTR32(t1,18) ^ SHR(t1, 3)) | 147 #define s0(x) (ROTR32(x, 7) ^ ROTR32(x,18) ^ SHR(x, 3)) |
146 #define s1(x) (t2 = x, ROTR32(t2,17) ^ ROTR32(t2,19) ^ SHR(t2,10)) | 148 #define s1(x) (ROTR32(x,17) ^ ROTR32(x,19) ^ SHR(x,10)) |
147 | 149 |
148 SHA256Context * | 150 SHA256Context * |
149 SHA256_NewContext(void) | 151 SHA256_NewContext(void) |
150 { | 152 { |
151 SHA256Context *ctx = PORT_New(SHA256Context); | 153 SHA256Context *ctx = PORT_New(SHA256Context); |
152 return ctx; | 154 return ctx; |
153 } | 155 } |
154 | 156 |
155 void | 157 void |
156 SHA256_DestroyContext(SHA256Context *ctx, PRBool freeit) | 158 SHA256_DestroyContext(SHA256Context *ctx, PRBool freeit) |
157 { | 159 { |
158 memset(ctx, 0, sizeof *ctx); | 160 memset(ctx, 0, sizeof *ctx); |
159 if (freeit) { | 161 if (freeit) { |
160 PORT_Free(ctx); | 162 PORT_Free(ctx); |
161 } | 163 } |
162 } | 164 } |
163 | 165 |
164 void | 166 void |
165 SHA256_Begin(SHA256Context *ctx) | 167 SHA256_Begin(SHA256Context *ctx) |
166 { | 168 { |
167 memset(ctx, 0, sizeof *ctx); | 169 memset(ctx, 0, sizeof *ctx); |
168 memcpy(H, H256, sizeof H256); | 170 memcpy(H, H256, sizeof H256); |
169 } | 171 } |
170 | 172 |
171 static void | 173 static void |
172 SHA256_Compress(SHA256Context *ctx) | 174 SHA256_Compress(SHA256Context *ctx) |
173 { | 175 { |
174 { | 176 { |
175 register PRUint32 t1, t2; | |
176 | |
177 #if defined(IS_LITTLE_ENDIAN) | 177 #if defined(IS_LITTLE_ENDIAN) |
178 BYTESWAP4(W[0]); | 178 BYTESWAP4(W[0]); |
179 BYTESWAP4(W[1]); | 179 BYTESWAP4(W[1]); |
180 BYTESWAP4(W[2]); | 180 BYTESWAP4(W[2]); |
181 BYTESWAP4(W[3]); | 181 BYTESWAP4(W[3]); |
182 BYTESWAP4(W[4]); | 182 BYTESWAP4(W[4]); |
183 BYTESWAP4(W[5]); | 183 BYTESWAP4(W[5]); |
184 BYTESWAP4(W[6]); | 184 BYTESWAP4(W[6]); |
185 BYTESWAP4(W[7]); | 185 BYTESWAP4(W[7]); |
186 BYTESWAP4(W[8]); | 186 BYTESWAP4(W[8]); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 memcpy(B, input, inputLen); | 419 memcpy(B, input, inputLen); |
420 } | 420 } |
421 | 421 |
422 void | 422 void |
423 SHA256_End(SHA256Context *ctx, unsigned char *digest, | 423 SHA256_End(SHA256Context *ctx, unsigned char *digest, |
424 unsigned int *digestLen, unsigned int maxDigestLen) | 424 unsigned int *digestLen, unsigned int maxDigestLen) |
425 { | 425 { |
426 unsigned int inBuf = ctx->sizeLo & 0x3f; | 426 unsigned int inBuf = ctx->sizeLo & 0x3f; |
427 unsigned int padLen = (inBuf < 56) ? (56 - inBuf) : (56 + 64 - inBuf); | 427 unsigned int padLen = (inBuf < 56) ? (56 - inBuf) : (56 + 64 - inBuf); |
428 PRUint32 hi, lo; | 428 PRUint32 hi, lo; |
429 #ifdef SWAP4MASK | |
430 PRUint32 t1; | |
431 #endif | |
432 | 429 |
433 hi = (ctx->sizeHi << 3) | (ctx->sizeLo >> 29); | 430 hi = (ctx->sizeHi << 3) | (ctx->sizeLo >> 29); |
434 lo = (ctx->sizeLo << 3); | 431 lo = (ctx->sizeLo << 3); |
435 | 432 |
436 SHA256_Update(ctx, pad, padLen); | 433 SHA256_Update(ctx, pad, padLen); |
437 | 434 |
438 #if defined(IS_LITTLE_ENDIAN) | 435 #if defined(IS_LITTLE_ENDIAN) |
439 W[14] = SHA_HTONL(hi); | 436 W[14] = SHA_HTONL(hi); |
440 W[15] = SHA_HTONL(lo); | 437 W[15] = SHA_HTONL(lo); |
441 #else | 438 #else |
(...skipping 18 matching lines...) Expand all Loading... |
460 if (digestLen) | 457 if (digestLen) |
461 *digestLen = padLen; | 458 *digestLen = padLen; |
462 } | 459 } |
463 | 460 |
464 void | 461 void |
465 SHA256_EndRaw(SHA256Context *ctx, unsigned char *digest, | 462 SHA256_EndRaw(SHA256Context *ctx, unsigned char *digest, |
466 unsigned int *digestLen, unsigned int maxDigestLen) | 463 unsigned int *digestLen, unsigned int maxDigestLen) |
467 { | 464 { |
468 PRUint32 h[8]; | 465 PRUint32 h[8]; |
469 unsigned int len; | 466 unsigned int len; |
470 #ifdef SWAP4MASK | |
471 PRUint32 t1; | |
472 #endif | |
473 | 467 |
474 memcpy(h, ctx->h, sizeof(h)); | 468 memcpy(h, ctx->h, sizeof(h)); |
475 | 469 |
476 #if defined(IS_LITTLE_ENDIAN) | 470 #if defined(IS_LITTLE_ENDIAN) |
477 BYTESWAP4(h[0]); | 471 BYTESWAP4(h[0]); |
478 BYTESWAP4(h[1]); | 472 BYTESWAP4(h[1]); |
479 BYTESWAP4(h[2]); | 473 BYTESWAP4(h[2]); |
480 BYTESWAP4(h[3]); | 474 BYTESWAP4(h[3]); |
481 BYTESWAP4(h[4]); | 475 BYTESWAP4(h[4]); |
482 BYTESWAP4(h[5]); | 476 BYTESWAP4(h[5]); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 #pragma intrinsic(_rotr64,_rotl64) | 641 #pragma intrinsic(_rotr64,_rotl64) |
648 #define ROTR64(x,n) _rotr64(x,n) | 642 #define ROTR64(x,n) _rotr64(x,n) |
649 #define ROTL64(x,n) _rotl64(x,n) | 643 #define ROTL64(x,n) _rotl64(x,n) |
650 #else | 644 #else |
651 #define ROTR64(x,n) ((x >> n) | (x << (64 - n))) | 645 #define ROTR64(x,n) ((x >> n) | (x << (64 - n))) |
652 #define ROTL64(x,n) ((x << n) | (x >> (64 - n))) | 646 #define ROTL64(x,n) ((x << n) | (x >> (64 - n))) |
653 #endif | 647 #endif |
654 | 648 |
655 #define S0(x) (ROTR64(x,28) ^ ROTR64(x,34) ^ ROTR64(x,39)) | 649 #define S0(x) (ROTR64(x,28) ^ ROTR64(x,34) ^ ROTR64(x,39)) |
656 #define S1(x) (ROTR64(x,14) ^ ROTR64(x,18) ^ ROTR64(x,41)) | 650 #define S1(x) (ROTR64(x,14) ^ ROTR64(x,18) ^ ROTR64(x,41)) |
657 #define s0(x) (t1 = x, ROTR64(t1, 1) ^ ROTR64(t1, 8) ^ SHR(t1,7)) | 651 #define s0(x) (ROTR64(x, 1) ^ ROTR64(x, 8) ^ SHR(x,7)) |
658 #define s1(x) (t2 = x, ROTR64(t2,19) ^ ROTR64(t2,61) ^ SHR(t2,6)) | 652 #define s1(x) (ROTR64(x,19) ^ ROTR64(x,61) ^ SHR(x,6)) |
659 | 653 |
660 #if PR_BYTES_PER_LONG == 8 | 654 #if PR_BYTES_PER_LONG == 8 |
661 #define ULLC(hi,lo) 0x ## hi ## lo ## UL | 655 #define ULLC(hi,lo) 0x ## hi ## lo ## UL |
662 #elif defined(_MSC_VER) | 656 #elif defined(_MSC_VER) |
663 #define ULLC(hi,lo) 0x ## hi ## lo ## ui64 | 657 #define ULLC(hi,lo) 0x ## hi ## lo ## ui64 |
664 #else | 658 #else |
665 #define ULLC(hi,lo) 0x ## hi ## lo ## ULL | 659 #define ULLC(hi,lo) 0x ## hi ## lo ## ULL |
666 #endif | 660 #endif |
667 | 661 |
| 662 #if defined(IS_LITTLE_ENDIAN) |
668 #if defined(_MSC_VER) | 663 #if defined(_MSC_VER) |
669 #pragma intrinsic(_byteswap_uint64) | 664 #pragma intrinsic(_byteswap_uint64) |
670 #define SHA_HTONLL(x) _byteswap_uint64(x) | 665 #define SHA_HTONLL(x) _byteswap_uint64(x) |
671 | 666 |
672 #elif defined(__GNUC__) && (defined(__x86_64__) || defined(__x86_64)) | 667 #elif defined(__GNUC__) && (defined(__x86_64__) || defined(__x86_64)) |
673 static __inline__ PRUint64 swap8b(PRUint64 value) | 668 static __inline__ PRUint64 swap8b(PRUint64 value) |
674 { | 669 { |
675 __asm__("bswapq %0" : "+r" (value)); | 670 __asm__("bswapq %0" : "+r" (value)); |
676 return (value); | 671 return (value); |
677 } | 672 } |
678 #define SHA_HTONLL(x) swap8b(x) | 673 #define SHA_HTONLL(x) swap8b(x) |
679 | 674 |
680 #else | 675 #else |
681 #define SHA_MASK16 ULLC(0000FFFF,0000FFFF) | 676 #define SHA_MASK16 ULLC(0000FFFF,0000FFFF) |
682 #define SHA_MASK8 ULLC(00FF00FF,00FF00FF) | 677 #define SHA_MASK8 ULLC(00FF00FF,00FF00FF) |
683 #define SHA_HTONLL(x) (t1 = x, \ | 678 static PRUint64 swap8b(PRUint64 x) |
684 t1 = ((t1 & SHA_MASK8 ) << 8) | ((t1 >> 8) & SHA_MASK8 ), \ | 679 { |
685 t1 = ((t1 & SHA_MASK16) << 16) | ((t1 >> 16) & SHA_MASK16), \ | 680 PRUint64 t1 = x; |
686 (t1 >> 32) | (t1 << 32)) | 681 t1 = ((t1 & SHA_MASK8 ) << 8) | ((t1 >> 8) & SHA_MASK8 ); |
| 682 t1 = ((t1 & SHA_MASK16) << 16) | ((t1 >> 16) & SHA_MASK16); |
| 683 return (t1 >> 32) | (t1 << 32); |
| 684 } |
| 685 #define SHA_HTONLL(x) swap8b(x) |
687 #endif | 686 #endif |
688 #define BYTESWAP8(x) x = SHA_HTONLL(x) | 687 #define BYTESWAP8(x) x = SHA_HTONLL(x) |
| 688 #endif /* defined(IS_LITTLE_ENDIAN) */ |
689 | 689 |
690 #else /* no long long */ | 690 #else /* no long long */ |
691 | 691 |
692 #if defined(IS_LITTLE_ENDIAN) | 692 #if defined(IS_LITTLE_ENDIAN) |
693 #define ULLC(hi,lo) { 0x ## lo ## U, 0x ## hi ## U } | 693 #define ULLC(hi,lo) { 0x ## lo ## U, 0x ## hi ## U } |
| 694 #define SHA_HTONLL(x) ( BYTESWAP4(x.lo), BYTESWAP4(x.hi), \ |
| 695 x.hi ^= x.lo ^= x.hi ^= x.lo, x) |
| 696 #define BYTESWAP8(x) do { PRUint32 tmp; BYTESWAP4(x.lo); BYTESWAP4(x.hi); \ |
| 697 tmp = x.lo; x.lo = x.hi; x.hi = tmp; } while (0) |
694 #else | 698 #else |
695 #define ULLC(hi,lo) { 0x ## hi ## U, 0x ## lo ## U } | 699 #define ULLC(hi,lo) { 0x ## hi ## U, 0x ## lo ## U } |
696 #endif | 700 #endif |
697 | 701 |
698 #define SHA_HTONLL(x) ( BYTESWAP4(x.lo), BYTESWAP4(x.hi), \ | |
699 x.hi ^= x.lo ^= x.hi ^= x.lo, x) | |
700 #define BYTESWAP8(x) do { PRUint32 tmp; BYTESWAP4(x.lo); BYTESWAP4(x.hi); \ | |
701 tmp = x.lo; x.lo = x.hi; x.hi = tmp; } while (0) | |
702 #endif | 702 #endif |
703 | 703 |
704 /* SHA-384 and SHA-512 constants, K512. */ | 704 /* SHA-384 and SHA-512 constants, K512. */ |
705 static const PRUint64 K512[80] = { | 705 static const PRUint64 K512[80] = { |
706 #if PR_BYTES_PER_LONG == 8 | 706 #if PR_BYTES_PER_LONG == 8 |
707 0x428a2f98d728ae22UL , 0x7137449123ef65cdUL , | 707 0x428a2f98d728ae22UL , 0x7137449123ef65cdUL , |
708 0xb5c0fbcfec4d3b2fUL , 0xe9b5dba58189dbbcUL , | 708 0xb5c0fbcfec4d3b2fUL , 0xe9b5dba58189dbbcUL , |
709 0x3956c25bf348b538UL , 0x59f111f1b605d019UL , | 709 0x3956c25bf348b538UL , 0x59f111f1b605d019UL , |
710 0x923f82a4af194f9bUL , 0xab1c5ed5da6d8118UL , | 710 0x923f82a4af194f9bUL , 0xab1c5ed5da6d8118UL , |
711 0xd807aa98a3030242UL , 0x12835b0145706fbeUL , | 711 0xd807aa98a3030242UL , 0x12835b0145706fbeUL , |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 h.hi += cy + S0hi(a) + Majx(a,b,c,hi); \ | 920 h.hi += cy + S0hi(a) + Majx(a,b,c,hi); \ |
921 DUMP(n,a,d,e,h) \ | 921 DUMP(n,a,d,e,h) \ |
922 } | 922 } |
923 #endif | 923 #endif |
924 | 924 |
925 static void | 925 static void |
926 SHA512_Compress(SHA512Context *ctx) | 926 SHA512_Compress(SHA512Context *ctx) |
927 { | 927 { |
928 #if defined(IS_LITTLE_ENDIAN) | 928 #if defined(IS_LITTLE_ENDIAN) |
929 { | 929 { |
930 #if defined(HAVE_LONG_LONG) | |
931 PRUint64 t1; | |
932 #else | |
933 PRUint32 t1; | |
934 #endif | |
935 BYTESWAP8(W[0]); | 930 BYTESWAP8(W[0]); |
936 BYTESWAP8(W[1]); | 931 BYTESWAP8(W[1]); |
937 BYTESWAP8(W[2]); | 932 BYTESWAP8(W[2]); |
938 BYTESWAP8(W[3]); | 933 BYTESWAP8(W[3]); |
939 BYTESWAP8(W[4]); | 934 BYTESWAP8(W[4]); |
940 BYTESWAP8(W[5]); | 935 BYTESWAP8(W[5]); |
941 BYTESWAP8(W[6]); | 936 BYTESWAP8(W[6]); |
942 BYTESWAP8(W[7]); | 937 BYTESWAP8(W[7]); |
943 BYTESWAP8(W[8]); | 938 BYTESWAP8(W[8]); |
944 BYTESWAP8(W[9]); | 939 BYTESWAP8(W[9]); |
945 BYTESWAP8(W[10]); | 940 BYTESWAP8(W[10]); |
946 BYTESWAP8(W[11]); | 941 BYTESWAP8(W[11]); |
947 BYTESWAP8(W[12]); | 942 BYTESWAP8(W[12]); |
948 BYTESWAP8(W[13]); | 943 BYTESWAP8(W[13]); |
949 BYTESWAP8(W[14]); | 944 BYTESWAP8(W[14]); |
950 BYTESWAP8(W[15]); | 945 BYTESWAP8(W[15]); |
951 } | 946 } |
952 #endif | 947 #endif |
953 | 948 |
954 { | 949 { |
955 PRUint64 t1, t2; | |
956 #ifdef NOUNROLL512 | 950 #ifdef NOUNROLL512 |
957 { | 951 { |
958 /* prepare the "message schedule" */ | 952 /* prepare the "message schedule" */ |
959 int t; | 953 int t; |
960 for (t = 16; t < 80; ++t) { | 954 for (t = 16; t < 80; ++t) { |
961 INITW(t); | 955 INITW(t); |
962 } | 956 } |
963 } | 957 } |
964 #else | 958 #else |
965 INITW(16); | 959 INITW(16); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1216 if (inputLen) | 1210 if (inputLen) |
1217 memcpy(B, input, inputLen); | 1211 memcpy(B, input, inputLen); |
1218 } | 1212 } |
1219 | 1213 |
1220 void | 1214 void |
1221 SHA512_End(SHA512Context *ctx, unsigned char *digest, | 1215 SHA512_End(SHA512Context *ctx, unsigned char *digest, |
1222 unsigned int *digestLen, unsigned int maxDigestLen) | 1216 unsigned int *digestLen, unsigned int maxDigestLen) |
1223 { | 1217 { |
1224 #if defined(HAVE_LONG_LONG) | 1218 #if defined(HAVE_LONG_LONG) |
1225 unsigned int inBuf = (unsigned int)ctx->sizeLo & 0x7f; | 1219 unsigned int inBuf = (unsigned int)ctx->sizeLo & 0x7f; |
1226 PRUint64 t1; | |
1227 #else | 1220 #else |
1228 unsigned int inBuf = (unsigned int)ctx->sizeLo.lo & 0x7f; | 1221 unsigned int inBuf = (unsigned int)ctx->sizeLo.lo & 0x7f; |
1229 PRUint32 t1; | |
1230 #endif | 1222 #endif |
1231 unsigned int padLen = (inBuf < 112) ? (112 - inBuf) : (112 + 128 - inBuf); | 1223 unsigned int padLen = (inBuf < 112) ? (112 - inBuf) : (112 + 128 - inBuf); |
1232 PRUint64 lo; | 1224 PRUint64 lo; |
1233 LL_SHL(lo, ctx->sizeLo, 3); | 1225 LL_SHL(lo, ctx->sizeLo, 3); |
1234 | 1226 |
1235 SHA512_Update(ctx, pad, padLen); | 1227 SHA512_Update(ctx, pad, padLen); |
1236 | 1228 |
1237 #if defined(HAVE_LONG_LONG) | 1229 #if defined(HAVE_LONG_LONG) |
1238 W[14] = 0; | 1230 W[14] = 0; |
1239 #else | 1231 #else |
(...skipping 21 matching lines...) Expand all Loading... |
1261 padLen = PR_MIN(SHA512_LENGTH, maxDigestLen); | 1253 padLen = PR_MIN(SHA512_LENGTH, maxDigestLen); |
1262 memcpy(digest, H, padLen); | 1254 memcpy(digest, H, padLen); |
1263 if (digestLen) | 1255 if (digestLen) |
1264 *digestLen = padLen; | 1256 *digestLen = padLen; |
1265 } | 1257 } |
1266 | 1258 |
1267 void | 1259 void |
1268 SHA512_EndRaw(SHA512Context *ctx, unsigned char *digest, | 1260 SHA512_EndRaw(SHA512Context *ctx, unsigned char *digest, |
1269 unsigned int *digestLen, unsigned int maxDigestLen) | 1261 unsigned int *digestLen, unsigned int maxDigestLen) |
1270 { | 1262 { |
1271 #if defined(HAVE_LONG_LONG) | |
1272 PRUint64 t1; | |
1273 #else | |
1274 PRUint32 t1; | |
1275 #endif | |
1276 PRUint64 h[8]; | 1263 PRUint64 h[8]; |
1277 unsigned int len; | 1264 unsigned int len; |
1278 | 1265 |
1279 memcpy(h, ctx->h, sizeof(h)); | 1266 memcpy(h, ctx->h, sizeof(h)); |
1280 | 1267 |
1281 #if defined(IS_LITTLE_ENDIAN) | 1268 #if defined(IS_LITTLE_ENDIAN) |
1282 BYTESWAP8(h[0]); | 1269 BYTESWAP8(h[0]); |
1283 BYTESWAP8(h[1]); | 1270 BYTESWAP8(h[1]); |
1284 BYTESWAP8(h[2]); | 1271 BYTESWAP8(h[2]); |
1285 BYTESWAP8(h[3]); | 1272 BYTESWAP8(h[3]); |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1589 } | 1576 } |
1590 printf("done\n"); | 1577 printf("done\n"); |
1591 } | 1578 } |
1592 return 0; | 1579 return 0; |
1593 } | 1580 } |
1594 | 1581 |
1595 void *PORT_Alloc(size_t len) { return malloc(len); } | 1582 void *PORT_Alloc(size_t len) { return malloc(len); } |
1596 void PORT_Free(void *ptr) { free(ptr); } | 1583 void PORT_Free(void *ptr) { free(ptr); } |
1597 void PORT_ZFree(void *ptr, size_t len) { memset(ptr, 0, len); free(ptr); } | 1584 void PORT_ZFree(void *ptr, size_t len) { memset(ptr, 0, len); free(ptr); } |
1598 #endif | 1585 #endif |
OLD | NEW |