2 4123af3c 2023-02-12 op * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 4123af3c 2023-02-12 op * Permission to use, copy, modify, and distribute this software for any
5 4123af3c 2023-02-12 op * purpose with or without fee is hereby granted, provided that the above
6 4123af3c 2023-02-12 op * copyright notice and this permission notice appear in all copies.
8 4123af3c 2023-02-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 4123af3c 2023-02-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 4123af3c 2023-02-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 4123af3c 2023-02-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 4123af3c 2023-02-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 4123af3c 2023-02-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 4123af3c 2023-02-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4123af3c 2023-02-12 op #include <sys/types.h>
18 0be5386d 2023-02-12 op #include <sys/queue.h>
20 4123af3c 2023-02-12 op #include <sha1.h>
21 4123af3c 2023-02-12 op #include <sha2.h>
22 4123af3c 2023-02-12 op #include <errno.h>
23 4123af3c 2023-02-12 op #include <stdio.h>
24 4123af3c 2023-02-12 op #include <stdlib.h>
25 3f81ccbd 2023-02-12 op #include <string.h>
26 4123af3c 2023-02-12 op #include <limits.h>
28 0be5386d 2023-02-12 op #include "got_object.h"
30 4123af3c 2023-02-12 op #include "got_lib_hash.h"
33 4123af3c 2023-02-12 op got_parse_xdigit(uint8_t *val, const char *hex)
39 4123af3c 2023-02-12 op lval = strtol(hex, &ep, 16);
40 4123af3c 2023-02-12 op if (hex[0] == '\0' || *ep != '\0')
42 4123af3c 2023-02-12 op if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
45 4123af3c 2023-02-12 op *val = (uint8_t)lval;
50 4123af3c 2023-02-12 op got_parse_sha1_digest(uint8_t *digest, const char *line)
52 4123af3c 2023-02-12 op uint8_t b = 0;
53 4123af3c 2023-02-12 op char hex[3] = {'\0', '\0', '\0'};
56 4123af3c 2023-02-12 op for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
57 4123af3c 2023-02-12 op if (line[0] == '\0' || line[1] == '\0')
59 4123af3c 2023-02-12 op for (j = 0; j < 2; j++) {
60 4123af3c 2023-02-12 op hex[j] = *line;
63 4123af3c 2023-02-12 op if (!got_parse_xdigit(&b, hex))
65 4123af3c 2023-02-12 op digest[i] = b;
72 4123af3c 2023-02-12 op got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
74 4123af3c 2023-02-12 op char *p = buf;
78 4123af3c 2023-02-12 op if (size < SHA1_DIGEST_STRING_LENGTH)
81 4123af3c 2023-02-12 op for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
82 4123af3c 2023-02-12 op snprintf(hex, sizeof(hex), "%.2x", digest[i]);
83 4123af3c 2023-02-12 op p[0] = hex[0];
84 4123af3c 2023-02-12 op p[1] = hex[1];
93 0be5386d 2023-02-12 op got_parse_sha256_digest(uint8_t *digest, const char *line)
95 0be5386d 2023-02-12 op uint8_t b = 0;
96 0be5386d 2023-02-12 op char hex[3] = {'\0', '\0', '\0'};
99 0be5386d 2023-02-12 op for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
100 0be5386d 2023-02-12 op if (line[0] == '\0' || line[1] == '\0')
102 0be5386d 2023-02-12 op for (j = 0; j < 2; j++) {
103 0be5386d 2023-02-12 op hex[j] = *line;
106 0be5386d 2023-02-12 op if (!got_parse_xdigit(&b, hex))
108 0be5386d 2023-02-12 op digest[i] = b;
115 0be5386d 2023-02-12 op got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
117 0be5386d 2023-02-12 op char *p = buf;
121 0be5386d 2023-02-12 op if (size < SHA256_DIGEST_STRING_LENGTH)
124 0be5386d 2023-02-12 op for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
125 0be5386d 2023-02-12 op snprintf(hex, sizeof(hex), "%.2x", digest[i]);
126 0be5386d 2023-02-12 op p[0] = hex[0];
127 0be5386d 2023-02-12 op p[1] = hex[1];
136 0be5386d 2023-02-12 op got_parse_hash_digest(uint8_t *digest, const char *line,
137 0be5386d 2023-02-12 op enum got_hash_algorithm algo)
139 0be5386d 2023-02-12 op switch (algo) {
140 0be5386d 2023-02-12 op case GOT_HASH_SHA1:
141 0be5386d 2023-02-12 op return got_parse_sha1_digest(digest, line);
142 0be5386d 2023-02-12 op case GOT_HASH_SHA256:
143 0be5386d 2023-02-12 op return got_parse_sha256_digest(digest, line);
150 82f7e4c8 2023-02-12 op got_hash_digest_to_str(const uint8_t *digest, char *buf, size_t size,
151 82f7e4c8 2023-02-12 op enum got_hash_algorithm algo)
153 82f7e4c8 2023-02-12 op switch (algo) {
154 82f7e4c8 2023-02-12 op case GOT_HASH_SHA1:
155 82f7e4c8 2023-02-12 op return got_sha1_digest_to_str(digest, buf, size);
156 82f7e4c8 2023-02-12 op case GOT_HASH_SHA256:
157 82f7e4c8 2023-02-12 op return got_sha256_digest_to_str(digest, buf, size);
164 82f7e4c8 2023-02-12 op got_hash_digest_length(enum got_hash_algorithm algo)
166 82f7e4c8 2023-02-12 op switch (algo) {
167 82f7e4c8 2023-02-12 op case GOT_HASH_SHA1:
168 82f7e4c8 2023-02-12 op return SHA1_DIGEST_LENGTH;
169 82f7e4c8 2023-02-12 op case GOT_HASH_SHA256:
170 82f7e4c8 2023-02-12 op return SHA256_DIGEST_LENGTH;
177 82f7e4c8 2023-02-12 op got_hash_digest_string_length(enum got_hash_algorithm algo)
179 82f7e4c8 2023-02-12 op switch (algo) {
180 82f7e4c8 2023-02-12 op case GOT_HASH_SHA1:
181 82f7e4c8 2023-02-12 op return SHA1_DIGEST_STRING_LENGTH;
182 82f7e4c8 2023-02-12 op case GOT_HASH_SHA256:
183 82f7e4c8 2023-02-12 op return SHA256_DIGEST_STRING_LENGTH;
190 3f81ccbd 2023-02-12 op got_hash_init(struct got_hash *hash, enum got_hash_algorithm algo)
192 3f81ccbd 2023-02-12 op memset(hash, 0, sizeof(*hash));
193 3f81ccbd 2023-02-12 op hash->algo = algo;
195 3f81ccbd 2023-02-12 op if (algo == GOT_HASH_SHA1)
196 3f81ccbd 2023-02-12 op SHA1Init(&hash->sha1_ctx);
197 3f81ccbd 2023-02-12 op else if (algo == GOT_HASH_SHA256)
198 3f81ccbd 2023-02-12 op SHA256Init(&hash->sha256_ctx);
202 3f81ccbd 2023-02-12 op got_hash_update(struct got_hash *hash, const void *data, size_t len)
204 3f81ccbd 2023-02-12 op if (hash->algo == GOT_HASH_SHA1)
205 3f81ccbd 2023-02-12 op SHA1Update(&hash->sha1_ctx, data, len);
206 3f81ccbd 2023-02-12 op else if (hash->algo == GOT_HASH_SHA256)
207 3f81ccbd 2023-02-12 op SHA256Update(&hash->sha256_ctx, data, len);
211 3f81ccbd 2023-02-12 op got_hash_final(struct got_hash *hash, uint8_t *out)
213 3f81ccbd 2023-02-12 op if (hash->algo == GOT_HASH_SHA1)
214 3f81ccbd 2023-02-12 op SHA1Final(out, &hash->sha1_ctx);
215 3f81ccbd 2023-02-12 op else if (hash->algo == GOT_HASH_SHA256)
216 3f81ccbd 2023-02-12 op SHA256Final(out, &hash->sha256_ctx);
220 3f81ccbd 2023-02-12 op got_hash_cmp(struct got_hash *hash, uint8_t *orig, uint8_t *target)
222 3f81ccbd 2023-02-12 op if (hash->algo == GOT_HASH_SHA1)
223 3f81ccbd 2023-02-12 op return memcmp(orig, target, SHA1_DIGEST_LENGTH);
224 3f81ccbd 2023-02-12 op else if (hash->algo == GOT_HASH_SHA256)
225 3f81ccbd 2023-02-12 op return memcmp(orig, target, SHA256_DIGEST_LENGTH);