| 1 |
/* mac.c - message authentication code dispatcher |
| 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 "mac-internal.h" |
| 28 |
|
| 29 |
|
| 30 |
/* This is the list of the digest implementations included in |
| 31 |
libgcrypt. */ |
| 32 |
static const gcry_mac_spec_t * const mac_list[] = { |
| 33 |
#if USE_SHA1 |
| 34 |
&_gcry_mac_type_spec_hmac_sha1, |
| 35 |
#endif |
| 36 |
#if USE_SHA256 |
| 37 |
&_gcry_mac_type_spec_hmac_sha256, |
| 38 |
&_gcry_mac_type_spec_hmac_sha224, |
| 39 |
#endif |
| 40 |
#if USE_SHA512 |
| 41 |
&_gcry_mac_type_spec_hmac_sha512, |
| 42 |
&_gcry_mac_type_spec_hmac_sha384, |
| 43 |
&_gcry_mac_type_spec_hmac_sha512_256, |
| 44 |
&_gcry_mac_type_spec_hmac_sha512_224, |
| 45 |
#endif |
| 46 |
#if USE_SHA3 |
| 47 |
&_gcry_mac_type_spec_hmac_sha3_224, |
| 48 |
&_gcry_mac_type_spec_hmac_sha3_256, |
| 49 |
&_gcry_mac_type_spec_hmac_sha3_384, |
| 50 |
&_gcry_mac_type_spec_hmac_sha3_512, |
| 51 |
#endif |
| 52 |
#if USE_GOST_R_3411_94 |
| 53 |
&_gcry_mac_type_spec_hmac_gost3411_94, |
| 54 |
&_gcry_mac_type_spec_hmac_gost3411_cp, |
| 55 |
#endif |
| 56 |
#if USE_GOST_R_3411_12 |
| 57 |
&_gcry_mac_type_spec_hmac_stribog256, |
| 58 |
&_gcry_mac_type_spec_hmac_stribog512, |
| 59 |
#endif |
| 60 |
#if USE_WHIRLPOOL |
| 61 |
&_gcry_mac_type_spec_hmac_whirlpool, |
| 62 |
#endif |
| 63 |
#if USE_RMD160 |
| 64 |
&_gcry_mac_type_spec_hmac_rmd160, |
| 65 |
#endif |
| 66 |
#if USE_TIGER |
| 67 |
&_gcry_mac_type_spec_hmac_tiger1, |
| 68 |
#endif |
| 69 |
#if USE_MD5 |
| 70 |
&_gcry_mac_type_spec_hmac_md5, |
| 71 |
#endif |
| 72 |
#if USE_MD4 |
| 73 |
&_gcry_mac_type_spec_hmac_md4, |
| 74 |
#endif |
| 75 |
#if USE_BLAKE2 |
| 76 |
&_gcry_mac_type_spec_hmac_blake2b_512, |
| 77 |
&_gcry_mac_type_spec_hmac_blake2b_384, |
| 78 |
&_gcry_mac_type_spec_hmac_blake2b_256, |
| 79 |
&_gcry_mac_type_spec_hmac_blake2b_160, |
| 80 |
&_gcry_mac_type_spec_hmac_blake2s_256, |
| 81 |
&_gcry_mac_type_spec_hmac_blake2s_224, |
| 82 |
&_gcry_mac_type_spec_hmac_blake2s_160, |
| 83 |
&_gcry_mac_type_spec_hmac_blake2s_128, |
| 84 |
#endif |
| 85 |
#if USE_SM3 |
| 86 |
&_gcry_mac_type_spec_hmac_sm3, |
| 87 |
#endif |
| 88 |
#if USE_BLOWFISH |
| 89 |
&_gcry_mac_type_spec_cmac_blowfish, |
| 90 |
#endif |
| 91 |
#if USE_DES |
| 92 |
&_gcry_mac_type_spec_cmac_tripledes, |
| 93 |
#endif |
| 94 |
#if USE_CAST5 |
| 95 |
&_gcry_mac_type_spec_cmac_cast5, |
| 96 |
#endif |
| 97 |
#if USE_AES |
| 98 |
&_gcry_mac_type_spec_cmac_aes, |
| 99 |
&_gcry_mac_type_spec_gmac_aes, |
| 100 |
&_gcry_mac_type_spec_poly1305mac_aes, |
| 101 |
#endif |
| 102 |
#if USE_TWOFISH |
| 103 |
&_gcry_mac_type_spec_cmac_twofish, |
| 104 |
&_gcry_mac_type_spec_gmac_twofish, |
| 105 |
&_gcry_mac_type_spec_poly1305mac_twofish, |
| 106 |
#endif |
| 107 |
#if USE_SERPENT |
| 108 |
&_gcry_mac_type_spec_cmac_serpent, |
| 109 |
&_gcry_mac_type_spec_gmac_serpent, |
| 110 |
&_gcry_mac_type_spec_poly1305mac_serpent, |
| 111 |
#endif |
| 112 |
#if USE_RFC2268 |
| 113 |
&_gcry_mac_type_spec_cmac_rfc2268, |
| 114 |
#endif |
| 115 |
#if USE_SEED |
| 116 |
&_gcry_mac_type_spec_cmac_seed, |
| 117 |
&_gcry_mac_type_spec_gmac_seed, |
| 118 |
&_gcry_mac_type_spec_poly1305mac_seed, |
| 119 |
#endif |
| 120 |
#if USE_CAMELLIA |
| 121 |
&_gcry_mac_type_spec_cmac_camellia, |
| 122 |
&_gcry_mac_type_spec_gmac_camellia, |
| 123 |
&_gcry_mac_type_spec_poly1305mac_camellia, |
| 124 |
#endif |
| 125 |
#if USE_IDEA |
| 126 |
&_gcry_mac_type_spec_cmac_idea, |
| 127 |
#endif |
| 128 |
#if USE_GOST28147 |
| 129 |
&_gcry_mac_type_spec_cmac_gost28147, |
| 130 |
&_gcry_mac_type_spec_gost28147_imit, |
| 131 |
#endif |
| 132 |
&_gcry_mac_type_spec_poly1305mac, |
| 133 |
#if USE_SM4 |
| 134 |
&_gcry_mac_type_spec_cmac_sm4, |
| 135 |
&_gcry_mac_type_spec_gmac_sm4, |
| 136 |
&_gcry_mac_type_spec_poly1305mac_sm4, |
| 137 |
#endif |
| 138 |
#if USE_ARIA |
| 139 |
&_gcry_mac_type_spec_cmac_aria, |
| 140 |
&_gcry_mac_type_spec_gmac_aria, |
| 141 |
&_gcry_mac_type_spec_poly1305mac_aria, |
| 142 |
#endif |
| 143 |
NULL |
| 144 |
}; |
| 145 |
|
| 146 |
/* HMAC implementations start with index 101 (enum gcry_mac_algos) */ |
| 147 |
static const gcry_mac_spec_t * const mac_list_algo101[] = |
| 148 |
{ |
| 149 |
#if USE_SHA256 |
| 150 |
&_gcry_mac_type_spec_hmac_sha256, |
| 151 |
&_gcry_mac_type_spec_hmac_sha224, |
| 152 |
#else |
| 153 |
NULL, |
| 154 |
NULL, |
| 155 |
#endif |
| 156 |
#if USE_SHA512 |
| 157 |
&_gcry_mac_type_spec_hmac_sha512, |
| 158 |
&_gcry_mac_type_spec_hmac_sha384, |
| 159 |
#else |
| 160 |
NULL, |
| 161 |
NULL, |
| 162 |
#endif |
| 163 |
#if USE_SHA1 |
| 164 |
&_gcry_mac_type_spec_hmac_sha1, |
| 165 |
#else |
| 166 |
NULL, |
| 167 |
#endif |
| 168 |
#if USE_MD5 |
| 169 |
&_gcry_mac_type_spec_hmac_md5, |
| 170 |
#else |
| 171 |
NULL, |
| 172 |
#endif |
| 173 |
#if USE_MD4 |
| 174 |
&_gcry_mac_type_spec_hmac_md4, |
| 175 |
#else |
| 176 |
NULL, |
| 177 |
#endif |
| 178 |
#if USE_RMD160 |
| 179 |
&_gcry_mac_type_spec_hmac_rmd160, |
| 180 |
#else |
| 181 |
NULL, |
| 182 |
#endif |
| 183 |
#if USE_TIGER |
| 184 |
&_gcry_mac_type_spec_hmac_tiger1, |
| 185 |
#else |
| 186 |
NULL, |
| 187 |
#endif |
| 188 |
#if USE_WHIRLPOOL |
| 189 |
&_gcry_mac_type_spec_hmac_whirlpool, |
| 190 |
#else |
| 191 |
NULL, |
| 192 |
#endif |
| 193 |
#if USE_GOST_R_3411_94 |
| 194 |
&_gcry_mac_type_spec_hmac_gost3411_94, |
| 195 |
#else |
| 196 |
NULL, |
| 197 |
#endif |
| 198 |
#if USE_GOST_R_3411_12 |
| 199 |
&_gcry_mac_type_spec_hmac_stribog256, |
| 200 |
&_gcry_mac_type_spec_hmac_stribog512, |
| 201 |
#else |
| 202 |
NULL, |
| 203 |
NULL, |
| 204 |
#endif |
| 205 |
#if USE_MD2 |
| 206 |
&_gcry_mac_type_spec_hmac_md2, |
| 207 |
#else |
| 208 |
NULL, |
| 209 |
#endif |
| 210 |
#if USE_SHA3 |
| 211 |
&_gcry_mac_type_spec_hmac_sha3_224, |
| 212 |
&_gcry_mac_type_spec_hmac_sha3_256, |
| 213 |
&_gcry_mac_type_spec_hmac_sha3_384, |
| 214 |
&_gcry_mac_type_spec_hmac_sha3_512, |
| 215 |
#else |
| 216 |
NULL, |
| 217 |
NULL, |
| 218 |
NULL, |
| 219 |
NULL, |
| 220 |
#endif |
| 221 |
#if USE_GOST_R_3411_94 |
| 222 |
&_gcry_mac_type_spec_hmac_gost3411_cp, |
| 223 |
#else |
| 224 |
NULL, |
| 225 |
#endif |
| 226 |
#if USE_BLAKE2 |
| 227 |
&_gcry_mac_type_spec_hmac_blake2b_512, |
| 228 |
&_gcry_mac_type_spec_hmac_blake2b_384, |
| 229 |
&_gcry_mac_type_spec_hmac_blake2b_256, |
| 230 |
&_gcry_mac_type_spec_hmac_blake2b_160, |
| 231 |
&_gcry_mac_type_spec_hmac_blake2s_256, |
| 232 |
&_gcry_mac_type_spec_hmac_blake2s_224, |
| 233 |
&_gcry_mac_type_spec_hmac_blake2s_160, |
| 234 |
&_gcry_mac_type_spec_hmac_blake2s_128, |
| 235 |
#else |
| 236 |
NULL, |
| 237 |
NULL, |
| 238 |
NULL, |
| 239 |
NULL, |
| 240 |
NULL, |
| 241 |
NULL, |
| 242 |
NULL, |
| 243 |
NULL, |
| 244 |
#endif |
| 245 |
#if USE_SM3 |
| 246 |
&_gcry_mac_type_spec_hmac_sm3, |
| 247 |
#else |
| 248 |
NULL, |
| 249 |
#endif |
| 250 |
#if USE_SHA512 |
| 251 |
&_gcry_mac_type_spec_hmac_sha512_256, |
| 252 |
&_gcry_mac_type_spec_hmac_sha512_224 |
| 253 |
#else |
| 254 |
NULL, |
| 255 |
NULL |
| 256 |
#endif |
| 257 |
}; |
| 258 |
|
| 259 |
/* CMAC implementations start with index 201 (enum gcry_mac_algos) */ |
| 260 |
static const gcry_mac_spec_t * const mac_list_algo201[] = |
| 261 |
{ |
| 262 |
#if USE_AES |
| 263 |
&_gcry_mac_type_spec_cmac_aes, |
| 264 |
#else |
| 265 |
NULL, |
| 266 |
#endif |
| 267 |
#if USE_DES |
| 268 |
&_gcry_mac_type_spec_cmac_tripledes, |
| 269 |
#else |
| 270 |
NULL, |
| 271 |
#endif |
| 272 |
#if USE_CAMELLIA |
| 273 |
&_gcry_mac_type_spec_cmac_camellia, |
| 274 |
#else |
| 275 |
NULL, |
| 276 |
#endif |
| 277 |
#if USE_CAST5 |
| 278 |
&_gcry_mac_type_spec_cmac_cast5, |
| 279 |
#else |
| 280 |
NULL, |
| 281 |
#endif |
| 282 |
#if USE_BLOWFISH |
| 283 |
&_gcry_mac_type_spec_cmac_blowfish, |
| 284 |
#else |
| 285 |
NULL, |
| 286 |
#endif |
| 287 |
#if USE_TWOFISH |
| 288 |
&_gcry_mac_type_spec_cmac_twofish, |
| 289 |
#else |
| 290 |
NULL, |
| 291 |
#endif |
| 292 |
#if USE_SERPENT |
| 293 |
&_gcry_mac_type_spec_cmac_serpent, |
| 294 |
#else |
| 295 |
NULL, |
| 296 |
#endif |
| 297 |
#if USE_SEED |
| 298 |
&_gcry_mac_type_spec_cmac_seed, |
| 299 |
#else |
| 300 |
NULL, |
| 301 |
#endif |
| 302 |
#if USE_RFC2268 |
| 303 |
&_gcry_mac_type_spec_cmac_rfc2268, |
| 304 |
#else |
| 305 |
NULL, |
| 306 |
#endif |
| 307 |
#if USE_IDEA |
| 308 |
&_gcry_mac_type_spec_cmac_idea, |
| 309 |
#else |
| 310 |
NULL, |
| 311 |
#endif |
| 312 |
#if USE_GOST28147 |
| 313 |
&_gcry_mac_type_spec_cmac_gost28147, |
| 314 |
#else |
| 315 |
NULL, |
| 316 |
#endif |
| 317 |
#if USE_SM4 |
| 318 |
&_gcry_mac_type_spec_cmac_sm4, |
| 319 |
#else |
| 320 |
NULL, |
| 321 |
#endif |
| 322 |
#if USE_ARIA |
| 323 |
&_gcry_mac_type_spec_cmac_aria |
| 324 |
#else |
| 325 |
NULL |
| 326 |
#endif |
| 327 |
}; |
| 328 |
|
| 329 |
/* GMAC implementations start with index 401 (enum gcry_mac_algos) */ |
| 330 |
static const gcry_mac_spec_t * const mac_list_algo401[] = |
| 331 |
{ |
| 332 |
#if USE_AES |
| 333 |
&_gcry_mac_type_spec_gmac_aes, |
| 334 |
#else |
| 335 |
NULL, |
| 336 |
#endif |
| 337 |
#if USE_CAMELLIA |
| 338 |
&_gcry_mac_type_spec_gmac_camellia, |
| 339 |
#else |
| 340 |
NULL, |
| 341 |
#endif |
| 342 |
#if USE_TWOFISH |
| 343 |
&_gcry_mac_type_spec_gmac_twofish, |
| 344 |
#else |
| 345 |
NULL, |
| 346 |
#endif |
| 347 |
#if USE_SERPENT |
| 348 |
&_gcry_mac_type_spec_gmac_serpent, |
| 349 |
#else |
| 350 |
NULL, |
| 351 |
#endif |
| 352 |
#if USE_SEED |
| 353 |
&_gcry_mac_type_spec_gmac_seed, |
| 354 |
#else |
| 355 |
NULL, |
| 356 |
#endif |
| 357 |
#if USE_SM4 |
| 358 |
&_gcry_mac_type_spec_gmac_sm4, |
| 359 |
#else |
| 360 |
NULL, |
| 361 |
#endif |
| 362 |
#if USE_ARIA |
| 363 |
&_gcry_mac_type_spec_gmac_aria |
| 364 |
#else |
| 365 |
NULL |
| 366 |
#endif |
| 367 |
}; |
| 368 |
|
| 369 |
/* Poly1305-MAC implementations start with index 501 (enum gcry_mac_algos) */ |
| 370 |
static const gcry_mac_spec_t * const mac_list_algo501[] = |
| 371 |
{ |
| 372 |
&_gcry_mac_type_spec_poly1305mac, |
| 373 |
#if USE_AES |
| 374 |
&_gcry_mac_type_spec_poly1305mac_aes, |
| 375 |
#else |
| 376 |
NULL, |
| 377 |
#endif |
| 378 |
#if USE_CAMELLIA |
| 379 |
&_gcry_mac_type_spec_poly1305mac_camellia, |
| 380 |
#else |
| 381 |
NULL, |
| 382 |
#endif |
| 383 |
#if USE_TWOFISH |
| 384 |
&_gcry_mac_type_spec_poly1305mac_twofish, |
| 385 |
#else |
| 386 |
NULL, |
| 387 |
#endif |
| 388 |
#if USE_SERPENT |
| 389 |
&_gcry_mac_type_spec_poly1305mac_serpent, |
| 390 |
#else |
| 391 |
NULL, |
| 392 |
#endif |
| 393 |
#if USE_SEED |
| 394 |
&_gcry_mac_type_spec_poly1305mac_seed, |
| 395 |
#else |
| 396 |
NULL, |
| 397 |
#endif |
| 398 |
#if USE_SM4 |
| 399 |
&_gcry_mac_type_spec_poly1305mac_sm4, |
| 400 |
#else |
| 401 |
NULL, |
| 402 |
#endif |
| 403 |
#if USE_ARIA |
| 404 |
&_gcry_mac_type_spec_poly1305mac_aria |
| 405 |
#else |
| 406 |
NULL |
| 407 |
#endif |
| 408 |
}; |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
/* Explicitly initialize this module. */ |
| 414 |
gcry_err_code_t |
| 415 |
_gcry_mac_init (void) |
| 416 |
{ |
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
/* Return the spec structure for the MAC algorithm ALGO. For an |
| 422 |
unknown algorithm NULL is returned. */ |
| 423 |
static const gcry_mac_spec_t * |
| 424 |
spec_from_algo (int algo) |
| 425 |
{ |
| 426 |
const gcry_mac_spec_t *spec = NULL; |
| 427 |
|
| 428 |
if (algo >= 101 && algo < 101 + DIM(mac_list_algo101)) |
| 429 |
spec = mac_list_algo101[algo - 101]; |
| 430 |
else if (algo >= 201 && algo < 201 + DIM(mac_list_algo201)) |
| 431 |
spec = mac_list_algo201[algo - 201]; |
| 432 |
else if (algo >= 401 && algo < 401 + DIM(mac_list_algo401)) |
| 433 |
spec = mac_list_algo401[algo - 401]; |
| 434 |
else if (algo >= 501 && algo < 501 + DIM(mac_list_algo501)) |
| 435 |
spec = mac_list_algo501[algo - 501]; |
| 436 |
#if USE_GOST28147 |
| 437 |
else if (algo == GCRY_MAC_GOST28147_IMIT) |
| 438 |
spec = &_gcry_mac_type_spec_gost28147_imit; |
| 439 |
#endif |
| 440 |
|
| 441 |
if (spec) |
| 442 |
gcry_assert (spec->algo == algo); |
| 443 |
|
| 444 |
return spec; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
/* Lookup a mac's spec by its name. */ |
| 449 |
static const gcry_mac_spec_t * |
| 450 |
spec_from_name (const char *name) |
| 451 |
{ |
| 452 |
const gcry_mac_spec_t *spec; |
| 453 |
int idx; |
| 454 |
|
| 455 |
for (idx = 0; (spec = mac_list[idx]); idx++) |
| 456 |
if (!stricmp (name, spec->name)) |
| 457 |
return spec; |
| 458 |
|
| 459 |
return NULL; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
/**************** |
| 464 |
* Map a string to the mac algo |
| 465 |
*/ |
| 466 |
int |
| 467 |
_gcry_mac_map_name (const char *string) |
| 468 |
{ |
| 469 |
const gcry_mac_spec_t *spec; |
| 470 |
|
| 471 |
if (!string) |
| 472 |
return 0; |
| 473 |
|
| 474 |
/* Not found, search a matching mac name. */ |
| 475 |
spec = spec_from_name (string); |
| 476 |
if (spec) |
| 477 |
return spec->algo; |
| 478 |
|
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
/**************** |
| 484 |
* This function simply returns the name of the algorithm or some constant |
| 485 |
* string when there is no algo. It will never return NULL. |
| 486 |
* Use the macro gcry_mac_test_algo() to check whether the algorithm |
| 487 |
* is valid. |
| 488 |
*/ |
| 489 |
const char * |
| 490 |
_gcry_mac_algo_name (int algorithm) |
| 491 |
{ |
| 492 |
const gcry_mac_spec_t *spec; |
| 493 |
|
| 494 |
spec = spec_from_algo (algorithm); |
| 495 |
return spec ? spec->name : "?"; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
static gcry_err_code_t |
| 500 |
check_mac_algo (int algorithm) |
| 501 |
{ |
| 502 |
const gcry_mac_spec_t *spec; |
| 503 |
|
| 504 |
spec = spec_from_algo (algorithm); |
| 505 |
if (spec && !spec->flags.disabled && (spec->flags.fips || !fips_mode ())) |
| 506 |
return 0; |
| 507 |
|
| 508 |
return GPG_ERR_MAC_ALGO; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
/**************** |
| 513 |
* Open a message digest handle for use with algorithm ALGO. |
| 514 |
*/ |
| 515 |
static gcry_err_code_t |
| 516 |
mac_open (gcry_mac_hd_t * hd, int algo, unsigned int flags, gcry_ctx_t ctx) |
| 517 |
{ |
| 518 |
const gcry_mac_spec_t *spec; |
| 519 |
gcry_err_code_t err; |
| 520 |
gcry_mac_hd_t h; |
| 521 |
int secure = !!(flags & GCRY_MAC_FLAG_SECURE); |
| 522 |
|
| 523 |
spec = spec_from_algo (algo); |
| 524 |
if (!spec) |
| 525 |
return GPG_ERR_MAC_ALGO; |
| 526 |
else if (spec->flags.disabled) |
| 527 |
return GPG_ERR_MAC_ALGO; |
| 528 |
else if (!spec->flags.fips && fips_mode ()) |
| 529 |
{ |
| 530 |
if (fips_check_rejection (GCRY_FIPS_FLAG_REJECT_MAC)) |
| 531 |
return GPG_ERR_MAC_ALGO; |
| 532 |
else |
| 533 |
fips_service_indicator_mark_non_compliant (); |
| 534 |
} |
| 535 |
else if (!spec->ops) |
| 536 |
return GPG_ERR_MAC_ALGO; |
| 537 |
else if (!spec->ops->open || !spec->ops->write || !spec->ops->setkey || |
| 538 |
!spec->ops->read || !spec->ops->verify || !spec->ops->reset) |
| 539 |
return GPG_ERR_MAC_ALGO; |
| 540 |
|
| 541 |
if (secure) |
| 542 |
h = xtrycalloc_secure (1, sizeof (*h)); |
| 543 |
else |
| 544 |
h = xtrycalloc (1, sizeof (*h)); |
| 545 |
|
| 546 |
if (!h) |
| 547 |
return gpg_err_code_from_syserror (); |
| 548 |
|
| 549 |
h->magic = secure ? CTX_MAC_MAGIC_SECURE : CTX_MAC_MAGIC_NORMAL; |
| 550 |
h->spec = spec; |
| 551 |
h->algo = algo; |
| 552 |
h->gcry_ctx = ctx; |
| 553 |
|
| 554 |
err = h->spec->ops->open (h); |
| 555 |
if (err) |
| 556 |
xfree (h); |
| 557 |
else |
| 558 |
*hd = h; |
| 559 |
|
| 560 |
return err; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
static gcry_err_code_t |
| 565 |
mac_reset (gcry_mac_hd_t hd) |
| 566 |
{ |
| 567 |
if (hd->spec->ops->reset) |
| 568 |
return hd->spec->ops->reset (hd); |
| 569 |
|
| 570 |
return 0; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
static void |
| 575 |
mac_close (gcry_mac_hd_t hd) |
| 576 |
{ |
| 577 |
if (hd->spec->ops->close) |
| 578 |
hd->spec->ops->close (hd); |
| 579 |
|
| 580 |
wipememory (hd, sizeof (*hd)); |
| 581 |
|
| 582 |
xfree (hd); |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
static gcry_err_code_t |
| 587 |
mac_setkey (gcry_mac_hd_t hd, const void *key, size_t keylen) |
| 588 |
{ |
| 589 |
if (!hd->spec->ops->setkey) |
| 590 |
return GPG_ERR_INV_ARG; |
| 591 |
if (keylen > 0 && !key) |
| 592 |
return GPG_ERR_INV_ARG; |
| 593 |
|
| 594 |
return hd->spec->ops->setkey (hd, key, keylen); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
static gcry_err_code_t |
| 599 |
mac_setiv (gcry_mac_hd_t hd, const void *iv, size_t ivlen) |
| 600 |
{ |
| 601 |
if (!hd->spec->ops->setiv) |
| 602 |
return GPG_ERR_INV_ARG; |
| 603 |
if (ivlen > 0 && !iv) |
| 604 |
return GPG_ERR_INV_ARG; |
| 605 |
|
| 606 |
return hd->spec->ops->setiv (hd, iv, ivlen); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
static gcry_err_code_t |
| 611 |
mac_write (gcry_mac_hd_t hd, const void *inbuf, size_t inlen) |
| 612 |
{ |
| 613 |
if (!hd->spec->ops->write) |
| 614 |
return GPG_ERR_INV_ARG; |
| 615 |
if (inlen > 0 && !inbuf) |
| 616 |
return GPG_ERR_INV_ARG; |
| 617 |
|
| 618 |
return hd->spec->ops->write (hd, inbuf, inlen); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
static gcry_err_code_t |
| 623 |
mac_read (gcry_mac_hd_t hd, void *outbuf, size_t * outlen) |
| 624 |
{ |
| 625 |
if (!outbuf || !outlen || *outlen == 0 || !hd->spec->ops->read) |
| 626 |
return GPG_ERR_INV_ARG; |
| 627 |
|
| 628 |
return hd->spec->ops->read (hd, outbuf, outlen); |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
static gcry_err_code_t |
| 633 |
mac_verify (gcry_mac_hd_t hd, const void *buf, size_t buflen) |
| 634 |
{ |
| 635 |
if (!buf || buflen == 0 || !hd->spec->ops->verify) |
| 636 |
return GPG_ERR_INV_ARG; |
| 637 |
|
| 638 |
return hd->spec->ops->verify (hd, buf, buflen); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
/* Create a MAC object for algorithm ALGO. FLAGS may be |
| 643 |
given as an bitwise OR of the gcry_mac_flags values. |
| 644 |
H is guaranteed to be a valid handle or NULL on error. */ |
| 645 |
gpg_err_code_t |
| 646 |
_gcry_mac_open (gcry_mac_hd_t * h, int algo, unsigned int flags, |
| 647 |
gcry_ctx_t ctx) |
| 648 |
{ |
| 649 |
gcry_err_code_t rc; |
| 650 |
gcry_mac_hd_t hd = NULL; |
| 651 |
|
| 652 |
if ((flags & ~GCRY_MAC_FLAG_SECURE)) |
| 653 |
rc = GPG_ERR_INV_ARG; |
| 654 |
else |
| 655 |
rc = mac_open (&hd, algo, flags, ctx); |
| 656 |
|
| 657 |
*h = rc ? NULL : hd; |
| 658 |
return rc; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
void |
| 663 |
_gcry_mac_close (gcry_mac_hd_t hd) |
| 664 |
{ |
| 665 |
if (hd) |
| 666 |
mac_close (hd); |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
gcry_err_code_t |
| 671 |
_gcry_mac_setkey (gcry_mac_hd_t hd, const void *key, size_t keylen) |
| 672 |
{ |
| 673 |
return mac_setkey (hd, key, keylen); |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
gcry_err_code_t |
| 678 |
_gcry_mac_setiv (gcry_mac_hd_t hd, const void *iv, size_t ivlen) |
| 679 |
{ |
| 680 |
return mac_setiv (hd, iv, ivlen); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
gcry_err_code_t |
| 685 |
_gcry_mac_write (gcry_mac_hd_t hd, const void *inbuf, size_t inlen) |
| 686 |
{ |
| 687 |
return mac_write (hd, inbuf, inlen); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
gcry_err_code_t |
| 692 |
_gcry_mac_read (gcry_mac_hd_t hd, void *outbuf, size_t * outlen) |
| 693 |
{ |
| 694 |
return mac_read (hd, outbuf, outlen); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
gcry_err_code_t |
| 699 |
_gcry_mac_verify (gcry_mac_hd_t hd, const void *buf, size_t buflen) |
| 700 |
{ |
| 701 |
return mac_verify (hd, buf, buflen); |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
int |
| 706 |
_gcry_mac_get_algo (gcry_mac_hd_t hd) |
| 707 |
{ |
| 708 |
return hd->algo; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
unsigned int |
| 713 |
_gcry_mac_get_algo_maclen (int algo) |
| 714 |
{ |
| 715 |
const gcry_mac_spec_t *spec; |
| 716 |
|
| 717 |
spec = spec_from_algo (algo); |
| 718 |
if (!spec || !spec->ops || !spec->ops->get_maclen) |
| 719 |
return 0; |
| 720 |
|
| 721 |
return spec->ops->get_maclen (algo); |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
unsigned int |
| 726 |
_gcry_mac_get_algo_keylen (int algo) |
| 727 |
{ |
| 728 |
const gcry_mac_spec_t *spec; |
| 729 |
|
| 730 |
spec = spec_from_algo (algo); |
| 731 |
if (!spec || !spec->ops || !spec->ops->get_keylen) |
| 732 |
return 0; |
| 733 |
|
| 734 |
return spec->ops->get_keylen (algo); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
gcry_err_code_t |
| 739 |
_gcry_mac_ctl (gcry_mac_hd_t hd, int cmd, void *buffer, size_t buflen) |
| 740 |
{ |
| 741 |
gcry_err_code_t rc; |
| 742 |
|
| 743 |
/* Currently not used. */ |
| 744 |
(void) hd; |
| 745 |
(void) buffer; |
| 746 |
(void) buflen; |
| 747 |
|
| 748 |
switch (cmd) |
| 749 |
{ |
| 750 |
case GCRYCTL_RESET: |
| 751 |
rc = mac_reset (hd); |
| 752 |
break; |
| 753 |
case GCRYCTL_SET_SBOX: |
| 754 |
if (hd->spec->ops->set_extra_info) |
| 755 |
rc = hd->spec->ops->set_extra_info |
| 756 |
(hd, GCRYCTL_SET_SBOX, buffer, buflen); |
| 757 |
else |
| 758 |
rc = GPG_ERR_NOT_SUPPORTED; |
| 759 |
break; |
| 760 |
default: |
| 761 |
rc = GPG_ERR_INV_OP; |
| 762 |
} |
| 763 |
return rc; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
/* Return information about the given MAC algorithm ALGO. |
| 768 |
|
| 769 |
GCRYCTL_TEST_ALGO: |
| 770 |
Returns 0 if the specified algorithm ALGO is available for use. |
| 771 |
BUFFER and NBYTES must be zero. |
| 772 |
|
| 773 |
Note: Because this function is in most cases used to return an |
| 774 |
integer value, we can make it easier for the caller to just look at |
| 775 |
the return value. The caller will in all cases consult the value |
| 776 |
and thereby detecting whether a error occurred or not (i.e. while |
| 777 |
checking the block size) |
| 778 |
*/ |
| 779 |
gcry_err_code_t |
| 780 |
_gcry_mac_algo_info (int algo, int what, void *buffer, size_t * nbytes) |
| 781 |
{ |
| 782 |
gcry_err_code_t rc = 0; |
| 783 |
unsigned int ui; |
| 784 |
|
| 785 |
switch (what) |
| 786 |
{ |
| 787 |
case GCRYCTL_GET_KEYLEN: |
| 788 |
if (buffer || (!nbytes)) |
| 789 |
rc = GPG_ERR_INV_ARG; |
| 790 |
else |
| 791 |
{ |
| 792 |
ui = _gcry_mac_get_algo_keylen (algo); |
| 793 |
if (ui > 0) |
| 794 |
*nbytes = (size_t) ui; |
| 795 |
else |
| 796 |
/* The only reason for an error is an invalid algo. */ |
| 797 |
rc = GPG_ERR_MAC_ALGO; |
| 798 |
} |
| 799 |
break; |
| 800 |
case GCRYCTL_TEST_ALGO: |
| 801 |
if (buffer || nbytes) |
| 802 |
rc = GPG_ERR_INV_ARG; |
| 803 |
else |
| 804 |
rc = check_mac_algo (algo); |
| 805 |
break; |
| 806 |
|
| 807 |
default: |
| 808 |
rc = GPG_ERR_INV_OP; |
| 809 |
} |
| 810 |
|
| 811 |
return rc; |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
/* Run the self-tests for the MAC. */ |
| 816 |
gpg_error_t |
| 817 |
_gcry_mac_selftest (int algo, int extended, selftest_report_func_t report) |
| 818 |
{ |
| 819 |
gcry_err_code_t ec; |
| 820 |
const gcry_mac_spec_t *spec; |
| 821 |
|
| 822 |
spec = spec_from_algo (algo); |
| 823 |
if (spec && !spec->flags.disabled |
| 824 |
&& (spec->flags.fips || !fips_mode ()) |
| 825 |
&& spec->ops && spec->ops->selftest) |
| 826 |
ec = spec->ops->selftest (algo, extended, report); |
| 827 |
else |
| 828 |
{ |
| 829 |
ec = GPG_ERR_MAC_ALGO; |
| 830 |
if (report) |
| 831 |
report ("mac", algo, "module", |
| 832 |
spec && !spec->flags.disabled |
| 833 |
&& (spec->flags.fips || !fips_mode ())? |
| 834 |
"no selftest available" : |
| 835 |
spec? "algorithm disabled" : |
| 836 |
"algorithm not found"); |
| 837 |
} |
| 838 |
|
| 839 |
return gpg_error (ec); |
| 840 |
} |