Meng-Huan Yu | 4cbaddc | 2019-05-08 18:31:53 +0800 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Wei-Cheng Xiao | 174e16f | 2020-02-20 16:12:55 +0800 | [diff] [blame] | 5 | #include <limits> |
Meng-Huan Yu | 4cbaddc | 2019-05-08 18:31:53 +0800 | [diff] [blame] | 6 | #include <memory> |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 10 | #include <base/check.h> |
Meng-Huan Yu | 4cbaddc | 2019-05-08 18:31:53 +0800 | [diff] [blame] | 11 | #include <base/strings/string_number_conversions.h> |
| 12 | #include <base/optional.h> |
| 13 | #include <base/logging.h> |
| 14 | #include <crypto/scoped_openssl_types.h> |
| 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | #include "libhwsec/crypto_utility.h" |
| 19 | |
| 20 | using testing::_; |
| 21 | using testing::NiceMock; |
| 22 | using testing::Return; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // RSA 1024 pre-generated key |
| 27 | // Public key: DER encoded SubjectPublicKeyInfo |
| 28 | // Private key: DER encoded PKCS#1 RSAPrivateKey |
| 29 | constexpr char kRsaDerPrivateKey[] = |
| 30 | "3082025c02010002818100c080fd814be63035ca6bd264a5b300ceea9e93702d66ebf0c0c3" |
| 31 | "cfd21a287c9195491225887b931d51a8b28813ecb2a256d427b13502b563103070d7de6ef8" |
| 32 | "e0dce3b48987926d576fa6136d9209e1da6fe3f59be83d879054c1e2233c5f28c4a426978e" |
| 33 | "97e18390c99e32f7e1ffd4187774c6939d15b5663c14ed08e741ea7b020301000102818025" |
| 34 | "2840c6764a06403bc43826293db6083a8d45543fcf3ff7869dc53d7ec315d85c0270b841f1" |
| 35 | "e8619e637ba27c8611acf8299747c33db4995c849d236dd1e9c3d7219ebaf3f90774144839" |
| 36 | "0284653102cac0bfbdd2da07903a71b62d4b4577136b87ba5f837c6e70baa3284610a2a770" |
| 37 | "598382e24e6b23c99824dc0ad0cec761024100fe441c16fc098a311b2b08f74e2ac58e269f" |
| 38 | "d12e6a72d4e83d5e0bb2c00ba71db7889481349367507883ebf6d82bfd1bb68e506ddc0472" |
| 39 | "57aa7ae8c9364d28d3024100c1d10ed2bcfea3806e60c991123727781fe9256332ead379de" |
| 40 | "9b1290599fd57f34ebed4adf0943da2349bc3bbf8ae7519181f4ac81db96be302bb8681140" |
| 41 | "aeb902407b3d5df4120339b4e57b2d6458c1c87146ed4d8156dd03aef117a549e565808199" |
| 42 | "d49c4e27c28e5fe599e384087101c42deebab314e21eddb8e6cbefc2df113b0240212930bd" |
| 43 | "13c3099c76c62e9698a4412787662f946a68cd2803d34a78d22ccbad891378b51fb6091af0" |
| 44 | "80b42910dba63a49880fa1d85206d7a18c496fdccbd159024100d932619513fe956c64428f" |
| 45 | "2dda042d3e20353b1b29b2e863b917708091866c47bf7a4492607af8eba66d8d7bf45d6ada" |
| 46 | "4d19aea9004c05449b8c6369a109e081"; |
| 47 | constexpr char kRsaDerPublicKey[] = |
| 48 | "30819f300d06092a864886f70d010101050003818d0030818902818100c080fd814be63035" |
| 49 | "ca6bd264a5b300ceea9e93702d66ebf0c0c3cfd21a287c9195491225887b931d51a8b28813" |
| 50 | "ecb2a256d427b13502b563103070d7de6ef8e0dce3b48987926d576fa6136d9209e1da6fe3" |
| 51 | "f59be83d879054c1e2233c5f28c4a426978e97e18390c99e32f7e1ffd4187774c6939d15b5" |
| 52 | "663c14ed08e741ea7b0203010001"; |
| 53 | |
| 54 | // EC P-256 (prime256v1) pre-generated key |
| 55 | // Public key: DER encoded SubjectPublicKeyInfo |
| 56 | // Private key: DER encoded ECPrivateKey |
| 57 | constexpr char kEcDerPrivateKey[] = |
| 58 | "307702010104207e1e647025d7bbf93ce583b88a0e554a22c5d711ea3754e39f9c6fabb2b6" |
| 59 | "6b6ba00a06082a8648ce3d030107a14403420004b6a397746f0cad8f1cdf1cb1ddafabe35e" |
| 60 | "65836a1a33a0f4b13ff5b0319cdb9f120b1e7cf733bcf0cdc490c251c982845e8326070e27" |
| 61 | "f007c82767acab1c2700"; |
| 62 | constexpr char kEcDerPublicKey[] = |
| 63 | "3059301306072a8648ce3d020106082a8648ce3d03010703420004b6a397746f0cad8f1cdf" |
| 64 | "1cb1ddafabe35e65836a1a33a0f4b13ff5b0319cdb9f120b1e7cf733bcf0cdc490c251c982" |
| 65 | "845e8326070e27f007c82767acab1c2700"; |
| 66 | |
| 67 | std::vector<uint8_t> HexDecode(const std::string& hex) { |
| 68 | std::vector<uint8_t> output; |
| 69 | CHECK(base::HexStringToBytes(hex, &output)); |
| 70 | return output; |
| 71 | } |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | namespace hwsec { |
| 76 | |
| 77 | class CryptoUtilityTest : public testing::Test { |
| 78 | public: |
| 79 | ~CryptoUtilityTest() override = default; |
| 80 | |
| 81 | void SetUp() override { |
| 82 | SetupPreGeneratedRsaKey(); |
| 83 | SetupPreGeneratedEcKey(); |
| 84 | } |
| 85 | |
| 86 | void SetupPreGeneratedRsaKey() { |
| 87 | std::vector<uint8_t> der_key = HexDecode(kRsaDerPrivateKey); |
| 88 | const unsigned char* buf = der_key.data(); |
| 89 | rsa_key_.reset(d2i_RSAPrivateKey(nullptr, &buf, der_key.size())); |
| 90 | } |
| 91 | |
| 92 | void SetupPreGeneratedEcKey() { |
| 93 | std::vector<uint8_t> der_key = HexDecode(kEcDerPrivateKey); |
| 94 | const unsigned char* buf = der_key.data(); |
| 95 | ecc_key_.reset(d2i_ECPrivateKey(nullptr, &buf, der_key.size())); |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | crypto::ScopedRSA rsa_key_; |
| 100 | crypto::ScopedEC_KEY ecc_key_; |
| 101 | }; |
| 102 | |
Wei-Cheng Xiao | 174e16f | 2020-02-20 16:12:55 +0800 | [diff] [blame] | 103 | TEST_F(CryptoUtilityTest, CreateSecureRandomBlobBadLength) { |
| 104 | static_assert(sizeof(size_t) >= sizeof(int), "size_t is smaller than int!"); |
| 105 | size_t int_max = static_cast<size_t>(std::numeric_limits<int>::max()); |
| 106 | EXPECT_EQ(CreateSecureRandomBlob(int_max + 1).size(), 0); |
| 107 | } |
| 108 | |
Meng-Huan Yu | 4cbaddc | 2019-05-08 18:31:53 +0800 | [diff] [blame] | 109 | TEST_F(CryptoUtilityTest, PreGeneratedKeyIsValid) { |
| 110 | EXPECT_TRUE(rsa_key_); |
| 111 | EXPECT_TRUE(ecc_key_); |
| 112 | } |
| 113 | |
| 114 | TEST_F(CryptoUtilityTest, RsaKeyToSubjectPublicKeyInfoBytesSuccess) { |
| 115 | auto opt_public_key = RsaKeyToSubjectPublicKeyInfoBytes(rsa_key_); |
| 116 | EXPECT_TRUE(opt_public_key); |
| 117 | EXPECT_EQ(*opt_public_key, HexDecode(kRsaDerPublicKey)); |
| 118 | } |
| 119 | |
| 120 | TEST_F(CryptoUtilityTest, RsaKeyToSubjectPublicKeyInfoBytesFailWithNullptr) { |
| 121 | EXPECT_FALSE(RsaKeyToSubjectPublicKeyInfoBytes(nullptr)); |
| 122 | } |
| 123 | |
| 124 | TEST_F(CryptoUtilityTest, EccKeyToSubjectPublicKeyInfoBytesSuccess) { |
| 125 | auto opt_public_key = EccKeyToSubjectPublicKeyInfoBytes(ecc_key_); |
| 126 | EXPECT_TRUE(opt_public_key); |
| 127 | EXPECT_EQ(*opt_public_key, HexDecode(kEcDerPublicKey)); |
| 128 | } |
| 129 | |
| 130 | TEST_F(CryptoUtilityTest, EccKeyToSubjectPublicKeyInfoBytesFailWithNullptr) { |
| 131 | EXPECT_FALSE(EccKeyToSubjectPublicKeyInfoBytes(nullptr)); |
| 132 | } |
| 133 | |
| 134 | } // namespace hwsec |