This is a fork of the mbed port of axTLS
Dependents: TLS_axTLS-Example HTTPSClientExample
crypto.h
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 00056 #ifndef CONFIG_SSL_ENABLE_CLIENT 00057 #define CONFIG_SSL_ENABLE_CLIENT 00058 #endif 00059 00060 #ifndef CONFIG_SSL_CERT_VERIFICATION 00061 #define CONFIG_SSL_CERT_VERIFICATION 00062 #endif 00063 00064 #elif defined(CONFIG_SSL_ENABLE_CLIENT) && !defined(CONFIG_SSL_CERT_VERIFICATION) 00065 #define CONFIG_SSL_CERT_VERIFICATION 00066 #endif 00067 00068 /************************************************************************** 00069 * AES declarations 00070 **************************************************************************/ 00071 00072 #define AES_MAXROUNDS 14 00073 #define AES_BLOCKSIZE 16 00074 #define AES_IV_SIZE 16 00075 00076 typedef struct aes_key_st 00077 { 00078 uint16_t rounds; 00079 uint16_t key_size; 00080 uint32_t ks[(AES_MAXROUNDS+1)*8]; 00081 uint8_t iv[AES_IV_SIZE]; 00082 } AES_CTX; 00083 00084 typedef enum 00085 { 00086 AES_MODE_128, 00087 AES_MODE_256 00088 } AES_MODE; 00089 00090 void AES_set_key(AES_CTX *ctx, const uint8_t *key, 00091 const uint8_t *iv, AES_MODE mode); 00092 void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg, 00093 uint8_t *out, int length); 00094 void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length); 00095 void AES_convert_key(AES_CTX *ctx); 00096 00097 /************************************************************************** 00098 * RC4 declarations 00099 **************************************************************************/ 00100 00101 typedef struct 00102 { 00103 uint8_t x, y, m[256]; 00104 } RC4_CTX; 00105 00106 void RC4_setup(RC4_CTX *s, const uint8_t *key, int length); 00107 void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length); 00108 00109 /************************************************************************** 00110 * SHA1 declarations 00111 **************************************************************************/ 00112 00113 #define SHA1_SIZE 20 00114 00115 /* 00116 * This structure will hold context information for the SHA-1 00117 * hashing operation 00118 */ 00119 typedef struct 00120 { 00121 uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */ 00122 uint32_t Length_Low; /* Message length in bits */ 00123 uint32_t Length_High; /* Message length in bits */ 00124 uint16_t Message_Block_Index; /* Index into message block array */ 00125 uint8_t Message_Block[64]; /* 512-bit message blocks */ 00126 } SHA1_CTX; 00127 00128 void SHA1_Init(SHA1_CTX *); 00129 void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len); 00130 void SHA1_Final(uint8_t *digest, SHA1_CTX *); 00131 00132 /************************************************************************** 00133 * MD2 declarations 00134 **************************************************************************/ 00135 00136 #define MD2_SIZE 16 00137 00138 typedef struct 00139 { 00140 unsigned char cksum[16]; /* checksum of the data block */ 00141 unsigned char state[48]; /* intermediate digest state */ 00142 unsigned char buffer[16]; /* data block being processed */ 00143 int left; /* amount of data in buffer */ 00144 } MD2_CTX; 00145 00146 EXP_FUNC void STDCALL MD2_Init(MD2_CTX *ctx); 00147 EXP_FUNC void STDCALL MD2_Update(MD2_CTX *ctx, const uint8_t *input, int ilen); 00148 EXP_FUNC void STDCALL MD2_Final(uint8_t *digest, MD2_CTX *ctx); 00149 00150 /************************************************************************** 00151 * MD5 declarations 00152 **************************************************************************/ 00153 00154 #define MD5_SIZE 16 00155 #define MAX_KEYBLOCK_SIZE 136 00156 typedef struct 00157 { 00158 uint32_t state[4]; /* state (ABCD) */ 00159 uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ 00160 uint8_t buffer[64]; /* input buffer */ 00161 } MD5_CTX; 00162 00163 EXP_FUNC void STDCALL MD5_Init(MD5_CTX *); 00164 EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len); 00165 EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *); 00166 00167 /************************************************************************** 00168 * HMAC declarations 00169 **************************************************************************/ 00170 void hmac_md5(const uint8_t *msg, int length, const uint8_t *key, 00171 int key_len, uint8_t *digest); 00172 void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key, 00173 int key_len, uint8_t *digest); 00174 00175 /************************************************************************** 00176 * RSA declarations 00177 **************************************************************************/ 00178 00179 typedef struct 00180 { 00181 bigint *m; /* modulus */ 00182 bigint *e; /* public exponent */ 00183 bigint *d; /* private exponent */ 00184 #ifdef CONFIG_BIGINT_CRT 00185 bigint *p; /* p as in m = pq */ 00186 bigint *q; /* q as in m = pq */ 00187 bigint *dP; /* d mod (p-1) */ 00188 bigint *dQ; /* d mod (q-1) */ 00189 bigint *qInv; /* q^-1 mod p */ 00190 #endif 00191 int num_octets; 00192 BI_CTX *bi_ctx; 00193 } RSA_CTX; 00194 00195 void RSA_priv_key_new(RSA_CTX **rsa_ctx, 00196 const uint8_t *modulus, int mod_len, 00197 const uint8_t *pub_exp, int pub_len, 00198 const uint8_t *priv_exp, int priv_len 00199 #ifdef CONFIG_BIGINT_CRT 00200 , const uint8_t *p, int p_len, 00201 const uint8_t *q, int q_len, 00202 const uint8_t *dP, int dP_len, 00203 const uint8_t *dQ, int dQ_len, 00204 const uint8_t *qInv, int qInv_len 00205 #endif 00206 ); 00207 void RSA_pub_key_new(RSA_CTX **rsa_ctx, 00208 const uint8_t *modulus, int mod_len, 00209 const uint8_t *pub_exp, int pub_len); 00210 void RSA_free(RSA_CTX *ctx); 00211 int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data, 00212 int is_decryption); 00213 bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg); 00214 //#if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT) 00215 bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len, 00216 bigint *modulus, bigint *pub_exp); 00217 bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg); 00218 int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len, 00219 uint8_t *out_data, int is_signing); 00220 void RSA_print(const RSA_CTX *ctx); 00221 //#endif 00222 00223 /************************************************************************** 00224 * RNG declarations 00225 **************************************************************************/ 00226 EXP_FUNC void STDCALL RNG_initialize(void); 00227 EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size); 00228 EXP_FUNC void STDCALL RNG_terminate(void); 00229 EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data); 00230 void get_random_NZ(int num_rand_bytes, uint8_t *rand_data); 00231 00232 #ifdef __cplusplus 00233 } 00234 #endif 00235 00236 #endif 00237 00238
Generated on Wed Jul 13 2022 19:30:07 by 1.7.2