Dibbler - a portable DHCPv6  1.0.2RC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cbc.h
Go to the documentation of this file.
1 /* cbc.h
2  *
3  * Cipher block chaining mode.
4  */
5 
6 /* nettle, low-level cryptographics library
7  *
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.
14  *
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.
19  *
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.
24  */
25 
26 #ifndef NETTLE_CBC_H_INCLUDED
27 #define NETTLE_CBC_H_INCLUDED
28 
29 #include "nettle-types.h"
30 
31 /* Name mangling */
32 #define cbc_encrypt nettle_cbc_encrypt
33 #define cbc_decrypt nettle_cbc_decrypt
34 
35 /* Uses a void * for cipher contexts.
36 
37  For block ciphers it would make sense with a const void * for the
38  context, but we use the same typedef for stream ciphers where the
39  internal state changes during the encryption. */
40 typedef void (*nettle_crypt_func)(void *ctx,
41  unsigned length, uint8_t *dst,
42  const uint8_t *src);
43 
44 void
46  unsigned block_size, uint8_t *iv,
47  unsigned length, uint8_t *dst,
48  const uint8_t *src);
49 
50 void
52  unsigned block_size, uint8_t *iv,
53  unsigned length, uint8_t *dst,
54  const uint8_t *src);
55 
56 #define CBC_CTX(type, size) \
57 { type ctx; uint8_t iv[size]; }
58 
59 #define CBC_SET_IV(ctx, data) \
60 memcpy((ctx)->iv, (data), sizeof((ctx)->iv))
61 
62 #if 0
63 #define CBC_ENCRYPT(self, f, length, dst, src) \
64 do { if (0) (f)(&(self)->ctx, 0, NULL, NULL); \
65  cbc_encrypt((void *) &(self)->ctx, \
66  (void (*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
67  sizeof((self)->iv), (self)->iv, \
68  (length), (dst), (src)); \
69 } while (0)
70 #endif
71 
72 #define CBC_ENCRYPT(self, f, length, dst, src) \
73 (0 ? ((f)(&(self)->ctx, 0, NULL, NULL)) \
74  : cbc_encrypt((void *) &(self)->ctx, \
75  (void (*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
76  sizeof((self)->iv), (self)->iv, \
77  (length), (dst), (src)))
78 
79 #define CBC_DECRYPT(self, f, length, dst, src) \
80 (0 ? ((f)(&(self)->ctx, 0, NULL, NULL)) \
81  : cbc_decrypt((void *) &(self)->ctx, \
82  (void (*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
83  sizeof((self)->iv), (self)->iv, \
84  (length), (dst), (src)))
85 
86 #if 0
87 /* Type safer variants */
88 #define CBC_ENCRYPT2(ctx, f, b, iv, l, dst, src) \
89 (0 ? ((f)((ctx),0,NULL,NULL)) \
90  : cbc_encrypt((void *)(ctx), \
91  (void (*)(void *, unsigned, uint8_t *, const uint8_t *)) (f), \
92  (b), (iv), (l), (dst), (src)))
93 
94 #endif
95 
96 #endif /* NETTLE_CBC_H_INCLUDED */
97