crypto-des-common.c revision 1.1 1 1.1 elric /* $NetBSD: crypto-des-common.c,v 1.1 2011/04/13 18:15:32 elric 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.1 elric void
45 1.1 elric _krb5_xor (DES_cblock *key, const unsigned char *b)
46 1.1 elric {
47 1.1 elric unsigned char *a = (unsigned char*)key;
48 1.1 elric a[0] ^= b[0];
49 1.1 elric a[1] ^= b[1];
50 1.1 elric a[2] ^= b[2];
51 1.1 elric a[3] ^= b[3];
52 1.1 elric a[4] ^= b[4];
53 1.1 elric a[5] ^= b[5];
54 1.1 elric a[6] ^= b[6];
55 1.1 elric a[7] ^= b[7];
56 1.1 elric }
57 1.1 elric
58 1.1 elric #if defined(DES3_OLD_ENCTYPE) || defined(HEIM_WEAK_CRYPTO)
59 1.1 elric krb5_error_code
60 1.1 elric _krb5_des_checksum(krb5_context context,
61 1.1 elric const EVP_MD *evp_md,
62 1.1 elric struct _krb5_key_data *key,
63 1.1 elric const void *data,
64 1.1 elric size_t len,
65 1.1 elric Checksum *cksum)
66 1.1 elric {
67 1.1 elric struct _krb5_evp_schedule *ctx = key->schedule->data;
68 1.1 elric EVP_MD_CTX *m;
69 1.1 elric DES_cblock ivec;
70 1.1 elric unsigned char *p = cksum->checksum.data;
71 1.1 elric
72 1.1 elric krb5_generate_random_block(p, 8);
73 1.1 elric
74 1.1 elric m = EVP_MD_CTX_create();
75 1.1 elric if (m == NULL) {
76 1.1 elric krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
77 1.1 elric return ENOMEM;
78 1.1 elric }
79 1.1 elric
80 1.1 elric EVP_DigestInit_ex(m, evp_md, NULL);
81 1.1 elric EVP_DigestUpdate(m, p, 8);
82 1.1 elric EVP_DigestUpdate(m, data, len);
83 1.1 elric EVP_DigestFinal_ex (m, p + 8, NULL);
84 1.1 elric EVP_MD_CTX_destroy(m);
85 1.1 elric memset (&ivec, 0, sizeof(ivec));
86 1.1 elric EVP_CipherInit_ex(&ctx->ectx, NULL, NULL, NULL, (void *)&ivec, -1);
87 1.1 elric EVP_Cipher(&ctx->ectx, p, p, 24);
88 1.1 elric
89 1.1 elric return 0;
90 1.1 elric }
91 1.1 elric
92 1.1 elric krb5_error_code
93 1.1 elric _krb5_des_verify(krb5_context context,
94 1.1 elric const EVP_MD *evp_md,
95 1.1 elric struct _krb5_key_data *key,
96 1.1 elric const void *data,
97 1.1 elric size_t len,
98 1.1 elric Checksum *C)
99 1.1 elric {
100 1.1 elric struct _krb5_evp_schedule *ctx = key->schedule->data;
101 1.1 elric EVP_MD_CTX *m;
102 1.1 elric unsigned char tmp[24];
103 1.1 elric unsigned char res[16];
104 1.1 elric DES_cblock ivec;
105 1.1 elric krb5_error_code ret = 0;
106 1.1 elric
107 1.1 elric m = EVP_MD_CTX_create();
108 1.1 elric if (m == NULL) {
109 1.1 elric krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
110 1.1 elric return ENOMEM;
111 1.1 elric }
112 1.1 elric
113 1.1 elric memset(&ivec, 0, sizeof(ivec));
114 1.1 elric EVP_CipherInit_ex(&ctx->dctx, NULL, NULL, NULL, (void *)&ivec, -1);
115 1.1 elric EVP_Cipher(&ctx->dctx, tmp, C->checksum.data, 24);
116 1.1 elric
117 1.1 elric EVP_DigestInit_ex(m, evp_md, NULL);
118 1.1 elric EVP_DigestUpdate(m, tmp, 8); /* confounder */
119 1.1 elric EVP_DigestUpdate(m, data, len);
120 1.1 elric EVP_DigestFinal_ex (m, res, NULL);
121 1.1 elric EVP_MD_CTX_destroy(m);
122 1.1 elric if(ct_memcmp(res, tmp + 8, sizeof(res)) != 0) {
123 1.1 elric krb5_clear_error_message (context);
124 1.1 elric ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
125 1.1 elric }
126 1.1 elric memset(tmp, 0, sizeof(tmp));
127 1.1 elric memset(res, 0, sizeof(res));
128 1.1 elric return ret;
129 1.1 elric }
130 1.1 elric
131 1.1 elric #endif
132 1.1 elric
133 1.1 elric static krb5_error_code
134 1.1 elric RSA_MD5_checksum(krb5_context context,
135 1.1 elric struct _krb5_key_data *key,
136 1.1 elric const void *data,
137 1.1 elric size_t len,
138 1.1 elric unsigned usage,
139 1.1 elric Checksum *C)
140 1.1 elric {
141 1.1 elric if (EVP_Digest(data, len, C->checksum.data, NULL, EVP_md5(), NULL) != 1)
142 1.1 elric krb5_abortx(context, "md5 checksum failed");
143 1.1 elric return 0;
144 1.1 elric }
145 1.1 elric
146 1.1 elric struct _krb5_checksum_type _krb5_checksum_rsa_md5 = {
147 1.1 elric CKSUMTYPE_RSA_MD5,
148 1.1 elric "rsa-md5",
149 1.1 elric 64,
150 1.1 elric 16,
151 1.1 elric F_CPROOF,
152 1.1 elric RSA_MD5_checksum,
153 1.1 elric NULL
154 1.1 elric };
155