2
0

symmetric.secretkey.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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('CryptoSecretKeyGenerator ...', function() {
  9. 'use strict';
  10. describe('should be able to ..', function() {
  11. it('generate encryption keys of the given length.', function() {
  12. cryptolib('symmetric.secretkey', 'commons', function(box) {
  13. var secretKeyFactory =
  14. new box.symmetric.secretkey.factory.SecretKeyFactory();
  15. var cryptoSecretKeyGenerator =
  16. secretKeyFactory.getCryptoSecretKeyGeneratorForEncryption();
  17. var converters = new box.commons.utils.Converters();
  18. // Generate key of a given length.
  19. var cryptoSecretKey = cryptoSecretKeyGenerator.generate();
  20. expect(cryptoSecretKey).toBeDefined();
  21. // Base64 decode encrypted data and decrypt data.
  22. var secretKey = converters.base64Decode(cryptoSecretKey);
  23. // Verify that the secret key is of length 10
  24. expect(
  25. secretKey.length ===
  26. box.policies.symmetric.secretkey.encryption.lengthBytes)
  27. .toBeTruthy();
  28. });
  29. });
  30. it('generate secret keys for mac of the given length.', function() {
  31. cryptolib('symmetric.secretkey', 'commons', function(box) {
  32. var secretKeyFactory =
  33. new box.symmetric.secretkey.factory.SecretKeyFactory();
  34. var cryptoSecretKeyGenerator =
  35. secretKeyFactory.getCryptoSecretKeyGeneratorForMac();
  36. var converters = new box.commons.utils.Converters();
  37. // Generate key of a given length.
  38. var cryptoSecretKey = cryptoSecretKeyGenerator.generate();
  39. expect(cryptoSecretKey).toBeDefined();
  40. // Base64 decode encrypted data and decrypt data.
  41. var secretKey = converters.base64Decode(cryptoSecretKey);
  42. // Verify that the secret key is of length 10
  43. expect(
  44. secretKey.length ===
  45. box.policies.symmetric.secretkey.mac.lengthBytes)
  46. .toBeTruthy();
  47. });
  48. });
  49. it('throw an error when the given length for encryption is not a number.',
  50. function() {
  51. var f = function(box) {
  52. var secretKeyFactory =
  53. new box.symmetric.secretkey.factory.SecretKeyFactory();
  54. var cryptoSecretKeyGenerator =
  55. secretKeyFactory.getCryptoSecretKeyGeneratorForEncryption();
  56. var thrownError = false;
  57. try {
  58. // Generate key of a given length.
  59. cryptoSecretKeyGenerator.generate();
  60. } catch (error) {
  61. thrownError = true;
  62. }
  63. expect(thrownError).toBeTruthy();
  64. };
  65. f.policies = {
  66. symmetric: {
  67. secretkey: {
  68. encryption: {lengthBytes: 'AAA'},
  69. mac: {lengthBytes: 'AAA'},
  70. secureRandom: {provider: 'Scytl'}
  71. }
  72. }
  73. };
  74. cryptolib('symmetric.secretkey', f);
  75. });
  76. it('throw an error when the given length for encryption is not within the accepted range of values.',
  77. function() {
  78. var f = function(box) {
  79. var secretKeyFactory =
  80. new box.symmetric.secretkey.factory.SecretKeyFactory();
  81. var cryptoSecretKeyGenerator =
  82. secretKeyFactory.getCryptoSecretKeyGeneratorForEncryption();
  83. var thrownError = false;
  84. try {
  85. // Generate key of a given length.
  86. cryptoSecretKeyGenerator.generate();
  87. } catch (error) {
  88. thrownError = true;
  89. }
  90. expect(thrownError).toBeTruthy();
  91. };
  92. f.policies = {
  93. symmetric: {
  94. secretkey: {
  95. encryption: {lengthBytes: -1},
  96. mac: {lengthBytes: -1},
  97. secureRandom: {provider: 'Scytl'}
  98. }
  99. }
  100. };
  101. cryptolib('symmetric.secretkey', f);
  102. });
  103. });
  104. });