goog.provide('wycheproof.webcryptoapi.RsaUtil');
goog.require('goog.testing.asserts');
goog.require('wycheproof.webcryptoapi.HashUtil');
var HashUtil = wycheproof.webcryptoapi.HashUtil;
wycheproof.webcryptoapi.RsaUtil.RSASSA_PKCS1 = 'RSASSA-PKCS1-v1_5';
wycheproof.webcryptoapi.RsaUtil.RSA_OAEP = 'RSA-OAEP';
wycheproof.webcryptoapi.RsaUtil.E_65537 = new Uint8Array([0x01, 0x00, 0x01]);
wycheproof.webcryptoapi.RsaUtil.E_3 = new Uint8Array([0x03]);
wycheproof.webcryptoapi.RsaUtil.importPublicKey =
function(e, n, schemeName, hashAlg, usages) {
return window.crypto.subtle.importKey(
'jwk', {
kty: 'RSA',
e: e,
n: n,
ext: true,
}, {
name: schemeName,
hash: {name: hashAlg},
},
false,
usages
);
};
wycheproof.webcryptoapi.RsaUtil.verify = function(pk, msg, sig, hashAlg, schemeName) {
return window.crypto.subtle.verify(
{
name: schemeName,
hash: hashAlg
},
pk,
sig,
msg
);
};
wycheproof.webcryptoapi.RsaUtil.generateKey
= function(schemeName, keySize, e, hashAlg, usages) {
return window.crypto.subtle.generateKey(
{
name: schemeName,
modulusLength: keySize,
publicExponent: e,
hash: {name: hashAlg},
},
true,
usages
);
};
wycheproof.webcryptoapi.RsaUtil.decrypt = function(schemeName, sk, ct) {
return window.crypto.subtle.decrypt({name: schemeName}, sk, ct);
};
wycheproof.webcryptoapi.RsaUtil.RsaSignatureTestCase
= function(id, e, n, hashAlg, scheme, msg, sig, result) {
this.id = id;
this.e = e;
this.n = n;
this.hashAlg = hashAlg;
this.scheme = scheme;
this.msg = msg;
this.sig = sig;
this.result = result;
};
wycheproof.webcryptoapi.RsaUtil.testVerification = function() {
var tc = this;
var promise = new Promise(function(resolve, reject){
RsaUtil.importPublicKey(tc.e, tc.n, tc.scheme, tc.hashAlg, ['verify'])
.then(function(pk){
wycheproof.webcryptoapi.RsaUtil.verify(pk, tc.msg, tc.sig,
tc.hashAlg, tc.scheme).then(function(isValid){
if ((tc.result == 'valid' && !isValid) ||
(tc.result == 'invalid' && isValid)) {
reject('Failed on test case ' + tc.id);
}
resolve();
}).catch(function(err){
reject('Unexpected exception on test case ' + tc.id + ": " + err);
});
}).catch(function(err){
reject('Failed to import public key in test case ' + tc.id + ': ' + err);
});
});
return promise;
};