blob: 2f2423b92db1c25ce2414c0108b07b534e513714 [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
Bill Richardson0c3ba242013-03-29 11:09:30 -07007#include <stdint.h>
Gaurav Shah7d122e22010-02-24 16:41:32 -08008#include <stdio.h>
9
Gaurav Shah5411c7a2010-03-31 10:56:49 -070010#include "cryptolib.h"
Gaurav Shah7d122e22010-02-24 16:41:32 -080011#include "file_keys.h"
Randall Spanglercce41712011-08-29 15:01:19 -070012#include "rsa_padding_test.h"
13#include "test_common.h"
14#include "utility.h"
15
16/* Test valid and invalid signatures */
17static 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() */
40static 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 Spangler9c9606b2011-08-30 16:44:44 -070047 TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, kNumAlgorithms,
Randall Spanglercce41712011-08-29 15:01:19 -070048 test_message_sha1_hash), 0, "RSAVerify() invalid alg");
49 TEST_EQ(RSAVerify(key, signatures[0], RSA1024NUMBYTES, 3,
Randall Spangler9c9606b2011-08-30 16:44:44 -070050 test_message_sha1_hash), 0, "RSAVerify() wrong alg");
Randall Spanglercce41712011-08-29 15:01:19 -070051
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 Shah7d122e22010-02-24 16:41:32 -080064
65int main(int argc, char* argv[]) {
Gaurav Shah7d122e22010-02-24 16:41:32 -080066 int error = 0;
67 RSAPublicKey* key;
Randall Spanglercce41712011-08-29 15:01:19 -070068
69 /* Read test key */
Gaurav Shah7d122e22010-02-24 16:41:32 -080070 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 Spanglercce41712011-08-29 15:01:19 -070080 /* Run tests */
81 TestSignatures(key);
82 TestRSAVerify(key);
Gaurav Shah7d122e22010-02-24 16:41:32 -080083
Randall Spanglercce41712011-08-29 15:01:19 -070084 /* Clean up and exit */
Gaurav Shah259de402010-03-12 17:42:03 -080085 RSAPublicKeyFree(key);
Randall Spanglercce41712011-08-29 15:01:19 -070086
87 if (!gTestSuccess)
88 error = 255;
89
Gaurav Shah7d122e22010-02-24 16:41:32 -080090 return error;
91}