Debian Codegraph @

contrib/fastpbkdf2.c

raw symbols SCIP
1 /*
2 * fast-pbkdf2 - Optimal PBKDF2-HMAC calculation
3 * Written in 2015 by Joseph Birr-Pixton <[email protected]>
4 *
5 * To the extent possible under law, the author(s) have dedicated all
6 * copyright and related and neighboring rights to this software to the
7 * public domain worldwide. This software is distributed without any
8 * warranty.
9 *
10 * You should have received a copy of the CC0 Public Domain Dedication
11 * along with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
13 */
14 /* librist. Copyright © 2019 SipRadius LLC. All right reserved.
15 * Author: Kuldeep Singh Dhaka <[email protected]>
16 * Author: Sergio Ammirata, Ph.D. <[email protected]>
17 *
18 * Changes:
19 * - Removed SHA1 and SHA512 as the functions are not available
20 * - Moved to self hosted SHA256 instead of OpenSSL
21 */
22
23 #include "fastpbkdf2.h"
24
25 #include <assert.h>
26 #include <string.h>
27
28 #if defined(__linux__) || defined(__GLIBC__)
29 #include <endian.h>
30 #elif defined(__APPLE__) || defined(__GLIBC__)
31 #include <machine/endian.h>
32 #define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
33 #define __BYTE_ORDER __DARWIN_BYTE_ORDER
34 #elif defined(__FreeBSD__)
35 #include <sys/endian.h>
36 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
37 #define __BYTE_ORDER _BYTE_ORDER
38 #endif /* __MacOS__ */
39
40 #include "sha256.h"
41
42 /* --- MSVC doesn't support C99 --- */
43 #ifdef _MSC_VER
44 #define restrict
45 #define _Pragma __pragma
46 #endif
47
48 /* --- Common useful things --- */
49 #define MIN(a, b) ((a) > (b)) ? (b) : (a)
50
51 static inline void write32_be(uint32_t n, uint8_t out[4])
52 {
53 #if defined(__GNUC__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN
54 *(uint32_t *)(out) = __builtin_bswap32(n);
55 #else
56 out[0] = (n >> 24) & 0xff;
57 out[1] = (n >> 16) & 0xff;
58 out[2] = (n >> 8) & 0xff;
59 out[3] = n & 0xff;
60 #endif
61 }
62
63 /*
64 * NOTE: This function is unused currently, but it is left incase it is needed in the
65 * future
66 */
67
68 /* static inline void write64_be(uint64_t n, uint8_t out[8])
69 * {
70 * #if defined(__GNUC__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN
71 * *(uint64_t *)(out) = __builtin_bswap64(n);
72 * #else
73 * write32_be((n >> 32) & 0xffffffff, out);
74 * write32_be(n & 0xffffffff, out + 4);
75 * #endif
76 * }
77 */
78
79 /* --- Optional OpenMP parallelisation of consecutive blocks --- */
80 #ifdef WITH_OPENMP
81 # define OPENMP_PARALLEL_FOR _Pragma("omp parallel for")
82 #else
83 # define OPENMP_PARALLEL_FOR
84 #endif
85
86 /* Prepare block (of blocksz bytes) to contain md padding denoting a msg-size
87 * message (in bytes). block has a prefix of used bytes.
88 *
89 * Message length is expressed in 32 bits (so suitable for sha1, sha256, sha512). */
90 static inline void md_pad(uint8_t *block, size_t blocksz, size_t used, size_t msg)
91 {
92 memset(block + used, 0, blocksz - used - 4);
93 block[used] = 0x80;
94 block += blocksz - 4;
95 write32_be((uint32_t) (msg * 8), block);
96 }
97
98 /* Internal function/type names for hash-specific things. */
99 #define HMAC_CTX(_name) HMAC_ ## _name ## _ctx
100 #define HMAC_INIT(_name) HMAC_ ## _name ## _init
101 #define HMAC_UPDATE(_name) HMAC_ ## _name ## _update
102 #define HMAC_FINAL(_name) HMAC_ ## _name ## _final
103
104 #define PBKDF2_F(_name) pbkdf2_f_ ## _name
105 #define PBKDF2(_name) pbkdf2_ ## _name
106
107 /* This macro expands to decls for the whole implementation for a given
108 * hash function. Arguments are:
109 *
110 * _name like 'sha1', added to symbol names
111 * _blocksz block size, in bytes
112 * _hashsz digest output, in bytes
113 * _ctx hash context type
114 * _init hash context initialisation function
115 * args: (_ctx *c)
116 * _update hash context update function
117 * args: (_ctx *c, const void *data, size_t ndata)
118 * _final hash context finish function
119 * args: (void *out, _ctx *c)
120 * _xform hash context raw block update function
121 * args: (_ctx *c, const void *data)
122 * _xcpy hash context raw copy function (only need copy hash state)
123 * args: (_ctx * restrict out, const _ctx *restrict in)
124 * _xtract hash context state extraction
125 * args: args (_ctx *restrict c, uint8_t *restrict out)
126 * _xxor hash context xor function (only need xor hash state)
127 * args: (_ctx *restrict out, const _ctx *restrict in)
128 *
129 * The resulting function is named PBKDF2(_name).
130 */
131 #define DECL_PBKDF2(_name, _blocksz, _hashsz, _ctx, \
132 _init, _update, _xform, _final, _xcpy, _xtract, _xxor) \
133 typedef struct { \
134 _ctx inner; \
135 _ctx outer; \
136 } HMAC_CTX(_name); \
137 \
138 static inline void HMAC_INIT(_name)(HMAC_CTX(_name) *ctx, \
139 const uint8_t *key, size_t nkey) \
140 { \
141 /* Prepare key: */ \
142 uint8_t k[_blocksz]; \
143 \
144 /* Shorten long keys. */ \
145 if (nkey > _blocksz) \
146 { \
147 _init(&ctx->inner); \
148 _update(&ctx->inner, key, nkey); \
149 _final(&ctx->inner, k); \
150 \
151 key = k; \
152 nkey = _hashsz; \
153 } \
154 \
155 /* Standard doesn't cover case where blocksz < hashsz. */ \
156 assert(nkey <= _blocksz); \
157 \
158 /* Right zero-pad short keys. */ \
159 if (k != key) \
160 memcpy(k, key, nkey); \
161 if (_blocksz > nkey) \
162 memset(k + nkey, 0, _blocksz - nkey); \
163 \
164 /* Start inner hash computation */ \
165 uint8_t blk_inner[_blocksz]; \
166 uint8_t blk_outer[_blocksz]; \
167 \
168 for (size_t i = 0; i < _blocksz; i++) \
169 { \
170 blk_inner[i] = 0x36 ^ k[i]; \
171 blk_outer[i] = 0x5c ^ k[i]; \
172 } \
173 \
174 _init(&ctx->inner); \
175 _update(&ctx->inner, blk_inner, sizeof blk_inner); \
176 \
177 /* And outer. */ \
178 _init(&ctx->outer); \
179 _update(&ctx->outer, blk_outer, sizeof blk_outer); \
180 } \
181 \
182 static inline void HMAC_UPDATE(_name)(HMAC_CTX(_name) *ctx, \
183 const void *data, size_t ndata) \
184 { \
185 _update(&ctx->inner, data, ndata); \
186 } \
187 \
188 static inline void HMAC_FINAL(_name)(HMAC_CTX(_name) *ctx, \
189 uint8_t out[_hashsz]) \
190 { \
191 _final(&ctx->inner, out); \
192 _update(&ctx->outer, out, _hashsz); \
193 _final(&ctx->outer, out); \
194 } \
195 \
196 \
197 /* --- PBKDF2 --- */ \
198 static inline void PBKDF2_F(_name)(const HMAC_CTX(_name) *startctx, \
199 uint32_t counter, \
200 const uint8_t *salt, size_t nsalt, \
201 uint32_t iterations, \
202 uint8_t *out) \
203 { \
204 uint8_t countbuf[4]; \
205 write32_be(counter, countbuf); \
206 \
207 /* Prepare loop-invariant padding block. */ \
208 uint8_t Ublock[_blocksz]; \
209 md_pad(Ublock, _blocksz, _hashsz, _blocksz + _hashsz); \
210 \
211 /* First iteration: \
212 * U_1 = PRF(P, S || INT_32_BE(i)) \
213 */ \
214 HMAC_CTX(_name) ctx = *startctx; \
215 HMAC_UPDATE(_name)(&ctx, salt, nsalt); \
216 HMAC_UPDATE(_name)(&ctx, countbuf, sizeof countbuf); \
217 HMAC_FINAL(_name)(&ctx, Ublock); \
218 _ctx result = ctx.outer; \
219 \
220 /* Subsequent iterations: \
221 * U_c = PRF(P, U_{c-1}) \
222 */ \
223 for (uint32_t i = 1; i < iterations; i++) \
224 { \
225 /* Complete inner hash with previous U */ \
226 _xcpy(&ctx.inner, &startctx->inner); \
227 _xform(&ctx.inner, Ublock); \
228 _xtract(&ctx.inner, Ublock); \
229 /* Complete outer hash with inner output */ \
230 _xcpy(&ctx.outer, &startctx->outer); \
231 _xform(&ctx.outer, Ublock); \
232 _xtract(&ctx.outer, Ublock); \
233 _xxor(&result, &ctx.outer); \
234 } \
235 \
236 /* Reform result into output buffer. */ \
237 _xtract(&result, out); \
238 } \
239 \
240 static inline void PBKDF2(_name)(const uint8_t *pw, size_t npw, \
241 const uint8_t *salt, size_t nsalt, \
242 uint32_t iterations, \
243 uint8_t *out, size_t nout) \
244 { \
245 assert(iterations); \
246 assert(out && nout); \
247 \
248 /* Starting point for inner loop. */ \
249 HMAC_CTX(_name) ctx; \
250 HMAC_INIT(_name)(&ctx, pw, npw); \
251 \
252 /* How many blocks do we need? */ \
253 uint32_t blocks_needed = (uint32_t)(nout + _hashsz - 1) / _hashsz; \
254 \
255 OPENMP_PARALLEL_FOR \
256 for (uint32_t counter = 1; counter <= blocks_needed; counter++) \
257 { \
258 uint8_t block[_hashsz]; \
259 PBKDF2_F(_name)(&ctx, counter, salt, nsalt, iterations, block); \
260 \
261 size_t offset = (counter - 1) * _hashsz; \
262 size_t taken = MIN(nout - offset, _hashsz); \
263 memcpy(out + offset, block, taken); \
264 } \
265 }
266
267 static inline void sha256_extract(SHA256_CTX *restrict ctx, uint8_t *restrict out)
268 {
269 write32_be(ctx->state[0], out);
270 write32_be(ctx->state[1], out + 4);
271 write32_be(ctx->state[2], out + 8);
272 write32_be(ctx->state[3], out + 12);
273 write32_be(ctx->state[4], out + 16);
274 write32_be(ctx->state[5], out + 20);
275 write32_be(ctx->state[6], out + 24);
276 write32_be(ctx->state[7], out + 28);
277 }
278
279 static inline void sha256_cpy(SHA256_CTX *restrict out, const SHA256_CTX *restrict in)
280 {
281 out->state[0] = in->state[0];
282 out->state[1] = in->state[1];
283 out->state[2] = in->state[2];
284 out->state[3] = in->state[3];
285 out->state[4] = in->state[4];
286 out->state[5] = in->state[5];
287 out->state[6] = in->state[6];
288 out->state[7] = in->state[7];
289 }
290
291 static inline void sha256_xor(SHA256_CTX *restrict out, const SHA256_CTX *restrict in)
292 {
293 out->state[0] ^= in->state[0];
294 out->state[1] ^= in->state[1];
295 out->state[2] ^= in->state[2];
296 out->state[3] ^= in->state[3];
297 out->state[4] ^= in->state[4];
298 out->state[5] ^= in->state[5];
299 out->state[6] ^= in->state[6];
300 out->state[7] ^= in->state[7];
301 }
302
303 DECL_PBKDF2(sha256,
304 64,
305 32,
306 SHA256_CTX,
307 _librist_SHA256_Init,
308 _librist_SHA256_Update,
309 _librist_SHA256_Transform,
310 _librist_SHA256_Final,
311 sha256_cpy,
312 sha256_extract,
313 sha256_xor)
314
315 void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw,
316 const uint8_t *salt, size_t nsalt,
317 uint32_t iterations,
318 uint8_t *out, size_t nout)
319 {
320 PBKDF2(sha256)(pw, npw, salt, nsalt, iterations, out, nout);
321 }