Path: blob/main/crypto/openssl/demos/mac/siphash.c
34870 views
/*1* Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <stdio.h>10#include <stdlib.h>11#include <openssl/core_names.h>12#include <openssl/evp.h>13#include <openssl/params.h>14#include <openssl/err.h>1516/*17* Taken from the test vector from the paper "SipHash: a fast short-input PRF".18* https://www.aumasson.jp/siphash/siphash.pdf19*/2021/*22* Hard coding the key into an application is very bad.23* It is done here solely for educational purposes.24*/25static unsigned char key[] = {260x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,270x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f28};2930static unsigned char data[] = {310x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,320x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e33};3435static const unsigned char expected_output[] = {360xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa137};3839/*40* A property query used for selecting the SIPHASH implementation.41*/42static char *propq = NULL;4344int main(int argc, char **argv)45{46int ret = EXIT_FAILURE;47EVP_MAC *mac = NULL;48EVP_MAC_CTX *mctx = NULL;49unsigned char out[8];50OSSL_PARAM params[4], *p = params;51OSSL_LIB_CTX *library_context = NULL;52unsigned int digest_len = 8, c_rounds = 2, d_rounds = 4;53size_t out_len = 0;5455library_context = OSSL_LIB_CTX_new();56if (library_context == NULL) {57fprintf(stderr, "OSSL_LIB_CTX_new() returned NULL\n");58goto end;59}6061/* Fetch the SipHash implementation */62mac = EVP_MAC_fetch(library_context, "SIPHASH", propq);63if (mac == NULL) {64fprintf(stderr, "EVP_MAC_fetch() returned NULL\n");65goto end;66}6768/* Create a context for the SipHash operation */69mctx = EVP_MAC_CTX_new(mac);70if (mctx == NULL) {71fprintf(stderr, "EVP_MAC_CTX_new() returned NULL\n");72goto end;73}7475/* SipHash can support either 8 or 16-byte digests. */76*p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_SIZE, &digest_len);7778/*79* The number of C-rounds and D-rounds is configurable. Standard SipHash80* uses values of 2 and 4 respectively. The following lines are unnecessary81* as they set the default, but demonstrate how to change these values.82*/83*p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_C_ROUNDS, &c_rounds);84*p++ = OSSL_PARAM_construct_uint(OSSL_MAC_PARAM_D_ROUNDS, &d_rounds);8586*p = OSSL_PARAM_construct_end();8788/* Initialise the SIPHASH operation */89if (!EVP_MAC_init(mctx, key, sizeof(key), params)) {90fprintf(stderr, "EVP_MAC_init() failed\n");91goto end;92}9394/* Make one or more calls to process the data to be authenticated */95if (!EVP_MAC_update(mctx, data, sizeof(data))) {96fprintf(stderr, "EVP_MAC_update() failed\n");97goto end;98}99100/* Make one call to the final to get the MAC */101if (!EVP_MAC_final(mctx, out, &out_len, sizeof(out))) {102fprintf(stderr, "EVP_MAC_final() failed\n");103goto end;104}105106printf("Generated MAC:\n");107BIO_dump_indent_fp(stdout, out, out_len, 2);108putchar('\n');109110if (out_len != sizeof(expected_output)) {111fprintf(stderr, "Generated MAC has an unexpected length\n");112goto end;113}114115if (CRYPTO_memcmp(expected_output, out, sizeof(expected_output)) != 0) {116fprintf(stderr, "Generated MAC does not match expected value\n");117goto end;118}119120ret = EXIT_SUCCESS;121end:122EVP_MAC_CTX_free(mctx);123EVP_MAC_free(mac);124OSSL_LIB_CTX_free(library_context);125if (ret != EXIT_SUCCESS)126ERR_print_errors_fp(stderr);127return ret;128}129130131