UNPKG

3.01 kBJavaScriptView Raw
1const assert = require('nanoassert')
2const Chacha20 = require('chacha20-universal')
3
4if (new Uint16Array([1])[0] !== 1) throw new Error('Big endian architecture is not supported.')
5
6exports.crypto_stream_chacha20_KEYBYTES = 32
7exports.crypto_stream_chacha20_NONCEBYTES = 8
8exports.crypto_stream_chacha20_MESSAGEBYTES_MAX = Number.MAX_SAFE_INTEGER
9
10exports.crypto_stream_chacha20_ietf_KEYBYTES = 32
11exports.crypto_stream_chacha20_ietf_NONCEBYTES = 12
12exports.crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX = 2 ** 32
13
14exports.crypto_stream_chacha20 = function (c, n, k) {
15 c.fill(0)
16 exports.crypto_stream_chacha20_xor(c, c, n, k)
17}
18
19exports.crypto_stream_chacha20_xor = function (c, m, n, k) {
20 assert(n.byteLength === exports.crypto_stream_chacha20_NONCEBYTES,
21 'n should be crypto_stream_chacha20_NONCEBYTES')
22 assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES,
23 'k should be crypto_stream_chacha20_KEYBYTES')
24
25 const xor = new Chacha20(n, k)
26 xor.update(c, m)
27 xor.final()
28}
29
30exports.crypto_stream_chacha20_xor_ic = function (c, m, n, ic, k) {
31 assert(n.byteLength === exports.crypto_stream_chacha20_NONCEBYTES,
32 'n should be crypto_stream_chacha20_NONCEBYTES')
33 assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES,
34 'k should be crypto_stream_chacha20_KEYBYTES')
35
36 const xor = new Chacha20(n, k, ic)
37 xor.update(c, m)
38 xor.final()
39}
40
41exports.crypto_stream_chacha20_xor_instance = function (n, k) {
42 assert(n.byteLength === exports.crypto_stream_chacha20_NONCEBYTES,
43 'n should be crypto_stream_chacha20_NONCEBYTES')
44 assert(k.byteLength === exports.crypto_stream_chacha20_KEYBYTES,
45 'k should be crypto_stream_chacha20_KEYBYTES')
46
47 return new Chacha20(n, k)
48}
49
50exports.crypto_stream_chacha20_ietf = function (c, n, k) {
51 c.fill(0)
52 exports.crypto_stream_chacha20_ietf_xor(c, c, n, k)
53}
54
55exports.crypto_stream_chacha20_ietf_xor = function (c, m, n, k) {
56 assert(n.byteLength === exports.crypto_stream_chacha20_ietf_NONCEBYTES,
57 'n should be crypto_stream_chacha20_ietf_NONCEBYTES')
58 assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES,
59 'k should be crypto_stream_chacha20_ietf_KEYBYTES')
60
61 const xor = new Chacha20(n, k)
62 xor.update(c, m)
63 xor.final()
64}
65
66exports.crypto_stream_chacha20_ietf_xor_ic = function (c, m, n, ic, k) {
67 assert(n.byteLength === exports.crypto_stream_chacha20_ietf_NONCEBYTES,
68 'n should be crypto_stream_chacha20_ietf_NONCEBYTES')
69 assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES,
70 'k should be crypto_stream_chacha20_ietf_KEYBYTES')
71
72 const xor = new Chacha20(n, k, ic)
73 xor.update(c, m)
74 xor.final()
75}
76
77exports.crypto_stream_chacha20_ietf_xor_instance = function (n, k) {
78 assert(n.byteLength === exports.crypto_stream_chacha20_ietf_NONCEBYTES,
79 'n should be crypto_stream_chacha20_ietf_NONCEBYTES')
80 assert(k.byteLength === exports.crypto_stream_chacha20_ietf_KEYBYTES,
81 'k should be crypto_stream_chacha20_ietf_KEYBYTES')
82
83 return new Chacha20(n, k)
84}