-
Notifications
You must be signed in to change notification settings - Fork 17
/
dh.js
48 lines (40 loc) · 1.11 KB
/
dh.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
/* eslint-disable camelcase */
const { crypto_kx_SEEDBYTES, crypto_kx_keypair, crypto_kx_seed_keypair } = require('sodium-universal/crypto_kx')
const { crypto_scalarmult_BYTES, crypto_scalarmult_SCALARBYTES, crypto_scalarmult } = require('sodium-universal/crypto_scalarmult')
const assert = require('nanoassert')
const DHLEN = crypto_scalarmult_BYTES
const PKLEN = crypto_scalarmult_BYTES
const SKLEN = crypto_scalarmult_SCALARBYTES
const SEEDLEN = crypto_kx_SEEDBYTES
const ALG = '25519'
module.exports = () => ({
DHLEN,
PKLEN,
SKLEN,
SEEDLEN,
ALG,
generateKeypair,
generateSeedKeypair,
dh
})
function generateKeypair (pk, sk) {
assert(pk.byteLength === PKLEN)
assert(sk.byteLength === SKLEN)
crypto_kx_keypair(pk, sk)
}
function generateSeedKeypair (pk, sk, seed) {
assert(pk.byteLength === PKLEN)
assert(sk.byteLength === SKLEN)
assert(seed.byteLength === SKLEN)
crypto_kx_seed_keypair(pk, sk, seed)
}
function dh (output, lsk, pk) {
assert(output.byteLength === DHLEN)
assert(lsk.byteLength === SKLEN)
assert(pk.byteLength === PKLEN)
crypto_scalarmult(
output,
lsk,
pk
)
}