1 /*
2 * Copyright (c) 2009 Chris K Cockrum <ckc@cockrum.net>
3 *
4 * Copyright (c) 2013 Jens Trillmann <jtrillma@tzi.de>
5 * Copyright (c) 2013 Marc Müller-Weinhardt <muewei@tzi.de>
6 * Copyright (c) 2013 Lars Schmertmann <lars@tzi.de>
7 * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 *
27 *
28 * This implementation is based in part on the paper Implementation of an
29 * Elliptic Curve Cryptosystem on an 8-bit Microcontroller [0] by
30 * Chris K Cockrum <ckc@cockrum.net>.
31 *
32 * [0]: http://cockrum.net/Implementation_of_ECC_on_an_8-bit_microcontroller.pdf
33 *
34 * This is a efficient ECC implementation on the secp256r1 curve for 32 Bit CPU
35 * architectures. It provides basic operations on the secp256r1 curve and support
36 * for ECDH and ECDSA.
37 */
38 #include <inttypes.h>
39
40 #define keyLengthInBytes 32
41 #define arrayLength 8
42
43 extern const uint32_t ecc_g_point_x[8];
44 extern const uint32_t ecc_g_point_y[8];
45
46 //ec Functions
47 void ecc_ec_mult(const uint32_t *px, const uint32_t *py, const uint32_t *secret, uint32_t *resultx, uint32_t *resulty);
48
ecc_ecdh(const uint32_t * px,const uint32_t * py,const uint32_t * secret,uint32_t * resultx,uint32_t * resulty)49 static inline void ecc_ecdh(const uint32_t *px, const uint32_t *py, const uint32_t *secret, uint32_t *resultx, uint32_t *resulty) {
50 ecc_ec_mult(px, py, secret, resultx, resulty);
51 }
52 int ecc_ecdsa_validate(const uint32_t *x, const uint32_t *y, const uint32_t *e, const uint32_t *r, const uint32_t *s);
53 int ecc_ecdsa_sign(const uint32_t *d, const uint32_t *e, const uint32_t *k, uint32_t *r, uint32_t *s);
54
55 int ecc_is_valid_key(const uint32_t * priv_key);
ecc_gen_pub_key(const uint32_t * priv_key,uint32_t * pub_x,uint32_t * pub_y)56 static inline void ecc_gen_pub_key(const uint32_t *priv_key, uint32_t *pub_x, uint32_t *pub_y)
57 {
58 ecc_ec_mult(ecc_g_point_x, ecc_g_point_y, priv_key, pub_x, pub_y);
59 }
60
61 #ifdef TEST_INCLUDE
62 //ec Functions
63 void ecc_ec_add(const uint32_t *px, const uint32_t *py, const uint32_t *qx, const uint32_t *qy, uint32_t *Sx, uint32_t *Sy);
64 void ecc_ec_double(const uint32_t *px, const uint32_t *py, uint32_t *Dx, uint32_t *Dy);
65
66 //simple Functions for addition and substraction of big numbers
67 uint32_t ecc_add( const uint32_t *x, const uint32_t *y, uint32_t *result, uint8_t length);
68 uint32_t ecc_sub( const uint32_t *x, const uint32_t *y, uint32_t *result, uint8_t length);
69
70 //field functions for big numbers
71 int ecc_fieldAdd(const uint32_t *x, const uint32_t *y, const uint32_t *reducer, uint32_t *result);
72 int ecc_fieldSub(const uint32_t *x, const uint32_t *y, const uint32_t *modulus, uint32_t *result);
73 int ecc_fieldMult(const uint32_t *x, const uint32_t *y, uint32_t *result, uint8_t length);
74 void ecc_fieldModP(uint32_t *A, const uint32_t *B);
75 void ecc_fieldModO(const uint32_t *A, uint32_t *result, uint8_t length);
76 void ecc_fieldInv(const uint32_t *A, const uint32_t *modulus, const uint32_t *reducer, uint32_t *B);
77
78 //simple functions to work with the big numbers
79 void ecc_copy(const uint32_t *from, uint32_t *to, uint8_t length);
80 int ecc_isSame(const uint32_t *A, const uint32_t *B, uint8_t length);
81 void ecc_setZero(uint32_t *A, const int length);
82 int ecc_isOne(const uint32_t* A);
83 void ecc_rshift(uint32_t* A);
84 int ecc_isGreater(const uint32_t *A, const uint32_t *B, uint8_t length);
85
86 #endif /* TEST_INCLUDE */
87