Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

rijndael.h

Go to the documentation of this file.
00001 #ifndef _RIJNDAEL_H_ 00002 #define _RIJNDAEL_H_ 00003 00004 // 00005 // File : rijndael.h 00006 // Creation date : Sun Nov 5 2000 03:21:05 CEST 00007 // Author : Szymon Stefanek (stefanek@tin.it) 00008 // 00009 // Another implementation of the Rijndael cipher. 00010 // This is intended to be an easily usable library file. 00011 // This code is public domain. 00012 // Based on the Vincent Rijmen and K.U.Leuven implementation 2.4. 00013 // 00014 00015 // 00016 // Original Copyright notice: 00017 // 00018 // rijndael-alg-fst.c v2.4 April '2000 00019 // rijndael-alg-fst.h 00020 // rijndael-api-fst.c 00021 // rijndael-api-fst.h 00022 // 00023 // Optimised ANSI C code 00024 // 00025 // authors: v1.0: Antoon Bosselaers 00026 // v2.0: Vincent Rijmen, K.U.Leuven 00027 // v2.3: Paulo Barreto 00028 // v2.4: Vincent Rijmen, K.U.Leuven 00029 // 00030 // This code is placed in the public domain. 00031 // 00032 00033 // 00034 // This implementation works on 128 , 192 , 256 bit keys 00035 // and on 128 bit blocks 00036 // 00037 00038 // 00039 // Example of usage: 00040 // 00041 // // Input data 00042 // unsigned char key[32]; // The key 00043 // initializeYour256BitKey(); // Obviously initialized with sth 00044 // const unsigned char * plainText = getYourPlainText(); // Your plain text 00045 // int plainTextLen = strlen(plainText); // Plain text length 00046 // 00047 // // Encrypting 00048 // Rijndael rin; 00049 // unsigned char output[plainTextLen + 16]; 00050 // 00051 // rin.init(Rijndael::CBC,Rijndael::Encrypt,key,Rijndael::Key32Bytes); 00052 // // It is a good idea to check the error code 00053 // int len = rin.padEncrypt(plainText,len,output); 00054 // if(len >= 0)useYourEncryptedText(); 00055 // else encryptError(len); 00056 // 00057 // // Decrypting: we can reuse the same object 00058 // unsigned char output2[len]; 00059 // rin.init(Rijndael::CBC,Rijndael::Decrypt,key,Rijndael::Key32Bytes)); 00060 // len = rin.padDecrypt(output,len,output2); 00061 // if(len >= 0)useYourDecryptedText(); 00062 // else decryptError(len); 00063 // 00064 00065 #define _MAX_KEY_COLUMNS (256/32) 00066 #define _MAX_ROUNDS 14 00067 #define MAX_IV_SIZE 16 00068 00069 // We assume that unsigned int is 32 bits long.... 00070 typedef unsigned char UINT8; 00071 typedef unsigned int UINT32; 00072 typedef unsigned short UINT16; 00073 00074 // Error codes 00075 #define RIJNDAEL_SUCCESS 0 00076 #define RIJNDAEL_UNSUPPORTED_MODE -1 00077 #define RIJNDAEL_UNSUPPORTED_DIRECTION -2 00078 #define RIJNDAEL_UNSUPPORTED_KEY_LENGTH -3 00079 #define RIJNDAEL_BAD_KEY -4 00080 #define RIJNDAEL_NOT_INITIALIZED -5 00081 #define RIJNDAEL_BAD_DIRECTION -6 00082 #define RIJNDAEL_CORRUPTED_DATA -7 00083 00084 class Rijndael 00085 { 00086 public: 00087 enum Direction { Encrypt , Decrypt }; 00088 enum Mode { ECB , CBC , CFB1 }; 00089 enum KeyLength { Key16Bytes , Key24Bytes , Key32Bytes }; 00090 // 00091 // Creates a Rijndael cipher object 00092 // You have to call init() before you can encrypt or decrypt stuff 00093 // 00094 Rijndael(); 00095 ~Rijndael(); 00096 protected: 00097 // Internal stuff 00098 enum State { Valid , Invalid }; 00099 00100 State m_state; 00101 Mode m_mode; 00102 Direction m_direction; 00103 UINT8 m_initVector[MAX_IV_SIZE]; 00104 UINT32 m_uRounds; 00105 UINT8 m_expandedKey[_MAX_ROUNDS+1][4][4]; 00106 public: 00108 // API 00110 00111 // init(): Initializes the crypt session 00112 // Returns RIJNDAEL_SUCCESS or an error code 00113 // mode : Rijndael::ECB, Rijndael::CBC or Rijndael::CFB1 00114 // You have to use the same mode for encrypting and decrypting 00115 // dir : Rijndael::Encrypt or Rijndael::Decrypt 00116 // A cipher instance works only in one direction 00117 // (Well , it could be easily modified to work in both 00118 // directions with a single init() call, but it looks 00119 // useless to me...anyway , it is a matter of generating 00120 // two expanded keys) 00121 // key : array of unsigned octets , it can be 16 , 24 or 32 bytes long 00122 // this CAN be binary data (it is not expected to be null terminated) 00123 // keyLen : Rijndael::Key16Bytes , Rijndael::Key24Bytes or Rijndael::Key32Bytes 00124 // initVector: initialization vector, you will usually use 0 here 00125 int init(Mode mode,Direction dir,const UINT8 *key,KeyLength keyLen,UINT8 * initVector = 0); 00126 // Encrypts the input array (can be binary data) 00127 // The input array length must be a multiple of 16 bytes, the remaining part 00128 // is DISCARDED. 00129 // so it actually encrypts inputLen / 128 blocks of input and puts it in outBuffer 00130 // Input len is in BITS! 00131 // outBuffer must be at least inputLen / 8 bytes long. 00132 // Returns the encrypted buffer length in BITS or an error code < 0 in case of error 00133 int blockEncrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer); 00134 // Encrypts the input array (can be binary data) 00135 // The input array can be any length , it is automatically padded on a 16 byte boundary. 00136 // Input len is in BYTES! 00137 // outBuffer must be at least (inputLen + 16) bytes long 00138 // Returns the encrypted buffer length in BYTES or an error code < 0 in case of error 00139 int padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer); 00140 // Decrypts the input vector 00141 // Input len is in BITS! 00142 // outBuffer must be at least inputLen / 8 bytes long 00143 // Returns the decrypted buffer length in BITS and an error code < 0 in case of error 00144 int blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer); 00145 // Decrypts the input vector 00146 // Input len is in BYTES! 00147 // outBuffer must be at least inputLen bytes long 00148 // Returns the decrypted buffer length in BYTES and an error code < 0 in case of error 00149 int padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer); 00150 protected: 00151 void keySched(UINT8 key[_MAX_KEY_COLUMNS][4]); 00152 void keyEncToDec(); 00153 void encrypt(const UINT8 a[16], UINT8 b[16]); 00154 void decrypt(const UINT8 a[16], UINT8 b[16]); 00155 }; 00156 00157 #endif // _RIJNDAEL_H_

Generated on Sun Mar 6 13:35:50 2005 for Komssys by doxygen 1.3.8