]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sbin/hastd/hast_checksum.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sbin / hastd / hast_checksum.c
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 #ifdef HAVE_CRYPTO
35 #include <openssl/sha.h>
36 #endif
37
38 #include <crc32.h>
39 #include <hast.h>
40 #include <nv.h>
41 #include <pjdlog.h>
42
43 #include "hast_checksum.h"
44
45 #ifdef HAVE_CRYPTO
46 #define MAX_HASH_SIZE   SHA256_DIGEST_LENGTH
47 #else
48 #define MAX_HASH_SIZE   4
49 #endif
50
51 static int
52 hast_crc32_checksum(const unsigned char *data, size_t size,
53     unsigned char *hash, size_t *hsizep)
54 {
55         uint32_t crc;
56
57         crc = crc32(data, size);
58         /* XXXPJD: Do we have to use htole32() on crc first? */
59         bcopy(&crc, hash, sizeof(crc));
60         *hsizep = sizeof(crc);
61
62         return (0);
63 }
64
65 #ifdef HAVE_CRYPTO
66 static int
67 hast_sha256_checksum(const unsigned char *data, size_t size,
68     unsigned char *hash, size_t *hsizep)
69 {
70         SHA256_CTX ctx;
71
72         SHA256_Init(&ctx);
73         SHA256_Update(&ctx, data, size);
74         SHA256_Final(hash, &ctx);
75         *hsizep = SHA256_DIGEST_LENGTH;
76
77         return (0);
78 }
79 #endif  /* HAVE_CRYPTO */
80
81 const char *
82 checksum_name(int num)
83 {
84
85         switch (num) {
86         case HAST_CHECKSUM_NONE:
87                 return ("none");
88         case HAST_CHECKSUM_CRC32:
89                 return ("crc32");
90         case HAST_CHECKSUM_SHA256:
91                 return ("sha256");
92         }
93         return ("unknown");
94 }
95
96 int
97 checksum_send(const struct hast_resource *res, struct nv *nv, void **datap,
98     size_t *sizep, bool *freedatap __unused)
99 {
100         unsigned char hash[MAX_HASH_SIZE];
101         size_t hsize;
102         int ret;
103
104         switch (res->hr_checksum) {
105         case HAST_CHECKSUM_NONE:
106                 return (0);
107         case HAST_CHECKSUM_CRC32:
108                 ret = hast_crc32_checksum(*datap, *sizep, hash, &hsize);
109                 break;
110 #ifdef HAVE_CRYPTO
111         case HAST_CHECKSUM_SHA256:
112                 ret = hast_sha256_checksum(*datap, *sizep, hash, &hsize);
113                 break;
114 #endif
115         default:
116                 PJDLOG_ABORT("Invalid checksum: %d.", res->hr_checksum);
117         }
118
119         if (ret != 0)
120                 return (ret);
121         nv_add_string(nv, checksum_name(res->hr_checksum), "checksum");
122         nv_add_uint8_array(nv, hash, hsize, "hash");
123         if (nv_error(nv) != 0) {
124                 errno = nv_error(nv);
125                 return (-1);
126         }
127         return (0);
128 }
129
130 int
131 checksum_recv(const struct hast_resource *res __unused, struct nv *nv,
132     void **datap, size_t *sizep, bool *freedatap __unused)
133 {
134         unsigned char chash[MAX_HASH_SIZE];
135         const unsigned char *rhash;
136         size_t chsize, rhsize;
137         const char *algo;
138         int ret;
139
140         algo = nv_get_string(nv, "checksum");
141         if (algo == NULL)
142                 return (0);     /* No checksum. */
143         rhash = nv_get_uint8_array(nv, &rhsize, "hash");
144         if (rhash == NULL) {
145                 pjdlog_error("Hash is missing.");
146                 return (-1);    /* Hash not found. */
147         }
148         if (strcmp(algo, "crc32") == 0)
149                 ret = hast_crc32_checksum(*datap, *sizep, chash, &chsize);
150 #ifdef HAVE_CRYPTO
151         else if (strcmp(algo, "sha256") == 0)
152                 ret = hast_sha256_checksum(*datap, *sizep, chash, &chsize);
153 #endif
154         else {
155                 pjdlog_error("Unknown checksum algorithm '%s'.", algo);
156                 return (-1);    /* Unknown checksum algorithm. */
157         }
158         if (rhsize != chsize) {
159                 pjdlog_error("Invalid hash size (%zu) for %s, should be %zu.",
160                     rhsize, algo, chsize);
161                 return (-1);    /* Different hash size. */
162         }
163         if (bcmp(rhash, chash, chsize) != 0) {
164                 pjdlog_error("Hash mismatch.");
165                 return (-1);    /* Hash mismatch. */
166         }
167
168         return (0);
169 }