2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
42 #include "hast_checksum.h"
44 #define MAX_HASH_SIZE SHA256_DIGEST_LENGTH
47 hast_crc32_checksum(const unsigned char *data, size_t size,
48 unsigned char *hash, size_t *hsizep)
52 crc = crc32(0L, Z_NULL, 0);
53 crc = crc32(crc, data, size);
55 /* XXXPJD: Do we have to use htole32() on crc first? */
56 bcopy(&crc, hash, sizeof(crc));
57 *hsizep = sizeof(crc);
61 hast_sha256_checksum(const unsigned char *data, size_t size,
62 unsigned char *hash, size_t *hsizep)
67 SHA256_Update(&ctx, data, size);
68 SHA256_Final(hash, &ctx);
69 *hsizep = SHA256_DIGEST_LENGTH;
73 checksum_name(int num)
77 case HAST_CHECKSUM_NONE:
79 case HAST_CHECKSUM_CRC32:
81 case HAST_CHECKSUM_SHA256:
88 checksum_send(const struct hast_resource *res, struct nv *nv, void **datap,
89 size_t *sizep, bool *freedatap __unused)
91 unsigned char hash[MAX_HASH_SIZE];
94 switch (res->hr_checksum) {
95 case HAST_CHECKSUM_NONE:
97 case HAST_CHECKSUM_CRC32:
98 hast_crc32_checksum(*datap, *sizep, hash, &hsize);
100 case HAST_CHECKSUM_SHA256:
101 hast_sha256_checksum(*datap, *sizep, hash, &hsize);
104 PJDLOG_ABORT("Invalid checksum: %d.", res->hr_checksum);
106 nv_add_string(nv, checksum_name(res->hr_checksum), "checksum");
107 nv_add_uint8_array(nv, hash, hsize, "hash");
108 if (nv_error(nv) != 0) {
109 errno = nv_error(nv);
116 checksum_recv(const struct hast_resource *res __unused, struct nv *nv,
117 void **datap, size_t *sizep, bool *freedatap __unused)
119 unsigned char chash[MAX_HASH_SIZE];
120 const unsigned char *rhash;
121 size_t chsize, rhsize;
124 algo = nv_get_string(nv, "checksum");
126 return (0); /* No checksum. */
127 rhash = nv_get_uint8_array(nv, &rhsize, "hash");
129 pjdlog_error("Hash is missing.");
130 return (-1); /* Hash not found. */
132 if (strcmp(algo, "crc32") == 0)
133 hast_crc32_checksum(*datap, *sizep, chash, &chsize);
134 else if (strcmp(algo, "sha256") == 0)
135 hast_sha256_checksum(*datap, *sizep, chash, &chsize);
137 pjdlog_error("Unknown checksum algorithm '%s'.", algo);
138 return (-1); /* Unknown checksum algorithm. */
140 if (rhsize != chsize) {
141 pjdlog_error("Invalid hash size (%zu) for %s, should be %zu.",
142 rhsize, algo, chsize);
143 return (-1); /* Different hash size. */
145 if (bcmp(rhash, chash, chsize) != 0) {
146 pjdlog_error("Hash mismatch.");
147 return (-1); /* Hash mismatch. */