1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
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
15 #define uECC_arm_thumb 4
18 /* If desired, you can define uECC_WORD_SIZE as appropriate for your platform (1, 4, or 8 bytes).
19 If uECC_WORD_SIZE is not explicitly defined then it will be automatically set based on your platform. */
21 /* Inline assembly options.
22 uECC_asm_none - Use standard C99 only.
23 uECC_asm_small - Use GCC inline assembly for the target platform (if available), optimized for minimum size.
24 uECC_asm_fast - Use GCC inline assembly optimized for maximum speed. */
25 #define uECC_asm_none 0
26 #define uECC_asm_small 1
27 #define uECC_asm_fast 2
29 #define uECC_ASM uECC_asm_none//uECC_asm_fast
32 /* Curve selection options. */
33 #define uECC_secp160r1 1
34 #define uECC_secp192r1 2
35 #define uECC_secp256r1 3
36 #define uECC_secp256k1 4
38 #define uECC_CURVE uECC_secp256r1
41 /* uECC_SQUARE_FUNC - If enabled (defined as nonzero), this will cause a specific function to be used for (scalar) squaring
42 instead of the generic multiplication function. This will make things faster by about 8% but increases the code size. */
43 #define uECC_SQUARE_FUNC 1
45 #define uECC_CONCAT1(a, b) a##b
46 #define uECC_CONCAT(a, b) uECC_CONCAT1(a, b)
48 #define uECC_size_1 20 /* secp160r1 */
49 #define uECC_size_2 24 /* secp192r1 */
50 #define uECC_size_3 32 /* secp256r1 */
51 #define uECC_size_4 32 /* secp256k1 */
53 #define uECC_BYTES uECC_CONCAT(uECC_size_, uECC_CURVE)
60 /* uECC_RNG_Function type
61 The RNG function should fill p_size random bytes into p_dest. It should return 1 if
62 p_dest was filled with random data, or 0 if the random data could not be generated.
63 The filled-in values should be either truly random, or from a cryptographically-secure PRNG.
65 A correctly functioning RNG function must be set (using uECC_set_rng()) before calling
66 uECC_make_key() or uECC_sign().
68 A correct RNG function is set by default when building for Windows, Linux, or OS X.
69 If you are building on another POSIX-compliant system that supports /dev/random or /dev/urandom,
70 you can define uECC_POSIX to use the predefined RNG. For embedded platforms there is no predefined
71 RNG function; you must provide your own.
73 typedef int (*uECC_RNG_Function)(uint8_t *p_dest, unsigned p_size);
75 /* uECC_set_rng() function.
76 Set the function that will be used to generate random bytes. The RNG function should
77 return 1 if the random data was generated, or 0 if the random data could not be generated.
79 On platforms where there is no predefined RNG function (eg embedded platforms), this must
80 be called before uECC_make_key() or uECC_sign() are used.
83 p_rng - The function that will be used to generate random bytes.
85 void uECC_set_rng(uECC_RNG_Function p_rng);
87 /* uECC_make_key() function.
88 Create a public/private key pair.
91 p_publicKey - Will be filled in with the public key.
92 p_privateKey - Will be filled in with the private key.
94 Returns 1 if the key pair was generated successfully, 0 if an error occurred.
96 int uECC_make_key(uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_privateKey[uECC_BYTES]);
98 /* uECC_shared_secret() function.
99 Compute a shared secret given your secret key and someone else's public key.
100 Note: It is recommended that you hash the result of uECC_shared_secret() before using it for symmetric encryption or HMAC.
103 p_publicKey - The public key of the remote party.
104 p_privateKey - Your private key.
107 p_secret - Will be filled in with the shared secret value.
109 Returns 1 if the shared secret was generated successfully, 0 if an error occurred.
111 int uECC_shared_secret(const uint8_t p_publicKey[uECC_BYTES*2], const uint8_t p_privateKey[uECC_BYTES], uint8_t p_secret[uECC_BYTES]);
113 /* uECC_compress() function.
114 Compress a public key.
117 p_publicKey - The public key to compress.
120 p_compressed - Will be filled in with the compressed public key.
122 void uECC_compress(const uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_compressed[uECC_BYTES+1]);
124 /* uECC_decompress() function.
125 Decompress a compressed public key.
128 p_compressed - The compressed public key.
131 p_publicKey - Will be filled in with the decompressed public key.
133 void uECC_decompress(const uint8_t p_compressed[uECC_BYTES+1], uint8_t p_publicKey[uECC_BYTES*2]);
135 /* uECC_sign() function.
136 Generate an ECDSA signature for a given hash value.
138 Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to
139 this function along with your private key.
142 p_privateKey - Your private key.
143 p_hash - The message hash to sign.
146 p_signature - Will be filled in with the signature value.
148 Returns 1 if the signature generated successfully, 0 if an error occurred.
150 int uECC_sign(const uint8_t p_privateKey[uECC_BYTES], const uint8_t p_hash[uECC_BYTES], uint8_t p_signature[uECC_BYTES*2]);
152 /* uECC_verify() function.
153 Verify an ECDSA signature.
155 Usage: Compute the hash of the signed data using the same hash as the signer and
156 pass it to this function along with the signer's public key and the signature values (r and s).
159 p_publicKey - The signer's public key
160 p_hash - The hash of the signed data.
161 p_signature - The signature value.
163 Returns 1 if the signature is valid, 0 if it is invalid.
165 int uECC_verify(const uint8_t p_publicKey[uECC_BYTES*2], const uint8_t p_hash[uECC_BYTES], const uint8_t p_signature[uECC_BYTES*2]);
168 } /* end of extern "C" */
171 #endif /* _MICRO_ECC_H_ */