]>
git.saurik.com Git - apple/xnu.git/blob - libkern/crypto/corecrypto_aesxts.c
2 * Copyright (c) 2012 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
37 * These are the interfaces required for XTS-AES support
41 xts_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
49 const struct ccmode_xts
*enc
, *dec
;
51 if (!g_crypto_funcs
) {
52 panic("%s: corecrypto not registered!\n", __FUNCTION__
);
55 enc
= g_crypto_funcs
->ccaes_xts_encrypt
;
56 dec
= g_crypto_funcs
->ccaes_xts_decrypt
;
59 panic("%s: xts mode not registered? enc=%p, dec=%p\n", __FUNCTION__
, enc
, dec
);
62 /* Make sure the context size for the mode fits in the one we have */
63 if ((enc
->size
> sizeof(xts
->enc
)) || (dec
->size
> sizeof(xts
->dec
))) {
64 panic("%s: inconsistent size for AES-XTS context", __FUNCTION__
);
67 int rc
= enc
->init(enc
, xts
->enc
, keylen
, key1
, key2
);
68 rc
|= dec
->init(dec
, xts
->dec
, keylen
, key1
, key2
);
74 xts_encrypt(const uint8_t *pt
, unsigned long ptlen
,
76 const uint8_t *iv
, // this can be considered the sector IV for this use
79 const struct ccmode_xts
*xtsenc
= g_crypto_funcs
->ccaes_xts_encrypt
;
80 ccxts_tweak_decl(xtsenc
->tweak_size
, tweak
);
83 panic("xts encrypt not a multiple of block size\n");
86 int rc
= xtsenc
->set_tweak(xts
->enc
, tweak
, iv
);
91 xtsenc
->xts(xts
->enc
, tweak
, ptlen
/ 16, pt
, ct
);
96 xts_decrypt(const uint8_t *ct
, unsigned long ptlen
,
98 const uint8_t *iv
, // this can be considered the sector IV for this use
101 const struct ccmode_xts
*xtsdec
= g_crypto_funcs
->ccaes_xts_decrypt
;
102 ccxts_tweak_decl(xtsdec
->tweak_size
, tweak
);
105 panic("xts decrypt not a multiple of block size\n");
108 int rc
= xtsdec
->set_tweak(xts
->dec
, tweak
, iv
);
113 xtsdec
->xts(xts
->dec
, tweak
, ptlen
/ 16, ct
, pt
);
118 xts_done(symmetric_xts
*xts __unused
)
120 cc_clear(sizeof(xts
->enc
), xts
->enc
);
121 cc_clear(sizeof(xts
->dec
), xts
->dec
);