Debian Codegraph @

cipher/rijndael-padlock.c

raw
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 {
40 /* BX and AX are not necessary correctly aligned. Thus we need to
41 copy them here. */
42 unsigned char a[16] __attribute__ ((aligned (16)));
43 unsigned char b[16] __attribute__ ((aligned (16)));
44 unsigned int cword[4] __attribute__ ((aligned (16)));
45 unsigned char *pa = a;
46 unsigned char *pb = b;
47 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 cword[0] = (ctx->rounds & 15); /* (The mask is just a safeguard.) */
53 cword[1] = 0;
54 cword[2] = 0;
55 cword[3] = 0;
56 if (decrypt_flag)
57 cword[0] |= 0x00000200;
58
59 memcpy (a, ax, 16);
60
61 blocks = 1; /* Init counter for just one block. */
62 #ifdef __x86_64__
63 asm volatile
64 ("pushfq\n\t" /* Force key reload. */
65 "popfq\n\t"
66 ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XCRYPT ECB. */
67 : "+S" (pa), "+D" (pb), "+c" (blocks)
68 : "d" (cword), "b" (ctx->padlockkey)
69 : "cc", "memory"
70 );
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 memcpy (bx, b, 16);
85
86 return (48 + 15 /* possible padding for alignment */);
87 }
88
89 unsigned int
90 _gcry_aes_padlock_encrypt (const RIJNDAEL_context *ctx,
91 unsigned char *bx, const unsigned char *ax)
92 {
93 return do_padlock(ctx, bx, ax, 0);
94 }
95
96 unsigned int
97 _gcry_aes_padlock_decrypt (const RIJNDAEL_context *ctx,
98 unsigned char *bx, const unsigned char *ax)
99 {
100 return do_padlock(ctx, bx, ax, 1);
101 }
102
103 void
104 _gcry_aes_padlock_prepare_decryption (RIJNDAEL_context *ctx)
105 {
106 /* Padlock does not need decryption subkeys. */
107 (void)ctx;
108 }
109 #endif /* USE_PADLOCK */