forked from sodium-friends/sodium-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_sign.js
176 lines (135 loc) · 5.63 KB
/
crypto_sign.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
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
171
172
173
174
175
176
var test = require('tape')
var fixtures = require('./fixtures/crypto_sign.json')
module.exports = function (sodium) {
test('crypto_sign_open fixtures', function (assert) {
for (var i = 0; i < fixtures.length; i++) {
var publicKey = Buffer.from(fixtures[i][1])
var message = Buffer.from(fixtures[i][3])
var signed = Buffer.from([].concat(fixtures[i][2], fixtures[i][3]))
if (!sodium.crypto_sign_open(message, signed, publicKey)) {
assert.fail('Failed on fixture #' + i)
assert.end()
return
}
}
assert.pass('Passed all fixtures')
assert.end()
})
test('crypto_sign fixtures', function (assert) {
var fixtures = require('./fixtures/crypto_sign.json')
for (var i = 0; i < fixtures.length; i++) {
var secretKey = Buffer.from([].concat(fixtures[i][0], fixtures[i][1]))
var message = Buffer.from(fixtures[i][3])
var expected = Buffer.from([].concat(fixtures[i][2], fixtures[i][3]))
var actual = Buffer.alloc(sodium.crypto_sign_BYTES + message.length)
sodium.crypto_sign(actual, message, secretKey)
if (Buffer.compare(actual, expected) !== 0) {
assert.fail('Failed on fixture #' + i)
assert.end()
return
}
}
assert.pass('Passed all fixtures')
assert.end()
})
test('crypto_sign_verify_detached fixtures', function (assert) {
var fixtures = require('./fixtures/crypto_sign.json')
for (var i = 0; i < fixtures.length; i++) {
var publicKey = Buffer.from(fixtures[i][1])
var message = Buffer.from(fixtures[i][3])
var signature = Buffer.from(fixtures[i][2])
if (!sodium.crypto_sign_verify_detached(signature, message, publicKey)) {
assert.fail('Failed on fixture #' + i)
assert.end()
return
}
}
assert.pass('Passed all fixtures')
assert.end()
})
test('crypto_sign_detached fixtures', function (assert) {
var fixtures = require('./fixtures/crypto_sign.json')
for (var i = 0; i < fixtures.length; i++) {
var secretKey = Buffer.from([].concat(fixtures[i][0], fixtures[i][1]))
var message = Buffer.from(fixtures[i][3])
var expected = Buffer.from(fixtures[i][2])
var actual = Buffer.alloc(sodium.crypto_sign_BYTES)
sodium.crypto_sign_detached(actual, message, secretKey)
if (Buffer.compare(actual, expected) !== 0) {
assert.fail('Failed on fixture #' + i)
assert.end()
return
}
}
assert.pass('Passed all fixtures')
assert.end()
})
test('libsodium', assert => {
let sig = Buffer.alloc(sodium.crypto_sign_BYTES)
let sm = Buffer.alloc(1024 + sodium.crypto_sign_BYTES)
let skpk = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES)
let pk = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES)
let sk = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES)
let smlen
let i
let test
sig.fill(0)
var pass = true
for (i = 0; i < fixtures.length; i++) {
test = parseTest(fixtures[i])
skpk.set(test.sk)
skpk.set(test.pk, sodium.crypto_sign_SEEDBYTES)
smlen = sodium.crypto_sign(sm, test.m, skpk)
pass &= smlen === sodium.crypto_sign_BYTES + test.m.byteLength
pass &= Buffer.compare(test.sig, sm.subarray(0, 64)) === 0
pass &= sodium.crypto_sign_open(test.m, sm.subarray(0, smlen), test.pk)
sodium.crypto_sign_detached(sig, test.m, skpk)
pass &= sig.byteLength !== 0 && sig.byteLength <= sodium.crypto_sign_BYTES
pass &= Buffer.compare(test.sig, sig) === 0
pass &= sodium.crypto_sign_verify_detached(sig, test.m.subarray(0, i), test.pk)
if (!pass) assert.fail('failed on fixture #' + i)
}
assert.pass('passed all fixtures')
for (let j = 1; j < 8; j++) {
sig[63] ^= (j << 5)
assert.notOk(sodium.crypto_sign_verify_detached(sig, test.m.subarray(0, i), test.pk))
sig[63] ^= (j << 5)
}
pk.fill(0)
assert.notOk(sodium.crypto_sign_verify_detached(sig, test.m.subarray(0, i), pk))
sig.subarray(0, 32).fill(0xff)
sig[0] = 0xdb
assert.notOk(sodium.crypto_sign_verify_detached(sig, test.m.subarray(0, i), pk))
sodium.crypto_sign_detached(sig, test.m.subarray(0, i), skpk)
pk.write('3eee494fb9eac773144e34b0c755affaf33ea782c0722e5ea8b150e61209ab36', 'hex')
assert.notOk(sodium.crypto_sign_verify_detached(sig, test.m.subarray(0, i), pk))
pk.write('0200000000000000000000000000000000000000000000000000000000000000', 'hex')
assert.notOk(sodium.crypto_sign_verify_detached(sig, test.m.subarray(0, i), pk))
pk.write('0500000000000000000000000000000000000000000000000000000000000000', 'hex')
assert.notOk(sodium.crypto_sign_verify_detached(sig, test.m.subarray(0, i), pk))
const keypair_seed = Buffer.from([
0x42, 0x11, 0x51, 0xa4, 0x59, 0xfa, 0xea, 0xde, 0x3d, 0x24, 0x71,
0x15, 0xf9, 0x4a, 0xed, 0xae, 0x42, 0x31, 0x81, 0x24, 0x09, 0x5a,
0xfa, 0xbe, 0x4d, 0x14, 0x51, 0xa5, 0x59, 0xfa, 0xed, 0xee
])
assert.equal(sodium.crypto_sign_seed_keypair(pk, sk, keypair_seed), 0)
assert.equal(sodium.crypto_sign_keypair(pk, sk), 0)
assert.assert(sodium.crypto_sign_BYTES > 0)
assert.assert(sodium.crypto_sign_SEEDBYTES > 0)
assert.assert(sodium.crypto_sign_PUBLICKEYBYTES > 0)
assert.assert(sodium.crypto_sign_SECRETKEYBYTES > 0)
assert.equal(sodium.crypto_sign_BYTES, 64)
assert.equal(sodium.crypto_sign_SEEDBYTES, 32)
assert.equal(sodium.crypto_sign_PUBLICKEYBYTES, 32)
assert.equal(sodium.crypto_sign_SECRETKEYBYTES, 64)
assert.end()
})
}
function parseTest (t) {
return {
sk: Buffer.from(t[0]),
pk: Buffer.from(t[1]),
sig: Buffer.from(t[2]),
m: Buffer.from(t[3])
}
}