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 | |
| 47 | /*! \struct rc4_t |
| 48 | \brief RC4 stream cipher state object |
| 49 | \var s State array |
| 50 | \var i Monotonic index |
| 51 | \var j Randomised index |
| 52 | |
| 53 | \warning This structure should not be accessed directly. To |
| 54 | \warning initialise a rc4_t object, you should use the rc4_key() |
| 55 | \warning function |
| 56 | |
| 57 | This structure holds the current state of the RC4 algorithm. |
| 58 | */ |
| 59 | typedef struct |
| 60 | { |
| 61 | unsigned int s[256]; |
| 62 | int i; |
| 63 | int j; |
| 64 | } rc4_t; |
| 65 | |
| 66 | /*! \fn void rc4_key(rc4_t *r, unsigned char *key, int len); |
| 67 | \brief Set up key structure of RC4 stream cipher |
| 68 | \param r pointer to RC4 structure to be seeded |
| 69 | \param key pointer to buffer containing raw key |
| 70 | \param len length of key |
| 71 | |
| 72 | This function set the internal state of the RC4 data structure |
| 73 | pointed to by \a r using the specified \a key of length \a len. |
| 74 | |
| 75 | This function can use up to 256 bytes of key, any more are ignored. |
| 76 | |
| 77 | \warning Stream ciphers (such as RC4) can be insecure if the same |
| 78 | \warning key is used repeatedly. Ensure that any key specified has |
| 79 | \warning an reasonably sized Initialisation Vector component. |
| 80 | */ |
| 81 | void rc4_key(rc4_t *r, unsigned char *key, int len); |
| 82 | |
| 83 | /*! \fn rc4_crypt(rc4_t *r, unsigned char *plaintext, int len); |
| 84 | \brief Crypt bytes using RC4 algorithm |
| 85 | \param r pointer to RC4 structure to be used |
| 86 | \param plaintext Pointer to bytes to encrypt |
| 87 | \param len number of bytes to crypt |
| 88 | |
| 89 | This function encrypts one or more bytes (pointed to by \a plaintext) |
| 90 | using the RC4 algorithm. \a r is a state structure that must be |
| 91 | initialiased using the rc4_key() function prior to use. |
| 92 | |
| 93 | Since RC4 XORs each byte of plaintext with a byte of keystream, |
| 94 | this function can be used for both encryption and decryption. |
| 95 | */ |
| 96 | void rc4_crypt(rc4_t *r, unsigned char *plaintext, int len); |
| 97 | |
| 98 | /*! \fn rc4_getbytes(rc4_t *r, unsigned char *buffer, int len); |
| 99 | \brief Generate key stream using the RC4 stream cipher |
| 100 | \param r pointer to RC4 structure to be used |
| 101 | \param buffer pointer to buffer in which to deposit keystream |
| 102 | \param len number of bytes to deposit |
| 103 | |
| 104 | This function gives access to the raw RC4 key stream. In this |
| 105 | consiguration RC4 can be used as a fast, strong pseudo-random |
| 106 | number generator with a very long period. |
| 107 | */ |
| 108 | void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len); |
| 109 | |
| 110 | #endif /* _RC4_H */ |