]> git.ipfire.org Git - thirdparty/qemu.git/blame - crypto/clmul.c
crypto: Add generic 8-bit carry-less multiply routines
[thirdparty/qemu.git] / crypto / clmul.c
CommitLineData
07f348d7
RH
1/*
2 * Carry-less multiply operations.
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 *
5 * Copyright (C) 2023 Linaro, Ltd.
6 */
7
8#include "qemu/osdep.h"
9#include "crypto/clmul.h"
10
11uint64_t clmul_8x8_low(uint64_t n, uint64_t m)
12{
13 uint64_t r = 0;
14
15 for (int i = 0; i < 8; ++i) {
16 uint64_t mask = (n & 0x0101010101010101ull) * 0xff;
17 r ^= m & mask;
18 m = (m << 1) & 0xfefefefefefefefeull;
19 n >>= 1;
20 }
21 return r;
22}
23
24static uint64_t clmul_8x4_even_int(uint64_t n, uint64_t m)
25{
26 uint64_t r = 0;
27
28 for (int i = 0; i < 8; ++i) {
29 uint64_t mask = (n & 0x0001000100010001ull) * 0xffff;
30 r ^= m & mask;
31 n >>= 1;
32 m <<= 1;
33 }
34 return r;
35}
36
37uint64_t clmul_8x4_even(uint64_t n, uint64_t m)
38{
39 n &= 0x00ff00ff00ff00ffull;
40 m &= 0x00ff00ff00ff00ffull;
41 return clmul_8x4_even_int(n, m);
42}
43
44uint64_t clmul_8x4_odd(uint64_t n, uint64_t m)
45{
46 return clmul_8x4_even(n >> 8, m >> 8);
47}
48
49static uint64_t unpack_8_to_16(uint64_t x)
50{
51 return (x & 0x000000ff)
52 | ((x & 0x0000ff00) << 8)
53 | ((x & 0x00ff0000) << 16)
54 | ((x & 0xff000000) << 24);
55}
56
57uint64_t clmul_8x4_packed(uint32_t n, uint32_t m)
58{
59 return clmul_8x4_even_int(unpack_8_to_16(n), unpack_8_to_16(m));
60}