Coverage Report

Created: 2023-02-22 06:14

/src/nettle-with-mini-gmp/cmac.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   AES-CMAC-128 (rfc 4493)
3
   Copyright (C) Stefan Metzmacher 2012
4
   Copyright (C) Jeremy Allison 2012
5
   Copyright (C) Michael Adam 2012
6
   Copyright (C) 2017, Red Hat Inc.
7
8
   This file is part of GNU Nettle.
9
10
   GNU Nettle is free software: you can redistribute it and/or
11
   modify it under the terms of either:
12
13
     * the GNU Lesser General Public License as published by the Free
14
       Software Foundation; either version 3 of the License, or (at your
15
       option) any later version.
16
17
   or
18
19
     * the GNU General Public License as published by the Free
20
       Software Foundation; either version 2 of the License, or (at your
21
       option) any later version.
22
23
   or both in parallel, as here.
24
25
   GNU Nettle is distributed in the hope that it will be useful,
26
   but WITHOUT ANY WARRANTY; without even the implied warranty of
27
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28
   General Public License for more details.
29
30
   You should have received copies of the GNU General Public License and
31
   the GNU Lesser General Public License along with this program.  If
32
   not, see http://www.gnu.org/licenses/.
33
*/
34
35
#if HAVE_CONFIG_H
36
# include "config.h"
37
#endif
38
39
#include <assert.h>
40
#include <stdlib.h>
41
#include <string.h>
42
43
#include "cmac.h"
44
45
#include "memxor.h"
46
#include "nettle-internal.h"
47
#include "block-internal.h"
48
#include "macros.h"
49
50
void
51
cmac128_set_key(struct cmac128_key *key, const void *cipher,
52
    nettle_cipher_func *encrypt)
53
1.02k
{
54
1.02k
  static const union nettle_block16 zero_block;
55
1.02k
  union nettle_block16 L;
56
57
  /* step 1 - generate subkeys k1 and k2 */
58
1.02k
  encrypt(cipher, 16, L.b, zero_block.b);
59
60
1.02k
  block16_mulx_be(&key->K1, &L);
61
1.02k
  block16_mulx_be(&key->K2, &key->K1);
62
1.02k
}
63
64
void
65
cmac128_init(struct cmac128_ctx *ctx)
66
4.43k
{
67
4.43k
  memset(&ctx->X, 0, sizeof(ctx->X));
68
4.43k
  ctx->index = 0;
69
4.43k
}
70
71
6.56k
#define MIN(x,y) ((x)<(y)?(x):(y))
72
73
void
74
cmac128_update(struct cmac128_ctx *ctx, const void *cipher,
75
         nettle_cipher_func *encrypt,
76
         size_t msg_len, const uint8_t *msg)
77
8.30k
{
78
8.30k
  union nettle_block16 Y;
79
  /*
80
   * check if we expand the block
81
   */
82
8.30k
  if (ctx->index < 16)
83
6.56k
    {
84
6.56k
      size_t len = MIN(16 - ctx->index, msg_len);
85
6.56k
      memcpy(&ctx->block.b[ctx->index], msg, len);
86
6.56k
      msg += len;
87
6.56k
      msg_len -= len;
88
6.56k
      ctx->index += len;
89
6.56k
    }
90
91
8.30k
  if (msg_len == 0) {
92
    /* if it is still the last block, we are done */
93
7.45k
    return;
94
7.45k
  }
95
96
  /*
97
   * now checksum everything but the last block
98
   */
99
853
  block16_xor3(&Y, &ctx->X, &ctx->block);
100
853
  encrypt(cipher, 16, ctx->X.b, Y.b);
101
102
2.11k
  while (msg_len > 16)
103
1.26k
    {
104
1.26k
      block16_xor_bytes (&Y, &ctx->X, msg);
105
1.26k
      encrypt(cipher, 16, ctx->X.b, Y.b);
106
1.26k
      msg += 16;
107
1.26k
      msg_len -= 16;
108
1.26k
    }
109
110
  /*
111
   * copy the last block, it will be processed in
112
   * cmac128_digest().
113
   */
114
853
  memcpy(ctx->block.b, msg, msg_len);
115
853
  ctx->index = msg_len;
116
853
}
117
118
void
119
cmac128_digest(struct cmac128_ctx *ctx, const struct cmac128_key *key,
120
         const void *cipher, nettle_cipher_func *encrypt,
121
         unsigned length, uint8_t *dst)
122
3.40k
{
123
3.40k
  union nettle_block16 Y;
124
125
  /* re-use ctx->block for memxor output */
126
3.40k
  if (ctx->index < 16)
127
1.63k
    {
128
1.63k
      ctx->block.b[ctx->index] = 0x80;
129
1.63k
      memset(ctx->block.b + ctx->index + 1, 0, 16 - 1 - ctx->index);
130
131
1.63k
      block16_xor (&ctx->block, &key->K2);
132
1.63k
    }
133
1.77k
  else
134
1.77k
    {
135
1.77k
      block16_xor (&ctx->block, &key->K1);
136
1.77k
    }
137
138
3.40k
  block16_xor3 (&Y, &ctx->block, &ctx->X);
139
140
3.40k
  assert(length <= 16);
141
3.40k
  if (length == 16)
142
3.40k
    {
143
3.40k
      encrypt(cipher, 16, dst, Y.b);
144
3.40k
    }
145
0
  else
146
0
    {
147
0
      encrypt(cipher, 16, ctx->block.b, Y.b);
148
0
      memcpy(dst, ctx->block.b, length);
149
0
    }
150
151
  /* reset state for re-use */
152
3.40k
  cmac128_init(ctx);
153
3.40k
}