d03ab969 |
1 | /* -*-c-*- |
2 | * |
d03ab969 |
3 | * Ciphertext 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_CFB_H |
29 | #define CATACOMB_CFB_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 | |
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 | \ |
79ba130c |
56 | /* --- Ciphertext feedback context --- */ \ |
d03ab969 |
57 | \ |
79ba130c |
58 | typedef struct pre##_cfbctx { \ |
59 | pre##_ctx ctx; /* Underlying cipher context */ \ |
e97df21b |
60 | unsigned off; /* Offset into @iv@ buffer */ \ |
79ba130c |
61 | octet iv[PRE##_BLKSZ]; /* Previous ciphertext or IV */ \ |
62 | } pre##_cfbctx; \ |
d03ab969 |
63 | \ |
64 | /* --- @pre_cfbgetiv@ --- * \ |
65 | * \ |
66 | * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \ |
4efe32ba |
67 | * @void *iv@ = pointer to output data block \ |
d03ab969 |
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 | \ |
79ba130c |
77 | extern void pre##_cfbgetiv(const pre##_cfbctx */*ctx*/, \ |
78 | void */*iv*/); \ |
d03ab969 |
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 | \ |
79ba130c |
90 | extern void pre##_cfbsetiv(pre##_cfbctx */*ctx*/, \ |
91 | const void */*iv*/); \ |
d03ab969 |
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 | \ |
79ba130c |
103 | extern void pre##_cfbbdry(pre##_cfbctx */*ctx*/); \ |
d03ab969 |
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 | \ |
79ba130c |
115 | extern void pre##_cfbsetkey(pre##_cfbctx */*ctx*/, \ |
116 | const pre##_ctx */*k*/); \ |
d03ab969 |
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 | \ |
79ba130c |
134 | extern void pre##_cfbinit(pre##_cfbctx */*ctx*/, \ |
135 | const void */*key*/, size_t /*sz*/, \ |
136 | const void */*iv*/); \ |
d03ab969 |
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 | \ |
79ba130c |
152 | extern void pre##_cfbencrypt(pre##_cfbctx */*ctx*/, \ |
153 | const void */*src*/, void */*dest*/, \ |
154 | size_t /*sz*/); \ |
d03ab969 |
155 | \ |
79ba130c |
156 | /* --- @pre_cfbencrypt@ --- * \ |
d03ab969 |
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 | * \ |
79ba130c |
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. \ |
d03ab969 |
168 | */ \ |
169 | \ |
79ba130c |
170 | extern void pre##_cfbdecrypt(pre##_cfbctx */*ctx*/, \ |
171 | const void */*src*/, void */*dest*/, \ |
172 | size_t /*sz*/); \ |
d03ab969 |
173 | \ |
79ba130c |
174 | /* --- Generic cipher interface --- */ \ |
d03ab969 |
175 | \ |
79ba130c |
176 | extern const gccipher pre##_cfb; |
d03ab969 |
177 | |
178 | /*----- That's all, folks -------------------------------------------------*/ |
179 | |
180 | #ifdef __cplusplus |
181 | } |
182 | #endif |
183 | |
184 | #endif |