]> git.ipfire.org Git - thirdparty/strongswan.git/blob - lib/libcrypto/include/cbc_generic.h
- import of strongswan-2.7.0
[thirdparty/strongswan.git] / lib / libcrypto / include / cbc_generic.h
1 #ifndef _CBC_GENERIC_H
2 #define _CBC_GENERIC_H
3 /*
4 * CBC macro helpers
5 *
6 * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 */
19
20 /*
21 * Heavily inspired in loop_AES
22 */
23 #define CBC_IMPL_BLK16(name, ctx_type, addr_type, enc_func, dec_func) \
24 int name(ctx_type *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt) { \
25 int ret=ilen, pos; \
26 const u_int32_t *iv_i; \
27 if ((ilen) % 16) return 0; \
28 if (encrypt) { \
29 pos=0; \
30 while(pos<ilen) { \
31 if (pos==0) \
32 iv_i=(const u_int32_t*) iv; \
33 else \
34 iv_i=(const u_int32_t*) (out-16); \
35 *((u_int32_t *)(&out[ 0])) = iv_i[0]^*((const u_int32_t *)(&in[ 0])); \
36 *((u_int32_t *)(&out[ 4])) = iv_i[1]^*((const u_int32_t *)(&in[ 4])); \
37 *((u_int32_t *)(&out[ 8])) = iv_i[2]^*((const u_int32_t *)(&in[ 8])); \
38 *((u_int32_t *)(&out[12])) = iv_i[3]^*((const u_int32_t *)(&in[12])); \
39 enc_func(ctx, (addr_type) out, (addr_type) out); \
40 in+=16; \
41 out+=16; \
42 pos+=16; \
43 } \
44 } else { \
45 pos=ilen-16; \
46 in+=pos; \
47 out+=pos; \
48 while(pos>=0) { \
49 dec_func(ctx, (const addr_type) in, (addr_type) out); \
50 if (pos==0) \
51 iv_i=(const u_int32_t*) (iv); \
52 else \
53 iv_i=(const u_int32_t*) (in-16); \
54 *((u_int32_t *)(&out[ 0])) ^= iv_i[0]; \
55 *((u_int32_t *)(&out[ 4])) ^= iv_i[1]; \
56 *((u_int32_t *)(&out[ 8])) ^= iv_i[2]; \
57 *((u_int32_t *)(&out[12])) ^= iv_i[3]; \
58 in-=16; \
59 out-=16; \
60 pos-=16; \
61 } \
62 } \
63 return ret; \
64 }
65 #define CBC_IMPL_BLK8(name, ctx_type, addr_type, enc_func, dec_func) \
66 int name(ctx_type *ctx, u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt) { \
67 int ret=ilen, pos; \
68 const u_int32_t *iv_i; \
69 if ((ilen) % 8) return 0; \
70 if (encrypt) { \
71 pos=0; \
72 while(pos<ilen) { \
73 if (pos==0) \
74 iv_i=(const u_int32_t*) iv; \
75 else \
76 iv_i=(const u_int32_t*) (out-8); \
77 *((u_int32_t *)(&out[ 0])) = iv_i[0]^*((const u_int32_t *)(&in[ 0])); \
78 *((u_int32_t *)(&out[ 4])) = iv_i[1]^*((const u_int32_t *)(&in[ 4])); \
79 enc_func(ctx, (addr_type)out, (addr_type)out); \
80 in+=8; \
81 out+=8; \
82 pos+=8; \
83 } \
84 } else { \
85 pos=ilen-8; \
86 in+=pos; \
87 out+=pos; \
88 while(pos>=0) { \
89 dec_func(ctx, (const addr_type)in, (addr_type)out); \
90 if (pos==0) \
91 iv_i=(const u_int32_t*) (iv); \
92 else \
93 iv_i=(const u_int32_t*) (in-8); \
94 *((u_int32_t *)(&out[ 0])) ^= iv_i[0]; \
95 *((u_int32_t *)(&out[ 4])) ^= iv_i[1]; \
96 in-=8; \
97 out-=8; \
98 pos-=8; \
99 } \
100 } \
101 return ret; \
102 }
103 #define CBC_DECL(name, ctx_type) \
104 int name(ctx_type *ctx, u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt)
105 /*
106 Eg.:
107 CBC_IMPL_BLK16(AES_cbc_encrypt, aes_context, u_int8_t *, aes_encrypt, aes_decrypt);
108 CBC_DECL(AES_cbc_encrypt, aes_context);
109 */
110 #endif /* _CBC_GENERIC_H */