joylink3_auth_uECC.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*Copyright (c) 2015-2050, JD Smart All rights reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. /* Copyright 2014, Kenneth MacKay. Licensed under the BSD 2-clause license. */
  12. #ifndef _UECC_H_
  13. #define _UECC_H_
  14. #include <stdint.h>
  15. /* Platform selection options.
  16. If uECC_PLATFORM is not defined, the code will try to guess it based on compiler macros.
  17. Possible values for uECC_PLATFORM are defined below: */
  18. #define uECC_arch_other 0
  19. #define uECC_x86 1
  20. #define uECC_x86_64 2
  21. #define uECC_arm 3
  22. #define uECC_arm_thumb 4
  23. #define jl3_uECC_arm_thumb2 5
  24. #define uECC_arm64 6
  25. #define jl3_uECC_avr 7
  26. /* If desired, you can define uECC_WORD_SIZE as appropriate for your platform (1, 4, or 8 bytes).
  27. If uECC_WORD_SIZE is not explicitly defined then it will be automatically set based on your
  28. platform. */
  29. /* Optimization level; trade speed for code size.
  30. Larger values produce code that is faster but larger.
  31. Currently supported values are 0 - 3; 0 is unusably slow for most applications. */
  32. #ifndef uECC_OPTIMIZATION_LEVEL
  33. #define uECC_OPTIMIZATION_LEVEL 2
  34. #endif
  35. /* uECC_SQUARE_FUNC - If enabled (defined as nonzero), this will cause a specific function to be
  36. used for (scalar) squaring instead of the generic multiplication function. This can make things
  37. faster somewhat faster, but increases the code size. */
  38. #ifndef uECC_SQUARE_FUNC
  39. #define uECC_SQUARE_FUNC 0
  40. #endif
  41. /* Curve support selection. Set to 0 to remove that curve. */
  42. #ifndef uECC_SUPPORTS_secp160r1
  43. #define uECC_SUPPORTS_secp160r1 1
  44. #endif
  45. #ifndef uECC_SUPPORTS_secp192r1
  46. #define uECC_SUPPORTS_secp192r1 0
  47. #endif
  48. #ifndef uECC_SUPPORTS_secp224r1
  49. #define uECC_SUPPORTS_secp224r1 0
  50. #endif
  51. #ifndef uECC_SUPPORTS_secp256r1
  52. #define uECC_SUPPORTS_secp256r1 1
  53. #endif
  54. #ifndef uECC_SUPPORTS_secp256k1
  55. #define uECC_SUPPORTS_secp256k1 0
  56. #endif
  57. /* Specifies whether compressed point format is supported.
  58. Set to 0 to disable point compression/decompression functions. */
  59. #ifndef uECC_SUPPORT_COMPRESSED_POINT
  60. #define uECC_SUPPORT_COMPRESSED_POINT 1
  61. #endif
  62. struct uECC_Curve_t;
  63. typedef const struct uECC_Curve_t * uECC_Curve;
  64. #ifdef __cplusplus
  65. extern "C"
  66. {
  67. #endif
  68. #if uECC_SUPPORTS_secp160r1
  69. uECC_Curve jl3_uECC_secp160r1(void);
  70. #endif
  71. #if uECC_SUPPORTS_secp192r1
  72. uECC_Curve uECC_secp192r1(void);
  73. #endif
  74. #if uECC_SUPPORTS_secp224r1
  75. uECC_Curve uECC_secp224r1(void);
  76. #endif
  77. #if uECC_SUPPORTS_secp256r1
  78. uECC_Curve uECC_secp256r1(void);
  79. #endif
  80. #if uECC_SUPPORTS_secp256k1
  81. uECC_Curve uECC_secp256k1(void);
  82. #endif
  83. /* uECC_RNG_Function type
  84. The RNG function should fill 'size' random bytes into 'dest'. It should return 1 if
  85. 'dest' was filled with random data, or 0 if the random data could not be generated.
  86. The filled-in values should be either truly random, or from a cryptographically-secure PRNG.
  87. A correctly functioning RNG function must be set (using jl3_uECC_set_rng()) before calling
  88. jl3_uECC_make_key() or jl3_uECC_sign().
  89. Setting a correctly functioning RNG function improves the resistance to side-channel attacks
  90. for jl3_uECC_shared_secret() and jl3_uECC_sign_deterministic().
  91. A correct RNG function is set by default when building for Windows, Linux, or OS X.
  92. If you are building on another POSIX-compliant system that supports /dev/random or /dev/urandom,
  93. you can define uECC_POSIX to use the predefined RNG. For embedded platforms there is no predefined
  94. RNG function; you must provide your own.
  95. */
  96. typedef int (*uECC_RNG_Function)(uint8_t *dest, unsigned size);
  97. /* jl3_uECC_set_rng() function.
  98. Set the function that will be used to generate random bytes. The RNG function should
  99. return 1 if the random data was generated, or 0 if the random data could not be generated.
  100. On platforms where there is no predefined RNG function (eg embedded platforms), this must
  101. be called before jl3_uECC_make_key() or jl3_uECC_sign() are used.
  102. Inputs:
  103. rng_function - The function that will be used to generate random bytes.
  104. */
  105. void jl3_uECC_set_rng(uECC_RNG_Function rng_function);
  106. /* jl3_uECC_get_rng() function.
  107. Returns the function that will be used to generate random bytes.
  108. */
  109. uECC_RNG_Function jl3_uECC_get_rng(void);
  110. /* jl3_uECC_make_key() function.
  111. Create a public/private key pair.
  112. Outputs:
  113. public_key - Will be filled in with the public key. Must be at least 2 * the curve size
  114. (in bytes) long. For example, if the curve is secp256r1, public_key must be 64
  115. bytes long.
  116. private_key - Will be filled in with the private key. Must be as long as the curve order; this
  117. is typically the same as the curve size, except for secp160r1. For example, if the
  118. curve is secp256r1, private_key must be 32 bytes long.
  119. For secp160r1, private_key must be 21 bytes long! Note that the first byte will
  120. almost always be 0 (there is about a 1 in 2^80 chance of it being non-zero).
  121. Returns 1 if the key pair was generated successfully, 0 if an error occurred.
  122. */
  123. int jl3_uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve);
  124. /* jl3_uECC_shared_secret() function.
  125. Compute a shared secret given your secret key and someone else's public key.
  126. Note: It is recommended that you hash the result of jl3_uECC_shared_secret() before using it for
  127. symmetric encryption or HMAC.
  128. Inputs:
  129. public_key - The public key of the remote party.
  130. private_key - Your private key.
  131. Outputs:
  132. secret - Will be filled in with the shared secret value. Must be the same size as the
  133. curve size; for example, if the curve is secp256r1, secret must be 32 bytes long.
  134. Returns 1 if the shared secret was generated successfully, 0 if an error occurred.
  135. */
  136. int jl3_uECC_shared_secret(const uint8_t *public_key,
  137. const uint8_t *private_key,
  138. uint8_t *secret,
  139. uECC_Curve curve);
  140. #if uECC_SUPPORT_COMPRESSED_POINT
  141. /* jl3_uECC_compress() function.
  142. Compress a public key.
  143. Inputs:
  144. public_key - The public key to compress.
  145. Outputs:
  146. compressed - Will be filled in with the compressed public key. Must be at least
  147. (curve size + 1) bytes long; for example, if the curve is secp256r1,
  148. compressed must be 33 bytes long.
  149. */
  150. void jl3_uECC_compress(const uint8_t *public_key, uint8_t *compressed, uECC_Curve curve);
  151. /* jl3_uECC_decompress() function.
  152. Decompress a compressed public key.
  153. Inputs:
  154. compressed - The compressed public key.
  155. Outputs:
  156. public_key - Will be filled in with the decompressed public key.
  157. */
  158. void jl3_uECC_decompress(const uint8_t *compressed, uint8_t *public_key, uECC_Curve curve);
  159. #endif /* uECC_SUPPORT_COMPRESSED_POINT */
  160. /* jl3_uECC_valid_public_key() function.
  161. Check to see if a public key is valid.
  162. Note that you are not required to check for a valid public key before using any other uECC
  163. functions. However, you may wish to avoid spending CPU time computing a shared secret or
  164. verifying a signature using an invalid public key.
  165. Inputs:
  166. public_key - The public key to check.
  167. Returns 1 if the public key is valid, 0 if it is invalid.
  168. */
  169. int jl3_uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve);
  170. /* jl3_uECC_compute_public_key() function.
  171. Compute the corresponding public key for a private key.
  172. Inputs:
  173. private_key - The private key to compute the public key for
  174. Outputs:
  175. public_key - Will be filled in with the corresponding public key
  176. Returns 1 if the key was computed successfully, 0 if an error occurred.
  177. */
  178. int jl3_uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key, uECC_Curve curve);
  179. /* jl3_uECC_sign() function.
  180. Generate an ECDSA signature for a given hash value.
  181. Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to
  182. this function along with your private key.
  183. Inputs:
  184. private_key - Your private key.
  185. message_hash - The hash of the message to sign.
  186. hash_size - The size of message_hash in bytes.
  187. Outputs:
  188. signature - Will be filled in with the signature value. Must be at least 2 * curve size long.
  189. For example, if the curve is secp256r1, signature must be 64 bytes long.
  190. Returns 1 if the signature generated successfully, 0 if an error occurred.
  191. */
  192. int jl3_uECC_sign(const uint8_t *private_key,
  193. const uint8_t *message_hash,
  194. unsigned hash_size,
  195. uint8_t *signature,
  196. uECC_Curve curve);
  197. /* uECC_HashContext structure.
  198. This is used to pass in an arbitrary hash function to jl3_uECC_sign_deterministic().
  199. The structure will be used for multiple hash computations; each time a new hash
  200. is computed, init_hash() will be called, followed by one or more calls to
  201. update_hash(), and finally a call to finish_hash() to produce the resulting hash.
  202. The intention is that you will create a structure that includes uECC_HashContext
  203. followed by any hash-specific data. For example:
  204. typedef struct SHA256_HashContext {
  205. uECC_HashContext uECC;
  206. SHA256_CTX ctx;
  207. } SHA256_HashContext;
  208. void init_SHA256(uECC_HashContext *base) {
  209. SHA256_HashContext *context = (SHA256_HashContext *)base;
  210. SHA256_Init(&context->ctx);
  211. }
  212. void update_SHA256(uECC_HashContext *base,
  213. const uint8_t *message,
  214. unsigned message_size) {
  215. SHA256_HashContext *context = (SHA256_HashContext *)base;
  216. SHA256_Update(&context->ctx, message, message_size);
  217. }
  218. void finish_SHA256(uECC_HashContext *base, uint8_t *hash_result) {
  219. SHA256_HashContext *context = (SHA256_HashContext *)base;
  220. SHA256_Final(hash_result, &context->ctx);
  221. }
  222. ... when signing ...
  223. {
  224. uint8_t tmp[32 + 32 + 64];
  225. SHA256_HashContext ctx = {{&init_SHA256, &update_SHA256, &finish_SHA256, 64, 32, tmp}};
  226. jl3_uECC_sign_deterministic(key, message_hash, &ctx.uECC, signature);
  227. }
  228. */
  229. typedef struct jl3_uECC_HashContext {
  230. void (*init_hash)(const struct jl3_uECC_HashContext *context);
  231. void (*update_hash)(const struct jl3_uECC_HashContext *context,
  232. const uint8_t *message,
  233. unsigned message_size);
  234. void (*finish_hash)(const struct jl3_uECC_HashContext *context, uint8_t *hash_result);
  235. unsigned block_size; /* Hash function block size in bytes, eg 64 for SHA-256. */
  236. unsigned result_size; /* Hash function result size in bytes, eg 32 for SHA-256. */
  237. uint8_t *tmp; /* Must point to a buffer of at least (2 * result_size + block_size) bytes. */
  238. } jl3_uECC_HashContext;
  239. /* jl3_uECC_sign_deterministic() function.
  240. Generate an ECDSA signature for a given hash value, using a deterministic algorithm
  241. (see RFC 6979). You do not need to set the RNG using jl3_uECC_set_rng() before calling
  242. this function; however, if the RNG is defined it will improve resistance to side-channel
  243. attacks.
  244. Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it to
  245. this function along with your private key and a hash context. Note that the message_hash
  246. does not need to be computed with the same hash function used by hash_context.
  247. Inputs:
  248. private_key - Your private key.
  249. message_hash - The hash of the message to sign.
  250. hash_size - The size of message_hash in bytes.
  251. hash_context - A hash context to use.
  252. Outputs:
  253. signature - Will be filled in with the signature value.
  254. Returns 1 if the signature generated successfully, 0 if an error occurred.
  255. */
  256. int jl3_uECC_sign_deterministic(const uint8_t *private_key,
  257. const uint8_t *message_hash,
  258. unsigned hash_size,
  259. const jl3_uECC_HashContext *hash_context,
  260. uint8_t *signature,
  261. uECC_Curve curve);
  262. /* jl3_uECC_verify() function.
  263. Verify an ECDSA signature.
  264. Usage: Compute the hash of the signed data using the same hash as the signer and
  265. pass it to this function along with the signer's public key and the signature values (r and s).
  266. Inputs:
  267. public_key - The signer's public key.
  268. message_hash - The hash of the signed data.
  269. hash_size - The size of message_hash in bytes.
  270. signature - The signature value.
  271. Returns 1 if the signature is valid, 0 if it is invalid.
  272. */
  273. int jl3_uECC_verify(const uint8_t *public_key,
  274. const uint8_t *message_hash,
  275. unsigned hash_size,
  276. const uint8_t *signature,
  277. uECC_Curve curve);
  278. #ifdef __cplusplus
  279. } /* end of extern "C" */
  280. #endif
  281. #endif /* _UECC_H_ */