1 /* crypto-1.2.4.js (c) 2013-2020 Kenji Urushima | kjur.github.io/jsrsasign/license
  2  */
  3 /*
  4  * crypto.js - Cryptographic Algorithm Provider class
  5  *
  6  * Copyright (c) 2013-2020 Kenji Urushima (kenji.urushima@gmail.com)
  7  *
  8  * This software is licensed under the terms of the MIT License.
  9  * https://kjur.github.io/jsrsasign/license
 10  *
 11  * The above copyright and license notice shall be 
 12  * included in all copies or substantial portions of the Software.
 13  */
 14 
 15 /**
 16  * @fileOverview
 17  * @name crypto-1.1.js
 18  * @author Kenji Urushima kenji.urushima@gmail.com
 19  * @version 1.2.4 (2020-Jul-28)
 20  * @since jsrsasign 2.2
 21  * @license <a href="https://kjur.github.io/jsrsasign/license/">MIT License</a>
 22  */
 23 
 24 /** 
 25  * kjur's class library name space
 26  * @name KJUR
 27  * @namespace kjur's class library name space
 28  */
 29 if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
 30 /**
 31  * kjur's cryptographic algorithm provider library name space
 32  * <p>
 33  * This namespace privides following crytpgrahic classes.
 34  * <ul>
 35  * <li>{@link KJUR.crypto.MessageDigest} - Java JCE(cryptograhic extension) style MessageDigest class</li>
 36  * <li>{@link KJUR.crypto.Signature} - Java JCE(cryptograhic extension) style Signature class</li>
 37  * <li>{@link KJUR.crypto.Cipher} - class for encrypting and decrypting data</li>
 38  * <li>{@link KJUR.crypto.Util} - cryptographic utility functions and properties</li>
 39  * </ul>
 40  * NOTE: Please ignore method summary and document of this namespace. This caused by a bug of jsdoc2.
 41  * </p>
 42  * @name KJUR.crypto
 43  * @namespace
 44  */
 45 if (typeof KJUR.crypto == "undefined" || !KJUR.crypto) KJUR.crypto = {};
 46 
 47 /**
 48  * static object for cryptographic function utilities
 49  * @name KJUR.crypto.Util
 50  * @class static object for cryptographic function utilities
 51  * @property {Array} DIGESTINFOHEAD PKCS#1 DigestInfo heading hexadecimal bytes for each hash algorithms
 52  * @property {Array} DEFAULTPROVIDER associative array of default provider name for each hash and signature algorithms
 53  * @description
 54  */
 55 KJUR.crypto.Util = new function() {
 56     this.DIGESTINFOHEAD = {
 57 	'sha1':      "3021300906052b0e03021a05000414",
 58         'sha224':    "302d300d06096086480165030402040500041c",
 59 	'sha256':    "3031300d060960864801650304020105000420",
 60 	'sha384':    "3041300d060960864801650304020205000430",
 61 	'sha512':    "3051300d060960864801650304020305000440",
 62 	'md2':       "3020300c06082a864886f70d020205000410",
 63 	'md5':       "3020300c06082a864886f70d020505000410",
 64 	'ripemd160': "3021300906052b2403020105000414",
 65     };
 66 
 67     /*
 68      * @since crypto 1.1.1
 69      */
 70     this.DEFAULTPROVIDER = {
 71 	'md5':			'cryptojs',
 72 	'sha1':			'cryptojs',
 73 	'sha224':		'cryptojs',
 74 	'sha256':		'cryptojs',
 75 	'sha384':		'cryptojs',
 76 	'sha512':		'cryptojs',
 77 	'ripemd160':		'cryptojs',
 78 	'hmacmd5':		'cryptojs',
 79 	'hmacsha1':		'cryptojs',
 80 	'hmacsha224':		'cryptojs',
 81 	'hmacsha256':		'cryptojs',
 82 	'hmacsha384':		'cryptojs',
 83 	'hmacsha512':		'cryptojs',
 84 	'hmacripemd160':	'cryptojs',
 85 
 86 	'MD5withRSA':		'cryptojs/jsrsa',
 87 	'SHA1withRSA':		'cryptojs/jsrsa',
 88 	'SHA224withRSA':	'cryptojs/jsrsa',
 89 	'SHA256withRSA':	'cryptojs/jsrsa',
 90 	'SHA384withRSA':	'cryptojs/jsrsa',
 91 	'SHA512withRSA':	'cryptojs/jsrsa',
 92 	'RIPEMD160withRSA':	'cryptojs/jsrsa',
 93 
 94 	'MD5withECDSA':		'cryptojs/jsrsa',
 95 	'SHA1withECDSA':	'cryptojs/jsrsa',
 96 	'SHA224withECDSA':	'cryptojs/jsrsa',
 97 	'SHA256withECDSA':	'cryptojs/jsrsa',
 98 	'SHA384withECDSA':	'cryptojs/jsrsa',
 99 	'SHA512withECDSA':	'cryptojs/jsrsa',
100 	'RIPEMD160withECDSA':	'cryptojs/jsrsa',
101 
102 	'SHA1withDSA':		'cryptojs/jsrsa',
103 	'SHA224withDSA':	'cryptojs/jsrsa',
104 	'SHA256withDSA':	'cryptojs/jsrsa',
105 
106 	'MD5withRSAandMGF1':		'cryptojs/jsrsa',
107 	'SHAwithRSAandMGF1':		'cryptojs/jsrsa',
108 	'SHA1withRSAandMGF1':		'cryptojs/jsrsa',
109 	'SHA224withRSAandMGF1':		'cryptojs/jsrsa',
110 	'SHA256withRSAandMGF1':		'cryptojs/jsrsa',
111 	'SHA384withRSAandMGF1':		'cryptojs/jsrsa',
112 	'SHA512withRSAandMGF1':		'cryptojs/jsrsa',
113 	'RIPEMD160withRSAandMGF1':	'cryptojs/jsrsa',
114     };
115 
116     /*
117      * @since crypto 1.1.2
118      */
119     this.CRYPTOJSMESSAGEDIGESTNAME = {
120 	'md5':		CryptoJS.algo.MD5,
121 	'sha1':		CryptoJS.algo.SHA1,
122 	'sha224':	CryptoJS.algo.SHA224,
123 	'sha256':	CryptoJS.algo.SHA256,
124 	'sha384':	CryptoJS.algo.SHA384,
125 	'sha512':	CryptoJS.algo.SHA512,
126 	'ripemd160':	CryptoJS.algo.RIPEMD160
127     };
128 
129     /**
130      * get hexadecimal DigestInfo
131      * @name getDigestInfoHex
132      * @memberOf KJUR.crypto.Util
133      * @function
134      * @param {String} hHash hexadecimal hash value
135      * @param {String} alg hash algorithm name (ex. 'sha1')
136      * @return {String} hexadecimal string DigestInfo ASN.1 structure
137      */
138     this.getDigestInfoHex = function(hHash, alg) {
139 	if (typeof this.DIGESTINFOHEAD[alg] == "undefined")
140 	    throw "alg not supported in Util.DIGESTINFOHEAD: " + alg;
141 	return this.DIGESTINFOHEAD[alg] + hHash;
142     };
143 
144     /**
145      * get PKCS#1 padded hexadecimal DigestInfo
146      * @name getPaddedDigestInfoHex
147      * @memberOf KJUR.crypto.Util
148      * @function
149      * @param {String} hHash hexadecimal hash value of message to be signed
150      * @param {String} alg hash algorithm name (ex. 'sha1')
151      * @param {Integer} keySize key bit length (ex. 1024)
152      * @return {String} hexadecimal string of PKCS#1 padded DigestInfo
153      */
154     this.getPaddedDigestInfoHex = function(hHash, alg, keySize) {
155 	var hDigestInfo = this.getDigestInfoHex(hHash, alg);
156 	var pmStrLen = keySize / 4; // minimum PM length
157 
158 	if (hDigestInfo.length + 22 > pmStrLen) // len(0001+ff(*8)+00+hDigestInfo)=22
159 	    throw "key is too short for SigAlg: keylen=" + keySize + "," + alg;
160 
161 	var hHead = "0001";
162 	var hTail = "00" + hDigestInfo;
163 	var hMid = "";
164 	var fLen = pmStrLen - hHead.length - hTail.length;
165 	for (var i = 0; i < fLen; i += 2) {
166 	    hMid += "ff";
167 	}
168 	var hPaddedMessage = hHead + hMid + hTail;
169 	return hPaddedMessage;
170     };
171 
172     /**
173      * get hexadecimal hash of string with specified algorithm
174      * @name hashString
175      * @memberOf KJUR.crypto.Util
176      * @function
177      * @param {String} s raw input string to be hashed
178      * @param {String} alg hash algorithm name
179      * @return {String} hexadecimal string of hash value
180      * @since 1.1.1
181      */
182     this.hashString = function(s, alg) {
183         var md = new KJUR.crypto.MessageDigest({'alg': alg});
184         return md.digestString(s);
185     };
186 
187     /**
188      * get hexadecimal hash of hexadecimal string with specified algorithm
189      * @name hashHex
190      * @memberOf KJUR.crypto.Util
191      * @function
192      * @param {String} sHex input hexadecimal string to be hashed
193      * @param {String} alg hash algorithm name
194      * @return {String} hexadecimal string of hash value
195      * @since 1.1.1
196      */
197     this.hashHex = function(sHex, alg) {
198         var md = new KJUR.crypto.MessageDigest({'alg': alg});
199         return md.digestHex(sHex);
200     };
201 
202     /**
203      * get hexadecimal SHA1 hash of string
204      * @name sha1
205      * @memberOf KJUR.crypto.Util
206      * @function
207      * @param {String} s raw input string to be hashed
208      * @return {String} hexadecimal string of hash value
209      * @since 1.0.3
210      */
211     this.sha1 = function(s) {
212 	return this.hashString(s, 'sha1');
213     };
214 
215     /**
216      * get hexadecimal SHA256 hash of string
217      * @name sha256
218      * @memberOf KJUR.crypto.Util
219      * @function
220      * @param {String} s raw input string to be hashed
221      * @return {String} hexadecimal string of hash value
222      * @since 1.0.3
223      */
224     this.sha256 = function(s) {
225 	return this.hashString(s, 'sha256');
226     };
227 
228     this.sha256Hex = function(s) {
229 	return this.hashHex(s, 'sha256');
230     };
231 
232     /**
233      * get hexadecimal SHA512 hash of string
234      * @name sha512
235      * @memberOf KJUR.crypto.Util
236      * @function
237      * @param {String} s raw input string to be hashed
238      * @return {String} hexadecimal string of hash value
239      * @since 1.0.3
240      */
241     this.sha512 = function(s) {
242 	return this.hashString(s, 'sha512');
243     };
244 
245     this.sha512Hex = function(s) {
246 	return this.hashHex(s, 'sha512');
247     };
248 
249     /**
250      * check if key object (RSA/DSA/ECDSA) or not
251      * @name isKey
252      * @memberOf KJUR.crypto.Util
253      * @function
254      * @param {Object} obj any type argument to be checked
255      * @return {Boolean} true if this is key object otherwise false
256      * @since 1.0.3
257      */
258     this.isKey = function(obj) {
259 	if (obj instanceof RSAKey ||
260 	    obj instanceof KJUR.crypto.DSA ||
261 	    obj instanceof KJUR.crypto.ECDSA) {
262 	    return true;
263 	} else {
264 	    return false;
265 	}
266     };
267 };
268 
269 /**
270  * get hexadecimal MD5 hash of string
271  * @name md5
272  * @memberOf KJUR.crypto.Util
273  * @function
274  * @param {String} s input string to be hashed
275  * @return {String} hexadecimal string of hash value
276  * @since 1.0.3
277  * @example
278  * Util.md5('aaa') → 47bce5c74f589f4867dbd57e9ca9f808
279  */
280 KJUR.crypto.Util.md5 = function(s) {
281     var md = new KJUR.crypto.MessageDigest({'alg':'md5', 'prov':'cryptojs'});
282     return md.digestString(s);
283 };
284 
285 /**
286  * get hexadecimal RIPEMD160 hash of string
287  * @name ripemd160
288  * @memberOf KJUR.crypto.Util
289  * @function
290  * @param {String} s input string to be hashed
291  * @return {String} hexadecimal string of hash value
292  * @since 1.0.3
293  * @example
294  * KJUR.crypto.Util.ripemd160("aaa") → 08889bd7b151aa174c21f33f59147fa65381edea
295  */
296 KJUR.crypto.Util.ripemd160 = function(s) {
297     var md = new KJUR.crypto.MessageDigest({'alg':'ripemd160', 'prov':'cryptojs'});
298     return md.digestString(s);
299 };
300 
301 // @since jsrsasign 7.0.0 crypto 1.1.11
302 KJUR.crypto.Util.SECURERANDOMGEN = new SecureRandom();
303 
304 /**
305  * get hexadecimal string of random value from with specified byte length<br/>
306  * @name getRandomHexOfNbytes
307  * @memberOf KJUR.crypto.Util
308  * @function
309  * @param {Integer} n length of bytes of random
310  * @return {String} hexadecimal string of random
311  * @since jsrsasign 7.0.0 crypto 1.1.11
312  * @example
313  * KJUR.crypto.Util.getRandomHexOfNbytes(3) → "6314af", "000000" or "001fb4"
314  * KJUR.crypto.Util.getRandomHexOfNbytes(128) → "8fbc..." in 1024bits 
315  */
316 KJUR.crypto.Util.getRandomHexOfNbytes = function(n) {
317     var ba = new Array(n);
318     KJUR.crypto.Util.SECURERANDOMGEN.nextBytes(ba);
319     return BAtohex(ba);
320 };
321 
322 /**
323  * get BigInteger object of random value from with specified byte length<br/>
324  * @name getRandomBigIntegerOfNbytes
325  * @memberOf KJUR.crypto.Util
326  * @function
327  * @param {Integer} n length of bytes of random
328  * @return {BigInteger} BigInteger object of specified random value
329  * @since jsrsasign 7.0.0 crypto 1.1.11
330  * @example
331  * KJUR.crypto.Util.getRandomBigIntegerOfNbytes(3) → 6314af of BigInteger
332  * KJUR.crypto.Util.getRandomBigIntegerOfNbytes(128) → 8fbc... of BigInteger
333  */
334 KJUR.crypto.Util.getRandomBigIntegerOfNbytes = function(n) {
335     return new BigInteger(KJUR.crypto.Util.getRandomHexOfNbytes(n), 16);
336 };
337 
338 /**
339  * get hexadecimal string of random value from with specified bit length<br/>
340  * @name getRandomHexOfNbits
341  * @memberOf KJUR.crypto.Util
342  * @function
343  * @param {Integer} n length of bits of random
344  * @return {String} hexadecimal string of random
345  * @since jsrsasign 7.0.0 crypto 1.1.11
346  * @example
347  * KJUR.crypto.Util.getRandomHexOfNbits(24) → "6314af", "000000" or "001fb4"
348  * KJUR.crypto.Util.getRandomHexOfNbits(1024) → "8fbc..." in 1024bits 
349  */
350 KJUR.crypto.Util.getRandomHexOfNbits = function(n) {
351     var n_remainder = n % 8;
352     var n_quotient = (n - n_remainder) / 8;
353     var ba = new Array(n_quotient + 1);
354     KJUR.crypto.Util.SECURERANDOMGEN.nextBytes(ba);
355     ba[0] = (((255 << n_remainder) & 255) ^ 255) & ba[0];
356     return BAtohex(ba);
357 };
358 
359 /**
360  * get BigInteger object of random value from with specified bit length<br/>
361  * @name getRandomBigIntegerOfNbits
362  * @memberOf KJUR.crypto.Util
363  * @function
364  * @param {Integer} n length of bits of random
365  * @return {BigInteger} BigInteger object of specified random value
366  * @since jsrsasign 7.0.0 crypto 1.1.11
367  * @example
368  * KJUR.crypto.Util.getRandomBigIntegerOfNbits(24) → 6314af of BigInteger
369  * KJUR.crypto.Util.getRandomBigIntegerOfNbits(1024) → 8fbc... of BigInteger
370  */
371 KJUR.crypto.Util.getRandomBigIntegerOfNbits = function(n) {
372     return new BigInteger(KJUR.crypto.Util.getRandomHexOfNbits(n), 16);
373 };
374 
375 /**
376  * get BigInteger object of random value from zero to max value<br/>
377  * @name getRandomBigIntegerZeroToMax
378  * @memberOf KJUR.crypto.Util
379  * @function
380  * @param {BigInteger} biMax max value of BigInteger object for random value
381  * @return {BigInteger} BigInteger object of specified random value
382  * @since jsrsasign 7.0.0 crypto 1.1.11
383  * @description
384  * This static method generates a BigInteger object with random value
385  * greater than or equal to zero and smaller than or equal to biMax
386  * (i.e. 0 ≤ result ≤ biMax).
387  * @example
388  * biMax = new BigInteger("3fa411...", 16);
389  * KJUR.crypto.Util.getRandomBigIntegerZeroToMax(biMax) → 8fbc... of BigInteger
390  */
391 KJUR.crypto.Util.getRandomBigIntegerZeroToMax = function(biMax) {
392     var bitLenMax = biMax.bitLength();
393     while (1) {
394 	var biRand = KJUR.crypto.Util.getRandomBigIntegerOfNbits(bitLenMax);
395 	if (biMax.compareTo(biRand) != -1) return biRand;
396     }
397 };
398 
399 /**
400  * get BigInteger object of random value from min value to max value<br/>
401  * @name getRandomBigIntegerMinToMax
402  * @memberOf KJUR.crypto.Util
403  * @function
404  * @param {BigInteger} biMin min value of BigInteger object for random value
405  * @param {BigInteger} biMax max value of BigInteger object for random value
406  * @return {BigInteger} BigInteger object of specified random value
407  * @since jsrsasign 7.0.0 crypto 1.1.11
408  * @description
409  * This static method generates a BigInteger object with random value
410  * greater than or equal to biMin and smaller than or equal to biMax
411  * (i.e. biMin ≤ result ≤ biMax).
412  * @example
413  * biMin = new BigInteger("2fa411...", 16);
414  * biMax = new BigInteger("3fa411...", 16);
415  * KJUR.crypto.Util.getRandomBigIntegerMinToMax(biMin, biMax) → 32f1... of BigInteger
416  */
417 KJUR.crypto.Util.getRandomBigIntegerMinToMax = function(biMin, biMax) {
418     var flagCompare = biMin.compareTo(biMax);
419     if (flagCompare == 1) throw "biMin is greater than biMax";
420     if (flagCompare == 0) return biMin;
421 
422     var biDiff = biMax.subtract(biMin);
423     var biRand = KJUR.crypto.Util.getRandomBigIntegerZeroToMax(biDiff);
424     return biRand.add(biMin);
425 };
426 
427 // === Mac ===============================================================
428 
429 /**
430  * MessageDigest class which is very similar to java.security.MessageDigest class<br/>
431  * @name KJUR.crypto.MessageDigest
432  * @class MessageDigest class which is very similar to java.security.MessageDigest class
433  * @param {Array} params parameters for constructor
434  * @property {Array} HASHLENGTH static Array of resulted byte length of hash (ex. HASHLENGTH["sha1"] == 20)
435  * @description
436  * <br/>
437  * Currently this supports following algorithm and providers combination:
438  * <ul>
439  * <li>md5 - cryptojs</li>
440  * <li>sha1 - cryptojs</li>
441  * <li>sha224 - cryptojs</li>
442  * <li>sha256 - cryptojs</li>
443  * <li>sha384 - cryptojs</li>
444  * <li>sha512 - cryptojs</li>
445  * <li>ripemd160 - cryptojs</li>
446  * <li>sha256 - sjcl (NEW from crypto.js 1.0.4)</li>
447  * </ul>
448  * @example
449  * // CryptoJS provider sample
450  * var md = new KJUR.crypto.MessageDigest({alg: "sha1", prov: "cryptojs"});
451  * md.updateString('aaa')
452  * var mdHex = md.digest()
453  *
454  * // SJCL(Stanford JavaScript Crypto Library) provider sample
455  * var md = new KJUR.crypto.MessageDigest({alg: "sha256", prov: "sjcl"}); // sjcl supports sha256 only
456  * md.updateString('aaa')
457  * var mdHex = md.digest()
458  *
459  * // HASHLENGTH property
460  * KJUR.crypto.MessageDigest.HASHLENGTH['sha1'] &rarr 20
461  * KJUR.crypto.MessageDigest.HASHLENGTH['sha512'] &rarr 64
462  */
463 KJUR.crypto.MessageDigest = function(params) {
464     var md = null;
465     var algName = null;
466     var provName = null;
467 
468     /**
469      * set hash algorithm and provider<br/>
470      * @name setAlgAndProvider
471      * @memberOf KJUR.crypto.MessageDigest#
472      * @function
473      * @param {String} alg hash algorithm name
474      * @param {String} prov provider name
475      * @description
476      * This methods set an algorithm and a cryptographic provider.<br/>
477      * Here is acceptable algorithm names ignoring cases and hyphens:
478      * <ul>
479      * <li>MD5</li>
480      * <li>SHA1</li>
481      * <li>SHA224</li>
482      * <li>SHA256</li>
483      * <li>SHA384</li>
484      * <li>SHA512</li>
485      * <li>RIPEMD160</li>
486      * </ul>
487      * NOTE: Since jsrsasign 6.2.0 crypto 1.1.10, this method ignores
488      * upper or lower cases. Also any hyphens (i.e. "-") will be ignored
489      * so that "SHA1" or "SHA-1" will be acceptable.
490      * @example
491      * // for SHA1
492      * md.setAlgAndProvider('sha1', 'cryptojs');
493      * md.setAlgAndProvider('SHA1');
494      * // for RIPEMD160
495      * md.setAlgAndProvider('ripemd160', 'cryptojs');
496      */
497     this.setAlgAndProvider = function(alg, prov) {
498 	alg = KJUR.crypto.MessageDigest.getCanonicalAlgName(alg);
499 
500 	if (alg !== null && prov === undefined) prov = KJUR.crypto.Util.DEFAULTPROVIDER[alg];
501 
502 	// for cryptojs
503 	if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(alg) != -1 &&
504 	    prov == 'cryptojs') {
505 	    try {
506 		this.md = KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME[alg].create();
507 	    } catch (ex) {
508 		throw "setAlgAndProvider hash alg set fail alg=" + alg + "/" + ex;
509 	    }
510 	    this.updateString = function(str) {
511 		this.md.update(str);
512 	    };
513 	    this.updateHex = function(hex) {
514 		var wHex = CryptoJS.enc.Hex.parse(hex);
515 		this.md.update(wHex);
516 	    };
517 	    this.digest = function() {
518 		var hash = this.md.finalize();
519 		return hash.toString(CryptoJS.enc.Hex);
520 	    };
521 	    this.digestString = function(str) {
522 		this.updateString(str);
523 		return this.digest();
524 	    };
525 	    this.digestHex = function(hex) {
526 		this.updateHex(hex);
527 		return this.digest();
528 	    };
529 	}
530 	if (':sha256:'.indexOf(alg) != -1 &&
531 	    prov == 'sjcl') {
532 	    try {
533 		this.md = new sjcl.hash.sha256();
534 	    } catch (ex) {
535 		throw "setAlgAndProvider hash alg set fail alg=" + alg + "/" + ex;
536 	    }
537 	    this.updateString = function(str) {
538 		this.md.update(str);
539 	    };
540 	    this.updateHex = function(hex) {
541 		var baHex = sjcl.codec.hex.toBits(hex);
542 		this.md.update(baHex);
543 	    };
544 	    this.digest = function() {
545 		var hash = this.md.finalize();
546 		return sjcl.codec.hex.fromBits(hash);
547 	    };
548 	    this.digestString = function(str) {
549 		this.updateString(str);
550 		return this.digest();
551 	    };
552 	    this.digestHex = function(hex) {
553 		this.updateHex(hex);
554 		return this.digest();
555 	    };
556 	}
557     };
558 
559     /**
560      * update digest by specified string
561      * @name updateString
562      * @memberOf KJUR.crypto.MessageDigest#
563      * @function
564      * @param {String} str string to update
565      * @description
566      * @example
567      * md.updateString('New York');
568      */
569     this.updateString = function(str) {
570 	throw "updateString(str) not supported for this alg/prov: " + this.algName + "/" + this.provName;
571     };
572 
573     /**
574      * update digest by specified hexadecimal string
575      * @name updateHex
576      * @memberOf KJUR.crypto.MessageDigest#
577      * @function
578      * @param {String} hex hexadecimal string to update
579      * @description
580      * @example
581      * md.updateHex('0afe36');
582      */
583     this.updateHex = function(hex) {
584 	throw "updateHex(hex) not supported for this alg/prov: " + this.algName + "/" + this.provName;
585     };
586 
587     /**
588      * completes hash calculation and returns hash result
589      * @name digest
590      * @memberOf KJUR.crypto.MessageDigest#
591      * @function
592      * @description
593      * @example
594      * md.digest()
595      */
596     this.digest = function() {
597 	throw "digest() not supported for this alg/prov: " + this.algName + "/" + this.provName;
598     };
599 
600     /**
601      * performs final update on the digest using string, then completes the digest computation
602      * @name digestString
603      * @memberOf KJUR.crypto.MessageDigest#
604      * @function
605      * @param {String} str string to final update
606      * @description
607      * @example
608      * md.digestString('aaa')
609      */
610     this.digestString = function(str) {
611 	throw "digestString(str) not supported for this alg/prov: " + this.algName + "/" + this.provName;
612     };
613 
614     /**
615      * performs final update on the digest using hexadecimal string, then completes the digest computation
616      * @name digestHex
617      * @memberOf KJUR.crypto.MessageDigest#
618      * @function
619      * @param {String} hex hexadecimal string to final update
620      * @description
621      * @example
622      * md.digestHex('0f2abd')
623      */
624     this.digestHex = function(hex) {
625 	throw "digestHex(hex) not supported for this alg/prov: " + this.algName + "/" + this.provName;
626     };
627 
628     if (params !== undefined) {
629 	if (params['alg'] !== undefined) {
630 	    this.algName = params['alg'];
631 	    if (params['prov'] === undefined)
632 		this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
633 	    this.setAlgAndProvider(this.algName, this.provName);
634 	}
635     }
636 };
637 
638 /**
639  * get canonical hash algorithm name<br/>
640  * @name getCanonicalAlgName
641  * @memberOf KJUR.crypto.MessageDigest
642  * @function
643  * @param {String} alg hash algorithm name (ex. MD5, SHA-1, SHA1, SHA512 et.al.)
644  * @return {String} canonical hash algorithm name
645  * @since jsrsasign 6.2.0 crypto 1.1.10
646  * @description
647  * This static method normalizes from any hash algorithm name such as
648  * "SHA-1", "SHA1", "MD5", "sha512" to lower case name without hyphens
649  * such as "sha1".
650  * @example
651  * KJUR.crypto.MessageDigest.getCanonicalAlgName("SHA-1") &rarr "sha1"
652  * KJUR.crypto.MessageDigest.getCanonicalAlgName("MD5")   &rarr "md5"
653  */
654 KJUR.crypto.MessageDigest.getCanonicalAlgName = function(alg) {
655     if (typeof alg === "string") {
656 	alg = alg.toLowerCase();
657 	alg = alg.replace(/-/, '');
658     }
659     return alg;
660 };
661 
662 /**
663  * get resulted hash byte length for specified algorithm name<br/>
664  * @name getHashLength
665  * @memberOf KJUR.crypto.MessageDigest
666  * @function
667  * @param {String} alg non-canonicalized hash algorithm name (ex. MD5, SHA-1, SHA1, SHA512 et.al.)
668  * @return {Integer} resulted hash byte length
669  * @since jsrsasign 6.2.0 crypto 1.1.10
670  * @description
671  * This static method returns resulted byte length for specified algorithm name such as "SHA-1".
672  * @example
673  * KJUR.crypto.MessageDigest.getHashLength("SHA-1") &rarr 20
674  * KJUR.crypto.MessageDigest.getHashLength("sha1") &rarr 20
675  */
676 KJUR.crypto.MessageDigest.getHashLength = function(alg) {
677     var MD = KJUR.crypto.MessageDigest
678     var alg2 = MD.getCanonicalAlgName(alg);
679     if (MD.HASHLENGTH[alg2] === undefined)
680 	throw "not supported algorithm: " + alg;
681     return MD.HASHLENGTH[alg2];
682 };
683 
684 // described in KJUR.crypto.MessageDigest class (since jsrsasign 6.2.0 crypto 1.1.10)
685 KJUR.crypto.MessageDigest.HASHLENGTH = {
686     'md5':		16,
687     'sha1':		20,
688     'sha224':		28,
689     'sha256':		32,
690     'sha384':		48,
691     'sha512':		64,
692     'ripemd160':	20
693 };
694 
695 // === Mac ===============================================================
696 
697 /**
698  * Mac(Message Authentication Code) class which is very similar to java.security.Mac class 
699  * @name KJUR.crypto.Mac
700  * @class Mac class which is very similar to java.security.Mac class
701  * @param {Array} params parameters for constructor
702  * @description
703  * <br/>
704  * Currently this supports following algorithm and providers combination:
705  * <ul>
706  * <li>hmacmd5 - cryptojs</li>
707  * <li>hmacsha1 - cryptojs</li>
708  * <li>hmacsha224 - cryptojs</li>
709  * <li>hmacsha256 - cryptojs</li>
710  * <li>hmacsha384 - cryptojs</li>
711  * <li>hmacsha512 - cryptojs</li>
712  * </ul>
713  * NOTE: HmacSHA224 and HmacSHA384 issue was fixed since jsrsasign 4.1.4.
714  * Please use 'ext/cryptojs-312-core-fix*.js' instead of 'core.js' of original CryptoJS
715  * to avoid those issue.
716  * <br/>
717  * NOTE2: Hmac signature bug was fixed in jsrsasign 4.9.0 by providing CryptoJS
718  * bug workaround.
719  * <br/>
720  * Please see {@link KJUR.crypto.Mac.setPassword}, how to provide password
721  * in various ways in detail.
722  * @example
723  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": "pass"});
724  * mac.updateString('aaa')
725  * mac.doFinal() → "5737da..."
726  *
727  * // other password representation 
728  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"hex":  "6161"}});
729  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"utf8": "aa"}});
730  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"rstr": "\x61\x61"}});
731  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"b64":  "Mi02/+...a=="}});
732  * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": {"b64u": "Mi02_-...a"}});
733  */
734 KJUR.crypto.Mac = function(params) {
735     var mac = null;
736     var pass = null;
737     var algName = null;
738     var provName = null;
739     var algProv = null;
740 
741     this.setAlgAndProvider = function(alg, prov) {
742 	alg = alg.toLowerCase();
743 
744 	if (alg == null) alg = "hmacsha1";
745 
746 	alg = alg.toLowerCase();
747         if (alg.substr(0, 4) != "hmac") {
748 	    throw "setAlgAndProvider unsupported HMAC alg: " + alg;
749 	}
750 
751 	if (prov === undefined) prov = KJUR.crypto.Util.DEFAULTPROVIDER[alg];
752 	this.algProv = alg + "/" + prov;
753 
754 	var hashAlg = alg.substr(4);
755 
756 	// for cryptojs
757 	if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(hashAlg) != -1 &&
758 	    prov == 'cryptojs') {
759 	    try {
760 		var mdObj = KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME[hashAlg];
761 		this.mac = CryptoJS.algo.HMAC.create(mdObj, this.pass);
762 	    } catch (ex) {
763 		throw "setAlgAndProvider hash alg set fail hashAlg=" + hashAlg + "/" + ex;
764 	    }
765 	    this.updateString = function(str) {
766 		this.mac.update(str);
767 	    };
768 	    this.updateHex = function(hex) {
769 		var wHex = CryptoJS.enc.Hex.parse(hex);
770 		this.mac.update(wHex);
771 	    };
772 	    this.doFinal = function() {
773 		var hash = this.mac.finalize();
774 		return hash.toString(CryptoJS.enc.Hex);
775 	    };
776 	    this.doFinalString = function(str) {
777 		this.updateString(str);
778 		return this.doFinal();
779 	    };
780 	    this.doFinalHex = function(hex) {
781 		this.updateHex(hex);
782 		return this.doFinal();
783 	    };
784 	}
785     };
786 
787     /**
788      * update digest by specified string<br/>
789      * @name updateString
790      * @memberOf KJUR.crypto.Mac#
791      * @function
792      * @param {String} str string to update
793      *
794      * @description
795      * @example
796      * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": "pass"});
797      * mac.updateString('aaa')
798      * mac.doFinal() → "5737da..."
799      */
800     this.updateString = function(str) {
801 	throw "updateString(str) not supported for this alg/prov: " + this.algProv;
802     };
803 
804     /**
805      * update digest by specified hexadecimal string<br/>
806      * @name updateHex
807      * @memberOf KJUR.crypto.Mac#
808      * @function
809      * @param {String} hex hexadecimal string to update
810      *
811      * @description
812      * @example
813      * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": "pass"});
814      * mac.updateHex('616161')
815      * mac.doFinal() → "5737da..."
816      */
817     this.updateHex = function(hex) {
818 	throw "updateHex(hex) not supported for this alg/prov: " + this.algProv;
819     };
820 
821     /**
822      * completes hash calculation and returns hash result<br/>
823      * @name doFinal
824      * @memberOf KJUR.crypto.Mac#
825      * @function
826      * @returns hexadecimal string of Mac result value
827      *
828      * @description
829      * @example
830      * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": "pass"});
831      * mac.updateString('aaa')
832      * mac.doFinal() → "5737da..."
833      */
834     this.doFinal = function() {
835 	throw "digest() not supported for this alg/prov: " + this.algProv;
836     };
837 
838     /**
839      * performs final update on the digest using string, then completes the digest computation<br/>
840      * @name doFinalString
841      * @memberOf KJUR.crypto.Mac#
842      * @function
843      * @param {String} str raw string to final update
844      * @returns hexadecimal string of Mac result value
845      *
846      * @description
847      * @example
848      * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": "pass"});
849      * mac.doFinalString("aaa") → "5737da..."
850      */
851     this.doFinalString = function(str) {
852 	throw "digestString(str) not supported for this alg/prov: " + this.algProv;
853     };
854 
855     /**
856      * performs final update on the digest using hexadecimal string, then completes the digest computation<br/>
857      * @name doFinalHex
858      * @memberOf KJUR.crypto.Mac#
859      * @function
860      * @param {String} hex hexadecimal string to final update
861      * @returns hexadecimal string of Mac result value
862      *
863      * @description
864      * @example
865      * var mac = new KJUR.crypto.Mac({alg: "HmacSHA256", "pass": "pass"});
866      * mac.doFinalHex("616161") → "5737da..."
867      */
868     this.doFinalHex = function(hex) {
869 	throw "digestHex(hex) not supported for this alg/prov: " + this.algProv;
870     };
871 
872     /**
873      * set password for Mac<br/>
874      * @name setPassword
875      * @memberOf KJUR.crypto.Mac#
876      * @function
877      * @param {Object} pass password for Mac
878      * @since crypto 1.1.7 jsrsasign 4.9.0
879      * @description
880      * This method will set password for (H)Mac internally.
881      * Argument 'pass' can be specified as following:
882      * <ul>
883      * <li>even length string of 0..9, a..f or A-F: implicitly specified as hexadecimal string</li>
884      * <li>not above string: implicitly specified as raw string</li>
885      * <li>{rstr: "\x65\x70"}: explicitly specified as raw string</li>
886      * <li>{hex: "6570"}: explicitly specified as hexacedimal string</li>
887      * <li>{utf8: "秘密"}: explicitly specified as UTF8 string</li>
888      * <li>{b64: "Mi78..=="}: explicitly specified as Base64 string</li>
889      * <li>{b64u: "Mi7-_"}: explicitly specified as Base64URL string</li>
890      * </ul>
891      * It is *STRONGLY RECOMMENDED* that explicit representation of password argument
892      * to avoid ambiguity. For example string  "6161" can mean a string "6161" or 
893      * a hexadecimal string of "aa" (i.e. \x61\x61).
894      * @example
895      * mac = KJUR.crypto.Mac({'alg': 'hmacsha256'});
896      * // set password by implicit raw string
897      * mac.setPassword("\x65\x70\xb9\x0b");
898      * mac.setPassword("password");
899      * // set password by implicit hexadecimal string
900      * mac.setPassword("6570b90b");
901      * mac.setPassword("6570B90B");
902      * // set password by explicit raw string
903      * mac.setPassword({"rstr": "\x65\x70\xb9\x0b"});
904      * // set password by explicit hexadecimal string
905      * mac.setPassword({"hex": "6570b90b"});
906      * // set password by explicit utf8 string
907      * mac.setPassword({"utf8": "passwordパスワード");
908      * // set password by explicit Base64 string
909      * mac.setPassword({"b64": "Mb+c3f/=="});
910      * // set password by explicit Base64URL string
911      * mac.setPassword({"b64u": "Mb-c3f_"});
912      */
913     this.setPassword = function(pass) {
914 	// internal this.pass shall be CryptoJS DWord Object for CryptoJS bug
915 	// work around. CrytoJS HMac password can be passed by
916 	// raw string as described in the manual however it doesn't
917 	// work properly in some case. If password was passed
918 	// by CryptoJS DWord which is not described in the manual
919 	// it seems to work. (fixed since crypto 1.1.7)
920 
921 	if (typeof pass == 'string') {
922 	    var hPass = pass;
923 	    if (pass.length % 2 == 1 || ! pass.match(/^[0-9A-Fa-f]+$/)) { // raw str
924 		hPass = rstrtohex(pass);
925 	    }
926 	    this.pass = CryptoJS.enc.Hex.parse(hPass);
927 	    return;
928 	}
929 
930 	if (typeof pass != 'object')
931 	    throw "KJUR.crypto.Mac unsupported password type: " + pass;
932 	
933 	var hPass = null;
934 	if (pass.hex  !== undefined) {
935 	    if (pass.hex.length % 2 != 0 || ! pass.hex.match(/^[0-9A-Fa-f]+$/))
936 		throw "Mac: wrong hex password: " + pass.hex;
937 	    hPass = pass.hex;
938 	}
939 	if (pass.utf8 !== undefined) hPass = utf8tohex(pass.utf8);
940 	if (pass.rstr !== undefined) hPass = rstrtohex(pass.rstr);
941 	if (pass.b64  !== undefined) hPass = b64tohex(pass.b64);
942 	if (pass.b64u !== undefined) hPass = b64utohex(pass.b64u);
943 
944 	if (hPass == null)
945 	    throw "KJUR.crypto.Mac unsupported password type: " + pass;
946 
947 	this.pass = CryptoJS.enc.Hex.parse(hPass);
948     };
949 
950     if (params !== undefined) {
951 	if (params.pass !== undefined) {
952 	    this.setPassword(params.pass);
953 	}
954 	if (params.alg !== undefined) {
955 	    this.algName = params.alg;
956 	    if (params['prov'] === undefined)
957 		this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
958 	    this.setAlgAndProvider(this.algName, this.provName);
959 	}
960     }
961 };
962 
963 // ====== Signature class ====================================================
964 /**
965  * Signature class which is very similar to java.security.Signature class
966  * @name KJUR.crypto.Signature
967  * @class Signature class which is very similar to java.security.Signature class
968  * @param {Array} params parameters for constructor
969  * @property {String} state Current state of this signature object whether 'SIGN', 'VERIFY' or null
970  * @description
971  * <br/>
972  * As for params of constructor's argument, it can be specify following attributes:
973  * <ul>
974  * <li>alg - signature algorithm name (ex. {MD5,SHA1,SHA224,SHA256,SHA384,SHA512,RIPEMD160}with{RSA,ECDSA,DSA})</li>
975  * <li>provider - currently 'cryptojs/jsrsa' only</li>
976  * </ul>
977  * <h4>SUPPORTED ALGORITHMS AND PROVIDERS</h4>
978  * This Signature class supports following signature algorithm and provider names:
979  * <ul>
980  * <li>MD5withRSA - cryptojs/jsrsa</li>
981  * <li>SHA1withRSA - cryptojs/jsrsa</li>
982  * <li>SHA224withRSA - cryptojs/jsrsa</li>
983  * <li>SHA256withRSA - cryptojs/jsrsa</li>
984  * <li>SHA384withRSA - cryptojs/jsrsa</li>
985  * <li>SHA512withRSA - cryptojs/jsrsa</li>
986  * <li>RIPEMD160withRSA - cryptojs/jsrsa</li>
987  * <li>MD5withECDSA - cryptojs/jsrsa</li>
988  * <li>SHA1withECDSA - cryptojs/jsrsa</li>
989  * <li>SHA224withECDSA - cryptojs/jsrsa</li>
990  * <li>SHA256withECDSA - cryptojs/jsrsa</li>
991  * <li>SHA384withECDSA - cryptojs/jsrsa</li>
992  * <li>SHA512withECDSA - cryptojs/jsrsa</li>
993  * <li>RIPEMD160withECDSA - cryptojs/jsrsa</li>
994  * <li>MD5withRSAandMGF1 - cryptojs/jsrsa</li>
995  * <li>SHAwithRSAandMGF1 - cryptojs/jsrsa</li>
996  * <li>SHA1withRSAandMGF1 - cryptojs/jsrsa</li>
997  * <li>SHA224withRSAandMGF1 - cryptojs/jsrsa</li>
998  * <li>SHA256withRSAandMGF1 - cryptojs/jsrsa</li>
999  * <li>SHA384withRSAandMGF1 - cryptojs/jsrsa</li>
1000  * <li>SHA512withRSAandMGF1 - cryptojs/jsrsa</li>
1001  * <li>RIPEMD160withRSAandMGF1 - cryptojs/jsrsa</li>
1002  * <li>SHA1withDSA - cryptojs/jsrsa</li>
1003  * <li>SHA224withDSA - cryptojs/jsrsa</li>
1004  * <li>SHA256withDSA - cryptojs/jsrsa</li>
1005  * </ul>
1006  * As for RSA-PSS signature algorithm names and signing parameters 
1007  * such as MGF function and salt length, please see
1008  * {@link KJUR.asn1.x509.AlgorithmIdentifier} class.
1009  *
1010  * Here are supported elliptic cryptographic curve names and their aliases for ECDSA:
1011  * <ul>
1012  * <li>secp256k1</li>
1013  * <li>secp256r1, NIST P-256, P-256, prime256v1</li>
1014  * <li>secp384r1, NIST P-384, P-384</li>
1015  * </ul>
1016  * NOTE1: DSA signing algorithm is also supported since crypto 1.1.5.
1017  * <h4>EXAMPLES</h4>
1018  * @example
1019  * // RSA signature generation
1020  * var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
1021  * sig.init(prvKeyPEM);
1022  * sig.updateString('aaa');
1023  * var hSigVal = sig.sign();
1024  *
1025  * // DSA signature validation
1026  * var sig2 = new KJUR.crypto.Signature({"alg": "SHA1withDSA"});
1027  * sig2.init(certPEM);
1028  * sig.updateString('aaa');
1029  * var isValid = sig2.verify(hSigVal);
1030  * 
1031  * // ECDSA signing
1032  * var sig = new KJUR.crypto.Signature({'alg':'SHA1withECDSA'});
1033  * sig.init(prvKeyPEM);
1034  * sig.updateString('aaa');
1035  * var sigValueHex = sig.sign();
1036  *
1037  * // ECDSA verifying
1038  * var sig2 = new KJUR.crypto.Signature({'alg':'SHA1withECDSA'});
1039  * sig.init(certPEM);
1040  * sig.updateString('aaa');
1041  * var isValid = sig.verify(sigValueHex);
1042  */
1043 KJUR.crypto.Signature = function(params) {
1044     var prvKey = null; // RSAKey/KJUR.crypto.{ECDSA,DSA} object for signing
1045     var pubKey = null; // RSAKey/KJUR.crypto.{ECDSA,DSA} object for verifying
1046 
1047     var md = null; // KJUR.crypto.MessageDigest object
1048     var sig = null;
1049     var algName = null;
1050     var provName = null;
1051     var algProvName = null;
1052     var mdAlgName = null;
1053     var pubkeyAlgName = null;	// rsa,ecdsa,rsaandmgf1(=rsapss)
1054     var state = null;
1055     var pssSaltLen = -1;
1056     var initParams = null;
1057 
1058     var sHashHex = null; // hex hash value for hex
1059     var hDigestInfo = null;
1060     var hPaddedDigestInfo = null;
1061     var hSign = null;
1062 
1063     this._setAlgNames = function() {
1064     var matchResult = this.algName.match(/^(.+)with(.+)$/);
1065 	if (matchResult) {
1066 	    this.mdAlgName = matchResult[1].toLowerCase();
1067 	    this.pubkeyAlgName = matchResult[2].toLowerCase();
1068 	    if (this.pubkeyAlgName == "rsaandmgf1" &&
1069 	        this.mdAlgName == "sha") {
1070 		this.mdAlgName = "sha1";
1071 	    }
1072 	}
1073     };
1074 
1075     this._zeroPaddingOfSignature = function(hex, bitLength) {
1076 	var s = "";
1077 	var nZero = bitLength / 4 - hex.length;
1078 	for (var i = 0; i < nZero; i++) {
1079 	    s = s + "0";
1080 	}
1081 	return s + hex;
1082     };
1083 
1084     /**
1085      * set signature algorithm and provider
1086      * @name setAlgAndProvider
1087      * @memberOf KJUR.crypto.Signature#
1088      * @function
1089      * @param {String} alg signature algorithm name
1090      * @param {String} prov provider name
1091      * @description
1092      * @example
1093      * md.setAlgAndProvider('SHA1withRSA', 'cryptojs/jsrsa');
1094      */
1095     this.setAlgAndProvider = function(alg, prov) {
1096 	this._setAlgNames();
1097 	if (prov != 'cryptojs/jsrsa')
1098 	    throw new Error("provider not supported: " + prov);
1099 
1100 	if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(this.mdAlgName) != -1) {
1101 	    try {
1102 		this.md = new KJUR.crypto.MessageDigest({'alg':this.mdAlgName});
1103 	    } catch (ex) {
1104 		throw new Error("setAlgAndProvider hash alg set fail alg=" +
1105 				this.mdAlgName + "/" + ex);
1106 	    }
1107 	    
1108 	    this.init = function(keyparam, pass) {
1109 		var keyObj = null;
1110 		try {
1111 		    if (pass === undefined) {
1112 			keyObj = KEYUTIL.getKey(keyparam);
1113 		    } else {
1114 			keyObj = KEYUTIL.getKey(keyparam, pass);
1115 		    }
1116 		} catch (ex) {
1117 		    throw "init failed:" + ex;
1118 		}
1119 
1120 		if (keyObj.isPrivate === true) {
1121 		    this.prvKey = keyObj;
1122 		    this.state = "SIGN";
1123 		} else if (keyObj.isPublic === true) {
1124 		    this.pubKey = keyObj;
1125 		    this.state = "VERIFY";
1126 		} else {
1127 		    throw "init failed.:" + keyObj;
1128 		}
1129 	    };
1130 
1131 	    this.updateString = function(str) {
1132 		this.md.updateString(str);
1133 	    };
1134 
1135 	    this.updateHex = function(hex) {
1136 		this.md.updateHex(hex);
1137 	    };
1138 
1139 	    this.sign = function() {
1140 		this.sHashHex = this.md.digest();
1141 		// hex parameter EC public key
1142 		if (this.prvKey === undefined &&
1143 		    this.ecprvhex !== undefined &&
1144 		    this.eccurvename !== undefined &&
1145 		    KJUR.crypto.ECDSA !== undefined) {
1146 		    this.prvKey = new KJUR.crypto.ECDSA({'curve': this.eccurvename,
1147 							 prv: this.ecprvhex});
1148 		}
1149 
1150 		// RSAPSS
1151 		if (this.prvKey instanceof RSAKey &&
1152 		    this.pubkeyAlgName === "rsaandmgf1") {
1153 		    this.hSign = this.prvKey.signWithMessageHashPSS(this.sHashHex,
1154 								    this.mdAlgName,
1155 								    this.pssSaltLen);
1156 		// RSA
1157 		} else if (this.prvKey instanceof RSAKey &&
1158 			   this.pubkeyAlgName === "rsa") {
1159 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex,
1160 								 this.mdAlgName);
1161 		// ECDSA
1162 		} else if (this.prvKey instanceof KJUR.crypto.ECDSA) {
1163 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
1164 		// DSA
1165 		} else if (this.prvKey instanceof KJUR.crypto.DSA) {
1166 		    this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
1167 		} else {
1168 		    throw "Signature: unsupported private key alg: " + this.pubkeyAlgName;
1169 		}
1170 		return this.hSign;
1171 	    };
1172 	    this.signString = function(str) {
1173 		this.updateString(str);
1174 		return this.sign();
1175 	    };
1176 	    this.signHex = function(hex) {
1177 		this.updateHex(hex);
1178 		return this.sign();
1179 	    };
1180 	    this.verify = function(hSigVal) {
1181 	        this.sHashHex = this.md.digest();
1182 		// hex parameter EC public key
1183 		if (this.pubKey === undefined &&
1184 		    this.ecpubhex !== undefined &&
1185 		    this.eccurvename !== undefined &&
1186 		    KJUR.crypto.ECDSA !== undefined) {
1187 		    this.pubKey = new KJUR.crypto.ECDSA({curve: this.eccurvename,
1188 							 pub: this.ecpubhex});
1189 		}
1190 
1191 		// RSAPSS
1192 		if (this.pubKey instanceof RSAKey &&
1193 		    this.pubkeyAlgName === "rsaandmgf1") {
1194 		    return this.pubKey.verifyWithMessageHashPSS(this.sHashHex, hSigVal, 
1195 								this.mdAlgName,
1196 								this.pssSaltLen);
1197 		// RSA
1198 		} else if (this.pubKey instanceof RSAKey &&
1199 			   this.pubkeyAlgName === "rsa") {
1200 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
1201                 // ECDSA
1202 		} else if (KJUR.crypto.ECDSA !== undefined &&
1203 			   this.pubKey instanceof KJUR.crypto.ECDSA) {
1204 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
1205                 // DSA
1206 		} else if (KJUR.crypto.DSA !== undefined &&
1207 			   this.pubKey instanceof KJUR.crypto.DSA) {
1208 		    return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
1209 		} else {
1210 		    throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
1211 		}
1212 	    };
1213 	}
1214     };
1215 
1216     /**
1217      * Initialize this object for signing or verifying depends on key
1218      * @name init
1219      * @memberOf KJUR.crypto.Signature#
1220      * @function
1221      * @param {Object} key specifying public or private key as plain/encrypted PKCS#5/8 PEM file, certificate PEM or {@link RSAKey}, {@link KJUR.crypto.DSA} or {@link KJUR.crypto.ECDSA} object
1222      * @param {String} pass (OPTION) passcode for encrypted private key
1223      * @since crypto 1.1.3
1224      * @description
1225      * This method is very useful initialize method for Signature class since
1226      * you just specify key then this method will automatically initialize it
1227      * using {@link KEYUTIL.getKey} method.
1228      * As for 'key',  following argument type are supported:
1229      * <h5>signing</h5>
1230      * <ul>
1231      * <li>PEM formatted PKCS#8 encrypted RSA/ECDSA private key concluding "BEGIN ENCRYPTED PRIVATE KEY"</li>
1232      * <li>PEM formatted PKCS#5 encrypted RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" and ",ENCRYPTED"</li>
1233      * <li>PEM formatted PKCS#8 plain RSA/ECDSA private key concluding "BEGIN PRIVATE KEY"</li>
1234      * <li>PEM formatted PKCS#5 plain RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" without ",ENCRYPTED"</li>
1235      * <li>RSAKey object of private key</li>
1236      * <li>KJUR.crypto.ECDSA object of private key</li>
1237      * <li>KJUR.crypto.DSA object of private key</li>
1238      * </ul>
1239      * <h5>verification</h5>
1240      * <ul>
1241      * <li>PEM formatted PKCS#8 RSA/EC/DSA public key concluding "BEGIN PUBLIC KEY"</li>
1242      * <li>PEM formatted X.509 certificate with RSA/EC/DSA public key concluding
1243      *     "BEGIN CERTIFICATE", "BEGIN X509 CERTIFICATE" or "BEGIN TRUSTED CERTIFICATE".</li>
1244      * <li>RSAKey object of public key</li>
1245      * <li>KJUR.crypto.ECDSA object of public key</li>
1246      * <li>KJUR.crypto.DSA object of public key</li>
1247      * </ul>
1248      * @example
1249      * sig.init(sCertPEM)
1250      */
1251     this.init = function(key, pass) {
1252 	throw "init(key, pass) not supported for this alg:prov=" +
1253 	      this.algProvName;
1254     };
1255 
1256     /**
1257      * Updates the data to be signed or verified by a string
1258      * @name updateString
1259      * @memberOf KJUR.crypto.Signature#
1260      * @function
1261      * @param {String} str string to use for the update
1262      * @description
1263      * @example
1264      * sig.updateString('aaa')
1265      */
1266     this.updateString = function(str) {
1267 	throw "updateString(str) not supported for this alg:prov=" + this.algProvName;
1268     };
1269 
1270     /**
1271      * Updates the data to be signed or verified by a hexadecimal string
1272      * @name updateHex
1273      * @memberOf KJUR.crypto.Signature#
1274      * @function
1275      * @param {String} hex hexadecimal string to use for the update
1276      * @description
1277      * @example
1278      * sig.updateHex('1f2f3f')
1279      */
1280     this.updateHex = function(hex) {
1281 	throw "updateHex(hex) not supported for this alg:prov=" + this.algProvName;
1282     };
1283 
1284     /**
1285      * Returns the signature bytes of all data updates as a hexadecimal string
1286      * @name sign
1287      * @memberOf KJUR.crypto.Signature#
1288      * @function
1289      * @return the signature bytes as a hexadecimal string
1290      * @description
1291      * @example
1292      * var hSigValue = sig.sign()
1293      */
1294     this.sign = function() {
1295 	throw "sign() not supported for this alg:prov=" + this.algProvName;
1296     };
1297 
1298     /**
1299      * performs final update on the sign using string, then returns the signature bytes of all data updates as a hexadecimal string
1300      * @name signString
1301      * @memberOf KJUR.crypto.Signature#
1302      * @function
1303      * @param {String} str string to final update
1304      * @return the signature bytes of a hexadecimal string
1305      * @description
1306      * @example
1307      * var hSigValue = sig.signString('aaa')
1308      */
1309     this.signString = function(str) {
1310 	throw "digestString(str) not supported for this alg:prov=" + this.algProvName;
1311     };
1312 
1313     /**
1314      * performs final update on the sign using hexadecimal string, then returns the signature bytes of all data updates as a hexadecimal string
1315      * @name signHex
1316      * @memberOf KJUR.crypto.Signature#
1317      * @function
1318      * @param {String} hex hexadecimal string to final update
1319      * @return the signature bytes of a hexadecimal string
1320      * @description
1321      * @example
1322      * var hSigValue = sig.signHex('1fdc33')
1323      */
1324     this.signHex = function(hex) {
1325 	throw "digestHex(hex) not supported for this alg:prov=" + this.algProvName;
1326     };
1327 
1328     /**
1329      * verifies the passed-in signature.
1330      * @name verify
1331      * @memberOf KJUR.crypto.Signature#
1332      * @function
1333      * @param {String} str string to final update
1334      * @return {Boolean} true if the signature was verified, otherwise false
1335      * @description
1336      * @example
1337      * var isValid = sig.verify('1fbcefdca4823a7(snip)')
1338      */
1339     this.verify = function(hSigVal) {
1340 	throw "verify(hSigVal) not supported for this alg:prov=" + this.algProvName;
1341     };
1342 
1343     this.initParams = params;
1344 
1345     if (params !== undefined) {
1346 	if (params.alg !== undefined) {
1347 	    this.algName = params.alg;
1348 	    if (params.prov === undefined) {
1349 		this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
1350 	    } else {
1351 		this.provName = params.prov;
1352 	    }
1353 	    this.algProvName = this.algName + ":" + this.provName;
1354 	    this.setAlgAndProvider(this.algName, this.provName);
1355 	    this._setAlgNames();
1356 	}
1357 
1358 	if (params['psssaltlen'] !== undefined) this.pssSaltLen = params['psssaltlen'];
1359 
1360 	if (params.prvkeypem !== undefined) {
1361 	    if (params.prvkeypas !== undefined) {
1362 		throw "both prvkeypem and prvkeypas parameters not supported";
1363 	    } else {
1364 		try {
1365 		    var prvKey = KEYUTIL.getKey(params.prvkeypem);
1366 		    this.init(prvKey);
1367 		} catch (ex) {
1368 		    throw "fatal error to load pem private key: " + ex;
1369 		}
1370 	    }
1371 	}
1372     }
1373 };
1374 
1375 // ====== Cipher class ============================================================
1376 /**
1377  * Cipher class to encrypt and decrypt data<br/>
1378  * @name KJUR.crypto.Cipher
1379  * @class Cipher class to encrypt and decrypt data<br/>
1380  * @param {Array} params parameters for constructor
1381  * @since jsrsasign 6.2.0 crypto 1.1.10
1382  * @description
1383  * Here is supported canonicalized cipher algorithm names and its standard names:
1384  * <ul>
1385  * <li>RSA - RSA/ECB/PKCS1Padding (default for RSAKey)</li>
1386  * <li>RSAOAEP - RSA/ECB/OAEPWithSHA-1AndMGF1Padding</li>
1387  * <li>RSAOAEP224 - RSA/ECB/OAEPWithSHA-224AndMGF1Padding(*)</li>
1388  * <li>RSAOAEP256 - RSA/ECB/OAEPWithSHA-256AndMGF1Padding</li>
1389  * <li>RSAOAEP384 - RSA/ECB/OAEPWithSHA-384AndMGF1Padding(*)</li>
1390  * <li>RSAOAEP512 - RSA/ECB/OAEPWithSHA-512AndMGF1Padding(*)</li>
1391  * </ul>
1392  * NOTE: (*) is not supported in Java JCE.<br/>
1393  * Currently this class supports only RSA encryption and decryption 
1394  * based on RSAES-OAEP and RSAES-PKCS1-v1_5 scheme. 
1395  * However it is planning to implement also symmetric ciphers near in the future */
1396 KJUR.crypto.Cipher = function(params) {
1397 };
1398 
1399 /**
1400  * encrypt raw string by specified key and algorithm<br/>
1401  * @name encrypt
1402  * @memberOf KJUR.crypto.Cipher
1403  * @function
1404  * @param {String} s input string to encrypt
1405  * @param {Object} keyObj RSAKey object or hexadecimal string of symmetric cipher key
1406  * @param {String} algName short/long algorithm name for encryption/decryption 
1407  * @return {String} hexadecimal encrypted string
1408  * @since jsrsasign 6.2.0 crypto 1.1.10
1409  * @description
1410  * This static method encrypts raw string with specified key and algorithm.
1411  * @example 
1412  * KJUR.crypto.Cipher.encrypt("aaa", pubRSAKeyObj) → "1abc2d..."
1413  * KJUR.crypto.Cipher.encrypt("aaa", pubRSAKeyObj, "RSAOAEP") → "23ab02..."
1414  */
1415 KJUR.crypto.Cipher.encrypt = function(s, keyObj, algName) {
1416     if (keyObj instanceof RSAKey && keyObj.isPublic) {
1417 	var algName2 = KJUR.crypto.Cipher.getAlgByKeyAndName(keyObj, algName);
1418 	if (algName2 === "RSA") return keyObj.encrypt(s);
1419 	if (algName2 === "RSAOAEP") return keyObj.encryptOAEP(s, "sha1");
1420 
1421 	var a = algName2.match(/^RSAOAEP(\d+)$/);
1422 	if (a !== null) return keyObj.encryptOAEP(s, "sha" + a[1]);
1423 
1424 	throw "Cipher.encrypt: unsupported algorithm for RSAKey: " + algName;
1425     } else {
1426 	throw "Cipher.encrypt: unsupported key or algorithm";
1427     }
1428 };
1429 
1430 /**
1431  * decrypt encrypted hexadecimal string with specified key and algorithm<br/>
1432  * @name decrypt
1433  * @memberOf KJUR.crypto.Cipher
1434  * @function
1435  * @param {String} hex hexadecial string of encrypted message
1436  * @param {Object} keyObj RSAKey object or hexadecimal string of symmetric cipher key
1437  * @param {String} algName short/long algorithm name for encryption/decryption
1438  * @return {String} decrypted raw string
1439  * @since jsrsasign 6.2.0 crypto 1.1.10
1440  * @description
1441  * This static method decrypts encrypted hexadecimal string with specified key and algorithm.
1442  * @example 
1443  * KJUR.crypto.Cipher.decrypt("aaa", prvRSAKeyObj) → "1abc2d..."
1444  * KJUR.crypto.Cipher.decrypt("aaa", prvRSAKeyObj, "RSAOAEP) → "23ab02..."
1445  */
1446 KJUR.crypto.Cipher.decrypt = function(hex, keyObj, algName) {
1447     if (keyObj instanceof RSAKey && keyObj.isPrivate) {
1448 	var algName2 = KJUR.crypto.Cipher.getAlgByKeyAndName(keyObj, algName);
1449 	if (algName2 === "RSA") return keyObj.decrypt(hex);
1450 	if (algName2 === "RSAOAEP") return keyObj.decryptOAEP(hex, "sha1");
1451 
1452 	var a = algName2.match(/^RSAOAEP(\d+)$/);
1453 	if (a !== null) return keyObj.decryptOAEP(hex, "sha" + a[1]);
1454 
1455 	throw "Cipher.decrypt: unsupported algorithm for RSAKey: " + algName;
1456     } else {
1457 	throw "Cipher.decrypt: unsupported key or algorithm";
1458     }
1459 };
1460 
1461 /**
1462  * get canonicalized encrypt/decrypt algorithm name by key and short/long algorithm name<br/>
1463  * @name getAlgByKeyAndName
1464  * @memberOf KJUR.crypto.Cipher
1465  * @function
1466  * @param {Object} keyObj RSAKey object or hexadecimal string of symmetric cipher key
1467  * @param {String} algName short/long algorithm name for encryption/decryption
1468  * @return {String} canonicalized algorithm name for encryption/decryption
1469  * @since jsrsasign 6.2.0 crypto 1.1.10
1470  * @description
1471  * Here is supported canonicalized cipher algorithm names and its standard names:
1472  * <ul>
1473  * <li>RSA - RSA/ECB/PKCS1Padding (default for RSAKey)</li>
1474  * <li>RSAOAEP - RSA/ECB/OAEPWithSHA-1AndMGF1Padding</li>
1475  * <li>RSAOAEP224 - RSA/ECB/OAEPWithSHA-224AndMGF1Padding(*)</li>
1476  * <li>RSAOAEP256 - RSA/ECB/OAEPWithSHA-256AndMGF1Padding</li>
1477  * <li>RSAOAEP384 - RSA/ECB/OAEPWithSHA-384AndMGF1Padding(*)</li>
1478  * <li>RSAOAEP512 - RSA/ECB/OAEPWithSHA-512AndMGF1Padding(*)</li>
1479  * </ul>
1480  * NOTE: (*) is not supported in Java JCE.
1481  * @example 
1482  * KJUR.crypto.Cipher.getAlgByKeyAndName(objRSAKey) → "RSA"
1483  * KJUR.crypto.Cipher.getAlgByKeyAndName(objRSAKey, "RSAOAEP") → "RSAOAEP"
1484  */
1485 KJUR.crypto.Cipher.getAlgByKeyAndName = function(keyObj, algName) {
1486     if (keyObj instanceof RSAKey) {
1487 	if (":RSA:RSAOAEP:RSAOAEP224:RSAOAEP256:RSAOAEP384:RSAOAEP512:".indexOf(algName) != -1)
1488 	    return algName;
1489 	if (algName === null || algName === undefined) return "RSA";
1490 	throw "getAlgByKeyAndName: not supported algorithm name for RSAKey: " + algName;
1491     }
1492     throw "getAlgByKeyAndName: not supported algorithm name: " + algName;
1493 }
1494 
1495 // ====== Other Utility class =====================================================
1496 
1497 /**
1498  * static object for cryptographic function utilities
1499  * @name KJUR.crypto.OID
1500  * @class static object for cryptography related OIDs
1501  * @property {Array} oidhex2name key value of hexadecimal OID and its name
1502  *           (ex. '2a8648ce3d030107' and 'secp256r1')
1503  * @since crypto 1.1.3
1504  * @description
1505  */
1506 KJUR.crypto.OID = new function() {
1507     this.oidhex2name = {
1508 	'2a864886f70d010101': 'rsaEncryption',
1509 	'2a8648ce3d0201': 'ecPublicKey',
1510 	'2a8648ce380401': 'dsa',
1511 	'2a8648ce3d030107': 'secp256r1',
1512 	'2b8104001f': 'secp192k1',
1513 	'2b81040021': 'secp224r1',
1514 	'2b8104000a': 'secp256k1',
1515 	'2b81040023': 'secp521r1',
1516 	'2b81040022': 'secp384r1',
1517 	'2a8648ce380403': 'SHA1withDSA', // 1.2.840.10040.4.3
1518 	'608648016503040301': 'SHA224withDSA', // 2.16.840.1.101.3.4.3.1
1519 	'608648016503040302': 'SHA256withDSA', // 2.16.840.1.101.3.4.3.2
1520     };
1521 };
1522