occ_rsa_key.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * Copyright (c) 2016 - 2019, 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. * RSA is a number theoretic public-key encryption and signature algorithm.
  42. *
  43. * These functions suport the setup of 1024 and 4069 RSA secret and public keys.
  44. */
  45. #ifndef OCC_RSA_KEY_H
  46. #define OCC_RSA_KEY_H
  47. #include <stdint.h>
  48. #include "nrf.h"
  49. #if defined(NRF51)
  50. #error "Oberon library currently doesn't support RSA for NRF51"
  51. #endif
  52. /**
  53. * The Public RSA Exponent.
  54. */
  55. #define PUB_EXP 65537 // 2^16 + 1
  56. /**@name 1024 Bit RSA Keys.
  57. *
  58. * This group of keys is used for 1024 bit RSA.
  59. */
  60. /**@{*/
  61. /**
  62. * 1024 bit RSA public key
  63. */
  64. typedef struct {
  65. uint32_t n[32];
  66. // e = 65537
  67. } occ_rsa1024_pub_key;
  68. /**
  69. * 1024 bit RSA secret key
  70. */
  71. typedef struct {
  72. uint32_t n[32];
  73. uint32_t d[32]; // x^(e*d) mod n == x
  74. } occ_rsa1024_key;
  75. /**
  76. * 1024 bit RSA secret key with CRT coefficients
  77. */
  78. typedef struct {
  79. uint32_t n[32];
  80. uint32_t p[16], q[16]; // primes, p*q = n
  81. uint32_t dp[16], dq[16]; // d mod (p-1), d mod (q-1)
  82. uint32_t qinv[16]; // 1/q mod p
  83. } occ_rsa1024_crt_key;
  84. /**@}*/
  85. /**@name 2048 Bit RSA Keys.
  86. *
  87. * This group of keys is used for 2048 bit RSA.
  88. */
  89. /**@{*/
  90. /**
  91. * 2048 bit RSA public key
  92. */
  93. typedef struct {
  94. uint32_t n[64];
  95. // e = 65537
  96. } occ_rsa2048_pub_key;
  97. /**
  98. * 2048 bit RSA secret key
  99. */
  100. typedef struct {
  101. uint32_t n[64];
  102. uint32_t d[64]; // x^(e*d) mod n == x
  103. } occ_rsa2048_key;
  104. /**
  105. * 2048 bit RSA secret key with CRT coefficients
  106. */
  107. typedef struct {
  108. uint32_t n[64];
  109. uint32_t p[32], q[32]; // primes, p*q = n
  110. uint32_t dp[32], dq[32]; // d mod (p-1), d mod (q-1)
  111. uint32_t qinv[32]; // 1/q mod p
  112. } occ_rsa2048_crt_key;
  113. /**@}*/
  114. /**@name 1024 Bit RSA key setup.
  115. *
  116. * This group of functions is used for 1024 bit RSA key setup.
  117. */
  118. /**@{*/
  119. /**
  120. * 1024 bit RSA public key setup.
  121. *
  122. * @param[out] k The initialzed public key.
  123. * @param n,nlen The RSA modulus. Must be exactly 1024 bits.
  124. *
  125. * @retruns -1 If the input length is not correct
  126. * @returns 0 Otherwise.
  127. * @remark The public exponent is fixed at 65537.
  128. */
  129. int occ_rsa1024_init_pub_key (occ_rsa1024_pub_key *k,
  130. const uint8_t *n, int nlen);
  131. /**
  132. * 1024 bit RSA secret key setup.
  133. *
  134. * @param[out] k The initialzed public key.
  135. * @param n,nlen The RSA modulus. Must be exactly 1024 bits.
  136. * @param d,dlen The secret exponent. Must be <= 1024 bits.
  137. *
  138. * @retruns -1 If the input length is not correct
  139. * @returns 0 Otherwise.
  140. */
  141. int occ_rsa1024_init_key (occ_rsa1024_key *k,
  142. const uint8_t *n, int nlen,
  143. const uint8_t *d, int dlen);
  144. /**
  145. * 1024 bit RSA secret key setup with CRT coefficients.
  146. *
  147. * @param[out] k The initialzed secret key.
  148. * @param p,plen The 1. RSA prime. Must be exactly 512 bits.
  149. * @param q,qlen The 2. RSA prime. Must be exactly 512 bits.
  150. * @param dp,dplen The 1. CRT exponent. dp = d mod (p-1).
  151. * @param dq,dqlen The 2. CRT exponent. dq = d mod (q-1).
  152. * @param qinv,qilen The CRT coefficient. qinv = 1/q mod p.
  153. *
  154. * @retruns -1 If the input length is not correct
  155. * @returns 0 Otherwise.
  156. */
  157. int occ_rsa1024_init_crt_key (occ_rsa1024_crt_key *k,
  158. const uint8_t *p, int plen,
  159. const uint8_t *q, int qlen,
  160. const uint8_t *dp, int dplen,
  161. const uint8_t *dq, int dqlen,
  162. const uint8_t *qinv, int qilen);
  163. /**@}*/
  164. /**@name 2048 Bit RSA key setup.
  165. *
  166. * This group of functions is used for 2048 bit RSA key setup.
  167. */
  168. /**@{*/
  169. /**
  170. * 2048 bit RSA public key setup.
  171. *
  172. * @param[out] k The initialzed public key.
  173. * @param n,nlen The RSA modulus. Must be exactly 2048 bits.
  174. *
  175. * @retruns -1 If the input length is not correct
  176. * @returns 0 Otherwise.
  177. * @remark The public exponent is fixed at 65537.
  178. */
  179. int occ_rsa2048_init_pub_key (occ_rsa2048_pub_key *k,
  180. const uint8_t *n, int nlen);
  181. /**
  182. * 2048 bit RSA secret key setup.
  183. *
  184. * @param[out] k The initialzed public key.
  185. * @param n,nlen The RSA modulus. Must be exactly 2048 bits.
  186. * @param d,dlen The secret exponent. Must be <= 2048 bits.
  187. *
  188. * @retruns -1 If the input length is not correct
  189. * @returns 0 Otherwise.
  190. */
  191. int occ_rsa2048_init_key (occ_rsa2048_key *k,
  192. const uint8_t *n, int nlen,
  193. const uint8_t *d, int dlen);
  194. /**
  195. * 2048 bit RSA secret key setup with CRT coefficients.
  196. *
  197. * @param[out] k The initialzed secret key.
  198. * @param p,plen The 1. RSA prime. Must be exactly 1024 bits.
  199. * @param q,qlen The 2. RSA prime. Must be exactly 1024 bits.
  200. * @param dp,dplen The 1. CRT exponent. dp = d mod (p-1).
  201. * @param dq,dqlen The 2. CRT exponent. dq = d mod (q-1).
  202. * @param qinv,qilen The CRT coefficient. qinv = 1/q mod p.
  203. *
  204. * @retruns -1 If the input length is not correct
  205. * @returns 0 Otherwise.
  206. */
  207. int occ_rsa2048_init_crt_key (occ_rsa2048_crt_key *k,
  208. const uint8_t *p, int plen,
  209. const uint8_t *q, int qlen,
  210. const uint8_t *dp, int dplen,
  211. const uint8_t *dq, int dqlen,
  212. const uint8_t *qinv, int qilen);
  213. /**@}*/
  214. #endif