blob: 251398f4770a217f25f5b6ab45511a41ad6d9119 [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
54
55/* Jump table for not having ifs */
56static const uint16_t gf128mul_table_bbe[2] = { 0x00, 0x87 };
57
58
59/*
60 * This function multiply a field element by x, by x^4 and by x^8
61 * in the polynomial field representation. It uses 64-bit word operations
62 * to gain speed but compensates for machine endianess and hence works
63 * correctly on both styles of machine.
64 */
65void mbedtls_gf128mul_x_ble(mbedtls_be128 r, const mbedtls_be128 x)
66{
67 uint64_t a, b, ra, rb;
68
69 GET_UINT64_LE(a, x, 0);
70 GET_UINT64_LE(b, x, 8);
71
72 ra = (a << 1) ^ gf128mul_table_bbe[b >> 63];
73 rb = (a >> 63) | (b << 1);
74
75 PUT_UINT64_LE(ra, r, 0);
76 PUT_UINT64_LE(rb, r, 8);
77}
78