Line data Source code
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 : typedef int (*setkey_func) (void *handle, const void *key, size_t keysize);
46 :
47 : typedef struct {
48 : const mac_entry_st *e;
49 : hash_func hash;
50 : output_func output;
51 : hash_deinit_func deinit;
52 : copy_func copy;
53 :
54 : const void *key; /* esoteric use by SSL3 MAC functions */
55 : int keysize;
56 :
57 : void *handle;
58 : } digest_hd_st;
59 :
60 : typedef struct {
61 : const mac_entry_st *e;
62 : int mac_len;
63 :
64 : hash_func hash;
65 : nonce_func setnonce;
66 : output_func output;
67 : hash_deinit_func deinit;
68 : copy_func copy;
69 : setkey_func setkey;
70 :
71 : void *handle;
72 : } mac_hd_st;
73 :
74 : /* basic functions */
75 : int _gnutls_digest_exists(gnutls_digest_algorithm_t algo);
76 :
77 : int _gnutls_mac_exists(gnutls_mac_algorithm_t algorithm);
78 : int _gnutls_mac_init(mac_hd_st *, const mac_entry_st * e,
79 : const void *key, int keylen);
80 :
81 : int _gnutls_mac_copy(const mac_hd_st * handle, mac_hd_st * dst);
82 :
83 : int _gnutls_mac_fast(gnutls_mac_algorithm_t algorithm, const void *key,
84 : int keylen, const void *text, size_t textlen,
85 : void *digest);
86 :
87 : inline static int
88 5923169 : _gnutls_mac(mac_hd_st * handle, const void *text, size_t textlen)
89 : {
90 5898098 : if (textlen > 0) {
91 5904835 : return handle->hash(handle->handle, text, textlen);
92 : }
93 : return 0;
94 : }
95 :
96 2468182 : inline static void _gnutls_mac_output(mac_hd_st * handle, void *digest)
97 : {
98 2468182 : if (digest != NULL) {
99 2468182 : handle->output(handle->handle, digest, handle->mac_len);
100 : }
101 8802 : }
102 :
103 : inline static int
104 6 : _gnutls_mac_set_nonce(mac_hd_st * handle, const void *nonce, size_t n_size)
105 : {
106 6 : if (handle->setnonce)
107 6 : return handle->setnonce(handle->handle, nonce, n_size);
108 : return 0;
109 : }
110 :
111 : inline static int
112 : _gnutls_mac_setkey(mac_hd_st * handle, const void *key, size_t key_size)
113 : {
114 : return handle->setkey(handle->handle, key, key_size);
115 : }
116 :
117 :
118 : void _gnutls_mac_deinit(mac_hd_st * handle, void *digest);
119 :
120 : /* Hash interface */
121 : int _gnutls_hash_init(digest_hd_st *, const mac_entry_st * e);
122 :
123 : inline static int
124 147800 : _gnutls_hash(digest_hd_st * handle, const void *text, size_t textlen)
125 : {
126 139452 : if (textlen > 0) {
127 145659 : return handle->hash(handle->handle, text, textlen);
128 : }
129 : return 0;
130 : }
131 :
132 : /* when the current output is needed without calling deinit
133 : */
134 : #define _gnutls_hash_output(h, d) \
135 : (h)->output((h)->handle, d, _gnutls_hash_get_algo_len((h)->e))
136 :
137 : void _gnutls_hash_deinit(digest_hd_st * handle, void *digest);
138 :
139 : int _gnutls_hash_copy(const digest_hd_st * handle, digest_hd_st * dst);
140 :
141 : int
142 : _gnutls_hash_fast(gnutls_digest_algorithm_t algorithm,
143 : const void *text, size_t textlen, void *digest);
144 :
145 : #ifdef ENABLE_SSL3
146 : /* helper functions */
147 : int _gnutls_mac_init_ssl3(digest_hd_st *, const mac_entry_st * e,
148 : void *key, int keylen);
149 : int _gnutls_mac_deinit_ssl3(digest_hd_st * handle, void *digest);
150 : int _gnutls_mac_output_ssl3(digest_hd_st * handle, void *digest);
151 :
152 : int _gnutls_ssl3_generate_random(void *secret, int secret_len,
153 : void *rnd, int random_len, int bytes,
154 : uint8_t * ret);
155 :
156 : int _gnutls_mac_deinit_ssl3_handshake(digest_hd_st * handle, void *digest,
157 : uint8_t * key, uint32_t key_size);
158 : #endif
159 :
160 : inline static int IS_SHA(gnutls_digest_algorithm_t algo)
161 : {
162 : if (algo == GNUTLS_DIG_SHA1 || algo == GNUTLS_DIG_SHA224 ||
163 : algo == GNUTLS_DIG_SHA256 || algo == GNUTLS_DIG_SHA384 ||
164 : algo == GNUTLS_DIG_SHA512)
165 : return 1;
166 : return 0;
167 : }
168 :
169 : #endif /* GNUTLS_LIB_HASH_INT_H */
|