3 * $Id: counter.h,v 1.3 2002/01/13 13:43:35 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.3 2002/01/13 13:43:35 mdw
36 * Revision 1.2 2001/06/17 00:10:51 mdw
39 * Revision 1.1 2000/06/17 10:51:42 mdw
40 * Counter mode ciphers and pseudo-random generator.
44 #ifndef CATACOMB_COUNTER_H
45 #define CATACOMB_COUNTER_H
51 /*----- Header files ------------------------------------------------------*/
55 #include <mLib/bits.h>
57 #ifndef CATACOMB_GCIPHER_H
61 #ifndef CATACOMB_GRAND_H
65 /*----- Macros ------------------------------------------------------------*/
67 /* --- @COUNTER_DECL@ --- *
69 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
71 * Use: Makes declarations for counter mode.
74 #define COUNTER_DECL(PRE, pre) \
76 /* --- Counter mode context --- */ \
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 */ \
85 /* --- @pre_countergetiv@ --- * \
87 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
89 * @void *iv@ = pointer to output data block \
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 \
99 extern void pre##_countergetiv(const pre##_counterctx */*ctx*/, \
102 /* --- @pre_countersetiv@ --- * \
104 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
105 * @cnost void *iv@ = pointer to IV to set \
109 * Use: Sets the IV to use for subsequent encryption. \
112 extern void pre##_countersetiv(pre##_counterctx */*ctx*/, \
113 const void */*iv*/); \
115 /* --- @pre_counterbdry@ --- * \
117 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
121 * Use: Inserts a boundary during encryption. Successful \
122 * decryption must place a similar boundary. \
125 extern void pre##_counterbdry(pre##_counterctx */*ctx*/); \
127 /* --- @pre_countersetkey@ --- * \
129 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
130 * @const pre_ctx *k@ = pointer to cipher context \
134 * Use: Sets the counter context to use a different cipher key. \
137 extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \
138 const pre##_ctx */*k*/); \
140 /* --- @pre_counterinit@ --- * \
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 \
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@. \
156 extern void pre##_counterinit(pre##_counterctx */*ctx*/, \
157 const void */*key*/, size_t /*sz*/, \
158 const void */*iv*/); \
160 /* --- @pre_counterencrypt@ --- * \
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 \
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. \
176 extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \
177 const void */*src*/, void */*dest*/, \
180 /* --- @pre_counterrand@ --- * \
182 * Arguments: @const void *k@ = pointer to key material \
183 * @size_t sz@ = size of key material \
185 * Returns: Pointer to generic random number generator interface. \
187 * Use: Creates a random number interface wrapper around an \
188 * counter-mode block cipher. \
191 extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \
193 /* --- Generic cipher interface --- */ \
195 extern const gccipher pre##_counter;
197 /*----- That's all, folks -------------------------------------------------*/