1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/*
* Embedded Linux library
* Copyright (C) 2011-2014 Intel Corporation
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include "useful.h"
#include "checksum.h"
#include "private.h"
#ifndef HAVE_LINUX_IF_ALG_H
#ifndef HAVE_LINUX_TYPES_H
typedef uint8_t __u8;
typedef uint16_t __u16;
typedef uint32_t __u32;
#else
#include <linux/types.h>
#endif
#ifndef AF_ALG
#define AF_ALG 38
#define PF_ALG AF_ALG
#endif
struct sockaddr_alg {
__u16 salg_family;
__u8 salg_type[14];
__u32 salg_feat;
__u32 salg_mask;
__u8 salg_name[64];
};
/* Socket options */
#define ALG_SET_KEY 1
#else
#include <linux/if_alg.h>
#endif
#ifndef SOL_ALG
#define SOL_ALG 279
#endif
struct checksum_info {
const char *name;
uint8_t digest_len;
bool supported;
};
static struct checksum_info checksum_algs[] = {
[L_CHECKSUM_MD4] = { .name = "md4", .digest_len = 16 },
[L_CHECKSUM_MD5] = { .name = "md5", .digest_len = 16 },
[L_CHECKSUM_SHA1] = { .name = "sha1", .digest_len = 20 },
[L_CHECKSUM_SHA224] = { .name = "sha224", .digest_len = 28 },
[L_CHECKSUM_SHA256] = { .name = "sha256", .digest_len = 32 },
[L_CHECKSUM_SHA384] = { .name = "sha384", .digest_len = 48 },
[L_CHECKSUM_SHA512] = { .name = "sha512", .digest_len = 64 },
[L_CHECKSUM_SHA3_224] = { .name = "sha3-224", .digest_len = 28 },
[L_CHECKSUM_SHA3_256] = { .name = "sha3-256", .digest_len = 32 },
[L_CHECKSUM_SHA3_384] = { .name = "sha3-384", .digest_len = 48 },
[L_CHECKSUM_SHA3_512] = { .name = "sha3-512", .digest_len = 64 },
};
static struct checksum_info checksum_cmac_aes_alg =
{ .name = "cmac(aes)", .digest_len = 16 };
static struct checksum_info checksum_hmac_algs[] = {
[L_CHECKSUM_MD4] = { .name = "hmac(md4)", .digest_len = 16 },
[L_CHECKSUM_MD5] = { .name = "hmac(md5)", .digest_len = 16 },
[L_CHECKSUM_SHA1] = { .name = "hmac(sha1)", .digest_len = 20 },
[L_CHECKSUM_SHA224] = { .name = "hmac(sha224)", .digest_len = 28 },
[L_CHECKSUM_SHA256] = { .name = "hmac(sha256)", .digest_len = 32 },
[L_CHECKSUM_SHA384] = { .name = "hmac(sha384)", .digest_len = 48 },
[L_CHECKSUM_SHA512] = { .name = "hmac(sha512)", .digest_len = 64 },
[L_CHECKSUM_SHA3_224] = { .name = "hmac(sha3-224)", .digest_len = 28 },
[L_CHECKSUM_SHA3_256] = { .name = "hmac(sha3-256)", .digest_len = 32 },
[L_CHECKSUM_SHA3_384] = { .name = "hmac(sha3-384)", .digest_len = 48 },
[L_CHECKSUM_SHA3_512] = { .name = "hmac(sha3-512)", .digest_len = 64 },
};
static const struct {
struct checksum_info *list;
size_t n;
} checksum_info_table[] = {
{ checksum_algs, L_ARRAY_SIZE(checksum_algs) },
{ &checksum_cmac_aes_alg, 1 },
{ checksum_hmac_algs, L_ARRAY_SIZE(checksum_hmac_algs) },
{}
};
/**
* SECTION:checksum
* @short_description: Checksum handling
*
* Checksum handling
*/
#define is_valid_index(array, i) ((i) >= 0 && (i) < L_ARRAY_SIZE(array))
/**
* l_checksum:
*
* Opaque object representing the checksum.
*/
struct l_checksum {
int sk;
const struct checksum_info *alg_info;
};
static int create_alg(const char *alg)
{
struct sockaddr_alg salg;
int sk;
sk = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
if (sk < 0)
return -1;
memset(&salg, 0, sizeof(salg));
salg.salg_family = AF_ALG;
strcpy((char *) salg.salg_type, "hash");
strcpy((char *) salg.salg_name, alg);
if (bind(sk, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
close(sk);
return -1;
}
return sk;
}
static struct l_checksum *checksum_new_common(const char *alg, int sockopt,
const void *data, size_t len,
struct checksum_info *info)
{
struct l_checksum *checksum;
int fd;
fd = create_alg(alg);
if (fd < 0)
return NULL;
if (data) {
if (setsockopt(fd, SOL_ALG, sockopt, data, len) < 0) {
close(fd);
return NULL;
}
}
checksum = l_new(struct l_checksum, 1);
checksum->sk = accept4(fd, NULL, 0, SOCK_CLOEXEC);
close(fd);
if (checksum->sk < 0) {
l_free(checksum);
return NULL;
}
checksum->alg_info = info;
return checksum;
}
/**
* l_checksum_new:
* @type: checksum type
*
* Creates new #l_checksum, using the checksum algorithm @type.
*
* Returns: a newly allocated #l_checksum object.
**/
LIB_EXPORT struct l_checksum *l_checksum_new(enum l_checksum_type type)
{
if (!is_valid_index(checksum_algs, type) || !checksum_algs[type].name)
return NULL;
return checksum_new_common(checksum_algs[type].name, 0, NULL, 0,
&checksum_algs[type]);
}
LIB_EXPORT struct l_checksum *l_checksum_new_cmac_aes(const void *key,
size_t key_len)
{
return checksum_new_common("cmac(aes)", ALG_SET_KEY, key, key_len,
&checksum_cmac_aes_alg);
}
LIB_EXPORT struct l_checksum *l_checksum_new_hmac(enum l_checksum_type type,
const void *key, size_t key_len)
{
if (!is_valid_index(checksum_hmac_algs, type) ||
!checksum_hmac_algs[type].name)
return NULL;
return checksum_new_common(checksum_hmac_algs[type].name,
ALG_SET_KEY, key, key_len,
&checksum_hmac_algs[type]);
}
/**
* l_checksum_clone:
* @checksum: parent checksum object
*
* Creates a new checksum with an independent copy of parent @checksum's
* state. l_checksum_get_digest can then be called on the parent or the
* clone without affecting the state of the other object.
**/
LIB_EXPORT struct l_checksum *l_checksum_clone(struct l_checksum *checksum)
{
struct l_checksum *clone;
if (unlikely(!checksum))
return NULL;
clone = l_new(struct l_checksum, 1);
clone->sk = accept4(checksum->sk, NULL, 0, SOCK_CLOEXEC);
if (clone->sk < 0) {
l_free(clone);
return NULL;
}
clone->alg_info = checksum->alg_info;
return clone;
}
/**
* l_checksum_free:
* @checksum: checksum object
*
* Frees the memory allocated for @checksum.
**/
LIB_EXPORT void l_checksum_free(struct l_checksum *checksum)
{
if (unlikely(!checksum))
return;
close(checksum->sk);
l_free(checksum);
}
/**
* l_checksum_reset:
* @checksum: checksum object
*
* Resets the internal state of @checksum.
**/
LIB_EXPORT void l_checksum_reset(struct l_checksum *checksum)
{
if (unlikely(!checksum))
return;
send(checksum->sk, NULL, 0, 0);
}
/**
* l_checksum_update:
* @checksum: checksum object
* @data: data pointer
* @len: length of data
*
* Updates checksum from @data pointer with @len bytes.
*
* Returns: true if the operation succeeded, false otherwise.
**/
LIB_EXPORT bool l_checksum_update(struct l_checksum *checksum,
const void *data, size_t len)
{
ssize_t written;
if (unlikely(!checksum))
return false;
written = send(checksum->sk, data, len, MSG_MORE);
if (written < 0)
return false;
return true;
}
/**
* l_checksum_updatev:
* @checksum: checksum object
* @iov: iovec pointer
* @iov_len: Number of iovec entries
*
* This is a iovec based version of l_checksum_update; it updates the checksum
* based on contents of @iov and @iov_len.
*
* Returns: true if the operation succeeded, false otherwise.
**/
LIB_EXPORT bool l_checksum_updatev(struct l_checksum *checksum,
const struct iovec *iov, size_t iov_len)
{
struct msghdr msg;
ssize_t written;
if (unlikely(!checksum))
return false;
if (unlikely(!iov) || unlikely(!iov_len))
return false;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = (struct iovec *) iov;
msg.msg_iovlen = iov_len;
written = sendmsg(checksum->sk, &msg, MSG_MORE);
if (written < 0)
return false;
return true;
}
/**
* l_checksum_get_digest:
* @checksum: checksum object
* @digest: output data buffer
* @len: length of output buffer
*
* Writes the digest from @checksum as raw binary data into the provided
* buffer or, if the buffer is shorter, the initial @len bytes of the digest
* data.
*
* Returns: Number of bytes written, or negative value if an error occurred.
**/
LIB_EXPORT ssize_t l_checksum_get_digest(struct l_checksum *checksum,
void *digest, size_t len)
{
ssize_t result;
if (unlikely(!checksum))
return -EINVAL;
if (unlikely(!digest))
return -EFAULT;
if (unlikely(!len))
return -EINVAL;
result = recv(checksum->sk, digest, len, 0);
if (result < 0)
return -errno;
if ((size_t) result < len && result < checksum->alg_info->digest_len)
return -EIO;
return result;
}
/**
* l_checksum_get_string:
* @checksum: checksum object
*
* Gets the digest from @checksum as hex encoded string.
*
* Returns: a newly allocated hex string
**/
LIB_EXPORT char *l_checksum_get_string(struct l_checksum *checksum)
{
unsigned char digest[64];
if (unlikely(!checksum))
return NULL;
l_checksum_get_digest(checksum, digest, sizeof(digest));
return l_util_hexstring(digest, checksum->alg_info->digest_len);
}
static void init_supported()
{
static bool initialized = false;
struct sockaddr_alg salg;
int sk;
unsigned int i, j;
if (likely(initialized))
return;
initialized = true;
sk = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
if (sk < 0)
return;
memset(&salg, 0, sizeof(salg));
salg.salg_family = AF_ALG;
strcpy((char *) salg.salg_type, "hash");
for (i = 0; checksum_info_table[i].list; i++)
for (j = 0; j < checksum_info_table[i].n; j++) {
struct checksum_info *info;
info = &checksum_info_table[i].list[j];
if (!info->name)
continue;
strcpy((char *) salg.salg_name, info->name);
if (bind(sk, (struct sockaddr *) &salg,
sizeof(salg)) < 0)
continue;
info->supported = true;
}
close(sk);
}
LIB_EXPORT bool l_checksum_is_supported(enum l_checksum_type type,
bool check_hmac)
{
const struct checksum_info *list;
init_supported();
if (!check_hmac) {
if (!is_valid_index(checksum_algs, type))
return false;
list = checksum_algs;
} else {
if (!is_valid_index(checksum_hmac_algs, type))
return false;
list = checksum_hmac_algs;
}
return list[type].supported;
}
LIB_EXPORT bool l_checksum_cmac_aes_supported()
{
init_supported();
return checksum_cmac_aes_alg.supported;
}
LIB_EXPORT ssize_t l_checksum_digest_length(enum l_checksum_type type)
{
return is_valid_index(checksum_algs, type) ?
checksum_algs[type].digest_len : 0;
}
|