| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: cfb.h,v 1.5 2004/04/08 01:36:15 mdw Exp $ |
| 4 | * |
| 5 | * Ciphertext feedback 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_CFB_H |
| 31 | #define CATACOMB_CFB_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 | /*----- Data structures ---------------------------------------------------*/ |
| 48 | |
| 49 | /* --- @CFB_DECL@ --- * |
| 50 | * |
| 51 | * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher |
| 52 | * |
| 53 | * Use: Creates declarations for CFB mode. |
| 54 | */ |
| 55 | |
| 56 | #define CFB_DECL(PRE, pre) \ |
| 57 | \ |
| 58 | /* --- Ciphertext feedback context --- */ \ |
| 59 | \ |
| 60 | typedef struct pre##_cfbctx { \ |
| 61 | pre##_ctx ctx; /* Underlying cipher context */ \ |
| 62 | unsigned off; /* Offset into @iv@ buffer */ \ |
| 63 | octet iv[PRE##_BLKSZ]; /* Previous ciphertext or IV */ \ |
| 64 | } pre##_cfbctx; \ |
| 65 | \ |
| 66 | /* --- @pre_cfbgetiv@ --- * \ |
| 67 | * \ |
| 68 | * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 69 | * @void *iv@ = pointer to output data block \ |
| 70 | * \ |
| 71 | * Returns: --- \ |
| 72 | * \ |
| 73 | * Use: Reads the currently set IV. Reading and setting an IV \ |
| 74 | * is not transparent to the cipher. It will add a `step' \ |
| 75 | * which must be matched by a similar operation during \ |
| 76 | * decryption. \ |
| 77 | */ \ |
| 78 | \ |
| 79 | extern void pre##_cfbgetiv(const pre##_cfbctx */*ctx*/, \ |
| 80 | void */*iv*/); \ |
| 81 | \ |
| 82 | /* --- @pre_cfbsetiv@ --- * \ |
| 83 | * \ |
| 84 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 85 | * @cnost void *iv@ = pointer to IV to set \ |
| 86 | * \ |
| 87 | * Returns: --- \ |
| 88 | * \ |
| 89 | * Use: Sets the IV to use for subsequent encryption. \ |
| 90 | */ \ |
| 91 | \ |
| 92 | extern void pre##_cfbsetiv(pre##_cfbctx */*ctx*/, \ |
| 93 | const void */*iv*/); \ |
| 94 | \ |
| 95 | /* --- @pre_cfbbdry@ --- * \ |
| 96 | * \ |
| 97 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 98 | * \ |
| 99 | * Returns: --- \ |
| 100 | * \ |
| 101 | * Use: Inserts a boundary during encryption. Successful \ |
| 102 | * decryption must place a similar boundary. \ |
| 103 | */ \ |
| 104 | \ |
| 105 | extern void pre##_cfbbdry(pre##_cfbctx */*ctx*/); \ |
| 106 | \ |
| 107 | /* --- @pre_cfbsetkey@ --- * \ |
| 108 | * \ |
| 109 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 110 | * @const pre_ctx *k@ = pointer to cipher context \ |
| 111 | * \ |
| 112 | * Returns: --- \ |
| 113 | * \ |
| 114 | * Use: Sets the CFB context to use a different cipher key. \ |
| 115 | */ \ |
| 116 | \ |
| 117 | extern void pre##_cfbsetkey(pre##_cfbctx */*ctx*/, \ |
| 118 | const pre##_ctx */*k*/); \ |
| 119 | \ |
| 120 | /* --- @pre_cfbinit@ --- * \ |
| 121 | * \ |
| 122 | * Arguments: @pre_cfbctx *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 CFB context ready for use. You should \ |
| 130 | * ensure that the IV chosen is unique: reusing an IV will \ |
| 131 | * compromise the security of at least the first block \ |
| 132 | * encrypted. This is equivalent to calls to @pre_init@, \ |
| 133 | * @pre_cfbsetkey@ and @pre_cfbsetiv@. \ |
| 134 | */ \ |
| 135 | \ |
| 136 | extern void pre##_cfbinit(pre##_cfbctx */*ctx*/, \ |
| 137 | const void */*key*/, size_t /*sz*/, \ |
| 138 | const void */*iv*/); \ |
| 139 | \ |
| 140 | /* --- @pre_cfbencrypt@ --- * \ |
| 141 | * \ |
| 142 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 143 | * @const void *src@ = pointer to source data \ |
| 144 | * @void *dest@ = pointer to destination data \ |
| 145 | * @size_t sz@ = size of block to be encrypted \ |
| 146 | * \ |
| 147 | * Returns: --- \ |
| 148 | * \ |
| 149 | * Use: Encrypts a block with a block cipher in CFB mode. The \ |
| 150 | * input block may be arbitrary in size. CFB mode is not \ |
| 151 | * sensitive to block boundaries. \ |
| 152 | */ \ |
| 153 | \ |
| 154 | extern void pre##_cfbencrypt(pre##_cfbctx */*ctx*/, \ |
| 155 | const void */*src*/, void */*dest*/, \ |
| 156 | size_t /*sz*/); \ |
| 157 | \ |
| 158 | /* --- @pre_cfbencrypt@ --- * \ |
| 159 | * \ |
| 160 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB 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 | * \ |
| 167 | * Use: Decrypts a block with a block cipher in CFB mode. The \ |
| 168 | * input block may be arbitrary in size. CFB mode is not \ |
| 169 | * sensitive to block boundaries. \ |
| 170 | */ \ |
| 171 | \ |
| 172 | extern void pre##_cfbdecrypt(pre##_cfbctx */*ctx*/, \ |
| 173 | const void */*src*/, void */*dest*/, \ |
| 174 | size_t /*sz*/); \ |
| 175 | \ |
| 176 | /* --- Generic cipher interface --- */ \ |
| 177 | \ |
| 178 | extern const gccipher pre##_cfb; |
| 179 | |
| 180 | /*----- That's all, folks -------------------------------------------------*/ |
| 181 | |
| 182 | #ifdef __cplusplus |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | #endif |