2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/evp.h>
13 #include <openssl/err.h>
14 #include "internal/siphash.h"
15 #include "siphash_local.h"
16 #include "internal/evp_int.h"
18 /* local SIPHASH structure is actually a SIPHASH */
20 struct evp_mac_impl_st {
24 static EVP_MAC_IMPL *siphash_new(void)
26 return OPENSSL_zalloc(sizeof(EVP_MAC_IMPL));
29 static void siphash_free(EVP_MAC_IMPL *sctx)
34 static int siphash_copy(EVP_MAC_IMPL *sdst, EVP_MAC_IMPL *ssrc)
40 static size_t siphash_size(EVP_MAC_IMPL *sctx)
42 return SipHash_hash_size(&sctx->ctx);
45 static int siphash_init(EVP_MAC_IMPL *sctx)
47 /* Not much to do here, actual initialization happens through controls */
51 static int siphash_update(EVP_MAC_IMPL *sctx, const unsigned char *data,
54 SipHash_Update(&sctx->ctx, data, datalen);
58 static int siphash_final(EVP_MAC_IMPL *sctx, unsigned char *out)
60 size_t hlen = siphash_size(sctx);
62 return SipHash_Final(&sctx->ctx, out, hlen);
65 static int siphash_ctrl(EVP_MAC_IMPL *sctx, int cmd, va_list args)
68 case EVP_MAC_CTRL_SET_SIZE:
70 size_t size = va_arg(args, size_t);
72 return SipHash_set_hash_size(&sctx->ctx, size);
75 case EVP_MAC_CTRL_SET_KEY:
77 const unsigned char *key = va_arg(args, const unsigned char *);
78 size_t keylen = va_arg(args, size_t);
80 if (key == NULL || keylen != SIPHASH_KEY_SIZE)
83 return SipHash_Init(&sctx->ctx, key, 0, 0);
92 static int siphash_ctrl_int(EVP_MAC_IMPL *sctx, int cmd, ...)
98 rv = siphash_ctrl(sctx, cmd, args);
104 static int siphash_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
106 return siphash_ctrl_int(ctx, cmd, buf, buflen);
109 static int siphash_ctrl_str(EVP_MAC_IMPL *ctx,
110 const char *type, const char *value)
114 if (strcmp(type, "digestsize") == 0) {
115 size_t hash_size = atoi(value);
117 return siphash_ctrl_int(ctx, EVP_MAC_CTRL_SET_SIZE, hash_size);
119 if (strcmp(type, "key") == 0)
120 return EVP_str2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
122 if (strcmp(type, "hexkey") == 0)
123 return EVP_hex2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
128 const EVP_MAC siphash_meth = {