60 if (!keyholder.newkeyid) {
64 key = keyholder.newkeyid;
80 memcpy(ivdata.
data, iv, ivdata.
size);
100 memcpy(iv, ivdata.
data, ivdata.
size);
101 givlen = ivdata.
size;
122bool cryptoAES128::buildKeyForAESEncryptOrDecrypt(
bool shouldEncrypt) {
123 if ((shouldEncrypt != PSA_KEY_USAGE_DECRYPT) || (shouldEncrypt != PSA_KEY_USAGE_ENCRYPT))
127 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) {
134 status = psa_aead_generate_nonce(&op, iv,
sizeof(iv), &givlen);
135 if (status != PSA_SUCCESS) {
138 DEBUG3(
"generated iv size = " + String(givlen));
142 status = psa_aead_set_nonce(&op, iv,
sizeof(iv));
143 if (status != PSA_SUCCESS) {
148 status = psa_aead_decrypt_setup(&op, key, PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8));
149 if (status != PSA_SUCCESS) {
153 status = psa_aead_set_nonce(&op, iv,
sizeof(iv));
154 if (status != PSA_SUCCESS) {
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);
178 buildKeyForAESEncryptOrDecrypt(
false);
180 int lastcharactercount = 0;
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++;
189 lastcharactercount = lastcharactercount +
sizeof(inpbuf);
190 status = psa_aead_update(&op, inpbuf,
sizeof(inpbuf), outbuf,
sizeof(outbuf), &olen);
191 if (status != PSA_SUCCESS) {
195 for (
int i = 0; i < olen; i++)
197 rtn.concat((
char)outbuf[i]);
199 DUMP(
"outbuf decrypt", outbuf,
sizeof(outbuf));
226 unsigned char outbuf[key_bits / 8];
227 assert(
sizeof(outbuf) == 16);
228 unsigned char inpbuf[(key_bits / 8)];
230 buildKeyForAESEncryptOrDecrypt();
233 std::list<unsigned char> outputbytes;
234 int lastcharactercount = 0;
235 int maxlength = datatoEncrypt.length();
236 const char* thetext = datatoEncrypt.c_str();
239 while (lastcharactercount < maxlength) {
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;
247 DUMP(
"inpbuf", inpbuf,
sizeof(inpbuf));
249 status = psa_aead_update(&op, inpbuf,
sizeof(inpbuf), outbuf,
sizeof(outbuf), &olen);
250 if (status != PSA_SUCCESS) {
254 for (
int i = 0; i < olen; i++)
255 outputbytes.push_back(outbuf[i]);
256 DUMP(
"outbuf", outbuf, olen);
259 status = psa_aead_abort(&op);
260 if (status != PSA_SUCCESS) {
264 int numbytes = outputbytes.size();
266 unsigned char *rtn = (*returndata)->data;
267 for (
int i = 0; i < numbytes; i++) {
268 rtn[i] = outputbytes.front();
269 outputbytes.pop_front();
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).
String cryptoerrortoString(psa_status_t err)
#define FALSEORSTOP(s, f)
variable size encrypted data
IV data for AES128 is 12 bytes.