-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
rsa.ts
208 lines (176 loc) · 6.41 KB
/
rsa.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import * as graphene from "graphene-pk11"
var RSA = graphene.RSA;
var Enums = graphene.Enums;
import * as alg from "./alg"
import * as iwc from "./iwebcrypto"
import {CryptoKey} from "./key"
let ALG_NAME_RSA_PKCS1 = "RSASSA-PKCS1-v1_5";
let ALG_NAME_RSA_PSS = "RSA-PSS";
let ALG_NAME_RSA_OAEP = "RSA-OAEP";
let HASH_ALGS = ["SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512"];
export class Rsa extends alg.AlgorithmBase {
static ALGORITHM_NAME: string = "";
static generateKey(session: graphene.Session, alg: any, extractable: boolean, keyUsages: string[], label?: string): iwc.ICryptoKeyPair {
var size = alg.modulusLength;
var exp = new Buffer(alg.publicExponent);
var _key = session.generate("RSA", null, {
"label": label,
"token": true,
"extractable": extractable,
"keyUsages": keyUsages,
"modulusLength": size,
"publicExponent": exp
});
return {
privateKey: new RsaKey(_key.privateKey, alg),
publicKey: new RsaKey(_key.publicKey, alg)
};
}
static checkRsaAlgorithm(alg: iwc.IAlgorithmIdentifier) {
if (alg.name.toLowerCase() !== this.ALGORITHM_NAME.toLowerCase())
throw new Error("RsaKeyGenParams: Wrong algrotiyhm name. Must be " + this.ALGORITHM_NAME);
}
static checkRsaGenParams(alg: IRsaKeyGenParams) {
this.checkRsaAlgorithm;
if (!alg.modulusLength)
throw new TypeError("RsaKeyGenParams: modulusLength: Missing required property");
if (alg.modulusLength < 256 || alg.modulusLength > 16384)
throw new TypeError("RsaKeyGenParams: The modulus length must be a multiple of 8 bits and >= 256 and <= 16384");
if (!(alg.publicExponent && alg.publicExponent instanceof Uint8Array))
throw new TypeError("RsaKeyGenParams: publicExponent: Missing or not a Uint8Array");
}
static checkAlgorithmHashedParams(alg: iwc.IAlgorithmIdentifier) {
super.checkAlgorithmHashedParams(alg);
var _alg = alg.hash;
_alg.name = _alg.name.toUpperCase();
if (HASH_ALGS.indexOf(_alg.name) == -1)
throw new Error("AlgorithmHashedParams: Unknow hash algorithm in use");
}
static wc2pk11(alg) {
RsaPKCS1.checkRsaAlgorithm(alg);
RsaPKCS1.checkAlgorithmHashedParams(alg);
var _alg = null;
switch (alg.hash.name.toUpperCase()) {
case "SHA-1":
_alg = "SHA1_RSA_PKCS";
break;
case "SHA-224":
_alg = "SHA224_RSA_PKCS";
break;
case "SHA-256":
_alg = "SHA256_RSA_PKCS";
break;
case "SHA-384":
_alg = "SHA384_RSA_PKCS";
break;
case "SHA-512":
_alg = "SHA512_RSA_PKCS";
break;
default:
throw new TypeError("Unknown Hash agorithm name in use");
}
return _alg;
}
}
export interface IRsaKeyGenParams extends iwc.IAlgorithmIdentifier {
modulusLength: number;
publicExponent: Uint8Array;
}
export class RsaKey extends CryptoKey {
modulusLength: number;
publicExponent: Uint8Array;
constructor(key, alg: IRsaKeyGenParams) {
super(key, alg);
this.modulusLength = alg.modulusLength;
this.publicExponent = alg.publicExponent;
//TODO: get params from key if alg params is empty
}
}
export class RsaPKCS1 extends Rsa {
static ALGORITHM_NAME: string = ALG_NAME_RSA_PKCS1;
static generateKey(session: graphene.Session, alg: IRsaKeyGenParams, extractable: boolean, keyUsages: string[], label?: string): iwc.ICryptoKeyPair {
this.checkAlgorithmIdentifier(alg);
this.checkRsaGenParams(alg);
this.checkAlgorithmHashedParams(alg);
var keyPair: iwc.ICryptoKeyPair = super.generateKey.apply(this, arguments);
return keyPair;
}
static sign(session: graphene.Session, alg: iwc.IAlgorithmIdentifier, key: CryptoKey, data: Buffer) {
RsaPKCS1.checkAlgorithmIdentifier(alg);
RsaPKCS1.checkPrivateKey(key);
var _alg = RsaPKCS1.wc2pk11(key.algorithm);
var signer = session.createSign(_alg, key.key);
signer.update(data);
var signature = signer.final();
return signature;
}
static verify(session: graphene.Session, alg: iwc.IAlgorithmIdentifier, key: CryptoKey, signature: Buffer, data: Buffer): boolean {
RsaPKCS1.checkAlgorithmIdentifier(alg);
RsaPKCS1.checkPublicKey(key);
var _alg = RsaPKCS1.wc2pk11(key.algorithm);
var signer = session.createVerify(_alg, key.key);
signer.update(data);
var res = signer.final(signature);
return res;
}
}
export class RsaPSS extends Rsa {
static ALGORITHM_NAME: string = ALG_NAME_RSA_PSS;
static generateKey(session: graphene.Session, alg: IRsaKeyGenParams, extractable: boolean, keyUsages: string[], label?: string): iwc.ICryptoKeyPair {
throw new Error("not realized in this implementation");
}
}
export class RsaOAEP extends Rsa {
static ALGORITHM_NAME: string = ALG_NAME_RSA_OAEP;
static generateKey(session: graphene.Session, alg: IRsaKeyGenParams, extractable: boolean, keyUsages: string[], label?: string): iwc.ICryptoKeyPair {
this.checkAlgorithmIdentifier(alg);
this.checkRsaGenParams(alg);
this.checkAlgorithmHashedParams(alg);
var keyPair: iwc.ICryptoKeyPair = super.generateKey.apply(arguments);
return keyPair;
}
static wc2pk11(alg) {
var params = null;
switch (alg.hash.name.toUpperCase()) {
case "SHA-1":
params = new RSA.RsaOAEPParams(Enums.Mechanism.SHA1, Enums.MGF1.SHA1);
break;
case "SHA-224":
params = new RSA.RsaOAEPParams(Enums.Mechanism.SHA224, Enums.MGF1.SHA224);
break;
case "SHA-256":
params = new RSA.RsaOAEPParams(Enums.Mechanism.SHA256, Enums.MGF1.SHA256);
break;
case "SHA-384":
params = new RSA.RsaOAEPParams(Enums.Mechanism.SHA384, Enums.MGF1.SHA384);
break;
case "SHA-512":
params = new RSA.RsaOAEPParams(Enums.Mechanism.SHA512, Enums.MGF1.SHA512);
break;
default:
throw new Error("Unknown hash name in use");
}
var res = { name: "RSA_PKCS_OAEP", params: params };
return res;
}
static encrypt(session: graphene.Session, alg: iwc.IAlgorithmIdentifier, key: CryptoKey, data: Buffer): Buffer {
RsaPKCS1.checkAlgorithmIdentifier(alg);
RsaPKCS1.checkPublicKey(key);
var _alg =RsaPKCS1.wc2pk11(key.algorithm);
var enc = session.createEncrypt(_alg, key.key);
var msg = new Buffer(0);
msg = Buffer.concat([msg, enc.update(data)]);
msg = Buffer.concat([msg, enc.final()]);
return msg;
}
static decrypt(session: graphene.Session, alg: iwc.IAlgorithmIdentifier, key: CryptoKey, data: Buffer): Buffer {
RsaPKCS1.checkAlgorithmIdentifier(alg);
RsaPKCS1.checkPrivateKey(key);
var _alg =RsaPKCS1.wc2pk11(key.algorithm);
var dec = session.createDecrypt(_alg, key.key);
var msg = new Buffer(0);
msg = Buffer.concat([msg, dec.update(data)]);
msg = Buffer.concat([msg, dec.final()]);
return msg;
}
}