blob: 51ecbe24672c4acf19de65e2283cfcad3892ce36 [file] [log] [blame]
Yingdi Yu04020922014-01-22 12:46:53 -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"
Yingdi Yu04020922014-01-22 12:46:53 -08009#endif
10
11#include <boost/test/unit_test.hpp>
12
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080013#include "security/key-chain.hpp"
Yingdi Yu04020922014-01-22 12:46:53 -080014#include <cryptopp/rsa.h>
15
16using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080017namespace ndn {
Yingdi Yu04020922014-01-22 12:46:53 -080018
19BOOST_AUTO_TEST_SUITE(TestSecTpmFile)
20
Yingdi Yu28fd32f2014-01-28 19:03:03 -080021BOOST_AUTO_TEST_CASE (Delete)
22{
23 SecTpmFile tpm;
24
25 Name keyName("/tmp/ksk-123456");
Yingdi Yu9a335352014-01-31 11:57:46 -080026 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Yingdi Yu28fd32f2014-01-28 19:03:03 -080027
28 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), true);
29 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), true);
30
31 tpm.deleteKeyPairInTpm(keyName);
32
33 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
34 BOOST_REQUIRE_EQUAL(tpm.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
35}
36
Yingdi Yu04020922014-01-22 12:46:53 -080037BOOST_AUTO_TEST_CASE (SignVerify)
38{
39 SecTpmFile tpm;
40
41 Name keyName("/tmp/ksk-123456");
Yingdi Yu9a335352014-01-31 11:57:46 -080042 BOOST_CHECK_NO_THROW(tpm.generateKeyPairInTpm(keyName, KEY_TYPE_RSA, 2048));
Yingdi Yu04020922014-01-22 12:46:53 -080043
Yingdi Yu04020922014-01-22 12:46:53 -080044 Data data("/tmp/test/1");
45 const uint8_t content[] = {0x01, 0x02, 0x03, 0x04};
46
47 Block sigBlock = tpm.signInTpm(content, sizeof(content), keyName, DIGEST_ALGORITHM_SHA256);
48 ptr_lib::shared_ptr<PublicKey> pubkeyPtr = tpm.getPublicKeyFromTpm(keyName);
49
50 {
51 using namespace CryptoPP;
52
53 RSA::PublicKey publicKey;
54 ByteQueue queue;
55 queue.Put(reinterpret_cast<const byte*>(pubkeyPtr->get().buf()), pubkeyPtr->get().size());
56 publicKey.Load(queue);
57
58 RSASS<PKCS1v15, SHA256>::Verifier verifier (publicKey);
59 bool result = verifier.VerifyMessage(content, sizeof(content),
60 sigBlock.value(), sigBlock.value_size());
61
62 BOOST_REQUIRE_EQUAL(result, true);
63 }
64
Yingdi Yu28fd32f2014-01-28 19:03:03 -080065 tpm.deleteKeyPairInTpm(keyName);
Yingdi Yu04020922014-01-22 12:46:53 -080066}
67
68BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080069
70} // namespace ndn