3 * $Id: counter.h,v 1.2 2001/06/17 00:10:51 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.2 2001/06/17 00:10:51 mdw
36 * Revision 1.1 2000/06/17 10:51:42 mdw
37 * Counter mode ciphers and pseudo-random generator.
41 #ifndef CATACOMB_COUNTER_H
42 #define CATACOMB_COUNTER_H
48 /*----- Header files ------------------------------------------------------*/
52 #include <mLib/bits.h>
54 #ifndef CATACOMB_GCIPHER_H
58 #ifndef CATACOMB_GRAND_H
62 /*----- Macros ------------------------------------------------------------*/
64 /* --- @COUNTER_DECL@ --- *
66 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
68 * Use: Makes declarations for counter mode.
71 #define COUNTER_DECL(PRE, pre) \
73 /* --- Counter mode context --- */ \
75 typedef struct pre##_counterctx { \
76 pre##_ctx ctx; /* Underlying cipher context */ \
77 unsigned off; /* Current offset in buffer */ \
78 octet buf[PRE##_BLKSZ]; /* Output buffer */ \
79 uint32 n[PRE##_BLKSZ / 4]; /* Counter */ \
82 /* --- @pre_countergetiv@ --- * \
84 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
86 * @void *iv@ = pointer to output data block \
90 * Use: Reads the currently set IV. Reading and setting an IV \
91 * is not transparent to the cipher. It will add a `step' \
92 * which must be matched by a similar operation during \
96 extern void pre##_countergetiv(const pre##_counterctx */*ctx*/, \
99 /* --- @pre_countersetiv@ --- * \
101 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
102 * @cnost void *iv@ = pointer to IV to set \
106 * Use: Sets the IV to use for subsequent encryption. \
109 extern void pre##_countersetiv(pre##_counterctx */*ctx*/, \
110 const void */*iv*/); \
112 /* --- @pre_counterbdry@ --- * \
114 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
118 * Use: Inserts a boundary during encryption. Successful \
119 * decryption must place a similar boundary. \
122 extern void pre##_counterbdry(pre##_counterctx */*ctx*/); \
124 /* --- @pre_countersetkey@ --- * \
126 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
127 * @const pre_ctx *k@ = pointer to cipher context \
131 * Use: Sets the counter context to use a different cipher key. \
134 extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \
135 const pre##_ctx */*k*/); \
137 /* --- @pre_counterinit@ --- * \
139 * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \
140 * @const void *key@ = pointer to the key buffer \
141 * @size_t sz@ = size of the key \
142 * @const void *iv@ = pointer to initialization vector \
146 * Use: Initializes a counter context ready for use. You \
147 * should ensure that the IV chosen is unique: reusing an \
148 * IV will compromise the security of the entire \
149 * plaintext. This is equivalent to calls to @pre_init@, \
150 * @pre_countersetkey@ and @pre_countersetiv@. \
153 extern void pre##_counterinit(pre##_counterctx */*ctx*/, \
154 const void */*key*/, size_t /*sz*/, \
155 const void */*iv*/); \
157 /* --- @pre_counterencrypt@ --- * \
159 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
160 * @const void *src@ = pointer to source data \
161 * @void *dest@ = pointer to destination data \
162 * @size_t sz@ = size of block to be encrypted \
166 * Use: Encrypts or decrypts a block with a block cipher in \
167 * counter mode: encryption and decryption are the same in \
168 * counter. The destination may be null to just churn the \
169 * feedback round for a bit. The source may be null to \
170 * use the cipher as a random data generator. \
173 extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \
174 const void */*src*/, void */*dest*/, \
177 /* --- @pre_counterrand@ --- * \
179 * Arguments: @const void *k@ = pointer to key material \
180 * @size_t sz@ = size of key material \
182 * Returns: Pointer to generic random number generator interface. \
184 * Use: Creates a random number interface wrapper around an \
185 * counter-mode block cipher. \
188 extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \
190 /* --- Generic cipher interface --- */ \
192 extern const gccipher pre##_counter;
194 /*----- That's all, folks -------------------------------------------------*/