2 * Copyright (C) 2000-2011 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 3 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
, size_t,
33 void *ciphertext
, size_t);
34 typedef int (*cipher_decrypt_func
) (void *hd
, const void *ciphertext
, size_t,
35 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);
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
;
57 int _gnutls_cipher_init (cipher_hd_st
*, gnutls_cipher_algorithm_t cipher
,
58 const gnutls_datum_t
* key
,
59 const gnutls_datum_t
* iv
);
61 inline static void _gnutls_cipher_setiv (const cipher_hd_st
* handle
,
62 const void *iv
, int ivlen
)
64 handle
->setiv(handle
->handle
, iv
, ivlen
);
68 _gnutls_cipher_encrypt2 (const cipher_hd_st
* handle
, const void *text
,
69 int textlen
, void *ciphertext
, int ciphertextlen
)
71 if (handle
!= NULL
&& handle
->handle
!= NULL
)
73 return handle
->encrypt (handle
->handle
, text
, textlen
, ciphertext
,
81 _gnutls_cipher_decrypt2 (const cipher_hd_st
* handle
, const void *ciphertext
,
82 int ciphertextlen
, void *text
, int textlen
)
84 if (handle
!= NULL
&& handle
->handle
!= NULL
)
86 return handle
->decrypt (handle
->handle
, ciphertext
, ciphertextlen
,
94 _gnutls_cipher_deinit (cipher_hd_st
* handle
)
96 if (handle
!= NULL
&& handle
->handle
!= NULL
)
98 handle
->deinit (handle
->handle
);
99 handle
->handle
= NULL
;
103 inline static unsigned int _gnutls_cipher_tag_len( cipher_hd_st
* handle
)
105 return handle
->tag_size
;
108 inline static unsigned int _gnutls_cipher_is_aead( cipher_hd_st
* handle
)
110 return handle
->is_aead
;
113 /* returns the tag in AUTHENC ciphers */
114 inline static void _gnutls_cipher_tag( const cipher_hd_st
* handle
, void* tag
, int tag_size
)
116 if (handle
!= NULL
&& handle
->handle
!= NULL
)
118 handle
->tag (handle
->handle
, tag
, tag_size
);
122 /* Add auth data for AUTHENC ciphers
124 inline static int _gnutls_cipher_auth (const cipher_hd_st
* handle
, const void *text
,
127 if (handle
!= NULL
&& handle
->handle
!= NULL
)
129 return handle
->auth (handle
->handle
, text
, textlen
);
131 return GNUTLS_E_INTERNAL_ERROR
;
134 #define _gnutls_cipher_encrypt(x,y,z) _gnutls_cipher_encrypt2(x,y,z,y,z)
135 #define _gnutls_cipher_decrypt(x,y,z) _gnutls_cipher_decrypt2(x,y,z,y,z)
137 /* auth_cipher API. Allows combining a cipher with a MAC.
149 int _gnutls_auth_cipher_init (auth_cipher_hd_st
* handle
,
150 gnutls_cipher_algorithm_t cipher
,
151 const gnutls_datum_t
* cipher_key
,
152 const gnutls_datum_t
* iv
,
153 gnutls_mac_algorithm_t mac
,
154 const gnutls_datum_t
* mac_key
, int ssl_hmac
);
156 int _gnutls_auth_cipher_add_auth (auth_cipher_hd_st
* handle
, const void *text
,
159 int _gnutls_auth_cipher_encrypt2_tag (auth_cipher_hd_st
* handle
, const uint8_t *text
,
160 int textlen
, void *ciphertext
, int ciphertextlen
,
161 void* tag_ptr
, int tag_size
,
163 int _gnutls_auth_cipher_decrypt2 (auth_cipher_hd_st
* handle
,
164 const void *ciphertext
, int ciphertextlen
,
165 void *text
, int textlen
);
166 int _gnutls_auth_cipher_tag( auth_cipher_hd_st
* handle
, void* tag
, int tag_size
);
168 inline static void _gnutls_auth_cipher_setiv (const auth_cipher_hd_st
* handle
,
169 const void *iv
, int ivlen
)
171 _gnutls_cipher_setiv(&handle
->cipher
, iv
, ivlen
);
174 inline static unsigned int _gnutls_auth_cipher_tag_len( auth_cipher_hd_st
* handle
)
176 return handle
->tag_size
;
179 inline static unsigned int _gnutls_auth_cipher_is_aead( auth_cipher_hd_st
* handle
)
181 return _gnutls_cipher_is_aead(&handle
->cipher
);
184 #define _gnutls_auth_cipher_encrypt_tag(x,y,z,t,s,a) _gnutls_auth_cipher_encrypt2_tag(x,y,z,y,z,t,s,a)
185 #define _gnutls_auth_cipher_decrypt(x,y,z) _gnutls_auth_cipher_decrypt2(x,y,z,y,z)
187 void _gnutls_auth_cipher_deinit (auth_cipher_hd_st
* handle
);
190 #endif /* GNUTLS_CIPHER_INT */