| 1 |
/* mac-gmac.c - GMAC glue for MAC API |
| 2 |
* Copyright (C) 2013 Jussi Kivilinna <[email protected]> |
| 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 |
{ |
| 34 |
switch (mac_algo) |
| 35 |
{ |
| 36 |
default: |
| 37 |
return GCRY_CIPHER_NONE; |
| 38 |
case GCRY_MAC_GMAC_AES: |
| 39 |
return GCRY_CIPHER_AES; |
| 40 |
case GCRY_MAC_GMAC_CAMELLIA: |
| 41 |
return GCRY_CIPHER_CAMELLIA128; |
| 42 |
case GCRY_MAC_GMAC_TWOFISH: |
| 43 |
return GCRY_CIPHER_TWOFISH; |
| 44 |
case GCRY_MAC_GMAC_SERPENT: |
| 45 |
return GCRY_CIPHER_SERPENT128; |
| 46 |
case GCRY_MAC_GMAC_SEED: |
| 47 |
return GCRY_CIPHER_SEED; |
| 48 |
case GCRY_MAC_GMAC_SM4: |
| 49 |
return GCRY_CIPHER_SM4; |
| 50 |
case GCRY_MAC_GMAC_ARIA: |
| 51 |
return GCRY_CIPHER_ARIA128; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
static gcry_err_code_t |
| 57 |
gmac_open (gcry_mac_hd_t h) |
| 58 |
{ |
| 59 |
gcry_err_code_t err; |
| 60 |
gcry_cipher_hd_t hd; |
| 61 |
int secure = (h->magic == CTX_MAC_MAGIC_SECURE); |
| 62 |
int cipher_algo; |
| 63 |
unsigned int flags; |
| 64 |
|
| 65 |
cipher_algo = map_mac_algo_to_cipher (h->spec->algo); |
| 66 |
flags = (secure ? GCRY_CIPHER_SECURE : 0); |
| 67 |
|
| 68 |
err = _gcry_cipher_open_internal (&hd, cipher_algo, GCRY_CIPHER_MODE_GCM, |
| 69 |
flags); |
| 70 |
if (err) |
| 71 |
return err; |
| 72 |
|
| 73 |
h->u.gmac.cipher_algo = cipher_algo; |
| 74 |
h->u.gmac.ctx = hd; |
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
static void |
| 80 |
gmac_close (gcry_mac_hd_t h) |
| 81 |
{ |
| 82 |
_gcry_cipher_close (h->u.gmac.ctx); |
| 83 |
h->u.gmac.ctx = NULL; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
static gcry_err_code_t |
| 88 |
gmac_setkey (gcry_mac_hd_t h, const unsigned char *key, size_t keylen) |
| 89 |
{ |
| 90 |
return _gcry_cipher_setkey (h->u.gmac.ctx, key, keylen); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static gcry_err_code_t |
| 95 |
gmac_setiv (gcry_mac_hd_t h, const unsigned char *iv, size_t ivlen) |
| 96 |
{ |
| 97 |
return _gcry_cipher_setiv (h->u.gmac.ctx, iv, ivlen); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
static gcry_err_code_t |
| 102 |
gmac_reset (gcry_mac_hd_t h) |
| 103 |
{ |
| 104 |
return _gcry_cipher_reset (h->u.gmac.ctx); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
static gcry_err_code_t |
| 109 |
gmac_write (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen) |
| 110 |
{ |
| 111 |
return _gcry_cipher_authenticate (h->u.gmac.ctx, buf, buflen); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
static gcry_err_code_t |
| 116 |
gmac_read (gcry_mac_hd_t h, unsigned char *outbuf, size_t * outlen) |
| 117 |
{ |
| 118 |
if (*outlen > GCRY_GCM_BLOCK_LEN) |
| 119 |
*outlen = GCRY_GCM_BLOCK_LEN; |
| 120 |
return _gcry_cipher_gettag (h->u.gmac.ctx, outbuf, *outlen); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
static gcry_err_code_t |
| 125 |
gmac_verify (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen) |
| 126 |
{ |
| 127 |
return _gcry_cipher_checktag (h->u.gmac.ctx, buf, buflen); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
static unsigned int |
| 132 |
gmac_get_maclen (int algo) |
| 133 |
{ |
| 134 |
(void)algo; |
| 135 |
return GCRY_GCM_BLOCK_LEN; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
static unsigned int |
| 140 |
gmac_get_keylen (int algo) |
| 141 |
{ |
| 142 |
return _gcry_cipher_get_algo_keylen (map_mac_algo_to_cipher (algo)); |
| 143 |
} |
| 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 |