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