Contiki-NG
Loading...
Searching...
No Matches
ecc.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021, Uppsala universitet.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * \addtogroup crypto
33 * @{
34 *
35 * \file
36 * Adapter for uECC.
37 * \author
38 * Konrad Krentz <konrad.krentz@gmail.com>
39 */
40
41#include "lib/ecc.h"
42#include "lib/csprng.h"
43#include "lib/sha-256.h"
44#include "uECC.h"
45
46static struct pt protothread;
47static const ecc_curve_t *ecc_curve;
48static uECC_Curve uecc_curve;
49static process_mutex_t mutex;
50
51/*---------------------------------------------------------------------------*/
52static int
53csprng_adapter(uint8_t *dest, unsigned size)
54{
55 return csprng_rand(dest, size);
56}
57/*---------------------------------------------------------------------------*/
58void
59ecc_init(void)
60{
61 process_mutex_init(&mutex);
62 uECC_set_rng(csprng_adapter);
63}
64/*---------------------------------------------------------------------------*/
66ecc_get_mutex(void)
67{
68 return &mutex;
69}
70/*---------------------------------------------------------------------------*/
71int
72ecc_enable(const ecc_curve_t *c)
73{
74 if(c == &ecc_curve_p_256) {
75 uecc_curve = uECC_secp256r1();
76 } else if(c == &ecc_curve_p_192) {
77 uecc_curve = uECC_secp192r1();
78 } else {
80 return 1;
81 }
82 ecc_curve = c;
83 return 0;
84}
85/*---------------------------------------------------------------------------*/
86struct pt *
87ecc_get_protothread(void)
88{
89 return &protothread;
90}
91/*---------------------------------------------------------------------------*/
92PT_THREAD(ecc_validate_public_key(const uint8_t *public_key,
93 int *const result))
94{
95 PT_BEGIN(&protothread);
96
97 *result = !uECC_valid_public_key(public_key, uecc_curve);
98
99 PT_END(&protothread);
100}
101/*---------------------------------------------------------------------------*/
102void
103ecc_compress_public_key(const uint8_t *uncompressed_public_key,
104 uint8_t *compressed_public_key)
105{
106 uECC_compress(uncompressed_public_key, compressed_public_key, uecc_curve);
107}
108/*---------------------------------------------------------------------------*/
109PT_THREAD(ecc_decompress_public_key(const uint8_t *compressed_public_key,
110 uint8_t *uncompressed_public_key,
111 int *const result))
112{
113 PT_BEGIN(&protothread);
114
115 uECC_decompress(compressed_public_key,
116 uncompressed_public_key,
117 uecc_curve);
118 *result = 0;
119
120 PT_END(&protothread);
121}
122/*---------------------------------------------------------------------------*/
123PT_THREAD(ecc_sign(const uint8_t *message_hash,
124 const uint8_t *private_key,
125 uint8_t *signature,
126 int *const result))
127{
128 PT_BEGIN(&protothread);
129
130 *result = !uECC_sign(private_key,
131 message_hash,
132 ecc_curve->bytes,
133 signature,
134 uecc_curve);
135
136 PT_END(&protothread);
137}
138/*---------------------------------------------------------------------------*/
139PT_THREAD(ecc_verify(const uint8_t *signature,
140 const uint8_t *message_hash,
141 const uint8_t *public_key,
142 int *const result))
143{
144 PT_BEGIN(&protothread);
145
146 *result = !uECC_verify(public_key,
147 message_hash,
148 ecc_curve->bytes,
149 signature,
150 uecc_curve);
151
152 PT_END(&protothread);
153}
154/*---------------------------------------------------------------------------*/
155PT_THREAD(ecc_generate_key_pair(uint8_t *public_key,
156 uint8_t *private_key,
157 int *const result))
158{
159 PT_BEGIN(&protothread);
160
161 *result = !uECC_make_key(public_key,
162 private_key,
163 uecc_curve);
164
165 PT_END(&protothread);
166}
167/*---------------------------------------------------------------------------*/
168PT_THREAD(ecc_generate_shared_secret(const uint8_t *public_key,
169 const uint8_t *private_key,
170 uint8_t *shared_secret,
171 int *const result))
172{
173 PT_BEGIN(&protothread);
174
175 *result = !uECC_shared_secret(public_key,
176 private_key,
177 shared_secret,
178 uecc_curve);
179
180 PT_END(&protothread);
181}
182/*---------------------------------------------------------------------------*/
183void
184ecc_disable(void)
185{
186 process_mutex_unlock(&mutex);
187}
188/*---------------------------------------------------------------------------*/
189
190/** @} */
An OFB-AES-128-based CSPRNG.
Header file of ECC.
bool csprng_rand(uint8_t *result, size_t len)
Generates a cryptographic random number.
Definition csprng.c:81
void process_mutex_init(process_mutex_t *mutex)
Initializes a process mutex.
void process_mutex_unlock(process_mutex_t *mutex)
Unlocks a process mutex.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
Definition pt.h:280
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition pt.h:265
#define PT_END(pt)
Declare the end of a protothread.
Definition pt.h:292
Platform-independent SHA-256 API.
Parameters of an ECC curve in little-endian word order.
Definition ecc-curve.h:53
const size_t bytes
Size of the curve in bytes.
Definition ecc-curve.h:61
Structure of a process mutex.