Merge branch 'vendor/BIND' into bind_vendor2
[dragonfly.git] / sys / crypto / via / padlock_cipher.c
1 /*-
2  * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * Copyright (c) 2004 Mark R V Murray
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  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 /*      $OpenBSD: via.c,v 1.3 2004/06/15 23:36:55 deraadt Exp $ */
29 /*      $FreeBSD: src/sys/crypto/via/padlock_cipher.c,v 1.5 2006/09/15 10:44:55 pjd Exp $ */
30 /*-
31  * Copyright (c) 2003 Jason Wright
32  * Copyright (c) 2003, 2004 Theo de Raadt
33  * All rights reserved.
34  *
35  * Permission to use, copy, modify, and distribute this software for any
36  * purpose with or without fee is hereby granted, provided that the above
37  * copyright notice and this permission notice appear in all copies.
38  *
39  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
40  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
41  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
42  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
43  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
44  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
45  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
52 #include <sys/malloc.h>
53 #include <sys/libkern.h>
54 #include <sys/uio.h>
55
56 #include <opencrypto/cryptodev.h>
57 #include <crypto/rijndael/rijndael.h>
58
59 #include <crypto/via/padlock.h>
60
61 #define PADLOCK_ROUND_COUNT_AES128      10
62 #define PADLOCK_ROUND_COUNT_AES192      12
63 #define PADLOCK_ROUND_COUNT_AES256      14
64
65 #define PADLOCK_ALGORITHM_TYPE_AES      0
66
67 #define PADLOCK_KEY_GENERATION_HW       0
68 #define PADLOCK_KEY_GENERATION_SW       1
69
70 #define PADLOCK_DIRECTION_ENCRYPT       0
71 #define PADLOCK_DIRECTION_DECRYPT       1
72
73 #define PADLOCK_KEY_SIZE_128    0
74 #define PADLOCK_KEY_SIZE_192    1
75 #define PADLOCK_KEY_SIZE_256    2
76
77 MALLOC_DECLARE(M_PADLOCK);
78
79 static __inline void
80 padlock_cbc(void *in, void *out, size_t count, void *key, union padlock_cw *cw,
81     void *iv)
82 {
83 #ifdef __GNUCLIKE_ASM
84         /* The .byte line is really VIA C3 "xcrypt-cbc" instruction */
85         __asm __volatile(
86                 "pushf                          \n\t"
87                 "popf                           \n\t"
88                 "rep                            \n\t"
89                 ".byte  0x0f, 0xa7, 0xd0"
90                         : "+a" (iv), "+c" (count), "+D" (out), "+S" (in)
91                         : "b" (key), "d" (cw)
92                         : "cc", "memory"
93                 );
94 #endif
95 }
96
97 static void
98 padlock_cipher_key_setup(struct padlock_session *ses, caddr_t key, int klen)
99 {
100         union padlock_cw *cw;
101         int i;
102
103         cw = &ses->ses_cw;
104         if (cw->cw_key_generation == PADLOCK_KEY_GENERATION_SW) {
105                 /* Build expanded keys for both directions */
106                 rijndaelKeySetupEnc(ses->ses_ekey, key, klen);
107                 rijndaelKeySetupDec(ses->ses_dkey, key, klen);
108                 for (i = 0; i < 4 * (RIJNDAEL_MAXNR + 1); i++) {
109                         ses->ses_ekey[i] = ntohl(ses->ses_ekey[i]);
110                         ses->ses_dkey[i] = ntohl(ses->ses_dkey[i]);
111                 }
112         } else {
113                 bcopy(key, ses->ses_ekey, klen);
114                 bcopy(key, ses->ses_dkey, klen);
115         }
116 }
117
118 int
119 padlock_cipher_setup(struct padlock_session *ses, struct cryptoini *encini)
120 {
121         union padlock_cw *cw;
122
123         if (encini->cri_klen != 128 && encini->cri_klen != 192 &&
124             encini->cri_klen != 256) {
125                 return (EINVAL);
126         }
127
128         cw = &ses->ses_cw;
129         bzero(cw, sizeof(*cw));
130         cw->cw_algorithm_type = PADLOCK_ALGORITHM_TYPE_AES;
131         cw->cw_key_generation = PADLOCK_KEY_GENERATION_SW;
132         cw->cw_intermediate = 0;
133         switch (encini->cri_klen) {
134         case 128:
135                 cw->cw_round_count = PADLOCK_ROUND_COUNT_AES128;
136                 cw->cw_key_size = PADLOCK_KEY_SIZE_128;
137 #ifdef HW_KEY_GENERATION
138                 /* This doesn't buy us much, that's why it is commented out. */
139                 cw->cw_key_generation = PADLOCK_KEY_GENERATION_HW;
140 #endif
141                 break;
142         case 192:
143                 cw->cw_round_count = PADLOCK_ROUND_COUNT_AES192;
144                 cw->cw_key_size = PADLOCK_KEY_SIZE_192;
145                 break;
146         case 256:
147                 cw->cw_round_count = PADLOCK_ROUND_COUNT_AES256;
148                 cw->cw_key_size = PADLOCK_KEY_SIZE_256;
149                 break;
150         }
151         if (encini->cri_key != NULL) {
152                 padlock_cipher_key_setup(ses, encini->cri_key,
153                     encini->cri_klen);
154         }
155
156         karc4rand(ses->ses_iv, sizeof(ses->ses_iv));
157         return (0);
158 }
159
160 /*
161  * Function checks if the given buffer is already 16 bytes aligned.
162  * If it is there is no need to allocate new buffer.
163  * If it isn't, new buffer is allocated.
164  */
165 static u_char *
166 padlock_cipher_alloc(struct cryptodesc *enccrd, struct cryptop *crp,
167     int *allocated)
168 {
169         u_char *addr;
170
171         if (crp->crp_flags & CRYPTO_F_IMBUF)
172                 goto alloc;
173         else {
174                 if (crp->crp_flags & CRYPTO_F_IOV) {
175                         struct uio *uio;
176                         struct iovec *iov;
177
178                         uio = (struct uio *)crp->crp_buf;
179                         if (uio->uio_iovcnt != 1)
180                                 goto alloc;
181                         iov = uio->uio_iov;
182                         addr = (u_char *)iov->iov_base + enccrd->crd_skip;
183                 } else {
184                         addr = (u_char *)crp->crp_buf;
185                 }
186                 if (((uintptr_t)addr & 0xf) != 0) /* 16 bytes aligned? */
187                         goto alloc;
188                 *allocated = 0;
189                 return (addr);
190         }
191 alloc:
192         *allocated = 1;
193         addr = kmalloc(enccrd->crd_len + 16, M_PADLOCK, M_NOWAIT);
194         return (addr);
195 }
196
197 int
198 padlock_cipher_process(struct padlock_session *ses, struct cryptodesc *enccrd,
199     struct cryptop *crp)
200 {
201         union padlock_cw *cw;
202         u_char *buf, *abuf;
203         uint32_t *key;
204         int allocated;
205
206         buf = padlock_cipher_alloc(enccrd, crp, &allocated);
207         if (buf == NULL)
208                 return (ENOMEM);
209         /* Buffer has to be 16 bytes aligned. */
210         abuf = PADLOCK_ALIGN(buf);
211
212         if ((enccrd->crd_flags & CRD_F_KEY_EXPLICIT) != 0) {
213                 padlock_cipher_key_setup(ses, enccrd->crd_key,
214                     enccrd->crd_klen);
215         }
216
217         cw = &ses->ses_cw;
218         cw->cw_filler0 = 0;
219         cw->cw_filler1 = 0;
220         cw->cw_filler2 = 0;
221         cw->cw_filler3 = 0;
222         if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) {
223                 cw->cw_direction = PADLOCK_DIRECTION_ENCRYPT;
224                 key = ses->ses_ekey;
225                 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
226                         bcopy(enccrd->crd_iv, ses->ses_iv, AES_BLOCK_LEN);
227
228                 if ((enccrd->crd_flags & CRD_F_IV_PRESENT) == 0) {
229                         crypto_copyback(crp->crp_flags, crp->crp_buf,
230                             enccrd->crd_inject, AES_BLOCK_LEN, ses->ses_iv);
231                 }
232         } else {
233                 cw->cw_direction = PADLOCK_DIRECTION_DECRYPT;
234                 key = ses->ses_dkey;
235                 if ((enccrd->crd_flags & CRD_F_IV_EXPLICIT) != 0)
236                         bcopy(enccrd->crd_iv, ses->ses_iv, AES_BLOCK_LEN);
237                 else {
238                         crypto_copydata(crp->crp_flags, crp->crp_buf,
239                             enccrd->crd_inject, AES_BLOCK_LEN, ses->ses_iv);
240                 }
241         }
242
243         if (allocated) {
244                 crypto_copydata(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
245                     enccrd->crd_len, abuf);
246         }
247
248         padlock_cbc(abuf, abuf, enccrd->crd_len / AES_BLOCK_LEN, key, cw,
249             ses->ses_iv);
250
251         if (allocated) {
252                 crypto_copyback(crp->crp_flags, crp->crp_buf, enccrd->crd_skip,
253                     enccrd->crd_len, abuf);
254         }
255
256         /* copy out last block for use as next session IV */
257         if ((enccrd->crd_flags & CRD_F_ENCRYPT) != 0) {
258                 crypto_copydata(crp->crp_flags, crp->crp_buf,
259                     enccrd->crd_skip + enccrd->crd_len - AES_BLOCK_LEN,
260                     AES_BLOCK_LEN, ses->ses_iv);
261         }
262
263         if (allocated) {
264                 bzero(buf, enccrd->crd_len + 16);
265                 kfree(buf, M_PADLOCK);
266         }
267         return (0);
268 }