1/* Copyright (c) (2010,2011,2012,2014,2015,2019) Apple Inc. All rights reserved.
2 *
3 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4 * is contained in the License.txt file distributed with corecrypto) and only to
5 * people who accept that license. IMPORTANT: Any license rights granted to you by
6 * Apple Inc. (if any) are limited to internal use within your organization only on
7 * devices and computers you own or control, for the sole purpose of verifying the
8 * security characteristics and correct functioning of the Apple Software. You may
9 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10 */
11
12#ifndef _CORECRYPTO_CCPAD_H_
13#define _CORECRYPTO_CCPAD_H_
14
15#include <corecrypto/ccmode.h>
16
17// CTS1,2,3 are defined in Addendum to 800-38A,
18// "Cipher Modes of Operation: Three Variants of Ciphertext Stealing for CBC Mode"
19// CTS3 is also known as "CTS" in RFC3962
20
21/* Contract is nbytes is at least 1 block + 1 byte. Also in is nbytes long out is nbytes long. */
22size_t ccpad_cts1_decrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv,
23 size_t nbytes, const void *in, void *out);
24
25/* Contract is nbytes is at least 1 block + 1 byte. Also in is nbytes long out is nbytes long. */
26size_t ccpad_cts1_encrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv,
27 size_t nbytes, const void *in, void *out);
28/* Contract is nbytes is at least 1 block + 1 byte. Also in is nbytes long out is nbytes long. */
29size_t ccpad_cts2_decrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv,
30 size_t nbytes, const void *in, void *out);
31
32/* Contract is nbytes is at least 1 block + 1 byte. Also in is nbytes long out is nbytes long. */
33size_t ccpad_cts2_encrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv,
34 size_t nbytes, const void *in, void *out);
35/* Contract is nbytes is at least 1 block + 1 byte. Also in is nbytes long out is nbytes long. */
36size_t ccpad_cts3_decrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv,
37 size_t nbytes, const void *in, void *out);
38
39/* Contract is nbytes is at least 1 block + 1 byte. Also in is nbytes long out is nbytes long. */
40size_t ccpad_cts3_encrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv,
41 size_t nbytes, const void *in, void *out);
42
43/* Contract is nbytes is non zero and a multiple of block_size. Furthermore in is nbytes long and out is nbytes long. Returns number of bytes written to out (technically we always write nbytes to out but the returned value is the number of bytes decrypted after removal of padding.
44
45 To be safe we remove the entire offending block if the pkcs7 padding checks failed. However we purposely don't report the failure to decode the padding since any use of this error leads to potential security exploits. So currently there is no way to distinguish between a full block of padding and bad padding.
46 */
47size_t ccpad_pkcs7_decrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv,
48 size_t nbytes, const void *in, void *out);
49
50/* Contract is in is nbytes long. Writes (nbytes / block_size) + 1 times block_size to out. In other words, out must be nbytes rounded down to the closest multiple of block_size plus block_size bytes. */
51size_t ccpad_pkcs7_encrypt(const struct ccmode_cbc *cbc, cccbc_ctx *ctx, cccbc_iv *iv,
52 size_t nbytes, const void *in, void *out);
53
54/* Contract is 'don't break CommonCrypto functionality that allows PKCS7 padding with ECB mode'. This is basically the same routines above, without an IV, because calling
55 crypt with an IV makes ecb cry (and crash) */
56
57size_t ccpad_pkcs7_ecb_decrypt(const struct ccmode_ecb *ecb, ccecb_ctx *ecb_key,
58 size_t nbytes, const void *in, void *out);
59
60size_t ccpad_pkcs7_ecb_encrypt(const struct ccmode_ecb *ecb, ccecb_ctx *ctx,
61 size_t nbytes, const void *in, void *out);
62
63/* Function common to ccpad_pkcs7_ecb_decrypt and ccpad_pkcs7_decrypt */
64size_t ccpad_pkcs7_decode(const size_t block_size, const uint8_t* last_block);
65
66/* Contract is nbytes is at least 1 block + 1 byte. Also in is nbytes long out is nbytes long. */
67size_t ccpad_xts_decrypt(const struct ccmode_xts *xts, ccxts_ctx *ctx, ccxts_tweak *tweak,
68 size_t nbytes, const void *in, void *out);
69
70/* Contract is nbytes is at least 1 block + 1 byte. Also in is nbytes long out is nbytes long. */
71void ccpad_xts_encrypt(const struct ccmode_xts *xts, ccxts_ctx *ctx, ccxts_tweak *tweak,
72 size_t nbytes, const void *in, void *out);
73
74#endif /* _CORECRYPTO_CCPAD_H_ */
75