Mercurial > dropbear
comparison src/headers/tomcrypt_mac.h @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
1 #ifdef HMAC | |
2 typedef struct Hmac_state { | |
3 hash_state md; | |
4 int hash; | |
5 hash_state hashstate; | |
6 unsigned char *key; | |
7 } hmac_state; | |
8 | |
9 int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned long keylen); | |
10 int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen); | |
11 int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen); | |
12 int hmac_test(void); | |
13 int hmac_memory(int hash, | |
14 const unsigned char *key, unsigned long keylen, | |
15 const unsigned char *in, unsigned long inlen, | |
16 unsigned char *out, unsigned long *outlen); | |
17 int hmac_memory_multi(int hash, | |
18 const unsigned char *key, unsigned long keylen, | |
19 unsigned char *out, unsigned long *outlen, | |
20 const unsigned char *in, unsigned long inlen, ...); | |
21 int hmac_file(int hash, const char *fname, const unsigned char *key, | |
22 unsigned long keylen, | |
23 unsigned char *dst, unsigned long *dstlen); | |
24 #endif | |
25 | |
26 #ifdef OMAC | |
27 | |
28 typedef struct { | |
29 int cipher_idx, | |
30 buflen, | |
31 blklen; | |
32 unsigned char block[MAXBLOCKSIZE], | |
33 prev[MAXBLOCKSIZE], | |
34 Lu[2][MAXBLOCKSIZE]; | |
35 symmetric_key key; | |
36 } omac_state; | |
37 | |
38 int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen); | |
39 int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen); | |
40 int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen); | |
41 int omac_memory(int cipher, | |
42 const unsigned char *key, unsigned long keylen, | |
43 const unsigned char *in, unsigned long inlen, | |
44 unsigned char *out, unsigned long *outlen); | |
45 int omac_memory_multi(int cipher, | |
46 const unsigned char *key, unsigned long keylen, | |
47 unsigned char *out, unsigned long *outlen, | |
48 const unsigned char *in, unsigned long inlen, ...); | |
49 int omac_file(int cipher, | |
50 const unsigned char *key, unsigned long keylen, | |
51 const char *filename, | |
52 unsigned char *out, unsigned long *outlen); | |
53 int omac_test(void); | |
54 #endif /* OMAC */ | |
55 | |
56 #ifdef PMAC | |
57 | |
58 typedef struct { | |
59 unsigned char Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */ | |
60 Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */ | |
61 Lr[MAXBLOCKSIZE], /* L * x^-1 */ | |
62 block[MAXBLOCKSIZE], /* currently accumulated block */ | |
63 checksum[MAXBLOCKSIZE]; /* current checksum */ | |
64 | |
65 symmetric_key key; /* scheduled key for cipher */ | |
66 unsigned long block_index; /* index # for current block */ | |
67 int cipher_idx, /* cipher idx */ | |
68 block_len, /* length of block */ | |
69 buflen; /* number of bytes in the buffer */ | |
70 } pmac_state; | |
71 | |
72 int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned long keylen); | |
73 int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen); | |
74 int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen); | |
75 | |
76 int pmac_memory(int cipher, | |
77 const unsigned char *key, unsigned long keylen, | |
78 const unsigned char *msg, unsigned long msglen, | |
79 unsigned char *out, unsigned long *outlen); | |
80 | |
81 int pmac_memory_multi(int cipher, | |
82 const unsigned char *key, unsigned long keylen, | |
83 unsigned char *out, unsigned long *outlen, | |
84 const unsigned char *in, unsigned long inlen, ...); | |
85 | |
86 int pmac_file(int cipher, | |
87 const unsigned char *key, unsigned long keylen, | |
88 const char *filename, | |
89 unsigned char *out, unsigned long *outlen); | |
90 | |
91 int pmac_test(void); | |
92 | |
93 /* internal functions */ | |
94 int pmac_ntz(unsigned long x); | |
95 void pmac_shift_xor(pmac_state *pmac); | |
96 | |
97 #endif /* PMAC */ | |
98 | |
99 #ifdef EAX_MODE | |
100 | |
101 #if !(defined(OMAC) && defined(CTR)) | |
102 #error EAX_MODE requires OMAC and CTR | |
103 #endif | |
104 | |
105 typedef struct { | |
106 unsigned char N[MAXBLOCKSIZE]; | |
107 symmetric_CTR ctr; | |
108 omac_state headeromac, ctomac; | |
109 } eax_state; | |
110 | |
111 int eax_init(eax_state *eax, int cipher, const unsigned char *key, unsigned long keylen, | |
112 const unsigned char *nonce, unsigned long noncelen, | |
113 const unsigned char *header, unsigned long headerlen); | |
114 | |
115 int eax_encrypt(eax_state *eax, const unsigned char *pt, unsigned char *ct, unsigned long length); | |
116 int eax_decrypt(eax_state *eax, const unsigned char *ct, unsigned char *pt, unsigned long length); | |
117 int eax_addheader(eax_state *eax, const unsigned char *header, unsigned long length); | |
118 int eax_done(eax_state *eax, unsigned char *tag, unsigned long *taglen); | |
119 | |
120 int eax_encrypt_authenticate_memory(int cipher, | |
121 const unsigned char *key, unsigned long keylen, | |
122 const unsigned char *nonce, unsigned long noncelen, | |
123 const unsigned char *header, unsigned long headerlen, | |
124 const unsigned char *pt, unsigned long ptlen, | |
125 unsigned char *ct, | |
126 unsigned char *tag, unsigned long *taglen); | |
127 | |
128 int eax_decrypt_verify_memory(int cipher, | |
129 const unsigned char *key, unsigned long keylen, | |
130 const unsigned char *nonce, unsigned long noncelen, | |
131 const unsigned char *header, unsigned long headerlen, | |
132 const unsigned char *ct, unsigned long ctlen, | |
133 unsigned char *pt, | |
134 unsigned char *tag, unsigned long taglen, | |
135 int *stat); | |
136 | |
137 int eax_test(void); | |
138 #endif /* EAX MODE */ | |
139 | |
140 #ifdef OCB_MODE | |
141 typedef struct { | |
142 unsigned char L[MAXBLOCKSIZE], /* L value */ | |
143 Ls[32][MAXBLOCKSIZE], /* L shifted by i bits to the left */ | |
144 Li[MAXBLOCKSIZE], /* value of Li [current value, we calc from previous recall] */ | |
145 Lr[MAXBLOCKSIZE], /* L * x^-1 */ | |
146 R[MAXBLOCKSIZE], /* R value */ | |
147 checksum[MAXBLOCKSIZE]; /* current checksum */ | |
148 | |
149 symmetric_key key; /* scheduled key for cipher */ | |
150 unsigned long block_index; /* index # for current block */ | |
151 int cipher, /* cipher idx */ | |
152 block_len; /* length of block */ | |
153 } ocb_state; | |
154 | |
155 int ocb_init(ocb_state *ocb, int cipher, | |
156 const unsigned char *key, unsigned long keylen, const unsigned char *nonce); | |
157 | |
158 int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct); | |
159 int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt); | |
160 | |
161 int ocb_done_encrypt(ocb_state *ocb, | |
162 const unsigned char *pt, unsigned long ptlen, | |
163 unsigned char *ct, | |
164 unsigned char *tag, unsigned long *taglen); | |
165 | |
166 int ocb_done_decrypt(ocb_state *ocb, | |
167 const unsigned char *ct, unsigned long ctlen, | |
168 unsigned char *pt, | |
169 const unsigned char *tag, unsigned long taglen, int *stat); | |
170 | |
171 int ocb_encrypt_authenticate_memory(int cipher, | |
172 const unsigned char *key, unsigned long keylen, | |
173 const unsigned char *nonce, | |
174 const unsigned char *pt, unsigned long ptlen, | |
175 unsigned char *ct, | |
176 unsigned char *tag, unsigned long *taglen); | |
177 | |
178 int ocb_decrypt_verify_memory(int cipher, | |
179 const unsigned char *key, unsigned long keylen, | |
180 const unsigned char *nonce, | |
181 const unsigned char *ct, unsigned long ctlen, | |
182 unsigned char *pt, | |
183 const unsigned char *tag, unsigned long taglen, | |
184 int *stat); | |
185 | |
186 int ocb_test(void); | |
187 | |
188 /* internal functions */ | |
189 void ocb_shift_xor(ocb_state *ocb, unsigned char *Z); | |
190 int ocb_ntz(unsigned long x); | |
191 int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen, | |
192 unsigned char *ct, unsigned char *tag, unsigned long *taglen, int mode); | |
193 | |
194 #endif /* OCB_MODE */ | |
195 | |
196 #ifdef CCM_MODE | |
197 | |
198 #define CCM_ENCRYPT 0 | |
199 #define CCM_DECRYPT 1 | |
200 | |
201 int ccm_memory(int cipher, | |
202 const unsigned char *key, unsigned long keylen, | |
203 const unsigned char *nonce, unsigned long noncelen, | |
204 const unsigned char *header, unsigned long headerlen, | |
205 unsigned char *pt, unsigned long ptlen, | |
206 unsigned char *ct, | |
207 unsigned char *tag, unsigned long *taglen, | |
208 int direction); | |
209 | |
210 int ccm_test(void); | |
211 | |
212 #endif /* CCM_MODE */ | |
213 | |
214 #ifdef GCM_MODE | |
215 | |
216 #define GCM_ENCRYPT 0 | |
217 #define GCM_DECRYPT 1 | |
218 | |
219 #define GCM_MODE_IV 0 | |
220 #define GCM_MODE_AAD 1 | |
221 #define GCM_MODE_TEXT 2 | |
222 | |
223 typedef struct { | |
224 symmetric_key K; | |
225 unsigned char H[16], /* multiplier */ | |
226 X[16], /* accumulator */ | |
227 Y[16], /* counter */ | |
228 Y_0[16], /* initial counter */ | |
229 buf[16]; /* buffer for stuff */ | |
230 | |
231 int cipher, /* which cipher */ | |
232 ivmode, /* Which mode is the IV in? */ | |
233 mode, /* mode the GCM code is in */ | |
234 buflen; /* length of data in buf */ | |
235 | |
236 ulong64 totlen, /* 64-bit counter used for IV and AAD */ | |
237 pttotlen; /* 64-bit counter for the PT */ | |
238 | |
239 #ifdef GCM_TABLES | |
240 unsigned char PC[16][256][16]; /* 16 tables of 8x128 */ | |
241 #endif | |
242 | |
243 } gcm_state; | |
244 | |
245 void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char *c); | |
246 void gcm_mult_h(gcm_state *gcm, unsigned char *I); | |
247 | |
248 int gcm_init(gcm_state *gcm, int cipher, | |
249 const unsigned char *key, int keylen); | |
250 | |
251 int gcm_reset(gcm_state *gcm); | |
252 | |
253 int gcm_add_iv(gcm_state *gcm, | |
254 const unsigned char *IV, unsigned long IVlen); | |
255 | |
256 int gcm_add_aad(gcm_state *gcm, | |
257 const unsigned char *adata, unsigned long adatalen); | |
258 | |
259 int gcm_process(gcm_state *gcm, | |
260 unsigned char *pt, unsigned long ptlen, | |
261 unsigned char *ct, | |
262 int direction); | |
263 | |
264 int gcm_done(gcm_state *gcm, | |
265 unsigned char *tag, unsigned long *taglen); | |
266 | |
267 int gcm_memory( int cipher, | |
268 const unsigned char *key, unsigned long keylen, | |
269 const unsigned char *IV, unsigned long IVlen, | |
270 const unsigned char *adata, unsigned long adatalen, | |
271 unsigned char *pt, unsigned long ptlen, | |
272 unsigned char *ct, | |
273 unsigned char *tag, unsigned long *taglen, | |
274 int direction); | |
275 int gcm_test(void); | |
276 | |
277 #endif /* GCM_MODE */ | |
278 | |
279 #ifdef PELICAN | |
280 | |
281 typedef struct pelican_state | |
282 { | |
283 symmetric_key K; | |
284 unsigned char state[16]; | |
285 int buflen; | |
286 } pelican_state; | |
287 | |
288 int pelican_init(pelican_state *pelmac, const unsigned char *key, unsigned long keylen); | |
289 int pelican_process(pelican_state *pelmac, const unsigned char *in, unsigned long inlen); | |
290 int pelican_done(pelican_state *pelmac, unsigned char *out); | |
291 int pelican_test(void); | |
292 | |
293 int pelican_memory(const unsigned char *key, unsigned long keylen, | |
294 const unsigned char *in, unsigned long inlen, | |
295 unsigned char *out); | |
296 | |
297 #endif |