blob: 3996cd4b2c054c75f5992369d4971ecf0d82086f [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ecc.h - TinyCrypt interface to common ECC functions */
2
Simon Butcher92c3d1f2019-09-09 17:25:08 +01003/*
4 * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved.
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
Jarno Lamsa18987a42019-04-24 15:40:43 +03008/* Copyright (c) 2014, Kenneth MacKay
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice, this
15 * list of conditions and the following disclaimer.
16 *
17 * * Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions are met:
39 *
40 * - Redistributions of source code must retain the above copyright notice,
41 * this list of conditions and the following disclaimer.
42 *
43 * - Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 *
47 * - Neither the name of Intel Corporation nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
52 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
55 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 */
63
64/**
65 * @file
66 * @brief -- Interface to common ECC functions.
67 *
68 * Overview: This software is an implementation of common functions
69 * necessary to elliptic curve cryptography. This implementation uses
70 * curve NIST p-256.
71 *
72 * Security: The curve NIST p-256 provides approximately 128 bits of security.
73 *
74 */
75
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +020076#if defined(MBEDTLS_USE_TINYCRYPT)
Jarno Lamsa18987a42019-04-24 15:40:43 +030077#ifndef __TC_UECC_H__
78#define __TC_UECC_H__
79
80#include <stdint.h>
81
82#ifdef __cplusplus
83extern "C" {
84#endif
85
86/* Word size (4 bytes considering 32-bits architectures) */
87#define uECC_WORD_SIZE 4
88
89/* setting max number of calls to prng: */
90#ifndef uECC_RNG_MAX_TRIES
91#define uECC_RNG_MAX_TRIES 64
92#endif
93
94/* defining data types to store word and bit counts: */
95typedef int8_t wordcount_t;
96typedef int16_t bitcount_t;
97/* defining data type for comparison result: */
98typedef int8_t cmpresult_t;
99/* defining data type to store ECC coordinate/point in 32bits words: */
100typedef unsigned int uECC_word_t;
101/* defining data type to store an ECC coordinate/point in 64bits words: */
102typedef uint64_t uECC_dword_t;
103
104/* defining masks useful for ecc computations: */
105#define HIGH_BIT_SET 0x80000000
106#define uECC_WORD_BITS 32
107#define uECC_WORD_BITS_SHIFT 5
108#define uECC_WORD_BITS_MASK 0x01F
109
110/* Number of words of 32 bits to represent an element of the the curve p-256: */
111#define NUM_ECC_WORDS 8
112/* Number of bytes to represent an element of the the curve p-256: */
113#define NUM_ECC_BYTES (uECC_WORD_SIZE*NUM_ECC_WORDS)
114
115/* structure that represents an elliptic curve (e.g. p256):*/
116struct uECC_Curve_t;
117typedef const struct uECC_Curve_t * uECC_Curve;
118struct uECC_Curve_t {
119 wordcount_t num_words;
120 wordcount_t num_bytes;
121 bitcount_t num_n_bits;
122 uECC_word_t p[NUM_ECC_WORDS];
123 uECC_word_t n[NUM_ECC_WORDS];
124 uECC_word_t G[NUM_ECC_WORDS * 2];
125 uECC_word_t b[NUM_ECC_WORDS];
126 void (*double_jacobian)(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * Z1,
127 uECC_Curve curve);
128 void (*x_side)(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve);
129 void (*mmod_fast)(uECC_word_t *result, uECC_word_t *product);
130};
131
132/*
133 * @brief computes doubling of point ion jacobian coordinates, in place.
134 * @param X1 IN/OUT -- x coordinate
135 * @param Y1 IN/OUT -- y coordinate
136 * @param Z1 IN/OUT -- z coordinate
137 * @param curve IN -- elliptic curve
138 */
139void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
140 uECC_word_t * Z1, uECC_Curve curve);
141
142/*
143 * @brief Computes x^3 + ax + b. result must not overlap x.
144 * @param result OUT -- x^3 + ax + b
145 * @param x IN -- value of x
146 * @param curve IN -- elliptic curve
147 */
148void x_side_default(uECC_word_t *result, const uECC_word_t *x,
149 uECC_Curve curve);
150
151/*
152 * @brief Computes result = product % curve_p
153 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
154 * @param result OUT -- product % curve_p
155 * @param product IN -- value to be reduced mod curve_p
156 */
157void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int *product);
158
159/* Bytes to words ordering: */
160#define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) 0x##d##c##b##a, 0x##h##g##f##e
161#define BYTES_TO_WORDS_4(a, b, c, d) 0x##d##c##b##a
162#define BITS_TO_WORDS(num_bits) \
163 ((num_bits + ((uECC_WORD_SIZE * 8) - 1)) / (uECC_WORD_SIZE * 8))
164#define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8)
165
166/* definition of curve NIST p-256: */
167static const struct uECC_Curve_t curve_secp256r1 = {
168 NUM_ECC_WORDS,
169 NUM_ECC_BYTES,
170 256, /* num_n_bits */ {
171 BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
172 BYTES_TO_WORDS_8(FF, FF, FF, FF, 00, 00, 00, 00),
173 BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00),
174 BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF)
175 }, {
176 BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3),
177 BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC),
178 BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
179 BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF)
180 }, {
181 BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4),
182 BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77),
183 BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8),
184 BYTES_TO_WORDS_8(47, 42, 2C, E1, F2, D1, 17, 6B),
185
186 BYTES_TO_WORDS_8(F5, 51, BF, 37, 68, 40, B6, CB),
187 BYTES_TO_WORDS_8(CE, 5E, 31, 6B, 57, 33, CE, 2B),
188 BYTES_TO_WORDS_8(16, 9E, 0F, 7C, 4A, EB, E7, 8E),
189 BYTES_TO_WORDS_8(9B, 7F, 1A, FE, E2, 42, E3, 4F)
190 }, {
191 BYTES_TO_WORDS_8(4B, 60, D2, 27, 3E, 3C, CE, 3B),
192 BYTES_TO_WORDS_8(F6, B0, 53, CC, B0, 06, 1D, 65),
193 BYTES_TO_WORDS_8(BC, 86, 98, 76, 55, BD, EB, B3),
194 BYTES_TO_WORDS_8(E7, 93, 3A, AA, D8, 35, C6, 5A)
195 },
196 &double_jacobian_default,
197 &x_side_default,
198 &vli_mmod_fast_secp256r1
199};
200
201uECC_Curve uECC_secp256r1(void);
202
203/*
204 * @brief Generates a random integer in the range 0 < random < top.
205 * Both random and top have num_words words.
206 * @param random OUT -- random integer in the range 0 < random < top
207 * @param top IN -- upper limit
208 * @param num_words IN -- number of words
209 * @return a random integer in the range 0 < random < top
210 */
211int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
212 wordcount_t num_words);
213
214
215/* uECC_RNG_Function type
216 * The RNG function should fill 'size' random bytes into 'dest'. It should
217 * return 1 if 'dest' was filled with random data, or 0 if the random data could
218 * not be generated. The filled-in values should be either truly random, or from
219 * a cryptographically-secure PRNG.
220 *
221 * A correctly functioning RNG function must be set (using uECC_set_rng())
222 * before calling uECC_make_key() or uECC_sign().
223 *
224 * Setting a correctly functioning RNG function improves the resistance to
225 * side-channel attacks for uECC_shared_secret().
226 *
227 * A correct RNG function is set by default. If you are building on another
228 * POSIX-compliant system that supports /dev/random or /dev/urandom, you can
229 * define uECC_POSIX to use the predefined RNG.
230 */
231typedef int(*uECC_RNG_Function)(uint8_t *dest, unsigned int size);
232
233/*
234 * @brief Set the function that will be used to generate random bytes. The RNG
235 * function should return 1 if the random data was generated, or 0 if the random
236 * data could not be generated.
237 *
238 * @note On platforms where there is no predefined RNG function, this must be
239 * called before uECC_make_key() or uECC_sign() are used.
240 *
241 * @param rng_function IN -- function that will be used to generate random bytes
242 */
243void uECC_set_rng(uECC_RNG_Function rng_function);
244
245/*
246 * @brief provides current uECC_RNG_Function.
247 * @return Returns the function that will be used to generate random bytes.
248 */
249uECC_RNG_Function uECC_get_rng(void);
250
251/*
252 * @brief computes the size of a private key for the curve in bytes.
253 * @param curve IN -- elliptic curve
254 * @return size of a private key for the curve in bytes.
255 */
256int uECC_curve_private_key_size(uECC_Curve curve);
257
258/*
259 * @brief computes the size of a public key for the curve in bytes.
260 * @param curve IN -- elliptic curve
261 * @return the size of a public key for the curve in bytes.
262 */
263int uECC_curve_public_key_size(uECC_Curve curve);
264
265/*
266 * @brief Compute the corresponding public key for a private key.
267 * @param private_key IN -- The private key to compute the public key for
268 * @param public_key OUT -- Will be filled in with the corresponding public key
269 * @param curve
270 * @return Returns 1 if key was computed successfully, 0 if an error occurred.
271 */
272int uECC_compute_public_key(const uint8_t *private_key,
273 uint8_t *public_key, uECC_Curve curve);
274
275/*
276 * @brief Compute public-key.
277 * @return corresponding public-key.
278 * @param result OUT -- public-key
279 * @param private_key IN -- private-key
280 * @param curve IN -- elliptic curve
281 */
282uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
283 uECC_word_t *private_key, uECC_Curve curve);
284
285/*
286 * @brief Regularize the bitcount for the private key so that attackers cannot
287 * use a side channel attack to learn the number of leading zeros.
288 * @return Regularized k
289 * @param k IN -- private-key
290 * @param k0 IN/OUT -- regularized k
291 * @param k1 IN/OUT -- regularized k
292 * @param curve IN -- elliptic curve
293 */
294uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
295 uECC_word_t *k1, uECC_Curve curve);
296
297/*
298 * @brief Point multiplication algorithm using Montgomery's ladder with co-Z
299 * coordinates. See http://eprint.iacr.org/2011/338.pdf.
300 * @note Result may overlap point.
301 * @param result OUT -- returns scalar*point
302 * @param point IN -- elliptic curve point
303 * @param scalar IN -- scalar
304 * @param initial_Z IN -- initial value for z
305 * @param num_bits IN -- number of bits in scalar
306 * @param curve IN -- elliptic curve
307 */
308void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
309 const uECC_word_t * scalar, const uECC_word_t * initial_Z,
310 bitcount_t num_bits, uECC_Curve curve);
311
312/*
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100313 * @brief Point multiplication algorithm using Montgomery's ladder with co-Z
314 * coordinates. See http://eprint.iacr.org/2011/338.pdf.
315 * Uses scalar regularization and coordinate randomization (if a global RNG
316 * function is set) in order to protect against some side channel attacks.
317 * @note Result may overlap point.
318 * @param result OUT -- returns scalar*point
319 * @param point IN -- elliptic curve point
320 * @param scalar IN -- scalar
321 * @param curve IN -- elliptic curve
322 */
323int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
324 const uECC_word_t * scalar, uECC_Curve curve);
325
326/*
Jarno Lamsa18987a42019-04-24 15:40:43 +0300327 * @brief Constant-time comparison to zero - secure way to compare long integers
328 * @param vli IN -- very long integer
329 * @param num_words IN -- number of words in the vli
330 * @return 1 if vli == 0, 0 otherwise.
331 */
332uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words);
333
334/*
335 * @brief Check if 'point' is the point at infinity
336 * @param point IN -- elliptic curve point
337 * @param curve IN -- elliptic curve
338 * @return if 'point' is the point at infinity, 0 otherwise.
339 */
340uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve);
341
342/*
343 * @brief computes the sign of left - right, in constant time.
344 * @param left IN -- left term to be compared
345 * @param right IN -- right term to be compared
346 * @param num_words IN -- number of words
347 * @return the sign of left - right
348 */
349cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
350 wordcount_t num_words);
351
352/*
353 * @brief computes sign of left - right, not in constant time.
354 * @note should not be used if inputs are part of a secret
355 * @param left IN -- left term to be compared
356 * @param right IN -- right term to be compared
357 * @param num_words IN -- number of words
358 * @return the sign of left - right
359 */
360cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, const uECC_word_t *right,
361 wordcount_t num_words);
362
363/*
364 * @brief Computes result = (left - right) % mod.
365 * @note Assumes that (left < mod) and (right < mod), and that result does not
366 * overlap mod.
367 * @param result OUT -- (left - right) % mod
368 * @param left IN -- leftright term in modular subtraction
369 * @param right IN -- right term in modular subtraction
370 * @param mod IN -- mod
371 * @param num_words IN -- number of words
372 */
373void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
374 const uECC_word_t *right, const uECC_word_t *mod,
375 wordcount_t num_words);
376
377/*
378 * @brief Computes P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) or
379 * P => P', Q => P + Q
380 * @note assumes Input P = (x1, y1, Z), Q = (x2, y2, Z)
381 * @param X1 IN -- x coordinate of P
382 * @param Y1 IN -- y coordinate of P
383 * @param X2 IN -- x coordinate of Q
384 * @param Y2 IN -- y coordinate of Q
385 * @param curve IN -- elliptic curve
386 */
387void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * X2,
388 uECC_word_t * Y2, uECC_Curve curve);
389
390/*
391 * @brief Computes (x1 * z^2, y1 * z^3)
392 * @param X1 IN -- previous x1 coordinate
393 * @param Y1 IN -- previous y1 coordinate
394 * @param Z IN -- z value
395 * @param curve IN -- elliptic curve
396 */
397void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z,
398 uECC_Curve curve);
399
400/*
401 * @brief Check if bit is set.
402 * @return Returns nonzero if bit 'bit' of vli is set.
403 * @warning It is assumed that the value provided in 'bit' is within the
404 * boundaries of the word-array 'vli'.
405 * @note The bit ordering layout assumed for vli is: {31, 30, ..., 0},
406 * {63, 62, ..., 32}, {95, 94, ..., 64}, {127, 126,..., 96} for a vli consisting
407 * of 4 uECC_word_t elements.
408 */
409uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit);
410
411/*
412 * @brief Computes result = product % mod, where product is 2N words long.
413 * @param result OUT -- product % mod
414 * @param mod IN -- module
415 * @param num_words IN -- number of words
416 * @warning Currently only designed to work for curve_p or curve_n.
417 */
418void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
419 const uECC_word_t *mod, wordcount_t num_words);
420
421/*
422 * @brief Computes modular product (using curve->mmod_fast)
423 * @param result OUT -- (left * right) mod % curve_p
424 * @param left IN -- left term in product
425 * @param right IN -- right term in product
426 * @param curve IN -- elliptic curve
427 */
428void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
429 const uECC_word_t *right, uECC_Curve curve);
430
431/*
432 * @brief Computes result = left - right.
433 * @note Can modify in place.
434 * @param result OUT -- left - right
435 * @param left IN -- left term in subtraction
436 * @param right IN -- right term in subtraction
437 * @param num_words IN -- number of words
438 * @return borrow
439 */
440uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
441 const uECC_word_t *right, wordcount_t num_words);
442
443/*
444 * @brief Constant-time comparison function(secure way to compare long ints)
445 * @param left IN -- left term in comparison
446 * @param right IN -- right term in comparison
447 * @param num_words IN -- number of words
448 * @return Returns 0 if left == right, 1 otherwise.
449 */
450uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
451 wordcount_t num_words);
452
453/*
454 * @brief Computes (left * right) % mod
455 * @param result OUT -- (left * right) % mod
456 * @param left IN -- left term in product
457 * @param right IN -- right term in product
458 * @param mod IN -- mod
459 * @param num_words IN -- number of words
460 */
461void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
462 const uECC_word_t *right, const uECC_word_t *mod,
463 wordcount_t num_words);
464
465/*
466 * @brief Computes (1 / input) % mod
467 * @note All VLIs are the same size.
468 * @note See "Euclid's GCD to Montgomery Multiplication to the Great Divide"
469 * @param result OUT -- (1 / input) % mod
470 * @param input IN -- value to be modular inverted
471 * @param mod IN -- mod
472 * @param num_words -- number of words
473 */
474void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
475 const uECC_word_t *mod, wordcount_t num_words);
476
477/*
478 * @brief Sets dest = src.
479 * @param dest OUT -- destination buffer
480 * @param src IN -- origin buffer
481 * @param num_words IN -- number of words
482 */
483void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
484 wordcount_t num_words);
485
486/*
487 * @brief Computes (left + right) % mod.
488 * @note Assumes that (left < mod) and right < mod), and that result does not
489 * overlap mod.
490 * @param result OUT -- (left + right) % mod.
491 * @param left IN -- left term in addition
492 * @param right IN -- right term in addition
493 * @param mod IN -- mod
494 * @param num_words IN -- number of words
495 */
496void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
497 const uECC_word_t *right, const uECC_word_t *mod,
498 wordcount_t num_words);
499
500/*
501 * @brief Counts the number of bits required to represent vli.
502 * @param vli IN -- very long integer
503 * @param max_words IN -- number of words
504 * @return number of bits in given vli
505 */
506bitcount_t uECC_vli_numBits(const uECC_word_t *vli,
507 const wordcount_t max_words);
508
509/*
510 * @brief Erases (set to 0) vli
511 * @param vli IN -- very long integer
512 * @param num_words IN -- number of words
513 */
514void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words);
515
516/*
517 * @brief check if it is a valid point in the curve
518 * @param point IN -- point to be checked
519 * @param curve IN -- elliptic curve
520 * @return 0 if point is valid
521 * @exception returns -1 if it is a point at infinity
522 * @exception returns -2 if x or y is smaller than p,
523 * @exception returns -3 if y^2 != x^3 + ax + b.
524 */
525int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve);
526
527/*
528 * @brief Check if a public key is valid.
529 * @param public_key IN -- The public key to be checked.
530 * @return returns 0 if the public key is valid
531 * @exception returns -1 if it is a point at infinity
532 * @exception returns -2 if x or y is smaller than p,
533 * @exception returns -3 if y^2 != x^3 + ax + b.
534 * @exception returns -4 if public key is the group generator.
535 *
536 * @note Note that you are not required to check for a valid public key before
537 * using any other uECC functions. However, you may wish to avoid spending CPU
538 * time computing a shared secret or verifying a signature using an invalid
539 * public key.
540 */
541int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve);
542
543 /*
544 * @brief Converts an integer in uECC native format to big-endian bytes.
545 * @param bytes OUT -- bytes representation
546 * @param num_bytes IN -- number of bytes
547 * @param native IN -- uECC native representation
548 */
549void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
550 const unsigned int *native);
551
552/*
553 * @brief Converts big-endian bytes to an integer in uECC native format.
554 * @param native OUT -- uECC native representation
555 * @param bytes IN -- bytes representation
556 * @param num_bytes IN -- number of bytes
557 */
558void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
559 int num_bytes);
560
561#ifdef __cplusplus
562}
563#endif
564
565#endif /* __TC_UECC_H__ */
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200566#endif /* MBEDTLS_USE_TINYCRYPT */