asymmetric.keypair.spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. describe('Crypto Encryption Key Pair Generator...', function() {
  9. 'use strict';
  10. describe('of the FORGE provider ..', function() {
  11. describe('should be able to ..', function() {
  12. it('be defined', function() {
  13. cryptolib('asymmetric.keypair', function(box) {
  14. var keyPairGeneratorFactory =
  15. new box.asymmetric.keypair.factory.KeyPairGeneratorFactory();
  16. var encryptionKeyPairGenerator =
  17. keyPairGeneratorFactory.getEncryptionCryptoKeyPairGenerator();
  18. expect(encryptionKeyPairGenerator).toBeDefined();
  19. });
  20. });
  21. it('Generate RSA key pair for encryption', function() {
  22. // Note: Key size here is set to value lower than minimum value allowed
  23. // by policy in order to keep test time reasonable.
  24. var keyBitLength = 1024;
  25. var f = function(box) {
  26. var keyPairGeneratorFactory =
  27. new box.asymmetric.keypair.factory.KeyPairGeneratorFactory();
  28. var encryptionKeyPairGenerator =
  29. keyPairGeneratorFactory.getEncryptionCryptoKeyPairGenerator();
  30. var cryptoEncryptionKeyPair = encryptionKeyPairGenerator.generate();
  31. var publicKeyPem = (cryptoEncryptionKeyPair.getPublicKey()).getPem();
  32. var privateKeyPem =
  33. (cryptoEncryptionKeyPair.getPrivateKey()).getPem();
  34. var publicKey = box.forge.pki.publicKeyFromPem(publicKeyPem);
  35. var privateKey = box.forge.pki.privateKeyFromPem(privateKeyPem);
  36. expect(publicKey.n.bitLength()).toBe(keyBitLength);
  37. expect(privateKey.n.bitLength()).toBe(keyBitLength);
  38. };
  39. f.policies = {
  40. asymmetric: {
  41. keypair: {
  42. encryption: {
  43. algorithm: Config.asymmetric.keypair.encryption.algorithm.RSA,
  44. provider: Config.asymmetric.keypair.encryption.provider.FORGE,
  45. keyLength: keyBitLength,
  46. publicExponent:
  47. Config.asymmetric.keypair.encryption.publicExponent.F4
  48. },
  49. secureRandom: {
  50. provider: Config.asymmetric.keypair.secureRandom.provider.SCYTL
  51. }
  52. }
  53. }
  54. };
  55. cryptolib('asymmetric.keypair', f);
  56. });
  57. it('throw an error when the given algorithm is wrong.', function() {
  58. var f = function(box) {
  59. var keyPairGeneratorFactory =
  60. new box.asymmetric.keypair.factory.KeyPairGeneratorFactory();
  61. expect(function() {
  62. keyPairGeneratorFactory.getEncryptionCryptoKeyPairGenerator()
  63. .generate();
  64. }).toThrow();
  65. };
  66. f.policies = {
  67. asymmetric: {
  68. keypair: {
  69. encryption: {algorithm: 'WRONG', provider: 'Forge'},
  70. secureRandom: {provider: 'Scytl'}
  71. }
  72. }
  73. };
  74. cryptolib('asymmetric.keypair', f);
  75. });
  76. it('throw an error when the given provider is wrong.', function() {
  77. var f = function(box) {
  78. var keyPairGeneratorFactory =
  79. new box.asymmetric.keypair.factory.KeyPairGeneratorFactory();
  80. expect(function() {
  81. keyPairGeneratorFactory.getEncryptionCryptoKeyPairGenerator()
  82. .generate();
  83. }).toThrow();
  84. };
  85. f.policies = {
  86. asymmetric: {
  87. keypair: {
  88. encryption: {algorithm: 'CORRECT', provider: 'WRONG'},
  89. secureRandom: {provider: 'Scytl'}
  90. }
  91. }
  92. };
  93. cryptolib('asymmetric.keypair', f);
  94. });
  95. });
  96. });
  97. });
  98. describe('Crypto Encryption Key Pair ...', function() {
  99. 'use strict';
  100. describe('should be able to ..', function() {
  101. it('be defined', function() {
  102. cryptolib('asymmetric.keypair', function(box) {
  103. var keyPairGeneratorFactory =
  104. new box.asymmetric.keypair.factory.KeyPairGeneratorFactory();
  105. var encryptionKeyPairGenerator =
  106. keyPairGeneratorFactory.getEncryptionCryptoKeyPairGenerator();
  107. var encryptionKeyPair = encryptionKeyPairGenerator.generate();
  108. expect(encryptionKeyPair).toBeDefined();
  109. });
  110. });
  111. });
  112. });