forked from blue/squawk
67 lines
1.5 KiB
C++
67 lines
1.5 KiB
C++
|
/*
|
||
|
* Created by victoria on 2021-05-13.
|
||
|
*/
|
||
|
|
||
|
#include "sha512_digest_openssl.h"
|
||
|
|
||
|
extern "C" {
|
||
|
#include <openssl/evp.h>
|
||
|
#include <openssl/rand.h>
|
||
|
#include <openssl/sha.h>
|
||
|
}
|
||
|
|
||
|
using namespace Signal::Crypto;
|
||
|
|
||
|
int Sha512::init(void **digest_context, void *) {
|
||
|
auto ctx = EVP_MD_CTX_create();
|
||
|
if (!ctx) {
|
||
|
return SG_ERR_NOMEM;
|
||
|
}
|
||
|
|
||
|
auto result = EVP_DigestInit_ex(ctx, EVP_sha512(), nullptr);
|
||
|
|
||
|
if (result == 1) {
|
||
|
*digest_context = ctx;
|
||
|
return SG_SUCCESS;
|
||
|
}
|
||
|
|
||
|
EVP_MD_CTX_destroy(ctx);
|
||
|
return SG_ERR_UNKNOWN;
|
||
|
}
|
||
|
|
||
|
int Sha512::update(void *digest_context, const uint8_t *data, size_t data_len, void *) {
|
||
|
auto ctx = static_cast<EVP_MD_CTX *>(digest_context);
|
||
|
auto result = EVP_DigestUpdate(ctx, data, data_len);
|
||
|
|
||
|
return (result == 1) ? SG_SUCCESS : SG_ERR_UNKNOWN;
|
||
|
}
|
||
|
|
||
|
int Sha512::final(void *digest_context, signal_buffer **output, void *) {
|
||
|
auto ctx = static_cast<EVP_MD_CTX *>(digest_context);
|
||
|
unsigned char md[EVP_MAX_MD_SIZE];
|
||
|
unsigned int len = 0;
|
||
|
|
||
|
auto result = EVP_DigestFinal_ex(ctx, md, &len);
|
||
|
if (result != 1) {
|
||
|
return SG_ERR_UNKNOWN;
|
||
|
}
|
||
|
|
||
|
result = EVP_DigestInit_ex(ctx, EVP_sha512(), nullptr);
|
||
|
if (result != 1) {
|
||
|
return SG_ERR_UNKNOWN;
|
||
|
}
|
||
|
|
||
|
signal_buffer *output_buffer = signal_buffer_create(md, len);
|
||
|
if (!output_buffer) {
|
||
|
return SG_ERR_NOMEM;
|
||
|
}
|
||
|
|
||
|
*output = output_buffer;
|
||
|
|
||
|
return SG_SUCCESS;
|
||
|
}
|
||
|
|
||
|
void Sha512::cleanup(void *digest_context, void *) {
|
||
|
auto ctx = static_cast<EVP_MD_CTX *>(digest_context);
|
||
|
EVP_MD_CTX_destroy(ctx);
|
||
|
}
|