Botan
1.11.15
|
00001 /* 00002 * PK Operation Types 00003 * (C) 2010 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_PK_OPERATIONS_H__ 00009 #define BOTAN_PK_OPERATIONS_H__ 00010 00011 #include <botan/pk_keys.h> 00012 #include <botan/secmem.h> 00013 #include <botan/rng.h> 00014 00015 namespace Botan { 00016 00017 namespace PK_Ops { 00018 00019 template<typename Key> 00020 struct PK_Spec 00021 { 00022 public: 00023 PK_Spec(const Key& key, const std::string& pad) : 00024 m_key(key), m_pad(pad) {} 00025 00026 std::string algo_name() const { return m_key.algo_name(); } 00027 00028 std::string as_string() const { return algo_name() + "/" + padding(); } 00029 00030 const Key& key() const { return m_key; } 00031 const std::string& padding() const { return m_pad; } 00032 private: 00033 const Key& m_key; 00034 const std::string m_pad; 00035 }; 00036 00037 /** 00038 * Public key encryption interface 00039 */ 00040 class BOTAN_DLL Encryption 00041 { 00042 public: 00043 virtual size_t max_input_bits() const = 0; 00044 00045 virtual secure_vector<byte> encrypt(const byte msg[], size_t msg_len, RandomNumberGenerator& rng) = 0; 00046 00047 typedef PK_Spec<Public_Key> Spec; 00048 00049 virtual ~Encryption() {} 00050 }; 00051 00052 /** 00053 * Public key decryption interface 00054 */ 00055 class BOTAN_DLL Decryption 00056 { 00057 public: 00058 virtual size_t max_input_bits() const = 0; 00059 00060 virtual secure_vector<byte> decrypt(const byte msg[], size_t msg_len) = 0; 00061 00062 typedef PK_Spec<Private_Key> Spec; 00063 00064 virtual ~Decryption() {} 00065 }; 00066 00067 /** 00068 * Public key signature creation interface 00069 */ 00070 class BOTAN_DLL Signature 00071 { 00072 public: 00073 /** 00074 * Find out the number of message parts supported by this scheme. 00075 * @return number of message parts 00076 */ 00077 virtual size_t message_parts() const { return 1; } 00078 00079 /** 00080 * Find out the message part size supported by this scheme/key. 00081 * @return size of the message parts 00082 */ 00083 virtual size_t message_part_size() const { return 0; } 00084 00085 /** 00086 * Get the maximum message size in bits supported by this public key. 00087 * @return maximum message in bits 00088 */ 00089 virtual size_t max_input_bits() const = 0; 00090 00091 /* 00092 * Perform a signature operation 00093 * @param msg the message 00094 * @param msg_len the length of msg in bytes 00095 * @param rng a random number generator 00096 */ 00097 virtual secure_vector<byte> sign(const byte msg[], size_t msg_len, 00098 RandomNumberGenerator& rng) = 0; 00099 00100 typedef PK_Spec<Private_Key> Spec; 00101 00102 virtual ~Signature() {} 00103 }; 00104 00105 /** 00106 * Public key signature verification interface 00107 */ 00108 class BOTAN_DLL Verification 00109 { 00110 public: 00111 /** 00112 * Get the maximum message size in bits supported by this public key. 00113 * @return maximum message in bits 00114 */ 00115 virtual size_t max_input_bits() const = 0; 00116 00117 /** 00118 * Find out the number of message parts supported by this scheme. 00119 * @return number of message parts 00120 */ 00121 virtual size_t message_parts() const { return 1; } 00122 00123 /** 00124 * Find out the message part size supported by this scheme/key. 00125 * @return size of the message parts 00126 */ 00127 virtual size_t message_part_size() const { return 0; } 00128 00129 /** 00130 * @return boolean specifying if this key type supports message 00131 * recovery and thus if you need to call verify() or verify_mr() 00132 */ 00133 virtual bool with_recovery() const = 0; 00134 00135 /* 00136 * Perform a signature check operation 00137 * @param msg the message 00138 * @param msg_len the length of msg in bytes 00139 * @param sig the signature 00140 * @param sig_len the length of sig in bytes 00141 * @returns if signature is a valid one for message 00142 */ 00143 virtual bool verify(const byte[], size_t, 00144 const byte[], size_t) 00145 { 00146 throw Invalid_State("Message recovery required"); 00147 } 00148 00149 /* 00150 * Perform a signature operation (with message recovery) 00151 * Only call this if with_recovery() returns true 00152 * @param msg the message 00153 * @param msg_len the length of msg in bytes 00154 * @returns recovered message 00155 */ 00156 virtual secure_vector<byte> verify_mr(const byte[], 00157 size_t) 00158 { 00159 throw Invalid_State("Message recovery not supported"); 00160 } 00161 00162 typedef PK_Spec<Public_Key> Spec; 00163 00164 virtual ~Verification() {} 00165 }; 00166 00167 /** 00168 * A generic key agreement Operation (eg DH or ECDH) 00169 */ 00170 class BOTAN_DLL Key_Agreement 00171 { 00172 public: 00173 /* 00174 * Perform a key agreement operation 00175 * @param w the other key value 00176 * @param w_len the length of w in bytes 00177 * @returns the agreed key 00178 */ 00179 virtual secure_vector<byte> agree(const byte w[], size_t w_len) = 0; 00180 00181 typedef PK_Spec<Private_Key> Spec; 00182 00183 virtual ~Key_Agreement() {} 00184 }; 00185 00186 } 00187 00188 } 00189 00190 #endif