2 1362b0e3 2023-02-04 op * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 1362b0e3 2023-02-04 op * Permission to use, copy, modify, and distribute this software for any
5 1362b0e3 2023-02-04 op * purpose with or without fee is hereby granted, provided that the above
6 1362b0e3 2023-02-04 op * copyright notice and this permission notice appear in all copies.
8 1362b0e3 2023-02-04 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1362b0e3 2023-02-04 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1362b0e3 2023-02-04 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1362b0e3 2023-02-04 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1362b0e3 2023-02-04 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1362b0e3 2023-02-04 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1362b0e3 2023-02-04 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1362b0e3 2023-02-04 op #include <sys/types.h>
18 87a4b05f 2023-02-04 op #include <sys/queue.h>
20 1362b0e3 2023-02-04 op #include <sha1.h>
21 1362b0e3 2023-02-04 op #include <sha2.h>
22 1362b0e3 2023-02-04 op #include <errno.h>
23 1362b0e3 2023-02-04 op #include <stdio.h>
24 1362b0e3 2023-02-04 op #include <stdlib.h>
25 1362b0e3 2023-02-04 op #include <limits.h>
27 1362b0e3 2023-02-04 op #include "got_lib_hash.h"
29 87a4b05f 2023-02-04 op #include "got_object.h"
32 1362b0e3 2023-02-04 op got_parse_xdigit(uint8_t *val, const char *hex)
38 1362b0e3 2023-02-04 op lval = strtol(hex, &ep, 16);
39 1362b0e3 2023-02-04 op if (hex[0] == '\0' || *ep != '\0')
41 1362b0e3 2023-02-04 op if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
44 1362b0e3 2023-02-04 op *val = (uint8_t)lval;
49 1362b0e3 2023-02-04 op got_parse_sha1_digest(uint8_t *digest, const char *line)
51 1362b0e3 2023-02-04 op uint8_t b = 0;
52 1362b0e3 2023-02-04 op char hex[3] = {'\0', '\0', '\0'};
55 1362b0e3 2023-02-04 op for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
56 1362b0e3 2023-02-04 op if (line[0] == '\0' || line[1] == '\0')
58 1362b0e3 2023-02-04 op for (j = 0; j < 2; j++) {
59 1362b0e3 2023-02-04 op hex[j] = *line;
62 1362b0e3 2023-02-04 op if (!got_parse_xdigit(&b, hex))
64 1362b0e3 2023-02-04 op digest[i] = b;
71 1362b0e3 2023-02-04 op got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
73 1362b0e3 2023-02-04 op char *p = buf;
77 1362b0e3 2023-02-04 op if (size < SHA1_DIGEST_STRING_LENGTH)
80 1362b0e3 2023-02-04 op for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
81 1362b0e3 2023-02-04 op snprintf(hex, sizeof(hex), "%.2x", digest[i]);
82 1362b0e3 2023-02-04 op p[0] = hex[0];
83 1362b0e3 2023-02-04 op p[1] = hex[1];
92 1362b0e3 2023-02-04 op got_parse_sha256_digest(uint8_t *digest, const char *line)
94 1362b0e3 2023-02-04 op uint8_t b = 0;
95 1362b0e3 2023-02-04 op char hex[3] = {'\0', '\0', '\0'};
98 1362b0e3 2023-02-04 op for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
99 1362b0e3 2023-02-04 op if (line[0] == '\0' || line[1] == '\0')
101 1362b0e3 2023-02-04 op for (j = 0; j < 2; j++) {
102 1362b0e3 2023-02-04 op hex[j] = *line;
105 1362b0e3 2023-02-04 op if (!got_parse_xdigit(&b, hex))
107 1362b0e3 2023-02-04 op digest[i] = b;
114 1362b0e3 2023-02-04 op got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
116 1362b0e3 2023-02-04 op char *p = buf;
120 1362b0e3 2023-02-04 op if (size < SHA256_DIGEST_STRING_LENGTH)
123 1362b0e3 2023-02-04 op for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
124 1362b0e3 2023-02-04 op snprintf(hex, sizeof(hex), "%.2x", digest[i]);
125 1362b0e3 2023-02-04 op p[0] = hex[0];
126 1362b0e3 2023-02-04 op p[1] = hex[1];
135 87a4b05f 2023-02-04 op got_parse_hash_digest(uint8_t *digest, const char *line, int algo, size_t *len)
137 87a4b05f 2023-02-04 op if (algo == GOT_HASH_SHA256) {
139 87a4b05f 2023-02-04 op *len = SHA256_DIGEST_STRING_LENGTH;
140 87a4b05f 2023-02-04 op return got_parse_sha256_digest(digest, line);
141 87a4b05f 2023-02-04 op } else if (algo == GOT_HASH_SHA1) {
143 87a4b05f 2023-02-04 op *len = SHA1_DIGEST_STRING_LENGTH;
144 87a4b05f 2023-02-04 op return got_parse_sha1_digest(digest, line);