forked from sodium-friends/sodium-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_secretbox.js
78 lines (54 loc) · 2.97 KB
/
crypto_secretbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
var tape = require('tape')
module.exports = function (sodium) {
tape('crypto_secretbox_easy', function (t) {
var message = Buffer.from('Hej, Verden!')
var output = Buffer.alloc(message.length + sodium.crypto_secretbox_MACBYTES)
var key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
sodium.randombytes_buf(key)
var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
sodium.randombytes_buf(nonce)
t.throws(function () {
sodium.crypto_secretbox_easy(Buffer.alloc(0), message, nonce, key)
}, 'throws if output is too small')
t.throws(function () {
sodium.crypto_secretbox_easy(Buffer.alloc(message.length), message, nonce, key)
}, 'throws if output is too small')
sodium.crypto_secretbox_easy(output, message, nonce, key)
t.notEqual(output, Buffer.alloc(output.length))
var result = Buffer.alloc(output.length - sodium.crypto_secretbox_MACBYTES)
t.notOk(sodium.crypto_secretbox_open_easy(result, output, Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES), key), 'could not decrypt')
t.ok(sodium.crypto_secretbox_open_easy(result, output, nonce, key), 'could decrypt')
t.same(result, message, 'decrypted message is correct')
t.end()
})
tape('crypto_secretbox_easy overwrite buffer', function (t) {
var output = Buffer.alloc(Buffer.byteLength('Hej, Verden!') + sodium.crypto_secretbox_MACBYTES)
output.write('Hej, Verden!', sodium.crypto_secretbox_MACBYTES)
var key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
sodium.randombytes_buf(key)
var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
sodium.randombytes_buf(nonce)
sodium.crypto_secretbox_easy(output, output.slice(sodium.crypto_secretbox_MACBYTES), nonce, key)
t.notEqual(output, Buffer.alloc(output.length))
t.ok(sodium.crypto_secretbox_open_easy(output.slice(sodium.crypto_secretbox_MACBYTES), output, nonce, key), 'could decrypt')
t.same(output.slice(sodium.crypto_secretbox_MACBYTES), Buffer.from('Hej, Verden!'), 'decrypted message is correct')
t.end()
})
tape('crypto_secretbox_detached', function (t) {
var message = Buffer.from('Hej, Verden!')
var output = Buffer.alloc(message.length)
var mac = Buffer.alloc(sodium.crypto_secretbox_MACBYTES)
var key = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES)
sodium.randombytes_buf(key)
var nonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES)
sodium.randombytes_buf(nonce)
sodium.crypto_secretbox_detached(output, mac, message, nonce, key)
t.notEqual(mac, Buffer.alloc(mac.length), 'mac not blank')
t.notEqual(output, Buffer.alloc(output.length), 'output not blank')
var result = Buffer.alloc(output.length)
t.notOk(sodium.crypto_secretbox_open_detached(result, output, mac, nonce, Buffer.alloc(key.length)), 'could not decrypt')
t.ok(sodium.crypto_secretbox_open_detached(result, output, mac, nonce, key), 'could decrypt')
t.same(result, message, 'decrypted message is correct')
t.end()
})
}