2016-01-20 19:25:02 +00:00
|
|
|
#ifndef _AES
|
|
|
|
#define _AES
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/aes.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
|
|
|
#define FAILURE -1
|
|
|
|
#define SUCCESS 0
|
|
|
|
|
|
|
|
#define AES_KEYLEN 256
|
|
|
|
#define AES_ROUNDS 6
|
|
|
|
|
|
|
|
class aes {
|
|
|
|
protected:
|
|
|
|
unsigned char* aesKey;
|
|
|
|
unsigned char* aesIV;
|
|
|
|
private:
|
|
|
|
std::string filename;
|
|
|
|
|
|
|
|
public:
|
|
|
|
aes(std::string _filename);
|
|
|
|
aes(const aes& _e);
|
|
|
|
void init_all();
|
|
|
|
void writeFile(unsigned char *file, size_t fileLength);
|
|
|
|
int readFile(unsigned char **file);
|
|
|
|
void clear_all();
|
|
|
|
~aes();
|
|
|
|
};
|
|
|
|
|
|
|
|
aes::aes(std::string _filename) {
|
|
|
|
filename = _filename;
|
|
|
|
aesKey = (unsigned char*)malloc(AES_KEYLEN/8);
|
|
|
|
aesIV = (unsigned char*)malloc(AES_KEYLEN/8);
|
|
|
|
}
|
|
|
|
|
|
|
|
aes::aes(const aes& _e) : filename(_e.filename) {
|
|
|
|
std::cerr << "salut\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void aes::init_all() {
|
|
|
|
|
|
|
|
unsigned char *aesPass = (unsigned char*)malloc(AES_KEYLEN/8);
|
|
|
|
unsigned char *aesSalt = (unsigned char*)malloc(8);
|
|
|
|
|
|
|
|
if(aesKey == NULL || aesIV == NULL || aesPass == NULL || aesSalt == NULL) {
|
|
|
|
exit(FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define USE_PBKDF
|
|
|
|
#ifdef USE_PBKDF
|
|
|
|
std::cerr << "utilisation de USE_PBKDF" << std::endl;
|
|
|
|
// Get some random data to use as the AES pass and salt
|
|
|
|
if(RAND_bytes(aesPass, AES_KEYLEN/8) == 0) {
|
|
|
|
exit(FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(RAND_bytes(aesSalt, 8) == 0) {
|
|
|
|
exit(FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), aesSalt, aesPass, AES_KEYLEN/8, AES_ROUNDS, aesKey, aesIV) == 0) {
|
|
|
|
exit(FAILURE);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if(RAND_bytes(aesKey, AES_KEYLEN/8) == 0) {
|
|
|
|
exit(FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(RAND_bytes(aesIV, AES_KEYLEN/8) == 0) {
|
|
|
|
exit(FAILURE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free(aesPass);
|
|
|
|
free(aesSalt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// peut être déporté le buffer lut avec methode pour travaillé dessus
|
|
|
|
int aes::readFile(unsigned char **file) {
|
|
|
|
FILE *fd = fopen(filename.c_str(), "rb");
|
|
|
|
if(fd == NULL) {
|
|
|
|
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine size of the file
|
|
|
|
fseek(fd, 0, SEEK_END);
|
|
|
|
size_t fileLength = ftell(fd);
|
|
|
|
fseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
// Allocate space for the file
|
|
|
|
*file = (unsigned char*)malloc(fileLength);
|
|
|
|
if(*file == NULL) {
|
|
|
|
fprintf(stderr, "Failed to allocate memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the file into the buffer
|
|
|
|
size_t bytesRead = fread(*file, 1, fileLength, fd);
|
|
|
|
|
|
|
|
if(bytesRead != fileLength) {
|
|
|
|
fprintf(stderr, "Error reading file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
|
|
|
|
return fileLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void aes::writeFile(unsigned char *file, size_t fileLength) {
|
|
|
|
FILE *fd = fopen(filename.c_str(), "wb");
|
|
|
|
if(fd == NULL) {
|
|
|
|
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bytesWritten = fwrite(file, 1, fileLength, fd);
|
|
|
|
|
|
|
|
if(bytesWritten != fileLength) {
|
|
|
|
fprintf(stderr, "Failed to write file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void aes::clear_all() {
|
|
|
|
free(aesIV);
|
|
|
|
free(aesKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
aes::~aes() {}
|
|
|
|
|
|
|
|
#endif
|