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 EVP_MAC_IMPL *siphash_dup(const EVP_MAC_IMPL *ssrc)
47 static size_t siphash_size(EVP_MAC_IMPL *sctx)
49 return SipHash_hash_size(&sctx->ctx);
52 static int siphash_init(EVP_MAC_IMPL *sctx)
54 /* Not much to do here, actual initialization happens through controls */
58 static int siphash_update(EVP_MAC_IMPL *sctx, const unsigned char *data,
61 SipHash_Update(&sctx->ctx, data, datalen);
65 static int siphash_final(EVP_MAC_IMPL *sctx, unsigned char *out)
67 size_t hlen = siphash_size(sctx);
69 return SipHash_Final(&sctx->ctx, out, hlen);
72 static int siphash_ctrl(EVP_MAC_IMPL *sctx, int cmd, va_list args)
75 case EVP_MAC_CTRL_SET_SIZE:
77 size_t size = va_arg(args, size_t);
79 return SipHash_set_hash_size(&sctx->ctx, size);
82 case EVP_MAC_CTRL_SET_KEY:
84 const unsigned char *key = va_arg(args, const unsigned char *);
85 size_t keylen = va_arg(args, size_t);
87 if (key == NULL || keylen != SIPHASH_KEY_SIZE)
90 return SipHash_Init(&sctx->ctx, key, 0, 0);
99 static int siphash_ctrl_int(EVP_MAC_IMPL *sctx, int cmd, ...)
105 rv = siphash_ctrl(sctx, cmd, args);
111 static int siphash_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
113 return siphash_ctrl_int(ctx, cmd, buf, buflen);
116 static int siphash_ctrl_str(EVP_MAC_IMPL *ctx,
117 const char *type, const char *value)
121 if (strcmp(type, "digestsize") == 0) {
122 size_t hash_size = atoi(value);
124 return siphash_ctrl_int(ctx, EVP_MAC_CTRL_SET_SIZE, hash_size);
126 if (strcmp(type, "key") == 0)
127 return EVP_str2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
129 if (strcmp(type, "hexkey") == 0)
130 return EVP_hex2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
135 const EVP_MAC siphash_meth = {