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