debian/: Add .gitignore
[disorder] / lib / authhash.c
1 /*
2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2006, 2007, 2009, 2013 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 #if HAVE_GCRYPT_H
24 # include <gcrypt.h>
25 #elif HAVE_BCRYPT_H
26 # include <bcrypt.h>
27 # pragma comment(lib, "bcrypt")
28 #else
29 # error No crypto API available
30 #endif
31
32 #include "hex.h"
33 #include "log.h"
34 #include "authhash.h"
35 #include "vector.h"
36
37 /** @brief Structure of algorithm lookup table */
38 struct algorithm {
39 /** @brief DisOrder algorithm name */
40 const char *name;
41
42 #if HAVE_GCRYPT_H
43 /** @brief gcrypt algorithm ID */
44 int id;
45 #elif HAVE_BCRYPT_H
46 /** @brief CNG algorithm ID */
47 const wchar_t *id;
48 #endif
49
50 };
51
52 /** @brief Algorithm lookup table
53 *
54 * We don't use gcry_md_map_name() since that would import gcrypt's API into
55 * the disorder protocol.
56 */
57 static const struct algorithm algorithms[] = {
58 #if HAVE_GCRYPT_H
59 { "SHA1", GCRY_MD_SHA1 },
60 { "sha1", GCRY_MD_SHA1 },
61 { "SHA256", GCRY_MD_SHA256 },
62 { "sha256", GCRY_MD_SHA256 },
63 { "SHA384", GCRY_MD_SHA384 },
64 { "sha384", GCRY_MD_SHA384 },
65 { "SHA512", GCRY_MD_SHA512 },
66 { "sha512", GCRY_MD_SHA512 },
67 #elif HAVE_BCRYPT_H
68 { "SHA1", BCRYPT_SHA1_ALGORITHM },
69 { "sha1", BCRYPT_SHA1_ALGORITHM },
70 { "SHA256", BCRYPT_SHA256_ALGORITHM },
71 { "sha256", BCRYPT_SHA256_ALGORITHM },
72 { "SHA384", BCRYPT_SHA384_ALGORITHM },
73 { "sha384", BCRYPT_SHA384_ALGORITHM },
74 { "SHA512", BCRYPT_SHA512_ALGORITHM },
75 { "sha512", BCRYPT_SHA512_ALGORITHM },
76 #endif
77 };
78
79 /** @brief Number of supported algorithms */
80 #define NALGORITHMS (sizeof algorithms / sizeof *algorithms)
81
82 /** @brief Perform the authorization hash function
83 * @param challenge Pointer to challange
84 * @param nchallenge Size of challenge
85 * @param password Password
86 * @param algo Algorithm to use
87 * @return Hex string or NULL on error
88 *
89 * Computes H(challenge|password) and returns it as a newly allocated hex
90 * string, or returns NULL on error.
91 */
92 char *authhash(const void *challenge, size_t nchallenge,
93 const char *password, const char *algo) {
94 #if HAVE_GCRYPT_H
95 gcrypt_hash_handle h;
96 int id;
97 #elif HAVE_BCRYPT_H
98 BCRYPT_ALG_HANDLE alg = 0;
99 BCRYPT_HASH_HANDLE hash = 0;
100 DWORD hashlen, hashlenlen;
101 PBYTE hashed = 0;
102 NTSTATUS rc;
103 struct dynstr d;
104 #endif
105 char *res;
106 size_t n;
107
108 assert(challenge != 0);
109 assert(password != 0);
110 assert(algo != 0);
111 for(n = 0; n < NALGORITHMS; ++n)
112 if(!strcmp(algo, algorithms[n].name))
113 break;
114 if(n >= NALGORITHMS)
115 return NULL;
116 #if HAVE_GCRYPT_H
117 id = algorithms[n].id;
118 #if HAVE_GCRY_ERROR_T
119 {
120 gcry_error_t e;
121
122 if((e = gcry_md_open(&h, id, 0))) {
123 disorder_error(0, "gcry_md_open: %s", gcry_strerror(e));
124 return 0;
125 }
126 }
127 #else
128 h = gcry_md_open(id, 0);
129 #endif
130 gcry_md_write(h, password, strlen(password));
131 gcry_md_write(h, challenge, nchallenge);
132 res = hex(gcry_md_read(h, id), gcry_md_get_algo_dlen(id));
133 gcry_md_close(h);
134 #elif HAVE_BCRYPT_H
135 dynstr_init(&d);
136 dynstr_append_string(&d, password);
137 dynstr_append_bytes(&d, challenge, nchallenge);
138 #define DO(fn, args) do { \
139 if((rc = fn args)) { disorder_error(0, "%s: %d", #fn, rc); goto error; } \
140 } while(0)
141 res = NULL;
142 DO(BCryptOpenAlgorithmProvider, (&alg, algorithms[n].id, NULL, 0));
143 DO(BCryptGetProperty, (alg, BCRYPT_HASH_LENGTH, (PBYTE)&hashlen, sizeof hashlen, &hashlenlen, 0));
144 DO(BCryptCreateHash, (alg, &hash, NULL, 0, NULL, 0, 0));
145 DO(BCryptHashData, (hash, d.vec, d.nvec, 0));
146 hashed = xmalloc(hashlen);
147 DO(BCryptFinishHash, (hash, hashed, hashlen, 0));
148 res = hex(hashed, hashlen);
149 error:
150 if(hash) BCryptDestroyHash(hash);
151 if(alg) BCryptCloseAlgorithmProvider(alg, 0);
152 xfree(hashed);
153 #endif
154 return res;
155 }
156
157 /** @brief Return non-zero if @p algo is a valid algorithm */
158 int valid_authhash(const char *algo) {
159 size_t n;
160
161 for(n = 0; n < NALGORITHMS; ++n)
162 if(!strcmp(algo, algorithms[n].name))
163 return 1;
164 return 0;
165 }
166
167 /*
168 Local Variables:
169 c-basic-offset:2
170 comment-column:40
171 fill-column:79
172 End:
173 */