chiark / gitweb /
Release 2.4.3.
[catacomb] / symm / cfb.h
1 /* -*-c-*-
2  *
3  * Ciphertext feedback for block ciphers
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 #ifndef CATACOMB_CFB_H
29 #define CATACOMB_CFB_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stddef.h>
38
39 #include <mLib/bits.h>
40
41 #ifndef CATACOMB_GCIPHER_H
42 #  include "gcipher.h"
43 #endif
44
45 /*----- Data structures ---------------------------------------------------*/
46
47 /* --- @CFB_DECL@ --- *
48  *
49  * Arguments:   @PRE@, @pre@ = prefixes for the underlying block cipher
50  *
51  * Use:         Creates declarations for CFB mode.
52  */
53
54 #define CFB_DECL(PRE, pre)                                              \
55                                                                         \
56 /* --- Ciphertext feedback context --- */                               \
57                                                                         \
58 typedef struct pre##_cfbctx {                                           \
59   pre##_ctx ctx;                        /* Underlying cipher context */ \
60   unsigned off;                         /* Offset into @iv@ buffer */   \
61   octet iv[PRE##_BLKSZ];                /* Previous ciphertext or IV */ \
62 } pre##_cfbctx;                                                         \
63                                                                         \
64 /* --- @pre_cfbgetiv@ --- *                                             \
65  *                                                                      \
66  * Arguments:   @const pre_cfbctx *ctx@ = pointer to CFB context block  \
67  *              @void *iv@ = pointer to output data block               \
68  *                                                                      \
69  * Returns:     ---                                                     \
70  *                                                                      \
71  * Use:         Reads the currently set IV.  Reading and setting an IV  \
72  *              is not transparent to the cipher.  It will add a `step' \
73  *              which must be matched by a similar operation during     \
74  *              decryption.                                             \
75  */                                                                     \
76                                                                         \
77 extern void pre##_cfbgetiv(const pre##_cfbctx */*ctx*/,                 \
78                            void */*iv*/);                               \
79                                                                         \
80 /* --- @pre_cfbsetiv@ --- *                                             \
81  *                                                                      \
82  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
83  *              @cnost void *iv@ = pointer to IV to set                 \
84  *                                                                      \
85  * Returns:     ---                                                     \
86  *                                                                      \
87  * Use:         Sets the IV to use for subsequent encryption.           \
88  */                                                                     \
89                                                                         \
90 extern void pre##_cfbsetiv(pre##_cfbctx */*ctx*/,                       \
91                            const void */*iv*/);                         \
92                                                                         \
93 /* --- @pre_cfbbdry@ --- *                                              \
94  *                                                                      \
95  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
96  *                                                                      \
97  * Returns:     ---                                                     \
98  *                                                                      \
99  * Use:         Inserts a boundary during encryption.  Successful       \
100  *              decryption must place a similar boundary.               \
101  */                                                                     \
102                                                                         \
103 extern void pre##_cfbbdry(pre##_cfbctx */*ctx*/);                       \
104                                                                         \
105 /* --- @pre_cfbsetkey@ --- *                                            \
106  *                                                                      \
107  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
108  *              @const pre_ctx *k@ = pointer to cipher context          \
109  *                                                                      \
110  * Returns:     ---                                                     \
111  *                                                                      \
112  * Use:         Sets the CFB context to use a different cipher key.     \
113  */                                                                     \
114                                                                         \
115 extern void pre##_cfbsetkey(pre##_cfbctx */*ctx*/,                      \
116                             const pre##_ctx */*k*/);                    \
117                                                                         \
118 /* --- @pre_cfbinit@ --- *                                              \
119  *                                                                      \
120  * Arguments:   @pre_cfbctx *ctx@ = pointer to cipher context           \
121  *              @const void *key@ = pointer to the key buffer           \
122  *              @size_t sz@ = size of the key                           \
123  *              @const void *iv@ = pointer to initialization vector     \
124  *                                                                      \
125  * Returns:     ---                                                     \
126  *                                                                      \
127  * Use:         Initializes a CFB context ready for use.  You should    \
128  *              ensure that the IV chosen is unique: reusing an IV will \
129  *              compromise the security of at least the first block     \
130  *              encrypted.  This is equivalent to calls to @pre_init@,  \
131  *              @pre_cfbsetkey@ and @pre_cfbsetiv@.                     \
132  */                                                                     \
133                                                                         \
134 extern void pre##_cfbinit(pre##_cfbctx */*ctx*/,                        \
135                           const void */*key*/, size_t /*sz*/,           \
136                           const void */*iv*/);                          \
137                                                                         \
138 /* --- @pre_cfbencrypt@ --- *                                           \
139  *                                                                      \
140  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
141  *              @const void *src@ = pointer to source data              \
142  *              @void *dest@ = pointer to destination data              \
143  *              @size_t sz@ = size of block to be encrypted             \
144  *                                                                      \
145  * Returns:     ---                                                     \
146  *                                                                      \
147  * Use:         Encrypts a block with a block cipher in CFB mode.  The  \
148  *              input block may be arbitrary in size.  CFB mode is not  \
149  *              sensitive to block boundaries.                          \
150  */                                                                     \
151                                                                         \
152 extern void pre##_cfbencrypt(pre##_cfbctx */*ctx*/,                     \
153                              const void */*src*/, void */*dest*/,       \
154                              size_t /*sz*/);                            \
155                                                                         \
156 /* --- @pre_cfbencrypt@ --- *                                           \
157  *                                                                      \
158  * Arguments:   @pre_cfbctx *ctx@ = pointer to CFB context block        \
159  *              @const void *src@ = pointer to source data              \
160  *              @void *dest@ = pointer to destination data              \
161  *              @size_t sz@ = size of block to be encrypted             \
162  *                                                                      \
163  * Returns:     ---                                                     \
164  *                                                                      \
165  * Use:         Decrypts a block with a block cipher in CFB mode.  The  \
166  *              input block may be arbitrary in size.  CFB mode is not  \
167  *              sensitive to block boundaries.                          \
168  */                                                                     \
169                                                                         \
170 extern void pre##_cfbdecrypt(pre##_cfbctx */*ctx*/,                     \
171                              const void */*src*/, void */*dest*/,       \
172                              size_t /*sz*/);                            \
173                                                                         \
174 /* --- Generic cipher interface --- */                                  \
175                                                                         \
176 extern const gccipher pre##_cfb;
177
178 /*----- That's all, folks -------------------------------------------------*/
179
180 #ifdef __cplusplus
181   }
182 #endif
183
184 #endif