]>
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) */
38 des_ecb_key_sched(des_cblock
*key
, des_ecb_key_schedule
*ks
)
40 const struct ccmode_ecb
*enc
= g_crypto_funcs
->ccdes_ecb_encrypt
;
41 const struct ccmode_ecb
*dec
= g_crypto_funcs
->ccdes_ecb_decrypt
;
43 /* Make sure the context size for the mode fits in the one we have */
44 if ((enc
->size
> sizeof(ks
->enc
)) || (dec
->size
> sizeof(ks
->dec
))) {
45 panic("%s: inconsistent size for DES-ECB context", __FUNCTION__
);
48 int rc
= enc
->init(enc
, ks
->enc
, CCDES_KEY_SIZE
, key
);
53 return dec
->init(dec
, ks
->dec
, CCDES_KEY_SIZE
, key
);
56 /* Simple des - 1 block */
58 des_ecb_encrypt(des_cblock
*in
, des_cblock
*out
, des_ecb_key_schedule
*ks
, int enc
)
60 const struct ccmode_ecb
*ecb
= enc
? g_crypto_funcs
->ccdes_ecb_encrypt
: g_crypto_funcs
->ccdes_ecb_decrypt
;
61 ccecb_ctx
*ctx
= enc
? ks
->enc
: ks
->dec
;
63 return ecb
->ecb(ctx
, 1, in
, out
);
67 /* Triple DES ECB - used by ipv6 (esp_core.c) */
69 des3_ecb_key_sched(des_cblock
*key
, des3_ecb_key_schedule
*ks
)
71 const struct ccmode_ecb
*enc
= g_crypto_funcs
->cctdes_ecb_encrypt
;
72 const struct ccmode_ecb
*dec
= g_crypto_funcs
->cctdes_ecb_decrypt
;
74 /* Make sure the context size for the mode fits in the one we have */
75 if ((enc
->size
> sizeof(ks
->enc
)) || (dec
->size
> sizeof(ks
->dec
))) {
76 panic("%s: inconsistent size for 3DES-ECB context", __FUNCTION__
);
79 int rc
= enc
->init(enc
, ks
->enc
, CCDES_KEY_SIZE
* 3, key
);
84 return dec
->init(dec
, ks
->dec
, CCDES_KEY_SIZE
* 3, key
);
87 /* Simple des - 1 block */
89 des3_ecb_encrypt(des_cblock
*in
, des_cblock
*out
, des3_ecb_key_schedule
*ks
, int enc
)
91 const struct ccmode_ecb
*ecb
= enc
? g_crypto_funcs
->cctdes_ecb_encrypt
: g_crypto_funcs
->cctdes_ecb_decrypt
;
92 ccecb_ctx
*ctx
= enc
? ks
->enc
: ks
->dec
;
94 return ecb
->ecb(ctx
, 1, in
, out
);
97 /* Raw key helper functions */
100 des_is_weak_key(des_cblock
*key
)
102 return g_crypto_funcs
->ccdes_key_is_weak_fn(key
, CCDES_KEY_SIZE
);