summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2011-08-29 15:01:19 -0700
committerRandall Spangler <rspangler@chromium.org>2011-08-29 15:20:11 -0700
commitcce4171545faa02611c1db6ca1c554370385b012 (patch)
tree7d489baa8ee2c311635a16a8abd2071a472ee249
parenta8c7fab879f0cb2ae64b3cef3a50200d32c1deac (diff)
downloadvboot-cce4171545faa02611c1db6ca1c554370385b012.tar.gz
Add more tests for rsa.c.
BUG=chromium-os:17564 TEST=make && make runtests Change-Id: Ib1cdb139b695a648fc500ad61c58efc86c7940a9 (cherry picked from commit 5178f3ce82020e8caa287ceee47f9556c0b3bae8) Reviewed-on: http://gerrit.chromium.org/gerrit/6860 Reviewed-by: Gaurav Shah <gauravsh@chromium.org> Tested-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--tests/rsa_padding_test.c82
1 files changed, 63 insertions, 19 deletions
diff --git a/tests/rsa_padding_test.c b/tests/rsa_padding_test.c
index 4ccb4b44..ce1d51a0 100644
--- a/tests/rsa_padding_test.c
+++ b/tests/rsa_padding_test.c
@@ -1,19 +1,71 @@
-/* Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
-#include "rsa_padding_test.h"
#include <stdio.h>
#include "cryptolib.h"
#include "file_keys.h"
+#include "rsa_padding_test.h"
+#include "test_common.h"
+#include "utility.h"
-int main(int argc, char* argv[]) {
+/* Test valid and invalid signatures */
+static void TestSignatures(RSAPublicKey* key) {
+ int unexpected_success;
int i;
+
+ /* The first test signature is valid. */
+ TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, 0,
+ test_message_sha1_hash), 1, "RSA Padding Test valid sig");
+
+ /* All other signatures should fail verification. */
+ unexpected_success = 0;
+ for (i = 1; i < sizeof(signatures) / sizeof(signatures[0]); i++) {
+ if (RSAVerify(key, signatures[i], RSA1024NUMBYTES, 0,
+ test_message_sha1_hash)) {
+ fprintf(stderr, "RSA Padding Test vector %d FAILED!\n", i);
+ unexpected_success++;
+ }
+ }
+ TEST_EQ(unexpected_success, 0, "RSA Padding Test invalid sigs");
+
+}
+
+
+/* Test other error conditions in RSAVerify() */
+static void TestRSAVerify(RSAPublicKey* key) {
+ uint8_t sig[RSA1024NUMBYTES];
+
+ TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, 0,
+ test_message_sha1_hash), 1, "RSAVerify() good");
+ TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES - 1, 0,
+ test_message_sha1_hash), 0, "RSAVerify() sig len");
+ TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, kNumAlgorithms + 1,
+ test_message_sha1_hash), 0, "RSAVerify() invalid alg");
+ TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, 3,
+ test_message_sha1_hash), 0, "RSAVerify() wrong key");
+
+ /* Corrupt the signature near start and end */
+ Memcpy(sig, signatures[0], RSA1024NUMBYTES);
+ sig[3] ^= 0x42;
+ TEST_EQ(RSAVerify(key, sig, RSA1024NUMBYTES, 0, test_message_sha1_hash), 0,
+ "RSAVerify() bad sig");
+
+ Memcpy(sig, signatures[0], RSA1024NUMBYTES);
+ sig[RSA1024NUMBYTES - 3] ^= 0x56;
+ TEST_EQ(RSAVerify(key, sig, RSA1024NUMBYTES, 0, test_message_sha1_hash), 0,
+ "RSAVerify() bad sig end");
+}
+
+
+int main(int argc, char* argv[]) {
int error = 0;
RSAPublicKey* key;
+
+ /* Read test key */
if (argc != 2) {
fprintf(stderr, "Usage: %s <test public key>\n", argv[0]);
return 1;
@@ -24,23 +76,15 @@ int main(int argc, char* argv[]) {
return 1;
}
- /* The first test signature is valid. */
- if (!RSAVerify(key, signatures[0], RSA1024NUMBYTES, 0,
- test_message_sha1_hash)) {
- fprintf(stderr, "RSA Padding Test vector 0 FAILED!\n");
- error = 255; /* Test failure. */
- }
- /* All other signatures should fail verification. */
- for (i = 1; i < sizeof(signatures) / sizeof(signatures[0]); i++) {
- if (RSAVerify(key, signatures[i], RSA1024NUMBYTES, 0,
- test_message_sha1_hash)) {
- fprintf(stderr, "RSA Padding Test vector %d FAILED!\n", i);
- error = 255; /* Test failure. */
- }
- }
- if (!error)
- fprintf(stderr, "RSA Padding Test PASSED for all test vectors.");
+ /* Run tests */
+ TestSignatures(key);
+ TestRSAVerify(key);
+ /* Clean up and exit */
RSAPublicKeyFree(key);
+
+ if (!gTestSuccess)
+ error = 255;
+
return error;
}