Home | History | Annotate | Line # | Download | only in krb5
      1  1.5  christos /*	$NetBSD: crypto-des-common.c,v 1.6 2023/06/19 21:41:44 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 1997 - 2008 Kungliga Tekniska Hgskolan
      5  1.1     elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1     elric  * All rights reserved.
      7  1.1     elric  *
      8  1.1     elric  * Redistribution and use in source and binary forms, with or without
      9  1.1     elric  * modification, are permitted provided that the following conditions
     10  1.1     elric  * are met:
     11  1.1     elric  *
     12  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     13  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     14  1.1     elric  *
     15  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     elric  *    documentation and/or other materials provided with the distribution.
     18  1.1     elric  *
     19  1.1     elric  * 3. Neither the name of the Institute nor the names of its contributors
     20  1.1     elric  *    may be used to endorse or promote products derived from this software
     21  1.1     elric  *    without specific prior written permission.
     22  1.1     elric  *
     23  1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     24  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     27  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1     elric  * SUCH DAMAGE.
     34  1.1     elric  */
     35  1.1     elric 
     36  1.1     elric /* Functions which are used by both single and triple DES enctypes */
     37  1.1     elric 
     38  1.1     elric #include "krb5_locl.h"
     39  1.1     elric 
     40  1.1     elric /*
     41  1.1     elric  * A = A xor B. A & B are 8 bytes.
     42  1.1     elric  */
     43  1.1     elric 
     44  1.2  christos KRB5_LIB_FUNCTION void KRB5_LIB_CALL
     45  1.2  christos _krb5_xor8(unsigned char *a, const unsigned char *b)
     46  1.1     elric {
     47  1.1     elric     a[0] ^= b[0];
     48  1.1     elric     a[1] ^= b[1];
     49  1.1     elric     a[2] ^= b[2];
     50  1.1     elric     a[3] ^= b[3];
     51  1.1     elric     a[4] ^= b[4];
     52  1.1     elric     a[5] ^= b[5];
     53  1.1     elric     a[6] ^= b[6];
     54  1.1     elric     a[7] ^= b[7];
     55  1.1     elric }
     56  1.1     elric 
     57  1.1     elric #if defined(DES3_OLD_ENCTYPE) || defined(HEIM_WEAK_CRYPTO)
     58  1.2  christos KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
     59  1.1     elric _krb5_des_checksum(krb5_context context,
     60  1.1     elric 		   const EVP_MD *evp_md,
     61  1.1     elric 		   struct _krb5_key_data *key,
     62  1.1     elric 		   const void *data,
     63  1.1     elric 		   size_t len,
     64  1.1     elric 		   Checksum *cksum)
     65  1.1     elric {
     66  1.1     elric     struct _krb5_evp_schedule *ctx = key->schedule->data;
     67  1.1     elric     EVP_MD_CTX *m;
     68  1.1     elric     DES_cblock ivec;
     69  1.1     elric     unsigned char *p = cksum->checksum.data;
     70  1.1     elric 
     71  1.1     elric     krb5_generate_random_block(p, 8);
     72  1.1     elric 
     73  1.1     elric     m = EVP_MD_CTX_create();
     74  1.2  christos     if (m == NULL)
     75  1.2  christos 	return krb5_enomem(context);
     76  1.1     elric 
     77  1.1     elric     EVP_DigestInit_ex(m, evp_md, NULL);
     78  1.1     elric     EVP_DigestUpdate(m, p, 8);
     79  1.1     elric     EVP_DigestUpdate(m, data, len);
     80  1.1     elric     EVP_DigestFinal_ex (m, p + 8, NULL);
     81  1.1     elric     EVP_MD_CTX_destroy(m);
     82  1.4  christos     memset_s (&ivec, sizeof(ivec), 0, sizeof(ivec));
     83  1.3  christos 
     84  1.3  christos #if OPENSSL_VERSION_NUMBER < 0x10100000UL
     85  1.3  christos     ctx->ectx = malloc(sizeof(*ctx->ectx));
     86  1.3  christos     EVP_CIPHER_CTX_init(ctx->ectx);
     87  1.3  christos #else
     88  1.3  christos     ctx->ectx = EVP_CIPHER_CTX_new();
     89  1.3  christos #endif
     90  1.3  christos 
     91  1.5  christos     if (!EVP_CipherInit_ex(ctx->ectx, NULL, NULL, NULL, (void *)&ivec, -1))
     92  1.5  christos 	krb5_abortx(context, "can't initialize cipher");
     93  1.3  christos     EVP_Cipher(ctx->ectx, p, p, 24);
     94  1.1     elric 
     95  1.1     elric     return 0;
     96  1.1     elric }
     97  1.1     elric 
     98  1.2  christos KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
     99  1.1     elric _krb5_des_verify(krb5_context context,
    100  1.1     elric 		 const EVP_MD *evp_md,
    101  1.1     elric 		 struct _krb5_key_data *key,
    102  1.1     elric 		 const void *data,
    103  1.1     elric 		 size_t len,
    104  1.1     elric 		 Checksum *C)
    105  1.1     elric {
    106  1.1     elric     struct _krb5_evp_schedule *ctx = key->schedule->data;
    107  1.1     elric     EVP_MD_CTX *m;
    108  1.1     elric     unsigned char tmp[24];
    109  1.1     elric     unsigned char res[16];
    110  1.1     elric     DES_cblock ivec;
    111  1.1     elric     krb5_error_code ret = 0;
    112  1.1     elric 
    113  1.1     elric     m = EVP_MD_CTX_create();
    114  1.2  christos     if (m == NULL)
    115  1.2  christos 	return krb5_enomem(context);
    116  1.1     elric 
    117  1.4  christos     memset_s (&ivec, sizeof(ivec), 0, sizeof(ivec));
    118  1.3  christos #if OPENSSL_VERSION_NUMBER < 0x10100000UL
    119  1.3  christos     ctx->dctx = malloc(sizeof(*ctx->dctx));
    120  1.3  christos     EVP_CIPHER_CTX_init(ctx->dctx);
    121  1.3  christos #else
    122  1.3  christos     ctx->dctx = EVP_CIPHER_CTX_new();
    123  1.3  christos #endif
    124  1.5  christos     if (!EVP_CipherInit_ex(ctx->dctx, NULL, NULL, NULL, (void *)&ivec, -1))
    125  1.5  christos 	krb5_abortx(context, "can't initialize cipher");
    126  1.3  christos     EVP_Cipher(ctx->dctx, tmp, C->checksum.data, 24);
    127  1.1     elric 
    128  1.1     elric     EVP_DigestInit_ex(m, evp_md, NULL);
    129  1.1     elric     EVP_DigestUpdate(m, tmp, 8); /* confounder */
    130  1.1     elric     EVP_DigestUpdate(m, data, len);
    131  1.1     elric     EVP_DigestFinal_ex (m, res, NULL);
    132  1.1     elric     EVP_MD_CTX_destroy(m);
    133  1.1     elric     if(ct_memcmp(res, tmp + 8, sizeof(res)) != 0) {
    134  1.1     elric 	krb5_clear_error_message (context);
    135  1.1     elric 	ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
    136  1.1     elric     }
    137  1.4  christos     memset_s (tmp, sizeof(tmp), 0, sizeof(tmp));
    138  1.4  christos     memset_s (res, sizeof(res), 0, sizeof(res));
    139  1.1     elric     return ret;
    140  1.1     elric }
    141  1.1     elric 
    142  1.1     elric #endif
    143  1.1     elric 
    144  1.1     elric static krb5_error_code
    145  1.1     elric RSA_MD5_checksum(krb5_context context,
    146  1.1     elric 		 struct _krb5_key_data *key,
    147  1.1     elric 		 const void *data,
    148  1.1     elric 		 size_t len,
    149  1.1     elric 		 unsigned usage,
    150  1.1     elric 		 Checksum *C)
    151  1.1     elric {
    152  1.1     elric     if (EVP_Digest(data, len, C->checksum.data, NULL, EVP_md5(), NULL) != 1)
    153  1.1     elric 	krb5_abortx(context, "md5 checksum failed");
    154  1.1     elric     return 0;
    155  1.1     elric }
    156  1.1     elric 
    157  1.1     elric struct _krb5_checksum_type _krb5_checksum_rsa_md5 = {
    158  1.1     elric     CKSUMTYPE_RSA_MD5,
    159  1.1     elric     "rsa-md5",
    160  1.1     elric     64,
    161  1.1     elric     16,
    162  1.1     elric     F_CPROOF,
    163  1.1     elric     RSA_MD5_checksum,
    164  1.1     elric     NULL
    165  1.1     elric };
    166