blob: 8917c92f5a3f63ce75ae0454a031fe8807710724 [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
10#define NDN_PRIVATE_KEY_STORAGE_HPP
11
12#include <string>
13#include "../../util/blob.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070014#include "../certificate/public-key.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070015#include "../security-common.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070016#include "../../name.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070017
18namespace ndn {
19
20class PrivateKeyStorage {
Jeff Thompsona50703f2013-09-17 14:24:15 -070021public:
Jeff Thompson7b79eb62013-09-12 18:48:29 -070022 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070023 * The virtual destructor.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070024 */
25 virtual
Jeff Thompson6c314bc2013-09-23 18:09:38 -070026 ~PrivateKeyStorage() {}
Jeff Thompson7b79eb62013-09-12 18:48:29 -070027
Jeff Thompson7b79eb62013-09-12 18:48:29 -070028 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070029 * 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 Thompson7b79eb62013-09-12 18:48:29 -070033 */
34 virtual void
Jeff Thompson6c314bc2013-09-23 18:09:38 -070035 generateKeyPair(const Name& keyName, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070036
37 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070038 * Get the public key
39 * @param keyName The name of public key.
40 * @return The public key.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070041 */
Jeff Thompson6c314bc2013-09-23 18:09:38 -070042 virtual ptr_lib::shared_ptr<PublicKey>
43 getPublicKey(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070044
45 /**
Jeff Thompson4c11b9f2013-09-13 11:05:28 -070046 * 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 Thompson7b79eb62013-09-12 18:48:29 -070049 * @param keyName The name of the signing key.
50 * @param digestAlgorithm the digest algorithm.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070051 * @return The signature, or a null pointer if signing fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070052 */
53 virtual Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070054 sign(const uint8_t *data, size_t dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070055
Jeff Thompson6c314bc2013-09-23 18:09:38 -070056 Blob
57 sign(const Blob& data, const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256)
58 {
59 sign(data.buf(), data.size(), keyName, digestAlgorithm);
60 }
61
Jeff Thompson7b79eb62013-09-12 18:48:29 -070062 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070063 * 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 Thompson7b79eb62013-09-12 18:48:29 -070069 */
Jeff Thompson6c314bc2013-09-23 18:09:38 -070070 virtual Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070071 decrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070072
73 Blob
74 decrypt(const Name& keyName, const Blob& data, bool isSymmetric = false)
75 {
76 decrypt(keyName, data.buf(), data.size(), isSymmetric);
77 }
Jeff Thompson7b79eb62013-09-12 18:48:29 -070078
79 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070080 * 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 Thompson7b79eb62013-09-12 18:48:29 -070086 */
Jeff Thompson6c314bc2013-09-23 18:09:38 -070087 virtual Blob
Jeff Thompson97223af2013-09-24 17:01:27 -070088 encrypt(const Name& keyName, const uint8_t* data, size_t dataLength, bool isSymmetric = false) = 0;
Jeff Thompson6c314bc2013-09-23 18:09:38 -070089
90 Blob
91 encrypt(const Name& keyName, const Blob& data, bool isSymmetric = false)
92 {
93 encrypt(keyName, data.buf(), data.size(), isSymmetric);
94 }
Jeff Thompson7b79eb62013-09-12 18:48:29 -070095
96 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070097 * @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 Thompson7b79eb62013-09-12 18:48:29 -0700101 */
102 virtual void
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700103 generateKey(const Name& keyName, KeyType keyType = KEY_TYPE_AES, int keySize = 256) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700104
105 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700106 * 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 Thompson7b79eb62013-09-12 18:48:29 -0700110 */
111 virtual bool
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700112 doesKeyExist(const Name& keyName, KeyClass keyClass) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700113};
114
115}
116
117#endif