| 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 | #include <libkern/crypto/crypto.h> |
| 33 | #include <libkern/crypto/rand.h> |
| 34 | |
| 35 | __BEGIN_DECLS |
| 36 | |
| 37 | #include <corecrypto/ccdigest.h> |
| 38 | #include <corecrypto/cchmac.h> |
| 39 | #include <corecrypto/ccmode.h> |
| 40 | #include <corecrypto/ccrng.h> |
| 41 | #include <corecrypto/ccrsa.h> |
| 42 | #include <corecrypto/ccchacha20poly1305.h> |
| 43 | |
| 44 | /* Function types */ |
| 45 | |
| 46 | /* digests */ |
| 47 | typedef void (*ccdigest_init_fn_t)(const struct ccdigest_info *di, ccdigest_ctx_t ctx); |
| 48 | typedef void (*ccdigest_update_fn_t)(const struct ccdigest_info *di, ccdigest_ctx_t ctx, |
| 49 | unsigned long len, const void *data); |
| 50 | typedef void (*ccdigest_final_fn_t)(const struct ccdigest_info *di, ccdigest_ctx_t ctx, |
| 51 | void *digest); |
| 52 | typedef void (*ccdigest_fn_t)(const struct ccdigest_info *di, unsigned long len, |
| 53 | const void *data, void *digest); |
| 54 | |
| 55 | /* hmac */ |
| 56 | typedef void (*cchmac_init_fn_t)(const struct ccdigest_info *di, cchmac_ctx_t ctx, |
| 57 | unsigned long key_len, const void *key); |
| 58 | typedef void (*cchmac_update_fn_t)(const struct ccdigest_info *di, cchmac_ctx_t ctx, |
| 59 | unsigned long data_len, const void *data); |
| 60 | typedef void (*cchmac_final_fn_t)(const struct ccdigest_info *di, cchmac_ctx_t ctx, |
| 61 | unsigned char *mac); |
| 62 | |
| 63 | typedef 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 */ |
| 68 | typedef 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); |
| 71 | typedef int (*ccgcm_inc_iv_fn_t)(const struct ccmode_gcm *mode, ccgcm_ctx *ctx, void *iv); |
| 72 | |
| 73 | typedef 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 */ |
| 87 | typedef 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 */ |
| 94 | typedef int (*ccdes_key_is_weak_fn_t)(void *key, unsigned long length); |
| 95 | typedef void (*ccdes_key_set_odd_parity_fn_t)(void *key, unsigned long length); |
| 96 | |
| 97 | /* CBC padding (such as PKCS7 or CTSx per NIST standard) */ |
| 98 | typedef size_t (*ccpad_cts3_crypt_fn_t)(const struct ccmode_cbc *cbc, cccbc_ctx *cbc_key, |
| 99 | cccbc_iv *iv, size_t nbytes, const void *in, void *out); |
| 100 | |
| 101 | /* rng */ |
| 102 | typedef struct ccrng_state *(*ccrng_fn_t)(int *error); |
| 103 | |
| 104 | /* rsa */ |
| 105 | typedef int (*ccrsa_make_pub_fn_t)(ccrsa_pub_ctx_t pubk, |
| 106 | size_t exp_nbytes, const uint8_t *exp, |
| 107 | size_t mod_nbytes, const uint8_t *mod); |
| 108 | |
| 109 | typedef int (*ccrsa_verify_pkcs1v15_fn_t)(ccrsa_pub_ctx_t key, const uint8_t *oid, |
| 110 | size_t digest_len, const uint8_t *digest, |
| 111 | size_t sig_len, const uint8_t *sig, |
| 112 | bool *valid); |
| 113 | |
| 114 | __enum_decl(crypto_digest_alg_t, unsigned int, { |
| 115 | CRYPTO_DIGEST_ALG_NONE, |
| 116 | CRYPTO_DIGEST_ALG_MD5, |
| 117 | CRYPTO_DIGEST_ALG_SHA1, |
| 118 | CRYPTO_DIGEST_ALG_SHA256, |
| 119 | CRYPTO_DIGEST_ALG_SHA384, |
| 120 | CRYPTO_DIGEST_ALG_SHA512 |
| 121 | }); |
| 122 | |
| 123 | typedef size_t (*crypto_digest_ctx_size_fn_t)( |
| 124 | crypto_digest_alg_t alg); |
| 125 | |
| 126 | typedef void (*crypto_digest_init_fn_t)( |
| 127 | crypto_digest_alg_t alg, |
| 128 | void *ctx, |
| 129 | size_t ctx_size); |
| 130 | |
| 131 | typedef void (*crypto_digest_update_fn_t)( |
| 132 | crypto_digest_alg_t alg, |
| 133 | void *ctx, |
| 134 | size_t ctx_size, |
| 135 | const void *data, |
| 136 | size_t data_size); |
| 137 | |
| 138 | typedef void (*crypto_digest_final_fn_t)( |
| 139 | crypto_digest_alg_t alg, |
| 140 | void *ctx, |
| 141 | size_t ctx_size, |
| 142 | void *digest, |
| 143 | size_t digest_size); |
| 144 | |
| 145 | typedef void (*crypto_digest_fn_t)( |
| 146 | crypto_digest_alg_t alg, |
| 147 | const void *data, |
| 148 | size_t data_size, |
| 149 | void *digest, |
| 150 | size_t digest_size); |
| 151 | |
| 152 | typedef size_t (*crypto_hmac_ctx_size_fn_t)( |
| 153 | crypto_digest_alg_t alg); |
| 154 | |
| 155 | typedef void (*crypto_hmac_init_fn_t)( |
| 156 | crypto_digest_alg_t alg, |
| 157 | void *ctx, |
| 158 | size_t ctx_size, |
| 159 | const void *key, |
| 160 | size_t key_size); |
| 161 | |
| 162 | typedef void (*crypto_hmac_update_fn_t)( |
| 163 | crypto_digest_alg_t alg, |
| 164 | void *ctx, |
| 165 | size_t ctx_size, |
| 166 | const void *data, |
| 167 | size_t data_size); |
| 168 | |
| 169 | typedef void (*crypto_hmac_final_generate_fn_t)( |
| 170 | crypto_digest_alg_t alg, |
| 171 | void *ctx, |
| 172 | size_t ctx_size, |
| 173 | void *tag, |
| 174 | size_t tag_size); |
| 175 | |
| 176 | typedef bool (*crypto_hmac_final_verify_fn_t)( |
| 177 | crypto_digest_alg_t alg, |
| 178 | void *ctx, |
| 179 | size_t ctx_size, |
| 180 | const void *tag, |
| 181 | size_t tag_size); |
| 182 | |
| 183 | typedef void (*crypto_hmac_generate_fn_t)( |
| 184 | crypto_digest_alg_t alg, |
| 185 | const void *key, |
| 186 | size_t key_size, |
| 187 | const void *data, |
| 188 | size_t data_size, |
| 189 | void *tag, |
| 190 | size_t tag_size); |
| 191 | |
| 192 | typedef bool (*crypto_hmac_verify_fn_t)( |
| 193 | crypto_digest_alg_t alg, |
| 194 | const void *key, |
| 195 | size_t key_size, |
| 196 | const void *data, |
| 197 | size_t data_size, |
| 198 | const void *tag, |
| 199 | size_t tag_size); |
| 200 | |
| 201 | typedef struct crypto_functions { |
| 202 | /* digests common functions */ |
| 203 | ccdigest_init_fn_t ccdigest_init_fn; |
| 204 | ccdigest_update_fn_t ccdigest_update_fn; |
| 205 | ccdigest_final_fn_t ccdigest_final_fn; |
| 206 | ccdigest_fn_t ccdigest_fn; |
| 207 | /* digest implementations */ |
| 208 | const struct ccdigest_info * ccmd5_di; |
| 209 | const struct ccdigest_info * ccsha1_di; |
| 210 | const struct ccdigest_info * ccsha256_di; |
| 211 | const struct ccdigest_info * ccsha384_di; |
| 212 | const struct ccdigest_info * ccsha512_di; |
| 213 | |
| 214 | /* hmac common function */ |
| 215 | cchmac_init_fn_t cchmac_init_fn; |
| 216 | cchmac_update_fn_t cchmac_update_fn; |
| 217 | cchmac_final_fn_t cchmac_final_fn; |
| 218 | cchmac_fn_t cchmac_fn; |
| 219 | |
| 220 | /* ciphers modes implementations */ |
| 221 | /* AES, ecb, cbc and xts */ |
| 222 | const struct ccmode_ecb *ccaes_ecb_encrypt; |
| 223 | const struct ccmode_ecb *ccaes_ecb_decrypt; |
| 224 | const struct ccmode_cbc *ccaes_cbc_encrypt; |
| 225 | const struct ccmode_cbc *ccaes_cbc_decrypt; |
| 226 | const struct ccmode_ctr *ccaes_ctr_crypt; |
| 227 | const struct ccmode_xts *ccaes_xts_encrypt; |
| 228 | const struct ccmode_xts *ccaes_xts_decrypt; |
| 229 | const struct ccmode_gcm *ccaes_gcm_encrypt; |
| 230 | const struct ccmode_gcm *ccaes_gcm_decrypt; |
| 231 | |
| 232 | ccgcm_init_with_iv_fn_t ccgcm_init_with_iv_fn; |
| 233 | ccgcm_inc_iv_fn_t ccgcm_inc_iv_fn; |
| 234 | |
| 235 | ccchacha20poly1305_fns_t ccchacha20poly1305_fns; |
| 236 | |
| 237 | /* DES, ecb and cbc */ |
| 238 | const struct ccmode_ecb *ccdes_ecb_encrypt; |
| 239 | const struct ccmode_ecb *ccdes_ecb_decrypt; |
| 240 | const struct ccmode_cbc *ccdes_cbc_encrypt; |
| 241 | const struct ccmode_cbc *ccdes_cbc_decrypt; |
| 242 | /* Triple DES, ecb and cbc */ |
| 243 | const struct ccmode_ecb *cctdes_ecb_encrypt; |
| 244 | const struct ccmode_ecb *cctdes_ecb_decrypt; |
| 245 | const struct ccmode_cbc *cctdes_cbc_encrypt; |
| 246 | const struct ccmode_cbc *cctdes_cbc_decrypt; |
| 247 | /* DES key helper functions */ |
| 248 | ccdes_key_is_weak_fn_t ccdes_key_is_weak_fn; |
| 249 | ccdes_key_set_odd_parity_fn_t ccdes_key_set_odd_parity_fn; |
| 250 | /* CTS3 padding+encrypt functions */ |
| 251 | ccpad_cts3_crypt_fn_t ccpad_cts3_encrypt_fn; |
| 252 | ccpad_cts3_crypt_fn_t ccpad_cts3_decrypt_fn; |
| 253 | |
| 254 | /* rng */ |
| 255 | ccrng_fn_t ccrng_fn; |
| 256 | |
| 257 | /* rsa */ |
| 258 | ccrsa_make_pub_fn_t ccrsa_make_pub_fn; |
| 259 | ccrsa_verify_pkcs1v15_fn_t ccrsa_verify_pkcs1v15_fn; |
| 260 | |
| 261 | // Random functions |
| 262 | crypto_random_generate_fn_t random_generate_fn; |
| 263 | crypto_random_uniform_fn_t random_uniform_fn; |
| 264 | crypto_random_kmem_ctx_size_fn_t random_kmem_ctx_size_fn; |
| 265 | crypto_random_kmem_init_fn_t random_kmem_init_fn; |
| 266 | |
| 267 | // Digest functions |
| 268 | crypto_digest_ctx_size_fn_t digest_ctx_size_fn; |
| 269 | crypto_digest_init_fn_t digest_init_fn; |
| 270 | crypto_digest_update_fn_t digest_update_fn; |
| 271 | crypto_digest_final_fn_t digest_final_fn; |
| 272 | crypto_digest_fn_t digest_fn; |
| 273 | |
| 274 | // HMAC functions |
| 275 | crypto_hmac_ctx_size_fn_t hmac_ctx_size_fn; |
| 276 | crypto_hmac_init_fn_t hmac_init_fn; |
| 277 | crypto_hmac_update_fn_t hmac_update_fn; |
| 278 | crypto_hmac_final_generate_fn_t hmac_final_generate_fn; |
| 279 | crypto_hmac_final_verify_fn_t hmac_final_verify_fn; |
| 280 | crypto_hmac_generate_fn_t hmac_generate_fn; |
| 281 | crypto_hmac_verify_fn_t hmac_verify_fn; |
| 282 | } *crypto_functions_t; |
| 283 | |
| 284 | int register_crypto_functions(const crypto_functions_t funcs); |
| 285 | |
| 286 | __END_DECLS |
| 287 | |
| 288 | #endif /*_CRYPTO_REGISTER_CRYPTO_H_*/ |
| 289 | |