-
Notifications
You must be signed in to change notification settings - Fork 17
/
cipher-state.js
130 lines (106 loc) · 3.25 KB
/
cipher-state.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
/* eslint-disable camelcase */
const { sodium_memzero } = require('sodium-universal/memory')
const { sodium_increment, sodium_memcmp, sodium_is_zero } = require('sodium-universal/helpers')
const assert = require('nanoassert')
module.exports = ({ cipher }) => {
const STATELEN = cipher.KEYLEN + cipher.NONCELEN
const NONCELEN = cipher.NONCELEN
const MACLEN = cipher.MACLEN
const KEY_BEGIN = 0
const KEY_END = cipher.KEYLEN
const NONCE_BEGIN = KEY_END
const NONCE_END = NONCE_BEGIN + cipher.NONCELEN
function initializeKey (state, key) {
assert(state.byteLength === STATELEN)
assert(key == null ? true : key.byteLength === cipher.KEYLEN)
if (key == null) {
sodium_memzero(state.subarray(KEY_BEGIN, KEY_END))
return
}
state.set(key)
sodium_memzero(state.subarray(NONCE_BEGIN, NONCE_END))
}
function hasKey (state) {
assert(state.byteLength === STATELEN)
const k = state.subarray(KEY_BEGIN, KEY_END)
return sodium_is_zero(k) === false
}
function setNonce (state, nonce) {
assert(state.byteLength === STATELEN)
assert(nonce.byteLength === NONCELEN)
state.set(nonce, NONCE_BEGIN)
}
const maxnonce = new Uint8Array(8).fill(0xff)
function encryptWithAd (state, out, ad, plaintext) {
assert(state.byteLength === STATELEN)
assert(out.byteLength != null)
assert(plaintext.byteLength != null)
const n = state.subarray(NONCE_BEGIN, NONCE_END)
if (sodium_memcmp(n, maxnonce)) throw new Error('Nonce overflow')
if (hasKey(state) === false) {
out.set(plaintext)
encryptWithAd.bytesRead = plaintext.byteLength
encryptWithAd.bytesWritten = encryptWithAd.bytesRead
return
}
const k = state.subarray(KEY_BEGIN, KEY_END)
cipher.encrypt(
out,
k,
n,
ad,
plaintext
)
encryptWithAd.bytesRead = cipher.encrypt.bytesRead
encryptWithAd.bytesWritten = cipher.encrypt.bytesWritten
sodium_increment(n)
}
encryptWithAd.bytesRead = 0
encryptWithAd.bytesWritten = 0
function decryptWithAd (state, out, ad, ciphertext) {
assert(state.byteLength === STATELEN)
assert(out.byteLength != null)
assert(ciphertext.byteLength != null)
const n = state.subarray(NONCE_BEGIN, NONCE_END)
if (sodium_memcmp(n, maxnonce)) throw new Error('Nonce overflow')
if (hasKey(state) === false) {
out.set(ciphertext)
decryptWithAd.bytesRead = ciphertext.byteLength
decryptWithAd.bytesWritten = decryptWithAd.bytesRead
return
}
const k = state.subarray(KEY_BEGIN, KEY_END)
cipher.decrypt(
out,
k,
n,
ad,
ciphertext
)
decryptWithAd.bytesRead = cipher.decrypt.bytesRead
decryptWithAd.bytesWritten = cipher.decrypt.bytesWritten
sodium_increment(n)
}
decryptWithAd.bytesRead = 0
decryptWithAd.bytesWritten = 0
function rekey (state) {
assert(state.byteLength === STATELEN)
const k = state.subarray(KEY_BEGIN, KEY_END)
cipher.rekey(k, k)
rekey.bytesRead = cipher.rekey.bytesRead
rekey.bytesWritten = cipher.rekey.bytesWritten
}
rekey.bytesRead = 0
rekey.bytesWritten = 0
return {
STATELEN,
NONCELEN,
MACLEN,
initializeKey,
hasKey,
setNonce,
encryptWithAd,
decryptWithAd,
rekey
}
}