Rough and ready port of axTLS

Committer:
ashleymills
Date:
Mon May 13 18:15:18 2013 +0000
Revision:
0:5a29fd060ac8
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:5a29fd060ac8 1 /*
ashleymills 0:5a29fd060ac8 2 * Copyright (c) 2007, Cameron Rich
ashleymills 0:5a29fd060ac8 3 *
ashleymills 0:5a29fd060ac8 4 * All rights reserved.
ashleymills 0:5a29fd060ac8 5 *
ashleymills 0:5a29fd060ac8 6 * Redistribution and use in source and binary forms, with or without
ashleymills 0:5a29fd060ac8 7 * modification, are permitted provided that the following conditions are met:
ashleymills 0:5a29fd060ac8 8 *
ashleymills 0:5a29fd060ac8 9 * * Redistributions of source code must retain the above copyright notice,
ashleymills 0:5a29fd060ac8 10 * this list of conditions and the following disclaimer.
ashleymills 0:5a29fd060ac8 11 * * Redistributions in binary form must reproduce the above copyright notice,
ashleymills 0:5a29fd060ac8 12 * this list of conditions and the following disclaimer in the documentation
ashleymills 0:5a29fd060ac8 13 * and/or other materials provided with the distribution.
ashleymills 0:5a29fd060ac8 14 * * Neither the name of the axTLS project nor the names of its contributors
ashleymills 0:5a29fd060ac8 15 * may be used to endorse or promote products derived from this software
ashleymills 0:5a29fd060ac8 16 * without specific prior written permission.
ashleymills 0:5a29fd060ac8 17 *
ashleymills 0:5a29fd060ac8 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
ashleymills 0:5a29fd060ac8 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
ashleymills 0:5a29fd060ac8 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
ashleymills 0:5a29fd060ac8 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
ashleymills 0:5a29fd060ac8 22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
ashleymills 0:5a29fd060ac8 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ashleymills 0:5a29fd060ac8 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
ashleymills 0:5a29fd060ac8 25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ashleymills 0:5a29fd060ac8 26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ashleymills 0:5a29fd060ac8 27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ashleymills 0:5a29fd060ac8 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ashleymills 0:5a29fd060ac8 29 */
ashleymills 0:5a29fd060ac8 30
ashleymills 0:5a29fd060ac8 31 /**
ashleymills 0:5a29fd060ac8 32 * @file crypto.h
ashleymills 0:5a29fd060ac8 33 */
ashleymills 0:5a29fd060ac8 34
ashleymills 0:5a29fd060ac8 35 #ifndef HEADER_CRYPTO_H
ashleymills 0:5a29fd060ac8 36 #define HEADER_CRYPTO_H
ashleymills 0:5a29fd060ac8 37
ashleymills 0:5a29fd060ac8 38 #ifdef __cplusplus
ashleymills 0:5a29fd060ac8 39 extern "C" {
ashleymills 0:5a29fd060ac8 40 #endif
ashleymills 0:5a29fd060ac8 41
ashleymills 0:5a29fd060ac8 42 #include "bigint_impl.h"
ashleymills 0:5a29fd060ac8 43 #include "bigint.h"
ashleymills 0:5a29fd060ac8 44
ashleymills 0:5a29fd060ac8 45 #ifndef STDCALL
ashleymills 0:5a29fd060ac8 46 #define STDCALL
ashleymills 0:5a29fd060ac8 47 #endif
ashleymills 0:5a29fd060ac8 48 #ifndef EXP_FUNC
ashleymills 0:5a29fd060ac8 49 #define EXP_FUNC
ashleymills 0:5a29fd060ac8 50 #endif
ashleymills 0:5a29fd060ac8 51
ashleymills 0:5a29fd060ac8 52
ashleymills 0:5a29fd060ac8 53 /* enable features based on a 'super-set' capbaility. */
ashleymills 0:5a29fd060ac8 54 #if defined(CONFIG_SSL_FULL_MODE)
ashleymills 0:5a29fd060ac8 55 #define CONFIG_SSL_ENABLE_CLIENT
ashleymills 0:5a29fd060ac8 56 #define CONFIG_SSL_CERT_VERIFICATION
ashleymills 0:5a29fd060ac8 57 #elif defined(CONFIG_SSL_ENABLE_CLIENT)
ashleymills 0:5a29fd060ac8 58 #define CONFIG_SSL_CERT_VERIFICATION
ashleymills 0:5a29fd060ac8 59 #endif
ashleymills 0:5a29fd060ac8 60
ashleymills 0:5a29fd060ac8 61 /**************************************************************************
ashleymills 0:5a29fd060ac8 62 * AES declarations
ashleymills 0:5a29fd060ac8 63 **************************************************************************/
ashleymills 0:5a29fd060ac8 64
ashleymills 0:5a29fd060ac8 65 #define AES_MAXROUNDS 14
ashleymills 0:5a29fd060ac8 66 #define AES_BLOCKSIZE 16
ashleymills 0:5a29fd060ac8 67 #define AES_IV_SIZE 16
ashleymills 0:5a29fd060ac8 68
ashleymills 0:5a29fd060ac8 69 typedef struct aes_key_st
ashleymills 0:5a29fd060ac8 70 {
ashleymills 0:5a29fd060ac8 71 uint16_t rounds;
ashleymills 0:5a29fd060ac8 72 uint16_t key_size;
ashleymills 0:5a29fd060ac8 73 uint32_t ks[(AES_MAXROUNDS+1)*8];
ashleymills 0:5a29fd060ac8 74 uint8_t iv[AES_IV_SIZE];
ashleymills 0:5a29fd060ac8 75 } AES_CTX;
ashleymills 0:5a29fd060ac8 76
ashleymills 0:5a29fd060ac8 77 typedef enum
ashleymills 0:5a29fd060ac8 78 {
ashleymills 0:5a29fd060ac8 79 AES_MODE_128,
ashleymills 0:5a29fd060ac8 80 AES_MODE_256
ashleymills 0:5a29fd060ac8 81 } AES_MODE;
ashleymills 0:5a29fd060ac8 82
ashleymills 0:5a29fd060ac8 83 void AES_set_key(AES_CTX *ctx, const uint8_t *key,
ashleymills 0:5a29fd060ac8 84 const uint8_t *iv, AES_MODE mode);
ashleymills 0:5a29fd060ac8 85 void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
ashleymills 0:5a29fd060ac8 86 uint8_t *out, int length);
ashleymills 0:5a29fd060ac8 87 void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
ashleymills 0:5a29fd060ac8 88 void AES_convert_key(AES_CTX *ctx);
ashleymills 0:5a29fd060ac8 89
ashleymills 0:5a29fd060ac8 90 /**************************************************************************
ashleymills 0:5a29fd060ac8 91 * RC4 declarations
ashleymills 0:5a29fd060ac8 92 **************************************************************************/
ashleymills 0:5a29fd060ac8 93
ashleymills 0:5a29fd060ac8 94 typedef struct
ashleymills 0:5a29fd060ac8 95 {
ashleymills 0:5a29fd060ac8 96 uint8_t x, y, m[256];
ashleymills 0:5a29fd060ac8 97 } RC4_CTX;
ashleymills 0:5a29fd060ac8 98
ashleymills 0:5a29fd060ac8 99 void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
ashleymills 0:5a29fd060ac8 100 void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
ashleymills 0:5a29fd060ac8 101
ashleymills 0:5a29fd060ac8 102 /**************************************************************************
ashleymills 0:5a29fd060ac8 103 * SHA1 declarations
ashleymills 0:5a29fd060ac8 104 **************************************************************************/
ashleymills 0:5a29fd060ac8 105
ashleymills 0:5a29fd060ac8 106 #define SHA1_SIZE 20
ashleymills 0:5a29fd060ac8 107
ashleymills 0:5a29fd060ac8 108 /*
ashleymills 0:5a29fd060ac8 109 * This structure will hold context information for the SHA-1
ashleymills 0:5a29fd060ac8 110 * hashing operation
ashleymills 0:5a29fd060ac8 111 */
ashleymills 0:5a29fd060ac8 112 typedef struct
ashleymills 0:5a29fd060ac8 113 {
ashleymills 0:5a29fd060ac8 114 uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
ashleymills 0:5a29fd060ac8 115 uint32_t Length_Low; /* Message length in bits */
ashleymills 0:5a29fd060ac8 116 uint32_t Length_High; /* Message length in bits */
ashleymills 0:5a29fd060ac8 117 uint16_t Message_Block_Index; /* Index into message block array */
ashleymills 0:5a29fd060ac8 118 uint8_t Message_Block[64]; /* 512-bit message blocks */
ashleymills 0:5a29fd060ac8 119 } SHA1_CTX;
ashleymills 0:5a29fd060ac8 120
ashleymills 0:5a29fd060ac8 121 void SHA1_Init(SHA1_CTX *);
ashleymills 0:5a29fd060ac8 122 void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len);
ashleymills 0:5a29fd060ac8 123 void SHA1_Final(uint8_t *digest, SHA1_CTX *);
ashleymills 0:5a29fd060ac8 124
ashleymills 0:5a29fd060ac8 125 /**************************************************************************
ashleymills 0:5a29fd060ac8 126 * MD2 declarations
ashleymills 0:5a29fd060ac8 127 **************************************************************************/
ashleymills 0:5a29fd060ac8 128
ashleymills 0:5a29fd060ac8 129 #define MD2_SIZE 16
ashleymills 0:5a29fd060ac8 130
ashleymills 0:5a29fd060ac8 131 typedef struct
ashleymills 0:5a29fd060ac8 132 {
ashleymills 0:5a29fd060ac8 133 unsigned char cksum[16]; /* checksum of the data block */
ashleymills 0:5a29fd060ac8 134 unsigned char state[48]; /* intermediate digest state */
ashleymills 0:5a29fd060ac8 135 unsigned char buffer[16]; /* data block being processed */
ashleymills 0:5a29fd060ac8 136 int left; /* amount of data in buffer */
ashleymills 0:5a29fd060ac8 137 } MD2_CTX;
ashleymills 0:5a29fd060ac8 138
ashleymills 0:5a29fd060ac8 139 EXP_FUNC void STDCALL MD2_Init(MD2_CTX *ctx);
ashleymills 0:5a29fd060ac8 140 EXP_FUNC void STDCALL MD2_Update(MD2_CTX *ctx, const uint8_t *input, int ilen);
ashleymills 0:5a29fd060ac8 141 EXP_FUNC void STDCALL MD2_Final(uint8_t *digest, MD2_CTX *ctx);
ashleymills 0:5a29fd060ac8 142
ashleymills 0:5a29fd060ac8 143 /**************************************************************************
ashleymills 0:5a29fd060ac8 144 * MD5 declarations
ashleymills 0:5a29fd060ac8 145 **************************************************************************/
ashleymills 0:5a29fd060ac8 146
ashleymills 0:5a29fd060ac8 147 #define MD5_SIZE 16
ashleymills 0:5a29fd060ac8 148
ashleymills 0:5a29fd060ac8 149 typedef struct
ashleymills 0:5a29fd060ac8 150 {
ashleymills 0:5a29fd060ac8 151 uint32_t state[4]; /* state (ABCD) */
ashleymills 0:5a29fd060ac8 152 uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
ashleymills 0:5a29fd060ac8 153 uint8_t buffer[64]; /* input buffer */
ashleymills 0:5a29fd060ac8 154 } MD5_CTX;
ashleymills 0:5a29fd060ac8 155
ashleymills 0:5a29fd060ac8 156 EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
ashleymills 0:5a29fd060ac8 157 EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
ashleymills 0:5a29fd060ac8 158 EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
ashleymills 0:5a29fd060ac8 159
ashleymills 0:5a29fd060ac8 160 /**************************************************************************
ashleymills 0:5a29fd060ac8 161 * HMAC declarations
ashleymills 0:5a29fd060ac8 162 **************************************************************************/
ashleymills 0:5a29fd060ac8 163 void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
ashleymills 0:5a29fd060ac8 164 int key_len, uint8_t *digest);
ashleymills 0:5a29fd060ac8 165 void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
ashleymills 0:5a29fd060ac8 166 int key_len, uint8_t *digest);
ashleymills 0:5a29fd060ac8 167
ashleymills 0:5a29fd060ac8 168 /**************************************************************************
ashleymills 0:5a29fd060ac8 169 * RSA declarations
ashleymills 0:5a29fd060ac8 170 **************************************************************************/
ashleymills 0:5a29fd060ac8 171
ashleymills 0:5a29fd060ac8 172 typedef struct
ashleymills 0:5a29fd060ac8 173 {
ashleymills 0:5a29fd060ac8 174 bigint *m; /* modulus */
ashleymills 0:5a29fd060ac8 175 bigint *e; /* public exponent */
ashleymills 0:5a29fd060ac8 176 bigint *d; /* private exponent */
ashleymills 0:5a29fd060ac8 177 #ifdef CONFIG_BIGINT_CRT
ashleymills 0:5a29fd060ac8 178 bigint *p; /* p as in m = pq */
ashleymills 0:5a29fd060ac8 179 bigint *q; /* q as in m = pq */
ashleymills 0:5a29fd060ac8 180 bigint *dP; /* d mod (p-1) */
ashleymills 0:5a29fd060ac8 181 bigint *dQ; /* d mod (q-1) */
ashleymills 0:5a29fd060ac8 182 bigint *qInv; /* q^-1 mod p */
ashleymills 0:5a29fd060ac8 183 #endif
ashleymills 0:5a29fd060ac8 184 int num_octets;
ashleymills 0:5a29fd060ac8 185 BI_CTX *bi_ctx;
ashleymills 0:5a29fd060ac8 186 } RSA_CTX;
ashleymills 0:5a29fd060ac8 187
ashleymills 0:5a29fd060ac8 188 void RSA_priv_key_new(RSA_CTX **rsa_ctx,
ashleymills 0:5a29fd060ac8 189 const uint8_t *modulus, int mod_len,
ashleymills 0:5a29fd060ac8 190 const uint8_t *pub_exp, int pub_len,
ashleymills 0:5a29fd060ac8 191 const uint8_t *priv_exp, int priv_len
ashleymills 0:5a29fd060ac8 192 #ifdef CONFIG_BIGINT_CRT
ashleymills 0:5a29fd060ac8 193 , const uint8_t *p, int p_len,
ashleymills 0:5a29fd060ac8 194 const uint8_t *q, int q_len,
ashleymills 0:5a29fd060ac8 195 const uint8_t *dP, int dP_len,
ashleymills 0:5a29fd060ac8 196 const uint8_t *dQ, int dQ_len,
ashleymills 0:5a29fd060ac8 197 const uint8_t *qInv, int qInv_len
ashleymills 0:5a29fd060ac8 198 #endif
ashleymills 0:5a29fd060ac8 199 );
ashleymills 0:5a29fd060ac8 200 void RSA_pub_key_new(RSA_CTX **rsa_ctx,
ashleymills 0:5a29fd060ac8 201 const uint8_t *modulus, int mod_len,
ashleymills 0:5a29fd060ac8 202 const uint8_t *pub_exp, int pub_len);
ashleymills 0:5a29fd060ac8 203 void RSA_free(RSA_CTX *ctx);
ashleymills 0:5a29fd060ac8 204 int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
ashleymills 0:5a29fd060ac8 205 int is_decryption);
ashleymills 0:5a29fd060ac8 206 bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
ashleymills 0:5a29fd060ac8 207 #if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
ashleymills 0:5a29fd060ac8 208 bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
ashleymills 0:5a29fd060ac8 209 bigint *modulus, bigint *pub_exp);
ashleymills 0:5a29fd060ac8 210 bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
ashleymills 0:5a29fd060ac8 211 int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
ashleymills 0:5a29fd060ac8 212 uint8_t *out_data, int is_signing);
ashleymills 0:5a29fd060ac8 213 void RSA_print(const RSA_CTX *ctx);
ashleymills 0:5a29fd060ac8 214 #endif
ashleymills 0:5a29fd060ac8 215
ashleymills 0:5a29fd060ac8 216 /**************************************************************************
ashleymills 0:5a29fd060ac8 217 * RNG declarations
ashleymills 0:5a29fd060ac8 218 **************************************************************************/
ashleymills 0:5a29fd060ac8 219 EXP_FUNC void STDCALL RNG_initialize(void);
ashleymills 0:5a29fd060ac8 220 EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
ashleymills 0:5a29fd060ac8 221 EXP_FUNC void STDCALL RNG_terminate(void);
ashleymills 0:5a29fd060ac8 222 EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
ashleymills 0:5a29fd060ac8 223 void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
ashleymills 0:5a29fd060ac8 224
ashleymills 0:5a29fd060ac8 225 #ifdef __cplusplus
ashleymills 0:5a29fd060ac8 226 }
ashleymills 0:5a29fd060ac8 227 #endif
ashleymills 0:5a29fd060ac8 228
ashleymills 0:5a29fd060ac8 229 #endif