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
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>
35
36/* Single DES ECB - used by ipv6 (esp_core.c) */
37int
38des_ecb_key_sched(des_cblock *key, des_ecb_key_schedule *ks)
39{
40 const struct ccmode_ecb *enc = g_crypto_funcs->ccdes_ecb_encrypt;
41 const struct ccmode_ecb *dec = g_crypto_funcs->ccdes_ecb_decrypt;
42
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__);
46 }
47
48 int rc = enc->init(enc, ks->enc, CCDES_KEY_SIZE, key);
49 if (rc) {
50 return rc;
51 }
52
53 return dec->init(dec, ks->dec, CCDES_KEY_SIZE, key);
54}
55
56/* Simple des - 1 block */
57int
58des_ecb_encrypt(des_cblock *in, des_cblock *out, des_ecb_key_schedule *ks, int enc)
59{
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;
62
63 return ecb->ecb(ctx, 1, in, out);
64}
65
66
67/* Triple DES ECB - used by ipv6 (esp_core.c) */
68int
69des3_ecb_key_sched(des_cblock *key, des3_ecb_key_schedule *ks)
70{
71 const struct ccmode_ecb *enc = g_crypto_funcs->cctdes_ecb_encrypt;
72 const struct ccmode_ecb *dec = g_crypto_funcs->cctdes_ecb_decrypt;
73
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__);
77 }
78
79 int rc = enc->init(enc, ks->enc, CCDES_KEY_SIZE * 3, key);
80 if (rc) {
81 return rc;
82 }
83
84 return dec->init(dec, ks->dec, CCDES_KEY_SIZE * 3, key);
85}
86
87/* Simple des - 1 block */
88int
89des3_ecb_encrypt(des_cblock *in, des_cblock *out, des3_ecb_key_schedule *ks, int enc)
90{
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;
93
94 return ecb->ecb(ctx, 1, in, out);
95}
96
97/* Raw key helper functions */
98
99int
100des_is_weak_key(des_cblock *key)
101{
102 return g_crypto_funcs->ccdes_key_is_weak_fn(key, CCDES_KEY_SIZE);
103}
104