2 * This file is part of DisOrder
3 * Copyright (C) 2004, 2006, 2007, 2009, 2013 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 */
27 # pragma comment(lib, "bcrypt")
29 # error No crypto API available
37 /** @brief Structure of algorithm lookup table */
39 /** @brief DisOrder algorithm name */
43 /** @brief gcrypt algorithm ID */
46 /** @brief CNG algorithm ID */
52 /** @brief Algorithm lookup table
54 * We don't use gcry_md_map_name() since that would import gcrypt's API into
55 * the disorder protocol.
57 static const struct algorithm algorithms
[] = {
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
},
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
},
79 /** @brief Number of supported algorithms */
80 #define NALGORITHMS (sizeof algorithms / sizeof *algorithms)
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
89 * Computes H(challenge|password) and returns it as a newly allocated hex
90 * string, or returns NULL on error.
92 char *authhash(const void *challenge
, size_t nchallenge
,
93 const char *password
, const char *algo
) {
98 BCRYPT_ALG_HANDLE alg
= 0;
99 BCRYPT_HASH_HANDLE hash
= 0;
100 DWORD hashlen
, hashlenlen
;
108 assert(challenge
!= 0);
109 assert(password
!= 0);
111 for(n
= 0; n
< NALGORITHMS
; ++n
)
112 if(!strcmp(algo
, algorithms
[n
].name
))
117 id
= algorithms
[n
].id
;
118 #if HAVE_GCRY_ERROR_T
122 if((e
= gcry_md_open(&h
, id
, 0))) {
123 disorder_error(0, "gcry_md_open: %s", gcry_strerror(e
));
128 h
= gcry_md_open(id
, 0);
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
));
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; } \
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
);
150 if(hash
) BCryptDestroyHash(hash
);
151 if(alg
) BCryptCloseAlgorithmProvider(alg
, 0);
157 /** @brief Return non-zero if @p algo is a valid algorithm */
158 int valid_authhash(const char *algo
) {
161 for(n
= 0; n
< NALGORITHMS
; ++n
)
162 if(!strcmp(algo
, algorithms
[n
].name
))