xref: /xnu-8792.41.9/libkern/libkern/crypto/register_crypto.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
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 __enum_decl(crypto_digest_alg_t, unsigned int, {
114 	CRYPTO_DIGEST_ALG_NONE,
115 	CRYPTO_DIGEST_ALG_MD5,
116 	CRYPTO_DIGEST_ALG_SHA1,
117 	CRYPTO_DIGEST_ALG_SHA256,
118 	CRYPTO_DIGEST_ALG_SHA384,
119 	CRYPTO_DIGEST_ALG_SHA512
120 });
121 
122 typedef size_t (*crypto_digest_ctx_size_fn_t)(
123 	crypto_digest_alg_t alg);
124 
125 typedef void (*crypto_digest_init_fn_t)(
126 	crypto_digest_alg_t alg,
127 	void *ctx,
128 	size_t ctx_size);
129 
130 typedef void (*crypto_digest_update_fn_t)(
131 	crypto_digest_alg_t alg,
132 	void *ctx,
133 	size_t ctx_size,
134 	const void *data,
135 	size_t data_size);
136 
137 typedef void (*crypto_digest_final_fn_t)(
138 	crypto_digest_alg_t alg,
139 	void *ctx,
140 	size_t ctx_size,
141 	void *digest,
142 	size_t digest_size);
143 
144 typedef void (*crypto_digest_fn_t)(
145 	crypto_digest_alg_t alg,
146 	const void *data,
147 	size_t data_size,
148 	void *digest,
149 	size_t digest_size);
150 
151 typedef size_t (*crypto_hmac_ctx_size_fn_t)(
152 	crypto_digest_alg_t alg);
153 
154 typedef void (*crypto_hmac_init_fn_t)(
155 	crypto_digest_alg_t alg,
156 	void *ctx,
157 	size_t ctx_size,
158 	const void *key,
159 	size_t key_size);
160 
161 typedef void (*crypto_hmac_update_fn_t)(
162 	crypto_digest_alg_t alg,
163 	void *ctx,
164 	size_t ctx_size,
165 	const void *data,
166 	size_t data_size);
167 
168 typedef void (*crypto_hmac_final_generate_fn_t)(
169 	crypto_digest_alg_t alg,
170 	void *ctx,
171 	size_t ctx_size,
172 	void *tag,
173 	size_t tag_size);
174 
175 typedef bool (*crypto_hmac_final_verify_fn_t)(
176 	crypto_digest_alg_t alg,
177 	void *ctx,
178 	size_t ctx_size,
179 	const void *tag,
180 	size_t tag_size);
181 
182 typedef void (*crypto_hmac_generate_fn_t)(
183 	crypto_digest_alg_t alg,
184 	const void *key,
185 	size_t key_size,
186 	const void *data,
187 	size_t data_size,
188 	void *tag,
189 	size_t tag_size);
190 
191 typedef bool (*crypto_hmac_verify_fn_t)(
192 	crypto_digest_alg_t alg,
193 	const void *key,
194 	size_t key_size,
195 	const void *data,
196 	size_t data_size,
197 	const void *tag,
198 	size_t tag_size);
199 
200 typedef struct crypto_functions {
201 	/* digests common functions */
202 	ccdigest_init_fn_t ccdigest_init_fn;
203 	ccdigest_update_fn_t ccdigest_update_fn;
204 	ccdigest_final_fn_t ccdigest_final_fn;
205 	ccdigest_fn_t ccdigest_fn;
206 	/* digest implementations */
207 	const struct ccdigest_info * ccmd5_di;
208 	const struct ccdigest_info * ccsha1_di;
209 	const struct ccdigest_info * ccsha256_di;
210 	const struct ccdigest_info * ccsha384_di;
211 	const struct ccdigest_info * ccsha512_di;
212 
213 	/* hmac common function */
214 	cchmac_init_fn_t cchmac_init_fn;
215 	cchmac_update_fn_t cchmac_update_fn;
216 	cchmac_final_fn_t cchmac_final_fn;
217 	cchmac_fn_t cchmac_fn;
218 
219 	/* ciphers modes implementations */
220 	/* AES, ecb, cbc and xts */
221 	const struct ccmode_ecb *ccaes_ecb_encrypt;
222 	const struct ccmode_ecb *ccaes_ecb_decrypt;
223 	const struct ccmode_cbc *ccaes_cbc_encrypt;
224 	const struct ccmode_cbc *ccaes_cbc_decrypt;
225 	const struct ccmode_ctr *ccaes_ctr_crypt;
226 	const struct ccmode_xts *ccaes_xts_encrypt;
227 	const struct ccmode_xts *ccaes_xts_decrypt;
228 	const struct ccmode_gcm *ccaes_gcm_encrypt;
229 	const struct ccmode_gcm *ccaes_gcm_decrypt;
230 
231 	ccgcm_init_with_iv_fn_t ccgcm_init_with_iv_fn;
232 	ccgcm_inc_iv_fn_t ccgcm_inc_iv_fn;
233 
234 	ccchacha20poly1305_fns_t ccchacha20poly1305_fns;
235 
236 	/* DES, ecb and cbc */
237 	const struct ccmode_ecb *ccdes_ecb_encrypt;
238 	const struct ccmode_ecb *ccdes_ecb_decrypt;
239 	const struct ccmode_cbc *ccdes_cbc_encrypt;
240 	const struct ccmode_cbc *ccdes_cbc_decrypt;
241 	/* Triple DES, ecb and cbc */
242 	const struct ccmode_ecb *cctdes_ecb_encrypt;
243 	const struct ccmode_ecb *cctdes_ecb_decrypt;
244 	const struct ccmode_cbc *cctdes_cbc_encrypt;
245 	const struct ccmode_cbc *cctdes_cbc_decrypt;
246 	/* DES key helper functions */
247 	ccdes_key_is_weak_fn_t ccdes_key_is_weak_fn;
248 	ccdes_key_set_odd_parity_fn_t ccdes_key_set_odd_parity_fn;
249 	/* CTS3 padding+encrypt functions */
250 	ccpad_cts3_crypt_fn_t ccpad_cts3_encrypt_fn;
251 	ccpad_cts3_crypt_fn_t ccpad_cts3_decrypt_fn;
252 
253 	/* rng */
254 	ccrng_fn_t ccrng_fn;
255 
256 	/* rsa */
257 	ccrsa_make_pub_fn_t        ccrsa_make_pub_fn;
258 	ccrsa_verify_pkcs1v15_fn_t ccrsa_verify_pkcs1v15_fn;
259 
260 	// Digest functions
261 	crypto_digest_ctx_size_fn_t digest_ctx_size_fn;
262 	crypto_digest_init_fn_t digest_init_fn;
263 	crypto_digest_update_fn_t digest_update_fn;
264 	crypto_digest_final_fn_t digest_final_fn;
265 	crypto_digest_fn_t digest_fn;
266 
267 	// HMAC functions
268 	crypto_hmac_ctx_size_fn_t hmac_ctx_size_fn;
269 	crypto_hmac_init_fn_t hmac_init_fn;
270 	crypto_hmac_update_fn_t hmac_update_fn;
271 	crypto_hmac_final_generate_fn_t hmac_final_generate_fn;
272 	crypto_hmac_final_verify_fn_t hmac_final_verify_fn;
273 	crypto_hmac_generate_fn_t hmac_generate_fn;
274 	crypto_hmac_verify_fn_t hmac_verify_fn;
275 } *crypto_functions_t;
276 
277 int register_crypto_functions(const crypto_functions_t funcs);
278 
279 #ifdef  __cplusplus
280 }
281 #endif
282 
283 #endif /*_CRYPTO_REGISTER_CRYPTO_H_*/
284