Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

p11_ecdh.cpp
Go to the documentation of this file.
1 /*
2 * PKCS#11 ECDH
3 * (C) 2016 Daniel Neus, Sirrix AG
4 * (C) 2016 Philipp Weber, Sirrix AG
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9 #include <botan/p11_ecdh.h>
10
11 #if defined(BOTAN_HAS_ECDH)
12
13 #include <botan/internal/p11_mechanism.h>
14 #include <botan/ber_dec.h>
15 #include <botan/der_enc.h>
16 #include <botan/pk_ops.h>
17 #include <botan/rng.h>
18
19 namespace Botan {
20
21 namespace PKCS11 {
22
23 ECDH_PublicKey PKCS11_ECDH_PublicKey::export_key() const
24 {
25 return ECDH_PublicKey(domain(), public_point());
26 }
27
28 ECDH_PrivateKey PKCS11_ECDH_PrivateKey::export_key() const
29 {
30 auto priv_key = get_attribute_value(AttributeType::Value);
31
32 Null_RNG rng;
33 return ECDH_PrivateKey(rng, domain(), BigInt::decode(priv_key));
34 }
35
36 secure_vector<uint8_t> PKCS11_ECDH_PrivateKey::private_key_bits() const
37 {
38 return export_key().private_key_bits();
39 }
40
41 namespace {
42 class PKCS11_ECDH_KA_Operation : public PK_Ops::Key_Agreement
43 {
44 public:
45 PKCS11_ECDH_KA_Operation(const PKCS11_EC_PrivateKey& key, const std::string& params)
46 : PK_Ops::Key_Agreement(), m_key(key), m_mechanism(MechanismWrapper::create_ecdh_mechanism(params))
47 {}
48
49
50 /// The encoding in V2.20 was not specified and resulted in different implementations choosing different encodings.
51 /// Applications relying only on a V2.20 encoding (e.g. the DER variant) other than the one specified now (raw) may not work with all V2.30 compliant tokens.
52 secure_vector<uint8_t> agree(size_t key_len, const uint8_t other_key[], size_t other_key_len, const uint8_t salt[],
53 size_t salt_len) override
54 {
55 std::vector<uint8_t> der_encoded_other_key;
56 if(m_key.point_encoding() == PublicPointEncoding::Der)
57 {
58 der_encoded_other_key = DER_Encoder().encode(other_key, other_key_len, OCTET_STRING).get_contents_unlocked();
59 m_mechanism.set_ecdh_other_key(der_encoded_other_key.data(), der_encoded_other_key.size());
60 }
61 else
62 {
63 m_mechanism.set_ecdh_other_key(other_key, other_key_len);
64 }
65
66 if(salt != nullptr && salt_len > 0)
67 {
68 m_mechanism.set_ecdh_salt(salt, salt_len);
69 }
70
71 ObjectHandle secret_handle = 0;
72 AttributeContainer attributes;
73 attributes.add_bool(AttributeType::Sensitive, false);
74 attributes.add_bool(AttributeType::Extractable, true);
75 attributes.add_numeric(AttributeType::Class, static_cast< CK_OBJECT_CLASS >(ObjectClass::SecretKey));
76 attributes.add_numeric(AttributeType::KeyType, static_cast< CK_KEY_TYPE >(KeyType::GenericSecret));
77 attributes.add_numeric(AttributeType::ValueLen, key_len);
78 m_key.module()->C_DeriveKey(m_key.session().handle(), m_mechanism.data(), m_key.handle(), attributes.data(),
79 attributes.count(), &secret_handle);
80
81 Object secret_object(m_key.session(), secret_handle);
82 secure_vector<uint8_t> secret = secret_object.get_attribute_value(AttributeType::Value);
83 if(secret.size() < key_len)
84 {
85 throw PKCS11_Error("ECDH key derivation secret length is too short");
86 }
87 secret.resize(key_len);
88 return secret;
89 }
90
91 private:
92 const PKCS11_EC_PrivateKey& m_key;
93 MechanismWrapper m_mechanism;
94 };
95
96 }
97
98 std::unique_ptr<PK_Ops::Key_Agreement>
99 PKCS11_ECDH_PrivateKey::create_key_agreement_op(RandomNumberGenerator&,
100 const std::string& params,
101 const std::string& /*provider*/) const
102 {
103 return std::unique_ptr<PK_Ops::Key_Agreement>(new PKCS11_ECDH_KA_Operation(*this, params));
104 }
105
106 PKCS11_ECDH_KeyPair generate_ecdh_keypair(Session& session, const EC_PublicKeyGenerationProperties& pub_props,
107 const EC_PrivateKeyGenerationProperties& priv_props)
108 {
109 ObjectHandle pub_key_handle = 0;
110 ObjectHandle priv_key_handle = 0;
111
112 Mechanism mechanism = { static_cast< CK_MECHANISM_TYPE >(MechanismType::EcKeyPairGen), nullptr, 0 };
113
114 session.module()->C_GenerateKeyPair(session.handle(), &mechanism,
115 pub_props.data(), pub_props.count(), priv_props.data(), priv_props.count(),
116 &pub_key_handle, &priv_key_handle);
117
118 return std::make_pair(PKCS11_ECDH_PublicKey(session, pub_key_handle), PKCS11_ECDH_PrivateKey(session, priv_key_handle));
119 }
120
121 }
122 }
123
124 #endif
CK_MECHANISM_TYPE
CK_ULONG CK_MECHANISM_TYPE
Definition: pkcs11t.h:583
Botan::PKCS11::MechanismType::EcKeyPairGen
Botan::PKCS11::AttributeType::ValueLen
Botan::PKCS11::PublicPointEncoding::Der
Botan::OCTET_STRING
Definition: asn1_obj.h:35
Botan::PKCS11::AttributeType::Class
Botan::PKCS11::AttributeType::Sensitive
Botan::PKCS11::AttributeType::Extractable
Botan::PKCS11::AttributeType::Value
Botan::PKCS11::KeyType::GenericSecret
Botan
Definition: alg_id.cpp:13
Botan::PKCS11::ObjectHandle
CK_OBJECT_HANDLE ObjectHandle
Definition: p11.h:846
Botan::PKCS11::AttributeType::KeyType
Botan::PKCS11::Mechanism
CK_MECHANISM Mechanism
Definition: p11.h:839
m_key
const TPM_PrivateKey & m_key
Definition: tpm.cpp:438
Botan::PKCS11::ObjectClass::SecretKey
Botan::BigInt::decode
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114