Skip to content

Commit

Permalink
For MSVC use alloca() instead of VLA
Browse files Browse the repository at this point in the history
Closes #20
  • Loading branch information
bwesterb committed Aug 2, 2022
1 parent 79ea9b7 commit 15f872c
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion ref/haraka.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdlib.h>

#include "haraka.h"
#include "utils.h"

#define HARAKAS_RATE 32

Expand Down Expand Up @@ -696,7 +697,7 @@ static void haraka_S_absorb(unsigned char *s, unsigned int r,
unsigned char p, const spx_ctx *ctx)
{
unsigned long long i;
unsigned char t[r];
SPX_VLA(uint8_t, t, r);

while (mlen >= r) {
/* XOR block to state */
Expand Down
4 changes: 2 additions & 2 deletions ref/sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ void sha512(uint8_t *out, const uint8_t *in, size_t inlen) {
void mgf1_256(unsigned char *out, unsigned long outlen,
const unsigned char *in, unsigned long inlen)
{
unsigned char inbuf[inlen + 4];
SPX_VLA(uint8_t, inbuf, inlen+4);
unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES];
unsigned long i;

Expand All @@ -653,7 +653,7 @@ void mgf1_256(unsigned char *out, unsigned long outlen,
void mgf1_512(unsigned char *out, unsigned long outlen,
const unsigned char *in, unsigned long inlen)
{
unsigned char inbuf[inlen + 4];
SPX_VLA(uint8_t, inbuf, inlen+4);
unsigned char outbuf[SPX_SHA512_OUTPUT_BYTES];
unsigned long i;

Expand Down
5 changes: 3 additions & 2 deletions ref/thash_haraka_robust.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "thash.h"
#include "address.h"
#include "params.h"
#include "utils.h"

#include "haraka.h"

Expand All @@ -13,8 +14,8 @@
void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks,
const spx_ctx *ctx, uint32_t addr[8])
{
unsigned char buf[SPX_ADDR_BYTES + inblocks*SPX_N];
unsigned char bitmask[inblocks * SPX_N];
SPX_VLA(uint8_t, buf, SPX_ADDR_BYTES + inblocks*SPX_N);
SPX_VLA(uint8_t, bitmask, inblocks*SPX_N);
unsigned char outbuf[32];
unsigned char buf_tmp[64];
unsigned int i;
Expand Down
3 changes: 2 additions & 1 deletion ref/thash_haraka_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "thash.h"
#include "address.h"
#include "params.h"
#include "utils.h"

#include "haraka.h"

Expand All @@ -13,7 +14,7 @@
void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks,
const spx_ctx *ctx, uint32_t addr[8])
{
unsigned char buf[SPX_ADDR_BYTES + inblocks*SPX_N];
SPX_VLA(uint8_t, buf, SPX_ADDR_BYTES + inblocks*SPX_N);
unsigned char outbuf[32];
unsigned char buf_tmp[64];

Expand Down
9 changes: 5 additions & 4 deletions ref/thash_sha2_robust.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "thash.h"
#include "address.h"
#include "params.h"
#include "utils.h"
#include "sha2.h"

#if SPX_SHA512
Expand All @@ -23,9 +24,9 @@ void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks,
return;
}
#endif
unsigned char buf[SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N];
unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES];
unsigned char bitmask[inblocks * SPX_N];
SPX_VLA(uint8_t, bitmask, inblocks * SPX_N);
SPX_VLA(uint8_t, buf, SPX_N + SPX_SHA256_OUTPUT_BYTES + inblocks*SPX_N);
uint8_t sha2_state[40];
unsigned int i;

Expand All @@ -49,9 +50,9 @@ void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks,
static void thash_512(unsigned char *out, const unsigned char *in, unsigned int inblocks,
const spx_ctx *ctx, uint32_t addr[8])
{
unsigned char buf[SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N];
unsigned char outbuf[SPX_SHA512_OUTPUT_BYTES];
unsigned char bitmask[inblocks * SPX_N];
SPX_VLA(uint8_t, bitmask, inblocks * SPX_N);
SPX_VLA(uint8_t, buf, SPX_N + SPX_SHA256_ADDR_BYTES + inblocks*SPX_N);
uint8_t sha2_state[72];
unsigned int i;

Expand Down
5 changes: 3 additions & 2 deletions ref/thash_sha2_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "thash.h"
#include "address.h"
#include "params.h"
#include "utils.h"
#include "sha2.h"

#if SPX_SHA512
Expand All @@ -24,9 +25,9 @@ void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks,
}
#endif

unsigned char buf[SPX_SHA256_ADDR_BYTES + inblocks*SPX_N];
unsigned char outbuf[SPX_SHA256_OUTPUT_BYTES];
uint8_t sha2_state[40];
SPX_VLA(uint8_t, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N);

/* Retrieve precomputed state containing pub_seed */
memcpy(sha2_state, ctx->state_seeded, 40 * sizeof(uint8_t));
Expand All @@ -42,9 +43,9 @@ void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks,
static void thash_512(unsigned char *out, const unsigned char *in, unsigned int inblocks,
const spx_ctx *ctx, uint32_t addr[8])
{
unsigned char buf[SPX_SHA256_ADDR_BYTES + inblocks*SPX_N];
unsigned char outbuf[SPX_SHA512_OUTPUT_BYTES];
uint8_t sha2_state[72];
SPX_VLA(uint8_t, buf, SPX_SHA256_ADDR_BYTES + inblocks*SPX_N);

/* Retrieve precomputed state containing pub_seed */
memcpy(sha2_state, ctx->state_seeded_512, 72 * sizeof(uint8_t));
Expand Down
5 changes: 3 additions & 2 deletions ref/thash_shake_robust.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "thash.h"
#include "address.h"
#include "params.h"
#include "utils.h"

#include "fips202.h"

Expand All @@ -13,8 +14,8 @@
void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks,
const spx_ctx *ctx, uint32_t addr[8])
{
unsigned char buf[SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N];
unsigned char bitmask[inblocks * SPX_N];
SPX_VLA(uint8_t, buf, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N);
SPX_VLA(uint8_t, bitmask, inblocks * SPX_N);
unsigned int i;

memcpy(buf, ctx->pub_seed, SPX_N);
Expand Down
3 changes: 2 additions & 1 deletion ref/thash_shake_simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "thash.h"
#include "address.h"
#include "params.h"
#include "utils.h"

#include "fips202.h"

Expand All @@ -13,7 +14,7 @@
void thash(unsigned char *out, const unsigned char *in, unsigned int inblocks,
const spx_ctx *ctx, uint32_t addr[8])
{
unsigned char buf[SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N];
SPX_VLA(uint8_t, buf, SPX_N + SPX_ADDR_BYTES + inblocks*SPX_N);

memcpy(buf, ctx->pub_seed, SPX_N);
memcpy(buf + SPX_N, addr, SPX_ADDR_BYTES);
Expand Down
4 changes: 2 additions & 2 deletions ref/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ void treehash(unsigned char *root, unsigned char *auth_path, const spx_ctx* ctx,
uint32_t /* addr_idx */, const uint32_t[8] /* tree_addr */),
uint32_t tree_addr[8])
{
unsigned char stack[(tree_height + 1)*SPX_N];
unsigned int heights[tree_height + 1];
SPX_VLA(uint8_t, stack, (tree_height+1)*SPX_N);
SPX_VLA(unsigned int, heights, tree_height+1);
unsigned int offset = 0;
uint32_t idx;
uint32_t tree_idx;
Expand Down
9 changes: 9 additions & 0 deletions ref/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
#include "context.h"


/* To support MSVC use alloca() instead of VLAs. See #20. */
#ifdef _MSC_VER
# include <alloca.h>
# define SPX_VLA(__t,__x,__s) __t *__x = (__t*)alloca((__s)*sizeof(__t))
#else
# define SPX_VLA(__t,__x,__s) __t __x[__s]
#endif

/**
* Converts the value of 'in' to 'outlen' bytes in big-endian byte order.
*/
Expand Down Expand Up @@ -44,4 +52,5 @@ void treehash(unsigned char *root, unsigned char *auth_path,
uint32_t /* addr_idx */, const uint32_t[8] /* tree_addr */),
uint32_t tree_addr[8]);


#endif
2 changes: 1 addition & 1 deletion ref/utilsx1.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void treehashx1(unsigned char *root, unsigned char *auth_path,
void *info)
{
/* This is where we keep the intermediate nodes */
unsigned char stack[tree_height*SPX_N];
SPX_VLA(uint8_t, stack, tree_height*SPX_N);

uint32_t idx;
uint32_t max_idx = (1 << tree_height) - 1;
Expand Down

0 comments on commit 15f872c

Please sign in to comment.