1 /*
2  *  Copyright (c) 2014, Peter Haag
3  *  Copyright (c) 2009, Peter Haag
4  *  Copyright (c) 2004-2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions are met:
9  *
10  *   * Redistributions of source code must retain the above copyright notice,
11  *     this list of conditions and the following disclaimer.
12  *   * Redistributions in binary form must reproduce the above copyright notice,
13  *     this list of conditions and the following disclaimer in the documentation
14  *     and/or other materials provided with the distribution.
15  *   * Neither the name of the author nor the names of its contributors may be
16  *     used to endorse or promote products derived from this software without
17  *     specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  *
31  *  $NfDump Author:$
32  *
33  *  $Id: rijndael.h 39 2009-11-25 08:11:15Z haag $
34  *
35  *  $LastChangedRevision: 39 $
36  *
37  */
38 
39 /* Original disclaimer
40  * Atlanta, Georgia 30332.
41  * All Rights Reserved
42  *
43  * The following Software is posted on the Internet by the Georgia
44  * Tech Research Corporation (GTRC). It was developed by employees
45  * of the Georgia Institute of Technology in the College of Computing.
46  * GTRC hereby grants to the user a non-exclusive, royalty-free
47  * license to utilize such Software for the User's own purposes
48  * pursuant to the following conditions.
49  *
50  *
51  * THE SOFTWARE IS LICENSED ON AN "AS IS" BASIS. GTRC MAKES NO WARRANTY
52  * THAT ALL ERRORS CAN BE OR HAVE BEEN ELIMINATED FROM THE SOFTWARE.
53  * GTRC SHALL NOT BE RESPONSIBLE FOR LOSSES OF ANY KIND RESULTING FROM
54  * THE USE OF THE SOFTWARE AND ITS ACCOMPANYING DOCUMENTATION, AND CAN
55  * IN NO WAY PROVIDE COMPENSATION FOR ANY LOSSES SUSTAINED, INCLUDING
56  * BUT NOT LIMITED TO ANY OBLIGATION, LIABILITY, RIGHT, CLAIM OR REMEDY
57  * FOR TORT, OF FOR ANY ACTUAL OR ALLEGED INFRINGEMENT OF PATENTS, COPYRIGHTS,
58  * TRADE SECRETS, OR SIMILAR RIGHTS OF THIRD PARTIES, NOR ANY BUSINESS
59  * EXPENSE, MACHINE DOWNTIME, OR DAMAGES CAUSED LICENSEE BY ANY DEFICIENCY,
60  * DEFECT OR ERROR IN THE SOFTWARE OR MALFUNCTION THEREOF, NOR ANY
61  * INCIDENTAL OR CONSEQUENTIAL DAMAGES, HOWEVER CAUSED. GTRC DISCLAIMS
62  * ALL WARRANTIES, BOTH EXPRESS AND IMPLIED RESPECTING THE USE AND
63  * OPERATION OF THE SOFTWARE AND ANY ACCOMPANYING DOCUMENTATION,
64  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65  * PARTICULAR PURPOSE AND ANY IMPLIED WARRANTY ARISING FROM COURSE
66  * OF PERFORMANCE, COURSE OF DEALING OR USAGE OF TRADE. GTRC MAKES NO
67  * WARRANTY THAT THE SOFTWARE IS ADEQUATELY OR COMPLETELY DESCRIBED
68  * IN, OR BEHAVES IN ACCORDANCE WITH ANY OF THE ACCOMPANYING
69  * DOCUMENTATION. THE USER OF THE SOFTWARE IS EXPECTED TO MAKE THE FINAL
70  * EVALUATION OF THE SOFTWARE'S USEFULNESS IN USER'S OWN ENVIRONMENT.
71  *
72  *
73  */
74 
75 #ifndef _RIJNDAEL_H_
76 #define _RIJNDAEL_H_ 1
77 //
78 // File : rijndael.h
79 // Creation date : Sun Nov 5 2000 03:21:05 CEST
80 // Author : Szymon Stefanek (stefanek@tin.it)
81 //
82 // Another implementation of the Rijndael cipher.
83 // This is intended to be an easily usable library file.
84 // This code is public domain.
85 // Based on the Vincent Rijmen and K.U.Leuven implementation 2.4.
86 //
87 
88 //
89 // Original Copyright notice:
90 //
91 //    rijndael-alg-fst.c   v2.4   April '2000
92 //    rijndael-alg-fst.h
93 //    rijndael-api-fst.c
94 //    rijndael-api-fst.h
95 //
96 //    Optimised ANSI C code
97 //
98 //    authors: v1.0: Antoon Bosselaers
99 //             v2.0: Vincent Rijmen, K.U.Leuven
100 //             v2.3: Paulo Barreto
101 //             v2.4: Vincent Rijmen, K.U.Leuven
102 //
103 //    This code is placed in the public domain.
104 //
105 
106 //
107 // This implementation works on 128 , 192 , 256 bit keys
108 // and on 128 bit blocks
109 //
110 
111 //
112 // Example of usage:
113 //
114 //  // Input data
115 //  unsigned char key[32];                       // The key
116 //  initializeYour256BitKey();                   // Obviously initialized with sth
117 //  const unsigned char * plainText = getYourPlainText(); // Your plain text
118 //  int plainTextLen = strlen(plainText);        // Plain text length
119 //
120 //  // Encrypting
121 //  Rijndael rin;
122 //  unsigned char output[plainTextLen + 16];
123 //
124 //  rin.init(Rijndael::CBC,Rijndael::Encrypt,key,Rijndael::Key32Bytes);
125 //  // It is a good idea to check the error code
126 //  int len = rin.padEncrypt(plainText,len,output);
127 //  if(len >= 0)useYourEncryptedText();
128 //  else encryptError(len);
129 //
130 //  // Decrypting: we can reuse the same object
131 //  unsigned char output2[len];
132 //  rin.init(Rijndael::CBC,Rijndael::Decrypt,key,Rijndael::Key32Bytes));
133 //  len = rin.padDecrypt(output,len,output2);
134 //  if(len >= 0)useYourDecryptedText();
135 //  else decryptError(len);
136 //
137 
138 #define _MAX_KEY_COLUMNS (256/32)
139 #define _MAX_ROUNDS      14
140 #define MAX_IV_SIZE      16
141 
142 // Error codes
143 #define RIJNDAEL_SUCCESS 0
144 #define RIJNDAEL_UNSUPPORTED_MODE -1
145 #define RIJNDAEL_UNSUPPORTED_DIRECTION -2
146 #define RIJNDAEL_UNSUPPORTED_KEY_LENGTH -3
147 #define RIJNDAEL_BAD_KEY -4
148 #define RIJNDAEL_NOT_INITIALIZED -5
149 #define RIJNDAEL_BAD_DIRECTION -6
150 #define RIJNDAEL_CORRUPTED_DATA -7
151 
152 
153 enum Direction { Encrypt , Decrypt };
154 enum Mode { ECB , CBC , CFB1 };
155 enum KeyLength { Key16Bytes , Key24Bytes , Key32Bytes };
156 
157 //////////////////////////////////////////////////////////////////////////////////////////
158 // API
159 //////////////////////////////////////////////////////////////////////////////////////////
160 
161 // init(): Initializes the crypt session
162 // Returns RIJNDAEL_SUCCESS or an error code
163 // mode      : ECB, CBC or CFB1
164 //             You have to use the same mode for encrypting and decrypting
165 // dir       : Encrypt or Decrypt
166 //             A cipher instance works only in one direction
167 //             (Well , it could be easily modified to work in both
168 //             directions with a single init() call, but it looks
169 //             useless to me...anyway , it is a matter of generating
170 //             two expanded keys)
171 // key       : array of unsigned octets , it can be 16 , 24 or 32 bytes long
172 //             this CAN be binary data (it is not expected to be null terminated)
173 // keyLen    : Key16Bytes , Key24Bytes or Key32Bytes
174 // initVector: initialization vector, you will usually use 0 here
175 int Rijndael_init(int mode,int dir,const uint8_t * key,int keyLen,uint8_t * initVector);
176 // Encrypts the input array (can be binary data)
177 // The input array length must be a multiple of 16 bytes, the remaining part
178 // is DISCARDED.
179 // so it actually encrypts inputLen / 128 blocks of input and puts it in outBuffer
180 // Input len is in BITS!
181 // outBuffer must be at least inputLen / 8 bytes long.
182 // Returns the encrypted buffer length in BITS or an error code < 0 in case of error
183 int Rijndael_blockEncrypt(const uint8_t *input, int inputLen, uint8_t *outBuffer);
184 // Encrypts the input array (can be binary data)
185 // The input array can be any length , it is automatically padded on a 16 byte boundary.
186 // Input len is in BYTES!
187 // outBuffer must be at least (inputLen + 16) bytes long
188 // Returns the encrypted buffer length in BYTES or an error code < 0 in case of error
189 int Rijndael_padEncrypt(const uint8_t *input, int inputOctets, uint8_t *outBuffer);
190 // Decrypts the input vector
191 // Input len is in BITS!
192 // outBuffer must be at least inputLen / 8 bytes long
193 // Returns the decrypted buffer length in BITS and an error code < 0 in case of error
194 int Rijndael_blockDecrypt(const uint8_t *input, int inputLen, uint8_t *outBuffer);
195 // Decrypts the input vector
196 // Input len is in BYTES!
197 // outBuffer must be at least inputLen bytes long
198 // Returns the decrypted buffer length in BYTES and an error code < 0 in case of error
199 int Rijndael_padDecrypt(const uint8_t *input, int inputOctets, uint8_t *outBuffer);
200 #endif
201