chiark / gitweb /
Version bump.
[catacomb] / cbc.h
1 /* -*-c-*-
2  *
3  * $Id: cbc.h,v 1.2 1999/12/10 23:16:39 mdw Exp $
4  *
5  * Ciphertext block chaining for block ciphers
6  *
7  * (c) 1999 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: cbc.h,v $
33  * Revision 1.2  1999/12/10 23:16:39  mdw
34  * Split mode macros into interface and implementation.
35  *
36  * Revision 1.1  1999/09/03 08:41:11  mdw
37  * Initial import.
38  *
39  */
40
41 #ifndef CATACOMB_CBC_H
42 #define CATACOMB_CBC_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 /*----- Macros ------------------------------------------------------------*/
59
60 /* --- @CBC_DECL@ --- *
61  *
62  * Arguments:   @PRE@, @pre@ = prefixes for the underlying block cipher
63  *
64  * Use:         Creates declarations for CBC stealing mode.
65  */
66
67 #define CBC_DECL(PRE, pre)                                              \
68                                                                         \
69 /* --- Cipher block chaining context --- */                             \
70                                                                         \
71 typedef struct pre##_cbcctx {                                           \
72   pre##_ctx ctx;                        /* Underlying cipher context */ \
73   uint32 iv[PRE##_BLKSZ / 4];           /* Previous ciphertext or IV */ \
74 } pre##_cbcctx;                                                         \
75                                                                         \
76 /* --- @pre_cbcgetiv@ --- *                                             \
77  *                                                                      \
78  * Arguments:   @const pre_cbcctx *ctx@ = pointer to CBC context block  \
79  *              @void *iv#@ = pointer to output data block              \
80  *                                                                      \
81  * Returns:     ---                                                     \
82  *                                                                      \
83  * Use:         Reads the currently set IV.  Reading and setting an IV  \
84  *              is transparent to the CBC encryption or decryption      \
85  *              process.                                                \
86  */                                                                     \
87                                                                         \
88 extern void pre##_cbcgetiv(const pre##_cbcctx */*ctx*/,                 \
89                            void */*iv*/);                               \
90                                                                         \
91 /* --- @pre_cbcsetiv@ --- *                                             \
92  *                                                                      \
93  * Arguments:   @pre_cbcctx *ctx@ = pointer to CBC context block        \
94  *              @cnost void *iv@ = pointer to IV to set                 \
95  *                                                                      \
96  * Returns:     ---                                                     \
97  *                                                                      \
98  * Use:         Sets the IV to use for subsequent encryption.           \
99  */                                                                     \
100                                                                         \
101 extern void pre##_cbcsetiv(pre##_cbcctx */*ctx*/,                       \
102                            const void */*iv*/);                         \
103                                                                         \
104 /* --- @pre_cbcsetkey@ --- *                                            \
105  *                                                                      \
106  * Arguments:   @pre_cbcctx *ctx@ = pointer to CBC context block        \
107  *              @const pre_ctx *k@ = pointer to cipher context          \
108  *                                                                      \
109  * Returns:     ---                                                     \
110  *                                                                      \
111  * Use:         Sets the CBC context to use a different cipher key.     \
112  */                                                                     \
113                                                                         \
114 extern void pre##_cbcsetkey(pre##_cbcctx */*ctx*/,                      \
115                             const pre##_ctx */*k*/);                    \
116                                                                         \
117 /* --- @pre_cbcinit@ --- *                                              \
118  *                                                                      \
119  * Arguments:   @pre_cbcctx *ctx@ = pointer to cipher context           \
120  *              @const void *key@ = pointer to the key buffer           \
121  *              @size_t sz@ = size of the key                           \
122  *              @const void *iv@ = pointer to initialization vector     \
123  *                                                                      \
124  * Returns:     ---                                                     \
125  *                                                                      \
126  * Use:         Initializes a CBC context ready for use.  The @iv@      \
127  *              argument may be passed as a null pointer to set a zero  \
128  *              IV.  Apart from that, this call is equivalent to calls  \
129  *              to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@.      \
130  */                                                                     \
131                                                                         \
132 extern void pre##_cbcinit(pre##_cbcctx */*ctx*/,                        \
133                           const void */*key*/, size_t /*sz*/,           \
134                           const void */*iv*/);                          \
135                                                                         \
136 /* --- @pre_cbcencrypt@ --- *                                           \
137  *                                                                      \
138  * Arguments:   @pre_cbcctx *ctx@ = pointer to CBC context block        \
139  *              @const void *src@ = pointer to source data              \
140  *              @void *dest@ = pointer to destination data              \
141  *              @size_t sz@ = size of block to be encrypted             \
142  *                                                                      \
143  * Returns:     ---                                                     \
144  *                                                                      \
145  * Use:         Encrypts a block with a block cipher in CBC mode, with  \
146  *              ciphertext stealing and other clever tricks.            \
147  *              Essentially, data can be encrypted in arbitrary sized   \
148  *              chunks, although decryption must use the same chunks.   \
149  */                                                                     \
150                                                                         \
151 extern void pre##_cbcencrypt(pre##_cbcctx */*ctx*/,                     \
152                              const void */*src*/, void */*dest*/,       \
153                              size_t /*sz*/);                            \
154                                                                         \
155 /* --- @pre_cbcdecrypt@ --- *                                           \
156  *                                                                      \
157  * Arguments:   @pre_cbcctx *ctx@ = pointer to CBC context block        \
158  *              @const void *src@ = pointer to source data              \
159  *              @void *dest@ = pointer to destination data              \
160  *              @size_t sz@ = size of block to be encrypted             \
161  *                                                                      \
162  * Returns:     ---                                                     \
163  *                                                                      \
164  * Use:         Decrypts a block with a block cipher in CBC mode, with  \
165  *              ciphertext stealing and other clever tricks.            \
166  *              Essentially, data can be encrypted in arbitrary sized   \
167  *              chunks, although decryption must use the same chunks.   \
168  */                                                                     \
169                                                                         \
170 extern void pre##_cbcdecrypt(pre##_cbcctx */*ctx*/,                     \
171                              const void */*src*/, void */*dest*/,       \
172                              size_t /*sz*/);                            \
173                                                                         \
174 /* --- Generic cipher interface --- */                                  \
175                                                                         \
176 extern const gccipher pre##_cbc;
177
178 /*----- That's all, folks -------------------------------------------------*/
179
180 #ifdef __cplusplus
181   }
182 #endif
183
184 #endif