1 /* 2 * Copyright (C) 2021 - 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 * 8 * This software is licensed under a dual BSD and GPL v2 license. 9 * See LICENSE file at the root folder of the project. 10 */ 11 #ifndef __MD5_H__ 12 #define __MD5_H__ 13 14 /* Include libec for useful types and macros */ 15 #include <libecc/libec.h> 16 17 /****************************************************/ 18 /* 19 * 32-bit integer manipulation macros 20 */ 21 #ifndef GET_UINT32_BE 22 #define GET_UINT32_BE(n, b, i) \ 23 do { \ 24 (n) = ( ((u32) (b)[(i) ]) << 24 ) \ 25 | ( ((u32) (b)[(i) + 1]) << 16 ) \ 26 | ( ((u32) (b)[(i) + 2]) << 8 ) \ 27 | ( ((u32) (b)[(i) + 3]) ); \ 28 } while( 0 ) 29 #endif 30 #ifndef GET_UINT32_LE 31 #define GET_UINT32_LE(n, b, i) \ 32 do { \ 33 (n) = ( ((u32) (b)[(i) + 3]) << 24 ) \ 34 | ( ((u32) (b)[(i) + 2]) << 16 ) \ 35 | ( ((u32) (b)[(i) + 1]) << 8 ) \ 36 | ( ((u32) (b)[(i) ]) ); \ 37 } while( 0 ) 38 #endif 39 40 41 #ifndef PUT_UINT32_BE 42 #define PUT_UINT32_BE(n, b, i) \ 43 do { \ 44 (b)[(i) ] = (u8) ( (n) >> 24 ); \ 45 (b)[(i) + 1] = (u8) ( (n) >> 16 ); \ 46 (b)[(i) + 2] = (u8) ( (n) >> 8 ); \ 47 (b)[(i) + 3] = (u8) ( (n) ); \ 48 } while( 0 ) 49 #endif 50 51 #ifndef PUT_UINT32_LE 52 #define PUT_UINT32_LE(n, b, i) \ 53 do { \ 54 (b)[(i) + 3] = (u8) ( (n) >> 24 ); \ 55 (b)[(i) + 2] = (u8) ( (n) >> 16 ); \ 56 (b)[(i) + 1] = (u8) ( (n) >> 8 ); \ 57 (b)[(i) ] = (u8) ( (n) ); \ 58 } while( 0 ) 59 #endif 60 61 /* 62 * 64-bit integer manipulation macros 63 */ 64 #ifndef PUT_UINT64_BE 65 #define PUT_UINT64_BE(n,b,i) \ 66 do { \ 67 (b)[(i) ] = (u8) ( (n) >> 56 ); \ 68 (b)[(i) + 1] = (u8) ( (n) >> 48 ); \ 69 (b)[(i) + 2] = (u8) ( (n) >> 40 ); \ 70 (b)[(i) + 3] = (u8) ( (n) >> 32 ); \ 71 (b)[(i) + 4] = (u8) ( (n) >> 24 ); \ 72 (b)[(i) + 5] = (u8) ( (n) >> 16 ); \ 73 (b)[(i) + 6] = (u8) ( (n) >> 8 ); \ 74 (b)[(i) + 7] = (u8) ( (n) ); \ 75 } while( 0 ) 76 #endif /* PUT_UINT64_BE */ 77 78 #ifndef PUT_UINT64_LE 79 #define PUT_UINT64_LE(n,b,i) \ 80 do { \ 81 (b)[(i) + 7] = (u8) ( (n) >> 56 ); \ 82 (b)[(i) + 6] = (u8) ( (n) >> 48 ); \ 83 (b)[(i) + 5] = (u8) ( (n) >> 40 ); \ 84 (b)[(i) + 4] = (u8) ( (n) >> 32 ); \ 85 (b)[(i) + 3] = (u8) ( (n) >> 24 ); \ 86 (b)[(i) + 2] = (u8) ( (n) >> 16 ); \ 87 (b)[(i) + 1] = (u8) ( (n) >> 8 ); \ 88 (b)[(i) ] = (u8) ( (n) ); \ 89 } while( 0 ) 90 #endif /* PUT_UINT64_LE */ 91 92 #define MD5_STATE_SIZE 4 93 #define MD5_BLOCK_SIZE 64 94 #define MD5_DIGEST_SIZE 16 95 #define MD5_DIGEST_SIZE_BITS 128 96 97 #define MD5_HASH_MAGIC ((word_t)(0x5684922132353193ULL)) 98 #define MD5_HASH_CHECK_INITIALIZED(A, ret, err) \ 99 MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == MD5_HASH_MAGIC), ret, err) 100 101 #define ROTL_MD5(x, n) ((((u32)(x)) << (n)) | (((u32)(x)) >> (32-(n)))) 102 103 typedef struct { 104 /* Number of bytes processed */ 105 u64 md5_total; 106 /* Internal state */ 107 u32 md5_state[MD5_STATE_SIZE]; 108 /* Internal buffer to handle updates in a block */ 109 u8 md5_buffer[MD5_BLOCK_SIZE]; 110 /* Initialization magic value */ 111 word_t magic; 112 } md5_context; 113 114 /* Init hash function. Returns 0 on success, -1 on error. */ 115 ATTRIBUTE_WARN_UNUSED_RET int md5_init(md5_context *ctx); 116 117 ATTRIBUTE_WARN_UNUSED_RET int md5_update(md5_context *ctx, const u8 *input, u32 ilen); 118 119 /* Finalize. Returns 0 on success, -1 on error.*/ 120 ATTRIBUTE_WARN_UNUSED_RET int md5_final(md5_context *ctx, u8 output[MD5_DIGEST_SIZE]); 121 122 /* 123 * Scattered version performing init/update/finalize on a vector of buffers 124 * 'inputs' with the length of each buffer passed via 'ilens'. The function 125 * loops on pointers in 'inputs' until it finds a NULL pointer. The function 126 * returns 0 on success, -1 on error. 127 */ 128 ATTRIBUTE_WARN_UNUSED_RET int md5_scattered(const u8 **inputs, const u32 *ilens, 129 u8 output[MD5_DIGEST_SIZE]); 130 131 /* 132 * Single call version performing init/update/final on given input. 133 * Returns 0 on success, -1 on error. 134 */ 135 ATTRIBUTE_WARN_UNUSED_RET int md5(const u8 *input, u32 ilen, u8 output[MD5_DIGEST_SIZE]); 136 137 #endif /* __MD5_H__ */ 138