Blame


1 be288a59 2023-02-23 thomas /*
2 be288a59 2023-02-23 thomas * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 be288a59 2023-02-23 thomas *
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.
7 be288a59 2023-02-23 thomas *
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.
15 be288a59 2023-02-23 thomas */
16 be288a59 2023-02-23 thomas
17 68069cf6 2023-03-08 thomas #include "got_compat.h"
18 68069cf6 2023-03-08 thomas
19 be288a59 2023-02-23 thomas #include <sys/types.h>
20 c8ae092d 2023-02-23 thomas #include <sys/queue.h>
21 c8ae092d 2023-02-23 thomas
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>
27 be288a59 2023-02-23 thomas
28 c8ae092d 2023-02-23 thomas #include "got_object.h"
29 c8ae092d 2023-02-23 thomas
30 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
31 be288a59 2023-02-23 thomas
32 be288a59 2023-02-23 thomas int
33 be288a59 2023-02-23 thomas got_parse_xdigit(uint8_t *val, const char *hex)
34 be288a59 2023-02-23 thomas {
35 be288a59 2023-02-23 thomas char *ep;
36 be288a59 2023-02-23 thomas long lval;
37 be288a59 2023-02-23 thomas
38 be288a59 2023-02-23 thomas errno = 0;
39 be288a59 2023-02-23 thomas lval = strtol(hex, &ep, 16);
40 be288a59 2023-02-23 thomas if (hex[0] == '\0' || *ep != '\0')
41 be288a59 2023-02-23 thomas return 0;
42 be288a59 2023-02-23 thomas if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
43 be288a59 2023-02-23 thomas return 0;
44 be288a59 2023-02-23 thomas
45 be288a59 2023-02-23 thomas *val = (uint8_t)lval;
46 be288a59 2023-02-23 thomas return 1;
47 be288a59 2023-02-23 thomas }
48 be288a59 2023-02-23 thomas
49 c8ae092d 2023-02-23 thomas static int
50 c8ae092d 2023-02-23 thomas parse_digest(uint8_t *digest, int len, const char *line)
51 be288a59 2023-02-23 thomas {
52 be288a59 2023-02-23 thomas uint8_t b = 0;
53 be288a59 2023-02-23 thomas char hex[3] = {'\0', '\0', '\0'};
54 be288a59 2023-02-23 thomas int i, j;
55 be288a59 2023-02-23 thomas
56 c8ae092d 2023-02-23 thomas for (i = 0; i < len; i++) {
57 be288a59 2023-02-23 thomas if (line[0] == '\0' || line[1] == '\0')
58 be288a59 2023-02-23 thomas return 0;
59 be288a59 2023-02-23 thomas for (j = 0; j < 2; j++) {
60 be288a59 2023-02-23 thomas hex[j] = *line;
61 be288a59 2023-02-23 thomas line++;
62 be288a59 2023-02-23 thomas }
63 be288a59 2023-02-23 thomas if (!got_parse_xdigit(&b, hex))
64 be288a59 2023-02-23 thomas return 0;
65 be288a59 2023-02-23 thomas digest[i] = b;
66 be288a59 2023-02-23 thomas }
67 be288a59 2023-02-23 thomas
68 be288a59 2023-02-23 thomas return 1;
69 be288a59 2023-02-23 thomas }
70 be288a59 2023-02-23 thomas
71 c8ae092d 2023-02-23 thomas static char *
72 c8ae092d 2023-02-23 thomas digest_to_str(const uint8_t *digest, int len, char *buf)
73 be288a59 2023-02-23 thomas {
74 6853f3b1 2023-02-24 thomas const char hex[] = "0123456789abcdef";
75 be288a59 2023-02-23 thomas char *p = buf;
76 be288a59 2023-02-23 thomas int i;
77 be288a59 2023-02-23 thomas
78 c8ae092d 2023-02-23 thomas for (i = 0; i < len; i++) {
79 6853f3b1 2023-02-24 thomas *p++ = hex[digest[i] >> 4];
80 6853f3b1 2023-02-24 thomas *p++ = hex[digest[i] & 0xf];
81 be288a59 2023-02-23 thomas }
82 6853f3b1 2023-02-24 thomas *p = '\0';
83 be288a59 2023-02-23 thomas
84 be288a59 2023-02-23 thomas return buf;
85 be288a59 2023-02-23 thomas }
86 c8ae092d 2023-02-23 thomas
87 c8ae092d 2023-02-23 thomas char *
88 c8ae092d 2023-02-23 thomas got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
89 c8ae092d 2023-02-23 thomas {
90 c8ae092d 2023-02-23 thomas if (size < SHA1_DIGEST_STRING_LENGTH)
91 c8ae092d 2023-02-23 thomas return NULL;
92 c8ae092d 2023-02-23 thomas return digest_to_str(digest, SHA1_DIGEST_LENGTH, buf);
93 c8ae092d 2023-02-23 thomas }
94 c8ae092d 2023-02-23 thomas
95 c8ae092d 2023-02-23 thomas char *
96 c8ae092d 2023-02-23 thomas got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
97 c8ae092d 2023-02-23 thomas {
98 c8ae092d 2023-02-23 thomas if (size < SHA256_DIGEST_STRING_LENGTH)
99 c8ae092d 2023-02-23 thomas return NULL;
100 c8ae092d 2023-02-23 thomas return digest_to_str(digest, SHA256_DIGEST_LENGTH, buf);
101 c8ae092d 2023-02-23 thomas }
102 c8ae092d 2023-02-23 thomas
103 c8ae092d 2023-02-23 thomas int
104 c8ae092d 2023-02-23 thomas got_parse_hash_digest(uint8_t *digest, const char *line,
105 c8ae092d 2023-02-23 thomas enum got_hash_algorithm algo)
106 c8ae092d 2023-02-23 thomas {
107 c8ae092d 2023-02-23 thomas switch (algo) {
108 c8ae092d 2023-02-23 thomas case GOT_HASH_SHA1:
109 c8ae092d 2023-02-23 thomas return parse_digest(digest, SHA1_DIGEST_LENGTH, line);
110 c8ae092d 2023-02-23 thomas case GOT_HASH_SHA256:
111 c8ae092d 2023-02-23 thomas return parse_digest(digest, SHA256_DIGEST_LENGTH, line);
112 c8ae092d 2023-02-23 thomas default:
113 c8ae092d 2023-02-23 thomas return 0;
114 c8ae092d 2023-02-23 thomas }
115 c8ae092d 2023-02-23 thomas }
116 c8ae092d 2023-02-23 thomas
117 c8ae092d 2023-02-23 thomas int
118 c8ae092d 2023-02-23 thomas got_parse_object_id(struct got_object_id *id, const char *line,
119 c8ae092d 2023-02-23 thomas enum got_hash_algorithm algo)
120 c8ae092d 2023-02-23 thomas {
121 c8ae092d 2023-02-23 thomas memset(id, 0, sizeof(*id));
122 c8ae092d 2023-02-23 thomas
123 c8ae092d 2023-02-23 thomas /* XXX: temporary until we grow got_object_id */
124 c8ae092d 2023-02-23 thomas if (algo != GOT_HASH_SHA1)
125 c8ae092d 2023-02-23 thomas return 0;
126 c8ae092d 2023-02-23 thomas
127 c8ae092d 2023-02-23 thomas return got_parse_hash_digest(id->sha1, line, algo);
128 c8ae092d 2023-02-23 thomas }
129 b16893ba 2023-02-24 thomas
130 b16893ba 2023-02-24 thomas void
131 b16893ba 2023-02-24 thomas got_hash_init(struct got_hash *hash, enum got_hash_algorithm algo)
132 b16893ba 2023-02-24 thomas {
133 b16893ba 2023-02-24 thomas memset(hash, 0, sizeof(*hash));
134 b16893ba 2023-02-24 thomas hash->algo = algo;
135 b16893ba 2023-02-24 thomas
136 b16893ba 2023-02-24 thomas if (algo == GOT_HASH_SHA1)
137 b16893ba 2023-02-24 thomas SHA1Init(&hash->sha1_ctx);
138 b16893ba 2023-02-24 thomas else if (algo == GOT_HASH_SHA256)
139 b16893ba 2023-02-24 thomas SHA256Init(&hash->sha256_ctx);
140 b16893ba 2023-02-24 thomas }
141 b16893ba 2023-02-24 thomas
142 b16893ba 2023-02-24 thomas void
143 b16893ba 2023-02-24 thomas got_hash_update(struct got_hash *hash, const void *data, size_t len)
144 b16893ba 2023-02-24 thomas {
145 b16893ba 2023-02-24 thomas if (hash->algo == GOT_HASH_SHA1)
146 b16893ba 2023-02-24 thomas SHA1Update(&hash->sha1_ctx, data, len);
147 b16893ba 2023-02-24 thomas else if (hash->algo == GOT_HASH_SHA256)
148 b16893ba 2023-02-24 thomas SHA256Update(&hash->sha256_ctx, data, len);
149 b16893ba 2023-02-24 thomas }
150 b16893ba 2023-02-24 thomas
151 b16893ba 2023-02-24 thomas void
152 b16893ba 2023-02-24 thomas got_hash_final(struct got_hash *hash, uint8_t *out)
153 b16893ba 2023-02-24 thomas {
154 b16893ba 2023-02-24 thomas if (hash->algo == GOT_HASH_SHA1)
155 b16893ba 2023-02-24 thomas SHA1Final(out, &hash->sha1_ctx);
156 b16893ba 2023-02-24 thomas else if (hash->algo == GOT_HASH_SHA256)
157 b16893ba 2023-02-24 thomas SHA256Final(out, &hash->sha256_ctx);
158 b16893ba 2023-02-24 thomas }
159 b16893ba 2023-02-24 thomas
160 b16893ba 2023-02-24 thomas void
161 b16893ba 2023-02-24 thomas got_hash_final_object_id(struct got_hash *hash, struct got_object_id *id)
162 b16893ba 2023-02-24 thomas {
163 b16893ba 2023-02-24 thomas memset(id, 0, sizeof(*id));
164 b16893ba 2023-02-24 thomas got_hash_final(hash, id->sha1);
165 b16893ba 2023-02-24 thomas }
166 b16893ba 2023-02-24 thomas
167 b16893ba 2023-02-24 thomas int
168 b16893ba 2023-02-24 thomas got_hash_cmp(enum got_hash_algorithm algo, uint8_t *b1, uint8_t *b2)
169 b16893ba 2023-02-24 thomas {
170 b16893ba 2023-02-24 thomas if (algo == GOT_HASH_SHA1)
171 b16893ba 2023-02-24 thomas return memcmp(b1, b2, SHA1_DIGEST_LENGTH);
172 b16893ba 2023-02-24 thomas else if (algo == GOT_HASH_SHA256)
173 b16893ba 2023-02-24 thomas return memcmp(b1, b2, SHA256_DIGEST_LENGTH);
174 b16893ba 2023-02-24 thomas return -1;
175 b16893ba 2023-02-24 thomas }