Skip to content

Commit

Permalink
Merge pull request #35 from wolfmcnally/master
Browse files Browse the repository at this point in the history
Use memzero() from bc-crypto-base instead of memset().
  • Loading branch information
wolfmcnally authored Aug 16, 2021
2 parents 329995b + 64b76ce commit 99f409f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 117 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"files.associations": {
"Makefile.in": "makefile",
"*.h": "c"
"*.h": "c",
"ratio": "c"
}
}
}
3 changes: 0 additions & 3 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H

/* Define to 1 if you have the `memset' function. */
#undef HAVE_MEMSET

/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H

Expand Down
96 changes: 9 additions & 87 deletions configure
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/sh
# Guess values for system-dependent variables and create Makefiles.
# Generated by GNU Autoconf 2.69 for bc-shamir 0.2.
# Generated by GNU Autoconf 2.69 for bc-shamir 0.3.
#
#
# Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc.
Expand Down Expand Up @@ -577,8 +577,8 @@ MAKEFLAGS=
# Identity of this package.
PACKAGE_NAME='bc-shamir'
PACKAGE_TARNAME='bc-shamir'
PACKAGE_VERSION='0.2'
PACKAGE_STRING='bc-shamir 0.2'
PACKAGE_VERSION='0.3'
PACKAGE_STRING='bc-shamir 0.3'
PACKAGE_BUGREPORT=''
PACKAGE_URL=''

Expand Down Expand Up @@ -1225,7 +1225,7 @@ if test "$ac_init_help" = "long"; then
# Omit some internal or obsolete options to make the list less imposing.
# This message is too long to be a string in the A/UX 3.1 sh.
cat <<_ACEOF
\`configure' configures bc-shamir 0.2 to adapt to many kinds of systems.
\`configure' configures bc-shamir 0.3 to adapt to many kinds of systems.
Usage: $0 [OPTION]... [VAR=VALUE]...
Expand Down Expand Up @@ -1286,7 +1286,7 @@ fi

if test -n "$ac_init_help"; then
case $ac_init_help in
short | recursive ) echo "Configuration of bc-shamir 0.2:";;
short | recursive ) echo "Configuration of bc-shamir 0.3:";;
esac
cat <<\_ACEOF
Expand Down Expand Up @@ -1366,7 +1366,7 @@ fi
test -n "$ac_init_help" && exit $ac_status
if $ac_init_version; then
cat <<\_ACEOF
bc-shamir configure 0.2
bc-shamir configure 0.3
generated by GNU Autoconf 2.69
Copyright (C) 2012 Free Software Foundation, Inc.
Expand Down Expand Up @@ -1844,78 +1844,11 @@ $as_echo "$ac_res" >&6; }
eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno

} # ac_fn_c_find_uintX_t

# ac_fn_c_check_func LINENO FUNC VAR
# ----------------------------------
# Tests whether FUNC exists, setting the cache variable VAR accordingly
ac_fn_c_check_func ()
{
as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2" >&5
$as_echo_n "checking for $2... " >&6; }
if eval \${$3+:} false; then :
$as_echo_n "(cached) " >&6
else
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
/* Define $2 to an innocuous variant, in case <limits.h> declares $2.
For example, HP-UX 11i <limits.h> declares gettimeofday. */
#define $2 innocuous_$2
/* System header to define __stub macros and hopefully few prototypes,
which can conflict with char $2 (); below.
Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
<limits.h> exists even on freestanding compilers. */
#ifdef __STDC__
# include <limits.h>
#else
# include <assert.h>
#endif
#undef $2
/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char $2 ();
/* The GNU C library defines this for functions which it implements
to always fail with ENOSYS. Some functions are actually named
something starting with __ and the normal name is an alias. */
#if defined __stub_$2 || defined __stub___$2
choke me
#endif
int
main ()
{
return $2 ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
eval "$3=yes"
else
eval "$3=no"
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
fi
eval ac_res=\$$3
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
$as_echo "$ac_res" >&6; }
eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno

} # ac_fn_c_check_func
cat >config.log <<_ACEOF
This file contains any messages produced by compilers while
running configure, to aid debugging if configure makes a mistake.
It was created by bc-shamir $as_me 0.2, which was
It was created by bc-shamir $as_me 0.3, which was
generated by GNU Autoconf 2.69. Invocation command line was
$ $0 $@
Expand Down Expand Up @@ -3863,17 +3796,6 @@ $as_echo "#define malloc rpl_malloc" >>confdefs.h
fi


for ac_func in memset
do :
ac_fn_c_check_func "$LINENO" "memset" "ac_cv_func_memset"
if test "x$ac_cv_func_memset" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_MEMSET 1
_ACEOF

fi
done


ac_config_files="$ac_config_files Makefile src/Makefile test/Makefile"

Expand Down Expand Up @@ -4383,7 +4305,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
# report actual input values of CONFIG_FILES etc. instead of their
# values after options handling.
ac_log="
This file was extended by bc-shamir $as_me 0.2, which was
This file was extended by bc-shamir $as_me 0.3, which was
generated by GNU Autoconf 2.69. Invocation command line was
CONFIG_FILES = $CONFIG_FILES
Expand Down Expand Up @@ -4445,7 +4367,7 @@ _ACEOF
cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
ac_cs_version="\\
bc-shamir config.status 0.2
bc-shamir config.status 0.3
configured by $0, generated by GNU Autoconf 2.69,
with options \\"\$ac_cs_config\\"
Expand Down
3 changes: 1 addition & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([bc-shamir], [0.2])
AC_INIT([bc-shamir], [0.3])
AC_CONFIG_SRCDIR([config.h.in])
AC_CONFIG_HEADERS([config.h])

Expand All @@ -29,7 +29,6 @@ AC_TYPE_UINT8_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_CHECK_FUNCS([memset])

AC_CONFIG_FILES([Makefile
src/Makefile
Expand Down
18 changes: 12 additions & 6 deletions src/hazmat.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@
#include <string.h>
#include "hazmat.h"

#if defined(ARDUINO) || defined(__EMSCRIPTEN__)
#include "bc-crypto-base.h"
#else
#include <bc-crypto-base/bc-crypto-base.h>
#endif

void
bitslice(uint32_t r[8], const uint8_t x[32])
{
memset(r, 0, sizeof(uint32_t[8]));
memzero(r, sizeof(uint32_t[8]));
for (size_t arr_idx = 0; arr_idx < 32; arr_idx++) {
uint32_t cur = (uint32_t) x[arr_idx];
for (size_t bit_idx = 0; bit_idx < 8; bit_idx++) {
Expand All @@ -40,7 +46,7 @@ bitslice(uint32_t r[8], const uint8_t x[32])
void
unbitslice(uint8_t r[32], const uint32_t x[8])
{
memset(r, 0, sizeof(uint8_t[32]));
memzero(r, sizeof(uint8_t[32]));
for (size_t bit_idx = 0; bit_idx < 8; bit_idx++) {
uint32_t cur = (uint32_t) x[bit_idx];
for (size_t arr_idx = 0; arr_idx < 32; arr_idx++) {
Expand Down Expand Up @@ -288,8 +294,8 @@ gf256_inv(uint32_t r[8], uint32_t x[8])
bitslice_setall(x, unbitsliced_x);

/* Calculate y */
memset(y, 0, sizeof(y));
memset(xpow, 0, sizeof(xpow));
memzero(y, sizeof(y));
memzero(xpow, sizeof(xpow));
xpow[0] = ~0;
gf256_add(y, poly0);
for (coeff_idx = 0; coeff_idx < (k-1); coeff_idx++) {
Expand Down Expand Up @@ -323,8 +329,8 @@ gf256_inv(uint32_t r[8], uint32_t x[8])

/* Use Lagrange basis polynomials to calculate the secret coefficient */
for (idx1 = 0; idx1 < k; idx1++) {
memset(num, 0, sizeof(num));
memset(denom, 0, sizeof(denom));
memzero(num, sizeof(num));
memzero(denom, sizeof(denom));
num[0] = ~0; /* num is the numerator (=1) */
denom[0] = ~0; /* denom is the numerator (=1) */
for (idx2 = 0; idx2 < k; idx2++) {
Expand Down
24 changes: 15 additions & 9 deletions src/interpolate.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
#include "hazmat.h"
#include "shamir-constants.h"

#if defined(ARDUINO) || defined(__EMSCRIPTEN__)
#include "bc-crypto-base.h"
#else
#include <bc-crypto-base/bc-crypto-base.h>
#endif

/*
* calculate the lagrange basis coefficients for the lagrange polynomial
* defined byt the x coordinates xc at the value x.
Expand Down Expand Up @@ -40,7 +46,7 @@ hazmat_lagrange_basis(uint8_t *values,

uint32_t numerator[8], denominator[8], temp[8];

memset(xx, 0, sizeof(xx));
memzero(xx, sizeof(xx));
for(i=0;i<n; ++i) {
xx[i] = xc[i];
}
Expand Down Expand Up @@ -138,7 +144,7 @@ int16_t interpolate(
uint8_t yv[SHAMIR_MAX_SECRET_SIZE*n];
uint8_t values[SHAMIR_MAX_SECRET_SIZE];

memset(yv,0,SHAMIR_MAX_SECRET_SIZE*n);
memzero(yv,SHAMIR_MAX_SECRET_SIZE*n);
for(uint8_t i=0; i<n; i++) {
y[i] = &yv[SHAMIR_MAX_SECRET_SIZE*i];
memcpy(y[i], yij[i], yl);
Expand All @@ -163,13 +169,13 @@ int16_t interpolate(
memcpy(result, values, yl);

// clean up stack
memset(lagrange, 0 , sizeof(lagrange));
memset(y_slice, 0, sizeof(y_slice));
memset(result_slice, 0, sizeof(result_slice));
memset(temp, 0, sizeof(temp));
memset(y, 0, sizeof(y));
memset(yv, 0, sizeof(yv));
memset(values, 0, sizeof(values));
memzero(lagrange, sizeof(lagrange));
memzero(y_slice, sizeof(y_slice));
memzero(result_slice, sizeof(result_slice));
memzero(temp, sizeof(temp));
memzero(y, sizeof(y));
memzero(yv, sizeof(yv));
memzero(values, sizeof(values));

return yl;
}
16 changes: 8 additions & 8 deletions src/shamir.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ int32_t split_secret(
}
}

memset(digest, 0, sizeof(digest));
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
memzero(digest, sizeof(digest));
memzero(x, sizeof(x));
memzero(y, sizeof(y));
}
return shard_count;
}
Expand Down Expand Up @@ -130,9 +130,9 @@ int32_t recover_secret(
if( interpolate(threshold, x, share_length, shares, DIGEST_INDEX, digest) < 0 ||
interpolate(threshold, x, share_length, shares, SECRET_INDEX, secret) < 0
) {
memset(secret, 0, sizeof(digest));
memset(digest, 0, sizeof(digest));
memset(verify, 0, sizeof(verify));
memzero(secret, sizeof(digest));
memzero(digest, sizeof(digest));
memzero(verify, sizeof(verify));

return SHAMIR_ERROR_INTERPOLATION_FAILURE;
}
Expand All @@ -144,8 +144,8 @@ int32_t recover_secret(
}


memset(digest, 0, sizeof(digest));
memset(verify, 0, sizeof(verify));
memzero(digest, sizeof(digest));
memzero(verify, sizeof(verify));

if(!valid) {
return SHAMIR_ERROR_CHECKSUM_FAILURE;
Expand Down

0 comments on commit 99f409f

Please sign in to comment.