| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: cfb.h,v 1.3 2000/06/17 10:50:55 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 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: cfb.h,v $ |
| 33 | * Revision 1.3 2000/06/17 10:50:55 mdw |
| 34 | * Change buffer offset to be unsigned. |
| 35 | * |
| 36 | * Revision 1.2 1999/12/10 23:16:39 mdw |
| 37 | * Split mode macros into interface and implementation. |
| 38 | * |
| 39 | * Revision 1.1 1999/09/03 08:41:11 mdw |
| 40 | * Initial import. |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | #ifndef CATACOMB_CFB_H |
| 45 | #define CATACOMB_CFB_H |
| 46 | |
| 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
| 51 | /*----- Header files ------------------------------------------------------*/ |
| 52 | |
| 53 | #include <stddef.h> |
| 54 | |
| 55 | #include <mLib/bits.h> |
| 56 | |
| 57 | #ifndef CATACOMB_GCIPHER_H |
| 58 | # include "gcipher.h" |
| 59 | #endif |
| 60 | |
| 61 | /*----- Data structures ---------------------------------------------------*/ |
| 62 | |
| 63 | /* --- @CFB_DECL@ --- * |
| 64 | * |
| 65 | * Arguments: @PRE@, @pre@ = prefixes for the underlying block cipher |
| 66 | * |
| 67 | * Use: Creates declarations for CFB mode. |
| 68 | */ |
| 69 | |
| 70 | #define CFB_DECL(PRE, pre) \ |
| 71 | \ |
| 72 | /* --- Ciphertext feedback context --- */ \ |
| 73 | \ |
| 74 | typedef struct pre##_cfbctx { \ |
| 75 | pre##_ctx ctx; /* Underlying cipher context */ \ |
| 76 | unsigned off; /* Offset into @iv@ buffer */ \ |
| 77 | octet iv[PRE##_BLKSZ]; /* Previous ciphertext or IV */ \ |
| 78 | } pre##_cfbctx; \ |
| 79 | \ |
| 80 | /* --- @pre_cfbgetiv@ --- * \ |
| 81 | * \ |
| 82 | * Arguments: @const pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 83 | * @void *iv#@ = pointer to output data block \ |
| 84 | * \ |
| 85 | * Returns: --- \ |
| 86 | * \ |
| 87 | * Use: Reads the currently set IV. Reading and setting an IV \ |
| 88 | * is not transparent to the cipher. It will add a `step' \ |
| 89 | * which must be matched by a similar operation during \ |
| 90 | * decryption. \ |
| 91 | */ \ |
| 92 | \ |
| 93 | extern void pre##_cfbgetiv(const pre##_cfbctx */*ctx*/, \ |
| 94 | void */*iv*/); \ |
| 95 | \ |
| 96 | /* --- @pre_cfbsetiv@ --- * \ |
| 97 | * \ |
| 98 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 99 | * @cnost void *iv@ = pointer to IV to set \ |
| 100 | * \ |
| 101 | * Returns: --- \ |
| 102 | * \ |
| 103 | * Use: Sets the IV to use for subsequent encryption. \ |
| 104 | */ \ |
| 105 | \ |
| 106 | extern void pre##_cfbsetiv(pre##_cfbctx */*ctx*/, \ |
| 107 | const void */*iv*/); \ |
| 108 | \ |
| 109 | /* --- @pre_cfbbdry@ --- * \ |
| 110 | * \ |
| 111 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 112 | * \ |
| 113 | * Returns: --- \ |
| 114 | * \ |
| 115 | * Use: Inserts a boundary during encryption. Successful \ |
| 116 | * decryption must place a similar boundary. \ |
| 117 | */ \ |
| 118 | \ |
| 119 | extern void pre##_cfbbdry(pre##_cfbctx */*ctx*/); \ |
| 120 | \ |
| 121 | /* --- @pre_cfbsetkey@ --- * \ |
| 122 | * \ |
| 123 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 124 | * @const pre_ctx *k@ = pointer to cipher context \ |
| 125 | * \ |
| 126 | * Returns: --- \ |
| 127 | * \ |
| 128 | * Use: Sets the CFB context to use a different cipher key. \ |
| 129 | */ \ |
| 130 | \ |
| 131 | extern void pre##_cfbsetkey(pre##_cfbctx */*ctx*/, \ |
| 132 | const pre##_ctx */*k*/); \ |
| 133 | \ |
| 134 | /* --- @pre_cfbinit@ --- * \ |
| 135 | * \ |
| 136 | * Arguments: @pre_cfbctx *ctx@ = pointer to cipher context \ |
| 137 | * @const void *key@ = pointer to the key buffer \ |
| 138 | * @size_t sz@ = size of the key \ |
| 139 | * @const void *iv@ = pointer to initialization vector \ |
| 140 | * \ |
| 141 | * Returns: --- \ |
| 142 | * \ |
| 143 | * Use: Initializes a CFB context ready for use. You should \ |
| 144 | * ensure that the IV chosen is unique: reusing an IV will \ |
| 145 | * compromise the security of at least the first block \ |
| 146 | * encrypted. This is equivalent to calls to @pre_init@, \ |
| 147 | * @pre_cfbsetkey@ and @pre_cfbsetiv@. \ |
| 148 | */ \ |
| 149 | \ |
| 150 | extern void pre##_cfbinit(pre##_cfbctx */*ctx*/, \ |
| 151 | const void */*key*/, size_t /*sz*/, \ |
| 152 | const void */*iv*/); \ |
| 153 | \ |
| 154 | /* --- @pre_cfbencrypt@ --- * \ |
| 155 | * \ |
| 156 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 157 | * @const void *src@ = pointer to source data \ |
| 158 | * @void *dest@ = pointer to destination data \ |
| 159 | * @size_t sz@ = size of block to be encrypted \ |
| 160 | * \ |
| 161 | * Returns: --- \ |
| 162 | * \ |
| 163 | * Use: Encrypts a block with a block cipher in CFB mode. The \ |
| 164 | * input block may be arbitrary in size. CFB mode is not \ |
| 165 | * sensitive to block boundaries. \ |
| 166 | */ \ |
| 167 | \ |
| 168 | extern void pre##_cfbencrypt(pre##_cfbctx */*ctx*/, \ |
| 169 | const void */*src*/, void */*dest*/, \ |
| 170 | size_t /*sz*/); \ |
| 171 | \ |
| 172 | /* --- @pre_cfbencrypt@ --- * \ |
| 173 | * \ |
| 174 | * Arguments: @pre_cfbctx *ctx@ = pointer to CFB context block \ |
| 175 | * @const void *src@ = pointer to source data \ |
| 176 | * @void *dest@ = pointer to destination data \ |
| 177 | * @size_t sz@ = size of block to be encrypted \ |
| 178 | * \ |
| 179 | * Returns: --- \ |
| 180 | * \ |
| 181 | * Use: Decrypts a block with a block cipher in CFB mode. The \ |
| 182 | * input block may be arbitrary in size. CFB mode is not \ |
| 183 | * sensitive to block boundaries. \ |
| 184 | */ \ |
| 185 | \ |
| 186 | extern void pre##_cfbdecrypt(pre##_cfbctx */*ctx*/, \ |
| 187 | const void */*src*/, void */*dest*/, \ |
| 188 | size_t /*sz*/); \ |
| 189 | \ |
| 190 | /* --- Generic cipher interface --- */ \ |
| 191 | \ |
| 192 | extern const gccipher pre##_cfb; |
| 193 | |
| 194 | /*----- That's all, folks -------------------------------------------------*/ |
| 195 | |
| 196 | #ifdef __cplusplus |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | #endif |