#include "bignum.h"
Go to the source code of this file.
Data Structures | |
struct | rsa_context |
Defines | |
#define | ERR_RSA_BAD_INPUT_DATA 0x0300 |
#define | ERR_RSA_INVALID_PADDING 0x0310 |
#define | ERR_RSA_KEY_GEN_FAILED 0x0320 |
#define | ERR_RSA_KEY_CHK_FAILED 0x0330 |
#define | ERR_RSA_KEY_RD_FAILED 0x0340 |
#define | ERR_RSA_KEY_WR_FAILED 0x0350 |
#define | ERR_RSA_PUBLIC_FAILED 0x0360 |
#define | ERR_RSA_PRIVATE_FAILED 0x0370 |
#define | ERR_RSA_VERIFY_FAILED 0x0380 |
#define | RSA_RAW 0 |
#define | RSA_MD2 2 |
#define | RSA_MD4 3 |
#define | RSA_MD5 4 |
#define | RSA_SHA1 5 |
#define | RSA_SIGN 0x01 |
#define | RSA_CRYPT 0x02 |
#define | ASN1_HASH_MDX |
#define | ASN1_HASH_SHA1 |
#define | KEY_LEN 128 |
#define | RSA_N |
#define | RSA_E "10001" |
#define | RSA_D |
#define | RSA_P |
#define | RSA_Q |
#define | RSA_DP |
#define | RSA_DQ |
#define | RSA_QP |
Functions | |
int | rsa_gen_key (rsa_context *ctx, int nbits, int exponent, int(*rng_f)(void *), void *rng_d) |
Generate an RSA keypair. | |
int | rsa_read_public (rsa_context *ctx, FILE *f) |
Read the public key from a file. | |
int | rsa_read_private (rsa_context *ctx, FILE *f) |
Read the private key from a file. | |
int | rsa_write_public (rsa_context *ctx, FILE *f) |
Write the public key into a file. | |
int | rsa_write_private (rsa_context *ctx, FILE *f) |
Write the private key into a file. | |
int | rsa_public (rsa_context *ctx, unsigned char *input, int ilen, unsigned char *output, int olen) |
Perform an RSA public key operation. | |
int | rsa_private (rsa_context *ctx, unsigned char *input, int ilen, unsigned char *output, int olen) |
Perform an RSA private key operation. | |
int | rsa_check_pubkey (rsa_context *ctx) |
Return 0 if the public key is valid, or ERR_RSA_KEY_CHECK_FAILED. | |
int | rsa_check_privkey (rsa_context *ctx) |
Return 0 if the private key is valid, or ERR_RSA_KEY_CHECK_FAILED. | |
int | rsa_pkcs1_encrypt (rsa_context *ctx, unsigned char *input, int ilen, unsigned char *output, int olen) |
Add the PKCS#1 v1.5 padding and do a public RSA. | |
int | rsa_pkcs1_decrypt (rsa_context *ctx, unsigned char *input, int ilen, unsigned char *output, int *olen) |
Do a private RSA, removes the PKCS#1 v1.5 padding. | |
int | rsa_pkcs1_sign (rsa_context *ctx, int alg_id, unsigned char *hash, int hashlen, unsigned char *sig, int siglen) |
Perform a private RSA to sign a message digest. | |
int | rsa_pkcs1_verify (rsa_context *ctx, int alg_id, unsigned char *hash, int hashlen, unsigned char *sig, int siglen) |
Perform a public RSA and check the message digest. | |
void | rsa_free (rsa_context *ctx) |
Free the components of an RSA key. | |
int | rsa_self_test (int verbose) |
Checkup routine. |
Definition in file rsa.h.
#define ASN1_HASH_MDX |
Value:
"\x30\x20\x30\x0C\x06\x08\x2A\x86\x48" \ "\x86\xF7\x0D\x02\x00\x05\x00\x04\x10"
Definition at line 44 of file rsa.h.
Referenced by rsa_pkcs1_sign(), and rsa_pkcs1_verify().
#define ASN1_HASH_SHA1 |
Value:
"\x30\x21\x30\x09\x06\x05\x2B\x0E\x03" \ "\x02\x1A\x05\x00\x04\x14"
Definition at line 48 of file rsa.h.
Referenced by rsa_pkcs1_sign(), and rsa_pkcs1_verify().
#define ERR_RSA_BAD_INPUT_DATA 0x0300 |
Definition at line 13 of file rsa.h.
Referenced by rsa_gen_key(), rsa_pkcs1_decrypt(), rsa_pkcs1_encrypt(), rsa_pkcs1_sign(), rsa_pkcs1_verify(), rsa_private(), and rsa_public().
#define ERR_RSA_INVALID_PADDING 0x0310 |
#define ERR_RSA_KEY_CHK_FAILED 0x0330 |
#define ERR_RSA_KEY_GEN_FAILED 0x0320 |
#define ERR_RSA_KEY_RD_FAILED 0x0340 |
#define ERR_RSA_KEY_WR_FAILED 0x0350 |
#define ERR_RSA_PRIVATE_FAILED 0x0370 |
#define ERR_RSA_PUBLIC_FAILED 0x0360 |
#define ERR_RSA_VERIFY_FAILED 0x0380 |
#define RSA_CRYPT 0x02 |
#define RSA_D |
Value:
"24BF6185468786FDD303083D25E64EFC" \ "66CA472BC44D253102F8B4A9D3BFA750" \ "91386C0077937FE33FA3252D28855837" \ "AE1B484A8A9A45F7EE8C0C634F99E8CD" \ "DF79C5CE07EE72C7F123142198164234" \ "CABB724CF78B8173B9F880FC86322407" \ "AF1FEDFDDE2BEB674CA15F3E81A1521E" \ "071513A1E85B5DFA031F21ECAE91A34D"
Definition at line 262 of file rsa.h.
Referenced by main().
#define RSA_DP |
#define RSA_DQ |
#define RSA_MD2 2 |
Definition at line 27 of file rsa.h.
Referenced by rsa_pkcs1_sign(), rsa_pkcs1_verify(), x509_cert_info(), and x509_hash().
#define RSA_MD4 3 |
Definition at line 28 of file rsa.h.
Referenced by rsa_pkcs1_sign(), rsa_pkcs1_verify(), x509_cert_info(), and x509_hash().
#define RSA_MD5 4 |
Definition at line 29 of file rsa.h.
Referenced by rsa_pkcs1_sign(), rsa_pkcs1_verify(), x509_cert_info(), and x509_hash().
#define RSA_N |
Value:
"9292758453063D803DD603D5E777D788" \ "8ED1D5BF35786190FA2F23EBC0848AEA" \ "DDA92CA6C3D80B32C4D109BE0F36D6AE" \ "7130B9CED7ACDF54CFC7555AC14EEBAB" \ "93A89813FBF3C4F8066D2D800F7C38A8" \ "1AE31942917403FF4946B0A83D3D3E05" \ "EE57C6F5F5606FB5D4BC6CD34EE0801A" \ "5E94BB77B07507233A0BC7BAC8F90F79"
Definition at line 251 of file rsa.h.
Referenced by main().
#define RSA_P |
#define RSA_Q |
#define RSA_QP |
#define RSA_RAW 0 |
Definition at line 26 of file rsa.h.
Referenced by rsa_pkcs1_sign(), rsa_pkcs1_verify(), ssl_parse_certificate_verify(), ssl_parse_server_key_exchange(), ssl_write_certificate_verify(), and ssl_write_server_key_exchange().
#define RSA_SHA1 5 |
Definition at line 30 of file rsa.h.
Referenced by main(), rsa_pkcs1_sign(), rsa_pkcs1_verify(), x509_cert_info(), and x509_hash().
#define RSA_SIGN 0x01 |
int rsa_check_privkey | ( | rsa_context * | ctx | ) |
Return 0 if the private key is valid, or ERR_RSA_KEY_CHECK_FAILED.
Definition at line 228 of file dsa.c.
References CHK, rsa_context::E, ERR_RSA_KEY_CHK_FAILED, mpi_cmp_int(), mpi_cmp_mpi(), mpi_free(), mpi_gcd(), mpi_init(), mpi_mul_mpi(), mpi_sub_int(), rsa_context::N, rsa_context::P, and rsa_context::Q.
Referenced by x509_parse_key().
int rsa_check_pubkey | ( | rsa_context * | ctx | ) |
Return 0 if the public key is valid, or ERR_RSA_KEY_CHECK_FAILED.
Definition at line 208 of file dsa.c.
References rsa_context::E, ERR_RSA_KEY_CHK_FAILED, mpi_msb(), rsa_context::N, and mpi::p.
Referenced by x509_add_certs().
void rsa_free | ( | rsa_context * | ctx | ) |
Free the components of an RSA key.
Definition at line 471 of file dsa.c.
References rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, mpi_free(), rsa_context::N, rsa_context::P, rsa_context::Q, rsa_context::QP, rsa_context::RN, rsa_context::RP, and rsa_context::RQ.
Referenced by main(), rsa_gen_key(), rsa_read_private(), rsa_read_public(), x509_free_cert(), and x509_parse_key().
int rsa_gen_key | ( | rsa_context * | ctx, | |
int | nbits, | |||
int | exponent, | |||
int(*)(void *) | rng_f, | |||
void * | rng_d | |||
) |
Generate an RSA keypair.
ctx | RSA context to be initialized | |
nbits | size of the public key in bits | |
exponent | public exponent (e.g., 65537) | |
rng_f | points to the RNG function | |
rng_d | points to the RNG data |
Definition at line 41 of file dsa.c.
References CHK, rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, ERR_RSA_BAD_INPUT_DATA, ERR_RSA_KEY_GEN_FAILED, rsa_context::len, mpi_cmp_int(), mpi_cmp_mpi(), mpi_free(), mpi_gcd(), mpi_gen_prime(), mpi_init(), mpi_inv_mod(), mpi_lset(), mpi_mod_mpi(), mpi_msb(), mpi_mul_mpi(), mpi_sub_int(), mpi_swap(), rsa_context::N, rsa_context::P, rsa_context::Q, rsa_context::QP, and rsa_free().
Referenced by main().
int rsa_pkcs1_decrypt | ( | rsa_context * | ctx, | |
unsigned char * | input, | |||
int | ilen, | |||
unsigned char * | output, | |||
int * | olen | |||
) |
Do a private RSA, removes the PKCS#1 v1.5 padding.
ctx | RSA context | |
input | buffer holding the encrypted data | |
ilen | must be the same as the modulus size | |
output | buffer that will hold the plaintext | |
olen | size of output buffer, will be updated to contain the length of the plaintext |
Definition at line 287 of file dsa.c.
References ERR_RSA_BAD_INPUT_DATA, ERR_RSA_INVALID_PADDING, int, rsa_context::len, RSA_CRYPT, and rsa_private().
Referenced by ssl_parse_client_key_exchange().
int rsa_pkcs1_encrypt | ( | rsa_context * | ctx, | |
unsigned char * | input, | |||
int | ilen, | |||
unsigned char * | output, | |||
int | olen | |||
) |
Add the PKCS#1 v1.5 padding and do a public RSA.
ctx | RSA context | |
input | buffer holding the data to be encrypted | |
ilen | length of the plaintext; cannot be longer than the modulus, minus 3+8 for padding | |
output | buffer that will hold the ciphertext | |
olen | must be the same as the modulus size (for example, 128 if RSA-1024 is used) |
Definition at line 257 of file dsa.c.
References ERR_RSA_BAD_INPUT_DATA, rsa_context::len, RSA_CRYPT, and rsa_public().
Referenced by ssl_write_client_key_exchange().
int rsa_pkcs1_sign | ( | rsa_context * | ctx, | |
int | alg_id, | |||
unsigned char * | hash, | |||
int | hashlen, | |||
unsigned char * | sig, | |||
int | siglen | |||
) |
Perform a private RSA to sign a message digest.
ctx | RSA context | |
alg_id | RSA_RAW, RSA_MD2/4/5 or RSA_SHA1 | |
hash | buffer holding the message digest | |
hashlen | message digest length | |
sig | buffer that will hold the ciphertext | |
siglen | must be the same as the modulus size (for example, 128 if RSA-1024 is used) |
Definition at line 325 of file dsa.c.
References ASN1_HASH_MDX, ASN1_HASH_SHA1, ERR_RSA_BAD_INPUT_DATA, rsa_context::len, RSA_MD2, RSA_MD4, RSA_MD5, rsa_private(), RSA_RAW, RSA_SHA1, and RSA_SIGN.
Referenced by main(), ssl_write_certificate_verify(), and ssl_write_server_key_exchange().
int rsa_pkcs1_verify | ( | rsa_context * | ctx, | |
int | alg_id, | |||
unsigned char * | hash, | |||
int | hashlen, | |||
unsigned char * | sig, | |||
int | siglen | |||
) |
Perform a public RSA and check the message digest.
ctx | points to an RSA public key | |
alg_id | RSA_RAW, RSA_MD2/4/5 or RSA_SHA1 | |
hash | buffer holding the message digest | |
hashlen | message digest length | |
sig | buffer holding the ciphertext | |
siglen | must be the same as the modulus size |
Definition at line 401 of file dsa.c.
References ASN1_HASH_MDX, ASN1_HASH_SHA1, ERR_RSA_BAD_INPUT_DATA, ERR_RSA_INVALID_PADDING, ERR_RSA_VERIFY_FAILED, int, rsa_context::len, RSA_MD2, RSA_MD4, RSA_MD5, rsa_public(), RSA_RAW, RSA_SHA1, and RSA_SIGN.
Referenced by main(), ssl_parse_certificate_verify(), ssl_parse_server_key_exchange(), and x509_verify_cert().
int rsa_private | ( | rsa_context * | ctx, | |
unsigned char * | input, | |||
int | ilen, | |||
unsigned char * | output, | |||
int | olen | |||
) |
Perform an RSA private key operation.
Definition at line 147 of file dsa.c.
References CHK, rsa_context::D, rsa_context::DP, rsa_context::DQ, ERR_RSA_BAD_INPUT_DATA, ERR_RSA_PRIVATE_FAILED, rsa_context::len, mpi_add_mpi(), mpi_cmp_mpi(), mpi_exp_mod(), mpi_free(), mpi_init(), mpi_mod_mpi(), mpi_mul_mpi(), mpi_read_binary(), mpi_sub_mpi(), mpi_write_binary(), rsa_context::N, rsa_context::P, rsa_context::Q, rsa_context::QP, rsa_context::RN, rsa_context::RP, and rsa_context::RQ.
Referenced by main(), rsa_pkcs1_decrypt(), and rsa_pkcs1_sign().
int rsa_public | ( | rsa_context * | ctx, | |
unsigned char * | input, | |||
int | ilen, | |||
unsigned char * | output, | |||
int | olen | |||
) |
Perform an RSA public key operation.
Definition at line 111 of file dsa.c.
References CHK, rsa_context::E, ERR_RSA_BAD_INPUT_DATA, ERR_RSA_PUBLIC_FAILED, rsa_context::len, mpi_cmp_mpi(), mpi_exp_mod(), mpi_free(), mpi_init(), mpi_read_binary(), mpi_write_binary(), rsa_context::N, and rsa_context::RN.
Referenced by main(), rsa_pkcs1_encrypt(), and rsa_pkcs1_verify().
int rsa_read_private | ( | rsa_context * | ctx, | |
FILE * | f | |||
) |
Read the private key from a file.
ctx | RSA context to be initialized | |
f | Handle of the source file |
Definition at line 136 of file rsa.c.
References CHK, rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, ERR_RSA_KEY_RD_FAILED, rsa_context::len, mpi_msb(), mpi_read_file(), rsa_context::N, rsa_context::P, rsa_context::Q, rsa_context::QP, and rsa_free().
Referenced by main().
int rsa_read_public | ( | rsa_context * | ctx, | |
FILE * | f | |||
) |
Read the public key from a file.
ctx | RSA context to be initialized | |
f | Handle of the source file |
Definition at line 111 of file rsa.c.
References CHK, rsa_context::E, ERR_RSA_KEY_RD_FAILED, rsa_context::len, mpi_msb(), mpi_read_file(), rsa_context::N, and rsa_free().
Referenced by main().
int rsa_write_private | ( | rsa_context * | ctx, | |
FILE * | f | |||
) |
Write the private key into a file.
ctx | RSA context holding the key | |
f | Handle of the destination file |
Definition at line 185 of file rsa.c.
References CHK, rsa_context::D, rsa_context::DP, rsa_context::DQ, rsa_context::E, ERR_RSA_KEY_WR_FAILED, mpi_write_file(), rsa_context::N, rsa_context::P, rsa_context::Q, and rsa_context::QP.
Referenced by main().
int rsa_write_public | ( | rsa_context * | ctx, | |
FILE * | f | |||
) |
Write the public key into a file.
ctx | RSA context holding the key | |
f | Handle of the destination file |
Definition at line 167 of file rsa.c.
References CHK, rsa_context::E, ERR_RSA_KEY_WR_FAILED, mpi_write_file(), and rsa_context::N.
Referenced by main().