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