| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: cbc.h,v 1.4 2004/04/08 01:36:15 mdw Exp $ |
| 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 | #ifndef CATACOMB_CBC_H |
| 31 | #define CATACOMB_CBC_H |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | /*----- Header files ------------------------------------------------------*/ |
| 38 | |
| 39 | #include <stddef.h> |
| 40 | |
| 41 | #include <mLib/bits.h> |
| 42 | |
| 43 | #ifndef CATACOMB_GCIPHER_H |
| 44 | # include "gcipher.h" |
| 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 | \ |
| 58 | /* --- Cipher block chaining context --- */ \ |
| 59 | \ |
| 60 | typedef struct pre##_cbcctx { \ |
| 61 | pre##_ctx ctx; /* Underlying cipher context */ \ |
| 62 | uint32 iv[PRE##_BLKSZ / 4]; /* Previous ciphertext or IV */ \ |
| 63 | } pre##_cbcctx; \ |
| 64 | \ |
| 65 | /* --- @pre_cbcgetiv@ --- * \ |
| 66 | * \ |
| 67 | * Arguments: @const pre_cbcctx *ctx@ = pointer to CBC context block \ |
| 68 | * @void *iv@ = pointer to output data block \ |
| 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 | \ |
| 77 | extern void pre##_cbcgetiv(const pre##_cbcctx */*ctx*/, \ |
| 78 | void */*iv*/); \ |
| 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 | \ |
| 90 | extern void pre##_cbcsetiv(pre##_cbcctx */*ctx*/, \ |
| 91 | const void */*iv*/); \ |
| 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 | \ |
| 103 | extern void pre##_cbcsetkey(pre##_cbcctx */*ctx*/, \ |
| 104 | const pre##_ctx */*k*/); \ |
| 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 | \ |
| 121 | extern void pre##_cbcinit(pre##_cbcctx */*ctx*/, \ |
| 122 | const void */*key*/, size_t /*sz*/, \ |
| 123 | const void */*iv*/); \ |
| 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 | \ |
| 140 | extern void pre##_cbcencrypt(pre##_cbcctx */*ctx*/, \ |
| 141 | const void */*src*/, void */*dest*/, \ |
| 142 | size_t /*sz*/); \ |
| 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 | * \ |
| 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. \ |
| 157 | */ \ |
| 158 | \ |
| 159 | extern void pre##_cbcdecrypt(pre##_cbcctx */*ctx*/, \ |
| 160 | const void */*src*/, void */*dest*/, \ |
| 161 | size_t /*sz*/); \ |
| 162 | \ |
| 163 | /* --- Generic cipher interface --- */ \ |
| 164 | \ |
| 165 | extern const gccipher pre##_cbc; |
| 166 | |
| 167 | /*----- That's all, folks -------------------------------------------------*/ |
| 168 | |
| 169 | #ifdef __cplusplus |
| 170 | } |
| 171 | #endif |
| 172 | |
| 173 | #endif |