]>
git.saurik.com Git - apple/xnu.git/blob - libkern/crypto/corecrypto_des.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@
30 #include <libkern/crypto/crypto_internal.h>
31 #include <libkern/libkern.h>
32 #include <kern/debug.h>
33 #include <libkern/crypto/des.h>
34 #include <corecrypto/ccmode.h>
36 /* Single DES ECB - used by ipv6 (esp_core.c) */
37 int des_ecb_key_sched(des_cblock
*key
, des_ecb_key_schedule
*ks
)
39 const struct ccmode_ecb
*enc
= g_crypto_funcs
->ccdes_ecb_encrypt
;
40 const struct ccmode_ecb
*dec
= g_crypto_funcs
->ccdes_ecb_decrypt
;
42 /* Make sure the context size for the mode fits in the one we have */
43 if((enc
->size
>sizeof(ks
->enc
)) || (dec
->size
>sizeof(ks
->dec
)))
44 panic("%s: inconsistent size for DES-ECB context", __FUNCTION__
);
46 enc
->init(enc
, ks
->enc
, CCDES_KEY_SIZE
, key
);
47 dec
->init(dec
, ks
->dec
, CCDES_KEY_SIZE
, key
);
49 /* The old DES interface could return -1 or -2 for weak keys and wrong parity,
50 but this was disabled all the time, so we never fail here */
54 /* Simple des - 1 block */
55 void des_ecb_encrypt(des_cblock
*in
, des_cblock
*out
, des_ecb_key_schedule
*ks
, int enc
)
57 const struct ccmode_ecb
*ecb
= enc
? g_crypto_funcs
->ccdes_ecb_encrypt
: g_crypto_funcs
->ccdes_ecb_decrypt
;
58 ccecb_ctx
*ctx
= enc
? ks
->enc
: ks
->dec
;
60 ecb
->ecb(ctx
, 1, in
, out
);
64 /* Triple DES ECB - used by ipv6 (esp_core.c) */
65 int des3_ecb_key_sched(des_cblock
*key
, des3_ecb_key_schedule
*ks
)
67 const struct ccmode_ecb
*enc
= g_crypto_funcs
->cctdes_ecb_encrypt
;
68 const struct ccmode_ecb
*dec
= g_crypto_funcs
->cctdes_ecb_decrypt
;
70 /* Make sure the context size for the mode fits in the one we have */
71 if((enc
->size
>sizeof(ks
->enc
)) || (dec
->size
>sizeof(ks
->dec
)))
72 panic("%s: inconsistent size for 3DES-ECB context", __FUNCTION__
);
74 enc
->init(enc
, ks
->enc
, CCDES_KEY_SIZE
*3, key
);
75 dec
->init(dec
, ks
->dec
, CCDES_KEY_SIZE
*3, key
);
77 /* The old DES interface could return -1 or -2 for weak keys and wrong parity,
78 but this was disabled all the time, so we never fail here */
82 /* Simple des - 1 block */
83 void des3_ecb_encrypt(des_cblock
*in
, des_cblock
*out
, des3_ecb_key_schedule
*ks
, int enc
)
85 const struct ccmode_ecb
*ecb
= enc
? g_crypto_funcs
->cctdes_ecb_encrypt
: g_crypto_funcs
->cctdes_ecb_decrypt
;
86 ccecb_ctx
*ctx
= enc
? ks
->enc
: ks
->dec
;
88 ecb
->ecb(ctx
, 1, in
, out
);
91 /* Single DES CBC - used by nfs_gss */
92 int des_cbc_key_sched(des_cblock
*key
, des_cbc_key_schedule
*ks
)
94 const struct ccmode_cbc
*enc
= g_crypto_funcs
->ccdes_cbc_encrypt
;
95 const struct ccmode_cbc
*dec
= g_crypto_funcs
->ccdes_cbc_decrypt
;
97 /* Make sure the context size for the mode fits in the one we have */
98 if((enc
->size
>sizeof(ks
->enc
)) || (dec
->size
>sizeof(ks
->dec
)))
99 panic("%s: inconsistent size for DES-CBC context", __FUNCTION__
);
102 cccbc_init(enc
, ks
->enc
, CCDES_KEY_SIZE
, key
);
103 cccbc_init(dec
, ks
->dec
, CCDES_KEY_SIZE
, key
);
105 /* The old DES interface could return -1 or -2 for weak keys and wrong parity,
106 but this was disabled all the time, so we never fail here */
110 /* this is normally only called with length an 8 bytes multiple */
112 des_cbc_encrypt(des_cblock
*in
, des_cblock
*out
, int32_t length
,
113 des_cbc_key_schedule
*ks
, des_cblock
*iv
, des_cblock
*retiv
, int encrypt
)
115 const struct ccmode_cbc
*cbc
= encrypt
?g_crypto_funcs
->ccdes_cbc_encrypt
:g_crypto_funcs
->ccdes_cbc_decrypt
;
116 cccbc_ctx
*ctx
= encrypt
? ks
->enc
: ks
->dec
;
118 cccbc_iv_decl(cbc
->block_size
, ctx_iv
);
124 cccbc_set_iv(cbc
, ctx_iv
, iv
);
126 cccbc_update(cbc
, ctx
, ctx_iv
, nblocks
, in
, out
);
130 memcpy(retiv
, ctx_iv
, 8);
133 /* Triple DES CBC - used by nfs_gss */
134 int des3_cbc_key_sched(des_cblock
*key
, des3_cbc_key_schedule
*ks
)
136 const struct ccmode_cbc
*enc
= g_crypto_funcs
->cctdes_cbc_encrypt
;
137 const struct ccmode_cbc
*dec
= g_crypto_funcs
->cctdes_cbc_decrypt
;
139 /* Make sure the context size for the mode fits in the one we have */
140 if((enc
->size
>sizeof(ks
->enc
)) || (dec
->size
>sizeof(ks
->dec
)))
141 panic("%s: inconsistent size for 3DES-CBC context", __FUNCTION__
);
143 cccbc_init(enc
, ks
->enc
, CCDES_KEY_SIZE
*3, key
);
144 cccbc_init(dec
, ks
->dec
, CCDES_KEY_SIZE
*3, key
);
146 /* The old DES interface could return -1 or -2 for weak keys and wrong parity,
147 but this was disabled all the time, so we never fail here */
151 /* this is normally only called with length an 8 bytes multiple */
153 des3_cbc_encrypt(des_cblock
*in
, des_cblock
*out
, int32_t length
,
154 des3_cbc_key_schedule
*ks
, des_cblock
*iv
, des_cblock
*retiv
, int encrypt
)
156 const struct ccmode_cbc
*cbc
= encrypt
?g_crypto_funcs
->cctdes_cbc_encrypt
:g_crypto_funcs
->cctdes_cbc_decrypt
;
157 cccbc_ctx
*ctx
= encrypt
? ks
->enc
: ks
->dec
;
159 cccbc_iv_decl(cbc
->block_size
, ctx_iv
);
165 cccbc_set_iv(cbc
, ctx_iv
, iv
);
167 cccbc_update(cbc
, ctx
, ctx_iv
, nblocks
, in
, out
);
171 memcpy(retiv
, ctx_iv
, 8);
176 * DES MAC implemented according to FIPS 113
177 * http://www.itl.nist.gov/fipspubs/fip113.htm
182 des_cbc_cksum(des_cblock
*in
, des_cblock
*out
,
183 int len
, des_cbc_key_schedule
*ks
)
185 const struct ccmode_cbc
*cbc
= g_crypto_funcs
->ccdes_cbc_encrypt
;
188 cccbc_iv_decl(cbc
->block_size
, ctx_iv
);
193 cccbc_set_iv(cbc
, ctx_iv
, NULL
);
195 cccbc_update(cbc
, ks
->enc
, ctx_iv
, 1, in
++, cksum
);
197 memcpy(out
, cksum
, sizeof(des_cblock
));
201 /* Raw key helper functions */
202 void des_fixup_key_parity(des_cblock
*key
)
204 g_crypto_funcs
->ccdes_key_set_odd_parity_fn(key
, CCDES_KEY_SIZE
);
207 int des_is_weak_key(des_cblock
*key
)
209 return g_crypto_funcs
->ccdes_key_is_weak_fn(key
, CCDES_KEY_SIZE
);