test_sodium_auth.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Created by bmf on 10/31/13.
  3. */
  4. "use strict";
  5. var assert = require('assert');
  6. var crypto = require('crypto');
  7. var sodium = require('../build/Release/sodium');
  8. var toBuffer = require('../lib/toBuffer');
  9. var key = Buffer.allocUnsafe(sodium.crypto_auth_KEYBYTES)
  10. key.fill('Jefe');
  11. key.fill(0,4,32);
  12. var c = toBuffer('what do ya want for nothing?', 'ascii');
  13. var key2 = toBuffer('Another one got caught today, it\'s all over the papers. "Teenager Arrested in Computer Crime Scandal", "Hacker Arrested after Bank Tampering"... Damn kids. They\'re all alike.', 'ascii');
  14. var expected1 = toBuffer(
  15. [ 0x16, 0x4b, 0x7a, 0x7b, 0xfc, 0xf8, 0x19, 0xe2,
  16. 0xe3, 0x95, 0xfb, 0xe7, 0x3b, 0x56, 0xe0, 0xa3,
  17. 0x87, 0xbd, 0x64, 0x22, 0x2e, 0x83, 0x1f, 0xd6,
  18. 0x10, 0x27, 0x0c, 0xd7, 0xea, 0x25, 0x05, 0x54 ]);
  19. var expected2 = toBuffer(
  20. [ 0x7b, 0x9d, 0x83, 0x38, 0xeb, 0x1e, 0x3d, 0xdd,
  21. 0xba, 0x8a, 0x9a, 0x35, 0x08, 0xd0, 0x34, 0xa1,
  22. 0xec, 0xbe, 0x75, 0x11, 0x37, 0xfa, 0x1b, 0xcb,
  23. 0xa0, 0xf9, 0x2a, 0x3e, 0x6d, 0xfc, 0x79, 0x80,
  24. 0xb8, 0x81, 0xa8, 0x64, 0x5f, 0x92, 0x67, 0x22,
  25. 0x74, 0x37, 0x96, 0x4b, 0xf3, 0x07, 0x0b, 0xe2,
  26. 0xb3, 0x36, 0xb3, 0xa3, 0x20, 0xf8, 0x25, 0xce,
  27. 0xc9, 0x87, 0x2d, 0xb2, 0x50, 0x4b, 0xf3, 0x6d ]);
  28. var expected3 = toBuffer(
  29. [ 0x73, 0xe0, 0x0d, 0xcb, 0xf4, 0xf8, 0xa3, 0x33,
  30. 0x30, 0xac, 0x52, 0xed, 0x2c, 0xc9, 0xd1, 0xb2,
  31. 0xef, 0xb1, 0x77, 0x13, 0xd3, 0xec, 0xe3, 0x96,
  32. 0x14, 0x9f, 0x37, 0x65, 0x3c, 0xfe, 0x70, 0xe7,
  33. 0x1f, 0x2c, 0x6f, 0x9a, 0x62, 0xc3, 0xc5, 0x3a,
  34. 0x31, 0x8a, 0x9a, 0x0b, 0x3b, 0x78, 0x60, 0xa4,
  35. 0x31, 0x6f, 0x72, 0x9b, 0x8d, 0x30, 0x0f, 0x15,
  36. 0x9b, 0x2f, 0x60, 0x93, 0xa8, 0x60, 0xc1, 0xed ]);
  37. var a = Buffer.allocUnsafe(sodium.crypto_auth_BYTES);
  38. var a2 = Buffer.allocUnsafe(sodium.crypto_auth_hmacsha512_BYTES);
  39. describe('LibSodium Auth', function() {
  40. it('crypto_auth should return the expected auth token', function(done) {
  41. var authToken = sodium.crypto_auth(c, key);
  42. assert.deepEqual(authToken, expected1);
  43. assert.equal(authToken.length, sodium.crypto_auth_BYTES);
  44. done();
  45. });
  46. it('crypto_auth_hmacsha512_* = crypto_auth_hmacsha512', function(done) {
  47. // Split the message in half
  48. var halfLength = Math.ceil(c.length / 2);
  49. var c1 = c.slice(0, halfLength);
  50. var c2 = c.slice(halfLength, c.length);
  51. var s1 = sodium.crypto_auth_hmacsha512_init(key);
  52. var s2 = sodium.crypto_auth_hmacsha512_update(s1, c1);
  53. var s3 = sodium.crypto_auth_hmacsha512_update(s2, c2);
  54. var a1 = sodium.crypto_auth_hmacsha512_final(s3);
  55. var a2 = sodium.crypto_auth_hmacsha512(c, key);
  56. // Assert that the states changed with each update
  57. assert.notDeepEqual(s1, s2);
  58. assert.notDeepEqual(s2, s3);
  59. assert.notDeepEqual(s1, s3);
  60. // Assert that it matches what we expected
  61. assert.deepEqual(a1, a2);
  62. // Is it the right token length
  63. assert.equal(a1.length, sodium.crypto_auth_hmacsha512_BYTES);
  64. assert.equal(a2.length, sodium.crypto_auth_hmacsha512_BYTES);
  65. done();
  66. });
  67. /* THIS TEST IS DISABLED DUE TO A LIBSODIUM BUG
  68. it('crypto_auth_hmacsha512_* = crypto_auth_hmacsha512', function(done) {
  69. // Split the message in half
  70. var c1 = c.slice(0, 1);
  71. var c2 = c.slice(0, c.length -1);
  72. var s1 = sodium.crypto_auth_hmacsha512_init(key2);
  73. var s2 = sodium.crypto_auth_hmacsha512_update(s1, c1);
  74. var s3 = sodium.crypto_auth_hmacsha512_update(s2, c2);
  75. var a1 = sodium.crypto_auth_hmacsha512_final(s3);
  76. // Assert that the states changed with each update
  77. assert.notDeepEqual(s1, s2);
  78. assert.notDeepEqual(s2, s3);
  79. assert.notDeepEqual(s1, s3);
  80. // Assert that it matches what we expected
  81. assert.deepEqual(a1, expected2);
  82. // Is it the right token length
  83. assert.equal(a1.length, sodium.crypto_auth_hmacsha512_BYTES);
  84. done();
  85. });
  86. */
  87. });