Imported Upstream version 3.0.21
[platform/upstream/gnutls.git] / lib / gnutls_hash_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 3 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_HASH_INT_H
24 #define GNUTLS_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 void (*reset_func) (void *ctx);
41 typedef int (*output_func) (void *src_ctx, void *digest, size_t digestsize);
42 typedef void (*deinit_func) (void *handle);
43
44 typedef struct
45 {
46   gnutls_digest_algorithm_t algorithm;
47   const void *key;
48   int keysize;
49
50   hash_func hash;
51   reset_func reset;
52   output_func output;
53   deinit_func deinit;
54
55   void *handle;
56 } digest_hd_st;
57
58 /* basic functions */
59 int _gnutls_hmac_exists(gnutls_mac_algorithm_t algorithm);
60 int _gnutls_hmac_init (digest_hd_st *, gnutls_mac_algorithm_t algorithm,
61                        const void *key, int keylen);
62 size_t _gnutls_hash_get_algo_len (gnutls_digest_algorithm_t algorithm);
63 #define _gnutls_hmac_get_algo_len(x) _gnutls_hash_get_algo_len((gnutls_digest_algorithm_t)x)
64 int _gnutls_hmac_fast (gnutls_mac_algorithm_t algorithm, const void *key,
65                        int keylen, const void *text, size_t textlen,
66                        void *digest);
67
68 inline static int
69 _gnutls_hmac (digest_hd_st * handle, const void *text, size_t textlen)
70 {
71   if (textlen > 0)
72     {
73       return handle->hash (handle->handle, text, textlen);
74     }
75   return 0;
76 }
77
78 inline static void
79 _gnutls_hmac_output (digest_hd_st * handle, void *digest)
80 {
81   size_t maclen;
82
83   maclen = _gnutls_hmac_get_algo_len (handle->algorithm);
84
85   if (digest != NULL)
86     {
87       handle->output (handle->handle, digest, maclen);
88     }
89 }
90
91 void
92 _gnutls_hmac_deinit (digest_hd_st * handle, void *digest);
93
94 inline static void
95 _gnutls_hmac_reset (digest_hd_st * handle)
96 {
97   if (handle->handle == NULL)
98     {
99       return;
100     }
101
102   handle->reset (handle->handle);
103 }
104
105
106 /* Hash interface */
107 int _gnutls_hash_init (digest_hd_st *, gnutls_digest_algorithm_t algorithm);
108
109 inline static int
110 _gnutls_hash (digest_hd_st * handle, const void *text, size_t textlen)
111 {
112   if (textlen > 0)
113     {
114       handle->hash (handle->handle, text, textlen);
115     }
116   return 0;
117 }
118
119 /* when the current output is needed without calling deinit
120  */
121 inline static void
122 _gnutls_hash_output (digest_hd_st * handle, void *digest)
123 {
124   size_t maclen;
125
126   maclen = _gnutls_hash_get_algo_len (handle->algorithm);
127
128   if (digest != NULL)
129     {
130       handle->output (handle->handle, digest, maclen);
131     }
132 }
133
134 inline static void
135 _gnutls_hash_reset (digest_hd_st * handle)
136 {
137   if (handle->handle == NULL)
138     {
139       return;
140     }
141
142   handle->reset (handle->handle);
143 }
144
145 void
146 _gnutls_hash_deinit (digest_hd_st * handle, void *digest);
147
148 int
149 _gnutls_hash_fast (gnutls_digest_algorithm_t algorithm,
150                    const void *text, size_t textlen, void *digest);
151
152 /* help functions */
153 int _gnutls_mac_init_ssl3 (digest_hd_st *, gnutls_mac_algorithm_t algorithm,
154                            void *key, int keylen);
155 int _gnutls_mac_deinit_ssl3 (digest_hd_st * handle, void *digest);
156 int _gnutls_mac_output_ssl3 (digest_hd_st * handle, void *digest);
157
158 int _gnutls_ssl3_generate_random (void *secret, int secret_len,
159                                   void *rnd, int random_len, int bytes,
160                                   uint8_t * ret);
161 int _gnutls_ssl3_hash_md5 (const void *first, int first_len,
162                            const void *second, int second_len,
163                            int ret_len, uint8_t * ret);
164
165 void _gnutls_mac_reset_ssl3 (digest_hd_st * handle);
166
167 int _gnutls_mac_deinit_ssl3_handshake (digest_hd_st * handle, void *digest,
168                                         uint8_t * key, uint32_t key_size);
169
170 inline static int IS_SHA(gnutls_digest_algorithm_t algo)
171 {
172   if (algo == GNUTLS_DIG_SHA1 || algo == GNUTLS_DIG_SHA224 ||
173       algo == GNUTLS_DIG_SHA256 || algo == GNUTLS_DIG_SHA384 ||
174       algo == GNUTLS_DIG_SHA512)
175       return 1;
176   return 0;
177 }
178
179 #endif /* GNUTLS_HASH_INT_H */