David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 1 | /* ec_dh.c - TinyCrypt implementation of EC-DH */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2015 by Intel Corporation, 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, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * - Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * - Neither the name of Intel Corporation nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | #include <tinycrypt/constants.h> |
| 33 | #include <tinycrypt/ecc.h> |
| 34 | |
| 35 | extern uint32_t curve_p[NUM_ECC_DIGITS]; |
| 36 | extern uint32_t curve_b[NUM_ECC_DIGITS]; |
| 37 | extern uint32_t curve_n[NUM_ECC_DIGITS]; |
| 38 | extern uint32_t curve_pb[NUM_ECC_DIGITS + 1]; |
| 39 | extern EccPoint curve_G; |
| 40 | |
| 41 | int32_t ecc_make_key(EccPoint *p_publicKey, uint32_t p_privateKey[NUM_ECC_DIGITS], |
| 42 | uint32_t p_random[NUM_ECC_DIGITS * 2]) |
| 43 | { |
| 44 | // computing modular reduction of p_random (see FIPS 186.4 B.4.1): |
| 45 | vli_mmod_barrett(p_privateKey, p_random, curve_p, curve_pb); |
| 46 | |
| 47 | /* Make sure the private key is in the range [1, n-1]. |
| 48 | * For the supported curve, n is always large enough |
| 49 | * that we only need to subtract once at most. |
| 50 | */ |
| 51 | uint32_t p_tmp[NUM_ECC_DIGITS]; |
| 52 | vli_sub(p_tmp, p_privateKey, curve_n, NUM_ECC_DIGITS); |
| 53 | |
| 54 | vli_cond_set(p_privateKey, p_privateKey, p_tmp, |
| 55 | vli_cmp(curve_n, p_privateKey, NUM_ECC_DIGITS) == 1); |
| 56 | |
| 57 | /* erasing temporary buffer used to store secret: */ |
| 58 | for (uint32_t i = 0; i < NUM_ECC_DIGITS; i++) |
| 59 | p_tmp[i] = 0; |
| 60 | |
| 61 | if (vli_isZero(p_privateKey)) { |
| 62 | return TC_CRYPTO_FAIL; /* The private key cannot be 0 (mod p). */ |
| 63 | } |
| 64 | |
| 65 | EccPointJacobi P; |
| 66 | |
| 67 | EccPoint_mult_safe(&P, &curve_G, p_privateKey); |
| 68 | EccPoint_toAffine(p_publicKey, &P); |
| 69 | |
| 70 | return TC_CRYPTO_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | /* Compute p_result = x^3 - 3x + b */ |
| 74 | static void curve_x_side(uint32_t p_result[NUM_ECC_DIGITS], |
| 75 | uint32_t x[NUM_ECC_DIGITS]) |
| 76 | { |
| 77 | |
| 78 | uint32_t _3[NUM_ECC_DIGITS] = {3}; /* -a = 3 */ |
| 79 | |
| 80 | vli_modSquare_fast(p_result, x); /* r = x^2 */ |
| 81 | vli_modSub(p_result, p_result, _3, curve_p); /* r = x^2 - 3 */ |
| 82 | vli_modMult_fast(p_result, p_result, x); /* r = x^3 - 3x */ |
| 83 | vli_modAdd(p_result, p_result, curve_b, curve_p); /* r = x^3 - 3x + b */ |
| 84 | |
| 85 | } |
| 86 | |
| 87 | int32_t ecc_valid_public_key(EccPoint *p_publicKey) |
| 88 | { |
| 89 | uint32_t l_tmp1[NUM_ECC_DIGITS]; |
| 90 | uint32_t l_tmp2[NUM_ECC_DIGITS]; |
| 91 | |
| 92 | if (EccPoint_isZero(p_publicKey)) { |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | if ((vli_cmp(curve_p, p_publicKey->x, NUM_ECC_DIGITS) != 1) || |
| 97 | (vli_cmp(curve_p, p_publicKey->y, NUM_ECC_DIGITS) != 1)) { |
| 98 | return -2; |
| 99 | } |
| 100 | |
| 101 | vli_modSquare_fast(l_tmp1, p_publicKey->y); /* tmp1 = y^2 */ |
| 102 | |
| 103 | curve_x_side(l_tmp2, p_publicKey->x); /* tmp2 = x^3 - 3x + b */ |
| 104 | |
| 105 | /* Make sure that y^2 == x^3 + ax + b */ |
| 106 | if (vli_cmp(l_tmp1, l_tmp2, NUM_ECC_DIGITS) != 0) { |
| 107 | return -3; |
| 108 | } |
| 109 | |
| 110 | if (vli_cmp(p_publicKey->x, curve_G.x, NUM_ECC_DIGITS) == 0 && |
| 111 | vli_cmp(p_publicKey->y, curve_G.y, NUM_ECC_DIGITS) == 0 ) |
| 112 | return -4; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int32_t ecdh_shared_secret(uint32_t p_secret[NUM_ECC_DIGITS], |
| 118 | EccPoint *p_publicKey, uint32_t p_privateKey[NUM_ECC_DIGITS]) |
| 119 | { |
| 120 | |
| 121 | EccPoint p_point; |
| 122 | EccPointJacobi P; |
| 123 | |
| 124 | EccPoint_mult_safe(&P, p_publicKey, p_privateKey); |
| 125 | if (EccPointJacobi_isZero(&P)) { |
| 126 | return TC_CRYPTO_FAIL; |
| 127 | } |
| 128 | EccPoint_toAffine(&p_point, &P); |
| 129 | vli_set(p_secret, p_point.x); |
| 130 | |
| 131 | return TC_CRYPTO_SUCCESS; |
| 132 | } |