Path: blob/main/crypto/heimdal/lib/krb5/crypto-des3.c
34889 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#include "krb5_locl.h"3435/*36*37*/3839static void40DES3_random_key(krb5_context context,41krb5_keyblock *key)42{43DES_cblock *k = key->keyvalue.data;44do {45krb5_generate_random_block(k, 3 * sizeof(DES_cblock));46DES_set_odd_parity(&k[0]);47DES_set_odd_parity(&k[1]);48DES_set_odd_parity(&k[2]);49} while(DES_is_weak_key(&k[0]) ||50DES_is_weak_key(&k[1]) ||51DES_is_weak_key(&k[2]));52}535455#ifdef DES3_OLD_ENCTYPE56static struct _krb5_key_type keytype_des3 = {57ETYPE_OLD_DES3_CBC_SHA1,58"des3",59168,6024,61sizeof(struct _krb5_evp_schedule),62DES3_random_key,63_krb5_evp_schedule,64_krb5_des3_salt,65_krb5_DES3_random_to_key,66_krb5_evp_cleanup,67EVP_des_ede3_cbc68};69#endif7071static struct _krb5_key_type keytype_des3_derived = {72ETYPE_OLD_DES3_CBC_SHA1,73"des3",74168,7524,76sizeof(struct _krb5_evp_schedule),77DES3_random_key,78_krb5_evp_schedule,79_krb5_des3_salt_derived,80_krb5_DES3_random_to_key,81_krb5_evp_cleanup,82EVP_des_ede3_cbc83};8485#ifdef DES3_OLD_ENCTYPE86static krb5_error_code87RSA_MD5_DES3_checksum(krb5_context context,88struct _krb5_key_data *key,89const void *data,90size_t len,91unsigned usage,92Checksum *C)93{94return _krb5_des_checksum(context, EVP_md5(), key, data, len, C);95}9697static krb5_error_code98RSA_MD5_DES3_verify(krb5_context context,99struct _krb5_key_data *key,100const void *data,101size_t len,102unsigned usage,103Checksum *C)104{105return _krb5_des_verify(context, EVP_md5(), key, data, len, C);106}107108struct _krb5_checksum_type _krb5_checksum_rsa_md5_des3 = {109CKSUMTYPE_RSA_MD5_DES3,110"rsa-md5-des3",11164,11224,113F_KEYED | F_CPROOF | F_VARIANT,114RSA_MD5_DES3_checksum,115RSA_MD5_DES3_verify116};117#endif118119struct _krb5_checksum_type _krb5_checksum_hmac_sha1_des3 = {120CKSUMTYPE_HMAC_SHA1_DES3,121"hmac-sha1-des3",12264,12320,124F_KEYED | F_CPROOF | F_DERIVED,125_krb5_SP_HMAC_SHA1_checksum,126NULL127};128129#ifdef DES3_OLD_ENCTYPE130struct _krb5_encryption_type _krb5_enctype_des3_cbc_md5 = {131ETYPE_DES3_CBC_MD5,132"des3-cbc-md5",1338,1348,1358,136&keytype_des3,137&_krb5_checksum_rsa_md5,138&_krb5_checksum_rsa_md5_des3,1390,140_krb5_evp_encrypt,1410,142NULL143};144#endif145146struct _krb5_encryption_type _krb5_enctype_des3_cbc_sha1 = {147ETYPE_DES3_CBC_SHA1,148"des3-cbc-sha1",1498,1508,1518,152&keytype_des3_derived,153&_krb5_checksum_sha1,154&_krb5_checksum_hmac_sha1_des3,155F_DERIVED,156_krb5_evp_encrypt,1570,158NULL159};160161#ifdef DES3_OLD_ENCTYPE162struct _krb5_encryption_type _krb5_enctype_old_des3_cbc_sha1 = {163ETYPE_OLD_DES3_CBC_SHA1,164"old-des3-cbc-sha1",1658,1668,1678,168&keytype_des3,169&_krb5_checksum_sha1,170&_krb5_checksum_hmac_sha1_des3,1710,172_krb5_evp_encrypt,1730,174NULL175};176#endif177178struct _krb5_encryption_type _krb5_enctype_des3_cbc_none = {179ETYPE_DES3_CBC_NONE,180"des3-cbc-none",1818,1828,1830,184&keytype_des3_derived,185&_krb5_checksum_none,186NULL,187F_PSEUDO,188_krb5_evp_encrypt,1890,190NULL191};192193void194_krb5_DES3_random_to_key(krb5_context context,195krb5_keyblock *key,196const void *data,197size_t size)198{199unsigned char *x = key->keyvalue.data;200const u_char *q = data;201DES_cblock *k;202int i, j;203204memset(key->keyvalue.data, 0, key->keyvalue.length);205for (i = 0; i < 3; ++i) {206unsigned char foo;207for (j = 0; j < 7; ++j) {208unsigned char b = q[7 * i + j];209210x[8 * i + j] = b;211}212foo = 0;213for (j = 6; j >= 0; --j) {214foo |= q[7 * i + j] & 1;215foo <<= 1;216}217x[8 * i + 7] = foo;218}219k = key->keyvalue.data;220for (i = 0; i < 3; i++) {221DES_set_odd_parity(&k[i]);222if(DES_is_weak_key(&k[i]))223_krb5_xor(&k[i], (const unsigned char*)"\0\0\0\0\0\0\0\xf0");224}225}226227228