Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:00:16

0001 /*
0002  * Copyright (C) 2008-2012 Free Software Foundation, Inc.
0003  *
0004  * Author: Nikos Mavrogiannopoulos
0005  *
0006  * This file is part of GnuTLS.
0007  *
0008  * The GnuTLS is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU Lesser General Public License
0010  * as published by the Free Software Foundation; either version 2.1 of
0011  * the License, or (at your option) any later version.
0012  *
0013  * This library is distributed in the hope that it will be useful, but
0014  * WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016  * Lesser General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU Lesser General Public License
0019  * along with this program.  If not, see <https://www.gnu.org/licenses/>
0020  *
0021  */
0022 
0023 #ifndef GNUTLS_CRYPTO_H
0024 #define GNUTLS_CRYPTO_H
0025 
0026 #include <gnutls/gnutls.h>
0027 
0028 #ifdef __cplusplus
0029 extern "C" {
0030 #endif
0031 
0032 typedef struct api_cipher_hd_st *gnutls_cipher_hd_t;
0033 
0034 int gnutls_cipher_init(gnutls_cipher_hd_t *handle,
0035                gnutls_cipher_algorithm_t cipher,
0036                const gnutls_datum_t *key, const gnutls_datum_t *iv);
0037 int gnutls_cipher_encrypt(const gnutls_cipher_hd_t handle, void *text,
0038               size_t textlen);
0039 int gnutls_cipher_decrypt(const gnutls_cipher_hd_t handle, void *ciphertext,
0040               size_t ciphertextlen);
0041 int gnutls_cipher_decrypt2(gnutls_cipher_hd_t handle, const void *ciphertext,
0042                size_t ciphertextlen, void *text, size_t textlen);
0043 int gnutls_cipher_encrypt2(gnutls_cipher_hd_t handle, const void *text,
0044                size_t textlen, void *ciphertext,
0045                size_t ciphertextlen);
0046 
0047 /**
0048  * gnutls_cipher_flags_t:
0049  * @GNUTLS_CIPHER_PADDING_PKCS7: Flag to indicate PKCS#7 padding
0050  *
0051  * Enumeration of flags to control block cipher padding, used by
0052  * gnutls_cipher_encrypt3() and gnutls_cipher_decrypt3().
0053  *
0054  * Since: 3.7.7
0055  */
0056 typedef enum gnutls_cipher_flags_t {
0057     GNUTLS_CIPHER_PADDING_PKCS7 = 1
0058 } gnutls_cipher_flags_t;
0059 
0060 int gnutls_cipher_encrypt3(gnutls_cipher_hd_t handle, const void *ptext,
0061                size_t ptext_len, void *ctext, size_t *ctext_len,
0062                unsigned flags);
0063 int gnutls_cipher_decrypt3(gnutls_cipher_hd_t handle, const void *ctext,
0064                size_t ctext_len, void *ptext, size_t *ptext_len,
0065                unsigned flags);
0066 
0067 void gnutls_cipher_set_iv(gnutls_cipher_hd_t handle, void *iv, size_t ivlen);
0068 
0069 int gnutls_cipher_tag(gnutls_cipher_hd_t handle, void *tag, size_t tag_size);
0070 int gnutls_cipher_add_auth(gnutls_cipher_hd_t handle, const void *text,
0071                size_t text_size);
0072 
0073 void gnutls_cipher_deinit(gnutls_cipher_hd_t handle);
0074 unsigned gnutls_cipher_get_block_size(gnutls_cipher_algorithm_t algorithm)
0075     __GNUTLS_CONST__;
0076 unsigned
0077 gnutls_cipher_get_iv_size(gnutls_cipher_algorithm_t algorithm) __GNUTLS_CONST__;
0078 unsigned gnutls_cipher_get_tag_size(gnutls_cipher_algorithm_t algorithm)
0079     __GNUTLS_CONST__;
0080 
0081 /* AEAD API
0082  */
0083 typedef struct api_aead_cipher_hd_st *gnutls_aead_cipher_hd_t;
0084 
0085 int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
0086                 gnutls_cipher_algorithm_t cipher,
0087                 const gnutls_datum_t *key);
0088 
0089 int gnutls_aead_cipher_set_key(gnutls_aead_cipher_hd_t handle,
0090                    const gnutls_datum_t *key);
0091 
0092 int gnutls_aead_cipher_decrypt(gnutls_aead_cipher_hd_t handle,
0093                    const void *nonce, size_t nonce_len,
0094                    const void *auth, size_t auth_len,
0095                    size_t tag_size, const void *ctext,
0096                    size_t ctext_len, void *ptext,
0097                    size_t *ptext_len);
0098 int gnutls_aead_cipher_encrypt(gnutls_aead_cipher_hd_t handle,
0099                    const void *nonce, size_t nonce_len,
0100                    const void *auth, size_t auth_len,
0101                    size_t tag_size, const void *ptext,
0102                    size_t ptext_len, void *ctext,
0103                    size_t *ctext_len);
0104 
0105 int gnutls_aead_cipher_encryptv(gnutls_aead_cipher_hd_t handle,
0106                 const void *nonce, size_t nonce_len,
0107                 const giovec_t *auth_iov, int auth_iovcnt,
0108                 size_t tag_size, const giovec_t *iov,
0109                 int iovcnt, void *ctext, size_t *ctext_len);
0110 
0111 int gnutls_aead_cipher_encryptv2(gnutls_aead_cipher_hd_t handle,
0112                  const void *nonce, size_t nonce_len,
0113                  const giovec_t *auth_iov, int auth_iovcnt,
0114                  const giovec_t *iov, int iovcnt, void *tag,
0115                  size_t *tag_size);
0116 
0117 int gnutls_aead_cipher_decryptv2(gnutls_aead_cipher_hd_t handle,
0118                  const void *nonce, size_t nonce_len,
0119                  const giovec_t *auth_iov, int auth_iovcnt,
0120                  const giovec_t *iov, int iovcnt, void *tag,
0121                  size_t tag_size);
0122 
0123 void gnutls_aead_cipher_deinit(gnutls_aead_cipher_hd_t handle);
0124 
0125 /* Hash - MAC API */
0126 
0127 typedef struct hash_hd_st *gnutls_hash_hd_t;
0128 typedef struct hmac_hd_st *gnutls_hmac_hd_t;
0129 
0130 size_t
0131 gnutls_mac_get_nonce_size(gnutls_mac_algorithm_t algorithm) __GNUTLS_CONST__;
0132 int gnutls_hmac_init(gnutls_hmac_hd_t *dig, gnutls_mac_algorithm_t algorithm,
0133              const void *key, size_t keylen);
0134 void gnutls_hmac_set_nonce(gnutls_hmac_hd_t handle, const void *nonce,
0135                size_t nonce_len);
0136 int gnutls_hmac(gnutls_hmac_hd_t handle, const void *text, size_t textlen);
0137 void gnutls_hmac_output(gnutls_hmac_hd_t handle, void *digest);
0138 void gnutls_hmac_deinit(gnutls_hmac_hd_t handle, void *digest);
0139 unsigned gnutls_hmac_get_len(gnutls_mac_algorithm_t algorithm) __GNUTLS_CONST__;
0140 unsigned
0141 gnutls_hmac_get_key_size(gnutls_mac_algorithm_t algorithm) __GNUTLS_CONST__;
0142 int gnutls_hmac_fast(gnutls_mac_algorithm_t algorithm, const void *key,
0143              size_t keylen, const void *text, size_t textlen,
0144              void *digest);
0145 gnutls_hmac_hd_t gnutls_hmac_copy(gnutls_hmac_hd_t handle);
0146 
0147 int gnutls_hash_init(gnutls_hash_hd_t *dig,
0148              gnutls_digest_algorithm_t algorithm);
0149 int gnutls_hash(gnutls_hash_hd_t handle, const void *text, size_t textlen);
0150 void gnutls_hash_output(gnutls_hash_hd_t handle, void *digest);
0151 void gnutls_hash_deinit(gnutls_hash_hd_t handle, void *digest);
0152 unsigned
0153 gnutls_hash_get_len(gnutls_digest_algorithm_t algorithm) __GNUTLS_CONST__;
0154 int gnutls_hash_fast(gnutls_digest_algorithm_t algorithm, const void *text,
0155              size_t textlen, void *digest);
0156 gnutls_hash_hd_t gnutls_hash_copy(gnutls_hash_hd_t handle);
0157 
0158 /* KDF API */
0159 
0160 int gnutls_hkdf_extract(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
0161             const gnutls_datum_t *salt, void *output);
0162 
0163 int gnutls_hkdf_expand(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
0164                const gnutls_datum_t *info, void *output, size_t length);
0165 
0166 int gnutls_pbkdf2(gnutls_mac_algorithm_t mac, const gnutls_datum_t *key,
0167           const gnutls_datum_t *salt, unsigned iter_count, void *output,
0168           size_t length);
0169 
0170 /* register ciphers */
0171 
0172 /**
0173  * gnutls_rnd_level_t:
0174  * @GNUTLS_RND_NONCE: Non-predictable random number.  Fatal in parts
0175  *   of session if broken, i.e., vulnerable to statistical analysis.
0176  * @GNUTLS_RND_RANDOM: Pseudo-random cryptographic random number.
0177  *   Fatal in session if broken. Example use: temporal keys.
0178  * @GNUTLS_RND_KEY: Fatal in many sessions if broken. Example use:
0179  *   Long-term keys.
0180  *
0181  * Enumeration of random quality levels.
0182  */
0183 typedef enum gnutls_rnd_level {
0184     GNUTLS_RND_NONCE = 0,
0185     GNUTLS_RND_RANDOM = 1,
0186     GNUTLS_RND_KEY = 2
0187 } gnutls_rnd_level_t;
0188 
0189 int gnutls_rnd(gnutls_rnd_level_t level, void *data, size_t len);
0190 
0191 void gnutls_rnd_refresh(void);
0192 
0193 /* API to override ciphers and MAC algorithms 
0194  */
0195 
0196 typedef int (*gnutls_cipher_init_func)(gnutls_cipher_algorithm_t, void **ctx,
0197                        int enc);
0198 typedef int (*gnutls_cipher_setkey_func)(void *ctx, const void *key,
0199                      size_t keysize);
0200 /* old style ciphers */
0201 typedef int (*gnutls_cipher_setiv_func)(void *ctx, const void *iv,
0202                     size_t ivsize);
0203 typedef int (*gnutls_cipher_getiv_func)(void *ctx, void *iv, size_t ivsize);
0204 typedef int (*gnutls_cipher_encrypt_func)(void *ctx, const void *plain,
0205                       size_t plainsize, void *encr,
0206                       size_t encrsize);
0207 typedef int (*gnutls_cipher_decrypt_func)(void *ctx, const void *encr,
0208                       size_t encrsize, void *plain,
0209                       size_t plainsize);
0210 
0211 /* aead ciphers */
0212 typedef int (*gnutls_cipher_auth_func)(void *ctx, const void *data,
0213                        size_t datasize);
0214 typedef void (*gnutls_cipher_tag_func)(void *ctx, void *tag, size_t tagsize);
0215 
0216 typedef int (*gnutls_cipher_aead_encrypt_func)(
0217     void *ctx, const void *nonce, size_t noncesize, const void *auth,
0218     size_t authsize, size_t tag_size, const void *plain, size_t plainsize,
0219     void *encr, size_t encrsize);
0220 typedef int (*gnutls_cipher_aead_decrypt_func)(
0221     void *ctx, const void *nonce, size_t noncesize, const void *auth,
0222     size_t authsize, size_t tag_size, const void *encr, size_t encrsize,
0223     void *plain, size_t plainsize);
0224 typedef void (*gnutls_cipher_deinit_func)(void *ctx);
0225 
0226 int gnutls_crypto_register_cipher(
0227     gnutls_cipher_algorithm_t algorithm, int priority,
0228     gnutls_cipher_init_func init, gnutls_cipher_setkey_func setkey,
0229     gnutls_cipher_setiv_func setiv, gnutls_cipher_encrypt_func encrypt,
0230     gnutls_cipher_decrypt_func decrypt,
0231     gnutls_cipher_deinit_func deinit) _GNUTLS_GCC_ATTR_DEPRECATED;
0232 
0233 int gnutls_crypto_register_aead_cipher(
0234     gnutls_cipher_algorithm_t algorithm, int priority,
0235     gnutls_cipher_init_func init, gnutls_cipher_setkey_func setkey,
0236     gnutls_cipher_aead_encrypt_func aead_encrypt,
0237     gnutls_cipher_aead_decrypt_func aead_decrypt,
0238     gnutls_cipher_deinit_func deinit) _GNUTLS_GCC_ATTR_DEPRECATED;
0239 
0240 typedef int (*gnutls_mac_init_func)(gnutls_mac_algorithm_t, void **ctx);
0241 typedef int (*gnutls_mac_setkey_func)(void *ctx, const void *key,
0242                       size_t keysize);
0243 typedef int (*gnutls_mac_setnonce_func)(void *ctx, const void *nonce,
0244                     size_t noncesize);
0245 typedef int (*gnutls_mac_hash_func)(void *ctx, const void *text,
0246                     size_t textsize);
0247 typedef int (*gnutls_mac_output_func)(void *src_ctx, void *digest,
0248                       size_t digestsize);
0249 typedef void (*gnutls_mac_deinit_func)(void *ctx);
0250 typedef int (*gnutls_mac_fast_func)(gnutls_mac_algorithm_t, const void *nonce,
0251                     size_t nonce_size, const void *key,
0252                     size_t keysize, const void *text,
0253                     size_t textsize, void *digest);
0254 typedef void *(*gnutls_mac_copy_func)(const void *ctx);
0255 
0256 int gnutls_crypto_register_mac(
0257     gnutls_mac_algorithm_t mac, int priority, gnutls_mac_init_func init,
0258     gnutls_mac_setkey_func setkey, gnutls_mac_setnonce_func setnonce,
0259     gnutls_mac_hash_func hash, gnutls_mac_output_func output,
0260     gnutls_mac_deinit_func deinit,
0261     gnutls_mac_fast_func hash_fast) _GNUTLS_GCC_ATTR_DEPRECATED;
0262 
0263 typedef int (*gnutls_digest_init_func)(gnutls_digest_algorithm_t, void **ctx);
0264 typedef int (*gnutls_digest_hash_func)(void *ctx, const void *text,
0265                        size_t textsize);
0266 typedef int (*gnutls_digest_output_func)(void *src_ctx, void *digest,
0267                      size_t digestsize);
0268 typedef void (*gnutls_digest_deinit_func)(void *ctx);
0269 typedef int (*gnutls_digest_fast_func)(gnutls_digest_algorithm_t,
0270                        const void *text, size_t textsize,
0271                        void *digest);
0272 typedef void *(*gnutls_digest_copy_func)(const void *ctx);
0273 
0274 int gnutls_crypto_register_digest(
0275     gnutls_digest_algorithm_t digest, int priority,
0276     gnutls_digest_init_func init, gnutls_digest_hash_func hash,
0277     gnutls_digest_output_func output, gnutls_digest_deinit_func deinit,
0278     gnutls_digest_fast_func hash_fast) _GNUTLS_GCC_ATTR_DEPRECATED;
0279 
0280 /* RSA-PKCS#1 1.5 helper functions */
0281 int gnutls_encode_ber_digest_info(gnutls_digest_algorithm_t hash,
0282                   const gnutls_datum_t *digest,
0283                   gnutls_datum_t *output);
0284 
0285 int gnutls_decode_ber_digest_info(const gnutls_datum_t *info,
0286                   gnutls_digest_algorithm_t *hash,
0287                   unsigned char *digest,
0288                   unsigned int *digest_size);
0289 
0290 int gnutls_decode_rs_value(const gnutls_datum_t *sig_value, gnutls_datum_t *r,
0291                gnutls_datum_t *s);
0292 int gnutls_encode_rs_value(gnutls_datum_t *sig_value, const gnutls_datum_t *r,
0293                const gnutls_datum_t *s);
0294 
0295 int gnutls_encode_gost_rs_value(gnutls_datum_t *sig_value,
0296                 const gnutls_datum_t *r,
0297                 const gnutls_datum_t *s);
0298 int gnutls_decode_gost_rs_value(const gnutls_datum_t *sig_value,
0299                 gnutls_datum_t *r, gnutls_datum_t *s);
0300 
0301 #ifdef __cplusplus
0302 }
0303 #endif
0304 
0305 #endif /* GNUTLS_CRYPTO_H */