/*1* Copyright (c) 2018-2019 iXsystems Inc. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR13* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES14* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.15* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,16* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT17* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,18* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY19* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT20* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF21* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.22*/2324#include <sys/types.h>25#include <sys/systm.h>26#include <sys/param.h>27#include <sys/endian.h>28#include <opencrypto/cbc_mac.h>29#include <opencrypto/xform_auth.h>3031/*32* Given two CCM_CBC_BLOCK_LEN blocks, xor33* them into dst, and then encrypt dst.34*/35static void36xor_and_encrypt(struct aes_cbc_mac_ctx *ctx,37const uint8_t *src, uint8_t *dst)38{39#define NWORDS (CCM_CBC_BLOCK_LEN / sizeof(uint64_t))40uint64_t b1[NWORDS], b2[NWORDS], temp[NWORDS];4142memcpy(b1, src, CCM_CBC_BLOCK_LEN);43memcpy(b2, dst, CCM_CBC_BLOCK_LEN);4445for (size_t count = 0; count < NWORDS; count++)46temp[count] = b1[count] ^ b2[count];47rijndaelEncrypt(ctx->keysched, ctx->rounds, (void *)temp, dst);48#undef NWORDS49}5051void52AES_CBC_MAC_Init(void *vctx)53{54struct aes_cbc_mac_ctx *ctx;5556ctx = vctx;57bzero(ctx, sizeof(*ctx));58}5960void61AES_CBC_MAC_Setkey(void *vctx, const uint8_t *key, u_int klen)62{63struct aes_cbc_mac_ctx *ctx;6465ctx = vctx;66ctx->rounds = rijndaelKeySetupEnc(ctx->keysched, key, klen * 8);67}6869/*70* This is called to set the nonce, aka IV.71*72* Note that the caller is responsible for constructing b0 as well73* as the length and padding around the AAD and passing that data74* to _Update.75*/76void77AES_CBC_MAC_Reinit(void *vctx, const uint8_t *nonce, u_int nonceLen)78{79struct aes_cbc_mac_ctx *ctx = vctx;8081ctx->nonce = nonce;82ctx->nonceLength = nonceLen;8384ctx->blockIndex = 0;8586/* XOR b0 with all 0's on first call to _Update. */87memset(ctx->block, 0, CCM_CBC_BLOCK_LEN);88}8990int91AES_CBC_MAC_Update(void *vctx, const void *vdata, u_int length)92{93struct aes_cbc_mac_ctx *ctx;94const uint8_t *data;95size_t copy_amt;9697ctx = vctx;98data = vdata;99100/*101* _Update can be called with non-aligned update lengths. Use102* the staging block when necessary.103*/104while (length != 0) {105uint8_t *ptr;106107/*108* If there is no partial block and the length is at109* least a full block, encrypt the full block without110* copying to the staging block.111*/112if (ctx->blockIndex == 0 && length >= CCM_CBC_BLOCK_LEN) {113xor_and_encrypt(ctx, data, ctx->block);114length -= CCM_CBC_BLOCK_LEN;115data += CCM_CBC_BLOCK_LEN;116continue;117}118119copy_amt = MIN(sizeof(ctx->staging_block) - ctx->blockIndex,120length);121ptr = ctx->staging_block + ctx->blockIndex;122bcopy(data, ptr, copy_amt);123data += copy_amt;124ctx->blockIndex += copy_amt;125length -= copy_amt;126if (ctx->blockIndex == sizeof(ctx->staging_block)) {127/* We've got a full block */128xor_and_encrypt(ctx, ctx->staging_block, ctx->block);129ctx->blockIndex = 0;130}131}132return (0);133}134135void136AES_CBC_MAC_Final(uint8_t *buf, void *vctx)137{138struct aes_cbc_mac_ctx *ctx;139uint8_t s0[CCM_CBC_BLOCK_LEN];140141ctx = vctx;142143/*144* We first need to check to see if we've got any data145* left over to encrypt.146*/147if (ctx->blockIndex != 0) {148memset(ctx->staging_block + ctx->blockIndex, 0,149CCM_CBC_BLOCK_LEN - ctx->blockIndex);150xor_and_encrypt(ctx, ctx->staging_block, ctx->block);151}152explicit_bzero(ctx->staging_block, sizeof(ctx->staging_block));153154bzero(s0, sizeof(s0));155s0[0] = (15 - ctx->nonceLength) - 1;156bcopy(ctx->nonce, s0 + 1, ctx->nonceLength);157rijndaelEncrypt(ctx->keysched, ctx->rounds, s0, s0);158for (size_t indx = 0; indx < AES_CBC_MAC_HASH_LEN; indx++)159buf[indx] = ctx->block[indx] ^ s0[indx];160explicit_bzero(s0, sizeof(s0));161}162163164