Apache Portable Runtime Utility Library
apr_crypto.h
Go to the documentation of this file.
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements. See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef APR_CRYPTO_H
18 #define APR_CRYPTO_H
19 
20 #include "apu.h"
21 #include "apr_pools.h"
22 #include "apr_tables.h"
23 #include "apr_hash.h"
24 #include "apu_errno.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
40 #if APU_HAVE_CRYPTO
41 
42 #ifndef APU_CRYPTO_RECOMMENDED_DRIVER
43 #if APU_HAVE_COMMONCRYPTO
44 #define APU_CRYPTO_RECOMMENDED_DRIVER "commoncrypto"
45 #else
46 #if APU_HAVE_OPENSSL
47 #define APU_CRYPTO_RECOMMENDED_DRIVER "openssl"
48 #else
49 #if APU_HAVE_NSS
50 #define APU_CRYPTO_RECOMMENDED_DRIVER "nss"
51 #else
52 #if APU_HAVE_MSCNG
53 #define APU_CRYPTO_RECOMMENDED_DRIVER "mscng"
54 #else
55 #if APU_HAVE_MSCAPI
56 #define APU_CRYPTO_RECOMMENDED_DRIVER "mscapi"
57 #else
58 #endif
59 #endif
60 #endif
61 #endif
62 #endif
63 #endif
64 
106 typedef enum
107 {
108  APR_KEY_NONE, APR_KEY_3DES_192,
114 
115 typedef enum
116 {
117  APR_MODE_NONE,
122 
123 /* These are opaque structs. Instantiation is up to each backend */
124 typedef struct apr_crypto_driver_t apr_crypto_driver_t;
125 typedef struct apr_crypto_t apr_crypto_t;
126 typedef struct apr_crypto_config_t apr_crypto_config_t;
127 typedef struct apr_crypto_key_t apr_crypto_key_t;
128 typedef struct apr_crypto_block_t apr_crypto_block_t;
129 
132  int keysize;
133  int blocksize;
134  int ivsize;
136 
140 
141 typedef struct apr_crypto_passphrase_t {
142  const char *pass;
143  apr_size_t passLen;
144  const unsigned char * salt;
145  apr_size_t saltLen;
146  int iterations;
148 
149 typedef struct apr_crypto_secret_t {
150  const unsigned char *secret;
151  apr_size_t secretLen;
153 
154 typedef enum {
160 
161 typedef struct apr_crypto_key_rec_t {
162  apr_crypto_key_type ktype;
165  int pad;
166  union {
167  apr_crypto_passphrase_t passphrase;
168  apr_crypto_secret_t secret;
169  } k;
171 
178 APU_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool);
179 
187 APU_DECLARE(apr_status_t) apr_crypto_clear(apr_pool_t *pool, void *buffer,
188  apr_size_t size);
189 
197 APR_DECLARE(apr_status_t) apr_crypto_memzero(void *buffer, apr_size_t size);
198 
208 APR_DECLARE(int) apr_crypto_equals(const void *buf1, const void *buf2,
209  apr_size_t size);
210 
229 APU_DECLARE(apr_status_t) apr_crypto_get_driver(
230  const apr_crypto_driver_t **driver,
231  const char *name, const char *params, const apu_err_t **result,
232  apr_pool_t *pool);
233 
240 APU_DECLARE(const char *) apr_crypto_driver_name(
241  const apr_crypto_driver_t *driver);
242 
250 APU_DECLARE(apr_status_t) apr_crypto_error(const apu_err_t **result,
251  const apr_crypto_t *f);
252 
268 APU_DECLARE(apr_status_t) apr_crypto_make(apr_crypto_t **f,
269  const apr_crypto_driver_t *driver, const char *params,
270  apr_pool_t *pool);
271 
281 APU_DECLARE(apr_status_t) apr_crypto_get_block_key_types(apr_hash_t **types,
282  const apr_crypto_t *f);
283 
293 APU_DECLARE(apr_status_t) apr_crypto_get_block_key_modes(apr_hash_t **modes,
294  const apr_crypto_t *f);
295 
312 APR_DECLARE(apr_status_t) apr_crypto_key(apr_crypto_key_t **key,
313  const apr_crypto_key_rec_t *rec, const apr_crypto_t *f, apr_pool_t *p);
314 
344 APU_DECLARE(apr_status_t) apr_crypto_passphrase(apr_crypto_key_t **key,
345  apr_size_t *ivSize, const char *pass, apr_size_t passLen,
346  const unsigned char * salt, apr_size_t saltLen,
347  const apr_crypto_block_key_type_e type,
348  const apr_crypto_block_key_mode_e mode, const int doPad,
349  const int iterations, const apr_crypto_t *f, apr_pool_t *p);
350 
367 APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_init(
368  apr_crypto_block_t **ctx, const unsigned char **iv,
369  const apr_crypto_key_t *key, apr_size_t *blockSize, apr_pool_t *p);
370 
389 APU_DECLARE(apr_status_t) apr_crypto_block_encrypt(unsigned char **out,
390  apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
391  apr_crypto_block_t *ctx);
392 
411 APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_finish(unsigned char *out,
412  apr_size_t *outlen, apr_crypto_block_t *ctx);
413 
427 APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_init(
428  apr_crypto_block_t **ctx, apr_size_t *blockSize,
429  const unsigned char *iv, const apr_crypto_key_t *key, apr_pool_t *p);
430 
449 APU_DECLARE(apr_status_t) apr_crypto_block_decrypt(unsigned char **out,
450  apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
451  apr_crypto_block_t *ctx);
452 
471 APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_finish(unsigned char *out,
472  apr_size_t *outlen, apr_crypto_block_t *ctx);
473 
480 APU_DECLARE(apr_status_t) apr_crypto_block_cleanup(apr_crypto_block_t *ctx);
481 
488 APU_DECLARE(apr_status_t) apr_crypto_cleanup(apr_crypto_t *f);
489 
496 APU_DECLARE(apr_status_t) apr_crypto_shutdown(
497  const apr_crypto_driver_t *driver);
498 
499 #endif /* APU_HAVE_CRYPTO */
500 
503 #ifdef __cplusplus
504 }
505 #endif
506 
507 #endif
apr_status_t apr_crypto_make(apr_crypto_t **f, const apr_crypto_driver_t *driver, const char *params, apr_pool_t *pool)
Create a context for supporting encryption. Keys, certificates, algorithms and other parameters will ...
Definition: apr_crypto.h:111
apr_status_t apr_crypto_block_decrypt(unsigned char **out, apr_size_t *outlen, const unsigned char *in, apr_size_t inlen, apr_crypto_block_t *ctx)
Decrypt data provided by in, write it to out.
Definition: apr_crypto.h:130
apr_crypto_block_key_type_e
Definition: apr_crypto.h:106
Definition: apr_crypto.h:156
Definition: apr_crypto.h:161
Definition: apr_crypto.h:137
apr_status_t apr_crypto_block_decrypt_init(apr_crypto_block_t **ctx, apr_size_t *blockSize, const unsigned char *iv, const apr_crypto_key_t *key, apr_pool_t *p)
Initialise a context for decrypting arbitrary data using the given key.
apr_status_t apr_crypto_cleanup(apr_crypto_t *f)
Clean encryption / decryption context.
apr_crypto_key_type
Definition: apr_crypto.h:154
apr_status_t apr_crypto_block_decrypt_finish(unsigned char *out, apr_size_t *outlen, apr_crypto_block_t *ctx)
Decrypt final data block, write it to out.
apr_status_t apr_crypto_get_driver(const apr_crypto_driver_t **driver, const char *name, const char *params, const apu_err_t **result, apr_pool_t *pool)
Get the driver struct for a name.
APR-Util Error Codes.
Definition: apr_crypto.h:149
apr_status_t apr_crypto_get_block_key_modes(apr_hash_t **modes, const apr_crypto_t *f)
Get a hash table of key modes, keyed by the name of the mode against a pointer to apr_crypto_block_ke...
apr_status_t apr_crypto_error(const apu_err_t **result, const apr_crypto_t *f)
Get the result of the last operation on a context. If the result is NULL, the operation was successfu...
Definition: apr_crypto.h:119
Definition: apr_crypto.h:141
Definition: apr_crypto.h:158
apr_status_t apr_crypto_passphrase(apr_crypto_key_t **key, apr_size_t *ivSize, const char *pass, apr_size_t passLen, const unsigned char *salt, apr_size_t saltLen, const apr_crypto_block_key_type_e type, const apr_crypto_block_key_mode_e mode, const int doPad, const int iterations, const apr_crypto_t *f, apr_pool_t *p)
Create a key from the given passphrase. By default, the PBKDF2 algorithm is used to generate the key ...
const char * apr_crypto_driver_name(const apr_crypto_driver_t *driver)
Return the name of the driver.
Definition: apr_crypto.h:109
Definition: apr_crypto.h:118
APR_DECLARE(apr_status_t) apr_crypto_memzero(void *buffer
Always zero out the buffer provided, without being optimized out by the compiler. ...
Definition: apr_crypto.h:110
apr_status_t apr_crypto_clear(apr_pool_t *pool, void *buffer, apr_size_t size)
Zero out the buffer provided when the pool is cleaned up.
Definition: apu_errno.h:161
apr_status_t apr_crypto_shutdown(const apr_crypto_driver_t *driver)
Shutdown the crypto library.
apr_status_t apr_crypto_block_encrypt_init(apr_crypto_block_t **ctx, const unsigned char **iv, const apr_crypto_key_t *key, apr_size_t *blockSize, apr_pool_t *p)
Initialise a context for encrypting arbitrary data using the given key.
apr_status_t apr_crypto_block_cleanup(apr_crypto_block_t *ctx)
Clean encryption / decryption context.
apr_status_t apr_crypto_block_encrypt(unsigned char **out, apr_size_t *outlen, const unsigned char *in, apr_size_t inlen, apr_crypto_block_t *ctx)
Encrypt data provided by in, write it to out.
apr_status_t apr_crypto_get_block_key_types(apr_hash_t **types, const apr_crypto_t *f)
Get a hash table of key types, keyed by the name of the type against a pointer to apr_crypto_block_ke...
apr_crypto_block_key_mode_e
Definition: apr_crypto.h:115
apr_status_t apr_crypto_init(apr_pool_t *pool)
Perform once-only initialisation. Call once only.
apr_status_t apr_crypto_block_encrypt_finish(unsigned char *out, apr_size_t *outlen, apr_crypto_block_t *ctx)
Encrypt final data block, write it to out.