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