index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /* jshint node:true */
  9. 'use strict';
  10. var KeyStoreService = require('./service');
  11. var validator = require('./input-validator');
  12. module.exports = {
  13. /**
  14. * Creates a new KeyStoreService object, which encapsulates a key store
  15. * service.
  16. *
  17. * @function newService
  18. * @global
  19. * @param {Object}
  20. * [options] An object containing optional arguments.
  21. * @param {Policy}
  22. * [options.policy=Default policy] The cryptographic policy to
  23. * use.
  24. * @param {PbkdfRandomService}
  25. * [options.pbkdfRandomService=Created internally] The PBKDF
  26. * service to use.
  27. * @param {SymmetricCryptographyService}
  28. * [options.symmetricCryptographyService=Created internally] The
  29. * symmetric cryptography service to use.
  30. * @param {ElGamalCryptographyService}
  31. * [options.elGamalCryptographyService=Created internally] The
  32. * ElGamal cryptography service to use.
  33. * @returns {KeyStoreService} The new KeyStoreService object.
  34. * @throws {Error}
  35. * If the input data validation fails.
  36. * @example <caption> How to use a cryptographic policy that sets the PBKDF
  37. * derivation key length to 16 bytes</caption>
  38. *
  39. * var cryptoPolicy = require('scytl-cryptopolicy');
  40. * var keyStore = require('scytl-keystore');
  41. *
  42. * var myPolicy = cryptoPolicy.newInstance();
  43. *
  44. * myPolicy.primitives.keyDerivation.pbkdf.keyLengthBytes =
  45. * cryptoPolicy.options.primitives.keyDerivation.pbkdf.keyLengthBytes.KL_16;
  46. *
  47. * var keyStoreService = keyStore.newService({policy: myPolicy});
  48. */
  49. newService: function(options) {
  50. checkData(options);
  51. return new KeyStoreService(options);
  52. }
  53. };
  54. function checkData(options) {
  55. options = options || {};
  56. if (typeof options.policy !== 'undefined') {
  57. validator.checkIsObjectWithProperties(
  58. options.policy, 'Cryptographic policy provided to key store service');
  59. }
  60. if (typeof options.pbkdfService !== 'undefined') {
  61. validator.checkIsObjectWithProperties(
  62. options.pbkdfService,
  63. 'PBKDF service object provided to key store service');
  64. }
  65. if (typeof options.symmetricCryptographyService !== 'undefined') {
  66. validator.checkIsObjectWithProperties(
  67. options.symmetricCryptographyService,
  68. 'Symmetric cryptography service object provided to key store service');
  69. }
  70. if (typeof options.elGamalCryptographyService !== 'undefined') {
  71. validator.checkIsObjectWithProperties(
  72. options.elGamalCryptographyService,
  73. 'ElGamal cryptography service object provided to key store service');
  74. }
  75. }