Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ build_script:
cp test.exe test-stock.exe
cp timing.exe timing-stock.exe
nmake -f makefile.msvc clean
nmake -f makefile.msvc all CFLAGS="/Ox /DUSE_LTM /DLTM_DESC /DLTC_NO_AES_NI /I../libtommath"
nmake -f makefile.msvc all CFLAGS="/Ox /DUSE_LTM /DLTM_DESC /DLTC_NO_ACCEL /I../libtommath"
test_script:
- cmd: >-
test-stock.exe
test.exe
timing-stock.exe cipher_ecb
timing.exe cipher_ecb
timing-stock.exe cipher_ecb aes
timing.exe cipher_ecb aes
timing-stock.exe hash sha
timing.exe hash sha
30 changes: 25 additions & 5 deletions demos/timing.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ static prng_state yarrow_prng;

static const char *filter_arg;

static LTC_INLINE int should_skip(const char *name)
{
if (name && filter_arg && strstr(name, filter_arg) == NULL)
return 1;
return 0;
}

static struct list {
int id;
ulong64 spd1, spd2, avg;
Expand Down Expand Up @@ -187,6 +194,9 @@ static void time_cipher_ecb(void)
fprintf(stderr, "\n\nECB Time Trials for the Symmetric Ciphers:\n");
no_results = 0;
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
if (should_skip(cipher_descriptor[x].name))
continue;

ecb_start(x, key, cipher_descriptor[x].min_key_length, 0, &ecb);

/* sanity check on cipher */
Expand Down Expand Up @@ -260,6 +270,9 @@ static void time_cipher_cbc(void)
fprintf(stderr, "\n\nCBC Time Trials for the Symmetric Ciphers:\n");
no_results = 0;
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
if (should_skip(cipher_descriptor[x].name))
continue;

cbc_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, &cbc);

/* sanity check on cipher */
Expand Down Expand Up @@ -333,6 +346,9 @@ static void time_cipher_ctr(void)
fprintf(stderr, "\n\nCTR Time Trials for the Symmetric Ciphers:\n");
no_results = 0;
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
if (should_skip(cipher_descriptor[x].name))
continue;

ctr_start(x, pt, key, cipher_descriptor[x].min_key_length, 0, CTR_COUNTER_LITTLE_ENDIAN, &ctr);

/* sanity check on cipher */
Expand Down Expand Up @@ -407,6 +423,9 @@ static void time_cipher_lrw(void)
no_results = 0;
for (x = 0; cipher_descriptor[x].name != NULL; x++) {
if (cipher_descriptor[x].block_length != 16) continue;
if (should_skip(cipher_descriptor[x].name))
continue;

lrw_start(x, pt, key, cipher_descriptor[x].min_key_length, key, 0, &lrw);

/* sanity check on cipher */
Expand Down Expand Up @@ -485,8 +504,7 @@ static void time_hash(void)
fprintf(stderr, "\n\nHASH Time Trials for:\n");
no_results = 0;
for (x = 0; hash_descriptor[x].name != NULL; x++) {

if (filter_arg && strstr(hash_descriptor[x].name, filter_arg) == NULL)
if (should_skip(hash_descriptor[x].name))
continue;

/* sanity check on hash */
Expand Down Expand Up @@ -601,8 +619,10 @@ static void time_prng(void)
unsigned long x, y;
int err;

fprintf(stderr, "Timing PRNGs (cycles/byte output, cycles add_entropy (32 bytes) :\n");
fprintf(stderr, "Timing PRNGs - cycles/byte output, cycles add_entropy (32 bytes) :\n");
for (x = 0; prng_descriptor[x].name != NULL; x++) {
if (should_skip(prng_descriptor[x].name))
continue;

/* sanity check on prng */
if ((err = prng_descriptor[x].test()) != CRYPT_OK) {
Expand All @@ -625,7 +645,7 @@ static void time_prng(void)
t1 = (t_read() - t1)>>1;
if (t1 < t2) t2 = t1;
}
fprintf(stderr, "%20s: %5"PRI64"u ", prng_descriptor[x].name, t2>>12);
fprintf(stderr, "%20s: %5"PRI64"u, ", prng_descriptor[x].name, t2>>12);
#undef DO2
#undef DO1

Expand Down Expand Up @@ -1381,7 +1401,7 @@ static void LTC_NORETURN die(int status)
"Run timing tests of all built-in algorithms, or only the one given in <alg>.\n\n"
"\talg\tThe algorithms to test. Use the '-l' option to check for valid values.\n"
"\tmpi\tThe MPI provider to use.\n"
"\tfilter\tFilter within the algorithm class (currently only for 'hash'es).\n"
"\tfilter\tFilter within the algorithm class (currently only for 'cipher's, 'hash'es, and 'prng's).\n"
"\t-l\tList all built-in algorithms that can be timed.\n"
"\t-h\tThe help you're looking at.\n\n"
"Examples:\n"
Expand Down
2 changes: 1 addition & 1 deletion helper.pl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ sub check_descriptor {
my @descriptors;
find({ wanted => sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir=>1 }, "./src/${which}/");
for my $f (@src) {
my @n = map { my $x = $_; $x =~ s/^.*?ltc_${what}_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_${what}_descriptor/ } split /\n/, read_file($f);
my @n = map { my $x = $_; $x =~ s/^.*?ltc_${what}_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /^[^()]*ltc_${what}_descriptor/ } split /\n/, read_file($f);
push @descriptors, @n if @n;
}
my $fails = 0;
Expand Down
24 changes: 24 additions & 0 deletions libtomcrypt_VS2008.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,14 @@
RelativePath="src\hashes\sha1.c"
>
</File>
<File
RelativePath="src\hashes\sha1_desc.c"
>
</File>
<File
RelativePath="src\hashes\sha1_x86.c"
>
</File>
<File
RelativePath="src\hashes\sha3.c"
>
Expand Down Expand Up @@ -934,10 +942,26 @@
RelativePath="src\hashes\sha2\sha224.c"
>
</File>
<File
RelativePath="src\hashes\sha2\sha224_desc.c"
>
</File>
<File
RelativePath="src\hashes\sha2\sha224_x86.c"
>
</File>
<File
RelativePath="src\hashes\sha2\sha256.c"
>
</File>
<File
RelativePath="src\hashes\sha2\sha256_desc.c"
>
</File>
<File
RelativePath="src\hashes\sha2\sha256_x86.c"
>
</File>
<File
RelativePath="src\hashes\sha2\sha384.c"
>
Expand Down
4 changes: 3 additions & 1 deletion makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ src/hashes/blake2b.o src/hashes/blake2s.o src/hashes/chc/chc.o src/hashes/helper
src/hashes/helper/hash_filehandle.o src/hashes/helper/hash_memory.o \
src/hashes/helper/hash_memory_multi.o src/hashes/md2.o src/hashes/md4.o src/hashes/md5.o \
src/hashes/rmd128.o src/hashes/rmd160.o src/hashes/rmd256.o src/hashes/rmd320.o src/hashes/sha1.o \
src/hashes/sha2/sha224.o src/hashes/sha2/sha256.o src/hashes/sha2/sha384.o src/hashes/sha2/sha512.o \
src/hashes/sha1_desc.o src/hashes/sha1_x86.o src/hashes/sha2/sha224.o src/hashes/sha2/sha224_desc.o \
src/hashes/sha2/sha224_x86.o src/hashes/sha2/sha256.o src/hashes/sha2/sha256_desc.o \
src/hashes/sha2/sha256_x86.o src/hashes/sha2/sha384.o src/hashes/sha2/sha512.o \
src/hashes/sha2/sha512_224.o src/hashes/sha2/sha512_256.o src/hashes/sha3.o src/hashes/sha3_test.o \
src/hashes/tiger.o src/hashes/whirl/whirl.o src/mac/blake2/blake2bmac.o \
src/mac/blake2/blake2bmac_file.o src/mac/blake2/blake2bmac_memory.o \
Expand Down
4 changes: 3 additions & 1 deletion makefile.msvc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ src/hashes/blake2b.obj src/hashes/blake2s.obj src/hashes/chc/chc.obj src/hashes/
src/hashes/helper/hash_filehandle.obj src/hashes/helper/hash_memory.obj \
src/hashes/helper/hash_memory_multi.obj src/hashes/md2.obj src/hashes/md4.obj src/hashes/md5.obj \
src/hashes/rmd128.obj src/hashes/rmd160.obj src/hashes/rmd256.obj src/hashes/rmd320.obj src/hashes/sha1.obj \
src/hashes/sha2/sha224.obj src/hashes/sha2/sha256.obj src/hashes/sha2/sha384.obj src/hashes/sha2/sha512.obj \
src/hashes/sha1_desc.obj src/hashes/sha1_x86.obj src/hashes/sha2/sha224.obj src/hashes/sha2/sha224_desc.obj \
src/hashes/sha2/sha224_x86.obj src/hashes/sha2/sha256.obj src/hashes/sha2/sha256_desc.obj \
src/hashes/sha2/sha256_x86.obj src/hashes/sha2/sha384.obj src/hashes/sha2/sha512.obj \
src/hashes/sha2/sha512_224.obj src/hashes/sha2/sha512_256.obj src/hashes/sha3.obj src/hashes/sha3_test.obj \
src/hashes/tiger.obj src/hashes/whirl/whirl.obj src/mac/blake2/blake2bmac.obj \
src/mac/blake2/blake2bmac_file.obj src/mac/blake2/blake2bmac_memory.obj \
Expand Down
4 changes: 3 additions & 1 deletion makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ src/hashes/blake2b.o src/hashes/blake2s.o src/hashes/chc/chc.o src/hashes/helper
src/hashes/helper/hash_filehandle.o src/hashes/helper/hash_memory.o \
src/hashes/helper/hash_memory_multi.o src/hashes/md2.o src/hashes/md4.o src/hashes/md5.o \
src/hashes/rmd128.o src/hashes/rmd160.o src/hashes/rmd256.o src/hashes/rmd320.o src/hashes/sha1.o \
src/hashes/sha2/sha224.o src/hashes/sha2/sha256.o src/hashes/sha2/sha384.o src/hashes/sha2/sha512.o \
src/hashes/sha1_desc.o src/hashes/sha1_x86.o src/hashes/sha2/sha224.o src/hashes/sha2/sha224_desc.o \
src/hashes/sha2/sha224_x86.o src/hashes/sha2/sha256.o src/hashes/sha2/sha256_desc.o \
src/hashes/sha2/sha256_x86.o src/hashes/sha2/sha384.o src/hashes/sha2/sha512.o \
src/hashes/sha2/sha512_224.o src/hashes/sha2/sha512_256.o src/hashes/sha3.o src/hashes/sha3_test.o \
src/hashes/tiger.o src/hashes/whirl/whirl.o src/mac/blake2/blake2bmac.o \
src/mac/blake2/blake2bmac_file.o src/mac/blake2/blake2bmac_memory.o \
Expand Down
7 changes: 6 additions & 1 deletion makefile_include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ src/hashes/blake2b.o src/hashes/blake2s.o src/hashes/chc/chc.o src/hashes/helper
src/hashes/helper/hash_filehandle.o src/hashes/helper/hash_memory.o \
src/hashes/helper/hash_memory_multi.o src/hashes/md2.o src/hashes/md4.o src/hashes/md5.o \
src/hashes/rmd128.o src/hashes/rmd160.o src/hashes/rmd256.o src/hashes/rmd320.o src/hashes/sha1.o \
src/hashes/sha2/sha224.o src/hashes/sha2/sha256.o src/hashes/sha2/sha384.o src/hashes/sha2/sha512.o \
src/hashes/sha1_desc.o src/hashes/sha1_x86.o src/hashes/sha2/sha224.o src/hashes/sha2/sha224_desc.o \
src/hashes/sha2/sha224_x86.o src/hashes/sha2/sha256.o src/hashes/sha2/sha256_desc.o \
src/hashes/sha2/sha256_x86.o src/hashes/sha2/sha384.o src/hashes/sha2/sha512.o \
src/hashes/sha2/sha512_224.o src/hashes/sha2/sha512_256.o src/hashes/sha3.o src/hashes/sha3_test.o \
src/hashes/tiger.o src/hashes/whirl/whirl.o src/mac/blake2/blake2bmac.o \
src/mac/blake2/blake2bmac_file.o src/mac/blake2/blake2bmac_memory.o \
Expand Down Expand Up @@ -516,6 +518,9 @@ install_test: $(call print-help,install_test,Installs the self-test binary) test
install_hooks: $(call print-help,install_hooks,Installs the git hooks)
for s in `ls hooks/`; do ln -s ../../hooks/$$s .git/hooks/$$s; done

uninstall_hooks: $(call print-help,uninstall_hooks,Uninstalls the git hooks)
for s in `ls hooks/`; do rm .git/hooks/$$s; done

HEADER_FILES=$(notdir $(HEADERS_PUB))
.common_uninstall:
$(UNINSTALL_CMD) $(DESTDIR)$(LIBPATH)/$(LIBNAME)
Expand Down
6 changes: 6 additions & 0 deletions sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ src/hashes/rmd160.c
src/hashes/rmd256.c
src/hashes/rmd320.c
src/hashes/sha1.c
src/hashes/sha1_desc.c
src/hashes/sha1_x86.c
src/hashes/sha2/sha224.c
src/hashes/sha2/sha224_desc.c
src/hashes/sha2/sha224_x86.c
src/hashes/sha2/sha256.c
src/hashes/sha2/sha256_desc.c
src/hashes/sha2/sha256_x86.c
src/hashes/sha2/sha384.c
src/hashes/sha2/sha512.c
src/hashes/sha2/sha512_224.c
Expand Down
67 changes: 19 additions & 48 deletions src/hashes/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define LTC_SMALL_STACK_SHA1
#endif

const struct ltc_hash_descriptor sha1_desc =
const struct ltc_hash_descriptor sha1_portable_desc =
{
"sha1",
2,
Expand All @@ -29,10 +29,10 @@ const struct ltc_hash_descriptor sha1_desc =
{ 1, 3, 14, 3, 2, 26, },
6,

&sha1_init,
&sha1_process,
&sha1_done,
&sha1_test,
&sha1_c_init,
&sha1_c_process,
&sha1_c_done,
&sha1_c_test,
NULL
};

Expand All @@ -42,9 +42,9 @@ const struct ltc_hash_descriptor sha1_desc =
#define F3(x,y,z) (x ^ y ^ z)

#ifdef LTC_CLEAN_STACK
static int ss_sha1_compress(hash_state *md, const unsigned char *buf)
static int ss_sha1_c_compress(hash_state *md, const unsigned char *buf)
#else
static int s_sha1_compress(hash_state *md, const unsigned char *buf)
static int s_sha1_c_compress(hash_state *md, const unsigned char *buf)
#endif
{
ulong32 a,b,c,d,e,i;
Expand Down Expand Up @@ -170,10 +170,10 @@ static int s_sha1_compress(hash_state *md, const unsigned char *buf)
}

#ifdef LTC_CLEAN_STACK
static int s_sha1_compress(hash_state *md, const unsigned char *buf)
static int s_sha1_c_compress(hash_state *md, const unsigned char *buf)
{
int err;
err = ss_sha1_compress(md, buf);
err = ss_sha1_c_compress(md, buf);
burn_stack(sizeof(ulong32) * 87);
return err;
}
Expand All @@ -184,9 +184,12 @@ static int s_sha1_compress(hash_state *md, const unsigned char *buf)
@param md The hash state you wish to initialize
@return CRYPT_OK if successful
*/
int sha1_init(hash_state * md)
int sha1_c_init(hash_state * md)
{
LTC_ARGCHK(md != NULL);

md->sha1.state = LTC_ALIGN_BUF(md->sha1.state_buf, 16);

md->sha1.state[0] = 0x67452301UL;
md->sha1.state[1] = 0xefcdab89UL;
md->sha1.state[2] = 0x98badcfeUL;
Expand All @@ -204,15 +207,15 @@ int sha1_init(hash_state * md)
@param inlen The length of the data (octets)
@return CRYPT_OK if successful
*/
HASH_PROCESS(sha1_process, s_sha1_compress, sha1, 64)
HASH_PROCESS(sha1_c_process, s_sha1_c_compress, sha1, 64)

/**
Terminate the hash to get the digest
@param md The hash state
@param out [out] The destination of the hash (20 bytes)
@return CRYPT_OK if successful
*/
int sha1_done(hash_state * md, unsigned char *out)
int sha1_c_done(hash_state * md, unsigned char *out)
{
int i;

Expand All @@ -237,7 +240,7 @@ int sha1_done(hash_state * md, unsigned char *out)
while (md->sha1.curlen < 64) {
md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
}
s_sha1_compress(md, md->sha1.buf);
s_sha1_c_compress(md, md->sha1.buf);
md->sha1.curlen = 0;
}

Expand All @@ -248,7 +251,7 @@ int sha1_done(hash_state * md, unsigned char *out)

/* store length */
STORE64H(md->sha1.length, md->sha1.buf+56);
s_sha1_compress(md, md->sha1.buf);
s_sha1_c_compress(md, md->sha1.buf);

/* copy output */
for (i = 0; i < 5; i++) {
Expand All @@ -264,41 +267,9 @@ int sha1_done(hash_state * md, unsigned char *out)
Self-test the hash
@return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
*/
int sha1_test(void)
int sha1_c_test(void)
{
#ifndef LTC_TEST
return CRYPT_NOP;
#else
static const struct {
const char *msg;
unsigned char hash[20];
} tests[] = {
{ "abc",
{ 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
0x9c, 0xd0, 0xd8, 0x9d }
},
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
{ 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
0xE5, 0x46, 0x70, 0xF1 }
}
};

int i;
unsigned char tmp[20];
hash_state md;

for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
sha1_init(&md);
sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
sha1_done(&md, tmp);
if (ltc_compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA1", i)) {
return CRYPT_FAIL_TESTVECTOR;
}
}
return CRYPT_OK;
#endif
return sha1_test_desc(&sha1_portable_desc, "SHA1 portable");
}

#undef F0
Expand Down
Loading
Loading