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,
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>
34 wrap_gcry_mac_init (gnutls_mac_algorithm_t algo
, void **ctx
)
37 unsigned int flags
= GCRY_MD_FLAG_HMAC
;
42 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_MD5
, flags
);
45 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA1
, flags
);
47 case GNUTLS_MAC_RMD160
:
48 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_RMD160
, flags
);
51 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_MD2
, flags
);
53 case GNUTLS_MAC_SHA256
:
54 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA256
, flags
);
56 case GNUTLS_MAC_SHA384
:
57 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA384
, flags
);
59 case GNUTLS_MAC_SHA512
:
60 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA512
, flags
);
64 return GNUTLS_E_INVALID_REQUEST
;
71 return GNUTLS_E_ENCRYPTION_FAILED
;
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
);
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
;
88 wrap_gcry_md_copy (void **bhd
, void *ahd
)
90 return gcry_md_copy ((gcry_md_hd_t
*) bhd
, (gcry_md_hd_t
) ahd
);
94 wrap_gcry_md_close (void *hd
)
100 wrap_gcry_hash_init (gnutls_mac_algorithm_t algo
, void **ctx
)
103 unsigned int flags
= 0;
108 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_MD5
, flags
);
110 case GNUTLS_DIG_SHA1
:
111 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA1
, flags
);
113 case GNUTLS_DIG_RMD160
:
114 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_RMD160
, flags
);
117 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_MD2
, flags
);
119 case GNUTLS_DIG_SHA256
:
120 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA256
, flags
);
122 case GNUTLS_DIG_SHA224
:
123 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA224
, flags
);
125 case GNUTLS_DIG_SHA384
:
126 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA384
, flags
);
128 case GNUTLS_DIG_SHA512
:
129 err
= gcry_md_open ((gcry_md_hd_t
*) ctx
, GCRY_MD_SHA512
, flags
);
133 return GNUTLS_E_INVALID_REQUEST
;
140 return GNUTLS_E_ENCRYPTION_FAILED
;
144 wrap_gcry_mac_output (void *src_ctx
, void *digest
, size_t digestsize
)
146 opaque
*_digest
= gcry_md_read (src_ctx
, 0);
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
);
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
,