]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/crypto/des.h
960e60e043e3cd81683b007a6999e96ab892fa72
[apple/xnu.git] / libkern / libkern / crypto / des.h
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 #ifndef _CRYPTO_DES_H
30 #define _CRYPTO_DES_H
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #include <corecrypto/ccmode.h>
37 #include <corecrypto/ccdes.h>
38 #include <corecrypto/ccn.h>
39
40 /* must be 32bit quantity */
41 #define DES_LONG u_int32_t
42
43 typedef unsigned char des_cblock[8];
44
45 /* Unholy hack: this is currently the size for the only implementation of DES in corecrypto */
46 #define DES_ECB_CTX_MAX_SIZE (64*4)
47 #define DES_CBC_CTX_MAX_SIZE (ccn_sizeof_size(sizeof(struct ccmode_ecb)) + ccn_sizeof_size(CCDES_BLOCK_SIZE) + ccn_sizeof_size(DES_ECB_CTX_MAX_SIZE))
48 #define DES3_ECB_CTX_MAX_SIZE (64*4*3)
49 #define DES3_CBC_CTX_MAX_SIZE (ccn_sizeof_size(sizeof(struct ccmode_ecb)) + ccn_sizeof_size(CCDES_BLOCK_SIZE) + ccn_sizeof_size(DES3_ECB_CTX_MAX_SIZE))
50
51
52 typedef struct{
53 ccecb_ctx_decl(DES_ECB_CTX_MAX_SIZE, enc);
54 ccecb_ctx_decl(DES_ECB_CTX_MAX_SIZE, dec);
55 } des_ecb_key_schedule;
56
57 typedef struct{
58 cccbc_ctx_decl(DES_CBC_CTX_MAX_SIZE, enc);
59 cccbc_ctx_decl(DES_CBC_CTX_MAX_SIZE, dec);
60 } des_cbc_key_schedule;
61
62 typedef struct{
63 ccecb_ctx_decl(DES3_ECB_CTX_MAX_SIZE, enc);
64 ccecb_ctx_decl(DES3_ECB_CTX_MAX_SIZE, dec);
65 } des3_ecb_key_schedule;
66
67 typedef struct{
68 cccbc_ctx_decl(DES3_CBC_CTX_MAX_SIZE, enc);
69 cccbc_ctx_decl(DES3_CBC_CTX_MAX_SIZE, dec);
70 } des3_cbc_key_schedule;
71
72 /* Only here for backward compatibility with smb kext */
73 typedef des_ecb_key_schedule des_key_schedule[1];
74 #define des_set_key des_ecb_key_sched
75
76 #define DES_ENCRYPT 1
77 #define DES_DECRYPT 0
78
79
80 /* Single DES ECB - 1 block */
81 int des_ecb_key_sched(des_cblock *key, des_ecb_key_schedule *ks);
82 void des_ecb_encrypt(des_cblock *in, des_cblock *out, des_ecb_key_schedule *ks, int encrypt);
83
84 /* Triple DES ECB - 1 block */
85 int des3_ecb_key_sched(des_cblock *key, des3_ecb_key_schedule *ks);
86 void des3_ecb_encrypt(des_cblock *block, des_cblock *, des3_ecb_key_schedule *ks, int encrypt);
87
88 /* Single DES CBC */
89 int des_cbc_key_sched(des_cblock *key, des_cbc_key_schedule *ks);
90 void des_cbc_encrypt(des_cblock *in, des_cblock *out, int32_t len,
91 des_cbc_key_schedule *ks, des_cblock *iv, des_cblock *retiv, int encrypt);
92
93 /* Triple DES CBC */
94 int des3_cbc_key_sched(des_cblock *key, des3_cbc_key_schedule *ks);
95 void des3_cbc_encrypt(des_cblock *in, des_cblock *out, int32_t len,
96 des3_cbc_key_schedule *ks, des_cblock *iv, des_cblock *retiv, int encrypt);
97
98 /* Single DES CBC-MAC */
99 void des_cbc_cksum(des_cblock *in, des_cblock *out, int len, des_cbc_key_schedule *ks);
100
101 void des_fixup_key_parity(des_cblock *key);
102 int des_is_weak_key(des_cblock *key);
103 // int des_set_key(des_cblock *, des_key_schedule); // Unsupported KPI.
104
105 #ifdef __cplusplus
106 }
107 #endif
108
109 #endif