blob: df8f1dfbb0866ea8480a483ee12cd835d0076783 [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
76#ifndef __TC_UECC_H__
77#define __TC_UECC_H__
78
79#include <stdint.h>
80
81#ifdef __cplusplus
82extern "C" {
83#endif
84
Manuel Pégourié-Gonnardc05f1502019-11-06 10:15:26 +010085/* Return values for functions, chosen with large Hamming distances between
86 * them (especially to SUCESS) to mitigate the impact of fault injection
87 * attacks flipping a low number of bits. */
88#define UECC_SUCCESS 0
89#define UECC_FAILURE 0x75555555
90#define UECC_ATTACK_DETECTED 0x7aaaaaaa
91
Jarno Lamsa18987a42019-04-24 15:40:43 +030092/* Word size (4 bytes considering 32-bits architectures) */
93#define uECC_WORD_SIZE 4
94
95/* setting max number of calls to prng: */
96#ifndef uECC_RNG_MAX_TRIES
97#define uECC_RNG_MAX_TRIES 64
98#endif
99
100/* defining data types to store word and bit counts: */
101typedef int8_t wordcount_t;
102typedef int16_t bitcount_t;
103/* defining data type for comparison result: */
104typedef int8_t cmpresult_t;
105/* defining data type to store ECC coordinate/point in 32bits words: */
106typedef unsigned int uECC_word_t;
107/* defining data type to store an ECC coordinate/point in 64bits words: */
108typedef uint64_t uECC_dword_t;
109
110/* defining masks useful for ecc computations: */
111#define HIGH_BIT_SET 0x80000000
112#define uECC_WORD_BITS 32
113#define uECC_WORD_BITS_SHIFT 5
114#define uECC_WORD_BITS_MASK 0x01F
115
116/* Number of words of 32 bits to represent an element of the the curve p-256: */
117#define NUM_ECC_WORDS 8
118/* Number of bytes to represent an element of the the curve p-256: */
119#define NUM_ECC_BYTES (uECC_WORD_SIZE*NUM_ECC_WORDS)
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100120#define NUM_ECC_BITS 256
Jarno Lamsa18987a42019-04-24 15:40:43 +0300121
122/* structure that represents an elliptic curve (e.g. p256):*/
123struct uECC_Curve_t;
124typedef const struct uECC_Curve_t * uECC_Curve;
125struct uECC_Curve_t {
126 wordcount_t num_words;
127 wordcount_t num_bytes;
128 bitcount_t num_n_bits;
129 uECC_word_t p[NUM_ECC_WORDS];
130 uECC_word_t n[NUM_ECC_WORDS];
131 uECC_word_t G[NUM_ECC_WORDS * 2];
132 uECC_word_t b[NUM_ECC_WORDS];
133 void (*double_jacobian)(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * Z1,
134 uECC_Curve curve);
135 void (*x_side)(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve);
136 void (*mmod_fast)(uECC_word_t *result, uECC_word_t *product);
137};
138
139/*
140 * @brief computes doubling of point ion jacobian coordinates, in place.
141 * @param X1 IN/OUT -- x coordinate
142 * @param Y1 IN/OUT -- y coordinate
143 * @param Z1 IN/OUT -- z coordinate
144 * @param curve IN -- elliptic curve
145 */
146void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
147 uECC_word_t * Z1, uECC_Curve curve);
148
149/*
150 * @brief Computes x^3 + ax + b. result must not overlap x.
151 * @param result OUT -- x^3 + ax + b
152 * @param x IN -- value of x
153 * @param curve IN -- elliptic curve
154 */
155void x_side_default(uECC_word_t *result, const uECC_word_t *x,
156 uECC_Curve curve);
157
158/*
159 * @brief Computes result = product % curve_p
160 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
161 * @param result OUT -- product % curve_p
162 * @param product IN -- value to be reduced mod curve_p
163 */
164void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int *product);
165
166/* Bytes to words ordering: */
167#define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) 0x##d##c##b##a, 0x##h##g##f##e
168#define BYTES_TO_WORDS_4(a, b, c, d) 0x##d##c##b##a
169#define BITS_TO_WORDS(num_bits) \
170 ((num_bits + ((uECC_WORD_SIZE * 8) - 1)) / (uECC_WORD_SIZE * 8))
171#define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8)
172
173/* definition of curve NIST p-256: */
174static const struct uECC_Curve_t curve_secp256r1 = {
175 NUM_ECC_WORDS,
176 NUM_ECC_BYTES,
177 256, /* num_n_bits */ {
178 BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
179 BYTES_TO_WORDS_8(FF, FF, FF, FF, 00, 00, 00, 00),
180 BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00),
181 BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF)
182 }, {
183 BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3),
184 BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC),
185 BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
186 BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF)
187 }, {
188 BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4),
189 BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77),
190 BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8),
191 BYTES_TO_WORDS_8(47, 42, 2C, E1, F2, D1, 17, 6B),
192
193 BYTES_TO_WORDS_8(F5, 51, BF, 37, 68, 40, B6, CB),
194 BYTES_TO_WORDS_8(CE, 5E, 31, 6B, 57, 33, CE, 2B),
195 BYTES_TO_WORDS_8(16, 9E, 0F, 7C, 4A, EB, E7, 8E),
196 BYTES_TO_WORDS_8(9B, 7F, 1A, FE, E2, 42, E3, 4F)
197 }, {
198 BYTES_TO_WORDS_8(4B, 60, D2, 27, 3E, 3C, CE, 3B),
199 BYTES_TO_WORDS_8(F6, B0, 53, CC, B0, 06, 1D, 65),
200 BYTES_TO_WORDS_8(BC, 86, 98, 76, 55, BD, EB, B3),
201 BYTES_TO_WORDS_8(E7, 93, 3A, AA, D8, 35, C6, 5A)
202 },
203 &double_jacobian_default,
204 &x_side_default,
205 &vli_mmod_fast_secp256r1
206};
207
208uECC_Curve uECC_secp256r1(void);
209
210/*
211 * @brief Generates a random integer in the range 0 < random < top.
212 * Both random and top have num_words words.
213 * @param random OUT -- random integer in the range 0 < random < top
214 * @param top IN -- upper limit
215 * @param num_words IN -- number of words
216 * @return a random integer in the range 0 < random < top
217 */
218int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
219 wordcount_t num_words);
220
221
222/* uECC_RNG_Function type
223 * The RNG function should fill 'size' random bytes into 'dest'. It should
224 * return 1 if 'dest' was filled with random data, or 0 if the random data could
225 * not be generated. The filled-in values should be either truly random, or from
226 * a cryptographically-secure PRNG.
227 *
228 * A correctly functioning RNG function must be set (using uECC_set_rng())
229 * before calling uECC_make_key() or uECC_sign().
230 *
231 * Setting a correctly functioning RNG function improves the resistance to
232 * side-channel attacks for uECC_shared_secret().
233 *
234 * A correct RNG function is set by default. If you are building on another
235 * POSIX-compliant system that supports /dev/random or /dev/urandom, you can
236 * define uECC_POSIX to use the predefined RNG.
237 */
238typedef int(*uECC_RNG_Function)(uint8_t *dest, unsigned int size);
239
240/*
241 * @brief Set the function that will be used to generate random bytes. The RNG
242 * function should return 1 if the random data was generated, or 0 if the random
243 * data could not be generated.
244 *
245 * @note On platforms where there is no predefined RNG function, this must be
246 * called before uECC_make_key() or uECC_sign() are used.
247 *
248 * @param rng_function IN -- function that will be used to generate random bytes
249 */
250void uECC_set_rng(uECC_RNG_Function rng_function);
251
252/*
253 * @brief provides current uECC_RNG_Function.
254 * @return Returns the function that will be used to generate random bytes.
255 */
256uECC_RNG_Function uECC_get_rng(void);
257
258/*
259 * @brief computes the size of a private key for the curve in bytes.
260 * @param curve IN -- elliptic curve
261 * @return size of a private key for the curve in bytes.
262 */
263int uECC_curve_private_key_size(uECC_Curve curve);
264
265/*
266 * @brief computes the size of a public key for the curve in bytes.
267 * @param curve IN -- elliptic curve
268 * @return the size of a public key for the curve in bytes.
269 */
270int uECC_curve_public_key_size(uECC_Curve curve);
271
272/*
273 * @brief Compute the corresponding public key for a private key.
274 * @param private_key IN -- The private key to compute the public key for
275 * @param public_key OUT -- Will be filled in with the corresponding public key
276 * @param curve
277 * @return Returns 1 if key was computed successfully, 0 if an error occurred.
278 */
279int uECC_compute_public_key(const uint8_t *private_key,
280 uint8_t *public_key, uECC_Curve curve);
281
282/*
283 * @brief Compute public-key.
284 * @return corresponding public-key.
285 * @param result OUT -- public-key
286 * @param private_key IN -- private-key
287 * @param curve IN -- elliptic curve
288 */
289uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
290 uECC_word_t *private_key, uECC_Curve curve);
291
292/*
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100293 * @brief Point multiplication algorithm using Montgomery's ladder with co-Z
294 * coordinates. See http://eprint.iacr.org/2011/338.pdf.
295 * Uses scalar regularization and coordinate randomization (if a global RNG
296 * function is set) in order to protect against some side channel attacks.
297 * @note Result may overlap point.
298 * @param result OUT -- returns scalar*point
299 * @param point IN -- elliptic curve point
300 * @param scalar IN -- scalar
301 * @param curve IN -- elliptic curve
302 */
303int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
304 const uECC_word_t * scalar, uECC_Curve curve);
305
306/*
Jarno Lamsa18987a42019-04-24 15:40:43 +0300307 * @brief Constant-time comparison to zero - secure way to compare long integers
308 * @param vli IN -- very long integer
309 * @param num_words IN -- number of words in the vli
310 * @return 1 if vli == 0, 0 otherwise.
311 */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100312uECC_word_t uECC_vli_isZero(const uECC_word_t *vli);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300313
314/*
315 * @brief Check if 'point' is the point at infinity
316 * @param point IN -- elliptic curve point
317 * @param curve IN -- elliptic curve
318 * @return if 'point' is the point at infinity, 0 otherwise.
319 */
320uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve);
321
322/*
323 * @brief computes the sign of left - right, in constant time.
324 * @param left IN -- left term to be compared
325 * @param right IN -- right term to be compared
326 * @param num_words IN -- number of words
327 * @return the sign of left - right
328 */
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +0100329cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300330
331/*
332 * @brief computes sign of left - right, not in constant time.
333 * @note should not be used if inputs are part of a secret
334 * @param left IN -- left term to be compared
335 * @param right IN -- right term to be compared
336 * @param num_words IN -- number of words
337 * @return the sign of left - right
338 */
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100339cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, const uECC_word_t *right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300340
341/*
342 * @brief Computes result = (left - right) % mod.
343 * @note Assumes that (left < mod) and (right < mod), and that result does not
344 * overlap mod.
345 * @param result OUT -- (left - right) % mod
346 * @param left IN -- leftright term in modular subtraction
347 * @param right IN -- right term in modular subtraction
348 * @param mod IN -- mod
349 * @param num_words IN -- number of words
350 */
351void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100352 const uECC_word_t *right, const uECC_word_t *mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300353
354/*
355 * @brief Computes P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) or
356 * P => P', Q => P + Q
357 * @note assumes Input P = (x1, y1, Z), Q = (x2, y2, Z)
358 * @param X1 IN -- x coordinate of P
359 * @param Y1 IN -- y coordinate of P
360 * @param X2 IN -- x coordinate of Q
361 * @param Y2 IN -- y coordinate of Q
362 * @param curve IN -- elliptic curve
363 */
364void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * X2,
365 uECC_word_t * Y2, uECC_Curve curve);
366
367/*
368 * @brief Computes (x1 * z^2, y1 * z^3)
369 * @param X1 IN -- previous x1 coordinate
370 * @param Y1 IN -- previous y1 coordinate
371 * @param Z IN -- z value
372 * @param curve IN -- elliptic curve
373 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100374void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300375
376/*
377 * @brief Check if bit is set.
378 * @return Returns nonzero if bit 'bit' of vli is set.
379 * @warning It is assumed that the value provided in 'bit' is within the
380 * boundaries of the word-array 'vli'.
381 * @note The bit ordering layout assumed for vli is: {31, 30, ..., 0},
382 * {63, 62, ..., 32}, {95, 94, ..., 64}, {127, 126,..., 96} for a vli consisting
383 * of 4 uECC_word_t elements.
384 */
385uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit);
386
387/*
388 * @brief Computes result = product % mod, where product is 2N words long.
389 * @param result OUT -- product % mod
390 * @param mod IN -- module
391 * @param num_words IN -- number of words
392 * @warning Currently only designed to work for curve_p or curve_n.
393 */
394void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100395 const uECC_word_t *mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300396
397/*
398 * @brief Computes modular product (using curve->mmod_fast)
399 * @param result OUT -- (left * right) mod % curve_p
400 * @param left IN -- left term in product
401 * @param right IN -- right term in product
402 * @param curve IN -- elliptic curve
403 */
404void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100405 const uECC_word_t *right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300406
407/*
408 * @brief Computes result = left - right.
409 * @note Can modify in place.
410 * @param result OUT -- left - right
411 * @param left IN -- left term in subtraction
412 * @param right IN -- right term in subtraction
413 * @param num_words IN -- number of words
414 * @return borrow
415 */
416uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100417 const uECC_word_t *right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300418
419/*
420 * @brief Constant-time comparison function(secure way to compare long ints)
421 * @param left IN -- left term in comparison
422 * @param right IN -- right term in comparison
423 * @param num_words IN -- number of words
Manuel Pégourié-Gonnard2b6312b2019-11-06 10:42:02 +0100424 * @return Returns 0 if left == right, non-zero otherwise.
Jarno Lamsa18987a42019-04-24 15:40:43 +0300425 */
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +0100426uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300427
428/*
429 * @brief Computes (left * right) % mod
430 * @param result OUT -- (left * right) % mod
431 * @param left IN -- left term in product
432 * @param right IN -- right term in product
433 * @param mod IN -- mod
434 * @param num_words IN -- number of words
435 */
436void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100437 const uECC_word_t *right, const uECC_word_t *mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300438
439/*
440 * @brief Computes (1 / input) % mod
441 * @note All VLIs are the same size.
442 * @note See "Euclid's GCD to Montgomery Multiplication to the Great Divide"
443 * @param result OUT -- (1 / input) % mod
444 * @param input IN -- value to be modular inverted
445 * @param mod IN -- mod
446 * @param num_words -- number of words
447 */
448void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100449 const uECC_word_t *mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300450
451/*
452 * @brief Sets dest = src.
453 * @param dest OUT -- destination buffer
454 * @param src IN -- origin buffer
455 * @param num_words IN -- number of words
456 */
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100457void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300458
459/*
460 * @brief Computes (left + right) % mod.
461 * @note Assumes that (left < mod) and right < mod), and that result does not
462 * overlap mod.
463 * @param result OUT -- (left + right) % mod.
464 * @param left IN -- left term in addition
465 * @param right IN -- right term in addition
466 * @param mod IN -- mod
467 * @param num_words IN -- number of words
468 */
469void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100470 const uECC_word_t *right, const uECC_word_t *mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300471
472/*
473 * @brief Counts the number of bits required to represent vli.
474 * @param vli IN -- very long integer
475 * @param max_words IN -- number of words
476 * @return number of bits in given vli
477 */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100478bitcount_t uECC_vli_numBits(const uECC_word_t *vli);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300479
480/*
481 * @brief Erases (set to 0) vli
482 * @param vli IN -- very long integer
483 * @param num_words IN -- number of words
484 */
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100485void uECC_vli_clear(uECC_word_t *vli);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300486
487/*
488 * @brief check if it is a valid point in the curve
489 * @param point IN -- point to be checked
490 * @param curve IN -- elliptic curve
491 * @return 0 if point is valid
492 * @exception returns -1 if it is a point at infinity
493 * @exception returns -2 if x or y is smaller than p,
494 * @exception returns -3 if y^2 != x^3 + ax + b.
495 */
496int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve);
497
498/*
499 * @brief Check if a public key is valid.
500 * @param public_key IN -- The public key to be checked.
501 * @return returns 0 if the public key is valid
502 * @exception returns -1 if it is a point at infinity
503 * @exception returns -2 if x or y is smaller than p,
504 * @exception returns -3 if y^2 != x^3 + ax + b.
505 * @exception returns -4 if public key is the group generator.
506 *
507 * @note Note that you are not required to check for a valid public key before
508 * using any other uECC functions. However, you may wish to avoid spending CPU
509 * time computing a shared secret or verifying a signature using an invalid
510 * public key.
511 */
512int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve);
513
514 /*
515 * @brief Converts an integer in uECC native format to big-endian bytes.
516 * @param bytes OUT -- bytes representation
517 * @param num_bytes IN -- number of bytes
518 * @param native IN -- uECC native representation
519 */
520void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
521 const unsigned int *native);
522
523/*
524 * @brief Converts big-endian bytes to an integer in uECC native format.
525 * @param native OUT -- uECC native representation
526 * @param bytes IN -- bytes representation
527 * @param num_bytes IN -- number of bytes
528 */
529void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
530 int num_bytes);
531
532#ifdef __cplusplus
533}
534#endif
535
536#endif /* __TC_UECC_H__ */