| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/hash.h> |
| 6 |
#include <aws/cal/private/opensslcrypto_common.h> |
| 7 |
|
| 8 |
#include <openssl/evp.h> |
| 9 |
#include <openssl/sha.h> |
| 10 |
|
| 11 |
static void s_destroy(struct aws_hash *hash); |
| 12 |
static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash); |
| 13 |
static int s_finalize(struct aws_hash *hash, struct aws_byte_buf *output); |
| 14 |
|
| 15 |
static struct aws_hash_vtable s_md5_vtable = { |
| 16 |
.destroy = s_destroy, |
| 17 |
.update = s_update, |
| 18 |
.finalize = s_finalize, |
| 19 |
.alg_name = "MD5", |
| 20 |
.provider = "OpenSSL Compatible libcrypto", |
| 21 |
}; |
| 22 |
|
| 23 |
static struct aws_hash_vtable s_sha512_vtable = { |
| 24 |
.destroy = s_destroy, |
| 25 |
.update = s_update, |
| 26 |
.finalize = s_finalize, |
| 27 |
.alg_name = "SHA512", |
| 28 |
.provider = "OpenSSL Compatible libcrypto", |
| 29 |
}; |
| 30 |
|
| 31 |
static struct aws_hash_vtable s_sha256_vtable = { |
| 32 |
.destroy = s_destroy, |
| 33 |
.update = s_update, |
| 34 |
.finalize = s_finalize, |
| 35 |
.alg_name = "SHA256", |
| 36 |
.provider = "OpenSSL Compatible libcrypto", |
| 37 |
}; |
| 38 |
|
| 39 |
static struct aws_hash_vtable s_sha1_vtable = { |
| 40 |
.destroy = s_destroy, |
| 41 |
.update = s_update, |
| 42 |
.finalize = s_finalize, |
| 43 |
.alg_name = "SHA1", |
| 44 |
.provider = "OpenSSL Compatible libcrypto", |
| 45 |
}; |
| 46 |
|
| 47 |
static void s_destroy(struct aws_hash *hash) { |
| 48 |
if (hash == NULL) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
EVP_MD_CTX *ctx = hash->impl; |
| 53 |
if (ctx != NULL) { |
| 54 |
g_aws_openssl_evp_md_ctx_table->free_fn(ctx); |
| 55 |
} |
| 56 |
|
| 57 |
aws_mem_release(hash->allocator, hash); |
| 58 |
} |
| 59 |
|
| 60 |
struct aws_hash *aws_md5_default_new(struct aws_allocator *allocator) { |
| 61 |
struct aws_hash *hash = aws_mem_acquire(allocator, sizeof(struct aws_hash)); |
| 62 |
|
| 63 |
hash->allocator = allocator; |
| 64 |
hash->vtable = &s_md5_vtable; |
| 65 |
hash->digest_size = AWS_MD5_LEN; |
| 66 |
EVP_MD_CTX *ctx = g_aws_openssl_evp_md_ctx_table->new_fn(); |
| 67 |
hash->impl = ctx; |
| 68 |
hash->good = true; |
| 69 |
|
| 70 |
AWS_FATAL_ASSERT(hash->impl); |
| 71 |
|
| 72 |
if (!g_aws_openssl_evp_md_ctx_table->init_ex_fn(ctx, EVP_md5(), NULL)) { |
| 73 |
s_destroy(hash); |
| 74 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
|
| 78 |
return hash; |
| 79 |
} |
| 80 |
|
| 81 |
struct aws_hash *aws_sha512_default_new(struct aws_allocator *allocator) { |
| 82 |
struct aws_hash *hash = aws_mem_acquire(allocator, sizeof(struct aws_hash)); |
| 83 |
|
| 84 |
hash->allocator = allocator; |
| 85 |
hash->vtable = &s_sha512_vtable; |
| 86 |
hash->digest_size = AWS_SHA512_LEN; |
| 87 |
EVP_MD_CTX *ctx = g_aws_openssl_evp_md_ctx_table->new_fn(); |
| 88 |
hash->impl = ctx; |
| 89 |
hash->good = true; |
| 90 |
|
| 91 |
AWS_FATAL_ASSERT(hash->impl); |
| 92 |
|
| 93 |
if (!g_aws_openssl_evp_md_ctx_table->init_ex_fn(ctx, EVP_sha512(), NULL)) { |
| 94 |
s_destroy(hash); |
| 95 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
return hash; |
| 100 |
} |
| 101 |
|
| 102 |
struct aws_hash *aws_sha256_default_new(struct aws_allocator *allocator) { |
| 103 |
struct aws_hash *hash = aws_mem_acquire(allocator, sizeof(struct aws_hash)); |
| 104 |
|
| 105 |
hash->allocator = allocator; |
| 106 |
hash->vtable = &s_sha256_vtable; |
| 107 |
hash->digest_size = AWS_SHA256_LEN; |
| 108 |
EVP_MD_CTX *ctx = g_aws_openssl_evp_md_ctx_table->new_fn(); |
| 109 |
hash->impl = ctx; |
| 110 |
hash->good = true; |
| 111 |
|
| 112 |
AWS_FATAL_ASSERT(hash->impl); |
| 113 |
|
| 114 |
if (!g_aws_openssl_evp_md_ctx_table->init_ex_fn(ctx, EVP_sha256(), NULL)) { |
| 115 |
s_destroy(hash); |
| 116 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
return hash; |
| 121 |
} |
| 122 |
|
| 123 |
struct aws_hash *aws_sha1_default_new(struct aws_allocator *allocator) { |
| 124 |
struct aws_hash *hash = aws_mem_acquire(allocator, sizeof(struct aws_hash)); |
| 125 |
|
| 126 |
hash->allocator = allocator; |
| 127 |
hash->vtable = &s_sha1_vtable; |
| 128 |
hash->digest_size = AWS_SHA1_LEN; |
| 129 |
EVP_MD_CTX *ctx = g_aws_openssl_evp_md_ctx_table->new_fn(); |
| 130 |
hash->impl = ctx; |
| 131 |
hash->good = true; |
| 132 |
|
| 133 |
AWS_FATAL_ASSERT(hash->impl); |
| 134 |
|
| 135 |
if (!g_aws_openssl_evp_md_ctx_table->init_ex_fn(ctx, EVP_sha1(), NULL)) { |
| 136 |
s_destroy(hash); |
| 137 |
aws_raise_error(AWS_ERROR_UNKNOWN); |
| 138 |
return NULL; |
| 139 |
} |
| 140 |
|
| 141 |
return hash; |
| 142 |
} |
| 143 |
|
| 144 |
static int s_update(struct aws_hash *hash, const struct aws_byte_cursor *to_hash) { |
| 145 |
if (!hash->good) { |
| 146 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 147 |
} |
| 148 |
|
| 149 |
EVP_MD_CTX *ctx = hash->impl; |
| 150 |
|
| 151 |
if (AWS_LIKELY(g_aws_openssl_evp_md_ctx_table->update_fn(ctx, to_hash->ptr, to_hash->len))) { |
| 152 |
return AWS_OP_SUCCESS; |
| 153 |
} |
| 154 |
|
| 155 |
hash->good = false; |
| 156 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 157 |
} |
| 158 |
|
| 159 |
static int s_finalize(struct aws_hash *hash, struct aws_byte_buf *output) { |
| 160 |
if (!hash->good) { |
| 161 |
return aws_raise_error(AWS_ERROR_INVALID_STATE); |
| 162 |
} |
| 163 |
|
| 164 |
EVP_MD_CTX *ctx = hash->impl; |
| 165 |
|
| 166 |
size_t buffer_len = output->capacity - output->len; |
| 167 |
|
| 168 |
if (buffer_len < hash->digest_size) { |
| 169 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 170 |
} |
| 171 |
|
| 172 |
if (AWS_LIKELY(g_aws_openssl_evp_md_ctx_table->final_ex_fn( |
| 173 |
ctx, output->buffer + output->len, (unsigned int *)&buffer_len))) { |
| 174 |
output->len += hash->digest_size; |
| 175 |
hash->good = false; |
| 176 |
return AWS_OP_SUCCESS; |
| 177 |
} |
| 178 |
|
| 179 |
hash->good = false; |
| 180 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 181 |
} |