d03ab969 |
1 | /* -*-c-*- |
d03ab969 |
2 | * |
3 | * Output feedback for block ciphers |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
d03ab969 |
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. |
45c0fd36 |
16 | * |
d03ab969 |
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. |
45c0fd36 |
21 | * |
d03ab969 |
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 | |
79ba130c |
28 | #ifndef CATACOMB_OFB_H |
29 | #define CATACOMB_OFB_H |
d03ab969 |
30 | |
31 | #ifdef __cplusplus |
32 | extern "C" { |
33 | #endif |
34 | |
35 | /*----- Header files ------------------------------------------------------*/ |
36 | |
79ba130c |
37 | #include <stddef.h> |
d03ab969 |
38 | |
39 | #include <mLib/bits.h> |
40 | |
79ba130c |
41 | #ifndef CATACOMB_GCIPHER_H |
42 | # include "gcipher.h" |
d03ab969 |
43 | #endif |
44 | |
79ba130c |
45 | #ifndef CATACOMB_GRAND_H |
46 | # include "grand.h" |
d03ab969 |
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 | \ |
79ba130c |
60 | /* --- Output feedback context --- */ \ |
d03ab969 |
61 | \ |
79ba130c |
62 | typedef struct pre##_ofbctx { \ |
63 | pre##_ctx ctx; /* Underlying cipher context */ \ |
75b1b624 |
64 | unsigned off; /* Current offset in buffer */ \ |
79ba130c |
65 | octet iv[PRE##_BLKSZ]; /* Output buffer and IV */ \ |
66 | } pre##_ofbctx; \ |
d03ab969 |
67 | \ |
68 | /* --- @pre_ofbgetiv@ --- * \ |
69 | * \ |
70 | * Arguments: @const pre_ofbctx *ctx@ = pointer to OFB context block \ |
4efe32ba |
71 | * @void *iv@ = pointer to output data block \ |
d03ab969 |
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 | \ |
79ba130c |
81 | extern void pre##_ofbgetiv(const pre##_ofbctx */*ctx*/, \ |
82 | void */*iv*/); \ |
d03ab969 |
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 | \ |
79ba130c |
94 | extern void pre##_ofbsetiv(pre##_ofbctx */*ctx*/, \ |
95 | const void */*iv*/); \ |
d03ab969 |
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 | \ |
79ba130c |
107 | extern void pre##_ofbbdry(pre##_ofbctx */*ctx*/); \ |
d03ab969 |
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 | \ |
79ba130c |
119 | extern void pre##_ofbsetkey(pre##_ofbctx */*ctx*/, \ |
120 | const pre##_ctx */*k*/); \ |
d03ab969 |
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 | \ |
79ba130c |
138 | extern void pre##_ofbinit(pre##_ofbctx */*ctx*/, \ |
139 | const void */*key*/, size_t /*sz*/, \ |
140 | const void */*iv*/); \ |
d03ab969 |
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 | \ |
79ba130c |
158 | extern void pre##_ofbencrypt(pre##_ofbctx */*ctx*/, \ |
159 | const void */*src*/, void */*dest*/, \ |
160 | size_t /*sz*/); \ |
d03ab969 |
161 | \ |
79ba130c |
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 | */ \ |
d03ab969 |
172 | \ |
79ba130c |
173 | extern grand *pre##_ofbrand(const void */*k*/, size_t /*sz*/); \ |
d03ab969 |
174 | \ |
79ba130c |
175 | /* --- Generic cipher interface --- */ \ |
d03ab969 |
176 | \ |
79ba130c |
177 | extern const gccipher pre##_ofb; |
d03ab969 |
178 | |
179 | /*----- That's all, folks -------------------------------------------------*/ |
180 | |
181 | #ifdef __cplusplus |
182 | } |
183 | #endif |
184 | |
185 | #endif |