crypto-1.1.js 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*! crypto-1.1.6.js (c) 2013-2015 Kenji Urushima | kjur.github.com/jsrsasign/license
  2. */
  3. /*
  4. * crypto.js - Cryptographic Algorithm Provider class
  5. *
  6. * Copyright (c) 2013-2015 Kenji Urushima (kenji.urushima@gmail.com)
  7. *
  8. * This software is licensed under the terms of the MIT License.
  9. * http://kjur.github.com/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. * @fileOverview
  16. * @name crypto-1.1.js
  17. * @author Kenji Urushima kenji.urushima@gmail.com
  18. * @version 1.1.6 (2015-Jun-07)
  19. * @since jsrsasign 2.2
  20. * @license <a href="http://kjur.github.io/jsrsasign/license/">MIT License</a>
  21. */
  22. /**
  23. * kjur's class library name space
  24. * @name KJUR
  25. * @namespace kjur's class library name space
  26. */
  27. if (typeof KJUR == "undefined" || !KJUR) KJUR = {};
  28. /**
  29. * kjur's cryptographic algorithm provider library name space
  30. * <p>
  31. * This namespace privides following crytpgrahic classes.
  32. * <ul>
  33. * <li>{@link KJUR.crypto.MessageDigest} - Java JCE(cryptograhic extension) style MessageDigest class</li>
  34. * <li>{@link KJUR.crypto.Signature} - Java JCE(cryptograhic extension) style Signature class</li>
  35. * <li>{@link KJUR.crypto.Util} - cryptographic utility functions and properties</li>
  36. * </ul>
  37. * NOTE: Please ignore method summary and document of this namespace. This caused by a bug of jsdoc2.
  38. * </p>
  39. * @name KJUR.crypto
  40. * @namespace
  41. */
  42. if (typeof KJUR.crypto == "undefined" || !KJUR.crypto) KJUR.crypto = {};
  43. /**
  44. * static object for cryptographic function utilities
  45. * @name KJUR.crypto.Util
  46. * @class static object for cryptographic function utilities
  47. * @property {Array} DIGESTINFOHEAD PKCS#1 DigestInfo heading hexadecimal bytes for each hash algorithms
  48. * @property {Array} DEFAULTPROVIDER associative array of default provider name for each hash and signature algorithms
  49. * @description
  50. */
  51. KJUR.crypto.Util = new function() {
  52. this.DIGESTINFOHEAD = {
  53. 'sha1': "3021300906052b0e03021a05000414",
  54. 'sha224': "302d300d06096086480165030402040500041c",
  55. 'sha256': "3031300d060960864801650304020105000420",
  56. 'sha384': "3041300d060960864801650304020205000430",
  57. 'sha512': "3051300d060960864801650304020305000440",
  58. 'md2': "3020300c06082a864886f70d020205000410",
  59. 'md5': "3020300c06082a864886f70d020505000410",
  60. 'ripemd160': "3021300906052b2403020105000414",
  61. };
  62. /**
  63. * @since crypto 1.1.1
  64. */
  65. this.DEFAULTPROVIDER = {
  66. 'md5': 'cryptojs',
  67. 'sha1': 'cryptojs',
  68. 'sha224': 'cryptojs',
  69. 'sha256': 'cryptojs',
  70. 'sha384': 'cryptojs',
  71. 'sha512': 'cryptojs',
  72. 'ripemd160': 'cryptojs',
  73. 'hmacmd5': 'cryptojs',
  74. 'hmacsha1': 'cryptojs',
  75. 'hmacsha224': 'cryptojs',
  76. 'hmacsha256': 'cryptojs',
  77. 'hmacsha384': 'cryptojs',
  78. 'hmacsha512': 'cryptojs',
  79. 'hmacripemd160': 'cryptojs',
  80. 'MD5withRSA': 'cryptojs/jsrsa',
  81. 'SHA1withRSA': 'cryptojs/jsrsa',
  82. 'SHA224withRSA': 'cryptojs/jsrsa',
  83. 'SHA256withRSA': 'cryptojs/jsrsa',
  84. 'SHA384withRSA': 'cryptojs/jsrsa',
  85. 'SHA512withRSA': 'cryptojs/jsrsa',
  86. 'RIPEMD160withRSA': 'cryptojs/jsrsa',
  87. 'MD5withECDSA': 'cryptojs/jsrsa',
  88. 'SHA1withECDSA': 'cryptojs/jsrsa',
  89. 'SHA224withECDSA': 'cryptojs/jsrsa',
  90. 'SHA256withECDSA': 'cryptojs/jsrsa',
  91. 'SHA384withECDSA': 'cryptojs/jsrsa',
  92. 'SHA512withECDSA': 'cryptojs/jsrsa',
  93. 'RIPEMD160withECDSA': 'cryptojs/jsrsa',
  94. 'SHA1withDSA': 'cryptojs/jsrsa',
  95. 'SHA224withDSA': 'cryptojs/jsrsa',
  96. 'SHA256withDSA': 'cryptojs/jsrsa',
  97. 'MD5withRSAandMGF1': 'cryptojs/jsrsa',
  98. 'SHA1withRSAandMGF1': 'cryptojs/jsrsa',
  99. 'SHA224withRSAandMGF1': 'cryptojs/jsrsa',
  100. 'SHA256withRSAandMGF1': 'cryptojs/jsrsa',
  101. 'SHA384withRSAandMGF1': 'cryptojs/jsrsa',
  102. 'SHA512withRSAandMGF1': 'cryptojs/jsrsa',
  103. 'RIPEMD160withRSAandMGF1': 'cryptojs/jsrsa',
  104. };
  105. /**
  106. * @since crypto 1.1.2
  107. */
  108. this.CRYPTOJSMESSAGEDIGESTNAME = {
  109. 'md5': 'CryptoJS.algo.MD5',
  110. 'sha1': 'CryptoJS.algo.SHA1',
  111. 'sha224': 'CryptoJS.algo.SHA224',
  112. 'sha256': 'CryptoJS.algo.SHA256',
  113. 'sha384': 'CryptoJS.algo.SHA384',
  114. 'sha512': 'CryptoJS.algo.SHA512',
  115. 'ripemd160': 'CryptoJS.algo.RIPEMD160'
  116. };
  117. /**
  118. * get hexadecimal DigestInfo
  119. * @name getDigestInfoHex
  120. * @memberOf KJUR.crypto.Util
  121. * @function
  122. * @param {String} hHash hexadecimal hash value
  123. * @param {String} alg hash algorithm name (ex. 'sha1')
  124. * @return {String} hexadecimal string DigestInfo ASN.1 structure
  125. */
  126. this.getDigestInfoHex = function(hHash, alg) {
  127. if (typeof this.DIGESTINFOHEAD[alg] == "undefined")
  128. throw "alg not supported in Util.DIGESTINFOHEAD: " + alg;
  129. return this.DIGESTINFOHEAD[alg] + hHash;
  130. };
  131. /**
  132. * get PKCS#1 padded hexadecimal DigestInfo
  133. * @name getPaddedDigestInfoHex
  134. * @memberOf KJUR.crypto.Util
  135. * @function
  136. * @param {String} hHash hexadecimal hash value of message to be signed
  137. * @param {String} alg hash algorithm name (ex. 'sha1')
  138. * @param {Integer} keySize key bit length (ex. 1024)
  139. * @return {String} hexadecimal string of PKCS#1 padded DigestInfo
  140. */
  141. this.getPaddedDigestInfoHex = function(hHash, alg, keySize) {
  142. var hDigestInfo = this.getDigestInfoHex(hHash, alg);
  143. var pmStrLen = keySize / 4; // minimum PM length
  144. if (hDigestInfo.length + 22 > pmStrLen) // len(0001+ff(*8)+00+hDigestInfo)=22
  145. throw "key is too short for SigAlg: keylen=" + keySize + "," + alg;
  146. var hHead = "0001";
  147. var hTail = "00" + hDigestInfo;
  148. var hMid = "";
  149. var fLen = pmStrLen - hHead.length - hTail.length;
  150. for (var i = 0; i < fLen; i += 2) {
  151. hMid += "ff";
  152. }
  153. var hPaddedMessage = hHead + hMid + hTail;
  154. return hPaddedMessage;
  155. };
  156. /**
  157. * get hexadecimal hash of string with specified algorithm
  158. * @name hashString
  159. * @memberOf KJUR.crypto.Util
  160. * @function
  161. * @param {String} s input string to be hashed
  162. * @param {String} alg hash algorithm name
  163. * @return {String} hexadecimal string of hash value
  164. * @since 1.1.1
  165. */
  166. this.hashString = function(s, alg) {
  167. var md = new KJUR.crypto.MessageDigest({'alg': alg});
  168. return md.digestString(s);
  169. };
  170. /**
  171. * get hexadecimal hash of hexadecimal string with specified algorithm
  172. * @name hashHex
  173. * @memberOf KJUR.crypto.Util
  174. * @function
  175. * @param {String} sHex input hexadecimal string to be hashed
  176. * @param {String} alg hash algorithm name
  177. * @return {String} hexadecimal string of hash value
  178. * @since 1.1.1
  179. */
  180. this.hashHex = function(sHex, alg) {
  181. var md = new KJUR.crypto.MessageDigest({'alg': alg});
  182. return md.digestHex(sHex);
  183. };
  184. /**
  185. * get hexadecimal SHA1 hash of string
  186. * @name sha1
  187. * @memberOf KJUR.crypto.Util
  188. * @function
  189. * @param {String} s input string to be hashed
  190. * @return {String} hexadecimal string of hash value
  191. * @since 1.0.3
  192. */
  193. this.sha1 = function(s) {
  194. var md = new KJUR.crypto.MessageDigest({'alg':'sha1', 'prov':'cryptojs'});
  195. return md.digestString(s);
  196. };
  197. /**
  198. * get hexadecimal SHA256 hash of string
  199. * @name sha256
  200. * @memberOf KJUR.crypto.Util
  201. * @function
  202. * @param {String} s input string to be hashed
  203. * @return {String} hexadecimal string of hash value
  204. * @since 1.0.3
  205. */
  206. this.sha256 = function(s) {
  207. var md = new KJUR.crypto.MessageDigest({'alg':'sha256', 'prov':'cryptojs'});
  208. return md.digestString(s);
  209. };
  210. this.sha256Hex = function(s) {
  211. var md = new KJUR.crypto.MessageDigest({'alg':'sha256', 'prov':'cryptojs'});
  212. return md.digestHex(s);
  213. };
  214. /**
  215. * get hexadecimal SHA512 hash of string
  216. * @name sha512
  217. * @memberOf KJUR.crypto.Util
  218. * @function
  219. * @param {String} s input string to be hashed
  220. * @return {String} hexadecimal string of hash value
  221. * @since 1.0.3
  222. */
  223. this.sha512 = function(s) {
  224. var md = new KJUR.crypto.MessageDigest({'alg':'sha512', 'prov':'cryptojs'});
  225. return md.digestString(s);
  226. };
  227. this.sha512Hex = function(s) {
  228. var md = new KJUR.crypto.MessageDigest({'alg':'sha512', 'prov':'cryptojs'});
  229. return md.digestHex(s);
  230. };
  231. /**
  232. * get hexadecimal MD5 hash of string
  233. * @name md5
  234. * @memberOf KJUR.crypto.Util
  235. * @function
  236. * @param {String} s input string to be hashed
  237. * @return {String} hexadecimal string of hash value
  238. * @since 1.0.3
  239. */
  240. this.md5 = function(s) {
  241. var md = new KJUR.crypto.MessageDigest({'alg':'md5', 'prov':'cryptojs'});
  242. return md.digestString(s);
  243. };
  244. /**
  245. * get hexadecimal RIPEMD160 hash of string
  246. * @name ripemd160
  247. * @memberOf KJUR.crypto.Util
  248. * @function
  249. * @param {String} s input string to be hashed
  250. * @return {String} hexadecimal string of hash value
  251. * @since 1.0.3
  252. */
  253. this.ripemd160 = function(s) {
  254. var md = new KJUR.crypto.MessageDigest({'alg':'ripemd160', 'prov':'cryptojs'});
  255. return md.digestString(s);
  256. };
  257. /**
  258. * @since 1.1.2
  259. */
  260. this.getCryptoJSMDByName = function(s) {
  261. };
  262. };
  263. /**
  264. * MessageDigest class which is very similar to java.security.MessageDigest class
  265. * @name KJUR.crypto.MessageDigest
  266. * @class MessageDigest class which is very similar to java.security.MessageDigest class
  267. * @param {Array} params parameters for constructor
  268. * @description
  269. * <br/>
  270. * Currently this supports following algorithm and providers combination:
  271. * <ul>
  272. * <li>md5 - cryptojs</li>
  273. * <li>sha1 - cryptojs</li>
  274. * <li>sha224 - cryptojs</li>
  275. * <li>sha256 - cryptojs</li>
  276. * <li>sha384 - cryptojs</li>
  277. * <li>sha512 - cryptojs</li>
  278. * <li>ripemd160 - cryptojs</li>
  279. * <li>sha256 - sjcl (NEW from crypto.js 1.0.4)</li>
  280. * </ul>
  281. * @example
  282. * // CryptoJS provider sample
  283. * var md = new KJUR.crypto.MessageDigest({alg: "sha1", prov: "cryptojs"});
  284. * md.updateString('aaa')
  285. * var mdHex = md.digest()
  286. *
  287. * // SJCL(Stanford JavaScript Crypto Library) provider sample
  288. * var md = new KJUR.crypto.MessageDigest({alg: "sha256", prov: "sjcl"}); // sjcl supports sha256 only
  289. * md.updateString('aaa')
  290. * var mdHex = md.digest()
  291. */
  292. KJUR.crypto.MessageDigest = function(params) {
  293. var md = null;
  294. var algName = null;
  295. var provName = null;
  296. /**
  297. * set hash algorithm and provider
  298. * @name setAlgAndProvider
  299. * @memberOf KJUR.crypto.MessageDigest
  300. * @function
  301. * @param {String} alg hash algorithm name
  302. * @param {String} prov provider name
  303. * @description
  304. * @example
  305. * // for SHA1
  306. * md.setAlgAndProvider('sha1', 'cryptojs');
  307. * // for RIPEMD160
  308. * md.setAlgAndProvider('ripemd160', 'cryptojs');
  309. */
  310. this.setAlgAndProvider = function(alg, prov) {
  311. if (alg != null && prov === undefined) prov = KJUR.crypto.Util.DEFAULTPROVIDER[alg];
  312. // for cryptojs
  313. if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(alg) != -1 &&
  314. prov == 'cryptojs') {
  315. try {
  316. this.md = eval(KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME[alg]).create();
  317. } catch (ex) {
  318. throw "setAlgAndProvider hash alg set fail alg=" + alg + "/" + ex;
  319. }
  320. this.updateString = function(str) {
  321. this.md.update(str);
  322. };
  323. this.updateHex = function(hex) {
  324. var wHex = CryptoJS.enc.Hex.parse(hex);
  325. this.md.update(wHex);
  326. };
  327. this.digest = function() {
  328. var hash = this.md.finalize();
  329. return hash.toString(CryptoJS.enc.Hex);
  330. };
  331. this.digestString = function(str) {
  332. this.updateString(str);
  333. return this.digest();
  334. };
  335. this.digestHex = function(hex) {
  336. this.updateHex(hex);
  337. return this.digest();
  338. };
  339. }
  340. if (':sha256:'.indexOf(alg) != -1 &&
  341. prov == 'sjcl') {
  342. try {
  343. this.md = new sjcl.hash.sha256();
  344. } catch (ex) {
  345. throw "setAlgAndProvider hash alg set fail alg=" + alg + "/" + ex;
  346. }
  347. this.updateString = function(str) {
  348. this.md.update(str);
  349. };
  350. this.updateHex = function(hex) {
  351. var baHex = sjcl.codec.hex.toBits(hex);
  352. this.md.update(baHex);
  353. };
  354. this.digest = function() {
  355. var hash = this.md.finalize();
  356. return sjcl.codec.hex.fromBits(hash);
  357. };
  358. this.digestString = function(str) {
  359. this.updateString(str);
  360. return this.digest();
  361. };
  362. this.digestHex = function(hex) {
  363. this.updateHex(hex);
  364. return this.digest();
  365. };
  366. }
  367. };
  368. /**
  369. * update digest by specified string
  370. * @name updateString
  371. * @memberOf KJUR.crypto.MessageDigest
  372. * @function
  373. * @param {String} str string to update
  374. * @description
  375. * @example
  376. * md.updateString('New York');
  377. */
  378. this.updateString = function(str) {
  379. throw "updateString(str) not supported for this alg/prov: " + this.algName + "/" + this.provName;
  380. };
  381. /**
  382. * update digest by specified hexadecimal string
  383. * @name updateHex
  384. * @memberOf KJUR.crypto.MessageDigest
  385. * @function
  386. * @param {String} hex hexadecimal string to update
  387. * @description
  388. * @example
  389. * md.updateHex('0afe36');
  390. */
  391. this.updateHex = function(hex) {
  392. throw "updateHex(hex) not supported for this alg/prov: " + this.algName + "/" + this.provName;
  393. };
  394. /**
  395. * completes hash calculation and returns hash result
  396. * @name digest
  397. * @memberOf KJUR.crypto.MessageDigest
  398. * @function
  399. * @description
  400. * @example
  401. * md.digest()
  402. */
  403. this.digest = function() {
  404. throw "digest() not supported for this alg/prov: " + this.algName + "/" + this.provName;
  405. };
  406. /**
  407. * performs final update on the digest using string, then completes the digest computation
  408. * @name digestString
  409. * @memberOf KJUR.crypto.MessageDigest
  410. * @function
  411. * @param {String} str string to final update
  412. * @description
  413. * @example
  414. * md.digestString('aaa')
  415. */
  416. this.digestString = function(str) {
  417. throw "digestString(str) not supported for this alg/prov: " + this.algName + "/" + this.provName;
  418. };
  419. /**
  420. * performs final update on the digest using hexadecimal string, then completes the digest computation
  421. * @name digestHex
  422. * @memberOf KJUR.crypto.MessageDigest
  423. * @function
  424. * @param {String} hex hexadecimal string to final update
  425. * @description
  426. * @example
  427. * md.digestHex('0f2abd')
  428. */
  429. this.digestHex = function(hex) {
  430. throw "digestHex(hex) not supported for this alg/prov: " + this.algName + "/" + this.provName;
  431. };
  432. if (params !== undefined) {
  433. if (params['alg'] !== undefined) {
  434. this.algName = params['alg'];
  435. if (params['prov'] === undefined)
  436. this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
  437. this.setAlgAndProvider(this.algName, this.provName);
  438. }
  439. }
  440. };
  441. /**
  442. * Mac(Message Authentication Code) class which is very similar to java.security.Mac class
  443. * @name KJUR.crypto.Mac
  444. * @class Mac class which is very similar to java.security.Mac class
  445. * @param {Array} params parameters for constructor
  446. * @description
  447. * <br/>
  448. * Currently this supports following algorithm and providers combination:
  449. * <ul>
  450. * <li>hmacmd5 - cryptojs</li>
  451. * <li>hmacsha1 - cryptojs</li>
  452. * <li>hmacsha224 - cryptojs</li>
  453. * <li>hmacsha256 - cryptojs</li>
  454. * <li>hmacsha384 - cryptojs</li>
  455. * <li>hmacsha512 - cryptojs</li>
  456. * </ul>
  457. * NOTE: HmacSHA224 and HmacSHA384 issue was fixed since jsrsasign 4.1.4.
  458. * Please use 'ext/cryptojs-312-core-fix*.js' instead of 'core.js' of original CryptoJS
  459. * to avoid those issue.
  460. * @example
  461. * var mac = new KJUR.crypto.Mac({alg: "HmacSHA1", prov: "cryptojs", "pass": "pass"});
  462. * mac.updateString('aaa')
  463. * var macHex = md.doFinal()
  464. */
  465. KJUR.crypto.Mac = function(params) {
  466. var mac = null;
  467. var pass = null;
  468. var algName = null;
  469. var provName = null;
  470. var algProv = null;
  471. this.setAlgAndProvider = function(alg, prov) {
  472. if (alg == null) alg = "hmacsha1";
  473. alg = alg.toLowerCase();
  474. if (alg.substr(0, 4) != "hmac") {
  475. throw "setAlgAndProvider unsupported HMAC alg: " + alg;
  476. }
  477. if (prov === undefined) prov = KJUR.crypto.Util.DEFAULTPROVIDER[alg];
  478. this.algProv = alg + "/" + prov;
  479. var hashAlg = alg.substr(4);
  480. // for cryptojs
  481. if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(hashAlg) != -1 &&
  482. prov == 'cryptojs') {
  483. try {
  484. var mdObj = eval(KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME[hashAlg]);
  485. this.mac = CryptoJS.algo.HMAC.create(mdObj, this.pass);
  486. } catch (ex) {
  487. throw "setAlgAndProvider hash alg set fail hashAlg=" + hashAlg + "/" + ex;
  488. }
  489. this.updateString = function(str) {
  490. this.mac.update(str);
  491. };
  492. this.updateHex = function(hex) {
  493. var wHex = CryptoJS.enc.Hex.parse(hex);
  494. this.mac.update(wHex);
  495. };
  496. this.doFinal = function() {
  497. var hash = this.mac.finalize();
  498. return hash.toString(CryptoJS.enc.Hex);
  499. };
  500. this.doFinalString = function(str) {
  501. this.updateString(str);
  502. return this.doFinal();
  503. };
  504. this.doFinalHex = function(hex) {
  505. this.updateHex(hex);
  506. return this.doFinal();
  507. };
  508. }
  509. };
  510. /**
  511. * update digest by specified string
  512. * @name updateString
  513. * @memberOf KJUR.crypto.Mac
  514. * @function
  515. * @param {String} str string to update
  516. * @description
  517. * @example
  518. * md.updateString('New York');
  519. */
  520. this.updateString = function(str) {
  521. throw "updateString(str) not supported for this alg/prov: " + this.algProv;
  522. };
  523. /**
  524. * update digest by specified hexadecimal string
  525. * @name updateHex
  526. * @memberOf KJUR.crypto.Mac
  527. * @function
  528. * @param {String} hex hexadecimal string to update
  529. * @description
  530. * @example
  531. * md.updateHex('0afe36');
  532. */
  533. this.updateHex = function(hex) {
  534. throw "updateHex(hex) not supported for this alg/prov: " + this.algProv;
  535. };
  536. /**
  537. * completes hash calculation and returns hash result
  538. * @name doFinal
  539. * @memberOf KJUR.crypto.Mac
  540. * @function
  541. * @description
  542. * @example
  543. * md.digest()
  544. */
  545. this.doFinal = function() {
  546. throw "digest() not supported for this alg/prov: " + this.algProv;
  547. };
  548. /**
  549. * performs final update on the digest using string, then completes the digest computation
  550. * @name doFinalString
  551. * @memberOf KJUR.crypto.Mac
  552. * @function
  553. * @param {String} str string to final update
  554. * @description
  555. * @example
  556. * md.digestString('aaa')
  557. */
  558. this.doFinalString = function(str) {
  559. throw "digestString(str) not supported for this alg/prov: " + this.algProv;
  560. };
  561. /**
  562. * performs final update on the digest using hexadecimal string,
  563. * then completes the digest computation
  564. * @name doFinalHex
  565. * @memberOf KJUR.crypto.Mac
  566. * @function
  567. * @param {String} hex hexadecimal string to final update
  568. * @description
  569. * @example
  570. * md.digestHex('0f2abd')
  571. */
  572. this.doFinalHex = function(hex) {
  573. throw "digestHex(hex) not supported for this alg/prov: " + this.algProv;
  574. };
  575. if (params !== undefined) {
  576. if (params['pass'] !== undefined) {
  577. this.pass = params['pass'];
  578. }
  579. if (params['alg'] !== undefined) {
  580. this.algName = params['alg'];
  581. if (params['prov'] === undefined)
  582. this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
  583. this.setAlgAndProvider(this.algName, this.provName);
  584. }
  585. }
  586. };
  587. /**
  588. * Signature class which is very similar to java.security.Signature class
  589. * @name KJUR.crypto.Signature
  590. * @class Signature class which is very similar to java.security.Signature class
  591. * @param {Array} params parameters for constructor
  592. * @property {String} state Current state of this signature object whether 'SIGN', 'VERIFY' or null
  593. * @description
  594. * <br/>
  595. * As for params of constructor's argument, it can be specify following attributes:
  596. * <ul>
  597. * <li>alg - signature algorithm name (ex. {MD5,SHA1,SHA224,SHA256,SHA384,SHA512,RIPEMD160}with{RSA,ECDSA,DSA})</li>
  598. * <li>provider - currently 'cryptojs/jsrsa' only</li>
  599. * </ul>
  600. * <h4>SUPPORTED ALGORITHMS AND PROVIDERS</h4>
  601. * This Signature class supports following signature algorithm and provider names:
  602. * <ul>
  603. * <li>MD5withRSA - cryptojs/jsrsa</li>
  604. * <li>SHA1withRSA - cryptojs/jsrsa</li>
  605. * <li>SHA224withRSA - cryptojs/jsrsa</li>
  606. * <li>SHA256withRSA - cryptojs/jsrsa</li>
  607. * <li>SHA384withRSA - cryptojs/jsrsa</li>
  608. * <li>SHA512withRSA - cryptojs/jsrsa</li>
  609. * <li>RIPEMD160withRSA - cryptojs/jsrsa</li>
  610. * <li>MD5withECDSA - cryptojs/jsrsa</li>
  611. * <li>SHA1withECDSA - cryptojs/jsrsa</li>
  612. * <li>SHA224withECDSA - cryptojs/jsrsa</li>
  613. * <li>SHA256withECDSA - cryptojs/jsrsa</li>
  614. * <li>SHA384withECDSA - cryptojs/jsrsa</li>
  615. * <li>SHA512withECDSA - cryptojs/jsrsa</li>
  616. * <li>RIPEMD160withECDSA - cryptojs/jsrsa</li>
  617. * <li>MD5withRSAandMGF1 - cryptojs/jsrsa</li>
  618. * <li>SHA1withRSAandMGF1 - cryptojs/jsrsa</li>
  619. * <li>SHA224withRSAandMGF1 - cryptojs/jsrsa</li>
  620. * <li>SHA256withRSAandMGF1 - cryptojs/jsrsa</li>
  621. * <li>SHA384withRSAandMGF1 - cryptojs/jsrsa</li>
  622. * <li>SHA512withRSAandMGF1 - cryptojs/jsrsa</li>
  623. * <li>RIPEMD160withRSAandMGF1 - cryptojs/jsrsa</li>
  624. * <li>SHA1withDSA - cryptojs/jsrsa</li>
  625. * <li>SHA224withDSA - cryptojs/jsrsa</li>
  626. * <li>SHA256withDSA - cryptojs/jsrsa</li>
  627. * </ul>
  628. * Here are supported elliptic cryptographic curve names and their aliases for ECDSA:
  629. * <ul>
  630. * <li>secp256k1</li>
  631. * <li>secp256r1, NIST P-256, P-256, prime256v1</li>
  632. * <li>secp384r1, NIST P-384, P-384</li>
  633. * </ul>
  634. * NOTE1: DSA signing algorithm is also supported since crypto 1.1.5.
  635. * <h4>EXAMPLES</h4>
  636. * @example
  637. * // RSA signature generation
  638. * var sig = new KJUR.crypto.Signature({"alg": "SHA1withRSA"});
  639. * sig.init(prvKeyPEM);
  640. * sig.updateString('aaa');
  641. * var hSigVal = sig.sign();
  642. *
  643. * // DSA signature validation
  644. * var sig2 = new KJUR.crypto.Signature({"alg": "SHA1withDSA"});
  645. * sig2.init(certPEM);
  646. * sig.updateString('aaa');
  647. * var isValid = sig2.verify(hSigVal);
  648. *
  649. * // ECDSA signing
  650. * var sig = new KJUR.crypto.Signature({'alg':'SHA1withECDSA'});
  651. * sig.init(prvKeyPEM);
  652. * sig.updateString('aaa');
  653. * var sigValueHex = sig.sign();
  654. *
  655. * // ECDSA verifying
  656. * var sig2 = new KJUR.crypto.Signature({'alg':'SHA1withECDSA'});
  657. * sig.init(certPEM);
  658. * sig.updateString('aaa');
  659. * var isValid = sig.verify(sigValueHex);
  660. */
  661. KJUR.crypto.Signature = function(params) {
  662. var prvKey = null; // RSAKey/KJUR.crypto.{ECDSA,DSA} object for signing
  663. var pubKey = null; // RSAKey/KJUR.crypto.{ECDSA,DSA} object for verifying
  664. var md = null; // KJUR.crypto.MessageDigest object
  665. var sig = null;
  666. var algName = null;
  667. var provName = null;
  668. var algProvName = null;
  669. var mdAlgName = null;
  670. var pubkeyAlgName = null; // rsa,ecdsa,rsaandmgf1(=rsapss)
  671. var state = null;
  672. var pssSaltLen = -1;
  673. var initParams = null;
  674. var sHashHex = null; // hex hash value for hex
  675. var hDigestInfo = null;
  676. var hPaddedDigestInfo = null;
  677. var hSign = null;
  678. this._setAlgNames = function() {
  679. if (this.algName.match(/^(.+)with(.+)$/)) {
  680. this.mdAlgName = RegExp.$1.toLowerCase();
  681. this.pubkeyAlgName = RegExp.$2.toLowerCase();
  682. }
  683. };
  684. this._zeroPaddingOfSignature = function(hex, bitLength) {
  685. var s = "";
  686. var nZero = bitLength / 4 - hex.length;
  687. for (var i = 0; i < nZero; i++) {
  688. s = s + "0";
  689. }
  690. return s + hex;
  691. };
  692. /**
  693. * set signature algorithm and provider
  694. * @name setAlgAndProvider
  695. * @memberOf KJUR.crypto.Signature
  696. * @function
  697. * @param {String} alg signature algorithm name
  698. * @param {String} prov provider name
  699. * @description
  700. * @example
  701. * md.setAlgAndProvider('SHA1withRSA', 'cryptojs/jsrsa');
  702. */
  703. this.setAlgAndProvider = function(alg, prov) {
  704. this._setAlgNames();
  705. if (prov != 'cryptojs/jsrsa')
  706. throw "provider not supported: " + prov;
  707. if (':md5:sha1:sha224:sha256:sha384:sha512:ripemd160:'.indexOf(this.mdAlgName) != -1) {
  708. try {
  709. this.md = new KJUR.crypto.MessageDigest({'alg':this.mdAlgName});
  710. } catch (ex) {
  711. throw "setAlgAndProvider hash alg set fail alg=" +
  712. this.mdAlgName + "/" + ex;
  713. }
  714. this.init = function(keyparam, pass) {
  715. var keyObj = null;
  716. try {
  717. if (pass === undefined) {
  718. keyObj = KEYUTIL.getKey(keyparam);
  719. } else {
  720. keyObj = KEYUTIL.getKey(keyparam, pass);
  721. }
  722. } catch (ex) {
  723. throw "init failed:" + ex;
  724. }
  725. if (keyObj.isPrivate === true) {
  726. this.prvKey = keyObj;
  727. this.state = "SIGN";
  728. } else if (keyObj.isPublic === true) {
  729. this.pubKey = keyObj;
  730. this.state = "VERIFY";
  731. } else {
  732. throw "init failed.:" + keyObj;
  733. }
  734. };
  735. this.initSign = function(params) {
  736. if (typeof params['ecprvhex'] == 'string' &&
  737. typeof params['eccurvename'] == 'string') {
  738. this.ecprvhex = params['ecprvhex'];
  739. this.eccurvename = params['eccurvename'];
  740. } else {
  741. this.prvKey = params;
  742. }
  743. this.state = "SIGN";
  744. };
  745. this.initVerifyByPublicKey = function(params) {
  746. if (typeof params['ecpubhex'] == 'string' &&
  747. typeof params['eccurvename'] == 'string') {
  748. this.ecpubhex = params['ecpubhex'];
  749. this.eccurvename = params['eccurvename'];
  750. } else if (params instanceof KJUR.crypto.ECDSA) {
  751. this.pubKey = params;
  752. } else if (params instanceof RSAKey) {
  753. this.pubKey = params;
  754. }
  755. this.state = "VERIFY";
  756. };
  757. this.initVerifyByCertificatePEM = function(certPEM) {
  758. var x509 = new X509();
  759. x509.readCertPEM(certPEM);
  760. this.pubKey = x509.subjectPublicKeyRSA;
  761. this.state = "VERIFY";
  762. };
  763. this.updateString = function(str) {
  764. this.md.updateString(str);
  765. };
  766. this.updateHex = function(hex) {
  767. this.md.updateHex(hex);
  768. };
  769. this.sign = function() {
  770. this.sHashHex = this.md.digest();
  771. if (typeof this.ecprvhex != "undefined" &&
  772. typeof this.eccurvename != "undefined") {
  773. var ec = new KJUR.crypto.ECDSA({'curve': this.eccurvename});
  774. this.hSign = ec.signHex(this.sHashHex, this.ecprvhex);
  775. } else if (this.prvKey instanceof RSAKey &&
  776. this.pubkeyAlgName == "rsaandmgf1") {
  777. this.hSign = this.prvKey.signWithMessageHashPSS(this.sHashHex,
  778. this.mdAlgName,
  779. this.pssSaltLen);
  780. } else if (this.prvKey instanceof RSAKey &&
  781. this.pubkeyAlgName == "rsa") {
  782. this.hSign = this.prvKey.signWithMessageHash(this.sHashHex,
  783. this.mdAlgName);
  784. } else if (this.prvKey instanceof KJUR.crypto.ECDSA) {
  785. this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
  786. } else if (this.prvKey instanceof KJUR.crypto.DSA) {
  787. this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
  788. } else {
  789. throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
  790. }
  791. return this.hSign;
  792. };
  793. this.signString = function(str) {
  794. this.updateString(str);
  795. return this.sign();
  796. };
  797. this.signHex = function(hex) {
  798. this.updateHex(hex);
  799. return this.sign();
  800. };
  801. this.verify = function(hSigVal) {
  802. this.sHashHex = this.md.digest();
  803. if (typeof this.ecpubhex != "undefined" &&
  804. typeof this.eccurvename != "undefined") {
  805. var ec = new KJUR.crypto.ECDSA({curve: this.eccurvename});
  806. return ec.verifyHex(this.sHashHex, hSigVal, this.ecpubhex);
  807. } else if (this.pubKey instanceof RSAKey &&
  808. this.pubkeyAlgName == "rsaandmgf1") {
  809. return this.pubKey.verifyWithMessageHashPSS(this.sHashHex, hSigVal,
  810. this.mdAlgName,
  811. this.pssSaltLen);
  812. } else if (this.pubKey instanceof RSAKey &&
  813. this.pubkeyAlgName == "rsa") {
  814. return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
  815. } else if (this.pubKey instanceof KJUR.crypto.ECDSA) {
  816. return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
  817. } else if (this.pubKey instanceof KJUR.crypto.DSA) {
  818. return this.pubKey.verifyWithMessageHash(this.sHashHex, hSigVal);
  819. } else {
  820. throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
  821. }
  822. };
  823. }
  824. };
  825. /**
  826. * Initialize this object for signing or verifying depends on key
  827. * @name init
  828. * @memberOf KJUR.crypto.Signature
  829. * @function
  830. * @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
  831. * @param {String} pass (OPTION) passcode for encrypted private key
  832. * @since crypto 1.1.3
  833. * @description
  834. * This method is very useful initialize method for Signature class since
  835. * you just specify key then this method will automatically initialize it
  836. * using {@link KEYUTIL.getKey} method.
  837. * As for 'key', following argument type are supported:
  838. * <h5>signing</h5>
  839. * <ul>
  840. * <li>PEM formatted PKCS#8 encrypted RSA/ECDSA private key concluding "BEGIN ENCRYPTED PRIVATE KEY"</li>
  841. * <li>PEM formatted PKCS#5 encrypted RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" and ",ENCRYPTED"</li>
  842. * <li>PEM formatted PKCS#8 plain RSA/ECDSA private key concluding "BEGIN PRIVATE KEY"</li>
  843. * <li>PEM formatted PKCS#5 plain RSA/DSA private key concluding "BEGIN RSA/DSA PRIVATE KEY" without ",ENCRYPTED"</li>
  844. * <li>RSAKey object of private key</li>
  845. * <li>KJUR.crypto.ECDSA object of private key</li>
  846. * <li>KJUR.crypto.DSA object of private key</li>
  847. * </ul>
  848. * <h5>verification</h5>
  849. * <ul>
  850. * <li>PEM formatted PKCS#8 RSA/EC/DSA public key concluding "BEGIN PUBLIC KEY"</li>
  851. * <li>PEM formatted X.509 certificate with RSA/EC/DSA public key concluding
  852. * "BEGIN CERTIFICATE", "BEGIN X509 CERTIFICATE" or "BEGIN TRUSTED CERTIFICATE".</li>
  853. * <li>RSAKey object of public key</li>
  854. * <li>KJUR.crypto.ECDSA object of public key</li>
  855. * <li>KJUR.crypto.DSA object of public key</li>
  856. * </ul>
  857. * @example
  858. * sig.init(sCertPEM)
  859. */
  860. this.init = function(key, pass) {
  861. throw "init(key, pass) not supported for this alg:prov=" +
  862. this.algProvName;
  863. };
  864. /**
  865. * Initialize this object for verifying with a public key
  866. * @name initVerifyByPublicKey
  867. * @memberOf KJUR.crypto.Signature
  868. * @function
  869. * @param {Object} param RSAKey object of public key or associative array for ECDSA
  870. * @since 1.0.2
  871. * @deprecated from crypto 1.1.5. please use init() method instead.
  872. * @description
  873. * Public key information will be provided as 'param' parameter and the value will be
  874. * following:
  875. * <ul>
  876. * <li>{@link RSAKey} object for RSA verification</li>
  877. * <li>associative array for ECDSA verification
  878. * (ex. <code>{'ecpubhex': '041f..', 'eccurvename': 'secp256r1'}</code>)
  879. * </li>
  880. * </ul>
  881. * @example
  882. * sig.initVerifyByPublicKey(rsaPrvKey)
  883. */
  884. this.initVerifyByPublicKey = function(rsaPubKey) {
  885. throw "initVerifyByPublicKey(rsaPubKeyy) not supported for this alg:prov=" +
  886. this.algProvName;
  887. };
  888. /**
  889. * Initialize this object for verifying with a certficate
  890. * @name initVerifyByCertificatePEM
  891. * @memberOf KJUR.crypto.Signature
  892. * @function
  893. * @param {String} certPEM PEM formatted string of certificate
  894. * @since 1.0.2
  895. * @deprecated from crypto 1.1.5. please use init() method instead.
  896. * @description
  897. * @example
  898. * sig.initVerifyByCertificatePEM(certPEM)
  899. */
  900. this.initVerifyByCertificatePEM = function(certPEM) {
  901. throw "initVerifyByCertificatePEM(certPEM) not supported for this alg:prov=" +
  902. this.algProvName;
  903. };
  904. /**
  905. * Initialize this object for signing
  906. * @name initSign
  907. * @memberOf KJUR.crypto.Signature
  908. * @function
  909. * @param {Object} param RSAKey object of public key or associative array for ECDSA
  910. * @deprecated from crypto 1.1.5. please use init() method instead.
  911. * @description
  912. * Private key information will be provided as 'param' parameter and the value will be
  913. * following:
  914. * <ul>
  915. * <li>{@link RSAKey} object for RSA signing</li>
  916. * <li>associative array for ECDSA signing
  917. * (ex. <code>{'ecprvhex': '1d3f..', 'eccurvename': 'secp256r1'}</code>)</li>
  918. * </ul>
  919. * @example
  920. * sig.initSign(prvKey)
  921. */
  922. this.initSign = function(prvKey) {
  923. throw "initSign(prvKey) not supported for this alg:prov=" + this.algProvName;
  924. };
  925. /**
  926. * Updates the data to be signed or verified by a string
  927. * @name updateString
  928. * @memberOf KJUR.crypto.Signature
  929. * @function
  930. * @param {String} str string to use for the update
  931. * @description
  932. * @example
  933. * sig.updateString('aaa')
  934. */
  935. this.updateString = function(str) {
  936. throw "updateString(str) not supported for this alg:prov=" + this.algProvName;
  937. };
  938. /**
  939. * Updates the data to be signed or verified by a hexadecimal string
  940. * @name updateHex
  941. * @memberOf KJUR.crypto.Signature
  942. * @function
  943. * @param {String} hex hexadecimal string to use for the update
  944. * @description
  945. * @example
  946. * sig.updateHex('1f2f3f')
  947. */
  948. this.updateHex = function(hex) {
  949. throw "updateHex(hex) not supported for this alg:prov=" + this.algProvName;
  950. };
  951. /**
  952. * Returns the signature bytes of all data updates as a hexadecimal string
  953. * @name sign
  954. * @memberOf KJUR.crypto.Signature
  955. * @function
  956. * @return the signature bytes as a hexadecimal string
  957. * @description
  958. * @example
  959. * var hSigValue = sig.sign()
  960. */
  961. this.sign = function() {
  962. throw "sign() not supported for this alg:prov=" + this.algProvName;
  963. };
  964. /**
  965. * performs final update on the sign using string, then returns the signature bytes of all data updates as a hexadecimal string
  966. * @name signString
  967. * @memberOf KJUR.crypto.Signature
  968. * @function
  969. * @param {String} str string to final update
  970. * @return the signature bytes of a hexadecimal string
  971. * @description
  972. * @example
  973. * var hSigValue = sig.signString('aaa')
  974. */
  975. this.signString = function(str) {
  976. throw "digestString(str) not supported for this alg:prov=" + this.algProvName;
  977. };
  978. /**
  979. * performs final update on the sign using hexadecimal string, then returns the signature bytes of all data updates as a hexadecimal string
  980. * @name signHex
  981. * @memberOf KJUR.crypto.Signature
  982. * @function
  983. * @param {String} hex hexadecimal string to final update
  984. * @return the signature bytes of a hexadecimal string
  985. * @description
  986. * @example
  987. * var hSigValue = sig.signHex('1fdc33')
  988. */
  989. this.signHex = function(hex) {
  990. throw "digestHex(hex) not supported for this alg:prov=" + this.algProvName;
  991. };
  992. /**
  993. * verifies the passed-in signature.
  994. * @name verify
  995. * @memberOf KJUR.crypto.Signature
  996. * @function
  997. * @param {String} str string to final update
  998. * @return {Boolean} true if the signature was verified, otherwise false
  999. * @description
  1000. * @example
  1001. * var isValid = sig.verify('1fbcefdca4823a7(snip)')
  1002. */
  1003. this.verify = function(hSigVal) {
  1004. throw "verify(hSigVal) not supported for this alg:prov=" + this.algProvName;
  1005. };
  1006. this.initParams = params;
  1007. if (params !== undefined) {
  1008. if (params['alg'] !== undefined) {
  1009. this.algName = params['alg'];
  1010. if (params['prov'] === undefined) {
  1011. this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName];
  1012. } else {
  1013. this.provName = params['prov'];
  1014. }
  1015. this.algProvName = this.algName + ":" + this.provName;
  1016. this.setAlgAndProvider(this.algName, this.provName);
  1017. this._setAlgNames();
  1018. }
  1019. if (params['psssaltlen'] !== undefined) this.pssSaltLen = params['psssaltlen'];
  1020. if (params['prvkeypem'] !== undefined) {
  1021. if (params['prvkeypas'] !== undefined) {
  1022. throw "both prvkeypem and prvkeypas parameters not supported";
  1023. } else {
  1024. try {
  1025. var prvKey = new RSAKey();
  1026. prvKey.readPrivateKeyFromPEMString(params['prvkeypem']);
  1027. this.initSign(prvKey);
  1028. } catch (ex) {
  1029. throw "fatal error to load pem private key: " + ex;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. };
  1035. /**
  1036. * static object for cryptographic function utilities
  1037. * @name KJUR.crypto.OID
  1038. * @class static object for cryptography related OIDs
  1039. * @property {Array} oidhex2name key value of hexadecimal OID and its name
  1040. * (ex. '2a8648ce3d030107' and 'secp256r1')
  1041. * @since crypto 1.1.3
  1042. * @description
  1043. */
  1044. KJUR.crypto.OID = new function() {
  1045. this.oidhex2name = {
  1046. '2a864886f70d010101': 'rsaEncryption',
  1047. '2a8648ce3d0201': 'ecPublicKey',
  1048. '2a8648ce380401': 'dsa',
  1049. '2a8648ce3d030107': 'secp256r1',
  1050. '2b8104001f': 'secp192k1',
  1051. '2b81040021': 'secp224r1',
  1052. '2b8104000a': 'secp256k1',
  1053. '2b81040023': 'secp521r1',
  1054. '2b81040022': 'secp384r1',
  1055. '2a8648ce380403': 'SHA1withDSA', // 1.2.840.10040.4.3
  1056. '608648016503040301': 'SHA224withDSA', // 2.16.840.1.101.3.4.3.1
  1057. '608648016503040302': 'SHA256withDSA', // 2.16.840.1.101.3.4.3.2
  1058. };
  1059. };