| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: rc4.h,v 1.2 1999/12/10 23:27:46 mdw Exp $ |
| 4 | * |
| 5 | * The alleged RC4 stream cipher |
| 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: rc4.h,v $ |
| 33 | * Revision 1.2 1999/12/10 23:27:46 mdw |
| 34 | * Generic cipher and RNG interfaces. |
| 35 | * |
| 36 | * Revision 1.1 1999/09/03 08:41:12 mdw |
| 37 | * Initial import. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | /*----- Notes on RC4 ------------------------------------------------------* |
| 42 | * |
| 43 | * RC4 is a stream cipher desgigned by Ron Rivest. For a while RC4 was a |
| 44 | * trade secret of RSA Data Security, Inc., but somehow source code for a |
| 45 | * cipher which interworks with RC4 was posted to the Cypherpunks mailing |
| 46 | * list. |
| 47 | */ |
| 48 | |
| 49 | #ifndef CATACOMB_RC4_H |
| 50 | #define CATACOMB_RC4_H |
| 51 | |
| 52 | #ifdef __cplusplus |
| 53 | extern "C" { |
| 54 | #endif |
| 55 | |
| 56 | /*----- Header files ------------------------------------------------------*/ |
| 57 | |
| 58 | #include <assert.h> |
| 59 | |
| 60 | #include <mLib/bits.h> |
| 61 | |
| 62 | #ifndef CATACOMB_GCIPHER_H |
| 63 | # include "gcipher.h" |
| 64 | #endif |
| 65 | |
| 66 | #ifndef CATACOMB_GRAND_H |
| 67 | # include "grand.h" |
| 68 | #endif |
| 69 | |
| 70 | /*----- Data structures ---------------------------------------------------*/ |
| 71 | |
| 72 | typedef struct rc4_ctx { |
| 73 | unsigned i, j; /* Indices into the @S@ table */ |
| 74 | unsigned f; /* Flags word */ |
| 75 | octet s[256]; /* The ever-changing @S@ table */ |
| 76 | } rc4_ctx; |
| 77 | |
| 78 | #define RC4F_OPEN 1u |
| 79 | |
| 80 | /*----- Macros ------------------------------------------------------------*/ |
| 81 | |
| 82 | /* --- @RC4_OPEN@ --- * |
| 83 | * |
| 84 | * Arguments: @ctx@ = pointer to an RC4 context |
| 85 | * @guts@ = code to perform within the RC4 context |
| 86 | * |
| 87 | * Use: Performs some code within an RC4 context. Some of the |
| 88 | * parameters are extracted from the context and held in local |
| 89 | * variables for speed. Multiple calls to @RC4_BYTE@ may be |
| 90 | * made within the open context. A context must only be |
| 91 | * opened once at a time. |
| 92 | */ |
| 93 | |
| 94 | #define RC4_OPEN(ctx, guts) do { \ |
| 95 | unsigned _rc4_i = (ctx)->i; \ |
| 96 | unsigned _rc4_j = (ctx)->j; \ |
| 97 | octet *_rc4_s = (ctx)->s; \ |
| 98 | \ |
| 99 | assert(((void)"RC4 context may only be opened once at a time", \ |
| 100 | ((ctx)->f & RC4F_OPEN) == 0)); \ |
| 101 | (ctx)->f |= RC4F_OPEN; \ |
| 102 | \ |
| 103 | guts \ |
| 104 | \ |
| 105 | (ctx)->f &= ~RC4F_OPEN; \ |
| 106 | (ctx)->i = _rc4_i; \ |
| 107 | (ctx)->j = _rc4_j; \ |
| 108 | } while (0) |
| 109 | |
| 110 | /* --- @RC4_BYTE@ --- * |
| 111 | * |
| 112 | * Arguments: @x@ = output variable to set |
| 113 | * |
| 114 | * Use: Extracts an octet from the lexically innermost open RC4 |
| 115 | * context and places it in the variable @x@. |
| 116 | */ |
| 117 | |
| 118 | #define RC4_BYTE(x) do { \ |
| 119 | unsigned _si, _sj; \ |
| 120 | _rc4_i = (_rc4_i + 1) & 0xff; \ |
| 121 | _si = _rc4_s[_rc4_i]; \ |
| 122 | _rc4_j = (_rc4_j + _si) & 0xff; \ |
| 123 | _sj = _rc4_s[_rc4_j]; \ |
| 124 | _rc4_s[_rc4_i] = _sj; \ |
| 125 | _rc4_s[_rc4_j] = _si; \ |
| 126 | (x) = _rc4_s[(_si + _sj) & 0xff]; \ |
| 127 | } while (0) |
| 128 | |
| 129 | /*----- Functions provided ------------------------------------------------*/ |
| 130 | |
| 131 | /* --- @rc4_init@ --- * |
| 132 | * |
| 133 | * Arguments: @rc4_ctx *ctx@ = pointer to context to initialize |
| 134 | * @const void *k@ = pointer to key data to use |
| 135 | * @size_t sz@ = size of the key data |
| 136 | * |
| 137 | * Returns: --- |
| 138 | * |
| 139 | * Use: Initializes an RC4 context ready for use. |
| 140 | */ |
| 141 | |
| 142 | extern void rc4_init(rc4_ctx */*ctx*/, const void */*k*/, size_t /*sz*/); |
| 143 | |
| 144 | /* --- @rc4_encrypt@ --- * |
| 145 | * |
| 146 | * Arguments: @rc4_ctx *ctx@ = pointer to context to use |
| 147 | * @const void *src@ = pointer to the source block |
| 148 | * @void *dest@ = pointer to the destination block |
| 149 | * @size_t sz@ = size of the block |
| 150 | * |
| 151 | * Returns: --- |
| 152 | * |
| 153 | * Use: Encrypts or decrypts a block of data. The destination may |
| 154 | * be null to just grind the generator around for a while. It's |
| 155 | * recommended that you say `@rc4_encrypt(&ctx, 0, 0, 1024)@' |
| 156 | * after initializing a new context, to prevent keystream |
| 157 | * guessing attacks. The source may be null to just extract a |
| 158 | * big lump of data from the generator. |
| 159 | */ |
| 160 | |
| 161 | extern void rc4_encrypt(rc4_ctx */*ctx*/, |
| 162 | const void */*src*/, void */*dest*/, |
| 163 | size_t /*sz*/); |
| 164 | |
| 165 | /*----- Generic cipher interface ------------------------------------------*/ |
| 166 | |
| 167 | extern const gccipher rc4; |
| 168 | |
| 169 | /*----- Generic random number generator interface -------------------------*/ |
| 170 | |
| 171 | /* --- @rc4_rand@ --- * |
| 172 | * |
| 173 | * Arguments: @const void *k@ = pointer to key material |
| 174 | * @size_t sz@ = size of key material |
| 175 | * |
| 176 | * Returns: Pointer to generic random number generator interface. |
| 177 | * |
| 178 | * Use: Creates a random number interface wrapper around an |
| 179 | * OFB-mode block cipher. |
| 180 | */ |
| 181 | |
| 182 | extern grand *rc4_rand(const void */*k*/, size_t /*sz*/); |
| 183 | |
| 184 | /*----- That's all, folks -------------------------------------------------*/ |
| 185 | |
| 186 | #ifdef __cplusplus |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | #endif |