xref: /xnu-10002.1.13/osfmk/corecrypto/cchmac_init.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /* Copyright (c) (2010,2011,2015,2016,2018,2019) Apple Inc. All rights reserved.
2*1031c584SApple OSS Distributions  *
3*1031c584SApple OSS Distributions  * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4*1031c584SApple OSS Distributions  * is contained in the License.txt file distributed with corecrypto) and only to
5*1031c584SApple OSS Distributions  * people who accept that license. IMPORTANT:  Any license rights granted to you by
6*1031c584SApple OSS Distributions  * Apple Inc. (if any) are limited to internal use within your organization only on
7*1031c584SApple OSS Distributions  * devices and computers you own or control, for the sole purpose of verifying the
8*1031c584SApple OSS Distributions  * security characteristics and correct functioning of the Apple Software.  You may
9*1031c584SApple OSS Distributions  * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10*1031c584SApple OSS Distributions  *
11*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
12*1031c584SApple OSS Distributions  *
13*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
14*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
15*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
16*1031c584SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
17*1031c584SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
18*1031c584SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
19*1031c584SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
20*1031c584SApple OSS Distributions  * terms of an Apple operating system software license agreement.
21*1031c584SApple OSS Distributions  *
22*1031c584SApple OSS Distributions  * Please obtain a copy of the License at
23*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
24*1031c584SApple OSS Distributions  *
25*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
26*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
27*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
28*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
29*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
30*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
31*1031c584SApple OSS Distributions  * limitations under the License.
32*1031c584SApple OSS Distributions  *
33*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
34*1031c584SApple OSS Distributions  */
35*1031c584SApple OSS Distributions 
36*1031c584SApple OSS Distributions #include "cc_internal.h"
37*1031c584SApple OSS Distributions #include <corecrypto/ccdigest_priv.h>
38*1031c584SApple OSS Distributions #include <corecrypto/cchmac.h>
39*1031c584SApple OSS Distributions #include <corecrypto/ccn.h>
40*1031c584SApple OSS Distributions #include <corecrypto/cc_priv.h>
41*1031c584SApple OSS Distributions 
42*1031c584SApple OSS Distributions /* The HMAC_<DIG> transform looks like:
43*1031c584SApple OSS Distributions  *  <DIG> (K XOR opad || <DIG> (K XOR ipad || text))
44*1031c584SApple OSS Distributions  *  Where K is a n byte key
45*1031c584SApple OSS Distributions  *  ipad is the byte 0x36 repeated 64 times.
46*1031c584SApple OSS Distributions  *  opad is the byte 0x5c repeated 64 times.
47*1031c584SApple OSS Distributions  *  text is the data being protected.
48*1031c584SApple OSS Distributions  */
49*1031c584SApple OSS Distributions void
cchmac_init(const struct ccdigest_info * di,cchmac_ctx_t hc,size_t key_len,const void * key_data)50*1031c584SApple OSS Distributions cchmac_init(const struct ccdigest_info *di, cchmac_ctx_t hc,
51*1031c584SApple OSS Distributions     size_t key_len, const void *key_data)
52*1031c584SApple OSS Distributions {
53*1031c584SApple OSS Distributions 	CC_ENSURE_DIT_ENABLED
54*1031c584SApple OSS Distributions 
55*1031c584SApple OSS Distributions 	const unsigned char *key = key_data;
56*1031c584SApple OSS Distributions 
57*1031c584SApple OSS Distributions 	/* Set cchmac_data(di, hc) to key ^ opad. */
58*1031c584SApple OSS Distributions 	size_t byte = 0;
59*1031c584SApple OSS Distributions 	if (key_len <= di->block_size) {
60*1031c584SApple OSS Distributions 		for (; byte < key_len; ++byte) {
61*1031c584SApple OSS Distributions 			cchmac_data(di, hc)[byte] = key[byte] ^ 0x5c;
62*1031c584SApple OSS Distributions 		}
63*1031c584SApple OSS Distributions 	} else {
64*1031c584SApple OSS Distributions 		/* Key is longer than di->block size, reset it to key=digest(key) */
65*1031c584SApple OSS Distributions 		ccdigest_init(di, cchmac_digest_ctx(di, hc));
66*1031c584SApple OSS Distributions 		ccdigest_update(di, cchmac_digest_ctx(di, hc), key_len, key);
67*1031c584SApple OSS Distributions 		ccdigest_final(di, cchmac_digest_ctx(di, hc), cchmac_data(di, hc));
68*1031c584SApple OSS Distributions 		key_len = di->output_size;
69*1031c584SApple OSS Distributions 		for (; byte < key_len; ++byte) {
70*1031c584SApple OSS Distributions 			cchmac_data(di, hc)[byte] ^= 0x5c;
71*1031c584SApple OSS Distributions 		}
72*1031c584SApple OSS Distributions 	}
73*1031c584SApple OSS Distributions 	/* Fill remainder of cchmac_data(di, hc) with opad. */
74*1031c584SApple OSS Distributions 	if (key_len < di->block_size) {
75*1031c584SApple OSS Distributions 		cc_memset(cchmac_data(di, hc) + key_len, 0x5c, di->block_size - key_len);
76*1031c584SApple OSS Distributions 	}
77*1031c584SApple OSS Distributions 
78*1031c584SApple OSS Distributions 	/* Set cchmac_ostate32(di, hc) to the state of the first round of the
79*1031c584SApple OSS Distributions 	 *  outer digest. */
80*1031c584SApple OSS Distributions 	ccdigest_copy_state(di, cchmac_ostate32(di, hc), di->initial_state);
81*1031c584SApple OSS Distributions 	di->compress(cchmac_ostate(di, hc), 1, cchmac_data(di, hc));
82*1031c584SApple OSS Distributions 
83*1031c584SApple OSS Distributions 	/* Set cchmac_data(di, hc) to key ^ ipad. */
84*1031c584SApple OSS Distributions 	for (byte = 0; byte < di->block_size; ++byte) {
85*1031c584SApple OSS Distributions 		cchmac_data(di, hc)[byte] ^= (0x5c ^ 0x36);
86*1031c584SApple OSS Distributions 	}
87*1031c584SApple OSS Distributions 	ccdigest_copy_state(di, cchmac_istate32(di, hc), di->initial_state);
88*1031c584SApple OSS Distributions 	di->compress(cchmac_istate(di, hc), 1, cchmac_data(di, hc));
89*1031c584SApple OSS Distributions 	cchmac_num(di, hc) = 0;
90*1031c584SApple OSS Distributions 	cchmac_nbits(di, hc) = di->block_size * 8;
91*1031c584SApple OSS Distributions }
92