2 be288a59 2023-02-23 thomas * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 be288a59 2023-02-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 be288a59 2023-02-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 be288a59 2023-02-23 thomas * copyright notice and this permission notice appear in all copies.
8 be288a59 2023-02-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 be288a59 2023-02-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 be288a59 2023-02-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 be288a59 2023-02-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 be288a59 2023-02-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 be288a59 2023-02-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 be288a59 2023-02-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 68069cf6 2023-03-08 thomas #include "got_compat.h"
19 be288a59 2023-02-23 thomas #include <sys/types.h>
20 c8ae092d 2023-02-23 thomas #include <sys/queue.h>
22 be288a59 2023-02-23 thomas #include <errno.h>
23 be288a59 2023-02-23 thomas #include <stdio.h>
24 be288a59 2023-02-23 thomas #include <stdlib.h>
25 c8ae092d 2023-02-23 thomas #include <string.h>
26 be288a59 2023-02-23 thomas #include <limits.h>
28 c8ae092d 2023-02-23 thomas #include "got_object.h"
29 232c0ac1 2023-04-22 thomas #include "got_error.h"
31 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
33 232c0ac1 2023-04-22 thomas struct got_object_id *
34 232c0ac1 2023-04-22 thomas got_object_id_dup(struct got_object_id *id1)
36 232c0ac1 2023-04-22 thomas struct got_object_id *id2;
38 232c0ac1 2023-04-22 thomas id2 = malloc(sizeof(*id2));
39 232c0ac1 2023-04-22 thomas if (id2 == NULL)
40 232c0ac1 2023-04-22 thomas return NULL;
41 232c0ac1 2023-04-22 thomas memcpy(id2, id1, sizeof(*id2));
42 232c0ac1 2023-04-22 thomas return id2;
46 232c0ac1 2023-04-22 thomas got_object_id_cmp(const struct got_object_id *id1,
47 232c0ac1 2023-04-22 thomas const struct got_object_id *id2)
49 232c0ac1 2023-04-22 thomas return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
52 232c0ac1 2023-04-22 thomas const struct got_error *
53 232c0ac1 2023-04-22 thomas got_object_id_str(char **outbuf, struct got_object_id *id)
55 232c0ac1 2023-04-22 thomas static const size_t len = GOT_OBJECT_ID_HEX_MAXLEN;
57 232c0ac1 2023-04-22 thomas *outbuf = malloc(len);
58 232c0ac1 2023-04-22 thomas if (*outbuf == NULL)
59 232c0ac1 2023-04-22 thomas return got_error_from_errno("malloc");
61 232c0ac1 2023-04-22 thomas if (got_object_id_hex(id, *outbuf, len) == NULL) {
62 232c0ac1 2023-04-22 thomas free(*outbuf);
63 232c0ac1 2023-04-22 thomas *outbuf = NULL;
64 232c0ac1 2023-04-22 thomas return got_error(GOT_ERR_BAD_OBJ_ID_STR);
67 232c0ac1 2023-04-22 thomas return NULL;
71 be288a59 2023-02-23 thomas got_parse_xdigit(uint8_t *val, const char *hex)
74 be288a59 2023-02-23 thomas long lval;
76 be288a59 2023-02-23 thomas errno = 0;
77 be288a59 2023-02-23 thomas lval = strtol(hex, &ep, 16);
78 be288a59 2023-02-23 thomas if (hex[0] == '\0' || *ep != '\0')
80 be288a59 2023-02-23 thomas if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
83 be288a59 2023-02-23 thomas *val = (uint8_t)lval;
87 c8ae092d 2023-02-23 thomas static int
88 c8ae092d 2023-02-23 thomas parse_digest(uint8_t *digest, int len, const char *line)
90 be288a59 2023-02-23 thomas uint8_t b = 0;
91 be288a59 2023-02-23 thomas char hex[3] = {'\0', '\0', '\0'};
94 c8ae092d 2023-02-23 thomas for (i = 0; i < len; i++) {
95 be288a59 2023-02-23 thomas if (line[0] == '\0' || line[1] == '\0')
97 be288a59 2023-02-23 thomas for (j = 0; j < 2; j++) {
98 be288a59 2023-02-23 thomas hex[j] = *line;
101 be288a59 2023-02-23 thomas if (!got_parse_xdigit(&b, hex))
102 be288a59 2023-02-23 thomas return 0;
103 be288a59 2023-02-23 thomas digest[i] = b;
106 be288a59 2023-02-23 thomas return 1;
109 c8ae092d 2023-02-23 thomas static char *
110 c8ae092d 2023-02-23 thomas digest_to_str(const uint8_t *digest, int len, char *buf)
112 6853f3b1 2023-02-24 thomas const char hex[] = "0123456789abcdef";
113 be288a59 2023-02-23 thomas char *p = buf;
116 c8ae092d 2023-02-23 thomas for (i = 0; i < len; i++) {
117 6853f3b1 2023-02-24 thomas *p++ = hex[digest[i] >> 4];
118 6853f3b1 2023-02-24 thomas *p++ = hex[digest[i] & 0xf];
120 6853f3b1 2023-02-24 thomas *p = '\0';
122 be288a59 2023-02-23 thomas return buf;
126 c8ae092d 2023-02-23 thomas got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
128 c8ae092d 2023-02-23 thomas if (size < SHA1_DIGEST_STRING_LENGTH)
129 c8ae092d 2023-02-23 thomas return NULL;
130 c8ae092d 2023-02-23 thomas return digest_to_str(digest, SHA1_DIGEST_LENGTH, buf);
134 c8ae092d 2023-02-23 thomas got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
136 c8ae092d 2023-02-23 thomas if (size < SHA256_DIGEST_STRING_LENGTH)
137 c8ae092d 2023-02-23 thomas return NULL;
138 c8ae092d 2023-02-23 thomas return digest_to_str(digest, SHA256_DIGEST_LENGTH, buf);
142 c8ae092d 2023-02-23 thomas got_parse_hash_digest(uint8_t *digest, const char *line,
143 c8ae092d 2023-02-23 thomas enum got_hash_algorithm algo)
145 c8ae092d 2023-02-23 thomas switch (algo) {
146 c8ae092d 2023-02-23 thomas case GOT_HASH_SHA1:
147 c8ae092d 2023-02-23 thomas return parse_digest(digest, SHA1_DIGEST_LENGTH, line);
148 c8ae092d 2023-02-23 thomas case GOT_HASH_SHA256:
149 c8ae092d 2023-02-23 thomas return parse_digest(digest, SHA256_DIGEST_LENGTH, line);
151 c8ae092d 2023-02-23 thomas return 0;
156 232c0ac1 2023-04-22 thomas got_object_id_hex(struct got_object_id *id, char *buf, size_t len)
158 232c0ac1 2023-04-22 thomas return got_sha1_digest_to_str(id->sha1, buf, len);
162 c8ae092d 2023-02-23 thomas got_parse_object_id(struct got_object_id *id, const char *line,
163 c8ae092d 2023-02-23 thomas enum got_hash_algorithm algo)
165 c8ae092d 2023-02-23 thomas memset(id, 0, sizeof(*id));
167 c8ae092d 2023-02-23 thomas /* XXX: temporary until we grow got_object_id */
168 c8ae092d 2023-02-23 thomas if (algo != GOT_HASH_SHA1)
169 c8ae092d 2023-02-23 thomas return 0;
171 c8ae092d 2023-02-23 thomas return got_parse_hash_digest(id->sha1, line, algo);
175 b16893ba 2023-02-24 thomas got_hash_init(struct got_hash *hash, enum got_hash_algorithm algo)
177 b16893ba 2023-02-24 thomas memset(hash, 0, sizeof(*hash));
178 b16893ba 2023-02-24 thomas hash->algo = algo;
180 b16893ba 2023-02-24 thomas if (algo == GOT_HASH_SHA1)
181 b16893ba 2023-02-24 thomas SHA1Init(&hash->sha1_ctx);
182 b16893ba 2023-02-24 thomas else if (algo == GOT_HASH_SHA256)
183 b16893ba 2023-02-24 thomas SHA256Init(&hash->sha256_ctx);
187 b16893ba 2023-02-24 thomas got_hash_update(struct got_hash *hash, const void *data, size_t len)
189 b16893ba 2023-02-24 thomas if (hash->algo == GOT_HASH_SHA1)
190 b16893ba 2023-02-24 thomas SHA1Update(&hash->sha1_ctx, data, len);
191 b16893ba 2023-02-24 thomas else if (hash->algo == GOT_HASH_SHA256)
192 b16893ba 2023-02-24 thomas SHA256Update(&hash->sha256_ctx, data, len);
196 b16893ba 2023-02-24 thomas got_hash_final(struct got_hash *hash, uint8_t *out)
198 b16893ba 2023-02-24 thomas if (hash->algo == GOT_HASH_SHA1)
199 b16893ba 2023-02-24 thomas SHA1Final(out, &hash->sha1_ctx);
200 b16893ba 2023-02-24 thomas else if (hash->algo == GOT_HASH_SHA256)
201 b16893ba 2023-02-24 thomas SHA256Final(out, &hash->sha256_ctx);
205 b16893ba 2023-02-24 thomas got_hash_final_object_id(struct got_hash *hash, struct got_object_id *id)
207 b16893ba 2023-02-24 thomas memset(id, 0, sizeof(*id));
208 b16893ba 2023-02-24 thomas got_hash_final(hash, id->sha1);
212 b16893ba 2023-02-24 thomas got_hash_cmp(enum got_hash_algorithm algo, uint8_t *b1, uint8_t *b2)
214 b16893ba 2023-02-24 thomas if (algo == GOT_HASH_SHA1)
215 b16893ba 2023-02-24 thomas return memcmp(b1, b2, SHA1_DIGEST_LENGTH);
216 b16893ba 2023-02-24 thomas else if (algo == GOT_HASH_SHA256)
217 b16893ba 2023-02-24 thomas return memcmp(b1, b2, SHA256_DIGEST_LENGTH);
218 b16893ba 2023-02-24 thomas return -1;