Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /*! \file rc4.h |
| 2 | \brief Header file for RC4 stream cipher routines |
| 3 | \author Damien Miller <djm@mindrot.org> |
| 4 | \version 0.0.0 |
| 5 | \date 1999 |
| 6 | |
| 7 | A simple implementation of the RC4 stream cipher, based on the |
| 8 | description given in _Bruce Schneier's_ "Applied Cryptography" |
| 9 | 2nd edition. |
| 10 | |
| 11 | Copyright 1999 Damien Miller |
| 12 | |
| 13 | Permission is hereby granted, free of charge, to any person |
| 14 | obtaining a copy of this software and associated documentation |
| 15 | files (the "Software"), to deal in the Software without |
| 16 | restriction, including without limitation the rights to use, copy, |
| 17 | modify, merge, publish, distribute, sublicense, and/or sell copies |
| 18 | of the Software, and to permit persons to whom the Software is |
| 19 | furnished to do so, subject to the following conditions: |
| 20 | |
| 21 | The above copyright notice and this permission notice shall be |
| 22 | included in all copies or substantial portions of the Software. |
| 23 | |
| 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 25 | KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 26 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
| 27 | AND NONINFRINGEMENT. IN NO EVENT SHALL DAMIEN MILLER BE LIABLE |
| 28 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 29 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 30 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 31 | |
| 32 | \warning None of these functions clears its memory after use. It |
| 33 | \warning is the responsability of the calling routines to ensure |
| 34 | \warning that any sensitive data (keystream, key or plaintext) is |
| 35 | \warning properly erased after use. |
| 36 | |
| 37 | \warning The name "RC4" is trademarked in the United States, |
| 38 | \warning you may need to use "RC4 compatible" or "ARC4" |
| 39 | \warning (Alleged RC4). |
| 40 | */ |
| 41 | |
| 42 | /* $Id: rc4.h,v 1.1.1.1 1999/10/26 05:48:13 damien Exp $ */ |
| 43 | |
| 44 | #ifndef _RC4_H |
| 45 | #define _RC4_H |
| 46 | |
Damien Miller | e413cba | 1999-10-28 14:12:54 +1000 | [diff] [blame] | 47 | #include "config.h" |
| 48 | #ifndef HAVE_ARC4RANDOM |
| 49 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | /*! \struct rc4_t |
| 51 | \brief RC4 stream cipher state object |
| 52 | \var s State array |
| 53 | \var i Monotonic index |
| 54 | \var j Randomised index |
| 55 | |
| 56 | \warning This structure should not be accessed directly. To |
| 57 | \warning initialise a rc4_t object, you should use the rc4_key() |
| 58 | \warning function |
| 59 | |
| 60 | This structure holds the current state of the RC4 algorithm. |
| 61 | */ |
| 62 | typedef struct |
| 63 | { |
| 64 | unsigned int s[256]; |
| 65 | int i; |
| 66 | int j; |
| 67 | } rc4_t; |
| 68 | |
| 69 | /*! \fn void rc4_key(rc4_t *r, unsigned char *key, int len); |
| 70 | \brief Set up key structure of RC4 stream cipher |
| 71 | \param r pointer to RC4 structure to be seeded |
| 72 | \param key pointer to buffer containing raw key |
| 73 | \param len length of key |
| 74 | |
| 75 | This function set the internal state of the RC4 data structure |
| 76 | pointed to by \a r using the specified \a key of length \a len. |
| 77 | |
| 78 | This function can use up to 256 bytes of key, any more are ignored. |
| 79 | |
| 80 | \warning Stream ciphers (such as RC4) can be insecure if the same |
| 81 | \warning key is used repeatedly. Ensure that any key specified has |
| 82 | \warning an reasonably sized Initialisation Vector component. |
| 83 | */ |
| 84 | void rc4_key(rc4_t *r, unsigned char *key, int len); |
| 85 | |
| 86 | /*! \fn rc4_crypt(rc4_t *r, unsigned char *plaintext, int len); |
| 87 | \brief Crypt bytes using RC4 algorithm |
| 88 | \param r pointer to RC4 structure to be used |
| 89 | \param plaintext Pointer to bytes to encrypt |
| 90 | \param len number of bytes to crypt |
| 91 | |
| 92 | This function encrypts one or more bytes (pointed to by \a plaintext) |
| 93 | using the RC4 algorithm. \a r is a state structure that must be |
| 94 | initialiased using the rc4_key() function prior to use. |
| 95 | |
| 96 | Since RC4 XORs each byte of plaintext with a byte of keystream, |
| 97 | this function can be used for both encryption and decryption. |
| 98 | */ |
| 99 | void rc4_crypt(rc4_t *r, unsigned char *plaintext, int len); |
| 100 | |
| 101 | /*! \fn rc4_getbytes(rc4_t *r, unsigned char *buffer, int len); |
| 102 | \brief Generate key stream using the RC4 stream cipher |
| 103 | \param r pointer to RC4 structure to be used |
| 104 | \param buffer pointer to buffer in which to deposit keystream |
| 105 | \param len number of bytes to deposit |
| 106 | |
| 107 | This function gives access to the raw RC4 key stream. In this |
| 108 | consiguration RC4 can be used as a fast, strong pseudo-random |
| 109 | number generator with a very long period. |
| 110 | */ |
| 111 | void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len); |
| 112 | |
Damien Miller | e413cba | 1999-10-28 14:12:54 +1000 | [diff] [blame] | 113 | #endif /* !HAVE_ARC4RANDOM */ |
| 114 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | #endif /* _RC4_H */ |