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 des_ecb_key_sched(des_cblock *key, des_ecb_key_schedule *ks)
38{
39 const struct ccmode_ecb *enc = g_crypto_funcs->ccdes_ecb_encrypt;
40 const struct ccmode_ecb *dec = g_crypto_funcs->ccdes_ecb_decrypt;
41
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__);
45
46 enc->init(enc, ks->enc, CCDES_KEY_SIZE, key);
47 dec->init(dec, ks->dec, CCDES_KEY_SIZE, key);
48
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 */
51 return 0;
52}
53
54/* Simple des - 1 block */
55void des_ecb_encrypt(des_cblock *in, des_cblock *out, des_ecb_key_schedule *ks, int enc)
56{
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;
59
60 ecb->ecb(ctx, 1, in, out);
61}
62
63
64/* Triple DES ECB - used by ipv6 (esp_core.c) */
65int des3_ecb_key_sched(des_cblock *key, des3_ecb_key_schedule *ks)
66{
67 int rc;
68 const struct ccmode_ecb *enc = g_crypto_funcs->cctdes_ecb_encrypt;
69 const struct ccmode_ecb *dec = g_crypto_funcs->cctdes_ecb_decrypt;
70
71 /* Make sure the context size for the mode fits in the one we have */
72 if((enc->size>sizeof(ks->enc)) || (dec->size>sizeof(ks->dec)))
73 panic("%s: inconsistent size for 3DES-ECB context", __FUNCTION__);
74
75 rc = enc->init(enc, ks->enc, CCDES_KEY_SIZE*3, key);
76 rc |= dec->init(dec, ks->dec, CCDES_KEY_SIZE*3, key);
77
78 return rc;
79}
80
81/* Simple des - 1 block */
82void des3_ecb_encrypt(des_cblock *in, des_cblock *out, des3_ecb_key_schedule *ks, int enc)
83{
84 const struct ccmode_ecb *ecb = enc ? g_crypto_funcs->cctdes_ecb_encrypt : g_crypto_funcs->cctdes_ecb_decrypt;
85 ccecb_ctx *ctx = enc ? ks->enc : ks->dec;
86
87 ecb->ecb(ctx, 1, in, out);
88}
89
90/* Raw key helper functions */
91
92int des_is_weak_key(des_cblock *key)
93{
94 return g_crypto_funcs->ccdes_key_is_weak_fn(key, CCDES_KEY_SIZE);
95}
96