1226031Sstas/* 2226031Sstas * Copyright (c) 1997 - 2008 Kungliga Tekniska H��gskolan 3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4226031Sstas * All rights reserved. 5226031Sstas * 6226031Sstas * Redistribution and use in source and binary forms, with or without 7226031Sstas * modification, are permitted provided that the following conditions 8226031Sstas * are met: 9226031Sstas * 10226031Sstas * 1. Redistributions of source code must retain the above copyright 11226031Sstas * notice, this list of conditions and the following disclaimer. 12226031Sstas * 13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright 14226031Sstas * notice, this list of conditions and the following disclaimer in the 15226031Sstas * documentation and/or other materials provided with the distribution. 16226031Sstas * 17226031Sstas * 3. Neither the name of the Institute nor the names of its contributors 18226031Sstas * may be used to endorse or promote products derived from this software 19226031Sstas * without specific prior written permission. 20226031Sstas * 21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24226031Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31226031Sstas * SUCH DAMAGE. 32226031Sstas */ 33226031Sstas 34226031Sstas/* Functions which are used by both single and triple DES enctypes */ 35226031Sstas 36226031Sstas#include "krb5_locl.h" 37226031Sstas 38226031Sstas/* 39226031Sstas * A = A xor B. A & B are 8 bytes. 40226031Sstas */ 41226031Sstas 42226031Sstasvoid 43226031Sstas_krb5_xor (DES_cblock *key, const unsigned char *b) 44226031Sstas{ 45226031Sstas unsigned char *a = (unsigned char*)key; 46226031Sstas a[0] ^= b[0]; 47226031Sstas a[1] ^= b[1]; 48226031Sstas a[2] ^= b[2]; 49226031Sstas a[3] ^= b[3]; 50226031Sstas a[4] ^= b[4]; 51226031Sstas a[5] ^= b[5]; 52226031Sstas a[6] ^= b[6]; 53226031Sstas a[7] ^= b[7]; 54226031Sstas} 55226031Sstas 56226031Sstas#if defined(DES3_OLD_ENCTYPE) || defined(HEIM_WEAK_CRYPTO) 57226031Sstaskrb5_error_code 58226031Sstas_krb5_des_checksum(krb5_context context, 59226031Sstas const EVP_MD *evp_md, 60226031Sstas struct _krb5_key_data *key, 61226031Sstas const void *data, 62226031Sstas size_t len, 63226031Sstas Checksum *cksum) 64226031Sstas{ 65226031Sstas struct _krb5_evp_schedule *ctx = key->schedule->data; 66226031Sstas EVP_MD_CTX *m; 67226031Sstas DES_cblock ivec; 68226031Sstas unsigned char *p = cksum->checksum.data; 69226031Sstas 70226031Sstas krb5_generate_random_block(p, 8); 71226031Sstas 72226031Sstas m = EVP_MD_CTX_create(); 73226031Sstas if (m == NULL) { 74226031Sstas krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", "")); 75226031Sstas return ENOMEM; 76226031Sstas } 77226031Sstas 78226031Sstas EVP_DigestInit_ex(m, evp_md, NULL); 79226031Sstas EVP_DigestUpdate(m, p, 8); 80226031Sstas EVP_DigestUpdate(m, data, len); 81226031Sstas EVP_DigestFinal_ex (m, p + 8, NULL); 82226031Sstas EVP_MD_CTX_destroy(m); 83226031Sstas memset (&ivec, 0, sizeof(ivec)); 84226031Sstas EVP_CipherInit_ex(&ctx->ectx, NULL, NULL, NULL, (void *)&ivec, -1); 85226031Sstas EVP_Cipher(&ctx->ectx, p, p, 24); 86226031Sstas 87226031Sstas return 0; 88226031Sstas} 89226031Sstas 90226031Sstaskrb5_error_code 91226031Sstas_krb5_des_verify(krb5_context context, 92226031Sstas const EVP_MD *evp_md, 93226031Sstas struct _krb5_key_data *key, 94226031Sstas const void *data, 95226031Sstas size_t len, 96226031Sstas Checksum *C) 97226031Sstas{ 98226031Sstas struct _krb5_evp_schedule *ctx = key->schedule->data; 99226031Sstas EVP_MD_CTX *m; 100226031Sstas unsigned char tmp[24]; 101226031Sstas unsigned char res[16]; 102226031Sstas DES_cblock ivec; 103226031Sstas krb5_error_code ret = 0; 104226031Sstas 105226031Sstas m = EVP_MD_CTX_create(); 106226031Sstas if (m == NULL) { 107226031Sstas krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", "")); 108226031Sstas return ENOMEM; 109226031Sstas } 110226031Sstas 111226031Sstas memset(&ivec, 0, sizeof(ivec)); 112226031Sstas EVP_CipherInit_ex(&ctx->dctx, NULL, NULL, NULL, (void *)&ivec, -1); 113226031Sstas EVP_Cipher(&ctx->dctx, tmp, C->checksum.data, 24); 114226031Sstas 115226031Sstas EVP_DigestInit_ex(m, evp_md, NULL); 116226031Sstas EVP_DigestUpdate(m, tmp, 8); /* confounder */ 117226031Sstas EVP_DigestUpdate(m, data, len); 118226031Sstas EVP_DigestFinal_ex (m, res, NULL); 119226031Sstas EVP_MD_CTX_destroy(m); 120226031Sstas if(ct_memcmp(res, tmp + 8, sizeof(res)) != 0) { 121226031Sstas krb5_clear_error_message (context); 122226031Sstas ret = KRB5KRB_AP_ERR_BAD_INTEGRITY; 123226031Sstas } 124226031Sstas memset(tmp, 0, sizeof(tmp)); 125226031Sstas memset(res, 0, sizeof(res)); 126226031Sstas return ret; 127226031Sstas} 128226031Sstas 129226031Sstas#endif 130226031Sstas 131226031Sstasstatic krb5_error_code 132226031SstasRSA_MD5_checksum(krb5_context context, 133226031Sstas struct _krb5_key_data *key, 134226031Sstas const void *data, 135226031Sstas size_t len, 136226031Sstas unsigned usage, 137226031Sstas Checksum *C) 138226031Sstas{ 139226031Sstas if (EVP_Digest(data, len, C->checksum.data, NULL, EVP_md5(), NULL) != 1) 140226031Sstas krb5_abortx(context, "md5 checksum failed"); 141226031Sstas return 0; 142226031Sstas} 143226031Sstas 144226031Sstasstruct _krb5_checksum_type _krb5_checksum_rsa_md5 = { 145226031Sstas CKSUMTYPE_RSA_MD5, 146226031Sstas "rsa-md5", 147226031Sstas 64, 148226031Sstas 16, 149226031Sstas F_CPROOF, 150226031Sstas RSA_MD5_checksum, 151226031Sstas NULL 152226031Sstas}; 153