-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathhsm-example.cpp
67 lines (58 loc) · 2.46 KB
/
hsm-example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* #%L
* %%
* Copyright (C) 2022 BMW Car IT GmbH
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
#include <mococrw/hsm.h>
#include <mococrw/key.h>
/* This example demonstrates how to create a PKCS11 engine object,
* and store/load a key inside the HSM.
*/
using namespace mococrw;
int main(void)
{
// Information for engine loading and key management.
std::string id("pkcs11");
std::string modulePath("/usr/lib/softhsm/libsofthsm2.so");
std::string tokenLabel("token-label");
// Don't hardcode the pin in your application, this is just for demonstration purposes
std::string pin("1234");
HsmEngine hsmEngine(id, modulePath, tokenLabel, pin);
utility::stringCleanse(pin);
/************** ECC key generation **************/
std::vector<uint8_t> keyIDECC{0x97};
std::string keyLabelECC("ecc-key-label");
ECCSpec ecspec;
// Keypair containing private and public key
auto eccPrivKey = AsymmetricKeypair::generateKeyOnHSM(hsmEngine, ecspec, keyLabelECC, keyIDECC);
// Public key can be loaded explicitly
auto pubKeyEcc = AsymmetricPublicKey::readPublicKeyFromHSM(hsmEngine, keyLabelECC, keyIDECC);
/************** RSA key generation **************/
std::vector<uint8_t> keyIDRSA{0x97, 0x34};
std::string keyLabelRSA{"rsa-key-label"};
mococrw::RSASpec rsaSpec;
// AsymmetricPrivateKey is an alias for AsymmetricKeypair so it can be used as well
auto rsaPrivKey =
AsymmetricPrivateKey::generateKeyOnHSM(hsmEngine, rsaSpec, keyLabelRSA, keyIDRSA);
// Private key can also be loaded explicitly
auto privKeyRsa = AsymmetricPrivateKey::readPrivateKeyFromHSM(hsmEngine, keyLabelRSA, keyIDRSA);
/**
* These keys are of types AsymmetricPrivateKey and AsymmetricPublicKey and as such can
* be used in various cryptographic algorithms where asymmetric cryptography is involved.
* See ecies-example.cpp, sig-example.cpp, rsa-example.cpp ...
*/
return 0;
}