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