/src/libgcrypt/cipher/rijndael-padlock.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Padlock accelerated AES for Libgcrypt |
2 | | * Copyright (C) 2000, 2001, 2002, 2003, 2007, |
3 | | * 2008, 2011, 2012 Free Software Foundation, Inc. |
4 | | * |
5 | | * This file is part of Libgcrypt. |
6 | | * |
7 | | * Libgcrypt is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation; either version 2.1 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * Libgcrypt is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include <config.h> |
22 | | #include <stdio.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> /* for memcmp() */ |
25 | | |
26 | | #include "types.h" /* for byte and u32 typedefs */ |
27 | | #include "g10lib.h" |
28 | | #include "cipher.h" |
29 | | #include "bufhelp.h" |
30 | | #include "rijndael-internal.h" |
31 | | |
32 | | #ifdef USE_PADLOCK |
33 | | |
34 | | /* Encrypt or decrypt one block using the padlock engine. A and B may |
35 | | be the same. */ |
36 | | static unsigned int |
37 | | do_padlock (const RIJNDAEL_context *ctx, unsigned char *bx, |
38 | | const unsigned char *ax, int decrypt_flag) |
39 | 0 | { |
40 | | /* BX and AX are not necessary correctly aligned. Thus we need to |
41 | | copy them here. */ |
42 | 0 | unsigned char a[16] __attribute__ ((aligned (16))); |
43 | 0 | unsigned char b[16] __attribute__ ((aligned (16))); |
44 | 0 | unsigned int cword[4] __attribute__ ((aligned (16))); |
45 | 0 | unsigned char *pa = a; |
46 | 0 | unsigned char *pb = b; |
47 | 0 | int blocks; |
48 | | |
49 | | /* The control word fields are: |
50 | | 127:12 11:10 9 8 7 6 5 4 3:0 |
51 | | RESERVED KSIZE CRYPT INTER KEYGN CIPHR ALIGN DGEST ROUND */ |
52 | 0 | cword[0] = (ctx->rounds & 15); /* (The mask is just a safeguard.) */ |
53 | 0 | cword[1] = 0; |
54 | 0 | cword[2] = 0; |
55 | 0 | cword[3] = 0; |
56 | 0 | if (decrypt_flag) |
57 | 0 | cword[0] |= 0x00000200; |
58 | |
|
59 | 0 | memcpy (a, ax, 16); |
60 | |
|
61 | 0 | blocks = 1; /* Init counter for just one block. */ |
62 | 0 | #ifdef __x86_64__ |
63 | 0 | asm volatile |
64 | 0 | ("pushfq\n\t" /* Force key reload. */ |
65 | 0 | "popfq\n\t" |
66 | 0 | ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XCRYPT ECB. */ |
67 | 0 | : "+S" (pa), "+D" (pb), "+c" (blocks) |
68 | 0 | : "d" (cword), "b" (ctx->padlockkey) |
69 | 0 | : "cc", "memory" |
70 | 0 | ); |
71 | | #else |
72 | | asm volatile |
73 | | ("pushfl\n\t" /* Force key reload. */ |
74 | | "popfl\n\t" |
75 | | "xchg %4, %%ebx\n\t" /* Load key. */ |
76 | | ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XCRYPT ECB. */ |
77 | | "xchg %4, %%ebx\n" /* Restore GOT register. */ |
78 | | : "+S" (pa), "+D" (pb), "+c" (blocks) |
79 | | : "d" (cword), "r" (ctx->padlockkey) |
80 | | : "cc", "memory" |
81 | | ); |
82 | | #endif |
83 | |
|
84 | 0 | memcpy (bx, b, 16); |
85 | |
|
86 | 0 | return (48 + 15 /* possible padding for alignment */); |
87 | 0 | } |
88 | | |
89 | | unsigned int |
90 | | _gcry_aes_padlock_encrypt (const RIJNDAEL_context *ctx, |
91 | | unsigned char *bx, const unsigned char *ax) |
92 | 0 | { |
93 | 0 | return do_padlock(ctx, bx, ax, 0); |
94 | 0 | } |
95 | | |
96 | | unsigned int |
97 | | _gcry_aes_padlock_decrypt (const RIJNDAEL_context *ctx, |
98 | | unsigned char *bx, const unsigned char *ax) |
99 | 0 | { |
100 | 0 | return do_padlock(ctx, bx, ax, 1); |
101 | 0 | } |
102 | | |
103 | | void |
104 | | _gcry_aes_padlock_prepare_decryption (RIJNDAEL_context *ctx) |
105 | 0 | { |
106 | | /* Padlock does not need decryption subkeys. */ |
107 | 0 | (void)ctx; |
108 | 0 | } |
109 | | #endif /* USE_PADLOCK */ |