cmac64.c 3.54 KB
Newer Older
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
1
/*
Niels Möller's avatar
Niels Möller committed
2
   CMAC-64, NIST SP 800-38B
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
   Copyright (C) Stefan Metzmacher 2012
   Copyright (C) Jeremy Allison 2012
   Copyright (C) Michael Adam 2012
   Copyright (C) 2017, Red Hat Inc.
   Copyright (C) 2019, Dmitry Eremin-Solenikov

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "cmac.h"

#include "nettle-internal.h"
47
#include "block-internal.h"
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
48 49 50 51 52 53 54 55 56 57 58 59
#include "macros.h"

void
cmac64_set_key(struct cmac64_key *key, const void *cipher,
	       nettle_cipher_func *encrypt)
{
  static const union nettle_block8 zero_block;
  union nettle_block8 L;

  /* step 1 - generate subkeys k1 and k2 */
  encrypt(cipher, 8, L.b, zero_block.b);

60 61
  block8_mulx_be(&key->K1, &L);
  block8_mulx_be(&key->K2, &key->K1);
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
}

void
cmac64_init(struct cmac64_ctx *ctx)
{
  memset(&ctx->X, 0, sizeof(ctx->X));
  ctx->index = 0;
}

#define MIN(x,y) ((x)<(y)?(x):(y))

void
cmac64_update(struct cmac64_ctx *ctx, const void *cipher,
	      nettle_cipher_func *encrypt,
	      size_t msg_len, const uint8_t *msg)
{
78
  union nettle_block8 Y;
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  /*
   * check if we expand the block
   */
  if (ctx->index < 8)
    {
      size_t len = MIN(8 - ctx->index, msg_len);
      memcpy(&ctx->block.b[ctx->index], msg, len);
      msg += len;
      msg_len -= len;
      ctx->index += len;
    }

  if (msg_len == 0) {
    /* if it is still the last block, we are done */
    return;
  }

  /*
   * now checksum everything but the last block
   */
99
  block8_xor3(&Y, &ctx->X, &ctx->block);
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
100 101 102 103
  encrypt(cipher, 8, ctx->X.b, Y.b);

  while (msg_len > 8)
    {
104
      block8_xor_bytes(&Y, &ctx->X, msg);
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      encrypt(cipher, 8, ctx->X.b, Y.b);
      msg += 8;
      msg_len -= 8;
    }

  /*
   * copy the last block, it will be processed in
   * cmac64_digest().
   */
  memcpy(ctx->block.b, msg, msg_len);
  ctx->index = msg_len;
}

void
cmac64_digest(struct cmac64_ctx *ctx, const struct cmac64_key *key,
	      const void *cipher, nettle_cipher_func *encrypt,
	      unsigned length, uint8_t *dst)
{
  union nettle_block8 Y;

  memset(ctx->block.b+ctx->index, 0, sizeof(ctx->block.b)-ctx->index);

  /* re-use ctx->block for memxor output */
  if (ctx->index < 8)
    {
      ctx->block.b[ctx->index] = 0x80;
131
      block8_xor(&ctx->block, &key->K2);
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
132 133 134
    }
  else
    {
135
      block8_xor(&ctx->block, &key->K1);
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
136 137
    }

138
  block8_xor3(&Y, &ctx->block, &ctx->X);
Dmitry Baryshkov's avatar
Dmitry Baryshkov committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

  assert(length <= 8);
  if (length == 8)
    {
      encrypt(cipher, 8, dst, Y.b);
    }
  else
    {
      encrypt(cipher, 8, ctx->block.b, Y.b);
      memcpy(dst, ctx->block.b, length);
    }

  /* reset state for re-use */
  memset(&ctx->X, 0, sizeof(ctx->X));
  ctx->index = 0;
}