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 <string.h>
26 #include <limits.h>
28 #include "got_object.h"
30 #include "got_lib_hash.h"
32 int
33 got_parse_xdigit(uint8_t *val, const char *hex)
34 {
35 char *ep;
36 long lval;
38 errno = 0;
39 lval = strtol(hex, &ep, 16);
40 if (hex[0] == '\0' || *ep != '\0')
41 return 0;
42 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
43 return 0;
45 *val = (uint8_t)lval;
46 return 1;
47 }
49 int
50 got_parse_sha1_digest(uint8_t *digest, const char *line)
51 {
52 uint8_t b = 0;
53 char hex[3] = {'\0', '\0', '\0'};
54 int i, j;
56 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
57 if (line[0] == '\0' || line[1] == '\0')
58 return 0;
59 for (j = 0; j < 2; j++) {
60 hex[j] = *line;
61 line++;
62 }
63 if (!got_parse_xdigit(&b, hex))
64 return 0;
65 digest[i] = b;
66 }
68 return 1;
69 }
71 char *
72 got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
73 {
74 char *p = buf;
75 char hex[3];
76 int i;
78 if (size < SHA1_DIGEST_STRING_LENGTH)
79 return NULL;
81 for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
82 snprintf(hex, sizeof(hex), "%.2x", digest[i]);
83 p[0] = hex[0];
84 p[1] = hex[1];
85 p += 2;
86 }
87 p[0] = '\0';
89 return buf;
90 }
92 int
93 got_parse_sha256_digest(uint8_t *digest, const char *line)
94 {
95 uint8_t b = 0;
96 char hex[3] = {'\0', '\0', '\0'};
97 int i, j;
99 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
100 if (line[0] == '\0' || line[1] == '\0')
101 return 0;
102 for (j = 0; j < 2; j++) {
103 hex[j] = *line;
104 line++;
106 if (!got_parse_xdigit(&b, hex))
107 return 0;
108 digest[i] = b;
111 return 1;
114 char *
115 got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
117 char *p = buf;
118 char hex[3];
119 int i;
121 if (size < SHA256_DIGEST_STRING_LENGTH)
122 return NULL;
124 for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
125 snprintf(hex, sizeof(hex), "%.2x", digest[i]);
126 p[0] = hex[0];
127 p[1] = hex[1];
128 p += 2;
130 p[0] = '\0';
132 return buf;
135 int
136 got_parse_hash_digest(uint8_t *digest, const char *line,
137 enum got_hash_algorithm algo)
139 switch (algo) {
140 case GOT_HASH_SHA1:
141 return got_parse_sha1_digest(digest, line);
142 case GOT_HASH_SHA256:
143 return got_parse_sha256_digest(digest, line);
144 default:
145 return 0;
149 char *
150 got_hash_digest_to_str(const uint8_t *digest, char *buf, size_t size,
151 enum got_hash_algorithm algo)
153 switch (algo) {
154 case GOT_HASH_SHA1:
155 return got_sha1_digest_to_str(digest, buf, size);
156 case GOT_HASH_SHA256:
157 return got_sha256_digest_to_str(digest, buf, size);
158 default:
159 return NULL;
163 size_t
164 got_hash_digest_length(enum got_hash_algorithm algo)
166 switch (algo) {
167 case GOT_HASH_SHA1:
168 return SHA1_DIGEST_LENGTH;
169 case GOT_HASH_SHA256:
170 return SHA256_DIGEST_LENGTH;
171 default:
172 return 0;
176 size_t
177 got_hash_digest_string_length(enum got_hash_algorithm algo)
179 switch (algo) {
180 case GOT_HASH_SHA1:
181 return SHA1_DIGEST_STRING_LENGTH;
182 case GOT_HASH_SHA256:
183 return SHA256_DIGEST_STRING_LENGTH;
184 default:
185 return 0;
189 void
190 got_hash_init(struct got_hash *hash, enum got_hash_algorithm algo)
192 memset(hash, 0, sizeof(*hash));
193 hash->algo = algo;
195 if (algo == GOT_HASH_SHA1)
196 SHA1Init(&hash->sha1_ctx);
197 else if (algo == GOT_HASH_SHA256)
198 SHA256Init(&hash->sha256_ctx);
201 void
202 got_hash_update(struct got_hash *hash, const void *data, size_t len)
204 if (hash->algo == GOT_HASH_SHA1)
205 SHA1Update(&hash->sha1_ctx, data, len);
206 else if (hash->algo == GOT_HASH_SHA256)
207 SHA256Update(&hash->sha256_ctx, data, len);
210 void
211 got_hash_final(struct got_hash *hash, uint8_t *out)
213 if (hash->algo == GOT_HASH_SHA1)
214 SHA1Final(out, &hash->sha1_ctx);
215 else if (hash->algo == GOT_HASH_SHA256)
216 SHA256Final(out, &hash->sha256_ctx);
219 int
220 got_hash_cmp(struct got_hash *hash, uint8_t *orig, uint8_t *target)
222 if (hash->algo == GOT_HASH_SHA1)
223 return memcmp(orig, target, SHA1_DIGEST_LENGTH);
224 else if (hash->algo == GOT_HASH_SHA256)
225 return memcmp(orig, target, SHA256_DIGEST_LENGTH);
226 return -1;