Exemplo n.º 1
0
void
umac32_digest (struct umac32_ctx *ctx,
	       size_t length, uint8_t *digest)
{
  uint32_t pad;

  assert (length > 0);
  assert (length <= 4);

  if (ctx->index > 0 || ctx->count == 0)
    {
      /* Zero pad to multiple of 32 */
      uint64_t y;
      unsigned pad = (ctx->index > 0) ? 31 & - ctx->index : 32;
      memset (ctx->block + ctx->index, 0, pad);

      y = _umac_nh (ctx->l1_key, ctx->index + pad, ctx->block)
	+ 8 * ctx->index;
      _umac_l2 (ctx->l2_key, ctx->l2_state, 1, ctx->count++, &y);
    }
  assert (ctx->count > 0);
  if ( !(ctx->nonce_low & _UMAC_NONCE_CACHED))
    {
      aes128_encrypt (&ctx->pdf_key, AES_BLOCK_SIZE,
		      (uint8_t *) ctx->pad_cache, ctx->nonce);
      ctx->nonce_low |= _UMAC_NONCE_CACHED;
    }

  pad = ctx->pad_cache[ctx->nonce_low & 3];

  /* Increment nonce */
  ctx->nonce_low++;
  if ( !(ctx->nonce_low & 3))
    {
      unsigned i = ctx->nonce_length - 1;

      ctx->nonce_low = 0;
      ctx->nonce[i] += 4;

      if (ctx->nonce[i] == 0 && i > 0)
	INCREMENT (i, ctx->nonce);
    }

  _umac_l2_final (ctx->l2_key, ctx->l2_state, 1, ctx->count);
  pad ^= ctx->l3_key2[0] ^ _umac_l3 (ctx->l3_key1, ctx->l2_state);
  memcpy (digest, &pad, length);

  /* Reinitialize */
  ctx->count = ctx->index = 0;
}
Exemplo n.º 2
0
void
umac96_digest (struct umac96_ctx *ctx,
	       size_t length, uint8_t *digest)
{
  uint32_t tag[4];
  unsigned i;

  assert (length > 0);
  assert (length <= 12);

  if (ctx->index > 0 || ctx->count == 0)
    {
      /* Zero pad to multiple of 32 */
      uint64_t y[3];
      unsigned pad = (ctx->index > 0) ? 31 & - ctx->index : 32;
      memset (ctx->block + ctx->index, 0, pad);

      _umac_nh_n (y, 3, ctx->l1_key, ctx->index + pad, ctx->block);
      y[0] += 8 * ctx->index;
      y[1] += 8 * ctx->index;
      y[2] += 8 * ctx->index;
      _umac_l2 (ctx->l2_key, ctx->l2_state, 3, ctx->count++, y);
    }
  assert (ctx->count > 0);

  aes128_encrypt (&ctx->pdf_key, AES_BLOCK_SIZE,
		  (uint8_t *) tag, ctx->nonce);

  INCREMENT (ctx->nonce_length, ctx->nonce);

  _umac_l2_final (ctx->l2_key, ctx->l2_state, 3, ctx->count);
  for (i = 0; i < 3; i++)
    tag[i] ^= ctx->l3_key2[i] ^ _umac_l3 (ctx->l3_key1 + 8*i,
					  ctx->l2_state + 2*i);

  memcpy (digest, tag, length);

  /* Reinitialize */
  ctx->count = ctx->index = 0;
}