kodi
crypto.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, 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 
35 #ifndef HEADER_CRYPTO_H
36 #define HEADER_CRYPTO_H
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #include "config.h"
43 #include "bigint_impl.h"
44 #include "bigint.h"
45 
46 /* enable features based on a 'super-set' capbaility. */
47 #if defined(CONFIG_SSL_FULL_MODE)
48 #define CONFIG_SSL_ENABLE_CLIENT
49 #define CONFIG_SSL_CERT_VERIFICATION
50 #elif defined(CONFIG_SSL_ENABLE_CLIENT)
51 #define CONFIG_SSL_CERT_VERIFICATION
52 #endif
53 
54 /**************************************************************************
55  * AES declarations
56  **************************************************************************/
57 
58 #define AES_MAXROUNDS 14
59 #define AES_BLOCKSIZE 16
60 #define AES_IV_SIZE 16
61 
62 typedef struct aes_key_st
63 {
64  uint16_t rounds;
65  uint16_t key_size;
66  uint32_t ks[(AES_MAXROUNDS+1)*8];
67  uint8_t iv[AES_IV_SIZE];
68 } AES_CTX;
69 
70 typedef enum
71 {
72  AES_MODE_128,
73  AES_MODE_256
74 } AES_MODE;
75 
76 void AES_set_key(AES_CTX *ctx, const uint8_t *key,
77  const uint8_t *iv, AES_MODE mode);
78 void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
79  uint8_t *out, int length);
80 void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
81 void AES_convert_key(AES_CTX *ctx);
82 
83 /**************************************************************************
84  * RC4 declarations
85  **************************************************************************/
86 
87 typedef struct
88 {
89  uint8_t x, y, m[256];
90 } RC4_CTX;
91 
92 void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
93 void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
94 
95 /**************************************************************************
96  * SHA256 declarations
97  **************************************************************************/
98 #define SHA256_SIZE 32
99 
100 /**************************************************************************
101  * SHA1 declarations
102  **************************************************************************/
103 
104 #define SHA1_SIZE 20
105 
106 /*
107  * This structure will hold context information for the SHA-1
108  * hashing operation
109  */
110 typedef struct
111 {
112  uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
113  uint32_t Length_Low; /* Message length in bits */
114  uint32_t Length_High; /* Message length in bits */
115  uint16_t Message_Block_Index; /* Index into message block array */
116  uint8_t Message_Block[64]; /* 512-bit message blocks */
117 } SHA1_CTX;
118 
119 void SHA1_Init(SHA1_CTX *);
120 void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len);
121 void SHA1_Final(uint8_t *digest, SHA1_CTX *);
122 
123 /**************************************************************************
124  * MD2 declarations
125  **************************************************************************/
126 
127 #define MD2_SIZE 16
128 
129 typedef struct
130 {
131  unsigned char cksum[16]; /* checksum of the data block */
132  unsigned char state[48]; /* intermediate digest state */
133  unsigned char buffer[16]; /* data block being processed */
134  int left; /* amount of data in buffer */
135 } MD2_CTX;
136 
137 EXP_FUNC void STDCALL MD2_Init(MD2_CTX *ctx);
138 EXP_FUNC void STDCALL MD2_Update(MD2_CTX *ctx, const uint8_t *input, int ilen);
139 EXP_FUNC void STDCALL MD2_Final(uint8_t *digest, MD2_CTX *ctx);
140 
141 /**************************************************************************
142  * MD5 declarations
143  **************************************************************************/
144 
145 #define MD5_SIZE 16
146 
147 typedef struct
148 {
149  uint32_t state[4]; /* state (ABCD) */
150  uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
151  uint8_t buffer[64]; /* input buffer */
152 } MD5_CTX;
153 
154 EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
155 EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
156 EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
157 
158 /**************************************************************************
159  * HMAC declarations
160  **************************************************************************/
161 void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
162  int key_len, uint8_t *digest);
163 void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
164  int key_len, uint8_t *digest);
165 
166 /**************************************************************************
167  * RSA declarations
168  **************************************************************************/
169 
170 typedef struct
171 {
172  bigint *m; /* modulus */
173  bigint *e; /* public exponent */
174  bigint *d; /* private exponent */
175 #ifdef CONFIG_BIGINT_CRT
176  bigint *p; /* p as in m = pq */
177  bigint *q; /* q as in m = pq */
178  bigint *dP; /* d mod (p-1) */
179  bigint *dQ; /* d mod (q-1) */
180  bigint *qInv; /* q^-1 mod p */
181 #endif
182  int num_octets;
183  BI_CTX *bi_ctx;
184 } RSA_CTX;
185 
186 void RSA_priv_key_new(RSA_CTX **rsa_ctx,
187  const uint8_t *modulus, int mod_len,
188  const uint8_t *pub_exp, int pub_len,
189  const uint8_t *priv_exp, int priv_len
190 #ifdef CONFIG_BIGINT_CRT
191  , const uint8_t *p, int p_len,
192  const uint8_t *q, int q_len,
193  const uint8_t *dP, int dP_len,
194  const uint8_t *dQ, int dQ_len,
195  const uint8_t *qInv, int qInv_len
196 #endif
197  );
198 void RSA_pub_key_new(RSA_CTX **rsa_ctx,
199  const uint8_t *modulus, int mod_len,
200  const uint8_t *pub_exp, int pub_len);
201 void RSA_free(RSA_CTX *ctx);
202 int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data,
203  int is_decryption);
204 bigint *RSA_private(const RSA_CTX *c, bigint *bi_msg);
205 #if defined(CONFIG_SSL_CERT_VERIFICATION) || defined(CONFIG_SSL_GENERATE_X509_CERT)
206 bigint *RSA_sign_verify(BI_CTX *ctx, const uint8_t *sig, int sig_len,
207  bigint *modulus, bigint *pub_exp);
208 bigint *RSA_public(const RSA_CTX * c, bigint *bi_msg);
209 int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
210  uint8_t *out_data, int is_signing);
211 void RSA_print(const RSA_CTX *ctx);
212 #endif
213 
214 /**************************************************************************
215  * RNG declarations
216  **************************************************************************/
217 EXP_FUNC void STDCALL RNG_initialize(void);
218 EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size);
219 EXP_FUNC void STDCALL RNG_terminate(void);
220 EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data);
221 void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 #endif
void SHA1_Final(uint8_t *digest, SHA1_CTX *)
Return the 160-bit message digest into the user's array.
Definition: sha1.c:88
EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len)
Accepts an array of octets as the next portion of the message.
Definition: md5.c:121
EXP_FUNC void STDCALL get_random(int num_rand_bytes, uint8_t *rand_data)
Set a series of bytes with a random number.
Definition: crypto_misc.c:192
EXP_FUNC void STDCALL MD5_Init(MD5_CTX *)
MD5 initialization - begins an MD5 operation, writing a new ctx.
Definition: md5.c:106
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
Set a series of bytes with a random number.
Definition: crypto_misc.c:239
void RC4_setup(RC4_CTX *s, const uint8_t *key, int length)
An implementation of the RC4/ARC4 algorithm.
Definition: rc4.c:43
void RSA_free(RSA_CTX *ctx)
Free up any RSA context resources.
Definition: rsa.c:102
EXP_FUNC void STDCALL RNG_terminate(void)
Terminate the RNG engine.
Definition: crypto_misc.c:180
int RSA_decrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint8_t *out_data, int is_decryption)
Use PKCS1.5 for decryption/verification.
Definition: rsa.c:143
void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key, int key_len, uint8_t *digest)
Perform HMAC-SHA1 NOTE: does not handle keys larger than the block size.
Definition: hmac.c:78
void hmac_md5(const uint8_t *msg, int length, const uint8_t *key, int key_len, uint8_t *digest)
HMAC implementation - This code was originally taken from RFC2104 See http://www.ietf.org/rfc/rfc2104.txt and http://www.faqs.org/rfcs/rfc2202.html.
Definition: hmac.c:45
void SHA1_Init(SHA1_CTX *)
Initialize the SHA1 context.
Definition: sha1.c:53
bigint * RSA_private(const RSA_CTX *c, bigint *bi_msg)
Performs m = c^d mod n.
Definition: rsa.c:192
Definition: crypto.h:170
void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
Encrypt a byte sequence (with a block size 16) using the AES cipher.
Definition: aes.c:284
Definition: crypto.h:62
EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *)
Return the 128-bit message digest into the user's array.
Definition: md5.c:157
void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length)
Decrypt a byte sequence (with a block size 16) using the AES cipher.
Definition: aes.c:323
void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length)
Perform the encrypt/decrypt operation (can use it for either since this is a stream cipher)...
Definition: rc4.c:72
Definition: crypto.h:87
void AES_set_key(AES_CTX *ctx, const uint8_t *key, const uint8_t *iv, AES_MODE mode)
Set up AES with the key/iv and cipher size.
Definition: aes.c:190
EXP_FUNC void STDCALL RNG_initialize(void)
Initialise the Random Number Generator engine.
Definition: crypto_misc.c:134
Definition: crypto.h:110
EXP_FUNC void STDCALL RNG_custom_init(const uint8_t *seed_buf, int size)
If no /dev/urandom, then initialise the RNG with something interesting.
Definition: crypto_misc.c:164
void AES_convert_key(AES_CTX *ctx)
Change a key for decryption.
Definition: aes.c:265
A big integer basic object.
Definition: bigint_impl.h:83
void SHA1_Update(SHA1_CTX *, const uint8_t *msg, int len)
Accepts an array of octets as the next portion of the message.
Definition: sha1.c:68
Definition: crypto.h:129
Definition: crypto.h:147
void RSA_priv_key_new(RSA_CTX **rsa_ctx, const uint8_t *modulus, int mod_len, const uint8_t *pub_exp, int pub_len, const uint8_t *priv_exp, int priv_len)
Implements the RSA public encryption algorithm.
Definition: rsa.c:43
Maintains the state of the cache, and a number of variables used in reduction.
Definition: bigint_impl.h:98