mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2026-01-09 00:51:29 +00:00
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>
This commit is contained in:
@@ -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"
|
||||
|
||||
/* 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 i;
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user