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