2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2006, 2007 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/authhash.c @brief The authorization hash */
29 /** @brief Structure of algorithm lookup table */
31 /** @brief DisOrder algorithm name */
34 /** @brief gcrypt algorithm ID */
38 /** @brief Algorithm lookup table
40 * We don't use gcry_md_map_name() since that would import gcrypt's API into
41 * the disorder protocol.
43 static const struct algorithm algorithms
[] = {
44 { "SHA1", GCRY_MD_SHA1
},
45 { "sha1", GCRY_MD_SHA1
},
46 { "SHA256", GCRY_MD_SHA256
},
47 { "sha256", GCRY_MD_SHA256
},
48 { "SHA384", GCRY_MD_SHA384
},
49 { "sha384", GCRY_MD_SHA384
},
50 { "SHA512", GCRY_MD_SHA512
},
51 { "sha512", GCRY_MD_SHA512
},
54 /** @brief Number of supported algorithms */
55 #define NALGORITHMS (sizeof algorithms / sizeof *algorithms)
57 /** @brief Perform the authorization hash function
58 * @param challenge Pointer to challange
59 * @param nchallenge Size of challenge
60 * @param password Password
61 * @param algo Algorithm to use
62 * @return Hex string or NULL on error
64 * Computes H(challenge|password) and returns it as a newly allocated hex
65 * string, or returns NULL on error.
67 const char *authhash(const void *challenge
, size_t nchallenge
,
68 const char *password
, const char *algo
) {
74 assert(challenge
!= 0);
75 assert(password
!= 0);
77 for(n
= 0; n
< NALGORITHMS
; ++n
)
78 if(!strcmp(algo
, algorithms
[n
].name
))
82 id
= algorithms
[n
].id
;
87 if((e
= gcry_md_open(&h
, id
, 0))) {
88 error(0, "gcry_md_open: %s", gcry_strerror(e
));
93 h
= gcry_md_open(id
, 0);
95 gcry_md_write(h
, password
, strlen(password
));
96 gcry_md_write(h
, challenge
, nchallenge
);
97 res
= hex(gcry_md_read(h
, id
), gcry_md_get_algo_dlen(id
));
102 /** @brief Return non-zero if @p algo is a valid algorithm */
103 int valid_authhash(const char *algo
) {
106 for(n
= 0; n
< NALGORITHMS
; ++n
)
107 if(!strcmp(algo
, algorithms
[n
].name
))