|
| 1 | +/* |
| 2 | + * Copyright (c) 2009-2011 Thomas Harning Jr. |
| 3 | + * Released under the MIT license. See included LICENSE details. |
| 4 | + */ |
| 5 | +#include "cryptoface.h" |
| 6 | +#include "cryptoface_impl.h" |
| 7 | +#include "digest_impl.h" |
| 8 | + |
| 9 | +#include <string.h> /* memcpy */ |
| 10 | +#include <nettle/nettle-meta.h> |
| 11 | + |
| 12 | +struct _nettle_digest { |
| 13 | + struct cf_digest digest; |
| 14 | + const struct nettle_hash *hash; |
| 15 | + char context[1]; |
| 16 | +}; |
| 17 | +#define NETTLE_DIGEST_STRUCT_SIZE(context_size) (sizeof (struct _nettle_digest) - 1 + context_size) |
| 18 | + |
| 19 | +static cf_digest_t create_hash_container(const struct nettle_hash *hash); |
| 20 | + |
| 21 | +static cf_rv_t _digest_update(cf_digest_t digest, void *data, size_t data_len) { |
| 22 | + struct _nettle_digest *impl = (struct _nettle_digest*)digest; |
| 23 | + impl->hash->update((void*)impl->context, data_len, data); |
| 24 | + return CF_S_OK; |
| 25 | +} |
| 26 | + |
| 27 | +static cf_rv_t _digest_finish(cf_digest_t digest, void *output, size_t *output_len) { |
| 28 | + struct _nettle_digest *impl = (struct _nettle_digest*)digest; |
| 29 | + size_t real_len = impl->hash->digest_size; |
| 30 | + if(!output && output_len) { |
| 31 | + *output_len = real_len; |
| 32 | + return CF_S_OK; |
| 33 | + } |
| 34 | + if(!output && !output_len) { |
| 35 | + free(impl); |
| 36 | + return CF_S_OK; |
| 37 | + } |
| 38 | + if(*output_len < real_len) { |
| 39 | + *output_len = real_len; |
| 40 | + return CF_E_INSUFFICIENT_BUFFER; |
| 41 | + } |
| 42 | + *output_len = real_len; |
| 43 | + impl->hash->digest((void*)impl->context, *output_len, output); |
| 44 | + free(impl); |
| 45 | + return CF_S_OK; |
| 46 | +} |
| 47 | + |
| 48 | +static cf_rv_t _digest_clone(cf_digest_t *new_digest, cf_digest_t source) { |
| 49 | + struct _nettle_digest *impl = (struct _nettle_digest*)impl; |
| 50 | + struct _nettle_digest *new_impl; |
| 51 | + *new_digest = create_hash_container(impl->hash); |
| 52 | + if (!*new_digest) { |
| 53 | + return CF_E_MEM; |
| 54 | + } |
| 55 | + new_impl = (struct _nettle_digest*)*new_digest; |
| 56 | + memcpy(new_impl->context, impl->context, impl->hash->context_size); |
| 57 | + return CF_S_OK; |
| 58 | +} |
| 59 | + |
| 60 | +static struct cf_digest_instance_ops nettle_digest_instance_ops = { |
| 61 | + _digest_update, |
| 62 | + _digest_finish, |
| 63 | + _digest_clone |
| 64 | +}; |
| 65 | + |
| 66 | +static cf_digest_t create_hash_container(const struct nettle_hash *hash) { |
| 67 | + struct _nettle_digest *impl = (struct _nettle_digest*)calloc(1, NETTLE_DIGEST_STRUCT_SIZE(hash->context_size)); |
| 68 | + if(!impl) { |
| 69 | + return NULL; |
| 70 | + } |
| 71 | + impl->hash = hash; |
| 72 | + impl->digest.ops = &nettle_digest_instance_ops; |
| 73 | + return (cf_digest_t)impl; |
| 74 | +} |
| 75 | + |
| 76 | +static cf_rv_t _digest_init(cf_digest_t *digest, cf_provider_t provider, cf_digest_id_t id) { |
| 77 | + const struct nettle_hash *hash = (struct nettle_hash *)id; |
| 78 | + struct _nettle_digest *impl; |
| 79 | + *digest = create_hash_container(hash); |
| 80 | + impl = (struct _nettle_digest*)*digest; |
| 81 | + if(!impl) { |
| 82 | + return CF_E_MEM; |
| 83 | + } |
| 84 | + hash->init((void*)impl->context); |
| 85 | + return CF_S_OK; |
| 86 | +} |
| 87 | + |
| 88 | +#define DIGEST(digest) (&digest), |
| 89 | + |
| 90 | +static const struct nettle_hash *hash_list[] = { |
| 91 | +#include "digest_list.h" |
| 92 | + NULL |
| 93 | +}; |
| 94 | +#undef DIGEST |
| 95 | + |
| 96 | +/* collect information */ |
| 97 | +static cf_rv_t _digest_list_begin(cf_provider_t provider, void **iter) { |
| 98 | + *iter = (void*)hash_list; |
| 99 | + return CF_S_OK; |
| 100 | +} |
| 101 | +static cf_rv_t _digest_list_next(cf_provider_t provider, void **iter, struct cf_digest_info *info) { |
| 102 | + const struct nettle_hash** hash_iter = (const struct nettle_hash**)*iter; |
| 103 | + const struct nettle_hash *hash = hash_iter ? *hash_iter : NULL; |
| 104 | + if(!hash) { |
| 105 | + return CF_S_COMPLETE; |
| 106 | + } |
| 107 | + info->id = (cf_digest_id_t)hash; |
| 108 | + info->name = hash->name; |
| 109 | + info->block_size = hash->digest_size; |
| 110 | + *iter = (void*)(hash_iter + 1); |
| 111 | + return CF_S_OK; |
| 112 | +} |
| 113 | +static cf_rv_t _digest_list_end(cf_provider_t provider, void **iter) { |
| 114 | + *iter = NULL; |
| 115 | + return CF_S_OK; |
| 116 | +} |
| 117 | + |
| 118 | +struct cf_digest_ops nettle_digest_ops = { |
| 119 | + _digest_init, |
| 120 | + _digest_list_begin, |
| 121 | + _digest_list_next, |
| 122 | + _digest_list_end |
| 123 | +}; |
0 commit comments