2 * Copyright 2021-2022 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/core_names.h>
13 #include <openssl/evp.h>
14 #include <openssl/params.h>
15 #include <openssl/err.h>
18 * Taken from the test vector from the paper "SipHash: a fast short-input PRF".
19 * https://www.aumasson.jp/siphash/siphash.pdf
23 * Hard coding the key into an application is very bad.
24 * It is done here solely for educational purposes.
26 static unsigned char key[] = {
27 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
28 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
31 static unsigned char data[] = {
32 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
36 static const unsigned char expected_output[] = {
37 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1
41 * A property query used for selecting the SIPHASH implementation.
43 static char *propq = NULL;
45 int main(int argc, char **argv)
47 int rv = EXIT_FAILURE;
49 EVP_MAC_CTX *mctx = NULL;
51 OSSL_PARAM params[4], *p = params;
52 OSSL_LIB_CTX *library_context = NULL;
53 unsigned int digest_len = 8, c_rounds = 2, d_rounds = 4;
56 library_context = OSSL_LIB_CTX_new();
57 if (library_context == NULL) {
58 fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
62 /* Fetch the SipHash implementation */
63 mac = EVP_MAC_fetch(library_context, "SIPHASH", propq);
65 fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
69 /* Create a context for the SipHash operation */
70 mctx = EVP_MAC_CTX_new(mac);
72 fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
76 /* SipHash can support either 8 or 16-byte digests. */
77 *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_SIZE, &digest_len);
80 * The number of C-rounds and D-rounds is configurable. Standard SipHash
81 * uses values of 2 and 4 respectively. The following lines are unnecessary
82 * as they set the default, but demonstrate how to change these values.
84 *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_C_ROUNDS, &c_rounds);
85 *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_D_ROUNDS, &d_rounds);
87 *p = OSSL_PARAM_construct_end();
89 /* Initialise the SIPHASH operation */
90 if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
91 fprintf(stderr, "EVP_MAC_init() failed\n");
95 /* Make one or more calls to process the data to be authenticated */
96 if (!EVP_MAC_update(mctx, data, sizeof(data))) {
97 fprintf(stderr, "EVP_MAC_update() failed\n");
101 /* Make one call to the final to get the MAC */
102 if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
103 fprintf(stderr, "EVP_MAC_final() failed\n");
107 printf("Generated MAC:\n");
108 BIO_dump_indent_fp(stdout, out, out_len, 2);
111 if (out_len != sizeof(expected_output)) {
112 fprintf(stderr, "Generated MAC has an unexpected length\n");
116 if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
117 fprintf(stderr, "Generated MAC does not match expected value\n");
123 EVP_MAC_CTX_free(mctx);
125 OSSL_LIB_CTX_free(library_context);
126 if (rv != EXIT_SUCCESS)
127 ERR_print_errors_fp(stderr);