blob: f1e6fa8dd216725ad45b188ea9842669f37bcbca [file] [log] [blame]
Yingdi Yu2d9c50f2014-01-21 18:25:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Xingyu Ma <maxy12@cs.ucla.edu>
5 * Yingdi Yu <yingdi@cs.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
9#ifndef NDN_SEC_TPM_FILE_HPP
10#define NDN_SEC_TPM_FILE_HPP
11
12#include "../common.hpp"
13
14#include "sec-tpm.hpp"
15
16namespace ndn
17{
18
19class SecTpmFile : public SecTpm
20{
21public:
22 struct Error : public SecTpm::Error { Error(const std::string &what) : SecTpm::Error(what) {} };
23
24 SecTpmFile(const std::string & dir = "");
25
26 /**
27 * @brief destructor
28 */
29 virtual
30 ~SecTpmFile() {};
31
32 /**
33 * Generate a pair of asymmetric keys.
34 * @param keyName The name of the key pair.
35 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
36 * @param keySize The size of the key pair.
37 */
38 virtual void
39 generateKeyPairInTpm(const Name & keyName, KeyType keyType, int keySize);
40
41 /**
Yingdi Yu28fd32f2014-01-28 19:03:03 -080042 * Delete a key pair of asymmetric keys.
43 * @param keyName The name of the key pair.
44 */
45 virtual void
46 deleteKeyPairInTpm(const Name &keyName);
47
48 /**
Yingdi Yu2d9c50f2014-01-21 18:25:00 -080049 * Get the public key
50 * @param keyName The name of public key.
51 * @return The public key.
52 */
53 virtual ptr_lib::shared_ptr<PublicKey>
54 getPublicKeyFromTpm(const Name & keyName);
55
56 /**
57 * Fetch the private key for keyName and sign the data, returning a signature block.
58 * Throw Error if signing fails.
59 * @param data Pointer to the input byte array.
60 * @param dataLength The length of data.
61 * @param keyName The name of the signing key.
62 * @param digestAlgorithm the digest algorithm.
63 * @return The signature block.
64 */
65 virtual Block
66 signInTpm(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm);
67
68 /**
69 * Decrypt data.
70 * @param keyName The name of the decrypting key.
71 * @param data The byte to be decrypted.
72 * @param dataLength the length of data.
73 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption is used.
74 * @return The decrypted data.
75 */
76 virtual ConstBufferPtr
77 decryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
78
79 /**
80 * Encrypt data.
81 * @param keyName The name of the encrypting key.
82 * @param data The byte to be encrypted.
83 * @param dataLength the length of data.
84 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used.
85 * @return The encrypted data.
86 */
87 virtual ConstBufferPtr
88 encryptInTpm(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric);
89
90
91 /**
92 * Generate a symmetric key.
93 * @param keyName The name of the key.
94 * @param keyType The type of the key, e.g. KEY_TYPE_AES.
95 * @param keySize The size of the key.
96 */
97 virtual void
98 generateSymmetricKeyInTpm(const Name& keyName, KeyType keyType, int keySize);
99
100 /**
101 * Check if a particular key exists.
102 * @param keyName The name of the key.
103 * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC.
104 * @return True if the key exists, otherwise false.
105 */
106 virtual bool
107 doesKeyExistInTpm(const Name& keyName, KeyClass keyClass);
108
Yingdi Yu4b752752014-02-18 12:24:03 -0800109 virtual bool
110 generateRandomBlock(uint8_t* res, size_t size);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800111
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800112protected:
113 /******************************
114 * From TrustedPlatformModule *
115 ******************************/
116 virtual ConstBufferPtr
117 exportPrivateKeyPkcs1FromTpm(const Name& keyName);
118
119 virtual bool
120 importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800121
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800122 virtual bool
123 importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
124
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800125private:
126 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800127 shared_ptr<Impl> m_impl;
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800128};
Yingdi Yu4b752752014-02-18 12:24:03 -0800129
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800130}//ndn
131
132#endif