1 /*
2 * Copyright (C) 2012 Vincent Hanquez <tab@snarc.org>
3 *
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 * 3. Neither the name of the author nor the names of his contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31 #include "cryptonite_cpu.h"
32 #include <stdint.h>
33
34 #ifdef ARCH_X86
cpuid(uint32_t info,uint32_t * eax,uint32_t * ebx,uint32_t * ecx,uint32_t * edx)35 static void cpuid(uint32_t info, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
36 {
37 *eax = info;
38 asm volatile
39 (
40 #ifdef __x86_64__
41 "mov %%rbx, %%rdi;"
42 #else
43 "mov %%ebx, %%edi;"
44 #endif
45 "cpuid;"
46 "mov %%ebx, %%esi;"
47 #ifdef __x86_64__
48 "mov %%rdi, %%rbx;"
49 #else
50 "mov %%edi, %%ebx;"
51 #endif
52 :"+a" (*eax), "=S" (*ebx), "=c" (*ecx), "=d" (*edx)
53 : :"edi");
54 }
55
56 #ifdef USE_AESNI
cryptonite_aesni_initialize_hw(void (* init_table)(int,int))57 void cryptonite_aesni_initialize_hw(void (*init_table)(int, int))
58 {
59 static int inited = 0;
60 if (inited == 0) {
61 uint32_t eax, ebx, ecx, edx;
62 int aesni, pclmul;
63
64 inited = 1;
65 cpuid(1, &eax, &ebx, &ecx, &edx);
66 aesni = (ecx & 0x02000000);
67 pclmul = (ecx & 0x00000001);
68 init_table(aesni, pclmul);
69 }
70 }
71 #else
72 #define cryptonite_aesni_initialize_hw(init_table) (0)
73 #endif
74
75 #endif
76