Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move aes features to aes.h(c)
Move aes features from security.h(c) to aes.h(c), fix correlated includes. This commit bring no new feature to the codebase. Refs: #56
- Loading branch information
Showing
6 changed files
with
257 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (C) 2022 Red Hat | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, | ||
* are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this list | ||
* of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, this | ||
* list of conditions and the following disclaimer in the documentation and/or other | ||
* materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without specific | ||
* prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | ||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <openssl/ssl.h> | ||
|
||
/** | ||
* Encrypt a string | ||
* @param plaintext The string | ||
* @param password The master password | ||
* @param ciphertext The ciphertext output | ||
* @param ciphertext_length The length of the ciphertext | ||
* @return 0 upon success, otherwise 1 | ||
*/ | ||
int | ||
pgmoneta_encrypt(char* plaintext, char* password, char** ciphertext, int* ciphertext_length); | ||
|
||
/** | ||
* Decrypt a string | ||
* @param ciphertext The string | ||
* @param ciphertext_length The length of the ciphertext | ||
* @param password The master password | ||
* @param plaintext The plaintext output | ||
* @return 0 upon success, otherwise 1 | ||
*/ | ||
int | ||
pgmoneta_decrypt(char* ciphertext, int ciphertext_length, char* password, char** plaintext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
* Copyright (C) 2022 Red Hat | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, | ||
* are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this list | ||
* of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, this | ||
* list of conditions and the following disclaimer in the documentation and/or other | ||
* materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its contributors may | ||
* be used to endorse or promote products derived from this software without specific | ||
* prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT | ||
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | ||
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <aes.h> | ||
|
||
static int derive_key_iv(char* password, unsigned char* key, unsigned char* iv); | ||
static int aes_encrypt(char* plaintext, unsigned char* key, unsigned char* iv, char** ciphertext, int* ciphertext_length); | ||
static int aes_decrypt(char* ciphertext, int ciphertext_length, unsigned char* key, unsigned char* iv, char** plaintext); | ||
|
||
// [public] | ||
int | ||
pgmoneta_encrypt(char* plaintext, char* password, char** ciphertext, int* ciphertext_length) | ||
{ | ||
unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
|
||
memset(&key, 0, sizeof(key)); | ||
memset(&iv, 0, sizeof(iv)); | ||
|
||
if (derive_key_iv(password, key, iv) != 0) | ||
{ | ||
return 1; | ||
} | ||
|
||
return aes_encrypt(plaintext, key, iv, ciphertext, ciphertext_length); | ||
} | ||
|
||
// [public] | ||
int | ||
pgmoneta_decrypt(char* ciphertext, int ciphertext_length, char* password, char** plaintext) | ||
{ | ||
unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
|
||
memset(&key, 0, sizeof(key)); | ||
memset(&iv, 0, sizeof(iv)); | ||
|
||
if (derive_key_iv(password, key, iv) != 0) | ||
{ | ||
return 1; | ||
} | ||
|
||
return aes_decrypt(ciphertext, ciphertext_length, key, iv, plaintext); | ||
} | ||
|
||
// [private] | ||
static int | ||
derive_key_iv(char* password, unsigned char* key, unsigned char* iv) | ||
{ | ||
|
||
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) | ||
OpenSSL_add_all_algorithms(); | ||
#endif | ||
|
||
if (!EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), NULL, | ||
(unsigned char*) password, strlen(password), 1, | ||
key, iv)) | ||
{ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
// [private] | ||
static int | ||
aes_encrypt(char* plaintext, unsigned char* key, unsigned char* iv, char** ciphertext, int* ciphertext_length) | ||
{ | ||
EVP_CIPHER_CTX* ctx = NULL; | ||
int length; | ||
size_t size; | ||
unsigned char* ct = NULL; | ||
int ct_length; | ||
|
||
if (!(ctx = EVP_CIPHER_CTX_new())) | ||
{ | ||
goto error; | ||
} | ||
|
||
if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) | ||
{ | ||
goto error; | ||
} | ||
|
||
size = strlen(plaintext) + EVP_CIPHER_block_size(EVP_aes_256_cbc()); | ||
ct = malloc(size); | ||
memset(ct, 0, size); | ||
|
||
if (EVP_EncryptUpdate(ctx, | ||
ct, &length, | ||
(unsigned char*)plaintext, strlen((char*)plaintext)) != 1) | ||
{ | ||
goto error; | ||
} | ||
|
||
ct_length = length; | ||
|
||
if (EVP_EncryptFinal_ex(ctx, ct + length, &length) != 1) | ||
{ | ||
goto error; | ||
} | ||
|
||
ct_length += length; | ||
|
||
EVP_CIPHER_CTX_free(ctx); | ||
|
||
*ciphertext = (char*)ct; | ||
*ciphertext_length = ct_length; | ||
|
||
return 0; | ||
|
||
error: | ||
if (ctx) | ||
{ | ||
EVP_CIPHER_CTX_free(ctx); | ||
} | ||
|
||
free(ct); | ||
|
||
return 1; | ||
} | ||
|
||
// [private] | ||
static int | ||
aes_decrypt(char* ciphertext, int ciphertext_length, unsigned char* key, unsigned char* iv, char** plaintext) | ||
{ | ||
EVP_CIPHER_CTX* ctx = NULL; | ||
int plaintext_length; | ||
int length; | ||
size_t size; | ||
char* pt = NULL; | ||
|
||
if (!(ctx = EVP_CIPHER_CTX_new())) | ||
{ | ||
goto error; | ||
} | ||
|
||
if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) | ||
{ | ||
goto error; | ||
} | ||
|
||
size = ciphertext_length + EVP_CIPHER_block_size(EVP_aes_256_cbc()); | ||
pt = malloc(size); | ||
memset(pt, 0, size); | ||
|
||
if (EVP_DecryptUpdate(ctx, | ||
(unsigned char*)pt, &length, | ||
(unsigned char*)ciphertext, ciphertext_length) != 1) | ||
{ | ||
goto error; | ||
} | ||
|
||
plaintext_length = length; | ||
|
||
if (EVP_DecryptFinal_ex(ctx, (unsigned char*)pt + length, &length) != 1) | ||
{ | ||
goto error; | ||
} | ||
|
||
plaintext_length += length; | ||
|
||
EVP_CIPHER_CTX_free(ctx); | ||
|
||
pt[plaintext_length] = 0; | ||
*plaintext = pt; | ||
|
||
return 0; | ||
|
||
error: | ||
if (ctx) | ||
{ | ||
EVP_CIPHER_CTX_free(ctx); | ||
} | ||
|
||
free(pt); | ||
|
||
return 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.