]>
Commit | Line | Data |
---|---|---|
12bb2223 NM |
1 | /* md-internal.h |
2 | ||
3 | Copyright (C) 2001, 2010, 2022 Niels Möller | |
4 | ||
5 | This file is part of GNU Nettle. | |
6 | ||
7 | GNU Nettle is free software: you can redistribute it and/or | |
8 | modify it under the terms of either: | |
9 | ||
10 | * the GNU Lesser General Public License as published by the Free | |
11 | Software Foundation; either version 3 of the License, or (at your | |
12 | option) any later version. | |
13 | ||
14 | or | |
15 | ||
16 | * the GNU General Public License as published by the Free | |
17 | Software Foundation; either version 2 of the License, or (at your | |
18 | option) any later version. | |
19 | ||
20 | or both in parallel, as here. | |
21 | ||
22 | GNU Nettle is distributed in the hope that it will be useful, | |
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 | General Public License for more details. | |
26 | ||
27 | You should have received copies of the GNU General Public License and | |
28 | the GNU Lesser General Public License along with this program. If | |
29 | not, see http://www.gnu.org/licenses/. | |
30 | */ | |
31 | ||
32 | #ifndef NETTLE_MD_INTERNAL_H_INCLUDED | |
33 | #define NETTLE_MD_INTERNAL_H_INCLUDED | |
34 | ||
bb9c0a1e NM |
35 | #include <string.h> |
36 | ||
12bb2223 NM |
37 | /* Internal helper macros for Merkle-Damgård hash functions. Assumes the context |
38 | structs includes the following fields: | |
39 | ||
40 | uint8_t block[...]; // Buffer holding one block | |
41 | unsigned int index; // Index into block | |
42 | */ | |
43 | ||
44 | #define MD_FILL_OR_RETURN(ctx, length, data) \ | |
45 | do { \ | |
46 | unsigned __md_left = sizeof((ctx)->block) - (ctx)->index; \ | |
47 | if ((length) < __md_left) \ | |
48 | { \ | |
49 | memcpy((ctx)->block + (ctx)->index, (data), (length)); \ | |
50 | (ctx)->index += (length); \ | |
51 | return; \ | |
52 | } \ | |
53 | memcpy((ctx)->block + (ctx)->index, (data), __md_left); \ | |
54 | (data) += __md_left; \ | |
55 | (length) -= __md_left; \ | |
12bb2223 NM |
56 | } while(0) |
57 | ||
bb9c0a1e NM |
58 | #define MD_FILL_OR_RETURN_INDEX(block_size, block, index, length, data) \ |
59 | do { \ | |
60 | unsigned __md_left = (block_size) - (index); \ | |
61 | if ((length) < __md_left) \ | |
62 | { \ | |
63 | memcpy(block + (index), (data), (length)); \ | |
64 | return (index) + (length); \ | |
65 | } \ | |
66 | memcpy((block) + (index), (data), __md_left); \ | |
67 | (data) += __md_left; \ | |
68 | (length) -= __md_left; \ | |
69 | } while(0) | |
12bb2223 | 70 | #endif /* NETTLE_MD_INTERNAL_H_INCLUDED */ |