2 * Copyright (C) 2008, 2010 Free Software Foundation, Inc.
4 * Author: Simon Josefsson
6 * This file is part of GNUTLS-EXTRA.
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <gnutls_int.h>
24 #include <gnutls/crypto.h>
25 #include <gnutls/extra.h>
31 md5init (gnutls_mac_algorithm_t mac
, void **ctx
)
33 *ctx
= gnutls_malloc (sizeof (struct md5_ctx
));
35 return GNUTLS_E_MEMORY_ERROR
;
41 md5hash (void *ctx
, const void *text
, size_t textsize
)
43 md5_process_bytes (text
, textsize
, ctx
);
48 md5copy (void **dst_ctx
, void *src_ctx
)
50 *dst_ctx
= gnutls_malloc (sizeof (struct md5_ctx
));
52 return GNUTLS_E_MEMORY_ERROR
;
53 memcpy (*dst_ctx
, src_ctx
, sizeof (struct md5_ctx
));
58 md5output (void *src_ctx
, void *digest
, size_t digestsize
)
60 char out
[MD5_DIGEST_SIZE
];
61 md5_finish_ctx (src_ctx
, out
);
62 memcpy (digest
, out
, digestsize
);
81 hmacmd5init (gnutls_mac_algorithm_t mac
, void **ctx
)
85 p
= gnutls_malloc (sizeof (struct hmacctx
));
100 hmacmd5setkey (void *ctx
, const void *key
, size_t keysize
)
102 struct hmacctx
*p
= ctx
;
105 gnutls_free (p
->key
);
107 p
->key
= gnutls_malloc (keysize
);
111 memcpy (p
->key
, key
, keysize
);
112 p
->keysize
= keysize
;
118 hmacmd5hash (void *ctx
, const void *text
, size_t textsize
)
120 struct hmacctx
*p
= ctx
;
123 new = gnutls_realloc (p
->data
, p
->datasize
+ textsize
);
127 memcpy (new + p
->datasize
, text
, textsize
);
130 p
->datasize
+= textsize
;
136 hmacmd5copy (void **dst_ctx
, void *src_ctx
)
138 struct hmacctx
*p
= src_ctx
;
141 q
= gnutls_malloc (sizeof (struct hmacctx
));
145 q
->data
= gnutls_malloc (p
->datasize
);
151 memcpy (q
->data
, p
->data
, p
->datasize
);
152 q
->datasize
= p
->datasize
;
154 q
->key
= gnutls_malloc (p
->keysize
);
158 gnutls_free (q
->data
);
161 memcpy (q
->key
, p
->key
, p
->keysize
);
162 q
->keysize
= p
->keysize
;
170 hmacmd5output (void *ctx
, void *digest
, size_t digestsize
)
172 struct hmacctx
*p
= ctx
;
173 char out
[MD5_DIGEST_SIZE
];
176 ret
= hmac_md5 (p
->key
, p
->keysize
, p
->data
, p
->datasize
, out
);
178 return GNUTLS_E_HASH_FAILED
;
180 memcpy (digest
, out
, digestsize
);
186 hmacmd5deinit (void *ctx
)
188 struct hmacctx
*p
= ctx
;
191 gnutls_free (p
->data
);
193 gnutls_free (p
->key
);
198 static gnutls_crypto_digest_st dig
= {
206 static gnutls_crypto_mac_st mac
= {
208 .setkey
= hmacmd5setkey
,
211 .output
= hmacmd5output
,
212 .deinit
= hmacmd5deinit
216 * gnutls_register_md5_handler:
218 * Register a non-libgcrypt based MD5 and HMAC-MD5 handler. This is
219 * useful if you run Libgcrypt in FIPS-mode. Normally TLS requires
220 * use of MD5, so without this you cannot use GnuTLS with libgcrypt in
223 * Returns: %GNUTLS_E_SUCCESS on success, otherwise an error.
228 gnutls_register_md5_handler (void)
232 ret
= gnutls_crypto_single_digest_register (GNUTLS_DIG_MD5
, INT_MAX
, &dig
);
236 ret
= gnutls_crypto_single_mac_register (GNUTLS_MAC_MD5
, INT_MAX
, &mac
);