123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- /*
- * 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';
- var CommonTestData = require('./data/common-data');
- var ValidationTestData = require('./data/validation-data');
- var elGamal = require('../lib/index');
- var secureRandom = require('scytl-securerandom');
- describe('The ElGamal cryptography module should be able to ...', function() {
- var elGamalService_;
- var secureRandomGenerator_;
- var group_;
- var publicKey_;
- var privateKey_;
- var publicKeyElements_;
- var privateKeyExponents_;
- var elements_;
- var elementValueStrings_;
- var nonObject_;
- var emptyObject_;
- var nonSecureRandomService_;
- var nonSecureRandomGenerator_;
- var nonMathematicalService_;
- var nonBoolean_;
- var nonString_;
- var nonJsonString_;
- var nonArray_;
- var nonStringArray_;
- var nonMathObject_;
- var nonMathObjectArray_;
- var nonElGamalObject_;
- var elementsFromAnotherGroup_;
- var publicKeyFromAnotherGroup_;
- var elGamalEncrypter_;
- var elGamalDecrypter_;
- var elGamalPrngDecrypter_;
- var encryptedElements_;
- var gamma_;
- var phis_;
- var secret_;
- var encryptedElementsFromAnotherGroup_;
- beforeAll(function() {
- elGamalService_ = elGamal.newService();
- secureRandomGenerator_ = secureRandom.newService().newRandomGenerator();
- var commonTestData = new CommonTestData();
- group_ = commonTestData.getGroup();
- publicKey_ = commonTestData.getPublicKey();
- privateKey_ = commonTestData.getPrivateKey();
- publicKeyElements_ = commonTestData.getPublicKeyElements();
- privateKeyExponents_ = commonTestData.getPrivateKeyExponents();
- elements_ = commonTestData.getZpGroupElements();
- elementValueStrings_ = commonTestData.getZpGroupElementValueStrings();
- var validationTestData = new ValidationTestData();
- nonObject_ = validationTestData.getNonObject();
- emptyObject_ = validationTestData.getEmptyObject();
- nonSecureRandomService_ = validationTestData.getNonSecureRandomService();
- nonSecureRandomGenerator_ =
- validationTestData.getNonSecureRandomGenerator();
- nonMathematicalService_ = validationTestData.getNonMathematicalService();
- nonBoolean_ = validationTestData.getNonBoolean();
- nonString_ = validationTestData.getNonString();
- nonJsonString_ = validationTestData.getNonJsonString();
- nonArray_ = validationTestData.getNonArray();
- nonStringArray_ = validationTestData.getNonStringArray();
- nonMathObject_ = validationTestData.getNonMathObject();
- nonMathObjectArray_ = validationTestData.getNonMathObjectArray();
- nonElGamalObject_ = validationTestData.getNonElGamalObject();
- elementsFromAnotherGroup_ =
- validationTestData.getElementsFromAnotherGroup();
- publicKeyFromAnotherGroup_ =
- validationTestData.getPublicKeyFromAnotherGroup();
- });
- beforeEach(function() {
- elGamalEncrypter_ = elGamalService_.newEncrypter().init(publicKey_);
- elGamalDecrypter_ = elGamalService_.newDecrypter().init(privateKey_);
- elGamalPrngDecrypter_ = elGamalService_.newPrngDecrypter().init(
- publicKey_, secureRandomGenerator_);
- var encryptedElementsAndSecret =
- elGamalEncrypter_.encrypt(elements_, {saveSecret: true});
- gamma_ = encryptedElementsAndSecret.gamma;
- phis_ = encryptedElementsAndSecret.phis;
- secret_ = encryptedElementsAndSecret.secret;
- encryptedElements_ = elGamalService_.newEncryptedElements(
- encryptedElementsAndSecret.gamma, encryptedElementsAndSecret.phis);
- encryptedElementsFromAnotherGroup_ =
- elGamalService_.newEncrypter()
- .init(publicKeyFromAnotherGroup_)
- .encrypt(elementsFromAnotherGroup_);
- });
- describe('create an ElGamal cryptography service that should be able to ..', function() {
- it('throw an error when being created, using an invalid secure random service object',
- function() {
- expect(function() {
- Object.create(elGamal.newService({secureRandomService: null}));
- }).toThrow();
- expect(function() {
- Object.create(elGamal.newService({secureRandomService: nonObject_}));
- }).toThrow();
- expect(function() {
- Object.create(
- elGamal.newService({secureRandomService: emptyObject_}));
- }).toThrow();
- });
- it('throw an error when being created, using an invalid mathematical service object',
- function() {
- expect(function() {
- Object.create(elGamal.newService({mathematicalService: null}));
- }).toThrow();
- expect(function() {
- Object.create(elGamal.newService({mathematicalService: nonObject_}));
- }).toThrow();
- expect(function() {
- Object.create(
- elGamal.newService({mathematicalService: emptyObject_}));
- }).toThrow();
- });
- it('throw an error when creating a new ElGamalPublicKey object, using invalid input data',
- function() {
- expect(function() {
- elGamalService_.newPublicKey(group_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPublicKey(undefined, publicKeyElements_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPublicKey(null, publicKeyElements_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPublicKey(nonMathObject_, publicKeyElements_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPublicKey(group_, undefined);
- }).toThrow();
- expect(function() {
- elGamalService_.newPublicKey(group_, null);
- }).toThrow();
- expect(function() {
- elGamalService_.newPublicKey(group_, nonArray_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPublicKey(group_, nonMathObjectArray_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPublicKey(nonJsonString_);
- }).toThrow();
- });
- it('throw an error when creating a new ElGamalPrivateKey object, using invalid input data',
- function() {
- expect(function() {
- elGamalService_.newPrivateKey(undefined, privateKeyExponents_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(null, privateKeyExponents_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(nonObject_, privateKeyExponents_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(emptyObject_, privateKeyExponents_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(group_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(group_, undefined);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(group_, null);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(group_, nonArray_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(group_, nonMathObjectArray_);
- }).toThrow();
- expect(function() {
- elGamalService_.newPrivateKey(nonJsonString_);
- }).toThrow();
- });
- it('throw an error when creating a new ElGamalKeyPair object, using invalid input data',
- function() {
- expect(function() {
- elGamalService_.newKeyPair(publicKey_);
- }).toThrow();
- expect(function() {
- elGamalService_.newKeyPair(undefined, privateKey_);
- }).toThrow();
- expect(function() {
- elGamalService_.newKeyPair(null, privateKey_);
- }).toThrow();
- expect(function() {
- elGamalService_.newKeyPair(nonElGamalObject_, privateKey_);
- }).toThrow();
- expect(function() {
- elGamalService_.newKeyPair(publicKey_, undefined);
- }).toThrow();
- expect(function() {
- elGamalService_.newKeyPair(publicKey_, null);
- }).toThrow();
- expect(function() {
- elGamalService_.newKeyPair(publicKey_, nonElGamalObject_);
- }).toThrow();
- });
- it('throw an error when creating a new ElGamalEncryptedElements object, using invalid input data',
- function() {
- expect(function() {
- elGamalService_.newEncryptedElements(undefined, phis_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(null, phis_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(nonObject_, phis_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(emptyObject_, phis_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(gamma_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(gamma_, undefined);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(gamma_, null);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(gamma_, nonArray_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(gamma_, nonMathObjectArray_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(gamma_, phis_, null);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(gamma_, phis_, nonObject_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(gamma_, phis_, emptyObject_);
- }).toThrow();
- expect(function() {
- elGamalService_.newEncryptedElements(nonJsonString_);
- }).toThrow();
- });
- describe('Create an encrypter/decrypter pair that should ..', function() {
- it('throw an error when being initialized, using invalid input data',
- function() {
- expect(function() {
- elGamalEncrypter_.init();
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.init(undefined);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.init(null);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.init(nonElGamalObject_);
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.init();
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.init(undefined);
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.init(null);
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.init(nonElGamalObject_);
- }).toThrow();
- });
- it('throw an error when encrypting an array of Zp group elements, using invalid input data',
- function() {
- expect(function() {
- elGamalEncrypter_.encrypt();
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(undefined);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(null);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(nonArray_);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(nonMathObjectArray_);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(
- elements_, {useShortExponent: nonBoolean_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(elements_, {preComputation: null});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(elements_, {preComputation: nonObject_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(
- elements_, {preComputation: emptyObject_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(
- elements_, {preComputation: nonElGamalObject_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(elementsFromAnotherGroup_);
- }).toThrow();
- });
- it('throw an error when encrypting an array of Zp group element strings, using invalid input data',
- function() {
- expect(function() {
- elGamalEncrypter_.encrypt();
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(undefined);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(null);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(nonArray_);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(nonStringArray_);
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(elements_, {preComputation: null});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(elements_, {preComputation: nonObject_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(
- elements_, {preComputation: emptyObject_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(
- elements_, {preComputation: nonElGamalObject_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(
- elements_, {useShortExponent: nonBoolean_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.encrypt(elements_, {saveSecret: nonBoolean_});
- }).toThrow();
- });
- it('throw an error when pre-computing an ElGamal encryption, using invalid input data',
- function() {
- expect(function() {
- elGamalEncrypter_.preCompute({useShortExponent: nonBoolean_});
- }).toThrow();
- expect(function() {
- elGamalEncrypter_.preCompute({saveSecret: nonBoolean_});
- }).toThrow();
- });
- it('throw an error when decrypting an ElGamalEncryptedElements object, using invalid input data',
- function() {
- expect(function() {
- elGamalDecrypter_.decrypt();
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.decrypt(undefined);
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.decrypt(null);
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.decrypt(nonElGamalObject_);
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.decrypt(
- encryptedElements_, {confirmMembership: null});
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.decrypt(
- encryptedElements_, {confirmMembership: nonBoolean_});
- }).toThrow();
- expect(function() {
- elGamalDecrypter_.decrypt(encryptedElementsFromAnotherGroup_);
- }).toThrow();
- });
- });
- describe('Create a PRNG decrypter that should ..', function() {
- it('throw an error when being initialized, using invalid input data',
- function() {
- expect(function() {
- elGamalPrngDecrypter_.init();
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.init(undefined);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.init(null);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.init(nonElGamalObject_);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.init(publicKey_);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.init(publicKey_, undefined);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.init(publicKey_, null);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.init(publicKey_, nonObject_);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.init(publicKey_, emptyObject_);
- }).toThrow();
- });
- it('throw an error when decrypting an ElGamalEncryptedElements object, using invalid input data',
- function() {
- expect(function() {
- elGamalPrngDecrypter_.decrypt();
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.decrypt(undefined);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.decrypt(null);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.decrypt(nonElGamalObject_);
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.decrypt(
- encryptedElements_, {confirmMembership: null});
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.decrypt(
- encryptedElements_, {confirmMembership: nonBoolean_});
- }).toThrow();
- expect(function() {
- elGamalPrngDecrypter_.decrypt(encryptedElementsFromAnotherGroup_);
- }).toThrow();
- });
- });
- });
- });
|