選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

drbg-test.js 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const assert = require('assert');
  3. const HmacDRBG = require('../');
  4. const hash = require('hash.js');
  5. describe('Hmac_DRBG', () => {
  6. it('should support hmac-drbg-sha256', () => {
  7. function doDrbg(opt) {
  8. const drbg = HmacDRBG({
  9. hash: hash.sha256,
  10. entropy: opt.entropy,
  11. entropyEnc: 'utf8',
  12. nonce: opt.nonce,
  13. nonceEnc: 'utf8',
  14. pers: opt.pers,
  15. persEnc: 'utf8'
  16. });
  17. return drbg.generate(opt.size, 'hex');
  18. }
  19. const test = [
  20. {
  21. entropy: 'totally random0123456789',
  22. nonce: 'secret nonce',
  23. pers: 'my drbg',
  24. size: 32,
  25. res: '018ec5f8e08c41e5ac974eb129ac297c5388ee1864324fa13d9b15cf98d9a157'
  26. },
  27. {
  28. entropy: 'totally random0123456789',
  29. nonce: 'secret nonce',
  30. pers: null,
  31. size: 32,
  32. res: 'ed5d61ecf0ef38258e62f03bbb49f19f2cd07ba5145a840d83b134d5963b3633'
  33. }
  34. ];
  35. for (let i = 0; i < test.length; i++)
  36. assert.equal(doDrbg(test[i]), test[i].res);
  37. });
  38. describe('NIST vector', function() {
  39. require('./fixtures/hmac-drbg-nist.json').forEach(function (opt) {
  40. it('should not fail at ' + opt.name, function() {
  41. const drbg = HmacDRBG({
  42. hash: hash.sha256,
  43. entropy: opt.entropy,
  44. nonce: opt.nonce,
  45. pers: opt.pers
  46. });
  47. let last;
  48. for (let i = 0; i < opt.add.length; i++) {
  49. let add = opt.add[i];
  50. last = drbg.generate(opt.expected.length / 2, 'hex', add);
  51. }
  52. assert.equal(last, opt.expected);
  53. });
  54. });
  55. });
  56. describe('reseeding', function() {
  57. it('should reseed', function() {
  58. const entropy = 'totally random string with many chars that I typed ' +
  59. 'in agony';
  60. const nonce = 'nonce';
  61. const pers = 'pers';
  62. const original = HmacDRBG({
  63. hash: hash.sha256,
  64. entropy,
  65. nonce,
  66. pers
  67. });
  68. const reseeded = HmacDRBG({
  69. hash: hash.sha256,
  70. entropy,
  71. nonce,
  72. pers
  73. });
  74. assert.strictEqual(original.generate(32, 'hex'),
  75. reseeded.generate(32, 'hex'));
  76. reseeded.reseed('another absolutely random string');
  77. assert.notEqual(original.generate(32, 'hex'),
  78. reseeded.generate(32, 'hex'));
  79. });
  80. });
  81. });