monocypher.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Monocypher version 4.0.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. #ifdef MONOCYPHER_CPP_NAMESPACE
  58. namespace MONOCYPHER_CPP_NAMESPACE {
  59. #elif defined(__cplusplus)
  60. extern "C" {
  61. #endif
  62. // Constant time comparisons
  63. // -------------------------
  64. // Return 0 if a and b are equal, -1 otherwise
  65. int crypto_verify16(const uint8_t a[16], const uint8_t b[16]);
  66. int crypto_verify32(const uint8_t a[32], const uint8_t b[32]);
  67. int crypto_verify64(const uint8_t a[64], const uint8_t b[64]);
  68. // Erase sensitive data
  69. // --------------------
  70. void crypto_wipe(void *secret, size_t size);
  71. // Authenticated encryption
  72. // ------------------------
  73. void crypto_aead_lock(uint8_t *cipher_text,
  74. uint8_t mac [16],
  75. const uint8_t key [32],
  76. const uint8_t nonce[24],
  77. const uint8_t *ad, size_t ad_size,
  78. const uint8_t *plain_text, size_t text_size);
  79. int crypto_aead_unlock(uint8_t *plain_text,
  80. const uint8_t mac [16],
  81. const uint8_t key [32],
  82. const uint8_t nonce[24],
  83. const uint8_t *ad, size_t ad_size,
  84. const uint8_t *cipher_text, size_t text_size);
  85. // Authenticated stream
  86. // --------------------
  87. typedef struct {
  88. uint64_t counter;
  89. uint8_t key[32];
  90. uint8_t nonce[8];
  91. } crypto_aead_ctx;
  92. void crypto_aead_init_x(crypto_aead_ctx *ctx,
  93. const uint8_t key[32], const uint8_t nonce[24]);
  94. void crypto_aead_init_djb(crypto_aead_ctx *ctx,
  95. const uint8_t key[32], const uint8_t nonce[8]);
  96. void crypto_aead_init_ietf(crypto_aead_ctx *ctx,
  97. const uint8_t key[32], const uint8_t nonce[12]);
  98. void crypto_aead_write(crypto_aead_ctx *ctx,
  99. uint8_t *cipher_text,
  100. uint8_t mac[16],
  101. const uint8_t *ad , size_t ad_size,
  102. const uint8_t *plain_text, size_t text_size);
  103. int crypto_aead_read(crypto_aead_ctx *ctx,
  104. uint8_t *plain_text,
  105. const uint8_t mac[16],
  106. const uint8_t *ad , size_t ad_size,
  107. const uint8_t *cipher_text, size_t text_size);
  108. // General purpose hash (BLAKE2b)
  109. // ------------------------------
  110. // Direct interface
  111. void crypto_blake2b(uint8_t *hash, size_t hash_size,
  112. const uint8_t *message, size_t message_size);
  113. void crypto_blake2b_keyed(uint8_t *hash, size_t hash_size,
  114. const uint8_t *key, size_t key_size,
  115. const uint8_t *message, size_t message_size);
  116. // Incremental interface
  117. typedef struct {
  118. // Do not rely on the size or contents of this type,
  119. // for they may change without notice.
  120. uint64_t hash[8];
  121. uint64_t input_offset[2];
  122. uint64_t input[16];
  123. size_t input_idx;
  124. size_t hash_size;
  125. } crypto_blake2b_ctx;
  126. void crypto_blake2b_init(crypto_blake2b_ctx *ctx, size_t hash_size);
  127. void crypto_blake2b_keyed_init(crypto_blake2b_ctx *ctx, size_t hash_size,
  128. const uint8_t *key, size_t key_size);
  129. void crypto_blake2b_update(crypto_blake2b_ctx *ctx,
  130. const uint8_t *message, size_t message_size);
  131. void crypto_blake2b_final(crypto_blake2b_ctx *ctx, uint8_t *hash);
  132. // Password key derivation (Argon2)
  133. // --------------------------------
  134. #define CRYPTO_ARGON2_D 0
  135. #define CRYPTO_ARGON2_I 1
  136. #define CRYPTO_ARGON2_ID 2
  137. typedef struct {
  138. uint32_t algorithm; // Argon2d, Argon2i, Argon2id
  139. uint32_t nb_blocks; // memory hardness, >= 8 * nb_lanes
  140. uint32_t nb_passes; // CPU hardness, >= 1 (>= 3 recommended for Argon2i)
  141. uint32_t nb_lanes; // parallelism level (single threaded anyway)
  142. } crypto_argon2_config;
  143. typedef struct {
  144. const uint8_t *pass;
  145. const uint8_t *salt;
  146. uint32_t pass_size;
  147. uint32_t salt_size; // 16 bytes recommended
  148. } crypto_argon2_inputs;
  149. typedef struct {
  150. const uint8_t *key; // may be NULL if no key
  151. const uint8_t *ad; // may be NULL if no additional data
  152. uint32_t key_size; // 0 if no key (32 bytes recommended otherwise)
  153. uint32_t ad_size; // 0 if no additional data
  154. } crypto_argon2_extras;
  155. extern const crypto_argon2_extras crypto_argon2_no_extras;
  156. void crypto_argon2(uint8_t *hash, uint32_t hash_size, void *work_area,
  157. crypto_argon2_config config,
  158. crypto_argon2_inputs inputs,
  159. crypto_argon2_extras extras);
  160. // Key exchange (X-25519)
  161. // ----------------------
  162. // Shared secrets are not quite random.
  163. // Hash them to derive an actual shared key.
  164. void crypto_x25519_public_key(uint8_t public_key[32],
  165. const uint8_t secret_key[32]);
  166. void crypto_x25519(uint8_t raw_shared_secret[32],
  167. const uint8_t your_secret_key [32],
  168. const uint8_t their_public_key [32]);
  169. // Conversion to EdDSA
  170. void crypto_x25519_to_eddsa(uint8_t eddsa[32], const uint8_t x25519[32]);
  171. // scalar "division"
  172. // Used for OPRF. Be aware that exponential blinding is less secure
  173. // than Diffie-Hellman key exchange.
  174. void crypto_x25519_inverse(uint8_t blind_salt [32],
  175. const uint8_t private_key[32],
  176. const uint8_t curve_point[32]);
  177. // "Dirty" versions of x25519_public_key().
  178. // Use with crypto_elligator_rev().
  179. // Leaks 3 bits of the private key.
  180. void crypto_x25519_dirty_small(uint8_t pk[32], const uint8_t sk[32]);
  181. void crypto_x25519_dirty_fast (uint8_t pk[32], const uint8_t sk[32]);
  182. // Signatures
  183. // ----------
  184. // EdDSA with curve25519 + BLAKE2b
  185. void crypto_eddsa_key_pair(uint8_t secret_key[64],
  186. uint8_t public_key[32],
  187. uint8_t seed[32]);
  188. void crypto_eddsa_sign(uint8_t signature [64],
  189. const uint8_t secret_key[64],
  190. const uint8_t *message, size_t message_size);
  191. int crypto_eddsa_check(const uint8_t signature [64],
  192. const uint8_t public_key[32],
  193. const uint8_t *message, size_t message_size);
  194. // Conversion to X25519
  195. void crypto_eddsa_to_x25519(uint8_t x25519[32], const uint8_t eddsa[32]);
  196. // EdDSA building blocks
  197. void crypto_eddsa_trim_scalar(uint8_t out[32], const uint8_t in[32]);
  198. void crypto_eddsa_reduce(uint8_t reduced[32], const uint8_t expanded[64]);
  199. void crypto_eddsa_mul_add(uint8_t r[32],
  200. const uint8_t a[32],
  201. const uint8_t b[32],
  202. const uint8_t c[32]);
  203. void crypto_eddsa_scalarbase(uint8_t point[32], const uint8_t scalar[32]);
  204. int crypto_eddsa_check_equation(const uint8_t signature[64],
  205. const uint8_t public_key[32],
  206. const uint8_t h_ram[32]);
  207. // Chacha20
  208. // --------
  209. // Specialised hash.
  210. // Used to hash X25519 shared secrets.
  211. void crypto_chacha20_h(uint8_t out[32],
  212. const uint8_t key[32],
  213. const uint8_t in [16]);
  214. // Unauthenticated stream cipher.
  215. // Don't forget to add authentication.
  216. uint64_t crypto_chacha20_djb(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. uint32_t crypto_chacha20_ietf(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[12],
  227. uint32_t ctr);
  228. uint64_t crypto_chacha20_x(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[24],
  233. uint64_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. typedef struct {
  245. // Do not rely on the size or contents of this type,
  246. // for they may change without notice.
  247. uint8_t c[16]; // chunk of the message
  248. size_t c_idx; // How many bytes are there in the chunk.
  249. uint32_t r [4]; // constant multiplier (from the secret key)
  250. uint32_t pad[4]; // random number added at the end (from the secret key)
  251. uint32_t h [5]; // accumulated hash
  252. } crypto_poly1305_ctx;
  253. void crypto_poly1305_init (crypto_poly1305_ctx *ctx, const uint8_t key[32]);
  254. void crypto_poly1305_update(crypto_poly1305_ctx *ctx,
  255. const uint8_t *message, size_t message_size);
  256. void crypto_poly1305_final (crypto_poly1305_ctx *ctx, uint8_t mac[16]);
  257. // Elligator 2
  258. // -----------
  259. // Elligator mappings proper
  260. void crypto_elligator_map(uint8_t curve [32], const uint8_t hidden[32]);
  261. int crypto_elligator_rev(uint8_t hidden[32], const uint8_t curve [32],
  262. uint8_t tweak);
  263. // Easy to use key pair generation
  264. void crypto_elligator_key_pair(uint8_t hidden[32], uint8_t secret_key[32],
  265. uint8_t seed[32]);
  266. #ifdef __cplusplus
  267. }
  268. #endif
  269. #endif // MONOCYPHER_H