diff options
| author | tedu <> | 2014-04-08 20:14:25 +0000 |
|---|---|---|
| committer | tedu <> | 2014-04-08 20:14:25 +0000 |
| commit | 77c6e64507cecd9e6112c84cf53dfeae4341f4ec (patch) | |
| tree | 77acb59ef8676365f0f2ff3402970b81b9c155e1 /src/lib/libc/crypt/bcrypt.c | |
| parent | f7eb965a357993008aede65496a25104a2f6451e (diff) | |
| download | openbsd-77c6e64507cecd9e6112c84cf53dfeae4341f4ec.tar.gz openbsd-77c6e64507cecd9e6112c84cf53dfeae4341f4ec.tar.bz2 openbsd-77c6e64507cecd9e6112c84cf53dfeae4341f4ec.zip | |
use char * for strings, saving casts. add return codes to base64 functions
Diffstat (limited to 'src/lib/libc/crypt/bcrypt.c')
| -rw-r--r-- | src/lib/libc/crypt/bcrypt.c | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c index 7760ac7c0c..d7af344b97 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.36 2014/03/24 00:00:29 tedu Exp $ */ | 1 | /* $OpenBSD: bcrypt.c,v 1.37 2014/04/08 20:14:25 tedu Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> | 4 | * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> |
| @@ -52,8 +52,8 @@ | |||
| 52 | 52 | ||
| 53 | char *bcrypt_gensalt(u_int8_t); | 53 | char *bcrypt_gensalt(u_int8_t); |
| 54 | 54 | ||
| 55 | static void encode_base64(u_int8_t *, u_int8_t *, u_int16_t); | 55 | static int encode_base64(char *, const u_int8_t *, size_t); |
| 56 | static void decode_base64(u_int8_t *, u_int16_t, u_int8_t *); | 56 | static int decode_base64(u_int8_t *, size_t, const char *); |
| 57 | 57 | ||
| 58 | /* | 58 | /* |
| 59 | * Generates a salt for this version of crypt. | 59 | * Generates a salt for this version of crypt. |
| @@ -74,7 +74,7 @@ bcrypt_initsalt(int log_rounds, uint8_t *salt, size_t saltbuflen) | |||
| 74 | log_rounds = 31; | 74 | log_rounds = 31; |
| 75 | 75 | ||
| 76 | snprintf(salt, saltbuflen, "$2a$%2.2u$", log_rounds); | 76 | snprintf(salt, saltbuflen, "$2a$%2.2u$", log_rounds); |
| 77 | encode_base64((uint8_t *)salt + 7, csalt, sizeof(csalt)); | 77 | encode_base64(salt + 7, csalt, sizeof(csalt)); |
| 78 | 78 | ||
| 79 | return 0; | 79 | return 0; |
| 80 | } | 80 | } |
| @@ -141,7 +141,7 @@ bcrypt_hashpass(const char *key, const char *salt, char *encrypted, | |||
| 141 | return -1; | 141 | return -1; |
| 142 | 142 | ||
| 143 | /* We dont want the base64 salt but the raw data */ | 143 | /* We dont want the base64 salt but the raw data */ |
| 144 | decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt); | 144 | decode_base64(csalt, BCRYPT_MAXSALT, salt); |
| 145 | salt_len = BCRYPT_MAXSALT; | 145 | salt_len = BCRYPT_MAXSALT; |
| 146 | if (minor <= 'a') | 146 | if (minor <= 'a') |
| 147 | key_len = (u_int8_t)(strlen(key) + (minor >= 'a' ? 1 : 0)); | 147 | key_len = (u_int8_t)(strlen(key) + (minor >= 'a' ? 1 : 0)); |
| @@ -194,8 +194,8 @@ bcrypt_hashpass(const char *key, const char *salt, char *encrypted, | |||
| 194 | 194 | ||
| 195 | snprintf(encrypted + i, 4, "%2.2u$", logr); | 195 | snprintf(encrypted + i, 4, "%2.2u$", logr); |
| 196 | 196 | ||
| 197 | encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT); | 197 | encode_base64(encrypted + i + 3, csalt, BCRYPT_MAXSALT); |
| 198 | encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext, | 198 | encode_base64(encrypted + strlen(encrypted), ciphertext, |
| 199 | 4 * BCRYPT_BLOCKS - 1); | 199 | 4 * BCRYPT_BLOCKS - 1); |
| 200 | memset(&state, 0, sizeof(state)); | 200 | memset(&state, 0, sizeof(state)); |
| 201 | memset(ciphertext, 0, sizeof(ciphertext)); | 201 | memset(ciphertext, 0, sizeof(ciphertext)); |
| @@ -260,19 +260,23 @@ const static u_int8_t index_64[128] = { | |||
| 260 | }; | 260 | }; |
| 261 | #define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)]) | 261 | #define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)]) |
| 262 | 262 | ||
| 263 | static void | 263 | /* |
| 264 | decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data) | 264 | * read buflen (after decoding) bytes of data from b64data |
| 265 | */ | ||
| 266 | static int | ||
| 267 | decode_base64(u_int8_t *buffer, size_t len, const char *b64data) | ||
| 265 | { | 268 | { |
| 266 | u_int8_t *bp = buffer; | 269 | u_int8_t *bp = buffer; |
| 267 | u_int8_t *p = data; | 270 | const u_int8_t *p = b64data; |
| 268 | u_int8_t c1, c2, c3, c4; | 271 | u_int8_t c1, c2, c3, c4; |
| 272 | |||
| 269 | while (bp < buffer + len) { | 273 | while (bp < buffer + len) { |
| 270 | c1 = CHAR64(*p); | 274 | c1 = CHAR64(*p); |
| 271 | c2 = CHAR64(*(p + 1)); | 275 | c2 = CHAR64(*(p + 1)); |
| 272 | 276 | ||
| 273 | /* Invalid data */ | 277 | /* Invalid data */ |
| 274 | if (c1 == 255 || c2 == 255) | 278 | if (c1 == 255 || c2 == 255) |
| 275 | break; | 279 | return -1; |
| 276 | 280 | ||
| 277 | *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4); | 281 | *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4); |
| 278 | if (bp >= buffer + len) | 282 | if (bp >= buffer + len) |
| @@ -293,14 +297,20 @@ decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data) | |||
| 293 | 297 | ||
| 294 | p += 4; | 298 | p += 4; |
| 295 | } | 299 | } |
| 300 | return 0; | ||
| 296 | } | 301 | } |
| 297 | 302 | ||
| 298 | static void | 303 | /* |
| 299 | encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) | 304 | * Turn len bytes of data into base64 encoded data. |
| 305 | * This works without = padding. | ||
| 306 | */ | ||
| 307 | static int | ||
| 308 | encode_base64(char *b64buffer, const u_int8_t *data, size_t len) | ||
| 300 | { | 309 | { |
| 301 | u_int8_t *bp = buffer; | 310 | u_int8_t *bp = b64buffer; |
| 302 | u_int8_t *p = data; | 311 | const u_int8_t *p = data; |
| 303 | u_int8_t c1, c2; | 312 | u_int8_t c1, c2; |
| 313 | |||
| 304 | while (p < data + len) { | 314 | while (p < data + len) { |
| 305 | c1 = *p++; | 315 | c1 = *p++; |
| 306 | *bp++ = Base64Code[(c1 >> 2)]; | 316 | *bp++ = Base64Code[(c1 >> 2)]; |
| @@ -323,6 +333,7 @@ encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) | |||
| 323 | *bp++ = Base64Code[c2 & 0x3f]; | 333 | *bp++ = Base64Code[c2 & 0x3f]; |
| 324 | } | 334 | } |
| 325 | *bp = '\0'; | 335 | *bp = '\0'; |
| 336 | return 0; | ||
| 326 | } | 337 | } |
| 327 | 338 | ||
| 328 | /* | 339 | /* |
