Root/target/linux/generic/files/crypto/ocf/c7108/aes-7108.c

1/*
2 * Copyright (C) 2006 Micronas USA
3 *
4 * 1. Redistributions of source code must retain the above copyright
5 * notice, this list of conditions and the following disclaimer.
6 * 2. Redistributions in binary form must reproduce the above copyright
7 * notice, this list of conditions and the following disclaimer in the
8 * documentation and/or other materials provided with the distribution.
9 * 3. The name of the author may not be used to endorse or promote products
10 * derived from this software without specific prior written permission.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
13 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
16 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 *
23 * Effort sponsored in part by the Defense Advanced Research Projects
24 * Agency (DARPA) and Air Force Research Laboratory, Air Force
25 * Materiel Command, USAF, under agreement number F30602-01-2-0537.
26 *
27 */
28
29//#include <linux/config.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/slab.h>
34#include <linux/sched.h>
35#include <linux/wait.h>
36#include <linux/crypto.h>
37#include <linux/mm.h>
38#include <linux/skbuff.h>
39#include <linux/random.h>
40#include <asm/io.h>
41#include <asm/delay.h>
42//#include <asm/scatterlist.h>
43#include <linux/scatterlist.h>
44#include <linux/dma-mapping.h>
45#include <linux/highmem.h>
46#include <cryptodev.h>
47#include <uio.h>
48#include <aes-7108.h>
49
50/* Runtime mode */
51static int c7108_crypto_mode = C7108_AES_CTRL_MODE_CTR;
52//static int c7108_crypto_mode = C7108_AES_CTRL_MODE_CBC;
53
54static int32_t c7108_id = -1;
55static struct cipher_7108 **c7108_sessions = NULL;
56static u_int32_t c7108_sesnum = 0;
57static unsigned long iobar;
58
59/* Crypto entry points */
60static int c7108_process(void *, struct cryptop *, int);
61static int c7108_newsession(void *, u_int32_t *, struct cryptoini *);
62static int c7108_freesession(void *, u_int64_t);
63
64/* Globals */
65static int debug = 0;
66static spinlock_t csr_mutex;
67
68/* Generic controller-based lock */
69#define AES_LOCK()\
70          spin_lock(&csr_mutex)
71#define AES_UNLOCK()\
72          spin_unlock(&csr_mutex)
73
74/* 7108 AES register access */
75#define c7108_reg_wr8(a,d) iowrite8(d, (void*)(iobar+(a)))
76#define c7108_reg_wr16(a,d) iowrite16(d, (void*)(iobar+(a)))
77#define c7108_reg_wr32(a,d) iowrite32(d, (void*)(iobar+(a)))
78#define c7108_reg_rd8(a) ioread8((void*)(iobar+(a)))
79#define c7108_reg_rd16(a) ioread16((void*)(iobar+(a)))
80#define c7108_reg_rd32(a) ioread32((void*)(iobar+(a)))
81
82static int
83c7108_xlate_key(int klen, u8* k8ptr, u32* k32ptr)
84{
85        int i, nw=0;
86    nw = ((klen >= 256) ? 8 : (klen >= 192) ? 6 : 4);
87    for ( i = 0; i < nw; i++) {
88        k32ptr[i] = (k8ptr[i+3] << 24) | (k8ptr[i+2] << 16) |
89                   (k8ptr[i+1] << 8) | k8ptr[i];
90        
91    }
92    return 0;
93}
94
95static int
96c7108_cache_key(int klen, u32* k32ptr, u8* k8ptr)
97{
98        int i, nb=0;
99    u8* ptr = (u8*)k32ptr;
100    nb = ((klen >= 256) ? 32 : (klen >= 192) ? 24 : 16);
101    for ( i = 0; i < nb; i++)
102        k8ptr[i] = ptr[i];
103    return 0;
104}
105
106static int
107c7108_aes_setup_dma(u32 src, u32 dst, u32 len)
108{
109        if (len < 16) {
110        printk("len < 16\n");
111        return -10;
112    }
113    if (len % 16) {
114        printk("len not multiple of 16\n");
115        return -11;
116    }
117    c7108_reg_wr16(C7108_AES_DMA_SRC0_LO, (u16) src);
118    c7108_reg_wr16(C7108_AES_DMA_SRC0_HI, (u16)((src & 0xffff0000) >> 16));
119    c7108_reg_wr16(C7108_AES_DMA_DST0_LO, (u16) dst);
120    c7108_reg_wr16(C7108_AES_DMA_DST0_HI, (u16)((dst & 0xffff0000) >> 16));
121    c7108_reg_wr16(C7108_AES_DMA_LEN, (u16) ((len / 16) - 1));
122
123    return 0;
124}
125
126static int
127c7108_aes_set_hw_iv(u8 iv[16])
128{
129        c7108_reg_wr16(C7108_AES_IV0_LO, (u16) ((iv[1] << 8) | iv[0]));
130    c7108_reg_wr16(C7108_AES_IV0_HI, (u16) ((iv[3] << 8) | iv[2]));
131    c7108_reg_wr16(C7108_AES_IV1_LO, (u16) ((iv[5] << 8) | iv[4]));
132    c7108_reg_wr16(C7108_AES_IV1_HI, (u16) ((iv[7] << 8) | iv[6]));
133    c7108_reg_wr16(C7108_AES_IV2_LO, (u16) ((iv[9] << 8) | iv[8]));
134    c7108_reg_wr16(C7108_AES_IV2_HI, (u16) ((iv[11] << 8) | iv[10]));
135    c7108_reg_wr16(C7108_AES_IV3_LO, (u16) ((iv[13] << 8) | iv[12]));
136    c7108_reg_wr16(C7108_AES_IV3_HI, (u16) ((iv[15] << 8) | iv[14]));
137
138    return 0;
139}
140
141static void
142c7108_aes_read_dkey(u32 * dkey)
143{
144        dkey[0] = (c7108_reg_rd16(C7108_AES_EKEY0_HI) << 16) |
145               c7108_reg_rd16(C7108_AES_EKEY0_LO);
146    dkey[1] = (c7108_reg_rd16(C7108_AES_EKEY1_HI) << 16) |
147               c7108_reg_rd16(C7108_AES_EKEY1_LO);
148    dkey[2] = (c7108_reg_rd16(C7108_AES_EKEY2_HI) << 16) |
149               c7108_reg_rd16(C7108_AES_EKEY2_LO);
150    dkey[3] = (c7108_reg_rd16(C7108_AES_EKEY3_HI) << 16) |
151               c7108_reg_rd16(C7108_AES_EKEY3_LO);
152    dkey[4] = (c7108_reg_rd16(C7108_AES_EKEY4_HI) << 16) |
153                   c7108_reg_rd16(C7108_AES_EKEY4_LO);
154    dkey[5] = (c7108_reg_rd16(C7108_AES_EKEY5_HI) << 16) |
155                   c7108_reg_rd16(C7108_AES_EKEY5_LO);
156    dkey[6] = (c7108_reg_rd16(C7108_AES_EKEY6_HI) << 16) |
157                   c7108_reg_rd16(C7108_AES_EKEY6_LO);
158    dkey[7] = (c7108_reg_rd16(C7108_AES_EKEY7_HI) << 16) |
159                   c7108_reg_rd16(C7108_AES_EKEY7_LO);
160}
161
162static int
163c7108_aes_cipher(int op,
164         u32 dst,
165         u32 src,
166         u32 len,
167         int klen,
168         u16 mode,
169         u32 key[8],
170         u8 iv[16])
171{
172        int rv = 0, cnt=0;
173    u16 ctrl = 0, stat = 0;
174
175    AES_LOCK();
176
177    /* Setup key length */
178    if (klen == 128) {
179        ctrl |= C7108_AES_KEY_LEN_128;
180    } else if (klen == 192) {
181        ctrl |= C7108_AES_KEY_LEN_192;
182    } else if (klen == 256) {
183        ctrl |= C7108_AES_KEY_LEN_256;
184    } else {
185        AES_UNLOCK();
186        return -3;
187    }
188    
189    /* Check opcode */
190    if (C7108_AES_ENCRYPT == op) {
191        ctrl |= C7108_AES_ENCRYPT;
192    } else if (C7108_AES_DECRYPT == op) {
193        ctrl |= C7108_AES_DECRYPT;
194    } else {
195        AES_UNLOCK();
196        return -4;
197    }
198    
199    /* check mode */
200    if ( (mode != C7108_AES_CTRL_MODE_CBC) &&
201         (mode != C7108_AES_CTRL_MODE_CFB) &&
202         (mode != C7108_AES_CTRL_MODE_OFB) &&
203         (mode != C7108_AES_CTRL_MODE_CTR) &&
204         (mode != C7108_AES_CTRL_MODE_ECB) ) {
205        AES_UNLOCK();
206        return -5;
207    }
208    
209    /* Now set mode */
210    ctrl |= mode;
211    
212    /* For CFB, OFB, and CTR, neither backward key
213     * expansion nor key inversion is required.
214     */
215    if ( (C7108_AES_DECRYPT == op) &&
216         (C7108_AES_CTRL_MODE_CBC == mode ||
217          C7108_AES_CTRL_MODE_ECB == mode ) ){
218
219        /* Program Key */
220        c7108_reg_wr16(C7108_AES_KEY0_LO, (u16) key[4]);
221        c7108_reg_wr16(C7108_AES_KEY0_HI, (u16) (key[4] >> 16));
222        c7108_reg_wr16(C7108_AES_KEY1_LO, (u16) key[5]);
223        c7108_reg_wr16(C7108_AES_KEY1_HI, (u16) (key[5] >> 16));
224        c7108_reg_wr16(C7108_AES_KEY2_LO, (u16) key[6]);
225        c7108_reg_wr16(C7108_AES_KEY2_HI, (u16) (key[6] >> 16));
226        c7108_reg_wr16(C7108_AES_KEY3_LO, (u16) key[7]);
227        c7108_reg_wr16(C7108_AES_KEY3_HI, (u16) (key[7] >> 16));
228        c7108_reg_wr16(C7108_AES_KEY6_LO, (u16) key[2]);
229        c7108_reg_wr16(C7108_AES_KEY6_HI, (u16) (key[2] >> 16));
230        c7108_reg_wr16(C7108_AES_KEY7_LO, (u16) key[3]);
231        c7108_reg_wr16(C7108_AES_KEY7_HI, (u16) (key[3] >> 16));
232        
233        
234        if (192 == klen) {
235        c7108_reg_wr16(C7108_AES_KEY4_LO, (u16) key[7]);
236        c7108_reg_wr16(C7108_AES_KEY4_HI, (u16) (key[7] >> 16));
237        c7108_reg_wr16(C7108_AES_KEY5_LO, (u16) key[7]);
238        c7108_reg_wr16(C7108_AES_KEY5_HI, (u16) (key[7] >> 16));
239        
240        } else if (256 == klen) {
241        /* 256 */
242        c7108_reg_wr16(C7108_AES_KEY4_LO, (u16) key[0]);
243        c7108_reg_wr16(C7108_AES_KEY4_HI, (u16) (key[0] >> 16));
244        c7108_reg_wr16(C7108_AES_KEY5_LO, (u16) key[1]);
245        c7108_reg_wr16(C7108_AES_KEY5_HI, (u16) (key[1] >> 16));
246        
247        }
248        
249    } else {
250        /* Program Key */
251        c7108_reg_wr16(C7108_AES_KEY0_LO, (u16) key[0]);
252        c7108_reg_wr16(C7108_AES_KEY0_HI, (u16) (key[0] >> 16));
253        c7108_reg_wr16(C7108_AES_KEY1_LO, (u16) key[1]);
254        c7108_reg_wr16(C7108_AES_KEY1_HI, (u16) (key[1] >> 16));
255        c7108_reg_wr16(C7108_AES_KEY2_LO, (u16) key[2]);
256        c7108_reg_wr16(C7108_AES_KEY2_HI, (u16) (key[2] >> 16));
257        c7108_reg_wr16(C7108_AES_KEY3_LO, (u16) key[3]);
258        c7108_reg_wr16(C7108_AES_KEY3_HI, (u16) (key[3] >> 16));
259        c7108_reg_wr16(C7108_AES_KEY4_LO, (u16) key[4]);
260        c7108_reg_wr16(C7108_AES_KEY4_HI, (u16) (key[4] >> 16));
261        c7108_reg_wr16(C7108_AES_KEY5_LO, (u16) key[5]);
262        c7108_reg_wr16(C7108_AES_KEY5_HI, (u16) (key[5] >> 16));
263        c7108_reg_wr16(C7108_AES_KEY6_LO, (u16) key[6]);
264        c7108_reg_wr16(C7108_AES_KEY6_HI, (u16) (key[6] >> 16));
265        c7108_reg_wr16(C7108_AES_KEY7_LO, (u16) key[7]);
266        c7108_reg_wr16(C7108_AES_KEY7_HI, (u16) (key[7] >> 16));
267        
268    }
269    
270    /* Set IV always */
271    c7108_aes_set_hw_iv(iv);
272    
273    /* Program DMA addresses */
274    if ((rv = c7108_aes_setup_dma(src, dst, len)) < 0) {
275        AES_UNLOCK();
276        return rv;
277    }
278
279    
280    /* Start AES cipher */
281    c7108_reg_wr16(C7108_AES_CTRL, ctrl | C7108_AES_GO);
282    
283    //printk("Ctrl: 0x%x\n", ctrl | C7108_AES_GO);
284    do {
285        /* TODO: interrupt mode */
286        // printk("aes_stat=0x%x\n", stat);
287        //udelay(100);
288    } while ((cnt++ < 1000000) &&
289         !((stat=c7108_reg_rd16(C7108_AES_CTRL))&C7108_AES_OP_DONE));
290
291
292    if ((mode == C7108_AES_CTRL_MODE_ECB)||
293        (mode == C7108_AES_CTRL_MODE_CBC)) {
294        /* Save out key when the lock is held ... */
295        c7108_aes_read_dkey(key);
296    }
297    
298    AES_UNLOCK();
299    return 0;
300    
301}
302
303/*
304 * Generate a new crypto device session.
305 */
306static int
307c7108_newsession(void *arg, u_int32_t *sid, struct cryptoini *cri)
308{
309    struct cipher_7108 **swd;
310    u_int32_t i;
311    char *algo;
312    int mode, xfm_type;
313
314    dprintk("%s()\n", __FUNCTION__);
315    if (sid == NULL || cri == NULL) {
316        dprintk("%s,%d - EINVAL\n", __FILE__, __LINE__);
317        return EINVAL;
318    }
319
320    if (c7108_sessions) {
321        for (i = 1; i < c7108_sesnum; i++)
322            if (c7108_sessions[i] == NULL)
323                break;
324    } else
325        i = 1; /* NB: to silence compiler warning */
326
327    if (c7108_sessions == NULL || i == c7108_sesnum) {
328        if (c7108_sessions == NULL) {
329        i = 1; /* We leave c7108_sessions[0] empty */
330        c7108_sesnum = CRYPTO_SW_SESSIONS;
331        } else
332        c7108_sesnum *= 2;
333        
334        swd = kmalloc(c7108_sesnum * sizeof(struct cipher_7108 *),
335              GFP_ATOMIC);
336        if (swd == NULL) {
337        /* Reset session number */
338        if (c7108_sesnum == CRYPTO_SW_SESSIONS)
339            c7108_sesnum = 0;
340        else
341            c7108_sesnum /= 2;
342        dprintk("%s,%d: ENOBUFS\n", __FILE__, __LINE__);
343        return ENOBUFS;
344        }
345        memset(swd, 0, c7108_sesnum * sizeof(struct cipher_7108 *));
346        
347        /* Copy existing sessions */
348        if (c7108_sessions) {
349        memcpy(swd, c7108_sessions,
350               (c7108_sesnum / 2) * sizeof(struct cipher_7108 *));
351        kfree(c7108_sessions);
352        }
353        
354        c7108_sessions = swd;
355
356    }
357    
358    swd = &c7108_sessions[i];
359    *sid = i;
360
361    while (cri) {
362        *swd = (struct cipher_7108 *)
363            kmalloc(sizeof(struct cipher_7108), GFP_ATOMIC);
364        if (*swd == NULL) {
365            c7108_freesession(NULL, i);
366            dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
367            return ENOBUFS;
368        }
369        memset(*swd, 0, sizeof(struct cipher_7108));
370
371        algo = NULL;
372        mode = 0;
373        xfm_type = HW_TYPE_CIPHER;
374
375        switch (cri->cri_alg) {
376
377        case CRYPTO_AES_CBC:
378            algo = "aes";
379            mode = CRYPTO_TFM_MODE_CBC;
380            c7108_crypto_mode = C7108_AES_CTRL_MODE_CBC;
381            break;
382#if 0
383        case CRYPTO_AES_CTR:
384            algo = "aes_ctr";
385            mode = CRYPTO_TFM_MODE_CBC;
386            c7108_crypto_mode = C7108_AES_CTRL_MODE_CTR;
387            break;
388        case CRYPTO_AES_ECB:
389            algo = "aes_ecb";
390            mode = CRYPTO_TFM_MODE_CBC;
391            c7108_crypto_mode = C7108_AES_CTRL_MODE_ECB;
392            break;
393        case CRYPTO_AES_OFB:
394            algo = "aes_ofb";
395            mode = CRYPTO_TFM_MODE_CBC;
396            c7108_crypto_mode = C7108_AES_CTRL_MODE_OFB;
397            break;
398        case CRYPTO_AES_CFB:
399            algo = "aes_cfb";
400            mode = CRYPTO_TFM_MODE_CBC;
401            c7108_crypto_mode = C7108_AES_CTRL_MODE_CFB;
402            break;
403#endif
404        default:
405                printk("unsupported crypto algorithm: %d\n",
406                   cri->cri_alg);
407            return -EINVAL;
408            break;
409        }
410
411
412        if (!algo || !*algo) {
413            printk("cypher_7108_crypto: Unknown algo 0x%x\n",
414               cri->cri_alg);
415            c7108_freesession(NULL, i);
416            return EINVAL;
417        }
418        
419        if (xfm_type == HW_TYPE_CIPHER) {
420            if (debug) {
421            dprintk("%s key:", __FUNCTION__);
422            for (i = 0; i < (cri->cri_klen + 7) / 8; i++)
423                dprintk("%s0x%02x", (i % 8) ? " " : "\n ",
424                    cri->cri_key[i]);
425            dprintk("\n");
426            }
427
428        } else if (xfm_type == SW_TYPE_HMAC ||
429               xfm_type == SW_TYPE_HASH) {
430            printk("cypher_7108_crypto: HMAC unsupported!\n");
431            return -EINVAL;
432            c7108_freesession(NULL, i);
433        } else {
434            printk("cypher_7108_crypto: "
435               "Unhandled xfm_type %d\n", xfm_type);
436            c7108_freesession(NULL, i);
437            return EINVAL;
438        }
439        
440        (*swd)->cri_alg = cri->cri_alg;
441        (*swd)->xfm_type = xfm_type;
442        
443        cri = cri->cri_next;
444        swd = &((*swd)->next);
445    }
446    return 0;
447}
448
449/*
450 * Free a session.
451 */
452static int
453c7108_freesession(void *arg, u_int64_t tid)
454{
455    struct cipher_7108 *swd;
456    u_int32_t sid = CRYPTO_SESID2LID(tid);
457
458    dprintk("%s()\n", __FUNCTION__);
459    if (sid > c7108_sesnum || c7108_sessions == NULL ||
460            c7108_sessions[sid] == NULL) {
461        dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
462        return(EINVAL);
463    }
464
465    /* Silently accept and return */
466    if (sid == 0)
467        return(0);
468
469    while ((swd = c7108_sessions[sid]) != NULL) {
470        c7108_sessions[sid] = swd->next;
471        kfree(swd);
472    }
473    return 0;
474}
475
476/*
477 * Process a hardware request.
478 */
479static int
480c7108_process(void *arg, struct cryptop *crp, int hint)
481{
482    struct cryptodesc *crd;
483    struct cipher_7108 *sw;
484    u_int32_t lid;
485    int type;
486    u32 hwkey[8];
487
488#define SCATTERLIST_MAX 16
489    struct scatterlist sg[SCATTERLIST_MAX];
490    int sg_num, sg_len, skip;
491    struct sk_buff *skb = NULL;
492    struct uio *uiop = NULL;
493
494    dprintk("%s()\n", __FUNCTION__);
495    /* Sanity check */
496    if (crp == NULL) {
497        dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
498        return EINVAL;
499    }
500
501    crp->crp_etype = 0;
502
503    if (crp->crp_desc == NULL || crp->crp_buf == NULL) {
504        dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
505        crp->crp_etype = EINVAL;
506        goto done;
507    }
508
509    lid = crp->crp_sid & 0xffffffff;
510    if (lid >= c7108_sesnum || lid == 0 || c7108_sessions == NULL ||
511            c7108_sessions[lid] == NULL) {
512        crp->crp_etype = ENOENT;
513        dprintk("%s,%d: ENOENT\n", __FILE__, __LINE__);
514        goto done;
515    }
516
517    /*
518     * do some error checking outside of the loop for SKB and IOV
519     * processing this leaves us with valid skb or uiop pointers
520     * for later
521     */
522    if (crp->crp_flags & CRYPTO_F_SKBUF) {
523        skb = (struct sk_buff *) crp->crp_buf;
524        if (skb_shinfo(skb)->nr_frags >= SCATTERLIST_MAX) {
525            printk("%s,%d: %d nr_frags > SCATTERLIST_MAX",
526                   __FILE__, __LINE__,
527                   skb_shinfo(skb)->nr_frags);
528            goto done;
529        }
530    } else if (crp->crp_flags & CRYPTO_F_IOV) {
531        uiop = (struct uio *) crp->crp_buf;
532        if (uiop->uio_iovcnt > SCATTERLIST_MAX) {
533            printk("%s,%d: %d uio_iovcnt > SCATTERLIST_MAX",
534                   __FILE__, __LINE__,
535                   uiop->uio_iovcnt);
536            goto done;
537        }
538    }
539
540    /* Go through crypto descriptors, processing as we go */
541    for (crd = crp->crp_desc; crd; crd = crd->crd_next) {
542        /*
543         * Find the crypto context.
544         *
545         * XXX Note that the logic here prevents us from having
546         * XXX the same algorithm multiple times in a session
547         * XXX (or rather, we can but it won't give us the right
548         * XXX results). To do that, we'd need some way of differentiating
549         * XXX between the various instances of an algorithm (so we can
550         * XXX locate the correct crypto context).
551         */
552        for (sw = c7108_sessions[lid];
553         sw && sw->cri_alg != crd->crd_alg;
554         sw = sw->next)
555        ;
556        
557        /* No such context ? */
558        if (sw == NULL) {
559        crp->crp_etype = EINVAL;
560        dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
561        goto done;
562        }
563        
564        skip = crd->crd_skip;
565        
566        /*
567         * setup the SG list skip from the start of the buffer
568         */
569        memset(sg, 0, sizeof(sg));
570        if (crp->crp_flags & CRYPTO_F_SKBUF) {
571        int i, len;
572        type = CRYPTO_BUF_SKBUF;
573        
574        sg_num = 0;
575        sg_len = 0;
576
577        if (skip < skb_headlen(skb)) {
578            //sg[sg_num].page = virt_to_page(skb->data + skip);
579            //sg[sg_num].offset = offset_in_page(skb->data + skip);
580            len = skb_headlen(skb) - skip;
581            if (len + sg_len > crd->crd_len)
582            len = crd->crd_len - sg_len;
583            //sg[sg_num].length = len;
584            sg_set_page(&sg[sg_num], virt_to_page(skb->data + skip), len, offset_in_page(skb->data + skip));
585            sg_len += sg[sg_num].length;
586            sg_num++;
587            skip = 0;
588        } else
589            skip -= skb_headlen(skb);
590        
591        for (i = 0; sg_len < crd->crd_len &&
592             i < skb_shinfo(skb)->nr_frags &&
593             sg_num < SCATTERLIST_MAX; i++) {
594            if (skip < skb_shinfo(skb)->frags[i].size) {
595            //sg[sg_num].page = skb_frag_page(&skb_shinfo(skb)->frags[i]);
596            //sg[sg_num].offset = skb_shinfo(skb)->frags[i].page_offset + skip;
597            len = skb_shinfo(skb)->frags[i].size - skip;
598            if (len + sg_len > crd->crd_len)
599                len = crd->crd_len - sg_len;
600            //sg[sg_num].length = len;
601            sg_set_page(&sg[sg_num], skb_frag_page(&skb_shinfo(skb)->frags[i]), len, skb_shinfo(skb)->frags[i].page_offset + skip);
602            sg_len += sg[sg_num].length;
603            sg_num++;
604            skip = 0;
605            } else
606            skip -= skb_shinfo(skb)->frags[i].size;
607        }
608        } else if (crp->crp_flags & CRYPTO_F_IOV) {
609        int len;
610        type = CRYPTO_BUF_IOV;
611        sg_len = 0;
612        for (sg_num = 0; sg_len < crd->crd_len &&
613             sg_num < uiop->uio_iovcnt &&
614             sg_num < SCATTERLIST_MAX; sg_num++) {
615            if (skip < uiop->uio_iov[sg_num].iov_len) {
616            //sg[sg_num].page = virt_to_page(uiop->uio_iov[sg_num].iov_base+skip);
617            //sg[sg_num].offset = offset_in_page(uiop->uio_iov[sg_num].iov_base+skip);
618            len = uiop->uio_iov[sg_num].iov_len - skip;
619            if (len + sg_len > crd->crd_len)
620                len = crd->crd_len - sg_len;
621            //sg[sg_num].length = len;
622            sg_set_page(&sg[sg_num], virt_to_page(uiop->uio_iov[sg_num].iov_base+skip), len, offset_in_page(uiop->uio_iov[sg_num].iov_base+skip));
623            sg_len += sg[sg_num].length;
624            skip = 0;
625            } else
626            skip -= uiop->uio_iov[sg_num].iov_len;
627        }
628        } else {
629        type = CRYPTO_BUF_CONTIG;
630        //sg[0].page = virt_to_page(crp->crp_buf + skip);
631        //sg[0].offset = offset_in_page(crp->crp_buf + skip);
632        sg_len = (crp->crp_ilen - skip);
633        if (sg_len > crd->crd_len)
634            sg_len = crd->crd_len;
635        //sg[0].length = sg_len;
636        sg_set_page(&sg[0], virt_to_page(crp->crp_buf + skip), sg_len, offset_in_page(crp->crp_buf + skip));
637        sg_num = 1;
638        }
639        if (sg_num > 0)
640        sg_mark_end(&sg[sg_num-1]);
641        
642        
643        switch (sw->xfm_type) {
644
645        case HW_TYPE_CIPHER: {
646
647        unsigned char iv[64];
648        unsigned char *ivp = iv;
649        int i;
650        int ivsize = 16; /* fixed for AES */
651        int blocksize = 16; /* fixed for AES */
652
653        if (sg_len < blocksize) {
654            crp->crp_etype = EINVAL;
655            dprintk("%s,%d: EINVAL len %d < %d\n",
656                __FILE__, __LINE__,
657                sg_len,
658                blocksize);
659            goto done;
660        }
661        
662        if (ivsize > sizeof(iv)) {
663            crp->crp_etype = EINVAL;
664            dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
665            goto done;
666        }
667        
668        if (crd->crd_flags & CRD_F_ENCRYPT) { /* encrypt */
669            
670            if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
671            ivp = crd->crd_iv;
672            } else {
673            get_random_bytes(ivp, ivsize);
674            }
675            /*
676             * do we have to copy the IV back to the buffer ?
677             */
678            if ((crd->crd_flags & CRD_F_IV_PRESENT) == 0) {
679                crypto_copyback(crp->crp_buf,
680                      crd->crd_inject,
681                      ivsize,
682                      (caddr_t)ivp);
683            }
684
685            c7108_xlate_key(crd->crd_klen,
686                    (u8*)crd->crd_key, (u32*)hwkey);
687
688            /* Encrypt SG list */
689            for (i = 0; i < sg_num; i++) {
690            sg[i].dma_address =
691                dma_map_single(NULL,
692                       kmap(sg_page(&sg[i])) + sg[i].offset, sg_len, DMA_BIDIRECTIONAL);
693#if 0
694            printk("sg[%d]:0x%08x, off 0x%08x "
695                   "kmap 0x%08x phys 0x%08x\n",
696                   i, sg[i].page, sg[i].offset,
697                   kmap(sg[i].page) + sg[i].offset,
698                   sg[i].dma_address);
699#endif
700            c7108_aes_cipher(C7108_AES_ENCRYPT,
701                     sg[i].dma_address,
702                     sg[i].dma_address,
703                     sg_len,
704                     crd->crd_klen,
705                     c7108_crypto_mode,
706                     hwkey,
707                     ivp);
708
709            if ((c7108_crypto_mode == C7108_AES_CTRL_MODE_CBC)||
710                (c7108_crypto_mode == C7108_AES_CTRL_MODE_ECB)) {
711                /* Read back expanded key and cache it in key
712                 * context.
713                 * NOTE: for ECB/CBC modes only (not CTR, CFB, OFB)
714                 * where you set the key once.
715                 */
716                c7108_cache_key(crd->crd_klen,
717                        (u32*)hwkey, (u8*)crd->crd_key);
718#if 0
719                printk("%s expanded key:", __FUNCTION__);
720                for (i = 0; i < (crd->crd_klen + 7) / 8; i++)
721                printk("%s0x%02x", (i % 8) ? " " : "\n ",
722                       crd->crd_key[i]);
723                printk("\n");
724#endif
725            }
726            }
727        }
728        else { /*decrypt */
729
730            if (crd->crd_flags & CRD_F_IV_EXPLICIT) {
731            ivp = crd->crd_iv;
732            } else {
733            crypto_copydata(crp->crp_buf, crd->crd_inject,
734                  ivsize, (caddr_t)ivp);
735            }
736
737            c7108_xlate_key(crd->crd_klen,
738                    (u8*)crd->crd_key, (u32*)hwkey);
739
740            /* Decrypt SG list */
741            for (i = 0; i < sg_num; i++) {
742            sg[i].dma_address =
743                dma_map_single(NULL,
744                       kmap(sg_page(&sg[i])) + sg[i].offset,
745                       sg_len, DMA_BIDIRECTIONAL);
746
747#if 0
748            printk("sg[%d]:0x%08x, off 0x%08x "
749                   "kmap 0x%08x phys 0x%08x\n",
750                   i, sg[i].page, sg[i].offset,
751                   kmap(sg[i].page) + sg[i].offset,
752                   sg[i].dma_address);
753#endif
754            c7108_aes_cipher(C7108_AES_DECRYPT,
755                     sg[i].dma_address,
756                     sg[i].dma_address,
757                     sg_len,
758                     crd->crd_klen,
759                     c7108_crypto_mode,
760                     hwkey,
761                     ivp);
762            }
763        }
764        } break;
765        case SW_TYPE_HMAC:
766        case SW_TYPE_HASH:
767        crp->crp_etype = EINVAL;
768        goto done;
769        break;
770        
771        case SW_TYPE_COMP:
772        crp->crp_etype = EINVAL;
773        goto done;
774        break;
775        
776        default:
777        /* Unknown/unsupported algorithm */
778        dprintk("%s,%d: EINVAL\n", __FILE__, __LINE__);
779        crp->crp_etype = EINVAL;
780        goto done;
781        }
782    }
783    
784done:
785    crypto_done(crp);
786    return 0;
787}
788
789static struct {
790    softc_device_decl sc_dev;
791} a7108dev;
792
793static device_method_t a7108_methods = {
794/* crypto device methods */
795    DEVMETHOD(cryptodev_newsession, c7108_newsession),
796    DEVMETHOD(cryptodev_freesession, c7108_freesession),
797    DEVMETHOD(cryptodev_process, c7108_process),
798    DEVMETHOD(cryptodev_kprocess, NULL)
799};
800
801static int
802cypher_7108_crypto_init(void)
803{
804    dprintk("%s(%p)\n", __FUNCTION__, cypher_7108_crypto_init);
805    
806    iobar = (unsigned long)ioremap(CCU_AES_REG_BASE, 0x4000);
807    printk("7108: AES @ 0x%08x (0x%08x phys) %s mode\n",
808           iobar, CCU_AES_REG_BASE,
809           c7108_crypto_mode & C7108_AES_CTRL_MODE_CBC ? "CBC" :
810           c7108_crypto_mode & C7108_AES_CTRL_MODE_ECB ? "ECB" :
811           c7108_crypto_mode & C7108_AES_CTRL_MODE_CTR ? "CTR" :
812           c7108_crypto_mode & C7108_AES_CTRL_MODE_CFB ? "CFB" :
813           c7108_crypto_mode & C7108_AES_CTRL_MODE_OFB ? "OFB" : "???");
814    csr_mutex = SPIN_LOCK_UNLOCKED;
815
816    memset(&a7108dev, 0, sizeof(a7108dev));
817    softc_device_init(&a7108dev, "aes7108", 0, a7108_methods);
818
819           c7108_id = crypto_get_driverid(softc_get_device(&a7108dev), CRYPTOCAP_F_HARDWARE);
820    if (c7108_id < 0)
821        panic("7108: crypto device cannot initialize!");
822
823// crypto_register(c7108_id, CRYPTO_AES_CBC, 0, 0, c7108_newsession, c7108_freesession, c7108_process, NULL);
824    crypto_register(c7108_id, CRYPTO_AES_CBC, 0, 0);
825
826    return(0);
827}
828
829static void
830cypher_7108_crypto_exit(void)
831{
832    dprintk("%s()\n", __FUNCTION__);
833    crypto_unregister_all(c7108_id);
834    c7108_id = -1;
835}
836
837module_init(cypher_7108_crypto_init);
838module_exit(cypher_7108_crypto_exit);
839
840MODULE_LICENSE("Dual BSD/GPL");
841MODULE_DESCRIPTION("Cypher 7108 Crypto (OCF module for kernel crypto)");
842

Archive Download this file



interactive