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