2021-11-04 15:58:51 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2025-04-05 11:26:07 -07:00
|
|
|
* ChaCha stream cipher (s390 optimized)
|
2021-11-04 15:58:51 +01:00
|
|
|
*
|
|
|
|
* Copyright IBM Corp. 2021
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define KMSG_COMPONENT "chacha_s390"
|
|
|
|
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
|
|
|
|
|
2025-04-05 11:26:07 -07:00
|
|
|
#include <crypto/chacha.h>
|
2021-11-04 15:58:51 +01:00
|
|
|
#include <linux/cpufeature.h>
|
2025-06-12 13:47:38 +02:00
|
|
|
#include <linux/export.h>
|
2021-11-04 15:58:51 +01:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/sizes.h>
|
2024-02-03 11:45:02 +01:00
|
|
|
#include <asm/fpu.h>
|
2021-11-04 15:58:51 +01:00
|
|
|
#include "chacha-s390.h"
|
|
|
|
|
2025-05-05 11:18:21 -07:00
|
|
|
void hchacha_block_arch(const struct chacha_state *state,
|
2025-05-05 11:18:24 -07:00
|
|
|
u32 out[HCHACHA_OUT_WORDS], int nrounds)
|
2022-05-08 15:09:44 +02:00
|
|
|
{
|
|
|
|
/* TODO: implement hchacha_block_arch() in assembly */
|
2025-05-05 11:18:24 -07:00
|
|
|
hchacha_block_generic(state, out, nrounds);
|
2022-05-08 15:09:44 +02:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(hchacha_block_arch);
|
|
|
|
|
2025-05-05 11:18:21 -07:00
|
|
|
void chacha_crypt_arch(struct chacha_state *state, u8 *dst, const u8 *src,
|
2022-05-08 15:09:44 +02:00
|
|
|
unsigned int bytes, int nrounds)
|
|
|
|
{
|
|
|
|
/* s390 chacha20 implementation has 20 rounds hard-coded,
|
|
|
|
* it cannot handle a block of data or less, but otherwise
|
|
|
|
* it can handle data of arbitrary size
|
|
|
|
*/
|
2025-04-05 11:26:07 -07:00
|
|
|
if (bytes <= CHACHA_BLOCK_SIZE || nrounds != 20 || !cpu_has_vx()) {
|
2022-05-08 15:09:44 +02:00
|
|
|
chacha_crypt_generic(state, dst, src, bytes, nrounds);
|
2025-04-05 11:26:07 -07:00
|
|
|
} else {
|
|
|
|
DECLARE_KERNEL_FPU_ONSTACK32(vxstate);
|
2022-05-08 15:09:44 +02:00
|
|
|
|
2025-04-05 11:26:07 -07:00
|
|
|
kernel_fpu_begin(&vxstate, KERNEL_VXR);
|
2025-05-05 11:18:21 -07:00
|
|
|
chacha20_vx(dst, src, bytes, &state->x[4], &state->x[12]);
|
2025-04-05 11:26:07 -07:00
|
|
|
kernel_fpu_end(&vxstate, KERNEL_VXR);
|
2021-11-04 15:58:51 +01:00
|
|
|
|
2025-05-05 11:18:21 -07:00
|
|
|
state->x[12] += round_up(bytes, CHACHA_BLOCK_SIZE) /
|
|
|
|
CHACHA_BLOCK_SIZE;
|
2021-11-04 15:58:51 +01:00
|
|
|
}
|
2025-04-05 11:26:07 -07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(chacha_crypt_arch);
|
2021-11-04 15:58:51 +01:00
|
|
|
|
2025-04-05 11:26:02 -07:00
|
|
|
bool chacha_is_arch_optimized(void)
|
|
|
|
{
|
|
|
|
return cpu_has_vx();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(chacha_is_arch_optimized);
|
|
|
|
|
2025-04-05 11:26:07 -07:00
|
|
|
MODULE_DESCRIPTION("ChaCha stream cipher (s390 optimized)");
|
2021-11-04 15:58:51 +01:00
|
|
|
MODULE_LICENSE("GPL v2");
|