LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/krb5 - crypto-aes-sha2.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 0 41 0.0 %
Date: 2024-04-21 15:09:00 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  *
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  *
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  *
      17             :  * 3. Neither the name of the Institute nor the names of its contributors
      18             :  *    may be used to endorse or promote products derived from this software
      19             :  *    without specific prior written permission.
      20             :  *
      21             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      22             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      25             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      26             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      27             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      28             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      29             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      30             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      31             :  * SUCH DAMAGE.
      32             :  */
      33             : 
      34             : #include "krb5_locl.h"
      35             : 
      36             : /*
      37             :  * AES HMAC-SHA2
      38             :  */
      39             : 
      40             : krb5_error_code
      41           0 : _krb5_aes_sha2_md_for_enctype(krb5_context context,
      42             :                               krb5_enctype enctype,
      43             :                               const EVP_MD **md)
      44             : {
      45           0 :     switch (enctype) {
      46           0 :     case ETYPE_AES128_CTS_HMAC_SHA256_128:
      47           0 :         *md = EVP_sha256();
      48           0 :         break;
      49           0 :     case ETYPE_AES256_CTS_HMAC_SHA384_192:
      50           0 :         *md = EVP_sha384();
      51           0 :         break;
      52           0 :     default:
      53           0 :         return KRB5_PROG_ETYPE_NOSUPP;
      54             :         break;
      55             :     }
      56           0 :     return 0;
      57             : }
      58             : 
      59             : static krb5_error_code
      60           0 : SP_HMAC_SHA2_checksum(krb5_context context,
      61             :                       krb5_crypto crypto,
      62             :                       struct _krb5_key_data *key,
      63             :                       unsigned usage,
      64             :                       const struct krb5_crypto_iov *iov,
      65             :                       int niov,
      66             :                       Checksum *result)
      67             : {
      68           0 :     krb5_error_code ret;
      69           0 :     const EVP_MD *md;
      70           0 :     unsigned char hmac[EVP_MAX_MD_SIZE];
      71           0 :     unsigned int hmaclen = sizeof(hmac);
      72             : 
      73           0 :     ret = _krb5_aes_sha2_md_for_enctype(context, key->key->keytype, &md);
      74           0 :     if (ret)
      75           0 :         return ret;
      76             : 
      77           0 :     ret = _krb5_evp_hmac_iov(context, crypto, key, iov, niov, hmac,
      78             :                              &hmaclen, md, NULL);
      79           0 :     if (ret)
      80           0 :         return ret;
      81             : 
      82           0 :     heim_assert(result->checksum.length <= hmaclen, "SHA2 internal error");
      83             : 
      84           0 :     memcpy(result->checksum.data, hmac, result->checksum.length);
      85             : 
      86           0 :     return 0;
      87             : }
      88             : 
      89             : static struct _krb5_key_type keytype_aes128_sha2 = {
      90             :     KRB5_ENCTYPE_AES128_CTS_HMAC_SHA256_128,
      91             :     "aes-128-sha2",
      92             :     128,
      93             :     16,
      94             :     sizeof(struct _krb5_evp_schedule),
      95             :     NULL,
      96             :     _krb5_evp_schedule,
      97             :     _krb5_AES_SHA2_salt,
      98             :     NULL,
      99             :     _krb5_evp_cleanup,
     100             :     EVP_aes_128_cbc
     101             : };
     102             : 
     103             : static struct _krb5_key_type keytype_aes256_sha2 = {
     104             :     KRB5_ENCTYPE_AES256_CTS_HMAC_SHA384_192,
     105             :     "aes-256-sha2",
     106             :     256,
     107             :     32,
     108             :     sizeof(struct _krb5_evp_schedule),
     109             :     NULL,
     110             :     _krb5_evp_schedule,
     111             :     _krb5_AES_SHA2_salt,
     112             :     NULL,
     113             :     _krb5_evp_cleanup,
     114             :     EVP_aes_256_cbc
     115             : };
     116             : 
     117             : struct _krb5_checksum_type _krb5_checksum_hmac_sha256_128_aes128 = {
     118             :     CKSUMTYPE_HMAC_SHA256_128_AES128,
     119             :     "hmac-sha256-128-aes128",
     120             :     64,
     121             :     16,
     122             :     F_KEYED | F_CPROOF | F_DERIVED,
     123             :     SP_HMAC_SHA2_checksum,
     124             :     NULL
     125             : };
     126             : 
     127             : struct _krb5_checksum_type _krb5_checksum_hmac_sha384_192_aes256 = {
     128             :     CKSUMTYPE_HMAC_SHA384_192_AES256,
     129             :     "hmac-sha384-192-aes256",
     130             :     128,
     131             :     24,
     132             :     F_KEYED | F_CPROOF | F_DERIVED,
     133             :     SP_HMAC_SHA2_checksum,
     134             :     NULL
     135             : };
     136             : 
     137             : static krb5_error_code
     138           0 : AES_SHA2_PRF(krb5_context context,
     139             :              krb5_crypto crypto,
     140             :              const krb5_data *in,
     141             :              krb5_data *out)
     142             : {
     143           0 :     krb5_error_code ret;
     144           0 :     krb5_data label;
     145           0 :     const EVP_MD *md = NULL;
     146             : 
     147           0 :     ret = _krb5_aes_sha2_md_for_enctype(context, crypto->et->type, &md);
     148           0 :     if (ret)
     149           0 :         return ret;
     150             : 
     151           0 :     label.data = "prf";
     152           0 :     label.length = 3;
     153             : 
     154           0 :     ret = krb5_data_alloc(out, EVP_MD_size(md));
     155           0 :     if (ret)
     156           0 :         return ret;
     157             : 
     158           0 :     ret = _krb5_SP800_108_HMAC_KDF(context, &crypto->key.key->keyvalue,
     159             :                                    &label, in, md, out);
     160             : 
     161           0 :     if (ret)
     162           0 :         krb5_data_free(out);
     163             : 
     164           0 :     return ret;
     165             : }
     166             : 
     167             : struct _krb5_encryption_type _krb5_enctype_aes128_cts_hmac_sha256_128 = {
     168             :     ETYPE_AES128_CTS_HMAC_SHA256_128,
     169             :     "aes128-cts-hmac-sha256-128",
     170             :     "aes128-cts-sha256",
     171             :     16,
     172             :     1,
     173             :     16,
     174             :     &keytype_aes128_sha2,
     175             :     NULL, /* should never be called */
     176             :     &_krb5_checksum_hmac_sha256_128_aes128,
     177             :     F_DERIVED | F_ENC_THEN_CKSUM | F_SP800_108_HMAC_KDF,
     178             :     _krb5_evp_encrypt_cts,
     179             :     NULL,
     180             :     16,
     181             :     AES_SHA2_PRF
     182             : };
     183             : 
     184             : struct _krb5_encryption_type _krb5_enctype_aes256_cts_hmac_sha384_192 = {
     185             :     ETYPE_AES256_CTS_HMAC_SHA384_192,
     186             :     "aes256-cts-hmac-sha384-192",
     187             :     "aes256-cts-sha384",
     188             :     16,
     189             :     1,
     190             :     16,
     191             :     &keytype_aes256_sha2,
     192             :     NULL, /* should never be called */
     193             :     &_krb5_checksum_hmac_sha384_192_aes256,
     194             :     F_DERIVED | F_ENC_THEN_CKSUM | F_SP800_108_HMAC_KDF,
     195             :     _krb5_evp_encrypt_cts,
     196             :     NULL,
     197             :     16,
     198             :     AES_SHA2_PRF
     199             : };

Generated by: LCOV version 1.14