| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#ifndef ENDIANESS_H |
| 33 |
#define ENDIANESS_H |
| 34 |
|
| 35 |
#include "common.h" |
| 36 |
|
| 37 |
static inline void u32to8_little(uint8_t *p, const uint32_t *w) |
| 38 |
{ |
| 39 |
#ifdef PYCRYPTO_LITTLE_ENDIAN |
| 40 |
memcpy(p, w, 4); |
| 41 |
#else |
| 42 |
p[0] = (uint8_t)*w; |
| 43 |
p[1] = (uint8_t)(*w >> 8); |
| 44 |
p[2] = (uint8_t)(*w >> 16); |
| 45 |
p[3] = (uint8_t)(*w >> 24); |
| 46 |
#endif |
| 47 |
} |
| 48 |
|
| 49 |
static inline void u8to32_little(uint32_t *w, const uint8_t *p) |
| 50 |
{ |
| 51 |
#ifdef PYCRYPTO_LITTLE_ENDIAN |
| 52 |
memcpy(w, p, 4); |
| 53 |
#else |
| 54 |
*w = (uint32_t)p[0] | (uint32_t)p[1]<<8 | (uint32_t)p[2]<<16 | (uint32_t)p[3]<<24; |
| 55 |
#endif |
| 56 |
} |
| 57 |
|
| 58 |
static inline void u32to8_big(uint8_t *p, const uint32_t *w) |
| 59 |
{ |
| 60 |
#ifdef PYCRYPTO_BIG_ENDIAN |
| 61 |
memcpy(p, w, 4); |
| 62 |
#else |
| 63 |
p[0] = (uint8_t)(*w >> 24); |
| 64 |
p[1] = (uint8_t)(*w >> 16); |
| 65 |
p[2] = (uint8_t)(*w >> 8); |
| 66 |
p[3] = (uint8_t)*w; |
| 67 |
#endif |
| 68 |
} |
| 69 |
|
| 70 |
static inline void u8to32_big(uint32_t *w, const uint8_t *p) |
| 71 |
{ |
| 72 |
#ifdef PYCRYPTO_BIG_ENDIAN |
| 73 |
memcpy(w, p, 4); |
| 74 |
#else |
| 75 |
*w = (uint32_t)p[3] | (uint32_t)p[2]<<8 | (uint32_t)p[1]<<16 | (uint32_t)p[0]<<24; |
| 76 |
#endif |
| 77 |
} |
| 78 |
|
| 79 |
static inline uint32_t load_u8to32_little(const uint8_t *p) |
| 80 |
{ |
| 81 |
uint32_t w; |
| 82 |
|
| 83 |
u8to32_little(&w, p); |
| 84 |
return w; |
| 85 |
} |
| 86 |
|
| 87 |
static inline uint32_t load_u8to32_big(const uint8_t *p) |
| 88 |
{ |
| 89 |
uint32_t w; |
| 90 |
|
| 91 |
u8to32_big(&w, p); |
| 92 |
return w; |
| 93 |
} |
| 94 |
|
| 95 |
#define LOAD_U32_LITTLE(p) load_u8to32_little(p) |
| 96 |
#define LOAD_U32_BIG(p) load_u8to32_big(p) |
| 97 |
|
| 98 |
#define STORE_U32_LITTLE(p, w) u32to8_little((p), &(w)) |
| 99 |
#define STORE_U32_BIG(p, w) u32to8_big((p), &(w)) |
| 100 |
|
| 101 |
static inline void u64to8_little(uint8_t *p, const uint64_t *w) |
| 102 |
{ |
| 103 |
#ifdef PYCRYPTO_LITTLE_ENDIAN |
| 104 |
memcpy(p, w, 8); |
| 105 |
#else |
| 106 |
p[0] = (uint8_t)*w; |
| 107 |
p[1] = (uint8_t)(*w >> 8); |
| 108 |
p[2] = (uint8_t)(*w >> 16); |
| 109 |
p[3] = (uint8_t)(*w >> 24); |
| 110 |
p[4] = (uint8_t)(*w >> 32); |
| 111 |
p[5] = (uint8_t)(*w >> 40); |
| 112 |
p[6] = (uint8_t)(*w >> 48); |
| 113 |
p[7] = (uint8_t)(*w >> 56); |
| 114 |
#endif |
| 115 |
} |
| 116 |
|
| 117 |
static inline void u8to64_little(uint64_t *w, const uint8_t *p) |
| 118 |
{ |
| 119 |
#ifdef PYCRYPTO_LITTLE_ENDIAN |
| 120 |
memcpy(w, p, 8); |
| 121 |
#else |
| 122 |
*w = (uint64_t)p[0] | |
| 123 |
(uint64_t)p[1] << 8 | |
| 124 |
(uint64_t)p[2] << 16 | |
| 125 |
(uint64_t)p[3] << 24 | |
| 126 |
(uint64_t)p[4] << 32 | |
| 127 |
(uint64_t)p[5] << 40 | |
| 128 |
(uint64_t)p[6] << 48 | |
| 129 |
(uint64_t)p[7] << 56; |
| 130 |
#endif |
| 131 |
} |
| 132 |
|
| 133 |
static inline void u64to8_big(uint8_t *p, const uint64_t *w) |
| 134 |
{ |
| 135 |
#ifdef PYCRYPTO_BIG_ENDIAN |
| 136 |
memcpy(p, w, 8); |
| 137 |
#else |
| 138 |
p[0] = (uint8_t)(*w >> 56); |
| 139 |
p[1] = (uint8_t)(*w >> 48); |
| 140 |
p[2] = (uint8_t)(*w >> 40); |
| 141 |
p[3] = (uint8_t)(*w >> 32); |
| 142 |
p[4] = (uint8_t)(*w >> 24); |
| 143 |
p[5] = (uint8_t)(*w >> 16); |
| 144 |
p[6] = (uint8_t)(*w >> 8); |
| 145 |
p[7] = (uint8_t)*w; |
| 146 |
#endif |
| 147 |
} |
| 148 |
|
| 149 |
static inline void u8to64_big(uint64_t *w, const uint8_t *p) |
| 150 |
{ |
| 151 |
#ifdef PYCRYPTO_BIG_ENDIAN |
| 152 |
memcpy(w, p, 8); |
| 153 |
#else |
| 154 |
*w = (uint64_t)p[0] << 56 | |
| 155 |
(uint64_t)p[1] << 48 | |
| 156 |
(uint64_t)p[2] << 40 | |
| 157 |
(uint64_t)p[3] << 32 | |
| 158 |
(uint64_t)p[4] << 24 | |
| 159 |
(uint64_t)p[5] << 16 | |
| 160 |
(uint64_t)p[6] << 8 | |
| 161 |
(uint64_t)p[7]; |
| 162 |
#endif |
| 163 |
} |
| 164 |
|
| 165 |
static inline uint64_t load_u8to64_little(const uint8_t *p) |
| 166 |
{ |
| 167 |
uint64_t w; |
| 168 |
|
| 169 |
u8to64_little(&w, p); |
| 170 |
return w; |
| 171 |
} |
| 172 |
|
| 173 |
static inline uint64_t load_u8to64_big(const uint8_t *p) |
| 174 |
{ |
| 175 |
uint64_t w; |
| 176 |
|
| 177 |
u8to64_big(&w, p); |
| 178 |
return w; |
| 179 |
} |
| 180 |
|
| 181 |
#define LOAD_U64_LITTLE(p) load_u8to64_little(p) |
| 182 |
#define LOAD_U64_BIG(p) load_u8to64_big(p) |
| 183 |
|
| 184 |
#define STORE_U64_LITTLE(p, w) u64to8_little((p), &(w)) |
| 185 |
#define STORE_U64_BIG(p, w) u64to8_big((p), &(w)) |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
static inline int bytes_to_words(uint64_t *x, size_t words, const uint8_t *in, size_t len) |
| 193 |
{ |
| 194 |
uint8_t buf8[8]; |
| 195 |
size_t words_used, bytes_in_msw, i; |
| 196 |
uint64_t *xp; |
| 197 |
|
| 198 |
if (0 == words || 0 == len) |
| 199 |
return ERR_NOT_ENOUGH_DATA; |
| 200 |
if (NULL == x || NULL == in) |
| 201 |
return ERR_NULL; |
| 202 |
|
| 203 |
memset(x, 0, words*sizeof(uint64_t)); |
| 204 |
|
| 205 |
|
| 206 |
for (; len > 0 && 0 == *in; in++, len--); |
| 207 |
if (0 == len) |
| 208 |
return 0; |
| 209 |
|
| 210 |
|
| 211 |
words_used = (len + 7) / 8; |
| 212 |
if (words_used > words) |
| 213 |
return ERR_MAX_DATA; |
| 214 |
|
| 215 |
|
| 216 |
bytes_in_msw = len % 8; |
| 217 |
if (bytes_in_msw == 0) |
| 218 |
bytes_in_msw = 8; |
| 219 |
|
| 220 |
|
| 221 |
memset(buf8, 0, 8); |
| 222 |
memcpy(buf8 + (8 - bytes_in_msw), in, bytes_in_msw); |
| 223 |
xp = &x[words_used-1]; |
| 224 |
*xp = LOAD_U64_BIG(buf8); |
| 225 |
in += bytes_in_msw; |
| 226 |
|
| 227 |
|
| 228 |
for (i=0; i<words_used-1; i++, in += 8) { |
| 229 |
xp--; |
| 230 |
*xp = LOAD_U64_BIG(in); |
| 231 |
} |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static inline int words_to_bytes(uint8_t *out, size_t len, const uint64_t *x, size_t words) |
| 240 |
{ |
| 241 |
size_t i; |
| 242 |
const uint64_t *msw; |
| 243 |
uint8_t buf8[8]; |
| 244 |
size_t partial, real_len; |
| 245 |
|
| 246 |
if (0 == words || 0 == len) |
| 247 |
return ERR_NOT_ENOUGH_DATA; |
| 248 |
if (NULL == x || NULL == out) |
| 249 |
return ERR_NULL; |
| 250 |
|
| 251 |
memset(out, 0, len); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
for (; words>0 && x[words-1]==0; words--); |
| 257 |
if (words == 0) |
| 258 |
return 0; |
| 259 |
msw = &x[words-1]; |
| 260 |
|
| 261 |
|
| 262 |
STORE_U64_BIG(buf8, *msw); |
| 263 |
for (partial=8; partial>0 && buf8[8-partial] == 0; partial--); |
| 264 |
assert(partial > 0); |
| 265 |
|
| 266 |
|
| 267 |
real_len = partial + 8*(words-1); |
| 268 |
if (real_len > len) |
| 269 |
return ERR_MAX_DATA; |
| 270 |
|
| 271 |
|
| 272 |
out += len - real_len; |
| 273 |
|
| 274 |
|
| 275 |
memcpy(out, buf8+(8-partial), partial); |
| 276 |
out += partial; |
| 277 |
msw--; |
| 278 |
|
| 279 |
|
| 280 |
for (i=0; i<words-1; i++, out += 8, msw--) |
| 281 |
STORE_U64_BIG(out, *msw); |
| 282 |
|
| 283 |
return 0; |
| 284 |
} |
| 285 |
|
| 286 |
#endif |