1*0a6a1f1dSLionel Sambuc /* $NetBSD: crypto-aes.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "krb5_locl.h"
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc /*
39ebfedea0SLionel Sambuc * AES
40ebfedea0SLionel Sambuc */
41ebfedea0SLionel Sambuc
42ebfedea0SLionel Sambuc static struct _krb5_key_type keytype_aes128 = {
43*0a6a1f1dSLionel Sambuc ENCTYPE_AES128_CTS_HMAC_SHA1_96,
44ebfedea0SLionel Sambuc "aes-128",
45ebfedea0SLionel Sambuc 128,
46ebfedea0SLionel Sambuc 16,
47ebfedea0SLionel Sambuc sizeof(struct _krb5_evp_schedule),
48ebfedea0SLionel Sambuc NULL,
49ebfedea0SLionel Sambuc _krb5_evp_schedule,
50ebfedea0SLionel Sambuc _krb5_AES_salt,
51ebfedea0SLionel Sambuc NULL,
52ebfedea0SLionel Sambuc _krb5_evp_cleanup,
53ebfedea0SLionel Sambuc EVP_aes_128_cbc
54ebfedea0SLionel Sambuc };
55ebfedea0SLionel Sambuc
56ebfedea0SLionel Sambuc static struct _krb5_key_type keytype_aes256 = {
57*0a6a1f1dSLionel Sambuc ENCTYPE_AES256_CTS_HMAC_SHA1_96,
58ebfedea0SLionel Sambuc "aes-256",
59ebfedea0SLionel Sambuc 256,
60ebfedea0SLionel Sambuc 32,
61ebfedea0SLionel Sambuc sizeof(struct _krb5_evp_schedule),
62ebfedea0SLionel Sambuc NULL,
63ebfedea0SLionel Sambuc _krb5_evp_schedule,
64ebfedea0SLionel Sambuc _krb5_AES_salt,
65ebfedea0SLionel Sambuc NULL,
66ebfedea0SLionel Sambuc _krb5_evp_cleanup,
67ebfedea0SLionel Sambuc EVP_aes_256_cbc
68ebfedea0SLionel Sambuc };
69ebfedea0SLionel Sambuc
70ebfedea0SLionel Sambuc struct _krb5_checksum_type _krb5_checksum_hmac_sha1_aes128 = {
71ebfedea0SLionel Sambuc CKSUMTYPE_HMAC_SHA1_96_AES_128,
72ebfedea0SLionel Sambuc "hmac-sha1-96-aes128",
73ebfedea0SLionel Sambuc 64,
74ebfedea0SLionel Sambuc 12,
75ebfedea0SLionel Sambuc F_KEYED | F_CPROOF | F_DERIVED,
76ebfedea0SLionel Sambuc _krb5_SP_HMAC_SHA1_checksum,
77ebfedea0SLionel Sambuc NULL
78ebfedea0SLionel Sambuc };
79ebfedea0SLionel Sambuc
80ebfedea0SLionel Sambuc struct _krb5_checksum_type _krb5_checksum_hmac_sha1_aes256 = {
81ebfedea0SLionel Sambuc CKSUMTYPE_HMAC_SHA1_96_AES_256,
82ebfedea0SLionel Sambuc "hmac-sha1-96-aes256",
83ebfedea0SLionel Sambuc 64,
84ebfedea0SLionel Sambuc 12,
85ebfedea0SLionel Sambuc F_KEYED | F_CPROOF | F_DERIVED,
86ebfedea0SLionel Sambuc _krb5_SP_HMAC_SHA1_checksum,
87ebfedea0SLionel Sambuc NULL
88ebfedea0SLionel Sambuc };
89ebfedea0SLionel Sambuc
90ebfedea0SLionel Sambuc static krb5_error_code
AES_PRF(krb5_context context,krb5_crypto crypto,const krb5_data * in,krb5_data * out)91ebfedea0SLionel Sambuc AES_PRF(krb5_context context,
92ebfedea0SLionel Sambuc krb5_crypto crypto,
93ebfedea0SLionel Sambuc const krb5_data *in,
94ebfedea0SLionel Sambuc krb5_data *out)
95ebfedea0SLionel Sambuc {
96ebfedea0SLionel Sambuc struct _krb5_checksum_type *ct = crypto->et->checksum;
97ebfedea0SLionel Sambuc krb5_error_code ret;
98ebfedea0SLionel Sambuc Checksum result;
99ebfedea0SLionel Sambuc krb5_keyblock *derived;
100ebfedea0SLionel Sambuc
101ebfedea0SLionel Sambuc result.cksumtype = ct->type;
102ebfedea0SLionel Sambuc ret = krb5_data_alloc(&result.checksum, ct->checksumsize);
103ebfedea0SLionel Sambuc if (ret) {
104ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, N_("malloc: out memory", ""));
105ebfedea0SLionel Sambuc return ret;
106ebfedea0SLionel Sambuc }
107ebfedea0SLionel Sambuc
108ebfedea0SLionel Sambuc ret = (*ct->checksum)(context, NULL, in->data, in->length, 0, &result);
109ebfedea0SLionel Sambuc if (ret) {
110ebfedea0SLionel Sambuc krb5_data_free(&result.checksum);
111ebfedea0SLionel Sambuc return ret;
112ebfedea0SLionel Sambuc }
113ebfedea0SLionel Sambuc
114ebfedea0SLionel Sambuc if (result.checksum.length < crypto->et->blocksize)
115ebfedea0SLionel Sambuc krb5_abortx(context, "internal prf error");
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc derived = NULL;
118ebfedea0SLionel Sambuc ret = krb5_derive_key(context, crypto->key.key,
119ebfedea0SLionel Sambuc crypto->et->type, "prf", 3, &derived);
120ebfedea0SLionel Sambuc if (ret)
121ebfedea0SLionel Sambuc krb5_abortx(context, "krb5_derive_key");
122ebfedea0SLionel Sambuc
123ebfedea0SLionel Sambuc ret = krb5_data_alloc(out, crypto->et->blocksize);
124ebfedea0SLionel Sambuc if (ret)
125ebfedea0SLionel Sambuc krb5_abortx(context, "malloc failed");
126ebfedea0SLionel Sambuc
127ebfedea0SLionel Sambuc {
128ebfedea0SLionel Sambuc const EVP_CIPHER *c = (*crypto->et->keytype->evp)();
129ebfedea0SLionel Sambuc EVP_CIPHER_CTX ctx;
130ebfedea0SLionel Sambuc
131ebfedea0SLionel Sambuc EVP_CIPHER_CTX_init(&ctx); /* ivec all zero */
132ebfedea0SLionel Sambuc EVP_CipherInit_ex(&ctx, c, NULL, derived->keyvalue.data, NULL, 1);
133ebfedea0SLionel Sambuc EVP_Cipher(&ctx, out->data, result.checksum.data,
134ebfedea0SLionel Sambuc crypto->et->blocksize);
135ebfedea0SLionel Sambuc EVP_CIPHER_CTX_cleanup(&ctx);
136ebfedea0SLionel Sambuc }
137ebfedea0SLionel Sambuc
138ebfedea0SLionel Sambuc krb5_data_free(&result.checksum);
139ebfedea0SLionel Sambuc krb5_free_keyblock(context, derived);
140ebfedea0SLionel Sambuc
141ebfedea0SLionel Sambuc return ret;
142ebfedea0SLionel Sambuc }
143ebfedea0SLionel Sambuc
144ebfedea0SLionel Sambuc struct _krb5_encryption_type _krb5_enctype_aes128_cts_hmac_sha1 = {
145ebfedea0SLionel Sambuc ETYPE_AES128_CTS_HMAC_SHA1_96,
146ebfedea0SLionel Sambuc "aes128-cts-hmac-sha1-96",
147ebfedea0SLionel Sambuc 16,
148ebfedea0SLionel Sambuc 1,
149ebfedea0SLionel Sambuc 16,
150ebfedea0SLionel Sambuc &keytype_aes128,
151ebfedea0SLionel Sambuc &_krb5_checksum_sha1,
152ebfedea0SLionel Sambuc &_krb5_checksum_hmac_sha1_aes128,
153ebfedea0SLionel Sambuc F_DERIVED,
154ebfedea0SLionel Sambuc _krb5_evp_encrypt_cts,
155ebfedea0SLionel Sambuc 16,
156ebfedea0SLionel Sambuc AES_PRF
157ebfedea0SLionel Sambuc };
158ebfedea0SLionel Sambuc
159ebfedea0SLionel Sambuc struct _krb5_encryption_type _krb5_enctype_aes256_cts_hmac_sha1 = {
160ebfedea0SLionel Sambuc ETYPE_AES256_CTS_HMAC_SHA1_96,
161ebfedea0SLionel Sambuc "aes256-cts-hmac-sha1-96",
162ebfedea0SLionel Sambuc 16,
163ebfedea0SLionel Sambuc 1,
164ebfedea0SLionel Sambuc 16,
165ebfedea0SLionel Sambuc &keytype_aes256,
166ebfedea0SLionel Sambuc &_krb5_checksum_sha1,
167ebfedea0SLionel Sambuc &_krb5_checksum_hmac_sha1_aes256,
168ebfedea0SLionel Sambuc F_DERIVED,
169ebfedea0SLionel Sambuc _krb5_evp_encrypt_cts,
170ebfedea0SLionel Sambuc 16,
171ebfedea0SLionel Sambuc AES_PRF
172ebfedea0SLionel Sambuc };
173