blob: 3b71d371bcbda14026f74bc3d41f45c3b1e47c20 [file] [log] [blame]
Jeff Thompson7b79eb62013-09-12 18:48:29 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
Jeff Thompson7b79eb62013-09-12 18:48:29 -07003 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
Jeff Thompson7687dc02013-09-13 11:54:07 -07004 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson7b79eb62013-09-12 18:48:29 -07005 * 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 Thompson6c314bc2013-09-23 18:09:38 -070013#include "../certificate/public-key.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070014#include "../security-common.hpp"
Jeff Thompson6c314bc2013-09-23 18:09:38 -070015#include "../../name.hpp"
Jeff Thompson7b79eb62013-09-12 18:48:29 -070016
17namespace ndn {
18
19class PrivateKeyStorage {
Jeff Thompsona50703f2013-09-17 14:24:15 -070020public:
Jeff Thompson7b79eb62013-09-12 18:48:29 -070021 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070022 * The virtual destructor.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070023 */
24 virtual
Jeff Thompson6c314bc2013-09-23 18:09:38 -070025 ~PrivateKeyStorage() {}
Jeff Thompson7b79eb62013-09-12 18:48:29 -070026
Jeff Thompson7b79eb62013-09-12 18:48:29 -070027 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070028 * 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 Thompson7b79eb62013-09-12 18:48:29 -070032 */
33 virtual void
Jeff Thompson6c314bc2013-09-23 18:09:38 -070034 generateKeyPair(const Name& keyName, KeyType keyType = KEY_TYPE_RSA, int keySize = 2048) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070035
36 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070037 * Get the public key
38 * @param keyName The name of public key.
39 * @return The public key.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070040 */
Jeff Thompson6c314bc2013-09-23 18:09:38 -070041 virtual ptr_lib::shared_ptr<PublicKey>
42 getPublicKey(const Name& keyName) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070043
44 /**
Jeff Thompson4c11b9f2013-09-13 11:05:28 -070045 * 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 Thompson7b79eb62013-09-12 18:48:29 -070048 * @param keyName The name of the signing key.
49 * @param digestAlgorithm the digest algorithm.
Jeff Thompson6c314bc2013-09-23 18:09:38 -070050 * @return The signature, or a null pointer if signing fails.
Jeff Thompson7b79eb62013-09-12 18:48:29 -070051 */
52 virtual Blob
Jeff Thompson6c314bc2013-09-23 18:09:38 -070053 sign(const unsigned char *data, unsigned int dataLength, const Name& keyName, DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -070054
Jeff Thompson6c314bc2013-09-23 18:09:38 -070055 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 Thompson7b79eb62013-09-12 18:48:29 -070061 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070062 * 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 Thompson7b79eb62013-09-12 18:48:29 -070068 */
Jeff Thompson6c314bc2013-09-23 18:09:38 -070069 virtual Blob
70 decrypt(const Name& keyName, const unsigned char* data, unsigned int dataLength, bool isSymmetric = false) = 0;
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 Thompson7b79eb62013-09-12 18:48:29 -070077
78 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070079 * 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 Thompson7b79eb62013-09-12 18:48:29 -070085 */
Jeff Thompson6c314bc2013-09-23 18:09:38 -070086 virtual Blob
87 encrypt(const Name& keyName, const unsigned char* data, unsigned int dataLength, bool isSymmetric = false) = 0;
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 Thompson7b79eb62013-09-12 18:48:29 -070094
95 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -070096 * @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 Thompson7b79eb62013-09-12 18:48:29 -0700100 */
101 virtual void
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700102 generateKey(const Name& keyName, KeyType keyType = KEY_TYPE_AES, int keySize = 256) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700103
104 /**
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700105 * 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 Thompson7b79eb62013-09-12 18:48:29 -0700109 */
110 virtual bool
Jeff Thompson6c314bc2013-09-23 18:09:38 -0700111 doesKeyExist(const Name& keyName, KeyClass keyClass) = 0;
Jeff Thompson7b79eb62013-09-12 18:48:29 -0700112};
113
114}
115
116#endif