xref: /freebsd/crypto/krb5/src/lib/crypto/openssl/cmac.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* lib/crypto/openssl/cmac.c - OpenSSL CMAC implementation */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2021 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert #include "crypto_int.h"
34*7f2fe78bSCy Schubert 
35*7f2fe78bSCy Schubert #ifdef K5_OPENSSL_CMAC
36*7f2fe78bSCy Schubert 
37*7f2fe78bSCy Schubert #include <openssl/evp.h>
38*7f2fe78bSCy Schubert #include <openssl/params.h>
39*7f2fe78bSCy Schubert #include <openssl/core_names.h>
40*7f2fe78bSCy Schubert 
41*7f2fe78bSCy Schubert krb5_error_code
krb5int_cmac_checksum(const struct krb5_enc_provider * enc,krb5_key key,const krb5_crypto_iov * data,size_t num_data,krb5_data * output)42*7f2fe78bSCy Schubert krb5int_cmac_checksum(const struct krb5_enc_provider *enc, krb5_key key,
43*7f2fe78bSCy Schubert                       const krb5_crypto_iov *data, size_t num_data,
44*7f2fe78bSCy Schubert                       krb5_data *output)
45*7f2fe78bSCy Schubert {
46*7f2fe78bSCy Schubert     int ok;
47*7f2fe78bSCy Schubert     EVP_MAC *mac = NULL;
48*7f2fe78bSCy Schubert     EVP_MAC_CTX *ctx = NULL;
49*7f2fe78bSCy Schubert     OSSL_PARAM params[2], *p = params;
50*7f2fe78bSCy Schubert     size_t i = 0, md_len;
51*7f2fe78bSCy Schubert     char *cipher;
52*7f2fe78bSCy Schubert 
53*7f2fe78bSCy Schubert     if (enc == &krb5int_enc_camellia128)
54*7f2fe78bSCy Schubert         cipher = "CAMELLIA-128-CBC";
55*7f2fe78bSCy Schubert     else if (enc == &krb5int_enc_camellia256)
56*7f2fe78bSCy Schubert         cipher = "CAMELLIA-256-CBC";
57*7f2fe78bSCy Schubert     else
58*7f2fe78bSCy Schubert         return KRB5_CRYPTO_INTERNAL;
59*7f2fe78bSCy Schubert 
60*7f2fe78bSCy Schubert     mac = EVP_MAC_fetch(NULL, "CMAC", NULL);
61*7f2fe78bSCy Schubert     if (mac == NULL)
62*7f2fe78bSCy Schubert         return KRB5_CRYPTO_INTERNAL;
63*7f2fe78bSCy Schubert 
64*7f2fe78bSCy Schubert     ctx = EVP_MAC_CTX_new(mac);
65*7f2fe78bSCy Schubert     if (ctx == NULL) {
66*7f2fe78bSCy Schubert         ok = 0;
67*7f2fe78bSCy Schubert         goto cleanup;
68*7f2fe78bSCy Schubert     }
69*7f2fe78bSCy Schubert 
70*7f2fe78bSCy Schubert     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER, cipher, 0);
71*7f2fe78bSCy Schubert     *p = OSSL_PARAM_construct_end();
72*7f2fe78bSCy Schubert 
73*7f2fe78bSCy Schubert     ok = EVP_MAC_init(ctx, key->keyblock.contents, key->keyblock.length,
74*7f2fe78bSCy Schubert                       params);
75*7f2fe78bSCy Schubert     for (i = 0; ok && i < num_data; i++) {
76*7f2fe78bSCy Schubert         const krb5_crypto_iov *iov = &data[i];
77*7f2fe78bSCy Schubert         if (!SIGN_IOV(iov))
78*7f2fe78bSCy Schubert             continue;
79*7f2fe78bSCy Schubert         ok = EVP_MAC_update(ctx, (uint8_t *)iov->data.data, iov->data.length);
80*7f2fe78bSCy Schubert     }
81*7f2fe78bSCy Schubert     ok = ok && EVP_MAC_final(ctx, (unsigned char *)output->data, &md_len,
82*7f2fe78bSCy Schubert                              output->length);
83*7f2fe78bSCy Schubert     if (!ok)
84*7f2fe78bSCy Schubert         goto cleanup;
85*7f2fe78bSCy Schubert     output->length = md_len;
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert cleanup:
88*7f2fe78bSCy Schubert     EVP_MAC_free(mac);
89*7f2fe78bSCy Schubert     EVP_MAC_CTX_free(ctx);
90*7f2fe78bSCy Schubert     return ok ? 0 : KRB5_CRYPTO_INTERNAL;
91*7f2fe78bSCy Schubert }
92*7f2fe78bSCy Schubert 
93*7f2fe78bSCy Schubert #endif /* K5_OPENSSL_CMAC */
94