ocrypto_rsa.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 RSA - Rivest-Shamir-Adleman algorithm
  42. * @ingroup nrf_oberon
  43. * @{
  44. * @brief RSA is a number theoretic public-key encryption and signature algorithm.
  45. * @}
  46. * @defgroup nrf_oberon_rsa_api RSA APIs
  47. * @ingroup nrf_oberon_rsa
  48. * @{
  49. * @brief APIs to for RSA encryption/decryption and sign/verify using PKCS1 v1.5, OEAP and PSS.
  50. *
  51. * These functions support RSA encryption and signatures with 1024 and 2048-bit
  52. * modulo and PKCS1 V1.5 padding.
  53. */
  54. #ifndef OCRYPTO_RSA_H
  55. #define OCRYPTO_RSA_H
  56. #include <stddef.h>
  57. #include <stdint.h>
  58. #include "ocrypto_rsa_key.h"
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. /**@name 1024-bit RSA Functions.
  63. *
  64. * This group of functions is used for 1024-bit RSA.
  65. */
  66. /**@{*/
  67. /**
  68. * 1024 bit RSA PKCS1 V1.5 encryption.
  69. *
  70. * The message @p m is encrypted to a ciphertext returned in @p c.
  71. *
  72. * @param[out] c The generated 128-byte ciphertext.
  73. * @param m The message to be encrypted.
  74. * @param mlen Length of @p m. 0 <= mlen <= 117.
  75. * @param seed The random seed to be used for the padding.
  76. * @param slen Length of @p seed. @p slen >= 125 - @p mlen.
  77. * @param pk A valid 1024-bit RSA public key.
  78. *
  79. * @retval -1 If the message is too long (mlen > 117).
  80. * @retval -2 If the seed is too short (slen < 125 - mlen).
  81. * @retval 0 Otherwise.
  82. *
  83. * @remark The key @p pk should be initialized with @c ocrypto_rsa1024_init_pub_key.
  84. * @remark The @p seed should consist of non-zero random bytes.
  85. * @remark @p c and @p m can point to the same address.
  86. */
  87. int ocrypto_rsa1024_pkcs1_v15_encrypt(
  88. uint8_t c[128],
  89. const uint8_t *m, size_t mlen,
  90. const uint8_t *seed, size_t slen,
  91. const ocrypto_rsa1024_pub_key *pk);
  92. /**
  93. * 1024-bit RSA PKCS1 V1.5 decryption.
  94. *
  95. * The ciphertext @p c is decrypted to the message returned in @p m.
  96. *
  97. * @param[out] m The decrypted message. The buffer must be long enough to hold the message.
  98. * @param mlen Length of @p m.
  99. * @param c The 128-byte ciphertext to decrypt.
  100. * @param k A valid 1024-bit RSA secret key.
  101. *
  102. * @retval -1 If decryption failed.
  103. * @retval -2 If the output buffer is too short (mlen < length of message).
  104. * @retval n If a message of length n was successfully decrypted.
  105. *
  106. * @remark The key @p k should be initialized with @c ocrypto_rsa1024_init_key.
  107. * @remark @p m and @p c can point to the same address.
  108. */
  109. int ocrypto_rsa1024_pkcs1_v15_decrypt(
  110. uint8_t *m, size_t mlen,
  111. const uint8_t c[128],
  112. const ocrypto_rsa1024_key *k);
  113. /**
  114. * 1024-bit RSA PKCS1 V1.5 decryption with CRT acceleration.
  115. *
  116. * The ciphertext @p c is decrypted to the message returned in @p m.
  117. *
  118. * @param[out] m The decrypted message. The buffer must be long enough to hold the message.
  119. * @param mlen Length of @p m.
  120. * @param c The 128-byte ciphertext to decrypt.
  121. * @param k A valid 1024-bit RSA secret key with CRT coefficients.
  122. *
  123. * @retval -1 If decryption failed.
  124. * @retval -2 If the output buffer is too short (mlen < length of message).
  125. * @retval n If a message of length n was successfully decrypted.
  126. *
  127. * @remark The key @p k should be initialized with @c ocrypto_rsa1024_init_crt_key.
  128. * @remark @p m and @p c can point to the same address.
  129. */
  130. int ocrypto_rsa1024_pkcs1_v15_crt_decrypt(
  131. uint8_t *m, size_t mlen,
  132. const uint8_t c[128],
  133. const ocrypto_rsa1024_crt_key *k);
  134. /**
  135. * 1024-bit RSA OAEP SHA256 encryption.
  136. *
  137. * The message @p m is encrypted to a ciphertext returned in @p c.
  138. *
  139. * @param[out] c The generated 128-byte ciphertext.
  140. * @param m The message to be encrypted.
  141. * @param mlen Length of @p m. 0 <= mlen <= 62.
  142. * @param label The label associated with the message.
  143. * @param llen Length of @p label. May be 0.
  144. * @param seed 32-byte random seed.
  145. * @param pk A valid 1024-bit RSA public key.
  146. *
  147. * @retval -1 If the message is too long (mlen > 62).
  148. * @retval 0 Otherwise.
  149. *
  150. * @remark The key @p pk should be initialized with @c ocrypto_rsa1024_init_pub_key.
  151. * @remark @p c and @p m can point to the same address.
  152. */
  153. int ocrypto_rsa1024_oaep_sha256_encrypt(
  154. uint8_t c[128],
  155. const uint8_t *m, size_t mlen,
  156. const uint8_t *label, size_t llen,
  157. const uint8_t seed[32],
  158. const ocrypto_rsa1024_pub_key *pk);
  159. /**
  160. * 1024-bit RSA OAEP SHA256 decryption.
  161. *
  162. * The ciphertext @p c is decrypted to the message returned in @p m.
  163. *
  164. * @param[out] m The decrypted message. The buffer must be long enough to hold the message.
  165. * @param mlen Length of @p m.
  166. * @param c The 128-byte ciphertext to decrypt.
  167. * @param label The label associated with the message.
  168. * @param llen Length of @p label. May be 0.
  169. * @param k A valid 1024-bit RSA secret key.
  170. *
  171. * @retval -1 If decryption failed.
  172. * @retval -2 If the output buffer is too short (mlen < length of message).
  173. * @retval n If a message of length n was successfully decrypted.
  174. *
  175. * @remark The key @p k should be initialized with @c ocrypto_rsa1024_init_key.
  176. * @remark @p m and @p c can point to the same address.
  177. */
  178. int ocrypto_rsa1024_oaep_sha256_decrypt(
  179. uint8_t *m, size_t mlen,
  180. const uint8_t c[128],
  181. const uint8_t *label, size_t llen,
  182. const ocrypto_rsa1024_key *k);
  183. /**
  184. * 1024-bit RSA OAEP SHA256 decryption with CRT acceleration.
  185. *
  186. * The ciphertext @p c is decrypted to the message returned in @p m.
  187. *
  188. * @param[out] m The decrypted message. The buffer must be long enough to hold the message.
  189. * @param mlen Length of @p m.
  190. * @param c The 128-byte ciphertext to decrypt.
  191. * @param label The label associated with the message.
  192. * @param llen Length of @p label. May be 0.
  193. * @param k A valid 1024-bit RSA secret key with CRT coefficients.
  194. *
  195. * @retval -1 If decryption failed.
  196. * @retval -2 If the output buffer is too short (mlen < length of message).
  197. * @retval n If a message of length n was successfully decrypted.
  198. *
  199. * @remark The key @p k should be initialized with @c ocrypto_rsa1024_init_crt_key.
  200. * @remark @p m and @p c can point to the same address.
  201. */
  202. int ocrypto_rsa1024_oaep_sha256_crt_decrypt(
  203. uint8_t *m, size_t mlen,
  204. const uint8_t c[128],
  205. const uint8_t *label, size_t llen,
  206. const ocrypto_rsa1024_crt_key *k);
  207. /**
  208. * 1024-bit RSA PKCS1 V1.5 SHA-256 sign.
  209. *
  210. * The message @p m is signed and the signature returned in @p s.
  211. *
  212. * @param[out] s The generated 128-byte signature.
  213. * @param m The message to be signed.
  214. * @param mlen Length of @p m.
  215. * @param k A valid 1024-bit RSA secret key.
  216. *
  217. * @return 0
  218. *
  219. * @remark The key @p k should be initialized with @c ocrypto_rsa1024_init_key.
  220. * @remark @p s and @p m can point to the same address.
  221. */
  222. int ocrypto_rsa1024_pkcs1_v15_sha256_sign(
  223. uint8_t s[128],
  224. const uint8_t *m, size_t mlen,
  225. const ocrypto_rsa1024_key *k);
  226. /**
  227. * 1024-bit RSA PKCS1 V1.5 SHA-256 sign with CRT acceleration.
  228. *
  229. * The message @p m is signed and the signature returned in @p s.
  230. *
  231. * @param[out] s The generated 128-byte signature.
  232. * @param m The message to be signed.
  233. * @param mlen Length of @p m.
  234. * @param k A valid 1024-bit RSA secret key with CRT coefficients.
  235. *
  236. * @return 0
  237. *
  238. * @remark The key @p k should be initialized with @c ocrypto_rsa1024_init_crt_key.
  239. * @remark @p s and @p m can point to the same address.
  240. */
  241. int ocrypto_rsa1024_pkcs1_v15_sha256_crt_sign(
  242. uint8_t s[128],
  243. const uint8_t *m, size_t mlen,
  244. const ocrypto_rsa1024_crt_key *k);
  245. /**
  246. * 1024-bit RSA PKCS1 V1.5 SHA-256 signature verify.
  247. *
  248. * The signature @p s is verified for a valid signature of message @p m.
  249. *
  250. * @param s The 128-byte signature.
  251. * @param m The signed message.
  252. * @param mlen Length of @p m.
  253. * @param pk A valid 1024-bit RSA public key.
  254. *
  255. * @retval 0 If the signature is valid.
  256. * @retval -1 If verification failed.
  257. *
  258. * @remark The key @p pk should be initialized with @c ocrypto_rsa1024_init_pub_key.
  259. */
  260. int ocrypto_rsa1024_pkcs1_v15_sha256_verify(
  261. const uint8_t s[128],
  262. const uint8_t *m, size_t mlen,
  263. const ocrypto_rsa1024_pub_key *pk);
  264. /**
  265. * 1024-bit RSA PSS SHA-256 sign.
  266. *
  267. * The message @p m is signed and the signature returned in @p s.
  268. *
  269. * @param[out] s The generated 128-byte signature.
  270. * @param m The message to be signed.
  271. * @param mlen Length of @p m.
  272. * @param salt The salt to be used.
  273. * @param slen Length of @p salt.
  274. * @param k A valid 1024-bit RSA secret key.
  275. *
  276. * @retval -2 If the salt is too long.
  277. * @retval 0 Otherwise.
  278. *
  279. * @remark The key @p k should be initialized with @c ocrypto_rsa1024_init_key.
  280. * @remark @p s and @p m can point to the same address.
  281. */
  282. int ocrypto_rsa1024_pss_sha256_sign(
  283. uint8_t s[128],
  284. const uint8_t *m, size_t mlen,
  285. const uint8_t *salt, size_t slen,
  286. const ocrypto_rsa1024_key *k);
  287. /**
  288. * 1024-bit RSA PSS SHA-256 sign with CRT acceleration.
  289. *
  290. * The message @p m is signed and the signature returned in @p s.
  291. *
  292. * @param[out] s The generated 128-byte signature.
  293. * @param m The message to be signed.
  294. * @param mlen Length of @p m.
  295. * @param salt The salt to be used.
  296. * @param slen Length of @p salt.
  297. * @param k A valid 1024-bit RSA secret key with CRT coefficients.
  298. *
  299. * @retval -2 If the salt is too long.
  300. * @retval 0 Otherwise.
  301. *
  302. * @remark The key @p k should be initialized with @c ocrypto_rsa1024_init_crt_key.
  303. * @remark @p s and @p m can point to the same address.
  304. */
  305. int ocrypto_rsa1024_pss_sha256_crt_sign(
  306. uint8_t s[128],
  307. const uint8_t *m, size_t mlen,
  308. const uint8_t *salt, size_t slen,
  309. const ocrypto_rsa1024_crt_key *k);
  310. /**
  311. * 1024-bit RSA PSS SHA-256 signature verify.
  312. *
  313. * The signature @p s is verified for a valid signature of message @p m.
  314. *
  315. * @param s The 128-byte signature.
  316. * @param m The signed message.
  317. * @param mlen Length of @p m.
  318. * @param slen The length of the salt.
  319. * @param pk A valid 1024-bit RSA public key.
  320. *
  321. * @retval 0 If the signature is valid.
  322. * @retval -1 If verification failed.
  323. * @retval -2 If the salt is too long.
  324. *
  325. * @remark The key @p pk should be initialized with @c ocrypto_rsa1024_init_pub_key.
  326. */
  327. int ocrypto_rsa1024_pss_sha256_verify(
  328. const uint8_t s[128],
  329. const uint8_t *m, size_t mlen,
  330. size_t slen, // salt length
  331. const ocrypto_rsa1024_pub_key *pk);
  332. /**@}*/
  333. /**@name 2048-bit RSA Functions.
  334. *
  335. * This group of functions is used for 2048-bit RSA.
  336. */
  337. /**@{*/
  338. /**
  339. * 2048-bit RSA PKCS1 V1.5 encryption.
  340. *
  341. * The message @p m is encrypted to a ciphertext returned in @p c.
  342. *
  343. * @param[out] c The generated 256-byte ciphertext.
  344. * @param m The message to be encrypted.
  345. * @param mlen Length of @p m. 0 <= @p mlen <= 245.
  346. * @param seed The random seed to be used for the padding.
  347. * @param slen Length of @p seed. @p slen >= 253 - @p mlen.
  348. * @param pk A valid 2048-bit RSA public key.
  349. *
  350. * @retval -1 If the message is too long (mlen > 245).
  351. * @retval -2 If the seed is too short (slen < 253 - mlen).
  352. * @retval 0 Otherwise.
  353. *
  354. * @remark The key @p pk should be initialized with @c ocrypto_rsa2048_init_pub_key.
  355. * @remark The @p seed should consist of non-zero random bytes.
  356. * @remark @p c and @p m can point to the same address.
  357. */
  358. int ocrypto_rsa2048_pkcs1_v15_encrypt(
  359. uint8_t c[256],
  360. const uint8_t *m, size_t mlen,
  361. const uint8_t *seed, size_t slen,
  362. const ocrypto_rsa2048_pub_key *pk);
  363. /**
  364. * 2048-bit RSA PKCS1 V1.5 decryption.
  365. *
  366. * The ciphertext @p c is decrypted to the message returned in @p m.
  367. *
  368. * @param[out] m The decrypted message. The buffer must be long enough to hold the message.
  369. * @param mlen Length of @p m.
  370. * @param c The 256-byte ciphertext to decrypt.
  371. * @param k A valid 2048-bit RSA secret key.
  372. * @retval -1 If decryption failed.
  373. * @retval -2 If the output buffer is too short (mlen < length of message).
  374. * @retval n If a message of length n was successfully decrypted.
  375. *
  376. * @remark The key @p k should be initialized with @c ocrypto_rsa2048_init_key.
  377. * @remark @p m and @p c can point to the same address.
  378. */
  379. int ocrypto_rsa2048_pkcs1_v15_decrypt(
  380. uint8_t *m, size_t mlen,
  381. const uint8_t c[256],
  382. const ocrypto_rsa2048_key *k);
  383. /**
  384. * 2048-bit RSA PKCS1 V1.5 decryption with CRT acceleration.
  385. *
  386. * The ciphertext @p c is decrypted to the message returned in @p m.
  387. *
  388. * @param[out] m The decrypted message. The buffer must be long enough to hold the message.
  389. * @param mlen Length of @p m.
  390. * @param c The 256-byte ciphertext to decrypt.
  391. * @param k A valid 2048-bit RSA secret key with CRT coefficients.
  392. * @retval -1 If decryption failed.
  393. * @retval -2 If the output buffer is too short (mlen < length of message).
  394. * @retval n If a message of length n was successfully decrypted.
  395. *
  396. * @remark The key @p k should be initialized with @c ocrypto_rsa2048_init_crt_key.
  397. * @remark @p m and @p c can point to the same address.
  398. */
  399. int ocrypto_rsa2048_pkcs1_v15_crt_decrypt(
  400. uint8_t *m, size_t mlen,
  401. const uint8_t c[256],
  402. const ocrypto_rsa2048_crt_key *k);
  403. /**
  404. * 2048-bit RSA OAEP SHA256 encryption.
  405. *
  406. * The message @p m is encrypted to a ciphertext returned in @p c.
  407. *
  408. * @param[out] c The generated 256-byte ciphertext.
  409. * @param m The message to be encrypted.
  410. * @param mlen Length of @p m. 0 <= mlen <= 190.
  411. * @param label The label associated with the message.
  412. * @param llen Length of @p label. May be 0.
  413. * @param seed 32-byte random seed.
  414. * @param pk A valid 2048-bit RSA public key.
  415. *
  416. * @retval -1 If the message is too long (mlen > 190).
  417. * @retval 0 Otherwise.
  418. *
  419. * @remark The key @p pk should be initialized with @c ocrypto_rsa2048_init_pub_key.
  420. * @remark @p c and @p m can point to the same address.
  421. */
  422. int ocrypto_rsa2048_oaep_sha256_encrypt(
  423. uint8_t c[256],
  424. const uint8_t *m, size_t mlen,
  425. const uint8_t *label, size_t llen,
  426. const uint8_t seed[32],
  427. const ocrypto_rsa2048_pub_key *pk);
  428. /**
  429. * 2048-bit RSA OAEP SHA256 decryption.
  430. *
  431. * The ciphertext @p c is decrypted to the message returned in @p m.
  432. *
  433. * @param[out] m The decrypted message. The buffer must be long enough to hold the message.
  434. * @param mlen Length of @p m.
  435. * @param c The 256-byte ciphertext to decrypt.
  436. * @param label The label associated with the message.
  437. * @param llen Length of @p label. May be 0.
  438. * @param k A valid 2048-bit RSA secret key.
  439. *
  440. * @retval -1 If decryption failed.
  441. * @retval -2 If the output buffer is too short (mlen < length of message).
  442. * @retval n If a message of length n was successfully decrypted.
  443. *
  444. * @remark The key @p k should be initialized with @c ocrypto_rsa2048_init_key.
  445. * @remark @p m and @p c can point to the same address.
  446. */
  447. int ocrypto_rsa2048_oaep_sha256_decrypt(
  448. uint8_t *m, size_t mlen,
  449. const uint8_t c[256],
  450. const uint8_t *label, size_t llen,
  451. const ocrypto_rsa2048_key *k);
  452. /**
  453. * 2048-bit RSA OAEP SHA256 decryption with CRT acceleration.
  454. *
  455. * The ciphertext @p c is decrypted to the message returned in @p m.
  456. *
  457. * @param[out] m The decrypted message. The buffer must be long enough to hold the message.
  458. * @param mlen Length of @p m.
  459. * @param c The 256-byte ciphertext to decrypt.
  460. * @param label The label associated with the message.
  461. * @param llen Length of @p label. May be 0.
  462. * @param k A valid 2048-bit RSA secret key with CRT coefficients.
  463. *
  464. * @retval -1 If decryption failed.
  465. * @retval -2 If the output buffer is too short (mlen < length of message).
  466. * @retval n If a message of length n was successfully decrypted.
  467. *
  468. * @remark The key @p k should be initialized with @c ocrypto_rsa2048_init_crt_key.
  469. * @remark @p m and @p c can point to the same address.
  470. */
  471. int ocrypto_rsa2048_oaep_sha256_crt_decrypt(
  472. uint8_t *m, size_t mlen,
  473. const uint8_t c[256],
  474. const uint8_t *label, size_t llen,
  475. const ocrypto_rsa2048_crt_key *k);
  476. /**
  477. * 2048-bit RSA PKCS1 V1.5 SHA-256 sign.
  478. *
  479. * The message @p m is signed and the signature returned in @p s.
  480. *
  481. * @param[out] s The generated 256-byte signature.
  482. * @param m The message to be signed.
  483. * @param mlen Length of @p m.
  484. * @param k A valid 2048-bit RSA secret key.
  485. * @returns 0
  486. *
  487. * @remark The key @p k should be initialized with @c ocrypto_rsa2048_init_key.
  488. * @remark @p s and @p m can point to the same address.
  489. */
  490. int ocrypto_rsa2048_pkcs1_v15_sha256_sign(
  491. uint8_t s[256],
  492. const uint8_t *m, size_t mlen,
  493. const ocrypto_rsa2048_key *k);
  494. /**
  495. * 2048-bit RSA PKCS1 V1.5 SHA-256 sign with CRT acceleration.
  496. *
  497. * The message @p m is signed and the signature returned in @p s.
  498. *
  499. * @param[out] s The generated 256-byte signature.
  500. * @param m The message to be signed.
  501. * @param mlen Length of @p m.
  502. * @param k A valid 2048-bit RSA secret key with CRT coefficients.
  503. * @returns 0
  504. *
  505. * @remark The key @p k should be initialized with @c ocrypto_rsa2048_init_crt_key.
  506. * @remark @p s and @p m can point to the same address.
  507. */
  508. int ocrypto_rsa2048_pkcs1_v15_sha256_crt_sign(
  509. uint8_t s[256],
  510. const uint8_t *m, size_t mlen,
  511. const ocrypto_rsa2048_crt_key *k);
  512. /**
  513. * 2048-bit RSA PKCS1 V1.5 SHA-256 signature verify.
  514. *
  515. * The signature @p s is verified for a correct signature of message @p m.
  516. *
  517. * @param s The 256-byte signature.
  518. * @param m The signed message.
  519. * @param mlen Length of @p m.
  520. * @param pk A valid 2048-bit RSA public key.
  521. *
  522. * @retval 0 If the signature is valid.
  523. * @retval -1 If verification failed.
  524. *
  525. * @remark The key @p pk should be initialized with @c ocrypto_rsa2048_init_pub_key.
  526. */
  527. int ocrypto_rsa2048_pkcs1_v15_sha256_verify(
  528. const uint8_t s[256],
  529. const uint8_t *m, size_t mlen,
  530. const ocrypto_rsa2048_pub_key *pk);
  531. /**
  532. * 2048-bit RSA PSS SHA-256 sign.
  533. *
  534. * The message @p m is signed and the signature returned in @p s.
  535. *
  536. * @param[out] s The generated 256-byte signature.
  537. * @param m The message to be signed.
  538. * @param mlen Length of @p m.
  539. * @param salt The salt to be used.
  540. * @param slen Length of @p salt.
  541. * @param k A valid 2048-bit RSA secret key.
  542. *
  543. * @retval -2 If the salt is too long.
  544. * @retval 0 Otherwise.
  545. *
  546. * @remark The key @p k should be initialized with @c ocrypto_rsa2048_init_key.
  547. * @remark @p s and @p m can point to the same address.
  548. */
  549. int ocrypto_rsa2048_pss_sha256_sign(
  550. uint8_t s[256],
  551. const uint8_t *m, size_t mlen,
  552. const uint8_t *salt, size_t slen,
  553. const ocrypto_rsa2048_key *k);
  554. /**
  555. * 2048-bit RSA PSS SHA-256 sign with CRT acceleration.
  556. *
  557. * The message @p m is signed and the signature returned in @p s.
  558. *
  559. * @param[out] s The generated 256-byte signature.
  560. * @param m The message to be signed.
  561. * @param mlen Length of @p m.
  562. * @param salt The salt to be used.
  563. * @param slen Length of @p salt.
  564. * @param k A valid 2048-bit RSA secret key with CRT coefficients.
  565. *
  566. * @retval -2 If the salt is too long.
  567. * @retval 0 Otherwise.
  568. *
  569. * @remark The key @p k should be initialized with @c ocrypto_rsa2048_init_crt_key.
  570. * @remark @p s and @p m can point to the same address.
  571. */
  572. int ocrypto_rsa2048_pss_sha256_crt_sign(
  573. uint8_t s[256],
  574. const uint8_t *m, size_t mlen,
  575. const uint8_t *salt, size_t slen,
  576. const ocrypto_rsa2048_crt_key *k);
  577. /**
  578. * 2048-bit RSA PSS SHA-256 signature verify.
  579. *
  580. * The signature @p s is verified for a valid signature of message @p m.
  581. *
  582. * @param s The 256-byte signature.
  583. * @param m The signed message.
  584. * @param mlen Length of @p m.
  585. * @param slen The length of the salt.
  586. * @param pk A valid 2048-bit RSA public key.
  587. *
  588. * @retval 0 If the signature is valid.
  589. * @retval -1 If verification failed.
  590. * @retval -2 If the salt is too long.
  591. *
  592. * @remark The key @p pk should be initialized with @c ocrypto_rsa2048_init_pub_key.
  593. */
  594. int ocrypto_rsa2048_pss_sha256_verify(
  595. const uint8_t s[256],
  596. const uint8_t *m, size_t mlen,
  597. size_t slen, // salt length
  598. const ocrypto_rsa2048_pub_key *pk);
  599. /**@}*/
  600. #ifdef __cplusplus
  601. }
  602. #endif
  603. #endif /* #ifndef OCRYPTO_RSA_H */
  604. /** @} */