Coverage Report

Created: 2024-11-21 07:03

/src/libgcrypt/cipher/mac-gmac.c
Line
Count
Source (jump to first uncovered line)
1
/* mac-gmac.c  -  GMAC glue for MAC API
2
 * Copyright (C) 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
3
 *
4
 * This file is part of Libgcrypt.
5
 *
6
 * Libgcrypt is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * Libgcrypt is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
#include <config.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <errno.h>
25
26
#include "g10lib.h"
27
#include "cipher.h"
28
#include "./mac-internal.h"
29
30
31
static int
32
map_mac_algo_to_cipher (int mac_algo)
33
0
{
34
0
  switch (mac_algo)
35
0
    {
36
0
    default:
37
0
      return GCRY_CIPHER_NONE;
38
0
    case GCRY_MAC_GMAC_AES:
39
0
      return GCRY_CIPHER_AES;
40
0
    case GCRY_MAC_GMAC_CAMELLIA:
41
0
      return GCRY_CIPHER_CAMELLIA128;
42
0
    case GCRY_MAC_GMAC_TWOFISH:
43
0
      return GCRY_CIPHER_TWOFISH;
44
0
    case GCRY_MAC_GMAC_SERPENT:
45
0
      return GCRY_CIPHER_SERPENT128;
46
0
    case GCRY_MAC_GMAC_SEED:
47
0
      return GCRY_CIPHER_SEED;
48
0
    case GCRY_MAC_GMAC_SM4:
49
0
      return GCRY_CIPHER_SM4;
50
0
    case GCRY_MAC_GMAC_ARIA:
51
0
      return GCRY_CIPHER_ARIA128;
52
0
    }
53
0
}
54
55
56
static gcry_err_code_t
57
gmac_open (gcry_mac_hd_t h)
58
0
{
59
0
  gcry_err_code_t err;
60
0
  gcry_cipher_hd_t hd;
61
0
  int secure = (h->magic == CTX_MAC_MAGIC_SECURE);
62
0
  int cipher_algo;
63
0
  unsigned int flags;
64
65
0
  cipher_algo = map_mac_algo_to_cipher (h->spec->algo);
66
0
  flags = (secure ? GCRY_CIPHER_SECURE : 0);
67
68
0
  err = _gcry_cipher_open_internal (&hd, cipher_algo, GCRY_CIPHER_MODE_GCM,
69
0
                                    flags);
70
0
  if (err)
71
0
    return err;
72
73
0
  h->u.gmac.cipher_algo = cipher_algo;
74
0
  h->u.gmac.ctx = hd;
75
0
  return 0;
76
0
}
77
78
79
static void
80
gmac_close (gcry_mac_hd_t h)
81
0
{
82
0
  _gcry_cipher_close (h->u.gmac.ctx);
83
0
  h->u.gmac.ctx = NULL;
84
0
}
85
86
87
static gcry_err_code_t
88
gmac_setkey (gcry_mac_hd_t h, const unsigned char *key, size_t keylen)
89
0
{
90
0
  return _gcry_cipher_setkey (h->u.gmac.ctx, key, keylen);
91
0
}
92
93
94
static gcry_err_code_t
95
gmac_setiv (gcry_mac_hd_t h, const unsigned char *iv, size_t ivlen)
96
0
{
97
0
  return _gcry_cipher_setiv (h->u.gmac.ctx, iv, ivlen);
98
0
}
99
100
101
static gcry_err_code_t
102
gmac_reset (gcry_mac_hd_t h)
103
0
{
104
0
  return _gcry_cipher_reset (h->u.gmac.ctx);
105
0
}
106
107
108
static gcry_err_code_t
109
gmac_write (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
110
0
{
111
0
  return _gcry_cipher_authenticate (h->u.gmac.ctx, buf, buflen);
112
0
}
113
114
115
static gcry_err_code_t
116
gmac_read (gcry_mac_hd_t h, unsigned char *outbuf, size_t * outlen)
117
0
{
118
0
  if (*outlen > GCRY_GCM_BLOCK_LEN)
119
0
    *outlen = GCRY_GCM_BLOCK_LEN;
120
0
  return _gcry_cipher_gettag (h->u.gmac.ctx, outbuf, *outlen);
121
0
}
122
123
124
static gcry_err_code_t
125
gmac_verify (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
126
0
{
127
0
  return _gcry_cipher_checktag (h->u.gmac.ctx, buf, buflen);
128
0
}
129
130
131
static unsigned int
132
gmac_get_maclen (int algo)
133
0
{
134
0
  (void)algo;
135
0
  return GCRY_GCM_BLOCK_LEN;
136
0
}
137
138
139
static unsigned int
140
gmac_get_keylen (int algo)
141
0
{
142
0
  return _gcry_cipher_get_algo_keylen (map_mac_algo_to_cipher (algo));
143
0
}
144
145
146
static gcry_mac_spec_ops_t gmac_ops = {
147
  gmac_open,
148
  gmac_close,
149
  gmac_setkey,
150
  gmac_setiv,
151
  gmac_reset,
152
  gmac_write,
153
  gmac_read,
154
  gmac_verify,
155
  gmac_get_maclen,
156
  gmac_get_keylen,
157
  NULL,
158
  NULL
159
};
160
161
162
#if USE_AES
163
const gcry_mac_spec_t _gcry_mac_type_spec_gmac_aes = {
164
  GCRY_MAC_GMAC_AES, {0, 0}, "GMAC_AES",
165
  &gmac_ops
166
};
167
#endif
168
#if USE_TWOFISH
169
const gcry_mac_spec_t _gcry_mac_type_spec_gmac_twofish = {
170
  GCRY_MAC_GMAC_TWOFISH, {0, 0}, "GMAC_TWOFISH",
171
  &gmac_ops
172
};
173
#endif
174
#if USE_SERPENT
175
const gcry_mac_spec_t _gcry_mac_type_spec_gmac_serpent = {
176
  GCRY_MAC_GMAC_SERPENT, {0, 0}, "GMAC_SERPENT",
177
  &gmac_ops
178
};
179
#endif
180
#if USE_SEED
181
const gcry_mac_spec_t _gcry_mac_type_spec_gmac_seed = {
182
  GCRY_MAC_GMAC_SEED, {0, 0}, "GMAC_SEED",
183
  &gmac_ops
184
};
185
#endif
186
#if USE_CAMELLIA
187
const gcry_mac_spec_t _gcry_mac_type_spec_gmac_camellia = {
188
  GCRY_MAC_GMAC_CAMELLIA, {0, 0}, "GMAC_CAMELLIA",
189
  &gmac_ops
190
};
191
#endif
192
#if USE_SM4
193
const gcry_mac_spec_t _gcry_mac_type_spec_gmac_sm4 = {
194
  GCRY_MAC_GMAC_SM4, {0, 0}, "GMAC_SM4",
195
  &gmac_ops
196
};
197
#endif
198
#if USE_ARIA
199
const gcry_mac_spec_t _gcry_mac_type_spec_gmac_aria = {
200
  GCRY_MAC_GMAC_ARIA, {0, 0}, "GMAC_ARIA",
201
  &gmac_ops
202
};
203
#endif