| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Tests for firmware image library. | 5 * Tests for firmware image library. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 | 10 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 TEST_EQ(rsa->algorithm, key->algorithm, "PublicKeyToRSA() algorithm"); | 38 TEST_EQ(rsa->algorithm, key->algorithm, "PublicKeyToRSA() algorithm"); |
| 39 RSAPublicKeyFree(rsa); | 39 RSAPublicKeyFree(rsa); |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 | 43 |
| 44 static void VerifyDataTest(const VbPublicKey* public_key, | 44 static void VerifyDataTest(const VbPublicKey* public_key, |
| 45 const VbPrivateKey* private_key) { | 45 const VbPrivateKey* private_key) { |
| 46 | 46 |
| 47 const uint8_t test_data[] = "This is some test data to sign."; | 47 const uint8_t test_data[] = "This is some test data to sign."; |
| 48 VbSignature *sig; | 48 VbSignature* sig; |
| 49 RSAPublicKey *rsa; | 49 RSAPublicKey* rsa; |
| 50 | 50 |
| 51 sig = CalculateSignature(test_data, sizeof(test_data), private_key); | 51 sig = CalculateSignature(test_data, sizeof(test_data), private_key); |
| 52 rsa = PublicKeyToRSA(public_key); | 52 rsa = PublicKeyToRSA(public_key); |
| 53 TEST_NEQ(sig && rsa, 0, "VerifyData() prerequisites"); | 53 TEST_NEQ(sig && rsa, 0, "VerifyData() prerequisites"); |
| 54 if (!sig || !rsa) | 54 if (!sig || !rsa) |
| 55 return; | 55 return; |
| 56 | 56 |
| 57 TEST_EQ(VerifyData(test_data, sig, rsa), 0, "VerifyData() ok"); | 57 TEST_EQ(VerifyData(test_data, sig, rsa), 0, "VerifyData() ok"); |
| 58 | 58 |
| 59 sig->sig_size -= 16; | 59 sig->sig_size -= 16; |
| 60 TEST_EQ(VerifyData(test_data, sig, rsa), 1, "VerifyData() wrong sig size"); | 60 TEST_EQ(VerifyData(test_data, sig, rsa), 1, "VerifyData() wrong sig size"); |
| 61 sig->sig_size += 16; | 61 sig->sig_size += 16; |
| 62 | 62 |
| 63 GetSignatureData(sig)[0] ^= 0x5A; | 63 GetSignatureData(sig)[0] ^= 0x5A; |
| 64 TEST_EQ(VerifyData(test_data, sig, rsa), 1, "VerifyData() wrong sig"); | 64 TEST_EQ(VerifyData(test_data, sig, rsa), 1, "VerifyData() wrong sig"); |
| 65 | 65 |
| 66 RSAPublicKeyFree(rsa); | 66 RSAPublicKeyFree(rsa); |
| 67 Free(sig); | 67 Free(sig); |
| 68 } | 68 } |
| 69 | 69 |
| 70 | 70 |
| 71 static void VerifyDigestTest(const VbPublicKey* public_key, |
| 72 const VbPrivateKey* private_key) { |
| 73 |
| 74 const uint8_t test_data[] = "This is some other test data to sign."; |
| 75 VbSignature* sig; |
| 76 RSAPublicKey* rsa; |
| 77 uint8_t* digest; |
| 78 |
| 79 sig = CalculateSignature(test_data, sizeof(test_data), private_key); |
| 80 rsa = PublicKeyToRSA(public_key); |
| 81 digest = DigestBuf(test_data, sizeof(test_data), public_key->algorithm); |
| 82 TEST_NEQ(sig && rsa && digest, 0, "VerifyData() prerequisites"); |
| 83 if (!sig || !rsa || !digest) |
| 84 return; |
| 85 |
| 86 TEST_EQ(VerifyDigest(digest, sig, rsa), 0, "VerifyDigest() ok"); |
| 87 |
| 88 GetSignatureData(sig)[0] ^= 0x5A; |
| 89 TEST_EQ(VerifyDigest(digest, sig, rsa), 1, "VerifyDigest() wrong sig"); |
| 90 |
| 91 RSAPublicKeyFree(rsa); |
| 92 Free(sig); |
| 93 Free(digest); |
| 94 } |
| 95 |
| 96 |
| 71 static void ReSignKernelPreamble(VbKernelPreambleHeader *h, | 97 static void ReSignKernelPreamble(VbKernelPreambleHeader *h, |
| 72 const VbPrivateKey *key) { | 98 const VbPrivateKey *key) { |
| 73 VbSignature *sig = CalculateSignature((const uint8_t*)h, | 99 VbSignature *sig = CalculateSignature((const uint8_t*)h, |
| 74 h->preamble_signature.data_size, key); | 100 h->preamble_signature.data_size, key); |
| 75 | 101 |
| 76 SignatureCopy(&h->preamble_signature, sig); | 102 SignatureCopy(&h->preamble_signature, sig); |
| 77 Free(sig); | 103 Free(sig); |
| 78 } | 104 } |
| 79 | 105 |
| 80 | 106 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 220 } |
| 195 | 221 |
| 196 public_key = PublicKeyReadKeyb(argv[3], key_algorithm, 1); | 222 public_key = PublicKeyReadKeyb(argv[3], key_algorithm, 1); |
| 197 if (!public_key) { | 223 if (!public_key) { |
| 198 fprintf(stderr, "Error reading public_key"); | 224 fprintf(stderr, "Error reading public_key"); |
| 199 return 1; | 225 return 1; |
| 200 } | 226 } |
| 201 | 227 |
| 202 VerifyPublicKeyToRSA(public_key); | 228 VerifyPublicKeyToRSA(public_key); |
| 203 VerifyDataTest(public_key, private_key); | 229 VerifyDataTest(public_key, private_key); |
| 230 VerifyDigestTest(public_key, private_key); |
| 204 VerifyKernelPreambleTest(public_key, private_key); | 231 VerifyKernelPreambleTest(public_key, private_key); |
| 205 | 232 |
| 206 if (public_key) | 233 if (public_key) |
| 207 Free(public_key); | 234 Free(public_key); |
| 208 if (private_key) | 235 if (private_key) |
| 209 Free(private_key); | 236 Free(private_key); |
| 210 | 237 |
| 211 return error_code; | 238 return error_code; |
| 212 } | 239 } |
| OLD | NEW |