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