1/*
2 * Copyright (c) 2012 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <libkern/crypto/crypto_internal.h>
30#include <libkern/libkern.h>
31#include <libkern/crypto/aesxts.h>
32#include <corecrypto/ccmode.h>
33#include <corecrypto/ccpad.h>
34#include <kern/debug.h>
35
36/*
37 * These are the interfaces required for XTS-AES support
38 */
39
40uint32_t
41xts_start(uint32_t cipher __unused, // ignored - we're doing this for xts-aes only
42		  const uint8_t *IV __unused, // ignored
43		  const uint8_t *key1, int keylen,
44		  const uint8_t *key2, int tweaklen __unused, // both keys are the same size for xts
45		  uint32_t num_rounds __unused, // ignored
46		  uint32_t options __unused,    // ignored
47		  symmetric_xts *xts)
48{
49		const struct ccmode_xts *enc, *dec;
50
51		if(!g_crypto_funcs)
52			panic("%s: corecrypto not registered!\n", __FUNCTION__);
53
54		enc = g_crypto_funcs->ccaes_xts_encrypt;
55		dec = g_crypto_funcs->ccaes_xts_decrypt;
56
57		if(!enc && !dec)
58			panic("%s: xts mode not registered? enc=%p, dec=%p\n", __FUNCTION__, enc, dec);
59
60		/* Make sure the context size for the mode fits in the one we have */
61		if((enc->size>sizeof(xts->enc)) || (dec->size>sizeof(xts->dec)))
62			panic("%s: inconsistent size for AES-XTS context", __FUNCTION__);
63
64		enc->init(enc, xts->enc, keylen, key1, key2);
65		dec->init(dec, xts->dec, keylen, key1, key2);
66
67		return 0; //never fails
68}
69
70int xts_encrypt(const uint8_t *pt, unsigned long ptlen,
71			uint8_t *ct,
72			const uint8_t *iv, // this can be considered the sector IV for this use
73			symmetric_xts *xts)
74{
75	const struct ccmode_xts *xtsenc = g_crypto_funcs->ccaes_xts_encrypt;
76	ccxts_tweak_decl(xtsenc->tweak_size, tweak);
77
78	if(ptlen%16) panic("xts encrypt not a multiple of block size\n");
79
80	xtsenc->set_tweak(xts->enc, tweak, iv);
81	xtsenc->xts(xts->enc, tweak, ptlen/16, pt, ct);
82
83	return 0; //never fails
84}
85
86int xts_decrypt(const uint8_t *ct, unsigned long ptlen,
87			uint8_t *pt,
88				const uint8_t *iv, // this can be considered the sector IV for this use
89			symmetric_xts *xts)
90{
91	const struct ccmode_xts *xtsdec = g_crypto_funcs->ccaes_xts_decrypt;
92	ccxts_tweak_decl(xtsdec->tweak_size, tweak);
93
94	if(ptlen%16) panic("xts decrypt not a multiple of block size\n");
95
96	xtsdec->set_tweak(xts->dec, tweak, iv);
97	xtsdec->xts(xts->dec, tweak, ptlen/16, ct, pt);
98
99	return 0; //never fails
100}
101
102void xts_done(symmetric_xts *xts __unused)
103{
104
105}
106