chiark / gitweb /
update CHANGES.html
[disorder] / lib / authhash.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2006, 2007 Richard Kettlewell
4 *
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 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20/** @file lib/authhash.c @brief The authorization hash */
21
22#include "common.h"
23
24#include <stddef.h>
25#include <gcrypt.h>
26
27#include "hex.h"
28#include "log.h"
29#include "authhash.h"
30
31/** @brief Structure of algorithm lookup table */
32struct algorithm {
33 const char *name;
34 int id;
35};
36
37/** @brief Algorithm lookup table
38 *
39 * We don't use gcry_md_map_name() since that would import gcrypt's API into
40 * the disorder protocol.
41 */
42static const struct algorithm algorithms[] = {
43 { "SHA1", GCRY_MD_SHA1 },
44 { "sha1", GCRY_MD_SHA1 },
45 { "SHA256", GCRY_MD_SHA256 },
46 { "sha256", GCRY_MD_SHA256 },
47 { "SHA384", GCRY_MD_SHA384 },
48 { "sha384", GCRY_MD_SHA384 },
49 { "SHA512", GCRY_MD_SHA512 },
50 { "sha512", GCRY_MD_SHA512 },
51};
52
53/** @brief Number of supported algorithms */
54#define NALGORITHMS (sizeof algorithms / sizeof *algorithms)
55
56/** @brief Perform the authorization hash function
57 * @param challenge Pointer to challange
58 * @param nchallenge Size of challenge
59 * @param password Password
60 * @param algo Algorithm to use
61 *
62 * Computes H(challenge|password) and returns it as a newly allocated hex
63 * string, or returns NULL on error.
64 */
65const char *authhash(const void *challenge, size_t nchallenge,
66 const char *password, const char *algo) {
67 gcrypt_hash_handle h;
68 const char *res;
69 size_t n;
70 int id;
71
72 assert(challenge != 0);
73 assert(password != 0);
74 assert(algo != 0);
75 for(n = 0; n < NALGORITHMS; ++n)
76 if(!strcmp(algo, algorithms[n].name))
77 break;
78 if(n >= NALGORITHMS)
79 return NULL;
80 id = algorithms[n].id;
81#if HAVE_GCRY_ERROR_T
82 {
83 gcry_error_t e;
84
85 if((e = gcry_md_open(&h, id, 0))) {
86 error(0, "gcry_md_open: %s", gcry_strerror(e));
87 return 0;
88 }
89 }
90#else
91 h = gcry_md_open(id, 0);
92#endif
93 gcry_md_write(h, password, strlen(password));
94 gcry_md_write(h, challenge, nchallenge);
95 res = hex(gcry_md_read(h, id), gcry_md_get_algo_dlen(id));
96 gcry_md_close(h);
97 return res;
98}
99
100/** @brief Return non-zero if @p algo is a valid algorithm */
101int valid_authhash(const char *algo) {
102 size_t n;
103
104 for(n = 0; n < NALGORITHMS; ++n)
105 if(!strcmp(algo, algorithms[n].name))
106 return 1;
107 return 0;
108}
109
110/*
111Local Variables:
112c-basic-offset:2
113comment-column:40
114fill-column:79
115End:
116*/