1 | /* |
---|
2 | 20080913 |
---|
3 | D. J. Bernstein |
---|
4 | Public domain. |
---|
5 | */ |
---|
6 | |
---|
7 | #include "sha512.h" |
---|
8 | |
---|
9 | extern int crypto_hashblocks(unsigned char *statebytes,const unsigned char *in,unsigned long long inlen); |
---|
10 | |
---|
11 | #define blocks crypto_hashblocks |
---|
12 | |
---|
13 | static const unsigned char iv[64] = { |
---|
14 | 0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08, |
---|
15 | 0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b, |
---|
16 | 0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b, |
---|
17 | 0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1, |
---|
18 | 0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1, |
---|
19 | 0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f, |
---|
20 | 0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b, |
---|
21 | 0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79 |
---|
22 | } ; |
---|
23 | |
---|
24 | typedef unsigned long long uint64; |
---|
25 | |
---|
26 | int crypto_hash_sha512(unsigned char *out,const unsigned char *in,unsigned long long inlen) |
---|
27 | { |
---|
28 | unsigned char h[64]; |
---|
29 | unsigned char padded[256]; |
---|
30 | int i; |
---|
31 | unsigned long long bytes = inlen; |
---|
32 | |
---|
33 | for (i = 0;i < 64;++i) h[i] = iv[i]; |
---|
34 | |
---|
35 | blocks(h,in,inlen); |
---|
36 | in += inlen; |
---|
37 | inlen &= 127; |
---|
38 | in -= inlen; |
---|
39 | |
---|
40 | for (i = 0;i < inlen;++i) padded[i] = in[i]; |
---|
41 | padded[inlen] = 0x80; |
---|
42 | |
---|
43 | if (inlen < 112) { |
---|
44 | for (i = inlen + 1;i < 119;++i) padded[i] = 0; |
---|
45 | padded[119] = bytes >> 61; |
---|
46 | padded[120] = bytes >> 53; |
---|
47 | padded[121] = bytes >> 45; |
---|
48 | padded[122] = bytes >> 37; |
---|
49 | padded[123] = bytes >> 29; |
---|
50 | padded[124] = bytes >> 21; |
---|
51 | padded[125] = bytes >> 13; |
---|
52 | padded[126] = bytes >> 5; |
---|
53 | padded[127] = bytes << 3; |
---|
54 | blocks(h,padded,128); |
---|
55 | } else { |
---|
56 | for (i = inlen + 1;i < 247;++i) padded[i] = 0; |
---|
57 | padded[247] = bytes >> 61; |
---|
58 | padded[248] = bytes >> 53; |
---|
59 | padded[249] = bytes >> 45; |
---|
60 | padded[250] = bytes >> 37; |
---|
61 | padded[251] = bytes >> 29; |
---|
62 | padded[252] = bytes >> 21; |
---|
63 | padded[253] = bytes >> 13; |
---|
64 | padded[254] = bytes >> 5; |
---|
65 | padded[255] = bytes << 3; |
---|
66 | blocks(h,padded,256); |
---|
67 | } |
---|
68 | |
---|
69 | for (i = 0;i < 64;++i) out[i] = h[i]; |
---|
70 | |
---|
71 | return 0; |
---|
72 | } |
---|