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 static int
50 parse_digest(uint8_t *digest, int len, 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 < len; 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 static char *
72 digest_to_str(const uint8_t *digest, int len, char *buf)
73 {
74 char *p = buf;
75 char hex[3];
76 int i;
78 for (i = 0; i < len; i++) {
79 snprintf(hex, sizeof(hex), "%.2x", digest[i]);
80 p[0] = hex[0];
81 p[1] = hex[1];
82 p += 2;
83 }
84 p[0] = '\0';
86 return buf;
87 }
89 char *
90 got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
91 {
92 if (size < SHA1_DIGEST_STRING_LENGTH)
93 return NULL;
94 return digest_to_str(digest, SHA1_DIGEST_LENGTH, buf);
95 }
97 char *
98 got_sha256_digest_to_str(const uint8_t *digest, char *buf, size_t size)
99 {
100 if (size < SHA256_DIGEST_STRING_LENGTH)
101 return NULL;
102 return digest_to_str(digest, SHA256_DIGEST_LENGTH, buf);
105 int
106 got_parse_hash_digest(uint8_t *digest, const char *line,
107 enum got_hash_algorithm algo)
109 switch (algo) {
110 case GOT_HASH_SHA1:
111 return parse_digest(digest, SHA1_DIGEST_LENGTH, line);
112 case GOT_HASH_SHA256:
113 return parse_digest(digest, SHA256_DIGEST_LENGTH, line);
114 default:
115 return 0;
119 int
120 got_parse_object_id(struct got_object_id *id, const char *line,
121 enum got_hash_algorithm algo)
123 memset(id, 0, sizeof(*id));
125 /* XXX: temporary until we grow got_object_id */
126 if (algo != GOT_HASH_SHA1)
127 return 0;
129 return got_parse_hash_digest(id->sha1, line, algo);
132 void
133 got_hash_init(struct got_hash *hash, enum got_hash_algorithm algo)
135 memset(hash, 0, sizeof(*hash));
136 hash->algo = algo;
138 if (algo == GOT_HASH_SHA1)
139 SHA1Init(&hash->sha1_ctx);
140 else if (algo == GOT_HASH_SHA256)
141 SHA256Init(&hash->sha256_ctx);
144 void
145 got_hash_update(struct got_hash *hash, const void *data, size_t len)
147 if (hash->algo == GOT_HASH_SHA1)
148 SHA1Update(&hash->sha1_ctx, data, len);
149 else if (hash->algo == GOT_HASH_SHA256)
150 SHA256Update(&hash->sha256_ctx, data, len);
153 void
154 got_hash_final(struct got_hash *hash, uint8_t *out)
156 if (hash->algo == GOT_HASH_SHA1)
157 SHA1Final(out, &hash->sha1_ctx);
158 else if (hash->algo == GOT_HASH_SHA256)
159 SHA256Final(out, &hash->sha256_ctx);
162 void
163 got_hash_final_object_id(struct got_hash *hash, struct got_object_id *id)
165 memset(id, 0, sizeof(*id));
166 got_hash_final(hash, id->sha1);
169 int
170 got_hash_cmp(enum got_hash_algorithm algo, uint8_t *b1, uint8_t *b2)
172 if (algo == GOT_HASH_SHA1)
173 return memcmp(b1, b2, SHA1_DIGEST_LENGTH);
174 else if (algo == GOT_HASH_SHA256)
175 return memcmp(b1, b2, SHA256_DIGEST_LENGTH);
176 return -1;