blob: 54464843a10cfcde4c0a9b647f8f0273027df322 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson7b79eb62013-09-12 18:48:29 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson7b79eb62013-09-12 18:48:29 -07004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07005 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7b79eb62013-09-12 18:48:29 -07006 * See COPYING for copyright and distribution information.
7 */
8
9#ifndef NDN_PRIVATE_KEY_STORAGE_HPP
Jeff Thompsone589c3f2013-10-12 17:30:50 -070010#define NDN_PRIVATE_KEY_STORAGE_HPP
Jeff Thompson7b79eb62013-09-12 18:48:29 -070011
12#include <string>
Jeff Thompson7b79eb62013-09-12 18:48:29 -070013#include "../security-common.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070014#include "../../name.hpp"
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080015#include "../../data.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070016
17namespace ndn {
18
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080019class PublicKey;
20
Jeff Thompson7b79eb62013-09-12 18:48:29 -070021class PrivateKeyStorage {
Jeff Thompsona50703f2013-09-17 14:24:15 -070022public:
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080023 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
24
Jeff Thompson7b79eb62013-09-12 18:48:29 -070025 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070026 * The virtual destructor.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070027 */
28 virtual
Jeff Thompson6c314bc2013-09-23 18:09:38 -070029 ~PrivateKeyStorage() {}
Jeff Thompson7b79eb62013-09-12 18:48:29 -070030
Jeff Thompson7b79eb62013-09-12 18:48:29 -070031 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070032 * Generate a pair of asymmetric keys.
33 * @param keyName The name of the key pair.
34 * @param keyType The type of the key pair, e.g. KEY_TYPE_RSA.
35 * @param keySize The size of the key pair.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070036 */
37 virtual void
Jeff Thompson6c314bc2013-09-23 18:09:38 -070038 generateKeyPair(const Name& keyName, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070039
40 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070041 * Get the public key
42 * @param keyName The name of public key.
43 * @return The public key.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070044 */
Jeff Thompson6c314bc2013-09-23 18:09:38 -070045 virtual ptr_lib::shared_ptr<PublicKey>
46 getPublicKey(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070047
48 /**
Jeff Thompson4c11b9f2013-09-13 11:05:28 -070049 * Fetch the private key for keyName and sign the data, returning a signature Blob.
50 * @param data Pointer to the input byte array.
51 * @param dataLength The length of data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070052 * @param keyName The name of the signing key.
53 * @param digestAlgorithm the digest algorithm.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070054 * @return The signature, or a null pointer if signing fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070055 */
Alexander Afanasyeve64788e2014-01-05 22:38:21 -080056 virtual Block
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080057 sign(const uint8_t *data, size_t dataLength,
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080058 const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) = 0;
59
Alexander Afanasyev04b22a92014-01-05 22:40:17 -080060 virtual void
61 sign(Data &data,
Alexander Afanasyevbf1a67a2014-01-05 23:36:13 -080062 const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) = 0;
63
Jeff Thompson7b79eb62013-09-12 18:48:29 -070064 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070065 * 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 Thompsoned978ee2013-12-13 12:29:43 -080069 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption is used.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070070 * @return The decrypted data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070071 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080072 virtual ConstBufferPtr
Jeff Thompson97223af2013-09-24 17:01:27 -070073 decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070074
Jeff Thompson7b79eb62013-09-12 18:48:29 -070075 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070076 * 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 Thompsoned978ee2013-12-13 12:29:43 -080080 * @param isSymmetric If true symmetric encryption is used, otherwise asymmetric encryption is used.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070081 * @return The encrypted data.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070082 */
Alexander Afanasyev64a3d812014-01-05 23:35:05 -080083 virtual ConstBufferPtr
Jeff Thompson97223af2013-09-24 17:01:27 -070084 encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070085
Jeff Thompson7b79eb62013-09-12 18:48:29 -070086 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070087 * @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 Thompson7b79eb62013-09-12 18:48:29 -070091 */
92 virtual void
Jeff Thompson6c314bc2013-09-23 18:09:38 -070093 generateKey(const Name& keyName, KeyType keyType = KEY_TYPE_AES, int keySize = 256) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070094
95 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070096 * 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 Thompson7b79eb62013-09-12 18:48:29 -0700100 */
101 virtual bool
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700102 doesKeyExist(const Name& keyName, KeyClass keyClass) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700103};
104
105}
106
107#endif