symmetric.mac.spec.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // Test HMAC message digester with SHA-256 hash function.
  9. describe('HmacDigest with SHA-256 hash function ...', function() {
  10. 'use strict';
  11. var _testString = 'Ox2fUJq1gAbX';
  12. var _hmacSecretKeyBase64 = 'lFoxPuafE3Kk4jcqqHAatdwfsRS8ZcaF9N4gg+85KoA=';
  13. var _hmacMessageDigestBase64 = '6qiFgZH6VWXti+ltAqmBO9CryybDJC6oHDJsD4bxVlI=';
  14. describe('should be able to ..', function() {
  15. it('verify identity when using identical data.', function() {
  16. cryptolib('symmetric.mac', 'commons.utils', function(box) {
  17. var macFactory = new box.symmetric.mac.factory.MacFactory();
  18. var converters = new box.commons.utils.Converters();
  19. var hmacSha256Digester = macFactory.create();
  20. var dataB64 = converters.base64Encode(_testString);
  21. var arrayDataB64 = [dataB64];
  22. var macB64 =
  23. hmacSha256Digester.generate(_hmacSecretKeyBase64, arrayDataB64);
  24. var verified = hmacSha256Digester.verify(
  25. macB64, _hmacSecretKeyBase64, arrayDataB64);
  26. // Verify that new message digest is equal to original.
  27. expect(verified).toBeTruthy();
  28. });
  29. });
  30. it('generate identical message digests when using identical data.',
  31. function() {
  32. cryptolib('symmetric.mac', 'commons.utils', function(box) {
  33. var macFactory = new box.symmetric.mac.factory.MacFactory();
  34. var converters = new box.commons.utils.Converters();
  35. var hmacSha256Digester = macFactory.create();
  36. var dataB64 = converters.base64Encode(_testString);
  37. var arrayDataB64 = [dataB64];
  38. var macB64_1 =
  39. hmacSha256Digester.generate(_hmacSecretKeyBase64, arrayDataB64);
  40. var macB64_2 =
  41. hmacSha256Digester.generate(_hmacSecretKeyBase64, arrayDataB64);
  42. // Verify that new message digest is equal to original.
  43. expect(macB64_1).toBe(macB64_2);
  44. });
  45. });
  46. });
  47. it('generate different message digests when using different data.',
  48. function() {
  49. cryptolib('symmetric.mac', 'commons.utils', function(box) {
  50. var macFactory = new box.symmetric.mac.factory.MacFactory();
  51. var converters = new box.commons.utils.Converters();
  52. var hmacSha256Digester = macFactory.create();
  53. var dataB64 = converters.base64Encode(_testString);
  54. var arrayDataB64 = [dataB64];
  55. var macB64_1 =
  56. hmacSha256Digester.generate(_hmacSecretKeyBase64, arrayDataB64);
  57. dataB64 = converters.base64Encode(_testString + 'x');
  58. arrayDataB64 = [dataB64];
  59. var macB64_2 =
  60. hmacSha256Digester.generate(_hmacSecretKeyBase64, arrayDataB64);
  61. // Verify that new message digest is equal to original.
  62. expect(macB64_1).not.toBe(macB64_2);
  63. });
  64. });
  65. it('generate message digest identical to that generated using Java HMACwithSHA256.',
  66. function() {
  67. cryptolib('symmetric.mac', 'commons.utils', function(box) {
  68. var macFactory = new box.symmetric.mac.factory.MacFactory();
  69. var converters = new box.commons.utils.Converters();
  70. var hmacSha256Digester = macFactory.create();
  71. var dataB64 = converters.base64Encode(_testString);
  72. var arrayDataB64 = [dataB64];
  73. var macB64 =
  74. hmacSha256Digester.generate(_hmacSecretKeyBase64, arrayDataB64);
  75. // Verify that new message digest is equal to original.
  76. expect(macB64).toBe(_hmacMessageDigestBase64);
  77. });
  78. });
  79. it('throw an error when the the given data to digest is incorrect.',
  80. function() {
  81. cryptolib('symmetric.mac', 'commons.utils', function(box) {
  82. var macFactory = new box.symmetric.mac.factory.MacFactory();
  83. var hmacSha256Digester = macFactory.create();
  84. expect(function() {
  85. hmacSha256Digester.generate(_hmacSecretKeyBase64, []);
  86. }).toThrow();
  87. });
  88. });
  89. it('throw an error when the given hash algorithm is wrong.', function() {
  90. var f = function(box) {
  91. var macFactory = new box.symmetric.mac.factory.MacFactory();
  92. var hmacSha256Digester = macFactory.create();
  93. var converters = new box.commons.utils.Converters();
  94. var dataB64 = converters.base64Encode(_testString);
  95. var arrayDataB64 = [dataB64];
  96. expect(function() {
  97. hmacSha256Digester.generate(_hmacSecretKeyBase64, arrayDataB64);
  98. }).toThrow();
  99. };
  100. f.policies = {symmetric: {mac: {hash: 'WRONG', provider: 'Forge'}}};
  101. cryptolib('symmetric.mac', 'commons.utils', f);
  102. });
  103. it('throw an error when the given provider is wrong.', function() {
  104. var f = function(box) {
  105. var macFactory = new box.symmetric.mac.factory.MacFactory();
  106. expect(function() {
  107. macFactory.create();
  108. }).toThrow();
  109. };
  110. f.policies = {symmetric: {mac: {hash: 'SHA256', provider: 'WRONG'}}};
  111. cryptolib('symmetric.mac', 'commons.utils', f);
  112. });
  113. it('generates a valid MAC', function() {
  114. var originalMessage = 'test';
  115. var secretKeyB64fromHTML = 'lFoxPuafE3Kk4jcqqHAatdwfsRS8ZcaF9N4gg+85KoA=';
  116. cryptolib('symmetric.mac', 'commons.utils', function(box) {
  117. var macFactory = new box.symmetric.mac.factory.MacFactory();
  118. var converters = new box.commons.utils.Converters();
  119. var hmacSha256Digester = macFactory.create();
  120. var dataB64 = converters.base64Encode(originalMessage);
  121. var arrayDataB64 = [dataB64];
  122. var macB64 =
  123. hmacSha256Digester.generate(secretKeyB64fromHTML, arrayDataB64);
  124. expect(macB64).toBe('px+hdJi/ucaNctN2yM9ZIGE9/mvK1ZhmOGvsZ19bSRI=');
  125. });
  126. });
  127. it('generates a valid MAC with special characters', function() {
  128. var originalMessage = 'tést';
  129. var secretKeyB64fromHTML = 'lFoxPuafE3Kk4jcqqHAatdwfsRS8ZcaF9N4gg+85KoA=';
  130. cryptolib('symmetric.mac', 'commons.utils', function(box) {
  131. var macFactory = new box.symmetric.mac.factory.MacFactory();
  132. var converters = new box.commons.utils.Converters();
  133. var hmacSha256Digester = macFactory.create();
  134. var dataB64 = converters.base64Encode(originalMessage);
  135. var arrayDataB64 = [dataB64];
  136. var macB64 =
  137. hmacSha256Digester.generate(secretKeyB64fromHTML, arrayDataB64);
  138. expect(macB64).toBe('6zRTAMeZqMvJaBCtwewm19R5ECQ4manZnk8uI4oOWus=');
  139. });
  140. });
  141. });