summaryrefslogtreecommitdiff
path: root/lib/gnutls_cipher_int.h
blob: ce51588df8eeccd6a778971c96c431279a5e733d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (C) 2000-2011 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

#ifndef GNUTLS_CIPHER_INT
#define GNUTLS_CIPHER_INT

#include <gnutls/crypto.h>
#include <crypto-backend.h>

extern int crypto_cipher_prio;
extern gnutls_crypto_cipher_st _gnutls_cipher_ops;

typedef int (*cipher_encrypt_func) (void *hd, const void *plaintext, size_t,
                                    void *ciphertext, size_t);
typedef int (*cipher_decrypt_func) (void *hd, const void *ciphertext, size_t,
                                    void *plaintext, size_t);
typedef void (*cipher_deinit_func) (void *hd);

typedef int (*cipher_auth_func) (void *hd, const void *data, size_t);
typedef int (*cipher_setiv_func) (void *hd, const void *iv, size_t);

typedef void (*cipher_tag_func) (void *hd, void *tag, size_t);

typedef struct
{
  void *handle;
  cipher_encrypt_func encrypt;
  cipher_decrypt_func decrypt;
  cipher_auth_func auth;
  cipher_tag_func tag;
  cipher_setiv_func setiv;
  cipher_deinit_func deinit;
  
  int tag_size;
  int is_aead:1;
} cipher_hd_st;

int _gnutls_cipher_init (cipher_hd_st *, gnutls_cipher_algorithm_t cipher,
                         const gnutls_datum_t * key,
                         const gnutls_datum_t * iv, int enc);

inline static void _gnutls_cipher_setiv (const cipher_hd_st * handle, 
    const void *iv, int ivlen)
{
  handle->setiv(handle->handle, iv, ivlen);
}

inline static int
_gnutls_cipher_encrypt2 (const cipher_hd_st * handle, const void *text,
                         int textlen, void *ciphertext, int ciphertextlen)
{
  if (handle != NULL && handle->handle != NULL)
    {
      return handle->encrypt (handle->handle, text, textlen, ciphertext,
                              ciphertextlen);
    }

  return 0;
}

inline static int
_gnutls_cipher_decrypt2 (const cipher_hd_st * handle, const void *ciphertext,
                         int ciphertextlen, void *text, int textlen)
{
  if (handle != NULL && handle->handle != NULL)
    {
      return handle->decrypt (handle->handle, ciphertext, ciphertextlen,
                              text, textlen);
    }

  return 0;
}

inline static void
_gnutls_cipher_deinit (cipher_hd_st * handle)
{
  if (handle != NULL && handle->handle != NULL)
    {
      handle->deinit (handle->handle);
      handle->handle = NULL;
    }
}

int _gnutls_cipher_exists(gnutls_cipher_algorithm_t cipher);
inline static unsigned int _gnutls_cipher_tag_len( cipher_hd_st * handle)
{
  return handle->tag_size;
}

inline static unsigned int _gnutls_cipher_is_aead( cipher_hd_st * handle)
{
  return handle->is_aead;
}

/* returns the tag in AUTHENC ciphers */
inline static void _gnutls_cipher_tag( const cipher_hd_st * handle, void* tag, int tag_size)
{
  if (handle != NULL && handle->handle != NULL)
    {
      handle->tag (handle->handle, tag, tag_size);
    }
}

/* Add auth data for AUTHENC ciphers
 */
inline static int _gnutls_cipher_auth (const cipher_hd_st * handle, const void *text,
                             int textlen)
{
  if (handle != NULL && handle->handle != NULL)
    {
      return handle->auth (handle->handle, text, textlen);
    }
  return GNUTLS_E_INTERNAL_ERROR;
}

#define _gnutls_cipher_encrypt(x,y,z) _gnutls_cipher_encrypt2(x,y,z,y,z)
#define _gnutls_cipher_decrypt(x,y,z) _gnutls_cipher_decrypt2(x,y,z,y,z)

/* auth_cipher API. Allows combining a cipher with a MAC.
 */

typedef struct
{
  cipher_hd_st cipher;
  digest_hd_st mac;
  unsigned int is_mac:1;
  unsigned int ssl_hmac:1;
  unsigned int is_null:1;
  int tag_size;
} auth_cipher_hd_st;

int _gnutls_auth_cipher_init (auth_cipher_hd_st * handle, 
  gnutls_cipher_algorithm_t cipher,
  const gnutls_datum_t * cipher_key,
  const gnutls_datum_t * iv,
  gnutls_mac_algorithm_t mac,
  const gnutls_datum_t * mac_key, int ssl_hmac, int enc);

int _gnutls_auth_cipher_add_auth (auth_cipher_hd_st * handle, const void *text,
                             int textlen);

int _gnutls_auth_cipher_encrypt2_tag (auth_cipher_hd_st * handle, const uint8_t *text,
                             int textlen, void *ciphertext, int ciphertextlen,
                             void* tag_ptr, int tag_size, 
                             int auth_size);
int _gnutls_auth_cipher_decrypt2 (auth_cipher_hd_st * handle,
                             const void *ciphertext, int ciphertextlen,
                             void *text, int textlen);
int _gnutls_auth_cipher_tag( auth_cipher_hd_st * handle, void* tag, int tag_size);

inline static void _gnutls_auth_cipher_setiv (const auth_cipher_hd_st * handle, 
    const void *iv, int ivlen)
{
  _gnutls_cipher_setiv(&handle->cipher, iv, ivlen);
}

inline static unsigned int _gnutls_auth_cipher_tag_len( auth_cipher_hd_st * handle)
{
  return handle->tag_size;
}

inline static unsigned int _gnutls_auth_cipher_is_aead( auth_cipher_hd_st * handle)
{
  return _gnutls_cipher_is_aead(&handle->cipher);
}

#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)
#define _gnutls_auth_cipher_decrypt(x,y,z) _gnutls_auth_cipher_decrypt2(x,y,z,y,z)

void _gnutls_auth_cipher_deinit (auth_cipher_hd_st * handle);


#endif /* GNUTLS_CIPHER_INT */