92e13132d115bef7201ec1fc672551bf80d0d038
[platform/upstream/iotivity.git] / extlibs / tinydtls / ecc / ecc.h
1 /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
2
3 #ifndef _MICRO_ECC_H_
4 #define _MICRO_ECC_H_
5
6 #include <stdint.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
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. */
20
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
28 #ifndef uECC_ASM
29     #define uECC_ASM uECC_asm_none//uECC_asm_fast
30 #endif
31
32 /* Curve selection options. */
33 #define uECC_secp160r1 1
34 #define uECC_secp192r1 2
35 #define uECC_secp256r1 3
36 #define uECC_secp256k1 4
37 #ifndef uECC_CURVE
38     #define uECC_CURVE uECC_secp256r1
39 #endif
40
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
44
45 #define uECC_CONCAT1(a, b) a##b
46 #define uECC_CONCAT(a, b) uECC_CONCAT1(a, b)
47
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 */
52
53 #define uECC_BYTES uECC_CONCAT(uECC_size_, uECC_CURVE)
54
55 #ifdef __cplusplus
56 extern "C"
57 {
58 #endif
59
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.
64
65 A correctly functioning RNG function must be set (using uECC_set_rng()) before calling
66 uECC_make_key() or uECC_sign().
67
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.
72 */
73 typedef int (*uECC_RNG_Function)(uint8_t *p_dest, unsigned p_size);
74
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.
78
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.
81
82 Inputs:
83     p_rng  - The function that will be used to generate random bytes.
84 */
85 void uECC_set_rng(uECC_RNG_Function p_rng);
86
87 /* uECC_make_key() function.
88 Create a public/private key pair.
89
90 Outputs:
91     p_publicKey  - Will be filled in with the public key.
92     p_privateKey - Will be filled in with the private key.
93
94 Returns 1 if the key pair was generated successfully, 0 if an error occurred.
95 */
96 int uECC_make_key(uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_privateKey[uECC_BYTES]);
97
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.
101
102 Inputs:
103     p_publicKey  - The public key of the remote party.
104     p_privateKey - Your private key.
105
106 Outputs:
107     p_secret - Will be filled in with the shared secret value.
108
109 Returns 1 if the shared secret was generated successfully, 0 if an error occurred.
110 */
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]);
112
113 /* uECC_compress() function.
114 Compress a public key.
115
116 Inputs:
117     p_publicKey - The public key to compress.
118
119 Outputs:
120     p_compressed - Will be filled in with the compressed public key.
121 */
122 void uECC_compress(const uint8_t p_publicKey[uECC_BYTES*2], uint8_t p_compressed[uECC_BYTES+1]);
123
124 /* uECC_decompress() function.
125 Decompress a compressed public key.
126
127 Inputs:
128     p_compressed - The compressed public key.
129
130 Outputs:
131     p_publicKey - Will be filled in with the decompressed public key.
132 */
133 void uECC_decompress(const uint8_t p_compressed[uECC_BYTES+1], uint8_t p_publicKey[uECC_BYTES*2]);
134
135 /* uECC_sign() function.
136 Generate an ECDSA signature for a given hash value.
137
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.
140
141 Inputs:
142     p_privateKey - Your private key.
143     p_hash       - The message hash to sign.
144
145 Outputs:
146     p_signature  - Will be filled in with the signature value.
147
148 Returns 1 if the signature generated successfully, 0 if an error occurred.
149 */
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]);
151
152 /* uECC_verify() function.
153 Verify an ECDSA signature.
154
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).
157
158 Inputs:
159     p_publicKey - The signer's public key
160     p_hash      - The hash of the signed data.
161     p_signature - The signature value.
162
163 Returns 1 if the signature is valid, 0 if it is invalid.
164 */
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]);
166
167 #ifdef __cplusplus
168 } /* end of extern "C" */
169 #endif
170
171 #endif /* _MICRO_ECC_H_ */