1*2c2f96dcSApple OSS Distributions /* Copyright (c) (2017-2019,2021,2022) Apple Inc. All rights reserved.
2*2c2f96dcSApple OSS Distributions *
3*2c2f96dcSApple OSS Distributions * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4*2c2f96dcSApple OSS Distributions * is contained in the License.txt file distributed with corecrypto) and only to
5*2c2f96dcSApple OSS Distributions * people who accept that license. IMPORTANT: Any license rights granted to you by
6*2c2f96dcSApple OSS Distributions * Apple Inc. (if any) are limited to internal use within your organization only on
7*2c2f96dcSApple OSS Distributions * devices and computers you own or control, for the sole purpose of verifying the
8*2c2f96dcSApple OSS Distributions * security characteristics and correct functioning of the Apple Software. You may
9*2c2f96dcSApple OSS Distributions * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10*2c2f96dcSApple OSS Distributions *
11*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
12*2c2f96dcSApple OSS Distributions *
13*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
14*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
15*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
16*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
17*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
18*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
19*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
20*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
21*2c2f96dcSApple OSS Distributions *
22*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
23*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
24*2c2f96dcSApple OSS Distributions *
25*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
26*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
27*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
28*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
29*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
30*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
31*2c2f96dcSApple OSS Distributions * limitations under the License.
32*2c2f96dcSApple OSS Distributions *
33*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
34*2c2f96dcSApple OSS Distributions */
35*2c2f96dcSApple OSS Distributions
36*2c2f96dcSApple OSS Distributions #include "cc_config.h"
37*2c2f96dcSApple OSS Distributions #include "cc_internal.h"
38*2c2f96dcSApple OSS Distributions #include "cc_macros.h"
39*2c2f96dcSApple OSS Distributions #include "fipspost_trace.h"
40*2c2f96dcSApple OSS Distributions #include <corecrypto/ccmode.h>
41*2c2f96dcSApple OSS Distributions
42*2c2f96dcSApple OSS Distributions size_t
cccbc_context_size(const struct ccmode_cbc * mode)43*2c2f96dcSApple OSS Distributions cccbc_context_size(const struct ccmode_cbc *mode)
44*2c2f96dcSApple OSS Distributions {
45*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
46*2c2f96dcSApple OSS Distributions
47*2c2f96dcSApple OSS Distributions return mode->size;
48*2c2f96dcSApple OSS Distributions }
49*2c2f96dcSApple OSS Distributions
50*2c2f96dcSApple OSS Distributions size_t
cccbc_block_size(const struct ccmode_cbc * mode)51*2c2f96dcSApple OSS Distributions cccbc_block_size(const struct ccmode_cbc *mode)
52*2c2f96dcSApple OSS Distributions {
53*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
54*2c2f96dcSApple OSS Distributions
55*2c2f96dcSApple OSS Distributions return mode->block_size;
56*2c2f96dcSApple OSS Distributions }
57*2c2f96dcSApple OSS Distributions
58*2c2f96dcSApple OSS Distributions int
cccbc_init(const struct ccmode_cbc * mode,cccbc_ctx * ctx,size_t key_len,const void * cc_sized_by (key_len)key)59*2c2f96dcSApple OSS Distributions cccbc_init(const struct ccmode_cbc *mode,
60*2c2f96dcSApple OSS Distributions cccbc_ctx *ctx,
61*2c2f96dcSApple OSS Distributions size_t key_len,
62*2c2f96dcSApple OSS Distributions const void *cc_sized_by(key_len)key)
63*2c2f96dcSApple OSS Distributions {
64*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
65*2c2f96dcSApple OSS Distributions
66*2c2f96dcSApple OSS Distributions return mode->init(mode, ctx, key_len, key);
67*2c2f96dcSApple OSS Distributions }
68*2c2f96dcSApple OSS Distributions
69*2c2f96dcSApple OSS Distributions int
cccbc_copy_iv(cccbc_iv * cc_sized_by (len)iv_ctx,const void * cc_sized_by (len)iv,size_t len)70*2c2f96dcSApple OSS Distributions cccbc_copy_iv(cccbc_iv *cc_sized_by(len)iv_ctx,
71*2c2f96dcSApple OSS Distributions const void *cc_sized_by(len)iv,
72*2c2f96dcSApple OSS Distributions size_t len)
73*2c2f96dcSApple OSS Distributions {
74*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
75*2c2f96dcSApple OSS Distributions
76*2c2f96dcSApple OSS Distributions #if CC_IBOOT
77*2c2f96dcSApple OSS Distributions // Currently ptrcheck in iboot doesn't understand the above annotations
78*2c2f96dcSApple OSS Distributions // and fails when we use cc_memcpy. A future version of ptrcheck will
79*2c2f96dcSApple OSS Distributions // fix this issue. See rdar://79987676
80*2c2f96dcSApple OSS Distributions memcpy(iv_ctx, iv, len);
81*2c2f96dcSApple OSS Distributions #else
82*2c2f96dcSApple OSS Distributions cc_memcpy(iv_ctx, iv, len);
83*2c2f96dcSApple OSS Distributions #endif
84*2c2f96dcSApple OSS Distributions return 0;
85*2c2f96dcSApple OSS Distributions }
86*2c2f96dcSApple OSS Distributions
87*2c2f96dcSApple OSS Distributions int
cccbc_clear_iv(cccbc_iv * cc_sized_by (len)iv_ctx,size_t len)88*2c2f96dcSApple OSS Distributions cccbc_clear_iv(cccbc_iv *cc_sized_by(len)iv_ctx, size_t len)
89*2c2f96dcSApple OSS Distributions {
90*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
91*2c2f96dcSApple OSS Distributions
92*2c2f96dcSApple OSS Distributions cc_clear(len, iv_ctx);
93*2c2f96dcSApple OSS Distributions return 0;
94*2c2f96dcSApple OSS Distributions }
95*2c2f96dcSApple OSS Distributions
96*2c2f96dcSApple OSS Distributions int
cccbc_set_iv(const struct ccmode_cbc * mode,cccbc_iv * iv_ctx,const void * iv)97*2c2f96dcSApple OSS Distributions cccbc_set_iv(const struct ccmode_cbc *mode, cccbc_iv *iv_ctx, const void *iv)
98*2c2f96dcSApple OSS Distributions {
99*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
100*2c2f96dcSApple OSS Distributions
101*2c2f96dcSApple OSS Distributions if (iv) {
102*2c2f96dcSApple OSS Distributions return cccbc_copy_iv(iv_ctx, iv, mode->block_size);
103*2c2f96dcSApple OSS Distributions }
104*2c2f96dcSApple OSS Distributions
105*2c2f96dcSApple OSS Distributions return cccbc_clear_iv(iv_ctx, mode->block_size);
106*2c2f96dcSApple OSS Distributions }
107*2c2f96dcSApple OSS Distributions
108*2c2f96dcSApple OSS Distributions int
cccbc_update(const struct ccmode_cbc * mode,const cccbc_ctx * ctx,cccbc_iv * iv,size_t nblocks,const void * cc_indexable in,void * cc_indexable out)109*2c2f96dcSApple OSS Distributions cccbc_update(const struct ccmode_cbc *mode,
110*2c2f96dcSApple OSS Distributions const cccbc_ctx *ctx,
111*2c2f96dcSApple OSS Distributions cccbc_iv *iv,
112*2c2f96dcSApple OSS Distributions size_t nblocks,
113*2c2f96dcSApple OSS Distributions const void *cc_indexable in,
114*2c2f96dcSApple OSS Distributions void *cc_indexable out)
115*2c2f96dcSApple OSS Distributions {
116*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
117*2c2f96dcSApple OSS Distributions
118*2c2f96dcSApple OSS Distributions return mode->cbc(ctx, iv, nblocks, in, out);
119*2c2f96dcSApple OSS Distributions }
120*2c2f96dcSApple OSS Distributions
121*2c2f96dcSApple OSS Distributions int
cccbc_one_shot(const struct ccmode_cbc * mode,size_t key_len,const void * cc_sized_by (key_len)key,const void * iv,size_t nblocks,const void * in,void * out)122*2c2f96dcSApple OSS Distributions cccbc_one_shot(const struct ccmode_cbc *mode,
123*2c2f96dcSApple OSS Distributions size_t key_len,
124*2c2f96dcSApple OSS Distributions const void *cc_sized_by(key_len)key,
125*2c2f96dcSApple OSS Distributions const void *iv,
126*2c2f96dcSApple OSS Distributions size_t nblocks,
127*2c2f96dcSApple OSS Distributions const void *in,
128*2c2f96dcSApple OSS Distributions void *out)
129*2c2f96dcSApple OSS Distributions {
130*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
131*2c2f96dcSApple OSS Distributions
132*2c2f96dcSApple OSS Distributions FIPSPOST_TRACE_EVENT;
133*2c2f96dcSApple OSS Distributions
134*2c2f96dcSApple OSS Distributions size_t iv_len = 0;
135*2c2f96dcSApple OSS Distributions if (iv) {
136*2c2f96dcSApple OSS Distributions iv_len = mode->block_size;
137*2c2f96dcSApple OSS Distributions }
138*2c2f96dcSApple OSS Distributions
139*2c2f96dcSApple OSS Distributions return cccbc_one_shot_explicit(mode,
140*2c2f96dcSApple OSS Distributions key_len,
141*2c2f96dcSApple OSS Distributions iv_len,
142*2c2f96dcSApple OSS Distributions mode->block_size,
143*2c2f96dcSApple OSS Distributions nblocks,
144*2c2f96dcSApple OSS Distributions key,
145*2c2f96dcSApple OSS Distributions cc_unsafe_forge_bidi_indexable(iv, iv_len),
146*2c2f96dcSApple OSS Distributions cc_unsafe_forge_bidi_indexable(in, mode->block_size * nblocks),
147*2c2f96dcSApple OSS Distributions cc_unsafe_forge_bidi_indexable(out, mode->block_size * nblocks));
148*2c2f96dcSApple OSS Distributions }
149*2c2f96dcSApple OSS Distributions
150*2c2f96dcSApple OSS Distributions int
cccbc_one_shot_explicit(const struct ccmode_cbc * mode,size_t key_len,size_t iv_len,size_t block_size,size_t nblocks,const void * cc_sized_by (key_len)key,const void * cc_sized_by (iv_len)iv,const void * cc_sized_by (block_size * nblocks)in,void * cc_sized_by (block_size * nblocks)out)151*2c2f96dcSApple OSS Distributions cccbc_one_shot_explicit(const struct ccmode_cbc *mode,
152*2c2f96dcSApple OSS Distributions size_t key_len,
153*2c2f96dcSApple OSS Distributions size_t iv_len,
154*2c2f96dcSApple OSS Distributions size_t block_size,
155*2c2f96dcSApple OSS Distributions size_t nblocks,
156*2c2f96dcSApple OSS Distributions const void *cc_sized_by(key_len)key,
157*2c2f96dcSApple OSS Distributions const void *cc_sized_by(iv_len)iv,
158*2c2f96dcSApple OSS Distributions const void *cc_sized_by(block_size * nblocks)in,
159*2c2f96dcSApple OSS Distributions void *cc_sized_by(block_size * nblocks)out)
160*2c2f96dcSApple OSS Distributions {
161*2c2f96dcSApple OSS Distributions CC_ENSURE_DIT_ENABLED
162*2c2f96dcSApple OSS Distributions
163*2c2f96dcSApple OSS Distributions FIPSPOST_TRACE_EVENT;
164*2c2f96dcSApple OSS Distributions
165*2c2f96dcSApple OSS Distributions // iv_len must be either equal to block_size, or 0 if the iv is NULL.
166*2c2f96dcSApple OSS Distributions // Once __sized_by_or_null is available, we can get rid of iv_len and use
167*2c2f96dcSApple OSS Distributions // cc_sized_by_or_null(block_size) to annotate the length of iv instead.
168*2c2f96dcSApple OSS Distributions if (block_size != mode->block_size || (iv_len != block_size && iv_len != 0)) {
169*2c2f96dcSApple OSS Distributions return CCERR_PARAMETER; /* Invalid input size */
170*2c2f96dcSApple OSS Distributions }
171*2c2f96dcSApple OSS Distributions
172*2c2f96dcSApple OSS Distributions int rc;
173*2c2f96dcSApple OSS Distributions cccbc_ctx_decl(mode->size, ctx);
174*2c2f96dcSApple OSS Distributions cccbc_iv_decl(mode->block_size, iv_ctx);
175*2c2f96dcSApple OSS Distributions rc = mode->init(mode, ctx, key_len, key);
176*2c2f96dcSApple OSS Distributions cc_require_or_return(rc == CCERR_OK, rc);
177*2c2f96dcSApple OSS Distributions if (iv) {
178*2c2f96dcSApple OSS Distributions cc_memcpy(iv_ctx, iv, mode->block_size);
179*2c2f96dcSApple OSS Distributions } else {
180*2c2f96dcSApple OSS Distributions cc_clear(mode->block_size, iv_ctx);
181*2c2f96dcSApple OSS Distributions }
182*2c2f96dcSApple OSS Distributions rc = mode->cbc(ctx, iv_ctx, nblocks, in, out);
183*2c2f96dcSApple OSS Distributions cccbc_ctx_clear(mode->size, ctx);
184*2c2f96dcSApple OSS Distributions return rc;
185*2c2f96dcSApple OSS Distributions }
186