2 * Copyright (c) 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include "rijndael-alg-fst.h"
41 * The next 4 functions are the pure software implementations
44 * AES_set_encrypt_key()
45 * AES_set_decrypt_key()
51 AES_set_encrypt_key_rj(const unsigned char *userkey, const int bits, AES_KEY *key)
53 key->u.aes_rj.rounds = rijndaelKeySetupEnc(key->u.aes_rj.key, userkey, bits);
54 if (key->u.aes_rj.rounds == 0)
60 AES_set_decrypt_key_rj(const unsigned char *userkey, const int bits, AES_KEY *key)
62 key->u.aes_rj.rounds = rijndaelKeySetupDec(key->u.aes_rj.key, userkey, bits);
63 if (key->u.aes_rj.rounds == 0)
69 AES_encrypt_rj(const unsigned char *in, unsigned char *out, const AES_KEY *key)
71 rijndaelEncrypt(key->u.aes_rj.key, key->u.aes_rj.rounds, in, out);
75 AES_decrypt_rj(const unsigned char *in, unsigned char *out, const AES_KEY *key)
77 rijndaelDecrypt(key->u.aes_rj.key, key->u.aes_rj.rounds, in, out);
81 * The next 4 functions are the runtime switch for Intel AES hardware
84 * AES_set_encrypt_key()
85 * AES_set_decrypt_key()
89 * If the hardware instructions don't exist, fall back to the software
92 * Currently only use the software implementations.
96 AES_set_encrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
98 return AES_set_encrypt_key_rj(userkey, bits, key);
102 AES_set_decrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
104 return AES_set_decrypt_key_rj(userkey, bits, key);
108 AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
110 return AES_encrypt_rj(in, out, key);
114 AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
116 return AES_decrypt_rj(in, out, key);
119 #endif /* SAMBA_RIJNDAEL */
121 #ifdef SAMBA_AES_CBC_ENCRYPT
123 AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
124 unsigned long size, const AES_KEY *key,
125 unsigned char *iv, int forward_encrypt)
127 unsigned char tmp[AES_BLOCK_SIZE];
130 if (forward_encrypt) {
131 while (size >= AES_BLOCK_SIZE) {
132 for (i = 0; i < AES_BLOCK_SIZE; i++)
133 tmp[i] = in[i] ^ iv[i];
134 AES_encrypt(tmp, out, key);
135 memcpy(iv, out, AES_BLOCK_SIZE);
136 size -= AES_BLOCK_SIZE;
137 in += AES_BLOCK_SIZE;
138 out += AES_BLOCK_SIZE;
141 for (i = 0; i < size; i++)
142 tmp[i] = in[i] ^ iv[i];
143 for (i = size; i < AES_BLOCK_SIZE; i++)
145 AES_encrypt(tmp, out, key);
146 memcpy(iv, out, AES_BLOCK_SIZE);
149 while (size >= AES_BLOCK_SIZE) {
150 memcpy(tmp, in, AES_BLOCK_SIZE);
151 AES_decrypt(tmp, out, key);
152 for (i = 0; i < AES_BLOCK_SIZE; i++)
154 memcpy(iv, tmp, AES_BLOCK_SIZE);
155 size -= AES_BLOCK_SIZE;
156 in += AES_BLOCK_SIZE;
157 out += AES_BLOCK_SIZE;
160 memcpy(tmp, in, AES_BLOCK_SIZE);
161 AES_decrypt(tmp, out, key);
162 for (i = 0; i < size; i++)
164 memcpy(iv, tmp, AES_BLOCK_SIZE);
168 #endif /* SAMBA_AES_CBC_ENCRYPT */
170 #ifdef SAMBA_AES_CFB8_ENCRYPT
172 AES_cfb8_encrypt(const unsigned char *in, unsigned char *out,
173 unsigned long size, const AES_KEY *key,
174 unsigned char *iv, int forward_encrypt)
178 for (i = 0; i < size; i++) {
179 unsigned char tmp[AES_BLOCK_SIZE + 1];
181 memcpy(tmp, iv, AES_BLOCK_SIZE);
182 AES_encrypt(iv, iv, key);
183 if (!forward_encrypt) {
184 tmp[AES_BLOCK_SIZE] = in[i];
186 out[i] = in[i] ^ iv[0];
187 if (forward_encrypt) {
188 tmp[AES_BLOCK_SIZE] = out[i];
190 memcpy(iv, &tmp[1], AES_BLOCK_SIZE);
193 #endif /* SAMBA_AES_CFB8_ENCRYPT */