]>
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
;
52 panic("%s: corecrypto not registered!\n", __FUNCTION__
);
54 enc
= g_crypto_funcs
->ccaes_xts_encrypt
;
55 dec
= g_crypto_funcs
->ccaes_xts_decrypt
;
58 panic("%s: xts mode not registered? enc=%p, dec=%p\n", __FUNCTION__
, enc
, dec
);
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__
);
64 enc
->init(enc
, xts
->enc
, keylen
, key1
, key2
);
65 dec
->init(dec
, xts
->dec
, keylen
, key1
, key2
);
67 return 0; //never fails
70 int xts_encrypt(const uint8_t *pt
, unsigned long ptlen
,
72 const uint8_t *iv
, // this can be considered the sector IV for this use
75 const struct ccmode_xts
*xtsenc
= g_crypto_funcs
->ccaes_xts_encrypt
;
76 ccxts_tweak_decl(xtsenc
->tweak_size
, tweak
);
78 if(ptlen%16
) panic("xts encrypt not a multiple of block size\n");
80 xtsenc
->set_tweak(xts
->enc
, tweak
, iv
);
81 xtsenc
->xts(xts
->enc
, tweak
, ptlen
/16, pt
, ct
);
83 return 0; //never fails
86 int xts_decrypt(const uint8_t *ct
, unsigned long ptlen
,
88 const uint8_t *iv
, // this can be considered the sector IV for this use
91 const struct ccmode_xts
*xtsdec
= g_crypto_funcs
->ccaes_xts_decrypt
;
92 ccxts_tweak_decl(xtsdec
->tweak_size
, tweak
);
94 if(ptlen%16
) panic("xts decrypt not a multiple of block size\n");
96 xtsdec
->set_tweak(xts
->dec
, tweak
, iv
);
97 xtsdec
->xts(xts
->dec
, tweak
, ptlen
/16, ct
, pt
);
99 return 0; //never fails
102 void xts_done(symmetric_xts
*xts __unused
)