5 /* nettle, low-level cryptographics library
7 * Copyright (C) 2001, 2010 Niels Möller
9 * The nettle library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or (at your
12 * option) any later version.
14 * The nettle library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the nettle library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 #ifndef NETTLE_MACROS_H_INCLUDED
26 #define NETTLE_MACROS_H_INCLUDED
28 /* Reads a 64-bit integer, in network, big-endian, byte order */
29 #define READ_UINT64(p) \
30 ( (((uint64_t) (p)[0]) << 56) \
31 | (((uint64_t) (p)[1]) << 48) \
32 | (((uint64_t) (p)[2]) << 40) \
33 | (((uint64_t) (p)[3]) << 32) \
34 | (((uint64_t) (p)[4]) << 24) \
35 | (((uint64_t) (p)[5]) << 16) \
36 | (((uint64_t) (p)[6]) << 8) \
37 | ((uint64_t) (p)[7]))
39 #define WRITE_UINT64(p, i) \
41 (p)[0] = ((i) >> 56) & 0xff; \
42 (p)[1] = ((i) >> 48) & 0xff; \
43 (p)[2] = ((i) >> 40) & 0xff; \
44 (p)[3] = ((i) >> 32) & 0xff; \
45 (p)[4] = ((i) >> 24) & 0xff; \
46 (p)[5] = ((i) >> 16) & 0xff; \
47 (p)[6] = ((i) >> 8) & 0xff; \
48 (p)[7] = (i) & 0xff; \
51 /* Reads a 32-bit integer, in network, big-endian, byte order */
52 #define READ_UINT32(p) \
53 ( (((uint32_t) (p)[0]) << 24) \
54 | (((uint32_t) (p)[1]) << 16) \
55 | (((uint32_t) (p)[2]) << 8) \
56 | ((uint32_t) (p)[3]))
58 #define WRITE_UINT32(p, i) \
60 (p)[0] = ((i) >> 24) & 0xff; \
61 (p)[1] = ((i) >> 16) & 0xff; \
62 (p)[2] = ((i) >> 8) & 0xff; \
63 (p)[3] = (i) & 0xff; \
66 /* Analogous macros, for 24 and 16 bit numbers */
67 #define READ_UINT24(p) \
68 ( (((uint32_t) (p)[0]) << 16) \
69 | (((uint32_t) (p)[1]) << 8) \
70 | ((uint32_t) (p)[2]))
72 #define WRITE_UINT24(p, i) \
74 (p)[0] = ((i) >> 16) & 0xff; \
75 (p)[1] = ((i) >> 8) & 0xff; \
76 (p)[2] = (i) & 0xff; \
79 #define READ_UINT16(p) \
80 ( (((uint32_t) (p)[0]) << 8) \
81 | ((uint32_t) (p)[1]))
83 #define WRITE_UINT16(p, i) \
85 (p)[0] = ((i) >> 8) & 0xff; \
86 (p)[1] = (i) & 0xff; \
89 /* And the other, little-endian, byteorder */
90 #define LE_READ_UINT32(p) \
91 ( (((uint32_t) (p)[3]) << 24) \
92 | (((uint32_t) (p)[2]) << 16) \
93 | (((uint32_t) (p)[1]) << 8) \
94 | ((uint32_t) (p)[0]))
96 #define LE_WRITE_UINT32(p, i) \
98 (p)[3] = ((i) >> 24) & 0xff; \
99 (p)[2] = ((i) >> 16) & 0xff; \
100 (p)[1] = ((i) >> 8) & 0xff; \
101 (p)[0] = (i) & 0xff; \
104 /* Analogous macros, for 16 bit numbers */
105 #define LE_READ_UINT16(p) \
106 ( (((uint32_t) (p)[1]) << 8) \
107 | ((uint32_t) (p)[0]))
109 #define LE_WRITE_UINT16(p, i) \
111 (p)[1] = ((i) >> 8) & 0xff; \
112 (p)[0] = (i) & 0xff; \
115 /* Macro to make it easier to loop over several blocks. */
116 #define FOR_BLOCKS(length, dst, src, blocksize) \
117 assert( !((length) % (blocksize))); \
118 for (; (length); ((length) -= (blocksize), \
119 (dst) += (blocksize), \
120 (src) += (blocksize)) )
122 /* Requires that size >= 2 */
123 #define INCREMENT(size, ctr) \
125 unsigned increment_i = (size) - 1; \
126 if (++(ctr)[increment_i] == 0) \
128 while (++(ctr)[--increment_i] == 0 \
129 && increment_i > 0) \
135 /* Helper macro for Merkle-Damgård hash functions. Assumes the context
136 structs includes the following fields:
138 xxx count_low, count_high; // Two word block count
139 uint8_t block[...]; // Buffer holding one block
140 unsigned int index; // Index into block
143 /* FIXME: Should probably switch to using uint64_t for the count, but
144 due to alignment and byte order that may be an ABI change. */
146 #define MD_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low)
148 /* Takes the compression function f as argument. NOTE: also clobbers
150 #define MD_UPDATE(ctx, length, data, f, incr) \
154 /* Try to fill partial block */ \
155 unsigned __md_left = sizeof((ctx)->block) - (ctx)->index; \
156 if ((length) < __md_left) \
158 memcpy((ctx)->block + (ctx)->index, (data), (length)); \
159 (ctx)->index += (length); \
160 goto __md_done; /* Finished */ \
164 memcpy((ctx)->block + (ctx)->index, (data), __md_left); \
166 f((ctx), (ctx)->block); \
169 (data) += __md_left; \
170 (length) -= __md_left; \
173 while ((length) >= sizeof((ctx)->block)) \
178 (data) += sizeof((ctx)->block); \
179 (length) -= sizeof((ctx)->block); \
181 memcpy ((ctx)->block, (data), (length)); \
182 (ctx)->index = (length); \
187 /* Pads the block to a block boundary with the bit pattern 1 0*,
188 leaving size octets for the length field at the end. If needed,
189 compresses the block and starts a new one. */
190 #define MD_PAD(ctx, size, f) \
193 __md_i = (ctx)->index; \
195 /* Set the first char of padding to 0x80. This is safe since there \
196 is always at least one byte free */ \
198 assert(__md_i < sizeof((ctx)->block)); \
199 (ctx)->block[__md_i++] = 0x80; \
201 if (__md_i > (sizeof((ctx)->block) - 2*sizeof((ctx)->count_low))) \
202 { /* No room for length in this block. Process it and \
203 pad with another one */ \
204 memset((ctx)->block + __md_i, 0, sizeof((ctx)->block) - __md_i); \
206 f((ctx), (ctx)->block); \
209 memset((ctx)->block + __md_i, 0, \
210 sizeof((ctx)->block) - (size) - __md_i); \
214 #endif /* NETTLE_MACROS_H_INCLUDED */