CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_CheckFirmware/monocypher.h
Views: 1798
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
54
#ifndef MONOCYPHER_H
55
#define MONOCYPHER_H
56
57
#include <stddef.h>
58
#include <stdint.h>
59
60
////////////////////////
61
/// Type definitions ///
62
////////////////////////
63
64
// Vtable for EdDSA with a custom hash.
65
// Instantiate it to define a custom hash.
66
// Its size, contents, and layout, are part of the public API.
67
typedef struct {
68
void (*hash)(uint8_t hash[64], const uint8_t *message, size_t message_size);
69
void (*init )(void *ctx);
70
void (*update)(void *ctx, const uint8_t *message, size_t message_size);
71
void (*final )(void *ctx, uint8_t hash[64]);
72
size_t ctx_size;
73
} crypto_sign_vtable;
74
75
// Do not rely on the size or contents of any of the types below,
76
// they may change without notice.
77
78
// Poly1305
79
typedef struct {
80
uint32_t r[4]; // constant multiplier (from the secret key)
81
uint32_t h[5]; // accumulated hash
82
uint32_t c[5]; // chunk of the message
83
uint32_t pad[4]; // random number added at the end (from the secret key)
84
size_t c_idx; // How many bytes are there in the chunk.
85
} crypto_poly1305_ctx;
86
87
// Hash (Blake2b)
88
typedef struct {
89
uint64_t hash[8];
90
uint64_t input_offset[2];
91
uint64_t input[16];
92
size_t input_idx;
93
size_t hash_size;
94
} crypto_blake2b_ctx;
95
96
// Signatures (EdDSA)
97
typedef struct {
98
const crypto_sign_vtable *hash;
99
uint8_t buf[96];
100
uint8_t pk [32];
101
} crypto_sign_ctx_abstract;
102
typedef crypto_sign_ctx_abstract crypto_check_ctx_abstract;
103
104
typedef struct {
105
crypto_sign_ctx_abstract ctx;
106
crypto_blake2b_ctx hash;
107
} crypto_sign_ctx;
108
typedef crypto_sign_ctx crypto_check_ctx;
109
110
////////////////////////////
111
/// High level interface ///
112
////////////////////////////
113
114
// Constant time comparisons
115
// -------------------------
116
117
// Return 0 if a and b are equal, -1 otherwise
118
int crypto_verify16(const uint8_t a[16], const uint8_t b[16]);
119
int crypto_verify32(const uint8_t a[32], const uint8_t b[32]);
120
int crypto_verify64(const uint8_t a[64], const uint8_t b[64]);
121
122
// Erase sensitive data
123
// --------------------
124
125
// Please erase all copies
126
void crypto_wipe(void *secret, size_t size);
127
128
129
// Authenticated encryption
130
// ------------------------
131
void crypto_lock(uint8_t mac[16],
132
uint8_t *cipher_text,
133
const uint8_t key[32],
134
const uint8_t nonce[24],
135
const uint8_t *plain_text, size_t text_size);
136
int crypto_unlock(uint8_t *plain_text,
137
const uint8_t key[32],
138
const uint8_t nonce[24],
139
const uint8_t mac[16],
140
const uint8_t *cipher_text, size_t text_size);
141
142
// With additional data
143
void crypto_lock_aead(uint8_t mac[16],
144
uint8_t *cipher_text,
145
const uint8_t key[32],
146
const uint8_t nonce[24],
147
const uint8_t *ad , size_t ad_size,
148
const uint8_t *plain_text, size_t text_size);
149
int crypto_unlock_aead(uint8_t *plain_text,
150
const uint8_t key[32],
151
const uint8_t nonce[24],
152
const uint8_t mac[16],
153
const uint8_t *ad , size_t ad_size,
154
const uint8_t *cipher_text, size_t text_size);
155
156
157
// General purpose hash (Blake2b)
158
// ------------------------------
159
160
// Direct interface
161
void crypto_blake2b(uint8_t hash[64],
162
const uint8_t *message, size_t message_size);
163
164
void crypto_blake2b_general(uint8_t *hash , size_t hash_size,
165
const uint8_t *key , size_t key_size, // optional
166
const uint8_t *message, size_t message_size);
167
168
// Incremental interface
169
void crypto_blake2b_init (crypto_blake2b_ctx *ctx);
170
void crypto_blake2b_update(crypto_blake2b_ctx *ctx,
171
const uint8_t *message, size_t message_size);
172
void crypto_blake2b_final (crypto_blake2b_ctx *ctx, uint8_t *hash);
173
174
void crypto_blake2b_general_init(crypto_blake2b_ctx *ctx, size_t hash_size,
175
const uint8_t *key, size_t key_size);
176
177
// vtable for signatures
178
extern const crypto_sign_vtable crypto_blake2b_vtable;
179
180
181
// Password key derivation (Argon2 i)
182
// ----------------------------------
183
void crypto_argon2i(uint8_t *hash, uint32_t hash_size, // >= 4
184
void *work_area, uint32_t nb_blocks, // >= 8
185
uint32_t nb_iterations, // >= 3
186
const uint8_t *password, uint32_t password_size,
187
const uint8_t *salt, uint32_t salt_size); // >= 8
188
189
void crypto_argon2i_general(uint8_t *hash, uint32_t hash_size,// >= 4
190
void *work_area, uint32_t nb_blocks,// >= 8
191
uint32_t nb_iterations, // >= 3
192
const uint8_t *password, uint32_t password_size,
193
const uint8_t *salt, uint32_t salt_size,// >= 8
194
const uint8_t *key, uint32_t key_size,
195
const uint8_t *ad, uint32_t ad_size);
196
197
198
// Key exchange (x25519 + HChacha20)
199
// ---------------------------------
200
#define crypto_key_exchange_public_key crypto_x25519_public_key
201
void crypto_key_exchange(uint8_t shared_key [32],
202
const uint8_t your_secret_key [32],
203
const uint8_t their_public_key[32]);
204
205
206
// Signatures (EdDSA with curve25519 + Blake2b)
207
// --------------------------------------------
208
209
// Generate public key
210
void crypto_sign_public_key(uint8_t public_key[32],
211
const uint8_t secret_key[32]);
212
213
// Direct interface
214
void crypto_sign(uint8_t signature [64],
215
const uint8_t secret_key[32],
216
const uint8_t public_key[32], // optional, may be 0
217
const uint8_t *message, size_t message_size);
218
int crypto_check(const uint8_t signature [64],
219
const uint8_t public_key[32],
220
const uint8_t *message, size_t message_size);
221
222
////////////////////////////
223
/// Low level primitives ///
224
////////////////////////////
225
226
// For experts only. You have been warned.
227
228
// Chacha20
229
// --------
230
231
// Specialised hash.
232
// Used to hash X25519 shared secrets.
233
void crypto_hchacha20(uint8_t out[32],
234
const uint8_t key[32],
235
const uint8_t in [16]);
236
237
// Unauthenticated stream cipher.
238
// Don't forget to add authentication.
239
void crypto_chacha20(uint8_t *cipher_text,
240
const uint8_t *plain_text,
241
size_t text_size,
242
const uint8_t key[32],
243
const uint8_t nonce[8]);
244
void crypto_xchacha20(uint8_t *cipher_text,
245
const uint8_t *plain_text,
246
size_t text_size,
247
const uint8_t key[32],
248
const uint8_t nonce[24]);
249
void crypto_ietf_chacha20(uint8_t *cipher_text,
250
const uint8_t *plain_text,
251
size_t text_size,
252
const uint8_t key[32],
253
const uint8_t nonce[12]);
254
uint64_t crypto_chacha20_ctr(uint8_t *cipher_text,
255
const uint8_t *plain_text,
256
size_t text_size,
257
const uint8_t key[32],
258
const uint8_t nonce[8],
259
uint64_t ctr);
260
uint64_t crypto_xchacha20_ctr(uint8_t *cipher_text,
261
const uint8_t *plain_text,
262
size_t text_size,
263
const uint8_t key[32],
264
const uint8_t nonce[24],
265
uint64_t ctr);
266
uint32_t crypto_ietf_chacha20_ctr(uint8_t *cipher_text,
267
const uint8_t *plain_text,
268
size_t text_size,
269
const uint8_t key[32],
270
const uint8_t nonce[12],
271
uint32_t ctr);
272
273
// Poly 1305
274
// ---------
275
276
// This is a *one time* authenticator.
277
// Disclosing the mac reveals the key.
278
// See crypto_lock() on how to use it properly.
279
280
// Direct interface
281
void crypto_poly1305(uint8_t mac[16],
282
const uint8_t *message, size_t message_size,
283
const uint8_t key[32]);
284
285
// Incremental interface
286
void crypto_poly1305_init (crypto_poly1305_ctx *ctx, const uint8_t key[32]);
287
void crypto_poly1305_update(crypto_poly1305_ctx *ctx,
288
const uint8_t *message, size_t message_size);
289
void crypto_poly1305_final (crypto_poly1305_ctx *ctx, uint8_t mac[16]);
290
291
292
// X-25519
293
// -------
294
295
// Shared secrets are not quite random.
296
// Hash them to derive an actual shared key.
297
void crypto_x25519_public_key(uint8_t public_key[32],
298
const uint8_t secret_key[32]);
299
void crypto_x25519(uint8_t raw_shared_secret[32],
300
const uint8_t your_secret_key [32],
301
const uint8_t their_public_key [32]);
302
303
// "Dirty" versions of x25519_public_key()
304
// Only use to generate ephemeral keys you want to hide.
305
// Note that those functions leaks 3 bits of the private key.
306
void crypto_x25519_dirty_small(uint8_t pk[32], const uint8_t sk[32]);
307
void crypto_x25519_dirty_fast (uint8_t pk[32], const uint8_t sk[32]);
308
309
// scalar "division"
310
// Used for OPRF. Be aware that exponential blinding is less secure
311
// than Diffie-Hellman key exchange.
312
void crypto_x25519_inverse(uint8_t blind_salt [32],
313
const uint8_t private_key[32],
314
const uint8_t curve_point[32]);
315
316
317
// EdDSA to X25519
318
// ---------------
319
void crypto_from_eddsa_private(uint8_t x25519[32], const uint8_t eddsa[32]);
320
void crypto_from_eddsa_public (uint8_t x25519[32], const uint8_t eddsa[32]);
321
322
323
// EdDSA -- Incremental interface
324
// ------------------------------
325
326
// Signing (2 passes)
327
// Make sure the two passes hash the same message,
328
// else you might reveal the private key.
329
void crypto_sign_init_first_pass(crypto_sign_ctx_abstract *ctx,
330
const uint8_t secret_key[32],
331
const uint8_t public_key[32]);
332
void crypto_sign_update(crypto_sign_ctx_abstract *ctx,
333
const uint8_t *message, size_t message_size);
334
void crypto_sign_init_second_pass(crypto_sign_ctx_abstract *ctx);
335
// use crypto_sign_update() again.
336
void crypto_sign_final(crypto_sign_ctx_abstract *ctx, uint8_t signature[64]);
337
338
// Verification (1 pass)
339
// Make sure you don't use (parts of) the message
340
// before you're done checking it.
341
void crypto_check_init (crypto_check_ctx_abstract *ctx,
342
const uint8_t signature[64],
343
const uint8_t public_key[32]);
344
void crypto_check_update(crypto_check_ctx_abstract *ctx,
345
const uint8_t *message, size_t message_size);
346
int crypto_check_final (crypto_check_ctx_abstract *ctx);
347
348
// Custom hash interface
349
void crypto_sign_public_key_custom_hash(uint8_t public_key[32],
350
const uint8_t secret_key[32],
351
const crypto_sign_vtable *hash);
352
void crypto_sign_init_first_pass_custom_hash(crypto_sign_ctx_abstract *ctx,
353
const uint8_t secret_key[32],
354
const uint8_t public_key[32],
355
const crypto_sign_vtable *hash);
356
void crypto_check_init_custom_hash(crypto_check_ctx_abstract *ctx,
357
const uint8_t signature[64],
358
const uint8_t public_key[32],
359
const crypto_sign_vtable *hash);
360
361
// Elligator 2
362
// -----------
363
364
// Elligator mappings proper
365
void crypto_hidden_to_curve(uint8_t curve [32], const uint8_t hidden[32]);
366
int crypto_curve_to_hidden(uint8_t hidden[32], const uint8_t curve [32],
367
uint8_t tweak);
368
369
// Easy to use key pair generation
370
void crypto_hidden_key_pair(uint8_t hidden[32], uint8_t secret_key[32],
371
uint8_t seed[32]);
372
373
374
#endif // MONOCYPHER_H
375
376