/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/library/dsa.c File Reference

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


Define Documentation

#define _CRT_SECURE_NO_DEPRECATE   1

Definition at line 28 of file dsa.c.


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.

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.

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.

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

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

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

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.

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.

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.

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.

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.


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