-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
382 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,68 @@ | ||
#include "chacha.h" | ||
#include <stdio.h> | ||
#include "../common/cpu_features.h" | ||
|
||
int RANDOMGEN_USE_SIMD; | ||
|
||
extern INLINE uint32_t chacha_next32(chacha_state_t *state); | ||
|
||
extern INLINE uint64_t chacha_next64(chacha_state_t *state); | ||
|
||
extern INLINE double chacha_next_double(chacha_state_t *state); | ||
|
||
void chacha_seed(chacha_state_t *state, uint64_t *seedval, uint64_t *stream, uint64_t *ctr) { | ||
// Using a 128-bit seed. | ||
state->keysetup[0] = seedval[0] & 0xffffffffu; | ||
state->keysetup[1] = seedval[0] >> 32; | ||
state->keysetup[2] = seedval[1] & 0xffffffffu; | ||
state->keysetup[3] = seedval[1] >> 32; | ||
// Using a 128-bit stream. | ||
state->keysetup[4] = stream[0] & 0xffffffffu; | ||
state->keysetup[5] = stream[0] >> 32; | ||
state->keysetup[6] = stream[1] & 0xffffffffu; | ||
state->keysetup[7] = stream[1] >> 32; | ||
|
||
/* Ensure str[0] is at a node where a block would be generated */ | ||
state->ctr[0] = ((ctr[0] >> 4) << 4); | ||
state->ctr[1] = ctr[1]; | ||
generate_block(state); | ||
/* Store correct value of counter */ | ||
state->ctr[0] = ctr[0]; | ||
#if defined(__SSE2__) && __SSE2__ | ||
#if defined(__SSSE3__) && __SSSE3__ | ||
#define CHACHA_FEATURE_REG RANDOMGEN_ECX | ||
#define CHACHA_FEATURE_FLAG 9 | ||
#else | ||
#define CHACHA_FEATURE_REG RANDOMGEN_EDX | ||
#define CHACHA_FEATURE_FLAG 26 | ||
#endif | ||
#else | ||
#define CHACHA_FEATURE_FLAG 0 | ||
#endif | ||
|
||
int chacha_simd_capable(void) { | ||
#if defined(__SSE2__) && __SSE2__ | ||
int flags[32]; | ||
feature_flags(flags, CHACHA_FEATURE_REG); | ||
RANDOMGEN_USE_SIMD = flags[CHACHA_FEATURE_FLAG]; | ||
return RANDOMGEN_USE_SIMD; | ||
#else | ||
RANDOMGEN_USE_SIMD = 0; | ||
return 0; | ||
#endif | ||
} | ||
|
||
void chacha_use_simd(int flag) { RANDOMGEN_USE_SIMD = flag; } | ||
|
||
void chacha_seed(chacha_state_t *state, uint64_t *seedval, uint64_t *stream, | ||
uint64_t *ctr) { | ||
chacha_simd_capable(); | ||
// Using a 128-bit seed. | ||
state->keysetup[0] = seedval[0] & 0xffffffffu; | ||
state->keysetup[1] = seedval[0] >> 32; | ||
state->keysetup[2] = seedval[1] & 0xffffffffu; | ||
state->keysetup[3] = seedval[1] >> 32; | ||
// Using a 128-bit stream. | ||
state->keysetup[4] = stream[0] & 0xffffffffu; | ||
state->keysetup[5] = stream[0] >> 32; | ||
state->keysetup[6] = stream[1] & 0xffffffffu; | ||
state->keysetup[7] = stream[1] >> 32; | ||
|
||
/* Ensure str[0] is at a node where a block would be generated */ | ||
state->ctr[0] = ((ctr[0] >> 4) << 4); | ||
state->ctr[1] = ctr[1]; | ||
generate_block(state); | ||
/* Store correct value of counter */ | ||
state->ctr[0] = ctr[0]; | ||
} | ||
|
||
void chacha_advance(chacha_state_t *state, uint64_t *delta) { | ||
int carry, idx = state->ctr[0] % 16; | ||
uint64_t orig; | ||
orig = state->ctr[0]; | ||
state->ctr[0] += delta[0]; | ||
carry = state->ctr[0] < orig; | ||
state->ctr[1] += (delta[1] + carry); | ||
if ((idx + delta[0] >= 16 || delta[1]) && ((state->ctr[0] % 16) != 0)) { | ||
generate_block(state); | ||
} | ||
int carry, idx = state->ctr[0] % 16; | ||
uint64_t orig; | ||
orig = state->ctr[0]; | ||
state->ctr[0] += delta[0]; | ||
carry = state->ctr[0] < orig; | ||
state->ctr[1] += (delta[1] + carry); | ||
if ((idx + delta[0] >= 16 || delta[1]) && ((state->ctr[0] % 16) != 0)) { | ||
generate_block(state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,54 @@ | ||
#include "cpu_features.h" | ||
|
||
void feature_flags(int flags[]) | ||
void feature_flags(int flags[32], int major) | ||
{ | ||
int i; | ||
#if defined(HAVE_CPUID) | ||
#if defined(HAVE_CPUID) && HAVE_CPUID | ||
#if defined(__clang__) || defined(__GNUC__) | ||
uint32_t num_ids = 0, eax = 0, ebx = 0, ecx = 0, edx = 0; | ||
uint32_t num_ids = 0, reg = 0, eax = 0, ebx = 0, ecx = 0, edx = 0; | ||
num_ids = __get_cpuid_max(0, &ebx); | ||
ebx = 0; | ||
if (num_ids >= 1) | ||
{ | ||
__get_cpuid(1, &eax, &ebx, &ecx, &edx); | ||
} | ||
#elif defined(_MSC_VER) && defined(_WIN32) | ||
int cpu_info[4]; | ||
int num_ids, ecx = 0; | ||
int cpu_info[4] = {0}; | ||
int num_ids, reg = 0, eax = 0, ebx = 0, ecx = 0, edx = 0; | ||
__cpuid(cpu_info, 0); | ||
num_ids = (int)cpu_info[0]; | ||
if (num_ids >= 1) | ||
{ | ||
__cpuidex(cpu_info, 1, 0); | ||
eax = cpu_info[0]; | ||
ebx = cpu_info[1]; | ||
ecx = cpu_info[2]; | ||
edx = cpu_info[3]; | ||
} | ||
#endif | ||
#else | ||
uint32_t ecx; | ||
ecx = 0; | ||
uint32_t reg, eax, ebx, ecx, edx; | ||
reg = 0; eax = 0; ebx = 0; ecx = 0; edx = 0; | ||
#endif | ||
switch(major){ | ||
case 0: | ||
reg = eax; | ||
break; | ||
|
||
case 1: | ||
reg = ebx; | ||
break; | ||
|
||
case 2: | ||
reg = ecx; | ||
break; | ||
|
||
case 3: | ||
reg = edx; | ||
break; | ||
} | ||
for (i = 0; i < 32; i++) | ||
{ | ||
flags[i] = (ecx >> i) & 0x1; | ||
flags[i] = (reg >> i) & 0x1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.