.gitignore: Ignore `ylwrap'.
[u/mdw/catacomb] / cbc.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: cbc.h,v 1.4 2004/04/08 01:36:15 mdw Exp $
d03ab969 4 *
5 * Ciphertext block chaining for block ciphers
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
d03ab969 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.
45c0fd36 18 *
d03ab969 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.
45c0fd36 23 *
d03ab969 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
79ba130c 30#ifndef CATACOMB_CBC_H
31#define CATACOMB_CBC_H
d03ab969 32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
79ba130c 39#include <stddef.h>
d03ab969 40
41#include <mLib/bits.h>
42
79ba130c 43#ifndef CATACOMB_GCIPHER_H
44# include "gcipher.h"
d03ab969 45#endif
46
47/*----- Macros ------------------------------------------------------------*/
48
49/* --- @CBC_DECL@ --- *
50 *
51 * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher
52 *
53 * Use: Creates declarations for CBC stealing mode.
54 */
55
56#define CBC_DECL(PRE, pre) \
57 \
79ba130c 58/* --- Cipher block chaining context --- */ \
d03ab969 59 \
79ba130c 60typedef struct pre##_cbcctx { \
61 pre##_ctx ctx; /* Underlying cipher context */ \
62 uint32 iv[PRE##_BLKSZ / 4]; /* Previous ciphertext or IV */ \
63} pre##_cbcctx; \
d03ab969 64 \
65/* --- @pre_cbcgetiv@ --- * \
66 * \
67 * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \
4efe32ba 68 * @void *iv@ = pointer to output data block \
d03ab969 69 * \
70 * Returns: --- \
71 * \
72 * Use: Reads the currently set IV. Reading and setting an IV \
73 * is transparent to the CBC encryption or decryption \
74 * process. \
75 */ \
76 \
79ba130c 77extern void pre##_cbcgetiv(const pre##_cbcctx */*ctx*/, \
78 void */*iv*/); \
d03ab969 79 \
80/* --- @pre_cbcsetiv@ --- * \
81 * \
82 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
83 * @cnost void *iv@ = pointer to IV to set \
84 * \
85 * Returns: --- \
86 * \
87 * Use: Sets the IV to use for subsequent encryption. \
88 */ \
89 \
79ba130c 90extern void pre##_cbcsetiv(pre##_cbcctx */*ctx*/, \
91 const void */*iv*/); \
d03ab969 92 \
93/* --- @pre_cbcsetkey@ --- * \
94 * \
95 * Arguments: @pre_cbcctx *ctx@ = pointer to CBC context block \
96 * @const pre_ctx *k@ = pointer to cipher context \
97 * \
98 * Returns: --- \
99 * \
100 * Use: Sets the CBC context to use a different cipher key. \
101 */ \
102 \
79ba130c 103extern void pre##_cbcsetkey(pre##_cbcctx */*ctx*/, \
104 const pre##_ctx */*k*/); \
d03ab969 105 \
106/* --- @pre_cbcinit@ --- * \
107 * \
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 \
112 * \
113 * Returns: --- \
114 * \
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@. \
119 */ \
120 \
79ba130c 121extern void pre##_cbcinit(pre##_cbcctx */*ctx*/, \
122 const void */*key*/, size_t /*sz*/, \
123 const void */*iv*/); \
d03ab969 124 \
125/* --- @pre_cbcencrypt@ --- * \
126 * \
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 \
131 * \
132 * Returns: --- \
133 * \
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. \
138 */ \
139 \
79ba130c 140extern void pre##_cbcencrypt(pre##_cbcctx */*ctx*/, \
141 const void */*src*/, void */*dest*/, \
142 size_t /*sz*/); \
d03ab969 143 \
144/* --- @pre_cbcdecrypt@ --- * \
145 * \
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 \
150 * \
151 * Returns: --- \
152 * \
79ba130c 153 * Use: Decrypts a block with a block cipher in CBC mode, with \
d03ab969 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. \
157 */ \
158 \
79ba130c 159extern void pre##_cbcdecrypt(pre##_cbcctx */*ctx*/, \
160 const void */*src*/, void */*dest*/, \
161 size_t /*sz*/); \
d03ab969 162 \
79ba130c 163/* --- Generic cipher interface --- */ \
d03ab969 164 \
79ba130c 165extern const gccipher pre##_cbc;
d03ab969 166
167/*----- That's all, folks -------------------------------------------------*/
168
169#ifdef __cplusplus
170 }
171#endif
172
173#endif