Imported Upstream version 0.9.0
[deb_shairplay.git] / src / lib / crypto / crypto.h
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
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 "config.h"
43 #include "bigint_impl.h"
44 #include "bigint.h"
45
46 #ifndef STDCALL
47 #define STDCALL
48 #endif
49 #ifndef EXP_FUNC
50 #define EXP_FUNC
51 #endif
52
53 /**************************************************************************
54 * AES declarations
55 **************************************************************************/
56
57 #define AES_MAXROUNDS 14
58 #define AES_BLOCKSIZE 16
59 #define AES_IV_SIZE 16
60
61 typedef struct aes_key_st
62 {
63 uint16_t rounds;
64 uint16_t key_size;
65 uint32_t ks[(AES_MAXROUNDS+1)*8];
66 uint8_t iv[AES_IV_SIZE];
67 } AES_CTX;
68
69 typedef enum
70 {
71 AES_MODE_128,
72 AES_MODE_256
73 } AES_MODE;
74
75 void AES_set_key(AES_CTX *ctx, const uint8_t *key,
76 const uint8_t *iv, AES_MODE mode);
77 void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg,
78 uint8_t *out, int length);
79 void AES_cbc_decrypt(AES_CTX *ks, const uint8_t *in, uint8_t *out, int length);
80 void AES_convert_key(AES_CTX *ctx);
81
82 /**************************************************************************
83 * RC4 declarations
84 **************************************************************************/
85
86 typedef struct
87 {
88 uint8_t x, y, m[256];
89 } RC4_CTX;
90
91 void RC4_setup(RC4_CTX *s, const uint8_t *key, int length);
92 void RC4_crypt(RC4_CTX *s, const uint8_t *msg, uint8_t *data, int length);
93
94 /**************************************************************************
95 * SHA1 declarations
96 **************************************************************************/
97
98 #define SHA1_SIZE 20
99
100 /*
101 * This structure will hold context information for the SHA-1
102 * hashing operation
103 */
104 typedef struct
105 {
106 uint32_t Intermediate_Hash[SHA1_SIZE/4]; /* Message Digest */
107 uint32_t Length_Low; /* Message length in bits */
108 uint32_t Length_High; /* Message length in bits */
109 uint16_t Message_Block_Index; /* Index into message block array */
110 uint8_t Message_Block[64]; /* 512-bit message blocks */
111 } SHA1_CTX;
112
113 void SHA1_Init(SHA1_CTX *);
114 void SHA1_Update(SHA1_CTX *, const uint8_t * msg, int len);
115 void SHA1_Final(uint8_t *digest, SHA1_CTX *);
116
117 /**************************************************************************
118 * MD5 declarations
119 **************************************************************************/
120
121 #define MD5_SIZE 16
122
123 typedef struct
124 {
125 uint32_t state[4]; /* state (ABCD) */
126 uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
127 uint8_t buffer[64]; /* input buffer */
128 } MD5_CTX;
129
130 EXP_FUNC void STDCALL MD5_Init(MD5_CTX *);
131 EXP_FUNC void STDCALL MD5_Update(MD5_CTX *, const uint8_t *msg, int len);
132 EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *);
133
134 /**************************************************************************
135 * HMAC declarations
136 **************************************************************************/
137 void hmac_md5(const uint8_t *msg, int length, const uint8_t *key,
138 int key_len, uint8_t *digest);
139 void hmac_sha1(const uint8_t *msg, int length, const uint8_t *key,
140 int key_len, uint8_t *digest);
141
142 #ifdef __cplusplus
143 }
144 #endif
145
146 #endif