]> the.earth.li Git - onak.git/blob - hash-helper.c
0.6.3 release
[onak.git] / hash-helper.c
1 /*
2  * hash-helper.c - Helper functions for calculating hashes
3  *
4  * Copyright Jonathan McDowell <noodles@earth.li>
5  *
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.
9  *
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
13  * more details.
14  *
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/>.
17  */
18
19 #include "build-config.h"
20 #include "hash-helper.h"
21 #include "onak.h"
22 #include "openpgp.h"
23
24 onak_status_t onak_hash(struct onak_hash_data *data, uint8_t *hash)
25 {
26         struct onak_hash_ctx hash_ctx;
27         int i;
28
29         if (data == NULL) {
30                 return ONAK_E_INVALID_PARAM;
31         }
32
33         if (data->chunks > MAX_HASH_CHUNKS) {
34                 return ONAK_E_INVALID_PARAM;
35         }
36
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]);
42                 }
43                 md5_digest(&hash_ctx.md5, MD5_DIGEST_SIZE, hash);
44                 break;
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]);
49                 }
50                 sha1_digest(&hash_ctx.sha1, SHA1_DIGEST_SIZE, hash);
51                 break;
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]);
56                 }
57                 sha1x_digest(&hash_ctx.sha1x, SHA1X_DIGEST_SIZE, hash);
58                 break;
59 #ifdef HAVE_NETTLE
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],
64                                 data->data[i]);
65                 }
66                 ripemd160_digest(&hash_ctx.ripemd160, RIPEMD160_DIGEST_SIZE,
67                         hash);
68                 break;
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],
73                                 data->data[i]);
74                 }
75                 sha224_digest(&hash_ctx.sha224, SHA224_DIGEST_SIZE, hash);
76                 break;
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],
81                                 data->data[i]);
82                 }
83                 sha256_digest(&hash_ctx.sha256, SHA256_DIGEST_SIZE, hash);
84                 break;
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],
89                                 data->data[i]);
90                 }
91                 sha384_digest(&hash_ctx.sha384, SHA384_DIGEST_SIZE, hash);
92                 break;
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],
97                                 data->data[i]);
98                 }
99                 sha512_digest(&hash_ctx.sha512, SHA512_DIGEST_SIZE, hash);
100                 break;
101 #endif
102         default:
103                 return ONAK_E_UNSUPPORTED_FEATURE;
104         }
105
106         return ONAK_E_OK;
107 }