Contiki 3.x
ecc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Lars Schmertmann <SmallLars@t-online.de>,
3  * Jens Trillmann <jtrillma@informatik.uni-bremen.de>.
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
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * This file is part of the Contiki operating system.
33  *
34  */
35 
36 /**
37  * \file
38  * Calculations on elliptic curve secp256r1
39  *
40  * This is a efficient ECC implementation on the secp256r1 curve for
41  * 32 Bit CPU architectures. It provides basic operations on the
42  * secp256r1 curve and support for ECDH and ECDSA.
43  *
44  * \author
45  * Lars Schmertmann <SmallLars@t-online.de>
46  * Jens Trillmann <jtrillma@informatik.uni-bremen.de>
47  */
48 
49 #ifndef ECC_H_
50 #define ECC_H_
51 
52 #include <stdint.h>
53 
54 /**
55  * \brief Checks if a (random) number is valid as scalar on elliptic curve secp256r1
56  *
57  * A (random) number is only usable as scalar on elliptic curve secp256r1 if
58  * it is lower than the order of the curve. For the check, you need to provide
59  * the order of elliptic curve secp256r1.
60  *
61  * uint32_t order[8] = {0xFC632551, 0xF3B9CAC2, 0xA7179E84, 0xBCE6FAAD, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF};
62  *
63  * \param key The (random) number to check for usability
64  * \param order The order of elliptic curve secp256r1
65  *
66  * \return 1 if key is valid
67  */
68 #define ecc_is_valid_key(key, order) (ecc_compare(order, key) == 1)
69 
70 /**
71  * \brief Compares the value of a with the value of b
72  *
73  * This function is only public because its needed for the macro ecc_is_valid_key.
74  * It does a comparison of two 256 bit numbers. The return values are 1, 0 or -1.
75  *
76  * \param a First number
77  * \param b Second number
78  *
79  * \return 1 if a is greater than b
80  0 if a is equal to b
81  -1 if a is less than b
82  */
83 int32_t ecc_compare(const uint32_t *a, const uint32_t *b);
84 
85 /**
86  * \brief ECC scalar multiplication on elliptic curve secp256r1
87  *
88  * This function does a scalar multiplication on elliptic curve secp256r1.
89  * For an Elliptic curve Diffie–Hellman you need two multiplications. First one
90  * with the base point of elliptic curve secp256r1 you need to provide.
91  *
92  * uint32_t base_x[8] = {0xd898c296, 0xf4a13945, 0x2deb33a0, 0x77037d81, 0x63a440f2, 0xf8bce6e5, 0xe12c4247, 0x6b17d1f2};
93  * uint32_t base_y[8] = {0x37bf51f5, 0xcbb64068, 0x6b315ece, 0x2bce3357, 0x7c0f9e16, 0x8ee7eb4a, 0xfe1a7f9b, 0x4fe342e2};
94  *
95  * \param resultx Pointer to memory to store the x-coordinate of the result
96  * \param resulty Pointer to memory to store the y-coordinate of the result
97  * \param px x-coordinate of the point to multiply with scalar
98  * \param py y-coordinate of the point to multiply with scalar
99  * \param secret Scalar for multiplication with elliptic curve point
100  */
101 void ecc_ec_mult(uint32_t *resultx, uint32_t *resulty, const uint32_t *px, const uint32_t *py, const uint32_t *secret);
102 
103 #endif /* ECC_H_ */
int32_t ecc_compare(const uint32_t *a, const uint32_t *b)
Compares the value of a with the value of b.
Definition: ecc.c:112
void ecc_ec_mult(uint32_t *resultx, uint32_t *resulty, const uint32_t *px, const uint32_t *py, const uint32_t *secret)
ECC scalar multiplication on elliptic curve secp256r1.
Definition: ecc.c:124