123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- /*
- * Copyright 2018 Scytl Secure Electronic Voting SA
- *
- * All rights reserved
- *
- * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
- */
- /* jshint node:true */
- 'use strict';
- /**
- * Input data validation utility for this module. Only intended for internal
- * use.
- */
- module.exports = {
- /**
- * Checks if a value is defined.
- *
- * @function checkIsDefined
- * @private
- * @param {Object}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is undefined.
- */
- checkIsDefined: function(value, label) {
- if (typeof value === 'undefined') {
- throw new TypeError(label + ' is undefined.');
- }
- },
- /**
- * Checks if a value is not null.
- *
- * @function checkIsNotNull
- * @private
- * @param {Object}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is null.
- */
- checkIsNotNull: function(value, label) {
- if (value === null) {
- throw new TypeError(label + ' is null.');
- }
- },
- /**
- * Checks if a value is defined and not null.
- *
- * @function checkIsDefinedAndNotNull
- * @private
- * @param {Object}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is not defined or it is null.
- */
- checkIsDefinedAndNotNull: function(value, label) {
- this.checkIsDefined(value, label);
- this.checkIsNotNull(value, label);
- },
- /**
- * Checks if a value is of an expected type.
- *
- * @function checkIsType
- * @private
- * @param {Object}
- * value The value to check.
- * @param {string}
- * type The expected type of the value.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is not of the expected type.
- */
- checkIsType: function(value, type, label) {
- var typeFound = typeof value;
- if (typeFound !== type) {
- throw new TypeError(
- 'Expected ' + label + ' to have type \'' + type + '\' ; Found: \'' +
- typeFound + '\'');
- }
- },
- /**
- * Checks if a value is an instance of an Object.
- *
- * @function checkIsInstanceOf
- * @private
- * @param {Object}
- * value The value to check.
- * @param {Object}
- * obj The Object to check against.
- * @param {string}
- * objName The Object name, for error handling purposes.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the object is undefined, null, or it is not an instance of
- * the Object.
- */
- checkIsInstanceOf: function(value, obj, objName, label) {
- this.checkIsDefinedAndNotNull(value, label);
- if (!(value instanceof obj)) {
- throw new TypeError(label + ' is not an instance of Object ' + objName);
- }
- },
- /**
- * Checks if a value is an object.
- *
- * @function checkIsObject
- * @private
- * @param {Object}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is not an object.
- */
- checkIsObject: function(value, label) {
- if (typeof value !== 'object') {
- throw new TypeError(label + ' is not an object.');
- }
- },
- /**
- * Checks if a value is an object and has properties.
- *
- * @function checkIsObjectWithProperties
- * @private
- * @param {Object}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is not an object or it has no properties.
- */
- checkIsObjectWithProperties: function(value, label) {
- this.checkIsDefinedAndNotNull(value, label);
- this.checkIsObject(value, label);
- if (!Object.getOwnPropertyNames(value).length) {
- throw new TypeError(label + ' does not have any properties.');
- }
- },
- /**
- * Checks if a value is an Array object.
- *
- * @function checkIsArray
- * @private
- * @param {Array}
- * array The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is not an Array object.
- */
- checkIsArray: function(value, label) {
- this.checkIsDefinedAndNotNull(value, 'Array of ' + label);
- if (value.constructor !== Array) {
- throw new TypeError('Array of ' + label + ' is not of type Array.');
- }
- },
- /**
- * Checks if a value is a non-empty string.
- *
- * @function checkIsNonEmptyString
- * @private
- * @param {string}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is not a non-empty string.
- */
- checkIsNonEmptyString: function(value, label) {
- this.checkIsType(value, 'string', label);
- if (value.length === 0) {
- throw new TypeError(label + ' is empty.');
- }
- },
- /**
- * Checks if a value is a non-empty Array object.
- *
- * @function checkIsNonEmptyArray
- * @private
- * @param {Array}
- * array The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is not a non-empty Array object.
- */
- checkIsNonEmptyArray: function(value, label) {
- this.checkIsArray(value, label);
- if (value.length < 1) {
- throw new TypeError('Array of ' + label + ' is empty.');
- }
- },
- /**
- * Checks if a value is an array of strings.
- *
- * @function checkIsStringArray
- * @private
- * @param {string[]}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If value is not an array of strings.
- */
- checkIsStringArray: function(value, label) {
- this.checkIsNonEmptyArray(value, label);
- for (var i = 0; i < value.length; i++) {
- this.checkIsType(value[i], 'string', 'Element ' + i + ' of ' + label);
- }
- },
- /**
- * Checks if a value is a valid JSON object.
- *
- * @function checkIsJsonObject
- * @private
- * @param {Object}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is a valid JSON object.
- */
- checkIsJsonObject: function(value, label) {
- try {
- JSON.parse(value);
- } catch (error) {
- throw new TypeError(label + ' is not a valid JSON object.');
- }
- },
- /**
- * Checks if a value is a valid JSON string.
- *
- * @function checkIsJsonString
- * @private
- * @param {Object}
- * value The value to check.
- * @param {string}
- * label The error message label for the value.
- * @throws {TypeError}
- * If the value is a valid JSON string.
- */
- checkIsJsonString: function(value, label) {
- this.checkIsNonEmptyString(value, label);
- this.checkIsJsonObject(value, label);
- },
- /**
- * Checks the validity of a ZpGroupElement object.
- *
- * @function checkZpGroupElement
- * @private
- * @param {ZpGroupElement}
- * element The object to check.
- * @param {string}
- * label The error message label for the object.
- * @param {Zpsubgroup}
- * [group] Optional parameter pertaining to a Zp subgroup. If
- * this option is provided, the modulus and order of the element
- * will be checked against those of the group.
- * @throws {TypeError}
- * If the value is not a valid ZpGroupElement object.
- */
- checkZpGroupElement: function(element, label, group) {
- this.checkIsObjectWithProperties(element, label);
- if (typeof group !== 'undefined') {
- var groupP = group.p;
- var groupQ = group.q;
- var elementP = element.p;
- if (!elementP.equals(groupP)) {
- throw new TypeError(
- 'Expected modulus p of ' + label +
- ' to equal modulus of Zp subgroup provided as input: ' + groupP +
- ' ; Found: ' + elementP);
- }
- var elementQ = element.q;
- if (!elementQ.equals(groupQ)) {
- throw new TypeError(
- 'Expected order q of ' + label +
- ' to equal order of Zp subgroup provided as input: ' + groupQ +
- ' ; Found: ' + elementQ);
- }
- }
- },
- /**
- * Checks the validity of an Exponent object.
- *
- * @function checkExponent
- * @private
- * @param {Exponent}
- * exponent The object to check.
- * @param {string}
- * label The error message label for the object.
- * @param {Zpsubgroup}
- * [q] Optional parameter pertaining to a Zp subgroup order. If
- * this option is provided, the order of the exponent will be
- * checked against that order.
- * @throws {TypeError}
- * If the value is not a valid Exponent value.
- */
- checkExponent: function(exponent, label, q) {
- this.checkIsObjectWithProperties(exponent, label);
- if (typeof q !== 'undefined') {
- var exponentQ = exponent.q;
- if (!exponentQ.equals(q)) {
- throw new TypeError(
- 'Expected order q of ' + label +
- ' to equal order of Zp subgroup provided as input: ' + q +
- ' ; Found: ' + exponentQ);
- }
- }
- },
- /**
- * Checks the validity of an array of ZpGroupElement objects.
- *
- * @function checkZpGroupElements
- * @private
- * @param {ZpGroupElement[]}
- * elements The array to check.
- * @param {string}
- * label The error message label for the array.
- * @param {Zpsubgroup}
- * [group] Optional parameter pertaining to a Zp subgroup. If
- * this option is provided, the modulus and order of each element
- * will be checked against those of the group.
- * @throws {TypeError}
- * If the array of ZpGroupElement objects is not valid.
- */
- checkZpGroupElements: function(elements, label, group) {
- this.checkIsNonEmptyArray(elements, label);
- for (var i = 0; i < elements.length; i++) {
- this.checkZpGroupElement(
- elements[i], 'element ' + i + ' of ' + label, group);
- }
- },
- /**
- * Checks the validity of an array of Exponent objects.
- *
- * @function checkExponents
- * @private
- * @param {Exponent[]}
- * exponents The array to check.
- * @param {string}
- * label The error message label for the array.
- * @param {Zpsubgroup}
- * [q] Optional parameter pertaining to a Zp subgroup order. If
- * this option is provided, the order of the exponent will be
- * checked against that order.
- * @throws {TypeError}
- * If the array of Exponent objects is not valid.
- */
- checkExponents: function(exponents, label, q) {
- this.checkIsNonEmptyArray(exponents, label);
- for (var i = 0; i < exponents.length; i++) {
- this.checkExponent(exponents[i], 'exponent ' + i + ' of ' + label, q);
- }
- },
- /**
- * Checks the validity of an ElGamalPublicKey object.
- *
- * @function checkElGamalPublicKey
- * @private
- * @param {ElGamalPublicKey}
- * publicKey The object to check.
- * @param {string}
- * label The error message label for the object.
- * @param {Zpsubgroup}
- * [group] Optional parameter pertaining to a Zp subgroup. If
- * this option is provided, the modulus and order of the public
- * key will be checked against those of the group.
- * @throws {TypeError}
- * If the ElGamalPublicKey object is not valid.
- */
- checkElGamalPublicKey: function(publicKey, label, group) {
- this.checkIsObjectWithProperties(publicKey, label);
- if (typeof group !== 'undefined') {
- var groupP = group.p;
- var groupQ = group.q;
- var publicKeyP = publicKey.group.p;
- if (!publicKeyP.equals(groupP)) {
- throw new TypeError(
- 'Expected modulus p of ' + label +
- ' to equal modulus of Zp subgroup provided as input: ' + groupP +
- ' ; Found: ' + publicKeyP);
- }
- var publicKeyQ = publicKey.group.q;
- if (!publicKeyQ.equals(groupQ)) {
- throw new TypeError(
- 'Expected order q of ' + label +
- ' to equal order of Zp subgroup provided as input: ' + groupQ +
- ' ; Found: ' + publicKeyQ);
- }
- }
- },
- /**
- * Checks the validity of an ElGamalPrivateKey object.
- *
- * @function checkElGamalPrivateKey
- * @private
- * @param {ElGamalPrivateKey}
- * privateKey The object to check.
- * @param {string}
- * label The error message label for the object.
- * @param {Zpsubgroup}
- * [group] Optional parameter pertaining to a Zp subgroup. If
- * this option is provided, the order of the private key will be
- * checked against that of the group.
- * @throws {TypeError}
- * If the ElGamalPrivateKey object is not valid.
- */
- checkElGamalPrivateKey: function(privateKey, label, group) {
- this.checkIsObjectWithProperties(privateKey, label);
- if (typeof group !== 'undefined') {
- var groupQ = group.q;
- var privateKeyQ = privateKey.group.q;
- if (!privateKeyQ.equals(groupQ)) {
- throw new TypeError(
- 'Expected order q of ' + label +
- ' to equal order of Zp subgroup provided as input: ' + groupQ +
- ' ; Found: ' + privateKeyQ);
- }
- }
- },
- /**
- * Checks the validity of an ElGamalEncryptedElements object.
- *
- * @function checkElGamalEncryptedElements
- * @private
- * @param {ElGamalEncryptedElements}
- * encryptedElements The object to check.
- * @param {string}
- * label The error message label for the object.
- * @param {Zpsubgroup}
- * [group] Optional parameter pertaining to a Zp subgroup. If
- * this option is provided, the modulus and order of the value's
- * secret and ElGamal encrypted elements will be checked against
- * those of the group.
- * @throws {TypeError}
- * If the ElGamalEncryptedElements object is not valid.
- */
- checkElGamalEncryptedElements: function(encryptedElements, label, group) {
- this.checkIsObjectWithProperties(encryptedElements, label);
- if (typeof group !== 'undefined') {
- var groupP = group.p;
- var groupQ = group.q;
- var gamma = encryptedElements.gamma;
- var gammaP = gamma.p;
- if (!gammaP.equals(groupP)) {
- throw new TypeError(
- 'Expected modulus p of gamma element of ' + label +
- ' to equal modulus of Zp subgroup provided as input: ' + groupP +
- ' ; Found: ' + gammaP);
- }
- var gammaQ = gamma.q;
- if (!gammaQ.equals(groupQ)) {
- throw new TypeError(
- 'Expected order q of gamma element of ' + label +
- ' to equal order of Zp subgroup provided as input: ' + groupQ +
- ' ; Found: ' + gammaQ);
- }
- }
- var secret = encryptedElements.secret;
- if (typeof secret !== 'undefined') {
- this.checkExponent(secret, group);
- }
- }
- };
|