-
Notifications
You must be signed in to change notification settings - Fork 12
/
crypto_aead_xchacha20poly1305.js
192 lines (162 loc) · 8.67 KB
/
crypto_aead_xchacha20poly1305.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const tape = require('tape')
const exp = [
new Uint8Array([
0xf8, 0xeb, 0xea, 0x48, 0x75, 0x04, 0x40, 0x66,
0xfc, 0x16, 0x2a, 0x06, 0x04, 0xe1, 0x71, 0xfe,
0xec, 0xfb, 0x3d, 0x20, 0x42, 0x52, 0x48, 0x56,
0x3b, 0xcf, 0xd5, 0xa1, 0x55, 0xdc, 0xc4, 0x7b,
0xbd, 0xa7, 0x0b, 0x86, 0xe5, 0xab, 0x9b, 0x55,
0x00, 0x2b, 0xd1, 0x27, 0x4c, 0x02, 0xdb, 0x35,
0x32, 0x1a, 0xcd, 0x7a, 0xf8, 0xb2, 0xe2, 0xd2,
0x50, 0x15, 0xe1, 0x36, 0xb7, 0x67, 0x94, 0x58,
0xe9, 0xf4, 0x32, 0x43, 0xbf, 0x71, 0x9d, 0x63,
0x9b, 0xad, 0xb5, 0xfe, 0xac, 0x03, 0xf8, 0x0a,
0x19, 0xa9, 0x6e, 0xf1, 0x0c, 0xb1, 0xd1, 0x53,
0x33, 0xa8, 0x37, 0xb9, 0x09, 0x46, 0xba, 0x38,
0x54, 0xee, 0x74, 0xda, 0x3f, 0x25, 0x85, 0xef,
0xc7, 0xe1, 0xe1, 0x70, 0xe1, 0x7e, 0x15, 0xe5,
0x63, 0xe7, 0x76, 0x01, 0xf4, 0xf8, 0x5c, 0xaf,
0xa8, 0xe5, 0x87, 0x76, 0x14, 0xe1, 0x43, 0xe6,
0x84, 0x20
]),
new Uint8Array([
0xf8, 0xeb, 0xea, 0x48, 0x75, 0x04, 0x40, 0x66,
0xfc, 0x16, 0x2a, 0x06, 0x04, 0xe1, 0x71, 0xfe,
0xec, 0xfb, 0x3d, 0x20, 0x42, 0x52, 0x48, 0x56,
0x3b, 0xcf, 0xd5, 0xa1, 0x55, 0xdc, 0xc4, 0x7b,
0xbd, 0xa7, 0x0b, 0x86, 0xe5, 0xab, 0x9b, 0x55,
0x00, 0x2b, 0xd1, 0x27, 0x4c, 0x02, 0xdb, 0x35,
0x32, 0x1a, 0xcd, 0x7a, 0xf8, 0xb2, 0xe2, 0xd2,
0x50, 0x15, 0xe1, 0x36, 0xb7, 0x67, 0x94, 0x58,
0xe9, 0xf4, 0x32, 0x43, 0xbf, 0x71, 0x9d, 0x63,
0x9b, 0xad, 0xb5, 0xfe, 0xac, 0x03, 0xf8, 0x0a,
0x19, 0xa9, 0x6e, 0xf1, 0x0c, 0xb1, 0xd1, 0x53,
0x33, 0xa8, 0x37, 0xb9, 0x09, 0x46, 0xba, 0x38,
0x54, 0xee, 0x74, 0xda, 0x3f, 0x25, 0x85, 0xef,
0xc7, 0xe1, 0xe1, 0x70, 0xe1, 0x7e, 0x15, 0xe5,
0x63, 0xe7, 0xe0, 0x96, 0xe0, 0x33, 0xd9, 0x1b,
0x63, 0xf7, 0xac, 0x92, 0xe9, 0x97, 0x2e, 0x0d,
0x43, 0xe5
]),
new Uint8Array([
0xf8, 0xeb, 0xea, 0x48, 0x75, 0x04, 0x40, 0x66,
0xfc, 0x16, 0x2a, 0x06, 0x04, 0xe1, 0x71, 0xfe,
0xec, 0xfb, 0x3d, 0x20, 0x42, 0x52, 0x48, 0x56,
0x3b, 0xcf, 0xd5, 0xa1, 0x55, 0xdc, 0xc4, 0x7b,
0xbd, 0xa7, 0x0b, 0x86, 0xe5, 0xab, 0x9b, 0x55,
0x00, 0x2b, 0xd1, 0x27, 0x4c, 0x02, 0xdb, 0x35,
0x32, 0x1a, 0xcd, 0x7a, 0xf8, 0xb2, 0xe2, 0xd2,
0x50, 0x15, 0xe1, 0x36, 0xb7, 0x67, 0x94, 0x58,
0xe9, 0xf4, 0x32, 0x43, 0xbf, 0x71, 0x9d, 0x63,
0x9b, 0xad, 0xb5, 0xfe, 0xac, 0x03, 0xf8, 0x0a,
0x19, 0xa9, 0x6e, 0xf1, 0x0c, 0xb1, 0xd1, 0x53,
0x33, 0xa8, 0x37, 0xb9, 0x09, 0x46, 0xba, 0x38,
0x54, 0xee, 0x74, 0xda, 0x3f, 0x25, 0x85, 0xef,
0xc7, 0xe1, 0xe1, 0x70, 0xe1, 0x7e, 0x15, 0xe5,
0x63, 0xe7, 0xe0, 0x96, 0xe0, 0x33, 0xd9, 0x1b,
0x63, 0xf7, 0xac, 0x92, 0xe9, 0x97, 0x2e, 0x0d,
0x43, 0xe5
])
]
module.exports = crypto_aead_xchacha20poly1305_ietf
function crypto_aead_xchacha20poly1305_ietf (sodium) {
tape('crypto_aead_xchacha20poly1305_ietf', t => {
const MLEN = 114
const ADLEN = 12
const CLEN = (MLEN + sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES)
const firstkey = new Uint8Array([
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
])
const MESSAGE = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."
const m = new Uint8Array(MLEN);
const nonce = new Uint8Array([
0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53
])
const ad = new Uint8Array([ 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 ])
const c = new Uint8Array(CLEN);
const detached_c = new Uint8Array(MLEN);
const key2 = new Uint8Array(sodium.crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
const mac = new Uint8Array(sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES);
const m2 = new Uint8Array(MLEN);
let found_clen;
let found_maclen;
let m2len;
let i;
t.assert(MESSAGE.length === MLEN)
m.set([
0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65,
0x6e, 0x74, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68,
0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39,
0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, 0x6f, 0x75, 0x6c, 0x64,
0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, 0x6e,
0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f,
0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c,
0x20, 0x73, 0x75, 0x6e, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, 0x74, 0x2e
])
found_clen = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(c, m, ad, null, nonce, firstkey);
t.assert(found_clen === MLEN + sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES, "found_clen should be properly set")
t.same(c, exp[0])
// found_maclen = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt_detached(detached_c, mac, m, ad, null, nonce, firstkey);
// t.assert(found_maclen === sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES, "found_maclen should be properly set")
// t.same(detached_c, c.subarray(0, MLEN), "detached ciphertext is good\n")
t.throws(() => sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(Buffer.alloc(0), null, c, ad, nonce, firstkey),
"null message throws")
t.throws(() => sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(Buffer.alloc(0), null, c, ad, nonce, firstkey),
"short message throws")
t.doesNotThrow(() => m2len = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(m2, null, c, ad, nonce, firstkey),
"sodium.crypto_aead_xchacha20poly1305_ietf_decrypt() should pass")
t.equal(m2len, MLEN, "m2len is properly set")
t.same(m, m2, "m === m2");
// m2.fill(0)
// t.doesNotThrow(() => sodium.crypto_aead_xchacha20poly1305_ietf_decrypt_detached(m2, null, c.subarray(0, MLEN), mac, ad, nonce, firstkey),
// "sodium.crypto_aead_xchacha20poly1305_ietf_decrypt_detached() should pass")
// t.same(m, m2, "m === m2");
for (i = 0; i < CLEN; i++) {
c[i] ^= (i + 1);
try {
sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(m2, null, c, ad, nonce, firstkey)
if (Buffer.compare(m, m2) !== 0) throw new Error()
t.fail('message can be forged')
} catch {
c[i] ^= (i + 1)
}
}
found_clen = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(c, m, null, null, nonce, firstkey);
t.equal(found_clen, CLEN, "found_clen is properly set (adlen = 0)")
t.same(c, exp[1])
t.doesNotThrow(() => m2len = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(m2, null, c, null, nonce, firstkey),
"sodium.crypto_aead_xchacha20poly1305_ietf_decrypt() should pass (adlen=0)\n")
t.equal(m2len, MLEN, "m2len is properly set (adlen=0)")
t.same(m, m2, "m === m2 (adlen=0)")
m2len = 1
t.throws(() => sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
m2.subarray(0, 1), null, Buffer.alloc(32),
sodium.randombytes_uniform(sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES),
null, nonce, firstkey),
"sodium.crypto_aead_xchacha20poly1305_ietf_decrypt() throws with a short ciphertext")
t.throws(() => sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(m2, null, c.subarray(0, 0), null, nonce, firstkey),
"sodium.crypto_aead_xchacha20poly1305_ietf_decrypt() worked with an empty ciphertext")
c.set(m)
found_clen = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(c, c.subarray(0, MLEN), null, null, nonce, firstkey)
t.equal(found_clen, CLEN, "clen is properly set (adlen=0)")
t.same(c, exp[2])
t.doesNotThrow(() => m2len = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(c.subarray(0, MLEN), null, c, null, nonce, firstkey),
"sodium.crypto_aead_xchacha20poly1305_ietf_decrypt() should pass (adlen=0)")
t.equal(m2len, MLEN, "m2len is properly set (adlen=0)")
t.same(m, c.subarray(0, MLEN), "m === c (adlen=0)")
sodium.crypto_aead_xchacha20poly1305_ietf_keygen(key2)
t.throws(() => sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(c, null, c, null, nonce, key2),
"sodium.crypto_aead_xchacha20poly1305_ietf_decrypt() with a wrong key should have failed")
t.equal(sodium.crypto_aead_xchacha20poly1305_ietf_ABYTES, 16)
t.equal(sodium.crypto_aead_xchacha20poly1305_ietf_KEYBYTES, 32)
t.equal(sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES, 24)
t.equal(sodium.crypto_aead_xchacha20poly1305_ietf_NSECBYTES, 0)
t.equal(sodium.crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX, 4294967279)
t.end()
})
}