blob: 8c57dc07f33eaa8eba27354cc3ac0d9567cd87b0 [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"
14#include <cryptopp/rsa.h>
15
16using namespace std;
17using namespace ndn;
18
19
20BOOST_AUTO_TEST_SUITE(TestSecTpmOsx)
21
22BOOST_AUTO_TEST_CASE (Delete)
23{
24 SecTpmOsx tpm;
25
26 Name keyName("/tmp/ksk-123456");
27 tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048);
28
29 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
30 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
31
32 tpm.deleteKeyPairInTpm(keyName);
33
34 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
35 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
36}
37
38BOOST_AUTO_TEST_CASE (SignVerify)
39{
40 SecTpmOsx tpm;
41
42 Name keyName("/tmp/ksk-123456");
43 tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048);
44
45 Data data("/tmp/test/1");
46 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
47
48 Block sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256);
49 ptr_lib::shared_ptr<PublicKey> pubkeyPtr = tpm.getPublicKeyFromTpm(keyName);
50
51 {
52 using namespace CryptoPP;
53
54 RSA::PublicKey publicKey;
55 ByteQueue queue;
56 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
57 publicKey.Load(queue);
58
59 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
60 bool result = verifier.VerifyMessage(content, sizeof(content),
61 sigBlock.value(), sigBlock.value_size());
62
63 BOOST_REQUIRE_EQUAL(result, true);
64 }
65
66 tpm.deleteKeyPairInTpm(keyName);
67}
68
69BOOST_AUTO_TEST_SUITE_END()