Hide store32()/load32() in the header

This commit is contained in:
Frank Denis 2016-02-26 13:22:33 +01:00
parent 49c57dfe0a
commit d9493834b2
2 changed files with 35 additions and 32 deletions

View File

@ -1,42 +1,10 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "core_hchacha20.h"
#include "crypto_core_hchacha20.h"
static inline uint32_t
load32(const void *src)
{
#ifdef NATIVE_LITTLE_ENDIAN
uint32_t w;
memcpy(&w, src, sizeof w);
return w;
#else
const uint8_t *p = (const uint8_t *) src;
uint32_t w = *p++;
w |= (uint32_t)(*p++) << 8;
w |= (uint32_t)(*p++) << 16;
w |= (uint32_t)(*p++) << 24;
return w;
#endif
}
static inline void
store32(void *dst, uint32_t w)
{
#ifdef NATIVE_LITTLE_ENDIAN
memcpy(dst, &w, sizeof w);
#else
uint8_t *p = (uint8_t *) dst;
*p++ = (uint8_t) w; w >>= 8;
*p++ = (uint8_t) w; w >>= 8;
*p++ = (uint8_t) w; w >>= 8;
*p++ = (uint8_t) w;
#endif
}
int
crypto_core_hchacha20(unsigned char *out, const unsigned char *in,
const unsigned char *k, const unsigned char *c)

View File

@ -1,6 +1,10 @@
#ifndef core_hchacha20_H
#define core_hchacha20_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define U8C(v) (v##U)
#define U32C(v) (v##U)
@ -21,4 +25,35 @@
c = PLUS(c, d); b = ROTATE(XOR(b, c), 7); \
} while(0)
static inline uint32_t
load32(const void *src)
{
#ifdef NATIVE_LITTLE_ENDIAN
uint32_t w;
memcpy(&w, src, sizeof w);
return w;
#else
const uint8_t *p = (const uint8_t *) src;
uint32_t w = *p++;
w |= (uint32_t)(*p++) << 8;
w |= (uint32_t)(*p++) << 16;
w |= (uint32_t)(*p++) << 24;
return w;
#endif
}
static inline void
store32(void *dst, uint32_t w)
{
#ifdef NATIVE_LITTLE_ENDIAN
memcpy(dst, &w, sizeof w);
#else
uint8_t *p = (uint8_t *) dst;
*p++ = (uint8_t) w; w >>= 8;
*p++ = (uint8_t) w; w >>= 8;
*p++ = (uint8_t) w; w >>= 8;
*p++ = (uint8_t) w;
#endif
}
#endif