35d66180 |
1 | /* -*-c-*- |
2 | * |
b9a27a11 |
3 | * $Id: counter.h,v 1.3 2002/01/13 13:43:35 mdw Exp $ |
35d66180 |
4 | * |
5 | * Block cipher counter mode (or long cycle mode) |
6 | * |
7 | * (c) 2000 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: counter.h,v $ |
b9a27a11 |
33 | * Revision 1.3 2002/01/13 13:43:35 mdw |
34 | * Indentation fix. |
35 | * |
4efe32ba |
36 | * Revision 1.2 2001/06/17 00:10:51 mdw |
37 | * Typesetting fixes |
38 | * |
35d66180 |
39 | * Revision 1.1 2000/06/17 10:51:42 mdw |
40 | * Counter mode ciphers and pseudo-random generator. |
41 | * |
42 | */ |
43 | |
44 | #ifndef CATACOMB_COUNTER_H |
45 | #define CATACOMB_COUNTER_H |
46 | |
47 | #ifdef __cplusplus |
48 | extern "C" { |
49 | #endif |
50 | |
51 | /*----- Header files ------------------------------------------------------*/ |
52 | |
53 | #include <stddef.h> |
54 | |
55 | #include <mLib/bits.h> |
56 | |
57 | #ifndef CATACOMB_GCIPHER_H |
58 | # include "gcipher.h" |
59 | #endif |
60 | |
61 | #ifndef CATACOMB_GRAND_H |
62 | # include "grand.h" |
63 | #endif |
64 | |
65 | /*----- Macros ------------------------------------------------------------*/ |
66 | |
67 | /* --- @COUNTER_DECL@ --- * |
68 | * |
69 | * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions |
70 | * |
71 | * Use: Makes declarations for counter mode. |
72 | */ |
73 | |
74 | #define COUNTER_DECL(PRE, pre) \ |
75 | \ |
76 | /* --- Counter mode context --- */ \ |
77 | \ |
78 | typedef struct pre##_counterctx { \ |
79 | pre##_ctx ctx; /* Underlying cipher context */ \ |
80 | unsigned off; /* Current offset in buffer */ \ |
81 | octet buf[PRE##_BLKSZ]; /* Output buffer */ \ |
82 | uint32 n[PRE##_BLKSZ / 4]; /* Counter */ \ |
83 | } pre##_counterctx; \ |
84 | \ |
85 | /* --- @pre_countergetiv@ --- * \ |
86 | * \ |
87 | * Arguments: @const pre_counterctx *ctx@ = pointer to counter \ |
88 | * context \ |
4efe32ba |
89 | * @void *iv@ = pointer to output data block \ |
35d66180 |
90 | * \ |
91 | * Returns: --- \ |
92 | * \ |
93 | * Use: Reads the currently set IV. Reading and setting an IV \ |
94 | * is not transparent to the cipher. It will add a `step' \ |
95 | * which must be matched by a similar operation during \ |
96 | * decryption. \ |
97 | */ \ |
98 | \ |
99 | extern void pre##_countergetiv(const pre##_counterctx */*ctx*/, \ |
100 | void */*iv*/); \ |
101 | \ |
102 | /* --- @pre_countersetiv@ --- * \ |
103 | * \ |
104 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
105 | * @cnost void *iv@ = pointer to IV to set \ |
106 | * \ |
107 | * Returns: --- \ |
108 | * \ |
109 | * Use: Sets the IV to use for subsequent encryption. \ |
110 | */ \ |
111 | \ |
112 | extern void pre##_countersetiv(pre##_counterctx */*ctx*/, \ |
113 | const void */*iv*/); \ |
114 | \ |
115 | /* --- @pre_counterbdry@ --- * \ |
116 | * \ |
117 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
118 | * \ |
119 | * Returns: --- \ |
120 | * \ |
121 | * Use: Inserts a boundary during encryption. Successful \ |
122 | * decryption must place a similar boundary. \ |
123 | */ \ |
124 | \ |
125 | extern void pre##_counterbdry(pre##_counterctx */*ctx*/); \ |
126 | \ |
127 | /* --- @pre_countersetkey@ --- * \ |
128 | * \ |
129 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
130 | * @const pre_ctx *k@ = pointer to cipher context \ |
131 | * \ |
132 | * Returns: --- \ |
133 | * \ |
134 | * Use: Sets the counter context to use a different cipher key. \ |
135 | */ \ |
136 | \ |
137 | extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \ |
138 | const pre##_ctx */*k*/); \ |
139 | \ |
140 | /* --- @pre_counterinit@ --- * \ |
141 | * \ |
142 | * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \ |
143 | * @const void *key@ = pointer to the key buffer \ |
144 | * @size_t sz@ = size of the key \ |
145 | * @const void *iv@ = pointer to initialization vector \ |
146 | * \ |
147 | * Returns: --- \ |
148 | * \ |
149 | * Use: Initializes a counter context ready for use. You \ |
150 | * should ensure that the IV chosen is unique: reusing an \ |
151 | * IV will compromise the security of the entire \ |
152 | * plaintext. This is equivalent to calls to @pre_init@, \ |
153 | * @pre_countersetkey@ and @pre_countersetiv@. \ |
154 | */ \ |
155 | \ |
156 | extern void pre##_counterinit(pre##_counterctx */*ctx*/, \ |
157 | const void */*key*/, size_t /*sz*/, \ |
b9a27a11 |
158 | const void */*iv*/); \ |
35d66180 |
159 | \ |
160 | /* --- @pre_counterencrypt@ --- * \ |
161 | * \ |
162 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
163 | * @const void *src@ = pointer to source data \ |
164 | * @void *dest@ = pointer to destination data \ |
165 | * @size_t sz@ = size of block to be encrypted \ |
166 | * \ |
167 | * Returns: --- \ |
168 | * \ |
169 | * Use: Encrypts or decrypts a block with a block cipher in \ |
170 | * counter mode: encryption and decryption are the same in \ |
171 | * counter. The destination may be null to just churn the \ |
172 | * feedback round for a bit. The source may be null to \ |
173 | * use the cipher as a random data generator. \ |
174 | */ \ |
175 | \ |
176 | extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \ |
177 | const void */*src*/, void */*dest*/, \ |
178 | size_t /*sz*/); \ |
179 | \ |
180 | /* --- @pre_counterrand@ --- * \ |
181 | * \ |
182 | * Arguments: @const void *k@ = pointer to key material \ |
183 | * @size_t sz@ = size of key material \ |
184 | * \ |
185 | * Returns: Pointer to generic random number generator interface. \ |
186 | * \ |
187 | * Use: Creates a random number interface wrapper around an \ |
188 | * counter-mode block cipher. \ |
189 | */ \ |
190 | \ |
191 | extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \ |
192 | \ |
193 | /* --- Generic cipher interface --- */ \ |
194 | \ |
195 | extern const gccipher pre##_counter; |
196 | |
197 | /*----- That's all, folks -------------------------------------------------*/ |
198 | |
199 | #ifdef __cplusplus |
200 | } |
201 | #endif |
202 | |
203 | #endif |