spiped 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +7 -0
  2. data/ext/spiped/extconf.rb +3 -0
  3. data/ext/spiped/spiped-source/BUILDING +46 -0
  4. data/ext/spiped/spiped-source/CHANGELOG +44 -0
  5. data/ext/spiped/spiped-source/COPYRIGHT +33 -0
  6. data/ext/spiped/spiped-source/Makefile +47 -0
  7. data/ext/spiped/spiped-source/Makefile.POSIX +27 -0
  8. data/ext/spiped/spiped-source/Makefile.inc +20 -0
  9. data/ext/spiped/spiped-source/Makefile.prog +23 -0
  10. data/ext/spiped/spiped-source/POSIX/README +10 -0
  11. data/ext/spiped/spiped-source/POSIX/posix-cflags.sh +10 -0
  12. data/ext/spiped/spiped-source/POSIX/posix-clock_realtime.c +3 -0
  13. data/ext/spiped/spiped-source/POSIX/posix-l.c +1 -0
  14. data/ext/spiped/spiped-source/POSIX/posix-l.sh +14 -0
  15. data/ext/spiped/spiped-source/POSIX/posix-msg_nosignal.c +3 -0
  16. data/ext/spiped/spiped-source/README +198 -0
  17. data/ext/spiped/spiped-source/STYLE +151 -0
  18. data/ext/spiped/spiped-source/lib/dnsthread/dnsthread.c +464 -0
  19. data/ext/spiped/spiped-source/lib/dnsthread/dnsthread.h +45 -0
  20. data/ext/spiped/spiped-source/libcperciva/alg/sha256.c +442 -0
  21. data/ext/spiped/spiped-source/libcperciva/alg/sha256.h +95 -0
  22. data/ext/spiped/spiped-source/libcperciva/cpusupport/Build/cpusupport-X86-AESNI.c +13 -0
  23. data/ext/spiped/spiped-source/libcperciva/cpusupport/Build/cpusupport-X86-CPUID.c +8 -0
  24. data/ext/spiped/spiped-source/libcperciva/cpusupport/Build/cpusupport.sh +37 -0
  25. data/ext/spiped/spiped-source/libcperciva/cpusupport/cpusupport.h +63 -0
  26. data/ext/spiped/spiped-source/libcperciva/cpusupport/cpusupport_x86_aesni.c +30 -0
  27. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aes.c +166 -0
  28. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aes.h +31 -0
  29. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aes_aesni.c +229 -0
  30. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aes_aesni.h +31 -0
  31. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aesctr.c +124 -0
  32. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_aesctr.h +41 -0
  33. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_dh.c +293 -0
  34. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_dh.h +43 -0
  35. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_dh_group14.c +46 -0
  36. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_dh_group14.h +9 -0
  37. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_entropy.c +215 -0
  38. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_entropy.h +14 -0
  39. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_verify_bytes.c +21 -0
  40. data/ext/spiped/spiped-source/libcperciva/crypto/crypto_verify_bytes.h +14 -0
  41. data/ext/spiped/spiped-source/libcperciva/datastruct/elasticarray.c +276 -0
  42. data/ext/spiped/spiped-source/libcperciva/datastruct/elasticarray.h +167 -0
  43. data/ext/spiped/spiped-source/libcperciva/datastruct/mpool.h +85 -0
  44. data/ext/spiped/spiped-source/libcperciva/datastruct/ptrheap.c +334 -0
  45. data/ext/spiped/spiped-source/libcperciva/datastruct/ptrheap.h +89 -0
  46. data/ext/spiped/spiped-source/libcperciva/datastruct/timerqueue.c +241 -0
  47. data/ext/spiped/spiped-source/libcperciva/datastruct/timerqueue.h +60 -0
  48. data/ext/spiped/spiped-source/libcperciva/events/events.c +203 -0
  49. data/ext/spiped/spiped-source/libcperciva/events/events.h +106 -0
  50. data/ext/spiped/spiped-source/libcperciva/events/events_immediate.c +149 -0
  51. data/ext/spiped/spiped-source/libcperciva/events/events_internal.h +95 -0
  52. data/ext/spiped/spiped-source/libcperciva/events/events_network.c +347 -0
  53. data/ext/spiped/spiped-source/libcperciva/events/events_network_selectstats.c +106 -0
  54. data/ext/spiped/spiped-source/libcperciva/events/events_timer.c +273 -0
  55. data/ext/spiped/spiped-source/libcperciva/network/network.h +95 -0
  56. data/ext/spiped/spiped-source/libcperciva/network/network_accept.c +103 -0
  57. data/ext/spiped/spiped-source/libcperciva/network/network_connect.c +258 -0
  58. data/ext/spiped/spiped-source/libcperciva/network/network_read.c +155 -0
  59. data/ext/spiped/spiped-source/libcperciva/network/network_write.c +188 -0
  60. data/ext/spiped/spiped-source/libcperciva/util/asprintf.c +49 -0
  61. data/ext/spiped/spiped-source/libcperciva/util/asprintf.h +16 -0
  62. data/ext/spiped/spiped-source/libcperciva/util/daemonize.c +134 -0
  63. data/ext/spiped/spiped-source/libcperciva/util/daemonize.h +10 -0
  64. data/ext/spiped/spiped-source/libcperciva/util/entropy.c +76 -0
  65. data/ext/spiped/spiped-source/libcperciva/util/entropy.h +13 -0
  66. data/ext/spiped/spiped-source/libcperciva/util/imalloc.h +33 -0
  67. data/ext/spiped/spiped-source/libcperciva/util/insecure_memzero.c +19 -0
  68. data/ext/spiped/spiped-source/libcperciva/util/insecure_memzero.h +33 -0
  69. data/ext/spiped/spiped-source/libcperciva/util/monoclock.c +52 -0
  70. data/ext/spiped/spiped-source/libcperciva/util/monoclock.h +14 -0
  71. data/ext/spiped/spiped-source/libcperciva/util/noeintr.c +54 -0
  72. data/ext/spiped/spiped-source/libcperciva/util/noeintr.h +14 -0
  73. data/ext/spiped/spiped-source/libcperciva/util/sock.c +472 -0
  74. data/ext/spiped/spiped-source/libcperciva/util/sock.h +56 -0
  75. data/ext/spiped/spiped-source/libcperciva/util/sock_internal.h +14 -0
  76. data/ext/spiped/spiped-source/libcperciva/util/sock_util.c +271 -0
  77. data/ext/spiped/spiped-source/libcperciva/util/sock_util.h +51 -0
  78. data/ext/spiped/spiped-source/libcperciva/util/sysendian.h +146 -0
  79. data/ext/spiped/spiped-source/libcperciva/util/warnp.c +76 -0
  80. data/ext/spiped/spiped-source/libcperciva/util/warnp.h +59 -0
  81. data/ext/spiped/spiped-source/proto/proto_conn.c +362 -0
  82. data/ext/spiped/spiped-source/proto/proto_conn.h +25 -0
  83. data/ext/spiped/spiped-source/proto/proto_crypt.c +396 -0
  84. data/ext/spiped/spiped-source/proto/proto_crypt.h +102 -0
  85. data/ext/spiped/spiped-source/proto/proto_handshake.c +330 -0
  86. data/ext/spiped/spiped-source/proto/proto_handshake.h +30 -0
  87. data/ext/spiped/spiped-source/proto/proto_pipe.c +202 -0
  88. data/ext/spiped/spiped-source/proto/proto_pipe.h +23 -0
  89. data/ext/spiped/spiped-source/spipe/Makefile +90 -0
  90. data/ext/spiped/spiped-source/spipe/README +24 -0
  91. data/ext/spiped/spiped-source/spipe/main.c +178 -0
  92. data/ext/spiped/spiped-source/spipe/pushbits.c +101 -0
  93. data/ext/spiped/spiped-source/spipe/pushbits.h +10 -0
  94. data/ext/spiped/spiped-source/spipe/spipe.1 +60 -0
  95. data/ext/spiped/spiped-source/spiped/Makefile +98 -0
  96. data/ext/spiped/spiped-source/spiped/README +62 -0
  97. data/ext/spiped/spiped-source/spiped/dispatch.c +214 -0
  98. data/ext/spiped/spiped-source/spiped/dispatch.h +27 -0
  99. data/ext/spiped/spiped-source/spiped/main.c +267 -0
  100. data/ext/spiped/spiped-source/spiped/spiped.1 +112 -0
  101. data/lib/spiped.rb +3 -0
  102. metadata +143 -0
@@ -0,0 +1,45 @@
1
+ #ifndef _DNSTHREAD_H_
2
+ #define _DNSTHREAD_H_
3
+
4
+ /* Opaque address structure. */
5
+ struct sock_addr;
6
+
7
+ /* Opaque thread token. */
8
+ typedef struct dnsthread_internal * DNSTHREAD;
9
+
10
+ /**
11
+ * dnsthread_spawn(void):
12
+ * Spawn a thread for performing address resolution. Return a token which can
13
+ * be passed to dnsthread_resolveone and dnsthread_kill.
14
+ */
15
+ DNSTHREAD dnsthread_spawn(void);
16
+
17
+ /**
18
+ * dnsthread_resolveone(T, addr, callback, cookie):
19
+ * Using the thread for which ${T} was returned by dnsthread_spawn, resolve
20
+ * the address ${addr}, which must be in one of the forms accepted by the
21
+ * sock_resolve function. If ${T} is already resolving an address, fail with
22
+ * EALREADY. Upon completion, invoke ${callback}(${cookie}, sas), where
23
+ * ${sas} is a NULL-terminated array of pointers to sock_addr structures or
24
+ * NULL on resolution failure.
25
+ */
26
+ int dnsthread_resolveone(DNSTHREAD, const char *,
27
+ int (*)(void *, struct sock_addr **), void *);
28
+
29
+ /**
30
+ * dnsthread_kill(T):
31
+ * Instruct an address resolution thread to die. If the thread does not have
32
+ * an address resolution operation currently pending, wait for the thread to
33
+ * die before returning.
34
+ */
35
+ int dnsthread_kill(DNSTHREAD);
36
+
37
+ /**
38
+ * dnsthread_resolve(addr, callback, cookie):
39
+ * Perform a non-blocking address resolution of ${addr}. This function may
40
+ * spawn a thread internally.
41
+ */
42
+ int dnsthread_resolve(const char *,
43
+ int (*)(void *, struct sock_addr **), void *);
44
+
45
+ #endif /* !_DNSTHREAD_H_ */
@@ -0,0 +1,442 @@
1
+ #include <assert.h>
2
+ #include <stdint.h>
3
+ #include <string.h>
4
+
5
+ #include "insecure_memzero.h"
6
+ #include "sysendian.h"
7
+
8
+ #include "sha256.h"
9
+
10
+ /*
11
+ * Encode a length len/4 vector of (uint32_t) into a length len vector of
12
+ * (uint8_t) in big-endian form. Assumes len is a multiple of 4.
13
+ */
14
+ static void
15
+ be32enc_vect(uint8_t * dst, const uint32_t * src, size_t len)
16
+ {
17
+ size_t i;
18
+
19
+ /* Sanity-check. */
20
+ assert(len % 4 == 0);
21
+
22
+ /* Encode vector, one word at a time. */
23
+ for (i = 0; i < len / 4; i++)
24
+ be32enc(dst + i * 4, src[i]);
25
+ }
26
+
27
+ /*
28
+ * Decode a big-endian length len vector of (uint8_t) into a length
29
+ * len/4 vector of (uint32_t). Assumes len is a multiple of 4.
30
+ */
31
+ static void
32
+ be32dec_vect(uint32_t * dst, const uint8_t * src, size_t len)
33
+ {
34
+ size_t i;
35
+
36
+ /* Sanity-check. */
37
+ assert(len % 4 == 0);
38
+
39
+ /* Decode vector, one word at a time. */
40
+ for (i = 0; i < len / 4; i++)
41
+ dst[i] = be32dec(src + i * 4);
42
+ }
43
+
44
+ /* Elementary functions used by SHA256 */
45
+ #define Ch(x, y, z) ((x & (y ^ z)) ^ z)
46
+ #define Maj(x, y, z) ((x & (y | z)) | (y & z))
47
+ #define SHR(x, n) (x >> n)
48
+ #define ROTR(x, n) ((x >> n) | (x << (32 - n)))
49
+ #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
50
+ #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
51
+ #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
52
+ #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
53
+
54
+ /* SHA256 round function */
55
+ #define RND(a, b, c, d, e, f, g, h, k) \
56
+ t0 = h + S1(e) + Ch(e, f, g) + k; \
57
+ t1 = S0(a) + Maj(a, b, c); \
58
+ d += t0; \
59
+ h = t0 + t1;
60
+
61
+ /* Adjusted round function for rotating state */
62
+ #define RNDr(S, W, i, k) \
63
+ RND(S[(64 - i) % 8], S[(65 - i) % 8], \
64
+ S[(66 - i) % 8], S[(67 - i) % 8], \
65
+ S[(68 - i) % 8], S[(69 - i) % 8], \
66
+ S[(70 - i) % 8], S[(71 - i) % 8], \
67
+ W[i] + k)
68
+
69
+ /*
70
+ * SHA256 block compression function. The 256-bit state is transformed via
71
+ * the 512-bit input block to produce a new state.
72
+ */
73
+ static void
74
+ SHA256_Transform(uint32_t * state, const uint8_t block[64])
75
+ {
76
+ uint32_t W[64];
77
+ uint32_t S[8];
78
+ uint32_t t0, t1;
79
+ int i;
80
+
81
+ /* 1. Prepare message schedule W. */
82
+ be32dec_vect(W, block, 64);
83
+ for (i = 16; i < 64; i++)
84
+ W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
85
+
86
+ /* 2. Initialize working variables. */
87
+ memcpy(S, state, 32);
88
+
89
+ /* 3. Mix. */
90
+ RNDr(S, W, 0, 0x428a2f98);
91
+ RNDr(S, W, 1, 0x71374491);
92
+ RNDr(S, W, 2, 0xb5c0fbcf);
93
+ RNDr(S, W, 3, 0xe9b5dba5);
94
+ RNDr(S, W, 4, 0x3956c25b);
95
+ RNDr(S, W, 5, 0x59f111f1);
96
+ RNDr(S, W, 6, 0x923f82a4);
97
+ RNDr(S, W, 7, 0xab1c5ed5);
98
+ RNDr(S, W, 8, 0xd807aa98);
99
+ RNDr(S, W, 9, 0x12835b01);
100
+ RNDr(S, W, 10, 0x243185be);
101
+ RNDr(S, W, 11, 0x550c7dc3);
102
+ RNDr(S, W, 12, 0x72be5d74);
103
+ RNDr(S, W, 13, 0x80deb1fe);
104
+ RNDr(S, W, 14, 0x9bdc06a7);
105
+ RNDr(S, W, 15, 0xc19bf174);
106
+ RNDr(S, W, 16, 0xe49b69c1);
107
+ RNDr(S, W, 17, 0xefbe4786);
108
+ RNDr(S, W, 18, 0x0fc19dc6);
109
+ RNDr(S, W, 19, 0x240ca1cc);
110
+ RNDr(S, W, 20, 0x2de92c6f);
111
+ RNDr(S, W, 21, 0x4a7484aa);
112
+ RNDr(S, W, 22, 0x5cb0a9dc);
113
+ RNDr(S, W, 23, 0x76f988da);
114
+ RNDr(S, W, 24, 0x983e5152);
115
+ RNDr(S, W, 25, 0xa831c66d);
116
+ RNDr(S, W, 26, 0xb00327c8);
117
+ RNDr(S, W, 27, 0xbf597fc7);
118
+ RNDr(S, W, 28, 0xc6e00bf3);
119
+ RNDr(S, W, 29, 0xd5a79147);
120
+ RNDr(S, W, 30, 0x06ca6351);
121
+ RNDr(S, W, 31, 0x14292967);
122
+ RNDr(S, W, 32, 0x27b70a85);
123
+ RNDr(S, W, 33, 0x2e1b2138);
124
+ RNDr(S, W, 34, 0x4d2c6dfc);
125
+ RNDr(S, W, 35, 0x53380d13);
126
+ RNDr(S, W, 36, 0x650a7354);
127
+ RNDr(S, W, 37, 0x766a0abb);
128
+ RNDr(S, W, 38, 0x81c2c92e);
129
+ RNDr(S, W, 39, 0x92722c85);
130
+ RNDr(S, W, 40, 0xa2bfe8a1);
131
+ RNDr(S, W, 41, 0xa81a664b);
132
+ RNDr(S, W, 42, 0xc24b8b70);
133
+ RNDr(S, W, 43, 0xc76c51a3);
134
+ RNDr(S, W, 44, 0xd192e819);
135
+ RNDr(S, W, 45, 0xd6990624);
136
+ RNDr(S, W, 46, 0xf40e3585);
137
+ RNDr(S, W, 47, 0x106aa070);
138
+ RNDr(S, W, 48, 0x19a4c116);
139
+ RNDr(S, W, 49, 0x1e376c08);
140
+ RNDr(S, W, 50, 0x2748774c);
141
+ RNDr(S, W, 51, 0x34b0bcb5);
142
+ RNDr(S, W, 52, 0x391c0cb3);
143
+ RNDr(S, W, 53, 0x4ed8aa4a);
144
+ RNDr(S, W, 54, 0x5b9cca4f);
145
+ RNDr(S, W, 55, 0x682e6ff3);
146
+ RNDr(S, W, 56, 0x748f82ee);
147
+ RNDr(S, W, 57, 0x78a5636f);
148
+ RNDr(S, W, 58, 0x84c87814);
149
+ RNDr(S, W, 59, 0x8cc70208);
150
+ RNDr(S, W, 60, 0x90befffa);
151
+ RNDr(S, W, 61, 0xa4506ceb);
152
+ RNDr(S, W, 62, 0xbef9a3f7);
153
+ RNDr(S, W, 63, 0xc67178f2);
154
+
155
+ /* 4. Mix local working variables into global state. */
156
+ for (i = 0; i < 8; i++)
157
+ state[i] += S[i];
158
+
159
+ /* Clean the stack. */
160
+ insecure_memzero(W, 256);
161
+ insecure_memzero(S, 32);
162
+ insecure_memzero(&t0, sizeof(uint32_t));
163
+ insecure_memzero(&t1, sizeof(uint32_t));
164
+ }
165
+
166
+ static uint8_t PAD[64] = {
167
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
171
+ };
172
+
173
+ /* Add padding and terminating bit-count. */
174
+ static void
175
+ SHA256_Pad(SHA256_CTX * ctx)
176
+ {
177
+ uint8_t len[8];
178
+ uint32_t r, plen;
179
+
180
+ /*
181
+ * Convert length to a vector of bytes -- we do this now rather
182
+ * than later because the length will change after we pad.
183
+ */
184
+ be64enc(len, ctx->count);
185
+
186
+ /* Add 1--64 bytes so that the resulting length is 56 mod 64. */
187
+ r = (ctx->count >> 3) & 0x3f;
188
+ plen = (r < 56) ? (56 - r) : (120 - r);
189
+ SHA256_Update(ctx, PAD, (size_t)plen);
190
+
191
+ /* Add the terminating bit-count. */
192
+ SHA256_Update(ctx, len, 8);
193
+ }
194
+
195
+ /* Magic initialization constants. */
196
+ static const uint32_t initstate[8] = {
197
+ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
198
+ 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
199
+ };
200
+
201
+ /**
202
+ * SHA256_Init(ctx):
203
+ * Initialize the SHA256 context ${ctx}.
204
+ */
205
+ void
206
+ SHA256_Init(SHA256_CTX * ctx)
207
+ {
208
+
209
+ /* Zero bits processed so far. */
210
+ ctx->count = 0;
211
+
212
+ /* Initialize state. */
213
+ memcpy(ctx->state, initstate, sizeof(initstate));
214
+ }
215
+
216
+ /**
217
+ * SHA256_Update(ctx, in, len):
218
+ * Input ${len} bytes from ${in} into the SHA256 context ${ctx}.
219
+ */
220
+ void
221
+ SHA256_Update(SHA256_CTX * ctx, const void * in, size_t len)
222
+ {
223
+ uint32_t r;
224
+ const uint8_t * src = in;
225
+
226
+ /* Return immediately if we have nothing to do. */
227
+ if (len == 0)
228
+ return;
229
+
230
+ /* Number of bytes left in the buffer from previous updates. */
231
+ r = (ctx->count >> 3) & 0x3f;
232
+
233
+ /* Update number of bits. */
234
+ ctx->count += (uint64_t)(len) << 3;
235
+
236
+ /* Handle the case where we don't need to perform any transforms. */
237
+ if (len < 64 - r) {
238
+ memcpy(&ctx->buf[r], src, len);
239
+ return;
240
+ }
241
+
242
+ /* Finish the current block. */
243
+ memcpy(&ctx->buf[r], src, 64 - r);
244
+ SHA256_Transform(ctx->state, ctx->buf);
245
+ src += 64 - r;
246
+ len -= 64 - r;
247
+
248
+ /* Perform complete blocks. */
249
+ while (len >= 64) {
250
+ SHA256_Transform(ctx->state, src);
251
+ src += 64;
252
+ len -= 64;
253
+ }
254
+
255
+ /* Copy left over data into buffer. */
256
+ memcpy(ctx->buf, src, len);
257
+ }
258
+
259
+ /**
260
+ * SHA256_Final(digest, ctx):
261
+ * Output the SHA256 hash of the data input to the context ${ctx} into the
262
+ * buffer ${digest}.
263
+ */
264
+ void
265
+ SHA256_Final(uint8_t digest[32], SHA256_CTX * ctx)
266
+ {
267
+
268
+ /* Add padding. */
269
+ SHA256_Pad(ctx);
270
+
271
+ /* Write the hash. */
272
+ be32enc_vect(digest, ctx->state, 32);
273
+
274
+ /* Clear the context state. */
275
+ insecure_memzero(ctx, sizeof(SHA256_CTX));
276
+ }
277
+
278
+ /**
279
+ * SHA256_Buf(in, len, digest):
280
+ * Compute the SHA256 hash of ${len} bytes from $in} and write it to ${digest}.
281
+ */
282
+ void
283
+ SHA256_Buf(const void * in, size_t len, uint8_t digest[32])
284
+ {
285
+ SHA256_CTX ctx;
286
+
287
+ SHA256_Init(&ctx);
288
+ SHA256_Update(&ctx, in, len);
289
+ SHA256_Final(digest, &ctx);
290
+ }
291
+
292
+ /**
293
+ * HMAC_SHA256_Init(ctx, K, Klen):
294
+ * Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
295
+ * ${K}.
296
+ */
297
+ void
298
+ HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
299
+ {
300
+ uint8_t pad[64];
301
+ uint8_t khash[32];
302
+ const uint8_t * K = _K;
303
+ size_t i;
304
+
305
+ /* If Klen > 64, the key is really SHA256(K). */
306
+ if (Klen > 64) {
307
+ SHA256_Init(&ctx->ictx);
308
+ SHA256_Update(&ctx->ictx, K, Klen);
309
+ SHA256_Final(khash, &ctx->ictx);
310
+ K = khash;
311
+ Klen = 32;
312
+ }
313
+
314
+ /* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
315
+ SHA256_Init(&ctx->ictx);
316
+ memset(pad, 0x36, 64);
317
+ for (i = 0; i < Klen; i++)
318
+ pad[i] ^= K[i];
319
+ SHA256_Update(&ctx->ictx, pad, 64);
320
+
321
+ /* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
322
+ SHA256_Init(&ctx->octx);
323
+ memset(pad, 0x5c, 64);
324
+ for (i = 0; i < Klen; i++)
325
+ pad[i] ^= K[i];
326
+ SHA256_Update(&ctx->octx, pad, 64);
327
+
328
+ /* Clean the stack. */
329
+ insecure_memzero(khash, 32);
330
+ insecure_memzero(pad, 64);
331
+ }
332
+
333
+ /**
334
+ * HMAC_SHA256_Update(ctx, in, len):
335
+ * Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
336
+ */
337
+ void
338
+ HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void * in, size_t len)
339
+ {
340
+
341
+ /* Feed data to the inner SHA256 operation. */
342
+ SHA256_Update(&ctx->ictx, in, len);
343
+ }
344
+
345
+ /**
346
+ * HMAC_SHA256_Final(digest, ctx):
347
+ * Output the HMAC-SHA256 of the data input to the context ${ctx} into the
348
+ * buffer ${digest}.
349
+ */
350
+ void
351
+ HMAC_SHA256_Final(uint8_t digest[32], HMAC_SHA256_CTX * ctx)
352
+ {
353
+ uint8_t ihash[32];
354
+
355
+ /* Finish the inner SHA256 operation. */
356
+ SHA256_Final(ihash, &ctx->ictx);
357
+
358
+ /* Feed the inner hash to the outer SHA256 operation. */
359
+ SHA256_Update(&ctx->octx, ihash, 32);
360
+
361
+ /* Finish the outer SHA256 operation. */
362
+ SHA256_Final(digest, &ctx->octx);
363
+
364
+ /* Clean the stack. */
365
+ insecure_memzero(ihash, 32);
366
+ }
367
+
368
+ /**
369
+ * HMAC_SHA256_Buf(K, Klen, in, len, digest):
370
+ * Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
371
+ * length ${Klen}, and write the result to ${digest}.
372
+ */
373
+ void
374
+ HMAC_SHA256_Buf(const void * K, size_t Klen, const void * in, size_t len,
375
+ uint8_t digest[32])
376
+ {
377
+ HMAC_SHA256_CTX ctx;
378
+
379
+ HMAC_SHA256_Init(&ctx, K, Klen);
380
+ HMAC_SHA256_Update(&ctx, in, len);
381
+ HMAC_SHA256_Final(digest, &ctx);
382
+ }
383
+
384
+ /**
385
+ * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
386
+ * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
387
+ * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
388
+ */
389
+ void
390
+ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
391
+ size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
392
+ {
393
+ HMAC_SHA256_CTX PShctx, hctx;
394
+ size_t i;
395
+ uint8_t ivec[4];
396
+ uint8_t U[32];
397
+ uint8_t T[32];
398
+ uint64_t j;
399
+ int k;
400
+ size_t clen;
401
+
402
+ /* Sanity-check. */
403
+ assert(dkLen <= 32 * (size_t)(UINT32_MAX));
404
+
405
+ /* Compute HMAC state after processing P and S. */
406
+ HMAC_SHA256_Init(&PShctx, passwd, passwdlen);
407
+ HMAC_SHA256_Update(&PShctx, salt, saltlen);
408
+
409
+ /* Iterate through the blocks. */
410
+ for (i = 0; i * 32 < dkLen; i++) {
411
+ /* Generate INT(i + 1). */
412
+ be32enc(ivec, (uint32_t)(i + 1));
413
+
414
+ /* Compute U_1 = PRF(P, S || INT(i)). */
415
+ memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
416
+ HMAC_SHA256_Update(&hctx, ivec, 4);
417
+ HMAC_SHA256_Final(U, &hctx);
418
+
419
+ /* T_i = U_1 ... */
420
+ memcpy(T, U, 32);
421
+
422
+ for (j = 2; j <= c; j++) {
423
+ /* Compute U_j. */
424
+ HMAC_SHA256_Init(&hctx, passwd, passwdlen);
425
+ HMAC_SHA256_Update(&hctx, U, 32);
426
+ HMAC_SHA256_Final(U, &hctx);
427
+
428
+ /* ... xor U_j ... */
429
+ for (k = 0; k < 32; k++)
430
+ T[k] ^= U[k];
431
+ }
432
+
433
+ /* Copy as many bytes as necessary into buf. */
434
+ clen = dkLen - i * 32;
435
+ if (clen > 32)
436
+ clen = 32;
437
+ memcpy(&buf[i * 32], T, clen);
438
+ }
439
+
440
+ /* Clean PShctx, since we never called _Final on it. */
441
+ insecure_memzero(&PShctx, sizeof(HMAC_SHA256_CTX));
442
+ }