+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | // Monocypher version __git__ |
| 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 | |
| 54 | #ifndef MONOCYPHER_H |
| 55 | #define MONOCYPHER_H |
| 56 | |
| 57 | #include <stddef.h> |
| 58 | #include <stdint.h> |
| 59 | |
| 60 | #ifdef MONOCYPHER_CPP_NAMESPACE |
| 61 | namespace MONOCYPHER_CPP_NAMESPACE { |
| 62 | #elif defined(__cplusplus) |
| 63 | extern "C" { |
| 64 | #endif |
| 65 | |
| 66 | // Constant time comparisons |
| 67 | // ------------------------- |
| 68 | |
| 69 | // Return 0 if a and b are equal, -1 otherwise |
| 70 | int crypto_verify16(const uint8_t a[16], const uint8_t b[16]); |
| 71 | int crypto_verify32(const uint8_t a[32], const uint8_t b[32]); |
| 72 | int crypto_verify64(const uint8_t a[64], const uint8_t b[64]); |
| 73 | |
| 74 | |
| 75 | // Erase sensitive data |
| 76 | // -------------------- |
| 77 | void crypto_wipe(void *secret, size_t size); |
| 78 | |
| 79 | |
| 80 | // Authenticated encryption |
| 81 | // ------------------------ |
| 82 | void crypto_aead_lock(uint8_t *cipher_text, |
| 83 | uint8_t mac [16], |
| 84 | const uint8_t key [32], |
| 85 | const uint8_t nonce[24], |
| 86 | const uint8_t *ad, size_t ad_size, |
| 87 | const uint8_t *plain_text, size_t text_size); |
| 88 | int crypto_aead_unlock(uint8_t *plain_text, |
| 89 | const uint8_t mac [16], |
| 90 | const uint8_t key [32], |
| 91 | const uint8_t nonce[24], |
| 92 | const uint8_t *ad, size_t ad_size, |
| 93 | const uint8_t *cipher_text, size_t text_size); |
| 94 | |
| 95 | // Authenticated stream |
| 96 | // -------------------- |
| 97 | typedef struct { |
| 98 | uint64_t counter; |
| 99 | uint8_t key[32]; |
| 100 | uint8_t nonce[8]; |
| 101 | } crypto_aead_ctx; |
| 102 | |
| 103 | void crypto_aead_init_x(crypto_aead_ctx *ctx, |
| 104 | const uint8_t key[32], const uint8_t nonce[24]); |
| 105 | void crypto_aead_init_djb(crypto_aead_ctx *ctx, |
| 106 | const uint8_t key[32], const uint8_t nonce[8]); |
| 107 | void crypto_aead_init_ietf(crypto_aead_ctx *ctx, |
| 108 | const uint8_t key[32], const uint8_t nonce[12]); |
| 109 | |
| 110 | void crypto_aead_write(crypto_aead_ctx *ctx, |
| 111 | uint8_t *cipher_text, |
| 112 | uint8_t mac[16], |
| 113 | const uint8_t *ad , size_t ad_size, |
| 114 | const uint8_t *plain_text, size_t text_size); |
| 115 | int crypto_aead_read(crypto_aead_ctx *ctx, |
| 116 | uint8_t *plain_text, |
| 117 | const uint8_t mac[16], |
| 118 | const uint8_t *ad , size_t ad_size, |
| 119 | const uint8_t *cipher_text, size_t text_size); |
| 120 | |
| 121 | |
| 122 | // General purpose hash (BLAKE2b) |
| 123 | // ------------------------------ |
| 124 | |
| 125 | // Direct interface |
| 126 | void crypto_blake2b(uint8_t *hash, size_t hash_size, |
| 127 | const uint8_t *message, size_t message_size); |
| 128 | |
| 129 | void crypto_blake2b_keyed(uint8_t *hash, size_t hash_size, |
| 130 | const uint8_t *key, size_t key_size, |
| 131 | const uint8_t *message, size_t message_size); |
| 132 | |
| 133 | // Incremental interface |
| 134 | typedef struct { |
| 135 | // Do not rely on the size or contents of this type, |
| 136 | // for they may change without notice. |
| 137 | uint64_t hash[8]; |
| 138 | uint64_t input_offset[2]; |
| 139 | uint64_t input[16]; |
| 140 | size_t input_idx; |
| 141 | size_t hash_size; |
| 142 | } crypto_blake2b_ctx; |
| 143 | |
| 144 | void crypto_blake2b_init(crypto_blake2b_ctx *ctx, size_t hash_size); |
| 145 | void crypto_blake2b_keyed_init(crypto_blake2b_ctx *ctx, size_t hash_size, |
| 146 | const uint8_t *key, size_t key_size); |
| 147 | void crypto_blake2b_update(crypto_blake2b_ctx *ctx, |
| 148 | const uint8_t *message, size_t message_size); |
| 149 | void crypto_blake2b_final(crypto_blake2b_ctx *ctx, uint8_t *hash); |
| 150 | |
| 151 | |
| 152 | // Password key derivation (Argon2) |
| 153 | // -------------------------------- |
| 154 | #define CRYPTO_ARGON2_D 0 |
| 155 | #define CRYPTO_ARGON2_I 1 |
| 156 | #define CRYPTO_ARGON2_ID 2 |
| 157 | |
| 158 | typedef struct { |
| 159 | uint32_t algorithm; // Argon2d, Argon2i, Argon2id |
| 160 | uint32_t nb_blocks; // memory hardness, >= 8 * nb_lanes |
| 161 | uint32_t nb_passes; // CPU hardness, >= 1 (>= 3 recommended for Argon2i) |
| 162 | uint32_t nb_lanes; // parallelism level, >= 1 |
| 163 | } crypto_argon2_config; |
| 164 | |
| 165 | typedef struct { |
| 166 | const uint8_t *pass; |
| 167 | const uint8_t *salt; |
| 168 | uint32_t pass_size; |
| 169 | uint32_t salt_size; // 16 bytes recommended |
| 170 | } crypto_argon2_inputs; |
| 171 | |
| 172 | typedef struct { |
| 173 | const uint8_t *key; // may be NULL if no key |
| 174 | const uint8_t *ad; // may be NULL if no additional data |
| 175 | uint32_t key_size; // 0 if no key (32 bytes recommended otherwise) |
| 176 | uint32_t ad_size; // 0 if no additional data |
| 177 | } crypto_argon2_extras; |
| 178 | |
| 179 | extern const crypto_argon2_extras crypto_argon2_no_extras; |
| 180 | |
| 181 | // Direct interface |
| 182 | void crypto_argon2(uint8_t *hash, uint32_t hash_size, void *work_area, |
| 183 | crypto_argon2_config config, |
| 184 | crypto_argon2_inputs inputs, |
| 185 | crypto_argon2_extras extras); |
| 186 | |
| 187 | // Parallel interface |
| 188 | typedef struct { |
| 189 | crypto_argon2_config config; |
| 190 | void *work_area; |
| 191 | uint32_t hash_size; |
| 192 | } crypto_argon2_ctx; |
| 193 | |
| 194 | void crypto_argon2_init(crypto_argon2_ctx *ctx, uint32_t hash_size, |
| 195 | void *work_area, |
| 196 | crypto_argon2_config config, |
| 197 | crypto_argon2_inputs inputs, |
| 198 | crypto_argon2_extras extras); |
| 199 | void crypto_argon2_segment(const crypto_argon2_ctx *ctx, |
| 200 | uint32_t pass, uint32_t slice, uint32_t lane); |
| 201 | void crypto_argon2_final(const crypto_argon2_ctx *ctx, uint8_t *hash); |
| 202 | |
| 203 | |
| 204 | // Key exchange (X-25519) |
| 205 | // ---------------------- |
| 206 | |
| 207 | // Shared secrets are not quite random. |
| 208 | // Hash them to derive an actual shared key. |
| 209 | void crypto_x25519_public_key(uint8_t public_key[32], |
| 210 | const uint8_t secret_key[32]); |
| 211 | void crypto_x25519_public_key_small(uint8_t public_key[32], |
| 212 | const uint8_t secret_key[32]); |
| 213 | void crypto_x25519(uint8_t raw_shared_secret[32], |
| 214 | const uint8_t your_secret_key [32], |
| 215 | const uint8_t their_public_key [32]); |
| 216 | |
| 217 | // Conversion to EdDSA |
| 218 | void crypto_x25519_to_eddsa(uint8_t eddsa[32], const uint8_t x25519[32]); |
| 219 | |
| 220 | // scalar "division" |
| 221 | // Used for OPRF. Be aware that exponential blinding is less secure |
| 222 | // than Diffie-Hellman key exchange. |
| 223 | void crypto_x25519_inverse(uint8_t blind_salt [32], |
| 224 | const uint8_t private_key[32], |
| 225 | const uint8_t curve_point[32]); |
| 226 | |
| 227 | // "Dirty" versions of x25519_public_key(). |
| 228 | // Use with crypto_elligator_rev(). |
| 229 | // Leaks 3 bits of the private key. |
| 230 | void crypto_x25519_dirty_small(uint8_t pk[32], const uint8_t sk[32]); |
| 231 | void crypto_x25519_dirty_fast (uint8_t pk[32], const uint8_t sk[32]); |
| 232 | |
| 233 | |
| 234 | // Signatures |
| 235 | // ---------- |
| 236 | |
| 237 | // EdDSA with curve25519 + BLAKE2b |
| 238 | void crypto_eddsa_key_pair(uint8_t secret_key[64], |
| 239 | uint8_t public_key[32], |
| 240 | uint8_t seed[32]); |
| 241 | void crypto_eddsa_sign(uint8_t signature [64], |
| 242 | const uint8_t secret_key[64], |
| 243 | const uint8_t *message, size_t message_size); |
| 244 | int crypto_eddsa_check(const uint8_t signature [64], |
| 245 | const uint8_t public_key[32], |
| 246 | const uint8_t *message, size_t message_size); |
| 247 | |
| 248 | // Conversion to X25519 |
| 249 | void crypto_eddsa_to_x25519(uint8_t x25519[32], const uint8_t eddsa[32]); |
| 250 | |
| 251 | // EdDSA building blocks |
| 252 | void crypto_eddsa_trim_scalar(uint8_t out[32], const uint8_t in[32]); |
| 253 | void crypto_eddsa_reduce(uint8_t reduced[32], const uint8_t expanded[64]); |
| 254 | void crypto_eddsa_mul_add(uint8_t r[32], |
| 255 | const uint8_t a[32], |
| 256 | const uint8_t b[32], |
| 257 | const uint8_t c[32]); |
| 258 | void crypto_eddsa_scalarbase(uint8_t point[32], const uint8_t scalar[32]); |
| 259 | int crypto_eddsa_check_equation(const uint8_t signature[64], |
| 260 | const uint8_t public_key[32], |
| 261 | const uint8_t h_ram[32]); |
| 262 | |
| 263 | |
| 264 | // Chacha20 |
| 265 | // -------- |
| 266 | |
| 267 | // Specialised hash. |
| 268 | // Used to hash X25519 shared secrets. |
| 269 | void crypto_chacha20_h(uint8_t out[32], |
| 270 | const uint8_t key[32], |
| 271 | const uint8_t in [16]); |
| 272 | |
| 273 | // Unauthenticated stream cipher. |
| 274 | // Don't forget to add authentication. |
| 275 | uint64_t crypto_chacha20_djb(uint8_t *cipher_text, |
| 276 | const uint8_t *plain_text, |
| 277 | size_t text_size, |
| 278 | const uint8_t key[32], |
| 279 | const uint8_t nonce[8], |
| 280 | uint64_t ctr); |
| 281 | uint32_t crypto_chacha20_ietf(uint8_t *cipher_text, |
| 282 | const uint8_t *plain_text, |
| 283 | size_t text_size, |
| 284 | const uint8_t key[32], |
| 285 | const uint8_t nonce[12], |
| 286 | uint32_t ctr); |
| 287 | uint64_t crypto_chacha20_x(uint8_t *cipher_text, |
| 288 | const uint8_t *plain_text, |
| 289 | size_t text_size, |
| 290 | const uint8_t key[32], |
| 291 | const uint8_t nonce[24], |
| 292 | uint64_t ctr); |
| 293 | |
| 294 | |
| 295 | // Poly 1305 |
| 296 | // --------- |
| 297 | |
| 298 | // This is a *one time* authenticator. |
| 299 | // Disclosing the mac reveals the key. |
| 300 | // See crypto_lock() on how to use it properly. |
| 301 | |
| 302 | // Direct interface |
| 303 | void crypto_poly1305(uint8_t mac[16], |
| 304 | const uint8_t *message, size_t message_size, |
| 305 | const uint8_t key[32]); |
| 306 | |
| 307 | // Incremental interface |
| 308 | typedef struct { |
| 309 | // Do not rely on the size or contents of this type, |
| 310 | // for they may change without notice. |
| 311 | uint8_t c[16]; // chunk of the message |
| 312 | size_t c_idx; // How many bytes are there in the chunk. |
| 313 | uint32_t r [4]; // constant multiplier (from the secret key) |
| 314 | uint32_t pad[4]; // random number added at the end (from the secret key) |
| 315 | uint32_t h [5]; // accumulated hash |
| 316 | } crypto_poly1305_ctx; |
| 317 | |
| 318 | void crypto_poly1305_init (crypto_poly1305_ctx *ctx, const uint8_t key[32]); |
| 319 | void crypto_poly1305_update(crypto_poly1305_ctx *ctx, |
| 320 | const uint8_t *message, size_t message_size); |
| 321 | void crypto_poly1305_final (crypto_poly1305_ctx *ctx, uint8_t mac[16]); |
| 322 | |
| 323 | |
| 324 | // Elligator 2 |
| 325 | // ----------- |
| 326 | |
| 327 | // Elligator mappings proper |
| 328 | void crypto_elligator_map(uint8_t curve [32], const uint8_t hidden[32]); |
| 329 | int crypto_elligator_rev(uint8_t hidden[32], const uint8_t curve [32], |
| 330 | uint8_t tweak); |
| 331 | |
| 332 | // Easy to use key pair generation |
| 333 | void crypto_elligator_key_pair(uint8_t hidden[32], uint8_t secret_key[32], |
| 334 | uint8_t seed[32]); |
| 335 | |
| 336 | #ifdef MONOCYPHER_CPP_NAMESPACE |
| 337 | } |
| 338 | #elif defined(__cplusplus) |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | #endif // MONOCYPHER_H |