commit fc56b8b449d6be99c47447eea1060e8c2b898dde Author: Zack Weinberg zackw@panix.com Date: Tue Jul 19 11:40:20 2011 -0700
Refactor crypt digest logic to reduce code duplication and overhead. Fix memory leaks in unittest_crypt.c and unittest.c. --- src/crypt.c | 55 ++++++++------------------ src/test/unittest.c | 5 ++- src/test/unittest_crypt.c | 96 ++++++++++++++++++++------------------------ 3 files changed, 65 insertions(+), 91 deletions(-)
diff --git a/src/crypt.c b/src/crypt.c index e338214..9019fc9 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -70,7 +70,13 @@ cleanup_crypto(void) Digests ===== */
-#ifdef USE_OPENSSL_SHA256 +#ifndef USE_OPENSSL_SHA256 +#define SHA256_CTX sha256_state +#define SHA256_Init(ctx) sha256_init(ctx) +#define SHA256_Update(ctx, buf, len) sha256_process(ctx, buf, len) +#define SHA256_Final(buf, ctx) sha256_done(ctx, buf) +#endif + struct digest_t { SHA256_CTX ctx; }; @@ -102,44 +108,17 @@ digest_update(digest_t *d, const uchar *buf, size_t len) size_t digest_getdigest(digest_t *d, uchar *buf, size_t len) { - uchar tmp[SHA256_LENGTH]; - int n = 32; - SHA256_Final(tmp, &d->ctx); - if (len < 32) - n = len; - memcpy(buf, tmp, n); - memset(tmp, 0, sizeof(tmp)); - return n; -} -#else -struct digest_t { - sha256_state ctx; -}; -digest_t * -digest_new(void) -{ - digest_t *d = xmalloc(sizeof(digest_t)); - sha256_init(&d->ctx); - return d; -} -void -digest_update(digest_t *d, const uchar *buf, size_t len) -{ - sha256_process(&d->ctx, buf, len); -} -size_t -digest_getdigest(digest_t *d, uchar *buf, size_t len) -{ - uchar tmp[SHA256_LENGTH]; - int n = 32; - sha256_done(&d->ctx, tmp); - if (len < 32) - n = len; - memcpy(buf, tmp, n); - memset(tmp, 0, sizeof(tmp)); - return n; + if (len >= SHA256_LENGTH) { + SHA256_Final(buf, &d->ctx); + return SHA256_LENGTH; + } else { + uchar tmp[SHA256_LENGTH]; + SHA256_Final(tmp, &d->ctx); + memcpy(buf, tmp, len); + memset(tmp, 0, SHA256_LENGTH); + return len; + } } -#endif
void digest_free(digest_t *d) diff --git a/src/test/unittest.c b/src/test/unittest.c index 8974c87..f872550 100644 --- a/src/test/unittest.c +++ b/src/test/unittest.c @@ -21,6 +21,9 @@ struct testgroup_t groups[] = { int main(int argc, const char **argv) { + int rv; initialize_crypto(); - return tinytest_main(argc, argv, groups); + rv = tinytest_main(argc, argv, groups); + cleanup_crypto(); + return rv; } diff --git a/src/test/unittest_crypt.c b/src/test/unittest_crypt.c index aef4ead..112da20 100644 --- a/src/test/unittest_crypt.c +++ b/src/test/unittest_crypt.c @@ -12,63 +12,55 @@ static void test_crypt_hashvec(void *data) { + struct testvec + { + const char *input; + const char *output; + }; + static const struct testvec testvecs[] = { + /* All test vectors taken from http://csrc.nist.gov/groups/STM/cavp/#03 + Note: code below relies on there being no NUL bytes in any input. */ + /* 0 bits */ + { "", + "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24" + "\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55" }, + /* 256 bits */ + { "\x8c\xf5\x3d\x90\x07\x7d\xf9\xa0\x43\xbf\x8d\x10\xb4\x70\xb1\x44" + "\x78\x44\x11\xc9\x3a\x4d\x50\x45\x56\x83\x4d\xae\x3e\xa4\xa5\xbb", + "\x56\x05\x9e\x8c\xb3\xc2\x97\x8b\x19\x82\x08\xbf\x5c\xa1\xe1\xea" + "\x56\x59\xb7\x37\xa5\x06\x32\x4b\x7c\xec\x75\xb5\xeb\xaf\x05\x7d" }, + /* 1304 bits */ + { "\xeb\xac\xcc\x34\xd6\xd6\xd3\xd2\x1e\xd0\xad\x2b\xa7\xc0\x7c\x21" + "\xd2\x53\xc4\x81\x4f\x4a\xd8\x9d\x32\x36\x92\x37\x49\x7f\x47\xa1" + "\xad\xab\xfa\x23\x98\xdd\xd0\x9d\x76\x9c\xc4\x6d\x3f\xd6\x9c\x93" + "\x03\x25\x1c\x13\xc7\x50\x79\x9b\x8f\x15\x11\x66\xbc\x26\x58\x60" + "\x98\x71\x16\x8b\x30\xa4\xd0\xa1\x62\xf1\x83\xfb\x36\x0f\x99\xb1" + "\x72\x81\x15\x03\x68\x1a\x11\xf8\x13\xc1\x6a\x44\x62\x72\xba\x6f" + "\xd4\x85\x86\x34\x45\x33\xb9\x28\x08\x56\x51\x9c\x35\x70\x59\xc3" + "\x44\xef\x17\x18\xdb\xaf\x86\xfa\xe5\xc1\x07\x99\xe4\x6b\x53\x16" + "\x88\x6f\xb4\xe6\x80\x90\x75\x78\x90\x53\x96\x17\xe4\x03\xc5\x11" + "\xa4\xf7\x8a\x19\xc8\x18\xc2\xea\x2e\x9d\x4e\x2d\xe9\x19\x0c\x9d" + "\xdd\xb8\x06", + "\xc9\x07\x18\x04\x43\xde\xe3\xcb\xcc\xb4\xc3\x13\x28\xe6\x25\x15" + "\x85\x27\xa5\x93\xb8\x78\xde\x1b\x8e\x4b\xa3\x7f\x1d\x69\xfb\x66" }, + { NULL, NULL } + }; + digest_t *d; uchar output[32]; - - /* First SHA256 test vector: - Test for '\x00' */ - d = digest_new(); - digest_update(d, (unsigned char*)"", 0); - digest_getdigest(d, output, 32); - tt_mem_op(output, ==, - "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8" - "\x99\x6f\xb9\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c" - "\xa4\x95\x99\x1b\x78\x52\xb8\x55", 32); - - /* Second SHA256 test vector: - Test for the 256-bit entry of: - http://csrc.nist.gov/groups/STM/cavp/#03 */ - d = digest_new(); - digest_update(d, - (unsigned char*)"\x8c\xf5\x3d\x90\x07\x7d\xf9\xa0\x43\xbf\x8d" - "\x10\xb4\x70\xb1\x44\x78\x44\x11\xc9\x3a\x4d\x50\x45\x56\x83" - "\x4d\xae\x3e\xa4\xa5\xbb", 32); - digest_getdigest(d, output, 32); - tt_mem_op(output, ==, - "\x56\x05\x9e\x8c\xb3\xc2\x97\x8b\x19\x82\x08\xbf" - "\x5c\xa1\xe1\xea\x56\x59\xb7\x37\xa5\x06\x32\x4b" - "\x7c\xec\x75\xb5\xeb\xaf\x05\x7d", 32); - - /* Third SHA test vector: - Test for the 1304-bit entry of: - http://csrc.nist.gov/groups/STM/cavp/#03 */ - d = digest_new(); - digest_update(d, - (unsigned char*)"\xeb\xac\xcc\x34\xd6\xd6\xd3\xd2\x1e\xd0\xad" - "\x2b\xa7\xc0\x7c\x21\xd2\x53\xc4\x81\x4f\x4a\xd8\x9d\x32\x36" - "\x92\x37\x49\x7f\x47\xa1\xad\xab\xfa\x23\x98\xdd\xd0\x9d\x76" - "\x9c\xc4\x6d\x3f\xd6\x9c\x93\x03\x25\x1c\x13\xc7\x50\x79\x9b" - "\x8f\x15\x11\x66\xbc\x26\x58\x60\x98\x71\x16\x8b\x30\xa4\xd0" - "\xa1\x62\xf1\x83\xfb\x36\x0f\x99\xb1\x72\x81\x15\x03\x68\x1a" - "\x11\xf8\x13\xc1\x6a\x44\x62\x72\xba\x6f\xd4\x85\x86\x34\x45" - "\x33\xb9\x28\x08\x56\x51\x9c\x35\x70\x59\xc3\x44\xef\x17\x18" - "\xdb\xaf\x86\xfa\xe5\xc1\x07\x99\xe4\x6b\x53\x16\x88\x6f\xb4" - "\xe6\x80\x90\x75\x78\x90\x53\x96\x17\xe4\x03\xc5\x11\xa4\xf7" - "\x8a\x19\xc8\x18\xc2\xea\x2e\x9d\x4e\x2d\xe9\x19\x0c\x9d\xdd" - "\xb8\x06", 163); - digest_getdigest(d, output, 32); - tt_mem_op(output, ==, - "\xc9\x07\x18\x04\x43\xde\xe3\xcb\xcc\xb4\xc3\x13" - "\x28\xe6\x25\x15\x85\x27\xa5\x93\xb8\x78\xde\x1b" - "\x8e\x4b\xa3\x7f\x1d\x69\xfb\x66", 32); + int i; + for (i = 0; testvecs[i].input; i++) { + d = digest_new(); + digest_update(d, (unsigned char *) testvecs[i].input, + strlen(testvecs[i].input)); + digest_getdigest(d, output, 32); + digest_free(d); + tt_mem_op(output, ==, testvecs[i].output, 32); + }
/* XXX Try doing init, update, update, output. */ - /* XXX add a base16-decode function so we can implement a tt_mem_op or - something */
- end: - if (d) - digest_free(d); + end:; }
static void
tor-commits@lists.torproject.org