12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #include "Openssl_crypto.h"
- #include "SymmetricEncryptionBox.h"
- // increments last 4 bytes (in big-endian order)
- uint32_t SymmetricEncryptionBox::aes_gcm_increment_iv_internal_call(uint8_t* iv) {
- uint32_t counter;
- for(counter=11;counter>7;counter--)
- {
- if(iv[counter] == 0xff)
- {
- if(counter - 1 == 7)
- return 0xff;
- iv[counter-1] = 0x01;
- iv[counter] = 0x0;
- }
- else
- iv[counter] += 1;
- }
- return 0;
- }
- void SymmetricEncryptionBox::set_symmetric_key(uint8_t* given_key)
- {
- uint32_t counter;
- for(counter=0; counter<16; counter++)
- symmetric_key[counter] = given_key[counter];
- }
- void SymmetricEncryptionBox::get_symmetric_key(uint8_t* op_key)
- {
- uint32_t counter;
- for(counter=0; counter<16; counter++)
- op_key[counter] = symmetric_key[counter];
- }
- // TODO: IMPORTANT - CHECK IF WE NEED TO DO THE AES GCM INCREMENT IV FOR DECRYPTION TOO.
- uint32_t SymmetricEncryptionBox::encrypt_decrypt(int enc, uint8_t* plaintext, uint32_t plaintext_length, uint8_t* ciphertext, uint32_t* ciphertext_length)
- {
- uint32_t actual_plaintext_length=plaintext_length;
- uint8_t tag[16];uint32_t counter;
- if(enc == 0)
- {
- for(counter=0;counter<16;counter++)
- tag[counter] = plaintext[counter + plaintext_length - 16];
- actual_plaintext_length-=16;
- }
- uint32_t return_status = aes_gcm_128(enc, symmetric_key, iv, plaintext, actual_plaintext_length, ciphertext, ciphertext_length, tag);
- if(enc == 1)
- {
- for(counter=0;counter<16;counter++)
- ciphertext[counter + *ciphertext_length] = tag[counter];
- *ciphertext_length=*ciphertext_length + 16;
- }
- // if(return_status == 0)
- // aes_gcm_increment_iv_internal_call(iv);
- return return_status;
- }
|