Cleanups.
[u/mdw/catacomb] / ofb.h
1 /* -*-c-*-
2 *
3 * $Id: ofb.h,v 1.5 2004/04/08 01:36:15 mdw Exp $
4 *
5 * Output 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 #ifndef CATACOMB_OFB_H
31 #define CATACOMB_OFB_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <stddef.h>
40
41 #include <mLib/bits.h>
42
43 #ifndef CATACOMB_GCIPHER_H
44 # include "gcipher.h"
45 #endif
46
47 #ifndef CATACOMB_GRAND_H
48 # include "grand.h"
49 #endif
50
51 /*----- Macros ------------------------------------------------------------*/
52
53 /* --- @OFB_DECL@ --- *
54 *
55 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
56 *
57 * Use: Makes declarations for output feedback mode.
58 */
59
60 #define OFB_DECL(PRE, pre) \
61 \
62 /* --- Output feedback context --- */ \
63 \
64 typedef struct pre##_ofbctx { \
65 pre##_ctx ctx; /* Underlying cipher context */ \
66 unsigned off; /* Current offset in buffer */ \
67 octet iv[PRE##_BLKSZ]; /* Output buffer and IV */ \
68 } pre##_ofbctx; \
69 \
70 /* --- @pre_ofbgetiv@ --- * \
71 * \
72 * Arguments: @const pre_ofbctx *ctx@ = pointer to OFB context block \
73 * @void *iv@ = pointer to output data block \
74 * \
75 * Returns: --- \
76 * \
77 * Use: Reads the currently set IV. Reading and setting an IV \
78 * is not transparent to the cipher. It will add a `step' \
79 * which must be matched by a similar operation during \
80 * decryption. \
81 */ \
82 \
83 extern void pre##_ofbgetiv(const pre##_ofbctx */*ctx*/, \
84 void */*iv*/); \
85 \
86 /* --- @pre_ofbsetiv@ --- * \
87 * \
88 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
89 * @cnost void *iv@ = pointer to IV to set \
90 * \
91 * Returns: --- \
92 * \
93 * Use: Sets the IV to use for subsequent encryption. \
94 */ \
95 \
96 extern void pre##_ofbsetiv(pre##_ofbctx */*ctx*/, \
97 const void */*iv*/); \
98 \
99 /* --- @pre_ofbbdry@ --- * \
100 * \
101 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
102 * \
103 * Returns: --- \
104 * \
105 * Use: Inserts a boundary during encryption. Successful \
106 * decryption must place a similar boundary. \
107 */ \
108 \
109 extern void pre##_ofbbdry(pre##_ofbctx */*ctx*/); \
110 \
111 /* --- @pre_ofbsetkey@ --- * \
112 * \
113 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
114 * @const pre_ctx *k@ = pointer to cipher context \
115 * \
116 * Returns: --- \
117 * \
118 * Use: Sets the OFB context to use a different cipher key. \
119 */ \
120 \
121 extern void pre##_ofbsetkey(pre##_ofbctx */*ctx*/, \
122 const pre##_ctx */*k*/); \
123 \
124 /* --- @pre_ofbinit@ --- * \
125 * \
126 * Arguments: @pre_ofbctx *ctx@ = pointer to cipher context \
127 * @const void *key@ = pointer to the key buffer \
128 * @size_t sz@ = size of the key \
129 * @const void *iv@ = pointer to initialization vector \
130 * \
131 * Returns: --- \
132 * \
133 * Use: Initializes a OFB context ready for use. You should \
134 * ensure that the IV chosen is unique: reusing an IV will \
135 * compromise the security of the entire plaintext. This \
136 * is equivalent to calls to @pre_init@, @pre_ofbsetkey@ \
137 * and @pre_ofbsetiv@. \
138 */ \
139 \
140 extern void pre##_ofbinit(pre##_ofbctx */*ctx*/, \
141 const void */*key*/, size_t /*sz*/, \
142 const void */*iv*/); \
143 \
144 /* --- @pre_ofbencrypt@ --- * \
145 * \
146 * Arguments: @pre_ofbctx *ctx@ = pointer to OFB context block \
147 * @const void *src@ = pointer to source data \
148 * @void *dest@ = pointer to destination data \
149 * @size_t sz@ = size of block to be encrypted \
150 * \
151 * Returns: --- \
152 * \
153 * Use: Encrypts or decrypts a block with a block cipher in OFB \
154 * mode: encryption and decryption are the same in OFB. \
155 * The destination may be null to just churn the feedback \
156 * round for a bit. The source may be null to use the \
157 * cipher as a random data generator. \
158 */ \
159 \
160 extern void pre##_ofbencrypt(pre##_ofbctx */*ctx*/, \
161 const void */*src*/, void */*dest*/, \
162 size_t /*sz*/); \
163 \
164 /* --- @pre_ofbrand@ --- * \
165 * \
166 * Arguments: @const void *k@ = pointer to key material \
167 * @size_t sz@ = size of key material \
168 * \
169 * Returns: Pointer to generic random number generator interface. \
170 * \
171 * Use: Creates a random number interface wrapper around an \
172 * OFB-mode block cipher. \
173 */ \
174 \
175 extern grand *pre##_ofbrand(const void */*k*/, size_t /*sz*/); \
176 \
177 /* --- Generic cipher interface --- */ \
178 \
179 extern const gccipher pre##_ofb;
180
181 /*----- That's all, folks -------------------------------------------------*/
182
183 #ifdef __cplusplus
184 }
185 #endif
186
187 #endif