tools.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 forge = require('node-forge');
  11. module.exports = Tools;
  12. function Tools() {
  13. var reseedDigester_;
  14. var updateDigester_;
  15. this.cipher = function(key, counter) {
  16. var aes_output = [];
  17. forge.aes._updateBlock(
  18. formatKey(key), formatSeed('' + counter), aes_output, false);
  19. var aes_buffer = forge.util.createBuffer();
  20. aes_buffer.putInt32(aes_output[0]);
  21. aes_buffer.putInt32(aes_output[1]);
  22. aes_buffer.putInt32(aes_output[2]);
  23. aes_buffer.putInt32(aes_output[3]);
  24. return aes_buffer.getBytes();
  25. };
  26. this.getReseedDigester = function() {
  27. if (typeof reseedDigester_ === 'undefined') {
  28. reseedDigester_ = forge.md.sha256.create();
  29. }
  30. return reseedDigester_;
  31. };
  32. this.getUpdateDigester = function() {
  33. if (typeof updateDigester_ === 'undefined') {
  34. updateDigester_ = forge.md.sha256.create();
  35. }
  36. return updateDigester_;
  37. };
  38. }
  39. function formatKey(key) {
  40. // Convert key into 32 bit integers.
  41. var tmp = forge.util.createBuffer(key);
  42. key = [
  43. tmp.getInt32(),
  44. tmp.getInt32(),
  45. tmp.getInt32(),
  46. tmp.getInt32(),
  47. tmp.getInt32(),
  48. tmp.getInt32(),
  49. tmp.getInt32(),
  50. tmp.getInt32()
  51. ];
  52. // return the expanded key
  53. return forge.aes._expandKey(key, false);
  54. }
  55. function formatSeed(seed) {
  56. // Convert seed into 32 bit integers.
  57. var tmp = forge.util.createBuffer(seed);
  58. seed = [
  59. tmp.getInt32(),
  60. tmp.getInt32(),
  61. tmp.getInt32(),
  62. tmp.getInt32()
  63. ];
  64. return seed;
  65. }