-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathed25519.js
140 lines (126 loc) · 4.31 KB
/
ed25519.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
const isBuffer = require('is-buffer')
const alloc = require('buffer-alloc-unsafe')
/* eslint-disable camelcase */
const {
crypto_sign_PUBLICKEYBYTES,
crypto_sign_SECRETKEYBYTES,
crypto_sign_SEEDBYTES,
crypto_sign_BYTES,
crypto_sign_verify_detached,
crypto_sign_seed_keypair,
crypto_sign_detached,
crypto_sign_keypair,
} = require('./sodium')
/**
* Generate a public and secret key pair from an optional
* seed buffer. This function calls crypto_sign_seed_keypair and
* crypto_sign_keypair internally.
*
* @public
* @param {(Buffer)} [seed]
* @return {Object}
* @throws TypeError
*/
function keyPair(seed) {
if (null === seed) {
throw new TypeError('ed25519.keyPair: Seed cannot be null.')
} else if (undefined !== seed) {
if (false === isBuffer(seed)) {
throw new TypeError('ed25519.keyPair: Expecting seed to be a buffer.')
} else if (0 === seed.length) {
throw new TypeError('ed25519.keyPair: Cannot use empty buffer as seed.')
} else if (seed.length < crypto_sign_SEEDBYTES) {
throw new TypeError(
'ed25519.keyPair: Seed buffer length too small. '
+ `Expecting size ${crypto_sign_SEEDBYTES}.`
)
} else if (seed.length > crypto_sign_SEEDBYTES) {
throw new TypeError(
'ed25519.keyPair: Seed buffer length too large. '
+ `Expecting size ${crypto_sign_SEEDBYTES}.`
)
}
}
const publicKey = alloc(crypto_sign_PUBLICKEYBYTES)
const secretKey = alloc(crypto_sign_SECRETKEYBYTES)
if (seed) {
crypto_sign_seed_keypair(publicKey, secretKey, seed)
} else {
crypto_sign_keypair(publicKey, secretKey)
}
return { publicKey, secretKey }
}
/**
* Sign a message buffer with a secret key buffer. This function calls
* `crypto_sign_detached` on a buffer of size `crypto_sign_BYTES`.
*
* @public
* @param {Buffer} message
* @param {Buffer} secretKey
* @return {Buffer}
* @throws TypeError
*/
function sign(message, secretKey) {
if (!message || false === isBuffer(message)) {
throw new TypeError('ed25519.sign: Expecting message to be a buffer.')
} else if (0 === message.length) {
throw new TypeError('ed25519.sign: Cannot sign an empty message.')
}
if (!secretKey || false === isBuffer(secretKey)) {
throw new TypeError('ed25519.sign: Expecting secretKey to be a buffer.')
} else if (0 === secretKey.length) {
throw new TypeError('ed25519.sign: Cannot sign with an empty secretKey.')
}
const buffer = alloc(crypto_sign_BYTES)
crypto_sign_detached(buffer, message, secretKey)
return buffer
}
/**
* Verify signature for a message signed with a given
* public key. This function calls `crypto_sign_verify_detached`
* internally.
*
* @public
* @param {Buffer} signature
* @param {Buffer} message
* @param {Buffer} publicKey
* @return {Boolean}
* @throws TypeError
*/
function verify(signature, message, publicKey) {
if (false === isBuffer(signature)) {
throw new TypeError('ed25519.verify: Expecting signature to be a buffer.')
} else if (0 === signature.length) {
/* eslint-disable-next-line function-paren-newline */
throw new TypeError(
'ed25519.verify: Cannot verify message with an signature buffer.'
)
} else if (signature.length < crypto_sign_BYTES) {
throw new TypeError('ed25519.verify: Signature buffer too small.')
} else if (signature.length > crypto_sign_BYTES) {
throw new TypeError('ed25519.verify: Signature buffer too large.')
}
if (false === isBuffer(message)) {
throw new TypeError('ed25519.verify: Expecting message to be a buffer.')
} else if (0 === message.length) {
throw new TypeError('ed25519.verify: Message buffer cannot be empty.')
}
if (false === isBuffer(publicKey)) {
throw new TypeError('ed25519.verify: Expecting publicKey to be a buffer.')
} else if (0 === publicKey.length) {
/* eslint-disable-next-line function-paren-newline */
throw new TypeError(
'ed25519.verify: Cannot verify message with an publicKey buffer.'
)
} else if (publicKey.length < crypto_sign_PUBLICKEYBYTES) {
throw new TypeError('ed25519.verify: Public key buffer too small.')
} else if (publicKey.length > crypto_sign_PUBLICKEYBYTES) {
throw new TypeError('ed25519.verify: Public key buffer too large.')
}
return crypto_sign_verify_detached(signature, message, publicKey)
}
module.exports = {
keyPair,
verify,
sign,
}