Contiki-NG
Loading...
Searching...
No Matches
ecc.h
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 * Header file of ECC.
37 *
38 * All input and output byte arrays of ecc_* functions
39 * \li are in big-endian byte order
40 * \li may overlap
41 * \li may reside on the stack
42 * \li must be word aligned if using uECC's little-endian mode,
43 * which is off by default
44 *
45 * \author
46 * Konrad Krentz <konrad.krentz@gmail.com>
47 */
48
49#ifndef ECC_H_
50#define ECC_H_
51
52#include "contiki.h"
53#include "lib/ecc-curve.h"
54#include "sys/process-mutex.h"
55
56#ifdef ECC_CONF_ENABLED
57#define ECC_ENABLED ECC_CONF_ENABLED
58#else /* ECC_CONF_ENABLED */
59#define ECC_ENABLED 0
60#endif /* ECC_CONF_ENABLED */
61
62/**
63 * \brief Initializes ECC.
64 */
65void ecc_init(void);
66
67/**
68 * \brief Provides a mutex to be locked before proceeding with ecc_enable().
69 * \return The mutex.
70 */
72
73/**
74 * \brief Sets up the ECC driver.
75 * \param curve The curve to use in subsequent calls.
76 * \return 0 on success and else a driver-specific error code.
77 * On error, the mutex is unlocked automatically and
78 * there is no need to call ecc_disable() either.
79 */
80int ecc_enable(const ecc_curve_t *curve);
81
82/**
83 * \brief Provides the protothread that runs long-running ECC operations.
84 * \return The protothread that runs long-running ECC operations.
85 */
86struct pt *ecc_get_protothread(void);
87
88/**
89 * \brief Validates a public key.
90 * \param public_key The 2|CURVE|-byte public key.
91 * \param result 0 on success and else a driver-specific error code.
92 */
93PT_THREAD(ecc_validate_public_key(const uint8_t *public_key, int *result));
94
95/**
96 * \brief Compresses a public key as per SECG SEC 1.
97 * \param uncompressed_public_key The uncompressed 2|CURVE|-byte public key.
98 * \param compressed_public_key The compressed (1+|CURVE|)-byte public key.
99 */
100void ecc_compress_public_key(const uint8_t *uncompressed_public_key,
101 uint8_t *compressed_public_key);
102
103/**
104 * \brief Decompresses a public key.
105 * \param compressed_public_key The compressed (1+|CURVE|)-byte public key.
106 * \param uncompressed_public_key The uncompressed 2|CURVE|-byte public key.
107 * \param result 0 on success and else a driver-specific
108 * error code.
109 */
110PT_THREAD(ecc_decompress_public_key(const uint8_t *compressed_public_key,
111 uint8_t *uncompressed_public_key,
112 int *result));
113
114/**
115 * \brief Generates an ECDSA signature for a message.
116 * \param message_hash The |CURVE|-byte hash over the message.
117 * \param private_key The |CURVE|-byte private key.
118 * \param signature The 2|CURVE|-byte signature.
119 * \param result 0 on success and else a driver-specific error code.
120 */
121PT_THREAD(ecc_sign(const uint8_t *message_hash,
122 const uint8_t *private_key,
123 uint8_t *signature,
124 int *result));
125
126/**
127 * \brief Verifies an ECDSA signature of a message.
128 * \param signature The 2|CURVE|-byte signature.
129 * \param message_hash The |CURVE|-byte hash over the message.
130 * \param public_key The 2|CURVE|-byte public key.
131 * \param result 0 on success and else a driver-specific error code.
132 */
133PT_THREAD(ecc_verify(const uint8_t *signature,
134 const uint8_t *message_hash,
135 const uint8_t *public_key,
136 int *result));
137
138/**
139 * \brief Generates a public/private key pair.
140 * \param public_key The 2|CURVE|-byte public key.
141 * \param private_key The |CURVE|-byte private key.
142 * \param result 0 on success and else a driver-specific error code.
143 */
144PT_THREAD(ecc_generate_key_pair(uint8_t *public_key,
145 uint8_t *private_key,
146 int *result));
147
148/**
149 * \brief Generates a shared secret as per ECDH.
150 *
151 * NOTE: Callers should derive symmetric keys from the shared secret via a
152 * key derivation function.
153 *
154 * \param public_key The peer's 2|CURVE|-byte public key.
155 * \param private_key Our |CURVE|-byte private key.
156 * \param shared_secret The resultant |CURVE|-byte shared secret.
157 * \param result 0 on success and else a driver-specific error code.
158 */
159PT_THREAD(ecc_generate_shared_secret(const uint8_t *public_key,
160 const uint8_t *private_key,
161 uint8_t *shared_secret,
162 int *result));
163
164/**
165 * \brief Shuts down the ECC driver and unlocks the mutex.
166 */
167void ecc_disable(void);
168
169#endif /* ECC_H_ */
170
171/** @} */
NIST curves for various key sizes.
void ecc_compress_public_key(const uint8_t *uncompressed_public_key, uint8_t *compressed_public_key)
Compresses a public key as per SECG SEC 1.
Definition ecc.c:103
void ecc_disable(void)
Shuts down the ECC driver and unlocks the mutex.
Definition ecc.c:184
char ecc_decompress_public_key(const uint8_t *compressed_public_key, uint8_t *uncompressed_public_key, int *result)
Decompresses a public key.
Definition ecc.c:111
char ecc_verify(const uint8_t *signature, const uint8_t *message_hash, const uint8_t *public_key, int *result)
Verifies an ECDSA signature of a message.
Definition ecc.c:142
char ecc_generate_key_pair(uint8_t *public_key, uint8_t *private_key, int *result)
Generates a public/private key pair.
Definition ecc.c:157
char ecc_validate_public_key(const uint8_t *public_key, int *result)
Validates a public key.
Definition ecc.c:93
void ecc_init(void)
Initializes ECC.
Definition ecc.c:59
process_mutex_t * ecc_get_mutex(void)
Provides a mutex to be locked before proceeding with ecc_enable().
Definition ecc.c:66
struct pt * ecc_get_protothread(void)
Provides the protothread that runs long-running ECC operations.
Definition ecc.c:87
int ecc_enable(const ecc_curve_t *curve)
Sets up the ECC driver.
Definition ecc.c:72
char ecc_generate_shared_secret(const uint8_t *public_key, const uint8_t *private_key, uint8_t *shared_secret, int *result)
Generates a shared secret as per ECDH.
Definition ecc.c:171
char ecc_sign(const uint8_t *message_hash, const uint8_t *private_key, uint8_t *signature, int *result)
Generates an ECDSA signature for a message.
Definition ecc.c:126
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition pt.h:265
Header file for Process mutexes.
Parameters of an ECC curve in little-endian word order.
Definition ecc-curve.h:53
Structure of a process mutex.