Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:15

0001 /* macros.h
0002 
0003    Copyright (C) 2001, 2010 Niels Möller
0004 
0005    This file is part of GNU Nettle.
0006 
0007    GNU Nettle is free software: you can redistribute it and/or
0008    modify it under the terms of either:
0009 
0010      * the GNU Lesser General Public License as published by the Free
0011        Software Foundation; either version 3 of the License, or (at your
0012        option) any later version.
0013 
0014    or
0015 
0016      * the GNU General Public License as published by the Free
0017        Software Foundation; either version 2 of the License, or (at your
0018        option) any later version.
0019 
0020    or both in parallel, as here.
0021 
0022    GNU Nettle is distributed in the hope that it will be useful,
0023    but WITHOUT ANY WARRANTY; without even the implied warranty of
0024    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0025    General Public License for more details.
0026 
0027    You should have received copies of the GNU General Public License and
0028    the GNU Lesser General Public License along with this program.  If
0029    not, see http://www.gnu.org/licenses/.
0030 */
0031 
0032 #ifndef NETTLE_MACROS_H_INCLUDED
0033 #define NETTLE_MACROS_H_INCLUDED
0034 
0035 /* Reads a 64-bit integer, in network, big-endian, byte order */
0036 #define READ_UINT64(p)              \
0037 (  (((uint64_t) (p)[0]) << 56)          \
0038  | (((uint64_t) (p)[1]) << 48)          \
0039  | (((uint64_t) (p)[2]) << 40)          \
0040  | (((uint64_t) (p)[3]) << 32)          \
0041  | (((uint64_t) (p)[4]) << 24)          \
0042  | (((uint64_t) (p)[5]) << 16)          \
0043  | (((uint64_t) (p)[6]) << 8)           \
0044  |  ((uint64_t) (p)[7]))
0045 
0046 #define WRITE_UINT64(p, i)          \
0047 do {                        \
0048   (p)[0] = ((i) >> 56) & 0xff;          \
0049   (p)[1] = ((i) >> 48) & 0xff;          \
0050   (p)[2] = ((i) >> 40) & 0xff;          \
0051   (p)[3] = ((i) >> 32) & 0xff;          \
0052   (p)[4] = ((i) >> 24) & 0xff;          \
0053   (p)[5] = ((i) >> 16) & 0xff;          \
0054   (p)[6] = ((i) >> 8) & 0xff;           \
0055   (p)[7] = (i) & 0xff;              \
0056 } while(0)
0057 
0058 /* Reads a 32-bit integer, in network, big-endian, byte order */
0059 #define READ_UINT32(p)              \
0060 (  (((uint32_t) (p)[0]) << 24)          \
0061  | (((uint32_t) (p)[1]) << 16)          \
0062  | (((uint32_t) (p)[2]) << 8)           \
0063  |  ((uint32_t) (p)[3]))
0064 
0065 #define WRITE_UINT32(p, i)          \
0066 do {                        \
0067   (p)[0] = ((i) >> 24) & 0xff;          \
0068   (p)[1] = ((i) >> 16) & 0xff;          \
0069   (p)[2] = ((i) >> 8) & 0xff;           \
0070   (p)[3] = (i) & 0xff;              \
0071 } while(0)
0072 
0073 /* Analogous macros, for 24 and 16 bit numbers */
0074 #define READ_UINT24(p)              \
0075 (  (((uint32_t) (p)[0]) << 16)          \
0076  | (((uint32_t) (p)[1]) << 8)           \
0077  |  ((uint32_t) (p)[2]))
0078 
0079 #define WRITE_UINT24(p, i)          \
0080 do {                        \
0081   (p)[0] = ((i) >> 16) & 0xff;          \
0082   (p)[1] = ((i) >> 8) & 0xff;           \
0083   (p)[2] = (i) & 0xff;              \
0084 } while(0)
0085 
0086 #define READ_UINT16(p)              \
0087 (  (((uint32_t) (p)[0]) << 8)           \
0088  |  ((uint32_t) (p)[1]))
0089 
0090 #define WRITE_UINT16(p, i)          \
0091 do {                        \
0092   (p)[0] = ((i) >> 8) & 0xff;           \
0093   (p)[1] = (i) & 0xff;              \
0094 } while(0)
0095 
0096 /* And the other, little-endian, byteorder */
0097 #define LE_READ_UINT64(p)           \
0098 (  (((uint64_t) (p)[7]) << 56)          \
0099  | (((uint64_t) (p)[6]) << 48)          \
0100  | (((uint64_t) (p)[5]) << 40)          \
0101  | (((uint64_t) (p)[4]) << 32)          \
0102  | (((uint64_t) (p)[3]) << 24)          \
0103  | (((uint64_t) (p)[2]) << 16)          \
0104  | (((uint64_t) (p)[1]) << 8)           \
0105  |  ((uint64_t) (p)[0]))
0106 
0107 #define LE_WRITE_UINT64(p, i)           \
0108 do {                        \
0109   (p)[7] = ((i) >> 56) & 0xff;          \
0110   (p)[6] = ((i) >> 48) & 0xff;          \
0111   (p)[5] = ((i) >> 40) & 0xff;          \
0112   (p)[4] = ((i) >> 32) & 0xff;          \
0113   (p)[3] = ((i) >> 24) & 0xff;          \
0114   (p)[2] = ((i) >> 16) & 0xff;          \
0115   (p)[1] = ((i) >> 8) & 0xff;           \
0116   (p)[0] = (i) & 0xff;              \
0117 } while (0)
0118 
0119 #define LE_READ_UINT32(p)           \
0120 (  (((uint32_t) (p)[3]) << 24)          \
0121  | (((uint32_t) (p)[2]) << 16)          \
0122  | (((uint32_t) (p)[1]) << 8)           \
0123  |  ((uint32_t) (p)[0]))
0124 
0125 #define LE_WRITE_UINT32(p, i)           \
0126 do {                        \
0127   (p)[3] = ((i) >> 24) & 0xff;          \
0128   (p)[2] = ((i) >> 16) & 0xff;          \
0129   (p)[1] = ((i) >> 8) & 0xff;           \
0130   (p)[0] = (i) & 0xff;              \
0131 } while(0)
0132 
0133 /* Analogous macros, for 16 bit numbers */
0134 #define LE_READ_UINT16(p)           \
0135   (  (((uint32_t) (p)[1]) << 8)         \
0136      |  ((uint32_t) (p)[0]))
0137 
0138 #define LE_WRITE_UINT16(p, i)           \
0139   do {                      \
0140     (p)[1] = ((i) >> 8) & 0xff;         \
0141     (p)[0] = (i) & 0xff;            \
0142   } while(0)
0143 
0144 /* Macro to make it easier to loop over several blocks. */
0145 #define FOR_BLOCKS(length, dst, src, blocksize) \
0146   assert( !((length) % (blocksize)));           \
0147   for (; (length); ((length) -= (blocksize),    \
0148           (dst) += (blocksize),     \
0149           (src) += (blocksize)) )
0150 
0151 /* The masking of the right shift is needed to allow n == 0 (using
0152    just 32 - n and 64 - n results in undefined behaviour). Most uses
0153    of these macros use a constant and non-zero rotation count. */
0154 #define ROTL32(n,x) (((x)<<(n)) | ((x)>>((-(n)&31))))
0155 
0156 #define ROTL64(n,x) (((x)<<(n)) | ((x)>>((-(n))&63)))
0157 
0158 /* Requires that size > 0 */
0159 #define INCREMENT(size, ctr)            \
0160   do {                      \
0161     unsigned increment_i = (size) - 1;      \
0162     if (++(ctr)[increment_i] == 0)      \
0163       while (increment_i > 0            \
0164          && ++(ctr)[--increment_i] == 0 )   \
0165     ;                   \
0166   } while (0)
0167 
0168 
0169 /* Helper macro for Merkle-Damgård hash functions. Assumes the context
0170    structs includes the following fields:
0171 
0172      uint8_t block[...];        // Buffer holding one block
0173      unsigned int index;        // Index into block
0174 */
0175 
0176 /* Currently used by sha512 (and sha384) only. */
0177 #define MD_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low)
0178 
0179 /* Takes the compression function f as argument. NOTE: also clobbers
0180    length and data. */
0181 #define MD_UPDATE(ctx, length, data, f, incr)               \
0182   do {                                  \
0183     if ((ctx)->index)                           \
0184       {                                 \
0185     /* Try to fill partial block */                 \
0186     unsigned __md_left = sizeof((ctx)->block) - (ctx)->index;   \
0187     if ((length) < __md_left)                   \
0188       {                             \
0189         memcpy((ctx)->block + (ctx)->index, (data), (length));  \
0190         (ctx)->index += (length);                   \
0191         goto __md_done; /* Finished */              \
0192       }                             \
0193     else                                \
0194       {                             \
0195         memcpy((ctx)->block + (ctx)->index, (data), __md_left); \
0196                                     \
0197         f((ctx), (ctx)->block);                 \
0198         (incr);                         \
0199                                     \
0200         (data) += __md_left;                    \
0201         (length) -= __md_left;                  \
0202       }                             \
0203       }                                 \
0204     while ((length) >= sizeof((ctx)->block))                \
0205       {                                 \
0206     f((ctx), (data));                       \
0207     (incr);                             \
0208                                     \
0209     (data) += sizeof((ctx)->block);                 \
0210     (length) -= sizeof((ctx)->block);               \
0211       }                                 \
0212     memcpy ((ctx)->block, (data), (length));                \
0213     (ctx)->index = (length);                        \
0214   __md_done:                                \
0215     ;                                   \
0216   } while (0)
0217 
0218 /* Pads the block to a block boundary with the bit pattern 1 0*,
0219    leaving size octets for the length field at the end. If needed,
0220    compresses the block and starts a new one. */
0221 #define MD_PAD(ctx, size, f)                        \
0222   do {                                  \
0223     unsigned __md_i;                            \
0224     __md_i = (ctx)->index;                      \
0225                                     \
0226     /* Set the first char of padding to 0x80. This is safe since there  \
0227        is always at least one byte free */              \
0228                                     \
0229     assert(__md_i < sizeof((ctx)->block));              \
0230     (ctx)->block[__md_i++] = 0x80;                  \
0231                                     \
0232     if (__md_i > (sizeof((ctx)->block) - (size)))           \
0233       { /* No room for length in this block. Process it and     \
0234        pad with another one */                  \
0235     memset((ctx)->block + __md_i, 0, sizeof((ctx)->block) - __md_i); \
0236                                     \
0237     f((ctx), (ctx)->block);                     \
0238     __md_i = 0;                         \
0239       }                                 \
0240     memset((ctx)->block + __md_i, 0,                    \
0241        sizeof((ctx)->block) - (size) - __md_i);         \
0242                                     \
0243   } while (0)
0244 
0245 #endif /* NETTLE_MACROS_H_INCLUDED */