diff options
author | Michael Brown <mcb30@ipxe.org> | 2012-01-11 10:38:28 +0000 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2012-01-11 10:38:28 +0000 |
commit | 18d288728114745211584249de930a9b1c7bcdcd (patch) | |
tree | 8c496e50526e5ac396cd11b78a42ab3cf54e1750 /src/tests/digest_test.c | |
parent | 187cd80106ad2f24e0d6f66ca1fec466f1f37a6b (diff) | |
download | ipxe-18d288728114745211584249de930a9b1c7bcdcd.zip ipxe-18d288728114745211584249de930a9b1c7bcdcd.tar.gz ipxe-18d288728114745211584249de930a9b1c7bcdcd.tar.bz2 |
[test] Add self-tests for SHA-1 algorithm
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/tests/digest_test.c')
-rw-r--r-- | src/tests/digest_test.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/tests/digest_test.c b/src/tests/digest_test.c new file mode 100644 index 0000000..7c2b4a4 --- /dev/null +++ b/src/tests/digest_test.c @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +FILE_LICENCE ( GPL2_OR_LATER ); + +/** @file + * + * Digest self-tests + * + */ + +#include <string.h> +#include <ipxe/crypto.h> +#include "digest_test.h" + +/** + * Test digest algorithm + * + * @v digest Digest algorithm + * @v fragments Digest test fragment list, or NULL + * @v data Test data + * @v len Length of test data + * @v expected Expected digest value + * @ret ok Digest value is as expected + */ +int digest_test ( struct digest_algorithm *digest, + struct digest_test_fragments *fragments, + void *data, size_t len, void *expected ) { + uint8_t ctx[digest->ctxsize]; + uint8_t out[digest->digestsize]; + size_t frag_len = 0; + unsigned int i; + + /* Initialise digest */ + digest_init ( digest, ctx ); + + /* Update digest fragment-by-fragment */ + for ( i = 0 ; len && ( i < ( sizeof ( fragments->len ) / + sizeof ( fragments->len[0] ) ) ) ; i++ ) { + if ( fragments ) + frag_len = fragments->len[i]; + if ( ( frag_len == 0 ) || ( frag_len < len ) ) + frag_len = len; + digest_update ( digest, ctx, data, frag_len ); + data += frag_len; + len -= frag_len; + } + + /* Finalise digest */ + digest_final ( digest, ctx, out ); + + /* Compare against expected output */ + return ( memcmp ( expected, out, sizeof ( out ) ) == 0 ); +} |