bcrypt_pbkdf 1.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ #define crypto_hash_sha512_BYTES 64U
2
+ int crypto_hashblocks_sha512(unsigned char *statebytes,const unsigned char *in,unsigned long long inlen);
3
+ int crypto_hash_sha512(unsigned char *out,const unsigned char *in,unsigned long long inlen);
@@ -0,0 +1,19 @@
1
+ #include <stdint.h>
2
+
3
+ typedef struct crypto_hash_sha512_state {
4
+ uint64_t state[8];
5
+ uint64_t count[2];
6
+ unsigned char buf[128];
7
+ } crypto_hash_sha512_state;
8
+
9
+ #define crypto_hash_sha512_BYTES 64U
10
+ int crypto_hash_sha512_init(crypto_hash_sha512_state *state);
11
+ int crypto_hashblocks_sha512(unsigned char *statebytes,const unsigned char *in,unsigned long long inlen);
12
+ int
13
+ crypto_hash_sha512_update(crypto_hash_sha512_state *state,
14
+ const unsigned char *in,
15
+ unsigned long long inlen);
16
+ int
17
+ crypto_hash_sha512_final(crypto_hash_sha512_state *state,
18
+ unsigned char *out);
19
+
@@ -0,0 +1,20 @@
1
+ /* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */
2
+ /* $OpenBSD: explicit_bzero.c,v 1.1 2014/01/22 21:06:45 tedu Exp $ */
3
+ /*
4
+ * Public domain.
5
+ * Written by Ted Unangst
6
+ */
7
+
8
+ #include "includes.h"
9
+
10
+ #ifndef HAVE_EXPLICIT_BZERO
11
+
12
+ /*
13
+ * explicit_bzero - don't let the compiler optimize away bzero
14
+ */
15
+ void
16
+ explicit_bzero(void *p, size_t n)
17
+ {
18
+ bzero(p, n);
19
+ }
20
+ #endif
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+ dir_config("bcrypt_pbkdf_ext")
3
+ create_makefile("bcrypt_pbkdf_ext")
@@ -0,0 +1,320 @@
1
+ /*-
2
+ * Copyright 2005,2007,2009 Colin Percival
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ * 1. Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ * 2. Redistributions in binary form must reproduce the above copyright
11
+ * notice, this list of conditions and the following disclaimer in the
12
+ * documentation and/or other materials provided with the distribution.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
+ * SUCH DAMAGE.
25
+ *
26
+ */
27
+
28
+ #include "crypto_hash_sha512.h"
29
+ #include "utils.h"
30
+
31
+ #include <sys/types.h>
32
+
33
+ #include <limits.h>
34
+ #include <stdint.h>
35
+ #include <stdlib.h>
36
+ #include <string.h>
37
+
38
+ /* Avoid namespace collisions with BSD <sys/endian.h>. */
39
+ #define be64dec _sha512_be64dec
40
+ #define be64enc _sha512_be64enc
41
+
42
+ static inline uint64_t
43
+ be64dec(const void *pp)
44
+ {
45
+ const uint8_t *p = (uint8_t const *)pp;
46
+
47
+ return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +
48
+ ((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +
49
+ ((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +
50
+ ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));
51
+ }
52
+
53
+ static inline void
54
+ be64enc(void *pp, uint64_t x)
55
+ {
56
+ uint8_t *p = (uint8_t *)pp;
57
+
58
+ p[7] = x & 0xff;
59
+ p[6] = (x >> 8) & 0xff;
60
+ p[5] = (x >> 16) & 0xff;
61
+ p[4] = (x >> 24) & 0xff;
62
+ p[3] = (x >> 32) & 0xff;
63
+ p[2] = (x >> 40) & 0xff;
64
+ p[1] = (x >> 48) & 0xff;
65
+ p[0] = (x >> 56) & 0xff;
66
+ }
67
+
68
+ static void
69
+ be64enc_vect(unsigned char *dst, const uint64_t *src, size_t len)
70
+ {
71
+ size_t i;
72
+
73
+ for (i = 0; i < len / 8; i++) {
74
+ be64enc(dst + i * 8, src[i]);
75
+ }
76
+ }
77
+
78
+ static void
79
+ be64dec_vect(uint64_t *dst, const unsigned char *src, size_t len)
80
+ {
81
+ size_t i;
82
+
83
+ for (i = 0; i < len / 8; i++) {
84
+ dst[i] = be64dec(src + i * 8);
85
+ }
86
+ }
87
+
88
+ #define Ch(x, y, z) ((x & (y ^ z)) ^ z)
89
+ #define Maj(x, y, z) ((x & (y | z)) | (y & z))
90
+ #define SHR(x, n) (x >> n)
91
+ #define ROTR(x, n) ((x >> n) | (x << (64 - n)))
92
+ #define S0(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
93
+ #define S1(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
94
+ #define s0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
95
+ #define s1(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))
96
+
97
+ #define RND(a, b, c, d, e, f, g, h, k) \
98
+ t0 = h + S1(e) + Ch(e, f, g) + k; \
99
+ t1 = S0(a) + Maj(a, b, c); \
100
+ d += t0; \
101
+ h = t0 + t1;
102
+
103
+ #define RNDr(S, W, i, k) \
104
+ RND(S[(80 - i) % 8], S[(81 - i) % 8], \
105
+ S[(82 - i) % 8], S[(83 - i) % 8], \
106
+ S[(84 - i) % 8], S[(85 - i) % 8], \
107
+ S[(86 - i) % 8], S[(87 - i) % 8], \
108
+ W[i] + k)
109
+
110
+ static void
111
+ SHA512_Transform(uint64_t *state, const unsigned char block[128])
112
+ {
113
+ uint64_t W[80];
114
+ uint64_t S[8];
115
+ uint64_t t0, t1;
116
+ int i;
117
+
118
+ be64dec_vect(W, block, 128);
119
+ for (i = 16; i < 80; i++) {
120
+ W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
121
+ }
122
+
123
+ memcpy(S, state, 64);
124
+
125
+ RNDr(S, W, 0, 0x428a2f98d728ae22ULL);
126
+ RNDr(S, W, 1, 0x7137449123ef65cdULL);
127
+ RNDr(S, W, 2, 0xb5c0fbcfec4d3b2fULL);
128
+ RNDr(S, W, 3, 0xe9b5dba58189dbbcULL);
129
+ RNDr(S, W, 4, 0x3956c25bf348b538ULL);
130
+ RNDr(S, W, 5, 0x59f111f1b605d019ULL);
131
+ RNDr(S, W, 6, 0x923f82a4af194f9bULL);
132
+ RNDr(S, W, 7, 0xab1c5ed5da6d8118ULL);
133
+ RNDr(S, W, 8, 0xd807aa98a3030242ULL);
134
+ RNDr(S, W, 9, 0x12835b0145706fbeULL);
135
+ RNDr(S, W, 10, 0x243185be4ee4b28cULL);
136
+ RNDr(S, W, 11, 0x550c7dc3d5ffb4e2ULL);
137
+ RNDr(S, W, 12, 0x72be5d74f27b896fULL);
138
+ RNDr(S, W, 13, 0x80deb1fe3b1696b1ULL);
139
+ RNDr(S, W, 14, 0x9bdc06a725c71235ULL);
140
+ RNDr(S, W, 15, 0xc19bf174cf692694ULL);
141
+ RNDr(S, W, 16, 0xe49b69c19ef14ad2ULL);
142
+ RNDr(S, W, 17, 0xefbe4786384f25e3ULL);
143
+ RNDr(S, W, 18, 0x0fc19dc68b8cd5b5ULL);
144
+ RNDr(S, W, 19, 0x240ca1cc77ac9c65ULL);
145
+ RNDr(S, W, 20, 0x2de92c6f592b0275ULL);
146
+ RNDr(S, W, 21, 0x4a7484aa6ea6e483ULL);
147
+ RNDr(S, W, 22, 0x5cb0a9dcbd41fbd4ULL);
148
+ RNDr(S, W, 23, 0x76f988da831153b5ULL);
149
+ RNDr(S, W, 24, 0x983e5152ee66dfabULL);
150
+ RNDr(S, W, 25, 0xa831c66d2db43210ULL);
151
+ RNDr(S, W, 26, 0xb00327c898fb213fULL);
152
+ RNDr(S, W, 27, 0xbf597fc7beef0ee4ULL);
153
+ RNDr(S, W, 28, 0xc6e00bf33da88fc2ULL);
154
+ RNDr(S, W, 29, 0xd5a79147930aa725ULL);
155
+ RNDr(S, W, 30, 0x06ca6351e003826fULL);
156
+ RNDr(S, W, 31, 0x142929670a0e6e70ULL);
157
+ RNDr(S, W, 32, 0x27b70a8546d22ffcULL);
158
+ RNDr(S, W, 33, 0x2e1b21385c26c926ULL);
159
+ RNDr(S, W, 34, 0x4d2c6dfc5ac42aedULL);
160
+ RNDr(S, W, 35, 0x53380d139d95b3dfULL);
161
+ RNDr(S, W, 36, 0x650a73548baf63deULL);
162
+ RNDr(S, W, 37, 0x766a0abb3c77b2a8ULL);
163
+ RNDr(S, W, 38, 0x81c2c92e47edaee6ULL);
164
+ RNDr(S, W, 39, 0x92722c851482353bULL);
165
+ RNDr(S, W, 40, 0xa2bfe8a14cf10364ULL);
166
+ RNDr(S, W, 41, 0xa81a664bbc423001ULL);
167
+ RNDr(S, W, 42, 0xc24b8b70d0f89791ULL);
168
+ RNDr(S, W, 43, 0xc76c51a30654be30ULL);
169
+ RNDr(S, W, 44, 0xd192e819d6ef5218ULL);
170
+ RNDr(S, W, 45, 0xd69906245565a910ULL);
171
+ RNDr(S, W, 46, 0xf40e35855771202aULL);
172
+ RNDr(S, W, 47, 0x106aa07032bbd1b8ULL);
173
+ RNDr(S, W, 48, 0x19a4c116b8d2d0c8ULL);
174
+ RNDr(S, W, 49, 0x1e376c085141ab53ULL);
175
+ RNDr(S, W, 50, 0x2748774cdf8eeb99ULL);
176
+ RNDr(S, W, 51, 0x34b0bcb5e19b48a8ULL);
177
+ RNDr(S, W, 52, 0x391c0cb3c5c95a63ULL);
178
+ RNDr(S, W, 53, 0x4ed8aa4ae3418acbULL);
179
+ RNDr(S, W, 54, 0x5b9cca4f7763e373ULL);
180
+ RNDr(S, W, 55, 0x682e6ff3d6b2b8a3ULL);
181
+ RNDr(S, W, 56, 0x748f82ee5defb2fcULL);
182
+ RNDr(S, W, 57, 0x78a5636f43172f60ULL);
183
+ RNDr(S, W, 58, 0x84c87814a1f0ab72ULL);
184
+ RNDr(S, W, 59, 0x8cc702081a6439ecULL);
185
+ RNDr(S, W, 60, 0x90befffa23631e28ULL);
186
+ RNDr(S, W, 61, 0xa4506cebde82bde9ULL);
187
+ RNDr(S, W, 62, 0xbef9a3f7b2c67915ULL);
188
+ RNDr(S, W, 63, 0xc67178f2e372532bULL);
189
+ RNDr(S, W, 64, 0xca273eceea26619cULL);
190
+ RNDr(S, W, 65, 0xd186b8c721c0c207ULL);
191
+ RNDr(S, W, 66, 0xeada7dd6cde0eb1eULL);
192
+ RNDr(S, W, 67, 0xf57d4f7fee6ed178ULL);
193
+ RNDr(S, W, 68, 0x06f067aa72176fbaULL);
194
+ RNDr(S, W, 69, 0x0a637dc5a2c898a6ULL);
195
+ RNDr(S, W, 70, 0x113f9804bef90daeULL);
196
+ RNDr(S, W, 71, 0x1b710b35131c471bULL);
197
+ RNDr(S, W, 72, 0x28db77f523047d84ULL);
198
+ RNDr(S, W, 73, 0x32caab7b40c72493ULL);
199
+ RNDr(S, W, 74, 0x3c9ebe0a15c9bebcULL);
200
+ RNDr(S, W, 75, 0x431d67c49c100d4cULL);
201
+ RNDr(S, W, 76, 0x4cc5d4becb3e42b6ULL);
202
+ RNDr(S, W, 77, 0x597f299cfc657e2aULL);
203
+ RNDr(S, W, 78, 0x5fcb6fab3ad6faecULL);
204
+ RNDr(S, W, 79, 0x6c44198c4a475817ULL);
205
+
206
+ for (i = 0; i < 8; i++) {
207
+ state[i] += S[i];
208
+ }
209
+
210
+ sodium_memzero((void *) W, sizeof W);
211
+ sodium_memzero((void *) S, sizeof S);
212
+ sodium_memzero((void *) &t0, sizeof t0);
213
+ sodium_memzero((void *) &t1, sizeof t1);
214
+ }
215
+
216
+ static unsigned char PAD[128] = {
217
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
220
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
221
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
222
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
223
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
224
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
225
+ };
226
+
227
+ static void
228
+ SHA512_Pad(crypto_hash_sha512_state *state)
229
+ {
230
+ unsigned char len[16];
231
+ uint64_t r, plen;
232
+
233
+ be64enc_vect(len, state->count, 16);
234
+
235
+ r = (state->count[1] >> 3) & 0x7f;
236
+ plen = (r < 112) ? (112 - r) : (240 - r);
237
+ crypto_hash_sha512_update(state, PAD, (unsigned long long) plen);
238
+
239
+ crypto_hash_sha512_update(state, len, 16);
240
+ }
241
+
242
+ int
243
+ crypto_hash_sha512_init(crypto_hash_sha512_state *state)
244
+ {
245
+ static const uint64_t sha512_initstate[8] = {
246
+ 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
247
+ 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
248
+ 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
249
+ 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
250
+ };
251
+
252
+ state->count[0] = state->count[1] = (uint64_t) 0U;
253
+ memcpy(state->state, sha512_initstate, sizeof sha512_initstate);
254
+
255
+ return 0;
256
+ }
257
+
258
+ int
259
+ crypto_hash_sha512_update(crypto_hash_sha512_state *state,
260
+ const unsigned char *in,
261
+ unsigned long long inlen)
262
+ {
263
+ uint64_t bitlen[2];
264
+ uint64_t r;
265
+ const unsigned char *src = in;
266
+
267
+ r = (state->count[1] >> 3) & 0x7f;
268
+
269
+ bitlen[1] = ((uint64_t)inlen) << 3;
270
+ bitlen[0] = ((uint64_t)inlen) >> 61;
271
+
272
+ /* LCOV_EXCL_START */
273
+ if ((state->count[1] += bitlen[1]) < bitlen[1]) {
274
+ state->count[0]++;
275
+ }
276
+ /* LCOV_EXCL_STOP */
277
+ state->count[0] += bitlen[0];
278
+
279
+ if (inlen < 128 - r) {
280
+ memcpy(&state->buf[r], src, inlen);
281
+ return 0;
282
+ }
283
+ memcpy(&state->buf[r], src, 128 - r);
284
+ SHA512_Transform(state->state, state->buf);
285
+ src += 128 - r;
286
+ inlen -= 128 - r;
287
+
288
+ while (inlen >= 128) {
289
+ SHA512_Transform(state->state, src);
290
+ src += 128;
291
+ inlen -= 128;
292
+ }
293
+ memcpy(state->buf, src, inlen); /* inlen < 128 */
294
+
295
+ return 0;
296
+ }
297
+
298
+ int
299
+ crypto_hash_sha512_final(crypto_hash_sha512_state *state,
300
+ unsigned char *out)
301
+ {
302
+ SHA512_Pad(state);
303
+ be64enc_vect(out, state->state, 64);
304
+ sodium_memzero((void *) state, sizeof *state);
305
+
306
+ return 0;
307
+ }
308
+
309
+ int
310
+ crypto_hash_sha512(unsigned char *out, const unsigned char *in,
311
+ unsigned long long inlen)
312
+ {
313
+ crypto_hash_sha512_state state;
314
+
315
+ crypto_hash_sha512_init(&state);
316
+ crypto_hash_sha512_update(&state, in, inlen);
317
+ crypto_hash_sha512_final(&state, out);
318
+
319
+ return 0;
320
+ }
@@ -0,0 +1,27 @@
1
+ #ifndef bcrypt_pbkdf_include_h
2
+ #define bcrypt_pbkdf_include_h
3
+
4
+ #include <stdint.h>
5
+ #include <sys/types.h>
6
+ #include <stdlib.h>
7
+ #include <strings.h>
8
+
9
+ #if defined(_WIN32)
10
+
11
+ typedef uint8_t u_int8_t;
12
+ typedef uint16_t u_int16_t;
13
+ typedef uint32_t u_int32_t;
14
+
15
+ #endif
16
+
17
+ #include "blf.h"
18
+
19
+ void explicit_bzero(void *p, size_t n);
20
+ int bcrypt_pbkdf(const char *pass, size_t passlen, const uint8_t *salt, size_t saltlen,
21
+ uint8_t *key, size_t keylen, unsigned int rounds);
22
+ void bcrypt_hash(const uint8_t *sha2pass, const uint8_t *sha2salt, uint8_t *out);
23
+
24
+ #define BCRYPT_WORDS 8
25
+ #define BCRYPT_HASHSIZE (BCRYPT_WORDS * 4)
26
+
27
+ #endif
@@ -0,0 +1,13 @@
1
+ #include "crypto_hash_sha512.h"
2
+
3
+ #define SHA2_CTX crypto_hash_sha512_state
4
+
5
+ #ifdef SHA512_DIGEST_LENGTH
6
+ # undef SHA512_DIGEST_LENGTH
7
+ #endif
8
+ #define SHA512_DIGEST_LENGTH crypto_hash_sha512_BYTES
9
+
10
+ inline void SHA512Init(SHA2_CTX* ctx) { crypto_hash_sha512_init(ctx); }
11
+ inline void SHA512Update(SHA2_CTX* ctx, const void *in, size_t inlen) { crypto_hash_sha512_update(ctx, in, inlen); }
12
+ inline void SHA512Final(uint8_t* out, SHA2_CTX* ctx) { crypto_hash_sha512_final(ctx, out); }
13
+
File without changes
@@ -0,0 +1,5 @@
1
+ #include <stdlib.h>
2
+ #include <stddef.h>
3
+
4
+ void explicit_bzero(void *p, size_t n);
5
+ #define sodium_memzero explicit_bzero
@@ -0,0 +1,24 @@
1
+ begin
2
+ RUBY_VERSION =~ /(\d+.\d+)/
3
+ require "#{$1}/bcrypt_pbkdf_ext"
4
+ rescue LoadError
5
+ require "bcrypt_pbkdf_ext"
6
+ end
7
+
8
+ module BCryptPbkdf
9
+ # generates a key from a password + salt returning a string with keylen bytes
10
+ # that can be used as cryptographic key.
11
+ #
12
+ # Remember to get a good random salt of at least 16 bytes. Using a higher
13
+ # rounds count will increase the cost of an exhaustive search but will also
14
+ # make derivation proportionally slower.
15
+ #
16
+ # Example:
17
+ # rounds = 10
18
+ # keylen = 64
19
+ # @key = BCryptPbkdf.key("my secret", "my salt", keylen, rounds)
20
+ def self.key(pass,salt,keylen,rounds)
21
+ BCryptPbkdf::Engine::__bc_crypt_pbkdf(pass,salt,keylen,rounds)
22
+ end
23
+ end
24
+