blob: dd84a1834d86f3b421a1af7e0be6e53bdf7fb671 [file] [log] [blame]
David Brownfecda2d2017-09-07 10:20:34 -06001/* ec_dsa.c - TinyCrypt implementation of EC-DSA */
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
33#include <tinycrypt/constants.h>
34#include <tinycrypt/ecc.h>
35
36extern uint32_t curve_n[NUM_ECC_DIGITS];
37extern EccPoint curve_G;
38extern uint32_t curve_nb[NUM_ECC_DIGITS + 1];
39
40int32_t ecdsa_sign(uint32_t r[NUM_ECC_DIGITS], uint32_t s[NUM_ECC_DIGITS],
41 uint32_t p_privateKey[NUM_ECC_DIGITS], uint32_t p_random[NUM_ECC_DIGITS],
42 uint32_t p_hash[NUM_ECC_DIGITS])
43{
44
45 uint32_t k[NUM_ECC_DIGITS], tmp[NUM_ECC_DIGITS];
46 EccPoint p_point;
47 EccPointJacobi P;
48
49 if (vli_isZero(p_random)) {
50 return TC_CRYPTO_FAIL; /* The random number must not be 0. */
51 }
52
53 vli_set(k, p_random);
54
55 vli_sub(tmp, k, curve_n, NUM_ECC_DIGITS);
56 vli_cond_set(k, k, tmp, vli_cmp(curve_n, k, NUM_ECC_DIGITS) == 1);
57
58 /* tmp = k * G */
59 EccPoint_mult_safe(&P, &curve_G, k);
60 EccPoint_toAffine(&p_point, &P);
61
62 /* r = x1 (mod n) */
63 vli_set(r, p_point.x);
64 if (vli_cmp(curve_n, r, NUM_ECC_DIGITS) != 1) {
65 vli_sub(r, r, curve_n, NUM_ECC_DIGITS);
66 }
67
68 if (vli_isZero(r)) {
69 return TC_CRYPTO_FAIL; /* If r == 0, fail (need a different random number). */
70 }
71
72 vli_modMult(s, r, p_privateKey, curve_n, curve_nb); /* s = r*d */
73 vli_modAdd(s, p_hash, s, curve_n); /* s = e + r*d */
74 vli_modInv(k, k, curve_n, curve_nb); /* k = 1 / k */
75 vli_modMult(s, s, k, curve_n, curve_nb); /* s = (e + r*d) / k */
76
77 return TC_CRYPTO_SUCCESS;
78}
79
80int32_t ecdsa_verify(EccPoint *p_publicKey, uint32_t p_hash[NUM_ECC_DIGITS],
81 uint32_t r[NUM_ECC_DIGITS], uint32_t s[NUM_ECC_DIGITS])
82{
83
84 uint32_t u1[NUM_ECC_DIGITS], u2[NUM_ECC_DIGITS];
85 uint32_t z[NUM_ECC_DIGITS];
86 EccPointJacobi P, R;
87 EccPoint p_point;
88
89 if (vli_isZero(r) || vli_isZero(s)) {
90 return TC_CRYPTO_FAIL; /* r, s must not be 0. */
91 }
92
93 if ((vli_cmp(curve_n, r, NUM_ECC_DIGITS) != 1) ||
94 (vli_cmp(curve_n, s, NUM_ECC_DIGITS) != 1)) {
95 return TC_CRYPTO_FAIL; /* r, s must be < n. */
96 }
97
98 /* Calculate u1 and u2. */
99 vli_modInv(z, s, curve_n, curve_nb); /* Z = s^-1 */
100 vli_modMult(u1, p_hash, z, curve_n, curve_nb); /* u1 = e/s */
101 vli_modMult(u2, r, z, curve_n, curve_nb); /* u2 = r/s */
102
103 /* calculate P = u1*G + u2*Q */
104 EccPoint_mult_unsafe(&P, &curve_G, u1);
105 EccPoint_mult_unsafe(&R, p_publicKey, u2);
106 EccPoint_add(&P, &R);
107 EccPoint_toAffine(&p_point, &P);
108
109 /* Accept only if P.x == r. */
110 if (!vli_sub(z, p_point.x, curve_n, NUM_ECC_DIGITS)) {
111 vli_set(p_point.x, z);
112 }
113
114 return (vli_cmp(p_point.x, r, NUM_ECC_DIGITS) == 0);
115}