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