xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/crypto-aes.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: crypto-aes.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 #include "krb5_locl.h"
37*ebfedea0SLionel Sambuc 
38*ebfedea0SLionel Sambuc /*
39*ebfedea0SLionel Sambuc  * AES
40*ebfedea0SLionel Sambuc  */
41*ebfedea0SLionel Sambuc 
42*ebfedea0SLionel Sambuc static struct _krb5_key_type keytype_aes128 = {
43*ebfedea0SLionel Sambuc     KEYTYPE_AES128,
44*ebfedea0SLionel Sambuc     "aes-128",
45*ebfedea0SLionel Sambuc     128,
46*ebfedea0SLionel Sambuc     16,
47*ebfedea0SLionel Sambuc     sizeof(struct _krb5_evp_schedule),
48*ebfedea0SLionel Sambuc     NULL,
49*ebfedea0SLionel Sambuc     _krb5_evp_schedule,
50*ebfedea0SLionel Sambuc     _krb5_AES_salt,
51*ebfedea0SLionel Sambuc     NULL,
52*ebfedea0SLionel Sambuc     _krb5_evp_cleanup,
53*ebfedea0SLionel Sambuc     EVP_aes_128_cbc
54*ebfedea0SLionel Sambuc };
55*ebfedea0SLionel Sambuc 
56*ebfedea0SLionel Sambuc static struct _krb5_key_type keytype_aes256 = {
57*ebfedea0SLionel Sambuc     KEYTYPE_AES256,
58*ebfedea0SLionel Sambuc     "aes-256",
59*ebfedea0SLionel Sambuc     256,
60*ebfedea0SLionel Sambuc     32,
61*ebfedea0SLionel Sambuc     sizeof(struct _krb5_evp_schedule),
62*ebfedea0SLionel Sambuc     NULL,
63*ebfedea0SLionel Sambuc     _krb5_evp_schedule,
64*ebfedea0SLionel Sambuc     _krb5_AES_salt,
65*ebfedea0SLionel Sambuc     NULL,
66*ebfedea0SLionel Sambuc     _krb5_evp_cleanup,
67*ebfedea0SLionel Sambuc     EVP_aes_256_cbc
68*ebfedea0SLionel Sambuc };
69*ebfedea0SLionel Sambuc 
70*ebfedea0SLionel Sambuc struct _krb5_checksum_type _krb5_checksum_hmac_sha1_aes128 = {
71*ebfedea0SLionel Sambuc     CKSUMTYPE_HMAC_SHA1_96_AES_128,
72*ebfedea0SLionel Sambuc     "hmac-sha1-96-aes128",
73*ebfedea0SLionel Sambuc     64,
74*ebfedea0SLionel Sambuc     12,
75*ebfedea0SLionel Sambuc     F_KEYED | F_CPROOF | F_DERIVED,
76*ebfedea0SLionel Sambuc     _krb5_SP_HMAC_SHA1_checksum,
77*ebfedea0SLionel Sambuc     NULL
78*ebfedea0SLionel Sambuc };
79*ebfedea0SLionel Sambuc 
80*ebfedea0SLionel Sambuc struct _krb5_checksum_type _krb5_checksum_hmac_sha1_aes256 = {
81*ebfedea0SLionel Sambuc     CKSUMTYPE_HMAC_SHA1_96_AES_256,
82*ebfedea0SLionel Sambuc     "hmac-sha1-96-aes256",
83*ebfedea0SLionel Sambuc     64,
84*ebfedea0SLionel Sambuc     12,
85*ebfedea0SLionel Sambuc     F_KEYED | F_CPROOF | F_DERIVED,
86*ebfedea0SLionel Sambuc     _krb5_SP_HMAC_SHA1_checksum,
87*ebfedea0SLionel Sambuc     NULL
88*ebfedea0SLionel Sambuc };
89*ebfedea0SLionel Sambuc 
90*ebfedea0SLionel Sambuc static krb5_error_code
91*ebfedea0SLionel Sambuc AES_PRF(krb5_context context,
92*ebfedea0SLionel Sambuc 	krb5_crypto crypto,
93*ebfedea0SLionel Sambuc 	const krb5_data *in,
94*ebfedea0SLionel Sambuc 	krb5_data *out)
95*ebfedea0SLionel Sambuc {
96*ebfedea0SLionel Sambuc     struct _krb5_checksum_type *ct = crypto->et->checksum;
97*ebfedea0SLionel Sambuc     krb5_error_code ret;
98*ebfedea0SLionel Sambuc     Checksum result;
99*ebfedea0SLionel Sambuc     krb5_keyblock *derived;
100*ebfedea0SLionel Sambuc 
101*ebfedea0SLionel Sambuc     result.cksumtype = ct->type;
102*ebfedea0SLionel Sambuc     ret = krb5_data_alloc(&result.checksum, ct->checksumsize);
103*ebfedea0SLionel Sambuc     if (ret) {
104*ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ret, N_("malloc: out memory", ""));
105*ebfedea0SLionel Sambuc 	return ret;
106*ebfedea0SLionel Sambuc     }
107*ebfedea0SLionel Sambuc 
108*ebfedea0SLionel Sambuc     ret = (*ct->checksum)(context, NULL, in->data, in->length, 0, &result);
109*ebfedea0SLionel Sambuc     if (ret) {
110*ebfedea0SLionel Sambuc 	krb5_data_free(&result.checksum);
111*ebfedea0SLionel Sambuc 	return ret;
112*ebfedea0SLionel Sambuc     }
113*ebfedea0SLionel Sambuc 
114*ebfedea0SLionel Sambuc     if (result.checksum.length < crypto->et->blocksize)
115*ebfedea0SLionel Sambuc 	krb5_abortx(context, "internal prf error");
116*ebfedea0SLionel Sambuc 
117*ebfedea0SLionel Sambuc     derived = NULL;
118*ebfedea0SLionel Sambuc     ret = krb5_derive_key(context, crypto->key.key,
119*ebfedea0SLionel Sambuc 			  crypto->et->type, "prf", 3, &derived);
120*ebfedea0SLionel Sambuc     if (ret)
121*ebfedea0SLionel Sambuc 	krb5_abortx(context, "krb5_derive_key");
122*ebfedea0SLionel Sambuc 
123*ebfedea0SLionel Sambuc     ret = krb5_data_alloc(out, crypto->et->blocksize);
124*ebfedea0SLionel Sambuc     if (ret)
125*ebfedea0SLionel Sambuc 	krb5_abortx(context, "malloc failed");
126*ebfedea0SLionel Sambuc 
127*ebfedea0SLionel Sambuc     {
128*ebfedea0SLionel Sambuc 	const EVP_CIPHER *c = (*crypto->et->keytype->evp)();
129*ebfedea0SLionel Sambuc 	EVP_CIPHER_CTX ctx;
130*ebfedea0SLionel Sambuc 
131*ebfedea0SLionel Sambuc 	EVP_CIPHER_CTX_init(&ctx); /* ivec all zero */
132*ebfedea0SLionel Sambuc 	EVP_CipherInit_ex(&ctx, c, NULL, derived->keyvalue.data, NULL, 1);
133*ebfedea0SLionel Sambuc 	EVP_Cipher(&ctx, out->data, result.checksum.data,
134*ebfedea0SLionel Sambuc 		   crypto->et->blocksize);
135*ebfedea0SLionel Sambuc 	EVP_CIPHER_CTX_cleanup(&ctx);
136*ebfedea0SLionel Sambuc     }
137*ebfedea0SLionel Sambuc 
138*ebfedea0SLionel Sambuc     krb5_data_free(&result.checksum);
139*ebfedea0SLionel Sambuc     krb5_free_keyblock(context, derived);
140*ebfedea0SLionel Sambuc 
141*ebfedea0SLionel Sambuc     return ret;
142*ebfedea0SLionel Sambuc }
143*ebfedea0SLionel Sambuc 
144*ebfedea0SLionel Sambuc struct _krb5_encryption_type _krb5_enctype_aes128_cts_hmac_sha1 = {
145*ebfedea0SLionel Sambuc     ETYPE_AES128_CTS_HMAC_SHA1_96,
146*ebfedea0SLionel Sambuc     "aes128-cts-hmac-sha1-96",
147*ebfedea0SLionel Sambuc     16,
148*ebfedea0SLionel Sambuc     1,
149*ebfedea0SLionel Sambuc     16,
150*ebfedea0SLionel Sambuc     &keytype_aes128,
151*ebfedea0SLionel Sambuc     &_krb5_checksum_sha1,
152*ebfedea0SLionel Sambuc     &_krb5_checksum_hmac_sha1_aes128,
153*ebfedea0SLionel Sambuc     F_DERIVED,
154*ebfedea0SLionel Sambuc     _krb5_evp_encrypt_cts,
155*ebfedea0SLionel Sambuc     16,
156*ebfedea0SLionel Sambuc     AES_PRF
157*ebfedea0SLionel Sambuc };
158*ebfedea0SLionel Sambuc 
159*ebfedea0SLionel Sambuc struct _krb5_encryption_type _krb5_enctype_aes256_cts_hmac_sha1 = {
160*ebfedea0SLionel Sambuc     ETYPE_AES256_CTS_HMAC_SHA1_96,
161*ebfedea0SLionel Sambuc     "aes256-cts-hmac-sha1-96",
162*ebfedea0SLionel Sambuc     16,
163*ebfedea0SLionel Sambuc     1,
164*ebfedea0SLionel Sambuc     16,
165*ebfedea0SLionel Sambuc     &keytype_aes256,
166*ebfedea0SLionel Sambuc     &_krb5_checksum_sha1,
167*ebfedea0SLionel Sambuc     &_krb5_checksum_hmac_sha1_aes256,
168*ebfedea0SLionel Sambuc     F_DERIVED,
169*ebfedea0SLionel Sambuc     _krb5_evp_encrypt_cts,
170*ebfedea0SLionel Sambuc     16,
171*ebfedea0SLionel Sambuc     AES_PRF
172*ebfedea0SLionel Sambuc };
173