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