-
Notifications
You must be signed in to change notification settings - Fork 31
/
abstract-handshake.ts
170 lines (135 loc) · 4.85 KB
/
abstract-handshake.ts
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
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { fromString as uint8ArrayFromString } from 'uint8arrays'
import type { bytes, bytes32 } from '../@types/basic.js'
import type { CipherState, MessageBuffer, SymmetricState } from '../@types/handshake.js'
import type { ICryptoInterface } from '../crypto.js'
import { logger } from '../logger.js'
import { Nonce } from '../nonce.js'
export abstract class AbstractHandshake {
public crypto: ICryptoInterface
constructor (crypto: ICryptoInterface) {
this.crypto = crypto
}
public encryptWithAd (cs: CipherState, ad: Uint8Array, plaintext: Uint8Array): bytes {
const e = this.encrypt(cs.k, cs.n, ad, plaintext)
cs.n.increment()
return e
}
public decryptWithAd (cs: CipherState, ad: Uint8Array, ciphertext: Uint8Array): {plaintext: bytes, valid: boolean} {
const { plaintext, valid } = this.decrypt(cs.k, cs.n, ad, ciphertext)
cs.n.increment()
return { plaintext, valid }
}
// Cipher state related
protected hasKey (cs: CipherState): boolean {
return !this.isEmptyKey(cs.k)
}
protected createEmptyKey (): bytes32 {
return new Uint8Array(32)
}
protected isEmptyKey (k: bytes32): boolean {
const emptyKey = this.createEmptyKey()
return uint8ArrayEquals(emptyKey, k)
}
protected encrypt (k: bytes32, n: Nonce, ad: Uint8Array, plaintext: Uint8Array): bytes {
n.assertValue()
return this.crypto.chaCha20Poly1305Encrypt(plaintext, n.getBytes(), ad, k)
}
protected encryptAndHash (ss: SymmetricState, plaintext: bytes): bytes {
let ciphertext
if (this.hasKey(ss.cs)) {
ciphertext = this.encryptWithAd(ss.cs, ss.h, plaintext)
} else {
ciphertext = plaintext
}
this.mixHash(ss, ciphertext)
return ciphertext
}
protected decrypt (k: bytes32, n: Nonce, ad: bytes, ciphertext: bytes): {plaintext: bytes, valid: boolean} {
n.assertValue()
const encryptedMessage = this.crypto.chaCha20Poly1305Decrypt(ciphertext, n.getBytes(), ad, k)
if (encryptedMessage) {
return {
plaintext: encryptedMessage,
valid: true
}
} else {
return {
plaintext: new Uint8Array(0),
valid: false
}
}
}
protected decryptAndHash (ss: SymmetricState, ciphertext: bytes): {plaintext: bytes, valid: boolean} {
let plaintext: bytes; let valid = true
if (this.hasKey(ss.cs)) {
({ plaintext, valid } = this.decryptWithAd(ss.cs, ss.h, ciphertext))
} else {
plaintext = ciphertext
}
this.mixHash(ss, ciphertext)
return { plaintext, valid }
}
protected dh (privateKey: bytes32, publicKey: bytes32): bytes32 {
try {
const derivedU8 = this.crypto.generateX25519SharedKey(privateKey, publicKey)
if (derivedU8.length === 32) {
return derivedU8
}
return derivedU8.subarray(0, 32)
} catch (e) {
const err = e as Error
logger(err.message)
return new Uint8Array(32)
}
}
protected mixHash (ss: SymmetricState, data: bytes): void {
ss.h = this.getHash(ss.h, data)
}
protected getHash (a: Uint8Array, b: Uint8Array): bytes32 {
const u = this.crypto.hashSHA256(uint8ArrayConcat([a, b], a.length + b.length))
return u
}
protected mixKey (ss: SymmetricState, ikm: bytes32): void {
const [ck, tempK] = this.crypto.getHKDF(ss.ck, ikm)
ss.cs = this.initializeKey(tempK)
ss.ck = ck
}
protected initializeKey (k: bytes32): CipherState {
return { k, n: new Nonce() }
}
// Symmetric state related
protected initializeSymmetric (protocolName: string): SymmetricState {
const protocolNameBytes = uint8ArrayFromString(protocolName, 'utf-8')
const h = this.hashProtocolName(protocolNameBytes)
const ck = h
const key = this.createEmptyKey()
const cs: CipherState = this.initializeKey(key)
return { cs, ck, h }
}
protected hashProtocolName (protocolName: Uint8Array): bytes32 {
if (protocolName.length <= 32) {
const h = new Uint8Array(32)
h.set(protocolName)
return h
} else {
return this.getHash(protocolName, new Uint8Array(0))
}
}
protected split (ss: SymmetricState): {cs1: CipherState, cs2: CipherState} {
const [tempk1, tempk2] = this.crypto.getHKDF(ss.ck, new Uint8Array(0))
const cs1 = this.initializeKey(tempk1)
const cs2 = this.initializeKey(tempk2)
return { cs1, cs2 }
}
protected writeMessageRegular (cs: CipherState, payload: bytes): MessageBuffer {
const ciphertext = this.encryptWithAd(cs, new Uint8Array(0), payload)
const ne = this.createEmptyKey()
const ns = new Uint8Array(0)
return { ne, ns, ciphertext }
}
protected readMessageRegular (cs: CipherState, message: MessageBuffer): {plaintext: bytes, valid: boolean} {
return this.decryptWithAd(cs, new Uint8Array(0), message.ciphertext)
}
}