ocrypto_rsa_key.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file
  41. * @defgroup nrf_oberon_rsa_key RSA key APIs
  42. * @ingroup nrf_oberon_rsa
  43. * @{
  44. * @brief Type declarations for RSA APIs.
  45. *
  46. * RSA is a number theoretic public-key encryption and signature algorithm.
  47. *
  48. * These functions support the setup of 1024 and 2048 RSA secret and public keys.
  49. */
  50. #ifndef OCRYPTO_RSA_KEY_H
  51. #define OCRYPTO_RSA_KEY_H
  52. #include <stddef.h>
  53. #include <stdint.h>
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * The Public RSA Exponent.
  59. */
  60. #define PUB_EXP 65537 // 2^16 + 1
  61. /**@name 1024-bit RSA Keys
  62. *
  63. * This group of keys is used for 1024-bit RSA.
  64. */
  65. /**@{*/
  66. /**
  67. * 1024-bit RSA public key.
  68. */
  69. typedef struct {
  70. /**@cond */
  71. uint32_t n[32];
  72. // e = 65537
  73. /**@endcond */
  74. } ocrypto_rsa1024_pub_key;
  75. /**
  76. * 1024 bit RSA secret key.
  77. */
  78. typedef struct {
  79. /**@cond */
  80. uint32_t n[32];
  81. uint32_t d[32]; // x^(e*d) mod n == x
  82. /**@endcond */
  83. } ocrypto_rsa1024_key;
  84. /**
  85. * 1024-bit RSA secret key with CRT coefficients.
  86. */
  87. typedef struct {
  88. /**@cond */
  89. uint32_t n[32];
  90. uint32_t p[16], q[16]; // primes, p*q = n
  91. uint32_t dp[16], dq[16]; // d mod (p-1), d mod (q-1)
  92. uint32_t qinv[16]; // 1/q mod p
  93. /**@endcond */
  94. } ocrypto_rsa1024_crt_key;
  95. /**@}*/
  96. /**@name 2048-bit RSA Keys
  97. *
  98. * This group of keys is used for 2048-bit RSA.
  99. */
  100. /**@{*/
  101. /**
  102. * 2048-bit RSA public key.
  103. */
  104. typedef struct {
  105. /**@cond */
  106. uint32_t n[64];
  107. // e = 65537
  108. /**@endcond */
  109. } ocrypto_rsa2048_pub_key;
  110. /**
  111. * 2048-bit RSA secret key.
  112. */
  113. typedef struct {
  114. /**@cond */
  115. uint32_t n[64];
  116. uint32_t d[64]; // x^(e*d) mod n == x
  117. /**@endcond */
  118. } ocrypto_rsa2048_key;
  119. /**
  120. * 2048-bit RSA secret key with CRT coefficients.
  121. */
  122. typedef struct {
  123. /**@cond */
  124. uint32_t n[64];
  125. uint32_t p[32], q[32]; // primes, p*q = n
  126. uint32_t dp[32], dq[32]; // d mod (p-1), d mod (q-1)
  127. uint32_t qinv[32]; // 1/q mod p
  128. /**@endcond */
  129. } ocrypto_rsa2048_crt_key;
  130. /**@}*/
  131. /**@name 1024-bit RSA key setup
  132. *
  133. * This group of functions is used for 1024-bit RSA key setup.
  134. */
  135. /**@{*/
  136. /**
  137. * 1024-bit RSA public key setup.
  138. *
  139. * @param[out] k The initialized public key.
  140. * @param n The RSA modulus. Must be exactly 1024 bits.
  141. * @param nlen Length of @p n.
  142. *
  143. * @retval -1 If the input length is invalid.
  144. * @retval 0 Otherwise.
  145. *
  146. * @remark The public exponent is fixed at 65537.
  147. */
  148. int ocrypto_rsa1024_init_pub_key(
  149. ocrypto_rsa1024_pub_key *k,
  150. const uint8_t *n, size_t nlen);
  151. /**
  152. * 1024-bit RSA secret key setup.
  153. *
  154. * @param[out] k The initialized public key.
  155. * @param n The RSA modulus. Must be exactly 1024 bits.
  156. * @param nlen Length of @p n.
  157. * @param d The secret exponent. Must be <= 1024 bits.
  158. * @param dlen Length of @p d.
  159. *
  160. * @retval -1 If the input length is invalid.
  161. * @retval 0 Otherwise.
  162. */
  163. int ocrypto_rsa1024_init_key(
  164. ocrypto_rsa1024_key *k,
  165. const uint8_t *n, size_t nlen,
  166. const uint8_t *d, size_t dlen);
  167. /**
  168. * 1024-bit RSA secret key setup with CRT coefficients.
  169. *
  170. * @param[out] k The initialized secret key.
  171. * @param p The 1. RSA prime. Must be exactly 512 bits.
  172. * @param plen Length of @p p.
  173. * @param q The 2. RSA prime. Must be exactly 512 bits.
  174. * @param qlen Length of @p q.
  175. * @param dp The 1. CRT exponent. dp = d mod (p-1).
  176. * @param dplen Length of @p dp.
  177. * @param dq The 2. CRT exponent. dq = d mod (q-1).
  178. * @param dqlen Length of @p dq.
  179. * @param qinv The CRT coefficient. qinv = 1/q mod p.
  180. * @param qilen Length of @p qinv.
  181. *
  182. * @retval -1 If the input length is invalid.
  183. * @retval 0 Otherwise.
  184. */
  185. int ocrypto_rsa1024_init_crt_key(
  186. ocrypto_rsa1024_crt_key *k,
  187. const uint8_t *p, size_t plen,
  188. const uint8_t *q, size_t qlen,
  189. const uint8_t *dp, size_t dplen,
  190. const uint8_t *dq, size_t dqlen,
  191. const uint8_t *qinv, size_t qilen);
  192. /**@}*/
  193. /**@name 2048-bit RSA key setup
  194. *
  195. * This group of functions is used for 2048-bit RSA key setup.
  196. */
  197. /**@{*/
  198. /**
  199. * 2048-bit RSA public key setup.
  200. *
  201. * @param[out] k The initialized public key.
  202. * @param n The RSA modulus. Must be exactly 2048 bits.
  203. * @param nlen Length of @p n.
  204. *
  205. * @retval -1 If the input length is invalid.
  206. * @retval 0 Otherwise.
  207. *
  208. * @remark The public exponent is fixed at 65537.
  209. */
  210. int ocrypto_rsa2048_init_pub_key(
  211. ocrypto_rsa2048_pub_key *k,
  212. const uint8_t *n, size_t nlen);
  213. /**
  214. * 2048-bit RSA secret key setup.
  215. *
  216. * @param[out] k The initialized public key.
  217. * @param n The RSA modulus. Must be exactly 2048 bits.
  218. * @param nlen Length of @p n.
  219. * @param d The secret exponent. Must be <= 2048 bits.
  220. * @param dlen Length of @p d.
  221. *
  222. * @retval -1 If the input length is invalid.
  223. * @retval 0 Otherwise.
  224. */
  225. int ocrypto_rsa2048_init_key(ocrypto_rsa2048_key *k,
  226. const uint8_t *n, size_t nlen,
  227. const uint8_t *d, size_t dlen);
  228. /**
  229. * 2048-bit RSA secret key setup with CRT coefficients.
  230. *
  231. * @param[out] k The initialized secret key.
  232. * @param p The 1. RSA prime. Must be exactly 1024 bits.
  233. * @param plen Length of @p p.
  234. * @param q The 2. RSA prime. Must be exactly 1024 bits.
  235. * @param qlen Length of @p q.
  236. * @param dp The 1. CRT exponent. dp = d mod (p-1).
  237. * @param dplen Length of @p dp.
  238. * @param dq The 2. CRT exponent. dq = d mod (q-1).
  239. * @param dqlen Length of @p dq.
  240. * @param qinv The CRT coefficient. qinv = 1/q mod p.
  241. * @param qilen Length of @p qinv.
  242. *
  243. * @retval -1 If the input length is invalid.
  244. * @retval 0 Otherwise.
  245. */
  246. int ocrypto_rsa2048_init_crt_key(
  247. ocrypto_rsa2048_crt_key *k,
  248. const uint8_t *p, size_t plen,
  249. const uint8_t *q, size_t qlen,
  250. const uint8_t *dp, size_t dplen,
  251. const uint8_t *dq, size_t dqlen,
  252. const uint8_t *qinv, size_t qilen);
  253. /**@}*/
  254. #ifdef __cplusplus
  255. }
  256. #endif
  257. #endif /* #ifndef OCRYPTO_RSA_KEY_H */
  258. /** @} */