2 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #ifndef GNUTLS_CIPHER_INT
24 #define GNUTLS_CIPHER_INT
26 #include <gnutls/crypto.h>
27 #include <crypto-backend.h>
29 extern int crypto_cipher_prio;
30 extern gnutls_crypto_cipher_st _gnutls_cipher_ops;
32 typedef int (*cipher_encrypt_func) (void *hd, const void *plaintext,
33 size_t, void *ciphertext, size_t);
34 typedef int (*cipher_decrypt_func) (void *hd, const void *ciphertext,
35 size_t, void *plaintext, size_t);
36 typedef void (*cipher_deinit_func) (void *hd);
38 typedef int (*cipher_auth_func) (void *hd, const void *data, size_t);
39 typedef int (*cipher_setiv_func) (void *hd, const void *iv, size_t);
41 typedef void (*cipher_tag_func) (void *hd, void *tag, size_t);
45 const cipher_entry_st *e;
46 cipher_encrypt_func encrypt;
47 cipher_decrypt_func decrypt;
48 cipher_auth_func auth;
50 cipher_setiv_func setiv;
51 cipher_deinit_func deinit;
54 int _gnutls_cipher_init(cipher_hd_st *, const cipher_entry_st * e,
55 const gnutls_datum_t * key,
56 const gnutls_datum_t * iv, int enc);
58 inline static void _gnutls_cipher_setiv(const cipher_hd_st * handle,
59 const void *iv, size_t ivlen)
61 handle->setiv(handle->handle, iv, ivlen);
65 _gnutls_cipher_encrypt2(const cipher_hd_st * handle, const void *text,
66 size_t textlen, void *ciphertext,
69 if (likely(handle != NULL && handle->handle != NULL)) {
70 return handle->encrypt(handle->handle, text, textlen,
71 ciphertext, ciphertextlen);
78 _gnutls_cipher_decrypt2(const cipher_hd_st * handle,
79 const void *ciphertext, size_t ciphertextlen,
80 void *text, size_t textlen)
82 if (likely(handle != NULL && handle->handle != NULL)) {
83 return handle->decrypt(handle->handle, ciphertext,
84 ciphertextlen, text, textlen);
90 inline static void _gnutls_cipher_deinit(cipher_hd_st * handle)
92 if (likely(handle != NULL && handle->handle != NULL)) {
93 handle->deinit(handle->handle);
94 handle->handle = NULL;
98 int _gnutls_cipher_exists(gnutls_cipher_algorithm_t cipher);
100 #define _gnutls_cipher_is_aead(h) _gnutls_cipher_algo_is_aead((h)->e)
102 /* returns the tag in AUTHENC ciphers */
103 inline static void _gnutls_cipher_tag(const cipher_hd_st * handle,
104 void *tag, size_t tag_size)
106 if (likely(handle != NULL && handle->handle != NULL)) {
107 handle->tag(handle->handle, tag, tag_size);
111 /* Add auth data for AUTHENC ciphers
113 inline static int _gnutls_cipher_auth(const cipher_hd_st * handle,
114 const void *text, size_t textlen)
116 if (likely(handle != NULL && handle->handle != NULL)) {
117 return handle->auth(handle->handle, text, textlen);
119 return GNUTLS_E_INTERNAL_ERROR;
122 #define _gnutls_cipher_encrypt(x,y,z) _gnutls_cipher_encrypt2(x,y,z,y,z)
123 #define _gnutls_cipher_decrypt(x,y,z) _gnutls_cipher_decrypt2(x,y,z,y,z)
125 /* auth_cipher API. Allows combining a cipher with a MAC.
134 unsigned int is_mac:1;
135 unsigned int ssl_hmac:1;
136 unsigned int non_null:1;
140 int _gnutls_auth_cipher_init(auth_cipher_hd_st * handle,
141 const cipher_entry_st * e,
142 const gnutls_datum_t * cipher_key,
143 const gnutls_datum_t * iv,
144 const mac_entry_st * me,
145 const gnutls_datum_t * mac_key, int ssl_hmac,
148 int _gnutls_auth_cipher_add_auth(auth_cipher_hd_st * handle,
149 const void *text, int textlen);
151 int _gnutls_auth_cipher_encrypt2_tag(auth_cipher_hd_st * handle,
152 const uint8_t * text, int textlen,
153 void *ciphertext, int ciphertextlen,
155 int _gnutls_auth_cipher_decrypt2(auth_cipher_hd_st * handle,
156 const void *ciphertext, int ciphertextlen,
157 void *text, int textlen);
158 int _gnutls_auth_cipher_tag(auth_cipher_hd_st * handle, void *tag,
161 inline static void _gnutls_auth_cipher_setiv(const auth_cipher_hd_st *
162 handle, const void *iv,
165 _gnutls_cipher_setiv(&handle->cipher, iv, ivlen);
169 int _gnutls_auth_cipher_set_mac_nonce(auth_cipher_hd_st * handle,
170 const void *nonce, int nonce_len)
172 if (handle->is_mac && !handle->ssl_hmac)
173 return _gnutls_mac_set_nonce(&handle->mac.mac, nonce,
179 inline static size_t _gnutls_auth_cipher_tag_len(auth_cipher_hd_st *
182 return handle->tag_size;
185 #define _gnutls_auth_cipher_is_aead(h) _gnutls_cipher_is_aead(&(h)->cipher)
187 void _gnutls_auth_cipher_deinit(auth_cipher_hd_st * handle);
190 #endif /* GNUTLS_CIPHER_INT */