Fix.
[shishi.git] / lib / crypto-md.c
blob54ab225f9915581cf97ba6a58fcaf0064c668773
1 /* crypto-md.c DES crypto functions
2 * Copyright (C) 2002, 2003 Simon Josefsson
3 * Copyright (C) 2003 Nicolas Pouvesle
5 * This file is part of Shishi.
7 * Shishi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Shishi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Shishi; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Note: This file is #include'd by crypto.c.
25 static int
26 md4_checksum (Shishi * handle,
27 Shishi_key * key,
28 int keyusage,
29 int cksumtype,
30 const char *in, size_t inlen, char **out, size_t * outlen)
32 #ifdef USE_GCRYPT
33 char *hash;
34 gcry_md_hd_t hd;
36 gcry_md_open (&hd, GCRY_MD_MD4, 0);
37 if (!hd)
38 return SHISHI_CRYPTO_INTERNAL_ERROR;
40 gcry_md_write (hd, in, inlen);
41 hash = gcry_md_read (hd, GCRY_MD_MD4);
42 if (hash == NULL)
44 shishi_error_printf (handle, "Libgcrypt failed to compute hash");
45 return SHISHI_CRYPTO_INTERNAL_ERROR;
48 *outlen = gcry_md_get_algo_dlen (GCRY_MD_MD4);
49 *out = xmemdup (*out, hash, *outlen);
51 gcry_md_close (hd);
52 #else
53 struct md4_ctx md4;
54 char digest[MD4_DIGEST_SIZE];
55 int rc;
57 md4_init (&md4);
58 md4_update (&md4, inlen, in);
59 md4_digest (&md4, sizeof (digest), digest);
61 *outlen = MD4_DIGEST_SIZE;
62 *out = xmemdup (*out, digest, *outlen);
63 #endif
64 return SHISHI_OK;
67 static int
68 md5_checksum (Shishi * handle,
69 Shishi_key * key,
70 int keyusage,
71 int cksumtype,
72 const char *in, size_t inlen, char **out, size_t * outlen)
74 #ifdef USE_GCRYPT
75 char *hash;
76 gcry_md_hd_t hd;
78 gcry_md_open (&hd, GCRY_MD_MD5, 0);
79 if (!hd)
80 return SHISHI_CRYPTO_INTERNAL_ERROR;
82 gcry_md_write (hd, in, inlen);
83 hash = gcry_md_read (hd, GCRY_MD_MD5);
84 if (hash == NULL)
86 shishi_error_printf (handle, "Libgcrypt failed to compute hash");
87 return SHISHI_CRYPTO_INTERNAL_ERROR;
90 *outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
91 *out = xmemdup (*out, hash, *outlen);
93 gcry_md_close (hd);
94 #else
95 struct md5_ctx md5;
96 char digest[MD5_DIGEST_SIZE];
98 md5_init (&md5);
99 md5_update (&md5, inlen, in);
100 md5_digest (&md5, sizeof (digest), digest);
102 *outlen = MD5_DIGEST_SIZE;
103 *out = xmemdup (*out, digest, *outlen);
104 #endif
105 return SHISHI_OK;