test_crypto_aead.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. var assert = require('assert');
  2. var sodium = require('../build/Release/sodium');
  3. var assert = require('assert');
  4. describe("AEAD", function () {
  5. it("aes256gcm should encrypt and decrypt to the same string", function (done) {
  6. var message = Buffer.from("This is a plain text message");
  7. var additionalData = Buffer.from("this is metadata");
  8. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_NPUBBYTES);
  9. sodium.randombytes_buf(nonce);
  10. var key = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_KEYBYTES);
  11. sodium.randombytes_buf(key);
  12. // If CPU does not support AES256gcm don't test
  13. if( !sodium.crypto_aead_aes256gcm_is_available() ) {
  14. console.log('AES 256 gcm not supported by CPU');
  15. done();
  16. return;
  17. }
  18. // Encrypt data
  19. var cipherText = sodium.crypto_aead_aes256gcm_encrypt(message, additionalData, nonce, key);
  20. // Decrypt Data
  21. var plainText = sodium.crypto_aead_aes256gcm_decrypt(cipherText, additionalData, nonce, key);
  22. // Test equality
  23. assert(sodium.compare(plainText, message)==0);
  24. done();
  25. });
  26. it("aes256gcm should encrypt and decrypt to the same string with null additional data", function (done) {
  27. var message = Buffer.from("This is a plain text message");
  28. var additionalData = Buffer.from("this is metadata");
  29. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_NPUBBYTES);
  30. sodium.randombytes_buf(nonce);
  31. var key = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_KEYBYTES);
  32. sodium.randombytes_buf(key);
  33. // If CPU does not support AES256gcm don't test
  34. if( !sodium.crypto_aead_aes256gcm_is_available() ) {
  35. console.log('AES 256 gcm not supported by CPU');
  36. done();
  37. return;
  38. }
  39. // Encrypt data
  40. var cipherText = sodium.crypto_aead_aes256gcm_encrypt(message, null, nonce, key);
  41. // Decrypt Data
  42. var plainText = sodium.crypto_aead_aes256gcm_decrypt(cipherText, null, nonce, key);
  43. // Test equality
  44. assert(sodium.compare(plainText, message)==0);
  45. done();
  46. });
  47. it("chacha20poly1305 should encrypt and decrypt to the same string", function (done) {
  48. var message = Buffer.from("This is a plain text message");
  49. var additionalData = Buffer.from("this is metadata");
  50. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_NPUBBYTES);
  51. sodium.randombytes_buf(nonce);
  52. var key = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_KEYBYTES);
  53. sodium.randombytes_buf(key);
  54. // Encrypt data
  55. var cipherText = sodium.crypto_aead_chacha20poly1305_encrypt(message, additionalData, nonce, key);
  56. // Decrypt Data
  57. var plainText = sodium.crypto_aead_chacha20poly1305_decrypt(cipherText, additionalData, nonce, key);
  58. // Test equality
  59. assert(sodium.compare(plainText, message)==0);
  60. done();
  61. });
  62. it("chacha20poly1305 should encrypt and decrypt to the same string with null additional data", function (done) {
  63. var message = Buffer.from("This is a plain text message");
  64. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_NPUBBYTES);
  65. sodium.randombytes_buf(nonce);
  66. var key = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_KEYBYTES);
  67. sodium.randombytes_buf(key);
  68. // Encrypt data
  69. var cipherText = sodium.crypto_aead_chacha20poly1305_encrypt(message, null, nonce, key);
  70. // Decrypt Data
  71. var plainText = sodium.crypto_aead_chacha20poly1305_decrypt(cipherText, null, nonce, key);
  72. // Test equality
  73. assert(sodium.compare(plainText, message)==0);
  74. done();
  75. });
  76. it("chacha20poly1305_ietf should encrypt and decrypt to the same string", function (done) {
  77. var message = Buffer.from("This is a plain text message");
  78. var additionalData = Buffer.from("this is metadata");
  79. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_ietf_NPUBBYTES);
  80. sodium.randombytes_buf(nonce);
  81. var key = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_ietf_KEYBYTES);
  82. sodium.randombytes_buf(key);
  83. // Encrypt data
  84. var cipherText = sodium.crypto_aead_chacha20poly1305_ietf_encrypt(message, additionalData, nonce, key);
  85. // Decrypt Data
  86. var plainText = sodium.crypto_aead_chacha20poly1305_ietf_decrypt(cipherText, additionalData, nonce, key);
  87. // Test equality
  88. assert(sodium.compare(plainText, message)==0);
  89. done();
  90. });
  91. it("chacha20poly1305_ietf should encrypt and decrypt to the same string with null additional data", function (done) {
  92. var message = Buffer.from("This is a plain text message");
  93. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_ietf_NPUBBYTES);
  94. sodium.randombytes_buf(nonce);
  95. var key = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_ietf_KEYBYTES);
  96. sodium.randombytes_buf(key);
  97. // Encrypt data
  98. var cipherText = sodium.crypto_aead_chacha20poly1305_ietf_encrypt(message, null, nonce, key);
  99. // Decrypt Data
  100. var plainText = sodium.crypto_aead_chacha20poly1305_ietf_decrypt(cipherText, null, nonce, key);
  101. // Test equality
  102. assert(sodium.compare(plainText, message)==0);
  103. done();
  104. });
  105. it("aes256gcm should encrypt and decrypt to the same string detached", function (done) {
  106. var message = Buffer.from("This is a plain text message");
  107. var additionalData = Buffer.from("this is metadata");
  108. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_NPUBBYTES);
  109. sodium.randombytes_buf(nonce);
  110. var key = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_KEYBYTES);
  111. sodium.randombytes_buf(key);
  112. // If CPU does not support AES256gcm don't test
  113. if( !sodium.crypto_aead_aes256gcm_is_available() ) {
  114. console.log('AES 256 gcm not supported by CPU');
  115. done();
  116. return;
  117. }
  118. // Encrypt data
  119. var c = sodium.crypto_aead_aes256gcm_encrypt_detached(message, additionalData, nonce, key);
  120. // Decrypt Data
  121. var plainText = sodium.crypto_aead_aes256gcm_decrypt_detached(c.cipherText, c.mac, additionalData, nonce, key);
  122. // Test equality
  123. assert(sodium.compare(plainText, message)==0);
  124. done();
  125. });
  126. it("chacha20poly1305 should encrypt and decrypt to the same string detached", function (done) {
  127. var message = Buffer.from("This is a plain text message");
  128. var additionalData = Buffer.from("this is metadata");
  129. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_NPUBBYTES);
  130. sodium.randombytes_buf(nonce);
  131. var key = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_KEYBYTES);
  132. sodium.randombytes_buf(key);
  133. // Encrypt data
  134. var c = sodium.crypto_aead_chacha20poly1305_encrypt_detached(message, additionalData, nonce, key);
  135. // Decrypt Data
  136. var plainText = sodium.crypto_aead_chacha20poly1305_decrypt_detached(c.cipherText, c.mac, additionalData, nonce, key);
  137. // Test equality
  138. assert(sodium.compare(plainText, message)==0);
  139. done();
  140. });
  141. it("chacha20poly1305_ietf should encrypt and decrypt to the same string detached", function (done) {
  142. var message = Buffer.from("This is a plain text message");
  143. var additionalData = Buffer.from("this is metadata");
  144. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_ietf_NPUBBYTES);
  145. sodium.randombytes_buf(nonce);
  146. var key = Buffer.allocUnsafe(sodium.crypto_aead_chacha20poly1305_ietf_KEYBYTES);
  147. sodium.randombytes_buf(key);
  148. // Encrypt data
  149. var c = sodium.crypto_aead_chacha20poly1305_ietf_encrypt_detached(message, additionalData, nonce, key);
  150. // Decrypt Data
  151. var plainText = sodium.crypto_aead_chacha20poly1305_ietf_decrypt_detached(c.cipherText, c.mac, additionalData, nonce, key);
  152. // Test equality
  153. assert(sodium.compare(plainText, message)==0);
  154. done();
  155. });
  156. });
  157. describe("AEAD Precompute Interface", function () {
  158. it("aes256gcm should encrypt and decrypt to the same string", function (done) {
  159. var message = Buffer.from("This is a plain text message");
  160. var additionalData = Buffer.from("this is metadata");
  161. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_NPUBBYTES);
  162. sodium.randombytes_buf(nonce);
  163. var key = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_KEYBYTES);
  164. sodium.randombytes_buf(key);
  165. var ctx = sodium.crypto_aead_aes256gcm_beforenm(key);
  166. // If CPU does not support AES256gcm don't test
  167. if( !sodium.crypto_aead_aes256gcm_is_available() ) {
  168. console.log('AES 256 gcm not supported by CPU');
  169. done();
  170. return;
  171. }
  172. // Encrypt data
  173. var cipherText = sodium.crypto_aead_aes256gcm_encrypt_afternm(message, additionalData, nonce, ctx);
  174. // Decrypt Data
  175. var plainText = sodium.crypto_aead_aes256gcm_decrypt_afternm(cipherText, additionalData, nonce, ctx);
  176. // Test equality
  177. assert(sodium.compare(plainText, message)==0);
  178. done();
  179. });
  180. it("aes256gcm should encrypt and decrypt to the same string with null additional data", function (done) {
  181. var message = Buffer.from("This is a plain text message");
  182. var additionalData = Buffer.from("this is metadata");
  183. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_NPUBBYTES);
  184. sodium.randombytes_buf(nonce);
  185. var key = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_KEYBYTES);
  186. sodium.randombytes_buf(key);
  187. var ctx = sodium.crypto_aead_aes256gcm_beforenm(key);
  188. // If CPU does not support AES256gcm don't test
  189. if( !sodium.crypto_aead_aes256gcm_is_available() ) {
  190. console.log('AES 256 gcm not supported by CPU');
  191. done();
  192. return;
  193. }
  194. // Encrypt data
  195. var cipherText = sodium.crypto_aead_aes256gcm_encrypt_afternm(message, null, nonce, ctx);
  196. // Decrypt Data
  197. var plainText = sodium.crypto_aead_aes256gcm_decrypt_afternm(cipherText, null, nonce, ctx);
  198. // Test equality
  199. assert(sodium.compare(plainText, message)==0);
  200. done();
  201. });
  202. it("aes256gcm should encrypt and decrypt to the same string detached", function (done) {
  203. var message = Buffer.from("This is a plain text message");
  204. var additionalData = Buffer.from("this is metadata");
  205. var nonce = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_NPUBBYTES);
  206. sodium.randombytes_buf(nonce);
  207. var key = Buffer.allocUnsafe(sodium.crypto_aead_aes256gcm_KEYBYTES);
  208. sodium.randombytes_buf(key);
  209. var ctx = sodium.crypto_aead_aes256gcm_beforenm(key);
  210. // If CPU does not support AES256gcm don't test
  211. if( !sodium.crypto_aead_aes256gcm_is_available() ) {
  212. console.log('AES 256 gcm not supported by CPU');
  213. done();
  214. return;
  215. }
  216. // Encrypt data
  217. var c = sodium.crypto_aead_aes256gcm_encrypt_detached_afternm(message, additionalData, nonce, ctx);
  218. // Decrypt Data
  219. var plainText = sodium.crypto_aead_aes256gcm_decrypt_detached_afternm(c.cipherText, c.mac, additionalData, nonce, ctx);
  220. // Test equality
  221. assert(sodium.compare(plainText, message)==0);
  222. done();
  223. });
  224. });