rsa.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. * \file rsa.h
  3. *
  4. * \brief The RSA public-key cryptosystem
  5. *
  6. * Copyright (C) 2006-2010, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. */
  27. #ifndef POLARSSL_RSA_H
  28. #define POLARSSL_RSA_H
  29. #include "polarssl/bignum.h"
  30. /*
  31. * RSA Error codes
  32. */
  33. #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080 /**< Bad input parameters to function. */
  34. #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100 /**< Input data contains invalid padding and is rejected. */
  35. #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180 /**< Something failed during generation of a key. */
  36. #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200 /**< Key failed to pass the libraries validity check. */
  37. #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280 /**< The public key operation failed. */
  38. #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300 /**< The private key operation failed. */
  39. #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380 /**< The PKCS#1 verification failed. */
  40. #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400 /**< The output buffer for decryption is not large enough. */
  41. #define POLARSSL_ERR_RSA_RNG_FAILED -0x4480 /**< The random generator failed to generate non-zeros. */
  42. /*
  43. * PKCS#1 constants
  44. */
  45. #define SIG_RSA_RAW 0
  46. #define SIG_RSA_MD2 2
  47. #define SIG_RSA_MD4 3
  48. #define SIG_RSA_MD5 4
  49. #define SIG_RSA_SHA1 5
  50. #define SIG_RSA_SHA224 14
  51. #define SIG_RSA_SHA256 11
  52. #define SIG_RSA_SHA384 12
  53. #define SIG_RSA_SHA512 13
  54. #define RSA_PUBLIC 0
  55. #define RSA_PRIVATE 1
  56. #define RSA_PKCS_V15 0
  57. #define RSA_PKCS_V21 1
  58. #define RSA_SIGN 1
  59. #define RSA_CRYPT 2
  60. #define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
  61. #define ASN1_STR_NULL "\x05"
  62. #define ASN1_STR_OID "\x06"
  63. #define ASN1_STR_OCTET_STRING "\x04"
  64. #define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
  65. #define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
  66. #define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
  67. #define OID_ISO_MEMBER_BODIES "\x2a"
  68. #define OID_ISO_IDENTIFIED_ORG "\x2b"
  69. /*
  70. * ISO Member bodies OID parts
  71. */
  72. #define OID_COUNTRY_US "\x86\x48"
  73. #define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
  74. /*
  75. * ISO Identified organization OID parts
  76. */
  77. #define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
  78. /*
  79. * DigestInfo ::= SEQUENCE {
  80. * digestAlgorithm DigestAlgorithmIdentifier,
  81. * digest Digest }
  82. *
  83. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  84. *
  85. * Digest ::= OCTET STRING
  86. */
  87. #define ASN1_HASH_MDX \
  88. ( \
  89. ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
  90. ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
  91. ASN1_STR_OID "\x08" \
  92. OID_DIGEST_ALG_MDX \
  93. ASN1_STR_NULL "\x00" \
  94. ASN1_STR_OCTET_STRING "\x10" \
  95. )
  96. #define ASN1_HASH_SHA1 \
  97. ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
  98. ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
  99. ASN1_STR_OID "\x05" \
  100. OID_HASH_ALG_SHA1 \
  101. ASN1_STR_NULL "\x00" \
  102. ASN1_STR_OCTET_STRING "\x14"
  103. #define ASN1_HASH_SHA2X \
  104. ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
  105. ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
  106. ASN1_STR_OID "\x09" \
  107. OID_HASH_ALG_SHA2X \
  108. ASN1_STR_NULL "\x00" \
  109. ASN1_STR_OCTET_STRING "\x00"
  110. /**
  111. * \brief RSA context structure
  112. */
  113. typedef struct
  114. {
  115. int ver; /*!< always 0 */
  116. size_t len; /*!< size(N) in chars */
  117. mpi N; /*!< public modulus */
  118. mpi E; /*!< public exponent */
  119. mpi D; /*!< private exponent */
  120. mpi P; /*!< 1st prime factor */
  121. mpi Q; /*!< 2nd prime factor */
  122. mpi DP; /*!< D % (P - 1) */
  123. mpi DQ; /*!< D % (Q - 1) */
  124. mpi QP; /*!< 1 / (Q % P) */
  125. mpi RN; /*!< cached R^2 mod N */
  126. mpi RP; /*!< cached R^2 mod P */
  127. mpi RQ; /*!< cached R^2 mod Q */
  128. int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
  129. RSA_PKCS_v21 for OAEP/PSS */
  130. int hash_id; /*!< Hash identifier of md_type_t as
  131. specified in the md.h header file
  132. for the EME-OAEP and EMSA-PSS
  133. encoding */
  134. }
  135. rsa_context;
  136. #ifdef __cplusplus
  137. extern "C" {
  138. #endif
  139. /**
  140. * \brief Initialize an RSA context
  141. *
  142. * \param ctx RSA context to be initialized
  143. * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
  144. * \param hash_id RSA_PKCS_V21 hash identifier
  145. *
  146. * \note The hash_id parameter is actually ignored
  147. * when using RSA_PKCS_V15 padding.
  148. */
  149. void rsa_init( rsa_context *ctx,
  150. int padding,
  151. int hash_id);
  152. /**
  153. * \brief Generate an RSA keypair
  154. *
  155. * \param ctx RSA context that will hold the key
  156. * \param f_rng RNG function
  157. * \param p_rng RNG parameter
  158. * \param nbits size of the public key in bits
  159. * \param exponent public exponent (e.g., 65537)
  160. *
  161. * \note rsa_init() must be called beforehand to setup
  162. * the RSA context.
  163. *
  164. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  165. */
  166. int rsa_gen_key( rsa_context *ctx,
  167. int (*f_rng)(void *),
  168. void *p_rng,
  169. unsigned int nbits, int exponent );
  170. /**
  171. * \brief Check a public RSA key
  172. *
  173. * \param ctx RSA context to be checked
  174. *
  175. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  176. */
  177. int rsa_check_pubkey( const rsa_context *ctx );
  178. /**
  179. * \brief Check a private RSA key
  180. *
  181. * \param ctx RSA context to be checked
  182. *
  183. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  184. */
  185. int rsa_check_privkey( const rsa_context *ctx );
  186. /**
  187. * \brief Do an RSA public key operation
  188. *
  189. * \param ctx RSA context
  190. * \param input input buffer
  191. * \param output output buffer
  192. *
  193. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  194. *
  195. * \note This function does NOT take care of message
  196. * padding. Also, be sure to set input[0] = 0 or assure that
  197. * input is smaller than N.
  198. *
  199. * \note The input and output buffers must be large
  200. * enough (eg. 128 bytes if RSA-1024 is used).
  201. */
  202. int rsa_public( rsa_context *ctx,
  203. const unsigned char *input,
  204. unsigned char *output );
  205. /**
  206. * \brief Do an RSA private key operation
  207. *
  208. * \param ctx RSA context
  209. * \param input input buffer
  210. * \param output output buffer
  211. *
  212. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  213. *
  214. * \note The input and output buffers must be large
  215. * enough (eg. 128 bytes if RSA-1024 is used).
  216. */
  217. int rsa_private( rsa_context *ctx,
  218. const unsigned char *input,
  219. unsigned char *output );
  220. /**
  221. * \brief Add the message padding, then do an RSA operation
  222. *
  223. * \param ctx RSA context
  224. * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding)
  225. * \param p_rng RNG parameter
  226. * \param mode RSA_PUBLIC or RSA_PRIVATE
  227. * \param ilen contains the plaintext length
  228. * \param input buffer holding the data to be encrypted
  229. * \param output buffer that will hold the ciphertext
  230. *
  231. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  232. *
  233. * \note The output buffer must be as large as the size
  234. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  235. */
  236. int rsa_pkcs1_encrypt( rsa_context *ctx,
  237. int (*f_rng)(void *),
  238. void *p_rng,
  239. int mode, size_t ilen,
  240. const unsigned char *input,
  241. unsigned char *output );
  242. /**
  243. * \brief Do an RSA operation, then remove the message padding
  244. *
  245. * \param ctx RSA context
  246. * \param mode RSA_PUBLIC or RSA_PRIVATE
  247. * \param olen will contain the plaintext length
  248. * \param input buffer holding the encrypted data
  249. * \param output buffer that will hold the plaintext
  250. * \param output_max_len maximum length of the output buffer
  251. *
  252. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  253. *
  254. * \note The output buffer must be as large as the size
  255. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  256. * an error is thrown.
  257. */
  258. int rsa_pkcs1_decrypt( rsa_context *ctx,
  259. int mode, size_t *olen,
  260. const unsigned char *input,
  261. unsigned char *output,
  262. size_t output_max_len );
  263. /**
  264. * \brief Do a private RSA to sign a message digest
  265. *
  266. * \param ctx RSA context
  267. * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding)
  268. * \param p_rng RNG parameter
  269. * \param mode RSA_PUBLIC or RSA_PRIVATE
  270. * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
  271. * \param hashlen message digest length (for SIG_RSA_RAW only)
  272. * \param hash buffer holding the message digest
  273. * \param sig buffer that will hold the ciphertext
  274. *
  275. * \return 0 if the signing operation was successful,
  276. * or an POLARSSL_ERR_RSA_XXX error code
  277. *
  278. * \note The "sig" buffer must be as large as the size
  279. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  280. *
  281. * \note In case of PKCS#1 v2.1 encoding keep in mind that
  282. * the hash_id in the RSA context is the one used for the
  283. * encoding. hash_id in the function call is the type of hash
  284. * that is encoded. According to RFC 3447 it is advised to
  285. * keep both hashes the same.
  286. */
  287. int rsa_pkcs1_sign( rsa_context *ctx,
  288. int (*f_rng)(void *),
  289. void *p_rng,
  290. int mode,
  291. int hash_id,
  292. unsigned int hashlen,
  293. const unsigned char *hash,
  294. unsigned char *sig );
  295. /**
  296. * \brief Do a public RSA and check the message digest
  297. *
  298. * \param ctx points to an RSA public key
  299. * \param mode RSA_PUBLIC or RSA_PRIVATE
  300. * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
  301. * \param hashlen message digest length (for SIG_RSA_RAW only)
  302. * \param hash buffer holding the message digest
  303. * \param sig buffer holding the ciphertext
  304. *
  305. * \return 0 if the verify operation was successful,
  306. * or an POLARSSL_ERR_RSA_XXX error code
  307. *
  308. * \note The "sig" buffer must be as large as the size
  309. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  310. *
  311. * \note In case of PKCS#1 v2.1 encoding keep in mind that
  312. * the hash_id in the RSA context is the one used for the
  313. * verification. hash_id in the function call is the type of hash
  314. * that is verified. According to RFC 3447 it is advised to
  315. * keep both hashes the same.
  316. */
  317. int rsa_pkcs1_verify( rsa_context *ctx,
  318. int mode,
  319. int hash_id,
  320. unsigned int hashlen,
  321. const unsigned char *hash,
  322. unsigned char *sig );
  323. /**
  324. * \brief Free the components of an RSA key
  325. *
  326. * \param ctx RSA Context to free
  327. */
  328. void rsa_free( rsa_context *ctx );
  329. /**
  330. * \brief Checkup routine
  331. *
  332. * \return 0 if successful, or 1 if the test failed
  333. */
  334. int rsa_self_test( int verbose );
  335. #ifdef __cplusplus
  336. }
  337. #endif
  338. #endif /* rsa.h */