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