More efficient Rabin-Miller test: with random witnesses, skip redundant
[u/mdw/catacomb] / counter.h
CommitLineData
35d66180 1/* -*-c-*-
2 *
4efe32ba 3 * $Id: counter.h,v 1.2 2001/06/17 00:10:51 mdw Exp $
35d66180 4 *
5 * Block cipher counter mode (or long cycle mode)
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: counter.h,v $
4efe32ba 33 * Revision 1.2 2001/06/17 00:10:51 mdw
34 * Typesetting fixes
35 *
35d66180 36 * Revision 1.1 2000/06/17 10:51:42 mdw
37 * Counter mode ciphers and pseudo-random generator.
38 *
39 */
40
41#ifndef CATACOMB_COUNTER_H
42#define CATACOMB_COUNTER_H
43
44#ifdef __cplusplus
45 extern "C" {
46#endif
47
48/*----- Header files ------------------------------------------------------*/
49
50#include <stddef.h>
51
52#include <mLib/bits.h>
53
54#ifndef CATACOMB_GCIPHER_H
55# include "gcipher.h"
56#endif
57
58#ifndef CATACOMB_GRAND_H
59# include "grand.h"
60#endif
61
62/*----- Macros ------------------------------------------------------------*/
63
64/* --- @COUNTER_DECL@ --- *
65 *
66 * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions
67 *
68 * Use: Makes declarations for counter mode.
69 */
70
71#define COUNTER_DECL(PRE, pre) \
72 \
73/* --- Counter mode context --- */ \
74 \
75typedef 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 */ \
80} pre##_counterctx; \
81 \
82/* --- @pre_countergetiv@ --- * \
83 * \
84 * Arguments: @const pre_counterctx *ctx@ = pointer to counter \
85 * context \
4efe32ba 86 * @void *iv@ = pointer to output data block \
35d66180 87 * \
88 * Returns: --- \
89 * \
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 \
93 * decryption. \
94 */ \
95 \
96extern void pre##_countergetiv(const pre##_counterctx */*ctx*/, \
97 void */*iv*/); \
98 \
99/* --- @pre_countersetiv@ --- * \
100 * \
101 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
102 * @cnost void *iv@ = pointer to IV to set \
103 * \
104 * Returns: --- \
105 * \
106 * Use: Sets the IV to use for subsequent encryption. \
107 */ \
108 \
109extern void pre##_countersetiv(pre##_counterctx */*ctx*/, \
110 const void */*iv*/); \
111 \
112/* --- @pre_counterbdry@ --- * \
113 * \
114 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
115 * \
116 * Returns: --- \
117 * \
118 * Use: Inserts a boundary during encryption. Successful \
119 * decryption must place a similar boundary. \
120 */ \
121 \
122extern void pre##_counterbdry(pre##_counterctx */*ctx*/); \
123 \
124/* --- @pre_countersetkey@ --- * \
125 * \
126 * Arguments: @pre_counterctx *ctx@ = pointer to counter context \
127 * @const pre_ctx *k@ = pointer to cipher context \
128 * \
129 * Returns: --- \
130 * \
131 * Use: Sets the counter context to use a different cipher key. \
132 */ \
133 \
134extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \
135 const pre##_ctx */*k*/); \
136 \
137/* --- @pre_counterinit@ --- * \
138 * \
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 \
143 * \
144 * Returns: --- \
145 * \
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@. \
151 */ \
152 \
153extern void pre##_counterinit(pre##_counterctx */*ctx*/, \
154 const void */*key*/, size_t /*sz*/, \
155 const void */*iv*/); \
156 \
157/* --- @pre_counterencrypt@ --- * \
158 * \
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 \
163 * \
164 * Returns: --- \
165 * \
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. \
171 */ \
172 \
173extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \
174 const void */*src*/, void */*dest*/, \
175 size_t /*sz*/); \
176 \
177/* --- @pre_counterrand@ --- * \
178 * \
179 * Arguments: @const void *k@ = pointer to key material \
180 * @size_t sz@ = size of key material \
181 * \
182 * Returns: Pointer to generic random number generator interface. \
183 * \
184 * Use: Creates a random number interface wrapper around an \
185 * counter-mode block cipher. \
186 */ \
187 \
188extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \
189 \
190/* --- Generic cipher interface --- */ \
191 \
192extern const gccipher pre##_counter;
193
194/*----- That's all, folks -------------------------------------------------*/
195
196#ifdef __cplusplus
197 }
198#endif
199
200#endif