blob: 7a94f33a85b448aefcec0a00576049e2ab8c3dda [file] [log] [blame]
Yingdi Yu28fd32f2014-01-28 19:03:03 -08001/**
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 Yube4150e2014-02-18 13:02:46 -080014#include "util/time.hpp"
Yingdi Yu28fd32f2014-01-28 19:03:03 -080015#include <cryptopp/rsa.h>
Yingdi Yube4150e2014-02-18 13:02:46 -080016#include <cryptopp/files.h>
17#include <cryptopp/hex.h>
Yingdi Yu28fd32f2014-01-28 19:03:03 -080018
19using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080020namespace ndn {
Yingdi Yu28fd32f2014-01-28 19:03:03 -080021
22BOOST_AUTO_TEST_SUITE(TestSecTpmOsx)
23
24BOOST_AUTO_TEST_CASE (Delete)
25{
26 SecTpmOsx tpm;
27
Yingdi Yube4150e2014-02-18 13:02:46 -080028 Name keyName("/TestSecTpmOsx/Delete/ksk-123456");
Yingdi Yu9a335352014-01-31 11:57:46 -080029 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080030
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
40BOOST_AUTO_TEST_CASE (SignVerify)
41{
42 SecTpmOsx tpm;
43
Yingdi Yube4150e2014-02-18 13:02:46 -080044 Name keyName("/TestSecTpmOsx/SignVerify/ksk-123456");
Yingdi Yu9a335352014-01-31 11:57:46 -080045 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080046
Yingdi Yube4150e2014-02-18 13:02:46 -080047 Data data("/TestSecTpmOsx/SignVaerify/Data/1");
Yingdi Yu28fd32f2014-01-28 19:03:03 -080048 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
49
50 Block sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256);
Yingdi Yube4150e2014-02-18 13:02:46 -080051 shared_ptr<PublicKey> pubkeyPtr = tpm.getPublicKeyFromTpm(keyName);
Yingdi Yu28fd32f2014-01-28 19:03:03 -080052
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 Yube4150e2014-02-18 13:02:46 -080063 sigBlock.value(), sigBlock.value_size());
Yingdi Yu28fd32f2014-01-28 19:03:03 -080064
65 BOOST_REQUIRE_EQUAL(result, true);
66 }
67
68 tpm.deleteKeyPairInTpm(keyName);
69}
70
Yingdi Yu4b752752014-02-18 12:24:03 -080071BOOST_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 Yu8dceb1d2014-02-18 12:45:10 -080091
92BOOST_AUTO_TEST_CASE (ExportImportKey)
93{
94 using namespace CryptoPP;
95
96 SecTpmOsx tpm;
97
Yingdi Yube4150e2014-02-18 13:02:46 -080098 Name keyName("/TestSecTpmOsx/ExportImportKey/ksk-" + boost::lexical_cast<string>(time::now()));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080099
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 Yube4150e2014-02-18 13:02:46 -0800105 ConstBufferPtr exported = tpm.exportPrivateKeyPkcs8FromTpm(keyName, "1234");
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800106 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 Yube4150e2014-02-18 13:02:46 -0800113 BOOST_REQUIRE(tpm.importPrivateKeyPkcs8IntoTpm(keyName, exported->buf(), exported->size(), "1234"));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800114
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 Yu28fd32f2014-01-28 19:03:03 -0800142BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800143
144} // namespace ndn