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 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 7 | #include <stdint.h> |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | |
Gaurav Shah | 5411c7a | 2010-03-31 10:56:49 -0700 | [diff] [blame] | 10 | #include "cryptolib.h" |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 11 | #include "file_keys.h" |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame] | 12 | #include "rsa_padding_test.h" |
| 13 | #include "test_common.h" |
| 14 | #include "utility.h" |
| 15 | |
| 16 | /* Test valid and invalid signatures */ |
| 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() */ |
| 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"); |
Randall Spangler | 9c9606b | 2011-08-30 16:44:44 -0700 | [diff] [blame] | 47 | TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, kNumAlgorithms, |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame] | 48 | test_message_sha1_hash), 0, "RSAVerify() invalid alg"); |
| 49 | TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, 3, |
Randall Spangler | 9c9606b | 2011-08-30 16:44:44 -0700 | [diff] [blame] | 50 | test_message_sha1_hash), 0, "RSAVerify() wrong alg"); |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame] | 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 | |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 64 | |
| 65 | int main(int argc, char* argv[]) { |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 66 | int error = 0; |
| 67 | RSAPublicKey* key; |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame] | 68 | |
| 69 | /* Read test key */ |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 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 | |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame] | 80 | /* Run tests */ |
| 81 | TestSignatures(key); |
| 82 | TestRSAVerify(key); |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 83 | |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame] | 84 | /* Clean up and exit */ |
Gaurav Shah | 259de40 | 2010-03-12 17:42:03 -0800 | [diff] [blame] | 85 | RSAPublicKeyFree(key); |
Randall Spangler | cce4171 | 2011-08-29 15:01:19 -0700 | [diff] [blame] | 86 | |
| 87 | if (!gTestSuccess) |
| 88 | error = 255; |
| 89 | |
Gaurav Shah | 7d122e2 | 2010-02-24 16:41:32 -0800 | [diff] [blame] | 90 | return error; |
| 91 | } |