1 /* 2 * NIST SP800-38D compliant GCM implementation 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 20 #include <crypto/aes-gcm.h> 21 #include <io.h> 22 #include <kernel/panic.h> 23 #include <string.h> 24 #include <tee_api_types.h> 25 #include <types_ext.h> 26 27 #include "aes-gcm-private.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 */ 48 void internal_aes_gcm_ghash_gen_tbl(struct internal_aes_gcm_ctx *ctx) 49 { 50 int i, j; 51 uint64_t vl, vh; 52 unsigned char h[16]; 53 54 memset(h, 0, 16); 55 internal_aes_gcm_encrypt_block(ctx, h, h); 56 57 vh = get_be64(h); 58 vl = get_be64(h + 8); 59 60 /* 8 = 1000 corresponds to 1 in GF(2^128) */ 61 ctx->HL[8] = vl; 62 ctx->HH[8] = vh; 63 64 /* 0 corresponds to 0 in GF(2^128) */ 65 ctx->HH[0] = 0; 66 ctx->HL[0] = 0; 67 68 for (i = 4; i > 0; i >>= 1) { 69 uint32_t T = (vl & 1) * 0xe1000000U; 70 71 vl = (vh << 63) | (vl >> 1); 72 vh = (vh >> 1) ^ ((uint64_t)T << 32); 73 74 ctx->HL[i] = vl; 75 ctx->HH[i] = vh; 76 } 77 78 for (i = 2; i <= 8; i *= 2) { 79 uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i; 80 81 vh = *HiH; 82 vl = *HiL; 83 for (j = 1; j < i; j++) { 84 HiH[j] = vh ^ ctx->HH[j]; 85 HiL[j] = vl ^ ctx->HL[j]; 86 } 87 } 88 89 } 90 91 /* 92 * Shoup's method for multiplication use this table with 93 * last4[x] = x times P^128 94 * where x and last4[x] are seen as elements of GF(2^128) as in [MGV] 95 */ 96 static const uint64_t last4[16] = { 97 0x0000, 0x1c20, 0x3840, 0x2460, 98 0x7080, 0x6ca0, 0x48c0, 0x54e0, 99 0xe100, 0xfd20, 0xd940, 0xc560, 100 0x9180, 0x8da0, 0xa9c0, 0xb5e0 101 }; 102 103 /* 104 * Sets output to x times H using the precomputed tables. 105 * x and output are seen as elements of GF(2^128) as in [MGV]. 106 */ 107 static void gcm_mult(struct internal_aes_gcm_ctx *ctx, 108 const unsigned char x[16], unsigned char output[16]) 109 { 110 int i = 0; 111 unsigned char lo, hi, rem; 112 uint64_t zh, zl; 113 114 lo = x[15] & 0xf; 115 116 zh = ctx->HH[lo]; 117 zl = ctx->HL[lo]; 118 119 for (i = 15; i >= 0; i--) { 120 lo = x[i] & 0xf; 121 hi = x[i] >> 4; 122 123 if (i != 15) { 124 rem = (unsigned char)zl & 0xf; 125 zl = (zh << 60) | (zl >> 4); 126 zh = (zh >> 4); 127 zh ^= (uint64_t)last4[rem] << 48; 128 zh ^= ctx->HH[lo]; 129 zl ^= ctx->HL[lo]; 130 } 131 132 rem = (unsigned char)zl & 0xf; 133 zl = (zh << 60) | (zl >> 4); 134 zh = (zh >> 4); 135 zh ^= (uint64_t)last4[rem] << 48; 136 zh ^= ctx->HH[hi]; 137 zl ^= ctx->HL[hi]; 138 } 139 140 put_be64(output, zh); 141 put_be64(output + 8, zl); 142 } 143 144 void internal_aes_gcm_ghash_update_block(struct internal_aes_gcm_ctx *ctx, 145 const void *data) 146 { 147 void *y = ctx->hash_state; 148 149 internal_aes_gcm_xor_block(y, data); 150 gcm_mult(ctx, y, y); 151 } 152