Skip to content

Commit

Permalink
Only copy W from mem on C variant
Browse files Browse the repository at this point in the history
the inline assembly version doesn't need to allocate an additional
buffer in order to work with W data as it uses hardware vectors
instead. However the C variant will overwrite the input buffer if it's
used directly as W.
  • Loading branch information
Gustavo Serra Scalet committed Mar 14, 2017
1 parent 5d5a889 commit 0324bdb
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion sha256_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
"memory" \
); } while (0)

void sha2_transform(base_type* _h, base_type* w) {
void sha2_transform(base_type* _h, unsigned char* w) {
vector_base_type a, b, c, d, e, f, g, h;
vector int vRb;

Expand Down
15 changes: 2 additions & 13 deletions sha2_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,14 @@ int sha2(unsigned char *input, size_t size, size_t padded_size) {
// Concatenate '1' to input.
input[size] = (unsigned char)(1 << 7);

// Swap bytes due to endianess .
unsigned char* input_swapped = (unsigned char *) calloc(padded_size, sizeof(unsigned char));
if (input_swapped == NULL) {
fprintf(stderr, "%s\n.", strerror(errno));
return errno;
}
//swap_bytes(input, input_swapped, padded_size);

// write total message size at the end (2 base_types).
write_size(input, size, padded_size - 2 * base_type_size);
swap_bytes(input + padded_size - 2 * base_type_size,
input + padded_size - 2 * base_type_size, 2 * base_type_size);

// Sha compression process.
for (size_t i = 0; i < padded_size; i = i + BLOCK_SIZE) {
base_type w[W_SIZE];
memcpy(w, input + i, 16 * sizeof(base_type));
sha2_transform(_h, w);
}
for (size_t i = 0; i < padded_size; i = i + BLOCK_SIZE)
sha2_transform(_h, input + i);

printf(
#if SHA_BITS == 256
Expand Down
2 changes: 1 addition & 1 deletion sha2_compress.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include "base-types.h"

void sha2_transform(base_type* _h, base_type* w);
void sha2_transform(base_type* _h, unsigned char* w);

#endif // _PPC64_LE_SHA2_COMPRESS_H_
7 changes: 5 additions & 2 deletions sha2_compress_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ void static inline sha2_round(base_type* a, base_type* b, base_type* c,
*a = tmp1 + tmp2;
}

void sha2_transform(base_type* _h, base_type* w) {
void sha2_transform(base_type* _h, unsigned char* w_in) {
base_type a, b, c, d, e, f, g, h;
int i;
// using a local w as it's going to be modified
base_type w[W_SIZE];

a = _h[0];
b = _h[1];
Expand All @@ -68,7 +70,8 @@ void sha2_transform(base_type* _h, base_type* w) {
g = _h[6];
h = _h[7];

swap_bytes((unsigned char*)w, (unsigned char*)w, BLOCK_SIZE);
// Only first 16 bytes needs to be copied as the ones above will be defined
swap_bytes(w_in, (unsigned char*)w, BLOCK_SIZE);

// Loop unrolling, from 0 to 15
for (i = 0; i < 16; i++) {
Expand Down
2 changes: 1 addition & 1 deletion sha512_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@
"memory" \
); } while (0)

void sha2_transform(base_type* _h, base_type* w) {
void sha2_transform(base_type* _h, unsigned char* w) {
vector_base_type a, b, c, d, e, f, g, h;
vector int vRb;

Expand Down

0 comments on commit 0324bdb

Please sign in to comment.