input-validator.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. /**
  11. * Input data validation utility for this module. Only intended for internal
  12. * use.
  13. */
  14. module.exports = {
  15. /**
  16. * Checks if a value is defined.
  17. *
  18. * @function checkIsDefined
  19. * @private
  20. * @param {Object}
  21. * value The value to check.
  22. * @param {string}
  23. * label The error message label for the value.
  24. * @throws {TypeError}
  25. * If the value is undefined.
  26. */
  27. checkIsDefined: function(value, label) {
  28. if (typeof value === 'undefined') {
  29. throw new TypeError(label + ' is undefined.');
  30. }
  31. },
  32. /**
  33. * Checks if a value is not null.
  34. *
  35. * @function checkIsNotNull
  36. * @private
  37. * @param {Object}
  38. * value The value to check.
  39. * @param {string}
  40. * label The error message label for the value.
  41. * @throws {TypeError}
  42. * If the value is null.
  43. */
  44. checkIsNotNull: function(value, label) {
  45. if (value === null) {
  46. throw new TypeError(label + ' is null.');
  47. }
  48. },
  49. /**
  50. * Checks if a value is defined and not null.
  51. *
  52. * @function checkIsDefinedAndNotNull
  53. * @private
  54. * @param {Object}
  55. * value The value to check.
  56. * @param {string}
  57. * label The error message label for the value.
  58. * @throws {TypeError}
  59. * If the value is not defined or it is null.
  60. */
  61. checkIsDefinedAndNotNull: function(value, label) {
  62. this.checkIsDefined(value, label);
  63. this.checkIsNotNull(value, label);
  64. },
  65. /**
  66. * Checks if a value is of an expected type.
  67. *
  68. * @function checkIsType
  69. * @private
  70. * @param {Object}
  71. * value The value to check.
  72. * @param {string}
  73. * type The expected type of the value.
  74. * @param {string}
  75. * label The error message label for the value.
  76. * @throws {TypeError}
  77. * If the value is not of the expected type.
  78. */
  79. checkIsType: function(value, type, label) {
  80. var typeFound = typeof value;
  81. if (typeFound !== type) {
  82. throw new TypeError(
  83. 'Expected ' + label + ' to have type \'' + type + '\' ; Found: \'' +
  84. typeFound + '\'');
  85. }
  86. },
  87. /**
  88. * Checks if a value is an instance of an Object.
  89. *
  90. * @function checkIsInstanceOf
  91. * @private
  92. * @param {Object}
  93. * value The value to check.
  94. * @param {Object}
  95. * obj The Object to check against.
  96. * @param {string}
  97. * objName The Object name, for error handling purposes.
  98. * @param {string}
  99. * label The error message label for the value.
  100. * @throws {TypeError}
  101. * If the object is undefined, null, or it is not an instance of
  102. * the Object.
  103. */
  104. checkIsInstanceOf: function(value, obj, objName, label) {
  105. this.checkIsDefinedAndNotNull(value, label);
  106. if (!(value instanceof obj)) {
  107. throw new TypeError(label + ' is not an instance of Object ' + objName);
  108. }
  109. },
  110. /**
  111. * Checks if a value is an object.
  112. *
  113. * @function checkIsObject
  114. * @private
  115. * @param {Object}
  116. * value The value to check.
  117. * @param {string}
  118. * label The error message label for the value.
  119. * @throws {TypeError}
  120. * If the value is not an object.
  121. */
  122. checkIsObject: function(value, label) {
  123. if (typeof value !== 'object') {
  124. throw new TypeError(label + ' is not an object.');
  125. }
  126. },
  127. /**
  128. * Checks if a value is an object and has properties.
  129. *
  130. * @function checkIsObjectWithProperties
  131. * @private
  132. * @param {Object}
  133. * value The value to check.
  134. * @param {string}
  135. * label The error message label for the value.
  136. * @throws {TypeError}
  137. * If the value is not an object or it has no properties.
  138. */
  139. checkIsObjectWithProperties: function(value, label) {
  140. this.checkIsDefinedAndNotNull(value, label);
  141. this.checkIsObject(value, label);
  142. if (!Object.getOwnPropertyNames(value).length) {
  143. throw new TypeError(label + ' does not have any properties.');
  144. }
  145. },
  146. /**
  147. * Checks if a value is an Array object.
  148. *
  149. * @function checkIsArray
  150. * @private
  151. * @param {Array}
  152. * array The value to check.
  153. * @param {string}
  154. * label The error message label for the value.
  155. * @throws {TypeError}
  156. * If the value is not an Array object.
  157. */
  158. checkIsArray: function(value, label) {
  159. this.checkIsDefinedAndNotNull(value, 'Array of ' + label);
  160. if (value.constructor !== Array) {
  161. throw new TypeError('Array of ' + label + ' is not of type Array.');
  162. }
  163. },
  164. /**
  165. * Checks if a value is a non-empty string.
  166. *
  167. * @function checkIsNonEmptyString
  168. * @private
  169. * @param {string}
  170. * value The value to check.
  171. * @param {string}
  172. * label The error message label for the value.
  173. * @throws {TypeError}
  174. * If the value is not a non-empty string.
  175. */
  176. checkIsNonEmptyString: function(value, label) {
  177. this.checkIsType(value, 'string', label);
  178. if (value.length === 0) {
  179. throw new TypeError(label + ' is empty.');
  180. }
  181. },
  182. /**
  183. * Checks if a value is a non-empty Array object.
  184. *
  185. * @function checkIsNonEmptyArray
  186. * @private
  187. * @param {Array}
  188. * array The value to check.
  189. * @param {string}
  190. * label The error message label for the value.
  191. * @throws {TypeError}
  192. * If the value is not a non-empty Array object.
  193. */
  194. checkIsNonEmptyArray: function(value, label) {
  195. this.checkIsArray(value, label);
  196. if (value.length < 1) {
  197. throw new TypeError('Array of ' + label + ' is empty.');
  198. }
  199. },
  200. /**
  201. * Checks if a value is an array of strings.
  202. *
  203. * @function checkIsStringArray
  204. * @private
  205. * @param {string[]}
  206. * value The value to check.
  207. * @param {string}
  208. * label The error message label for the value.
  209. * @throws {TypeError}
  210. * If value is not an array of strings.
  211. */
  212. checkIsStringArray: function(value, label) {
  213. this.checkIsNonEmptyArray(value, label);
  214. for (var i = 0; i < value.length; i++) {
  215. this.checkIsType(value[i], 'string', 'Element ' + i + ' of ' + label);
  216. }
  217. },
  218. /**
  219. * Checks if a value is a valid JSON object.
  220. *
  221. * @function checkIsJsonObject
  222. * @private
  223. * @param {Object}
  224. * value The value to check.
  225. * @param {string}
  226. * label The error message label for the value.
  227. * @throws {TypeError}
  228. * If the value is a valid JSON object.
  229. */
  230. checkIsJsonObject: function(value, label) {
  231. try {
  232. JSON.parse(value);
  233. } catch (error) {
  234. throw new TypeError(label + ' is not a valid JSON object.');
  235. }
  236. },
  237. /**
  238. * Checks if a value is a valid JSON string.
  239. *
  240. * @function checkIsJsonString
  241. * @private
  242. * @param {Object}
  243. * value The value to check.
  244. * @param {string}
  245. * label The error message label for the value.
  246. * @throws {TypeError}
  247. * If the value is a valid JSON string.
  248. */
  249. checkIsJsonString: function(value, label) {
  250. this.checkIsNonEmptyString(value, label);
  251. this.checkIsJsonObject(value, label);
  252. },
  253. /**
  254. * Checks the validity of a ZpGroupElement object.
  255. *
  256. * @function checkZpGroupElement
  257. * @private
  258. * @param {ZpGroupElement}
  259. * element The object to check.
  260. * @param {string}
  261. * label The error message label for the object.
  262. * @param {Zpsubgroup}
  263. * [group] Optional parameter pertaining to a Zp subgroup. If
  264. * this option is provided, the modulus and order of the element
  265. * will be checked against those of the group.
  266. * @throws {TypeError}
  267. * If the value is not a valid ZpGroupElement object.
  268. */
  269. checkZpGroupElement: function(element, label, group) {
  270. this.checkIsObjectWithProperties(element, label);
  271. if (typeof group !== 'undefined') {
  272. var groupP = group.p;
  273. var groupQ = group.q;
  274. var elementP = element.p;
  275. if (!elementP.equals(groupP)) {
  276. throw new TypeError(
  277. 'Expected modulus p of ' + label +
  278. ' to equal modulus of Zp subgroup provided as input: ' + groupP +
  279. ' ; Found: ' + elementP);
  280. }
  281. var elementQ = element.q;
  282. if (!elementQ.equals(groupQ)) {
  283. throw new TypeError(
  284. 'Expected order q of ' + label +
  285. ' to equal order of Zp subgroup provided as input: ' + groupQ +
  286. ' ; Found: ' + elementQ);
  287. }
  288. }
  289. },
  290. /**
  291. * Checks the validity of an Exponent object.
  292. *
  293. * @function checkExponent
  294. * @private
  295. * @param {Exponent}
  296. * exponent The object to check.
  297. * @param {string}
  298. * label The error message label for the object.
  299. * @param {Zpsubgroup}
  300. * [q] Optional parameter pertaining to a Zp subgroup order. If
  301. * this option is provided, the order of the exponent will be
  302. * checked against that order.
  303. * @throws {TypeError}
  304. * If the value is not a valid Exponent value.
  305. */
  306. checkExponent: function(exponent, label, q) {
  307. this.checkIsObjectWithProperties(exponent, label);
  308. if (typeof q !== 'undefined') {
  309. var exponentQ = exponent.q;
  310. if (!exponentQ.equals(q)) {
  311. throw new TypeError(
  312. 'Expected order q of ' + label +
  313. ' to equal order of Zp subgroup provided as input: ' + q +
  314. ' ; Found: ' + exponentQ);
  315. }
  316. }
  317. },
  318. /**
  319. * Checks the validity of an array of ZpGroupElement objects.
  320. *
  321. * @function checkZpGroupElements
  322. * @private
  323. * @param {ZpGroupElement[]}
  324. * elements The array to check.
  325. * @param {string}
  326. * label The error message label for the array.
  327. * @param {Zpsubgroup}
  328. * [group] Optional parameter pertaining to a Zp subgroup. If
  329. * this option is provided, the modulus and order of each element
  330. * will be checked against those of the group.
  331. * @throws {TypeError}
  332. * If the array of ZpGroupElement objects is not valid.
  333. */
  334. checkZpGroupElements: function(elements, label, group) {
  335. this.checkIsNonEmptyArray(elements, label);
  336. for (var i = 0; i < elements.length; i++) {
  337. this.checkZpGroupElement(
  338. elements[i], 'element ' + i + ' of ' + label, group);
  339. }
  340. },
  341. /**
  342. * Checks the validity of an array of Exponent objects.
  343. *
  344. * @function checkExponents
  345. * @private
  346. * @param {Exponent[]}
  347. * exponents The array to check.
  348. * @param {string}
  349. * label The error message label for the array.
  350. * @param {Zpsubgroup}
  351. * [q] Optional parameter pertaining to a Zp subgroup order. If
  352. * this option is provided, the order of the exponent will be
  353. * checked against that order.
  354. * @throws {TypeError}
  355. * If the array of Exponent objects is not valid.
  356. */
  357. checkExponents: function(exponents, label, q) {
  358. this.checkIsNonEmptyArray(exponents, label);
  359. for (var i = 0; i < exponents.length; i++) {
  360. this.checkExponent(exponents[i], 'exponent ' + i + ' of ' + label, q);
  361. }
  362. },
  363. /**
  364. * Checks the validity of an ElGamalPublicKey object.
  365. *
  366. * @function checkElGamalPublicKey
  367. * @private
  368. * @param {ElGamalPublicKey}
  369. * publicKey The object to check.
  370. * @param {string}
  371. * label The error message label for the object.
  372. * @param {Zpsubgroup}
  373. * [group] Optional parameter pertaining to a Zp subgroup. If
  374. * this option is provided, the modulus and order of the public
  375. * key will be checked against those of the group.
  376. * @throws {TypeError}
  377. * If the ElGamalPublicKey object is not valid.
  378. */
  379. checkElGamalPublicKey: function(publicKey, label, group) {
  380. this.checkIsObjectWithProperties(publicKey, label);
  381. if (typeof group !== 'undefined') {
  382. var groupP = group.p;
  383. var groupQ = group.q;
  384. var publicKeyP = publicKey.group.p;
  385. if (!publicKeyP.equals(groupP)) {
  386. throw new TypeError(
  387. 'Expected modulus p of ' + label +
  388. ' to equal modulus of Zp subgroup provided as input: ' + groupP +
  389. ' ; Found: ' + publicKeyP);
  390. }
  391. var publicKeyQ = publicKey.group.q;
  392. if (!publicKeyQ.equals(groupQ)) {
  393. throw new TypeError(
  394. 'Expected order q of ' + label +
  395. ' to equal order of Zp subgroup provided as input: ' + groupQ +
  396. ' ; Found: ' + publicKeyQ);
  397. }
  398. }
  399. },
  400. /**
  401. * Checks the validity of an ElGamalPrivateKey object.
  402. *
  403. * @function checkElGamalPrivateKey
  404. * @private
  405. * @param {ElGamalPrivateKey}
  406. * privateKey The object to check.
  407. * @param {string}
  408. * label The error message label for the object.
  409. * @param {Zpsubgroup}
  410. * [group] Optional parameter pertaining to a Zp subgroup. If
  411. * this option is provided, the order of the private key will be
  412. * checked against that of the group.
  413. * @throws {TypeError}
  414. * If the ElGamalPrivateKey object is not valid.
  415. */
  416. checkElGamalPrivateKey: function(privateKey, label, group) {
  417. this.checkIsObjectWithProperties(privateKey, label);
  418. if (typeof group !== 'undefined') {
  419. var groupQ = group.q;
  420. var privateKeyQ = privateKey.group.q;
  421. if (!privateKeyQ.equals(groupQ)) {
  422. throw new TypeError(
  423. 'Expected order q of ' + label +
  424. ' to equal order of Zp subgroup provided as input: ' + groupQ +
  425. ' ; Found: ' + privateKeyQ);
  426. }
  427. }
  428. },
  429. /**
  430. * Checks the validity of an ElGamalEncryptedElements object.
  431. *
  432. * @function checkElGamalEncryptedElements
  433. * @private
  434. * @param {ElGamalEncryptedElements}
  435. * encryptedElements The object to check.
  436. * @param {string}
  437. * label The error message label for the object.
  438. * @param {Zpsubgroup}
  439. * [group] Optional parameter pertaining to a Zp subgroup. If
  440. * this option is provided, the modulus and order of the value's
  441. * secret and ElGamal encrypted elements will be checked against
  442. * those of the group.
  443. * @throws {TypeError}
  444. * If the ElGamalEncryptedElements object is not valid.
  445. */
  446. checkElGamalEncryptedElements: function(encryptedElements, label, group) {
  447. this.checkIsObjectWithProperties(encryptedElements, label);
  448. if (typeof group !== 'undefined') {
  449. var groupP = group.p;
  450. var groupQ = group.q;
  451. var gamma = encryptedElements.gamma;
  452. var gammaP = gamma.p;
  453. if (!gammaP.equals(groupP)) {
  454. throw new TypeError(
  455. 'Expected modulus p of gamma element of ' + label +
  456. ' to equal modulus of Zp subgroup provided as input: ' + groupP +
  457. ' ; Found: ' + gammaP);
  458. }
  459. var gammaQ = gamma.q;
  460. if (!gammaQ.equals(groupQ)) {
  461. throw new TypeError(
  462. 'Expected order q of gamma element of ' + label +
  463. ' to equal order of Zp subgroup provided as input: ' + groupQ +
  464. ' ; Found: ' + gammaQ);
  465. }
  466. }
  467. var secret = encryptedElements.secret;
  468. if (typeof secret !== 'undefined') {
  469. this.checkExponent(secret, group);
  470. }
  471. }
  472. };