Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 3 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_PRIVATE_KEY_STORAGE_HPP |
| 9 | #define NDN_PRIVATE_KEY_STORAGE_HPP |
| 10 | |
| 11 | #include <string> |
| 12 | #include "../../util/blob.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 13 | #include "../certificate/public-key.hpp" |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 14 | #include "../security-common.hpp" |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 15 | #include "../../name.hpp" |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 16 | |
| 17 | namespace ndn { |
| 18 | |
| 19 | class PrivateKeyStorage { |
Jeff Thompson | a50703f | 2013-09-17 14:24:15 -0700 | [diff] [blame] | 20 | public: |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 21 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 22 | * The virtual destructor. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 23 | */ |
| 24 | virtual |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 25 | ~PrivateKeyStorage() {} |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 26 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 27 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 28 | * Generate a pair of asymmetric keys. |
| 29 | * @param keyName The name of the key pair. |
| 30 | * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA. |
| 31 | * @param keySize The size of the key pair. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 32 | */ |
| 33 | virtual void |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 34 | generateKeyPair(const Name& keyName, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 37 | * Get the public key |
| 38 | * @param keyName The name of public key. |
| 39 | * @return The public key. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 40 | */ |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 41 | virtual ptr_lib::shared_ptr<PublicKey> |
| 42 | getPublicKey(const Name& keyName) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
Jeff Thompson | 4c11b9f | 2013-09-13 11:05:28 -0700 | [diff] [blame] | 45 | * Fetch the private key for keyName and sign the data, returning a signature Blob. |
| 46 | * @param data Pointer to the input byte array. |
| 47 | * @param dataLength The length of data. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 48 | * @param keyName The name of the signing key. |
| 49 | * @param digestAlgorithm the digest algorithm. |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 50 | * @return The signature, or a null pointer if signing fails. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 51 | */ |
| 52 | virtual Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 53 | sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 54 | |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 55 | Blob |
| 56 | sign(const Blob& data, const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) |
| 57 | { |
| 58 | sign(data.buf(), data.size(), keyName, digestAlgorithm); |
| 59 | } |
| 60 | |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 61 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 62 | * Decrypt data. |
| 63 | * @param keyName The name of the decrypting key. |
| 64 | * @param data The byte to be decrypted. |
| 65 | * @param dataLength the length of data. |
| 66 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used. |
| 67 | * @return The decrypted data. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 68 | */ |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 69 | virtual Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 70 | decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false) = 0; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 71 | |
| 72 | Blob |
| 73 | decrypt(const Name& keyName, const Blob& data, bool isSymmetric = false) |
| 74 | { |
| 75 | decrypt(keyName, data.buf(), data.size(), isSymmetric); |
| 76 | } |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 79 | * Encrypt data. |
| 80 | * @param keyName The name of the encrypting key. |
| 81 | * @param data The byte to be encrypted. |
| 82 | * @param dataLength the length of data. |
| 83 | * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric decryption is used. |
| 84 | * @return The encrypted data. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 85 | */ |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 86 | virtual Blob |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame^] | 87 | encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false) = 0; |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 88 | |
| 89 | Blob |
| 90 | encrypt(const Name& keyName, const Blob& data, bool isSymmetric = false) |
| 91 | { |
| 92 | encrypt(keyName, data.buf(), data.size(), isSymmetric); |
| 93 | } |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 96 | * @brief Generate a symmetric key. |
| 97 | * @param keyName The name of the key. |
| 98 | * @param keyType The type of the key, e.g. KEY_TYPE_AES. |
| 99 | * @param keySize The size of the key. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 100 | */ |
| 101 | virtual void |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 102 | generateKey(const Name& keyName, KeyType keyType = KEY_TYPE_AES, int keySize = 256) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 105 | * Check if a particular key exists. |
| 106 | * @param keyName The name of the key. |
| 107 | * @param keyClass The class of the key, e.g. KEY_CLASS_PUBLIC, KEY_CLASS_PRIVATE, or KEY_CLASS_SYMMETRIC. |
| 108 | * @return True if the key exists, otherwise false. |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 109 | */ |
| 110 | virtual bool |
Jeff Thompson | 6c314bc | 2013-09-23 18:09:38 -0700 | [diff] [blame] | 111 | doesKeyExist(const Name& keyName, KeyClass keyClass) = 0; |
Jeff Thompson | 7b79eb6 | 2013-09-12 18:48:29 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | #endif |