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 <https://www.gnu.org/licenses/>
20 *
21 */
22
23 #ifndef GNUTLS_LIB_HASH_INT_H
24 #define GNUTLS_LIB_HASH_INT_H
25
26 #include "gnutls_int.h"
27 #include <gnutls/crypto.h>
28 #include <crypto-backend.h>
29 #include <crypto.h>
30
31 /* for message digests */
32
33 extern int crypto_mac_prio;
34 extern gnutls_crypto_mac_st _gnutls_mac_ops;
35
36 extern int crypto_digest_prio;
37 extern gnutls_crypto_digest_st _gnutls_digest_ops;
38
39 typedef int (*hash_func) (void *handle, const void *text, size_t size);
40 typedef int (*nonce_func) (void *handle, const void *text, size_t size);
41 typedef int (*output_func) (void *src_ctx, void *digest,
42 size_t digestsize);
43 typedef void (*hash_deinit_func) (void *handle);
44 typedef void *(*copy_func) (const void *handle);
45
46 typedef struct {
47 const mac_entry_st *e;
48 hash_func hash;
49 output_func output;
50 hash_deinit_func deinit;
51 copy_func copy;
52
53 const void *key; /* esoteric use by SSL3 MAC functions */
54 int keysize;
55
56 void *handle;
57 } digest_hd_st;
58
59 typedef struct {
60 const mac_entry_st *e;
61 int mac_len;
62
63 hash_func hash;
64 nonce_func setnonce;
65 output_func output;
66 hash_deinit_func deinit;
67 copy_func copy;
68
69 void *handle;
70 } mac_hd_st;
71
72 /* basic functions */
73 int _gnutls_digest_exists(gnutls_digest_algorithm_t algo);
74
75 int _gnutls_mac_exists(gnutls_mac_algorithm_t algorithm);
76 int _gnutls_mac_init(mac_hd_st *, const mac_entry_st * e,
77 const void *key, int keylen);
78
79 int _gnutls_mac_copy(const mac_hd_st * handle, mac_hd_st * dst);
80
81 int _gnutls_mac_fast(gnutls_mac_algorithm_t algorithm, const void *key,
82 int keylen, const void *text, size_t textlen,
83 void *digest);
84
85 inline static int
_gnutls_mac(mac_hd_st * handle,const void * text,size_t textlen)86 _gnutls_mac(mac_hd_st * handle, const void *text, size_t textlen)
87 {
88 if (textlen > 0) {
89 return handle->hash(handle->handle, text, textlen);
90 }
91 return 0;
92 }
93
_gnutls_mac_output(mac_hd_st * handle,void * digest)94 inline static void _gnutls_mac_output(mac_hd_st * handle, void *digest)
95 {
96 if (digest != NULL) {
97 handle->output(handle->handle, digest, handle->mac_len);
98 }
99 }
100
101 inline static int
_gnutls_mac_set_nonce(mac_hd_st * handle,const void * nonce,size_t n_size)102 _gnutls_mac_set_nonce(mac_hd_st * handle, const void *nonce, size_t n_size)
103 {
104 if (handle->setnonce)
105 return handle->setnonce(handle->handle, nonce, n_size);
106 return 0;
107 }
108
109 void _gnutls_mac_deinit(mac_hd_st * handle, void *digest);
110
111 /* Hash interface */
112 int _gnutls_hash_init(digest_hd_st *, const mac_entry_st * e);
113
114 inline static int
_gnutls_hash(digest_hd_st * handle,const void * text,size_t textlen)115 _gnutls_hash(digest_hd_st * handle, const void *text, size_t textlen)
116 {
117 if (textlen > 0) {
118 return handle->hash(handle->handle, text, textlen);
119 }
120 return 0;
121 }
122
123 /* when the current output is needed without calling deinit
124 */
125 #define _gnutls_hash_output(h, d) \
126 (h)->output((h)->handle, d, _gnutls_hash_get_algo_len((h)->e))
127
128 void _gnutls_hash_deinit(digest_hd_st * handle, void *digest);
129
130 int _gnutls_hash_copy(const digest_hd_st * handle, digest_hd_st * dst);
131
132 int
133 _gnutls_hash_fast(gnutls_digest_algorithm_t algorithm,
134 const void *text, size_t textlen, void *digest);
135
136 #ifdef ENABLE_SSL3
137 /* helper functions */
138 int _gnutls_mac_init_ssl3(digest_hd_st *, const mac_entry_st * e,
139 void *key, int keylen);
140 int _gnutls_mac_deinit_ssl3(digest_hd_st * handle, void *digest);
141 int _gnutls_mac_output_ssl3(digest_hd_st * handle, void *digest);
142
143 int _gnutls_ssl3_generate_random(void *secret, int secret_len,
144 void *rnd, int random_len, int bytes,
145 uint8_t * ret);
146
147 int _gnutls_mac_deinit_ssl3_handshake(digest_hd_st * handle, void *digest,
148 uint8_t * key, uint32_t key_size);
149 #endif
150
IS_SHA(gnutls_digest_algorithm_t algo)151 inline static int IS_SHA(gnutls_digest_algorithm_t algo)
152 {
153 if (algo == GNUTLS_DIG_SHA1 || algo == GNUTLS_DIG_SHA224 ||
154 algo == GNUTLS_DIG_SHA256 || algo == GNUTLS_DIG_SHA384 ||
155 algo == GNUTLS_DIG_SHA512)
156 return 1;
157 return 0;
158 }
159
160 #endif /* GNUTLS_LIB_HASH_INT_H */
161