Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Yingdi Yu <yingdi0@cs.ucla.edu> |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #if __clang__ |
| 8 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 9 | #endif |
| 10 | |
| 11 | #include <boost/test/unit_test.hpp> |
| 12 | |
| 13 | #include "security/key-chain.hpp" |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 14 | #include "util/time.hpp" |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 15 | #include <cryptopp/rsa.h> |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 16 | #include <cryptopp/files.h> |
| 17 | #include <cryptopp/hex.h> |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 18 | |
| 19 | using namespace std; |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 20 | namespace ndn { |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 21 | |
| 22 | BOOST_AUTO_TEST_SUITE(TestSecTpmOsx) |
| 23 | |
| 24 | BOOST_AUTO_TEST_CASE (Delete) |
| 25 | { |
| 26 | SecTpmOsx tpm; |
| 27 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 28 | Name keyName("/TestSecTpmOsx/Delete/ksk-123456"); |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 29 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048)); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 30 | |
| 31 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true); |
| 32 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true); |
| 33 | |
| 34 | tpm.deleteKeyPairInTpm(keyName); |
| 35 | |
| 36 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false); |
| 37 | BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false); |
| 38 | } |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE (SignVerify) |
| 41 | { |
| 42 | SecTpmOsx tpm; |
| 43 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 44 | Name keyName("/TestSecTpmOsx/SignVerify/ksk-123456"); |
Yingdi Yu | 9a33535 | 2014-01-31 11:57:46 -0800 | [diff] [blame] | 45 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048)); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 46 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 47 | Data data("/TestSecTpmOsx/SignVaerify/Data/1"); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 48 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
| 49 | |
| 50 | Block sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256); |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 51 | shared_ptr<PublicKey> pubkeyPtr = tpm.getPublicKeyFromTpm(keyName); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 52 | |
| 53 | { |
| 54 | using namespace CryptoPP; |
| 55 | |
| 56 | RSA::PublicKey publicKey; |
| 57 | ByteQueue queue; |
| 58 | queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size()); |
| 59 | publicKey.Load(queue); |
| 60 | |
| 61 | RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey); |
| 62 | bool result = verifier.VerifyMessage(content, sizeof(content), |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 63 | sigBlock.value(), sigBlock.value_size()); |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 64 | |
| 65 | BOOST_REQUIRE_EQUAL(result, true); |
| 66 | } |
| 67 | |
| 68 | tpm.deleteKeyPairInTpm(keyName); |
| 69 | } |
| 70 | |
Yingdi Yu | 4b75275 | 2014-02-18 12:24:03 -0800 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE (RandomGenerator) |
| 72 | { |
| 73 | SecTpmOsx tpm; |
| 74 | |
| 75 | size_t scale = 1000; |
| 76 | size_t size = 256 * scale; |
| 77 | uint8_t* block = new uint8_t[size]; |
| 78 | tpm.generateRandomBlock(block, size); |
| 79 | |
| 80 | map<uint8_t, int> counter; |
| 81 | for(size_t i = 0; i < size; i++) |
| 82 | counter[block[i]] += 1; |
| 83 | |
| 84 | float dev = 0.0; |
| 85 | for(size_t i = 0; i != 255; i++) |
| 86 | dev += ((counter[i] - scale) * (counter[i] - scale)) * 1.0 / (scale * scale); |
| 87 | |
| 88 | BOOST_CHECK_CLOSE(dev / 256, 0.001, 100); |
| 89 | |
| 90 | } |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 91 | |
| 92 | BOOST_AUTO_TEST_CASE (ExportImportKey) |
| 93 | { |
| 94 | using namespace CryptoPP; |
| 95 | |
| 96 | SecTpmOsx tpm; |
| 97 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 98 | Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" + boost::lexical_cast<string>(time::now())); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 99 | |
| 100 | BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048)); |
| 101 | |
| 102 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true); |
| 103 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true); |
| 104 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 105 | ConstBufferPtr exported = tpm.exportPrivateKeyPkcs8FromTpm(keyName, "1234"); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 106 | shared_ptr<PublicKey> pubkeyPtr = tpm.getPublicKeyFromTpm(keyName); |
| 107 | |
| 108 | tpm.deleteKeyPairInTpm(keyName); |
| 109 | |
| 110 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false); |
| 111 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false); |
| 112 | |
Yingdi Yu | be4150e | 2014-02-18 13:02:46 -0800 | [diff] [blame^] | 113 | BOOST_REQUIRE(tpm.importPrivateKeyPkcs8IntoTpm(keyName, exported->buf(), exported->size(), "1234")); |
Yingdi Yu | 8dceb1d | 2014-02-18 12:45:10 -0800 | [diff] [blame] | 114 | |
| 115 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == true); |
| 116 | BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == true); |
| 117 | |
| 118 | const uint8_t content[] = {0x01, 0x02, 0x03, 0x04}; |
| 119 | Block sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256); |
| 120 | |
| 121 | { |
| 122 | using namespace CryptoPP; |
| 123 | |
| 124 | RSA::PublicKey publicKey; |
| 125 | ByteQueue queue; |
| 126 | queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size()); |
| 127 | publicKey.Load(queue); |
| 128 | |
| 129 | RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey); |
| 130 | bool result = verifier.VerifyMessage(content, sizeof(content), |
| 131 | sigBlock.value(), sigBlock.value_size()); |
| 132 | |
| 133 | BOOST_REQUIRE_EQUAL(result, true); |
| 134 | } |
| 135 | |
| 136 | tpm.deleteKeyPairInTpm(keyName); |
| 137 | // This is some problem related to Mac OS Key chain, and we will fix it later. |
| 138 | // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE) == false); |
| 139 | // BOOST_REQUIRE(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC) == false); |
| 140 | } |
| 141 | |
Yingdi Yu | 28fd32f | 2014-01-28 19:03:03 -0800 | [diff] [blame] | 142 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 143 | |
| 144 | } // namespace ndn |