summaryrefslogtreecommitdiff
path: root/nacl/crypto_onetimeauth/poly1305/ref/auth.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-02 09:53:34 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-02 09:53:34 -0400
commite2967396ac73cb7410787886cdaf072a184ffc49 (patch)
tree527a74d25a4a0705fc641994fd35bfab22662034 /nacl/crypto_onetimeauth/poly1305/ref/auth.c
parent8928c817df345f29aa0b194743595aa11bd6a8ba (diff)
Added NaCl crypto library.
Diffstat (limited to 'nacl/crypto_onetimeauth/poly1305/ref/auth.c')
-rw-r--r--nacl/crypto_onetimeauth/poly1305/ref/auth.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/nacl/crypto_onetimeauth/poly1305/ref/auth.c b/nacl/crypto_onetimeauth/poly1305/ref/auth.c
new file mode 100644
index 00000000..06cf115d
--- /dev/null
+++ b/nacl/crypto_onetimeauth/poly1305/ref/auth.c
@@ -0,0 +1,104 @@
1/*
220080912
3D. J. Bernstein
4Public domain.
5*/
6
7#include "crypto_onetimeauth.h"
8
9static void add(unsigned int h[17],const unsigned int c[17])
10{
11 unsigned int j;
12 unsigned int u;
13 u = 0;
14 for (j = 0;j < 17;++j) { u += h[j] + c[j]; h[j] = u & 255; u >>= 8; }
15}
16
17static void squeeze(unsigned int h[17])
18{
19 unsigned int j;
20 unsigned int u;
21 u = 0;
22 for (j = 0;j < 16;++j) { u += h[j]; h[j] = u & 255; u >>= 8; }
23 u += h[16]; h[16] = u & 3;
24 u = 5 * (u >> 2);
25 for (j = 0;j < 16;++j) { u += h[j]; h[j] = u & 255; u >>= 8; }
26 u += h[16]; h[16] = u;
27}
28
29static const unsigned int minusp[17] = {
30 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252
31} ;
32
33static void freeze(unsigned int h[17])
34{
35 unsigned int horig[17];
36 unsigned int j;
37 unsigned int negative;
38 for (j = 0;j < 17;++j) horig[j] = h[j];
39 add(h,minusp);
40 negative = -(h[16] >> 7);
41 for (j = 0;j < 17;++j) h[j] ^= negative & (horig[j] ^ h[j]);
42}
43
44static void mulmod(unsigned int h[17],const unsigned int r[17])
45{
46 unsigned int hr[17];
47 unsigned int i;
48 unsigned int j;
49 unsigned int u;
50
51 for (i = 0;i < 17;++i) {
52 u = 0;
53 for (j = 0;j <= i;++j) u += h[j] * r[i - j];
54 for (j = i + 1;j < 17;++j) u += 320 * h[j] * r[i + 17 - j];
55 hr[i] = u;
56 }
57 for (i = 0;i < 17;++i) h[i] = hr[i];
58 squeeze(h);
59}
60
61int crypto_onetimeauth(unsigned char *out,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
62{
63 unsigned int j;
64 unsigned int r[17];
65 unsigned int h[17];
66 unsigned int c[17];
67
68 r[0] = k[0];
69 r[1] = k[1];
70 r[2] = k[2];
71 r[3] = k[3] & 15;
72 r[4] = k[4] & 252;
73 r[5] = k[5];
74 r[6] = k[6];
75 r[7] = k[7] & 15;
76 r[8] = k[8] & 252;
77 r[9] = k[9];
78 r[10] = k[10];
79 r[11] = k[11] & 15;
80 r[12] = k[12] & 252;
81 r[13] = k[13];
82 r[14] = k[14];
83 r[15] = k[15] & 15;
84 r[16] = 0;
85
86 for (j = 0;j < 17;++j) h[j] = 0;
87
88 while (inlen > 0) {
89 for (j = 0;j < 17;++j) c[j] = 0;
90 for (j = 0;(j < 16) && (j < inlen);++j) c[j] = in[j];
91 c[j] = 1;
92 in += j; inlen -= j;
93 add(h,c);
94 mulmod(h,r);
95 }
96
97 freeze(h);
98
99 for (j = 0;j < 16;++j) c[j] = k[j + 16];
100 c[16] = 0;
101 add(h,c);
102 for (j = 0;j < 16;++j) out[j] = h[j];
103 return 0;
104}