| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Ciphertext block chaining for block ciphers |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 | |
| 28 | #ifndef CATACOMB_CBC_H |
| 29 | #define CATACOMB_CBC_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #include <stddef.h> |
| 38 | |
| 39 | #include <mLib/bits.h> |
| 40 | |
| 41 | #ifndef CATACOMB_GCIPHER_H |
| 42 | # include "gcipher.h" |
| 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 | \ |
| 56 | /* --- Cipher block chaining context --- */ \ |
| 57 | \ |
| 58 | typedef struct pre##_cbcctx { \ |
| 59 | pre##_ctx ctx; /* Underlying cipher context */ \ |
| 60 | uint32 iv[PRE##_BLKSZ / 4]; /* Previous ciphertext or IV */ \ |
| 61 | } pre##_cbcctx; \ |
| 62 | \ |
| 63 | /* --- @pre_cbcgetiv@ --- * \ |
| 64 | * \ |
| 65 | * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \ |
| 66 | * @void *iv@ = pointer to output data block \ |
| 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 | \ |
| 75 | extern void pre##_cbcgetiv(const pre##_cbcctx */*ctx*/, \ |
| 76 | void */*iv*/); \ |
| 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 | \ |
| 88 | extern void pre##_cbcsetiv(pre##_cbcctx */*ctx*/, \ |
| 89 | const void */*iv*/); \ |
| 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 | \ |
| 101 | extern void pre##_cbcsetkey(pre##_cbcctx */*ctx*/, \ |
| 102 | const pre##_ctx */*k*/); \ |
| 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 | \ |
| 119 | extern void pre##_cbcinit(pre##_cbcctx */*ctx*/, \ |
| 120 | const void */*key*/, size_t /*sz*/, \ |
| 121 | const void */*iv*/); \ |
| 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 | \ |
| 138 | extern void pre##_cbcencrypt(pre##_cbcctx */*ctx*/, \ |
| 139 | const void */*src*/, void */*dest*/, \ |
| 140 | size_t /*sz*/); \ |
| 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 | * \ |
| 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. \ |
| 155 | */ \ |
| 156 | \ |
| 157 | extern void pre##_cbcdecrypt(pre##_cbcctx */*ctx*/, \ |
| 158 | const void */*src*/, void */*dest*/, \ |
| 159 | size_t /*sz*/); \ |
| 160 | \ |
| 161 | /* --- Generic cipher interface --- */ \ |
| 162 | \ |
| 163 | extern const gccipher pre##_cbc; |
| 164 | |
| 165 | /*----- That's all, folks -------------------------------------------------*/ |
| 166 | |
| 167 | #ifdef __cplusplus |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | #endif |