Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1 | /* ecc.h - TinyCrypt interface to common ECC functions */ |
| 2 | |
Simon Butcher | 92c3d1f | 2019-09-09 17:25:08 +0100 | [diff] [blame] | 3 | /* |
| 4 | * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved. |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 8 | /* 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 |
| 82 | extern "C" { |
| 83 | #endif |
| 84 | |
Manuel Pégourié-Gonnard | c05f150 | 2019-11-06 10:15:26 +0100 | [diff] [blame] | 85 | /* 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. */ |
Andrzej Kurek | e6d8db0 | 2020-08-09 23:41:40 -0400 | [diff] [blame] | 88 | #define UECC_SUCCESS 0xCD |
| 89 | #define UECC_FAILURE 0x52 |
| 90 | #define UECC_FAULT_DETECTED 0x3B |
Manuel Pégourié-Gonnard | c05f150 | 2019-11-06 10:15:26 +0100 | [diff] [blame] | 91 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 92 | /* 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: */ |
Kevin Bracey | f40c792 | 2020-07-16 21:02:02 +0300 | [diff] [blame] | 101 | typedef int_fast8_t wordcount_t; |
| 102 | typedef int_fast16_t bitcount_t; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 103 | /* defining data type for comparison result: */ |
Kevin Bracey | f40c792 | 2020-07-16 21:02:02 +0300 | [diff] [blame] | 104 | typedef int_fast8_t cmpresult_t; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 105 | /* defining data type to store ECC coordinate/point in 32bits words: */ |
| 106 | typedef unsigned int uECC_word_t; |
| 107 | /* defining data type to store an ECC coordinate/point in 64bits words: */ |
| 108 | typedef 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é-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 120 | #define NUM_ECC_BITS 256 |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 121 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 122 | /* |
| 123 | * @brief computes doubling of point ion jacobian coordinates, in place. |
| 124 | * @param X1 IN/OUT -- x coordinate |
| 125 | * @param Y1 IN/OUT -- y coordinate |
| 126 | * @param Z1 IN/OUT -- z coordinate |
| 127 | * @param curve IN -- elliptic curve |
| 128 | */ |
| 129 | void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1, |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 130 | uECC_word_t * Z1); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * @brief Computes result = product % curve_p |
| 134 | * from http://www.nsa.gov/ia/_files/nist-routines.pdf |
| 135 | * @param result OUT -- product % curve_p |
| 136 | * @param product IN -- value to be reduced mod curve_p |
| 137 | */ |
| 138 | void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int *product); |
| 139 | |
| 140 | /* Bytes to words ordering: */ |
| 141 | #define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) 0x##d##c##b##a, 0x##h##g##f##e |
| 142 | #define BYTES_TO_WORDS_4(a, b, c, d) 0x##d##c##b##a |
| 143 | #define BITS_TO_WORDS(num_bits) \ |
| 144 | ((num_bits + ((uECC_WORD_SIZE * 8) - 1)) / (uECC_WORD_SIZE * 8)) |
| 145 | #define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8) |
| 146 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 147 | extern const uECC_word_t curve_p[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 148 | extern const uECC_word_t curve_n[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | a611508 | 2019-11-21 10:29:14 +0100 | [diff] [blame] | 149 | extern const uECC_word_t curve_G[2 * NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | ffd1399 | 2019-11-21 10:39:06 +0100 | [diff] [blame] | 150 | extern const uECC_word_t curve_b[NUM_ECC_WORDS]; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * @brief Generates a random integer in the range 0 < random < top. |
| 154 | * Both random and top have num_words words. |
| 155 | * @param random OUT -- random integer in the range 0 < random < top |
| 156 | * @param top IN -- upper limit |
| 157 | * @param num_words IN -- number of words |
Andrzej Kurek | 3a0df03 | 2020-06-12 06:32:13 -0400 | [diff] [blame] | 158 | * @return UECC_SUCCESS in case of success |
| 159 | * @return UECC_FAILURE upon failure |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 160 | */ |
| 161 | int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top, |
| 162 | wordcount_t num_words); |
| 163 | |
| 164 | |
| 165 | /* uECC_RNG_Function type |
| 166 | * The RNG function should fill 'size' random bytes into 'dest'. It should |
Andrzej Kurek | 090365f | 2020-06-08 11:00:51 -0400 | [diff] [blame] | 167 | * return 'size' if 'dest' was filled with random data of 'size' length, or 0 |
| 168 | * if the random data could not be generated. The filled-in values should be |
| 169 | * either truly random, or from a cryptographically-secure PRNG. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 170 | * |
| 171 | * A correctly functioning RNG function must be set (using uECC_set_rng()) |
| 172 | * before calling uECC_make_key() or uECC_sign(). |
| 173 | * |
| 174 | * Setting a correctly functioning RNG function improves the resistance to |
| 175 | * side-channel attacks for uECC_shared_secret(). |
| 176 | * |
| 177 | * A correct RNG function is set by default. If you are building on another |
| 178 | * POSIX-compliant system that supports /dev/random or /dev/urandom, you can |
| 179 | * define uECC_POSIX to use the predefined RNG. |
| 180 | */ |
| 181 | typedef int(*uECC_RNG_Function)(uint8_t *dest, unsigned int size); |
| 182 | |
| 183 | /* |
| 184 | * @brief Set the function that will be used to generate random bytes. The RNG |
Andrzej Kurek | 090365f | 2020-06-08 11:00:51 -0400 | [diff] [blame] | 185 | * function should return 'size' if the random data of length 'size' was |
| 186 | * generated, or 0 if the random data could not be generated. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 187 | * |
| 188 | * @note On platforms where there is no predefined RNG function, this must be |
| 189 | * called before uECC_make_key() or uECC_sign() are used. |
| 190 | * |
| 191 | * @param rng_function IN -- function that will be used to generate random bytes |
| 192 | */ |
| 193 | void uECC_set_rng(uECC_RNG_Function rng_function); |
| 194 | |
| 195 | /* |
| 196 | * @brief provides current uECC_RNG_Function. |
| 197 | * @return Returns the function that will be used to generate random bytes. |
| 198 | */ |
| 199 | uECC_RNG_Function uECC_get_rng(void); |
| 200 | |
| 201 | /* |
| 202 | * @brief computes the size of a private key for the curve in bytes. |
| 203 | * @param curve IN -- elliptic curve |
| 204 | * @return size of a private key for the curve in bytes. |
| 205 | */ |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 206 | int uECC_curve_private_key_size(void); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * @brief computes the size of a public key for the curve in bytes. |
| 210 | * @param curve IN -- elliptic curve |
| 211 | * @return the size of a public key for the curve in bytes. |
| 212 | */ |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 213 | int uECC_curve_public_key_size(void); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * @brief Compute the corresponding public key for a private key. |
| 217 | * @param private_key IN -- The private key to compute the public key for |
| 218 | * @param public_key OUT -- Will be filled in with the corresponding public key |
| 219 | * @param curve |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 220 | * @return UECC_SUCCESS or UECC_FAILURE or UECC_FAULT_DETECTED |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 221 | */ |
| 222 | int uECC_compute_public_key(const uint8_t *private_key, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 223 | uint8_t *public_key); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * @brief Compute public-key. |
| 227 | * @return corresponding public-key. |
| 228 | * @param result OUT -- public-key |
| 229 | * @param private_key IN -- private-key |
| 230 | * @param curve IN -- elliptic curve |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 231 | * @return UECC_SUCCESS or UECC_FAILURE or UECC_FAULT_DETECTED |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 232 | */ |
| 233 | uECC_word_t EccPoint_compute_public_key(uECC_word_t *result, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 234 | uECC_word_t *private_key); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 235 | |
| 236 | /* |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 237 | * @brief Point multiplication algorithm using Montgomery's ladder with co-Z |
| 238 | * coordinates. See http://eprint.iacr.org/2011/338.pdf. |
| 239 | * Uses scalar regularization and coordinate randomization (if a global RNG |
| 240 | * function is set) in order to protect against some side channel attacks. |
| 241 | * @note Result may overlap point. |
| 242 | * @param result OUT -- returns scalar*point |
| 243 | * @param point IN -- elliptic curve point |
| 244 | * @param scalar IN -- scalar |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 245 | * @return UECC_SUCCESS or UECC_FAILURE or UECC_FAULT_DETECTED |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 246 | */ |
| 247 | int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 248 | const uECC_word_t * scalar); |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 249 | |
| 250 | /* |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 251 | * @brief Constant-time comparison to zero - secure way to compare long integers |
| 252 | * @param vli IN -- very long integer |
| 253 | * @param num_words IN -- number of words in the vli |
| 254 | * @return 1 if vli == 0, 0 otherwise. |
| 255 | */ |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 256 | uECC_word_t uECC_vli_isZero(const uECC_word_t *vli); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * @brief Check if 'point' is the point at infinity |
| 260 | * @param point IN -- elliptic curve point |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 261 | * @return if 'point' is the point at infinity, 0 otherwise. |
| 262 | */ |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 263 | uECC_word_t EccPoint_isZero(const uECC_word_t *point); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * @brief computes the sign of left - right, in constant time. |
| 267 | * @param left IN -- left term to be compared |
| 268 | * @param right IN -- right term to be compared |
| 269 | * @param num_words IN -- number of words |
| 270 | * @return the sign of left - right |
| 271 | */ |
Manuel Pégourié-Gonnard | 2cb3eea | 2019-11-04 14:43:35 +0100 | [diff] [blame] | 272 | cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * @brief computes sign of left - right, not in constant time. |
| 276 | * @note should not be used if inputs are part of a secret |
| 277 | * @param left IN -- left term to be compared |
| 278 | * @param right IN -- right term to be compared |
| 279 | * @param num_words IN -- number of words |
| 280 | * @return the sign of left - right |
| 281 | */ |
Manuel Pégourié-Gonnard | a752191 | 2019-11-04 14:31:35 +0100 | [diff] [blame] | 282 | cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, const uECC_word_t *right); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * @brief Computes result = (left - right) % mod. |
| 286 | * @note Assumes that (left < mod) and (right < mod), and that result does not |
| 287 | * overlap mod. |
| 288 | * @param result OUT -- (left - right) % mod |
| 289 | * @param left IN -- leftright term in modular subtraction |
| 290 | * @param right IN -- right term in modular subtraction |
| 291 | * @param mod IN -- mod |
| 292 | * @param num_words IN -- number of words |
| 293 | */ |
| 294 | void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | 1b0875d | 2019-11-04 14:50:54 +0100 | [diff] [blame] | 295 | const uECC_word_t *right, const uECC_word_t *mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * @brief Computes P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) or |
| 299 | * P => P', Q => P + Q |
| 300 | * @note assumes Input P = (x1, y1, Z), Q = (x2, y2, Z) |
| 301 | * @param X1 IN -- x coordinate of P |
| 302 | * @param Y1 IN -- y coordinate of P |
| 303 | * @param X2 IN -- x coordinate of Q |
| 304 | * @param Y2 IN -- y coordinate of Q |
| 305 | * @param curve IN -- elliptic curve |
| 306 | */ |
| 307 | void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * X2, |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 308 | uECC_word_t * Y2); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * @brief Computes (x1 * z^2, y1 * z^3) |
| 312 | * @param X1 IN -- previous x1 coordinate |
| 313 | * @param Y1 IN -- previous y1 coordinate |
| 314 | * @param Z IN -- z value |
| 315 | * @param curve IN -- elliptic curve |
| 316 | */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 317 | void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 318 | |
| 319 | /* |
| 320 | * @brief Check if bit is set. |
| 321 | * @return Returns nonzero if bit 'bit' of vli is set. |
| 322 | * @warning It is assumed that the value provided in 'bit' is within the |
| 323 | * boundaries of the word-array 'vli'. |
| 324 | * @note The bit ordering layout assumed for vli is: {31, 30, ..., 0}, |
| 325 | * {63, 62, ..., 32}, {95, 94, ..., 64}, {127, 126,..., 96} for a vli consisting |
| 326 | * of 4 uECC_word_t elements. |
| 327 | */ |
| 328 | uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit); |
| 329 | |
| 330 | /* |
| 331 | * @brief Computes result = product % mod, where product is 2N words long. |
| 332 | * @param result OUT -- product % mod |
| 333 | * @param mod IN -- module |
| 334 | * @param num_words IN -- number of words |
| 335 | * @warning Currently only designed to work for curve_p or curve_n. |
| 336 | */ |
| 337 | void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product, |
Manuel Pégourié-Gonnard | 10349e4 | 2019-11-04 14:57:53 +0100 | [diff] [blame] | 338 | const uECC_word_t *mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * @brief Computes modular product (using curve->mmod_fast) |
| 342 | * @param result OUT -- (left * right) mod % curve_p |
| 343 | * @param left IN -- left term in product |
| 344 | * @param right IN -- right term in product |
| 345 | * @param curve IN -- elliptic curve |
| 346 | */ |
| 347 | void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 348 | const uECC_word_t *right); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 349 | |
| 350 | /* |
| 351 | * @brief Computes result = left - right. |
| 352 | * @note Can modify in place. |
| 353 | * @param result OUT -- left - right |
| 354 | * @param left IN -- left term in subtraction |
| 355 | * @param right IN -- right term in subtraction |
| 356 | * @param num_words IN -- number of words |
| 357 | * @return borrow |
| 358 | */ |
| 359 | uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 360 | const uECC_word_t *right); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * @brief Constant-time comparison function(secure way to compare long ints) |
| 364 | * @param left IN -- left term in comparison |
| 365 | * @param right IN -- right term in comparison |
| 366 | * @param num_words IN -- number of words |
Manuel Pégourié-Gonnard | 2b6312b | 2019-11-06 10:42:02 +0100 | [diff] [blame] | 367 | * @return Returns 0 if left == right, non-zero otherwise. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 368 | */ |
Manuel Pégourié-Gonnard | 2eca3d3 | 2019-11-04 14:33:09 +0100 | [diff] [blame] | 369 | uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 370 | |
| 371 | /* |
| 372 | * @brief Computes (left * right) % mod |
| 373 | * @param result OUT -- (left * right) % mod |
| 374 | * @param left IN -- left term in product |
| 375 | * @param right IN -- right term in product |
| 376 | * @param mod IN -- mod |
| 377 | * @param num_words IN -- number of words |
| 378 | */ |
| 379 | void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | 3e20adf | 2019-11-04 15:00:43 +0100 | [diff] [blame] | 380 | const uECC_word_t *right, const uECC_word_t *mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * @brief Computes (1 / input) % mod |
| 384 | * @note All VLIs are the same size. |
| 385 | * @note See "Euclid's GCD to Montgomery Multiplication to the Great Divide" |
| 386 | * @param result OUT -- (1 / input) % mod |
| 387 | * @param input IN -- value to be modular inverted |
| 388 | * @param mod IN -- mod |
| 389 | * @param num_words -- number of words |
| 390 | */ |
| 391 | void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input, |
Manuel Pégourié-Gonnard | 9135348 | 2019-11-04 15:04:20 +0100 | [diff] [blame] | 392 | const uECC_word_t *mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 393 | |
| 394 | /* |
| 395 | * @brief Sets dest = src. |
| 396 | * @param dest OUT -- destination buffer |
| 397 | * @param src IN -- origin buffer |
| 398 | * @param num_words IN -- number of words |
| 399 | */ |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 400 | void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 401 | |
| 402 | /* |
| 403 | * @brief Computes (left + right) % mod. |
| 404 | * @note Assumes that (left < mod) and right < mod), and that result does not |
| 405 | * overlap mod. |
| 406 | * @param result OUT -- (left + right) % mod. |
| 407 | * @param left IN -- left term in addition |
| 408 | * @param right IN -- right term in addition |
| 409 | * @param mod IN -- mod |
| 410 | * @param num_words IN -- number of words |
| 411 | */ |
| 412 | void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | 0779be7 | 2019-11-04 14:48:22 +0100 | [diff] [blame] | 413 | const uECC_word_t *right, const uECC_word_t *mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 414 | |
| 415 | /* |
| 416 | * @brief Counts the number of bits required to represent vli. |
| 417 | * @param vli IN -- very long integer |
| 418 | * @param max_words IN -- number of words |
| 419 | * @return number of bits in given vli |
| 420 | */ |
Manuel Pégourié-Gonnard | 2bf5a12 | 2019-11-04 12:56:59 +0100 | [diff] [blame] | 421 | bitcount_t uECC_vli_numBits(const uECC_word_t *vli); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * @brief Erases (set to 0) vli |
| 425 | * @param vli IN -- very long integer |
| 426 | * @param num_words IN -- number of words |
| 427 | */ |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 428 | void uECC_vli_clear(uECC_word_t *vli); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * @brief check if it is a valid point in the curve |
| 432 | * @param point IN -- point to be checked |
| 433 | * @param curve IN -- elliptic curve |
| 434 | * @return 0 if point is valid |
| 435 | * @exception returns -1 if it is a point at infinity |
| 436 | * @exception returns -2 if x or y is smaller than p, |
| 437 | * @exception returns -3 if y^2 != x^3 + ax + b. |
| 438 | */ |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 439 | int uECC_valid_point(const uECC_word_t *point); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 440 | |
| 441 | /* |
| 442 | * @brief Check if a public key is valid. |
| 443 | * @param public_key IN -- The public key to be checked. |
| 444 | * @return returns 0 if the public key is valid |
| 445 | * @exception returns -1 if it is a point at infinity |
| 446 | * @exception returns -2 if x or y is smaller than p, |
| 447 | * @exception returns -3 if y^2 != x^3 + ax + b. |
| 448 | * @exception returns -4 if public key is the group generator. |
| 449 | * |
| 450 | * @note Note that you are not required to check for a valid public key before |
| 451 | * using any other uECC functions. However, you may wish to avoid spending CPU |
| 452 | * time computing a shared secret or verifying a signature using an invalid |
| 453 | * public key. |
| 454 | */ |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 455 | int uECC_valid_public_key(const uint8_t *public_key); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * @brief Converts an integer in uECC native format to big-endian bytes. |
| 459 | * @param bytes OUT -- bytes representation |
| 460 | * @param num_bytes IN -- number of bytes |
| 461 | * @param native IN -- uECC native representation |
| 462 | */ |
| 463 | void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes, |
| 464 | const unsigned int *native); |
| 465 | |
| 466 | /* |
| 467 | * @brief Converts big-endian bytes to an integer in uECC native format. |
| 468 | * @param native OUT -- uECC native representation |
| 469 | * @param bytes IN -- bytes representation |
| 470 | * @param num_bytes IN -- number of bytes |
| 471 | */ |
| 472 | void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes, |
| 473 | int num_bytes); |
| 474 | |
| 475 | #ifdef __cplusplus |
| 476 | } |
| 477 | #endif |
| 478 | |
| 479 | #endif /* __TC_UECC_H__ */ |