| 1 |
// Copyright (c) 2010 The OpenSSL Project. All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/cmac.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <openssl/aes.h> |
| 11 |
#include <openssl/cipher.h> |
| 12 |
#include <openssl/mem.h> |
| 13 |
|
| 14 |
#include "../../internal.h" |
| 15 |
|
| 16 |
|
| 17 |
struct cmac_ctx_st { |
| 18 |
EVP_CIPHER_CTX cipher_ctx; |
| 19 |
// k1 and k2 are the CMAC subkeys. See |
| 20 |
// https://tools.ietf.org/html/rfc4493#section-2.3 |
| 21 |
uint8_t k1[AES_BLOCK_SIZE]; |
| 22 |
uint8_t k2[AES_BLOCK_SIZE]; |
| 23 |
// Last (possibly partial) scratch |
| 24 |
uint8_t block[AES_BLOCK_SIZE]; |
| 25 |
// block_used contains the number of valid bytes in |block|. |
| 26 |
unsigned block_used; |
| 27 |
}; |
| 28 |
|
| 29 |
static void CMAC_CTX_init(CMAC_CTX *ctx) { |
| 30 |
EVP_CIPHER_CTX_init(&ctx->cipher_ctx); |
| 31 |
} |
| 32 |
|
| 33 |
static void CMAC_CTX_cleanup(CMAC_CTX *ctx) { |
| 34 |
EVP_CIPHER_CTX_cleanup(&ctx->cipher_ctx); |
| 35 |
OPENSSL_cleanse(ctx->k1, sizeof(ctx->k1)); |
| 36 |
OPENSSL_cleanse(ctx->k2, sizeof(ctx->k2)); |
| 37 |
OPENSSL_cleanse(ctx->block, sizeof(ctx->block)); |
| 38 |
} |
| 39 |
|
| 40 |
int AES_CMAC(uint8_t out[16], const uint8_t *key, size_t key_len, |
| 41 |
const uint8_t *in, size_t in_len) { |
| 42 |
const EVP_CIPHER *cipher; |
| 43 |
switch (key_len) { |
| 44 |
case 16: |
| 45 |
cipher = EVP_aes_128_cbc(); |
| 46 |
break; |
| 47 |
case 32: |
| 48 |
cipher = EVP_aes_256_cbc(); |
| 49 |
break; |
| 50 |
default: |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
// We have to verify that all the CMAC services actually succeed before |
| 55 |
// updating the indicator state, so we lock the state here. |
| 56 |
FIPS_service_indicator_lock_state(); |
| 57 |
|
| 58 |
size_t scratch_out_len; |
| 59 |
CMAC_CTX ctx; |
| 60 |
CMAC_CTX_init(&ctx); |
| 61 |
|
| 62 |
const int ok = CMAC_Init(&ctx, key, key_len, cipher, NULL /* engine */) && |
| 63 |
CMAC_Update(&ctx, in, in_len) && |
| 64 |
CMAC_Final(&ctx, out, &scratch_out_len); |
| 65 |
|
| 66 |
FIPS_service_indicator_unlock_state(); |
| 67 |
if(ok) { |
| 68 |
AES_CMAC_verify_service_indicator(&ctx); |
| 69 |
} |
| 70 |
CMAC_CTX_cleanup(&ctx); |
| 71 |
return ok; |
| 72 |
} |
| 73 |
|
| 74 |
CMAC_CTX *CMAC_CTX_new(void) { |
| 75 |
CMAC_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
| 76 |
if (ctx != NULL) { |
| 77 |
// NO-OP: struct already zeroed |
| 78 |
//CMAC_CTX_init(ctx); |
| 79 |
} |
| 80 |
return ctx; |
| 81 |
} |
| 82 |
|
| 83 |
void CMAC_CTX_free(CMAC_CTX *ctx) { |
| 84 |
if (ctx == NULL) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
CMAC_CTX_cleanup(ctx); |
| 89 |
OPENSSL_free(ctx); |
| 90 |
} |
| 91 |
|
| 92 |
int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in) { |
| 93 |
if (!EVP_CIPHER_CTX_copy(&out->cipher_ctx, &in->cipher_ctx)) { |
| 94 |
return 0; |
| 95 |
} |
| 96 |
OPENSSL_memcpy(out->k1, in->k1, AES_BLOCK_SIZE); |
| 97 |
OPENSSL_memcpy(out->k2, in->k2, AES_BLOCK_SIZE); |
| 98 |
OPENSSL_memcpy(out->block, in->block, AES_BLOCK_SIZE); |
| 99 |
out->block_used = in->block_used; |
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
// binary_field_mul_x_128 treats the 128 bits at |in| as an element of GF(2¹²⁸) |
| 104 |
// with a hard-coded reduction polynomial and sets |out| as x times the input. |
| 105 |
// |
| 106 |
// See https://tools.ietf.org/html/rfc4493#section-2.3 |
| 107 |
static void binary_field_mul_x_128(uint8_t out[16], const uint8_t in[16]) { |
| 108 |
unsigned i; |
| 109 |
|
| 110 |
// Shift |in| to left, including carry. |
| 111 |
for (i = 0; i < 15; i++) { |
| 112 |
out[i] = (in[i] << 1) | (in[i+1] >> 7); |
| 113 |
} |
| 114 |
|
| 115 |
// If MSB set fixup with R. |
| 116 |
const uint8_t carry = in[0] >> 7; |
| 117 |
out[i] = (in[i] << 1) ^ ((0 - carry) & 0x87); |
| 118 |
} |
| 119 |
|
| 120 |
// binary_field_mul_x_64 behaves like |binary_field_mul_x_128| but acts on an |
| 121 |
// element of GF(2⁶⁴). |
| 122 |
// |
| 123 |
// See https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf |
| 124 |
static void binary_field_mul_x_64(uint8_t out[8], const uint8_t in[8]) { |
| 125 |
unsigned i; |
| 126 |
|
| 127 |
// Shift |in| to left, including carry. |
| 128 |
for (i = 0; i < 7; i++) { |
| 129 |
out[i] = (in[i] << 1) | (in[i+1] >> 7); |
| 130 |
} |
| 131 |
|
| 132 |
// If MSB set fixup with R. |
| 133 |
const uint8_t carry = in[0] >> 7; |
| 134 |
out[i] = (in[i] << 1) ^ ((0 - carry) & 0x1b); |
| 135 |
} |
| 136 |
|
| 137 |
static const uint8_t kZeroIV[AES_BLOCK_SIZE] = {0}; |
| 138 |
|
| 139 |
int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t key_len, |
| 140 |
const EVP_CIPHER *cipher, ENGINE *engine) { |
| 141 |
// We have to avoid the underlying AES-CBC |EVP_CIPHER| services updating the |
| 142 |
// indicator state, so we lock the state here. |
| 143 |
FIPS_service_indicator_lock_state(); |
| 144 |
int ret = 0; |
| 145 |
uint8_t scratch[AES_BLOCK_SIZE]; |
| 146 |
|
| 147 |
size_t block_size = EVP_CIPHER_block_size(cipher); |
| 148 |
if ((block_size != AES_BLOCK_SIZE && block_size != 8 /* 3-DES */) || |
| 149 |
EVP_CIPHER_key_length(cipher) != key_len || |
| 150 |
!EVP_EncryptInit_ex(&ctx->cipher_ctx, cipher, NULL, key, kZeroIV) || |
| 151 |
!EVP_Cipher(&ctx->cipher_ctx, scratch, kZeroIV, block_size) || |
| 152 |
// Reset context again ready for first data. |
| 153 |
!EVP_EncryptInit_ex(&ctx->cipher_ctx, NULL, NULL, NULL, kZeroIV)) { |
| 154 |
goto end; |
| 155 |
} |
| 156 |
|
| 157 |
if (block_size == AES_BLOCK_SIZE) { |
| 158 |
binary_field_mul_x_128(ctx->k1, scratch); |
| 159 |
binary_field_mul_x_128(ctx->k2, ctx->k1); |
| 160 |
} else { |
| 161 |
binary_field_mul_x_64(ctx->k1, scratch); |
| 162 |
binary_field_mul_x_64(ctx->k2, ctx->k1); |
| 163 |
} |
| 164 |
ctx->block_used = 0; |
| 165 |
ret = 1; |
| 166 |
|
| 167 |
end: |
| 168 |
FIPS_service_indicator_unlock_state(); |
| 169 |
return ret; |
| 170 |
} |
| 171 |
|
| 172 |
int CMAC_Reset(CMAC_CTX *ctx) { |
| 173 |
ctx->block_used = 0; |
| 174 |
return EVP_EncryptInit_ex(&ctx->cipher_ctx, NULL, NULL, NULL, kZeroIV); |
| 175 |
} |
| 176 |
|
| 177 |
int CMAC_Update(CMAC_CTX *ctx, const uint8_t *in, size_t in_len) { |
| 178 |
// We have to avoid the underlying AES-CBC |EVP_Cipher| services updating the |
| 179 |
// indicator state, so we lock the state here. |
| 180 |
int ret = 0; |
| 181 |
FIPS_service_indicator_lock_state(); |
| 182 |
|
| 183 |
size_t block_size = EVP_CIPHER_CTX_block_size(&ctx->cipher_ctx); |
| 184 |
assert(block_size <= AES_BLOCK_SIZE); |
| 185 |
uint8_t scratch[AES_BLOCK_SIZE]; |
| 186 |
|
| 187 |
if (ctx->block_used > 0) { |
| 188 |
size_t todo = block_size - ctx->block_used; |
| 189 |
if (in_len < todo) { |
| 190 |
todo = in_len; |
| 191 |
} |
| 192 |
|
| 193 |
OPENSSL_memcpy(ctx->block + ctx->block_used, in, todo); |
| 194 |
in += todo; |
| 195 |
in_len -= todo; |
| 196 |
ctx->block_used += todo; |
| 197 |
|
| 198 |
// If |in_len| is zero then either |ctx->block_used| is less than |
| 199 |
// |block_size|, in which case we can stop here, or |ctx->block_used| is |
| 200 |
// exactly |block_size| but there's no more data to process. In the latter |
| 201 |
// case we don't want to process this block now because it might be the last |
| 202 |
// block and that block is treated specially. |
| 203 |
if (in_len == 0) { |
| 204 |
ret = 1; |
| 205 |
goto end; |
| 206 |
} |
| 207 |
|
| 208 |
assert(ctx->block_used == block_size); |
| 209 |
|
| 210 |
if (!EVP_Cipher(&ctx->cipher_ctx, scratch, ctx->block, block_size)) { |
| 211 |
goto end; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// Encrypt all but one of the remaining blocks. |
| 216 |
while (in_len > block_size) { |
| 217 |
if (!EVP_Cipher(&ctx->cipher_ctx, scratch, in, block_size)) { |
| 218 |
goto end; |
| 219 |
} |
| 220 |
in += block_size; |
| 221 |
in_len -= block_size; |
| 222 |
} |
| 223 |
|
| 224 |
OPENSSL_memcpy(ctx->block, in, in_len); |
| 225 |
// |in_len| is bounded by |block_size|, which fits in |unsigned|. |
| 226 |
OPENSSL_STATIC_ASSERT(EVP_MAX_BLOCK_LENGTH < UINT_MAX, |
| 227 |
EVP_MAX_BLOCK_LENGTH_is_too_large); |
| 228 |
ctx->block_used = (unsigned)in_len; |
| 229 |
ret = 1; |
| 230 |
|
| 231 |
end: |
| 232 |
FIPS_service_indicator_unlock_state(); |
| 233 |
return ret; |
| 234 |
} |
| 235 |
|
| 236 |
int CMAC_Final(CMAC_CTX *ctx, uint8_t *out, size_t *out_len) { |
| 237 |
// We have to avoid the underlying AES-CBC |EVP_Cipher| services updating the |
| 238 |
// indicator state, so we lock the state here. |
| 239 |
FIPS_service_indicator_lock_state(); |
| 240 |
int ret = 0; |
| 241 |
size_t block_size = EVP_CIPHER_CTX_block_size(&ctx->cipher_ctx); |
| 242 |
assert(block_size <= AES_BLOCK_SIZE); |
| 243 |
|
| 244 |
*out_len = block_size; |
| 245 |
if (out == NULL) { |
| 246 |
ret = 1; |
| 247 |
goto end; |
| 248 |
} |
| 249 |
|
| 250 |
const uint8_t *mask = ctx->k1; |
| 251 |
|
| 252 |
if (ctx->block_used != block_size) { |
| 253 |
// If the last block is incomplete, terminate it with a single 'one' bit |
| 254 |
// followed by zeros. |
| 255 |
ctx->block[ctx->block_used] = 0x80; |
| 256 |
OPENSSL_memset(ctx->block + ctx->block_used + 1, 0, |
| 257 |
block_size - (ctx->block_used + 1)); |
| 258 |
|
| 259 |
mask = ctx->k2; |
| 260 |
} |
| 261 |
|
| 262 |
for (unsigned i = 0; i < block_size; i++) { |
| 263 |
out[i] = ctx->block[i] ^ mask[i]; |
| 264 |
} |
| 265 |
ret = EVP_Cipher(&ctx->cipher_ctx, out, out, block_size); |
| 266 |
|
| 267 |
end: |
| 268 |
FIPS_service_indicator_unlock_state(); |
| 269 |
if(ret) { |
| 270 |
AES_CMAC_verify_service_indicator(ctx); |
| 271 |
} |
| 272 |
return ret; |
| 273 |
} |
| 274 |
|
| 275 |
EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx) { |
| 276 |
return &ctx->cipher_ctx; |
| 277 |
} |