Blame


1 53bf0b54 2023-02-23 op /*
2 53bf0b54 2023-02-23 op * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 53bf0b54 2023-02-23 op *
4 53bf0b54 2023-02-23 op * Permission to use, copy, modify, and distribute this software for any
5 53bf0b54 2023-02-23 op * purpose with or without fee is hereby granted, provided that the above
6 53bf0b54 2023-02-23 op * copyright notice and this permission notice appear in all copies.
7 53bf0b54 2023-02-23 op *
8 53bf0b54 2023-02-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 53bf0b54 2023-02-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 53bf0b54 2023-02-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 53bf0b54 2023-02-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 53bf0b54 2023-02-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 53bf0b54 2023-02-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 53bf0b54 2023-02-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 53bf0b54 2023-02-23 op */
16 53bf0b54 2023-02-23 op
17 53bf0b54 2023-02-23 op #include <sys/types.h>
18 53bf0b54 2023-02-23 op #include <sha1.h>
19 53bf0b54 2023-02-23 op #include <sha2.h>
20 53bf0b54 2023-02-23 op #include <errno.h>
21 53bf0b54 2023-02-23 op #include <stdio.h>
22 53bf0b54 2023-02-23 op #include <stdlib.h>
23 53bf0b54 2023-02-23 op #include <limits.h>
24 53bf0b54 2023-02-23 op
25 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
26 53bf0b54 2023-02-23 op
27 53bf0b54 2023-02-23 op int
28 53bf0b54 2023-02-23 op got_parse_xdigit(uint8_t *val, const char *hex)
29 53bf0b54 2023-02-23 op {
30 53bf0b54 2023-02-23 op char *ep;
31 53bf0b54 2023-02-23 op long lval;
32 53bf0b54 2023-02-23 op
33 53bf0b54 2023-02-23 op errno = 0;
34 53bf0b54 2023-02-23 op lval = strtol(hex, &ep, 16);
35 53bf0b54 2023-02-23 op if (hex[0] == '\0' || *ep != '\0')
36 53bf0b54 2023-02-23 op return 0;
37 53bf0b54 2023-02-23 op if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
38 53bf0b54 2023-02-23 op return 0;
39 53bf0b54 2023-02-23 op
40 53bf0b54 2023-02-23 op *val = (uint8_t)lval;
41 53bf0b54 2023-02-23 op return 1;
42 53bf0b54 2023-02-23 op }
43 53bf0b54 2023-02-23 op
44 53bf0b54 2023-02-23 op int
45 53bf0b54 2023-02-23 op got_parse_sha1_digest(uint8_t *digest, const char *line)
46 53bf0b54 2023-02-23 op {
47 53bf0b54 2023-02-23 op uint8_t b = 0;
48 53bf0b54 2023-02-23 op char hex[3] = {'\0', '\0', '\0'};
49 53bf0b54 2023-02-23 op int i, j;
50 53bf0b54 2023-02-23 op
51 53bf0b54 2023-02-23 op for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
52 53bf0b54 2023-02-23 op if (line[0] == '\0' || line[1] == '\0')
53 53bf0b54 2023-02-23 op return 0;
54 53bf0b54 2023-02-23 op for (j = 0; j < 2; j++) {
55 53bf0b54 2023-02-23 op hex[j] = *line;
56 53bf0b54 2023-02-23 op line++;
57 53bf0b54 2023-02-23 op }
58 53bf0b54 2023-02-23 op if (!got_parse_xdigit(&b, hex))
59 53bf0b54 2023-02-23 op return 0;
60 53bf0b54 2023-02-23 op digest[i] = b;
61 53bf0b54 2023-02-23 op }
62 53bf0b54 2023-02-23 op
63 53bf0b54 2023-02-23 op return 1;
64 53bf0b54 2023-02-23 op }
65 53bf0b54 2023-02-23 op
66 53bf0b54 2023-02-23 op char *
67 53bf0b54 2023-02-23 op got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
68 53bf0b54 2023-02-23 op {
69 53bf0b54 2023-02-23 op char *p = buf;
70 53bf0b54 2023-02-23 op char hex[3];
71 53bf0b54 2023-02-23 op int i;
72 53bf0b54 2023-02-23 op
73 53bf0b54 2023-02-23 op if (size < SHA1_DIGEST_STRING_LENGTH)
74 53bf0b54 2023-02-23 op return NULL;
75 53bf0b54 2023-02-23 op
76 53bf0b54 2023-02-23 op for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
77 53bf0b54 2023-02-23 op snprintf(hex, sizeof(hex), "%.2x", digest[i]);
78 53bf0b54 2023-02-23 op p[0] = hex[0];
79 53bf0b54 2023-02-23 op p[1] = hex[1];
80 53bf0b54 2023-02-23 op p += 2;
81 53bf0b54 2023-02-23 op }
82 53bf0b54 2023-02-23 op p[0] = '\0';
83 53bf0b54 2023-02-23 op
84 53bf0b54 2023-02-23 op return buf;
85 53bf0b54 2023-02-23 op }