chiark / gitweb /
Doxygen for C test infrastructure
[disorder] / lib / authhash.c
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 3 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,
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.
14  * 
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/>.
17  */
18 /** @file lib/authhash.c @brief The authorization hash */
19
20 #include "common.h"
21
22 #include <stddef.h>
23 #include <gcrypt.h>
24
25 #include "hex.h"
26 #include "log.h"
27 #include "authhash.h"
28
29 /** @brief Structure of algorithm lookup table */
30 struct algorithm {
31   const char *name;
32   int id;
33 };
34
35 /** @brief Algorithm lookup table
36  *
37  * We don't use gcry_md_map_name() since that would import gcrypt's API into
38  * the disorder protocol.
39  */
40 static const struct algorithm algorithms[] = {
41   { "SHA1", GCRY_MD_SHA1 },
42   { "sha1", GCRY_MD_SHA1 },
43   { "SHA256", GCRY_MD_SHA256 },
44   { "sha256", GCRY_MD_SHA256 },
45   { "SHA384", GCRY_MD_SHA384 },
46   { "sha384", GCRY_MD_SHA384 },
47   { "SHA512", GCRY_MD_SHA512 },
48   { "sha512", GCRY_MD_SHA512 },
49 };
50
51 /** @brief Number of supported algorithms */
52 #define NALGORITHMS (sizeof algorithms / sizeof *algorithms)
53
54 /** @brief Perform the authorization hash function
55  * @param challenge Pointer to challange
56  * @param nchallenge Size of challenge
57  * @param password Password
58  * @param algo Algorithm to use
59  *
60  * Computes H(challenge|password) and returns it as a newly allocated hex
61  * string, or returns NULL on error.
62  */
63 const char *authhash(const void *challenge, size_t nchallenge,
64                      const char *password, const char *algo) {
65   gcrypt_hash_handle h;
66   const char *res;
67   size_t n;
68   int id;
69
70   assert(challenge != 0);
71   assert(password != 0);
72   assert(algo != 0);
73   for(n = 0; n < NALGORITHMS; ++n)
74     if(!strcmp(algo, algorithms[n].name))
75       break;
76   if(n >= NALGORITHMS)
77     return NULL;
78   id = algorithms[n].id;
79 #if HAVE_GCRY_ERROR_T
80   {
81     gcry_error_t e;
82     
83     if((e = gcry_md_open(&h, id, 0))) {
84       error(0, "gcry_md_open: %s", gcry_strerror(e));
85       return 0;
86     }
87   }
88 #else
89   h = gcry_md_open(id, 0);
90 #endif
91   gcry_md_write(h, password, strlen(password));
92   gcry_md_write(h, challenge, nchallenge);
93   res = hex(gcry_md_read(h, id), gcry_md_get_algo_dlen(id));
94   gcry_md_close(h);
95   return res;
96 }
97
98 /** @brief Return non-zero if @p algo is a valid algorithm */
99 int valid_authhash(const char *algo) {
100   size_t n;
101
102   for(n = 0; n < NALGORITHMS; ++n)
103     if(!strcmp(algo, algorithms[n].name))
104       return 1;
105   return 0;
106 }
107
108 /*
109 Local Variables:
110 c-basic-offset:2
111 comment-column:40
112 fill-column:79
113 End:
114 */