Fix.
[shishi.git] / crypto / cbc.h
blobdba52fdd8d36e925f80bbd1756c8d16c2afb3493
1 /* cbc.h
3 * Cipher block chaining mode.
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
9 *
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * MA 02111-1307, USA.
26 #ifndef NETTLE_CBC_H_INCLUDED
27 #define NETTLE_CBC_H_INCLUDED
29 #include <inttypes.h>
31 /* Uses a void * for cipher contexts. */
33 void
34 cbc_encrypt(void *ctx, void (*f)(void *ctx,
35 unsigned length, uint8_t *dst,
36 const uint8_t *src),
37 unsigned block_size, uint8_t *iv,
38 unsigned length, uint8_t *dst,
39 const uint8_t *src);
41 void
42 cbc_decrypt(void *ctx, void (*f)(void *ctx,
43 unsigned length, uint8_t *dst,
44 const uint8_t *src),
45 unsigned block_size, uint8_t *iv,
46 unsigned length, uint8_t *dst,
47 const uint8_t *src);
49 #define CBC_CTX(type, size) \
50 { type ctx; uint8_t iv[size]; }
52 #define CBC_SET_IV(ctx, data) \
53 memcpy((ctx)->iv, (data), sizeof((ctx)->iv))
55 #if 0
56 #define CBC_ENCRYPT(self, f, length, dst, src) \
57 do { if (0) (f)(&(self)->ctx, 0, NULL, NULL); \
58 cbc_encrypt((void *) &(self)->ctx, \
59 (void (*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
60 sizeof((self)->iv), (self)->iv, \
61 (length), (dst), (src)); \
62 } while (0)
63 #endif
65 #define CBC_ENCRYPT(self, f, length, dst, src) \
66 (0 ? ((f)(&(self)->ctx, 0, NULL, NULL)) \
67 : cbc_encrypt((void *) &(self)->ctx, \
68 (void (*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
69 sizeof((self)->iv), (self)->iv, \
70 (length), (dst), (src)))
72 #define CBC_DECRYPT(self, f, length, dst, src) \
73 (0 ? ((f)(&(self)->ctx, 0, NULL, NULL)) \
74 : cbc_decrypt((void *) &(self)->ctx, \
75 (void (*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
76 sizeof((self)->iv), (self)->iv, \
77 (length), (dst), (src)))
79 #if 0
80 /* Type safer variants */
81 #define CBC_ENCRYPT2(ctx, f, b, iv, l, dst, src) \
82 (0 ? ((f)((ctx),0,NULL,NULL)) \
83 : cbc_encrypt((void *)(ctx), \
84 (void (*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
85 (b), (iv), (l), (dst), (src)))
87 #endif
89 #endif /* NETTLE_CBC_H_INCLUDED */