2
0

index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. var CryptographicPolicy = require('./policy');
  11. var options = require('./options');
  12. var validator = require('./input-validator');
  13. module.exports = {
  14. /**
  15. * Creates a new CryptographicPolicy object, which encapsulates a
  16. * cryptographic policy.
  17. *
  18. * <p>
  19. * By default, this object is created from the default cryptographic policy.
  20. * A customized policy may be provided instead, with the requirement that it
  21. * only uses options defined in {@link options}.
  22. *
  23. * @function newInstance
  24. * @global
  25. * @param {Object}
  26. * [policy=Default policy] The cryptographic policy to use, as a
  27. * JSON object.
  28. * @returns {CryptographicPolicy} The new CryptographicPolicy object.
  29. * @throws {Error} If the input data validation fails.
  30. */
  31. newInstance: function(policy) {
  32. checkData(policy);
  33. return new CryptographicPolicy(policy);
  34. },
  35. /**
  36. * The cryptographic policy options.
  37. *
  38. * @name options
  39. * @global
  40. *
  41. * @property {object} primitives.securerandom - Policy for generating
  42. * message digests.
  43. * @property {object} primitives.messageDigest.algorithm - Hash algorithm.
  44. * @property {string} primitives.messageDigest.algorithm.SHA256 - 'SHA256'
  45. * @property {string} primitives.messageDigest.algorithm.SHA512_224
  46. * -'SHA512/224'
  47. *
  48. * @property {object} primitives.keyDerivation - Policy for deriving keys.
  49. * @property {object} primitives.keyDerivation.pbkdf - Password-based key
  50. * derivation function (PBKDF)
  51. * @property {object} primitives.keyDerivation.pbkdf.keyLengthBytes - Key
  52. * length, in bytes.
  53. * @property {number} primitives.keyDerivation.pbkdf.keyLengthBytes.KL_16 -
  54. * 16
  55. * @property {number} primitives.keyDerivation.pbkdf.keyLengthBytes.KL_32 -
  56. * 32
  57. * @property {object} primitives.keyDerivation.pbkdf.minSaltLengthBytes -
  58. * Minimum salt length, in bytes.
  59. * @property {number}
  60. * primitives.keyDerivation.pbkdf.minSaltLengthBytes.SL_20 - 20.
  61. * @property {number}
  62. * primitives.keyDerivation.pbkdf.minSaltLengthBytes.SL_32 - 32
  63. * @property {object} primitives.keyDerivation.pbkdf.hashAlgorithm - Hash
  64. * algorithm.
  65. * @property {string} primitives.keyDerivation.pbkdf.hashAlgorithm.SHA256 -
  66. * 'SHA256'
  67. * @property {string}
  68. * primitives.keyDerivation.pbkdf.hashAlgorithm.SHA512_224 -
  69. * 'SHA512/224'
  70. * @property {object} primitives.keyDerivation.pbkdf.numIterations - Number
  71. * of iterations.
  72. * @property {number} primitives.keyDerivation.pbkdf.I_1 - 1
  73. * @property {number} primitives.keyDerivation.pbkdf.I_8000 - 8000
  74. * @property {number} primitives.keyDerivation.pbkdf.I_16000 - 16000
  75. * @property {number} primitives.keyDerivation.pbkdf.I_32000 - 32000
  76. * @property {number} primitives.keyDerivation.pbkdf.I_64000 - 64000
  77. *
  78. * @property {object} symmetric.secretkey - Policy for generating secret
  79. * keys.
  80. * @property {object} symmetric.secretkey.encryption - Secret key for
  81. * encryption.
  82. * @property {object} symmetric.secretKey.encryption.lengthBytes - Key
  83. * length, in bytes.
  84. * @property {number} symmetric.secretKey.encryption.lengthBytes.KL_16 - 16
  85. * @property {number} symmetric.secretKey.encryption.lengthBytes.KL_32 - 32
  86. * @property {object} symmetric.secretkey.mac - Secret key for MAC
  87. * generation.
  88. * @property {object} symmetric.secretKey.mac.lengthBytes - Key length, in
  89. * bytes.
  90. * @property {number} symmetric.secretKey.mac.lengthBytes.KL_16 - 16
  91. * @property {number} symmetric.secretKey.mac.lengthBytes.KL_32 - 32
  92. *
  93. * @property {object} symmetric.cipher - Policy for symmetrically encrypting
  94. * and decrypting data.
  95. * @property {object} symmetric.cipher.algorithm - Cipher algorithm.
  96. * @property {object} symmetric.cipher.algorithm.AES_GCM - AES-GCM
  97. * algorithm.
  98. * @property {string} symmetric.cipher.algorithm.AES_GCM.name - 'AES-GCM'
  99. * @property {object} symmetric.cipher.algorithm.AES_GCM.keyLengthBytes -
  100. * Key length, in bytes.
  101. * @property {number}
  102. * symmetric.cipher.algorithm.AES_GCM.keyLengthBytes.KL_16 - 16
  103. * @property {number}
  104. * symmetric.cipher.algorithm.AES_GCM.keyLengthBytes.KL_32 - 32
  105. * @property {object} symmetric.cipher.algorithm.AES_GCM.tagLengthBytes -
  106. * Tag length, in bytes.
  107. * @property {number}
  108. * symmetric.cipher.algorithm.AES_GCM.tagLengthBytes.TL_16 - 16
  109. * @property {number}
  110. * symmetric.cipher.algorithm.AES_GCM.tagLengthBytes.TL_32 - 32
  111. * @property {object} symmetric.cipher.ivLengthBytes - Initialization vector
  112. * length, in bytes.
  113. * @property {number} symmetric.cipher.ivLengthBytes.IVL_12 - 12
  114. * @property {number} symmetric.cipher.ivLengthBytes.IVL_16 - 16
  115. * @property {number} symmetric.cipher.ivLengthBytes.IVL_32 - 32
  116. * @property {number} symmetric.cipher.ivLengthBytes.IVL_64 - 64
  117. *
  118. * @property {object} symmetric.mac - Policy for generating MAC's.
  119. * @property {object} symmetric.mac.hashAlgorithm - Hash algorithm.
  120. * @property {string} symmetric.mac.hashAlgorithm.SHA256 = 'SHA256'
  121. * @property {string} symmetric.mac.hashAlgorithm.SHA512_224 = 'SHA512/224'
  122. *
  123. * @property {object} asymmetric.keyPair - Policy for generating key pairs.
  124. * @property {object} asymmetric.keyPair.encryption - Encryption key pair.
  125. * @property {object} asymmetric.keyPair.encryption.algorithm - Key pair
  126. * generation algorithm.
  127. * @property {string} asymmetric.keyPair.encryption.algorithm.RSA - 'RSA'.
  128. * @property {object} asymmetric.keyPair.encryption.keyLengthBits - Key
  129. * length, in bits.
  130. * @property {number} asymmetric.keyPair.encryption.keyLengthBits.KL_2048 -
  131. * 2048
  132. * @property {number} asymmetric.keyPair.encryption.keyLengthBits.KL_3072 -
  133. * 3072
  134. * @property {number} asymmetric.keyPair.encryption.keyLengthBits.KL_4096 -
  135. * 4096
  136. * @property {object} asymmetric.keyPair.encryption.publicExponent - Public
  137. * exponent.
  138. * @property {number} asymmetric.keyPair.encryption.publicExponent.F4 -
  139. * 65537
  140. *
  141. * @property {object} asymmetric.signer - Policy for signing data and
  142. * verifying signatures.
  143. * @property {object} asymmetric.signer.algorithm - Signer algorithm.
  144. * @property {string} asymmetric.signer.algorithm.RSA - 'RSA'
  145. * @property {object} asymmetric.signer.hashAlgorithm - Hash algorithm.
  146. * @property {string} asymmetric.signer.hashAlgorithm.SHA256 - SHA256'.
  147. * @property {string} asymmetric.signer.hashAlgorithm.SHA256_224 -
  148. * 'SHA512/224'
  149. * @property {object} asymmetric.signer.padding - Padding.
  150. * @property {object} asymmetric.signer.padding.PSS - PSS padding.
  151. * @property {string} asymmetric.signer.padding.PSS.name - 'PSS'
  152. * @property {object} asymmetric.signer.padding.PSS.hashAlgorithm - Hash
  153. * algorithm.
  154. * @property {string} asymmetric.signer.padding.hashAlgorithm.SHA256 -
  155. * 'SHA256'
  156. * @property {string} asymmetric.signer.padding.hashAlgorithm.SHA256_224 -
  157. * 'SHA512/224'
  158. * @property {object} asymmetric.signer.padding.PSS.saltLengthBytes - Salt
  159. * length, in bytes.
  160. * @property {number} asymmetric.signer.padding.PSS.saltLengthBytes.SL_32 -
  161. * 32
  162. * @property {number} asymmetric.signer.padding.PSS.saltLengthBytes.SL_64 -
  163. * 64
  164. * @property {object} asymmetric.signer.padding.PSS.maskGenerator - Mask
  165. * generation function.
  166. * @property {object} asymmetric.signer.padding.PSS.maskGenerator.MGF1 -
  167. * MGF1 mask generation algorithm.
  168. * @property {string} asymmetric.signer.padding.PSS.maskGenerator.MGF1.name -
  169. * 'MGF1'
  170. * @property {object}
  171. * asymmetric.signer.padding.PSS.maskGenerator.MGF1.hashAlgorithm -
  172. * Hash algorithm.
  173. * @property {string}
  174. * asymmetric.signer.padding.PSS.maskGenerator.MGF1.hashAlgorithm.SHA256
  175. * -'SHA256'
  176. * @property {string}
  177. * asymmetric.signer.padding.PSS.maskGenerator.MGF1.hashAlgorithm.SHA256_224
  178. * - 'SHA512/224'
  179. * @property {object} asymmetric.signer.publicExponent - Public exponent.
  180. * @property {number} asymmetric.signer.publicExponent.F4 - 65537
  181. *
  182. * @property {object} asymmetric.cipher - Policy for asymmetrically
  183. * encrypting and decrypting data.
  184. * @property {object} asymmetric.cipher.algorithm - Cipher algorithm.
  185. * @property {object} asymmetric.cipher.algorithm.RSA_OAEP - RSA-OAEP
  186. * algorithm.
  187. * @property {string} asymmetric.cipher.algorithm.RSA_OAEP.name. -
  188. * 'RSA-OAEP'.
  189. * @property {object} asymmetric.cipher.algorithm.RSA_OAEP.hashAlgorithm -
  190. * Hash algorithm.
  191. * @property {string}
  192. * asymmetric.cipher.algorithm.RSA_OAEP.hashAlgorithm.SHA256 -
  193. * 'SHA-256'
  194. * @property {string}
  195. * asymmetric.cipher.algorithm.RSA_OAEP.hashAlgorithm.SHA512_224 -
  196. * 'SHA512/224'
  197. * @property {object} asymmetric.cipher.algorithm.RSA_OAEP.maskGenerator -
  198. * Mask generation function.
  199. * @property {object}
  200. * asymmetric.cipher.algorithm.RSA_OAEP.maskGenerator.MGF1 - MGF1
  201. * mask generation function.
  202. * @property {string}
  203. * asymmetric.cipher.algorithm.RSA_OAEP.maskGenerator.MGF1.name -
  204. * 'MGF1'
  205. * @property {object}
  206. * asymmetric.cipher.algorithm.RSA_OAEP.maskGenerator.MGF1.hashAlgorithm
  207. * - Hash algorithm.
  208. * @property {string}
  209. * asymmetric.cipher.algorithm.RSA_OAEP.maskGenerator.MGF1.hashAlgorithm.SHA256
  210. * - 'SHA256'
  211. * @property {string}
  212. * asymmetric.cipher.algorithm.RSA_OAEP.maskGenerator.MGF1.hashAlgorithm.SHA512_224
  213. * - 'SHA512/224'
  214. * @property {object} asymmetric.cipher.algorithm.RSA_KEM - RSA-KEM
  215. * algorithm.
  216. * @property {string} asymmetric.cipher.algorithm.RSA_KEM.name - 'RSA-KEM'
  217. * @property {object} asymmetric.cipher.algorithm.RSA_KEM.hashAlgorithm -
  218. * Hash algorithm.
  219. * @property {string}
  220. * asymmetric.cipher.algorithm.RSA_KEM.hashAlgorithm.SHA256 -
  221. * 'SHA256'
  222. * @property {string}
  223. * asymmetric.cipher.algorithm.RSA_KEM.hashAlgorithm.SHA512_224 -
  224. * 'SHA512/224'
  225. * @property {object}
  226. * asymmetric.cipher.algorithm.RSA_KEM.secretKeyLengthBytes -
  227. * Secret key length, in bytes.
  228. * @property {number}
  229. * asymmetric.cipher.algorithm.RSA_KEM.secretKeyLengthBytes.KL_16 -
  230. * 16
  231. * @property {number}
  232. * asymmetric.cipher.algorithm.RSA_KEM.secretKeyLengthBytes.KL_24 -
  233. * 24
  234. * @property {number}
  235. * asymmetric.cipher.algorithm.RSA_KEM.secretKeyLengthBytes.KL_32 -
  236. * 32
  237. * @property {object} asymmetric.cipher.algorithm.RSA_KEM.ivLengthBytes -
  238. * Initialization vector length, in bytes.
  239. * @property {number}
  240. * asymmetric.cipher.algorithm.RSA_KEM.ivLengthBytes.IVL_12 - 12
  241. * @property {number}
  242. * asymmetric.cipher.algorithm.RSA_KEM.ivLengthBytes.IVL_16 - 16
  243. * @property {number}
  244. * asymmetric.cipher.algorithm.RSA_KEM.ivLengthBytes.IVL_32 - 32
  245. * @property {number}
  246. * asymmetric.cipher.algorithm.RSA_KEM.ivLengthBytes.IVL_64 - 64
  247. * @property {object} asymmetric.cipher.algorithm.RSA_KEM.tagLengthBytes
  248. * -Tag length, in bytes.
  249. * @property {object}
  250. * asymmetric.cipher.algorithm.RSA_KEM.tagLengthBytes.TL_12 - 12
  251. * @property {object}
  252. * asymmetric.cipher.algorithm.RSA_KEM.tagLengthBytes.TL_16 - 16
  253. * @property {object}
  254. * asymmetric.cipher.algorithm.RSA_KEM.tagLengthBytes.TL_32 - 32
  255. * @property {object}
  256. * asymmetric.cipher.algorithm.RSA_KEM.tagLengthBytes.TL_64 - 64
  257. * @property {object} asymmetric.cipher.algorithm.RSA_KEM.keyDeriver - Key
  258. * deriver
  259. * @property {object} asymmetric.cipher.algorithm.RSA_KEM.keyDeriver.name -
  260. * Key deriver name.
  261. * @property {string}
  262. * asymmetric.cipher.algorithm.RSA_KEM.keyDeriver.name.KDF1 -
  263. * 'KDF1'
  264. * @property {string}
  265. * asymmetric.cipher.algorithm.RSA_KEM.keyDeriver.name.KDF2 -
  266. * 'KDF2'
  267. * @property {string}
  268. * asymmetric.cipher.algorithm.RSA_KEM.keyDeriver.name.MGF1 -
  269. * 'MGF1'
  270. * @property {object}
  271. * asymmetric.cipher.algorithm.RSA_KEM.keyDeriver.hashAlgorithm -
  272. * Hash function.
  273. * @property {string}
  274. * asymmetric.cipher.algorithm.RSA_KEM.keyDeriver.hashAlgorithm.SHA256
  275. * - 'SHA256'
  276. * @property {string}
  277. * asymmetric.cipher.algorithm.RSA_KEM.keyDeriver.hashAlgorithm.SHA512_224
  278. * - 'SHA512/224'
  279. *
  280. * @property {object} proofs - Policy for generating zero-knowledge proofs.
  281. * @property {object} proofs.messageDigest - Policy for generating message
  282. * digests.
  283. *
  284. * @property {object} digitalEnvelope - Policy for generating digital
  285. * envelopes.
  286. * @property {object} digitalEnvelope.symmetric - Policy for symmetric
  287. * cryptography operations.
  288. * @property {object} digitalEnvelope.asymmetric.cipher - Policy for
  289. * asymmetric cipher operations.
  290. */
  291. options: options
  292. };
  293. function checkData(policy) {
  294. if (typeof policy !== 'undefined') {
  295. validator.checkIsObjectWithProperties(policy, 'Cryptographic policy');
  296. }
  297. }