-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashEncrypt.cpp
66 lines (51 loc) · 1.63 KB
/
HashEncrypt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cstring>
#include "HashEncrypt.h"
/**
* CTOR. Create an object to hash arrays using GCM-128 procedure.
* @param key 128-bit key
* @param iv initialization vector
* @param ivSizeBytes size in bytes of the initialization vector
*/
HashEncrypt::HashEncrypt(const unsigned char *key, const unsigned char *iv, size_t ivSizeBytes)
{
// copy the 128-bit key
memcpy(_key, key, 16);
//copy the iv:
_iv = new unsigned char[ivSizeBytes];
memcpy(_iv, iv, ivSizeBytes);
EVP_CIPHER_CTX_init(_ctx);
EVP_EncryptInit_ex(_ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); //TODO check return value == 1
EVP_CIPHER_CTX_ctrl(_ctx, EVP_CTRL_GCM_SET_IVLEN, ivSizeBytes, NULL);
EVP_EncryptInit_ex(_ctx, NULL, NULL, _key, _iv);
}
/**
* DTOR
*/
HashEncrypt::~HashEncrypt()
{
EVP_CIPHER_CTX_cleanup(_ctx);
delete[] _iv;
_iv = nullptr;
}
/**
* Update the hash object with data
* @param in data
* @param inSizeBytes size of data in bytes
*/
void HashEncrypt::hashUpdate(unsigned char *in, int inSizeBytes)
{
//CXXPROF_ACTIVITY("gcm update");
EVP_EncryptUpdate(_ctx, NULL, &_unusedOutl, in, inSizeBytes); //TODO check return value == 1
}
/**
* Get the final hash of all the updated data
* @param out destination buffer for the hash value
* @param outSizeBytes will hold the number of written bytes
*/
void HashEncrypt::hashFinal(unsigned char *out, unsigned int *outSizeBytes)
{
//CXXPROF_ACTIVITY("gcm final");
EVP_EncryptFinal_ex(_ctx, NULL, &_unusedOutl); //TODO check return value == 1
EVP_CIPHER_CTX_ctrl(_ctx, EVP_CTRL_GCM_GET_TAG, _finalSizeBytes, out);
*outSizeBytes = _finalSizeBytes;
}