blob: 661d0d3cdbc96b7889f63b633855d1f3db6202b0 [file] [log] [blame]
Aorimnb0536582016-01-31 12:08:23 +01001/*
2 * GF(2^128) multiplication functions
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21#include <stdint.h>
22
23#include "mbedtls/gf128mul.h"
24
25/* Endianess with 64 bits values */
26#ifndef GET_UINT64_LE
27#define GET_UINT64_LE(n,b,i) \
28{ \
29 (n) = ( (uint64_t) (b)[(i) + 7] << 56 ) \
30 | ( (uint64_t) (b)[(i) + 6] << 48 ) \
31 | ( (uint64_t) (b)[(i) + 5] << 40 ) \
32 | ( (uint64_t) (b)[(i) + 4] << 32 ) \
33 | ( (uint64_t) (b)[(i) + 3] << 24 ) \
34 | ( (uint64_t) (b)[(i) + 2] << 16 ) \
35 | ( (uint64_t) (b)[(i) + 1] << 8 ) \
36 | ( (uint64_t) (b)[(i) ] ); \
37}
38#endif
39
40#ifndef PUT_UINT64_LE
41#define PUT_UINT64_LE(n,b,i) \
42{ \
43 (b)[(i) + 7] = (unsigned char) ( (n) >> 56 ); \
44 (b)[(i) + 6] = (unsigned char) ( (n) >> 48 ); \
45 (b)[(i) + 5] = (unsigned char) ( (n) >> 40 ); \
46 (b)[(i) + 4] = (unsigned char) ( (n) >> 32 ); \
47 (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
48 (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
49 (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
50 (b)[(i) ] = (unsigned char) ( (n) ); \
51}
52#endif
53
Aorimnb0536582016-01-31 12:08:23 +010054/*
55 * This function multiply a field element by x, by x^4 and by x^8
56 * in the polynomial field representation. It uses 64-bit word operations
57 * to gain speed but compensates for machine endianess and hence works
58 * correctly on both styles of machine.
59 */
60void mbedtls_gf128mul_x_ble(mbedtls_be128 r, const mbedtls_be128 x)
61{
62 uint64_t a, b, ra, rb;
63
64 GET_UINT64_LE(a, x, 0);
65 GET_UINT64_LE(b, x, 8);
66
Jaeden Amero97cc3b12018-05-29 19:04:39 +010067 ra = (a << 1) ^ 0x0087 >> ( 8 - ( ( b >> 63 ) << 3 ) );
Aorimnb0536582016-01-31 12:08:23 +010068 rb = (a >> 63) | (b << 1);
69
70 PUT_UINT64_LE(ra, r, 0);
71 PUT_UINT64_LE(rb, r, 8);
72}
73