Debian Codegraph @

lib/kafs/rxkad_kdf.c

raw
1 /*
2 * Copyright (c) 1995-2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2013-2014 Carnegie Mellon University
7 * All rights reserved.
8 *
9 * Portions Copyright (c) 2013 by the Massachusetts Institute of Technology
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * 3. Neither the name of the Institute nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 #define HC_DEPRECATED_CRYPTO
41 #include "kafs_locl.h"
42
43 static int rxkad_derive_des_key(const void *, size_t, char[8]);
44 static int compress_parity_bits(void *, size_t *);
45
46 /**
47 * Use NIST SP800-108 with HMAC(MD5) in counter mode as the PRF to derive a
48 * des key from another type of key.
49 *
50 * L is 64, as we take 64 random bits and turn them into a 56-bit des key.
51 * The output of hmac_md5 is 128 bits; we take the first 64 only, so n
52 * properly should be 1. However, we apply a slight variation due to the
53 * possibility of producing a weak des key. If the output key is weak, do NOT
54 * simply correct it, instead, the counter is advanced and the next output
55 * used. As such, we code so as to have n be the full 255 permitted by our
56 * encoding of the counter i in an 8-bit field. L itself is encoded as a
57 * 32-bit field, big-endian. We use the constant string "rxkad" as a label
58 * for this key derivation, the standard NUL byte separator, and omit a
59 * key-derivation context. The input key is unique to the krb5 service ticket,
60 * which is unlikely to be used in an other location. If it is used in such
61 * a fashion, both locations will derive the same des key from the PRF, but
62 * this is no different from if a krb5 des key had been used in the same way,
63 * as traditional krb5 rxkad uses the ticket session key directly as the token
64 * key.
65 *
66 * @param[in] in pointer to input key data
67 * @param[in] insize length of input key data
68 * @param[out] out 8-byte buffer to hold the derived key
69 *
70 * @return Returns 0 to indicate success, or an error code.
71 *
72 * @retval KRB5DES_WEAK_KEY Successive derivation attempts with all
73 * 255 possible counter values each produced weak DES keys. This input
74 * cannot be used to produce a usable key.
75 */
76 static int
77 rxkad_derive_des_key(const void *in, size_t insize, char out[8])
78 {
79 unsigned char i;
80 static unsigned char label[] = "rxkad";
81 /* bits of output, as 32 bit word, MSB first */
82 static unsigned char Lbuf[4] = { 0, 0, 0, 64 };
83 /* only needs to be 16 for md5, but lets be sure it fits */
84 unsigned char tmp[64];
85 unsigned int mdsize;
86 DES_cblock ktmp;
87 HMAC_CTX mctx;
88
89 /* stop when 8 bit counter wraps to 0 */
90 for (i = 1; i; i++) {
91 HMAC_CTX_init(&mctx);
92 HMAC_Init_ex(&mctx, in, insize, EVP_md5(), NULL);
93 HMAC_Update(&mctx, &i, 1);
94 HMAC_Update(&mctx, label, sizeof(label)); /* includes label and separator */
95 HMAC_Update(&mctx, Lbuf, 4);
96 mdsize = sizeof(tmp);
97 HMAC_Final(&mctx, tmp, &mdsize);
98 memcpy(ktmp, tmp, 8);
99 DES_set_odd_parity(&ktmp);
100 if (!DES_is_weak_key(&ktmp)) {
101 memcpy(out, ktmp, 8);
102 return 0;
103 }
104 }
105 return KRB5DES_WEAK_KEY;
106 }
107
108 /**
109 * This is the inverse of the random-to-key for 3des specified in
110 * rfc3961, converting blocks of 8 bytes to blocks of 7 bytes by distributing
111 * the bits of each 8th byte as the lsb of the previous 7 bytes.
112 *
113 * @param[in,out] buffer Buffer containing the key to be converted
114 * @param[in,out] bufsiz Points to the size of the key data. On
115 * return, this is updated to reflect the size of the compressed data.
116 *
117 * @return Returns 0 to indicate success, or an error code.
118 *
119 * @retval KRB5_BAD_KEYSIZE The key size was not a multiple of 8 bytes.
120 */
121 static int
122 compress_parity_bits(void *buffer, size_t *bufsiz)
123 {
124 unsigned char *cb, tmp;
125 int i, j, nk;
126
127 if (*bufsiz % 8 != 0)
128 return KRB5_BAD_KEYSIZE;
129 cb = (unsigned char *)buffer;
130 nk = *bufsiz / 8;
131 for (i = 0; i < nk; i++) {
132 tmp = cb[8 * i + 7] >> 1;
133 for (j = 0; j < 7; j++) {
134 cb[8 * i + j] &= 0xfe;
135 cb[8 * i + j] |= tmp & 0x1;
136 tmp >>= 1;
137 }
138 }
139 for (i = 1; i < nk; i++)
140 memmove(cb + 7 * i, cb + 8 * i, 7);
141 *bufsiz = 7 * nk;
142 return 0;
143 }
144
145 /**
146 * Derive a DES key for use with rxkad and fcrypt from a given Kerberos
147 * key of (almost) any type. This function encodes enctype-specific
148 * knowledge about how to derive a DES key from a given key type.
149 * If given a des key, use it directly; otherwise, perform any parity
150 * fixup that may be needed and pass through to the hmad-md5 bits.
151 *
152 * @param[in] enctype Kerberos enctype of the input key
153 * @param[in] keydata Input key data
154 * @param[in] keylen Size of input key data
155 * @param[out] output 8-byte buffer to hold the derived key
156 *
157 * @return Returns 0 to indicate success, or an error code.
158 *
159 * @retval KRB5_PROG_ETYPE_NOSUPP The enctype is one for which rxkad-kdf
160 * is not supported. This includes several reserved enctypes, enctype
161 * values used in PKINIT to stand for CMS algorithm identifiers, and all
162 * private-use (negative) enctypes.
163 *
164 * @retval KRB5_BAD_KEYSIZE The key size was not a multiple of 8 bytes
165 * (for 3DES key types), exactly 8 bytes (for DES key types), or at least
166 * 8 bytes (for other key types).
167 *
168 * @retval KRB5DES_WEAK_KEY Successive derivation attempts with all
169 * 255 possible counter values each produced weak DES keys. This input
170 * cannot be used to produce a usable key.
171 */
172 int
173 _kafs_derive_des_key(krb5_enctype enctype, void *keydata, size_t keylen,
174 char output[8])
175 {
176 int ret = 0;
177
178 switch ((int)enctype) {
179 case ETYPE_DES_CBC_CRC:
180 case ETYPE_DES_CBC_MD4:
181 case ETYPE_DES_CBC_MD5:
182 if (keylen != 8)
183 return KRB5_BAD_KEYSIZE;
184
185 /* Extract session key */
186 memcpy(output, keydata, 8);
187 break;
188 case ETYPE_NULL:
189 case 4:
190 case 6:
191 case 8:
192 case 9:
193 case 10:
194 case 11:
195 case 12:
196 case 13:
197 case 14:
198 case 15:
199 return KRB5_PROG_ETYPE_NOSUPP;
200 /*In order to become a "Cryptographic Key" as specified in
201 * SP800-108, it must be indistinguishable from a random bitstring. */
202 case ETYPE_DES3_CBC_MD5:
203 case ETYPE_OLD_DES3_CBC_SHA1:
204 case ETYPE_DES3_CBC_SHA1:
205 ret = compress_parity_bits(keydata, &keylen);
206 if (ret)
207 return ret;
208 /* FALLTHROUGH */
209 default:
210 if (enctype < 0)
211 return KRB5_PROG_ETYPE_NOSUPP;
212 if (keylen < 7)
213 return KRB5_BAD_KEYSIZE;
214 ret = rxkad_derive_des_key(keydata, keylen, output);
215 }
216 return ret;
217 }