chiark / gitweb /
Version bump.
[catacomb] / counter.h
1 /* -*-c-*-
2  *
3  * $Id: counter.h,v 1.1 2000/06/17 10:51:42 mdw Exp $
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 $
33  * Revision 1.1  2000/06/17 10:51:42  mdw
34  * Counter mode ciphers and pseudo-random generator.
35  *
36  */
37
38 #ifndef CATACOMB_COUNTER_H
39 #define CATACOMB_COUNTER_H
40
41 #ifdef __cplusplus
42   extern "C" {
43 #endif
44
45 /*----- Header files ------------------------------------------------------*/
46
47 #include <stddef.h>
48
49 #include <mLib/bits.h>
50
51 #ifndef CATACOMB_GCIPHER_H
52 #  include "gcipher.h"
53 #endif
54
55 #ifndef CATACOMB_GRAND_H
56 #  include "grand.h"
57 #endif
58
59 /*----- Macros ------------------------------------------------------------*/
60
61 /* --- @COUNTER_DECL@ --- *
62  *
63  * Arguments:   @PRE@, @pre@ = prefixes for block cipher definitions
64  *
65  * Use:         Makes declarations for counter mode.
66  */
67
68 #define COUNTER_DECL(PRE, pre)                                          \
69                                                                         \
70 /* --- Counter mode context --- */                                      \
71                                                                         \
72 typedef struct pre##_counterctx {                                       \
73   pre##_ctx ctx;                        /* Underlying cipher context */ \
74   unsigned off;                         /* Current offset in buffer */  \
75   octet buf[PRE##_BLKSZ];               /* Output buffer */             \
76   uint32 n[PRE##_BLKSZ / 4];            /* Counter */                   \
77 } pre##_counterctx;                                                     \
78                                                                         \
79 /* --- @pre_countergetiv@ --- *                                         \
80  *                                                                      \
81  * Arguments:   @const pre_counterctx *ctx@ = pointer to counter        \
82  *                      context                                         \
83  *              @void *iv#@ = pointer to output data block              \
84  *                                                                      \
85  * Returns:     ---                                                     \
86  *                                                                      \
87  * Use:         Reads the currently set IV.  Reading and setting an IV  \
88  *              is not transparent to the cipher.  It will add a `step' \
89  *              which must be matched by a similar operation during     \
90  *              decryption.                                             \
91  */                                                                     \
92                                                                         \
93 extern void pre##_countergetiv(const pre##_counterctx */*ctx*/,         \
94                                void */*iv*/);                           \
95                                                                         \
96 /* --- @pre_countersetiv@ --- *                                         \
97  *                                                                      \
98  * Arguments:   @pre_counterctx *ctx@ = pointer to counter context      \
99  *              @cnost void *iv@ = pointer to IV to set                 \
100  *                                                                      \
101  * Returns:     ---                                                     \
102  *                                                                      \
103  * Use:         Sets the IV to use for subsequent encryption.           \
104  */                                                                     \
105                                                                         \
106 extern void pre##_countersetiv(pre##_counterctx */*ctx*/,               \
107                                const void */*iv*/);                     \
108                                                                         \
109 /* --- @pre_counterbdry@ --- *                                          \
110  *                                                                      \
111  * Arguments:   @pre_counterctx *ctx@ = pointer to counter context      \
112  *                                                                      \
113  * Returns:     ---                                                     \
114  *                                                                      \
115  * Use:         Inserts a boundary during encryption.  Successful       \
116  *              decryption must place a similar boundary.               \
117  */                                                                     \
118                                                                         \
119 extern void pre##_counterbdry(pre##_counterctx */*ctx*/);               \
120                                                                         \
121 /* --- @pre_countersetkey@ --- *                                        \
122  *                                                                      \
123  * Arguments:   @pre_counterctx *ctx@ = pointer to counter context      \
124  *              @const pre_ctx *k@ = pointer to cipher context          \
125  *                                                                      \
126  * Returns:     ---                                                     \
127  *                                                                      \
128  * Use:         Sets the counter context to use a different cipher key. \
129  */                                                                     \
130                                                                         \
131 extern void pre##_countersetkey(pre##_counterctx */*ctx*/,              \
132                                 const pre##_ctx */*k*/);                \
133                                                                         \
134 /* --- @pre_counterinit@ --- *                                          \
135  *                                                                      \
136  * Arguments:   @pre_counterctx *ctx@ = pointer to cipher context       \
137  *              @const void *key@ = pointer to the key buffer           \
138  *              @size_t sz@ = size of the key                           \
139  *              @const void *iv@ = pointer to initialization vector     \
140  *                                                                      \
141  * Returns:     ---                                                     \
142  *                                                                      \
143  * Use:         Initializes a counter context ready for use.  You       \
144  *              should ensure that the IV chosen is unique: reusing an  \
145  *              IV will compromise the security of the entire           \
146  *              plaintext.  This is equivalent to calls to @pre_init@,  \
147  *              @pre_countersetkey@ and @pre_countersetiv@.             \
148  */                                                                     \
149                                                                         \
150 extern void pre##_counterinit(pre##_counterctx */*ctx*/,                \
151                               const void */*key*/, size_t /*sz*/,       \
152                           const void */*iv*/);                          \
153                                                                         \
154 /* --- @pre_counterencrypt@ --- *                                       \
155  *                                                                      \
156  * Arguments:   @pre_counterctx *ctx@ = pointer to counter context      \
157  *              @const void *src@ = pointer to source data              \
158  *              @void *dest@ = pointer to destination data              \
159  *              @size_t sz@ = size of block to be encrypted             \
160  *                                                                      \
161  * Returns:     ---                                                     \
162  *                                                                      \
163  * Use:         Encrypts or decrypts a block with a block cipher in     \
164  *              counter mode: encryption and decryption are the same in \
165  *              counter.  The destination may be null to just churn the \
166  *              feedback round for a bit.  The source may be null to    \
167  *              use the cipher as a random data generator.              \
168  */                                                                     \
169                                                                         \
170 extern void pre##_counterencrypt(pre##_counterctx */*ctx*/,             \
171                                  const void */*src*/, void */*dest*/,   \
172                                  size_t /*sz*/);                        \
173                                                                         \
174 /* --- @pre_counterrand@ --- *                                          \
175  *                                                                      \
176  * Arguments:   @const void *k@ = pointer to key material               \
177  *              @size_t sz@ = size of key material                      \
178  *                                                                      \
179  * Returns:     Pointer to generic random number generator interface.   \
180  *                                                                      \
181  * Use:         Creates a random number interface wrapper around an     \
182  *              counter-mode block cipher.                              \
183  */                                                                     \
184                                                                         \
185 extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/);      \
186                                                                         \
187 /* --- Generic cipher interface --- */                                  \
188                                                                         \
189 extern const gccipher pre##_counter;
190
191 /*----- That's all, folks -------------------------------------------------*/
192
193 #ifdef __cplusplus
194   }
195 #endif
196
197 #endif