1*b0d17251Schristos /*
2*b0d17251Schristos * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b0d17251Schristos * this file except in compliance with the License. You can obtain a copy
6*b0d17251Schristos * in the file LICENSE in the source distribution or at
7*b0d17251Schristos * https://www.openssl.org/source/license.html
8*b0d17251Schristos */
9*b0d17251Schristos
10*b0d17251Schristos #include <stdio.h>
11*b0d17251Schristos #include <stdlib.h>
12*b0d17251Schristos #include <openssl/core_names.h>
13*b0d17251Schristos #include <openssl/evp.h>
14*b0d17251Schristos #include <openssl/params.h>
15*b0d17251Schristos #include <openssl/err.h>
16*b0d17251Schristos
17*b0d17251Schristos /*
18*b0d17251Schristos * Taken from the test vector from the paper "SipHash: a fast short-input PRF".
19*b0d17251Schristos * https://www.aumasson.jp/siphash/siphash.pdf
20*b0d17251Schristos */
21*b0d17251Schristos
22*b0d17251Schristos /*
23*b0d17251Schristos * Hard coding the key into an application is very bad.
24*b0d17251Schristos * It is done here solely for educational purposes.
25*b0d17251Schristos */
26*b0d17251Schristos static unsigned char key[] = {
27*b0d17251Schristos 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
28*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
29*b0d17251Schristos };
30*b0d17251Schristos
31*b0d17251Schristos static unsigned char data[] = {
32*b0d17251Schristos 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33*b0d17251Schristos 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e
34*b0d17251Schristos };
35*b0d17251Schristos
36*b0d17251Schristos static const unsigned char expected_output[] = {
37*b0d17251Schristos 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1
38*b0d17251Schristos };
39*b0d17251Schristos
40*b0d17251Schristos /*
41*b0d17251Schristos * A property query used for selecting the SIPHASH implementation.
42*b0d17251Schristos */
43*b0d17251Schristos static char *propq = NULL;
44*b0d17251Schristos
main(int argc,char ** argv)45*b0d17251Schristos int main(int argc, char **argv)
46*b0d17251Schristos {
47*b0d17251Schristos int rv = EXIT_FAILURE;
48*b0d17251Schristos EVP_MAC *mac = NULL;
49*b0d17251Schristos EVP_MAC_CTX *mctx = NULL;
50*b0d17251Schristos unsigned char out[8];
51*b0d17251Schristos OSSL_PARAM params[4], *p = params;
52*b0d17251Schristos OSSL_LIB_CTX *library_context = NULL;
53*b0d17251Schristos unsigned int digest_len = 8, c_rounds = 2, d_rounds = 4;
54*b0d17251Schristos size_t out_len = 0;
55*b0d17251Schristos
56*b0d17251Schristos library_context = OSSL_LIB_CTX_new();
57*b0d17251Schristos if (library_context == NULL) {
58*b0d17251Schristos fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");
59*b0d17251Schristos goto end;
60*b0d17251Schristos }
61*b0d17251Schristos
62*b0d17251Schristos /* Fetch the SipHash implementation */
63*b0d17251Schristos mac = EVP_MAC_fetch(library_context, "SIPHASH", propq);
64*b0d17251Schristos if (mac == NULL) {
65*b0d17251Schristos fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");
66*b0d17251Schristos goto end;
67*b0d17251Schristos }
68*b0d17251Schristos
69*b0d17251Schristos /* Create a context for the SipHash operation */
70*b0d17251Schristos mctx = EVP_MAC_CTX_new(mac);
71*b0d17251Schristos if (mctx == NULL) {
72*b0d17251Schristos fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");
73*b0d17251Schristos goto end;
74*b0d17251Schristos }
75*b0d17251Schristos
76*b0d17251Schristos /* SipHash can support either 8 or 16-byte digests. */
77*b0d17251Schristos *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_SIZE, &digest_len);
78*b0d17251Schristos
79*b0d17251Schristos /*
80*b0d17251Schristos * The number of C-rounds and D-rounds is configurable. Standard SipHash
81*b0d17251Schristos * uses values of 2 and 4 respectively. The following lines are unnecessary
82*b0d17251Schristos * as they set the default, but demonstrate how to change these values.
83*b0d17251Schristos */
84*b0d17251Schristos *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_C_ROUNDS, &c_rounds);
85*b0d17251Schristos *p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_D_ROUNDS, &d_rounds);
86*b0d17251Schristos
87*b0d17251Schristos *p = OSSL_PARAM_construct_end();
88*b0d17251Schristos
89*b0d17251Schristos /* Initialise the SIPHASH operation */
90*b0d17251Schristos if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {
91*b0d17251Schristos fprintf(stderr, "EVP_MAC_init() failed\n");
92*b0d17251Schristos goto end;
93*b0d17251Schristos }
94*b0d17251Schristos
95*b0d17251Schristos /* Make one or more calls to process the data to be authenticated */
96*b0d17251Schristos if (!EVP_MAC_update(mctx, data, sizeof(data))) {
97*b0d17251Schristos fprintf(stderr, "EVP_MAC_update() failed\n");
98*b0d17251Schristos goto end;
99*b0d17251Schristos }
100*b0d17251Schristos
101*b0d17251Schristos /* Make one call to the final to get the MAC */
102*b0d17251Schristos if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {
103*b0d17251Schristos fprintf(stderr, "EVP_MAC_final() failed\n");
104*b0d17251Schristos goto end;
105*b0d17251Schristos }
106*b0d17251Schristos
107*b0d17251Schristos printf("Generated MAC:\n");
108*b0d17251Schristos BIO_dump_indent_fp(stdout, out, out_len, 2);
109*b0d17251Schristos putchar('\n');
110*b0d17251Schristos
111*b0d17251Schristos if (out_len != sizeof(expected_output)) {
112*b0d17251Schristos fprintf(stderr, "Generated MAC has an unexpected length\n");
113*b0d17251Schristos goto end;
114*b0d17251Schristos }
115*b0d17251Schristos
116*b0d17251Schristos if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {
117*b0d17251Schristos fprintf(stderr, "Generated MAC does not match expected value\n");
118*b0d17251Schristos goto end;
119*b0d17251Schristos }
120*b0d17251Schristos
121*b0d17251Schristos rv = EXIT_SUCCESS;
122*b0d17251Schristos end:
123*b0d17251Schristos EVP_MAC_CTX_free(mctx);
124*b0d17251Schristos EVP_MAC_free(mac);
125*b0d17251Schristos OSSL_LIB_CTX_free(library_context);
126*b0d17251Schristos if (rv != EXIT_SUCCESS)
127*b0d17251Schristos ERR_print_errors_fp(stderr);
128*b0d17251Schristos return rv;
129*b0d17251Schristos }
130