1 // SPDX-License-Identifier: Apache-2.0
2 /*
3 * Copyright (c) 2017-2020, Linaro Limited
4 *
5 * NIST SP800-38D compliant GCM implementation
6 *
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 #include <crypto/crypto.h>
23 #include <crypto/internal_aes-gcm.h>
24 #include <io.h>
25 #include <string.h>
26 #include <tee_api_types.h>
27 #include <types_ext.h>
28
29 /*
30 * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
31 *
32 * See also:
33 * [MGV] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/
34 gcm-revised-spec.pdf
35 *
36 * We use the algorithm described as Shoup's method with 4-bit tables in
37 * [MGV] 4.1, pp. 12-13, to enhance speed without using too much memory.
38 */
39
40 /*
41 * Precompute small multiples of H, that is set
42 * HH[i] || HL[i] = H times i,
43 * where i is seen as a field element as in [MGV], ie high-order bits
44 * correspond to low powers of P. The result is stored in the same way, that
45 * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL
46 * corresponds to P^127.
47 */
internal_aes_gcm_ghash_gen_tbl(struct internal_ghash_key * ghash_key,const struct internal_aes_gcm_key * ek)48 void internal_aes_gcm_ghash_gen_tbl(struct internal_ghash_key *ghash_key,
49 const struct internal_aes_gcm_key *ek)
50 {
51 int i, j;
52 uint64_t vl, vh;
53 unsigned char h[16];
54
55 memset(h, 0, 16);
56 crypto_aes_enc_block(ek->data, sizeof(ek->data), ek->rounds, h, h);
57
58 vh = get_be64(h);
59 vl = get_be64(h + 8);
60
61 /* 8 = 1000 corresponds to 1 in GF(2^128) */
62 ghash_key->HL[8] = vl;
63 ghash_key->HH[8] = vh;
64
65 /* 0 corresponds to 0 in GF(2^128) */
66 ghash_key->HH[0] = 0;
67 ghash_key->HL[0] = 0;
68
69 for (i = 4; i > 0; i >>= 1) {
70 uint32_t T = (vl & 1) * 0xe1000000U;
71
72 vl = (vh << 63) | (vl >> 1);
73 vh = (vh >> 1) ^ ((uint64_t)T << 32);
74
75 ghash_key->HL[i] = vl;
76 ghash_key->HH[i] = vh;
77 }
78
79 for (i = 2; i <= 8; i *= 2) {
80 uint64_t *HiL = ghash_key->HL + i;
81 uint64_t *HiH = ghash_key->HH + i;
82
83 vh = *HiH;
84 vl = *HiL;
85 for (j = 1; j < i; j++) {
86 HiH[j] = vh ^ ghash_key->HH[j];
87 HiL[j] = vl ^ ghash_key->HL[j];
88 }
89 }
90 }
91
92 /*
93 * Shoup's method for multiplication use this table with
94 * last4[x] = x times P^128
95 * where x and last4[x] are seen as elements of GF(2^128) as in [MGV]
96 */
97 static const uint64_t last4[16] = {
98 0x0000, 0x1c20, 0x3840, 0x2460,
99 0x7080, 0x6ca0, 0x48c0, 0x54e0,
100 0xe100, 0xfd20, 0xd940, 0xc560,
101 0x9180, 0x8da0, 0xa9c0, 0xb5e0
102 };
103
104 /*
105 * Sets output to x times H using the precomputed tables.
106 * x and output are seen as elements of GF(2^128) as in [MGV].
107 */
internal_aes_gcm_ghash_mult_tbl(struct internal_ghash_key * ghash_key,const unsigned char x[16],unsigned char output[16])108 void internal_aes_gcm_ghash_mult_tbl(struct internal_ghash_key *ghash_key,
109 const unsigned char x[16],
110 unsigned char output[16])
111 {
112 int i = 0;
113 unsigned char lo = 0, hi = 0, rem = 0;
114 uint64_t zh = 0, zl = 0;
115
116 lo = x[15] & 0xf;
117
118 zh = ghash_key->HH[lo];
119 zl = ghash_key->HL[lo];
120
121 for (i = 15; i >= 0; i--) {
122 lo = x[i] & 0xf;
123 hi = x[i] >> 4;
124
125 if (i != 15) {
126 rem = (unsigned char)zl & 0xf;
127 zl = (zh << 60) | (zl >> 4);
128 zh = (zh >> 4);
129 zh ^= (uint64_t)last4[rem] << 48;
130 zh ^= ghash_key->HH[lo];
131 zl ^= ghash_key->HL[lo];
132 }
133
134 rem = (unsigned char)zl & 0xf;
135 zl = (zh << 60) | (zl >> 4);
136 zh = (zh >> 4);
137 zh ^= (uint64_t)last4[rem] << 48;
138 zh ^= ghash_key->HH[hi];
139 zl ^= ghash_key->HL[hi];
140 }
141
142 put_be64(output, zh);
143 put_be64(output + 8, zl);
144 }
145