2 * hash-helper.c - Helper functions for calculating hashes
4 * Copyright Jonathan McDowell <noodles@earth.li>
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <https://www.gnu.org/licenses/>.
19 #include "build-config.h"
20 #include "hash-helper.h"
24 onak_status_t onak_hash(struct onak_hash_data *data, uint8_t *hash)
26 struct onak_hash_ctx hash_ctx;
30 return ONAK_E_INVALID_PARAM;
33 if (data->chunks > MAX_HASH_CHUNKS) {
34 return ONAK_E_INVALID_PARAM;
37 switch (data->hashtype) {
38 case OPENPGP_HASH_MD5:
39 md5_init(&hash_ctx.md5);
40 for (i = 0; i < data->chunks; i++) {
41 md5_update(&hash_ctx.md5, data->len[i], data->data[i]);
43 md5_digest(&hash_ctx.md5, MD5_DIGEST_SIZE, hash);
45 case OPENPGP_HASH_SHA1:
46 sha1_init(&hash_ctx.sha1);
47 for (i = 0; i < data->chunks; i++) {
48 sha1_update(&hash_ctx.sha1, data->len[i], data->data[i]);
50 sha1_digest(&hash_ctx.sha1, SHA1_DIGEST_SIZE, hash);
52 case OPENPGP_HASH_SHA1X:
53 sha1x_init(&hash_ctx.sha1x);
54 for (i = 0; i < data->chunks; i++) {
55 sha1x_update(&hash_ctx.sha1x, data->len[i], data->data[i]);
57 sha1x_digest(&hash_ctx.sha1x, SHA1X_DIGEST_SIZE, hash);
60 case OPENPGP_HASH_RIPEMD160:
61 ripemd160_init(&hash_ctx.ripemd160);
62 for (i = 0; i < data->chunks; i++) {
63 ripemd160_update(&hash_ctx.ripemd160, data->len[i],
66 ripemd160_digest(&hash_ctx.ripemd160, RIPEMD160_DIGEST_SIZE,
69 case OPENPGP_HASH_SHA224:
70 sha224_init(&hash_ctx.sha224);
71 for (i = 0; i < data->chunks; i++) {
72 sha224_update(&hash_ctx.sha224, data->len[i],
75 sha224_digest(&hash_ctx.sha224, SHA224_DIGEST_SIZE, hash);
77 case OPENPGP_HASH_SHA256:
78 sha256_init(&hash_ctx.sha256);
79 for (i = 0; i < data->chunks; i++) {
80 sha256_update(&hash_ctx.sha256, data->len[i],
83 sha256_digest(&hash_ctx.sha256, SHA256_DIGEST_SIZE, hash);
85 case OPENPGP_HASH_SHA384:
86 sha384_init(&hash_ctx.sha384);
87 for (i = 0; i < data->chunks; i++) {
88 sha384_update(&hash_ctx.sha384, data->len[i],
91 sha384_digest(&hash_ctx.sha384, SHA384_DIGEST_SIZE, hash);
93 case OPENPGP_HASH_SHA512:
94 sha512_init(&hash_ctx.sha512);
95 for (i = 0; i < data->chunks; i++) {
96 sha512_update(&hash_ctx.sha512, data->len[i],
99 sha512_digest(&hash_ctx.sha512, SHA512_DIGEST_SIZE, hash);
103 return ONAK_E_UNSUPPORTED_FEATURE;