blob: ce1d51a0ba4267f44e21d7ee121b26ce6c4d3ddc [file] [log] [blame]
Randall Spanglercce41712011-08-29 15:01:19 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gaurav Shah7d122e22010-02-24 16:41:32 -08002 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
Gaurav Shah7d122e22010-02-24 16:41:32 -08006
7#include <stdio.h>
8
Gaurav Shah5411c7a2010-03-31 10:56:49 -07009#include "cryptolib.h"
Gaurav Shah7d122e22010-02-24 16:41:32 -080010#include "file_keys.h"
Randall Spanglercce41712011-08-29 15:01:19 -070011#include "rsa_padding_test.h"
12#include "test_common.h"
13#include "utility.h"
14
15/* Test valid and invalid signatures */
16static void TestSignatures(RSAPublicKey* key) {
17 int unexpected_success;
18 int i;
19
20 /* The first test signature is valid. */
21 TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, 0,
22 test_message_sha1_hash), 1, "RSA Padding Test valid sig");
23
24 /* All other signatures should fail verification. */
25 unexpected_success = 0;
26 for (i = 1; i < sizeof(signatures) / sizeof(signatures[0]); i++) {
27 if (RSAVerify(key, signatures[i], RSA1024NUMBYTES, 0,
28 test_message_sha1_hash)) {
29 fprintf(stderr, "RSA Padding Test vector %d FAILED!\n", i);
30 unexpected_success++;
31 }
32 }
33 TEST_EQ(unexpected_success, 0, "RSA Padding Test invalid sigs");
34
35}
36
37
38/* Test other error conditions in RSAVerify() */
39static void TestRSAVerify(RSAPublicKey* key) {
40 uint8_t sig[RSA1024NUMBYTES];
41
42 TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, 0,
43 test_message_sha1_hash), 1, "RSAVerify() good");
44 TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES - 1, 0,
45 test_message_sha1_hash), 0, "RSAVerify() sig len");
46 TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, kNumAlgorithms + 1,
47 test_message_sha1_hash), 0, "RSAVerify() invalid alg");
48 TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, 3,
49 test_message_sha1_hash), 0, "RSAVerify() wrong key");
50
51 /* Corrupt the signature near start and end */
52 Memcpy(sig, signatures[0], RSA1024NUMBYTES);
53 sig[3] ^= 0x42;
54 TEST_EQ(RSAVerify(key, sig, RSA1024NUMBYTES, 0, test_message_sha1_hash), 0,
55 "RSAVerify() bad sig");
56
57 Memcpy(sig, signatures[0], RSA1024NUMBYTES);
58 sig[RSA1024NUMBYTES - 3] ^= 0x56;
59 TEST_EQ(RSAVerify(key, sig, RSA1024NUMBYTES, 0, test_message_sha1_hash), 0,
60 "RSAVerify() bad sig end");
61}
62
Gaurav Shah7d122e22010-02-24 16:41:32 -080063
64int main(int argc, char* argv[]) {
Gaurav Shah7d122e22010-02-24 16:41:32 -080065 int error = 0;
66 RSAPublicKey* key;
Randall Spanglercce41712011-08-29 15:01:19 -070067
68 /* Read test key */
Gaurav Shah7d122e22010-02-24 16:41:32 -080069 if (argc != 2) {
70 fprintf(stderr, "Usage: %s <test public key>\n", argv[0]);
71 return 1;
72 }
73 key = RSAPublicKeyFromFile(argv[1]);
74 if (!key) {
75 fprintf(stderr, "Couldn't read RSA public key for the test.\n");
76 return 1;
77 }
78
Randall Spanglercce41712011-08-29 15:01:19 -070079 /* Run tests */
80 TestSignatures(key);
81 TestRSAVerify(key);
Gaurav Shah7d122e22010-02-24 16:41:32 -080082
Randall Spanglercce41712011-08-29 15:01:19 -070083 /* Clean up and exit */
Gaurav Shah259de402010-03-12 17:42:03 -080084 RSAPublicKeyFree(key);
Randall Spanglercce41712011-08-29 15:01:19 -070085
86 if (!gTestSuccess)
87 error = 255;
88
Gaurav Shah7d122e22010-02-24 16:41:32 -080089 return error;
90}