Honor uninitialized private key in destructor
[gnutls.git] / lib / gcrypt / mac.c
blob0b1099e289167170218ec50ff867536817e7cfc6
1 /*
2 * Copyright (C) 2008, 2010 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
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.
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* This file provides is the backend hash/mac API for libgcrypt.
28 #include <gnutls_int.h>
29 #include <gnutls_hash_int.h>
30 #include <gnutls_errors.h>
31 #include <gcrypt.h>
33 static int
34 wrap_gcry_mac_init (gnutls_mac_algorithm_t algo, void **ctx)
36 int err;
37 unsigned int flags = GCRY_MD_FLAG_HMAC;
39 switch (algo)
41 case GNUTLS_MAC_MD5:
42 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_MD5, flags);
43 break;
44 case GNUTLS_MAC_SHA1:
45 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA1, flags);
46 break;
47 case GNUTLS_MAC_RMD160:
48 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_RMD160, flags);
49 break;
50 case GNUTLS_MAC_MD2:
51 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_MD2, flags);
52 break;
53 case GNUTLS_MAC_SHA256:
54 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA256, flags);
55 break;
56 case GNUTLS_MAC_SHA384:
57 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA384, flags);
58 break;
59 case GNUTLS_MAC_SHA512:
60 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA512, flags);
61 break;
62 default:
63 gnutls_assert ();
64 return GNUTLS_E_INVALID_REQUEST;
67 if (err == 0)
68 return 0;
70 gnutls_assert ();
71 return GNUTLS_E_ENCRYPTION_FAILED;
74 static int
75 wrap_gcry_md_setkey (void *ctx, const void *key, size_t keylen)
77 return gcry_md_setkey ((gcry_md_hd_t) ctx, key, keylen);
80 static int
81 wrap_gcry_md_write (void *ctx, const void *text, size_t textsize)
83 gcry_md_write (ctx, text, textsize);
84 return GNUTLS_E_SUCCESS;
87 static int
88 wrap_gcry_md_copy (void **bhd, void *ahd)
90 return gcry_md_copy ((gcry_md_hd_t *) bhd, (gcry_md_hd_t) ahd);
93 static void
94 wrap_gcry_md_close (void *hd)
96 gcry_md_close (hd);
99 static int
100 wrap_gcry_hash_init (gnutls_mac_algorithm_t algo, void **ctx)
102 int err;
103 unsigned int flags = 0;
105 switch (algo)
107 case GNUTLS_DIG_MD5:
108 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_MD5, flags);
109 break;
110 case GNUTLS_DIG_SHA1:
111 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA1, flags);
112 break;
113 case GNUTLS_DIG_RMD160:
114 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_RMD160, flags);
115 break;
116 case GNUTLS_DIG_MD2:
117 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_MD2, flags);
118 break;
119 case GNUTLS_DIG_SHA256:
120 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA256, flags);
121 break;
122 case GNUTLS_DIG_SHA224:
123 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA224, flags);
124 break;
125 case GNUTLS_DIG_SHA384:
126 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA384, flags);
127 break;
128 case GNUTLS_DIG_SHA512:
129 err = gcry_md_open ((gcry_md_hd_t *) ctx, GCRY_MD_SHA512, flags);
130 break;
131 default:
132 gnutls_assert ();
133 return GNUTLS_E_INVALID_REQUEST;
136 if (err == 0)
137 return 0;
139 gnutls_assert ();
140 return GNUTLS_E_ENCRYPTION_FAILED;
143 static int
144 wrap_gcry_mac_output (void *src_ctx, void *digest, size_t digestsize)
146 opaque *_digest = gcry_md_read (src_ctx, 0);
148 if (_digest != NULL)
150 unsigned int len = gcry_md_get_algo_dlen (gcry_md_get_algo (src_ctx));
152 if (len <= digestsize && digest != NULL)
153 memcpy (digest, _digest, len);
155 return 0;
158 gnutls_assert ();
159 return GNUTLS_E_HASH_FAILED;
163 gnutls_crypto_mac_st _gnutls_mac_ops = {
164 .init = wrap_gcry_mac_init,
165 .setkey = wrap_gcry_md_setkey,
166 .hash = wrap_gcry_md_write,
167 .output = wrap_gcry_mac_output,
168 .deinit = wrap_gcry_md_close,
171 gnutls_crypto_digest_st _gnutls_digest_ops = {
172 .init = wrap_gcry_hash_init,
173 .hash = wrap_gcry_md_write,
174 .copy = wrap_gcry_md_copy,
175 .output = wrap_gcry_mac_output,
176 .deinit = wrap_gcry_md_close,