]> git.saurik.com Git - apple/xnu.git/blame_incremental - libkern/crypto/corecrypto_aesxts.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / libkern / crypto / corecrypto_aesxts.c
... / ...
CommitLineData
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
55 enc = g_crypto_funcs->ccaes_xts_encrypt;
56 dec = g_crypto_funcs->ccaes_xts_decrypt;
57
58 if (!enc || !dec) {
59 panic("%s: xts mode not registered? enc=%p, dec=%p\n", __FUNCTION__, enc, dec);
60 }
61
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__);
65 }
66
67 int rc = enc->init(enc, xts->enc, keylen, key1, key2);
68 rc |= dec->init(dec, xts->dec, keylen, key1, key2);
69
70 return rc;
71}
72
73int
74xts_encrypt(const uint8_t *pt, unsigned long ptlen,
75 uint8_t *ct,
76 const uint8_t *iv, // this can be considered the sector IV for this use
77 symmetric_xts *xts)
78{
79 const struct ccmode_xts *xtsenc = g_crypto_funcs->ccaes_xts_encrypt;
80 ccxts_tweak_decl(xtsenc->tweak_size, tweak);
81
82 if (ptlen % 16) {
83 panic("xts encrypt not a multiple of block size\n");
84 }
85
86 int rc = xtsenc->set_tweak(xts->enc, tweak, iv);
87 if (rc) {
88 return rc;
89 }
90
91 xtsenc->xts(xts->enc, tweak, ptlen / 16, pt, ct);
92 return 0;
93}
94
95int
96xts_decrypt(const uint8_t *ct, unsigned long ptlen,
97 uint8_t *pt,
98 const uint8_t *iv, // this can be considered the sector IV for this use
99 symmetric_xts *xts)
100{
101 const struct ccmode_xts *xtsdec = g_crypto_funcs->ccaes_xts_decrypt;
102 ccxts_tweak_decl(xtsdec->tweak_size, tweak);
103
104 if (ptlen % 16) {
105 panic("xts decrypt not a multiple of block size\n");
106 }
107
108 int rc = xtsdec->set_tweak(xts->dec, tweak, iv);
109 if (rc) {
110 return rc;
111 }
112
113 xtsdec->xts(xts->dec, tweak, ptlen / 16, ct, pt);
114 return 0;
115}
116
117void
118xts_done(symmetric_xts *xts __unused)
119{
120 cc_clear(sizeof(xts->enc), xts->enc);
121 cc_clear(sizeof(xts->dec), xts->dec);
122}