123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- /*
- * 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
- */
- describe('Signers: ', function() {
- 'use strict';
- describe(' from the Forge provider ..', function() {
- describe('should be able to ..', function() {
- var _plaintext = 'Ox2fUJq1gAbX';
- var _privateKeyPem =
- '-----BEGIN RSA PRIVATE KEY-----MIIEowIBAAKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQABAoIBAC/tn34Wf3k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DKiFCxCUZ948+9NuG+DnpXDWzw//r8mPBRRGGsqFws5Aipp7yjQ3kRDCCzGelPCVhHyfmKqA+8ewXPulKS3/wIyHIvaXmsuAtTfurHtpRyzjKmCBK1Y6WQ3trIXvo7s-----END RSA PRIVATE KEY-----';
- var _publicKeyPem =
- '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAglypZU45bnf2opnRI+Y51VqouBpvDT33xIB/OtbwKzwVpi+JrjBFtfk33tE9t/dSRs79CK94HRhCWcOiLa2qWPrjeZ9SBiEyScrhIvRZVBF41zBgwQNuRvJCsKmAqlZaFNJDZxEP4repmlBn1CfVFmfrXmOKqwP5F7l9ZtucveRzsfmF1yVPFkW8TMuB3YqMiyymyqHlS8ujCsu5I8tpgPbwuxdMOY94fNhSXrYkY8IuX1g1zdq/Z1jluOaR/UqK4UpnbuJaH/F0VgDNiWh6cTD0DFGEk0b70i5wU4Q3L/S6XZQRvSuADoCbhwBKuFL5pW5n865oLVb5S3wuVdWaGwIDAQAB-----END PUBLIC KEY-----';
- it('generate a signature for some data and verify the signature when SHA256withRSA is used',
- function() {
- cryptolib('asymmetric.signer', 'commons.utils', function() {
- var f = function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var dataB64 = converters.base64Encode(_plaintext);
- var arrayDataBase64 = [dataB64];
- var signatureBase64 =
- cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
- // Check whether digital signature can be verified.
- var verified = cryptoForgeSigner.verify(
- signatureBase64, _publicKeyPem, arrayDataBase64);
- // Verify that digital signature was successfully verified.
- expect(verified).toBeTruthy();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- provider: 'Forge',
- hash: 'SHA256',
- publicExponent: 65537,
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', 'commons.utils', f);
- });
- });
- it('generate a signature for some data and verify the signature when SHA256withRSA/PSS is used',
- function() {
- cryptolib('asymmetric.signer', 'commons.utils', function() {
- var f = function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var dataB64 = converters.base64Encode(_plaintext);
- var arrayDataBase64 = [dataB64];
- var signatureBase64 =
- cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
- // Check whether digital signature can be verified.
- var verified = cryptoForgeSigner.verify(
- signatureBase64, _publicKeyPem, arrayDataBase64);
- // Verify that digital signature was successfully verified.
- expect(verified).toBeTruthy();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- provider: 'Forge',
- hash: 'SHA256',
- padding: {
- PSS: {
- name: 'PSS',
- hash: 'SHA256',
- mask: {MGF1: {name: 'MGF1', hash: 'SHA256'}},
- saltLengthBytes: 32
- }
- },
- publicExponent: 65537,
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', 'commons.utils', f);
- });
- });
- it('generate a signature for an empty data and verify the signature when SHA256withRSA is used',
- function() {
- cryptolib('asymmetric.signer', 'commons.utils', function() {
- var f = function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var dataB64 = converters.base64Encode('');
- var arrayDataBase64 = [dataB64];
- var signatureBase64 =
- cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
- // Check whether digital signature can be verified.
- var verified = cryptoForgeSigner.verify(
- signatureBase64, _publicKeyPem, arrayDataBase64);
- // Verify that digital signature was successfully verified.
- expect(verified).toBeTruthy();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- provider: 'Forge',
- hash: 'SHA256',
- publicExponent: 65537,
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', 'commons.utils', f);
- });
- });
- it('detect a null element in data when signing', function() {
- cryptolib('asymmetric.signer', 'commons.utils', function() {
- var f = function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var dataElement1 = 'dataElement1';
- var dataElement2 = 'dataElement2';
- var dataElement3 = null;
- var dataElement4 = 'dataElement4';
- var dataElement1B64 = converters.base64Encode(dataElement1);
- var dataElement2B64 = converters.base64Encode(dataElement2);
- var dataElement4B64 = converters.base64Encode(dataElement4);
- var arrayDataBase64 = [
- dataElement1B64, dataElement2B64, dataElement3, dataElement4B64
- ];
- var call = function() {
- cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
- };
- expect(call).toThrow();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- provider: 'Forge',
- hash: 'SHA256',
- publicExponent: 65537,
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', 'commons.utils', f);
- });
- });
- it('detect that a signature is false when SHA256withRSA is used',
- function() {
- cryptolib('asymmetric.signer', 'commons.utils', function() {
- var f = function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var dataB64 = converters.base64Encode(_plaintext);
- var dataB64Different =
- converters.base64Encode('Some other string.');
- var arrayDataBase64 = [dataB64];
- var arrayDataBase64Different = [dataB64Different];
- var signatureBase64 =
- cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
- // Check whether digital signature can be verified.
- var verified = cryptoForgeSigner.verify(
- signatureBase64, _publicKeyPem, arrayDataBase64Different);
- // Verify that digital signature was successfully verified.
- expect(!verified).toBeTruthy();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- provider: 'Forge',
- hash: 'SHA256',
- publicExponent: 65537,
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', 'commons.utils', f);
- });
- });
- it('detect that a signature is false when SHA256withRSA/PSS is used',
- function() {
- cryptolib('asymmetric.signer', 'commons.utils', function() {
- var f = function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var dataB64 = converters.base64Encode(_plaintext);
- var dataB64Different =
- converters.base64Encode('Some other string.');
- var arrayDataBase64 = [dataB64];
- var arrayDataBase64Different = [dataB64Different];
- var signatureBase64 =
- cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
- // Check whether digital signature can be verified.
- var verified = cryptoForgeSigner.verify(
- signatureBase64, _publicKeyPem, arrayDataBase64Different);
- // Verify that digital signature was successfully verified.
- expect(!verified).toBeTruthy();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- provider: 'Forge',
- hash: 'SHA256',
- padding: {
- PSS: {
- name: 'PSS',
- hash: 'SHA256',
- mask: {MGF1: {name: 'MGF1', hash: 'SHA256'}},
- saltLengthBytes: 32
- }
- },
- publicExponent: 65537,
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', 'commons.utils', f);
- });
- });
- it('verify a RSA signature that was generated in Java', function() {
- cryptolib('asymmetric.signer', 'commons.utils', function() {
- var f = function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var signedData = 'This is a string from Java.';
- var signedDataB64 = converters.base64Encode(signedData);
- var arraySignedDataB64 = [signedDataB64];
- // this is a signature generated in Java (using RSA)
- var signatureFromJavaRsa =
- 'KuQPmBRE/JpNQ5mvh1Y1LvW3Jr/ZYlYqOgdbgXLVZp+gjVtXHDgFZzIrC8+S+VdTs9WINjxN8aIZlbiemZ+YvXZ7fDOoJsZJRboyrFN9VGz6sWWVXAv28Xj1a2lPNlys8dO3uY0s5R77kDCvBHV4nJh0Hq2Ry+JRjRcrlPsIiDWzm9iTY7V6XzkVGCo58qWyQtloC0s73i8UM6V2nYu1oGDuHqUcvDbstElGAJrn7u4QtP2Kgzd1GSkvbSKhsjR2RniiJ7cD278i27NAEPXY+qqmWQ4c+zt3BKG4Aj4EtKa/4AyARmLC78KjZpfiGPTUzQZbMQudAMmKgSTQhx7ONg==';
- // Check whether digital signature can be verified.
- var verified = cryptoForgeSigner.verify(
- signatureFromJavaRsa, _publicKeyPem, arraySignedDataB64);
- // Verify that digital signature was successfully verified.
- expect(verified).toBeTruthy();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- provider: 'Forge',
- hash: 'SHA256',
- publicExponent: 65537,
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', 'commons.utils', f);
- });
- });
- it('verify a RSA/PSS signature that was generated in Java', function() {
- cryptolib('asymmetric.signer', 'commons.utils', function() {
- var f = function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var signedData = 'This is a string from Java.';
- var signedDataB64 = converters.base64Encode(signedData);
- var arraySignedDataB64 = [signedDataB64];
- // This is a signature generated in Java (using RSA/PSS)
- var signatureFromJavaRsa =
- 'JSYZ0i3Yz3L8J3bUOLOP/Kk83qkhoZ9NqBm9eUpaFoZY8gD42XHJxXyPmDYMnDFMr+J5GypfS9vheYtgn2hag8n3DcG86I80UCNuEZLEhyXpIsN4mv3rDb/xjMXlnDLWiK6MTBTDVImKfwpX6x/xw9YtcG9ADACRefH4++H9fhpg8yXUfT9OacOPHgjoW/y4hJmaKVk+IVDgLAKfToNWwVm2xU0SnFryQ9PUJeR8KHttBLR+gpZZpn+bQMcEvikw8bzAYGX4yXxGB9UgoIsz6xNyxY6P6UyJSal/JajURLuSwW7JpxjtthbyKS6SUXtH+NEg36ZpMhd8QC6EPzwrlQ==';
- // Check whether digital signature can be verified.
- var verified = cryptoForgeSigner.verify(
- signatureFromJavaRsa, _publicKeyPem, arraySignedDataB64);
- // Verify that digital signature was successfully verified.
- expect(verified).toBeTruthy();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- provider: 'Forge',
- hash: 'SHA256',
- padding: {
- PSS: {
- name: 'PSS',
- hash: 'SHA256',
- mask: {MGF1: {name: 'MGF1', hash: 'SHA256'}},
- saltLengthBytes: 32
- }
- },
- publicExponent: 65537,
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', 'commons.utils', f);
- });
- });
- it('generate a RSA digital signature for some data and verify the signature.',
- function() {
- cryptolib('asymmetric.signer', 'commons.utils', function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var converters = new box.commons.utils.Converters();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var dataB64 = converters.base64Encode(_plaintext);
- var arrayDataBase64 = [dataB64];
- var signatureBase64 =
- cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
- // Check whether digital signature can be verified.
- var verified = cryptoForgeSigner.verify(
- signatureBase64, _publicKeyPem, arrayDataBase64);
- // Verify that digital signature was successfully verified.
- expect(verified).toBeTruthy();
- });
- });
- it('throw an error when the array of data is null.', function() {
- cryptolib('asymmetric.signer', function(box) {
- var signerFactory = new box.asymmetric.signer.factory.SignerFactory();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- expect(function() {
- cryptoForgeSigner.sign(_privateKeyPem, null);
- }).toThrow();
- });
- });
- it('throw an error when the array of data contains less than one element.',
- function() {
- cryptolib('asymmetric.signer', function(box) {
- var signerFactory =
- new box.asymmetric.signer.factory.SignerFactory();
- var cryptoForgeSigner = signerFactory.getCryptoSigner();
- var arrayDataBase64 = [];
- expect(function() {
- cryptoForgeSigner.sign(_privateKeyPem, arrayDataBase64);
- }).toThrow();
- });
- });
- it('throw an error when the given algorithm is wrong.', function() {
- var f = function(box) {
- var signerFactory = new box.asymmetric.signer.factory.SignerFactory();
- expect(function() {
- signerFactory.getCryptoSigner();
- }).toThrow();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'WRONG',
- provider: 'Forge',
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', f);
- });
- it('throw an error when the given provider is wrong.', function() {
- var f = function(box) {
- var signerFactory = new box.asymmetric.signer.factory.SignerFactory();
- expect(function() {
- signerFactory.getCryptoSigner();
- }).toThrow();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'CORRECT',
- provider: 'WRONG',
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', f);
- });
- it('throw an error when the given hash algorithm is wrong.', function() {
- var f = function(box) {
- var signerFactory = new box.asymmetric.signer.factory.SignerFactory();
- expect(function() {
- signerFactory.getCryptoSigner();
- }).toThrow();
- };
- f.policies = {
- asymmetric: {
- signer: {
- algorithm: 'RSA',
- hash: 'WRONG',
- provider: 'Forge',
- secureRandom: {provider: 'Scytl'}
- }
- }
- };
- cryptolib('asymmetric.signer', f);
- });
- });
- });
- });
|