forked from auth0/jwt-handbook-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsassa.js
153 lines (139 loc) · 6.15 KB
/
rsassa.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
import {
os2ip,
i2osp,
rsasp1,
rsavp1,
emsaPkcs1v1_5,
mgf1,
emsaPssEncode,
emsaPssVerify
} from './pkcs';
import { hashTypes } from './sha256.js';
import { uint8ArrayEquals } from './utils.js';
/**
* Produces a signature for a message using the RSASSA algorithm as defined
* in PKCS#1.
* @param {privateKey} RSA private key, an object with
* three members: size (size in bits), n (the modulus) and
* d (the private exponent), both bigInts
* (big-integer library).
* @param {hashFn} the hash function as required by PKCS#1,
* it should take a Uint8Array and return a Uint8Array
* @param {hashType} A symbol identifying the type of hash function passed.
* For now, only "SHA-256" is supported. See the "hashTypes"
* object for possible values.
* @param {message} A String or Uint8Array with arbitrary data to sign
* @return {Uint8Array} The signature as a Uint8Array
*/
export function signPkcs1v1_5(privateKey, hashFn, hashType, message) {
if(hashType !== hashTypes.sha256) {
throw new Error('unsupported hash type');
}
const encodedMessage =
emsaPkcs1v1_5(hashFn, hashType, privateKey.size / 8, message);
const intMessage = os2ip(encodedMessage);
const intSignature = rsasp1(privateKey, intMessage);
const signature = i2osp(intSignature, privateKey.size / 8);
return signature;
}
/**
* Verifies a signature for a message using the RSASSA algorithm as defined
* in PKCS#1.
* @param {publicKey} RSA private key, an object with
* three members: size (size in bits), n (the modulus) and
* e (the public exponent), both bigInts
* (big-integer library).
* @param {hashFn} the hash function as required by PKCS#1,
* it should take a Uint8Array and return a Uint8Array
* @param {hashType} A symbol identifying the type of hash function passed.
* For now, only "SHA-256" is supported. See the "hashTypes"
* object for possible values.
* @param {message} A String or Uint8Array with arbitrary data to verify
* @param {signature} A Uint8Array with the signature
* @return {Boolean} true if the signature is valid, false otherwise.
*/
export function verifyPkcs1v1_5(publicKey,
hashFn,
hashType,
message,
signature) {
if(signature.length !== publicKey.size / 8) {
throw new Error('invalid signature length');
}
const intSignature = os2ip(signature);
const intVerification = rsavp1(publicKey, intSignature);
const verificationMessage = i2osp(intVerification, publicKey.size / 8);
const encodedMessage =
emsaPkcs1v1_5(hashFn, hashType, publicKey.size / 8, message);
return uint8ArrayEquals(encodedMessage, verificationMessage);
}
/**
* Produces a signature for a message using the RSASSA algorithm as defined
* in PKCS using PSS.
* @param {privateKey} RSA private key, an object with
* three members: size (size in bits), n (the modulus) and
* d (the private exponent), both bigInts
* (big-integer library).
* @param {hashFn} the hash function as required by PKCS#1,
* it should take a Uint8Array and return a Uint8Array
* @param {hashType} A symbol identifying the type of hash function passed.
* For now, only "SHA-256" is supported. See the "hashTypes"
* object for possible values.
* @param {message} A String or Uint8Array with arbitrary data to sign
* @return {Uint8Array} The signature as a Uint8Array
*/
export function signPss(privateKey, hashFn, hashType, message) {
if(hashType !== hashTypes.sha256) {
throw new Error('unsupported hash type');
}
const encodedMessage = emsaPssEncode(hashFn,
hashType,
mgf1.bind(null, hashFn),
256 / 8, //size of hash
privateKey.size - 1,
message);
const intMessage = os2ip(encodedMessage);
const intSignature = rsasp1(privateKey, intMessage);
const signature = i2osp(intSignature, privateKey.size / 8);
return signature;
}
/**
* Verifies a signature for a message using the RSASSA algorithm as defined
* in PKCS using PSS.
* @param {publicKey} RSA private key, an object with
* three members: size (size in bits), n (the modulus) and
* e (the public exponent), both bigInts
* (big-integer library).
* @param {hashFn} the hash function as required by PKCS#1,
* it should take a Uint8Array and return a Uint8Array
* @param {hashType} A symbol identifying the type of hash function passed.
* For now, only "SHA-256" is supported. See the "hashTypes"
* object for possible values.
* @param {message} A String or Uint8Array with arbitrary data to verify
* @param {signature} A Uint8Array with the signature
* @return {Boolean} true if the signature is valid, false otherwise.
*/
export function verifyPss(publicKey, hashFn, hashType, message, signature) {
if(signature.length !== publicKey.size / 8) {
throw new Error('invalid signature length');
}
const intSignature = os2ip(signature);
const intVerification = rsavp1(publicKey, intSignature);
const verificationMessage =
i2osp(intVerification, Math.ceil( (publicKey.size - 1) / 8 ));
return emsaPssVerify(hashFn,
hashType,
mgf1.bind(null, hashFn),
256 / 8,
publicKey.size - 1,
message,
verificationMessage);
}
export const pkcs1v1_5 = Object.freeze({
sign: signPkcs1v1_5,
verify: verifyPkcs1v1_5
});
export const pss = Object.freeze({
sign: signPss,
verify: verifyPss
});