PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/include/polarssl/rsa.h

https://github.com/leg0/polarssl
C Header | 382 lines | 136 code | 35 blank | 211 comment | 0 complexity | dc3ab0fccb14aca2551f40e05eb60943 MD5 | raw file
Possible License(s): GPL-2.0
  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 "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_SHA1_ALT \
  104. ASN1_STR_CONSTRUCTED_SEQUENCE "\x1F" \
  105. ASN1_STR_CONSTRUCTED_SEQUENCE "\x07" \
  106. ASN1_STR_OID "\x05" \
  107. OID_HASH_ALG_SHA1 \
  108. ASN1_STR_OCTET_STRING "\x14"
  109. #define ASN1_HASH_SHA2X \
  110. ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
  111. ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
  112. ASN1_STR_OID "\x09" \
  113. OID_HASH_ALG_SHA2X \
  114. ASN1_STR_NULL "\x00" \
  115. ASN1_STR_OCTET_STRING "\x00"
  116. /**
  117. * \brief RSA context structure
  118. */
  119. typedef struct
  120. {
  121. int ver; /*!< always 0 */
  122. size_t len; /*!< size(N) in chars */
  123. mpi N; /*!< public modulus */
  124. mpi E; /*!< public exponent */
  125. mpi D; /*!< private exponent */
  126. mpi P; /*!< 1st prime factor */
  127. mpi Q; /*!< 2nd prime factor */
  128. mpi DP; /*!< D % (P - 1) */
  129. mpi DQ; /*!< D % (Q - 1) */
  130. mpi QP; /*!< 1 / (Q % P) */
  131. mpi RN; /*!< cached R^2 mod N */
  132. mpi RP; /*!< cached R^2 mod P */
  133. mpi RQ; /*!< cached R^2 mod Q */
  134. int padding; /*!< RSA_PKCS_V15 for 1.5 padding and
  135. RSA_PKCS_v21 for OAEP/PSS */
  136. int hash_id; /*!< Hash identifier of md_type_t as
  137. specified in the md.h header file
  138. for the EME-OAEP and EMSA-PSS
  139. encoding */
  140. }
  141. rsa_context;
  142. #ifdef __cplusplus
  143. extern "C" {
  144. #endif
  145. /**
  146. * \brief Initialize an RSA context
  147. *
  148. * Note: Set padding to RSA_PKCS_V21 for the RSAES-OAEP
  149. * encryption scheme and the RSASSA-PSS signature scheme.
  150. *
  151. * \param ctx RSA context to be initialized
  152. * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
  153. * \param hash_id RSA_PKCS_V21 hash identifier
  154. *
  155. * \note The hash_id parameter is actually ignored
  156. * when using RSA_PKCS_V15 padding.
  157. */
  158. void rsa_init( rsa_context *ctx,
  159. int padding,
  160. int hash_id);
  161. /**
  162. * \brief Generate an RSA keypair
  163. *
  164. * \param ctx RSA context that will hold the key
  165. * \param f_rng RNG function
  166. * \param p_rng RNG parameter
  167. * \param nbits size of the public key in bits
  168. * \param exponent public exponent (e.g., 65537)
  169. *
  170. * \note rsa_init() must be called beforehand to setup
  171. * the RSA context.
  172. *
  173. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  174. */
  175. int rsa_gen_key( rsa_context *ctx,
  176. int (*f_rng)(void *, unsigned char *, size_t),
  177. void *p_rng,
  178. unsigned int nbits, int exponent );
  179. /**
  180. * \brief Check a public RSA key
  181. *
  182. * \param ctx RSA context to be checked
  183. *
  184. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  185. */
  186. int rsa_check_pubkey( const rsa_context *ctx );
  187. /**
  188. * \brief Check a private RSA key
  189. *
  190. * \param ctx RSA context to be checked
  191. *
  192. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  193. */
  194. int rsa_check_privkey( const rsa_context *ctx );
  195. /**
  196. * \brief Do an RSA public key operation
  197. *
  198. * \param ctx RSA context
  199. * \param input input buffer
  200. * \param output output buffer
  201. *
  202. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  203. *
  204. * \note This function does NOT take care of message
  205. * padding. Also, be sure to set input[0] = 0 or assure that
  206. * input is smaller than N.
  207. *
  208. * \note The input and output buffers must be large
  209. * enough (eg. 128 bytes if RSA-1024 is used).
  210. */
  211. int rsa_public( rsa_context *ctx,
  212. const unsigned char *input,
  213. unsigned char *output );
  214. /**
  215. * \brief Do an RSA private key operation
  216. *
  217. * \param ctx RSA context
  218. * \param input input buffer
  219. * \param output output buffer
  220. *
  221. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  222. *
  223. * \note The input and output buffers must be large
  224. * enough (eg. 128 bytes if RSA-1024 is used).
  225. */
  226. int rsa_private( rsa_context *ctx,
  227. const unsigned char *input,
  228. unsigned char *output );
  229. /**
  230. * \brief Add the message padding, then do an RSA operation
  231. *
  232. * \param ctx RSA context
  233. * \param f_rng RNG function (Needed for padding and PKCS#1 v2.1 encoding)
  234. * \param p_rng RNG parameter
  235. * \param mode RSA_PUBLIC or RSA_PRIVATE
  236. * \param ilen contains the plaintext length
  237. * \param input buffer holding the data to be encrypted
  238. * \param output buffer that will hold the ciphertext
  239. *
  240. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  241. *
  242. * \note The output buffer must be as large as the size
  243. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  244. */
  245. int rsa_pkcs1_encrypt( rsa_context *ctx,
  246. int (*f_rng)(void *, unsigned char *, size_t),
  247. void *p_rng,
  248. int mode, size_t ilen,
  249. const unsigned char *input,
  250. unsigned char *output );
  251. /**
  252. * \brief Do an RSA operation, then remove the message padding
  253. *
  254. * \param ctx RSA context
  255. * \param mode RSA_PUBLIC or RSA_PRIVATE
  256. * \param olen will contain the plaintext length
  257. * \param input buffer holding the encrypted data
  258. * \param output buffer that will hold the plaintext
  259. * \param output_max_len maximum length of the output buffer
  260. *
  261. * \return 0 if successful, or an POLARSSL_ERR_RSA_XXX error code
  262. *
  263. * \note The output buffer must be as large as the size
  264. * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise
  265. * an error is thrown.
  266. */
  267. int rsa_pkcs1_decrypt( rsa_context *ctx,
  268. int mode, size_t *olen,
  269. const unsigned char *input,
  270. unsigned char *output,
  271. size_t output_max_len );
  272. /**
  273. * \brief Do a private RSA to sign a message digest
  274. *
  275. * \param ctx RSA context
  276. * \param f_rng RNG function (Needed for PKCS#1 v2.1 encoding)
  277. * \param p_rng RNG parameter
  278. * \param mode RSA_PUBLIC or RSA_PRIVATE
  279. * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
  280. * \param hashlen message digest length (for SIG_RSA_RAW only)
  281. * \param hash buffer holding the message digest
  282. * \param sig buffer that will hold the ciphertext
  283. *
  284. * \return 0 if the signing operation was successful,
  285. * or an POLARSSL_ERR_RSA_XXX error code
  286. *
  287. * \note The "sig" buffer must be as large as the size
  288. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  289. *
  290. * \note In case of PKCS#1 v2.1 encoding keep in mind that
  291. * the hash_id in the RSA context is the one used for the
  292. * encoding. hash_id in the function call is the type of hash
  293. * that is encoded. According to RFC 3447 it is advised to
  294. * keep both hashes the same.
  295. */
  296. int rsa_pkcs1_sign( rsa_context *ctx,
  297. int (*f_rng)(void *, unsigned char *, size_t),
  298. void *p_rng,
  299. int mode,
  300. int hash_id,
  301. unsigned int hashlen,
  302. const unsigned char *hash,
  303. unsigned char *sig );
  304. /**
  305. * \brief Do a public RSA and check the message digest
  306. *
  307. * \param ctx points to an RSA public key
  308. * \param mode RSA_PUBLIC or RSA_PRIVATE
  309. * \param hash_id SIG_RSA_RAW, SIG_RSA_MD{2,4,5} or SIG_RSA_SHA{1,224,256,384,512}
  310. * \param hashlen message digest length (for SIG_RSA_RAW only)
  311. * \param hash buffer holding the message digest
  312. * \param sig buffer holding the ciphertext
  313. *
  314. * \return 0 if the verify operation was successful,
  315. * or an POLARSSL_ERR_RSA_XXX error code
  316. *
  317. * \note The "sig" buffer must be as large as the size
  318. * of ctx->N (eg. 128 bytes if RSA-1024 is used).
  319. *
  320. * \note In case of PKCS#1 v2.1 encoding keep in mind that
  321. * the hash_id in the RSA context is the one used for the
  322. * verification. hash_id in the function call is the type of hash
  323. * that is verified. According to RFC 3447 it is advised to
  324. * keep both hashes the same.
  325. */
  326. int rsa_pkcs1_verify( rsa_context *ctx,
  327. int mode,
  328. int hash_id,
  329. unsigned int hashlen,
  330. const unsigned char *hash,
  331. unsigned char *sig );
  332. /**
  333. * \brief Free the components of an RSA key
  334. *
  335. * \param ctx RSA Context to free
  336. */
  337. void rsa_free( rsa_context *ctx );
  338. /**
  339. * \brief Checkup routine
  340. *
  341. * \return 0 if successful, or 1 if the test failed
  342. */
  343. int rsa_self_test( int verbose );
  344. #ifdef __cplusplus
  345. }
  346. #endif
  347. #endif /* rsa.h */