lib/crypto/powerpc/curve25519.h

Source file repositories/reference/linux-study-clean/lib/crypto/powerpc/curve25519.h

File Facts

System
Linux kernel
Corpus path
lib/crypto/powerpc/curve25519.h
Extension
.h
Size
4452 bytes
Lines
187
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: implementation source
Status
source implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright 2024- IBM Corp.
 *
 * X25519 scalar multiplication with 51 bits limbs for PPC64le.
 *   Based on RFC7748 and AArch64 optimized implementation for X25519
 *     - Algorithm 1 Scalar multiplication of a variable point
 */

#include <linux/types.h>
#include <linux/jump_label.h>
#include <linux/kernel.h>

#include <linux/cpufeature.h>
#include <linux/processor.h>

typedef uint64_t fe51[5];

asmlinkage void x25519_fe51_mul(fe51 h, const fe51 f, const fe51 g);
asmlinkage void x25519_fe51_sqr(fe51 h, const fe51 f);
asmlinkage void x25519_fe51_mul121666(fe51 h, fe51 f);
asmlinkage void x25519_fe51_sqr_times(fe51 h, const fe51 f, int n);
asmlinkage void x25519_fe51_frombytes(fe51 h, const uint8_t *s);
asmlinkage void x25519_fe51_tobytes(uint8_t *s, const fe51 h);
asmlinkage void x25519_cswap(fe51 p, fe51 q, unsigned int bit);

#define fmul x25519_fe51_mul
#define fsqr x25519_fe51_sqr
#define fmul121666 x25519_fe51_mul121666
#define fe51_tobytes x25519_fe51_tobytes

static void fadd(fe51 h, const fe51 f, const fe51 g)
{
	h[0] = f[0] + g[0];
	h[1] = f[1] + g[1];
	h[2] = f[2] + g[2];
	h[3] = f[3] + g[3];
	h[4] = f[4] + g[4];
}

/*
 * Prime = 2 ** 255 - 19, 255 bits
 *    (0x7fffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffed)
 *
 * Prime in 5 51-bit limbs
 */
static fe51 prime51 = { 0x7ffffffffffed, 0x7ffffffffffff, 0x7ffffffffffff, 0x7ffffffffffff, 0x7ffffffffffff};

static void fsub(fe51 h, const fe51 f, const fe51 g)
{
	h[0] = (f[0] + ((prime51[0] * 2))) - g[0];
	h[1] = (f[1] + ((prime51[1] * 2))) - g[1];
	h[2] = (f[2] + ((prime51[2] * 2))) - g[2];
	h[3] = (f[3] + ((prime51[3] * 2))) - g[3];
	h[4] = (f[4] + ((prime51[4] * 2))) - g[4];
}

static void fe51_frombytes(fe51 h, const uint8_t *s)
{
	/*
	 * Make sure 64-bit aligned.
	 */
	unsigned char sbuf[32+8];
	unsigned char *sb = PTR_ALIGN((void *)sbuf, 8);

	memcpy(sb, s, 32);
	x25519_fe51_frombytes(h, sb);
}

static void finv(fe51 o, const fe51 i)
{
	fe51 a0, b, c, t00;

	fsqr(a0, i);
	x25519_fe51_sqr_times(t00, a0, 2);

	fmul(b, t00, i);
	fmul(a0, b, a0);

	fsqr(t00, a0);

	fmul(b, t00, b);
	x25519_fe51_sqr_times(t00, b, 5);

	fmul(b, t00, b);
	x25519_fe51_sqr_times(t00, b, 10);

	fmul(c, t00, b);
	x25519_fe51_sqr_times(t00, c, 20);

Annotation

Implementation Notes