Path: blob/main/crypto/heimdal/lib/krb5/crypto-des-common.c
34878 views
/*1* Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233/* Functions which are used by both single and triple DES enctypes */3435#include "krb5_locl.h"3637/*38* A = A xor B. A & B are 8 bytes.39*/4041void42_krb5_xor (DES_cblock *key, const unsigned char *b)43{44unsigned char *a = (unsigned char*)key;45a[0] ^= b[0];46a[1] ^= b[1];47a[2] ^= b[2];48a[3] ^= b[3];49a[4] ^= b[4];50a[5] ^= b[5];51a[6] ^= b[6];52a[7] ^= b[7];53}5455#if defined(DES3_OLD_ENCTYPE) || defined(HEIM_WEAK_CRYPTO)56krb5_error_code57_krb5_des_checksum(krb5_context context,58const EVP_MD *evp_md,59struct _krb5_key_data *key,60const void *data,61size_t len,62Checksum *cksum)63{64struct _krb5_evp_schedule *ctx = key->schedule->data;65EVP_MD_CTX *m;66DES_cblock ivec;67unsigned char *p = cksum->checksum.data;6869krb5_generate_random_block(p, 8);7071m = EVP_MD_CTX_create();72if (m == NULL) {73krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));74return ENOMEM;75}7677EVP_DigestInit_ex(m, evp_md, NULL);78EVP_DigestUpdate(m, p, 8);79EVP_DigestUpdate(m, data, len);80EVP_DigestFinal_ex (m, p + 8, NULL);81EVP_MD_CTX_destroy(m);82memset (&ivec, 0, sizeof(ivec));83EVP_CipherInit_ex(ctx->ectx, NULL, NULL, NULL, (void *)&ivec, -1);84EVP_Cipher(ctx->ectx, p, p, 24);8586return 0;87}8889krb5_error_code90_krb5_des_verify(krb5_context context,91const EVP_MD *evp_md,92struct _krb5_key_data *key,93const void *data,94size_t len,95Checksum *C)96{97struct _krb5_evp_schedule *ctx = key->schedule->data;98EVP_MD_CTX *m;99unsigned char tmp[24];100unsigned char res[16];101DES_cblock ivec;102krb5_error_code ret = 0;103104m = EVP_MD_CTX_create();105if (m == NULL) {106krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));107return ENOMEM;108}109110memset(&ivec, 0, sizeof(ivec));111EVP_CipherInit_ex(ctx->dctx, NULL, NULL, NULL, (void *)&ivec, -1);112EVP_Cipher(ctx->dctx, tmp, C->checksum.data, 24);113114EVP_DigestInit_ex(m, evp_md, NULL);115EVP_DigestUpdate(m, tmp, 8); /* confounder */116EVP_DigestUpdate(m, data, len);117EVP_DigestFinal_ex (m, res, NULL);118EVP_MD_CTX_destroy(m);119if(ct_memcmp(res, tmp + 8, sizeof(res)) != 0) {120krb5_clear_error_message (context);121ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;122}123memset(tmp, 0, sizeof(tmp));124memset(res, 0, sizeof(res));125return ret;126}127128#endif129130static krb5_error_code131RSA_MD5_checksum(krb5_context context,132struct _krb5_key_data *key,133const void *data,134size_t len,135unsigned usage,136Checksum *C)137{138if (EVP_Digest(data, len, C->checksum.data, NULL, EVP_md5(), NULL) != 1)139krb5_abortx(context, "md5 checksum failed");140return 0;141}142143struct _krb5_checksum_type _krb5_checksum_rsa_md5 = {144CKSUMTYPE_RSA_MD5,145"rsa-md5",14664,14716,148F_CPROOF,149RSA_MD5_checksum,150NULL151};152153154