monocypher.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Monocypher version 3.1.2
  2. //
  3. // This file is dual-licensed. Choose whichever licence you want from
  4. // the two licences listed below.
  5. //
  6. // The first licence is a regular 2-clause BSD licence. The second licence
  7. // is the CC-0 from Creative Commons. It is intended to release Monocypher
  8. // to the public domain. The BSD licence serves as a fallback option.
  9. //
  10. // SPDX-License-Identifier: BSD-2-Clause OR CC0-1.0
  11. //
  12. // ------------------------------------------------------------------------
  13. //
  14. // Copyright (c) 2017-2019, Loup Vaillant
  15. // All rights reserved.
  16. //
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are
  20. // met:
  21. //
  22. // 1. Redistributions of source code must retain the above copyright
  23. // notice, this list of conditions and the following disclaimer.
  24. //
  25. // 2. Redistributions in binary form must reproduce the above copyright
  26. // notice, this list of conditions and the following disclaimer in the
  27. // documentation and/or other materials provided with the
  28. // distribution.
  29. //
  30. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. //
  42. // ------------------------------------------------------------------------
  43. //
  44. // Written in 2017-2019 by Loup Vaillant
  45. //
  46. // To the extent possible under law, the author(s) have dedicated all copyright
  47. // and related neighboring rights to this software to the public domain
  48. // worldwide. This software is distributed without any warranty.
  49. //
  50. // You should have received a copy of the CC0 Public Domain Dedication along
  51. // with this software. If not, see
  52. // <https://creativecommons.org/publicdomain/zero/1.0/>
  53. #ifndef MONOCYPHER_H
  54. #define MONOCYPHER_H
  55. #include <stddef.h>
  56. #include <stdint.h>
  57. ////////////////////////
  58. /// Type definitions ///
  59. ////////////////////////
  60. // Vtable for EdDSA with a custom hash.
  61. // Instantiate it to define a custom hash.
  62. // Its size, contents, and layout, are part of the public API.
  63. typedef struct {
  64. void (*hash)(uint8_t hash[64], const uint8_t *message, size_t message_size);
  65. void (*init )(void *ctx);
  66. void (*update)(void *ctx, const uint8_t *message, size_t message_size);
  67. void (*final )(void *ctx, uint8_t hash[64]);
  68. size_t ctx_size;
  69. } crypto_sign_vtable;
  70. // Do not rely on the size or contents of any of the types below,
  71. // they may change without notice.
  72. // Poly1305
  73. typedef struct {
  74. uint32_t r[4]; // constant multiplier (from the secret key)
  75. uint32_t h[5]; // accumulated hash
  76. uint32_t c[5]; // chunk of the message
  77. uint32_t pad[4]; // random number added at the end (from the secret key)
  78. size_t c_idx; // How many bytes are there in the chunk.
  79. } crypto_poly1305_ctx;
  80. // Hash (Blake2b)
  81. typedef struct {
  82. uint64_t hash[8];
  83. uint64_t input_offset[2];
  84. uint64_t input[16];
  85. size_t input_idx;
  86. size_t hash_size;
  87. } crypto_blake2b_ctx;
  88. // Signatures (EdDSA)
  89. typedef struct {
  90. const crypto_sign_vtable *hash;
  91. uint8_t buf[96];
  92. uint8_t pk [32];
  93. } crypto_sign_ctx_abstract;
  94. typedef crypto_sign_ctx_abstract crypto_check_ctx_abstract;
  95. typedef struct {
  96. crypto_sign_ctx_abstract ctx;
  97. crypto_blake2b_ctx hash;
  98. } crypto_sign_ctx;
  99. typedef crypto_sign_ctx crypto_check_ctx;
  100. ////////////////////////////
  101. /// High level interface ///
  102. ////////////////////////////
  103. // Constant time comparisons
  104. // -------------------------
  105. // Return 0 if a and b are equal, -1 otherwise
  106. int crypto_verify16(const uint8_t a[16], const uint8_t b[16]);
  107. int crypto_verify32(const uint8_t a[32], const uint8_t b[32]);
  108. int crypto_verify64(const uint8_t a[64], const uint8_t b[64]);
  109. // Erase sensitive data
  110. // --------------------
  111. // Please erase all copies
  112. void crypto_wipe(void *secret, size_t size);
  113. // Authenticated encryption
  114. // ------------------------
  115. void crypto_lock(uint8_t mac[16],
  116. uint8_t *cipher_text,
  117. const uint8_t key[32],
  118. const uint8_t nonce[24],
  119. const uint8_t *plain_text, size_t text_size);
  120. int crypto_unlock(uint8_t *plain_text,
  121. const uint8_t key[32],
  122. const uint8_t nonce[24],
  123. const uint8_t mac[16],
  124. const uint8_t *cipher_text, size_t text_size);
  125. // With additional data
  126. void crypto_lock_aead(uint8_t mac[16],
  127. uint8_t *cipher_text,
  128. const uint8_t key[32],
  129. const uint8_t nonce[24],
  130. const uint8_t *ad , size_t ad_size,
  131. const uint8_t *plain_text, size_t text_size);
  132. int crypto_unlock_aead(uint8_t *plain_text,
  133. const uint8_t key[32],
  134. const uint8_t nonce[24],
  135. const uint8_t mac[16],
  136. const uint8_t *ad , size_t ad_size,
  137. const uint8_t *cipher_text, size_t text_size);
  138. // General purpose hash (Blake2b)
  139. // ------------------------------
  140. // Direct interface
  141. void crypto_blake2b(uint8_t hash[64],
  142. const uint8_t *message, size_t message_size);
  143. void crypto_blake2b_general(uint8_t *hash , size_t hash_size,
  144. const uint8_t *key , size_t key_size, // optional
  145. const uint8_t *message, size_t message_size);
  146. // Incremental interface
  147. void crypto_blake2b_init (crypto_blake2b_ctx *ctx);
  148. void crypto_blake2b_update(crypto_blake2b_ctx *ctx,
  149. const uint8_t *message, size_t message_size);
  150. void crypto_blake2b_final (crypto_blake2b_ctx *ctx, uint8_t *hash);
  151. void crypto_blake2b_general_init(crypto_blake2b_ctx *ctx, size_t hash_size,
  152. const uint8_t *key, size_t key_size);
  153. // vtable for signatures
  154. extern const crypto_sign_vtable crypto_blake2b_vtable;
  155. // Password key derivation (Argon2 i)
  156. // ----------------------------------
  157. void crypto_argon2i(uint8_t *hash, uint32_t hash_size, // >= 4
  158. void *work_area, uint32_t nb_blocks, // >= 8
  159. uint32_t nb_iterations, // >= 3
  160. const uint8_t *password, uint32_t password_size,
  161. const uint8_t *salt, uint32_t salt_size); // >= 8
  162. void crypto_argon2i_general(uint8_t *hash, uint32_t hash_size,// >= 4
  163. void *work_area, uint32_t nb_blocks,// >= 8
  164. uint32_t nb_iterations, // >= 3
  165. const uint8_t *password, uint32_t password_size,
  166. const uint8_t *salt, uint32_t salt_size,// >= 8
  167. const uint8_t *key, uint32_t key_size,
  168. const uint8_t *ad, uint32_t ad_size);
  169. // Key exchange (x25519 + HChacha20)
  170. // ---------------------------------
  171. #define crypto_key_exchange_public_key crypto_x25519_public_key
  172. void crypto_key_exchange(uint8_t shared_key [32],
  173. const uint8_t your_secret_key [32],
  174. const uint8_t their_public_key[32]);
  175. // Signatures (EdDSA with curve25519 + Blake2b)
  176. // --------------------------------------------
  177. // Generate public key
  178. void crypto_sign_public_key(uint8_t public_key[32],
  179. const uint8_t secret_key[32]);
  180. // Direct interface
  181. void crypto_sign(uint8_t signature [64],
  182. const uint8_t secret_key[32],
  183. const uint8_t public_key[32], // optional, may be 0
  184. const uint8_t *message, size_t message_size);
  185. int crypto_check(const uint8_t signature [64],
  186. const uint8_t public_key[32],
  187. const uint8_t *message, size_t message_size);
  188. ////////////////////////////
  189. /// Low level primitives ///
  190. ////////////////////////////
  191. // For experts only. You have been warned.
  192. // Chacha20
  193. // --------
  194. // Specialised hash.
  195. // Used to hash X25519 shared secrets.
  196. void crypto_hchacha20(uint8_t out[32],
  197. const uint8_t key[32],
  198. const uint8_t in [16]);
  199. // Unauthenticated stream cipher.
  200. // Don't forget to add authentication.
  201. void crypto_chacha20(uint8_t *cipher_text,
  202. const uint8_t *plain_text,
  203. size_t text_size,
  204. const uint8_t key[32],
  205. const uint8_t nonce[8]);
  206. void crypto_xchacha20(uint8_t *cipher_text,
  207. const uint8_t *plain_text,
  208. size_t text_size,
  209. const uint8_t key[32],
  210. const uint8_t nonce[24]);
  211. void crypto_ietf_chacha20(uint8_t *cipher_text,
  212. const uint8_t *plain_text,
  213. size_t text_size,
  214. const uint8_t key[32],
  215. const uint8_t nonce[12]);
  216. uint64_t crypto_chacha20_ctr(uint8_t *cipher_text,
  217. const uint8_t *plain_text,
  218. size_t text_size,
  219. const uint8_t key[32],
  220. const uint8_t nonce[8],
  221. uint64_t ctr);
  222. uint64_t crypto_xchacha20_ctr(uint8_t *cipher_text,
  223. const uint8_t *plain_text,
  224. size_t text_size,
  225. const uint8_t key[32],
  226. const uint8_t nonce[24],
  227. uint64_t ctr);
  228. uint32_t crypto_ietf_chacha20_ctr(uint8_t *cipher_text,
  229. const uint8_t *plain_text,
  230. size_t text_size,
  231. const uint8_t key[32],
  232. const uint8_t nonce[12],
  233. uint32_t ctr);
  234. // Poly 1305
  235. // ---------
  236. // This is a *one time* authenticator.
  237. // Disclosing the mac reveals the key.
  238. // See crypto_lock() on how to use it properly.
  239. // Direct interface
  240. void crypto_poly1305(uint8_t mac[16],
  241. const uint8_t *message, size_t message_size,
  242. const uint8_t key[32]);
  243. // Incremental interface
  244. void crypto_poly1305_init (crypto_poly1305_ctx *ctx, const uint8_t key[32]);
  245. void crypto_poly1305_update(crypto_poly1305_ctx *ctx,
  246. const uint8_t *message, size_t message_size);
  247. void crypto_poly1305_final (crypto_poly1305_ctx *ctx, uint8_t mac[16]);
  248. // X-25519
  249. // -------
  250. // Shared secrets are not quite random.
  251. // Hash them to derive an actual shared key.
  252. void crypto_x25519_public_key(uint8_t public_key[32],
  253. const uint8_t secret_key[32]);
  254. void crypto_x25519(uint8_t raw_shared_secret[32],
  255. const uint8_t your_secret_key [32],
  256. const uint8_t their_public_key [32]);
  257. // "Dirty" versions of x25519_public_key()
  258. // Only use to generate ephemeral keys you want to hide.
  259. // Note that those functions leaks 3 bits of the private key.
  260. void crypto_x25519_dirty_small(uint8_t pk[32], const uint8_t sk[32]);
  261. void crypto_x25519_dirty_fast (uint8_t pk[32], const uint8_t sk[32]);
  262. // scalar "division"
  263. // Used for OPRF. Be aware that exponential blinding is less secure
  264. // than Diffie-Hellman key exchange.
  265. void crypto_x25519_inverse(uint8_t blind_salt [32],
  266. const uint8_t private_key[32],
  267. const uint8_t curve_point[32]);
  268. // EdDSA to X25519
  269. // ---------------
  270. void crypto_from_eddsa_private(uint8_t x25519[32], const uint8_t eddsa[32]);
  271. void crypto_from_eddsa_public (uint8_t x25519[32], const uint8_t eddsa[32]);
  272. // EdDSA -- Incremental interface
  273. // ------------------------------
  274. // Signing (2 passes)
  275. // Make sure the two passes hash the same message,
  276. // else you might reveal the private key.
  277. void crypto_sign_init_first_pass(crypto_sign_ctx_abstract *ctx,
  278. const uint8_t secret_key[32],
  279. const uint8_t public_key[32]);
  280. void crypto_sign_update(crypto_sign_ctx_abstract *ctx,
  281. const uint8_t *message, size_t message_size);
  282. void crypto_sign_init_second_pass(crypto_sign_ctx_abstract *ctx);
  283. // use crypto_sign_update() again.
  284. void crypto_sign_final(crypto_sign_ctx_abstract *ctx, uint8_t signature[64]);
  285. // Verification (1 pass)
  286. // Make sure you don't use (parts of) the message
  287. // before you're done checking it.
  288. void crypto_check_init (crypto_check_ctx_abstract *ctx,
  289. const uint8_t signature[64],
  290. const uint8_t public_key[32]);
  291. void crypto_check_update(crypto_check_ctx_abstract *ctx,
  292. const uint8_t *message, size_t message_size);
  293. int crypto_check_final (crypto_check_ctx_abstract *ctx);
  294. // Custom hash interface
  295. void crypto_sign_public_key_custom_hash(uint8_t public_key[32],
  296. const uint8_t secret_key[32],
  297. const crypto_sign_vtable *hash);
  298. void crypto_sign_init_first_pass_custom_hash(crypto_sign_ctx_abstract *ctx,
  299. const uint8_t secret_key[32],
  300. const uint8_t public_key[32],
  301. const crypto_sign_vtable *hash);
  302. void crypto_check_init_custom_hash(crypto_check_ctx_abstract *ctx,
  303. const uint8_t signature[64],
  304. const uint8_t public_key[32],
  305. const crypto_sign_vtable *hash);
  306. // Elligator 2
  307. // -----------
  308. // Elligator mappings proper
  309. void crypto_hidden_to_curve(uint8_t curve [32], const uint8_t hidden[32]);
  310. int crypto_curve_to_hidden(uint8_t hidden[32], const uint8_t curve [32],
  311. uint8_t tweak);
  312. // Easy to use key pair generation
  313. void crypto_hidden_key_pair(uint8_t hidden[32], uint8_t secret_key[32],
  314. uint8_t seed[32]);
  315. #endif // MONOCYPHER_H