xref: /xnu-8020.121.3/libkern/libkern/crypto/register_crypto.h (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
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_REGISTER_CRYPTO_H_
30 #define _CRYPTO_REGISTER_CRYPTO_H_
31 
32 #ifdef  __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <corecrypto/ccdigest.h>
37 #include <corecrypto/cchmac.h>
38 #include <corecrypto/ccmode.h>
39 #include <corecrypto/ccrng.h>
40 #include <corecrypto/ccrsa.h>
41 #include <corecrypto/ccchacha20poly1305.h>
42 
43 /* Function types */
44 
45 /* digests */
46 typedef void (*ccdigest_init_fn_t)(const struct ccdigest_info *di, ccdigest_ctx_t ctx);
47 typedef void (*ccdigest_update_fn_t)(const struct ccdigest_info *di, ccdigest_ctx_t ctx,
48     unsigned long len, const void *data);
49 typedef void (*ccdigest_final_fn_t)(const struct ccdigest_info *di, ccdigest_ctx_t ctx,
50     void *digest);
51 typedef void (*ccdigest_fn_t)(const struct ccdigest_info *di, unsigned long len,
52     const void *data, void *digest);
53 
54 /* hmac */
55 typedef void (*cchmac_init_fn_t)(const struct ccdigest_info *di, cchmac_ctx_t ctx,
56     unsigned long key_len, const void *key);
57 typedef void (*cchmac_update_fn_t)(const struct ccdigest_info *di, cchmac_ctx_t ctx,
58     unsigned long data_len, const void *data);
59 typedef void (*cchmac_final_fn_t)(const struct ccdigest_info *di, cchmac_ctx_t ctx,
60     unsigned char *mac);
61 
62 typedef void (*cchmac_fn_t)(const struct ccdigest_info *di, unsigned long key_len,
63     const void *key, unsigned long data_len, const void *data,
64     unsigned char *mac);
65 
66 /* gcm */
67 typedef int (*ccgcm_init_with_iv_fn_t)(const struct ccmode_gcm *mode, ccgcm_ctx *ctx,
68     size_t key_nbytes, const void *key,
69     const void *iv);
70 typedef int (*ccgcm_inc_iv_fn_t)(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, void *iv);
71 
72 typedef const struct ccchacha20poly1305_fns {
73 	const struct ccchacha20poly1305_info *(*info)(void);
74 	int (*init)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *key);
75 	int (*reset)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx);
76 	int (*setnonce)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *nonce);
77 	int (*incnonce)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, uint8_t *nonce);
78 	int (*aad)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes, const void *aad);
79 	int (*encrypt)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes, const void *ptext, void *ctext);
80 	int (*finalize)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, uint8_t *tag);
81 	int (*decrypt)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, size_t nbytes, const void *ctext, void *ptext);
82 	int (*verify)(const struct ccchacha20poly1305_info *info, ccchacha20poly1305_ctx *ctx, const uint8_t *tag);
83 } *ccchacha20poly1305_fns_t;
84 
85 /* pbkdf2 */
86 typedef void (*ccpbkdf2_hmac_fn_t)(const struct ccdigest_info *di,
87     unsigned long passwordLen, const void *password,
88     unsigned long saltLen, const void *salt,
89     unsigned long iterations,
90     unsigned long dkLen, void *dk);
91 
92 /* des weak key testing */
93 typedef int (*ccdes_key_is_weak_fn_t)(void *key, unsigned long  length);
94 typedef void (*ccdes_key_set_odd_parity_fn_t)(void *key, unsigned long length);
95 
96 /* CBC padding (such as PKCS7 or CTSx per NIST standard) */
97 typedef size_t (*ccpad_cts3_crypt_fn_t)(const struct ccmode_cbc *cbc, cccbc_ctx *cbc_key,
98     cccbc_iv *iv, size_t nbytes, const void *in, void *out);
99 
100 /* rng */
101 typedef struct ccrng_state *(*ccrng_fn_t)(int *error);
102 
103 /* rsa */
104 typedef int (*ccrsa_make_pub_fn_t)(ccrsa_pub_ctx_t pubk,
105     size_t exp_nbytes, const uint8_t *exp,
106     size_t mod_nbytes, const uint8_t *mod);
107 
108 typedef int (*ccrsa_verify_pkcs1v15_fn_t)(ccrsa_pub_ctx_t key, const uint8_t *oid,
109     size_t digest_len, const uint8_t *digest,
110     size_t sig_len, const uint8_t *sig,
111     bool *valid);
112 
113 typedef struct crypto_functions {
114 	/* digests common functions */
115 	ccdigest_init_fn_t ccdigest_init_fn;
116 	ccdigest_update_fn_t ccdigest_update_fn;
117 	ccdigest_final_fn_t ccdigest_final_fn;
118 	ccdigest_fn_t ccdigest_fn;
119 	/* digest implementations */
120 	const struct ccdigest_info * ccmd5_di;
121 	const struct ccdigest_info * ccsha1_di;
122 	const struct ccdigest_info * ccsha256_di;
123 	const struct ccdigest_info * ccsha384_di;
124 	const struct ccdigest_info * ccsha512_di;
125 
126 	/* hmac common function */
127 	cchmac_init_fn_t cchmac_init_fn;
128 	cchmac_update_fn_t cchmac_update_fn;
129 	cchmac_final_fn_t cchmac_final_fn;
130 	cchmac_fn_t cchmac_fn;
131 
132 	/* ciphers modes implementations */
133 	/* AES, ecb, cbc and xts */
134 	const struct ccmode_ecb *ccaes_ecb_encrypt;
135 	const struct ccmode_ecb *ccaes_ecb_decrypt;
136 	const struct ccmode_cbc *ccaes_cbc_encrypt;
137 	const struct ccmode_cbc *ccaes_cbc_decrypt;
138 	const struct ccmode_ctr *ccaes_ctr_crypt;
139 	const struct ccmode_xts *ccaes_xts_encrypt;
140 	const struct ccmode_xts *ccaes_xts_decrypt;
141 	const struct ccmode_gcm *ccaes_gcm_encrypt;
142 	const struct ccmode_gcm *ccaes_gcm_decrypt;
143 
144 	ccgcm_init_with_iv_fn_t ccgcm_init_with_iv_fn;
145 	ccgcm_inc_iv_fn_t ccgcm_inc_iv_fn;
146 
147 	ccchacha20poly1305_fns_t ccchacha20poly1305_fns;
148 
149 	/* DES, ecb and cbc */
150 	const struct ccmode_ecb *ccdes_ecb_encrypt;
151 	const struct ccmode_ecb *ccdes_ecb_decrypt;
152 	const struct ccmode_cbc *ccdes_cbc_encrypt;
153 	const struct ccmode_cbc *ccdes_cbc_decrypt;
154 	/* Triple DES, ecb and cbc */
155 	const struct ccmode_ecb *cctdes_ecb_encrypt;
156 	const struct ccmode_ecb *cctdes_ecb_decrypt;
157 	const struct ccmode_cbc *cctdes_cbc_encrypt;
158 	const struct ccmode_cbc *cctdes_cbc_decrypt;
159 	/* DES key helper functions */
160 	ccdes_key_is_weak_fn_t ccdes_key_is_weak_fn;
161 	ccdes_key_set_odd_parity_fn_t ccdes_key_set_odd_parity_fn;
162 	/* CTS3 padding+encrypt functions */
163 	ccpad_cts3_crypt_fn_t ccpad_cts3_encrypt_fn;
164 	ccpad_cts3_crypt_fn_t ccpad_cts3_decrypt_fn;
165 
166 	/* rng */
167 	ccrng_fn_t ccrng_fn;
168 
169 	/* rsa */
170 	ccrsa_make_pub_fn_t        ccrsa_make_pub_fn;
171 	ccrsa_verify_pkcs1v15_fn_t ccrsa_verify_pkcs1v15_fn;
172 } *crypto_functions_t;
173 
174 int register_crypto_functions(const crypto_functions_t funcs);
175 
176 #ifdef  __cplusplus
177 }
178 #endif
179 
180 #endif /*_CRYPTO_REGISTER_CRYPTO_H_*/
181