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