3 * $Id: counter.h,v 1.4 2004/04/08 01:36:15 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 #ifndef CATACOMB_COUNTER_H
31 #define CATACOMB_COUNTER_H
37 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
43 #ifndef CATACOMB_GCIPHER_H
47 #ifndef CATACOMB_GRAND_H
51 /*----- Macros ------------------------------------------------------------*/
53 /* --- @COUNTER_DECL@ --- *
55 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
57 * Use: Makes declarations for counter mode.
60 #define COUNTER_DECL(PRE, pre) \
62 /* --- Counter mode context --- */ \
64 typedef struct pre##_counterctx { \
65 pre##_ctx ctx; /* Underlying cipher context */ \
66 unsigned off; /* Current offset in buffer */ \
67 octet buf[PRE##_BLKSZ]; /* Output buffer */ \
68 uint32 n[PRE##_BLKSZ / 4]; /* Counter */ \
71 /* --- @pre_countergetiv@ --- * \
73 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
75 * @void *iv@ = pointer to output data block \
79 * Use: Reads the currently set IV. Reading and setting an IV \
80 * is not transparent to the cipher. It will add a `step' \
81 * which must be matched by a similar operation during \
85 extern void pre##_countergetiv(const pre##_counterctx */*ctx*/, \
88 /* --- @pre_countersetiv@ --- * \
90 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
91 * @cnost void *iv@ = pointer to IV to set \
95 * Use: Sets the IV to use for subsequent encryption. \
98 extern void pre##_countersetiv(pre##_counterctx */*ctx*/, \
99 const void */*iv*/); \
101 /* --- @pre_counterbdry@ --- * \
103 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
107 * Use: Inserts a boundary during encryption. Successful \
108 * decryption must place a similar boundary. \
111 extern void pre##_counterbdry(pre##_counterctx */*ctx*/); \
113 /* --- @pre_countersetkey@ --- * \
115 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
116 * @const pre_ctx *k@ = pointer to cipher context \
120 * Use: Sets the counter context to use a different cipher key. \
123 extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \
124 const pre##_ctx */*k*/); \
126 /* --- @pre_counterinit@ --- * \
128 * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \
129 * @const void *key@ = pointer to the key buffer \
130 * @size_t sz@ = size of the key \
131 * @const void *iv@ = pointer to initialization vector \
135 * Use: Initializes a counter context ready for use. You \
136 * should ensure that the IV chosen is unique: reusing an \
137 * IV will compromise the security of the entire \
138 * plaintext. This is equivalent to calls to @pre_init@, \
139 * @pre_countersetkey@ and @pre_countersetiv@. \
142 extern void pre##_counterinit(pre##_counterctx */*ctx*/, \
143 const void */*key*/, size_t /*sz*/, \
144 const void */*iv*/); \
146 /* --- @pre_counterencrypt@ --- * \
148 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
149 * @const void *src@ = pointer to source data \
150 * @void *dest@ = pointer to destination data \
151 * @size_t sz@ = size of block to be encrypted \
155 * Use: Encrypts or decrypts a block with a block cipher in \
156 * counter mode: encryption and decryption are the same in \
157 * counter. The destination may be null to just churn the \
158 * feedback round for a bit. The source may be null to \
159 * use the cipher as a random data generator. \
162 extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \
163 const void */*src*/, void */*dest*/, \
166 /* --- @pre_counterrand@ --- * \
168 * Arguments: @const void *k@ = pointer to key material \
169 * @size_t sz@ = size of key material \
171 * Returns: Pointer to generic random number generator interface. \
173 * Use: Creates a random number interface wrapper around an \
174 * counter-mode block cipher. \
177 extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \
179 /* --- Generic cipher interface --- */ \
181 extern const gccipher pre##_counter;
183 /*----- That's all, folks -------------------------------------------------*/