AES128ESP32 Library
Loading...
Searching...
No Matches
cryptoAES128.cpp
Go to the documentation of this file.
1#include <cstring>
2#include "psaerr2str.h"
3#include "AES128ESP32.h"
4// #include "psaerr2str.h"
5// #include "AES128ESP32.h"
6// #include "cryptoAES128.h"
7#include <list>
46
48 DEBUG3("");
49 psa_crypto_init();
50}
51
59 DEBUG3("");
60 if (!keyholder.newkeyid) {
62 return false;
63 }
64 key = keyholder.newkeyid;
65 return true;
66}
67
78 DEBUG3("");
79 if (ivdata.size == AES128IVSIZE) {
80 memcpy(ivdata.data, iv, ivdata.size);
81 return true;
82 }
84 return false;
85}
86
87
96
98 DEBUG3("");
99 if (ivdata.size == AES128IVSIZE) {
100 memcpy(iv, ivdata.data, ivdata.size);
101 givlen = ivdata.size;
102 return true;
103 }
105 return false;
106}
114 DEBUG3("");
115 memset(iv, 0, AES128IVSIZE);
116 givlen = 0;
117}
118
120// probably should be private or protected??
121
122bool cryptoAES128::buildKeyForAESEncryptOrDecrypt(bool shouldEncrypt) {
123 if ((shouldEncrypt != PSA_KEY_USAGE_DECRYPT) || (shouldEncrypt != PSA_KEY_USAGE_ENCRYPT))
124 assert(true);
125
126 psa_status_t status;
127 /*psa_aead_operation_t */ op = PSA_AEAD_OPERATION_INIT;
128 if (shouldEncrypt == true) {
129 status = psa_aead_encrypt_setup(&op, key, PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8));
130 if (status != PSA_SUCCESS) {
131 FALSEORSTOP(status, "psa_aead_encrypt_setup");
132 }
133 if (!givlen) {
134 status = psa_aead_generate_nonce(&op, iv, sizeof(iv), &givlen);
135 if (status != PSA_SUCCESS) {
136 FALSEORSTOP(status, "psa_aead_generate_nonce");
137 }
138 DEBUG3("generated iv size = " + String(givlen));
139 assert(givlen == AES128IVSIZE);
140 } else {
141 // 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
142 status = psa_aead_set_nonce(&op, iv, sizeof(iv));
143 if (status != PSA_SUCCESS) {
144 FALSEORSTOP(status, "psa_aead_set_nonce");
145 }
146 }
147 } else {
148 status = psa_aead_decrypt_setup(&op, key, PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8));
149 if (status != PSA_SUCCESS) {
150 FALSEORSTOP(status, "psa_aead_decrypt_setup");
151 }
152 // 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
153 status = psa_aead_set_nonce(&op, iv, sizeof(iv));
154 if (status != PSA_SUCCESS) {
155 FALSEORSTOP(status, "psa_aead_set_nonce");
156 }
157 }
158 return true;
159}
160
169bool cryptoAES128::Decrypt(encryptedData &encdata, String &rtn) {
170 DEBUG3("");
171 psa_status_t status;
172 /*String*/ rtn = "";
173 unsigned char outbuf[key_bits / 8];
174 assert(sizeof(outbuf) == 16);
175 unsigned char inpbuf[key_bits / 8];
176 assert(encdata.size % 16 == 0);
177
178 buildKeyForAESEncryptOrDecrypt(false);
179 DEBUG3("len = " + String(encdata.size));
180 int lastcharactercount = 0;
181 size_t olen;
182 unsigned char *bytes = encdata.data;
183 while (lastcharactercount < encdata.size) {
184 DEBUG3("while lastcharactercount = " + String(lastcharactercount));
185 for (int i = 0; i < sizeof(inpbuf); i++)
186 inpbuf[i] = *bytes++;
187
188 // DUMP("inpbuf decrypt", inpbuf, sizeof(inpbuf));
189 lastcharactercount = lastcharactercount + sizeof(inpbuf);
190 status = psa_aead_update(&op, inpbuf, sizeof(inpbuf), outbuf, sizeof(outbuf), &olen);
191 if (status != PSA_SUCCESS) {
192 FALSEORSTOP(status, "psa_aead_update");
193 }
194
195 for (int i = 0; i < olen; i++)
196 if (outbuf[i])
197 rtn.concat((char)outbuf[i]);
198
199 DUMP("outbuf decrypt", outbuf, sizeof(outbuf));
200 } // while
201 psa_aead_abort(&op);
202 // return rtn;
203 return true;
204}
205
206
223bool cryptoAES128::Encrypt(String datatoEncrypt, encryptedData **returndata) {
224 DEBUG3("");
225 psa_status_t status;
226 unsigned char outbuf[key_bits / 8];
227 assert(sizeof(outbuf) == 16);
228 unsigned char inpbuf[(key_bits / 8)];
229
230 buildKeyForAESEncryptOrDecrypt();
231
232 size_t olen;
233 std::list<unsigned char> outputbytes;
234 int lastcharactercount = 0;
235 int maxlength = datatoEncrypt.length();
236 const char* thetext = datatoEncrypt.c_str();
237
238 // copy at most key_bits /8
239 while (lastcharactercount < maxlength) {
240 DEBUG3("start of loop");
241 memset(inpbuf,0,sizeof(inpbuf));
242 size_t copymax = (lastcharactercount + 16 < maxlength-lastcharactercount) ? lastcharactercount + 16 : maxlength-lastcharactercount;
243 DEBUG3("copy max = " + String(copymax));
244 memcpy(inpbuf, thetext+lastcharactercount, copymax);
245 lastcharactercount += 16;
246
247 DUMP("inpbuf", inpbuf, sizeof(inpbuf));
248
249 status = psa_aead_update(&op, inpbuf, sizeof(inpbuf), outbuf, sizeof(outbuf), &olen);
250 if (status != PSA_SUCCESS) {
251 FALSEORSTOP(status, "psa_aead_update");
252 }
253 // move encrytped data to output buffer
254 for (int i = 0; i < olen; i++)
255 outputbytes.push_back(outbuf[i]);
256 DUMP("outbuf", outbuf, olen);
257 }
258
259 status = psa_aead_abort(&op);
260 if (status != PSA_SUCCESS) {
261 FALSEORSTOP(status, "psa_aead_update");
262 }
263
264 int numbytes = outputbytes.size(); //
265 /*encryptedData */ *returndata = new encryptedData(numbytes);
266 unsigned char *rtn = (*returndata)->data;
267 for (int i = 0; i < numbytes; i++) {
268 rtn[i] = outputbytes.front();
269 outputbytes.pop_front();
270 }
271 return true;
272}
bool setKeyaccess(pass2Key *keyholder)
bool Encrypt(String datatoEncrypt, encryptedData **)
bool Decrypt(encryptedData &encdata, String &rtn)
bool setIVbytes(ivData &ivdata)
bool getIVbytes(ivData &ivdata)
Create suitable keys for key generation (the masterkey) and encryption/decryption (newkey).
Definition pbkdf2.h:31
#define AES128IVSIZE
#define DEBUG3
Definition delays.h:39
String cryptoerrortoString(psa_status_t err)
#define DUMP(t, b, l)
Definition psaerr2str.h:22
#define AES128_ERROR2
Definition psaerr2str.h:26
#define AES128_ERROR1
Definition psaerr2str.h:25
#define FALSEORSTOP(s, f)
Definition psaerr2str.h:15
size_t size
unsigned char * data
variable size encrypted data
IV data for AES128 is 12 bytes.