Skip to content

Commit

Permalink
Pull hash state into a structure, refactor include files
Browse files Browse the repository at this point in the history
  • Loading branch information
rongarret committed Jul 26, 2016
1 parent 622f1fe commit 7692956
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
14 changes: 4 additions & 10 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@

#include "tweetnacl.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef unsigned char u8;
typedef unsigned long u32;
typedef unsigned long long u64;
typedef long long i64;
typedef const unsigned char * string;

int crypto_hash_stream(u8 *out, const u8 *m, u64 n);
#include "tweetnacl.h"
#include "tweetnacl-aux.h"

void randombytes(u8 *s, u64 n) {
FILE *f = fopen("/dev/urandom", "r");
Expand Down Expand Up @@ -51,7 +44,8 @@ int main(int argc, char *argv[]) {
printf("\n\n");

rewind(f);
crypto_hash_stream(h, m, mlen);
hash_state hs;
crypto_hash_stream(h, &hs);
fclose(f);
printf("crypto_hash_stream:\n");
for (int i=0; i<crypto_hash_BYTES; i++) printf("%02x", h[i]);
Expand Down
20 changes: 20 additions & 0 deletions tweetnacl-aux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#define FOR(i, n) for (i = 0; (int)i < (int)n; ++i)
#define sv static void

typedef unsigned char u8;
typedef unsigned long u32;
typedef unsigned long long u64;
typedef long long i64;
typedef i64 gf[16];

typedef const unsigned char * string;

typedef struct {
u8 h[64];
u64 msglen;
u64 a[8], z[8];
u8 x[256];
} hash_state;

int crypto_hash_stream(u8 *out, hash_state *hs);
void spk2epk(u8 *epk, u8 *spk);
29 changes: 12 additions & 17 deletions tweetnacl.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#include "tweetnacl.h"
#define FOR(i, n) for (i = 0; (int)i < (int)n; ++i)
#define sv static void

typedef unsigned char u8;
typedef unsigned long u32;
typedef unsigned long long u64;
typedef long long i64;
typedef i64 gf[16];
#include "tweetnacl-aux.h"

extern void randombytes(u8 *, u64);

static const u8 _0[16], _9[32] = {9};
Expand Down Expand Up @@ -603,34 +597,35 @@ extern int crypto_hash_stream_read_block(u8* buf) {
}
*/

int crypto_hash_stream(u8 *out) {
u8 h[64];
int crypto_hash_stream(u8 *out, hash_state *hs) {
u8 *h = hs->h;
u8 buf[128];
u64 msglen = 0;
hs->msglen = 0;
int i;

FOR(i, 64) h[i] = iv[i];

// Process all but the final block
u64 a[8], z[8];
u64 *a = hs->a;
u64 *z = hs->z;
FOR(i, 8) a[i] = z[i] = dl64(h + 8 * i);
int n = crypto_hash_stream_read_block(buf);
msglen += n;
hs->msglen += n;
while (n >= 128) {
crypto_hashblock(buf, a, z);
n = crypto_hash_stream_read_block(buf);
msglen += n;
hs->msglen += n;
}
FOR(i, 8) ts64(h + 8 * i, z[i]);

// Process final block, n is the length of the final block
u8 x[256];
u8 *x = hs->x;
FOR(i, 256) x[i] = 0;
FOR(i, n) x[i] = buf[i];
x[n] = 128;
n = 256 - 128 * (n < 112);
x[n - 9] = msglen >> 61;
ts64(x + n - 8, msglen << 3);
x[n - 9] = (hs->msglen) >> 61;
ts64(x + n - 8, (hs->msglen) << 3);
crypto_hashblocks(h, x, n);

FOR(i, 64) out[i] = h[i];
Expand Down

0 comments on commit 7692956

Please sign in to comment.