PolarSSL v1.2.8
rsa.h
Go to the documentation of this file.
1 
27 #ifndef POLARSSL_RSA_H
28 #define POLARSSL_RSA_H
29 
30 #include "bignum.h"
31 
32 /*
33  * RSA Error codes
34  */
35 #define POLARSSL_ERR_RSA_BAD_INPUT_DATA -0x4080
36 #define POLARSSL_ERR_RSA_INVALID_PADDING -0x4100
37 #define POLARSSL_ERR_RSA_KEY_GEN_FAILED -0x4180
38 #define POLARSSL_ERR_RSA_KEY_CHECK_FAILED -0x4200
39 #define POLARSSL_ERR_RSA_PUBLIC_FAILED -0x4280
40 #define POLARSSL_ERR_RSA_PRIVATE_FAILED -0x4300
41 #define POLARSSL_ERR_RSA_VERIFY_FAILED -0x4380
42 #define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE -0x4400
43 #define POLARSSL_ERR_RSA_RNG_FAILED -0x4480
45 /*
46  * PKCS#1 constants
47  */
48 #define SIG_RSA_RAW 0
49 #define SIG_RSA_MD2 2
50 #define SIG_RSA_MD4 3
51 #define SIG_RSA_MD5 4
52 #define SIG_RSA_SHA1 5
53 #define SIG_RSA_SHA224 14
54 #define SIG_RSA_SHA256 11
55 #define SIG_RSA_SHA384 12
56 #define SIG_RSA_SHA512 13
57 
58 #define RSA_PUBLIC 0
59 #define RSA_PRIVATE 1
60 
61 #define RSA_PKCS_V15 0
62 #define RSA_PKCS_V21 1
63 
64 #define RSA_SIGN 1
65 #define RSA_CRYPT 2
66 
67 #define ASN1_STR_CONSTRUCTED_SEQUENCE "\x30"
68 #define ASN1_STR_NULL "\x05"
69 #define ASN1_STR_OID "\x06"
70 #define ASN1_STR_OCTET_STRING "\x04"
71 
72 #define OID_DIGEST_ALG_MDX "\x2A\x86\x48\x86\xF7\x0D\x02\x00"
73 #define OID_HASH_ALG_SHA1 "\x2b\x0e\x03\x02\x1a"
74 #define OID_HASH_ALG_SHA2X "\x60\x86\x48\x01\x65\x03\x04\x02\x00"
75 
76 #define OID_ISO_MEMBER_BODIES "\x2a"
77 #define OID_ISO_IDENTIFIED_ORG "\x2b"
78 
79 /*
80  * ISO Member bodies OID parts
81  */
82 #define OID_COUNTRY_US "\x86\x48"
83 #define OID_RSA_DATA_SECURITY "\x86\xf7\x0d"
84 
85 /*
86  * ISO Identified organization OID parts
87  */
88 #define OID_OIW_SECSIG_SHA1 "\x0e\x03\x02\x1a"
89 
90 /*
91  * DigestInfo ::= SEQUENCE {
92  * digestAlgorithm DigestAlgorithmIdentifier,
93  * digest Digest }
94  *
95  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
96  *
97  * Digest ::= OCTET STRING
98  */
99 #define ASN1_HASH_MDX \
100 ( \
101  ASN1_STR_CONSTRUCTED_SEQUENCE "\x20" \
102  ASN1_STR_CONSTRUCTED_SEQUENCE "\x0C" \
103  ASN1_STR_OID "\x08" \
104  OID_DIGEST_ALG_MDX \
105  ASN1_STR_NULL "\x00" \
106  ASN1_STR_OCTET_STRING "\x10" \
107 )
108 
109 #define ASN1_HASH_SHA1 \
110  ASN1_STR_CONSTRUCTED_SEQUENCE "\x21" \
111  ASN1_STR_CONSTRUCTED_SEQUENCE "\x09" \
112  ASN1_STR_OID "\x05" \
113  OID_HASH_ALG_SHA1 \
114  ASN1_STR_NULL "\x00" \
115  ASN1_STR_OCTET_STRING "\x14"
116 
117 #define ASN1_HASH_SHA1_ALT \
118  ASN1_STR_CONSTRUCTED_SEQUENCE "\x1F" \
119  ASN1_STR_CONSTRUCTED_SEQUENCE "\x07" \
120  ASN1_STR_OID "\x05" \
121  OID_HASH_ALG_SHA1 \
122  ASN1_STR_OCTET_STRING "\x14"
123 
124 #define ASN1_HASH_SHA2X \
125  ASN1_STR_CONSTRUCTED_SEQUENCE "\x11" \
126  ASN1_STR_CONSTRUCTED_SEQUENCE "\x0d" \
127  ASN1_STR_OID "\x09" \
128  OID_HASH_ALG_SHA2X \
129  ASN1_STR_NULL "\x00" \
130  ASN1_STR_OCTET_STRING "\x00"
131 
135 typedef struct
136 {
137  int ver;
138  size_t len;
140  mpi N;
141  mpi E;
143  mpi D;
144  mpi P;
145  mpi Q;
146  mpi DP;
147  mpi DQ;
148  mpi QP;
150  mpi RN;
151  mpi RP;
152  mpi RQ;
154  int padding;
156  int hash_id;
160 }
162 
163 #ifdef __cplusplus
164 extern "C" {
165 #endif
166 
180 void rsa_init( rsa_context *ctx,
181  int padding,
182  int hash_id);
183 
198 int rsa_gen_key( rsa_context *ctx,
199  int (*f_rng)(void *, unsigned char *, size_t),
200  void *p_rng,
201  unsigned int nbits, int exponent );
202 
210 int rsa_check_pubkey( const rsa_context *ctx );
211 
219 int rsa_check_privkey( const rsa_context *ctx );
220 
237 int rsa_public( rsa_context *ctx,
238  const unsigned char *input,
239  unsigned char *output );
240 
253 int rsa_private( rsa_context *ctx,
254  const unsigned char *input,
255  unsigned char *output );
256 
276  int (*f_rng)(void *, unsigned char *, size_t),
277  void *p_rng,
278  int mode, size_t ilen,
279  const unsigned char *input,
280  unsigned char *output );
281 
299  int (*f_rng)(void *, unsigned char *, size_t),
300  void *p_rng,
301  int mode, size_t ilen,
302  const unsigned char *input,
303  unsigned char *output );
304 
324  int (*f_rng)(void *, unsigned char *, size_t),
325  void *p_rng,
326  int mode,
327  const unsigned char *label, size_t label_len,
328  size_t ilen,
329  const unsigned char *input,
330  unsigned char *output );
331 
351  int mode, size_t *olen,
352  const unsigned char *input,
353  unsigned char *output,
354  size_t output_max_len );
355 
373  int mode, size_t *olen,
374  const unsigned char *input,
375  unsigned char *output,
376  size_t output_max_len );
377 
397  int mode,
398  const unsigned char *label, size_t label_len,
399  size_t *olen,
400  const unsigned char *input,
401  unsigned char *output,
402  size_t output_max_len );
403 
430 int rsa_pkcs1_sign( rsa_context *ctx,
431  int (*f_rng)(void *, unsigned char *, size_t),
432  void *p_rng,
433  int mode,
434  int hash_id,
435  unsigned int hashlen,
436  const unsigned char *hash,
437  unsigned char *sig );
438 
456  int mode,
457  int hash_id,
458  unsigned int hashlen,
459  const unsigned char *hash,
460  unsigned char *sig );
461 
487  int (*f_rng)(void *, unsigned char *, size_t),
488  void *p_rng,
489  int mode,
490  int hash_id,
491  unsigned int hashlen,
492  const unsigned char *hash,
493  unsigned char *sig );
494 
519 int rsa_pkcs1_verify( rsa_context *ctx,
520  int mode,
521  int hash_id,
522  unsigned int hashlen,
523  const unsigned char *hash,
524  unsigned char *sig );
525 
543  int mode,
544  int hash_id,
545  unsigned int hashlen,
546  const unsigned char *hash,
547  unsigned char *sig );
548 
573  int mode,
574  int hash_id,
575  unsigned int hashlen,
576  const unsigned char *hash,
577  unsigned char *sig );
578 
584 void rsa_free( rsa_context *ctx );
585 
591 int rsa_self_test( int verbose );
592 
593 #ifdef __cplusplus
594 }
595 #endif
596 
597 #endif /* rsa.h */
int rsa_rsassa_pkcs1_v15_verify(rsa_context *ctx, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
int rsa_private(rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA private key operation.
int rsa_self_test(int verbose)
Checkup routine.
int rsa_rsaes_oaep_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t ilen, const unsigned char *input, unsigned char *output)
Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
int rsa_rsassa_pkcs1_v15_sign(rsa_context *ctx, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
int rsa_check_privkey(const rsa_context *ctx)
Check a private RSA key.
int padding
Definition: rsa.h:154
int rsa_rsaes_pkcs1_v15_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
int rsa_pkcs1_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Generic wrapper to perform a PKCS#1 signature using the mode from the context.
int rsa_rsaes_oaep_decrypt(rsa_context *ctx, int mode, const unsigned char *label, size_t label_len, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
mpi DQ
Definition: rsa.h:147
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
mpi RP
Definition: rsa.h:151
MPI structure.
Definition: bignum.h:164
int rsa_pkcs1_verify(rsa_context *ctx, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Generic wrapper to perform a PKCS#1 verification using the mode from the context. ...
Multi-precision integer library.
size_t len
Definition: rsa.h:138
mpi P
Definition: rsa.h:144
mpi Q
Definition: rsa.h:145
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
RSA context structure.
Definition: rsa.h:135
mpi D
Definition: rsa.h:143
int rsa_pkcs1_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Generic wrapper to perform a PKCS#1 encryption using the mode from the context.
mpi QP
Definition: rsa.h:148
mpi N
Definition: rsa.h:140
int rsa_pkcs1_decrypt(rsa_context *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Generic wrapper to perform a PKCS#1 decryption using the mode from the context.
mpi RQ
Definition: rsa.h:152
mpi E
Definition: rsa.h:141
mpi DP
Definition: rsa.h:146
int rsa_rsassa_pss_verify(rsa_context *ctx, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
int hash_id
Definition: rsa.h:156
int rsa_gen_key(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, unsigned int nbits, int exponent)
Generate an RSA keypair.
void rsa_init(rsa_context *ctx, int padding, int hash_id)
Initialize an RSA context.
int rsa_rsaes_pkcs1_v15_decrypt(rsa_context *ctx, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
int rsa_rsassa_pss_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, int hash_id, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
mpi RN
Definition: rsa.h:150
int ver
Definition: rsa.h:137
int rsa_public(rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA public key operation.