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