1 /*- 2 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <errno.h> 31 #include <string.h> 32 #include <strings.h> 33 34 #include <crc32.h> 35 #include <hast.h> 36 #include <nv.h> 37 #include <sha256.h> 38 #include <pjdlog.h> 39 40 #include "hast_checksum.h" 41 42 #define MAX_HASH_SIZE SHA256_DIGEST_LENGTH 43 44 static void 45 hast_crc32_checksum(const unsigned char *data, size_t size, 46 unsigned char *hash, size_t *hsizep) 47 { 48 uint32_t crc; 49 50 crc = crc32(data, size); 51 /* XXXPJD: Do we have to use htole32() on crc first? */ 52 bcopy(&crc, hash, sizeof(crc)); 53 *hsizep = sizeof(crc); 54 } 55 56 static void 57 hast_sha256_checksum(const unsigned char *data, size_t size, 58 unsigned char *hash, size_t *hsizep) 59 { 60 SHA256_CTX ctx; 61 62 SHA256_Init(&ctx); 63 SHA256_Update(&ctx, data, size); 64 SHA256_Final(hash, &ctx); 65 *hsizep = SHA256_DIGEST_LENGTH; 66 } 67 68 const char * 69 checksum_name(int num) 70 { 71 72 switch (num) { 73 case HAST_CHECKSUM_NONE: 74 return ("none"); 75 case HAST_CHECKSUM_CRC32: 76 return ("crc32"); 77 case HAST_CHECKSUM_SHA256: 78 return ("sha256"); 79 } 80 return ("unknown"); 81 } 82 83 int 84 checksum_send(const struct hast_resource *res, struct nv *nv, void **datap, 85 size_t *sizep, bool *freedatap __unused) 86 { 87 unsigned char hash[MAX_HASH_SIZE]; 88 size_t hsize; 89 90 switch (res->hr_checksum) { 91 case HAST_CHECKSUM_NONE: 92 return (0); 93 case HAST_CHECKSUM_CRC32: 94 hast_crc32_checksum(*datap, *sizep, hash, &hsize); 95 break; 96 case HAST_CHECKSUM_SHA256: 97 hast_sha256_checksum(*datap, *sizep, hash, &hsize); 98 break; 99 default: 100 PJDLOG_ABORT("Invalid checksum: %d.", res->hr_checksum); 101 } 102 nv_add_string(nv, checksum_name(res->hr_checksum), "checksum"); 103 nv_add_uint8_array(nv, hash, hsize, "hash"); 104 if (nv_error(nv) != 0) { 105 errno = nv_error(nv); 106 return (-1); 107 } 108 return (0); 109 } 110 111 int 112 checksum_recv(const struct hast_resource *res __unused, struct nv *nv, 113 void **datap, size_t *sizep, bool *freedatap __unused) 114 { 115 unsigned char chash[MAX_HASH_SIZE]; 116 const unsigned char *rhash; 117 size_t chsize, rhsize; 118 const char *algo; 119 120 algo = nv_get_string(nv, "checksum"); 121 if (algo == NULL) 122 return (0); /* No checksum. */ 123 rhash = nv_get_uint8_array(nv, &rhsize, "hash"); 124 if (rhash == NULL) { 125 pjdlog_error("Hash is missing."); 126 return (-1); /* Hash not found. */ 127 } 128 if (strcmp(algo, "crc32") == 0) 129 hast_crc32_checksum(*datap, *sizep, chash, &chsize); 130 else if (strcmp(algo, "sha256") == 0) 131 hast_sha256_checksum(*datap, *sizep, chash, &chsize); 132 else { 133 pjdlog_error("Unknown checksum algorithm '%s'.", algo); 134 return (-1); /* Unknown checksum algorithm. */ 135 } 136 if (rhsize != chsize) { 137 pjdlog_error("Invalid hash size (%zu) for %s, should be %zu.", 138 rhsize, algo, chsize); 139 return (-1); /* Different hash size. */ 140 } 141 if (bcmp(rhash, chash, chsize) != 0) { 142 pjdlog_error("Hash mismatch."); 143 return (-1); /* Hash mismatch. */ 144 } 145 146 return (0); 147 } 148