1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2012 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions
30*c54f35caSApple OSS Distributions #include <libkern/crypto/crypto_internal.h>
31*c54f35caSApple OSS Distributions #include <libkern/libkern.h>
32*c54f35caSApple OSS Distributions #include <kern/debug.h>
33*c54f35caSApple OSS Distributions #include <libkern/crypto/des.h>
34*c54f35caSApple OSS Distributions #include <corecrypto/ccmode.h>
35*c54f35caSApple OSS Distributions
36*c54f35caSApple OSS Distributions /* Single DES ECB - used by ipv6 (esp_core.c) */
37*c54f35caSApple OSS Distributions int
des_ecb_key_sched(des_cblock * key,des_ecb_key_schedule * ks)38*c54f35caSApple OSS Distributions des_ecb_key_sched(des_cblock *key, des_ecb_key_schedule *ks)
39*c54f35caSApple OSS Distributions {
40*c54f35caSApple OSS Distributions const struct ccmode_ecb *enc = g_crypto_funcs->ccdes_ecb_encrypt;
41*c54f35caSApple OSS Distributions const struct ccmode_ecb *dec = g_crypto_funcs->ccdes_ecb_decrypt;
42*c54f35caSApple OSS Distributions
43*c54f35caSApple OSS Distributions /* Make sure the context size for the mode fits in the one we have */
44*c54f35caSApple OSS Distributions if ((enc->size > sizeof(ks->enc)) || (dec->size > sizeof(ks->dec))) {
45*c54f35caSApple OSS Distributions panic("%s: inconsistent size for DES-ECB context", __FUNCTION__);
46*c54f35caSApple OSS Distributions }
47*c54f35caSApple OSS Distributions
48*c54f35caSApple OSS Distributions int rc = enc->init(enc, ks->enc, CCDES_KEY_SIZE, key);
49*c54f35caSApple OSS Distributions if (rc) {
50*c54f35caSApple OSS Distributions return rc;
51*c54f35caSApple OSS Distributions }
52*c54f35caSApple OSS Distributions
53*c54f35caSApple OSS Distributions return dec->init(dec, ks->dec, CCDES_KEY_SIZE, key);
54*c54f35caSApple OSS Distributions }
55*c54f35caSApple OSS Distributions
56*c54f35caSApple OSS Distributions /* Simple des - 1 block */
57*c54f35caSApple OSS Distributions int
des_ecb_encrypt(des_cblock * in,des_cblock * out,des_ecb_key_schedule * ks,int enc)58*c54f35caSApple OSS Distributions des_ecb_encrypt(des_cblock *in, des_cblock *out, des_ecb_key_schedule *ks, int enc)
59*c54f35caSApple OSS Distributions {
60*c54f35caSApple OSS Distributions const struct ccmode_ecb *ecb = enc ? g_crypto_funcs->ccdes_ecb_encrypt : g_crypto_funcs->ccdes_ecb_decrypt;
61*c54f35caSApple OSS Distributions ccecb_ctx *ctx = enc ? ks->enc : ks->dec;
62*c54f35caSApple OSS Distributions
63*c54f35caSApple OSS Distributions return ecb->ecb(ctx, 1, in, out);
64*c54f35caSApple OSS Distributions }
65*c54f35caSApple OSS Distributions
66*c54f35caSApple OSS Distributions
67*c54f35caSApple OSS Distributions /* Triple DES ECB - used by ipv6 (esp_core.c) */
68*c54f35caSApple OSS Distributions int
des3_ecb_key_sched(des_cblock * key,des3_ecb_key_schedule * ks)69*c54f35caSApple OSS Distributions des3_ecb_key_sched(des_cblock *key, des3_ecb_key_schedule *ks)
70*c54f35caSApple OSS Distributions {
71*c54f35caSApple OSS Distributions const struct ccmode_ecb *enc = g_crypto_funcs->cctdes_ecb_encrypt;
72*c54f35caSApple OSS Distributions const struct ccmode_ecb *dec = g_crypto_funcs->cctdes_ecb_decrypt;
73*c54f35caSApple OSS Distributions
74*c54f35caSApple OSS Distributions /* Make sure the context size for the mode fits in the one we have */
75*c54f35caSApple OSS Distributions if ((enc->size > sizeof(ks->enc)) || (dec->size > sizeof(ks->dec))) {
76*c54f35caSApple OSS Distributions panic("%s: inconsistent size for 3DES-ECB context", __FUNCTION__);
77*c54f35caSApple OSS Distributions }
78*c54f35caSApple OSS Distributions
79*c54f35caSApple OSS Distributions int rc = enc->init(enc, ks->enc, CCDES_KEY_SIZE * 3, key);
80*c54f35caSApple OSS Distributions if (rc) {
81*c54f35caSApple OSS Distributions return rc;
82*c54f35caSApple OSS Distributions }
83*c54f35caSApple OSS Distributions
84*c54f35caSApple OSS Distributions return dec->init(dec, ks->dec, CCDES_KEY_SIZE * 3, key);
85*c54f35caSApple OSS Distributions }
86*c54f35caSApple OSS Distributions
87*c54f35caSApple OSS Distributions /* Simple des - 1 block */
88*c54f35caSApple OSS Distributions int
des3_ecb_encrypt(des_cblock * in,des_cblock * out,des3_ecb_key_schedule * ks,int enc)89*c54f35caSApple OSS Distributions des3_ecb_encrypt(des_cblock *in, des_cblock *out, des3_ecb_key_schedule *ks, int enc)
90*c54f35caSApple OSS Distributions {
91*c54f35caSApple OSS Distributions const struct ccmode_ecb *ecb = enc ? g_crypto_funcs->cctdes_ecb_encrypt : g_crypto_funcs->cctdes_ecb_decrypt;
92*c54f35caSApple OSS Distributions ccecb_ctx *ctx = enc ? ks->enc : ks->dec;
93*c54f35caSApple OSS Distributions
94*c54f35caSApple OSS Distributions return ecb->ecb(ctx, 1, in, out);
95*c54f35caSApple OSS Distributions }
96*c54f35caSApple OSS Distributions
97*c54f35caSApple OSS Distributions /* Raw key helper functions */
98*c54f35caSApple OSS Distributions
99*c54f35caSApple OSS Distributions int
des_is_weak_key(des_cblock * key)100*c54f35caSApple OSS Distributions des_is_weak_key(des_cblock *key)
101*c54f35caSApple OSS Distributions {
102*c54f35caSApple OSS Distributions return g_crypto_funcs->ccdes_key_is_weak_fn(key, CCDES_KEY_SIZE);
103*c54f35caSApple OSS Distributions }
104