ruby-fastpbkdf2 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +43 -0
- data/LICENSE.txt +21 -0
- data/README.md +314 -0
- data/ext/fastpbkdf2/endian.h +71 -0
- data/ext/fastpbkdf2/extconf.rb +59 -0
- data/ext/fastpbkdf2/fastpbkdf2.c +402 -0
- data/ext/fastpbkdf2/fastpbkdf2.h +71 -0
- data/ext/fastpbkdf2/fastpbkdf2_ruby.c +142 -0
- data/ext/fastpbkdf2/fastpbkdf2_ruby.o +0 -0
- data/ext/fastpbkdf2/fastpbkdf2_wrapper.c +16 -0
- data/ext/fastpbkdf2/fastpbkdf2_wrapper.h +48 -0
- data/ext/fastpbkdf2/fastpbkdf2_wrapper.o +0 -0
- data/lib/fastpbkdf2/fastpbkdf2.bundle +0 -0
- data/lib/fastpbkdf2/version.rb +3 -0
- data/lib/fastpbkdf2.rb +31 -0
- data/vendor/fastpbkdf2/LICENSE +117 -0
- data/vendor/fastpbkdf2/fastpbkdf2.c +402 -0
- data/vendor/fastpbkdf2/fastpbkdf2.h +71 -0
- metadata +107 -0
@@ -0,0 +1,402 @@
|
|
1
|
+
/*
|
2
|
+
* fast-pbkdf2 - Optimal PBKDF2-HMAC calculation
|
3
|
+
* Written in 2015 by Joseph Birr-Pixton <jpixton@gmail.com>
|
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
|
+
|
15
|
+
#include "fastpbkdf2.h"
|
16
|
+
|
17
|
+
#include <assert.h>
|
18
|
+
#include <string.h>
|
19
|
+
#if defined(__GNUC__) && !defined(__APPLE__)
|
20
|
+
#include <endian.h>
|
21
|
+
#endif
|
22
|
+
|
23
|
+
#include <openssl/sha.h>
|
24
|
+
|
25
|
+
/* --- MSVC doesn't support C99 --- */
|
26
|
+
#ifdef _MSC_VER
|
27
|
+
#define restrict
|
28
|
+
#define _Pragma __pragma
|
29
|
+
#endif
|
30
|
+
|
31
|
+
/* --- Common useful things --- */
|
32
|
+
#define MIN(a, b) ((a) > (b)) ? (b) : (a)
|
33
|
+
|
34
|
+
static inline void write32_be(uint32_t n, uint8_t out[4])
|
35
|
+
{
|
36
|
+
#if defined(__GNUC__) && !defined(__APPLE__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN
|
37
|
+
*(uint32_t *)(out) = __builtin_bswap32(n);
|
38
|
+
#else
|
39
|
+
out[0] = (n >> 24) & 0xff;
|
40
|
+
out[1] = (n >> 16) & 0xff;
|
41
|
+
out[2] = (n >> 8) & 0xff;
|
42
|
+
out[3] = n & 0xff;
|
43
|
+
#endif
|
44
|
+
}
|
45
|
+
|
46
|
+
static inline void write64_be(uint64_t n, uint8_t out[8])
|
47
|
+
{
|
48
|
+
#if defined(__GNUC__) && !defined(__APPLE__) && __GNUC__ >= 4 && __BYTE_ORDER == __LITTLE_ENDIAN
|
49
|
+
*(uint64_t *)(out) = __builtin_bswap64(n);
|
50
|
+
#else
|
51
|
+
write32_be((n >> 32) & 0xffffffff, out);
|
52
|
+
write32_be(n & 0xffffffff, out + 4);
|
53
|
+
#endif
|
54
|
+
}
|
55
|
+
|
56
|
+
/* --- Optional OpenMP parallelisation of consecutive blocks --- */
|
57
|
+
#ifdef WITH_OPENMP
|
58
|
+
# define OPENMP_PARALLEL_FOR _Pragma("omp parallel for")
|
59
|
+
#else
|
60
|
+
# define OPENMP_PARALLEL_FOR
|
61
|
+
#endif
|
62
|
+
|
63
|
+
/* Prepare block (of blocksz bytes) to contain md padding denoting a msg-size
|
64
|
+
* message (in bytes). block has a prefix of used bytes.
|
65
|
+
*
|
66
|
+
* Message length is expressed in 32 bits (so suitable for sha1, sha256, sha512). */
|
67
|
+
static inline void md_pad(uint8_t *block, size_t blocksz, size_t used, size_t msg)
|
68
|
+
{
|
69
|
+
memset(block + used, 0, blocksz - used - 4);
|
70
|
+
block[used] = 0x80;
|
71
|
+
block += blocksz - 4;
|
72
|
+
write32_be((uint32_t) (msg * 8), block);
|
73
|
+
}
|
74
|
+
|
75
|
+
/* Internal function/type names for hash-specific things. */
|
76
|
+
#define HMAC_CTX(_name) HMAC_ ## _name ## _ctx
|
77
|
+
#define HMAC_INIT(_name) HMAC_ ## _name ## _init
|
78
|
+
#define HMAC_UPDATE(_name) HMAC_ ## _name ## _update
|
79
|
+
#define HMAC_FINAL(_name) HMAC_ ## _name ## _final
|
80
|
+
|
81
|
+
#define PBKDF2_F(_name) pbkdf2_f_ ## _name
|
82
|
+
#define PBKDF2(_name) pbkdf2_ ## _name
|
83
|
+
|
84
|
+
/* This macro expands to decls for the whole implementation for a given
|
85
|
+
* hash function. Arguments are:
|
86
|
+
*
|
87
|
+
* _name like 'sha1', added to symbol names
|
88
|
+
* _blocksz block size, in bytes
|
89
|
+
* _hashsz digest output, in bytes
|
90
|
+
* _ctx hash context type
|
91
|
+
* _init hash context initialisation function
|
92
|
+
* args: (_ctx *c)
|
93
|
+
* _update hash context update function
|
94
|
+
* args: (_ctx *c, const void *data, size_t ndata)
|
95
|
+
* _final hash context finish function
|
96
|
+
* args: (void *out, _ctx *c)
|
97
|
+
* _xform hash context raw block update function
|
98
|
+
* args: (_ctx *c, const void *data)
|
99
|
+
* _xcpy hash context raw copy function (only need copy hash state)
|
100
|
+
* args: (_ctx * restrict out, const _ctx *restrict in)
|
101
|
+
* _xtract hash context state extraction
|
102
|
+
* args: args (_ctx *restrict c, uint8_t *restrict out)
|
103
|
+
* _xxor hash context xor function (only need xor hash state)
|
104
|
+
* args: (_ctx *restrict out, const _ctx *restrict in)
|
105
|
+
*
|
106
|
+
* The resulting function is named PBKDF2(_name).
|
107
|
+
*/
|
108
|
+
#define DECL_PBKDF2(_name, _blocksz, _hashsz, _ctx, \
|
109
|
+
_init, _update, _xform, _final, _xcpy, _xtract, _xxor) \
|
110
|
+
typedef struct { \
|
111
|
+
_ctx inner; \
|
112
|
+
_ctx outer; \
|
113
|
+
} HMAC_CTX(_name); \
|
114
|
+
\
|
115
|
+
static inline void HMAC_INIT(_name)(HMAC_CTX(_name) *ctx, \
|
116
|
+
const uint8_t *key, size_t nkey) \
|
117
|
+
{ \
|
118
|
+
/* Prepare key: */ \
|
119
|
+
uint8_t k[_blocksz]; \
|
120
|
+
\
|
121
|
+
/* Shorten long keys. */ \
|
122
|
+
if (nkey > _blocksz) \
|
123
|
+
{ \
|
124
|
+
_init(&ctx->inner); \
|
125
|
+
_update(&ctx->inner, key, nkey); \
|
126
|
+
_final(k, &ctx->inner); \
|
127
|
+
\
|
128
|
+
key = k; \
|
129
|
+
nkey = _hashsz; \
|
130
|
+
} \
|
131
|
+
\
|
132
|
+
/* Standard doesn't cover case where blocksz < hashsz. */ \
|
133
|
+
assert(nkey <= _blocksz); \
|
134
|
+
\
|
135
|
+
/* Right zero-pad short keys. */ \
|
136
|
+
if (k != key) \
|
137
|
+
memcpy(k, key, nkey); \
|
138
|
+
if (_blocksz > nkey) \
|
139
|
+
memset(k + nkey, 0, _blocksz - nkey); \
|
140
|
+
\
|
141
|
+
/* Start inner hash computation */ \
|
142
|
+
uint8_t blk_inner[_blocksz]; \
|
143
|
+
uint8_t blk_outer[_blocksz]; \
|
144
|
+
\
|
145
|
+
for (size_t i = 0; i < _blocksz; i++) \
|
146
|
+
{ \
|
147
|
+
blk_inner[i] = 0x36 ^ k[i]; \
|
148
|
+
blk_outer[i] = 0x5c ^ k[i]; \
|
149
|
+
} \
|
150
|
+
\
|
151
|
+
_init(&ctx->inner); \
|
152
|
+
_update(&ctx->inner, blk_inner, sizeof blk_inner); \
|
153
|
+
\
|
154
|
+
/* And outer. */ \
|
155
|
+
_init(&ctx->outer); \
|
156
|
+
_update(&ctx->outer, blk_outer, sizeof blk_outer); \
|
157
|
+
} \
|
158
|
+
\
|
159
|
+
static inline void HMAC_UPDATE(_name)(HMAC_CTX(_name) *ctx, \
|
160
|
+
const void *data, size_t ndata) \
|
161
|
+
{ \
|
162
|
+
_update(&ctx->inner, data, ndata); \
|
163
|
+
} \
|
164
|
+
\
|
165
|
+
static inline void HMAC_FINAL(_name)(HMAC_CTX(_name) *ctx, \
|
166
|
+
uint8_t out[_hashsz]) \
|
167
|
+
{ \
|
168
|
+
_final(out, &ctx->inner); \
|
169
|
+
_update(&ctx->outer, out, _hashsz); \
|
170
|
+
_final(out, &ctx->outer); \
|
171
|
+
} \
|
172
|
+
\
|
173
|
+
\
|
174
|
+
/* --- PBKDF2 --- */ \
|
175
|
+
static inline void PBKDF2_F(_name)(const HMAC_CTX(_name) *startctx, \
|
176
|
+
uint32_t counter, \
|
177
|
+
const uint8_t *salt, size_t nsalt, \
|
178
|
+
uint32_t iterations, \
|
179
|
+
uint8_t *out) \
|
180
|
+
{ \
|
181
|
+
uint8_t countbuf[4]; \
|
182
|
+
write32_be(counter, countbuf); \
|
183
|
+
\
|
184
|
+
/* Prepare loop-invariant padding block. */ \
|
185
|
+
uint8_t Ublock[_blocksz]; \
|
186
|
+
md_pad(Ublock, _blocksz, _hashsz, _blocksz + _hashsz); \
|
187
|
+
\
|
188
|
+
/* First iteration: \
|
189
|
+
* U_1 = PRF(P, S || INT_32_BE(i)) \
|
190
|
+
*/ \
|
191
|
+
HMAC_CTX(_name) ctx = *startctx; \
|
192
|
+
HMAC_UPDATE(_name)(&ctx, salt, nsalt); \
|
193
|
+
HMAC_UPDATE(_name)(&ctx, countbuf, sizeof countbuf); \
|
194
|
+
HMAC_FINAL(_name)(&ctx, Ublock); \
|
195
|
+
_ctx result = ctx.outer; \
|
196
|
+
\
|
197
|
+
/* Subsequent iterations: \
|
198
|
+
* U_c = PRF(P, U_{c-1}) \
|
199
|
+
*/ \
|
200
|
+
for (uint32_t i = 1; i < iterations; i++) \
|
201
|
+
{ \
|
202
|
+
/* Complete inner hash with previous U */ \
|
203
|
+
_xcpy(&ctx.inner, &startctx->inner); \
|
204
|
+
_xform(&ctx.inner, Ublock); \
|
205
|
+
_xtract(&ctx.inner, Ublock); \
|
206
|
+
/* Complete outer hash with inner output */ \
|
207
|
+
_xcpy(&ctx.outer, &startctx->outer); \
|
208
|
+
_xform(&ctx.outer, Ublock); \
|
209
|
+
_xtract(&ctx.outer, Ublock); \
|
210
|
+
_xxor(&result, &ctx.outer); \
|
211
|
+
} \
|
212
|
+
\
|
213
|
+
/* Reform result into output buffer. */ \
|
214
|
+
_xtract(&result, out); \
|
215
|
+
} \
|
216
|
+
\
|
217
|
+
static inline void PBKDF2(_name)(const uint8_t *pw, size_t npw, \
|
218
|
+
const uint8_t *salt, size_t nsalt, \
|
219
|
+
uint32_t iterations, \
|
220
|
+
uint8_t *out, size_t nout) \
|
221
|
+
{ \
|
222
|
+
assert(iterations); \
|
223
|
+
assert(out && nout); \
|
224
|
+
\
|
225
|
+
/* Starting point for inner loop. */ \
|
226
|
+
HMAC_CTX(_name) ctx; \
|
227
|
+
HMAC_INIT(_name)(&ctx, pw, npw); \
|
228
|
+
\
|
229
|
+
/* How many blocks do we need? */ \
|
230
|
+
uint32_t blocks_needed = (uint32_t)(nout + _hashsz - 1) / _hashsz; \
|
231
|
+
\
|
232
|
+
OPENMP_PARALLEL_FOR \
|
233
|
+
for (uint32_t counter = 1; counter <= blocks_needed; counter++) \
|
234
|
+
{ \
|
235
|
+
uint8_t block[_hashsz]; \
|
236
|
+
PBKDF2_F(_name)(&ctx, counter, salt, nsalt, iterations, block); \
|
237
|
+
\
|
238
|
+
size_t offset = (counter - 1) * _hashsz; \
|
239
|
+
size_t taken = MIN(nout - offset, _hashsz); \
|
240
|
+
memcpy(out + offset, block, taken); \
|
241
|
+
} \
|
242
|
+
}
|
243
|
+
|
244
|
+
static inline void sha1_extract(SHA_CTX *restrict ctx, uint8_t *restrict out)
|
245
|
+
{
|
246
|
+
write32_be(ctx->h0, out);
|
247
|
+
write32_be(ctx->h1, out + 4);
|
248
|
+
write32_be(ctx->h2, out + 8);
|
249
|
+
write32_be(ctx->h3, out + 12);
|
250
|
+
write32_be(ctx->h4, out + 16);
|
251
|
+
}
|
252
|
+
|
253
|
+
static inline void sha1_cpy(SHA_CTX *restrict out, const SHA_CTX *restrict in)
|
254
|
+
{
|
255
|
+
out->h0 = in->h0;
|
256
|
+
out->h1 = in->h1;
|
257
|
+
out->h2 = in->h2;
|
258
|
+
out->h3 = in->h3;
|
259
|
+
out->h4 = in->h4;
|
260
|
+
}
|
261
|
+
|
262
|
+
static inline void sha1_xor(SHA_CTX *restrict out, const SHA_CTX *restrict in)
|
263
|
+
{
|
264
|
+
out->h0 ^= in->h0;
|
265
|
+
out->h1 ^= in->h1;
|
266
|
+
out->h2 ^= in->h2;
|
267
|
+
out->h3 ^= in->h3;
|
268
|
+
out->h4 ^= in->h4;
|
269
|
+
}
|
270
|
+
|
271
|
+
DECL_PBKDF2(sha1,
|
272
|
+
SHA_CBLOCK,
|
273
|
+
SHA_DIGEST_LENGTH,
|
274
|
+
SHA_CTX,
|
275
|
+
SHA1_Init,
|
276
|
+
SHA1_Update,
|
277
|
+
SHA1_Transform,
|
278
|
+
SHA1_Final,
|
279
|
+
sha1_cpy,
|
280
|
+
sha1_extract,
|
281
|
+
sha1_xor)
|
282
|
+
|
283
|
+
static inline void sha256_extract(SHA256_CTX *restrict ctx, uint8_t *restrict out)
|
284
|
+
{
|
285
|
+
write32_be(ctx->h[0], out);
|
286
|
+
write32_be(ctx->h[1], out + 4);
|
287
|
+
write32_be(ctx->h[2], out + 8);
|
288
|
+
write32_be(ctx->h[3], out + 12);
|
289
|
+
write32_be(ctx->h[4], out + 16);
|
290
|
+
write32_be(ctx->h[5], out + 20);
|
291
|
+
write32_be(ctx->h[6], out + 24);
|
292
|
+
write32_be(ctx->h[7], out + 28);
|
293
|
+
}
|
294
|
+
|
295
|
+
static inline void sha256_cpy(SHA256_CTX *restrict out, const SHA256_CTX *restrict in)
|
296
|
+
{
|
297
|
+
out->h[0] = in->h[0];
|
298
|
+
out->h[1] = in->h[1];
|
299
|
+
out->h[2] = in->h[2];
|
300
|
+
out->h[3] = in->h[3];
|
301
|
+
out->h[4] = in->h[4];
|
302
|
+
out->h[5] = in->h[5];
|
303
|
+
out->h[6] = in->h[6];
|
304
|
+
out->h[7] = in->h[7];
|
305
|
+
}
|
306
|
+
|
307
|
+
static inline void sha256_xor(SHA256_CTX *restrict out, const SHA256_CTX *restrict in)
|
308
|
+
{
|
309
|
+
out->h[0] ^= in->h[0];
|
310
|
+
out->h[1] ^= in->h[1];
|
311
|
+
out->h[2] ^= in->h[2];
|
312
|
+
out->h[3] ^= in->h[3];
|
313
|
+
out->h[4] ^= in->h[4];
|
314
|
+
out->h[5] ^= in->h[5];
|
315
|
+
out->h[6] ^= in->h[6];
|
316
|
+
out->h[7] ^= in->h[7];
|
317
|
+
}
|
318
|
+
|
319
|
+
DECL_PBKDF2(sha256,
|
320
|
+
SHA256_CBLOCK,
|
321
|
+
SHA256_DIGEST_LENGTH,
|
322
|
+
SHA256_CTX,
|
323
|
+
SHA256_Init,
|
324
|
+
SHA256_Update,
|
325
|
+
SHA256_Transform,
|
326
|
+
SHA256_Final,
|
327
|
+
sha256_cpy,
|
328
|
+
sha256_extract,
|
329
|
+
sha256_xor)
|
330
|
+
|
331
|
+
static inline void sha512_extract(SHA512_CTX *restrict ctx, uint8_t *restrict out)
|
332
|
+
{
|
333
|
+
write64_be(ctx->h[0], out);
|
334
|
+
write64_be(ctx->h[1], out + 8);
|
335
|
+
write64_be(ctx->h[2], out + 16);
|
336
|
+
write64_be(ctx->h[3], out + 24);
|
337
|
+
write64_be(ctx->h[4], out + 32);
|
338
|
+
write64_be(ctx->h[5], out + 40);
|
339
|
+
write64_be(ctx->h[6], out + 48);
|
340
|
+
write64_be(ctx->h[7], out + 56);
|
341
|
+
}
|
342
|
+
|
343
|
+
static inline void sha512_cpy(SHA512_CTX *restrict out, const SHA512_CTX *restrict in)
|
344
|
+
{
|
345
|
+
out->h[0] = in->h[0];
|
346
|
+
out->h[1] = in->h[1];
|
347
|
+
out->h[2] = in->h[2];
|
348
|
+
out->h[3] = in->h[3];
|
349
|
+
out->h[4] = in->h[4];
|
350
|
+
out->h[5] = in->h[5];
|
351
|
+
out->h[6] = in->h[6];
|
352
|
+
out->h[7] = in->h[7];
|
353
|
+
}
|
354
|
+
|
355
|
+
static inline void sha512_xor(SHA512_CTX *restrict out, const SHA512_CTX *restrict in)
|
356
|
+
{
|
357
|
+
out->h[0] ^= in->h[0];
|
358
|
+
out->h[1] ^= in->h[1];
|
359
|
+
out->h[2] ^= in->h[2];
|
360
|
+
out->h[3] ^= in->h[3];
|
361
|
+
out->h[4] ^= in->h[4];
|
362
|
+
out->h[5] ^= in->h[5];
|
363
|
+
out->h[6] ^= in->h[6];
|
364
|
+
out->h[7] ^= in->h[7];
|
365
|
+
}
|
366
|
+
|
367
|
+
DECL_PBKDF2(sha512,
|
368
|
+
SHA512_CBLOCK,
|
369
|
+
SHA512_DIGEST_LENGTH,
|
370
|
+
SHA512_CTX,
|
371
|
+
SHA512_Init,
|
372
|
+
SHA512_Update,
|
373
|
+
SHA512_Transform,
|
374
|
+
SHA512_Final,
|
375
|
+
sha512_cpy,
|
376
|
+
sha512_extract,
|
377
|
+
sha512_xor)
|
378
|
+
|
379
|
+
void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw,
|
380
|
+
const uint8_t *salt, size_t nsalt,
|
381
|
+
uint32_t iterations,
|
382
|
+
uint8_t *out, size_t nout)
|
383
|
+
{
|
384
|
+
PBKDF2(sha1)(pw, npw, salt, nsalt, iterations, out, nout);
|
385
|
+
}
|
386
|
+
|
387
|
+
void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw,
|
388
|
+
const uint8_t *salt, size_t nsalt,
|
389
|
+
uint32_t iterations,
|
390
|
+
uint8_t *out, size_t nout)
|
391
|
+
{
|
392
|
+
PBKDF2(sha256)(pw, npw, salt, nsalt, iterations, out, nout);
|
393
|
+
}
|
394
|
+
|
395
|
+
void fastpbkdf2_hmac_sha512(const uint8_t *pw, size_t npw,
|
396
|
+
const uint8_t *salt, size_t nsalt,
|
397
|
+
uint32_t iterations,
|
398
|
+
uint8_t *out, size_t nout)
|
399
|
+
{
|
400
|
+
PBKDF2(sha512)(pw, npw, salt, nsalt, iterations, out, nout);
|
401
|
+
}
|
402
|
+
|
@@ -0,0 +1,71 @@
|
|
1
|
+
/*
|
2
|
+
* fastpbkdf2 - Faster PBKDF2-HMAC calculation
|
3
|
+
* Written in 2015 by Joseph Birr-Pixton <jpixton@gmail.com>
|
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
|
+
|
15
|
+
#ifndef FASTPBKDF2_H
|
16
|
+
#define FASTPBKDF2_H
|
17
|
+
|
18
|
+
#include <stdlib.h>
|
19
|
+
#include <stdint.h>
|
20
|
+
|
21
|
+
#ifdef __cplusplus
|
22
|
+
extern "C" {
|
23
|
+
#endif
|
24
|
+
|
25
|
+
/** Calculates PBKDF2-HMAC-SHA1.
|
26
|
+
*
|
27
|
+
* @p npw bytes at @p pw are the password input.
|
28
|
+
* @p nsalt bytes at @p salt are the salt input.
|
29
|
+
* @p iterations is the PBKDF2 iteration count and must be non-zero.
|
30
|
+
* @p nout bytes of output are written to @p out. @p nout must be non-zero.
|
31
|
+
*
|
32
|
+
* This function cannot fail; it does not report errors.
|
33
|
+
*/
|
34
|
+
void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw,
|
35
|
+
const uint8_t *salt, size_t nsalt,
|
36
|
+
uint32_t iterations,
|
37
|
+
uint8_t *out, size_t nout);
|
38
|
+
|
39
|
+
/** Calculates PBKDF2-HMAC-SHA256.
|
40
|
+
*
|
41
|
+
* @p npw bytes at @p pw are the password input.
|
42
|
+
* @p nsalt bytes at @p salt are the salt input.
|
43
|
+
* @p iterations is the PBKDF2 iteration count and must be non-zero.
|
44
|
+
* @p nout bytes of output are written to @p out. @p nout must be non-zero.
|
45
|
+
*
|
46
|
+
* This function cannot fail; it does not report errors.
|
47
|
+
*/
|
48
|
+
void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw,
|
49
|
+
const uint8_t *salt, size_t nsalt,
|
50
|
+
uint32_t iterations,
|
51
|
+
uint8_t *out, size_t nout);
|
52
|
+
|
53
|
+
/** Calculates PBKDF2-HMAC-SHA512.
|
54
|
+
*
|
55
|
+
* @p npw bytes at @p pw are the password input.
|
56
|
+
* @p nsalt bytes at @p salt are the salt input.
|
57
|
+
* @p iterations is the PBKDF2 iteration count and must be non-zero.
|
58
|
+
* @p nout bytes of output are written to @p out. @p nout must be non-zero.
|
59
|
+
*
|
60
|
+
* This function cannot fail; it does not report errors.
|
61
|
+
*/
|
62
|
+
void fastpbkdf2_hmac_sha512(const uint8_t *pw, size_t npw,
|
63
|
+
const uint8_t *salt, size_t nsalt,
|
64
|
+
uint32_t iterations,
|
65
|
+
uint8_t *out, size_t nout);
|
66
|
+
|
67
|
+
#ifdef __cplusplus
|
68
|
+
}
|
69
|
+
#endif
|
70
|
+
|
71
|
+
#endif
|
@@ -0,0 +1,142 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include "fastpbkdf2_wrapper.h"
|
3
|
+
|
4
|
+
static VALUE mFastpbkdf2;
|
5
|
+
|
6
|
+
/*
|
7
|
+
* call-seq:
|
8
|
+
* FastPBKDF2.pbkdf2_hmac_sha1(password, salt, iterations, length) -> String
|
9
|
+
*
|
10
|
+
* Calculates PBKDF2-HMAC-SHA1 and returns the derived key as a binary string.
|
11
|
+
*
|
12
|
+
* * +password+ - Password string or binary data
|
13
|
+
* * +salt+ - Salt string or binary data
|
14
|
+
* * +iterations+ - Number of iterations (must be > 0)
|
15
|
+
* * +length+ - Length of output key in bytes (must be > 0)
|
16
|
+
*/
|
17
|
+
static VALUE
|
18
|
+
rb_fastpbkdf2_hmac_sha1(VALUE self __attribute__((unused)), VALUE password, VALUE salt, VALUE iterations, VALUE length)
|
19
|
+
{
|
20
|
+
Check_Type(password, T_STRING);
|
21
|
+
Check_Type(salt, T_STRING);
|
22
|
+
Check_Type(iterations, T_FIXNUM);
|
23
|
+
Check_Type(length, T_FIXNUM);
|
24
|
+
|
25
|
+
const uint8_t *pw = (const uint8_t *)RSTRING_PTR(password);
|
26
|
+
size_t npw = RSTRING_LEN(password);
|
27
|
+
const uint8_t *salt_ptr = (const uint8_t *)RSTRING_PTR(salt);
|
28
|
+
size_t nsalt = RSTRING_LEN(salt);
|
29
|
+
uint32_t iter = NUM2UINT(iterations);
|
30
|
+
size_t nout = NUM2UINT(length);
|
31
|
+
|
32
|
+
if (iter == 0) {
|
33
|
+
rb_raise(rb_eArgError, "iterations must be greater than 0");
|
34
|
+
}
|
35
|
+
if (nout == 0) {
|
36
|
+
rb_raise(rb_eArgError, "length must be greater than 0");
|
37
|
+
}
|
38
|
+
|
39
|
+
VALUE result = rb_str_new(NULL, nout);
|
40
|
+
uint8_t *out = (uint8_t *)RSTRING_PTR(result);
|
41
|
+
|
42
|
+
fastpbkdf2_hmac_sha1(pw, npw, salt_ptr, nsalt, iter, out, nout);
|
43
|
+
|
44
|
+
return result;
|
45
|
+
}
|
46
|
+
|
47
|
+
/*
|
48
|
+
* call-seq:
|
49
|
+
* FastPBKDF2.pbkdf2_hmac_sha256(password, salt, iterations, length) -> String
|
50
|
+
*
|
51
|
+
* Calculates PBKDF2-HMAC-SHA256 and returns the derived key as a binary string.
|
52
|
+
*
|
53
|
+
* * +password+ - Password string or binary data
|
54
|
+
* * +salt+ - Salt string or binary data
|
55
|
+
* * +iterations+ - Number of iterations (must be > 0)
|
56
|
+
* * +length+ - Length of output key in bytes (must be > 0)
|
57
|
+
*/
|
58
|
+
static VALUE
|
59
|
+
rb_fastpbkdf2_hmac_sha256(VALUE self __attribute__((unused)), VALUE password, VALUE salt, VALUE iterations, VALUE length)
|
60
|
+
{
|
61
|
+
Check_Type(password, T_STRING);
|
62
|
+
Check_Type(salt, T_STRING);
|
63
|
+
Check_Type(iterations, T_FIXNUM);
|
64
|
+
Check_Type(length, T_FIXNUM);
|
65
|
+
|
66
|
+
const uint8_t *pw = (const uint8_t *)RSTRING_PTR(password);
|
67
|
+
size_t npw = RSTRING_LEN(password);
|
68
|
+
const uint8_t *salt_ptr = (const uint8_t *)RSTRING_PTR(salt);
|
69
|
+
size_t nsalt = RSTRING_LEN(salt);
|
70
|
+
uint32_t iter = NUM2UINT(iterations);
|
71
|
+
size_t nout = NUM2UINT(length);
|
72
|
+
|
73
|
+
if (iter == 0) {
|
74
|
+
rb_raise(rb_eArgError, "iterations must be greater than 0");
|
75
|
+
}
|
76
|
+
if (nout == 0) {
|
77
|
+
rb_raise(rb_eArgError, "length must be greater than 0");
|
78
|
+
}
|
79
|
+
|
80
|
+
VALUE result = rb_str_new(NULL, nout);
|
81
|
+
uint8_t *out = (uint8_t *)RSTRING_PTR(result);
|
82
|
+
|
83
|
+
fastpbkdf2_hmac_sha256(pw, npw, salt_ptr, nsalt, iter, out, nout);
|
84
|
+
|
85
|
+
return result;
|
86
|
+
}
|
87
|
+
|
88
|
+
/*
|
89
|
+
* call-seq:
|
90
|
+
* FastPBKDF2.pbkdf2_hmac_sha512(password, salt, iterations, length) -> String
|
91
|
+
*
|
92
|
+
* Calculates PBKDF2-HMAC-SHA512 and returns the derived key as a binary string.
|
93
|
+
*
|
94
|
+
* * +password+ - Password string or binary data
|
95
|
+
* * +salt+ - Salt string or binary data
|
96
|
+
* * +iterations+ - Number of iterations (must be > 0)
|
97
|
+
* * +length+ - Length of output key in bytes (must be > 0)
|
98
|
+
*/
|
99
|
+
static VALUE
|
100
|
+
rb_fastpbkdf2_hmac_sha512(VALUE self __attribute__((unused)), VALUE password, VALUE salt, VALUE iterations, VALUE length)
|
101
|
+
{
|
102
|
+
Check_Type(password, T_STRING);
|
103
|
+
Check_Type(salt, T_STRING);
|
104
|
+
Check_Type(iterations, T_FIXNUM);
|
105
|
+
Check_Type(length, T_FIXNUM);
|
106
|
+
|
107
|
+
const uint8_t *pw = (const uint8_t *)RSTRING_PTR(password);
|
108
|
+
size_t npw = RSTRING_LEN(password);
|
109
|
+
const uint8_t *salt_ptr = (const uint8_t *)RSTRING_PTR(salt);
|
110
|
+
size_t nsalt = RSTRING_LEN(salt);
|
111
|
+
uint32_t iter = NUM2UINT(iterations);
|
112
|
+
size_t nout = NUM2UINT(length);
|
113
|
+
|
114
|
+
if (iter == 0) {
|
115
|
+
rb_raise(rb_eArgError, "iterations must be greater than 0");
|
116
|
+
}
|
117
|
+
if (nout == 0) {
|
118
|
+
rb_raise(rb_eArgError, "length must be greater than 0");
|
119
|
+
}
|
120
|
+
|
121
|
+
VALUE result = rb_str_new(NULL, nout);
|
122
|
+
uint8_t *out = (uint8_t *)RSTRING_PTR(result);
|
123
|
+
|
124
|
+
fastpbkdf2_hmac_sha512(pw, npw, salt_ptr, nsalt, iter, out, nout);
|
125
|
+
|
126
|
+
return result;
|
127
|
+
}
|
128
|
+
|
129
|
+
void
|
130
|
+
Init_fastpbkdf2(void)
|
131
|
+
{
|
132
|
+
mFastpbkdf2 = rb_define_module("FastPBKDF2");
|
133
|
+
|
134
|
+
rb_define_module_function(mFastpbkdf2, "pbkdf2_hmac_sha1", rb_fastpbkdf2_hmac_sha1, 4);
|
135
|
+
rb_define_module_function(mFastpbkdf2, "pbkdf2_hmac_sha256", rb_fastpbkdf2_hmac_sha256, 4);
|
136
|
+
rb_define_module_function(mFastpbkdf2, "pbkdf2_hmac_sha512", rb_fastpbkdf2_hmac_sha512, 4);
|
137
|
+
|
138
|
+
// Add shorter, cleaner aliases using rb_define_alias
|
139
|
+
rb_define_alias(rb_singleton_class(mFastpbkdf2), "sha1", "pbkdf2_hmac_sha1");
|
140
|
+
rb_define_alias(rb_singleton_class(mFastpbkdf2), "sha256", "pbkdf2_hmac_sha256");
|
141
|
+
rb_define_alias(rb_singleton_class(mFastpbkdf2), "sha512", "pbkdf2_hmac_sha512");
|
142
|
+
}
|
Binary file
|
@@ -0,0 +1,16 @@
|
|
1
|
+
/*
|
2
|
+
* fastpbkdf2_wrapper.c - Platform compatibility wrapper for fastpbkdf2
|
3
|
+
*
|
4
|
+
* This wrapper sets up the proper environment for fastpbkdf2.c compilation
|
5
|
+
* without modifying the upstream source files.
|
6
|
+
*/
|
7
|
+
|
8
|
+
/*
|
9
|
+
* fastpbkdf2_wrapper.c - Cross-platform wrapper for fastpbkdf2.c
|
10
|
+
*
|
11
|
+
* This wrapper includes fastpbkdf2.c with cross-platform endian compatibility.
|
12
|
+
* The local endian.h file handles platform differences automatically.
|
13
|
+
*/
|
14
|
+
|
15
|
+
/* Now include the actual fastpbkdf2.c implementation */
|
16
|
+
#include "../../vendor/fastpbkdf2/fastpbkdf2.c"
|
@@ -0,0 +1,48 @@
|
|
1
|
+
/*
|
2
|
+
* fastpbkdf2_wrapper.h - Platform compatibility wrapper for fastpbkdf2
|
3
|
+
*
|
4
|
+
* This header handles platform-specific differences without modifying
|
5
|
+
* the upstream fastpbkdf2 source files.
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef FASTPBKDF2_WRAPPER_H
|
9
|
+
#define FASTPBKDF2_WRAPPER_H
|
10
|
+
|
11
|
+
/* Platform-specific compatibility fixes */
|
12
|
+
#ifdef __APPLE__
|
13
|
+
/* On macOS, we have __GNUC__ but don't want to use the GCC-specific
|
14
|
+
* endian.h and __BYTE_ORDER checks. Let's define the endian macros
|
15
|
+
* we need using the Apple-specific headers instead. */
|
16
|
+
#include <machine/endian.h>
|
17
|
+
#include <libkern/OSByteOrder.h>
|
18
|
+
|
19
|
+
/* Define the Linux-style endian macros that fastpbkdf2.c expects */
|
20
|
+
#ifndef __BYTE_ORDER
|
21
|
+
#define __BYTE_ORDER BYTE_ORDER
|
22
|
+
#endif
|
23
|
+
#ifndef __LITTLE_ENDIAN
|
24
|
+
#define __LITTLE_ENDIAN LITTLE_ENDIAN
|
25
|
+
#endif
|
26
|
+
#ifndef __BIG_ENDIAN
|
27
|
+
#define __BIG_ENDIAN BIG_ENDIAN
|
28
|
+
#endif
|
29
|
+
#endif
|
30
|
+
|
31
|
+
/* Windows platform compatibility fixes */
|
32
|
+
#ifdef _WIN32
|
33
|
+
/* Windows doesn't have endian.h, but it's always little-endian on x86/x64 */
|
34
|
+
#ifndef __BYTE_ORDER
|
35
|
+
#define __BYTE_ORDER 1234
|
36
|
+
#endif
|
37
|
+
#ifndef __LITTLE_ENDIAN
|
38
|
+
#define __LITTLE_ENDIAN 1234
|
39
|
+
#endif
|
40
|
+
#ifndef __BIG_ENDIAN
|
41
|
+
#define __BIG_ENDIAN 4321
|
42
|
+
#endif
|
43
|
+
#endif
|
44
|
+
|
45
|
+
/* Include the actual fastpbkdf2 header */
|
46
|
+
#include "../../vendor/fastpbkdf2/fastpbkdf2.h"
|
47
|
+
|
48
|
+
#endif /* FASTPBKDF2_WRAPPER_H */
|
Binary file
|
Binary file
|