3 * $Id: counter.h,v 1.1 2000/06/17 10:51:42 mdw Exp $
5 * Block cipher counter mode (or long cycle mode)
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2000/06/17 10:51:42 mdw
34 * Counter mode ciphers and pseudo-random generator.
38 #ifndef CATACOMB_COUNTER_H
39 #define CATACOMB_COUNTER_H
45 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/bits.h>
51 #ifndef CATACOMB_GCIPHER_H
55 #ifndef CATACOMB_GRAND_H
59 /*----- Macros ------------------------------------------------------------*/
61 /* --- @COUNTER_DECL@ --- *
63 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
65 * Use: Makes declarations for counter mode.
68 #define COUNTER_DECL(PRE, pre) \
70 /* --- Counter mode context --- */ \
72 typedef struct pre##_counterctx { \
73 pre##_ctx ctx; /* Underlying cipher context */ \
74 unsigned off; /* Current offset in buffer */ \
75 octet buf[PRE##_BLKSZ]; /* Output buffer */ \
76 uint32 n[PRE##_BLKSZ / 4]; /* Counter */ \
79 /* --- @pre_countergetiv@ --- * \
81 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
83 * @void *iv#@ = pointer to output data block \
87 * Use: Reads the currently set IV. Reading and setting an IV \
88 * is not transparent to the cipher. It will add a `step' \
89 * which must be matched by a similar operation during \
93 extern void pre##_countergetiv(const pre##_counterctx */*ctx*/, \
96 /* --- @pre_countersetiv@ --- * \
98 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
99 * @cnost void *iv@ = pointer to IV to set \
103 * Use: Sets the IV to use for subsequent encryption. \
106 extern void pre##_countersetiv(pre##_counterctx */*ctx*/, \
107 const void */*iv*/); \
109 /* --- @pre_counterbdry@ --- * \
111 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
115 * Use: Inserts a boundary during encryption. Successful \
116 * decryption must place a similar boundary. \
119 extern void pre##_counterbdry(pre##_counterctx */*ctx*/); \
121 /* --- @pre_countersetkey@ --- * \
123 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
124 * @const pre_ctx *k@ = pointer to cipher context \
128 * Use: Sets the counter context to use a different cipher key. \
131 extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \
132 const pre##_ctx */*k*/); \
134 /* --- @pre_counterinit@ --- * \
136 * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \
137 * @const void *key@ = pointer to the key buffer \
138 * @size_t sz@ = size of the key \
139 * @const void *iv@ = pointer to initialization vector \
143 * Use: Initializes a counter context ready for use. You \
144 * should ensure that the IV chosen is unique: reusing an \
145 * IV will compromise the security of the entire \
146 * plaintext. This is equivalent to calls to @pre_init@, \
147 * @pre_countersetkey@ and @pre_countersetiv@. \
150 extern void pre##_counterinit(pre##_counterctx */*ctx*/, \
151 const void */*key*/, size_t /*sz*/, \
152 const void */*iv*/); \
154 /* --- @pre_counterencrypt@ --- * \
156 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
157 * @const void *src@ = pointer to source data \
158 * @void *dest@ = pointer to destination data \
159 * @size_t sz@ = size of block to be encrypted \
163 * Use: Encrypts or decrypts a block with a block cipher in \
164 * counter mode: encryption and decryption are the same in \
165 * counter. The destination may be null to just churn the \
166 * feedback round for a bit. The source may be null to \
167 * use the cipher as a random data generator. \
170 extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \
171 const void */*src*/, void */*dest*/, \
174 /* --- @pre_counterrand@ --- * \
176 * Arguments: @const void *k@ = pointer to key material \
177 * @size_t sz@ = size of key material \
179 * Returns: Pointer to generic random number generator interface. \
181 * Use: Creates a random number interface wrapper around an \
182 * counter-mode block cipher. \
185 extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \
187 /* --- Generic cipher interface --- */ \
189 extern const gccipher pre##_counter;
191 /*----- That's all, folks -------------------------------------------------*/