-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.ts
65 lines (60 loc) · 1.94 KB
/
signature.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
import { numberToUint32LE } from './codec'
import type { Sodium } from './sodium'
/**
* Calculate a multipart detached signature of multiple elements.
*
* Use `verifyMultipartSignature` for verification.
*
* Algorithm: Ed25519ph, with prepended manifest (see `generateManifest`)
*
* @param privateKey Signature private key
* @param items Buffers to include in the calculation
*/
export function multipartSignature(
sodium: Sodium,
privateKey: Uint8Array,
...items: Uint8Array[]
) {
const state = assembleMultipartSignatureState(sodium, items)
return sodium.crypto_sign_final_create(state, privateKey)
}
/**
* Verify integrity and provenance of a set of items, by verifying the signature
* of the hash of those items.
*
* @param publicKey Signature public key
* @param signature As returned by `multipartSignature`
* @param items Items to verify
*/
export function verifyMultipartSignature(
sodium: Sodium,
publicKey: Uint8Array,
signature: Uint8Array,
...items: Uint8Array[]
) {
const state = assembleMultipartSignatureState(sodium, items)
return sodium.crypto_sign_final_verify(state, signature, publicKey)
}
// Internal --
export function assembleMultipartSignatureState(
sodium: Sodium,
items: Uint8Array[]
) {
const state = sodium.crypto_sign_init()
// Include a representation of the structure of the input items (manifest)
// as the first element, to prevent canonicalisation attacks:
const manifest = generateSignatureManifest(items)
sodium.crypto_sign_update(state, manifest)
sodium.memzero(manifest)
// Then add each item to the internal hash
items.forEach(item => sodium.crypto_sign_update(state, item))
return state
}
function generateSignatureManifest(items: Uint8Array[]) {
const manifest = new Uint8Array(4 + items.length * 4)
manifest.set(numberToUint32LE(items.length))
items.forEach((item, index) => {
manifest.set(numberToUint32LE(item.byteLength), 4 + index * 4)
})
return manifest
}