lib: crypt: Prepare the existing code to switch to Intel AES hardware instructions.
[metze/samba/wip.git] / lib / crypto / aes.c
1 /*
2  * Copyright (c) 2003 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #include "replace.h"
35 #include "aes.h"
36
37 #ifdef SAMBA_RIJNDAEL
38 #include "rijndael-alg-fst.h"
39
40 /*
41  * The next 4 functions are the pure software implementations
42  * of:
43  *
44  * AES_set_encrypt_key()
45  * AES_set_decrypt_key()
46  * AES_encrypt()
47  * AES_decrypt()
48  */
49
50 static int
51 AES_set_encrypt_key_rj(const unsigned char *userkey, const int bits, AES_KEY *key)
52 {
53     key->u.aes_rj.rounds = rijndaelKeySetupEnc(key->u.aes_rj.key, userkey, bits);
54     if (key->u.aes_rj.rounds == 0)
55         return -1;
56     return 0;
57 }
58
59 static int
60 AES_set_decrypt_key_rj(const unsigned char *userkey, const int bits, AES_KEY *key)
61 {
62     key->u.aes_rj.rounds = rijndaelKeySetupDec(key->u.aes_rj.key, userkey, bits);
63     if (key->u.aes_rj.rounds == 0)
64         return -1;
65     return 0;
66 }
67
68 static void
69 AES_encrypt_rj(const unsigned char *in, unsigned char *out, const AES_KEY *key)
70 {
71     rijndaelEncrypt(key->u.aes_rj.key, key->u.aes_rj.rounds, in, out);
72 }
73
74 static void
75 AES_decrypt_rj(const unsigned char *in, unsigned char *out, const AES_KEY *key)
76 {
77     rijndaelDecrypt(key->u.aes_rj.key, key->u.aes_rj.rounds, in, out);
78 }
79
80 /*
81  * The next 4 functions are the runtime switch for Intel AES hardware
82  * implementations of:
83  *
84  * AES_set_encrypt_key()
85  * AES_set_decrypt_key()
86  * AES_encrypt()
87  * AES_decrypt()
88  *
89  * If the hardware instructions don't exist, fall back to the software
90  * versions.
91  *
92  * Currently only use the software implementations.
93  */
94
95 int
96 AES_set_encrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
97 {
98         return AES_set_encrypt_key_rj(userkey, bits, key);
99 }
100
101 int
102 AES_set_decrypt_key(const unsigned char *userkey, const int bits, AES_KEY *key)
103 {
104         return AES_set_decrypt_key_rj(userkey, bits, key);
105 }
106
107 void
108 AES_encrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
109 {
110         return AES_encrypt_rj(in, out, key);
111 }
112
113 void
114 AES_decrypt(const unsigned char *in, unsigned char *out, const AES_KEY *key)
115 {
116         return AES_decrypt_rj(in, out, key);
117 }
118
119 #endif /* SAMBA_RIJNDAEL */
120
121 #ifdef SAMBA_AES_CBC_ENCRYPT
122 void
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)
126 {
127     unsigned char tmp[AES_BLOCK_SIZE];
128     int i;
129
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;
139         }
140         if (size) {
141             for (i = 0; i < size; i++)
142                 tmp[i] = in[i] ^ iv[i];
143             for (i = size; i < AES_BLOCK_SIZE; i++)
144                 tmp[i] = iv[i];
145             AES_encrypt(tmp, out, key);
146             memcpy(iv, out, AES_BLOCK_SIZE);
147         }
148     } else {
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++)
153                 out[i] ^= iv[i];
154             memcpy(iv, tmp, AES_BLOCK_SIZE);
155             size -= AES_BLOCK_SIZE;
156             in += AES_BLOCK_SIZE;
157             out += AES_BLOCK_SIZE;
158         }
159         if (size) {
160             memcpy(tmp, in, AES_BLOCK_SIZE);
161             AES_decrypt(tmp, out, key);
162             for (i = 0; i < size; i++)
163                 out[i] ^= iv[i];
164             memcpy(iv, tmp, AES_BLOCK_SIZE);
165         }
166     }
167 }
168 #endif /* SAMBA_AES_CBC_ENCRYPT */
169
170 #ifdef SAMBA_AES_CFB8_ENCRYPT
171 void
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)
175 {
176     int i;
177
178     for (i = 0; i < size; i++) {
179         unsigned char tmp[AES_BLOCK_SIZE + 1];
180
181         memcpy(tmp, iv, AES_BLOCK_SIZE);
182         AES_encrypt(iv, iv, key);
183         if (!forward_encrypt) {
184             tmp[AES_BLOCK_SIZE] = in[i];
185         }
186         out[i] = in[i] ^ iv[0];
187         if (forward_encrypt) {
188             tmp[AES_BLOCK_SIZE] = out[i];
189         }
190         memcpy(iv, &tmp[1], AES_BLOCK_SIZE);
191     }
192 }
193 #endif /* SAMBA_AES_CFB8_ENCRYPT */