crypto.h File Reference

#include "bigint.h"
#include <string.h>

Go to the source code of this file.

Data Structures

struct  aes_key_st
struct  RC4_CTX
struct  SHA1_CTX
struct  MD5_CTX
struct  RSA_CTX
struct  _x509_ctx
struct  CA_CERT_CTX
struct  BUF_MEM

Defines

#define AES_MAXROUNDS   14
#define SHA1_SIZE   20
#define MD5_SIZE   16
#define X509_OK   0
#define X509_NOT_OK   -1
#define X509_VFY_ERROR_NO_TRUSTED_CERT   -2
#define X509_VFY_ERROR_BAD_SIGNATURE   -3
#define X509_VFY_ERROR_NOT_YET_VALID   -4
#define X509_VFY_ERROR_EXPIRED   -5
#define X509_VFY_ERROR_SELF_SIGNED   -6
#define X509_VFY_ERROR_INVALID_CHAIN   -7
#define X509_VFY_ERROR_UNSUPPORTED_DIGEST   -8
#define X509_INVALID_PRIV_KEY   -9
#define X509_NUM_DN_TYPES   3
#define X509_COMMON_NAME   0
#define X509_ORGANIZATION   1
#define X509_ORGANIZATIONAL_TYPE   2
#define ASN1_INTEGER   0x02
#define ASN1_BIT_STRING   0x03
#define ASN1_OCTET_STRING   0x04
#define ASN1_NULL   0x05
#define ASN1_OID   0x06
#define ASN1_PRINTABLE_STR   0x13
#define ASN1_TELETEX_STR   0x14
#define ASN1_IA5_STR   0x16
#define ASN1_UTC_TIME   0x17
#define ASN1_SEQUENCE   0x30
#define ASN1_SET   0x31
#define ASN1_IMPLICIT_TAG   0x80
#define ASN1_EXPLICIT_TAG   0xa0
#define SALT_SIZE   8
#define print_blob(...)

Typedefs

typedef struct aes_key_st AES_CTX
typedef struct _x509_ctx X509_CTX
typedef void(* crypt_func )(void *, const uint8_t *, uint8_t *, int)
typedef void(* hmac_func )(const uint8_t *msg, int length, const uint8_t *key, int key_len, uint8_t *digest)

Enumerations

enum  AES_MODE { AES_MODE_128, AES_MODE_256 }

Functions

void AES_set_key (AES_CTX *ctx, const uint8_t *key, const uint8_t *iv, AES_MODE mode)
 Set up AES with the key/iv and cipher size.
void AES_cbc_encrypt (AES_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
void AES_cbc_decrypt (AES_CTX *ks, const uint8_t *in, uint8_t *out, int length)
void AES_convert_key (AES_CTX *ctx)
 Change a key for decryption.
void AES_encrypt (const AES_CTX *ctx, uint32_t *data)
 Encrypt a single block (16 bytes) of data.
void AES_decrypt (const AES_CTX *ctx, uint32_t *data)
 Decrypt a single block (16 bytes) of data.
void RC4_setup (RC4_CTX *s, const uint8_t *key, int length)
void RC4_crypt (RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length)
void SHA1Init (SHA1_CTX *)
 Initialize the SHA1 context.
void SHA1Update (SHA1_CTX *, const uint8_t *msg, int len)
 Accepts an array of octets as the next portion of the message.
void SHA1Final (SHA1_CTX *, uint8_t *digest)
 Return the 160-bit message digest into the user's array.
void MD5Init (MD5_CTX *)
void MD5Update (MD5_CTX *, const uint8_t *msg, int len)
void MD5Final (MD5_CTX *, uint8_t *digest)
void hmac_md5 (const uint8_t *msg, int length, const uint8_t *key, int key_len, uint8_t *digest)
void hmac_sha1 (const uint8_t *msg, int length, const uint8_t *key, int key_len, uint8_t *digest)
void RNG_initialize (const uint8_t *seed_buf, int size)
void RNG_terminate (void)
void get_random (int num_rand_bytes, uint8_t *rand_data)
static void get_random_NZ (int num_rand_bytes, uint8_t *rand_data)
void RSA_priv_key_new (RSA_CTX **rsa_ctx, const uint8_t *modulus, int mod_len, const uint8_t *pub_exp, int pub_len, const uint8_t *priv_exp, int priv_len)
 Implements the RSA public encryption algorithm.
void RSA_pub_key_new (RSA_CTX **rsa_ctx, const uint8_t *modulus, int mod_len, const uint8_t *pub_exp, int pub_len)
void RSA_free (RSA_CTX *ctx)
 Free up any RSA context resources.
int RSA_decrypt (const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data, int is_decryption)
 Use PKCS1.5 for decryption/verification.
bigintRSA_private (const RSA_CTX *c, bigint *bi_msg)
 Performs m = c^d mod n.
bigintRSA_raw_sign_verify (RSA_CTX *c, bigint *bi_msg)
bigintRSA_sign_verify (BI_CTX *ctx, const uint8_t *sig, int sig_len, bigint *modulus, bigint *pub_exp)
bigintRSA_public (const RSA_CTX *c, bigint *bi_msg)
 Performs c = m^e mod n.
int RSA_encrypt (const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len, uint8_t *out_data, int is_signing)
 Use PKCS1.5 for encryption/signing.
void RSA_print (const RSA_CTX *ctx)
int asn1_get_private_key (const uint8_t *buf, int len, RSA_CTX **rsa_ctx)
int asn1_next_obj (const uint8_t *buf, int *offset, int obj_type)
int asn1_skip_obj (const uint8_t *buf, int *offset, int obj_type)
int asn1_get_int (const uint8_t *buf, int *offset, uint8_t **object)
int x509_new (const uint8_t *cert, int *len, X509_CTX **ctx)
void x509_free (X509_CTX *x509_ctx)
int x509_verify (const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert)
const uint8_tx509_get_signature (const uint8_t *asn1_signature, int *len)
BUF_MEM buf_new (void)
void buf_grow (BUF_MEM *bm, int len)
void buf_free (BUF_MEM *bm)
int get_file (const char *filename, uint8_t **buf)

Variables

const char *const unsupported_str


Define Documentation

#define AES_MAXROUNDS   14

Definition at line 36 of file crypto.h.

#define SHA1_SIZE   20

Definition at line 77 of file crypto.h.

Referenced by ccmp_kie_mic(), pbkdf2_sha1(), pbkdf2_sha1_f(), prf_sha1(), SHA1Final(), and wpa_check_pmkid().

#define MD5_SIZE   16

Definition at line 102 of file crypto.h.

#define X509_OK   0

Definition at line 189 of file crypto.h.

#define X509_NOT_OK   -1

Definition at line 190 of file crypto.h.

#define X509_VFY_ERROR_NO_TRUSTED_CERT   -2

Definition at line 191 of file crypto.h.

#define X509_VFY_ERROR_BAD_SIGNATURE   -3

Definition at line 192 of file crypto.h.

#define X509_VFY_ERROR_NOT_YET_VALID   -4

Definition at line 193 of file crypto.h.

#define X509_VFY_ERROR_EXPIRED   -5

Definition at line 194 of file crypto.h.

#define X509_VFY_ERROR_SELF_SIGNED   -6

Definition at line 195 of file crypto.h.

#define X509_VFY_ERROR_INVALID_CHAIN   -7

Definition at line 196 of file crypto.h.

#define X509_VFY_ERROR_UNSUPPORTED_DIGEST   -8

Definition at line 197 of file crypto.h.

#define X509_INVALID_PRIV_KEY   -9

Definition at line 198 of file crypto.h.

#define X509_NUM_DN_TYPES   3

Definition at line 203 of file crypto.h.

#define X509_COMMON_NAME   0

Definition at line 204 of file crypto.h.

#define X509_ORGANIZATION   1

Definition at line 205 of file crypto.h.

#define X509_ORGANIZATIONAL_TYPE   2

Definition at line 206 of file crypto.h.

#define ASN1_INTEGER   0x02

Definition at line 208 of file crypto.h.

Referenced by x509_public_key(), and x509_rsa_public_key().

#define ASN1_BIT_STRING   0x03

Definition at line 209 of file crypto.h.

Referenced by x509_public_key().

#define ASN1_OCTET_STRING   0x04

Definition at line 210 of file crypto.h.

#define ASN1_NULL   0x05

Definition at line 211 of file crypto.h.

#define ASN1_OID   0x06

Definition at line 212 of file crypto.h.

Referenced by x509_rsa_public_key().

#define ASN1_PRINTABLE_STR   0x13

Definition at line 213 of file crypto.h.

#define ASN1_TELETEX_STR   0x14

Definition at line 214 of file crypto.h.

#define ASN1_IA5_STR   0x16

Definition at line 215 of file crypto.h.

#define ASN1_UTC_TIME   0x17

Definition at line 216 of file crypto.h.

#define ASN1_SEQUENCE   0x30

Definition at line 217 of file crypto.h.

Referenced by x509_public_key(), and x509_rsa_public_key().

#define ASN1_SET   0x31

Definition at line 218 of file crypto.h.

#define ASN1_IMPLICIT_TAG   0x80

Definition at line 219 of file crypto.h.

#define ASN1_EXPLICIT_TAG   0xa0

Definition at line 220 of file crypto.h.

Referenced by x509_public_key().

#define SALT_SIZE   8

Definition at line 222 of file crypto.h.

#define print_blob ( ...   ) 

Definition at line 293 of file crypto.h.


Typedef Documentation

typedef struct aes_key_st AES_CTX

typedef struct _x509_ctx X509_CTX

Definition at line 243 of file crypto.h.

typedef void(* crypt_func)(void *, const uint8_t *, uint8_t *, int)

Definition at line 273 of file crypto.h.

typedef void(* hmac_func)(const uint8_t *msg, int length, const uint8_t *key, int key_len, uint8_t *digest)

Definition at line 274 of file crypto.h.


Enumeration Type Documentation

enum AES_MODE

Enumerator:
AES_MODE_128 
AES_MODE_256 

Definition at line 46 of file crypto.h.

00047 {
00048     AES_MODE_128,
00049     AES_MODE_256
00050 } AES_MODE;


Function Documentation

void AES_set_key ( AES_CTX ctx,
const uint8_t key,
const uint8_t iv,
AES_MODE  mode 
)

Set up AES with the key/iv and cipher size.

Definition at line 165 of file aes.c.

References AES_MODE_128, AES_MODE_256, aes_sbox, aes_key_st::iv, aes_key_st::key_size, aes_key_st::ks, memcpy, Rcon, and aes_key_st::rounds.

Referenced by aes_setkey().

00167 {
00168     int i, ii;
00169     uint32_t *W, tmp, tmp2;
00170     const unsigned char *ip;
00171     int words;
00172 
00173     switch (mode)
00174     {
00175         case AES_MODE_128:
00176             i = 10;
00177             words = 4;
00178             break;
00179 
00180         case AES_MODE_256:
00181             i = 14;
00182             words = 8;
00183             break;
00184 
00185         default:        /* fail silently */
00186             return;
00187     }
00188 
00189     ctx->rounds = i;
00190     ctx->key_size = words;
00191     W = ctx->ks;
00192     for (i = 0; i < words; i+=2)
00193     {
00194         W[i+0]= ((uint32_t)key[ 0]<<24)|
00195             ((uint32_t)key[ 1]<<16)|
00196             ((uint32_t)key[ 2]<< 8)|
00197             ((uint32_t)key[ 3]    );
00198         W[i+1]= ((uint32_t)key[ 4]<<24)|
00199             ((uint32_t)key[ 5]<<16)|
00200             ((uint32_t)key[ 6]<< 8)|
00201             ((uint32_t)key[ 7]    );
00202         key += 8;
00203     }
00204 
00205     ip = Rcon;
00206     ii = 4 * (ctx->rounds+1);
00207     for (i = words; i<ii; i++)
00208     {
00209         tmp = W[i-1];
00210 
00211         if ((i % words) == 0)
00212         {
00213             tmp2 =(uint32_t)aes_sbox[(tmp    )&0xff]<< 8;
00214             tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<<16;
00215             tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<24;
00216             tmp2|=(uint32_t)aes_sbox[(tmp>>24)     ];
00217             tmp=tmp2^(((unsigned int)*ip)<<24);
00218             ip++;
00219         }
00220 
00221         if ((words == 8) && ((i % words) == 4))
00222         {
00223             tmp2 =(uint32_t)aes_sbox[(tmp    )&0xff]    ;
00224             tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<< 8;
00225             tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<16;
00226             tmp2|=(uint32_t)aes_sbox[(tmp>>24)     ]<<24;
00227             tmp=tmp2;
00228         }
00229 
00230         W[i]=W[i-words]^tmp;
00231     }
00232 
00233     /* copy the iv across */
00234     memcpy(ctx->iv, iv, 16);
00235 }

void AES_cbc_encrypt ( AES_CTX ctx,
const uint8_t msg,
uint8_t out,
int  length 
)

void AES_cbc_decrypt ( AES_CTX ks,
const uint8_t in,
uint8_t out,
int  length 
)

void AES_convert_key ( AES_CTX ctx  ) 

Change a key for decryption.

Definition at line 240 of file aes.c.

References inv_mix_col, k, aes_key_st::ks, and aes_key_st::rounds.

Referenced by aes_decrypt().

00241 {
00242     int i;
00243     uint32_t *k,w,t1,t2,t3,t4;
00244 
00245     k = ctx->ks;
00246     k += 4;
00247 
00248     for (i=ctx->rounds*4; i>4; i--)
00249     {
00250         w= *k;
00251         w = inv_mix_col(w,t1,t2,t3,t4);
00252         *k++ =w;
00253     }
00254 }

void AES_encrypt ( const AES_CTX ctx,
uint32_t data 
)

Encrypt a single block (16 bytes) of data.

Definition at line 363 of file aes.c.

References aes_sbox, AES_xtime(), k, aes_key_st::ks, and aes_key_st::rounds.

Referenced by aes_encrypt().

00364 {
00365     /* To make this code smaller, generate the sbox entries on the fly.
00366      * This will have a really heavy effect upon performance.
00367      */
00368     uint32_t tmp[4];
00369     uint32_t tmp1, old_a0, a0, a1, a2, a3, row;
00370     int curr_rnd;
00371     int rounds = ctx->rounds; 
00372     const uint32_t *k = ctx->ks;
00373 
00374     /* Pre-round key addition */
00375     for (row = 0; row < 4; row++)
00376     {
00377         data[row] ^= *(k++);
00378     }
00379 
00380     /* Encrypt one block. */
00381     for (curr_rnd = 0; curr_rnd < rounds; curr_rnd++)
00382     {
00383         /* Perform ByteSub and ShiftRow operations together */
00384         for (row = 0; row < 4; row++)
00385         {
00386             a0 = (uint32_t)aes_sbox[(data[row%4]>>24)&0xFF];
00387             a1 = (uint32_t)aes_sbox[(data[(row+1)%4]>>16)&0xFF];
00388             a2 = (uint32_t)aes_sbox[(data[(row+2)%4]>>8)&0xFF]; 
00389             a3 = (uint32_t)aes_sbox[(data[(row+3)%4])&0xFF];
00390 
00391             /* Perform MixColumn iff not last round */
00392             if (curr_rnd < (rounds - 1))
00393             {
00394                 tmp1 = a0 ^ a1 ^ a2 ^ a3;
00395                 old_a0 = a0;
00396 
00397                 a0 ^= tmp1 ^ AES_xtime(a0 ^ a1);
00398                 a1 ^= tmp1 ^ AES_xtime(a1 ^ a2);
00399                 a2 ^= tmp1 ^ AES_xtime(a2 ^ a3);
00400                 a3 ^= tmp1 ^ AES_xtime(a3 ^ old_a0);
00401 
00402             }
00403 
00404             tmp[row] = ((a0 << 24) | (a1 << 16) | (a2 << 8) | a3);
00405         }
00406 
00407         /* KeyAddition - note that it is vital that this loop is separate from
00408            the MixColumn operation, which must be atomic...*/ 
00409         for (row = 0; row < 4; row++)
00410         {
00411             data[row] = tmp[row] ^ *(k++);
00412         }
00413     }
00414 }

void AES_decrypt ( const AES_CTX ctx,
uint32_t data 
)

Decrypt a single block (16 bytes) of data.

Definition at line 419 of file aes.c.

References aes_isbox, AES_xtime(), k, aes_key_st::ks, and aes_key_st::rounds.

Referenced by aes_decrypt().

00420 { 
00421     uint32_t tmp[4];
00422     uint32_t xt0,xt1,xt2,xt3,xt4,xt5,xt6;
00423     uint32_t a0, a1, a2, a3, row;
00424     int curr_rnd;
00425     int rounds = ctx->rounds;
00426     uint32_t *k = (uint32_t*)ctx->ks + ((rounds+1)*4);
00427 
00428     /* pre-round key addition */
00429     for (row=4; row > 0;row--)
00430     {
00431         data[row-1] ^= *(--k);
00432     }
00433 
00434     /* Decrypt one block */
00435     for (curr_rnd=0; curr_rnd < rounds; curr_rnd++)
00436     {
00437         /* Perform ByteSub and ShiftRow operations together */
00438         for (row = 4; row > 0; row--)
00439         {
00440             a0 = aes_isbox[(data[(row+3)%4]>>24)&0xFF];
00441             a1 = aes_isbox[(data[(row+2)%4]>>16)&0xFF];
00442             a2 = aes_isbox[(data[(row+1)%4]>>8)&0xFF];
00443             a3 = aes_isbox[(data[row%4])&0xFF];
00444 
00445             /* Perform MixColumn iff not last round */
00446             if (curr_rnd<(rounds-1))
00447             {
00448                 /* The MDS cofefficients (0x09, 0x0B, 0x0D, 0x0E)
00449                    are quite large compared to encryption; this 
00450                    operation slows decryption down noticeably. */
00451                 xt0 = AES_xtime(a0^a1);
00452                 xt1 = AES_xtime(a1^a2);
00453                 xt2 = AES_xtime(a2^a3);
00454                 xt3 = AES_xtime(a3^a0);
00455                 xt4 = AES_xtime(xt0^xt1);
00456                 xt5 = AES_xtime(xt1^xt2);
00457                 xt6 = AES_xtime(xt4^xt5);
00458 
00459                 xt0 ^= a1^a2^a3^xt4^xt6;
00460                 xt1 ^= a0^a2^a3^xt5^xt6;
00461                 xt2 ^= a0^a1^a3^xt4^xt6;
00462                 xt3 ^= a0^a1^a2^xt5^xt6;
00463                 tmp[row-1] = ((xt0<<24)|(xt1<<16)|(xt2<<8)|xt3);
00464             }
00465             else
00466                 tmp[row-1] = ((a0<<24)|(a1<<16)|(a2<<8)|a3);
00467         }
00468 
00469         for (row = 4; row > 0; row--)
00470         {
00471             data[row-1] = tmp[row-1] ^ *(--k);
00472         }
00473     }
00474 }

void RC4_setup ( RC4_CTX s,
const uint8_t key,
int  length 
)

void RC4_crypt ( RC4_CTX s,
const uint8_t msg,
uint8_t data,
int  length 
)

void SHA1Init ( SHA1_CTX  ) 

Initialize the SHA1 context.

Definition at line 40 of file sha1.c.

References SHA1_CTX::Intermediate_Hash, SHA1_CTX::Length_High, SHA1_CTX::Length_Low, and SHA1_CTX::Message_Block_Index.

Referenced by sha1_init().

00041 {
00042     ctx->Length_Low             = 0;
00043     ctx->Length_High            = 0;
00044     ctx->Message_Block_Index    = 0;
00045     ctx->Intermediate_Hash[0]   = 0x67452301;
00046     ctx->Intermediate_Hash[1]   = 0xEFCDAB89;
00047     ctx->Intermediate_Hash[2]   = 0x98BADCFE;
00048     ctx->Intermediate_Hash[3]   = 0x10325476;
00049     ctx->Intermediate_Hash[4]   = 0xC3D2E1F0;
00050 }

void SHA1Update ( SHA1_CTX ,
const uint8_t msg,
int  len 
)

Accepts an array of octets as the next portion of the message.

Definition at line 55 of file sha1.c.

References SHA1_CTX::Length_High, SHA1_CTX::Length_Low, SHA1_CTX::Message_Block, SHA1_CTX::Message_Block_Index, and SHA1ProcessMessageBlock().

Referenced by sha1_update().

00056 {
00057     while (len--)
00058     {
00059         ctx->Message_Block[ctx->Message_Block_Index++] = (*msg & 0xFF);
00060 
00061         ctx->Length_Low += 8;
00062         if (ctx->Length_Low == 0)
00063         {
00064             ctx->Length_High++;
00065         }
00066 
00067         if (ctx->Message_Block_Index == 64)
00068         {
00069             SHA1ProcessMessageBlock(ctx);
00070         }
00071 
00072         msg++;
00073     }
00074 }

void SHA1Final ( SHA1_CTX ,
uint8_t digest 
)

Return the 160-bit message digest into the user's array.

Definition at line 79 of file sha1.c.

References SHA1_CTX::Intermediate_Hash, SHA1_CTX::Length_High, SHA1_CTX::Length_Low, memset(), SHA1_CTX::Message_Block, SHA1_SIZE, and SHA1PadMessage().

Referenced by sha1_final().

00080 {
00081     int i;
00082 
00083     SHA1PadMessage(ctx);
00084     memset(ctx->Message_Block, 0, 64);
00085     ctx->Length_Low = 0;    /* and clear length */
00086     ctx->Length_High = 0;
00087 
00088     for  (i = 0; i < SHA1_SIZE; i++)
00089     {
00090         digest[i] = ctx->Intermediate_Hash[i>>2] >> 8 * ( 3 - ( i & 0x03 ) );
00091     }
00092 }

void MD5Init ( MD5_CTX  ) 

void MD5Update ( MD5_CTX ,
const uint8_t msg,
int  len 
)

void MD5Final ( MD5_CTX ,
uint8_t digest 
)

void hmac_md5 ( const uint8_t msg,
int  length,
const uint8_t key,
int  key_len,
uint8_t digest 
)

void hmac_sha1 ( const uint8_t msg,
int  length,
const uint8_t key,
int  key_len,
uint8_t digest 
)

void RNG_initialize ( const uint8_t seed_buf,
int  size 
)

void RNG_terminate ( void   ) 

void get_random ( int  num_rand_bytes,
uint8_t rand_data 
)

static void get_random_NZ ( int  num_rand_bytes,
uint8_t rand_data 
) [inline, static]

Definition at line 132 of file crypto.h.

References memset().

Referenced by RSA_encrypt().

00132                                                                          {
00133         memset ( rand_data, 0x01, num_rand_bytes );
00134 }

void RSA_priv_key_new ( RSA_CTX **  ctx,
const uint8_t modulus,
int  mod_len,
const uint8_t pub_exp,
int  pub_len,
const uint8_t priv_exp,
int  priv_len 
)

Implements the RSA public encryption algorithm.

Uses the bigint library to perform its calculations.

Definition at line 34 of file rsa.c.

References RSA_CTX::bi_ctx, bi_import(), bi_permanent(), bi_set_mod(), RSA_CTX::d, and RSA_pub_key_new().

00046 {
00047     RSA_CTX *rsa_ctx;
00048     BI_CTX *bi_ctx;
00049     RSA_pub_key_new(ctx, modulus, mod_len, pub_exp, pub_len);
00050     rsa_ctx = *ctx;
00051     bi_ctx = rsa_ctx->bi_ctx;
00052     rsa_ctx->d = bi_import(bi_ctx, priv_exp, priv_len);
00053     bi_permanent(rsa_ctx->d);
00054 
00055 #ifdef CONFIG_BIGINT_CRT
00056     rsa_ctx->p = bi_import(bi_ctx, p, p_len);
00057     rsa_ctx->q = bi_import(bi_ctx, q, q_len);
00058     rsa_ctx->dP = bi_import(bi_ctx, dP, dP_len);
00059     rsa_ctx->dQ = bi_import(bi_ctx, dQ, dQ_len);
00060     rsa_ctx->qInv = bi_import(bi_ctx, qInv, qInv_len);
00061     bi_permanent(rsa_ctx->dP);
00062     bi_permanent(rsa_ctx->dQ);
00063     bi_permanent(rsa_ctx->qInv);
00064     bi_set_mod(bi_ctx, rsa_ctx->p, BIGINT_P_OFFSET);
00065     bi_set_mod(bi_ctx, rsa_ctx->q, BIGINT_Q_OFFSET);
00066 #endif
00067 }

void RSA_pub_key_new ( RSA_CTX **  rsa_ctx,
const uint8_t modulus,
int  mod_len,
const uint8_t pub_exp,
int  pub_len 
)

Definition at line 69 of file rsa.c.

References RSA_CTX::bi_ctx, bi_import(), bi_initialize(), bi_permanent(), bi_set_mod(), BIGINT_M_OFFSET, calloc(), RSA_CTX::e, RSA_CTX::m, and RSA_CTX::num_octets.

Referenced by RSA_priv_key_new(), and tls_send_client_key_exchange().

00072 {
00073     RSA_CTX *rsa_ctx;
00074     BI_CTX *bi_ctx = bi_initialize();
00075     *ctx = (RSA_CTX *)calloc(1, sizeof(RSA_CTX));
00076     rsa_ctx = *ctx;
00077     rsa_ctx->bi_ctx = bi_ctx;
00078     rsa_ctx->num_octets = (mod_len & 0xFFF0);
00079     rsa_ctx->m = bi_import(bi_ctx, modulus, mod_len);
00080     bi_set_mod(bi_ctx, rsa_ctx->m, BIGINT_M_OFFSET);
00081     rsa_ctx->e = bi_import(bi_ctx, pub_exp, pub_len);
00082     bi_permanent(rsa_ctx->e);
00083 }

void RSA_free ( RSA_CTX ctx  ) 

Free up any RSA context resources.

Definition at line 88 of file rsa.c.

References RSA_CTX::bi_ctx, bi_depermanent(), bi_free(), bi_free_mod(), bi_terminate(), BIGINT_M_OFFSET, RSA_CTX::d, RSA_CTX::e, free(), and NULL.

Referenced by tls_send_client_key_exchange().

00089 {
00090     BI_CTX *bi_ctx;
00091     if (rsa_ctx == NULL)                /* deal with ptrs that are null */
00092         return;
00093 
00094     bi_ctx = rsa_ctx->bi_ctx;
00095 
00096     bi_depermanent(rsa_ctx->e);
00097     bi_free(bi_ctx, rsa_ctx->e);
00098     bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET);
00099 
00100     if (rsa_ctx->d)
00101     {
00102         bi_depermanent(rsa_ctx->d);
00103         bi_free(bi_ctx, rsa_ctx->d);
00104 #ifdef CONFIG_BIGINT_CRT
00105         bi_depermanent(rsa_ctx->dP);
00106         bi_depermanent(rsa_ctx->dQ);
00107         bi_depermanent(rsa_ctx->qInv);
00108         bi_free(bi_ctx, rsa_ctx->dP);
00109         bi_free(bi_ctx, rsa_ctx->dQ);
00110         bi_free(bi_ctx, rsa_ctx->qInv);
00111         bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET);
00112         bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET);
00113 #endif
00114     }
00115 
00116     bi_terminate(bi_ctx);
00117     free(rsa_ctx);
00118 }

int RSA_decrypt ( const RSA_CTX ctx,
const uint8_t in_data,
uint8_t out_data,
int  is_decryption 
)

Use PKCS1.5 for decryption/verification.

Parameters:
ctx [in] The context
in_data [in] The data to encrypt (must be < modulus size-11)
out_data [out] The encrypted data.
is_decryption [in] Decryption or verify operation.
Returns:
The number of bytes that were originally encrypted. -1 on error.
See also:
http://www.rsasecurity.com/rsalabs/node.asp?id=2125

Definition at line 129 of file rsa.c.

References RSA_CTX::bi_ctx, bi_export(), bi_import(), free(), malloc(), memcpy, memset(), RSA_CTX::num_octets, RSA_private(), RSA_public(), and size.

00131 {
00132     int byte_size = ctx->num_octets;
00133     uint8_t *block;
00134     int i, size;
00135     bigint *decrypted_bi, *dat_bi;
00136 
00137     memset(out_data, 0, byte_size); /* initialise */
00138 
00139     /* decrypt */
00140     dat_bi = bi_import(ctx->bi_ctx, in_data, byte_size);
00141 #ifdef CONFIG_SSL_CERT_VERIFICATION
00142     decrypted_bi = is_decryption ?  /* decrypt or verify? */
00143             RSA_private(ctx, dat_bi) : RSA_public(ctx, dat_bi);
00144 #else   /* always a decryption */
00145     decrypted_bi = RSA_private(ctx, dat_bi);
00146 #endif
00147 
00148     /* convert to a normal block */
00149     block = (uint8_t *)malloc(byte_size);
00150     bi_export(ctx->bi_ctx, decrypted_bi, block, byte_size);
00151 
00152     i = 10; /* start at the first possible non-padded byte */
00153 
00154 #ifdef CONFIG_SSL_CERT_VERIFICATION
00155     if (is_decryption == 0) /* PKCS1.5 signing pads with "0xff"s */
00156     {
00157         while (block[i++] == 0xff && i < byte_size);
00158 
00159         if (block[i-2] != 0xff)
00160             i = byte_size;     /*ensure size is 0 */   
00161     }
00162     else                    /* PKCS1.5 encryption padding is random */
00163 #endif
00164     {
00165         while (block[i++] && i < byte_size);
00166     }
00167     size = byte_size - i;
00168 
00169     /* get only the bit we want */
00170     if (size > 0)
00171         memcpy(out_data, &block[i], size);
00172     
00173     free(block);
00174     return size ? size : -1;
00175 }

bigint* RSA_private ( const RSA_CTX c,
bigint bi_msg 
)

Performs m = c^d mod n.

Definition at line 180 of file rsa.c.

References RSA_CTX::bi_ctx, bi_mod_power(), BIGINT_M_OFFSET, RSA_CTX::d, and BI_CTX::mod_offset.

Referenced by RSA_decrypt(), and RSA_encrypt().

00181 {
00182 #ifdef CONFIG_BIGINT_CRT
00183     return bi_crt(c, bi_msg);
00184 #else
00185     BI_CTX *ctx = c->bi_ctx;
00186     ctx->mod_offset = BIGINT_M_OFFSET;
00187     return bi_mod_power(ctx, bi_msg, c->d);
00188 #endif
00189 }

bigint* RSA_raw_sign_verify ( RSA_CTX c,
bigint bi_msg 
)

bigint* RSA_sign_verify ( BI_CTX ctx,
const uint8_t sig,
int  sig_len,
bigint modulus,
bigint pub_exp 
)

bigint* RSA_public ( const RSA_CTX c,
bigint bi_msg 
)

Performs c = m^e mod n.

Definition at line 245 of file rsa.c.

References RSA_CTX::bi_ctx, bi_mod_power(), BIGINT_M_OFFSET, RSA_CTX::e, and BI_CTX::mod_offset.

Referenced by RSA_decrypt(), and RSA_encrypt().

00246 {
00247     c->bi_ctx->mod_offset = BIGINT_M_OFFSET;
00248     return bi_mod_power(c->bi_ctx, bi_msg, c->e);
00249 }

int RSA_encrypt ( const RSA_CTX ctx,
const uint8_t in_data,
uint16_t  in_len,
uint8_t out_data,
int  is_signing 
)

Use PKCS1.5 for encryption/signing.

see http://www.rsasecurity.com/rsalabs/node.asp?id=2125

Definition at line 255 of file rsa.c.

References RSA_CTX::bi_ctx, bi_export(), bi_import(), get_random_NZ(), memcpy, memset(), RSA_CTX::num_octets, RSA_private(), and RSA_public().

Referenced by tls_send_client_key_exchange().

00257 {
00258     int byte_size = ctx->num_octets;
00259     int num_pads_needed = byte_size-in_len-3;
00260     bigint *dat_bi, *encrypt_bi;
00261 
00262     /* note: in_len+11 must be > byte_size */
00263     out_data[0] = 0;     /* ensure encryption block is < modulus */
00264 
00265     if (is_signing)
00266     {
00267         out_data[1] = 1;        /* PKCS1.5 signing pads with "0xff"'s */
00268         memset(&out_data[2], 0xff, num_pads_needed);
00269     }
00270     else /* randomize the encryption padding with non-zero bytes */   
00271     {
00272         out_data[1] = 2;
00273         get_random_NZ(num_pads_needed, &out_data[2]);
00274     }
00275 
00276     out_data[2+num_pads_needed] = 0;
00277     memcpy(&out_data[3+num_pads_needed], in_data, in_len);
00278 
00279     /* now encrypt it */
00280     dat_bi = bi_import(ctx->bi_ctx, out_data, byte_size);
00281     encrypt_bi = is_signing ? RSA_private(ctx, dat_bi) : 
00282         RSA_public(ctx, dat_bi);
00283     bi_export(ctx->bi_ctx, encrypt_bi, out_data, byte_size);
00284     return byte_size;
00285 }

void RSA_print ( const RSA_CTX ctx  ) 

int asn1_get_private_key ( const uint8_t buf,
int  len,
RSA_CTX **  rsa_ctx 
)

int asn1_next_obj ( const uint8_t buf,
int *  offset,
int  obj_type 
)

int asn1_skip_obj ( const uint8_t buf,
int *  offset,
int  obj_type 
)

int asn1_get_int ( const uint8_t buf,
int *  offset,
uint8_t **  object 
)

int x509_new ( const uint8_t cert,
int *  len,
X509_CTX **  ctx 
)

void x509_free ( X509_CTX x509_ctx  ) 

int x509_verify ( const CA_CERT_CTX ca_cert_ctx,
const X509_CTX cert 
)

const uint8_t* x509_get_signature ( const uint8_t asn1_signature,
int *  len 
)

BUF_MEM buf_new ( void   ) 

void buf_grow ( BUF_MEM bm,
int  len 
)

void buf_free ( BUF_MEM bm  ) 

int get_file ( const char *  filename,
uint8_t **  buf 
)


Variable Documentation

const char* const unsupported_str


Generated on Tue Apr 6 20:01:15 2010 for gPXE by  doxygen 1.5.7.1