Debian Codegraph @

ccast/axTLS/crypto.h

raw symbols SCIP
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
39 extern "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 defined(CONFIG_SSL_FULL_MODE)
55 #define CONFIG_SSL_ENABLE_CLIENT
56 //#define CONFIG_SSL_CERT_VERIFICATION
57 #elif defined(CONFIG_SSL_ENABLE_CLIENT)
58 //#define CONFIG_SSL_CERT_VERIFICATION // GWG
59 #endif
60
61 /**************************************************************************
62 * AES declarations
63 **************************************************************************/
64
65 #define AES_MAXROUNDS 14
66 #define AES_BLOCKSIZE 16
67 #define AES_IV_SIZE 16
68
69 typedef struct aes_key_st
70 {
71 uint16_t rounds;
72 uint16_t key_size;
73 uint32_t ks[(AES_MAXROUNDS+1)*8];
74 uint8_t iv[AES_IV_SIZE];
75 } AES_CTX;
76
77 typedef enum
78 {
79 AES_MODE_128,
80 AES_MODE_256
81 } AES_MODE;
82
83 void AES_set_key(AES_CTX *ctx, const uint8_t *key,
84 const uint8_t *iv, AES_MODE mode);
85 void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
86 uint8_t *out, int length);
87 void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
88 void AES_convert_key(AES_CTX *ctx);
89
90 /**************************************************************************
91 * RC4 declarations
92 **************************************************************************/
93
94 typedef struct
95 {
96 uint8_t x, y, m[256];
97 } RC4_CTX;
98
99 void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
100 void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
101
102 /**************************************************************************
103 * SHA1 declarations
104 **************************************************************************/
105
106 #define SHA1_SIZE 20
107
108 /*
109 * This structure will hold context information for the SHA-1
110 * hashing operation
111 */
112 typedef struct
113 {
114 uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
115 uint32_t Length_Low; /* Message length in bits */
116 uint32_t Length_High; /* Message length in bits */
117 uint16_t Message_Block_Index; /* Index into message block array */
118 uint8_t Message_Block[64]; /* 512-bit message blocks */
119 } SHA1_CTX;
120
121 void SHA1_Init(SHA1_CTX *);
122 void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len);
123 void SHA1_Final(uint8_t *digest, SHA1_CTX *);
124
125 /**************************************************************************
126 * SHA256 declarations
127 **************************************************************************/
128
129 #define SHA256_SIZE 32
130
131 typedef struct
132 {
133 uint32_t total[2];
134 uint32_t state[8];
135 uint8_t buffer[64];
136 } SHA256_CTX;
137
138 void SHA256_Init(SHA256_CTX *c);
139 void SHA256_Update(SHA256_CTX *, const uint8_t *input, int len);
140 void SHA256_Final(uint8_t *digest, SHA256_CTX *);
141
142 /**************************************************************************
143 * SHA512 declarations
144 **************************************************************************/
145
146 #define SHA512_SIZE 64
147
148 typedef struct
149 {
150 union
151 {
152 uint64_t h[8];
153 uint8_t digest[64];
154 } h_dig;
155 union
156 {
157 uint64_t w[80];
158 uint8_t buffer[128];
159 } w_buf;
160 size_t size;
161 uint64_t totalSize;
162 } SHA512_CTX;
163
164 void SHA512_Init(SHA512_CTX *c);
165 void SHA512_Update(SHA512_CTX *, const uint8_t *input, int len);
166 void SHA512_Final(uint8_t *digest, SHA512_CTX *);
167
168 /**************************************************************************
169 * SHA384 declarations
170 **************************************************************************/
171
172 #define SHA384_SIZE 48
173
174 typedef SHA512_CTX SHA384_CTX;
175 void SHA384_Init(SHA384_CTX *c);
176 void SHA384_Update(SHA384_CTX *, const uint8_t *input, int len);
177 void SHA384_Final(uint8_t *digest, SHA384_CTX *);
178
179 /**************************************************************************
180 * MD5 declarations
181 **************************************************************************/
182
183 #define MD5_SIZE 16
184
185 typedef struct
186 {
187 uint32_t state[4]; /* state (ABCD) */
188 uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
189 uint8_t buffer[64]; /* input buffer */
190 } MD5_CTX;
191
192 EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
193 EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
194 EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
195
196 /**************************************************************************
197 * HMAC declarations
198 **************************************************************************/
199 void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
200 int key_len, uint8_t *digest);
201 void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
202 int key_len, uint8_t *digest);
203 void hmac_sha256(const uint8_t *msg, int length, const uint8_t *key,
204 int key_len, uint8_t *digest);
205
206 /**************************************************************************
207 * RSA declarations
208 **************************************************************************/
209
210 typedef struct
211 {
212 bigint *m; /* modulus */
213 bigint *e; /* public exponent */
214 bigint *d; /* private exponent */
215 #ifdef CONFIG_BIGINT_CRT
216 bigint *p; /* p as in m = pq */
217 bigint *q; /* q as in m = pq */
218 bigint *dP; /* d mod (p-1) */
219 bigint *dQ; /* d mod (q-1) */
220 bigint *qInv; /* q^-1 mod p */
221 #endif
222 int num_octets;
223 BI_CTX *bi_ctx;
224 } RSA_CTX;
225
226 void RSA_priv_key_new(RSA_CTX **rsa_ctx,
227 const uint8_t *modulus, int mod_len,
228 const uint8_t *pub_exp, int pub_len,
229 const uint8_t *priv_exp, int priv_len
230 #ifdef CONFIG_BIGINT_CRT
231 , const uint8_t *p, int p_len,
232 const uint8_t *q, int q_len,
233 const uint8_t *dP, int dP_len,
234 const uint8_t *dQ, int dQ_len,
235 const uint8_t *qInv, int qInv_len
236 #endif
237 );
238 void RSA_pub_key_new(RSA_CTX **rsa_ctx,
239 const uint8_t *modulus, int mod_len,
240 const uint8_t *pub_exp, int pub_len);
241 void RSA_free(RSA_CTX *ctx);
242 int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
243 int out_len, int is_decryption);
244 int RSA_decrypt2(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data); // GWG
245 bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
246 #if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
247 bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
248 bigint *modulus, bigint *pub_exp);
249 #endif
250 #ifdef CONFIG_DEBUG
251 void RSA_print(const RSA_CTX *ctx);
252 #endif
253 int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
254 uint8_t *out_data, int is_signing);
255 bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
256
257 /**************************************************************************
258 * RNG declarations
259 **************************************************************************/
260 EXP_FUNC void STDCALL RNG_initialize(void);
261 EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
262 EXP_FUNC void STDCALL RNG_terminate(void);
263 EXP_FUNC int STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
264 int get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
265
266 #ifdef __cplusplus
267 }
268 #endif
269
270 #endif