3 * Ciphertext block chaining for block ciphers
5 * (c) 1999 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_CBC_H
29 #define CATACOMB_CBC_H
35 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/bits.h>
41 #ifndef CATACOMB_GCIPHER_H
45 /*----- Macros ------------------------------------------------------------*/
47 /* --- @CBC_DECL@ --- *
49 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
51 * Use: Creates declarations for CBC stealing mode.
54 #define CBC_DECL(PRE, pre) \
56 /* --- Cipher block chaining context --- */ \
58 typedef struct pre##_cbcctx { \
59 pre##_ctx ctx; /* Underlying cipher context */ \
60 uint32 iv[PRE##_BLKSZ / 4]; /* Previous ciphertext or IV */ \
63 /* --- @pre_cbcgetiv@ --- * \
65 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
66 * @void *iv@ = pointer to output data block \
70 * Use: Reads the currently set IV. Reading and setting an IV \
71 * is transparent to the CBC encryption or decryption \
75 extern void pre##_cbcgetiv(const pre##_cbcctx */*ctx*/, \
78 /* --- @pre_cbcsetiv@ --- * \
80 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
81 * @cnost void *iv@ = pointer to IV to set \
85 * Use: Sets the IV to use for subsequent encryption. \
88 extern void pre##_cbcsetiv(pre##_cbcctx */*ctx*/, \
89 const void */*iv*/); \
91 /* --- @pre_cbcsetkey@ --- * \
93 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
94 * @const pre_ctx *k@ = pointer to cipher context \
98 * Use: Sets the CBC context to use a different cipher key. \
101 extern void pre##_cbcsetkey(pre##_cbcctx */*ctx*/, \
102 const pre##_ctx */*k*/); \
104 /* --- @pre_cbcinit@ --- * \
106 * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \
107 * @const void *key@ = pointer to the key buffer \
108 * @size_t sz@ = size of the key \
109 * @const void *iv@ = pointer to initialization vector \
113 * Use: Initializes a CBC context ready for use. The @iv@ \
114 * argument may be passed as a null pointer to set a zero \
115 * IV. Apart from that, this call is equivalent to calls \
116 * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \
119 extern void pre##_cbcinit(pre##_cbcctx */*ctx*/, \
120 const void */*key*/, size_t /*sz*/, \
121 const void */*iv*/); \
123 /* --- @pre_cbcencrypt@ --- * \
125 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
126 * @const void *src@ = pointer to source data \
127 * @void *dest@ = pointer to destination data \
128 * @size_t sz@ = size of block to be encrypted \
132 * Use: Encrypts a block with a block cipher in CBC mode, with \
133 * ciphertext stealing and other clever tricks. \
134 * Essentially, data can be encrypted in arbitrary sized \
135 * chunks, although decryption must use the same chunks. \
138 extern void pre##_cbcencrypt(pre##_cbcctx */*ctx*/, \
139 const void */*src*/, void */*dest*/, \
142 /* --- @pre_cbcdecrypt@ --- * \
144 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
145 * @const void *src@ = pointer to source data \
146 * @void *dest@ = pointer to destination data \
147 * @size_t sz@ = size of block to be encrypted \
151 * Use: Decrypts a block with a block cipher in CBC mode, with \
152 * ciphertext stealing and other clever tricks. \
153 * Essentially, data can be encrypted in arbitrary sized \
154 * chunks, although decryption must use the same chunks. \
157 extern void pre##_cbcdecrypt(pre##_cbcctx */*ctx*/, \
158 const void */*src*/, void */*dest*/, \
161 /* --- Generic cipher interface --- */ \
163 extern const gccipher pre##_cbc;
165 /*----- That's all, folks -------------------------------------------------*/