]> git.saurik.com Git - apple/xnu.git/blame - libkern/crypto/corecrypto_aesxts.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / libkern / crypto / corecrypto_aesxts.c
CommitLineData
316670eb
A
1/*
2 * Copyright (c) 2012 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
316670eb
A
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.
0a7de745 14 *
316670eb
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
316670eb
A
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.
0a7de745 25 *
316670eb
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
0a7de745 28
316670eb
A
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
0a7de745
A
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)
316670eb 48{
0a7de745
A
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
f427ee49 58 if (!enc || !dec) {
0a7de745
A
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
cb323159
A
67 int rc = enc->init(enc, xts->enc, keylen, key1, key2);
68 rc |= dec->init(dec, xts->dec, keylen, key1, key2);
0a7de745 69
cb323159 70 return rc;
316670eb
A
71}
72
0a7de745
A
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)
316670eb
A
78{
79 const struct ccmode_xts *xtsenc = g_crypto_funcs->ccaes_xts_encrypt;
80 ccxts_tweak_decl(xtsenc->tweak_size, tweak);
0a7de745
A
81
82 if (ptlen % 16) {
83 panic("xts encrypt not a multiple of block size\n");
84 }
316670eb 85
cb323159
A
86 int rc = xtsenc->set_tweak(xts->enc, tweak, iv);
87 if (rc) {
88 return rc;
89 }
0a7de745 90
cb323159
A
91 xtsenc->xts(xts->enc, tweak, ptlen / 16, pt, ct);
92 return 0;
316670eb
A
93}
94
0a7de745
A
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)
316670eb
A
100{
101 const struct ccmode_xts *xtsdec = g_crypto_funcs->ccaes_xts_decrypt;
102 ccxts_tweak_decl(xtsdec->tweak_size, tweak);
103
0a7de745
A
104 if (ptlen % 16) {
105 panic("xts decrypt not a multiple of block size\n");
106 }
316670eb 107
cb323159
A
108 int rc = xtsdec->set_tweak(xts->dec, tweak, iv);
109 if (rc) {
110 return rc;
111 }
316670eb 112
cb323159
A
113 xtsdec->xts(xts->dec, tweak, ptlen / 16, ct, pt);
114 return 0;
316670eb
A
115}
116
0a7de745
A
117void
118xts_done(symmetric_xts *xts __unused)
316670eb 119{
3e170ce0
A
120 cc_clear(sizeof(xts->enc), xts->enc);
121 cc_clear(sizeof(xts->dec), xts->dec);
316670eb 122}