sodium.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * # Main module file
  3. *
  4. * @module sodium
  5. * @name node-sodium
  6. * @author Pedro Paixao
  7. * @date 11/9/13
  8. *
  9. */
  10. /* jslint node: true */
  11. 'use strict';
  12. // Base
  13. var binding = require('../build/Release/sodium');
  14. var toBuffer = require('./toBuffer');
  15. // Public Key
  16. var Box = require('./box');
  17. var Sign = require('./sign');
  18. // Symmetric Key
  19. var SecretBox = require('./secretbox');
  20. var Auth = require('./auth');
  21. var OneTimeAuth = require('./onetime-auth');
  22. var Stream = require('./stream');
  23. // Elliptic Curve Diffie-Hellman using Curve25519
  24. var ECDH = require('./ecdh');
  25. // Nonces
  26. var BoxNonce = require('./nonces/box-nonce');
  27. var SecretBoxNonce = require('./nonces/secretbox-nonce');
  28. var StreamNonce = require('./nonces/stream-nonce');
  29. // Keys
  30. var AuthKey = require('./keys/auth-key');
  31. var BoxKey = require('./keys/box-key');
  32. var OneTimeKey = require('./keys/onetime-key');
  33. var SecretBoxKey = require('./keys/secretbox-key');
  34. var SignKey = require('./keys/sign-key');
  35. var StreamKey = require('./keys/stream-key');
  36. var DHKey = require('./keys/dh-key');
  37. /**
  38. * # API
  39. * Export all low level lib sodium functions directly
  40. * for developers that are used to lib sodium C interface
  41. */
  42. module.exports.api = binding;
  43. /** `libsodium` version */
  44. module.exports.version = binding.version;
  45. module.exports.versionMinor = binding.versionMinor;
  46. module.exports.versionMajor = binding.versionMajor;
  47. /** Utilities */
  48. module.exports.Utils = {
  49. memzero: binding.memzero,
  50. memcmp: binding.memcmp,
  51. verify16: binding.crypto_verify_16,
  52. verify32: binding.crypto_verify_32,
  53. verify64: binding.crypto_verify_64,
  54. toBuffer: toBuffer,
  55. };
  56. module.exports.Utils.to_hex = function (args) {
  57. var ret = "";
  58. for ( var i = 0; i < args.length; i++ )
  59. ret += (args[i] < 16 ? "0" : "") + args[i].toString(16);
  60. return ret; //.toUpperCase();
  61. };
  62. module.exports.Utils.from_hex = function (str) {
  63. if (typeof str == 'string') {
  64. var ret = new Uint8Array(Math.floor(str.length / 2));
  65. var i = 0;
  66. str.replace(/(..)/g, function(str) { ret[i++] = parseInt(str, 16);});
  67. return ret;
  68. }
  69. };
  70. /** Hash functions */
  71. module.exports.Hash = {
  72. /** Default message hash */
  73. hash: binding.crypto_hash,
  74. /** SHA 256 */
  75. sha256: binding.crypto_hash_sha256,
  76. /** SHA 512 */
  77. sha512: binding.crypto_hash_sha512,
  78. /** Size of hash buffer in bytes */
  79. bytes: binding.crypto_hash_BYTES,
  80. /** Size of hash block */
  81. blockBytes: binding.crypto_hash_BLOCKBYTES,
  82. /** Default primitive */
  83. primitive: binding.crypto_hash_PRIMITIVE
  84. };
  85. /** Random Functions */
  86. module.exports.Random = {
  87. /** Fill buffer with random bytest */
  88. buffer : binding.randombytes_buf,
  89. /** Initialize OS dependent random device */
  90. stir : binding.randombytes_stir,
  91. /** Close the random device */
  92. close : binding.randombytes_close,
  93. /** Return a random 32-bit unsigned value */
  94. rand : binding.randombytes_random,
  95. /** Return a value between 0 and upper_bound using a uniform distribution */
  96. uniform : binding.randombytes_uniform
  97. };
  98. // Public Key
  99. module.exports.Box = Box;
  100. module.exports.Sign = Sign;
  101. // Symmetric Key
  102. module.exports.Auth = Auth;
  103. module.exports.SecretBox = SecretBox;
  104. module.exports.Stream = Stream;
  105. module.exports.OneTimeAuth = OneTimeAuth;
  106. // Nonces
  107. module.exports.Nonces = {
  108. Box: BoxNonce,
  109. SecretBox: SecretBoxNonce,
  110. Stream: StreamNonce
  111. };
  112. // Symmetric Keys
  113. module.exports.Key = {
  114. SecretBox: SecretBoxKey,
  115. Auth: AuthKey,
  116. OneTimeAuth: OneTimeKey,
  117. Stream: StreamKey,
  118. // Public/Secret Key Pairs
  119. Box: BoxKey,
  120. Sign: SignKey,
  121. ECDH: DHKey
  122. };
  123. // Elliptic Curve Diffie-Hellman with Curve25519
  124. module.exports.ECDH = ECDH;
  125. /**
  126. * Lib Sodium Constants
  127. *
  128. * the base library defines several important constant that you should use to
  129. * check the size of buffers, nonces, keys, etc.
  130. *
  131. * All constants represent the size of the buffer or zone of a buffer in bytes
  132. */
  133. module.exports.Const = {};
  134. /** ScalarMult related constants */
  135. module.exports.Const.ECDH = {
  136. /** Size of scalar buffers */
  137. scalarBytes: binding.crypto_scalarmult_SCALARBYTES,
  138. /** Size of scalar buffers */
  139. bytes: binding.crypto_scalarmult_BYTES,
  140. /** Size of the public and secret keys */
  141. keyBytes: binding.crypto_scalarmult_BYTES,
  142. /** String name of the default crypto primitive used in scalarmult operations */
  143. primitive: binding.crypto_scalarmult_PRIMITIVE
  144. };
  145. /** ScalarMult related constants */
  146. module.exports.Const.ScalarMult = {
  147. /** Size of scalar buffers */
  148. scalarBytes: binding.crypto_scalarmult_SCALARBYTES,
  149. /** Size of the scalarmult keys and points */
  150. bytes: binding.crypto_scalarmult_BYTES,
  151. /** String name of the default crypto primitive used in scalarmult operations */
  152. primitive: binding.crypto_scalarmult_PRIMITIVE
  153. };
  154. /** Hash related constants */
  155. module.exports.Const.Hash = {
  156. /** Size of hash buffer in bytes */
  157. bytes: binding.crypto_hash_BYTES,
  158. /** Size of hash block */
  159. blockBytes: binding.crypto_hash_BLOCKBYTES,
  160. /** Default primitive */
  161. primitive: binding.crypto_hash_PRIMITIVE
  162. };
  163. /** Box related constant sizes in bytes */
  164. module.exports.Const.Box = {
  165. /** Box Nonce buffer size in bytes */
  166. nonceBytes : binding.crypto_box_NONCEBYTES,
  167. /** Box Public Key buffer size in bytes */
  168. publicKeyBytes : binding.crypto_box_PUBLICKEYBYTES,
  169. /** Box Public Key buffer size in bytes */
  170. secretKeyBytes : binding.crypto_box_SECRETKEYBYTES,
  171. /**
  172. * Messages passed to low level API should be padded with zeroBytes at the beginning.
  173. * This implementation automatically pads the message, so no need to do it on your own
  174. */
  175. zeroBytes : binding.crypto_box_ZEROBYTES,
  176. /**
  177. * Encrypted messages are padded with zeroBoxSize bytes of zeros. If the padding is not
  178. * there the message will not decrypt successfully.
  179. */
  180. boxZeroBytes : binding.crypto_box_BOXZEROBYTES,
  181. /**
  182. * Padding used in beforenm method. Like zeroBytes this implementation automatically
  183. * pads the message.
  184. *
  185. * @see Const.Box.zeroBytes
  186. */
  187. beforenmBytes : binding.crypto_box_BEFORENMBYTES,
  188. /** String name of the default crypto primitive used in box operations */
  189. primitive: binding.crypto_box_PRIMITIVE
  190. };
  191. /** Authentication Constants */
  192. module.exports.Const.Auth = {
  193. /** Size of the authentication token */
  194. bytes: binding.crypto_auth_BYTES,
  195. /** Size of the secret key used to generate the authentication token */
  196. keyBytes: binding.crypto_auth_KEYBYTES,
  197. /** String name of the default crypto primitive used in auth operations */
  198. primitive: binding.crypto_auth_PRIMITIVE
  199. };
  200. /** One Time Authentication Constants */
  201. module.exports.Const.OneTimeAuth = {
  202. /** Size of the authentication token */
  203. bytes: binding.crypto_onetimeauth_BYTES,
  204. /** Size of the secret key used to generate the authentication token */
  205. keyBytes: binding.crypto_onetimeauth_KEYBYTES,
  206. /** String name of the default crypto primitive used in onetimeauth operations */
  207. primitive: binding.crypto_onetimeauth_PRIMITIVE
  208. };
  209. /** SecretBox Symmetric Key Crypto Constants */
  210. module.exports.Const.SecretBox = {
  211. /** SecretBox padding of cipher text buffer */
  212. boxZeroBytes: binding.crypto_secretbox_BOXZEROBYTES,
  213. /** Size of the secret key used to encrypt/decrypt messages */
  214. keyBytes: binding.crypto_secretbox_KEYBYTES,
  215. /** Size of the Nonce used in encryption/decryption of messages */
  216. nonceBytes: binding.crypto_secretbox_NONCEBYTES,
  217. /** Passing of message. This implementation does message padding automatically */
  218. zeroBytes: binding.crypto_secretbox_ZEROBYTES,
  219. /** String name of the default crypto primitive used in secretbox operations */
  220. primitive: binding.crypto_secretbox_PRIMITIVE
  221. };
  222. /** Digital message signature constants */
  223. module.exports.Const.Sign = {
  224. /** Size of the generated message signature */
  225. bytes: binding.crypto_sign_BYTES,
  226. /** Size of the public key used to verify signatures */
  227. publicKeyBytes: binding.crypto_sign_PUBLICKEYBYTES,
  228. /** Size of the secret key used to sign a message */
  229. secretKeyBytes: binding.crypto_sign_SECRETKEYBYTES,
  230. /** String name of the default crypto primitive used in sign operations */
  231. primitive: binding.crypto_sign_PRIMITIVE
  232. };
  233. /** Symmetric Encryption Constants */
  234. module.exports.Const.Stream = {
  235. /** Size of secret key used to encrypt/decrypt messages */
  236. keyBytes : binding.crypto_stream_KEYBYTES,
  237. /** Size of nonce used to encrypt/decrypt messages */
  238. nonceBytes : binding.crypto_stream_NONCEBYTES,
  239. /** String name of the default crypto primitive used in stream operations */
  240. primitive: binding.crypto_stream_PRIMITIVE
  241. };
  242. /** Short hash related constants */
  243. module.exports.Const.ShortHash = {
  244. /** Size of short hash buffer in bytes*/
  245. bytes: binding.crypto_shorthash_BYTES,
  246. /** Size of short hash Key buffer in bytes */
  247. keyBytes: binding.crypto_shorthash_KEYBYTES,
  248. /** String name of primitive used to calculate short hash */
  249. primitive: binding.crypto_shorthash_PRIMITIVE
  250. };