3 * Block cipher counter mode (or long cycle mode)
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_COUNTER_H
29 #define CATACOMB_COUNTER_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/bits.h>
41 #ifndef CATACOMB_GCIPHER_H
45 #ifndef CATACOMB_GRAND_H
49 /*----- Macros ------------------------------------------------------------*/
51 /* --- @COUNTER_DECL@ --- *
53 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
55 * Use: Makes declarations for counter mode.
58 #define COUNTER_DECL(PRE, pre) \
60 /* --- Counter mode context --- */ \
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 */ \
69 /* --- @pre_countergetiv@ --- * \
71 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
73 * @void *iv@ = pointer to output data block \
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 \
83 extern void pre##_countergetiv(const pre##_counterctx */*ctx*/, \
86 /* --- @pre_countersetiv@ --- * \
88 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
89 * @cnost void *iv@ = pointer to IV to set \
93 * Use: Sets the IV to use for subsequent encryption. \
96 extern void pre##_countersetiv(pre##_counterctx */*ctx*/, \
97 const void */*iv*/); \
99 /* --- @pre_counterbdry@ --- * \
101 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
105 * Use: Inserts a boundary during encryption. Successful \
106 * decryption must place a similar boundary. \
109 extern void pre##_counterbdry(pre##_counterctx */*ctx*/); \
111 /* --- @pre_countersetkey@ --- * \
113 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
114 * @const pre_ctx *k@ = pointer to cipher context \
118 * Use: Sets the counter context to use a different cipher key. \
121 extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \
122 const pre##_ctx */*k*/); \
124 /* --- @pre_counterinit@ --- * \
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 \
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@. \
140 extern void pre##_counterinit(pre##_counterctx */*ctx*/, \
141 const void */*key*/, size_t /*sz*/, \
142 const void */*iv*/); \
144 /* --- @pre_counterencrypt@ --- * \
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 \
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. \
160 extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \
161 const void */*src*/, void */*dest*/, \
164 /* --- @pre_counterrand@ --- * \
166 * Arguments: @const void *k@ = pointer to key material \
167 * @size_t sz@ = size of key material \
169 * Returns: Pointer to generic random number generator interface. \
171 * Use: Creates a random number interface wrapper around an \
172 * counter-mode block cipher. \
175 extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \
177 /* --- Generic cipher interface --- */ \
179 extern const gccipher pre##_counter;
181 /*----- That's all, folks -------------------------------------------------*/