-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from ozbenh/ossl-helpers
Create helpers for openssl AES use
- Loading branch information
Showing
8 changed files
with
246 additions
and
330 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,122 @@ | ||
/* | ||
* ossl_helpers.c -- Helper wrappers around openssl functions | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA | ||
*/ | ||
|
||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
|
||
#include "ossl_helpers.h" | ||
|
||
#include <openssl/conf.h> | ||
#include <openssl/evp.h> | ||
#include <openssl/err.h> | ||
|
||
struct ossl_aes_ctx | ||
{ | ||
EVP_CIPHER_CTX *c; | ||
const unsigned char *key; | ||
const unsigned char *iv; | ||
}; | ||
|
||
void ossl_aes_random_key(unsigned char *key, const unsigned char *pepper) | ||
{ | ||
static unsigned char default_key[AES_BLOCK] = { | ||
0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70, | ||
0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0 | ||
}; /* AES data reduction key */ | ||
unsigned char stack_junk[AES_BLOCK]; | ||
int fd, i; | ||
|
||
/* Try getting some randomness from the kernel */ | ||
fd = open("/dev/urandom", O_RDONLY); | ||
if (fd >= 0) { | ||
int r = read(fd, key, sizeof key); | ||
close(fd); | ||
} | ||
|
||
/* Mix in our default key */ | ||
for (i = 0; i < AES_BLOCK && pepper; i++) | ||
key[i] ^= default_key[i]; | ||
|
||
/* Mix in stack junk */ | ||
for (i = 0; i < AES_BLOCK && pepper; i++) | ||
key[i] ^= stack_junk[i]; | ||
|
||
/* Spice it up if we can */ | ||
for (i = 0; i < AES_BLOCK && pepper; i++) | ||
key[i] ^= pepper[i]; | ||
} | ||
|
||
|
||
struct ossl_aes_ctx *ossl_aes_init(const unsigned char *key, | ||
const unsigned char *iv) | ||
{ | ||
struct ossl_aes_ctx *ctx; | ||
|
||
ctx = malloc(sizeof(*ctx)); | ||
if (!ctx) | ||
return NULL; | ||
|
||
ctx->c = EVP_CIPHER_CTX_new(); | ||
if (!ctx->c) { | ||
free(ctx); | ||
return NULL; | ||
} | ||
ctx->key = key; | ||
ctx->iv = iv; | ||
return ctx; | ||
} | ||
|
||
void ossl_aes_exit(struct ossl_aes_ctx *ctx) | ||
{ | ||
EVP_CIPHER_CTX_free(ctx->c); | ||
free(ctx); | ||
} | ||
|
||
int ossl_aes_encrypt(struct ossl_aes_ctx *ctx, | ||
unsigned char *plaintext, int plaintext_len, | ||
unsigned char *ciphertext) | ||
{ | ||
int len, ciphertext_len; | ||
|
||
if(1 != EVP_EncryptInit_ex(ctx->c, EVP_aes_128_cbc(), NULL, ctx->key, ctx->iv)) | ||
return 0; | ||
|
||
/* | ||
* Provide the message to be encrypted, and obtain the encrypted output. | ||
* EVP_EncryptUpdate can be called multiple times if necessary | ||
*/ | ||
if(1 != EVP_EncryptUpdate(ctx->c, ciphertext, &len, plaintext, plaintext_len)) | ||
return 0; | ||
|
||
ciphertext_len = len; | ||
|
||
/* | ||
* Finalise the encryption. Further ciphertext bytes may be written at | ||
* this stage. | ||
*/ | ||
if(1 != EVP_EncryptFinal_ex(ctx->c, ciphertext + len, &len)) | ||
return 0; | ||
ciphertext_len += len; | ||
|
||
return ciphertext_len; | ||
} | ||
|
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,58 @@ | ||
/* | ||
* ossl_helpers.h -- Helper wrappers around openssl functions | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 2 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA | ||
*/ | ||
|
||
#ifndef OSSL_HELPERS__H | ||
#define OSSL_HELPERS__H | ||
|
||
#define AES_BLOCK 16 | ||
|
||
struct ossl_aes_ctx; | ||
|
||
extern int ossl_aes_encrypt(struct ossl_aes_ctx *ctx, | ||
unsigned char *plaintext, int plaintext_len, | ||
unsigned char *ciphertext); | ||
|
||
extern struct ossl_aes_ctx *ossl_aes_init(const unsigned char *key, | ||
const unsigned char *iv); | ||
extern void ossl_aes_exit(struct ossl_aes_ctx *ctx); | ||
extern void ossl_aes_random_key(unsigned char *key, const unsigned char *pepper); | ||
|
||
static inline int ossl_aes_mangle(struct ossl_aes_ctx *ctx, unsigned char *data, | ||
size_t size) | ||
{ | ||
int ciphertext_len; | ||
|
||
/* | ||
* Buffer for ciphertext. Ensure the buffer is long enough for the | ||
* ciphertext which may be longer than the plaintext, depending on the | ||
* algorithm and mode. | ||
* | ||
* For AES, one extra AES block should be sufficient. | ||
*/ | ||
unsigned char ciphertext[size + AES_BLOCK]; | ||
|
||
/* Encrypt the plaintext */ | ||
ciphertext_len = ossl_aes_encrypt(ctx, data, size, ciphertext); | ||
if (!ciphertext_len) | ||
return -1; | ||
|
||
memcpy(data, ciphertext, size); | ||
return ciphertext_len; | ||
} | ||
|
||
#endif /* OSSL_HELPERS__H */ |
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.