Mistake on this page?
Report an issue in GitHub or email us
uECC_ll.h
1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
2 
3 #ifndef _MICRO_ECC_LL_H_
4 #define _MICRO_ECC_LL_H_
5 
6 #include "wsf_types.h"
7 
8 /* Platform selection options.
9 If uECC_PLATFORM is not defined, the code will try to guess it based on compiler macros.
10 Possible values for uECC_PLATFORM are defined below: */
11 #define uECC_arch_other 0
12 #define uECC_x86 1
13 #define uECC_x86_64 2
14 #define uECC_arm 3
15 #define uECC_arm_thumb 4
16 #define uECC_avr 5
17 #define uECC_arm_thumb2 6
18 
19 /* If desired, you can define uECC_WORD_SIZE as appropriate for your platform (1, 4, or 8 bytes).
20 If uECC_WORD_SIZE is not explicitly defined then it will be automatically set based on your
21 platform. */
22 
23 /* Inline assembly options.
24 uECC_asm_none - Use standard C99 only.
25 uECC_asm_small - Use GCC inline assembly for the target platform (if available), optimized for
26  minimum size.
27 uECC_asm_fast - Use GCC inline assembly optimized for maximum speed. */
28 #define uECC_asm_none 0
29 #define uECC_asm_small 1
30 #define uECC_asm_fast 2
31 #ifndef uECC_ASM
32  #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__) && defined(__GNUC__) /* Only support GCC inline asm for now */
33  #define uECC_ASM uECC_asm_fast
34  #else
35  #define uECC_ASM uECC_asm_none
36  #endif
37 #endif
38 
39 /* Curve selection options. */
40 #define uECC_secp160r1 1
41 #define uECC_secp192r1 2
42 #define uECC_secp256r1 3
43 #define uECC_secp256k1 4
44 #define uECC_secp224r1 5
45 #ifndef uECC_CURVE
46  #define uECC_CURVE uECC_secp256r1
47 #endif
48 
49 /* uECC_SQUARE_FUNC - If enabled (defined as nonzero), this will cause a specific function to be
50 used for (scalar) squaring instead of the generic multiplication function. This will make things
51 faster by about 8% but increases the code size. */
52 #ifndef uECC_SQUARE_FUNC
53  #define uECC_SQUARE_FUNC 1
54 #endif
55 
56 #define uECC_CONCAT1(a, b) a##b
57 #define uECC_CONCAT(a, b) uECC_CONCAT1(a, b)
58 
59 #define uECC_size_1 20 /* secp160r1 */
60 #define uECC_size_2 24 /* secp192r1 */
61 #define uECC_size_3 32 /* secp256r1 */
62 #define uECC_size_4 32 /* secp256k1 */
63 #define uECC_size_5 28 /* secp224r1 */
64 
65 #define uECC_BYTES uECC_CONCAT(uECC_size_, uECC_CURVE)
66 
67 #ifdef __cplusplus
68 extern "C"
69 {
70 #endif
71 
72 /* uECC_RNG_Function type
73 The RNG function should fill 'size' random bytes into 'dest'. It should return 1 if
74 'dest' was filled with random data, or 0 if the random data could not be generated.
75 The filled-in values should be either truly random, or from a cryptographically-secure PRNG.
76 
77 A correctly functioning RNG function must be set (using uECC_set_rng()) before calling
78 uECC_make_key() or uECC_sign().
79 
80 Setting a correctly functioning RNG function improves the resistance to side-channel attacks
81 for uECC_shared_secret() and uECC_sign_deterministic().
82 
83 A correct RNG function is set by default when building for Windows, Linux, or OS X.
84 If you are building on another POSIX-compliant system that supports /dev/random or /dev/urandom,
85 you can define uECC_POSIX to use the predefined RNG. For embedded platforms there is no predefined
86 RNG function; you must provide your own.
87 */
88 typedef int (*uECC_RNG_Function)(uint8_t *dest, unsigned size);
89 
90 /* uECC_set_rng() function.
91 Set the function that will be used to generate random bytes. The RNG function should
92 return 1 if the random data was generated, or 0 if the random data could not be generated.
93 
94 On platforms where there is no predefined RNG function (eg embedded platforms), this must
95 be called before uECC_make_key() or uECC_sign() are used.
96 
97 Inputs:
98  rng_function - The function that will be used to generate random bytes.
99 */
100 void uECC_set_rng_ll(uECC_RNG_Function rng_function);
101 
102 /* uECC_make_key() function.
103 Create a public/private key pair.
104 
105 Outputs:
106  public_key - Will be filled in with the public key.
107  private_key - Will be filled in with the private key.
108 
109 Returns 1 if the key pair was generated successfully, 0 if an error occurred.
110 */
111 void uECC_make_key_start(const uint8_t private_key[uECC_BYTES]);
112 int uECC_make_key_continue(void);
113 void uECC_make_key_complete(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]);
114 
115 /* uECC_valid_public_key() function.
116 Check to see if a public key is valid.
117 
118 Note that you are not required to check for a valid public key before using any other uECC
119 functions. However, you may wish to avoid spending CPU time computing a shared secret or
120 verifying a signature using an invalid public key.
121 
122 Inputs:
123  public_key - The public key to check.
124 
125 Returns 1 if the public key is valid, 0 if it is invalid.
126 */
127 int uECC_valid_public_key_ll(const uint8_t public_key[uECC_BYTES*2]);
128 
129 /* uECC_shared_secret() function.
130 Compute a shared secret given your secret key and someone else's public key.
131 Note: It is recommended that you hash the result of uECC_shared_secret() before using it for
132 symmetric encryption or HMAC.
133 
134 Inputs:
135  public_key - The public key of the remote party.
136  private_key - Your private key.
137 
138 Outputs:
139  secret - Will be filled in with the shared secret value.
140 
141 Returns 1 if the shared secret was generated successfully, 0 if an error occurred.
142 */
143 void uECC_shared_secret_start(const uint8_t public_key[uECC_BYTES*2],
144  const uint8_t private_key[uECC_BYTES]);
145 int uECC_shared_secret_continue(void);
146 void uECC_shared_secret_complete(uint8_t secret[uECC_BYTES]);
147 
148 #ifdef __cplusplus
149 } /* end of extern "C" */
150 #endif
151 
152 #endif /* _MICRO_ECC_LL_H_ */
Platform-independent data types.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.