I have the following draft to keep my data secure in transit.
- Generate 2048 bit RSA key
- sha256sum that private key and use that sum as aad, key and iv parameters in the following aes encryption function
12 int aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *aad,
13 int aad_len, unsigned char *key, unsigned char *iv,
14 unsigned char *ciphertext, unsigned char *tag)
15 {
16 EVP_CIPHER_CTX *ctx = NULL;
17 int len = 0, ciphertext_len = 0;
18 if (!(ctx = EVP_CIPHER_CTX_new())) {
19 handleErrors();
20 return -1;
21 }
22 if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL)) {
23 handleErrors();
24 return -1;
25 }
26 if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL)) {
27 handleErrors();
28 return -1;
29 }
30 if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) handleErrors();
31 if (aad && aad_len > 0) {
32 if (1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len)) {
33 handleErrors();
34 return -1;
35 }
36 }
37 if (plaintext) {
38 if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) {
39 handleErrors();
40 return -1;
41 }
42 ciphertext_len = len;
43 }
44 if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) {
45 handleErrors();
46 return -1;
47 }
48 ciphertext_len += len;
49 if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag)) {
50 handleErrors();
51 return -1;
52 }
53 EVP_CIPHER_CTX_free(ctx);
54 return ciphertext_len;
55 }
Is that even secure? Can one single sha256sum of private key be universally used for AES encrypting multiple files on different content?