1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 18:15:18 -07:00

Use existing functions for unaligned access in hash_sha*

This commit is contained in:
Frank Denis 2016-03-24 15:02:34 +01:00
parent e07a45223f
commit a3a2b74bd8
3 changed files with 46 additions and 75 deletions

View File

@ -28,6 +28,7 @@
#include "crypto_hash_sha256.h"
#include "utils.h"
#include "../../../sodium/common.h"
#include <sys/types.h>
@ -36,53 +37,13 @@
#include <stdlib.h>
#include <string.h>
/* Avoid namespace collisions with BSD <sys/endian.h>. */
#define be32dec _sha256_be32dec
#define be32enc _sha256_be32enc
#define be64enc _sha256_be64enc
static inline uint32_t
be32dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
}
static inline void
be32enc(void *pp, uint32_t x)
{
uint8_t *p = (uint8_t *)pp;
p[3] = x & 0xff;
p[2] = (x >> 8) & 0xff;
p[1] = (x >> 16) & 0xff;
p[0] = (x >> 24) & 0xff;
}
static inline void
be64enc(void * pp, uint64_t x)
{
uint8_t * p = (uint8_t *)pp;
p[7] = x & 0xff;
p[6] = (x >> 8) & 0xff;
p[5] = (x >> 16) & 0xff;
p[4] = (x >> 24) & 0xff;
p[3] = (x >> 32) & 0xff;
p[2] = (x >> 40) & 0xff;
p[1] = (x >> 48) & 0xff;
p[0] = (x >> 56) & 0xff;
}
static void
be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
{
size_t i;
for (i = 0; i < len / 4; i++) {
be32enc(dst + i * 4, src[i]);
STORE32_BE(dst + i * 4, src[i]);
}
}
@ -92,7 +53,7 @@ be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
size_t i;
for (i = 0; i < len / 4; i++) {
dst[i] = be32dec(src + i * 4);
dst[i] = LOAD32_BE(src + i * 4);
}
}
@ -221,7 +182,7 @@ SHA256_Pad(crypto_hash_sha256_state *state)
unsigned char len[8];
uint32_t r, plen;
be64enc(len, state->count);
STORE64_BE(len, state->count);
r = (state->count >> 3) & 0x3f;
plen = (r < 56) ? (56 - r) : (120 - r);

View File

@ -28,6 +28,7 @@
#include "crypto_hash_sha512.h"
#include "utils.h"
#include "../../../sodium/common.h"
#include <sys/types.h>
@ -36,43 +37,13 @@
#include <stdlib.h>
#include <string.h>
/* Avoid namespace collisions with BSD <sys/endian.h>. */
#define be64dec _sha512_be64dec
#define be64enc _sha512_be64enc
static inline uint64_t
be64dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +
((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +
((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +
((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));
}
static inline void
be64enc(void *pp, uint64_t x)
{
uint8_t *p = (uint8_t *)pp;
p[7] = x & 0xff;
p[6] = (x >> 8) & 0xff;
p[5] = (x >> 16) & 0xff;
p[4] = (x >> 24) & 0xff;
p[3] = (x >> 32) & 0xff;
p[2] = (x >> 40) & 0xff;
p[1] = (x >> 48) & 0xff;
p[0] = (x >> 56) & 0xff;
}
static void
be64enc_vect(unsigned char *dst, const uint64_t *src, size_t len)
{
size_t i;
for (i = 0; i < len / 8; i++) {
be64enc(dst + i * 8, src[i]);
STORE64_BE(dst + i * 8, src[i]);
}
}
@ -82,7 +53,7 @@ be64dec_vect(uint64_t *dst, const unsigned char *src, size_t len)
size_t i;
for (i = 0; i < len / 8; i++) {
dst[i] = be64dec(src + i * 8);
dst[i] = LOAD64_BE(src + i * 8);
}
}

View File

@ -77,6 +77,27 @@ store32_le(uint8_t dst[4], uint32_t w)
/* ----- */
#define LOAD64_BE(SRC) load64_be(SRC)
static inline uint64_t
load64_be(const uint8_t src[8])
{
#ifdef NATIVE_BIG_ENDIAN
uint64_t w;
memcpy(&w, src, sizeof w);
return w;
#else
uint64_t w = (uint64_t) src[7];
w |= (uint64_t) src[6] << 8;
w |= (uint64_t) src[5] << 16;
w |= (uint64_t) src[4] << 24;
w |= (uint64_t) src[3] << 32;
w |= (uint64_t) src[2] << 40;
w |= (uint64_t) src[1] << 48;
w |= (uint64_t) src[0] << 56;
return w;
#endif
}
#define LOAD32_BE(SRC) load32_be(SRC)
static inline uint32_t
load32_be(const uint8_t src[4])
@ -94,6 +115,24 @@ load32_be(const uint8_t src[4])
#endif
}
#define STORE64_BE(DST, W) store64_be((DST), (W))
static inline void
store64_be(uint8_t dst[8], uint64_t w)
{
#ifdef NATIVE_BIG_ENDIAN
memcpy(dst, &w, sizeof w);
#else
dst[7] = (uint8_t) w; w >>= 8;
dst[6] = (uint8_t) w; w >>= 8;
dst[5] = (uint8_t) w; w >>= 8;
dst[4] = (uint8_t) w; w >>= 8;
dst[3] = (uint8_t) w; w >>= 8;
dst[2] = (uint8_t) w; w >>= 8;
dst[1] = (uint8_t) w; w >>= 8;
dst[0] = (uint8_t) w;
#endif
}
#define STORE32_BE(DST, W) store32_be((DST), (W))
static inline void
store32_be(uint8_t dst[4], uint32_t w)