occ_rsa.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Copyright (c) 2016 - 2018, 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 support RSA encrytion and signatures with 1024 and 2048 bit
  44. * modulo and PKCS1 V1.5 padding.
  45. */
  46. #ifndef OCC_RSA_H
  47. #define OCC_RSA_H
  48. #include <stdint.h>
  49. #include "occ_rsa_key.h"
  50. #include "nrf.h"
  51. #if defined(NRF51)
  52. #error "Oberon library currently doesn't support RSA for NRF51"
  53. #endif
  54. /**@name 1024 Bit RSA Functions.
  55. *
  56. * This group of functions is used for 1024 bit RSA.
  57. */
  58. /**@{*/
  59. /**
  60. * 1024 bit RSA PKCS1 V1.5 encryption.
  61. *
  62. * The message @p m is encryted to a ciphertext returned in @p c.
  63. *
  64. * @param[out] c The generated 128 byte cyphertext.
  65. * @param m,mlen The message to be encrypted. 0 <= mlen <= 117.
  66. * @param seed,slen The random sedd to be used for the padding. slen >= 125 - mlen.
  67. * @param pk A valid 1024 bit RSA public key.
  68. *
  69. * @returns -1 If the message is too long (mlen > 117).
  70. * @returns -2 If the seed is too short (slen < 125 - mlen).
  71. * @returns 0 Otherwise.
  72. *
  73. * @remark The key @p pk should be initialized with @c occ_rsa1024_init_pub_key.
  74. * @remark The @p seed should consist of non-zero random bytes.
  75. * @remark @p c and @p m can point to the same address.
  76. */
  77. int occ_rsa1024_pkcs1_v15_encrypt(uint8_t c[128], const uint8_t *m, int mlen, const uint8_t *seed, int slen, const occ_rsa1024_pub_key *pk);
  78. /**
  79. * 1024 bit RSA PKCS1 V1.5 decryption.
  80. *
  81. * The ciphertext @p c is decryted to the message returned in @p m.
  82. *
  83. * @param[out] m,mlen The decypted message. The buffer must be long enough to hold the meaasge.
  84. * @param c The 128 byte cyphertext to decrypt.
  85. * @param k A valid 1024 bit RSA secret key.
  86. * @returns -1 If decrytion failed.
  87. * @returns -2 If the output buffer is too short (mlen < length of message).
  88. * @returns n If a message of length n was successfully decrypted.
  89. *
  90. * @remark The key @p k should be initialized with @c occ_rsa1024_init_key.
  91. * @remark @p m and @p c can point to the same address.
  92. */
  93. int occ_rsa1024_pkcs1_v15_decrypt(uint8_t *m, int mlen, const uint8_t c[128], const occ_rsa1024_key *k);
  94. /**
  95. * 1024 bit RSA PKCS1 V1.5 decryption with CRT acceleration.
  96. *
  97. * The ciphertext @p c is decryted to the message returned in @p m.
  98. *
  99. * @param[out] m,mlen The decypted message. The buffer must be long enough to hold the meaasge.
  100. * @param c The 128 byte cyphertext to decrypt.
  101. * @param k A valid 1024 bit RSA secret key with CRT coefficients.
  102. * @returns -1 If decrytion failed.
  103. * @returns -2 If the output buffer is too short (mlen < length of message).
  104. * @returns n If a message of length n was successfully decrypted.
  105. *
  106. * @remark The key @p k should be initialized with @c occ_rsa1024_init_crt_key.
  107. * @remark @p m and @p c can point to the same address.
  108. */
  109. int occ_rsa1024_pkcs1_v15_crt_decrypt(uint8_t *m, int mlen, const uint8_t c[128], const occ_rsa1024_crt_key *k);
  110. /**
  111. * 1024 bit RSA PKCS1 V1.5 SHA-256 sign.
  112. *
  113. * The message @p m is signed and the signature returned in @p s.
  114. *
  115. * @param[out] s The generated 128 byte signature.
  116. * @param m,mlen The message to be signed.
  117. * @param k A valid 1024 bit RSA secret key.
  118. * @returns 0
  119. *
  120. * @remark The key @p k should be initialized with @c occ_rsa1024_init_key.
  121. * @remark @p s and @p m can point to the same address.
  122. */
  123. int occ_rsa1024_pkcs1_v15_sha256_sign(uint8_t s[128], const uint8_t *m, int mlen, const occ_rsa1024_key *k);
  124. /**
  125. * 1024 bit RSA PKCS1 V1.5 SHA-256 sign with CRT acceleration.
  126. *
  127. * The message @p m is signed and the signature returned in @p s.
  128. *
  129. * @param[out] s The generated 128 byte signature.
  130. * @param m,mlen The message to be signed.
  131. * @param k A valid 1024 bit RSA secret key with CRT coefficients.
  132. * @returns 0
  133. *
  134. * @remark The key @p k should be initialized with @c occ_rsa1024_init_crt_key.
  135. * @remark @p s and @p m can point to the same address.
  136. */
  137. int occ_rsa1024_pkcs1_v15_sha256_crt_sign(uint8_t s[128], const uint8_t *m, int mlen, const occ_rsa1024_crt_key *k);
  138. /**
  139. * 1024 bit RSA PKCS1 V1.5 SHA-256 signature verify.
  140. *
  141. * The signature @p s is verified for a correct signature of message @p m.
  142. *
  143. * @param s The 128 byte signature.
  144. * @param m,mlen The signed message.
  145. * @param pk A valid 1024 bit RSA public key.
  146. *
  147. * @returns 0 If the signature is successfully verified.
  148. * @returns -1 If verification failed.
  149. *
  150. * @remark The key @p pk should be initialized with @c occ_rsa1024_init_pub_key.
  151. */
  152. int occ_rsa1024_pkcs1_v15_sha256_verify(const uint8_t s[128], const uint8_t *m, int mlen, const occ_rsa1024_pub_key *pk);
  153. /**@}*/
  154. /**@name 2048 Bit RSA Functions.
  155. *
  156. * This group of functions is used for 2048 bit RSA.
  157. */
  158. /**@{*/
  159. /**
  160. * 2048 bit RSA PKCS1 V1.5 encryption.
  161. *
  162. * The message @p m is encryted to a ciphertext returned in @p c.
  163. *
  164. * @param[out] c The generated 256 byte cyphertext.
  165. * @param m,mlen The message to be encrypted. 0 <= mlen <= 245.
  166. * @param seed,slen The random sedd to be used for the padding. slen >= 253 - mlen.
  167. * @param pk A valid 2048 bit RSA public key.
  168. *
  169. * @returns -1 If the message is too long (mlen > 245).
  170. * @returns -2 If the seed is too short (slen < 253 - mlen).
  171. * @returns 0 Otherwise.
  172. *
  173. * @remark The key @p pk should be initialized with @c occ_rsa2048_init_pub_key.
  174. * @remark The @p seed should consist of non-zero random bytes.
  175. * @remark @p c and @p m can point to the same address.
  176. */
  177. int occ_rsa2048_pkcs1_v15_encrypt(uint8_t c[256], const uint8_t *m, int mlen, const uint8_t *seed, int slen, const occ_rsa2048_pub_key *pk);
  178. /**
  179. * 2048 bit RSA PKCS1 V1.5 decryption.
  180. *
  181. * The ciphertext @p c is decryted to the message returned in @p m.
  182. *
  183. * @param[out] m,mlen The decypted message. The buffer must be long enough to hold the meaasge.
  184. * @param c The 256 byte cyphertext to decrypt.
  185. * @param k A valid 2048 bit RSA secret key.
  186. * @returns -1 If decrytion failed.
  187. * @returns -2 If the output buffer is too short (mlen < length of message).
  188. * @returns n If a message of length n was successfully decrypted.
  189. *
  190. * @remark The key @p k should be initialized with @c occ_rsa2048_init_key.
  191. * @remark @p m and @p c can point to the same address.
  192. */
  193. int occ_rsa2048_pkcs1_v15_decrypt(uint8_t *m, int mlen, const uint8_t c[256], const occ_rsa2048_key *k);
  194. /**
  195. * 2048 bit RSA PKCS1 V1.5 decryption with CRT acceleration.
  196. *
  197. * The ciphertext @p c is decryted to the message returned in @p m.
  198. *
  199. * @param[out] m,mlen The decypted message. The buffer must be long enough to hold the meaasge.
  200. * @param c The 256 byte cyphertext to decrypt.
  201. * @param k A valid 2048 bit RSA secret key with CRT coefficients.
  202. * @returns -1 If decrytion failed.
  203. * @returns -2 If the output buffer is too short (mlen < length of message).
  204. * @returns n If a message of length n was successfully decrypted.
  205. *
  206. * @remark The key @p k should be initialized with @c occ_rsa2048_init_crt_key.
  207. * @remark @p m and @p c can point to the same address.
  208. */
  209. int occ_rsa2048_pkcs1_v15_crt_decrypt(uint8_t *m, int mlen, const uint8_t c[256], const occ_rsa2048_crt_key *k);
  210. /**
  211. * 2048 bit RSA PKCS1 V1.5 SHA-256 sign.
  212. *
  213. * The message @p m is signed and the signature returned in @p s.
  214. *
  215. * @param[out] s The generated 256 byte signature.
  216. * @param m,mlen The message to be signed.
  217. * @param k A valid 2048 bit RSA secret key.
  218. * @returns 0
  219. *
  220. * @remark The key @p k should be initialized with @c occ_rsa2048_init_key.
  221. * @remark @p s and @p m can point to the same address.
  222. */
  223. int occ_rsa2048_pkcs1_v15_sha256_sign(uint8_t s[256], const uint8_t *m, int mlen, const occ_rsa2048_key *k);
  224. /**
  225. * 2048 bit RSA PKCS1 V1.5 SHA-256 sign with CRT acceleration.
  226. *
  227. * The message @p m is signed and the signature returned in @p s.
  228. *
  229. * @param[out] s The generated 256 byte signature.
  230. * @param m,mlen The message to be signed.
  231. * @param k A valid 2048 bit RSA secret key with CRT coefficients.
  232. * @returns 0
  233. *
  234. * @remark The key @p k should be initialized with @c occ_rsa2048_init_crt_key.
  235. * @remark @p s and @p m can point to the same address.
  236. */
  237. int occ_rsa2048_pkcs1_v15_sha256_crt_sign(uint8_t s[256], const uint8_t *m, int mlen, const occ_rsa2048_crt_key *k);
  238. /**
  239. * 2048 bit RSA PKCS1 V1.5 SHA-256 signature verify.
  240. *
  241. * The signature @p s is verified for a correct signature of message @p m.
  242. *
  243. * @param s The 256 byte signature.
  244. * @param m,mlen The signed message.
  245. * @param pk A valid 2048 bit RSA public key.
  246. *
  247. * @returns 0 If the signature is successfully verified.
  248. * @returns -1 If verification failed.
  249. *
  250. * @remark The key @p pk should be initialized with @c occ_rsa2048_init_pub_key.
  251. */
  252. int occ_rsa2048_pkcs1_v15_sha256_verify(const uint8_t s[256], const uint8_t *m, int mlen, const occ_rsa2048_pub_key *pk);
  253. /**@}*/
  254. #endif