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