1 /* 2 * Copyright (C) 2017 - This file is part of libecc project 3 * 4 * Authors: 5 * Ryad BENADJILA <ryadbenadjila@gmail.com> 6 * Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr> 7 * Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr> 8 * 9 * Contributors: 10 * Nicolas VIVET <nicolas.vivet@ssi.gouv.fr> 11 * Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr> 12 * 13 * This software is licensed under a dual BSD and GPL v2 license. 14 * See LICENSE file at the root folder of the project. 15 */ 16 #include <libecc/hash/hash_algs.h> 17 18 /* 19 * Return the hash mapping entry 'hm' associated with given hash name 20 * 'hash_name'. The function returns 0 on success, -1 on error. 'hm' 21 * is only meaningful on success. 22 */ 23 ATTRIBUTE_WARN_UNUSED_RET int get_hash_by_name(const char *hash_name, const hash_mapping **hm) 24 { 25 const hash_mapping *_hm = NULL; 26 int ret, check; 27 u8 i; 28 29 MUST_HAVE(((hash_name != NULL) && (hm != NULL)), ret, err); 30 31 ret = -1; 32 for (i = 0, _hm = &hash_maps[i]; _hm->type != UNKNOWN_HASH_ALG; 33 _hm = &hash_maps[++i]) { 34 const char *exp_name = (const char *)_hm->name; 35 36 if ((!are_str_equal(hash_name, exp_name, &check)) && check) { 37 (*hm) = _hm; 38 ret = 0; 39 break; 40 } 41 } 42 43 err: 44 return ret; 45 } 46 47 /* 48 * Return the hash mapping entry 'hm' associated with given hash type value. 49 * The function returns 0 on success, -1 on error. 'hm' is not meaningfull 50 * on error. 51 */ 52 ATTRIBUTE_WARN_UNUSED_RET int get_hash_by_type(hash_alg_type hash_type, const hash_mapping **hm) 53 { 54 const hash_mapping *_hm = NULL; 55 int ret; 56 u8 i; 57 58 MUST_HAVE((hm != NULL), ret, err); 59 60 ret = -1; 61 for (i = 0, _hm = &hash_maps[i]; _hm->type != UNKNOWN_HASH_ALG; 62 _hm = &hash_maps[++i]) { 63 if (_hm->type == hash_type) { 64 (*hm) = _hm; 65 ret = 0; 66 break; 67 } 68 } 69 70 err: 71 return ret; 72 } 73 74 /* 75 * Returns respectively in digest_size and block_size param the digest size 76 * and block size for given hash function, if return value of the function is 0. 77 * If return value is -1, then the hash algorithm is not known and output 78 * parameters are not modified. 79 */ 80 ATTRIBUTE_WARN_UNUSED_RET int get_hash_sizes(hash_alg_type hash_type, u8 *digest_size, u8 *block_size) 81 { 82 const hash_mapping *m; 83 int ret; 84 u8 i; 85 86 ret = -1; 87 for (i = 0, m = &hash_maps[i]; m->type != UNKNOWN_HASH_ALG; 88 m = &hash_maps[++i]) { 89 if (m->type == hash_type) { 90 if (digest_size != NULL) { 91 (*digest_size) = m->digest_size; 92 } 93 if (block_size != NULL) { 94 (*block_size) = m->block_size; 95 } 96 ret = 0; 97 break; 98 } 99 } 100 101 return ret; 102 } 103 104 /* 105 * Helper that sanity checks the provided hash mapping against our 106 * constant ones. Returns 0 on success, -1 on error. 107 */ 108 ATTRIBUTE_WARN_UNUSED_RET int hash_mapping_callbacks_sanity_check(const hash_mapping *h) 109 { 110 const hash_mapping *m; 111 int ret = -1, check; 112 u8 i; 113 114 MUST_HAVE((h != NULL), ret, err); 115 116 /* We just check is our mapping is indeed 117 * one of the registered mappings. 118 */ 119 for (i = 0, m = &hash_maps[i]; m->type != UNKNOWN_HASH_ALG; 120 m = &hash_maps[++i]) { 121 if (m->type == h->type) { 122 if ((!are_str_equal_nlen(m->name, h->name, MAX_HASH_ALG_NAME_LEN, &check)) && (!check)){ 123 goto err; 124 } else if (m->digest_size != h->digest_size) { 125 goto err; 126 } else if(m->block_size != h->block_size) { 127 goto err; 128 } else if(m->hfunc_init != h->hfunc_init) { 129 goto err; 130 } else if(m->hfunc_update != h->hfunc_update) { 131 goto err; 132 } else if(m->hfunc_finalize != h->hfunc_finalize) { 133 goto err; 134 } else if(m->hfunc_scattered != h->hfunc_scattered) { 135 goto err; 136 } else{ 137 ret = 0; 138 } 139 } 140 } 141 142 err: 143 return ret; 144 } 145 146 /*****************************************/ 147 /* Trampolines to each specific function to 148 * handle typing of our generic union structure. 149 */ 150 #ifdef WITH_HASH_SHA224 151 ATTRIBUTE_WARN_UNUSED_RET int _sha224_init(hash_context * hctx) 152 { 153 return sha224_init((sha224_context*)hctx); 154 } 155 ATTRIBUTE_WARN_UNUSED_RET int _sha224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 156 { 157 return sha224_update((sha224_context*)hctx, chunk, chunklen); 158 } 159 ATTRIBUTE_WARN_UNUSED_RET int _sha224_final(hash_context * hctx, unsigned char *output) 160 { 161 return sha224_final((sha224_context*)hctx, output); 162 } 163 #endif 164 #ifdef WITH_HASH_SHA256 165 ATTRIBUTE_WARN_UNUSED_RET int _sha256_init(hash_context * hctx) 166 { 167 return sha256_init((sha256_context*)hctx); 168 } 169 ATTRIBUTE_WARN_UNUSED_RET int _sha256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 170 { 171 return sha256_update((sha256_context*)hctx, chunk, chunklen); 172 } 173 ATTRIBUTE_WARN_UNUSED_RET int _sha256_final(hash_context * hctx, unsigned char *output) 174 { 175 return sha256_final((sha256_context*)hctx, output); 176 } 177 #endif 178 #ifdef WITH_HASH_SHA384 179 ATTRIBUTE_WARN_UNUSED_RET int _sha384_init(hash_context * hctx) 180 { 181 return sha384_init((sha384_context*)hctx); 182 } 183 ATTRIBUTE_WARN_UNUSED_RET int _sha384_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 184 { 185 return sha384_update((sha384_context*)hctx, chunk, chunklen); 186 } 187 ATTRIBUTE_WARN_UNUSED_RET int _sha384_final(hash_context * hctx, unsigned char *output) 188 { 189 return sha384_final((sha384_context*)hctx, output); 190 } 191 #endif 192 #ifdef WITH_HASH_SHA512 193 ATTRIBUTE_WARN_UNUSED_RET int _sha512_init(hash_context * hctx) 194 { 195 return sha512_init((sha512_context*)hctx); 196 } 197 ATTRIBUTE_WARN_UNUSED_RET int _sha512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 198 { 199 return sha512_update((sha512_context*)hctx, chunk, chunklen); 200 } 201 ATTRIBUTE_WARN_UNUSED_RET int _sha512_final(hash_context * hctx, unsigned char *output) 202 { 203 return sha512_final((sha512_context*)hctx, output); 204 } 205 #endif 206 #ifdef WITH_HASH_SHA512_224 207 ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_init(hash_context * hctx) 208 { 209 return sha512_224_init((sha512_224_context*)hctx); 210 } 211 ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 212 { 213 return sha512_224_update((sha512_224_context*)hctx, chunk, chunklen); 214 } 215 ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_final(hash_context * hctx, unsigned char *output) 216 { 217 return sha512_224_final((sha512_224_context*)hctx, output); 218 } 219 #endif 220 #ifdef WITH_HASH_SHA512_256 221 ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_init(hash_context * hctx) 222 { 223 return sha512_256_init((sha512_256_context*)hctx); 224 } 225 ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 226 { 227 return sha512_256_update((sha512_256_context*)hctx, chunk, chunklen); 228 } 229 ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_final(hash_context * hctx, unsigned char *output) 230 { 231 return sha512_256_final((sha512_256_context*)hctx, output); 232 } 233 #endif 234 #ifdef WITH_HASH_SHA3_224 235 ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_init(hash_context * hctx) 236 { 237 return sha3_224_init((sha3_224_context*)hctx); 238 } 239 ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 240 { 241 return sha3_224_update((sha3_224_context*)hctx, chunk, chunklen); 242 } 243 ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_final(hash_context * hctx, unsigned char *output) 244 { 245 return sha3_224_final((sha3_224_context*)hctx, output); 246 } 247 #endif 248 #ifdef WITH_HASH_SHA3_256 249 ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_init(hash_context * hctx) 250 { 251 return sha3_256_init((sha3_256_context*)hctx); 252 } 253 ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 254 { 255 return sha3_256_update((sha3_256_context*)hctx, chunk, chunklen); 256 } 257 ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_final(hash_context * hctx, unsigned char *output) 258 { 259 return sha3_256_final((sha3_256_context*)hctx, output); 260 } 261 #endif 262 #ifdef WITH_HASH_SHA3_384 263 ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_init(hash_context * hctx) 264 { 265 return sha3_384_init((sha3_384_context*)hctx); 266 } 267 ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 268 { 269 return sha3_384_update((sha3_384_context*)hctx, chunk, chunklen); 270 } 271 ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_final(hash_context * hctx, unsigned char *output) 272 { 273 return sha3_384_final((sha3_384_context*)hctx, output); 274 } 275 #endif 276 #ifdef WITH_HASH_SHA3_512 277 ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_init(hash_context * hctx) 278 { 279 return sha3_512_init((sha3_512_context*)hctx); 280 } 281 ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 282 { 283 return sha3_512_update((sha3_512_context*)hctx, chunk, chunklen); 284 } 285 ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_final(hash_context * hctx, unsigned char *output) 286 { 287 return sha3_512_final((sha3_512_context*)hctx, output); 288 } 289 #endif 290 #ifdef WITH_HASH_SM3 291 ATTRIBUTE_WARN_UNUSED_RET int _sm3_init(hash_context * hctx) 292 { 293 return sm3_init((sm3_context*)hctx); 294 } 295 ATTRIBUTE_WARN_UNUSED_RET int _sm3_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 296 { 297 return sm3_update((sm3_context*)hctx, chunk, chunklen); 298 } 299 ATTRIBUTE_WARN_UNUSED_RET int _sm3_final(hash_context * hctx, unsigned char *output) 300 { 301 return sm3_final((sm3_context*)hctx, output); 302 } 303 #endif 304 #ifdef WITH_HASH_SHAKE256 305 ATTRIBUTE_WARN_UNUSED_RET int _shake256_init(hash_context * hctx) 306 { 307 return shake256_init((shake256_context*)hctx); 308 } 309 ATTRIBUTE_WARN_UNUSED_RET int _shake256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 310 { 311 return shake256_update((shake256_context*)hctx, chunk, chunklen); 312 } 313 ATTRIBUTE_WARN_UNUSED_RET int _shake256_final(hash_context * hctx, unsigned char *output) 314 { 315 return shake256_final((shake256_context*)hctx, output); 316 } 317 #endif 318 #ifdef WITH_HASH_STREEBOG256 319 ATTRIBUTE_WARN_UNUSED_RET int _streebog256_init(hash_context * hctx) 320 { 321 return streebog256_init((streebog256_context*)hctx); 322 } 323 ATTRIBUTE_WARN_UNUSED_RET int _streebog256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 324 { 325 return streebog256_update((streebog256_context*)hctx, chunk, chunklen); 326 } 327 ATTRIBUTE_WARN_UNUSED_RET int _streebog256_final(hash_context * hctx, unsigned char *output) 328 { 329 return streebog256_final((streebog256_context*)hctx, output); 330 } 331 #endif 332 #ifdef WITH_HASH_STREEBOG512 333 ATTRIBUTE_WARN_UNUSED_RET int _streebog512_init(hash_context * hctx) 334 { 335 return streebog512_init((streebog512_context*)hctx); 336 } 337 ATTRIBUTE_WARN_UNUSED_RET int _streebog512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 338 { 339 return streebog512_update((streebog512_context*)hctx, chunk, chunklen); 340 } 341 ATTRIBUTE_WARN_UNUSED_RET int _streebog512_final(hash_context * hctx, unsigned char *output) 342 { 343 return streebog512_final((streebog512_context*)hctx, output); 344 } 345 #endif 346 #ifdef WITH_HASH_RIPEMD160 347 ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_init(hash_context * hctx) 348 { 349 return ripemd160_init((ripemd160_context*)hctx); 350 } 351 ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 352 { 353 return ripemd160_update((ripemd160_context*)hctx, chunk, chunklen); 354 } 355 ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_final(hash_context * hctx, unsigned char *output) 356 { 357 return ripemd160_final((ripemd160_context*)hctx, output); 358 } 359 #endif 360 #ifdef WITH_HASH_BELT_HASH 361 ATTRIBUTE_WARN_UNUSED_RET int _belt_hash_init(hash_context * hctx) 362 { 363 return belt_hash_init((belt_hash_context*)hctx); 364 } 365 ATTRIBUTE_WARN_UNUSED_RET int _belt_hash_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 366 { 367 return belt_hash_update((belt_hash_context*)hctx, chunk, chunklen); 368 } 369 ATTRIBUTE_WARN_UNUSED_RET int _belt_hash_final(hash_context * hctx, unsigned char *output) 370 { 371 return belt_hash_final((belt_hash_context*)hctx, output); 372 } 373 #endif 374 #ifdef WITH_HASH_BASH224 375 ATTRIBUTE_WARN_UNUSED_RET int _bash224_init(hash_context * hctx) 376 { 377 return bash224_init((bash224_context*)hctx); 378 } 379 ATTRIBUTE_WARN_UNUSED_RET int _bash224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 380 { 381 return bash224_update((bash224_context*)hctx, chunk, chunklen); 382 } 383 ATTRIBUTE_WARN_UNUSED_RET int _bash224_final(hash_context * hctx, unsigned char *output) 384 { 385 return bash224_final((bash224_context*)hctx, output); 386 } 387 #endif 388 #ifdef WITH_HASH_BASH256 389 ATTRIBUTE_WARN_UNUSED_RET int _bash256_init(hash_context * hctx) 390 { 391 return bash256_init((bash256_context*)hctx); 392 } 393 ATTRIBUTE_WARN_UNUSED_RET int _bash256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 394 { 395 return bash256_update((bash256_context*)hctx, chunk, chunklen); 396 } 397 ATTRIBUTE_WARN_UNUSED_RET int _bash256_final(hash_context * hctx, unsigned char *output) 398 { 399 return bash256_final((bash256_context*)hctx, output); 400 } 401 #endif 402 #ifdef WITH_HASH_BASH384 403 ATTRIBUTE_WARN_UNUSED_RET int _bash384_init(hash_context * hctx) 404 { 405 return bash384_init((bash384_context*)hctx); 406 } 407 ATTRIBUTE_WARN_UNUSED_RET int _bash384_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 408 { 409 return bash384_update((bash384_context*)hctx, chunk, chunklen); 410 } 411 ATTRIBUTE_WARN_UNUSED_RET int _bash384_final(hash_context * hctx, unsigned char *output) 412 { 413 return bash384_final((bash384_context*)hctx, output); 414 } 415 #endif 416 #ifdef WITH_HASH_BASH512 417 ATTRIBUTE_WARN_UNUSED_RET int _bash512_init(hash_context * hctx) 418 { 419 return bash512_init((bash512_context*)hctx); 420 } 421 ATTRIBUTE_WARN_UNUSED_RET int _bash512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen) 422 { 423 return bash512_update((bash512_context*)hctx, chunk, chunklen); 424 } 425 ATTRIBUTE_WARN_UNUSED_RET int _bash512_final(hash_context * hctx, unsigned char *output) 426 { 427 return bash512_final((bash512_context*)hctx, output); 428 } 429 #endif 430