joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crypto.h Source File

crypto.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007, Cameron Rich
00003  * 
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without 
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright notice, 
00010  *   this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright notice, 
00012  *   this list of conditions and the following disclaimer in the documentation 
00013  *   and/or other materials provided with the distribution.
00014  * * Neither the name of the axTLS project nor the names of its contributors 
00015  *   may be used to endorse or promote products derived from this software 
00016  *   without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 /**
00032  * @file crypto.h
00033  */
00034 
00035 #ifndef HEADER_CRYPTO_H
00036 #define HEADER_CRYPTO_H
00037 
00038 #ifdef __cplusplus
00039 extern "C" {
00040 #endif
00041 
00042 #include "bigint_impl.h"
00043 #include "bigint.h"
00044 
00045 #ifndef STDCALL
00046 #define STDCALL
00047 #endif
00048 #ifndef EXP_FUNC
00049 #define EXP_FUNC
00050 #endif
00051 
00052 
00053 /* enable features based on a 'super-set' capbaility. */
00054 #if defined(CONFIG_SSL_FULL_MODE) 
00055 #define CONFIG_SSL_ENABLE_CLIENT
00056 #define CONFIG_SSL_CERT_VERIFICATION
00057 #elif defined(CONFIG_SSL_ENABLE_CLIENT)
00058 #define CONFIG_SSL_CERT_VERIFICATION
00059 #endif
00060 
00061 /**************************************************************************
00062  * AES declarations 
00063  **************************************************************************/
00064 
00065 #define AES_MAXROUNDS            14
00066 #define AES_BLOCKSIZE           16
00067 #define AES_IV_SIZE             16
00068 
00069 typedef struct aes_key_st 
00070 {
00071     uint16_t rounds;
00072     uint16_t key_size;
00073     uint32_t ks[(AES_MAXROUNDS+1)*8];
00074     uint8_t iv[AES_IV_SIZE];
00075 } AES_CTX;
00076 
00077 typedef enum
00078 {
00079     AES_MODE_128,
00080     AES_MODE_256
00081 } AES_MODE;
00082 
00083 void AES_set_key(AES_CTX *ctx, const uint8_t *key, 
00084         const uint8_t *iv, AES_MODE mode);
00085 void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg, 
00086         uint8_t *out, int length);
00087 void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
00088 void AES_convert_key(AES_CTX *ctx);
00089 
00090 /**************************************************************************
00091  * RC4 declarations 
00092  **************************************************************************/
00093 
00094 typedef struct 
00095 {
00096     uint8_t x, y, m[256];
00097 } RC4_CTX;
00098 
00099 void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
00100 void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
00101 
00102 /**************************************************************************
00103  * SHA1 declarations 
00104  **************************************************************************/
00105 
00106 #define SHA1_SIZE   20
00107 
00108 /*
00109  *  This structure will hold context information for the SHA-1
00110  *  hashing operation
00111  */
00112 typedef struct 
00113 {
00114     uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
00115     uint32_t Length_Low;            /* Message length in bits */
00116     uint32_t Length_High;           /* Message length in bits */
00117     uint16_t Message_Block_Index;   /* Index into message block array   */
00118     uint8_t Message_Block[64];      /* 512-bit message blocks */
00119 } SHA1_CTX;
00120 
00121 void SHA1_Init(SHA1_CTX *);
00122 void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len);
00123 void SHA1_Final(uint8_t *digest, SHA1_CTX *);
00124 
00125 /**************************************************************************
00126  * MD2 declarations 
00127  **************************************************************************/
00128 
00129 #define MD2_SIZE 16
00130 
00131 typedef struct
00132 {
00133     unsigned char cksum[16];    /* checksum of the data block */
00134     unsigned char state[48];    /* intermediate digest state */
00135     unsigned char buffer[16];   /* data block being processed */
00136     int left;                   /* amount of data in buffer */
00137 } MD2_CTX;
00138 
00139 EXP_FUNC void STDCALL MD2_Init(MD2_CTX *ctx);
00140 EXP_FUNC void STDCALL MD2_Update(MD2_CTX *ctx, const uint8_t *input, int ilen);
00141 EXP_FUNC void STDCALL MD2_Final(uint8_t *digest, MD2_CTX *ctx);
00142 
00143 /**************************************************************************
00144  * MD5 declarations 
00145  **************************************************************************/
00146 
00147 #define MD5_SIZE    16
00148 #define MAX_KEYBLOCK_SIZE 136
00149 typedef struct 
00150 {
00151   uint32_t state[4];        /* state (ABCD) */
00152   uint32_t count[2];        /* number of bits, modulo 2^64 (lsb first) */
00153   uint8_t buffer[64];       /* input buffer */
00154 } MD5_CTX;
00155 
00156 EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
00157 EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
00158 EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
00159 
00160 /**************************************************************************
00161  * HMAC declarations 
00162  **************************************************************************/
00163 void hmac_md5(const uint8_t *msg, int length, const uint8_t *key, 
00164         int key_len, uint8_t *digest);
00165 void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key, 
00166         int key_len, uint8_t *digest);
00167 
00168 /**************************************************************************
00169  * RSA declarations 
00170  **************************************************************************/
00171 
00172 typedef struct 
00173 {
00174     bigint *m;              /* modulus */
00175     bigint *e;              /* public exponent */
00176     bigint *d;              /* private exponent */
00177 #ifdef CONFIG_BIGINT_CRT
00178     bigint *p;              /* p as in m = pq */
00179     bigint *q;              /* q as in m = pq */
00180     bigint *dP;             /* d mod (p-1) */
00181     bigint *dQ;             /* d mod (q-1) */
00182     bigint *qInv;           /* q^-1 mod p */
00183 #endif
00184     int num_octets;
00185     BI_CTX *bi_ctx;
00186 } RSA_CTX;
00187 
00188 void RSA_priv_key_new(RSA_CTX **rsa_ctx, 
00189         const uint8_t *modulus, int mod_len,
00190         const uint8_t *pub_exp, int pub_len,
00191         const uint8_t *priv_exp, int priv_len
00192 #ifdef CONFIG_BIGINT_CRT
00193       , const uint8_t *p, int p_len,
00194         const uint8_t *q, int q_len,
00195         const uint8_t *dP, int dP_len,
00196         const uint8_t *dQ, int dQ_len,
00197         const uint8_t *qInv, int qInv_len
00198 #endif
00199         );
00200 void RSA_pub_key_new(RSA_CTX **rsa_ctx, 
00201         const uint8_t *modulus, int mod_len,
00202         const uint8_t *pub_exp, int pub_len);
00203 void RSA_free(RSA_CTX *ctx);
00204 int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
00205         int is_decryption);
00206 bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
00207 #if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
00208 bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
00209         bigint *modulus, bigint *pub_exp);
00210 bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
00211 int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len, 
00212         uint8_t *out_data, int is_signing);
00213 void RSA_print(const RSA_CTX *ctx);
00214 #endif
00215 
00216 /**************************************************************************
00217  * RNG declarations 
00218  **************************************************************************/
00219 EXP_FUNC void STDCALL RNG_initialize(void);
00220 EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
00221 EXP_FUNC void STDCALL RNG_terminate(void);
00222 EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
00223 void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
00224 
00225 #ifdef __cplusplus
00226 }
00227 #endif
00228 
00229 #endif