summaryrefslogtreecommitdiffhomepage
path: root/src/crypto/zinc/curve25519/curve25519-x86_64-glue.h
blob: 4499cd9f2c590cc86ca8b8dfa3c8510d1809015b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 */

#include <zinc/curve25519.h>
#include <crypto/algapi.h>
#include <asm/cpufeature.h>
#include <asm/processor.h>

#include "curve25519-x86_64.h"

static bool curve25519_use_bmi2 __ro_after_init;
static bool curve25519_use_adx __ro_after_init;

void __init curve25519_fpu_init(void)
{
	curve25519_use_bmi2 = boot_cpu_has(X86_FEATURE_BMI2);
	curve25519_use_adx = boot_cpu_has(X86_FEATURE_BMI2) &&
			     boot_cpu_has(X86_FEATURE_ADX);
}

static inline bool curve25519_arch(u8 mypublic[CURVE25519_POINT_SIZE],
				   const u8 secret[CURVE25519_POINT_SIZE],
				   const u8 basepoint[CURVE25519_POINT_SIZE])
{
	if (curve25519_use_adx) {
		curve25519_adx(mypublic, secret, basepoint);
		return true;
	} else if (curve25519_use_bmi2) {
		curve25519_bmi2(mypublic, secret, basepoint);
		return true;
	}
	return false;
}

static inline bool curve25519_base_arch(u8 pub[CURVE25519_POINT_SIZE],
					const u8 secret[CURVE25519_POINT_SIZE])
{
	if (curve25519_use_adx) {
		curve25519_adx_base(pub, secret);
		return true;
	} else if (curve25519_use_bmi2) {
		curve25519_bmi2_base(pub, secret);
		return true;
	}
	return false;
}

#define HAVE_CURVE25519_ARCH_IMPLEMENTATION