diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-02 09:53:34 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-02 09:53:34 -0400 |
commit | e2967396ac73cb7410787886cdaf072a184ffc49 (patch) | |
tree | 527a74d25a4a0705fc641994fd35bfab22662034 /nacl/crypto_hash | |
parent | 8928c817df345f29aa0b194743595aa11bd6a8ba (diff) |
Added NaCl crypto library.
Diffstat (limited to 'nacl/crypto_hash')
-rw-r--r-- | nacl/crypto_hash/measure.c | 66 | ||||
-rw-r--r-- | nacl/crypto_hash/sha256/checksum | 1 | ||||
-rw-r--r-- | nacl/crypto_hash/sha256/ref/api.h | 1 | ||||
-rw-r--r-- | nacl/crypto_hash/sha256/ref/hash.c | 69 | ||||
-rw-r--r-- | nacl/crypto_hash/sha256/ref/implementors | 1 | ||||
-rw-r--r-- | nacl/crypto_hash/sha256/used | 0 | ||||
-rw-r--r-- | nacl/crypto_hash/sha512/checksum | 1 | ||||
-rw-r--r-- | nacl/crypto_hash/sha512/ref/api.h | 1 | ||||
-rw-r--r-- | nacl/crypto_hash/sha512/ref/hash.c | 71 | ||||
-rw-r--r-- | nacl/crypto_hash/sha512/ref/implementors | 1 | ||||
-rw-r--r-- | nacl/crypto_hash/sha512/selected | 0 | ||||
-rw-r--r-- | nacl/crypto_hash/sha512/used | 0 | ||||
-rw-r--r-- | nacl/crypto_hash/try.c | 77 | ||||
-rw-r--r-- | nacl/crypto_hash/wrapper-hash.cpp | 10 |
14 files changed, 299 insertions, 0 deletions
diff --git a/nacl/crypto_hash/measure.c b/nacl/crypto_hash/measure.c new file mode 100644 index 00000000..cec0404d --- /dev/null +++ b/nacl/crypto_hash/measure.c | |||
@@ -0,0 +1,66 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include "randombytes.h" | ||
3 | #include "cpucycles.h" | ||
4 | #include "crypto_hash.h" | ||
5 | |||
6 | extern void printentry(long long,const char *,long long *,long long); | ||
7 | extern unsigned char *alignedcalloc(unsigned long long); | ||
8 | extern const char *primitiveimplementation; | ||
9 | extern const char *implementationversion; | ||
10 | extern const char *sizenames[]; | ||
11 | extern const long long sizes[]; | ||
12 | extern void allocate(void); | ||
13 | extern void measure(void); | ||
14 | |||
15 | const char *primitiveimplementation = crypto_hash_IMPLEMENTATION; | ||
16 | const char *implementationversion = crypto_hash_VERSION; | ||
17 | const char *sizenames[] = { "outputbytes", 0 }; | ||
18 | const long long sizes[] = { crypto_hash_BYTES }; | ||
19 | |||
20 | #define MAXTEST_BYTES 4096 | ||
21 | #ifdef SUPERCOP | ||
22 | #define MGAP 8192 | ||
23 | #else | ||
24 | #define MGAP 8 | ||
25 | #endif | ||
26 | |||
27 | static unsigned char *h; | ||
28 | static unsigned char *m; | ||
29 | |||
30 | void preallocate(void) | ||
31 | { | ||
32 | } | ||
33 | |||
34 | void allocate(void) | ||
35 | { | ||
36 | h = alignedcalloc(crypto_hash_BYTES); | ||
37 | m = alignedcalloc(MAXTEST_BYTES); | ||
38 | } | ||
39 | |||
40 | #define TIMINGS 15 | ||
41 | static long long cycles[TIMINGS + 1]; | ||
42 | |||
43 | static void printcycles(long long mlen) | ||
44 | { | ||
45 | int i; | ||
46 | for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i]; | ||
47 | printentry(mlen,"cycles",cycles,TIMINGS); | ||
48 | } | ||
49 | |||
50 | void measure(void) | ||
51 | { | ||
52 | int i; | ||
53 | int loop; | ||
54 | int mlen; | ||
55 | |||
56 | for (loop = 0;loop < LOOPS;++loop) { | ||
57 | for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / MGAP) { | ||
58 | randombytes(m,mlen); | ||
59 | for (i = 0;i <= TIMINGS;++i) { | ||
60 | cycles[i] = cpucycles(); | ||
61 | crypto_hash(h,m,mlen); | ||
62 | } | ||
63 | printcycles(mlen); | ||
64 | } | ||
65 | } | ||
66 | } | ||
diff --git a/nacl/crypto_hash/sha256/checksum b/nacl/crypto_hash/sha256/checksum new file mode 100644 index 00000000..ee52aa30 --- /dev/null +++ b/nacl/crypto_hash/sha256/checksum | |||
@@ -0,0 +1 @@ | |||
86df8bd202b2a2b5fdc04a7f50a591e43a345849c12fef08d487109648a08e05 | |||
diff --git a/nacl/crypto_hash/sha256/ref/api.h b/nacl/crypto_hash/sha256/ref/api.h new file mode 100644 index 00000000..ae8c7f6a --- /dev/null +++ b/nacl/crypto_hash/sha256/ref/api.h | |||
@@ -0,0 +1 @@ | |||
#define CRYPTO_BYTES 32 | |||
diff --git a/nacl/crypto_hash/sha256/ref/hash.c b/nacl/crypto_hash/sha256/ref/hash.c new file mode 100644 index 00000000..21ce68a0 --- /dev/null +++ b/nacl/crypto_hash/sha256/ref/hash.c | |||
@@ -0,0 +1,69 @@ | |||
1 | /* | ||
2 | 20080913 | ||
3 | D. J. Bernstein | ||
4 | Public domain. | ||
5 | */ | ||
6 | |||
7 | #include "crypto_hashblocks_sha256.h" | ||
8 | #include "crypto_hash.h" | ||
9 | |||
10 | #define blocks crypto_hashblocks_sha256 | ||
11 | |||
12 | typedef unsigned int uint32; | ||
13 | |||
14 | static const char iv[32] = { | ||
15 | 0x6a,0x09,0xe6,0x67, | ||
16 | 0xbb,0x67,0xae,0x85, | ||
17 | 0x3c,0x6e,0xf3,0x72, | ||
18 | 0xa5,0x4f,0xf5,0x3a, | ||
19 | 0x51,0x0e,0x52,0x7f, | ||
20 | 0x9b,0x05,0x68,0x8c, | ||
21 | 0x1f,0x83,0xd9,0xab, | ||
22 | 0x5b,0xe0,0xcd,0x19, | ||
23 | } ; | ||
24 | |||
25 | int crypto_hash(unsigned char *out,const unsigned char *in,unsigned long long inlen) | ||
26 | { | ||
27 | unsigned char h[32]; | ||
28 | unsigned char padded[128]; | ||
29 | int i; | ||
30 | unsigned long long bits = inlen << 3; | ||
31 | |||
32 | for (i = 0;i < 32;++i) h[i] = iv[i]; | ||
33 | |||
34 | blocks(h,in,inlen); | ||
35 | in += inlen; | ||
36 | inlen &= 63; | ||
37 | in -= inlen; | ||
38 | |||
39 | for (i = 0;i < inlen;++i) padded[i] = in[i]; | ||
40 | padded[inlen] = 0x80; | ||
41 | |||
42 | if (inlen < 56) { | ||
43 | for (i = inlen + 1;i < 56;++i) padded[i] = 0; | ||
44 | padded[56] = bits >> 56; | ||
45 | padded[57] = bits >> 48; | ||
46 | padded[58] = bits >> 40; | ||
47 | padded[59] = bits >> 32; | ||
48 | padded[60] = bits >> 24; | ||
49 | padded[61] = bits >> 16; | ||
50 | padded[62] = bits >> 8; | ||
51 | padded[63] = bits; | ||
52 | blocks(h,padded,64); | ||
53 | } else { | ||
54 | for (i = inlen + 1;i < 120;++i) padded[i] = 0; | ||
55 | padded[120] = bits >> 56; | ||
56 | padded[121] = bits >> 48; | ||
57 | padded[122] = bits >> 40; | ||
58 | padded[123] = bits >> 32; | ||
59 | padded[124] = bits >> 24; | ||
60 | padded[125] = bits >> 16; | ||
61 | padded[126] = bits >> 8; | ||
62 | padded[127] = bits; | ||
63 | blocks(h,padded,128); | ||
64 | } | ||
65 | |||
66 | for (i = 0;i < 32;++i) out[i] = h[i]; | ||
67 | |||
68 | return 0; | ||
69 | } | ||
diff --git a/nacl/crypto_hash/sha256/ref/implementors b/nacl/crypto_hash/sha256/ref/implementors new file mode 100644 index 00000000..962e7d8e --- /dev/null +++ b/nacl/crypto_hash/sha256/ref/implementors | |||
@@ -0,0 +1 @@ | |||
Daniel J. Bernstein (wrapper around crypto_hashblocks/sha256) | |||
diff --git a/nacl/crypto_hash/sha256/used b/nacl/crypto_hash/sha256/used new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/nacl/crypto_hash/sha256/used | |||
diff --git a/nacl/crypto_hash/sha512/checksum b/nacl/crypto_hash/sha512/checksum new file mode 100644 index 00000000..edf714e9 --- /dev/null +++ b/nacl/crypto_hash/sha512/checksum | |||
@@ -0,0 +1 @@ | |||
9a2a989e136a02c3362c98e6e1e0b52fab980a1dafbebe4dd5e44d15d061742e35fb686befd4e33c608d251c96e26c020f90d92bb7ec8a657f79bb8e0b00a473 | |||
diff --git a/nacl/crypto_hash/sha512/ref/api.h b/nacl/crypto_hash/sha512/ref/api.h new file mode 100644 index 00000000..de9380d7 --- /dev/null +++ b/nacl/crypto_hash/sha512/ref/api.h | |||
@@ -0,0 +1 @@ | |||
#define CRYPTO_BYTES 64 | |||
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 | /* | ||
2 | 20080913 | ||
3 | D. J. Bernstein | ||
4 | Public domain. | ||
5 | */ | ||
6 | |||
7 | #include "crypto_hashblocks_sha512.h" | ||
8 | #include "crypto_hash.h" | ||
9 | |||
10 | #define blocks crypto_hashblocks_sha512 | ||
11 | |||
12 | static 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 | |||
23 | typedef unsigned long long uint64; | ||
24 | |||
25 | int 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 | } | ||
diff --git a/nacl/crypto_hash/sha512/ref/implementors b/nacl/crypto_hash/sha512/ref/implementors new file mode 100644 index 00000000..40afca09 --- /dev/null +++ b/nacl/crypto_hash/sha512/ref/implementors | |||
@@ -0,0 +1 @@ | |||
Daniel J. Bernstein (wrapper around crypto_hashblocks/sha512) | |||
diff --git a/nacl/crypto_hash/sha512/selected b/nacl/crypto_hash/sha512/selected new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/nacl/crypto_hash/sha512/selected | |||
diff --git a/nacl/crypto_hash/sha512/used b/nacl/crypto_hash/sha512/used new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/nacl/crypto_hash/sha512/used | |||
diff --git a/nacl/crypto_hash/try.c b/nacl/crypto_hash/try.c new file mode 100644 index 00000000..fab49c99 --- /dev/null +++ b/nacl/crypto_hash/try.c | |||
@@ -0,0 +1,77 @@ | |||
1 | /* | ||
2 | * crypto_hash/try.c version 20090118 | ||
3 | * D. J. Bernstein | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <stdlib.h> | ||
8 | #include "crypto_hash.h" | ||
9 | |||
10 | extern unsigned char *alignedcalloc(unsigned long long); | ||
11 | |||
12 | const char *primitiveimplementation = crypto_hash_IMPLEMENTATION; | ||
13 | |||
14 | #define MAXTEST_BYTES (10000 + crypto_hash_BYTES) | ||
15 | #define CHECKSUM_BYTES 4096 | ||
16 | #define TUNE_BYTES 1536 | ||
17 | |||
18 | static unsigned char *h; | ||
19 | static unsigned char *h2; | ||
20 | static unsigned char *m; | ||
21 | static unsigned char *m2; | ||
22 | |||
23 | void preallocate(void) | ||
24 | { | ||
25 | } | ||
26 | |||
27 | void allocate(void) | ||
28 | { | ||
29 | h = alignedcalloc(crypto_hash_BYTES); | ||
30 | h2 = alignedcalloc(crypto_hash_BYTES); | ||
31 | m = alignedcalloc(MAXTEST_BYTES); | ||
32 | m2 = alignedcalloc(MAXTEST_BYTES); | ||
33 | } | ||
34 | |||
35 | void predoit(void) | ||
36 | { | ||
37 | } | ||
38 | |||
39 | void doit(void) | ||
40 | { | ||
41 | crypto_hash(h,m,TUNE_BYTES); | ||
42 | } | ||
43 | |||
44 | char checksum[crypto_hash_BYTES * 2 + 1]; | ||
45 | |||
46 | const char *checksum_compute(void) | ||
47 | { | ||
48 | long long i; | ||
49 | long long j; | ||
50 | |||
51 | for (i = 0;i < CHECKSUM_BYTES;++i) { | ||
52 | long long hlen = crypto_hash_BYTES; | ||
53 | long long mlen = i; | ||
54 | for (j = -16;j < 0;++j) h[j] = random(); | ||
55 | for (j = hlen;j < hlen + 16;++j) h[j] = random(); | ||
56 | for (j = -16;j < hlen + 16;++j) h2[j] = h[j]; | ||
57 | for (j = -16;j < 0;++j) m[j] = random(); | ||
58 | for (j = mlen;j < mlen + 16;++j) m[j] = random(); | ||
59 | for (j = -16;j < mlen + 16;++j) m2[j] = m[j]; | ||
60 | if (crypto_hash(h,m,mlen) != 0) return "crypto_hash returns nonzero"; | ||
61 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_hash writes to input"; | ||
62 | for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_hash writes before output"; | ||
63 | for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_hash writes after output"; | ||
64 | if (crypto_hash(m2,m2,mlen) != 0) return "crypto_hash returns nonzero"; | ||
65 | for (j = 0;j < hlen;++j) if (m2[j] != h[j]) return "crypto_hash does not handle overlap"; | ||
66 | for (j = 0;j < mlen;++j) m[j] ^= h[j % hlen]; | ||
67 | m[mlen] = h[0]; | ||
68 | } | ||
69 | if (crypto_hash(h,m,CHECKSUM_BYTES) != 0) return "crypto_hash returns nonzero"; | ||
70 | |||
71 | for (i = 0;i < crypto_hash_BYTES;++i) { | ||
72 | checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)]; | ||
73 | checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]]; | ||
74 | } | ||
75 | checksum[2 * i] = 0; | ||
76 | return 0; | ||
77 | } | ||
diff --git a/nacl/crypto_hash/wrapper-hash.cpp b/nacl/crypto_hash/wrapper-hash.cpp new file mode 100644 index 00000000..4c0fb590 --- /dev/null +++ b/nacl/crypto_hash/wrapper-hash.cpp | |||
@@ -0,0 +1,10 @@ | |||
1 | #include <string> | ||
2 | using std::string; | ||
3 | #include "crypto_hash.h" | ||
4 | |||
5 | string crypto_hash(const string &m) | ||
6 | { | ||
7 | unsigned char h[crypto_hash_BYTES]; | ||
8 | crypto_hash(h,(const unsigned char *) m.c_str(),m.size()); | ||
9 | return string((char *) h,sizeof h); | ||
10 | } | ||