Use a single way to do unaligned memory access/endianness conversion

This commit is contained in:
Frank Denis
2016-02-27 12:46:07 +01:00
parent d9493834b2
commit 6dc466ee8b
28 changed files with 439 additions and 905 deletions
+1 -2
View File
@@ -70,7 +70,6 @@ libsodium_la_SOURCES = \
crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.c \
crypto_pwhash/scryptsalsa208sha256/pbkdf2-sha256.h \
crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.c \
crypto_pwhash/scryptsalsa208sha256/sysendian.h \
crypto_pwhash/scryptsalsa208sha256/nosse/pwhash_scryptsalsa208sha256_nosse.c \
crypto_scalarmult/crypto_scalarmult.c \
crypto_scalarmult/curve25519/scalarmult_curve25519.c \
@@ -103,6 +102,7 @@ libsodium_la_SOURCES = \
crypto_verify/64/verify_64_api.c \
crypto_verify/64/ref/verify_64.c \
randombytes/randombytes.c \
sodium/common.h \
sodium/core.c \
sodium/runtime.c \
sodium/utils.c \
@@ -179,7 +179,6 @@ libsodium_la_SOURCES += \
crypto_stream/aes128ctr/stream_aes128ctr_api.c \
crypto_stream/aes128ctr/portable/beforenm_aes128ctr.c \
crypto_stream/aes128ctr/portable/common.h \
crypto_stream/aes128ctr/portable/common_aes128ctr.c \
crypto_stream/aes128ctr/portable/consts.h \
crypto_stream/aes128ctr/portable/consts_aes128ctr.c \
crypto_stream/aes128ctr/portable/int128.h \
@@ -4,6 +4,7 @@
#include "core_hchacha20.h"
#include "crypto_core_hchacha20.h"
#include "../../sodium/common.h"
int
crypto_core_hchacha20(unsigned char *out, const unsigned char *in,
@@ -19,23 +20,23 @@ crypto_core_hchacha20(unsigned char *out, const unsigned char *in,
x2 = U32C(0x79622d32);
x3 = U32C(0x6b206574);
} else {
x0 = load32(c + 0);
x1 = load32(c + 4);
x2 = load32(c + 8);
x3 = load32(c + 12);
x0 = LOAD32_LE(c + 0);
x1 = LOAD32_LE(c + 4);
x2 = LOAD32_LE(c + 8);
x3 = LOAD32_LE(c + 12);
}
x4 = load32(k + 0);
x5 = load32(k + 4);
x6 = load32(k + 8);
x7 = load32(k + 12);
x8 = load32(k + 16);
x9 = load32(k + 20);
x10 = load32(k + 24);
x11 = load32(k + 28);
x12 = load32(in + 0);
x13 = load32(in + 4);
x14 = load32(in + 8);
x15 = load32(in + 12);
x4 = LOAD32_LE(k + 0);
x5 = LOAD32_LE(k + 4);
x6 = LOAD32_LE(k + 8);
x7 = LOAD32_LE(k + 12);
x8 = LOAD32_LE(k + 16);
x9 = LOAD32_LE(k + 20);
x10 = LOAD32_LE(k + 24);
x11 = LOAD32_LE(k + 28);
x12 = LOAD32_LE(in + 0);
x13 = LOAD32_LE(in + 4);
x14 = LOAD32_LE(in + 8);
x15 = LOAD32_LE(in + 12);
for (i = 0; i < 10; i++) {
QUARTERROUND(x0, x4, x8, x12);
@@ -48,14 +49,14 @@ crypto_core_hchacha20(unsigned char *out, const unsigned char *in,
QUARTERROUND(x3, x4, x9, x14);
}
store32(out + 0, x0);
store32(out + 4, x1);
store32(out + 8, x2);
store32(out + 12, x3);
store32(out + 16, x12);
store32(out + 20, x13);
store32(out + 24, x14);
store32(out + 28, x15);
STORE32_LE(out + 0, x0);
STORE32_LE(out + 4, x1);
STORE32_LE(out + 8, x2);
STORE32_LE(out + 12, x3);
STORE32_LE(out + 16, x12);
STORE32_LE(out + 20, x13);
STORE32_LE(out + 24, x14);
STORE32_LE(out + 28, x15);
return 0;
}
@@ -25,35 +25,4 @@
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
@@ -5,6 +5,7 @@ Public domain.
*/
#include "crypto_core_hsalsa20.h"
#include "../../sodium/common.h"
#define ROUNDS 20
@@ -15,24 +16,6 @@ static uint32 rotate(uint32 u,int c)
return (u << c) | (u >> (32 - c));
}
static uint32 load_littleendian(const unsigned char *x)
{
return
(uint32) (x[0]) \
| (((uint32) (x[1])) << 8) \
| (((uint32) (x[2])) << 16) \
| (((uint32) (x[3])) << 24)
;
}
static void store_littleendian(unsigned char *x,uint32 u)
{
x[0] = u; u >>= 8;
x[1] = u; u >>= 8;
x[2] = u; u >>= 8;
x[3] = u;
}
int crypto_core_hsalsa20(
unsigned char *out,
const unsigned char *in,
@@ -43,22 +26,22 @@ int crypto_core_hsalsa20(
uint32 x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
int i;
x0 = load_littleendian(c + 0);
x1 = load_littleendian(k + 0);
x2 = load_littleendian(k + 4);
x3 = load_littleendian(k + 8);
x4 = load_littleendian(k + 12);
x5 = load_littleendian(c + 4);
x6 = load_littleendian(in + 0);
x7 = load_littleendian(in + 4);
x8 = load_littleendian(in + 8);
x9 = load_littleendian(in + 12);
x10 = load_littleendian(c + 8);
x11 = load_littleendian(k + 16);
x12 = load_littleendian(k + 20);
x13 = load_littleendian(k + 24);
x14 = load_littleendian(k + 28);
x15 = load_littleendian(c + 12);
x0 = LOAD32_LE(c + 0);
x1 = LOAD32_LE(k + 0);
x2 = LOAD32_LE(k + 4);
x3 = LOAD32_LE(k + 8);
x4 = LOAD32_LE(k + 12);
x5 = LOAD32_LE(c + 4);
x6 = LOAD32_LE(in + 0);
x7 = LOAD32_LE(in + 4);
x8 = LOAD32_LE(in + 8);
x9 = LOAD32_LE(in + 12);
x10 = LOAD32_LE(c + 8);
x11 = LOAD32_LE(k + 16);
x12 = LOAD32_LE(k + 20);
x13 = LOAD32_LE(k + 24);
x14 = LOAD32_LE(k + 28);
x15 = LOAD32_LE(c + 12);
for (i = ROUNDS;i > 0;i -= 2) {
x4 ^= rotate( x0+x12, 7);
@@ -95,14 +78,14 @@ int crypto_core_hsalsa20(
x15 ^= rotate(x14+x13,18);
}
store_littleendian(out + 0,x0);
store_littleendian(out + 4,x5);
store_littleendian(out + 8,x10);
store_littleendian(out + 12,x15);
store_littleendian(out + 16,x6);
store_littleendian(out + 20,x7);
store_littleendian(out + 24,x8);
store_littleendian(out + 28,x9);
STORE32_LE(out + 0,x0);
STORE32_LE(out + 4,x5);
STORE32_LE(out + 8,x10);
STORE32_LE(out + 12,x15);
STORE32_LE(out + 16,x6);
STORE32_LE(out + 20,x7);
STORE32_LE(out + 24,x8);
STORE32_LE(out + 28,x9);
return 0;
}
@@ -5,6 +5,7 @@ Public domain.
*/
#include "crypto_core_salsa20.h"
#include "../../sodium/common.h"
#define ROUNDS 20
@@ -15,24 +16,6 @@ static uint32 rotate(uint32 u,int c)
return (u << c) | (u >> (32 - c));
}
static uint32 load_littleendian(const unsigned char *x)
{
return
(uint32) (x[0]) \
| (((uint32) (x[1])) << 8) \
| (((uint32) (x[2])) << 16) \
| (((uint32) (x[3])) << 24)
;
}
static void store_littleendian(unsigned char *x,uint32 u)
{
x[0] = u; u >>= 8;
x[1] = u; u >>= 8;
x[2] = u; u >>= 8;
x[3] = u;
}
int crypto_core_salsa20(
unsigned char *out,
const unsigned char *in,
@@ -44,22 +27,22 @@ int crypto_core_salsa20(
uint32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
int i;
j0 = x0 = load_littleendian(c + 0);
j1 = x1 = load_littleendian(k + 0);
j2 = x2 = load_littleendian(k + 4);
j3 = x3 = load_littleendian(k + 8);
j4 = x4 = load_littleendian(k + 12);
j5 = x5 = load_littleendian(c + 4);
j6 = x6 = load_littleendian(in + 0);
j7 = x7 = load_littleendian(in + 4);
j8 = x8 = load_littleendian(in + 8);
j9 = x9 = load_littleendian(in + 12);
j10 = x10 = load_littleendian(c + 8);
j11 = x11 = load_littleendian(k + 16);
j12 = x12 = load_littleendian(k + 20);
j13 = x13 = load_littleendian(k + 24);
j14 = x14 = load_littleendian(k + 28);
j15 = x15 = load_littleendian(c + 12);
j0 = x0 = LOAD32_LE(c + 0);
j1 = x1 = LOAD32_LE(k + 0);
j2 = x2 = LOAD32_LE(k + 4);
j3 = x3 = LOAD32_LE(k + 8);
j4 = x4 = LOAD32_LE(k + 12);
j5 = x5 = LOAD32_LE(c + 4);
j6 = x6 = LOAD32_LE(in + 0);
j7 = x7 = LOAD32_LE(in + 4);
j8 = x8 = LOAD32_LE(in + 8);
j9 = x9 = LOAD32_LE(in + 12);
j10 = x10 = LOAD32_LE(c + 8);
j11 = x11 = LOAD32_LE(k + 16);
j12 = x12 = LOAD32_LE(k + 20);
j13 = x13 = LOAD32_LE(k + 24);
j14 = x14 = LOAD32_LE(k + 28);
j15 = x15 = LOAD32_LE(c + 12);
for (i = ROUNDS;i > 0;i -= 2) {
x4 ^= rotate( x0+x12, 7);
@@ -113,22 +96,22 @@ int crypto_core_salsa20(
x14 += j14;
x15 += j15;
store_littleendian(out + 0,x0);
store_littleendian(out + 4,x1);
store_littleendian(out + 8,x2);
store_littleendian(out + 12,x3);
store_littleendian(out + 16,x4);
store_littleendian(out + 20,x5);
store_littleendian(out + 24,x6);
store_littleendian(out + 28,x7);
store_littleendian(out + 32,x8);
store_littleendian(out + 36,x9);
store_littleendian(out + 40,x10);
store_littleendian(out + 44,x11);
store_littleendian(out + 48,x12);
store_littleendian(out + 52,x13);
store_littleendian(out + 56,x14);
store_littleendian(out + 60,x15);
STORE32_LE(out + 0,x0);
STORE32_LE(out + 4,x1);
STORE32_LE(out + 8,x2);
STORE32_LE(out + 12,x3);
STORE32_LE(out + 16,x4);
STORE32_LE(out + 20,x5);
STORE32_LE(out + 24,x6);
STORE32_LE(out + 28,x7);
STORE32_LE(out + 32,x8);
STORE32_LE(out + 36,x9);
STORE32_LE(out + 40,x10);
STORE32_LE(out + 44,x11);
STORE32_LE(out + 48,x12);
STORE32_LE(out + 52,x13);
STORE32_LE(out + 56,x14);
STORE32_LE(out + 60,x15);
return 0;
}
@@ -5,6 +5,7 @@ Public domain.
*/
#include "crypto_core_salsa2012.h"
#include "../../sodium/common.h"
#define ROUNDS 12
@@ -15,24 +16,6 @@ static uint32 rotate(uint32 u,int c)
return (u << c) | (u >> (32 - c));
}
static uint32 load_littleendian(const unsigned char *x)
{
return
(uint32) (x[0]) \
| (((uint32) (x[1])) << 8) \
| (((uint32) (x[2])) << 16) \
| (((uint32) (x[3])) << 24)
;
}
static void store_littleendian(unsigned char *x,uint32 u)
{
x[0] = u; u >>= 8;
x[1] = u; u >>= 8;
x[2] = u; u >>= 8;
x[3] = u;
}
int crypto_core_salsa2012(
unsigned char *out,
const unsigned char *in,
@@ -44,22 +27,22 @@ int crypto_core_salsa2012(
uint32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
int i;
j0 = x0 = load_littleendian(c + 0);
j1 = x1 = load_littleendian(k + 0);
j2 = x2 = load_littleendian(k + 4);
j3 = x3 = load_littleendian(k + 8);
j4 = x4 = load_littleendian(k + 12);
j5 = x5 = load_littleendian(c + 4);
j6 = x6 = load_littleendian(in + 0);
j7 = x7 = load_littleendian(in + 4);
j8 = x8 = load_littleendian(in + 8);
j9 = x9 = load_littleendian(in + 12);
j10 = x10 = load_littleendian(c + 8);
j11 = x11 = load_littleendian(k + 16);
j12 = x12 = load_littleendian(k + 20);
j13 = x13 = load_littleendian(k + 24);
j14 = x14 = load_littleendian(k + 28);
j15 = x15 = load_littleendian(c + 12);
j0 = x0 = LOAD32_LE(c + 0);
j1 = x1 = LOAD32_LE(k + 0);
j2 = x2 = LOAD32_LE(k + 4);
j3 = x3 = LOAD32_LE(k + 8);
j4 = x4 = LOAD32_LE(k + 12);
j5 = x5 = LOAD32_LE(c + 4);
j6 = x6 = LOAD32_LE(in + 0);
j7 = x7 = LOAD32_LE(in + 4);
j8 = x8 = LOAD32_LE(in + 8);
j9 = x9 = LOAD32_LE(in + 12);
j10 = x10 = LOAD32_LE(c + 8);
j11 = x11 = LOAD32_LE(k + 16);
j12 = x12 = LOAD32_LE(k + 20);
j13 = x13 = LOAD32_LE(k + 24);
j14 = x14 = LOAD32_LE(k + 28);
j15 = x15 = LOAD32_LE(c + 12);
for (i = ROUNDS;i > 0;i -= 2) {
x4 ^= rotate( x0+x12, 7);
@@ -113,22 +96,22 @@ int crypto_core_salsa2012(
x14 += j14;
x15 += j15;
store_littleendian(out + 0,x0);
store_littleendian(out + 4,x1);
store_littleendian(out + 8,x2);
store_littleendian(out + 12,x3);
store_littleendian(out + 16,x4);
store_littleendian(out + 20,x5);
store_littleendian(out + 24,x6);
store_littleendian(out + 28,x7);
store_littleendian(out + 32,x8);
store_littleendian(out + 36,x9);
store_littleendian(out + 40,x10);
store_littleendian(out + 44,x11);
store_littleendian(out + 48,x12);
store_littleendian(out + 52,x13);
store_littleendian(out + 56,x14);
store_littleendian(out + 60,x15);
STORE32_LE(out + 0,x0);
STORE32_LE(out + 4,x1);
STORE32_LE(out + 8,x2);
STORE32_LE(out + 12,x3);
STORE32_LE(out + 16,x4);
STORE32_LE(out + 20,x5);
STORE32_LE(out + 24,x6);
STORE32_LE(out + 28,x7);
STORE32_LE(out + 32,x8);
STORE32_LE(out + 36,x9);
STORE32_LE(out + 40,x10);
STORE32_LE(out + 44,x11);
STORE32_LE(out + 48,x12);
STORE32_LE(out + 52,x13);
STORE32_LE(out + 56,x14);
STORE32_LE(out + 60,x15);
return 0;
}
@@ -5,6 +5,7 @@ Public domain.
*/
#include "crypto_core_salsa208.h"
#include "../../sodium/common.h"
#define ROUNDS 8
@@ -15,24 +16,6 @@ static uint32 rotate(uint32 u,int c)
return (u << c) | (u >> (32 - c));
}
static uint32 load_littleendian(const unsigned char *x)
{
return
(uint32) (x[0]) \
| (((uint32) (x[1])) << 8) \
| (((uint32) (x[2])) << 16) \
| (((uint32) (x[3])) << 24)
;
}
static void store_littleendian(unsigned char *x,uint32 u)
{
x[0] = u; u >>= 8;
x[1] = u; u >>= 8;
x[2] = u; u >>= 8;
x[3] = u;
}
int crypto_core_salsa208(
unsigned char *out,
const unsigned char *in,
@@ -44,22 +27,22 @@ int crypto_core_salsa208(
uint32 j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
int i;
j0 = x0 = load_littleendian(c + 0);
j1 = x1 = load_littleendian(k + 0);
j2 = x2 = load_littleendian(k + 4);
j3 = x3 = load_littleendian(k + 8);
j4 = x4 = load_littleendian(k + 12);
j5 = x5 = load_littleendian(c + 4);
j6 = x6 = load_littleendian(in + 0);
j7 = x7 = load_littleendian(in + 4);
j8 = x8 = load_littleendian(in + 8);
j9 = x9 = load_littleendian(in + 12);
j10 = x10 = load_littleendian(c + 8);
j11 = x11 = load_littleendian(k + 16);
j12 = x12 = load_littleendian(k + 20);
j13 = x13 = load_littleendian(k + 24);
j14 = x14 = load_littleendian(k + 28);
j15 = x15 = load_littleendian(c + 12);
j0 = x0 = LOAD32_LE(c + 0);
j1 = x1 = LOAD32_LE(k + 0);
j2 = x2 = LOAD32_LE(k + 4);
j3 = x3 = LOAD32_LE(k + 8);
j4 = x4 = LOAD32_LE(k + 12);
j5 = x5 = LOAD32_LE(c + 4);
j6 = x6 = LOAD32_LE(in + 0);
j7 = x7 = LOAD32_LE(in + 4);
j8 = x8 = LOAD32_LE(in + 8);
j9 = x9 = LOAD32_LE(in + 12);
j10 = x10 = LOAD32_LE(c + 8);
j11 = x11 = LOAD32_LE(k + 16);
j12 = x12 = LOAD32_LE(k + 20);
j13 = x13 = LOAD32_LE(k + 24);
j14 = x14 = LOAD32_LE(k + 28);
j15 = x15 = LOAD32_LE(c + 12);
for (i = ROUNDS;i > 0;i -= 2) {
x4 ^= rotate( x0+x12, 7);
@@ -113,22 +96,22 @@ int crypto_core_salsa208(
x14 += j14;
x15 += j15;
store_littleendian(out + 0,x0);
store_littleendian(out + 4,x1);
store_littleendian(out + 8,x2);
store_littleendian(out + 12,x3);
store_littleendian(out + 16,x4);
store_littleendian(out + 20,x5);
store_littleendian(out + 24,x6);
store_littleendian(out + 28,x7);
store_littleendian(out + 32,x8);
store_littleendian(out + 36,x9);
store_littleendian(out + 40,x10);
store_littleendian(out + 44,x11);
store_littleendian(out + 48,x12);
store_littleendian(out + 52,x13);
store_littleendian(out + 56,x14);
store_littleendian(out + 60,x15);
STORE32_LE(out + 0,x0);
STORE32_LE(out + 4,x1);
STORE32_LE(out + 8,x2);
STORE32_LE(out + 12,x3);
STORE32_LE(out + 16,x4);
STORE32_LE(out + 20,x5);
STORE32_LE(out + 24,x6);
STORE32_LE(out + 28,x7);
STORE32_LE(out + 32,x8);
STORE32_LE(out + 36,x9);
STORE32_LE(out + 40,x10);
STORE32_LE(out + 44,x11);
STORE32_LE(out + 48,x12);
STORE32_LE(out + 52,x13);
STORE32_LE(out + 56,x14);
STORE32_LE(out + 60,x15);
return 0;
}
@@ -19,95 +19,6 @@
#include "utils.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 uint64_t load64( const void *src )
{
#ifdef NATIVE_LITTLE_ENDIAN
uint64_t w;
memcpy(&w, src, sizeof w);
return w;
#else
const uint8_t *p = ( const uint8_t * )src;
uint64_t w = *p++;
w |= ( uint64_t )( *p++ ) << 8;
w |= ( uint64_t )( *p++ ) << 16;
w |= ( uint64_t )( *p++ ) << 24;
w |= ( uint64_t )( *p++ ) << 32;
w |= ( uint64_t )( *p++ ) << 40;
w |= ( uint64_t )( *p++ ) << 48;
w |= ( uint64_t )( *p++ ) << 56;
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
}
static inline void store64( void *dst, uint64_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; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8;
*p++ = ( uint8_t )w;
#endif
}
static inline uint64_t load48( const void *src )
{
const uint8_t *p = ( const uint8_t * )src;
uint64_t w = *p++;
w |= ( uint64_t )( *p++ ) << 8;
w |= ( uint64_t )( *p++ ) << 16;
w |= ( uint64_t )( *p++ ) << 24;
w |= ( uint64_t )( *p++ ) << 32;
w |= ( uint64_t )( *p++ ) << 40;
return w;
}
static inline void store48( void *dst, uint64_t w )
{
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; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8;
*p++ = ( uint8_t )w;
}
static inline uint32_t rotl32( const uint32_t w, const unsigned c )
{
return ( w << c ) | ( w >> ( 32 - c ) );
@@ -65,7 +65,7 @@ extern "C" {
uint8_t key_length; // 2
uint8_t fanout; // 3
uint8_t depth; // 4
uint32_t leaf_length; // 8
uint8_t leaf_length[4]; // 8
uint8_t node_offset[6];// 14
uint8_t node_depth; // 15
uint8_t inner_length; // 16
@@ -90,8 +90,8 @@ CRYPTO_ALIGN( 64 ) typedef struct blake2s_state_
uint8_t key_length; // 2
uint8_t fanout; // 3
uint8_t depth; // 4
uint32_t leaf_length; // 8
uint64_t node_offset; // 16
uint8_t leaf_length[4]; // 8
uint8_t node_offset[8]; // 16
uint8_t node_depth; // 17
uint8_t inner_length; // 18
uint8_t reserved[14]; // 32
@@ -4,6 +4,7 @@
#include "blake2.h"
#include "blake2-impl.h"
#include "../../sodium/common.h"
static const uint64_t blake2b_IV[8] =
{
@@ -36,7 +37,7 @@ int blake2b_compress_ref( blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYT
int i;
for( i = 0; i < 16; ++i )
m[i] = load64( block + i * sizeof( m[i] ) );
m[i] = LOAD64_LE( block + i * sizeof( m[i] ) );
for( i = 0; i < 8; ++i )
v[i] = S->h[i];
@@ -19,6 +19,7 @@
#include "blake2.h"
#include "blake2-impl.h"
#include "runtime.h"
#include "../../sodium/common.h"
#ifdef HAVE_TI_MODE
# if defined(__SIZEOF_INT128__)
@@ -105,13 +106,13 @@ static inline int blake2b_param_set_max_depth( blake2b_param *P, const uint8_t d
static inline int blake2b_param_set_leaf_length( blake2b_param *P, const uint32_t leaf_length )
{
store32( &P->leaf_length, leaf_length );
STORE32_LE( P->leaf_length, leaf_length );
return 0;
}
static inline int blake2b_param_set_node_offset( blake2b_param *P, const uint64_t node_offset )
{
store64( &P->node_offset, node_offset );
STORE64_LE( P->node_offset, node_offset );
return 0;
}
@@ -160,7 +161,7 @@ int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
/* IV XOR ParamBlock */
for( i = 0; i < 8; ++i )
S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
S->h[i] ^= LOAD64_LE( p + sizeof( S->h[i] ) * i );
return 0;
}
@@ -175,8 +176,8 @@ int blake2b_init( blake2b_state *S, const uint8_t outlen )
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store64( &P->node_offset, 0 );
STORE32_LE( P->leaf_length, 0 );
STORE64_LE( P->node_offset, 0 );
P->node_depth = 0;
P->inner_length = 0;
memset( P->reserved, 0, sizeof( P->reserved ) );
@@ -196,8 +197,8 @@ int blake2b_init_salt_personal( blake2b_state *S, const uint8_t outlen,
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store64( &P->node_offset, 0 );
STORE32_LE( P->leaf_length, 0 );
STORE64_LE( P->node_offset, 0 );
P->node_depth = 0;
P->inner_length = 0;
memset( P->reserved, 0, sizeof( P->reserved ) );
@@ -226,8 +227,8 @@ int blake2b_init_key( blake2b_state *S, const uint8_t outlen, const void *key, c
P->key_length = keylen;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store64( &P->node_offset, 0 );
STORE32_LE( P->leaf_length, 0 );
STORE64_LE( P->node_offset, 0 );
P->node_depth = 0;
P->inner_length = 0;
memset( P->reserved, 0, sizeof( P->reserved ) );
@@ -259,8 +260,8 @@ int blake2b_init_key_salt_personal( blake2b_state *S, const uint8_t outlen, cons
P->key_length = keylen;
P->fanout = 1;
P->depth = 1;
store32( &P->leaf_length, 0 );
store64( &P->node_offset, 0 );
STORE32_LE( P->leaf_length, 0 );
STORE64_LE( P->node_offset, 0 );
P->node_depth = 0;
P->inner_length = 0;
memset( P->reserved, 0, sizeof( P->reserved ) );
@@ -345,7 +346,7 @@ int blake2b_final( blake2b_state *S, uint8_t *out, uint8_t outlen )
int i;
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
STORE64_LE( buffer + sizeof( S->h[i] ) * i, S->h[i] );
memcpy( out, buffer, outlen );
}
#endif
@@ -10,6 +10,8 @@
# define POLY1305_NOINLINE
#endif
#include "../../../sodium/common.h"
#define poly1305_block_size 16
/* 17 + sizeof(unsigned long long) + 14*sizeof(unsigned long) */
@@ -22,36 +24,15 @@ typedef struct poly1305_state_internal_t {
unsigned char final;
} poly1305_state_internal_t;
/* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */
static unsigned long
U8TO32(const unsigned char *p)
{
return
(((unsigned long)(p[0] & 0xff) ) |
((unsigned long)(p[1] & 0xff) << 8) |
((unsigned long)(p[2] & 0xff) << 16) |
((unsigned long)(p[3] & 0xff) << 24));
}
/* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */
static void
U32TO8(unsigned char *p, unsigned long v)
{
p[0] = (v ) & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = (v >> 16) & 0xff;
p[3] = (v >> 24) & 0xff;
}
static void
poly1305_init(poly1305_state_internal_t *st, const unsigned char key[32])
{
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff;
st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03;
st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff;
st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff;
st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff;
st->r[0] = (LOAD32_LE(&key[ 0]) ) & 0x3ffffff;
st->r[1] = (LOAD32_LE(&key[ 3]) >> 2) & 0x3ffff03;
st->r[2] = (LOAD32_LE(&key[ 6]) >> 4) & 0x3ffc0ff;
st->r[3] = (LOAD32_LE(&key[ 9]) >> 6) & 0x3f03fff;
st->r[4] = (LOAD32_LE(&key[12]) >> 8) & 0x00fffff;
/* h = 0 */
st->h[0] = 0;
@@ -61,10 +42,10 @@ poly1305_init(poly1305_state_internal_t *st, const unsigned char key[32])
st->h[4] = 0;
/* save pad for later */
st->pad[0] = U8TO32(&key[16]);
st->pad[1] = U8TO32(&key[20]);
st->pad[2] = U8TO32(&key[24]);
st->pad[3] = U8TO32(&key[28]);
st->pad[0] = LOAD32_LE(&key[16]);
st->pad[1] = LOAD32_LE(&key[20]);
st->pad[2] = LOAD32_LE(&key[24]);
st->pad[3] = LOAD32_LE(&key[28]);
st->leftover = 0;
st->final = 0;
@@ -99,11 +80,11 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, unsigned
while (bytes >= poly1305_block_size) {
/* h += m[i] */
h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff;
h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff;
h4 += (U8TO32(m+12) >> 8) | hibit;
h0 += (LOAD32_LE(m+ 0) ) & 0x3ffffff;
h1 += (LOAD32_LE(m+ 3) >> 2) & 0x3ffffff;
h2 += (LOAD32_LE(m+ 6) >> 4) & 0x3ffffff;
h3 += (LOAD32_LE(m+ 9) >> 6) & 0x3ffffff;
h4 += (LOAD32_LE(m+12) >> 8) | hibit;
/* h *= r */
d0 = ((unsigned long long)h0 * r0) + ((unsigned long long)h1 * s4) + ((unsigned long long)h2 * s3) + ((unsigned long long)h3 * s2) + ((unsigned long long)h4 * s1);
@@ -197,10 +178,10 @@ poly1305_finish(poly1305_state_internal_t *st, unsigned char mac[16])
f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f;
f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f;
U32TO8(mac + 0, h0);
U32TO8(mac + 4, h1);
U32TO8(mac + 8, h2);
U32TO8(mac + 12, h3);
STORE32_LE(mac + 0, h0);
STORE32_LE(mac + 4, h1);
STORE32_LE(mac + 8, h2);
STORE32_LE(mac + 12, h3);
/* zero out the state */
sodium_memzero((void *)st, sizeof *st);
@@ -22,6 +22,8 @@ typedef unsigned uint128_t __attribute__ ((mode(TI)));
# define POLY1305_NOINLINE
#endif
#include "../../../sodium/common.h"
#define poly1305_block_size 16
/* 17 + sizeof(unsigned long long) + 8*sizeof(unsigned long long) */
@@ -34,43 +36,14 @@ typedef struct poly1305_state_internal_t {
unsigned char final;
} poly1305_state_internal_t;
/* interpret eight 8 bit unsigned integers as a 64 bit unsigned integer in little endian */
static unsigned long long
U8TO64(const unsigned char *p)
{
return
(((unsigned long long)(p[0] & 0xff) ) |
((unsigned long long)(p[1] & 0xff) << 8) |
((unsigned long long)(p[2] & 0xff) << 16) |
((unsigned long long)(p[3] & 0xff) << 24) |
((unsigned long long)(p[4] & 0xff) << 32) |
((unsigned long long)(p[5] & 0xff) << 40) |
((unsigned long long)(p[6] & 0xff) << 48) |
((unsigned long long)(p[7] & 0xff) << 56));
}
/* store a 64 bit unsigned integer as eight 8 bit unsigned integers in little endian */
static void
U64TO8(unsigned char *p, unsigned long long v)
{
p[0] = (v ) & 0xff;
p[1] = (v >> 8) & 0xff;
p[2] = (v >> 16) & 0xff;
p[3] = (v >> 24) & 0xff;
p[4] = (v >> 32) & 0xff;
p[5] = (v >> 40) & 0xff;
p[6] = (v >> 48) & 0xff;
p[7] = (v >> 56) & 0xff;
}
static void
poly1305_init(poly1305_state_internal_t *st, const unsigned char key[32])
{
unsigned long long t0,t1;
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
t0 = U8TO64(&key[0]);
t1 = U8TO64(&key[8]);
t0 = LOAD64_LE(&key[0]);
t1 = LOAD64_LE(&key[8]);
st->r[0] = ( t0 ) & 0xffc0fffffff;
st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
@@ -82,8 +55,8 @@ poly1305_init(poly1305_state_internal_t *st, const unsigned char key[32])
st->h[2] = 0;
/* save pad for later */
st->pad[0] = U8TO64(&key[16]);
st->pad[1] = U8TO64(&key[24]);
st->pad[0] = LOAD64_LE(&key[16]);
st->pad[1] = LOAD64_LE(&key[24]);
st->leftover = 0;
st->final = 0;
@@ -114,8 +87,8 @@ poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, unsigned
unsigned long long t0,t1;
/* h += m[i] */
t0 = U8TO64(&m[0]);
t1 = U8TO64(&m[8]);
t0 = LOAD64_LE(&m[0]);
t1 = LOAD64_LE(&m[8]);
h0 += (( t0 ) & 0xfffffffffff);
h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
@@ -200,8 +173,8 @@ poly1305_finish(poly1305_state_internal_t *st, unsigned char mac[16])
h0 = ((h0 ) | (h1 << 44));
h1 = ((h1 >> 20) | (h2 << 24));
U64TO8(&mac[0], h0);
U64TO8(&mac[8], h1);
STORE64_LE(&mac[0], h0);
STORE64_LE(&mac[8], h1);
/* zero out the state */
sodium_memzero((void *)st, sizeof *st);
@@ -23,6 +23,7 @@
#include "crypto_generichash_blake2b.h"
#include "runtime.h"
#include "utils.h"
#include "../../sodium/common.h"
#include "argon2-core.h"
#include "argon2-impl.h"
@@ -53,14 +54,14 @@ void xor_block(block *dst, const block *src) {
static void load_block(block *dst, const void *input) {
unsigned i;
for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
dst->v[i] = load64((const uint8_t *)input + i * sizeof(dst->v[i]));
dst->v[i] = LOAD64_LE((const uint8_t *)input + i * sizeof(dst->v[i]));
}
}
static void store_block(void *output, const block *src) {
unsigned i;
for (i = 0; i < ARGON2_QWORDS_IN_BLOCK; ++i) {
store64((uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
STORE64_LE((uint8_t *)output + i * sizeof(src->v[i]), src->v[i]);
}
}
@@ -423,14 +424,14 @@ void fill_first_blocks(uint8_t *blockhash, const argon2_instance_t *instance) {
uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
for (l = 0; l < instance->lanes; ++l) {
store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
STORE32_LE(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
STORE32_LE(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
ARGON2_PREHASH_SEED_LENGTH);
load_block(&instance->region->memory[l * instance->lane_length + 0],
blockhash_bytes);
store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
STORE32_LE(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);
blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE, blockhash,
ARGON2_PREHASH_SEED_LENGTH);
load_block(&instance->region->memory[l * instance->lane_length + 1],
@@ -451,26 +452,26 @@ void initial_hash(uint8_t *blockhash, argon2_context *context,
crypto_generichash_blake2b_init(&BlakeHash, NULL, 0U,
ARGON2_PREHASH_DIGEST_LENGTH);
store32(&value, context->lanes);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, context->lanes);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
store32(&value, context->outlen);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, context->outlen);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
store32(&value, context->m_cost);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, context->m_cost);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
store32(&value, context->t_cost);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, context->t_cost);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
store32(&value, ARGON2_VERSION_NUMBER);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, ARGON2_VERSION_NUMBER);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
store32(&value, (uint32_t)type);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, (uint32_t)type);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
store32(&value, context->pwdlen);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, context->pwdlen);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
if (context->pwd != NULL) {
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)context->pwd,
@@ -482,16 +483,16 @@ void initial_hash(uint8_t *blockhash, argon2_context *context,
}
}
store32(&value, context->saltlen);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, context->saltlen);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
if (context->salt != NULL) {
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)context->salt,
context->saltlen);
}
store32(&value, context->secretlen);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, context->secretlen);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
if (context->secret != NULL) {
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)context->secret,
@@ -503,8 +504,8 @@ void initial_hash(uint8_t *blockhash, argon2_context *context,
}
}
store32(&value, context->adlen);
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)&value, sizeof(value));
STORE32_LE(value, context->adlen);
crypto_generichash_blake2b_update(&BlakeHash, value, sizeof(value));
if (context->ad != NULL) {
crypto_generichash_blake2b_update(&BlakeHash, (const uint8_t *)context->ad,
@@ -17,95 +17,6 @@
#include <stdint.h>
#include <string.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 uint64_t load64( const void *src )
{
#ifdef NATIVE_LITTLE_ENDIAN
uint64_t w;
memcpy(&w, src, sizeof w);
return w;
#else
const uint8_t *p = ( const uint8_t * )src;
uint64_t w = *p++;
w |= ( uint64_t )( *p++ ) << 8;
w |= ( uint64_t )( *p++ ) << 16;
w |= ( uint64_t )( *p++ ) << 24;
w |= ( uint64_t )( *p++ ) << 32;
w |= ( uint64_t )( *p++ ) << 40;
w |= ( uint64_t )( *p++ ) << 48;
w |= ( uint64_t )( *p++ ) << 56;
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
}
static inline void store64( void *dst, uint64_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; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8;
*p++ = ( uint8_t )w;
#endif
}
static inline uint64_t load48( const void *src )
{
const uint8_t *p = ( const uint8_t * )src;
uint64_t w = *p++;
w |= ( uint64_t )( *p++ ) << 8;
w |= ( uint64_t )( *p++ ) << 16;
w |= ( uint64_t )( *p++ ) << 24;
w |= ( uint64_t )( *p++ ) << 32;
w |= ( uint64_t )( *p++ ) << 40;
return w;
}
static inline void store48( void *dst, uint64_t w )
{
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; w >>= 8;
*p++ = ( uint8_t )w; w >>= 8;
*p++ = ( uint8_t )w;
}
static inline uint32_t rotl32( const uint32_t w, const unsigned c )
{
return ( w << c ) | ( w >> ( 32 - c ) );
@@ -5,6 +5,7 @@
#include "crypto_generichash_blake2b.h"
#include "utils.h"
#include "../../sodium/common.h"
#include "argon2-impl.h"
@@ -19,7 +20,7 @@ int blake2b_long(void *pout, size_t outlen, const void *in, size_t inlen) {
}
/* Ensure little-endian byte order! */
store32(outlen_bytes, (uint32_t)outlen);
STORE32_LE(outlen_bytes, (uint32_t)outlen);
#define TRY(statement) \
do { \
@@ -35,8 +35,8 @@
#include <string.h>
#include "../pbkdf2-sha256.h"
#include "../sysendian.h"
#include "../crypto_scrypt.h"
#include "../../../sodium/common.h"
static inline void
blkcpy_64(escrypt_block_t *dest, const escrypt_block_t *src)
@@ -207,7 +207,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
/* 1: X <-- B */
for (k = 0; k < 32 * r; k++)
X[k] = le32dec(&B[4 * k]);
X[k] = LOAD32_LE(&B[4 * k]);
/* 2: for i = 0 to N - 1 do */
for (i = 0; i < N; i += 2) {
@@ -242,7 +242,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
}
/* 10: B' <-- X */
for (k = 0; k < 32 * r; k++)
le32enc(&B[4 * k], X[k]);
STORE32_LE(&B[4 * k], X[k]);
}
/**
@@ -32,8 +32,8 @@
#include "crypto_auth_hmacsha256.h"
#include "pbkdf2-sha256.h"
#include "sysendian.h"
#include "utils.h"
#include "../../sodium/common.h"
/**
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
@@ -60,7 +60,7 @@ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
crypto_auth_hmacsha256_update(&PShctx, salt, saltlen);
for (i = 0; i * 32 < dkLen; i++) {
be32enc(ivec, (uint32_t)(i + 1));
STORE32_BE(ivec, (uint32_t)(i + 1));
memcpy(&hctx, &PShctx, sizeof(crypto_auth_hmacsha256_state));
crypto_auth_hmacsha256_update(&hctx, ivec, 4);
crypto_auth_hmacsha256_final(&hctx, U);
@@ -45,8 +45,8 @@
#include <string.h>
#include "../pbkdf2-sha256.h"
#include "../sysendian.h"
#include "../crypto_scrypt.h"
#include "../../../sodium/common.h"
#if defined(__XOP__) && defined(DISABLED)
#define ARX(out, in1, in2, s) \
@@ -239,7 +239,7 @@ smix(uint8_t * B, size_t r, uint32_t N, void * V, void * XY)
for (k = 0; k < 2 * r; k++) {
for (i = 0; i < 16; i++) {
X32[k * 16 + i] =
le32dec(&B[(k * 16 + (i * 5 % 16)) * 4]);
LOAD32_LE(&B[(k * 16 + (i * 5 % 16)) * 4]);
}
}
@@ -289,8 +289,7 @@ smix(uint8_t * B, size_t r, uint32_t N, void * V, void * XY)
/* 10: B' <-- X */
for (k = 0; k < 2 * r; k++) {
for (i = 0; i < 16; i++) {
le32enc(&B[(k * 16 + (i * 5 % 16)) * 4],
X32[k * 16 + i]);
STORE32_LE(&B[(k * 16 + (i * 5 % 16)) * 4], X32[k * 16 + i]);
}
}
}
@@ -1,146 +0,0 @@
#ifndef sysendian_H
#define sysendian_H
#include <stdint.h>
/* Avoid namespace collisions with BSD <sys/endian.h>. */
#define be16dec scrypt_be16dec
#define be16enc scrypt_be16enc
#define be32dec scrypt_be32dec
#define be32enc scrypt_be32enc
#define be64dec scrypt_be64dec
#define be64enc scrypt_be64enc
#define le16dec scrypt_le16dec
#define le16enc scrypt_le16enc
#define le32dec scrypt_le32dec
#define le32enc scrypt_le32enc
#define le64dec scrypt_le64dec
#define le64enc scrypt_le64enc
static inline uint16_t
be16dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
return ((uint16_t)(p[1]) + ((uint16_t)(p[0]) << 8));
}
static inline void
be16enc(void *pp, uint16_t x)
{
uint8_t * p = (uint8_t *)pp;
p[1] = x & 0xff;
p[0] = (x >> 8) & 0xff;
}
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 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 inline uint16_t
le16dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
return ((uint16_t)(p[0]) + ((uint16_t)(p[1]) << 8));
}
static inline void
le16enc(void *pp, uint16_t x)
{
uint8_t * p = (uint8_t *)pp;
p[0] = x & 0xff;
p[1] = (x >> 8) & 0xff;
}
static inline uint32_t
le32dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
}
static inline void
le32enc(void *pp, uint32_t x)
{
uint8_t * p = (uint8_t *)pp;
p[0] = x & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
}
static inline uint64_t
le64dec(const void *pp)
{
const uint8_t *p = (uint8_t const *)pp;
return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) +
((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) +
((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) +
((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56));
}
static inline void
le64enc(void *pp, uint64_t x)
{
uint8_t * p = (uint8_t *)pp;
p[0] = x & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
p[4] = (x >> 32) & 0xff;
p[5] = (x >> 40) & 0xff;
p[6] = (x >> 48) & 0xff;
p[7] = (x >> 56) & 0xff;
}
#endif /* !_SYSENDIAN_H_ */
@@ -2,6 +2,7 @@
#include "crypto_uint64.h"
#include "crypto_uint32.h"
#include "crypto_uint8.h"
#include "../../sodium/common.h"
typedef crypto_uint64 u64;
typedef crypto_uint32 u32;
@@ -9,24 +10,6 @@ typedef crypto_uint8 u8;
#define ROTL(x,b) (u64)( ((x) << (b)) | ( (x) >> (64 - (b))) )
#define U32TO8_LE(p, v) \
(p)[0] = (u8)((v) ); (p)[1] = (u8)((v) >> 8); \
(p)[2] = (u8)((v) >> 16); (p)[3] = (u8)((v) >> 24);
#define U64TO8_LE(p, v) \
U32TO8_LE((p), (u32)((v) )); \
U32TO8_LE((p) + 4, (u32)((v) >> 32));
#define U8TO64_LE(p) \
(((u64)((p)[0]) ) | \
((u64)((p)[1]) << 8) | \
((u64)((p)[2]) << 16) | \
((u64)((p)[3]) << 24) | \
((u64)((p)[4]) << 32) | \
((u64)((p)[5]) << 40) | \
((u64)((p)[6]) << 48) | \
((u64)((p)[7]) << 56))
#define SIPROUND \
do { \
v0 += v1; v1=ROTL(v1,13); v1 ^= v0; v0=ROTL(v0,32); \
@@ -44,8 +27,8 @@ int crypto_shorthash_siphash24(unsigned char *out, const unsigned char *in,
u64 v2 = 0x6c7967656e657261ULL;
u64 v3 = 0x7465646279746573ULL;
u64 b;
u64 k0 = U8TO64_LE( k );
u64 k1 = U8TO64_LE( k + 8 );
u64 k0 = LOAD64_LE( k );
u64 k1 = LOAD64_LE( k + 8 );
u64 m;
const u8 *end = in + inlen - ( inlen % sizeof( u64 ) );
const int left = inlen & 7;
@@ -57,7 +40,7 @@ int crypto_shorthash_siphash24(unsigned char *out, const unsigned char *in,
for ( ; in != end; in += 8 )
{
m = U8TO64_LE( in );
m = LOAD64_LE( in );
v3 ^= m;
SIPROUND;
SIPROUND;
@@ -86,7 +69,7 @@ int crypto_shorthash_siphash24(unsigned char *out, const unsigned char *in,
SIPROUND;
SIPROUND;
b = v0 ^ v1 ^ v2 ^ v3;
U64TO8_LE( out, b );
STORE64_LE( out, b );
return 0;
}
@@ -88,9 +88,9 @@ int crypto_stream_aes128ctr_afternm(unsigned char *out, unsigned long long len,
if(len < 128) goto partial;
if(len == 128) goto full;
tmp = load32_bigendian(np + 12);
tmp = LOAD32_BE(np + 12);
tmp += 8;
store32_bigendian(np + 12, tmp);
STORE32_BE(np + 12, tmp);
*(int128 *) (out + 0) = xmm8;
*(int128 *) (out + 16) = xmm9;
@@ -111,9 +111,9 @@ int crypto_stream_aes128ctr_afternm(unsigned char *out, unsigned long long len,
lensav = len;
len >>= 4;
tmp = load32_bigendian(np + 12);
tmp = LOAD32_BE(np + 12);
tmp += len;
store32_bigendian(np + 12, tmp);
STORE32_BE(np + 12, tmp);
blp = bl;
*(int128 *)(blp + 0) = xmm8;
@@ -140,9 +140,9 @@ int crypto_stream_aes128ctr_afternm(unsigned char *out, unsigned long long len,
full:
tmp = load32_bigendian(np + 12);
tmp = LOAD32_BE(np + 12);
tmp += 8;
store32_bigendian(np + 12, tmp);
STORE32_BE(np + 12, tmp);
*(int128 *) (out + 0) = xmm8;
*(int128 *) (out + 16) = xmm9;
@@ -5,24 +5,7 @@
#define COMMON_H
#include "types.h"
#define load32_bigendian crypto_stream_aes128ctr_portable_load32_bigendian
uint32 load32_bigendian(const unsigned char *x);
#define store32_bigendian crypto_stream_aes128ctr_portable_store32_bigendian
void store32_bigendian(unsigned char *x,uint32 u);
#define load32_littleendian crypto_stream_aes128ctr_portable_load32_littleendian
uint32 load32_littleendian(const unsigned char *x);
#define store32_littleendian crypto_stream_aes128ctr_portable_store32_littleendian
void store32_littleendian(unsigned char *x,uint32 u);
#define load64_littleendian crypto_stream_aes128ctr_portable_load64_littleendian
uint64 load64_littleendian(const unsigned char *x);
#define store64_littleendian crypto_stream_aes128ctr_portable_store64_littleendian
void store64_littleendian(unsigned char *x,uint64 u);
#include "../../sodium/common.h"
/* Macros required only for key expansion */
@@ -1,64 +0,0 @@
#include "common.h"
uint32 load32_bigendian(const unsigned char *x)
{
return
(uint32) (x[3]) \
| (((uint32) (x[2])) << 8) \
| (((uint32) (x[1])) << 16) \
| (((uint32) (x[0])) << 24)
;
}
void store32_bigendian(unsigned char *x,uint32 u)
{
x[3] = u; u >>= 8;
x[2] = u; u >>= 8;
x[1] = u; u >>= 8;
x[0] = u;
}
uint32 load32_littleendian(const unsigned char *x)
{
return
(uint32) (x[0]) \
| (((uint32) (x[1])) << 8) \
| (((uint32) (x[2])) << 16) \
| (((uint32) (x[3])) << 24)
;
}
void store32_littleendian(unsigned char *x,uint32 u)
{
x[0] = u; u >>= 8;
x[1] = u; u >>= 8;
x[2] = u; u >>= 8;
x[3] = u;
}
uint64 load64_littleendian(const unsigned char *x)
{
return
(uint64) (x[0]) \
| (((uint64) (x[1])) << 8) \
| (((uint64) (x[2])) << 16) \
| (((uint64) (x[3])) << 24)
| (((uint64) (x[4])) << 32)
| (((uint64) (x[5])) << 40)
| (((uint64) (x[6])) << 48)
| (((uint64) (x[7])) << 56)
;
}
void store64_littleendian(unsigned char *x,uint64 u)
{
x[0] = u; u >>= 8;
x[1] = u; u >>= 8;
x[2] = u; u >>= 8;
x[3] = u; u >>= 8;
x[4] = u; u >>= 8;
x[5] = u; u >>= 8;
x[6] = u; u >>= 8;
x[7] = u;
}
@@ -68,42 +68,42 @@ void rshift32_littleendian(int128 *r, const unsigned int n)
{
unsigned char *rp = (unsigned char *)r;
uint32 t;
t = load32_littleendian(rp);
t = LOAD32_LE(rp);
t >>= n;
store32_littleendian(rp, t);
t = load32_littleendian(rp+4);
STORE32_LE(rp, t);
t = LOAD32_LE(rp+4);
t >>= n;
store32_littleendian(rp+4, t);
t = load32_littleendian(rp+8);
STORE32_LE(rp+4, t);
t = LOAD32_LE(rp+8);
t >>= n;
store32_littleendian(rp+8, t);
t = load32_littleendian(rp+12);
STORE32_LE(rp+8, t);
t = LOAD32_LE(rp+12);
t >>= n;
store32_littleendian(rp+12, t);
STORE32_LE(rp+12, t);
}
void rshift64_littleendian(int128 *r, const unsigned int n)
{
unsigned char *rp = (unsigned char *)r;
uint64 t;
t = load64_littleendian(rp);
t = LOAD64_LE(rp);
t >>= n;
store64_littleendian(rp, t);
t = load64_littleendian(rp+8);
STORE64_LE(rp, t);
t = LOAD64_LE(rp+8);
t >>= n;
store64_littleendian(rp+8, t);
STORE64_LE(rp+8, t);
}
void lshift64_littleendian(int128 *r, const unsigned int n)
{
unsigned char *rp = (unsigned char *)r;
uint64 t;
t = load64_littleendian(rp);
t = LOAD64_LE(rp);
t <<= n;
store64_littleendian(rp, t);
t = load64_littleendian(rp+8);
STORE64_LE(rp, t);
t = LOAD64_LE(rp+8);
t <<= n;
store64_littleendian(rp+8, t);
STORE64_LE(rp+8, t);
}
void toggle(int128 *r)
@@ -116,16 +116,16 @@ void xor_rcon(int128 *r)
{
unsigned char *rp = (unsigned char *)r;
uint32 t;
t = load32_littleendian(rp+12);
t = LOAD32_LE(rp+12);
t ^= 0xffffffff;
store32_littleendian(rp+12, t);
STORE32_LE(rp+12, t);
}
void add_uint32_big(int128 *r, uint32 x)
{
unsigned char *rp = (unsigned char *)r;
uint32 t;
t = load32_littleendian(rp+12);
t = LOAD32_LE(rp+12);
t += x;
store32_littleendian(rp+12, t);
STORE32_LE(rp+12, t);
}
@@ -88,9 +88,9 @@ int crypto_stream_aes128ctr_xor_afternm(unsigned char *out, const unsigned char
if(len < 128) goto partial;
if(len == 128) goto full;
tmp = load32_bigendian(np + 12);
tmp = LOAD32_BE(np + 12);
tmp += 8;
store32_bigendian(np + 12, tmp);
STORE32_BE(np + 12, tmp);
xor2(&xmm8, (const int128 *)(in + 0));
xor2(&xmm9, (const int128 *)(in + 16));
@@ -121,9 +121,9 @@ int crypto_stream_aes128ctr_xor_afternm(unsigned char *out, const unsigned char
lensav = len;
len >>= 4;
tmp = load32_bigendian(np + 12);
tmp = LOAD32_BE(np + 12);
tmp += len;
store32_bigendian(np + 12, tmp);
STORE32_BE(np + 12, tmp);
blp = bl;
*(int128 *)(blp + 0) = xmm8;
@@ -152,9 +152,9 @@ int crypto_stream_aes128ctr_xor_afternm(unsigned char *out, const unsigned char
full:
tmp = load32_bigendian(np + 12);
tmp = LOAD32_BE(np + 12);
tmp += 8;
store32_bigendian(np + 12, tmp);
STORE32_BE(np + 12, tmp);
xor2(&xmm8, (const int128 *)(in + 0));
xor2(&xmm9, (const int128 *)(in + 16));
@@ -13,6 +13,7 @@
#include "crypto_stream_chacha20.h"
#include "stream_chacha20_ref.h"
#include "../stream_chacha20.h"
#include "../../sodium/common.h"
struct chacha_ctx {
uint32_t input[16];
@@ -32,20 +33,6 @@ typedef struct chacha_ctx chacha_ctx;
#define ROTL32(v, n) \
(U32V((v) << (n)) | ((v) >> (32 - (n))))
#define U8TO32_LITTLE(p) \
(((u32)((p)[0]) ) | \
((u32)((p)[1]) << 8) | \
((u32)((p)[2]) << 16) | \
((u32)((p)[3]) << 24))
#define U32TO8_LITTLE(p, v) \
do { \
(p)[0] = U8V((v) ); \
(p)[1] = U8V((v) >> 8); \
(p)[2] = U8V((v) >> 16); \
(p)[3] = U8V((v) >> 24); \
} while (0)
#define ROTATE(v,c) (ROTL32(v,c))
#define XOR(v,w) ((v) ^ (w))
#define PLUS(v,w) (U32V((v) + (w)))
@@ -66,38 +53,38 @@ chacha_keysetup(chacha_ctx *ctx, const u8 *k)
{
const unsigned char *constants;
ctx->input[4] = U8TO32_LITTLE(k + 0);
ctx->input[5] = U8TO32_LITTLE(k + 4);
ctx->input[6] = U8TO32_LITTLE(k + 8);
ctx->input[7] = U8TO32_LITTLE(k + 12);
ctx->input[4] = LOAD32_LE(k + 0);
ctx->input[5] = LOAD32_LE(k + 4);
ctx->input[6] = LOAD32_LE(k + 8);
ctx->input[7] = LOAD32_LE(k + 12);
k += 16;
constants = sigma;
ctx->input[8] = U8TO32_LITTLE(k + 0);
ctx->input[9] = U8TO32_LITTLE(k + 4);
ctx->input[10] = U8TO32_LITTLE(k + 8);
ctx->input[11] = U8TO32_LITTLE(k + 12);
ctx->input[0] = U8TO32_LITTLE(constants + 0);
ctx->input[1] = U8TO32_LITTLE(constants + 4);
ctx->input[2] = U8TO32_LITTLE(constants + 8);
ctx->input[3] = U8TO32_LITTLE(constants + 12);
ctx->input[8] = LOAD32_LE(k + 0);
ctx->input[9] = LOAD32_LE(k + 4);
ctx->input[10] = LOAD32_LE(k + 8);
ctx->input[11] = LOAD32_LE(k + 12);
ctx->input[0] = LOAD32_LE(constants + 0);
ctx->input[1] = LOAD32_LE(constants + 4);
ctx->input[2] = LOAD32_LE(constants + 8);
ctx->input[3] = LOAD32_LE(constants + 12);
}
static void
chacha_ivsetup(chacha_ctx *ctx, const u8 *iv, const u8 *counter)
{
ctx->input[12] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 0);
ctx->input[13] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 4);
ctx->input[14] = U8TO32_LITTLE(iv + 0);
ctx->input[15] = U8TO32_LITTLE(iv + 4);
ctx->input[12] = counter == NULL ? 0 : LOAD32_LE(counter + 0);
ctx->input[13] = counter == NULL ? 0 : LOAD32_LE(counter + 4);
ctx->input[14] = LOAD32_LE(iv + 0);
ctx->input[15] = LOAD32_LE(iv + 4);
}
static void
chacha_ietf_ivsetup(chacha_ctx *ctx, const u8 *iv, const u8 *counter)
{
ctx->input[12] = counter == NULL ? 0 : U8TO32_LITTLE(counter);
ctx->input[13] = U8TO32_LITTLE(iv + 0);
ctx->input[14] = U8TO32_LITTLE(iv + 4);
ctx->input[15] = U8TO32_LITTLE(iv + 8);
ctx->input[12] = counter == NULL ? 0 : LOAD32_LE(counter);
ctx->input[13] = LOAD32_LE(iv + 0);
ctx->input[14] = LOAD32_LE(iv + 4);
ctx->input[15] = LOAD32_LE(iv + 8);
}
static void
@@ -185,22 +172,22 @@ chacha_encrypt_bytes(chacha_ctx *ctx, const u8 *m, u8 *c, unsigned long long byt
x14 = PLUS(x14, j14);
x15 = PLUS(x15, j15);
x0 = XOR(x0, U8TO32_LITTLE(m + 0));
x1 = XOR(x1, U8TO32_LITTLE(m + 4));
x2 = XOR(x2, U8TO32_LITTLE(m + 8));
x3 = XOR(x3, U8TO32_LITTLE(m + 12));
x4 = XOR(x4, U8TO32_LITTLE(m + 16));
x5 = XOR(x5, U8TO32_LITTLE(m + 20));
x6 = XOR(x6, U8TO32_LITTLE(m + 24));
x7 = XOR(x7, U8TO32_LITTLE(m + 28));
x8 = XOR(x8, U8TO32_LITTLE(m + 32));
x9 = XOR(x9, U8TO32_LITTLE(m + 36));
x10 = XOR(x10, U8TO32_LITTLE(m + 40));
x11 = XOR(x11, U8TO32_LITTLE(m + 44));
x12 = XOR(x12, U8TO32_LITTLE(m + 48));
x13 = XOR(x13, U8TO32_LITTLE(m + 52));
x14 = XOR(x14, U8TO32_LITTLE(m + 56));
x15 = XOR(x15, U8TO32_LITTLE(m + 60));
x0 = XOR(x0, LOAD32_LE(m + 0));
x1 = XOR(x1, LOAD32_LE(m + 4));
x2 = XOR(x2, LOAD32_LE(m + 8));
x3 = XOR(x3, LOAD32_LE(m + 12));
x4 = XOR(x4, LOAD32_LE(m + 16));
x5 = XOR(x5, LOAD32_LE(m + 20));
x6 = XOR(x6, LOAD32_LE(m + 24));
x7 = XOR(x7, LOAD32_LE(m + 28));
x8 = XOR(x8, LOAD32_LE(m + 32));
x9 = XOR(x9, LOAD32_LE(m + 36));
x10 = XOR(x10, LOAD32_LE(m + 40));
x11 = XOR(x11, LOAD32_LE(m + 44));
x12 = XOR(x12, LOAD32_LE(m + 48));
x13 = XOR(x13, LOAD32_LE(m + 52));
x14 = XOR(x14, LOAD32_LE(m + 56));
x15 = XOR(x15, LOAD32_LE(m + 60));
j12 = PLUSONE(j12);
/* LCOV_EXCL_START */
@@ -209,22 +196,22 @@ chacha_encrypt_bytes(chacha_ctx *ctx, const u8 *m, u8 *c, unsigned long long byt
}
/* LCOV_EXCL_STOP */
U32TO8_LITTLE(c + 0, x0);
U32TO8_LITTLE(c + 4, x1);
U32TO8_LITTLE(c + 8, x2);
U32TO8_LITTLE(c + 12, x3);
U32TO8_LITTLE(c + 16, x4);
U32TO8_LITTLE(c + 20, x5);
U32TO8_LITTLE(c + 24, x6);
U32TO8_LITTLE(c + 28, x7);
U32TO8_LITTLE(c + 32, x8);
U32TO8_LITTLE(c + 36, x9);
U32TO8_LITTLE(c + 40, x10);
U32TO8_LITTLE(c + 44, x11);
U32TO8_LITTLE(c + 48, x12);
U32TO8_LITTLE(c + 52, x13);
U32TO8_LITTLE(c + 56, x14);
U32TO8_LITTLE(c + 60, x15);
STORE32_LE(c + 0, x0);
STORE32_LE(c + 4, x1);
STORE32_LE(c + 8, x2);
STORE32_LE(c + 12, x3);
STORE32_LE(c + 16, x4);
STORE32_LE(c + 20, x5);
STORE32_LE(c + 24, x6);
STORE32_LE(c + 28, x7);
STORE32_LE(c + 32, x8);
STORE32_LE(c + 36, x9);
STORE32_LE(c + 40, x10);
STORE32_LE(c + 44, x11);
STORE32_LE(c + 48, x12);
STORE32_LE(c + 52, x13);
STORE32_LE(c + 56, x14);
STORE32_LE(c + 60, x15);
if (bytes <= 64) {
if (bytes < 64) {
@@ -296,8 +283,8 @@ stream_ref_xor_ic(unsigned char *c, const unsigned char *m,
}
ic_high = U32V(ic >> 32);
ic_low = U32V(ic);
U32TO8_LITTLE(&ic_bytes[0], ic_low);
U32TO8_LITTLE(&ic_bytes[4], ic_high);
STORE32_LE(&ic_bytes[0], ic_low);
STORE32_LE(&ic_bytes[4], ic_high);
chacha_keysetup(&ctx, k);
chacha_ivsetup(&ctx, n, ic_bytes);
chacha_encrypt_bytes(&ctx, m, c, mlen);
@@ -318,7 +305,7 @@ stream_ietf_ref_xor_ic(unsigned char *c, const unsigned char *m,
if (!mlen) {
return 0;
}
U32TO8_LITTLE(ic_bytes, ic);
STORE32_LE(ic_bytes, ic);
chacha_keysetup(&ctx, k);
chacha_ietf_ivsetup(&ctx, n, ic_bytes);
chacha_encrypt_bytes(&ctx, m, c, mlen);
+111
View File
@@ -0,0 +1,111 @@
#ifndef common_H
#define common_H 1
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#define LOAD64_LE(SRC) load64_le(SRC)
static inline uint64_t
load64_le(const uint8_t src[8])
{
#ifdef NATIVE_LITTLE_ENDIAN
uint64_t w;
memcpy(&w, src, sizeof w);
return w;
#else
uint64_t w = (uint64_t) src[0];
w |= (uint64_t) src[1] << 8;
w |= (uint64_t) src[2] << 16;
w |= (uint64_t) src[3] << 24;
w |= (uint64_t) src[4] << 32;
w |= (uint64_t) src[5] << 40;
w |= (uint64_t) src[6] << 48;
w |= (uint64_t) src[7] << 56;
return w;
#endif
}
#define STORE64_LE(DST, W) store64_le((DST), (W))
static inline void
store64_le(uint8_t dst[8], uint64_t w)
{
#ifdef NATIVE_LITTLE_ENDIAN
memcpy(dst, &w, sizeof w);
#else
dst[0] = (uint8_t) w; w >>= 8;
dst[1] = (uint8_t) w; w >>= 8;
dst[2] = (uint8_t) w; w >>= 8;
dst[3] = (uint8_t) w; w >>= 8;
dst[4] = (uint8_t) w; w >>= 8;
dst[5] = (uint8_t) w; w >>= 8;
dst[6] = (uint8_t) w; w >>= 8;
dst[7] = (uint8_t) w;
#endif
}
#define LOAD32_LE(SRC) load32_le(SRC)
static inline uint32_t
load32_le(const uint8_t src[4])
{
#ifdef NATIVE_LITTLE_ENDIAN
uint32_t w;
memcpy(&w, src, sizeof w);
return w;
#else
uint32_t w = (uint32_t) src[0];
w |= (uint32_t) src[1] << 8;
w |= (uint32_t) src[2] << 16;
w |= (uint32_t) src[3] << 24;
return w;
#endif
}
#define STORE32_LE(DST, W) store32_le((DST), (W))
static inline void
store32_le(uint8_t dst[4], uint32_t w)
{
#ifdef NATIVE_LITTLE_ENDIAN
memcpy(dst, &w, sizeof w);
#else
dst[0] = (uint8_t) w; w >>= 8;
dst[1] = (uint8_t) w; w >>= 8;
dst[2] = (uint8_t) w; w >>= 8;
dst[3] = (uint8_t) w;
#endif
}
/* ----- */
#define LOAD32_BE(SRC) load32_be(SRC)
static inline uint32_t
load32_be(const uint8_t src[4])
{
#ifdef NATIVE_BIG_ENDIAN
uint32_t w;
memcpy(&w, src, sizeof w);
return w;
#else
uint32_t w = (uint32_t) src[3];
w |= (uint32_t) src[2] << 8;
w |= (uint32_t) src[1] << 16;
w |= (uint32_t) src[0] << 24;
return w;
#endif
}
#define STORE32_BE(DST, W) store32_be((DST), (W))
static inline void
store32_be(uint8_t dst[4], uint32_t w)
{
#ifdef NATIVE_BIG_ENDIAN
memcpy(dst, &w, sizeof w);
#else
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
}
#endif