| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 54 |
#if defined(CONFIG_SSL_FULL_MODE) |
| 55 |
#define CONFIG_SSL_ENABLE_CLIENT |
| 56 |
|
| 57 |
#elif defined(CONFIG_SSL_ENABLE_CLIENT) |
| 58 |
|
| 59 |
#endif |
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 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 |
|
| 104 |
|
| 105 |
|
| 106 |
#define SHA1_SIZE 20 |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
typedef struct |
| 113 |
{ |
| 114 |
uint32_t Intermediate_Hash[SHA1_SIZE/4]; |
| 115 |
uint32_t Length_Low; |
| 116 |
uint32_t Length_High; |
| 117 |
uint16_t Message_Block_Index; |
| 118 |
uint8_t Message_Block[64]; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
#define MD5_SIZE 16 |
| 184 |
|
| 185 |
typedef struct |
| 186 |
{ |
| 187 |
uint32_t state[4]; |
| 188 |
uint32_t count[2]; |
| 189 |
uint8_t buffer[64]; |
| 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 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
typedef struct |
| 211 |
{ |
| 212 |
bigint *m; |
| 213 |
bigint *e; |
| 214 |
bigint *d; |
| 215 |
#ifdef CONFIG_BIGINT_CRT |
| 216 |
bigint *p; |
| 217 |
bigint *q; |
| 218 |
bigint *dP; |
| 219 |
bigint *dQ; |
| 220 |
bigint *qInv; |
| 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); |
| 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 |
|
| 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 |