xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/crypto-aes-sha2.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: crypto-aes-sha2.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2b9d004c6Schristos 
3b9d004c6Schristos /*
4b9d004c6Schristos  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5b9d004c6Schristos  * (Royal Institute of Technology, Stockholm, Sweden).
6b9d004c6Schristos  * All rights reserved.
7b9d004c6Schristos  *
8b9d004c6Schristos  * Redistribution and use in source and binary forms, with or without
9b9d004c6Schristos  * modification, are permitted provided that the following conditions
10b9d004c6Schristos  * are met:
11b9d004c6Schristos  *
12b9d004c6Schristos  * 1. Redistributions of source code must retain the above copyright
13b9d004c6Schristos  *    notice, this list of conditions and the following disclaimer.
14b9d004c6Schristos  *
15b9d004c6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16b9d004c6Schristos  *    notice, this list of conditions and the following disclaimer in the
17b9d004c6Schristos  *    documentation and/or other materials provided with the distribution.
18b9d004c6Schristos  *
19b9d004c6Schristos  * 3. Neither the name of the Institute nor the names of its contributors
20b9d004c6Schristos  *    may be used to endorse or promote products derived from this software
21b9d004c6Schristos  *    without specific prior written permission.
22b9d004c6Schristos  *
23b9d004c6Schristos  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24b9d004c6Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25b9d004c6Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26b9d004c6Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27b9d004c6Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28b9d004c6Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29b9d004c6Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30b9d004c6Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31b9d004c6Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32b9d004c6Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33b9d004c6Schristos  * SUCH DAMAGE.
34b9d004c6Schristos  */
35b9d004c6Schristos 
36b9d004c6Schristos #include "krb5_locl.h"
37b9d004c6Schristos 
38b9d004c6Schristos /*
39b9d004c6Schristos  * AES HMAC-SHA2
40b9d004c6Schristos  */
41b9d004c6Schristos 
42b9d004c6Schristos krb5_error_code
_krb5_aes_sha2_md_for_enctype(krb5_context context,krb5_enctype enctype,const EVP_MD ** md)43b9d004c6Schristos _krb5_aes_sha2_md_for_enctype(krb5_context context,
44b9d004c6Schristos 			      krb5_enctype enctype,
45b9d004c6Schristos 			      const EVP_MD **md)
46b9d004c6Schristos {
47b9d004c6Schristos     switch (enctype) {
48b9d004c6Schristos     case ETYPE_AES128_CTS_HMAC_SHA256_128:
49b9d004c6Schristos 	*md = EVP_sha256();
50b9d004c6Schristos 	break;
51b9d004c6Schristos     case ETYPE_AES256_CTS_HMAC_SHA384_192:
52b9d004c6Schristos 	*md = EVP_sha384();
53b9d004c6Schristos 	break;
54b9d004c6Schristos     default:
55b9d004c6Schristos 	return KRB5_PROG_ETYPE_NOSUPP;
56b9d004c6Schristos 	break;
57b9d004c6Schristos     }
58b9d004c6Schristos     return 0;
59b9d004c6Schristos }
60b9d004c6Schristos 
61b9d004c6Schristos static krb5_error_code
SP_HMAC_SHA2_checksum(krb5_context context,struct _krb5_key_data * key,const void * data,size_t len,unsigned usage,Checksum * result)62b9d004c6Schristos SP_HMAC_SHA2_checksum(krb5_context context,
63b9d004c6Schristos 		      struct _krb5_key_data *key,
64b9d004c6Schristos 		      const void *data,
65b9d004c6Schristos 		      size_t len,
66b9d004c6Schristos 		      unsigned usage,
67b9d004c6Schristos 		      Checksum *result)
68b9d004c6Schristos {
69b9d004c6Schristos     krb5_error_code ret;
70b9d004c6Schristos     const EVP_MD *md;
71b9d004c6Schristos     unsigned char hmac[EVP_MAX_MD_SIZE];
72b9d004c6Schristos     unsigned int hmaclen = sizeof(hmac);
73b9d004c6Schristos 
74b9d004c6Schristos     ret = _krb5_aes_sha2_md_for_enctype(context, key->key->keytype, &md);
75b9d004c6Schristos     if (ret)
76b9d004c6Schristos 	return ret;
77b9d004c6Schristos 
78b9d004c6Schristos     HMAC(md, key->key->keyvalue.data, key->key->keyvalue.length,
79b9d004c6Schristos 	 data, len, hmac, &hmaclen);
80b9d004c6Schristos 
81b9d004c6Schristos     heim_assert(result->checksum.length <= hmaclen, "SHA2 internal error");
82b9d004c6Schristos 
83b9d004c6Schristos     memcpy(result->checksum.data, hmac, result->checksum.length);
84b9d004c6Schristos 
85b9d004c6Schristos     return 0;
86b9d004c6Schristos }
87b9d004c6Schristos 
88b9d004c6Schristos static struct _krb5_key_type keytype_aes128_sha2 = {
89b9d004c6Schristos     KRB5_ENCTYPE_AES128_CTS_HMAC_SHA256_128,
90b9d004c6Schristos     "aes-128-sha2",
91b9d004c6Schristos     128,
92b9d004c6Schristos     16,
93b9d004c6Schristos     sizeof(struct _krb5_evp_schedule),
94b9d004c6Schristos     NULL,
95b9d004c6Schristos     _krb5_evp_schedule,
96b9d004c6Schristos     _krb5_AES_SHA2_salt,
97b9d004c6Schristos     NULL,
98b9d004c6Schristos     _krb5_evp_cleanup,
99b9d004c6Schristos     EVP_aes_128_cbc
100b9d004c6Schristos };
101b9d004c6Schristos 
102b9d004c6Schristos static struct _krb5_key_type keytype_aes256_sha2 = {
103b9d004c6Schristos     KRB5_ENCTYPE_AES256_CTS_HMAC_SHA384_192,
104b9d004c6Schristos     "aes-256-sha2",
105b9d004c6Schristos     256,
106b9d004c6Schristos     32,
107b9d004c6Schristos     sizeof(struct _krb5_evp_schedule),
108b9d004c6Schristos     NULL,
109b9d004c6Schristos     _krb5_evp_schedule,
110b9d004c6Schristos     _krb5_AES_SHA2_salt,
111b9d004c6Schristos     NULL,
112b9d004c6Schristos     _krb5_evp_cleanup,
113b9d004c6Schristos     EVP_aes_256_cbc
114b9d004c6Schristos };
115b9d004c6Schristos 
116b9d004c6Schristos struct _krb5_checksum_type _krb5_checksum_hmac_sha256_128_aes128 = {
117b9d004c6Schristos     CKSUMTYPE_HMAC_SHA256_128_AES128,
118b9d004c6Schristos     "hmac-sha256-128-aes128",
119b9d004c6Schristos     64,
120b9d004c6Schristos     16,
121b9d004c6Schristos     F_KEYED | F_CPROOF | F_DERIVED,
122b9d004c6Schristos     SP_HMAC_SHA2_checksum,
123b9d004c6Schristos     NULL
124b9d004c6Schristos };
125b9d004c6Schristos 
126b9d004c6Schristos struct _krb5_checksum_type _krb5_checksum_hmac_sha384_192_aes256 = {
127b9d004c6Schristos     CKSUMTYPE_HMAC_SHA384_192_AES256,
128b9d004c6Schristos     "hmac-sha384-192-aes256",
129b9d004c6Schristos     128,
130b9d004c6Schristos     24,
131b9d004c6Schristos     F_KEYED | F_CPROOF | F_DERIVED,
132b9d004c6Schristos     SP_HMAC_SHA2_checksum,
133b9d004c6Schristos     NULL
134b9d004c6Schristos };
135b9d004c6Schristos 
136b9d004c6Schristos static krb5_error_code
AES_SHA2_PRF(krb5_context context,krb5_crypto crypto,const krb5_data * in,krb5_data * out)137b9d004c6Schristos AES_SHA2_PRF(krb5_context context,
138b9d004c6Schristos 	     krb5_crypto crypto,
139b9d004c6Schristos 	     const krb5_data *in,
140b9d004c6Schristos 	     krb5_data *out)
141b9d004c6Schristos {
142b9d004c6Schristos     krb5_error_code ret;
143b9d004c6Schristos     krb5_data label;
144b9d004c6Schristos     const EVP_MD *md = NULL;
145b9d004c6Schristos 
146b9d004c6Schristos     ret = _krb5_aes_sha2_md_for_enctype(context, crypto->et->type, &md);
147b9d004c6Schristos     if (ret)
148b9d004c6Schristos 	return ret;
149b9d004c6Schristos 
150b9d004c6Schristos     label.data = "prf";
151b9d004c6Schristos     label.length = 3;
152b9d004c6Schristos 
153b9d004c6Schristos     ret = krb5_data_alloc(out, EVP_MD_size(md));
154b9d004c6Schristos     if (ret)
155b9d004c6Schristos 	return ret;
156b9d004c6Schristos 
157b9d004c6Schristos     ret = _krb5_SP800_108_HMAC_KDF(context, &crypto->key.key->keyvalue,
158b9d004c6Schristos 				   &label, in, md, out);
159b9d004c6Schristos 
160b9d004c6Schristos     if (ret)
161b9d004c6Schristos 	krb5_data_free(out);
162b9d004c6Schristos 
163b9d004c6Schristos     return ret;
164b9d004c6Schristos }
165b9d004c6Schristos 
166b9d004c6Schristos struct _krb5_encryption_type _krb5_enctype_aes128_cts_hmac_sha256_128 = {
167b9d004c6Schristos     ETYPE_AES128_CTS_HMAC_SHA256_128,
168b9d004c6Schristos     "aes128-cts-hmac-sha256-128",
169b9d004c6Schristos     "aes128-cts-sha256",
170b9d004c6Schristos     16,
171b9d004c6Schristos     1,
172b9d004c6Schristos     16,
173b9d004c6Schristos     &keytype_aes128_sha2,
174b9d004c6Schristos     NULL, /* should never be called */
175b9d004c6Schristos     &_krb5_checksum_hmac_sha256_128_aes128,
176b9d004c6Schristos     F_DERIVED | F_ENC_THEN_CKSUM | F_SP800_108_HMAC_KDF,
177b9d004c6Schristos     _krb5_evp_encrypt_cts,
178b9d004c6Schristos     16,
179b9d004c6Schristos     AES_SHA2_PRF
180b9d004c6Schristos };
181b9d004c6Schristos 
182b9d004c6Schristos struct _krb5_encryption_type _krb5_enctype_aes256_cts_hmac_sha384_192 = {
183b9d004c6Schristos     ETYPE_AES256_CTS_HMAC_SHA384_192,
184b9d004c6Schristos     "aes256-cts-hmac-sha384-192",
185b9d004c6Schristos     "aes256-cts-sha384",
186b9d004c6Schristos     16,
187b9d004c6Schristos     1,
188b9d004c6Schristos     16,
189b9d004c6Schristos     &keytype_aes256_sha2,
190b9d004c6Schristos     NULL, /* should never be called */
191b9d004c6Schristos     &_krb5_checksum_hmac_sha384_192_aes256,
192b9d004c6Schristos     F_DERIVED | F_ENC_THEN_CKSUM | F_SP800_108_HMAC_KDF,
193b9d004c6Schristos     _krb5_evp_encrypt_cts,
194b9d004c6Schristos     16,
195b9d004c6Schristos     AES_SHA2_PRF
196b9d004c6Schristos };
197