AES128ESP32 Library
Loading...
Searching...
No Matches
pbkdf2.cpp
Go to the documentation of this file.
1#include "psaerr2str.h"
2#include "AES128ESP32.h"
3// #include "pbkdf2.h"
4
7 DEBUG3("");
8 psa_crypto_init();
9}
10
15 DEBUG3("");
16 if (masterkeyid) {
17 psa_destroy_key(masterkeyid);
18 masterkeyid = 0;
19 }
20 if (newkeyid) {
21 psa_destroy_key(newkeyid);
22 newkeyid = 0;
23 }
24 // mbedtls_psa_crypto_free(); // this is not a good idea because it will erase ALL crypto data. so if there are multiple crypto objects running this will break them
25 // perhaps we should give a static method that can be called by the when it's safe to free the crypto store.
26}
27
41 DEBUG3("");
42 // assert(masterkeyid);
43 if (!masterkeyid) {
45 return false;
46 }
47
48 DEBUG3("psa_export_key");
49 size_t key_size = 0;
50 psa_status_t status = psa_export_key(masterkeyid, mkd.data, mkd.size, &key_size);
51 if (status != PSA_SUCCESS) {
52 FALSEORSTOP(status, "psa_export_key");
53 }
54 // assert(key_size == mkd.size);
55 if (key_size!=mkd.size) {
57 return false;
58 }
59 return true;
60}
70 DEBUG3("");
71 if (masterkeyid) {
72 DEBUG3("masterkeyid is already set. removing masterkey, this should never happed");
73 psa_destroy_key(masterkeyid);
74 }
75 DEBUG3("adding masterkey");
76 psa_key_attributes_t attributes = getattributes(true);
77
78 psa_status_t status = psa_import_key(&attributes, mkd.data, mkd.size, &masterkeyid);
79 if (status != PSA_SUCCESS) {
80 FALSEORSTOP(status, "psa_import_key");
81 }
82 return true;
83}
84
88
92psa_key_attributes_t pass2Key::getattributes(bool formaster) {
93 DEBUG3("");
94 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
95 if (formaster) {
96 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
97 psa_set_key_algorithm(&attributes, PSA_ALG_HKDF(PSA_ALG_SHA_256));
98 psa_set_key_type(&attributes, PSA_KEY_TYPE_DERIVE);
99 } else {
100 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
101 psa_set_key_algorithm(&attributes, PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8));
102 psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
103 }
104 psa_set_key_bits(&attributes, AES128BYTES * 8);
105 return attributes;
106}
107
118 DEBUG3("");
119
120 // assert(!masterkeyid);
121 if (masterkeyid) {
123 return false;
124 }
125 psa_key_attributes_t attributes = getattributes(true);
126
127 psa_status_t status = psa_generate_key(&attributes, &masterkeyid);
128 if (status != PSA_SUCCESS) {
129 FALSEORSTOP(status, "psa_generate_key");
130 }
131 return true;
132}
133
134// #warning derived key salt is hardcoded
151// future: handle the salt information also as parameters, what about the number of loops counts? I'm not currently do anything about that.
152// update esp32 does not support PBKDFK2 so no counts are supported for this type of key gen.
153
155 DEBUG3("");
156
157 String tsalt = MSTRKEYSALT;
158 const unsigned char *salt = (const unsigned char *)tsalt.c_str();
159 const size_t saltlen = tsalt.length();
160 // assert(masterkeyid);
161 if (!masterkeyid) {
163 }
164
165#ifdef DEBUGLEVEL
166 derivedkeytext = text;
167#endif
168 size_t secretlength = text.length();
169 unsigned char secret[secretlength + 1];
170 memcpy(secret, text.c_str(), secretlength);
171 psa_status_t status = PSA_SUCCESS;
172 psa_key_attributes_t attributes = getattributes();
173 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
174
175 /* leaving this here for historical reasons - basically the esp32 does not support PBKDF2 algorithms
176 if the above was working it would need a COST value
177 status = psa_key_derivation_input_integer(&operation, PSA_KEY_DERIVATION_INPUT_COST, iteration_count);
178 */
179
180 DEBUG3("psa_key_derivation_setup PSA_ALG_HKDF");
181 status = psa_key_derivation_setup(&operation, PSA_ALG_HKDF(PSA_ALG_SHA_256));
182 if (status != PSA_SUCCESS) {
183 psa_key_derivation_abort(&operation);
184 FALSEORSTOP(status, "psa_key_derivation_setup");
185 }
186
187 DEBUG3("psa_key_derivation_input_bytes PSA_KEY_DERIVATION_INPUT_SALT =" + tsalt);
188 status = psa_key_derivation_input_bytes(&operation, PSA_KEY_DERIVATION_INPUT_SALT, (uint8_t *)salt, saltlen);
189 if (status != PSA_SUCCESS) {
190 psa_key_derivation_abort(&operation);
191 FALSEORSTOP(status, "psa_key_derivation_input_bytes");
192 }
193
194 DEBUG3("psa_key_derivation_input_key PSA_KEY_DERIVATION_INPUT_SECRET");
195 status = psa_key_derivation_input_key(&operation, PSA_KEY_DERIVATION_INPUT_SECRET, masterkeyid);
196 if (status != PSA_SUCCESS) {
197 psa_key_derivation_abort(&operation);
198 FALSEORSTOP(status, "psa_key_derivation_input_key");
199 }
200
201 DEBUG3("psa_key_derivation_input_bytes PSA_KEY_DERIVATION_INPUT_INFO");
202 status = psa_key_derivation_input_bytes(&operation, PSA_KEY_DERIVATION_INPUT_INFO, (const uint8_t *)secret, sizeof(secret));
203 if (status != PSA_SUCCESS) {
204 psa_key_derivation_abort(&operation);
205 FALSEORSTOP(status, "psa_key_derivation_input_bytes");
206 }
207
208 DEBUG3("psa_key_derivation_set_capacity ");
209 status = psa_key_derivation_set_capacity(&operation, AES128BYTES /*NUMBYTESX*/);
210 if (status != PSA_SUCCESS) {
211 psa_key_derivation_abort(&operation);
212 FALSEORSTOP(status, "psa_key_derivation_set_capacity");
213 }
214
215 DEBUG3("psa_key_derivation_output_key");
216 status = psa_key_derivation_output_key(&attributes, &operation, &newkeyid);
217 if (status != PSA_SUCCESS) {
218 psa_key_derivation_abort(&operation);
219 FALSEORSTOP(status, "psa_key_derivation_output_key");
220 }
221
222 // always clean up the operation
223 DEBUG3("psa_key_derivation_abort");
224 status = psa_key_derivation_abort(&operation);
225 if (status != PSA_SUCCESS) {
226 FALSEORSTOP(status, "psa_key_derivation_abort");
227 }
228
229 return true;
230}
bool setMasterKey(MasterKeyData &mkd)
Definition pbkdf2.cpp:69
~pass2Key()
Definition pbkdf2.cpp:14
bool makeMasterKey()
Definition pbkdf2.cpp:117
bool getMasterKey(MasterKeyData &mkd)
Definition pbkdf2.cpp:40
pass2Key()
Definition pbkdf2.cpp:6
bool deriveNewKeyfromText(String text)
Definition pbkdf2.cpp:154
#define AES128BYTES
#define DEBUG3
Definition delays.h:39
#define MSTRKEYSALT
When a newkey is generated it will use this value as the salt/iv. This is only used for the newkey ge...
Definition pbkdf2.h:14
String cryptoerrortoString(psa_status_t err)
#define AES128_ERROR5
Definition psaerr2str.h:29
#define AES128_ERROR3
Definition psaerr2str.h:27
#define AES128_ERROR4
Definition psaerr2str.h:28
#define AES128_ERROR6
Definition psaerr2str.h:30
#define FALSEORSTOP(s, f)
Definition psaerr2str.h:15
masterkey data for AES128 is 16bytes
size_t size
unsigned char * data