Loading...
Searching...
No Matches
key_generator.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019-2022 ExpoLab, UC Davis
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 */
25
27
28#include <cryptopp/aes.h>
29#include <cryptopp/cmac.h>
30#include <cryptopp/dsa.h>
31#include <cryptopp/eccrypto.h>
32#include <cryptopp/filters.h>
33#include <cryptopp/hex.h>
34#include <cryptopp/osrng.h>
35#include <cryptopp/pssr.h>
36#include <cryptopp/rsa.h>
37#include <cryptopp/sha.h>
38#include <cryptopp/whrlpool.h>
39#include <cryptopp/xed25519.h>
40#include <glog/logging.h>
41
42//#include <cryptopp/modes.h>
43//#include <iomanip>
44
45namespace resdb {
46
47namespace {
48
49//==============================================================================
50// Return a KeyPairHex for each node. This function is used by the nodes to
51// obtain their keyPair
52SecretKey RsaGenerateHexKeyPair(unsigned int key_size) {
53 SecretKey key_pair;
54 // PGP Random Pool-like generator
55 CryptoPP::AutoSeededRandomPool rng;
56
57 // generate keys
58 CryptoPP::RSA::PrivateKey private_key;
59 private_key.GenerateRandomWithKeySize(rng, key_size);
60 CryptoPP::RSA::PublicKey public_key(private_key);
61
62 // save keys
63 public_key.Save(CryptoPP::HexEncoder(
64 new CryptoPP::StringSink(*key_pair.mutable_public_key()))
65 .Ref());
66 private_key.Save(CryptoPP::HexEncoder(new CryptoPP::StringSink(
67 *key_pair.mutable_private_key()))
68 .Ref());
69
70 return key_pair;
71}
72
73// Initialize the signer and the keys for ED25519
74SecretKey ED25519GenerateKeys() {
75 CryptoPP::ed25519::Signer signer;
76 SecretKey key_pair;
77 CryptoPP::AutoSeededRandomPool prng;
78 // Initilize signer;
79 signer.AccessPrivateKey().GenerateRandom(prng);
80
81 const CryptoPP::ed25519PrivateKey &private_key =
82 dynamic_cast<const CryptoPP::ed25519PrivateKey &>(signer.GetPrivateKey());
83
84 key_pair.mutable_private_key()->assign(
85 (char *)private_key.GetPrivateKeyBytePtr(),
86 CryptoPP::ed25519PrivateKey::SECRET_KEYLENGTH);
87 key_pair.mutable_public_key()->assign(
88 (char *)private_key.GetPublicKeyBytePtr(),
89 CryptoPP::ed25519PrivateKey::PUBLIC_KEYLENGTH);
90 return key_pair;
91}
92
93// CMAC-AES key generator
94// Return a keyPairHex where just the priv key is set
95SecretKey CmacGenerateHexKey(unsigned int key_size) {
96 SecretKey key_pair;
97 std::string private_key;
98 CryptoPP::AutoSeededRandomPool prng;
99 // int keySize = 16 ; // AES::DEFAULT_KEYLENGTH
100 CryptoPP::SecByteBlock key(key_size);
101 prng.GenerateBlock(
102 key.data(), key.size()); // Key is a SecByteBlock that contains the key.
103
104 CryptoPP::StringSource ss(key.data(), key.size(), true,
105 new CryptoPP::HexEncoder(new CryptoPP::StringSink(
106 private_key)) // HexEncoder
107 ); // StringSource
108 key_pair.set_private_key(private_key);
109 key_pair.set_public_key(private_key);
110 return key_pair;
111}
112
113SecretKey ECDSAGenerateKeys() {
114 CryptoPP::AutoSeededRandomPool prng;
115 CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256>::PrivateKey privateKey;
116 CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP> params(
117 CryptoPP::ASN1::secp256k1());
118 privateKey.Initialize(prng, params);
119
120 CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256>::PublicKey publicKey;
121 privateKey.MakePublicKey(publicKey);
122
123 SecretKey key_pair;
124
125 // save keys
126 publicKey.Save(CryptoPP::HexEncoder(
127 new CryptoPP::StringSink(*key_pair.mutable_public_key()))
128 .Ref());
129 privateKey.Save(CryptoPP::HexEncoder(
130 new CryptoPP::StringSink(*key_pair.mutable_private_key()))
131 .Ref());
132 return key_pair;
133}
134
135} // namespace
136
137SecretKey KeyGenerator::GeneratorKeys(SignatureInfo::HashType type) {
138 SecretKey key;
139 switch (type) {
140 case SignatureInfo::RSA:
141 key = RsaGenerateHexKeyPair(3072);
142 break;
143 case SignatureInfo::ED25519: {
144 key = ED25519GenerateKeys();
145 break;
146 }
147 case SignatureInfo::CMAC_AES: {
148 key = CmacGenerateHexKey(16);
149 break;
150 }
151 case SignatureInfo::ECDSA: {
152 key = ECDSAGenerateKeys();
153 break;
154 }
155 default:
156 break;
157 }
158 key.set_hash_type(type);
159 return key;
160}
161
162} // namespace resdb
static SecretKey GeneratorKeys(SignatureInfo::HashType type)