-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcryptoPrimatives.ts
172 lines (153 loc) · 5.47 KB
/
cryptoPrimatives.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
171
172
/*
* Copyright 2019 - MATTR Limited
* All rights reserved
* Confidential and proprietary
*/
import crypto, { JsonWebKey } from "crypto";
import { AlgorithmTypes } from "../sign";
export const signEcdsaSha256 =
(key: JsonWebKey) =>
async (data: Uint8Array): Promise<Uint8Array> => {
const keyObject = crypto.createPrivateKey({ key, format: "jwk" });
return crypto.createSign("SHA256").update(data).sign({ key: keyObject, dsaEncoding: "ieee-p1363" });
};
export const verifyEcdsaSha256 =
(key: JsonWebKey) =>
async (data: Uint8Array, signature: Uint8Array): Promise<boolean> => {
const keyObject = crypto.createPublicKey({ key, format: "jwk" });
return crypto.createVerify("SHA256").update(data).verify({ key: keyObject, dsaEncoding: "ieee-p1363" }, signature);
};
export const signRssV1_5Sha256 =
(key: JsonWebKey) =>
async (data: Uint8Array): Promise<Uint8Array> => {
const keyObject = crypto.createPrivateKey({ key, format: "jwk" });
return crypto
.createSign("SHA256")
.update(data)
.sign({ key: keyObject, dsaEncoding: "ieee-p1363", padding: crypto.constants.RSA_PKCS1_PADDING });
};
export const verifyRssV1_5Sha256 =
(key: JsonWebKey) =>
async (data: Uint8Array, signature: Uint8Array): Promise<boolean> => {
const keyObject = crypto.createPublicKey({ key, format: "jwk" });
return crypto
.createVerify("SHA256")
.update(data)
.verify({ key: keyObject, dsaEncoding: "ieee-p1363", padding: crypto.constants.RSA_PKCS1_PADDING }, signature);
};
export const signHmacSha256 = (key: JsonWebKey) => {
return async (data: Uint8Array): Promise<Uint8Array> => {
const keyObject = key.k ? crypto.createSecretKey(key.k, "base64") : undefined;
if (keyObject == undefined) {
throw Error("Unable to parse key object");
}
const hmac = crypto.createHmac("SHA256", keyObject);
hmac.write(data);
hmac.end();
return hmac.read();
};
};
export const verifyHmacSha256 =
(key: JsonWebKey) =>
async (data: Uint8Array, signature: Uint8Array): Promise<boolean> => {
const keyObject = key.k ? crypto.createSecretKey(key.k, "base64") : undefined;
if (keyObject == undefined) {
throw Error("Unable to parse key object");
}
const hmac = crypto.createHmac("SHA256", keyObject);
hmac.write(data);
hmac.end();
return Buffer.compare(hmac.read(), signature) == 0;
};
export const signEcdsaSha384 =
(key: JsonWebKey) =>
async (data: Uint8Array): Promise<Uint8Array> => {
const keyObject = crypto.createPrivateKey({ key, format: "jwk" });
return crypto.createSign("SHA384").update(data).sign({ key: keyObject, dsaEncoding: "ieee-p1363" });
};
export const verifyEcdsaSha384 =
(key: JsonWebKey) =>
async (data: Uint8Array, signature: Uint8Array): Promise<boolean> => {
const keyObject = crypto.createPublicKey({ key, format: "jwk" });
return await crypto
.createVerify("SHA384")
.update(data)
.verify({ key: keyObject, dsaEncoding: "ieee-p1363" }, signature);
};
export const signRsaPssSha512 =
(key: JsonWebKey) =>
async (data: Uint8Array): Promise<Uint8Array> => {
const keyObject = crypto.createPrivateKey({ key, format: "jwk" });
return await crypto
.createSign("SHA512")
.update(data)
.sign({ key: keyObject, dsaEncoding: "ieee-p1363", padding: crypto.constants.RSA_PKCS1_PSS_PADDING });
};
export const verifyRsaPssSha512 =
(key: JsonWebKey) =>
async (data: Uint8Array, signature: Uint8Array): Promise<boolean> => {
const keyObject = crypto.createPublicKey({ key, format: "jwk" });
return crypto
.createVerify("SHA512")
.update(data)
.verify(
{ key: keyObject, dsaEncoding: "ieee-p1363", padding: crypto.constants.RSA_PKCS1_PSS_PADDING },
signature
);
};
export const signEd25519 =
(key: JsonWebKey) =>
async (data: Uint8Array): Promise<Uint8Array> => {
const keyObject = crypto.createPrivateKey({ key, format: "jwk" });
return crypto.sign(null, data, keyObject);
};
export const verifyEd25519 =
(key: JsonWebKey) =>
async (data: Uint8Array, signature: Uint8Array): Promise<boolean> => {
const keyObject = crypto.createPublicKey({ key, format: "jwk" });
return crypto.verify(null, data, keyObject, signature);
};
type VerifyFunctionWrapper = (privateKey: JsonWebKey) => VerifyFunction;
type VerifyFunction = (data: Uint8Array, signature: Uint8Array) => Promise<boolean>;
type SignFunctionWrapper = (privateKey: JsonWebKey) => SignFunction;
type SignFunction = (data: Uint8Array) => Promise<Uint8Array>;
export const verifyDefault =
(keyMap: { [keyid: string]: { key: JsonWebKey } }) =>
async (
signatureParams: { keyid: string; alg: AlgorithmTypes },
data: Uint8Array,
signature: Uint8Array
): Promise<boolean> => {
return algMap[signatureParams.alg].verify(keyMap[signatureParams.keyid].key)(data, signature);
};
export const algMap: {
[key: string]: {
sign: SignFunctionWrapper;
verify: VerifyFunctionWrapper;
};
} = {
["rsa-pss-sha512"]: {
sign: signRsaPssSha512,
verify: verifyRsaPssSha512,
},
["rsa-v1_5-sha256"]: {
sign: signRssV1_5Sha256,
verify: verifyRssV1_5Sha256,
},
["hmac-sha256"]: {
sign: signHmacSha256,
verify: verifyHmacSha256,
},
["ecdsa-p384-sha384"]: {
sign: signEcdsaSha384,
verify: verifyEcdsaSha384,
},
["ecdsa-p256-sha256"]: {
sign: signEcdsaSha256,
verify: verifyEcdsaSha256,
},
["ed25519"]: {
sign: signEd25519,
verify: verifyEd25519,
},
};