scrypty 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Rakefile +18 -2
- data/ext/crypto_aesctr.c +6 -6
- data/ext/crypto_aesctr.h +6 -6
- data/ext/crypto_scrypt-nosse.c.orig +4 -4
- data/ext/crypto_scrypt-ref.c +4 -4
- data/ext/crypto_scrypt-sse.c.orig +4 -4
- data/ext/crypto_scrypt.h +2 -2
- data/ext/memlimit.c +1 -1
- data/ext/memlimit.h +2 -2
- data/ext/ruby_ext.c +4 -4
- data/ext/scryptenc.c +60 -60
- data/ext/scryptenc.h +8 -8
- data/ext/scryptenc_cpuperf.c +4 -4
- data/ext/scryptenc_cpuperf.h +2 -2
- data/ext/sha256.c +32 -32
- data/ext/sha256.h +14 -14
- data/lib/scrypty/version.rb +1 -1
- metadata +2 -2
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -13,8 +13,24 @@ file "ext/scrypty_ext.so" => FileList["ext/*.c", "ext/*.h", "ext/Makefile"] do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
file "lib/scrypty_ext.so" => "ext/scrypty_ext.so" do
|
17
|
+
cp 'ext/scrypty_ext.so', 'lib/scrypty_ext.so'
|
18
|
+
end
|
19
|
+
|
16
20
|
desc "Compile the scrypty extension"
|
17
|
-
task :compile => "
|
21
|
+
task :compile => "lib/scrypty_ext.so"
|
22
|
+
|
23
|
+
desc "Prefix C function names"
|
24
|
+
task :prefix do
|
25
|
+
names = %w{SHA256Context SHA256_CTX HMAC_SHA256Context HMAC_SHA256_CTX SHA256_Init SHA256_Update SHA256_Final HMAC_SHA256_Init HMAC_SHA256_Update HMAC_SHA256_Final PBKDF2_SHA256 crypto_aesctr_init crypto_aesctr_stream crypto_aesctr_free crypto_scrypt memtouse scryptenc_cpuperf scryptenc_buf scryptdec_buf scryptenc_file scryptdec_file}
|
26
|
+
`git ls-files ext`.chomp.split(/\s+/).each do |filename|
|
27
|
+
data = File.read(filename)
|
28
|
+
names.each do |name|
|
29
|
+
data.gsub!(/(?<!")\b#{name}\b(?!")/m, "scrypty_#{name}")
|
30
|
+
end
|
31
|
+
File.open(filename, "w") { |f| f.write(data) }
|
32
|
+
end
|
33
|
+
end
|
18
34
|
|
19
35
|
CLEAN.clear
|
20
|
-
CLEAN.include(["ext/*.o", "ext/*.so", "ext/extconf.h", "ext/Makefile"])
|
36
|
+
CLEAN.include(["ext/*.o", "ext/*.so", "lib/*.so", "ext/extconf.h", "ext/Makefile"])
|
data/ext/crypto_aesctr.c
CHANGED
@@ -45,13 +45,13 @@ struct crypto_aesctr {
|
|
45
45
|
};
|
46
46
|
|
47
47
|
/**
|
48
|
-
*
|
48
|
+
* scrypty_crypto_aesctr_init(key, nonce):
|
49
49
|
* Prepare to encrypt/decrypt data with AES in CTR mode, using the provided
|
50
50
|
* expanded key and nonce. The key provided must remain valid for the
|
51
51
|
* lifetime of the stream.
|
52
52
|
*/
|
53
53
|
struct crypto_aesctr *
|
54
|
-
|
54
|
+
scrypty_crypto_aesctr_init(AES_KEY * key, uint64_t nonce)
|
55
55
|
{
|
56
56
|
struct crypto_aesctr * stream;
|
57
57
|
|
@@ -73,13 +73,13 @@ err0:
|
|
73
73
|
}
|
74
74
|
|
75
75
|
/**
|
76
|
-
*
|
76
|
+
* scrypty_crypto_aesctr_stream(stream, inbuf, outbuf, buflen):
|
77
77
|
* Generate the next ${buflen} bytes of the AES-CTR stream and xor them with
|
78
78
|
* bytes from ${inbuf}, writing the result into ${outbuf}. If the buffers
|
79
79
|
* ${inbuf} and ${outbuf} overlap, they must be identical.
|
80
80
|
*/
|
81
81
|
void
|
82
|
-
|
82
|
+
scrypty_crypto_aesctr_stream(struct crypto_aesctr * stream, const uint8_t * inbuf,
|
83
83
|
uint8_t * outbuf, size_t buflen)
|
84
84
|
{
|
85
85
|
uint8_t pblk[16];
|
@@ -106,11 +106,11 @@ crypto_aesctr_stream(struct crypto_aesctr * stream, const uint8_t * inbuf,
|
|
106
106
|
}
|
107
107
|
|
108
108
|
/**
|
109
|
-
*
|
109
|
+
* scrypty_crypto_aesctr_free(stream):
|
110
110
|
* Free the provided stream object.
|
111
111
|
*/
|
112
112
|
void
|
113
|
-
|
113
|
+
scrypty_crypto_aesctr_free(struct crypto_aesctr * stream)
|
114
114
|
{
|
115
115
|
int i;
|
116
116
|
|
data/ext/crypto_aesctr.h
CHANGED
@@ -34,26 +34,26 @@
|
|
34
34
|
#include <openssl/aes.h>
|
35
35
|
|
36
36
|
/**
|
37
|
-
*
|
37
|
+
* scrypty_crypto_aesctr_init(key, nonce):
|
38
38
|
* Prepare to encrypt/decrypt data with AES in CTR mode, using the provided
|
39
39
|
* expanded key and nonce. The key provided must remain valid for the
|
40
40
|
* lifetime of the stream.
|
41
41
|
*/
|
42
|
-
struct crypto_aesctr *
|
42
|
+
struct crypto_aesctr * scrypty_crypto_aesctr_init(AES_KEY *, uint64_t);
|
43
43
|
|
44
44
|
/**
|
45
|
-
*
|
45
|
+
* scrypty_crypto_aesctr_stream(stream, inbuf, outbuf, buflen):
|
46
46
|
* Generate the next ${buflen} bytes of the AES-CTR stream and xor them with
|
47
47
|
* bytes from ${inbuf}, writing the result into ${outbuf}. If the buffers
|
48
48
|
* ${inbuf} and ${outbuf} overlap, they must be identical.
|
49
49
|
*/
|
50
|
-
void
|
50
|
+
void scrypty_crypto_aesctr_stream(struct crypto_aesctr *, const uint8_t *,
|
51
51
|
uint8_t *, size_t);
|
52
52
|
|
53
53
|
/**
|
54
|
-
*
|
54
|
+
* scrypty_crypto_aesctr_free(stream):
|
55
55
|
* Free the provided stream object.
|
56
56
|
*/
|
57
|
-
void
|
57
|
+
void scrypty_crypto_aesctr_free(struct crypto_aesctr *);
|
58
58
|
|
59
59
|
#endif /* !_CRYPTO_AESCTR_H_ */
|
@@ -222,7 +222,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
|
|
222
222
|
}
|
223
223
|
|
224
224
|
/**
|
225
|
-
*
|
225
|
+
* scrypty_crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
|
226
226
|
* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
|
227
227
|
* p, buflen) and write the result into buf. The parameters r, p, and buflen
|
228
228
|
* must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
|
@@ -231,7 +231,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
|
|
231
231
|
* Return 0 on success; or -1 on error.
|
232
232
|
*/
|
233
233
|
int
|
234
|
-
|
234
|
+
scrypty_crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
235
235
|
const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
|
236
236
|
uint8_t * buf, size_t buflen)
|
237
237
|
{
|
@@ -304,7 +304,7 @@ crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
|
304
304
|
#endif
|
305
305
|
|
306
306
|
/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
|
307
|
-
|
307
|
+
scrypty_PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
|
308
308
|
|
309
309
|
/* 2: for i = 0 to p - 1 do */
|
310
310
|
for (i = 0; i < p; i++) {
|
@@ -313,7 +313,7 @@ crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
|
313
313
|
}
|
314
314
|
|
315
315
|
/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
|
316
|
-
|
316
|
+
scrypty_PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
|
317
317
|
|
318
318
|
/* Free memory. */
|
319
319
|
#ifdef MAP_ANON
|
data/ext/crypto_scrypt-ref.c
CHANGED
@@ -204,7 +204,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint8_t * V, uint8_t * XY)
|
|
204
204
|
}
|
205
205
|
|
206
206
|
/**
|
207
|
-
*
|
207
|
+
* scrypty_crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
|
208
208
|
* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
|
209
209
|
* p, buflen) and write the result into buf. The parameters r, p, and buflen
|
210
210
|
* must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
|
@@ -213,7 +213,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint8_t * V, uint8_t * XY)
|
|
213
213
|
* Return 0 on success; or -1 on error.
|
214
214
|
*/
|
215
215
|
int
|
216
|
-
|
216
|
+
scrypty_crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
217
217
|
const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
|
218
218
|
uint8_t * buf, size_t buflen)
|
219
219
|
{
|
@@ -255,7 +255,7 @@ crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
|
255
255
|
goto err2;
|
256
256
|
|
257
257
|
/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
|
258
|
-
|
258
|
+
scrypty_PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
|
259
259
|
|
260
260
|
/* 2: for i = 0 to p - 1 do */
|
261
261
|
for (i = 0; i < p; i++) {
|
@@ -264,7 +264,7 @@ crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
|
264
264
|
}
|
265
265
|
|
266
266
|
/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
|
267
|
-
|
267
|
+
scrypty_PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
|
268
268
|
|
269
269
|
/* Free memory. */
|
270
270
|
free(V);
|
@@ -250,7 +250,7 @@ smix(uint8_t * B, size_t r, uint64_t N, void * V, void * XY)
|
|
250
250
|
}
|
251
251
|
|
252
252
|
/**
|
253
|
-
*
|
253
|
+
* scrypty_crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
|
254
254
|
* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
|
255
255
|
* p, buflen) and write the result into buf. The parameters r, p, and buflen
|
256
256
|
* must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
|
@@ -259,7 +259,7 @@ smix(uint8_t * B, size_t r, uint64_t N, void * V, void * XY)
|
|
259
259
|
* Return 0 on success; or -1 on error.
|
260
260
|
*/
|
261
261
|
int
|
262
|
-
|
262
|
+
scrypty_crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
263
263
|
const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
|
264
264
|
uint8_t * buf, size_t buflen)
|
265
265
|
{
|
@@ -332,7 +332,7 @@ crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
|
332
332
|
#endif
|
333
333
|
|
334
334
|
/* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
|
335
|
-
|
335
|
+
scrypty_PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
|
336
336
|
|
337
337
|
/* 2: for i = 0 to p - 1 do */
|
338
338
|
for (i = 0; i < p; i++) {
|
@@ -341,7 +341,7 @@ crypto_scrypt(const uint8_t * passwd, size_t passwdlen,
|
|
341
341
|
}
|
342
342
|
|
343
343
|
/* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
|
344
|
-
|
344
|
+
scrypty_PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
|
345
345
|
|
346
346
|
/* Free memory. */
|
347
347
|
#ifdef MAP_ANON
|
data/ext/crypto_scrypt.h
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
#include <stdint.h>
|
33
33
|
|
34
34
|
/**
|
35
|
-
*
|
35
|
+
* scrypty_crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
|
36
36
|
* Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
|
37
37
|
* p, buflen) and write the result into buf. The parameters r, p, and buflen
|
38
38
|
* must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
|
@@ -40,7 +40,7 @@
|
|
40
40
|
*
|
41
41
|
* Return 0 on success; or -1 on error.
|
42
42
|
*/
|
43
|
-
int
|
43
|
+
int scrypty_crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t,
|
44
44
|
uint32_t, uint32_t, uint8_t *, size_t);
|
45
45
|
|
46
46
|
#endif /* !_CRYPTO_SCRYPT_H_ */
|
data/ext/memlimit.c
CHANGED
@@ -233,7 +233,7 @@ memlimit_sysconf(size_t * memlimit)
|
|
233
233
|
#endif
|
234
234
|
|
235
235
|
int
|
236
|
-
|
236
|
+
scrypty_memtouse(size_t maxmem, double maxmemfrac, size_t * memlimit)
|
237
237
|
{
|
238
238
|
size_t sysctl_memlimit, sysinfo_memlimit, rlimit_memlimit;
|
239
239
|
size_t sysconf_memlimit;
|
data/ext/memlimit.h
CHANGED
@@ -32,11 +32,11 @@
|
|
32
32
|
#include <stddef.h>
|
33
33
|
|
34
34
|
/**
|
35
|
-
*
|
35
|
+
* scrypty_memtouse(maxmem, maxmemfrac, memlimit):
|
36
36
|
* Examine the system and return via memlimit the amount of RAM which should
|
37
37
|
* be used -- the specified fraction of the available RAM, but no more than
|
38
38
|
* maxmem, and no less than 1MiB.
|
39
39
|
*/
|
40
|
-
int
|
40
|
+
int scrypty_memtouse(size_t, double, size_t *);
|
41
41
|
|
42
42
|
#endif /* !_MEMLIMIT_H_ */
|
data/ext/ruby_ext.c
CHANGED
@@ -142,7 +142,7 @@ scrypty_buffer(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtim
|
|
142
142
|
rb_out = rb_str_new(NULL, out_len);
|
143
143
|
out = RSTRING_PTR(rb_out);
|
144
144
|
|
145
|
-
errorcode =
|
145
|
+
errorcode = scrypty_scryptenc_buf((const uint8_t *) data, data_len,
|
146
146
|
(uint8_t *) out, (const uint8_t *) password, password_len,
|
147
147
|
maxmem, maxmemfrac, maxtime);
|
148
148
|
}
|
@@ -150,7 +150,7 @@ scrypty_buffer(rb_obj, rb_data, rb_password, rb_maxmem, rb_maxmemfrac, rb_maxtim
|
|
150
150
|
rb_out = rb_str_new(NULL, data_len);
|
151
151
|
out = RSTRING_PTR(rb_out);
|
152
152
|
|
153
|
-
errorcode =
|
153
|
+
errorcode = scrypty_scryptdec_buf((const uint8_t *) data, data_len,
|
154
154
|
(uint8_t *) out, &out_len, (const uint8_t *) password, password_len,
|
155
155
|
maxmem, maxmemfrac, maxtime);
|
156
156
|
}
|
@@ -246,11 +246,11 @@ scrypty_file(rb_obj, rb_infn, rb_outfn, rb_password, rb_maxmem, rb_maxmemfrac, r
|
|
246
246
|
out = rb_io_stdio_file(out_p);
|
247
247
|
|
248
248
|
if (encrypt) {
|
249
|
-
errorcode =
|
249
|
+
errorcode = scrypty_scryptenc_file(in, out, (const uint8_t *) password,
|
250
250
|
password_len, maxmem, maxmemfrac, maxtime);
|
251
251
|
}
|
252
252
|
else {
|
253
|
-
errorcode =
|
253
|
+
errorcode = scrypty_scryptdec_file(in, out, (const uint8_t *) password,
|
254
254
|
password_len, maxmem, maxmemfrac, maxtime);
|
255
255
|
}
|
256
256
|
rb_io_close(rb_infile);
|
data/ext/scryptenc.c
CHANGED
@@ -64,11 +64,11 @@ pickparams(size_t maxmem, double maxmemfrac, double maxtime,
|
|
64
64
|
int rc;
|
65
65
|
|
66
66
|
/* Figure out how much memory to use. */
|
67
|
-
if (
|
67
|
+
if (scrypty_memtouse(maxmem, maxmemfrac, &memlimit))
|
68
68
|
return (1);
|
69
69
|
|
70
70
|
/* Figure out how fast the CPU is. */
|
71
|
-
if ((rc =
|
71
|
+
if ((rc = scrypty_scryptenc_cpuperf(&opps)) != 0)
|
72
72
|
return (rc);
|
73
73
|
opslimit = opps * maxtime;
|
74
74
|
|
@@ -131,11 +131,11 @@ checkparams(size_t maxmem, double maxmemfrac, double maxtime,
|
|
131
131
|
int rc;
|
132
132
|
|
133
133
|
/* Figure out the maximum amount of memory we can use. */
|
134
|
-
if (
|
134
|
+
if (scrypty_memtouse(maxmem, maxmemfrac, &memlimit))
|
135
135
|
return (1);
|
136
136
|
|
137
137
|
/* Figure out how fast the CPU is. */
|
138
|
-
if ((rc =
|
138
|
+
if ((rc = scrypty_scryptenc_cpuperf(&opps)) != 0)
|
139
139
|
return (rc);
|
140
140
|
opslimit = opps * maxtime;
|
141
141
|
|
@@ -209,9 +209,9 @@ scryptenc_setup(uint8_t header[96], uint8_t dk[64],
|
|
209
209
|
uint64_t N;
|
210
210
|
uint32_t r;
|
211
211
|
uint32_t p;
|
212
|
-
|
212
|
+
scrypty_SHA256_CTX ctx;
|
213
213
|
uint8_t * key_hmac = &dk[32];
|
214
|
-
|
214
|
+
scrypty_HMAC_SHA256_CTX hctx;
|
215
215
|
int rc;
|
216
216
|
|
217
217
|
/* Pick values for N, r, p. */
|
@@ -225,7 +225,7 @@ scryptenc_setup(uint8_t header[96], uint8_t dk[64],
|
|
225
225
|
return (rc);
|
226
226
|
|
227
227
|
/* Generate the derived keys. */
|
228
|
-
if (
|
228
|
+
if (scrypty_crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
|
229
229
|
return (3);
|
230
230
|
|
231
231
|
/* Construct the file header. */
|
@@ -237,15 +237,15 @@ scryptenc_setup(uint8_t header[96], uint8_t dk[64],
|
|
237
237
|
memcpy(&header[16], salt, 32);
|
238
238
|
|
239
239
|
/* Add header checksum. */
|
240
|
-
|
241
|
-
|
242
|
-
|
240
|
+
scrypty_SHA256_Init(&ctx);
|
241
|
+
scrypty_SHA256_Update(&ctx, header, 48);
|
242
|
+
scrypty_SHA256_Final(hbuf, &ctx);
|
243
243
|
memcpy(&header[48], hbuf, 16);
|
244
244
|
|
245
245
|
/* Add header signature (used for verifying password). */
|
246
|
-
|
247
|
-
|
248
|
-
|
246
|
+
scrypty_HMAC_SHA256_Init(&hctx, key_hmac, 32);
|
247
|
+
scrypty_HMAC_SHA256_Update(&hctx, header, 64);
|
248
|
+
scrypty_HMAC_SHA256_Final(hbuf, &hctx);
|
249
249
|
memcpy(&header[64], hbuf, 32);
|
250
250
|
|
251
251
|
/* Success! */
|
@@ -263,9 +263,9 @@ scryptdec_setup(const uint8_t header[96], uint8_t dk[64],
|
|
263
263
|
uint32_t r;
|
264
264
|
uint32_t p;
|
265
265
|
uint64_t N;
|
266
|
-
|
266
|
+
scrypty_SHA256_CTX ctx;
|
267
267
|
uint8_t * key_hmac = &dk[32];
|
268
|
-
|
268
|
+
scrypty_HMAC_SHA256_CTX hctx;
|
269
269
|
int rc;
|
270
270
|
|
271
271
|
/* Parse N, r, p, salt. */
|
@@ -275,9 +275,9 @@ scryptdec_setup(const uint8_t header[96], uint8_t dk[64],
|
|
275
275
|
memcpy(salt, &header[16], 32);
|
276
276
|
|
277
277
|
/* Verify header checksum. */
|
278
|
-
|
279
|
-
|
280
|
-
|
278
|
+
scrypty_SHA256_Init(&ctx);
|
279
|
+
scrypty_SHA256_Update(&ctx, header, 48);
|
280
|
+
scrypty_SHA256_Final(hbuf, &ctx);
|
281
281
|
if (memcmp(&header[48], hbuf, 16))
|
282
282
|
return (7);
|
283
283
|
|
@@ -291,13 +291,13 @@ scryptdec_setup(const uint8_t header[96], uint8_t dk[64],
|
|
291
291
|
|
292
292
|
/* Compute the derived keys. */
|
293
293
|
N = (uint64_t)(1) << logN;
|
294
|
-
if (
|
294
|
+
if (scrypty_crypto_scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
|
295
295
|
return (3);
|
296
296
|
|
297
297
|
/* Check header signature (i.e., verify password). */
|
298
|
-
|
299
|
-
|
300
|
-
|
298
|
+
scrypty_HMAC_SHA256_Init(&hctx, key_hmac, 32);
|
299
|
+
scrypty_HMAC_SHA256_Update(&hctx, header, 64);
|
300
|
+
scrypty_HMAC_SHA256_Final(hbuf, &hctx);
|
301
301
|
if (memcmp(hbuf, &header[64], 32))
|
302
302
|
return (11);
|
303
303
|
|
@@ -306,13 +306,13 @@ scryptdec_setup(const uint8_t header[96], uint8_t dk[64],
|
|
306
306
|
}
|
307
307
|
|
308
308
|
/**
|
309
|
-
*
|
309
|
+
* scrypty_scryptenc_buf(inbuf, inbuflen, outbuf, passwd, passwdlen,
|
310
310
|
* maxmem, maxmemfrac, maxtime):
|
311
311
|
* Encrypt inbuflen bytes from inbuf, writing the resulting inbuflen + 128
|
312
312
|
* bytes to outbuf.
|
313
313
|
*/
|
314
314
|
int
|
315
|
-
|
315
|
+
scrypty_scryptenc_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
|
316
316
|
const uint8_t * passwd, size_t passwdlen,
|
317
317
|
size_t maxmem, double maxmemfrac, double maxtime)
|
318
318
|
{
|
@@ -322,7 +322,7 @@ scryptenc_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
|
|
322
322
|
uint8_t * key_enc = dk;
|
323
323
|
uint8_t * key_hmac = &dk[32];
|
324
324
|
int rc;
|
325
|
-
|
325
|
+
scrypty_HMAC_SHA256_CTX hctx;
|
326
326
|
AES_KEY key_enc_exp;
|
327
327
|
struct crypto_aesctr * AES;
|
328
328
|
|
@@ -337,15 +337,15 @@ scryptenc_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
|
|
337
337
|
/* Encrypt data. */
|
338
338
|
if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
|
339
339
|
return (5);
|
340
|
-
if ((AES =
|
340
|
+
if ((AES = scrypty_crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
|
341
341
|
return (6);
|
342
|
-
|
343
|
-
|
342
|
+
scrypty_crypto_aesctr_stream(AES, inbuf, &outbuf[96], inbuflen);
|
343
|
+
scrypty_crypto_aesctr_free(AES);
|
344
344
|
|
345
345
|
/* Add signature. */
|
346
|
-
|
347
|
-
|
348
|
-
|
346
|
+
scrypty_HMAC_SHA256_Init(&hctx, key_hmac, 32);
|
347
|
+
scrypty_HMAC_SHA256_Update(&hctx, outbuf, 96 + inbuflen);
|
348
|
+
scrypty_HMAC_SHA256_Final(hbuf, &hctx);
|
349
349
|
memcpy(&outbuf[96 + inbuflen], hbuf, 32);
|
350
350
|
|
351
351
|
/* Zero sensitive data. */
|
@@ -357,14 +357,14 @@ scryptenc_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
|
|
357
357
|
}
|
358
358
|
|
359
359
|
/**
|
360
|
-
*
|
360
|
+
* scrypty_scryptdec_buf(inbuf, inbuflen, outbuf, outlen, passwd, passwdlen,
|
361
361
|
* maxmem, maxmemfrac, maxtime):
|
362
362
|
* Decrypt inbuflen bytes fro inbuf, writing the result into outbuf and the
|
363
363
|
* decrypted data length to outlen. The allocated length of outbuf must
|
364
364
|
* be at least inbuflen.
|
365
365
|
*/
|
366
366
|
int
|
367
|
-
|
367
|
+
scrypty_scryptdec_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
|
368
368
|
size_t * outlen, const uint8_t * passwd, size_t passwdlen,
|
369
369
|
size_t maxmem, double maxmemfrac, double maxtime)
|
370
370
|
{
|
@@ -373,7 +373,7 @@ scryptdec_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
|
|
373
373
|
uint8_t * key_enc = dk;
|
374
374
|
uint8_t * key_hmac = &dk[32];
|
375
375
|
int rc;
|
376
|
-
|
376
|
+
scrypty_HMAC_SHA256_CTX hctx;
|
377
377
|
AES_KEY key_enc_exp;
|
378
378
|
struct crypto_aesctr * AES;
|
379
379
|
|
@@ -400,16 +400,16 @@ scryptdec_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
|
|
400
400
|
/* Decrypt data. */
|
401
401
|
if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
|
402
402
|
return (5);
|
403
|
-
if ((AES =
|
403
|
+
if ((AES = scrypty_crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
|
404
404
|
return (6);
|
405
|
-
|
406
|
-
|
405
|
+
scrypty_crypto_aesctr_stream(AES, &inbuf[96], outbuf, inbuflen - 128);
|
406
|
+
scrypty_crypto_aesctr_free(AES);
|
407
407
|
*outlen = inbuflen - 128;
|
408
408
|
|
409
409
|
/* Verify signature. */
|
410
|
-
|
411
|
-
|
412
|
-
|
410
|
+
scrypty_HMAC_SHA256_Init(&hctx, key_hmac, 32);
|
411
|
+
scrypty_HMAC_SHA256_Update(&hctx, inbuf, inbuflen - 32);
|
412
|
+
scrypty_HMAC_SHA256_Final(hbuf, &hctx);
|
413
413
|
if (memcmp(hbuf, &inbuf[inbuflen - 32], 32))
|
414
414
|
return (7);
|
415
415
|
|
@@ -422,13 +422,13 @@ scryptdec_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
|
|
422
422
|
}
|
423
423
|
|
424
424
|
/**
|
425
|
-
*
|
425
|
+
* scrypty_scryptenc_file(infile, outfile, passwd, passwdlen,
|
426
426
|
* maxmem, maxmemfrac, maxtime):
|
427
427
|
* Read a stream from infile and encrypt it, writing the resulting stream to
|
428
428
|
* outfile.
|
429
429
|
*/
|
430
430
|
int
|
431
|
-
|
431
|
+
scrypty_scryptenc_file(FILE * infile, FILE * outfile,
|
432
432
|
const uint8_t * passwd, size_t passwdlen,
|
433
433
|
size_t maxmem, double maxmemfrac, double maxtime)
|
434
434
|
{
|
@@ -439,7 +439,7 @@ scryptenc_file(FILE * infile, FILE * outfile,
|
|
439
439
|
uint8_t * key_enc = dk;
|
440
440
|
uint8_t * key_hmac = &dk[32];
|
441
441
|
size_t readlen;
|
442
|
-
|
442
|
+
scrypty_HMAC_SHA256_CTX hctx;
|
443
443
|
AES_KEY key_enc_exp;
|
444
444
|
struct crypto_aesctr * AES;
|
445
445
|
int rc;
|
@@ -450,8 +450,8 @@ scryptenc_file(FILE * infile, FILE * outfile,
|
|
450
450
|
return (rc);
|
451
451
|
|
452
452
|
/* Hash and write the header. */
|
453
|
-
|
454
|
-
|
453
|
+
scrypty_HMAC_SHA256_Init(&hctx, key_hmac, 32);
|
454
|
+
scrypty_HMAC_SHA256_Update(&hctx, header, 96);
|
455
455
|
if (fwrite(header, 96, 1, outfile) != 1)
|
456
456
|
return (12);
|
457
457
|
|
@@ -461,24 +461,24 @@ scryptenc_file(FILE * infile, FILE * outfile,
|
|
461
461
|
*/
|
462
462
|
if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
|
463
463
|
return (5);
|
464
|
-
if ((AES =
|
464
|
+
if ((AES = scrypty_crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
|
465
465
|
return (6);
|
466
466
|
do {
|
467
467
|
if ((readlen = fread(buf, 1, ENCBLOCK, infile)) == 0)
|
468
468
|
break;
|
469
|
-
|
470
|
-
|
469
|
+
scrypty_crypto_aesctr_stream(AES, buf, buf, readlen);
|
470
|
+
scrypty_HMAC_SHA256_Update(&hctx, buf, readlen);
|
471
471
|
if (fwrite(buf, 1, readlen, outfile) < readlen)
|
472
472
|
return (12);
|
473
473
|
} while (1);
|
474
|
-
|
474
|
+
scrypty_crypto_aesctr_free(AES);
|
475
475
|
|
476
476
|
/* Did we exit the loop due to a read error? */
|
477
477
|
if (ferror(infile))
|
478
478
|
return (13);
|
479
479
|
|
480
480
|
/* Compute the final HMAC and output it. */
|
481
|
-
|
481
|
+
scrypty_HMAC_SHA256_Final(hbuf, &hctx);
|
482
482
|
if (fwrite(hbuf, 32, 1, outfile) != 1)
|
483
483
|
return (12);
|
484
484
|
|
@@ -491,13 +491,13 @@ scryptenc_file(FILE * infile, FILE * outfile,
|
|
491
491
|
}
|
492
492
|
|
493
493
|
/**
|
494
|
-
*
|
494
|
+
* scrypty_scryptdec_file(infile, outfile, passwd, passwdlen,
|
495
495
|
* maxmem, maxmemfrac, maxtime):
|
496
496
|
* Read a stream from infile and decrypt it, writing the resulting stream to
|
497
497
|
* outfile.
|
498
498
|
*/
|
499
499
|
int
|
500
|
-
|
500
|
+
scrypty_scryptdec_file(FILE * infile, FILE * outfile,
|
501
501
|
const uint8_t * passwd, size_t passwdlen,
|
502
502
|
size_t maxmem, double maxmemfrac, double maxtime)
|
503
503
|
{
|
@@ -509,7 +509,7 @@ scryptdec_file(FILE * infile, FILE * outfile,
|
|
509
509
|
uint8_t * key_hmac = &dk[32];
|
510
510
|
size_t buflen = 0;
|
511
511
|
size_t readlen;
|
512
|
-
|
512
|
+
scrypty_HMAC_SHA256_CTX hctx;
|
513
513
|
AES_KEY key_enc_exp;
|
514
514
|
struct crypto_aesctr * AES;
|
515
515
|
int rc;
|
@@ -548,8 +548,8 @@ scryptdec_file(FILE * infile, FILE * outfile,
|
|
548
548
|
return (rc);
|
549
549
|
|
550
550
|
/* Start hashing with the header. */
|
551
|
-
|
552
|
-
|
551
|
+
scrypty_HMAC_SHA256_Init(&hctx, key_hmac, 32);
|
552
|
+
scrypty_HMAC_SHA256_Update(&hctx, header, 96);
|
553
553
|
|
554
554
|
/*
|
555
555
|
* We don't know how long the encrypted data block is (we can't know,
|
@@ -559,7 +559,7 @@ scryptdec_file(FILE * infile, FILE * outfile,
|
|
559
559
|
*/
|
560
560
|
if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
|
561
561
|
return (5);
|
562
|
-
if ((AES =
|
562
|
+
if ((AES = scrypty_crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
|
563
563
|
return (6);
|
564
564
|
do {
|
565
565
|
/* Read data until we have more than 32 bytes of it. */
|
@@ -574,8 +574,8 @@ scryptdec_file(FILE * infile, FILE * outfile,
|
|
574
574
|
* Decrypt, hash, and output everything except the last 32
|
575
575
|
* bytes out of what we have in our buffer.
|
576
576
|
*/
|
577
|
-
|
578
|
-
|
577
|
+
scrypty_HMAC_SHA256_Update(&hctx, buf, buflen - 32);
|
578
|
+
scrypty_crypto_aesctr_stream(AES, buf, buf, buflen - 32);
|
579
579
|
if (fwrite(buf, 1, buflen - 32, outfile) < buflen - 32)
|
580
580
|
return (12);
|
581
581
|
|
@@ -583,7 +583,7 @@ scryptdec_file(FILE * infile, FILE * outfile,
|
|
583
583
|
memmove(buf, &buf[buflen - 32], 32);
|
584
584
|
buflen = 32;
|
585
585
|
} while (1);
|
586
|
-
|
586
|
+
scrypty_crypto_aesctr_free(AES);
|
587
587
|
|
588
588
|
/* Did we exit the loop due to a read error? */
|
589
589
|
if (ferror(infile))
|
@@ -594,7 +594,7 @@ scryptdec_file(FILE * infile, FILE * outfile,
|
|
594
594
|
return (7);
|
595
595
|
|
596
596
|
/* Verify signature. */
|
597
|
-
|
597
|
+
scrypty_HMAC_SHA256_Final(hbuf, &hctx);
|
598
598
|
if (memcmp(hbuf, buf, 32))
|
599
599
|
return (7);
|
600
600
|
|
data/ext/scryptenc.h
CHANGED
@@ -73,40 +73,40 @@
|
|
73
73
|
*/
|
74
74
|
|
75
75
|
/**
|
76
|
-
*
|
76
|
+
* scrypty_scryptenc_buf(inbuf, inbuflen, outbuf, passwd, passwdlen,
|
77
77
|
* maxmem, maxmemfrac, maxtime):
|
78
78
|
* Encrypt inbuflen bytes from inbuf, writing the resulting inbuflen + 128
|
79
79
|
* bytes to outbuf.
|
80
80
|
*/
|
81
|
-
int
|
81
|
+
int scrypty_scryptenc_buf(const uint8_t *, size_t, uint8_t *,
|
82
82
|
const uint8_t *, size_t, size_t, double, double);
|
83
83
|
|
84
84
|
/**
|
85
|
-
*
|
85
|
+
* scrypty_scryptdec_buf(inbuf, inbuflen, outbuf, outlen, passwd, passwdlen,
|
86
86
|
* maxmem, maxmemfrac, maxtime):
|
87
87
|
* Decrypt inbuflen bytes from inbuf, writing the result into outbuf and the
|
88
88
|
* decrypted data length to outlen. The allocated length of outbuf must
|
89
89
|
* be at least inbuflen.
|
90
90
|
*/
|
91
|
-
int
|
91
|
+
int scrypty_scryptdec_buf(const uint8_t *, size_t, uint8_t *, size_t *,
|
92
92
|
const uint8_t *, size_t, size_t, double, double);
|
93
93
|
|
94
94
|
/**
|
95
|
-
*
|
95
|
+
* scrypty_scryptenc_file(infile, outfile, passwd, passwdlen,
|
96
96
|
* maxmem, maxmemfrac, maxtime):
|
97
97
|
* Read a stream from infile and encrypt it, writing the resulting stream to
|
98
98
|
* outfile.
|
99
99
|
*/
|
100
|
-
int
|
100
|
+
int scrypty_scryptenc_file(FILE *, FILE *, const uint8_t *, size_t,
|
101
101
|
size_t, double, double);
|
102
102
|
|
103
103
|
/**
|
104
|
-
*
|
104
|
+
* scrypty_scryptdec_file(infile, outfile, passwd, passwdlen,
|
105
105
|
* maxmem, maxmemfrac, maxtime):
|
106
106
|
* Read a stream from infile and decrypt it, writing the resulting stream to
|
107
107
|
* outfile.
|
108
108
|
*/
|
109
|
-
int
|
109
|
+
int scrypty_scryptdec_file(FILE *, FILE *, const uint8_t *, size_t,
|
110
110
|
size_t, double, double);
|
111
111
|
|
112
112
|
#endif /* !_SCRYPTENC_H_ */
|
data/ext/scryptenc_cpuperf.c
CHANGED
@@ -122,12 +122,12 @@ getclockdiff(struct timespec * st, double * diffd)
|
|
122
122
|
}
|
123
123
|
|
124
124
|
/**
|
125
|
-
*
|
125
|
+
* scrypty_scryptenc_cpuperf(opps):
|
126
126
|
* Estimate the number of salsa20/8 cores which can be executed per second,
|
127
127
|
* and return the value via opps.
|
128
128
|
*/
|
129
129
|
int
|
130
|
-
|
130
|
+
scrypty_scryptenc_cpuperf(double * opps)
|
131
131
|
{
|
132
132
|
struct timespec st;
|
133
133
|
double resd, diffd;
|
@@ -146,7 +146,7 @@ scryptenc_cpuperf(double * opps)
|
|
146
146
|
return (2);
|
147
147
|
do {
|
148
148
|
/* Do an scrypt. */
|
149
|
-
if (
|
149
|
+
if (scrypty_crypto_scrypt(NULL, 0, NULL, 0, 16, 1, 1, NULL, 0))
|
150
150
|
return (3);
|
151
151
|
|
152
152
|
/* Has the clock ticked? */
|
@@ -161,7 +161,7 @@ scryptenc_cpuperf(double * opps)
|
|
161
161
|
return (2);
|
162
162
|
do {
|
163
163
|
/* Do an scrypt. */
|
164
|
-
if (
|
164
|
+
if (scrypty_crypto_scrypt(NULL, 0, NULL, 0, 128, 1, 1, NULL, 0))
|
165
165
|
return (3);
|
166
166
|
|
167
167
|
/* We invoked the salsa20/8 core 512 times. */
|
data/ext/scryptenc_cpuperf.h
CHANGED
@@ -30,10 +30,10 @@
|
|
30
30
|
#define _SCRYPTENC_CPUPERF_H_
|
31
31
|
|
32
32
|
/**
|
33
|
-
*
|
33
|
+
* scrypty_scryptenc_cpuperf(opps):
|
34
34
|
* Estimate the number of salsa20/8 cores which can be executed per second,
|
35
35
|
* and return the value via opps.
|
36
36
|
*/
|
37
|
-
int
|
37
|
+
int scrypty_scryptenc_cpuperf(double *);
|
38
38
|
|
39
39
|
#endif /* !_SCRYPTENC_CPUPERF_H_ */
|
data/ext/sha256.c
CHANGED
@@ -190,7 +190,7 @@ static unsigned char PAD[64] = {
|
|
190
190
|
|
191
191
|
/* Add padding and terminating bit-count. */
|
192
192
|
static void
|
193
|
-
SHA256_Pad(
|
193
|
+
SHA256_Pad(scrypty_SHA256_CTX * ctx)
|
194
194
|
{
|
195
195
|
unsigned char len[8];
|
196
196
|
uint32_t r, plen;
|
@@ -204,15 +204,15 @@ SHA256_Pad(SHA256_CTX * ctx)
|
|
204
204
|
/* Add 1--64 bytes so that the resulting length is 56 mod 64 */
|
205
205
|
r = (ctx->count[1] >> 3) & 0x3f;
|
206
206
|
plen = (r < 56) ? (56 - r) : (120 - r);
|
207
|
-
|
207
|
+
scrypty_SHA256_Update(ctx, PAD, (size_t)plen);
|
208
208
|
|
209
209
|
/* Add the terminating bit-count */
|
210
|
-
|
210
|
+
scrypty_SHA256_Update(ctx, len, 8);
|
211
211
|
}
|
212
212
|
|
213
213
|
/* SHA-256 initialization. Begins a SHA-256 operation. */
|
214
214
|
void
|
215
|
-
|
215
|
+
scrypty_SHA256_Init(scrypty_SHA256_CTX * ctx)
|
216
216
|
{
|
217
217
|
|
218
218
|
/* Zero bits processed so far */
|
@@ -231,7 +231,7 @@ SHA256_Init(SHA256_CTX * ctx)
|
|
231
231
|
|
232
232
|
/* Add bytes into the hash */
|
233
233
|
void
|
234
|
-
|
234
|
+
scrypty_SHA256_Update(scrypty_SHA256_CTX * ctx, const void *in, size_t len)
|
235
235
|
{
|
236
236
|
uint32_t bitlen[2];
|
237
237
|
uint32_t r;
|
@@ -277,7 +277,7 @@ SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len)
|
|
277
277
|
* and clears the context state.
|
278
278
|
*/
|
279
279
|
void
|
280
|
-
|
280
|
+
scrypty_SHA256_Final(unsigned char digest[32], scrypty_SHA256_CTX * ctx)
|
281
281
|
{
|
282
282
|
|
283
283
|
/* Add padding */
|
@@ -292,7 +292,7 @@ SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx)
|
|
292
292
|
|
293
293
|
/* Initialize an HMAC-SHA256 operation with the given key. */
|
294
294
|
void
|
295
|
-
|
295
|
+
scrypty_HMAC_SHA256_Init(scrypty_HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
|
296
296
|
{
|
297
297
|
unsigned char pad[64];
|
298
298
|
unsigned char khash[32];
|
@@ -301,26 +301,26 @@ HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
|
|
301
301
|
|
302
302
|
/* If Klen > 64, the key is really SHA256(K). */
|
303
303
|
if (Klen > 64) {
|
304
|
-
|
305
|
-
|
306
|
-
|
304
|
+
scrypty_SHA256_Init(&ctx->ictx);
|
305
|
+
scrypty_SHA256_Update(&ctx->ictx, K, Klen);
|
306
|
+
scrypty_SHA256_Final(khash, &ctx->ictx);
|
307
307
|
K = khash;
|
308
308
|
Klen = 32;
|
309
309
|
}
|
310
310
|
|
311
311
|
/* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
|
312
|
-
|
312
|
+
scrypty_SHA256_Init(&ctx->ictx);
|
313
313
|
memset(pad, 0x36, 64);
|
314
314
|
for (i = 0; i < Klen; i++)
|
315
315
|
pad[i] ^= K[i];
|
316
|
-
|
316
|
+
scrypty_SHA256_Update(&ctx->ictx, pad, 64);
|
317
317
|
|
318
318
|
/* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
|
319
|
-
|
319
|
+
scrypty_SHA256_Init(&ctx->octx);
|
320
320
|
memset(pad, 0x5c, 64);
|
321
321
|
for (i = 0; i < Klen; i++)
|
322
322
|
pad[i] ^= K[i];
|
323
|
-
|
323
|
+
scrypty_SHA256_Update(&ctx->octx, pad, 64);
|
324
324
|
|
325
325
|
/* Clean the stack. */
|
326
326
|
memset(khash, 0, 32);
|
@@ -328,42 +328,42 @@ HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
|
|
328
328
|
|
329
329
|
/* Add bytes to the HMAC-SHA256 operation. */
|
330
330
|
void
|
331
|
-
|
331
|
+
scrypty_HMAC_SHA256_Update(scrypty_HMAC_SHA256_CTX * ctx, const void *in, size_t len)
|
332
332
|
{
|
333
333
|
|
334
334
|
/* Feed data to the inner SHA256 operation. */
|
335
|
-
|
335
|
+
scrypty_SHA256_Update(&ctx->ictx, in, len);
|
336
336
|
}
|
337
337
|
|
338
338
|
/* Finish an HMAC-SHA256 operation. */
|
339
339
|
void
|
340
|
-
|
340
|
+
scrypty_HMAC_SHA256_Final(unsigned char digest[32], scrypty_HMAC_SHA256_CTX * ctx)
|
341
341
|
{
|
342
342
|
unsigned char ihash[32];
|
343
343
|
|
344
344
|
/* Finish the inner SHA256 operation. */
|
345
|
-
|
345
|
+
scrypty_SHA256_Final(ihash, &ctx->ictx);
|
346
346
|
|
347
347
|
/* Feed the inner hash to the outer SHA256 operation. */
|
348
|
-
|
348
|
+
scrypty_SHA256_Update(&ctx->octx, ihash, 32);
|
349
349
|
|
350
350
|
/* Finish the outer SHA256 operation. */
|
351
|
-
|
351
|
+
scrypty_SHA256_Final(digest, &ctx->octx);
|
352
352
|
|
353
353
|
/* Clean the stack. */
|
354
354
|
memset(ihash, 0, 32);
|
355
355
|
}
|
356
356
|
|
357
357
|
/**
|
358
|
-
*
|
358
|
+
* scrypty_PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
|
359
359
|
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
|
360
360
|
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
|
361
361
|
*/
|
362
362
|
void
|
363
|
-
|
363
|
+
scrypty_PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
|
364
364
|
size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
|
365
365
|
{
|
366
|
-
|
366
|
+
scrypty_HMAC_SHA256_CTX PShctx, hctx;
|
367
367
|
size_t i;
|
368
368
|
uint8_t ivec[4];
|
369
369
|
uint8_t U[32];
|
@@ -373,8 +373,8 @@ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
|
|
373
373
|
size_t clen;
|
374
374
|
|
375
375
|
/* Compute HMAC state after processing P and S. */
|
376
|
-
|
377
|
-
|
376
|
+
scrypty_HMAC_SHA256_Init(&PShctx, passwd, passwdlen);
|
377
|
+
scrypty_HMAC_SHA256_Update(&PShctx, salt, saltlen);
|
378
378
|
|
379
379
|
/* Iterate through the blocks. */
|
380
380
|
for (i = 0; i * 32 < dkLen; i++) {
|
@@ -382,18 +382,18 @@ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
|
|
382
382
|
be32enc(ivec, (uint32_t)(i + 1));
|
383
383
|
|
384
384
|
/* Compute U_1 = PRF(P, S || INT(i)). */
|
385
|
-
memcpy(&hctx, &PShctx, sizeof(
|
386
|
-
|
387
|
-
|
385
|
+
memcpy(&hctx, &PShctx, sizeof(scrypty_HMAC_SHA256_CTX));
|
386
|
+
scrypty_HMAC_SHA256_Update(&hctx, ivec, 4);
|
387
|
+
scrypty_HMAC_SHA256_Final(U, &hctx);
|
388
388
|
|
389
389
|
/* T_i = U_1 ... */
|
390
390
|
memcpy(T, U, 32);
|
391
391
|
|
392
392
|
for (j = 2; j <= c; j++) {
|
393
393
|
/* Compute U_j. */
|
394
|
-
|
395
|
-
|
396
|
-
|
394
|
+
scrypty_HMAC_SHA256_Init(&hctx, passwd, passwdlen);
|
395
|
+
scrypty_HMAC_SHA256_Update(&hctx, U, 32);
|
396
|
+
scrypty_HMAC_SHA256_Final(U, &hctx);
|
397
397
|
|
398
398
|
/* ... xor U_j ... */
|
399
399
|
for (k = 0; k < 32; k++)
|
@@ -408,5 +408,5 @@ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
|
|
408
408
|
}
|
409
409
|
|
410
410
|
/* Clean PShctx, since we never called _Final on it. */
|
411
|
-
memset(&PShctx, 0, sizeof(
|
411
|
+
memset(&PShctx, 0, sizeof(scrypty_HMAC_SHA256_CTX));
|
412
412
|
}
|
data/ext/sha256.h
CHANGED
@@ -33,30 +33,30 @@
|
|
33
33
|
|
34
34
|
#include <stdint.h>
|
35
35
|
|
36
|
-
typedef struct
|
36
|
+
typedef struct scrypty_SHA256Context {
|
37
37
|
uint32_t state[8];
|
38
38
|
uint32_t count[2];
|
39
39
|
unsigned char buf[64];
|
40
|
-
}
|
40
|
+
} scrypty_SHA256_CTX;
|
41
41
|
|
42
|
-
typedef struct
|
43
|
-
|
44
|
-
|
45
|
-
}
|
42
|
+
typedef struct scrypty_HMAC_SHA256Context {
|
43
|
+
scrypty_SHA256_CTX ictx;
|
44
|
+
scrypty_SHA256_CTX octx;
|
45
|
+
} scrypty_HMAC_SHA256_CTX;
|
46
46
|
|
47
|
-
void
|
48
|
-
void
|
49
|
-
void
|
50
|
-
void
|
51
|
-
void
|
52
|
-
void
|
47
|
+
void scrypty_SHA256_Init(scrypty_SHA256_CTX *);
|
48
|
+
void scrypty_SHA256_Update(scrypty_SHA256_CTX *, const void *, size_t);
|
49
|
+
void scrypty_SHA256_Final(unsigned char [32], scrypty_SHA256_CTX *);
|
50
|
+
void scrypty_HMAC_SHA256_Init(scrypty_HMAC_SHA256_CTX *, const void *, size_t);
|
51
|
+
void scrypty_HMAC_SHA256_Update(scrypty_HMAC_SHA256_CTX *, const void *, size_t);
|
52
|
+
void scrypty_HMAC_SHA256_Final(unsigned char [32], scrypty_HMAC_SHA256_CTX *);
|
53
53
|
|
54
54
|
/**
|
55
|
-
*
|
55
|
+
* scrypty_PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
|
56
56
|
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
|
57
57
|
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
|
58
58
|
*/
|
59
|
-
void
|
59
|
+
void scrypty_PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
|
60
60
|
uint64_t, uint8_t *, size_t);
|
61
61
|
|
62
62
|
#endif /* !_SHA256_H_ */
|
data/lib/scrypty/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scrypty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-10 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Uses the scrypt algorithm by Colin Percival to encrypt/decrypt data
|
15
15
|
email:
|