[Upstream] x509: optimize subject alternative name access
[platform/upstream/gnutls.git] / lib / gnutls_cipher_int.h
1 /*
2  * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
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.
12  *
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.
17  *
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/>
20  *
21  */
22
23 #ifndef GNUTLS_CIPHER_INT
24 #define GNUTLS_CIPHER_INT
25
26 #include <gnutls/crypto.h>
27 #include <crypto-backend.h>
28
29 extern int crypto_cipher_prio;
30 extern gnutls_crypto_cipher_st _gnutls_cipher_ops;
31
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);
37
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);
40
41 typedef void (*cipher_tag_func) (void *hd, void *tag, size_t);
42
43 typedef struct {
44         void *handle;
45         const cipher_entry_st *e;
46         cipher_encrypt_func encrypt;
47         cipher_decrypt_func decrypt;
48         cipher_auth_func auth;
49         cipher_tag_func tag;
50         cipher_setiv_func setiv;
51         cipher_deinit_func deinit;
52 } cipher_hd_st;
53
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);
57
58 inline static void _gnutls_cipher_setiv(const cipher_hd_st * handle,
59                                         const void *iv, size_t ivlen)
60 {
61         handle->setiv(handle->handle, iv, ivlen);
62 }
63
64 inline static int
65 _gnutls_cipher_encrypt2(const cipher_hd_st * handle, const void *text,
66                         size_t textlen, void *ciphertext,
67                         size_t ciphertextlen)
68 {
69         if (likely(handle != NULL && handle->handle != NULL)) {
70                 return handle->encrypt(handle->handle, text, textlen,
71                                        ciphertext, ciphertextlen);
72         }
73
74         return 0;
75 }
76
77 inline static int
78 _gnutls_cipher_decrypt2(const cipher_hd_st * handle,
79                         const void *ciphertext, size_t ciphertextlen,
80                         void *text, size_t textlen)
81 {
82         if (likely(handle != NULL && handle->handle != NULL)) {
83                 return handle->decrypt(handle->handle, ciphertext,
84                                        ciphertextlen, text, textlen);
85         }
86
87         return 0;
88 }
89
90 inline static void _gnutls_cipher_deinit(cipher_hd_st * handle)
91 {
92         if (likely(handle != NULL && handle->handle != NULL)) {
93                 handle->deinit(handle->handle);
94                 handle->handle = NULL;
95         }
96 }
97
98 int _gnutls_cipher_exists(gnutls_cipher_algorithm_t cipher);
99
100 #define _gnutls_cipher_is_aead(h) _gnutls_cipher_algo_is_aead((h)->e)
101
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)
105 {
106         if (likely(handle != NULL && handle->handle != NULL)) {
107                 handle->tag(handle->handle, tag, tag_size);
108         }
109 }
110
111 /* Add auth data for AUTHENC ciphers
112  */
113 inline static int _gnutls_cipher_auth(const cipher_hd_st * handle,
114                                       const void *text, size_t textlen)
115 {
116         if (likely(handle != NULL && handle->handle != NULL)) {
117                 return handle->auth(handle->handle, text, textlen);
118         }
119         return GNUTLS_E_INTERNAL_ERROR;
120 }
121
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)
124
125 /* auth_cipher API. Allows combining a cipher with a MAC.
126  */
127
128 typedef struct {
129         cipher_hd_st cipher;
130         union {
131                 digest_hd_st dig;
132                 mac_hd_st mac;
133         } mac;
134         unsigned int is_mac:1;
135         unsigned int ssl_hmac:1;
136         unsigned int non_null:1;
137         size_t tag_size;
138 } auth_cipher_hd_st;
139
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,
146                              int enc);
147
148 int _gnutls_auth_cipher_add_auth(auth_cipher_hd_st * handle,
149                                  const void *text, int textlen);
150
151 int _gnutls_auth_cipher_encrypt2_tag(auth_cipher_hd_st * handle,
152                                      const uint8_t * text, int textlen,
153                                      void *ciphertext, int ciphertextlen,
154                                      int pad_size);
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,
159                             int tag_size);
160
161 inline static void _gnutls_auth_cipher_setiv(const auth_cipher_hd_st *
162                                              handle, const void *iv,
163                                              size_t ivlen)
164 {
165         _gnutls_cipher_setiv(&handle->cipher, iv, ivlen);
166 }
167
168 inline static
169 int _gnutls_auth_cipher_set_mac_nonce(auth_cipher_hd_st * handle,
170                                       const void *nonce, int nonce_len)
171 {
172         if (handle->is_mac && !handle->ssl_hmac)
173                 return _gnutls_mac_set_nonce(&handle->mac.mac, nonce,
174                                              nonce_len);
175         else
176                 return 0;
177 }
178
179 inline static size_t _gnutls_auth_cipher_tag_len(auth_cipher_hd_st *
180                                                  handle)
181 {
182         return handle->tag_size;
183 }
184
185 #define _gnutls_auth_cipher_is_aead(h) _gnutls_cipher_is_aead(&(h)->cipher)
186
187 void _gnutls_auth_cipher_deinit(auth_cipher_hd_st * handle);
188
189
190 #endif                          /* GNUTLS_CIPHER_INT */