2
0

symmetric.secretkey.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. cryptolib.modules.symmetric = cryptolib.modules.symmetric || {};
  9. /**
  10. * Defines a symmetric secret key
  11. *
  12. * @namespace symmetric/secretkey
  13. */
  14. cryptolib.modules.symmetric.secretkey = function(box) {
  15. 'use strict';
  16. box.symmetric = box.symmetric || {};
  17. box.symmetric.secretkey = {};
  18. /**
  19. * Allows a symmetric secret key factory to be obtained.
  20. *
  21. * @exports symmetric/secretkey/factory
  22. */
  23. box.symmetric.secretkey.factory = {};
  24. var policies = {
  25. secretkey: {
  26. encryption: {
  27. lengthBytes: box.policies.symmetric.secretkey.encryption.lengthBytes
  28. },
  29. mac: {lengthBytes: box.policies.symmetric.secretkey.mac.lengthBytes}
  30. }
  31. };
  32. var utils, randomFactory, exceptions, secureRandom, lengthBytes;
  33. var f = function(box) {
  34. utils = box.commons.utils;
  35. exceptions = box.commons.exceptions;
  36. randomFactory =
  37. new box.primitives.securerandom.factory.SecureRandomFactory();
  38. };
  39. f.policies = {
  40. primitives: {
  41. secureRandom:
  42. {provider: box.policies.symmetric.secretkey.secureRandom.provider}
  43. }
  44. };
  45. cryptolib('commons', 'primitives.securerandom', f);
  46. /**
  47. * Validates the received length argument, in bytes.
  48. *
  49. * Confirms that:
  50. * <ul>
  51. * <li>the type of <code>lengthBytes</code> is number.</li>
  52. * <li><code>lengthBytes</code> is an integer with a value not less than
  53. * 1.</li>
  54. * </ul>
  55. *
  56. * @function validateParameter
  57. * @param lengthBytes
  58. * {number} the length, in bytes, to be validated.
  59. */
  60. var validateParameter = function(lengthBytes) {
  61. if (typeof lengthBytes !== 'number' || lengthBytes % 1 !== 0 ||
  62. lengthBytes < 1) {
  63. throw new exceptions.CryptoLibException(
  64. 'The given key length is not valid.');
  65. }
  66. };
  67. /**
  68. * Represents a secret key factory.
  69. *
  70. * @class
  71. */
  72. box.symmetric.secretkey.factory.SecretKeyFactory = function() {
  73. secureRandom = randomFactory.getCryptoRandomBytes();
  74. };
  75. box.symmetric.secretkey.factory.SecretKeyFactory.prototype = {
  76. /**
  77. * Get a CryptoSecretKeyGenerator for encryption.
  78. *
  79. * @function
  80. * @returns {symmetric/secretkey.CryptoSecretKeyGenerator} a
  81. * CryptoSecretKeyGenerator for encryption.
  82. */
  83. getCryptoSecretKeyGeneratorForEncryption: function() {
  84. lengthBytes = policies.secretkey.encryption.lengthBytes;
  85. return this.getCryptoSecretKeyGenerator();
  86. },
  87. /**
  88. * Get a CryptoSecretKeyGenerator for MAC.
  89. *
  90. * @function
  91. * @returns {symmetric/secretkey.CryptoSecretKeyGenerator} a
  92. * CryptoSecretKeyGenerator for MAC.
  93. */
  94. getCryptoSecretKeyGeneratorForMac: function() {
  95. lengthBytes = policies.secretkey.mac.lengthBytes;
  96. return this.getCryptoSecretKeyGenerator();
  97. },
  98. /**
  99. * Create a new CryptoSecretKeyGenerator.
  100. *
  101. * @function
  102. * @returns {symmetric/secretkey.CryptoSecretKeyGenerator} a
  103. * CryptoSecretKeyGenerator.
  104. */
  105. getCryptoSecretKeyGenerator: function() {
  106. return new CryptoSecretKeyGenerator(secureRandom);
  107. }
  108. };
  109. /**
  110. * Defines a Secret Key generator.
  111. *
  112. * @class
  113. * @memberof symmetric/secretkey
  114. * @param randomGenerator
  115. * {CryptoScytlRandomBytes} a secure random bytes generator.
  116. */
  117. function CryptoSecretKeyGenerator(randomGenerator) {
  118. this.converters = new utils.Converters();
  119. this.randomGenerator = randomGenerator;
  120. }
  121. CryptoSecretKeyGenerator.prototype = {
  122. /**
  123. * Generates a secret key.
  124. *
  125. * @function
  126. * @return A secret key, as string in Base64 encoded format.
  127. */
  128. generate: function() {
  129. try {
  130. validateParameter(lengthBytes);
  131. var key = this.randomGenerator.nextRandom(lengthBytes);
  132. var keyBase64 = this.converters.base64Encode(key);
  133. return keyBase64;
  134. } catch (error) {
  135. throw new exceptions.CryptoLibException(
  136. 'CryptoSecretKey could not be generated.', error);
  137. }
  138. }
  139. };
  140. };