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