| 1 |
/* mac-internal.h - Internal defs for mac.c |
| 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 |
|
| 22 |
#include "g10lib.h" |
| 23 |
#include "cipher-proto.h" |
| 24 |
#include "gost.h" |
| 25 |
|
| 26 |
|
| 27 |
/* The data object used to hold a handle to an encryption object. */ |
| 28 |
struct gcry_mac_handle; |
| 29 |
|
| 30 |
/* The data object used to hold poly1305-mac context. */ |
| 31 |
struct poly1305mac_context_s; |
| 32 |
|
| 33 |
|
| 34 |
/* |
| 35 |
* |
| 36 |
* Message authentication code related definitions. |
| 37 |
* |
| 38 |
*/ |
| 39 |
|
| 40 |
|
| 41 |
/* Magic values for the context structure. */ |
| 42 |
#define CTX_MAC_MAGIC_NORMAL 0x59d9b8af |
| 43 |
#define CTX_MAC_MAGIC_SECURE 0x12c27cd0 |
| 44 |
|
| 45 |
|
| 46 |
/* MAC module functions. */ |
| 47 |
typedef gcry_err_code_t (*gcry_mac_open_func_t)(gcry_mac_hd_t h); |
| 48 |
typedef void (*gcry_mac_close_func_t)(gcry_mac_hd_t h); |
| 49 |
typedef gcry_err_code_t (*gcry_mac_setkey_func_t)(gcry_mac_hd_t h, |
| 50 |
const unsigned char *key, |
| 51 |
size_t keylen); |
| 52 |
typedef gcry_err_code_t (*gcry_mac_setiv_func_t)(gcry_mac_hd_t h, |
| 53 |
const unsigned char *iv, |
| 54 |
size_t ivlen); |
| 55 |
typedef gcry_err_code_t (*gcry_mac_reset_func_t)(gcry_mac_hd_t h); |
| 56 |
typedef gcry_err_code_t (*gcry_mac_write_func_t)(gcry_mac_hd_t h, |
| 57 |
const unsigned char *inbuf, |
| 58 |
size_t inlen); |
| 59 |
typedef gcry_err_code_t (*gcry_mac_read_func_t)(gcry_mac_hd_t h, |
| 60 |
unsigned char *outbuf, |
| 61 |
size_t *outlen); |
| 62 |
typedef gcry_err_code_t (*gcry_mac_verify_func_t)(gcry_mac_hd_t h, |
| 63 |
const unsigned char *inbuf, |
| 64 |
size_t inlen); |
| 65 |
typedef unsigned int (*gcry_mac_get_maclen_func_t)(int algo); |
| 66 |
typedef unsigned int (*gcry_mac_get_keylen_func_t)(int algo); |
| 67 |
|
| 68 |
/* The type used to convey additional information to a MAC. */ |
| 69 |
typedef gpg_err_code_t (*gcry_mac_set_extra_info_t) |
| 70 |
(gcry_mac_hd_t h, int what, const void *buffer, size_t buflen); |
| 71 |
|
| 72 |
typedef struct gcry_mac_spec_ops |
| 73 |
{ |
| 74 |
gcry_mac_open_func_t open; |
| 75 |
gcry_mac_close_func_t close; |
| 76 |
gcry_mac_setkey_func_t setkey; |
| 77 |
gcry_mac_setiv_func_t setiv; |
| 78 |
gcry_mac_reset_func_t reset; |
| 79 |
gcry_mac_write_func_t write; |
| 80 |
gcry_mac_read_func_t read; |
| 81 |
gcry_mac_verify_func_t verify; |
| 82 |
gcry_mac_get_maclen_func_t get_maclen; |
| 83 |
gcry_mac_get_keylen_func_t get_keylen; |
| 84 |
gcry_mac_set_extra_info_t set_extra_info; |
| 85 |
selftest_func_t selftest; |
| 86 |
} gcry_mac_spec_ops_t; |
| 87 |
|
| 88 |
|
| 89 |
/* Module specification structure for message authentication codes. */ |
| 90 |
typedef struct gcry_mac_spec |
| 91 |
{ |
| 92 |
int algo; |
| 93 |
struct { |
| 94 |
unsigned int disabled:1; |
| 95 |
unsigned int fips:1; |
| 96 |
} flags; |
| 97 |
const char *name; |
| 98 |
const gcry_mac_spec_ops_t *ops; |
| 99 |
} gcry_mac_spec_t; |
| 100 |
|
| 101 |
/* The handle structure. */ |
| 102 |
struct gcry_mac_handle |
| 103 |
{ |
| 104 |
int magic; |
| 105 |
int algo; |
| 106 |
const gcry_mac_spec_t *spec; |
| 107 |
gcry_ctx_t gcry_ctx; |
| 108 |
union { |
| 109 |
struct { |
| 110 |
gcry_md_hd_t md_ctx; |
| 111 |
int md_algo; |
| 112 |
} hmac; |
| 113 |
struct { |
| 114 |
gcry_cipher_hd_t ctx; |
| 115 |
int cipher_algo; |
| 116 |
unsigned int blklen; |
| 117 |
} cmac; |
| 118 |
struct { |
| 119 |
gcry_cipher_hd_t ctx; |
| 120 |
int cipher_algo; |
| 121 |
} gmac; |
| 122 |
struct { |
| 123 |
struct poly1305mac_context_s *ctx; |
| 124 |
} poly1305mac; |
| 125 |
struct { |
| 126 |
GOST28147_context ctx; |
| 127 |
u32 n1, n2; |
| 128 |
unsigned int unused; |
| 129 |
unsigned int count; |
| 130 |
unsigned char lastiv[8]; /* IMIT blocksize */ |
| 131 |
} imit; |
| 132 |
} u; |
| 133 |
}; |
| 134 |
|
| 135 |
|
| 136 |
/* |
| 137 |
* The HMAC algorithm specifications (mac-hmac.c). |
| 138 |
*/ |
| 139 |
#if USE_SHA1 |
| 140 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha1; |
| 141 |
#endif |
| 142 |
#if USE_SHA256 |
| 143 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha256; |
| 144 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha224; |
| 145 |
#endif |
| 146 |
#if USE_SHA512 |
| 147 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512; |
| 148 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha384; |
| 149 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512_224; |
| 150 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha512_256; |
| 151 |
#endif |
| 152 |
#if USE_SHA3 |
| 153 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_224; |
| 154 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_256; |
| 155 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_384; |
| 156 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sha3_512; |
| 157 |
#endif |
| 158 |
#if USE_GOST_R_3411_94 |
| 159 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_gost3411_94; |
| 160 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_gost3411_cp; |
| 161 |
#endif |
| 162 |
#if USE_GOST_R_3411_12 |
| 163 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_stribog256; |
| 164 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_stribog512; |
| 165 |
#endif |
| 166 |
#if USE_WHIRLPOOL |
| 167 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_whirlpool; |
| 168 |
#endif |
| 169 |
#if USE_RMD160 |
| 170 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_rmd160; |
| 171 |
#endif |
| 172 |
#if USE_TIGER |
| 173 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_tiger1; |
| 174 |
#endif |
| 175 |
#if USE_MD5 |
| 176 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_md5; |
| 177 |
#endif |
| 178 |
#if USE_MD4 |
| 179 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_md4; |
| 180 |
#endif |
| 181 |
#if USE_BLAKE2 |
| 182 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_512; |
| 183 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_384; |
| 184 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_256; |
| 185 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2b_160; |
| 186 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_256; |
| 187 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_224; |
| 188 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_160; |
| 189 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_blake2s_128; |
| 190 |
#endif |
| 191 |
#if USE_SM3 |
| 192 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_hmac_sm3; |
| 193 |
#endif |
| 194 |
|
| 195 |
/* |
| 196 |
* The CMAC algorithm specifications (mac-cmac.c). |
| 197 |
*/ |
| 198 |
#if USE_BLOWFISH |
| 199 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_blowfish; |
| 200 |
#endif |
| 201 |
#if USE_DES |
| 202 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_tripledes; |
| 203 |
#endif |
| 204 |
#if USE_CAST5 |
| 205 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_cast5; |
| 206 |
#endif |
| 207 |
#if USE_AES |
| 208 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_aes; |
| 209 |
#endif |
| 210 |
#if USE_TWOFISH |
| 211 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_twofish; |
| 212 |
#endif |
| 213 |
#if USE_SERPENT |
| 214 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_serpent; |
| 215 |
#endif |
| 216 |
#if USE_RFC2268 |
| 217 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_rfc2268; |
| 218 |
#endif |
| 219 |
#if USE_SEED |
| 220 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_seed; |
| 221 |
#endif |
| 222 |
#if USE_CAMELLIA |
| 223 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_camellia; |
| 224 |
#endif |
| 225 |
#if USE_IDEA |
| 226 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_idea; |
| 227 |
#endif |
| 228 |
#if USE_GOST28147 |
| 229 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_gost28147; |
| 230 |
#endif |
| 231 |
#if USE_GOST28147 |
| 232 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_gost28147_imit; |
| 233 |
#endif |
| 234 |
#if USE_SM4 |
| 235 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_sm4; |
| 236 |
#endif |
| 237 |
#if USE_ARIA |
| 238 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_cmac_aria; |
| 239 |
#endif |
| 240 |
|
| 241 |
/* |
| 242 |
* The GMAC algorithm specifications (mac-gmac.c). |
| 243 |
*/ |
| 244 |
#if USE_AES |
| 245 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_aes; |
| 246 |
#endif |
| 247 |
#if USE_TWOFISH |
| 248 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_twofish; |
| 249 |
#endif |
| 250 |
#if USE_SERPENT |
| 251 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_serpent; |
| 252 |
#endif |
| 253 |
#if USE_SEED |
| 254 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_seed; |
| 255 |
#endif |
| 256 |
#if USE_CAMELLIA |
| 257 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_camellia; |
| 258 |
#endif |
| 259 |
#if USE_SM4 |
| 260 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_sm4; |
| 261 |
#endif |
| 262 |
#if USE_ARIA |
| 263 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_gmac_aria; |
| 264 |
#endif |
| 265 |
|
| 266 |
/* |
| 267 |
* The Poly1305 MAC algorithm specifications (mac-poly1305.c). |
| 268 |
*/ |
| 269 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac; |
| 270 |
#if USE_AES |
| 271 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_aes; |
| 272 |
#endif |
| 273 |
#if USE_CAMELLIA |
| 274 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_camellia; |
| 275 |
#endif |
| 276 |
#if USE_TWOFISH |
| 277 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_twofish; |
| 278 |
#endif |
| 279 |
#if USE_SERPENT |
| 280 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_serpent; |
| 281 |
#endif |
| 282 |
#if USE_SEED |
| 283 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_seed; |
| 284 |
#endif |
| 285 |
#if USE_SM4 |
| 286 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_sm4; |
| 287 |
#endif |
| 288 |
#if USE_ARIA |
| 289 |
extern const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_aria; |
| 290 |
#endif |