6#include "psa/crypto_values.h"
13#define PRINTERRORxxxx(function) \
14 DEBUG2(String(function) + " failed " + errortoString(status)); \
17#include "psa/crypto.h"
63 unsigned char iv[
IVSIZE] = { 0x00 };
67 psa_aead_operation_t op;
74 psa_status_t status = psa_crypto_init();
75 if (status != PSA_SUCCESS) {
88 key = keyholder->newkeyid;
95 key = keyholder.newkeyid;
114 assert(byteslength ==
IVSIZE);
115 memcpy(iv, bytes, byteslength);
116 givlen = byteslength;
130 void buildKeyForAESEncryptOrDecrypt(
bool shouldEncrypt =
true) {
131 if ((shouldEncrypt != PSA_KEY_USAGE_DECRYPT) || (shouldEncrypt != PSA_KEY_USAGE_ENCRYPT))
135 op = PSA_AEAD_OPERATION_INIT;
136 if (shouldEncrypt ==
true) {
137 status = psa_aead_encrypt_setup(&op, key, PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8));
138 if (status != PSA_SUCCESS) {
142 status = psa_aead_generate_nonce(&op, iv,
sizeof(iv), &givlen);
143 if (status != PSA_SUCCESS) {
146 DEBUG3(
"generated iv size = " + String(givlen));
150 status = psa_aead_set_nonce(&op, iv,
sizeof(iv));
151 if (status != PSA_SUCCESS) {
156 status = psa_aead_decrypt_setup(&op, key, PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 8));
157 if (status != PSA_SUCCESS) {
161 status = psa_aead_set_nonce(&op, iv,
sizeof(iv));
162 if (status != PSA_SUCCESS) {
182 String
Decrypt(
unsigned char *bytes,
size_t len) {
186 unsigned char outbuf[key_bits / 8];
187 assert(
sizeof(outbuf) == 16);
188 unsigned char inpbuf[key_bits / 8];
189 assert(len % 16 == 0);
191 buildKeyForAESEncryptOrDecrypt(
false);
192 DEBUG3(
"len = " + String(len));
193 int lastcharactercount = 0;
195 while (lastcharactercount < len) {
196 DEBUG3(
"while lastcharactercount = " + String(lastcharactercount));
197 for (
int i = 0; i <
sizeof(inpbuf); i++)
198 inpbuf[i] = *bytes++;
200 DUMP(
"inpbuf decrypt", inpbuf,
sizeof(inpbuf));
201 lastcharactercount = lastcharactercount +
sizeof(inpbuf);
202 status = psa_aead_update(&op, inpbuf,
sizeof(inpbuf), outbuf,
sizeof(outbuf), &olen);
203 if (status != PSA_SUCCESS) {
207 for (
int i = 0; i < olen; i++)
209 rtn.concat((
char)outbuf[i]);
211 DUMP(
"outbuf decrypt", outbuf,
sizeof(outbuf));
218 Encrypt(String datatoEncrypt,
size_t &outputsize) {
220 unsigned char outbuf[key_bits / 8];
221 assert(
sizeof(outbuf) == 16);
222 unsigned char inpbuf[key_bits / 8];
224 buildKeyForAESEncryptOrDecrypt();
227 std::list<unsigned char> outputbytes;
228 int lastcharactercount = 0;
229 int maxlength = datatoEncrypt.length();
232 while (lastcharactercount < maxlength) {
234 String dataforthisround = datatoEncrypt.substring(lastcharactercount, lastcharactercount + 16);
236 dataforthisround.getBytes(inpbuf, dataforthisround.length() + 1);
237 lastcharactercount += dataforthisround.length();
240 int stilltodo =
sizeof(inpbuf) - dataforthisround.length();
241 unsigned char *p = inpbuf + dataforthisround.length();
242 for (
int i = 0; i < stilltodo; i++)
245 DUMP(
"inpbuf", inpbuf,
sizeof(inpbuf));
250 status = psa_aead_update(&op, inpbuf,
sizeof(inpbuf), outbuf,
sizeof(outbuf), &olen);
251 if (status != PSA_SUCCESS) {
256 for (
int i = 0; i < olen; i++)
257 outputbytes.push_back(outbuf[i]);
259 DUMP(
"outbuf", outbuf, olen);
262 status = psa_aead_abort(&op);
263 if (status != PSA_SUCCESS) {
266 int numbytes = outputbytes.size();
267 outputsize = numbytes;
268 unsigned char *rtn = (
unsigned char *)malloc((
size_t)numbytes);
271 for (
int i = 0; i < numbytes; i++) {
273 rtn[i] = outputbytes.front();
275 outputbytes.pop_front();
void setkeyaccess(pass2Key *keyholder)
void setIVbytes(unsigned char *bytes, size_t byteslength)
unsigned char * Encrypt(String datatoEncrypt, size_t &outputsize)
void setkeyaccess(pass2Key &keyholder)
unsigned char * getIVbytes(size_t &IVsize)
String Decrypt(unsigned char *bytes, size_t len)
#define PRINTERROR2(status, function)