blob: 7a92ec1285df5d754baf7d08bee7d9f49e6820ec [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
109 std::string
110 nameTransform(const std::string &keyName, const std::string &extension);
Yingdi Yu4b752752014-02-18 12:24:03 -0800111 virtual bool
112 generateRandomBlock(uint8_t* res, size_t size);
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800113
114private:
115 void
116 maintainMapping(std::string str1, std::string str2);
117
118private:
119 class Impl;
Yingdi Yu4b752752014-02-18 12:24:03 -0800120 shared_ptr<Impl> m_impl;
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800121};
Yingdi Yu4b752752014-02-18 12:24:03 -0800122
Yingdi Yu2d9c50f2014-01-21 18:25:00 -0800123}//ndn
124
125#endif