Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sha1.h>
21 #include <sha2.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <limits.h>
27 #include "got_lib_hash.h"
29 #include "got_object.h"
31 int
32 got_parse_xdigit(uint8_t *val, const char *hex)
33 {
34 char *ep;
35 long lval;
37 errno = 0;
38 lval = strtol(hex, &ep, 16);
39 if (hex[0] == '\0' || *ep != '\0')
40 return 0;
41 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
42 return 0;
44 *val = (uint8_t)lval;
45 return 1;
46 }
48 int
49 got_parse_sha1_digest(uint8_t *digest, const char *line)
50 {
51 uint8_t b = 0;
52 char hex[3] = {'\0', '\0', '\0'};
53 int i, j;
55 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
56 if (line[0] == '\0' || line[1] == '\0')
57 return 0;
58 for (j = 0; j < 2; j++) {
59 hex[j] = *line;
60 line++;
61 }
62 if (!got_parse_xdigit(&b, hex))
63 return 0;
64 digest[i] = b;
65 }
67 return 1;
68 }
70 char *
71 got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
72 {
73 char *p = buf;
74 char hex[3];
75 int i;
77 if (size < SHA1_DIGEST_STRING_LENGTH)
78 return NULL;
80 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
81 snprintf(hex, sizeof(hex), "%.2x", digest[i]);
82 p[0] = hex[0];
83 p[1] = hex[1];
84 p += 2;
85 }
86 p[0] = '\0';
88 return buf;
89 }
91 int
92 got_parse_sha256_digest(uint8_t *digest, const char *line)
93 {
94 uint8_t b = 0;
95 char hex[3] = {'\0', '\0', '\0'};
96 int i, j;
98 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
99 if (line[0] == '\0' || line[1] == '\0')
100 return 0;
101 for (j = 0; j < 2; j++) {
102 hex[j] = *line;
103 line++;
105 if (!got_parse_xdigit(&b, hex))
106 return 0;
107 digest[i] = b;
110 return 1;
113 char *
114 got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
116 char *p = buf;
117 char hex[3];
118 int i;
120 if (size < SHA256_DIGEST_STRING_LENGTH)
121 return NULL;
123 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
124 snprintf(hex, sizeof(hex), "%.2x", digest[i]);
125 p[0] = hex[0];
126 p[1] = hex[1];
127 p += 2;
129 p[0] = '\0';
131 return buf;
134 int
135 got_parse_hash_digest(uint8_t *digest, const char *line, int algo, size_t *len)
137 if (algo == GOT_HASH_SHA256) {
138 if (len)
139 *len = SHA256_DIGEST_STRING_LENGTH;
140 return got_parse_sha256_digest(digest, line);
141 } else if (algo == GOT_HASH_SHA1) {
142 if (len)
143 *len = SHA1_DIGEST_STRING_LENGTH;
144 return got_parse_sha1_digest(digest, line);
147 return 0;