3 * $Id: cbc.h,v 1.4 2004/04/08 01:36:15 mdw Exp $
5 * Ciphertext block chaining for block ciphers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 #ifndef CATACOMB_CBC_H
31 #define CATACOMB_CBC_H
37 /*----- Header files ------------------------------------------------------*/
41 #include <mLib/bits.h>
43 #ifndef CATACOMB_GCIPHER_H
47 /*----- Macros ------------------------------------------------------------*/
49 /* --- @CBC_DECL@ --- *
51 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
53 * Use: Creates declarations for CBC stealing mode.
56 #define CBC_DECL(PRE, pre) \
58 /* --- Cipher block chaining context --- */ \
60 typedef struct pre##_cbcctx { \
61 pre##_ctx ctx; /* Underlying cipher context */ \
62 uint32 iv[PRE##_BLKSZ / 4]; /* Previous ciphertext or IV */ \
65 /* --- @pre_cbcgetiv@ --- * \
67 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
68 * @void *iv@ = pointer to output data block \
72 * Use: Reads the currently set IV. Reading and setting an IV \
73 * is transparent to the CBC encryption or decryption \
77 extern void pre##_cbcgetiv(const pre##_cbcctx */*ctx*/, \
80 /* --- @pre_cbcsetiv@ --- * \
82 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
83 * @cnost void *iv@ = pointer to IV to set \
87 * Use: Sets the IV to use for subsequent encryption. \
90 extern void pre##_cbcsetiv(pre##_cbcctx */*ctx*/, \
91 const void */*iv*/); \
93 /* --- @pre_cbcsetkey@ --- * \
95 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
96 * @const pre_ctx *k@ = pointer to cipher context \
100 * Use: Sets the CBC context to use a different cipher key. \
103 extern void pre##_cbcsetkey(pre##_cbcctx */*ctx*/, \
104 const pre##_ctx */*k*/); \
106 /* --- @pre_cbcinit@ --- * \
108 * Arguments: @pre_cbcctx *ctx@ = pointer to cipher context \
109 * @const void *key@ = pointer to the key buffer \
110 * @size_t sz@ = size of the key \
111 * @const void *iv@ = pointer to initialization vector \
115 * Use: Initializes a CBC context ready for use. The @iv@ \
116 * argument may be passed as a null pointer to set a zero \
117 * IV. Apart from that, this call is equivalent to calls \
118 * to @pre_init@, @pre_cbcsetkey@ and @pre_cbcsetiv@. \
121 extern void pre##_cbcinit(pre##_cbcctx */*ctx*/, \
122 const void */*key*/, size_t /*sz*/, \
123 const void */*iv*/); \
125 /* --- @pre_cbcencrypt@ --- * \
127 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
128 * @const void *src@ = pointer to source data \
129 * @void *dest@ = pointer to destination data \
130 * @size_t sz@ = size of block to be encrypted \
134 * Use: Encrypts a block with a block cipher in CBC mode, with \
135 * ciphertext stealing and other clever tricks. \
136 * Essentially, data can be encrypted in arbitrary sized \
137 * chunks, although decryption must use the same chunks. \
140 extern void pre##_cbcencrypt(pre##_cbcctx */*ctx*/, \
141 const void */*src*/, void */*dest*/, \
144 /* --- @pre_cbcdecrypt@ --- * \
146 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
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: Decrypts a block with a block cipher in CBC mode, with \
154 * ciphertext stealing and other clever tricks. \
155 * Essentially, data can be encrypted in arbitrary sized \
156 * chunks, although decryption must use the same chunks. \
159 extern void pre##_cbcdecrypt(pre##_cbcctx */*ctx*/, \
160 const void */*src*/, void */*dest*/, \
163 /* --- Generic cipher interface --- */ \
165 extern const gccipher pre##_cbc;
167 /*----- That's all, folks -------------------------------------------------*/