#include <stdlib.h>#include <string.h>#include <stdio.h>#include "xyssl/rsa.h"Go to the source code of this file.
Defines | |
| #define | _CRT_SECURE_NO_DEPRECATE 1 |
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. | |
| int rsa_check_privkey | ( | rsa_context * | ctx | ) |
Return 0 if the private key is valid, or ERR_RSA_KEY_CHECK_FAILED.
Definition at line 329 of file rsa.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 309 of file rsa.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 572 of file rsa.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 44 of file rsa.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 388 of file rsa.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 358 of file rsa.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 426 of file rsa.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 502 of file rsa.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 248 of file rsa.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 212 of file rsa.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 139 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 114 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 188 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 170 of file rsa.c.
References CHK, rsa_context::E, ERR_RSA_KEY_WR_FAILED, mpi_write_file(), and rsa_context::N.
Referenced by main().
1.5.4