35d66180 |
1 | /* -*-c-*- |
35d66180 |
2 | * |
3 | * Block cipher counter mode (or long cycle mode) |
4 | * |
5 | * (c) 2000 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
35d66180 |
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 | * |
35d66180 |
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 | * |
35d66180 |
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 | |
35d66180 |
28 | #ifndef CATACOMB_COUNTER_H |
29 | #define CATACOMB_COUNTER_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 | /* --- @COUNTER_DECL@ --- * |
52 | * |
53 | * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions |
54 | * |
55 | * Use: Makes declarations for counter mode. |
56 | */ |
57 | |
58 | #define COUNTER_DECL(PRE, pre) \ |
59 | \ |
60 | /* --- Counter mode context --- */ \ |
61 | \ |
62 | typedef struct pre##_counterctx { \ |
63 | pre##_ctx ctx; /* Underlying cipher context */ \ |
64 | unsigned off; /* Current offset in buffer */ \ |
65 | octet buf[PRE##_BLKSZ]; /* Output buffer */ \ |
66 | uint32 n[PRE##_BLKSZ / 4]; /* Counter */ \ |
67 | } pre##_counterctx; \ |
68 | \ |
69 | /* --- @pre_countergetiv@ --- * \ |
70 | * \ |
71 | * Arguments: @const pre_counterctx *ctx@ = pointer to counter \ |
72 | * context \ |
4efe32ba |
73 | * @void *iv@ = pointer to output data block \ |
35d66180 |
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##_countergetiv(const pre##_counterctx */*ctx*/, \ |
84 | void */*iv*/); \ |
85 | \ |
86 | /* --- @pre_countersetiv@ --- * \ |
87 | * \ |
88 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
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##_countersetiv(pre##_counterctx */*ctx*/, \ |
97 | const void */*iv*/); \ |
98 | \ |
99 | /* --- @pre_counterbdry@ --- * \ |
100 | * \ |
101 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
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##_counterbdry(pre##_counterctx */*ctx*/); \ |
110 | \ |
111 | /* --- @pre_countersetkey@ --- * \ |
112 | * \ |
113 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
114 | * @const pre_ctx *k@ = pointer to cipher context \ |
115 | * \ |
116 | * Returns: --- \ |
117 | * \ |
118 | * Use: Sets the counter context to use a different cipher key. \ |
119 | */ \ |
120 | \ |
121 | extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \ |
122 | const pre##_ctx */*k*/); \ |
123 | \ |
124 | /* --- @pre_counterinit@ --- * \ |
125 | * \ |
126 | * Arguments: @pre_counterctx *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 counter context ready for use. You \ |
134 | * should ensure that the IV chosen is unique: reusing an \ |
135 | * IV will compromise the security of the entire \ |
136 | * plaintext. This is equivalent to calls to @pre_init@, \ |
137 | * @pre_countersetkey@ and @pre_countersetiv@. \ |
138 | */ \ |
139 | \ |
140 | extern void pre##_counterinit(pre##_counterctx */*ctx*/, \ |
141 | const void */*key*/, size_t /*sz*/, \ |
b9a27a11 |
142 | const void */*iv*/); \ |
35d66180 |
143 | \ |
144 | /* --- @pre_counterencrypt@ --- * \ |
145 | * \ |
146 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
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 \ |
154 | * counter mode: encryption and decryption are the same in \ |
155 | * counter. The destination may be null to just churn the \ |
156 | * feedback round for a bit. The source may be null to \ |
157 | * use the cipher as a random data generator. \ |
158 | */ \ |
159 | \ |
160 | extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \ |
161 | const void */*src*/, void */*dest*/, \ |
162 | size_t /*sz*/); \ |
163 | \ |
164 | /* --- @pre_counterrand@ --- * \ |
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 | * counter-mode block cipher. \ |
173 | */ \ |
174 | \ |
175 | extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \ |
176 | \ |
177 | /* --- Generic cipher interface --- */ \ |
178 | \ |
179 | extern const gccipher pre##_counter; |
180 | |
181 | /*----- That's all, folks -------------------------------------------------*/ |
182 | |
183 | #ifdef __cplusplus |
184 | } |
185 | #endif |
186 | |
187 | #endif |