7 #include "xyssl/bignum.h"
9 #define XYSSL_ERR_RSA_BAD_INPUT_DATA -0x0400
10 #define XYSSL_ERR_RSA_INVALID_PADDING -0x0410
11 #define XYSSL_ERR_RSA_KEY_GEN_FAILED -0x0420
12 #define XYSSL_ERR_RSA_KEY_CHECK_FAILED -0x0430
13 #define XYSSL_ERR_RSA_PUBLIC_FAILED -0x0440
14 #define XYSSL_ERR_RSA_PRIVATE_FAILED -0x0450
15 #define XYSSL_ERR_RSA_VERIFY_FAILED -0x0460
30 #define RSA_PKCS_V15 0
31 #define RSA_PKCS_V21 1
37 * DigestInfo ::= SEQUENCE {
38 * digestAlgorithm DigestAlgorithmIdentifier,
41 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
43 * Digest ::= OCTET STRING
45 #define ASN1_HASH_MDX \
46 "\x30\x20\x30\x0C\x06\x08\x2A\x86\x48" \
47 "\x86\xF7\x0D\x02\x00\x05\x00\x04\x10"
49 #define ASN1_HASH_SHA1 \
50 "\x30\x21\x30\x09\x06\x05\x2B\x0E\x03" \
51 "\x02\x1A\x05\x00\x04\x14"
54 * \brief RSA context structure
58 int ver
; /*!< always 0 */
59 int len
; /*!< size(N) in chars */
61 mpi N
; /*!< public modulus */
62 mpi E
; /*!< public exponent */
64 mpi D
; /*!< private exponent */
65 mpi P
; /*!< 1st prime factor */
66 mpi Q
; /*!< 2nd prime factor */
67 mpi DP
; /*!< D % (P - 1) */
68 mpi DQ
; /*!< D % (Q - 1) */
69 mpi QP
; /*!< 1 / (Q % P) */
71 mpi RN
; /*!< cached R^2 mod N */
72 mpi RP
; /*!< cached R^2 mod P */
73 mpi RQ
; /*!< cached R^2 mod Q */
75 int padding
; /*!< 1.5 or OAEP/PSS */
76 int hash_id
; /*!< hash identifier */
77 int (*f_rng
)(void *); /*!< RNG function */
78 void *p_rng
; /*!< RNG parameter */
87 * \brief Initialize an RSA context
89 * \param ctx RSA context to be initialized
90 * \param padding RSA_PKCS_V15 or RSA_PKCS_V21
91 * \param hash_id RSA_PKCS_V21 hash identifier
92 * \param f_rng RNG function
93 * \param p_rng RNG parameter
95 * \note The hash_id parameter is actually ignored
96 * when using RSA_PKCS_V15 padding.
98 * \note Currently (xyssl-0.8), RSA_PKCS_V21 padding
101 void rsa_init( rsa_context
*ctx
,
104 int (*f_rng
)(void *),
108 * \brief Generate an RSA keypair
110 * \param ctx RSA context that will hold the key
111 * \param nbits size of the public key in bits
112 * \param exponent public exponent (e.g., 65537)
114 * \note rsa_init() must be called beforehand to setup
115 * the RSA context (especially f_rng and p_rng).
117 * \return 0 if successful, or an XYSSL_ERR_RSA_XXX error code
119 int rsa_gen_key( rsa_context
*ctx
, int nbits
, int exponent
);
122 * \brief Check a public RSA key
124 * \param ctx RSA context to be checked
126 * \return 0 if successful, or an XYSSL_ERR_RSA_XXX error code
128 int rsa_check_pubkey( rsa_context
*ctx
);
131 * \brief Check a private RSA key
133 * \param ctx RSA context to be checked
135 * \return 0 if successful, or an XYSSL_ERR_RSA_XXX error code
137 int rsa_check_privkey( rsa_context
*ctx
);
140 * \brief Do an RSA public key operation
142 * \param ctx RSA context
143 * \param input input buffer
144 * \param output output buffer
146 * \return 0 if successful, or an XYSSL_ERR_RSA_XXX error code
148 * \note This function does NOT take care of message
149 * padding. Also, be sure to set input[0] = 0.
151 * \note The input and output buffers must be large
152 * enough (eg. 128 bytes if RSA-1024 is used).
154 int rsa_public( rsa_context
*ctx
,
155 unsigned char *input
,
156 unsigned char *output
);
159 * \brief Do an RSA private key operation
161 * \param ctx RSA context
162 * \param input input buffer
163 * \param output output buffer
165 * \return 0 if successful, or an XYSSL_ERR_RSA_XXX error code
167 * \note The input and output buffers must be large
168 * enough (eg. 128 bytes if RSA-1024 is used).
170 int rsa_private( rsa_context
*ctx
,
171 unsigned char *input
,
172 unsigned char *output
);
175 * \brief Add the message padding, then do an RSA operation
177 * \param ctx RSA context
178 * \param mode RSA_PUBLIC or RSA_PRIVATE
179 * \param ilen contains the the plaintext length
180 * \param input buffer holding the data to be encrypted
181 * \param output buffer that will hold the ciphertext
183 * \return 0 if successful, or an XYSSL_ERR_RSA_XXX error code
185 * \note The output buffer must be as large as the size
186 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
188 int rsa_pkcs1_encrypt( rsa_context
*ctx
,
190 unsigned char *input
,
191 unsigned char *output
);
194 * \brief Do an RSA operation, then remove the message padding
196 * \param ctx RSA context
197 * \param mode RSA_PUBLIC or RSA_PRIVATE
198 * \param input buffer holding the encrypted data
199 * \param output buffer that will hold the plaintext
200 * \param olen will contain the plaintext length
202 * \return 0 if successful, or an XYSSL_ERR_RSA_XXX error code
204 * \note The output buffer must be as large as the size
205 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
207 int rsa_pkcs1_decrypt( rsa_context
*ctx
,
209 unsigned char *input
,
210 unsigned char *output
);
213 * \brief Do a private RSA to sign a message digest
215 * \param ctx RSA context
216 * \param mode RSA_PUBLIC or RSA_PRIVATE
217 * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
218 * \param hashlen message digest length (for RSA_RAW only)
219 * \param hash buffer holding the message digest
220 * \param sig buffer that will hold the ciphertext
222 * \return 0 if the signing operation was successful,
223 * or an XYSSL_ERR_RSA_XXX error code
225 * \note The "sig" buffer must be as large as the size
226 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
228 int rsa_pkcs1_sign( rsa_context
*ctx
,
233 unsigned char *sig
);
236 * \brief Do a public RSA and check the message digest
238 * \param ctx points to an RSA public key
239 * \param mode RSA_PUBLIC or RSA_PRIVATE
240 * \param hash_id RSA_RAW, RSA_MD{2,4,5} or RSA_SHA{1,256}
241 * \param hashlen message digest length (for RSA_RAW only)
242 * \param hash buffer holding the message digest
243 * \param sig buffer holding the ciphertext
245 * \return 0 if the verify operation was successful,
246 * or an XYSSL_ERR_RSA_XXX error code
248 * \note The "sig" buffer must be as large as the size
249 * of ctx->N (eg. 128 bytes if RSA-1024 is used).
251 int rsa_pkcs1_verify( rsa_context
*ctx
,
256 unsigned char *sig
);
259 * \brief Free the components of an RSA key
261 void rsa_free( rsa_context
*ctx
);
264 * \brief Checkup routine
266 * \return 0 if successful, or 1 if the test failed
268 int rsa_self_test( int verbose
);