1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/* rijndael - An implementation of the Rijndael cipher.
* Copyright (C) 2000 Rafael R. Sevilla <sevillar@team.ph.inter.net>
*
* Currently maintained by brian d foy, <bdfoy@cpan.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Rijndael is a 128/192/256-bit block cipher that accepts key sizes of
* 128, 192, or 256 bits, designed by Joan Daemen and Vincent Rijmen. See
* http://www.esat.kuleuven.ac.be/~rijmen/rijndael/ for details.
*/
#if !defined(RIJNDAEL_H)
#define RIJNDAEL_H
#include <stdlib.h>
#include <sys/types.h>
#ifdef _CRYPT_RIJNDAEL_H_TYPES
#undef _CRYPT_RIJNDAEL_H_TYPES
#endif
/* Irix. We could include stdint.h and use uint8_t but that also
* requires that we specifically drive the compiler in C99 mode.
* Defining UINT8 as unsigned char is, ultimately, what stdint.h
* would do anyway.
*/
#if defined(_SGIAPI) || defined( __sgi )
#define _CRYPT_RIJNDAEL_H_TYPES
typedef __uint32_t UINT32;
typedef unsigned char UINT8;
#endif
/* Solaris has sys/types.h, but doesn't act like everyone else
* GCC defines __sun__ and __sun (report from Todd Ross)
* Solaris cc defines __sun
* MirBSD defines the same types as Solaris
*/
#if defined( __sun__ ) || defined( __sun ) || defined( __MirBSD__ )
#define _CRYPT_RIJNDAEL_H_TYPES
typedef uint32_t UINT32;
typedef uint8_t UINT8;
#endif
/* Mac OS X 10.3 defines things differently than most other
systems */
#if defined( __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ ) && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__-0 < 1140
#define _CRYPT_RIJNDAEL_H_TYPES
typedef u_int32_t UINT32;
typedef u_char UINT8;
#endif
/* Mac OS X 10.3 defines things differently than most other
systems */
#if defined(__APPLE__) && ! defined(__DARWIN_UNIX03)
#define _CRYPT_RIJNDAEL_H_TYPES
typedef u_int32_t UINT32;
typedef u_char UINT8;
#endif
/* I expect this to be the usual case */
#if ! defined(_CRYPT_RIJNDAEL_H_TYPES) && ( defined(_SYS_TYPES_H) || defined(_SYS_TYPES_H_) )
#define _CRYPT_RIJNDAEL_H_TYPES
typedef __uint32_t UINT32;
typedef __uint8_t UINT8;
#endif
#if defined(__CYGWIN__) && ! defined(_CRYPT_RIJNDAEL_H_TYPES)
#define _CRYPT_RIJNDAEL_H_TYPES
typedef unsigned int UINT32;
typedef unsigned char UINT8;
#endif
#if defined(__MINGW32__) && ! defined(_CRYPT_RIJNDAEL_H_TYPES)
#define _CRYPT_RIJNDAEL_H_TYPES
typedef unsigned int UINT32;
typedef unsigned char UINT8;
#endif
#if defined(WIN32) && ! defined(_CRYPT_RIJNDAEL_H_TYPES)
#define _CRYPT_RIJNDAEL_H_TYPES
typedef unsigned int UINT32;
typedef unsigned char UINT8;
#endif
#if ! defined(_CRYPT_RIJNDAEL_H_TYPES)
#define _CRYPT_RIJNDAEL_H_TYPES
typedef unsigned int UINT32;
typedef unsigned char UINT8;
#endif
/* Other block sizes and key lengths are possible, but in the context of
* the ssh protocols, 256 bits is the default.
*/
#define RIJNDAEL_BLOCKSIZE 16
#define RIJNDAEL_KEYSIZE 32
#define MODE_ECB 1 /* Are we ciphering in ECB mode? */
#define MODE_CBC 2 /* Are we ciphering in CBC mode? */
#define MODE_CFB 3 /* Are we ciphering in 128-bit CFB mode? */
#define MODE_PCBC 4 /* Are we ciphering in PCBC mode? */
#define MODE_OFB 5 /* Are we ciphering in 128-bit OFB mode? */
#define MODE_CTR 6 /* Are we ciphering in counter mode? */
/* Allow keys of size 128 <= bits <= 256 */
#define RIJNDAEL_MIN_KEYSIZE 16
#define RIJNDAEL_MAX_KEYSIZE 32
typedef struct {
UINT32 keys[60]; /* maximum size of key schedule */
UINT32 ikeys[60]; /* inverse key schedule */
int nrounds; /* number of rounds to use for our key size */
int mode; /* encryption mode */
} RIJNDAEL_context;
/* This basically performs Rijndael's key scheduling algorithm, as it's the
* only initialization required anyhow. The key size is specified in bytes,
* but the only valid values are 16 (128 bits), 24 (192 bits), and 32 (256
* bits). If a value other than these three is specified, the key will be
* truncated to the closest value less than the key size specified, e.g.
* specifying 7 will use only the first 6 bytes of the key given. DO NOT
* PASS A VALUE LESS THAN 16 TO KEYSIZE!
*/
void
rijndael_setup(RIJNDAEL_context *ctx, size_t keysize, const UINT8 *key);
/*
* rijndael_encrypt()
*
* Encrypt 16 bytes of data with the Rijndael algorithm. Before this
* function can be used, rijndael_setup must be used in order to initialize
* Rijndael's key schedule.
*
* This function always encrypts 16 bytes of plaintext to 16 bytes of
* ciphertext. The memory areas of the plaintext and the ciphertext can
* overlap.
*/
void
rijndael_encrypt(RIJNDAEL_context *context,
const UINT8 *plaintext,
UINT8 *ciphertext);
/*
* rijndael_decrypt()
*
* Decrypt 16 bytes of data with the Rijndael algorithm.
*
* Before this function can be used, rijndael_setup() must be used in order
* to set up the key schedule required for the decryption algorithm.
*
* This function always decrypts 16 bytes of ciphertext to 16 bytes of
* plaintext. The memory areas of the plaintext and the ciphertext can
* overlap.
*/
void
rijndael_decrypt(RIJNDAEL_context *context,
const UINT8 *ciphertext,
UINT8 *plaintext);
/* Encrypt a block of plaintext in a mode specified in the context */
void
block_encrypt(RIJNDAEL_context *ctx, UINT8 *input, int inputlen,
UINT8 *output, UINT8 *iv);
/* Decrypt a block of plaintext in a mode specified in the context */
void
block_decrypt(RIJNDAEL_context *ctx, UINT8 *input, int inputlen,
UINT8 *output, UINT8 *iv);
#endif /* RIJNDAEL_H */
|