Contiki-NG
ecc-algorithm.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Institute for Pervasive Computing, ETH Zurich.
3  * All rights reserved.
4  *
5  * Author: Andreas Dröscher <contiki@anticat.ch>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. 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  * 3. Neither the name of the Institute nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 /**
32  * \addtogroup cc2538-ecc
33  * @{
34  *
35  * \defgroup cc2538-ecc-algo cc2538 ECC Algorithms
36  *
37  * This is a implementation of ECDH, ECDSA sign and ECDSA verify. It
38  * uses ecc-driver to communicate with the PKA. It uses continuations
39  * to free the main CPU / thread while the PKA is calculating.
40  *
41  * \note
42  * Only one request can be processed at a time.
43  * Maximal supported key length is 384bit (12 words).
44  * @{
45  *
46  * \file
47  * Header file for the cc2538 ECC Algorithms
48  */
49 #ifndef ECC_ALGORITHM_H_
50 #define ECC_ALGORITHM_H_
51 
52 #include "dev/bignum-driver.h"
53 #include "dev/ecc-driver.h"
54 
55 typedef struct {
56  /* Containers for the State */
57  struct pt pt;
58  struct process *process;
59 
60  /* Input Variables */
61  uint32_t a[12]; /**< Left Number */
62  uint32_t b[12]; /**< Right Number */
63  uint8_t size; /**< Length of a and b */
64 
65  /* Output Variables */
66  uint8_t result; /**< Result Code */
67 } ecc_compare_state_t;
68 
69 /**
70  * \brief Do a compare of two big numbers
71  *
72  * This function can be used for ECDH as well as
73  * Calculating a Public Key for ECDSA
74  */
75 PT_THREAD(ecc_compare(ecc_compare_state_t *state));
76 
77 typedef struct {
78  /* Containers for the State */
79  struct pt pt;
80  struct process *process;
81 
82  /* Input Variables */
83  ecc_curve_info_t *curve_info; /**< Curve defining the CyclicGroup */
84  ec_point_t point_in; /**< Generator Point */
85  uint32_t secret[12]; /**< Secret */
86 
87  /* Variables Holding intermediate data (initialized/used internally) */
88  uint32_t rv; /**< Address of Next Result in PKA SRAM */
89 
90  /* Output Variables */
91  uint8_t result; /**< Result Code */
92  ec_point_t point_out; /**< Generated Point */
93 } ecc_multiply_state_t;
94 
95 /**
96  * \brief Do a Multiplication on a EC
97  *
98  * This function can be used for ECDH as well as
99  * Calculating a Public Key for ECDSA
100  */
101 PT_THREAD(ecc_multiply(ecc_multiply_state_t *state));
102 
103 typedef struct {
104  /* Containers for the State */
105  struct pt pt;
106  struct process *process;
107 
108  /* Input Variables */
109  ecc_curve_info_t *curve_info; /**< Curve defining the CyclicGroup */
110  uint32_t secret[12]; /**< Secret Key */
111  uint32_t k_e[12]; /**< Ephemeral Key */
112  uint32_t hash[12]; /**< Hash to be signed */
113 
114  /* Variables Holding intermediate data (initialized/used internally) */
115  uint32_t rv; /**< Address of Next Result in PKA SRAM */
116  uint32_t k_e_inv[12]; /**< Inverted ephemeral Key */
117  uint32_t len; /**< Length of intermediate Result */
118 
119  /* Output Variables */
120  uint8_t result; /**< Result Code */
121  ec_point_t point_r; /**< Signature R (x coordinate) */
122  uint32_t signature_s[24]; /**< Signature S */
123 } ecc_dsa_sign_state_t;
124 
125 /**
126  * \brief Sign a Hash
127  *
128  * This function has to be called several times until the
129  * pt state is EXIT
130  * If the result code is 0 (SUCCESS) the signature can be
131  * read from point_r and signature_s
132  */
133 PT_THREAD(ecc_dsa_sign(ecc_dsa_sign_state_t *state));
134 
135 typedef struct {
136  /* Containers for the State */
137  struct pt pt;
138  struct process *process;
139 
140  /* Input Variables */
141  ecc_curve_info_t *curve_info; /**< Curve defining the CyclicGroup */
142  uint32_t signature_r[12]; /**< Signature R */
143  uint32_t signature_s[12]; /**< Signature S */
144  uint32_t hash[12]; /**< Hash to be signed */
145  ec_point_t public; /**< Signature R (x coordinate) */
146 
147  /* Variables Holding intermediate data (initialized/used internally) */
148  uint32_t rv; /**< Address of Next Result in PKA SRAM */
149  uint32_t s_inv[12]; /**< Inverted ephemeral Key */
150  uint32_t u1[24]; /**< Intermediate result */
151  uint32_t u2[24]; /**< Intermediate result */
152  ec_point_t p1; /**< Intermediate result */
153  ec_point_t p2; /**< Intermediate result */
154  uint32_t len; /**< Length of intermediate Result */
155 
156  /* Output Variables */
157  uint8_t result; /**< Result Code */
158 } ecc_dsa_verify_state_t;
159 
160 /**
161  * \brief Verify Signature
162  *
163  * This function has to be called several times until the
164  * pt state is EXIT
165  * If the result code is 0 (SUCCESS) the verification
166  * was success full.
167  * \note some error codes signal internal errors
168  * and others signal falls signatures.
169  */
170 PT_THREAD(ecc_dsa_verify(ecc_dsa_verify_state_t *state));
171 
172 #endif /* ECC_ALGORITHM_H_ */
173 
174 /**
175  * @}
176  * @}
177  */
178 
Header file for the cc2538 ECC driver.
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
Header file for the cc2538 BigNum driver.