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