/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/include/xyssl/rsa.h File Reference

#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.


Detailed Description

Definition in file rsa.h.


Define Documentation

#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

Definition at line 14 of file rsa.h.

Referenced by rsa_pkcs1_decrypt(), and rsa_pkcs1_verify().

#define ERR_RSA_KEY_CHK_FAILED   0x0330

Definition at line 16 of file rsa.h.

Referenced by rsa_check_privkey(), and rsa_check_pubkey().

#define ERR_RSA_KEY_GEN_FAILED   0x0320

Definition at line 15 of file rsa.h.

Referenced by rsa_gen_key().

#define ERR_RSA_KEY_RD_FAILED   0x0340

Definition at line 17 of file rsa.h.

Referenced by rsa_read_private(), and rsa_read_public().

#define ERR_RSA_KEY_WR_FAILED   0x0350

Definition at line 18 of file rsa.h.

Referenced by rsa_write_private(), and rsa_write_public().

#define ERR_RSA_PRIVATE_FAILED   0x0370

Definition at line 20 of file rsa.h.

Referenced by rsa_private().

#define ERR_RSA_PUBLIC_FAILED   0x0360

Definition at line 19 of file rsa.h.

Referenced by rsa_public().

#define ERR_RSA_VERIFY_FAILED   0x0380

Definition at line 21 of file rsa.h.

Referenced by rsa_pkcs1_verify().

#define KEY_LEN   128

Definition at line 249 of file rsa.h.

Referenced by main().

#define RSA_CRYPT   0x02

Definition at line 33 of file rsa.h.

Referenced by rsa_pkcs1_decrypt(), and rsa_pkcs1_encrypt().

#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

Value:

"C1ACF567564274FB07A0BBAD5D26E298" \
                "3C94D22288ACD763FD8E5600ED4A702D" \
                "F84198A5F06C2E72236AE490C93F07F8" \
                "3CC559CD27BC2D1CA488811730BB5725"

Definition at line 281 of file rsa.h.

Referenced by main().

#define RSA_DQ

Value:

"4959CBF6F8FEF750AEE6977C155579C7" \
                "D8AAEA56749EA28623272E4F7D0592AF" \
                "7C1F1313CAC9471B5C523BFE592F517B" \
                "407A1BD76C164B93DA2D32A383E58357"

Definition at line 286 of file rsa.h.

Referenced by main().

#define RSA_E   "10001"

Definition at line 260 of file rsa.h.

Referenced by main().

#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

Value:

"C36D0EB7FCD285223CFB5AABA5BDA3D8" \
                "2C01CAD19EA484A87EA4377637E75500" \
                "FCB2005C5C7DD6EC4AC023CDA285D796" \
                "C3D9E75E1EFC42488BB4F1D13AC30A57"

Definition at line 271 of file rsa.h.

Referenced by main().

#define RSA_Q

Value:

"C000DF51A7C77AE8D7C7370C1FF55B69" \
                "E211C2B9E5DB1ED0BF61D0D9899620F4" \
                "910E4168387E3C30AA1E00C339A79508" \
                "8452DD96A9A5EA5D9DCA68DA636032AF"

Definition at line 276 of file rsa.h.

Referenced by main().

#define RSA_QP

Value:

"9AE7FBC99546432DF71896FC239EADAE" \
                "F38D18D2B2F0E2DD275AA977E2BF4411" \
                "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
                "A74206CEC169D74BF5A8C50D6F48EA08"

Definition at line 291 of file rsa.h.

Referenced by main().

#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

Definition at line 32 of file rsa.h.

Referenced by rsa_pkcs1_sign(), and rsa_pkcs1_verify().


Function Documentation

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.

Parameters:
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
Returns:
0 if successful, or an ERR_RSA_XXX error code

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.

Parameters:
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
Returns:
0 if successful, or an ERR_RSA_XXX error code

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.

Parameters:
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)
Returns:
0 if successful, or an ERR_RSA_XXX error code

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.

Parameters:
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)
Returns:
0 if the signing operation was successful, or an ERR_RSA_XXX error code

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.

Parameters:
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
Returns:
0 if the verify operation was successful, or an ERR_RSA_XXX error code

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.

Returns:
0 if successful, or an ERR_RSA_XXX error code
Note:
This function does not take care of message padding: both ilen and olen must be equal to the modulus size (ctx->len). Also, be sure to set input[0] = 0.

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.

Returns:
0 if successful, or an ERR_RSA_XXX error code
Note:
This function does not take care of message padding: both ilen and olen must be equal to the modulus size (ctx->len). Also, be sure to set input[0] = 0.

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.

Parameters:
ctx RSA context to be initialized
f Handle of the source file
Returns:
0 if successful, or an ERR_RSA_XXX error code

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.

Parameters:
ctx RSA context to be initialized
f Handle of the source file
Returns:
0 if successful, or an ERR_RSA_XXX error code

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_self_test ( int  verbose  ) 

Checkup routine.

Returns:
0 if successful, or 1 if the test failed

Definition at line 585 of file dsa.c.

Referenced by main().

int rsa_write_private ( rsa_context ctx,
FILE *  f 
)

Write the private key into a file.

Parameters:
ctx RSA context holding the key
f Handle of the destination file
Returns:
0 if successful, or an ERR_RSA_XXX error code

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.

Parameters:
ctx RSA context holding the key
f Handle of the destination file
Returns:
0 if successful, or an ERR_RSA_XXX error code

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().


Generated on Fri May 16 14:49:56 2008 for Mobile-C by  doxygen 1.5.4