| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: counter.h,v 1.4 2004/04/08 01:36:15 mdw Exp $ |
| 4 | * |
| 5 | * Block cipher counter mode (or long cycle mode) |
| 6 | * |
| 7 | * (c) 2000 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_COUNTER_H |
| 31 | #define CATACOMB_COUNTER_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 | #ifndef CATACOMB_GRAND_H |
| 48 | # include "grand.h" |
| 49 | #endif |
| 50 | |
| 51 | /*----- Macros ------------------------------------------------------------*/ |
| 52 | |
| 53 | /* --- @COUNTER_DECL@ --- * |
| 54 | * |
| 55 | * Arguments: @PRE@, @pre@ = prefixes for block cipher definitions |
| 56 | * |
| 57 | * Use: Makes declarations for counter mode. |
| 58 | */ |
| 59 | |
| 60 | #define COUNTER_DECL(PRE, pre) \ |
| 61 | \ |
| 62 | /* --- Counter mode context --- */ \ |
| 63 | \ |
| 64 | typedef struct pre##_counterctx { \ |
| 65 | pre##_ctx ctx; /* Underlying cipher context */ \ |
| 66 | unsigned off; /* Current offset in buffer */ \ |
| 67 | octet buf[PRE##_BLKSZ]; /* Output buffer */ \ |
| 68 | uint32 n[PRE##_BLKSZ / 4]; /* Counter */ \ |
| 69 | } pre##_counterctx; \ |
| 70 | \ |
| 71 | /* --- @pre_countergetiv@ --- * \ |
| 72 | * \ |
| 73 | * Arguments: @const pre_counterctx *ctx@ = pointer to counter \ |
| 74 | * context \ |
| 75 | * @void *iv@ = pointer to output data block \ |
| 76 | * \ |
| 77 | * Returns: --- \ |
| 78 | * \ |
| 79 | * Use: Reads the currently set IV. Reading and setting an IV \ |
| 80 | * is not transparent to the cipher. It will add a `step' \ |
| 81 | * which must be matched by a similar operation during \ |
| 82 | * decryption. \ |
| 83 | */ \ |
| 84 | \ |
| 85 | extern void pre##_countergetiv(const pre##_counterctx */*ctx*/, \ |
| 86 | void */*iv*/); \ |
| 87 | \ |
| 88 | /* --- @pre_countersetiv@ --- * \ |
| 89 | * \ |
| 90 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 91 | * @cnost void *iv@ = pointer to IV to set \ |
| 92 | * \ |
| 93 | * Returns: --- \ |
| 94 | * \ |
| 95 | * Use: Sets the IV to use for subsequent encryption. \ |
| 96 | */ \ |
| 97 | \ |
| 98 | extern void pre##_countersetiv(pre##_counterctx */*ctx*/, \ |
| 99 | const void */*iv*/); \ |
| 100 | \ |
| 101 | /* --- @pre_counterbdry@ --- * \ |
| 102 | * \ |
| 103 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 104 | * \ |
| 105 | * Returns: --- \ |
| 106 | * \ |
| 107 | * Use: Inserts a boundary during encryption. Successful \ |
| 108 | * decryption must place a similar boundary. \ |
| 109 | */ \ |
| 110 | \ |
| 111 | extern void pre##_counterbdry(pre##_counterctx */*ctx*/); \ |
| 112 | \ |
| 113 | /* --- @pre_countersetkey@ --- * \ |
| 114 | * \ |
| 115 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 116 | * @const pre_ctx *k@ = pointer to cipher context \ |
| 117 | * \ |
| 118 | * Returns: --- \ |
| 119 | * \ |
| 120 | * Use: Sets the counter context to use a different cipher key. \ |
| 121 | */ \ |
| 122 | \ |
| 123 | extern void pre##_countersetkey(pre##_counterctx */*ctx*/, \ |
| 124 | const pre##_ctx */*k*/); \ |
| 125 | \ |
| 126 | /* --- @pre_counterinit@ --- * \ |
| 127 | * \ |
| 128 | * Arguments: @pre_counterctx *ctx@ = pointer to cipher context \ |
| 129 | * @const void *key@ = pointer to the key buffer \ |
| 130 | * @size_t sz@ = size of the key \ |
| 131 | * @const void *iv@ = pointer to initialization vector \ |
| 132 | * \ |
| 133 | * Returns: --- \ |
| 134 | * \ |
| 135 | * Use: Initializes a counter context ready for use. You \ |
| 136 | * should ensure that the IV chosen is unique: reusing an \ |
| 137 | * IV will compromise the security of the entire \ |
| 138 | * plaintext. This is equivalent to calls to @pre_init@, \ |
| 139 | * @pre_countersetkey@ and @pre_countersetiv@. \ |
| 140 | */ \ |
| 141 | \ |
| 142 | extern void pre##_counterinit(pre##_counterctx */*ctx*/, \ |
| 143 | const void */*key*/, size_t /*sz*/, \ |
| 144 | const void */*iv*/); \ |
| 145 | \ |
| 146 | /* --- @pre_counterencrypt@ --- * \ |
| 147 | * \ |
| 148 | * Arguments: @pre_counterctx *ctx@ = pointer to counter context \ |
| 149 | * @const void *src@ = pointer to source data \ |
| 150 | * @void *dest@ = pointer to destination data \ |
| 151 | * @size_t sz@ = size of block to be encrypted \ |
| 152 | * \ |
| 153 | * Returns: --- \ |
| 154 | * \ |
| 155 | * Use: Encrypts or decrypts a block with a block cipher in \ |
| 156 | * counter mode: encryption and decryption are the same in \ |
| 157 | * counter. The destination may be null to just churn the \ |
| 158 | * feedback round for a bit. The source may be null to \ |
| 159 | * use the cipher as a random data generator. \ |
| 160 | */ \ |
| 161 | \ |
| 162 | extern void pre##_counterencrypt(pre##_counterctx */*ctx*/, \ |
| 163 | const void */*src*/, void */*dest*/, \ |
| 164 | size_t /*sz*/); \ |
| 165 | \ |
| 166 | /* --- @pre_counterrand@ --- * \ |
| 167 | * \ |
| 168 | * Arguments: @const void *k@ = pointer to key material \ |
| 169 | * @size_t sz@ = size of key material \ |
| 170 | * \ |
| 171 | * Returns: Pointer to generic random number generator interface. \ |
| 172 | * \ |
| 173 | * Use: Creates a random number interface wrapper around an \ |
| 174 | * counter-mode block cipher. \ |
| 175 | */ \ |
| 176 | \ |
| 177 | extern grand *pre##_counterrand(const void */*k*/, size_t /*sz*/); \ |
| 178 | \ |
| 179 | /* --- Generic cipher interface --- */ \ |
| 180 | \ |
| 181 | extern const gccipher pre##_counter; |
| 182 | |
| 183 | /*----- That's all, folks -------------------------------------------------*/ |
| 184 | |
| 185 | #ifdef __cplusplus |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | #endif |