1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-20 02:25:14 -07:00

scrypt: corrected pointer alias issues causing scrypt to fail on MIPS64

by jfoug <jfoug at cox.net> via JtR issue #1032
This commit is contained in:
Frank Denis 2015-02-15 22:31:04 +01:00
parent 788d8d0178
commit 663fe8229e
2 changed files with 71 additions and 25 deletions

View File

@ -32,6 +32,12 @@
#include <stdint.h>
#if SIZE_MAX > 0xffffffffUL
# define ARCH_BITS 64
#else
# define ARCH_BITS 32
#endif
#define crypto_pwhash_scryptsalsa208sha256_STRPREFIXBYTES 14
#define crypto_pwhash_scryptsalsa208sha256_STRSETTINGBYTES 57
#define crypto_pwhash_scryptsalsa208sha256_STRSALTBYTES 32
@ -46,6 +52,11 @@ typedef struct {
size_t size;
} escrypt_region_t;
typedef union {
uint64_t d[8];
uint32_t w[16];
} escrypt_block_t;
typedef escrypt_region_t escrypt_local_t;
extern int escrypt_init_local(escrypt_local_t * __local);

View File

@ -39,27 +39,61 @@
#include "../crypto_scrypt.h"
static inline void
blkcpy(void * dest, const void * src, size_t len)
blkcpy_64(escrypt_block_t *dest, const escrypt_block_t *src)
{
size_t * D = (size_t *) dest;
const size_t * S = (const size_t *) src;
size_t L = len / sizeof(size_t);
size_t i;
int i;
for (i = 0; i < L; i++)
D[i] = S[i];
#if (ARCH_BITS==32)
for (i = 0; i < 16; ++i)
dest->w[i] = src->w[i];
#else
for (i = 0; i < 8; ++i)
dest->d[i] = src->d[i];
#endif
}
static inline void
blkxor(void * dest, const void * src, size_t len)
blkxor_64(escrypt_block_t *dest, const escrypt_block_t *src)
{
size_t * D = (size_t *) dest;
const size_t * S = (const size_t *) src;
size_t L = len / sizeof(size_t);
size_t i;
int i;
for (i = 0; i < L; i++)
D[i] ^= S[i];
#if (ARCH_BITS==32)
for (i = 0; i < 16; ++i)
dest->w[i] ^= src->w[i];
#else
for (i = 0; i < 8; ++i)
dest->d[i] ^= src->d[i];
#endif
}
static inline void
blkcpy(escrypt_block_t *dest, const escrypt_block_t *src, int len)
{
int i, L;
#if (ARCH_BITS==32)
L = (len>>2);
for (i = 0; i < L; ++i)
dest->w[i] = src->w[i];
#else
L = (len>>3);
for (i = 0; i < L; ++i)
dest->d[i] = src->d[i];
#endif
}
static inline void
blkxor(escrypt_block_t *dest, const escrypt_block_t *src, int len)
{
int i, L;
#if (ARCH_BITS==32)
L = (len>>2);
for (i = 0; i < L; ++i)
dest->w[i] ^= src->w[i];
#else
L = (len>>3);
for (i = 0; i < L; ++i)
dest->d[i] ^= src->d[i];
#endif
}
/**
@ -69,10 +103,11 @@ blkxor(void * dest, const void * src, size_t len)
static void
salsa20_8(uint32_t B[16])
{
uint32_t x[16];
escrypt_block_t X;
uint32_t *x = X.w;
size_t i;
blkcpy(x, B, 64);
blkcpy_64(&X, (escrypt_block_t*)B);
for (i = 0; i < 8; i += 2) {
#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
/* Operate on columns. */
@ -118,25 +153,25 @@ blockmix_salsa8(const uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
size_t i;
/* 1: X <-- B_{2r - 1} */
blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
blkcpy_64((escrypt_block_t*)X, (escrypt_block_t*)&Bin[(2 * r - 1) * 16]);
/* 2: for i = 0 to 2r - 1 do */
for (i = 0; i < 2 * r; i += 2) {
/* 3: X <-- H(X \xor B_i) */
blkxor(X, &Bin[i * 16], 64);
blkxor_64((escrypt_block_t*)X, (escrypt_block_t*)&Bin[i * 16]);
salsa20_8(X);
/* 4: Y_i <-- X */
/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
blkcpy(&Bout[i * 8], X, 64);
blkcpy_64((escrypt_block_t*)&Bout[i * 8], (escrypt_block_t*)X);
/* 3: X <-- H(X \xor B_i) */
blkxor(X, &Bin[i * 16 + 16], 64);
blkxor_64((escrypt_block_t*)X, (escrypt_block_t*)&Bin[i * 16 + 16]);
salsa20_8(X);
/* 4: Y_i <-- X */
/* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
blkcpy(&Bout[i * 8 + r * 16], X, 64);
blkcpy_64((escrypt_block_t*)&Bout[i * 8 + r * 16], (escrypt_block_t*)X);
}
}
@ -177,13 +212,13 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
/* 2: for i = 0 to N - 1 do */
for (i = 0; i < N; i += 2) {
/* 3: V_i <-- X */
blkcpy(&V[i * (32 * r)], X, 128 * r);
blkcpy((escrypt_block_t*)&V[i * (32 * r)], (escrypt_block_t*)X, 128 * r);
/* 4: X <-- H(X) */
blockmix_salsa8(X, Y, Z, r);
/* 3: V_i <-- X */
blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
blkcpy((escrypt_block_t*)&V[(i + 1) * (32 * r)], (escrypt_block_t*)Y, 128 * r);
/* 4: X <-- H(X) */
blockmix_salsa8(Y, X, Z, r);
@ -195,14 +230,14 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
j = integerify(X, r) & (N - 1);
/* 8: X <-- H(X \xor V_j) */
blkxor(X, &V[j * (32 * r)], 128 * r);
blkxor((escrypt_block_t*)X, (escrypt_block_t*)&V[j * (32 * r)], 128 * r);
blockmix_salsa8(X, Y, Z, r);
/* 7: j <-- Integerify(X) mod N */
j = integerify(Y, r) & (N - 1);
/* 8: X <-- H(X \xor V_j) */
blkxor(Y, &V[j * (32 * r)], 128 * r);
blkxor((escrypt_block_t*)Y, (escrypt_block_t*)&V[j * (32 * r)], 128 * r);
blockmix_salsa8(Y, X, Z, r);
}
/* 10: B' <-- X */