Coverage Report

Created: 2024-06-28 06:39

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