summaryrefslogtreecommitdiff
path: root/src/lib/libc/crypt
diff options
context:
space:
mode:
authortedu <>2014-11-24 22:47:01 +0000
committertedu <>2014-11-24 22:47:01 +0000
commitbea5237492c48b30ca9a1fc350d30cadc1260e7d (patch)
treece006477c1da3420df8cc114ee2f4a1574cd427d /src/lib/libc/crypt
parentb655215ea66ce8a38a7f44725eaa0dcd1fd21e41 (diff)
downloadopenbsd-bea5237492c48b30ca9a1fc350d30cadc1260e7d.tar.gz
openbsd-bea5237492c48b30ca9a1fc350d30cadc1260e7d.tar.bz2
openbsd-bea5237492c48b30ca9a1fc350d30cadc1260e7d.zip
introduce a hashspace define and check that there's enough space to
write out a hash. also simplify writing out the hash.
Diffstat (limited to 'src/lib/libc/crypt')
-rw-r--r--src/lib/libc/crypt/bcrypt.c25
1 files changed, 10 insertions, 15 deletions
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c
index 9b0001de0f..94758ca40b 100644
--- a/src/lib/libc/crypt/bcrypt.c
+++ b/src/lib/libc/crypt/bcrypt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bcrypt.c,v 1.45 2014/07/20 04:22:34 guenther Exp $ */ 1/* $OpenBSD: bcrypt.c,v 1.46 2014/11/24 22:47:01 tedu Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> 4 * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
@@ -50,6 +50,7 @@
50#define BCRYPT_MINLOGROUNDS 4 /* we have log2(rounds) in salt */ 50#define BCRYPT_MINLOGROUNDS 4 /* we have log2(rounds) in salt */
51 51
52#define BCRYPT_SALTSPACE (7 + (BCRYPT_MAXSALT * 4 + 2) / 3 + 1) 52#define BCRYPT_SALTSPACE (7 + (BCRYPT_MAXSALT * 4 + 2) / 3 + 1)
53#define BCRYPT_HASHSPACE 61
53 54
54char *bcrypt_gensalt(u_int8_t); 55char *bcrypt_gensalt(u_int8_t);
55 56
@@ -96,6 +97,9 @@ bcrypt_hashpass(const char *key, const char *salt, char *encrypted,
96 u_int8_t csalt[BCRYPT_MAXSALT]; 97 u_int8_t csalt[BCRYPT_MAXSALT];
97 u_int32_t cdata[BCRYPT_BLOCKS]; 98 u_int32_t cdata[BCRYPT_BLOCKS];
98 99
100 if (encryptedlen < BCRYPT_HASHSPACE)
101 return -1;
102
99 /* Check and discard "$" identifier */ 103 /* Check and discard "$" identifier */
100 if (salt[0] != '$') 104 if (salt[0] != '$')
101 return -1; 105 return -1;
@@ -177,17 +181,9 @@ bcrypt_hashpass(const char *key, const char *salt, char *encrypted,
177 } 181 }
178 182
179 183
180 i = 0; 184 snprintf(encrypted, 8, "$2%c$%2.2u$", minor, logr);
181 encrypted[i++] = '$'; 185 encode_base64(encrypted + 7, csalt, BCRYPT_MAXSALT);
182 encrypted[i++] = BCRYPT_VERSION; 186 encode_base64(encrypted + 7 + 22, ciphertext, 4 * BCRYPT_BLOCKS - 1);
183 encrypted[i++] = minor;
184 encrypted[i++] = '$';
185
186 snprintf(encrypted + i, 4, "%2.2u$", logr);
187
188 encode_base64(encrypted + i + 3, csalt, BCRYPT_MAXSALT);
189 encode_base64(encrypted + strlen(encrypted), ciphertext,
190 4 * BCRYPT_BLOCKS - 1);
191 explicit_bzero(&state, sizeof(state)); 187 explicit_bzero(&state, sizeof(state));
192 explicit_bzero(ciphertext, sizeof(ciphertext)); 188 explicit_bzero(ciphertext, sizeof(ciphertext));
193 explicit_bzero(csalt, sizeof(csalt)); 189 explicit_bzero(csalt, sizeof(csalt));
@@ -216,7 +212,7 @@ bcrypt_newhash(const char *pass, int log_rounds, char *hash, size_t hashlen)
216int 212int
217bcrypt_checkpass(const char *pass, const char *goodhash) 213bcrypt_checkpass(const char *pass, const char *goodhash)
218{ 214{
219 char hash[_PASSWORD_LEN]; 215 char hash[BCRYPT_HASHSPACE];
220 216
221 if (bcrypt_hashpass(pass, goodhash, hash, sizeof(hash)) != 0) 217 if (bcrypt_hashpass(pass, goodhash, hash, sizeof(hash)) != 0)
222 return -1; 218 return -1;
@@ -345,7 +341,7 @@ bcrypt_gensalt(u_int8_t log_rounds)
345char * 341char *
346bcrypt(const char *pass, const char *salt) 342bcrypt(const char *pass, const char *salt)
347{ 343{
348 static char gencrypted[_PASSWORD_LEN]; 344 static char gencrypted[BCRYPT_HASHSPACE];
349 static char gerror[2]; 345 static char gerror[2];
350 346
351 /* How do I handle errors ? Return ':' */ 347 /* How do I handle errors ? Return ':' */
@@ -355,4 +351,3 @@ bcrypt(const char *pass, const char *salt)
355 351
356 return gencrypted; 352 return gencrypted;
357} 353}
358