Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 6 | |
| 7 | #include <stdio.h> |
| 8 | |
Gaurav Shah | 5411c7a | 2010-03-31 10:56:49 -0700 | [diff] [blame] | 9 | #include "cryptolib.h" |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 10 | #include "file_keys.h" |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame^] | 11 | #include "rsa_padding_test.h" |
| 12 | #include "test_common.h" |
| 13 | #include "utility.h" |
| 14 | |
| 15 | /* Test valid and invalid signatures */ |
| 16 | static 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() */ |
| 39 | static 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 Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 63 | |
| 64 | int main(int argc, char* argv[]) { |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 65 | int error = 0; |
| 66 | RSAPublicKey* key; |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame^] | 67 | |
| 68 | /* Read test key */ |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 69 | 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 Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame^] | 79 | /* Run tests */ |
| 80 | TestSignatures(key); |
| 81 | TestRSAVerify(key); |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 82 | |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame^] | 83 | /* Clean up and exit */ |
Gaurav Shah | 259de40 | 2010-03-12 17:42:03 -0800 | [diff] [blame] | 84 | RSAPublicKeyFree(key); |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame^] | 85 | |
| 86 | if (!gTestSuccess) |
| 87 | error = 255; |
| 88 | |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 89 | return error; |
| 90 | } |