summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortedu <>2014-03-19 02:43:03 +0000
committertedu <>2014-03-19 02:43:03 +0000
commit6599f49f9dc029f7f82060247daceed15250609d (patch)
tree1f0613958c96667cba4698dec0d7fcc638f73141 /src
parent05d1f3009a063727afdc1c02210bb8449b1d86d3 (diff)
downloadopenbsd-6599f49f9dc029f7f82060247daceed15250609d.tar.gz
openbsd-6599f49f9dc029f7f82060247daceed15250609d.tar.bz2
openbsd-6599f49f9dc029f7f82060247daceed15250609d.zip
consolidate the base64 code in one place, and remove inadequate test code
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/crypt/bcrypt.c142
1 files changed, 56 insertions, 86 deletions
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c
index b108cfe04c..2df2508246 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.29 2014/02/24 19:45:43 tedu Exp $ */ 1/* $OpenBSD: bcrypt.c,v 1.30 2014/03/19 02:43:03 tedu Exp $ */
2 2
3/* 3/*
4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
@@ -72,61 +72,6 @@ static char encrypted[_PASSWORD_LEN];
72static char gsalt[7 + (BCRYPT_MAXSALT * 4 + 2) / 3 + 1]; 72static char gsalt[7 + (BCRYPT_MAXSALT * 4 + 2) / 3 + 1];
73static char error[] = ":"; 73static char error[] = ":";
74 74
75const static u_int8_t Base64Code[] =
76"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
77
78const static u_int8_t index_64[128] = {
79 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
80 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
81 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
82 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
83 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
84 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
85 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
86 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
87 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
88 255, 255, 255, 255, 255, 255, 28, 29, 30,
89 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
90 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
91 51, 52, 53, 255, 255, 255, 255, 255
92};
93#define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
94
95static void
96decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data)
97{
98 u_int8_t *bp = buffer;
99 u_int8_t *p = data;
100 u_int8_t c1, c2, c3, c4;
101 while (bp < buffer + len) {
102 c1 = CHAR64(*p);
103 c2 = CHAR64(*(p + 1));
104
105 /* Invalid data */
106 if (c1 == 255 || c2 == 255)
107 break;
108
109 *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
110 if (bp >= buffer + len)
111 break;
112
113 c3 = CHAR64(*(p + 2));
114 if (c3 == 255)
115 break;
116
117 *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
118 if (bp >= buffer + len)
119 break;
120
121 c4 = CHAR64(*(p + 3));
122 if (c4 == 255)
123 break;
124 *bp++ = ((c3 & 0x03) << 6) | c4;
125
126 p += 4;
127 }
128}
129
130static void 75static void
131encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr) 76encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr)
132{ 77{
@@ -284,6 +229,61 @@ bcrypt(const char *key, const char *salt)
284 return encrypted; 229 return encrypted;
285} 230}
286 231
232const static u_int8_t Base64Code[] =
233"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
234
235const static u_int8_t index_64[128] = {
236 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
237 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
238 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
239 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
240 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
241 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
242 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
243 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
244 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
245 255, 255, 255, 255, 255, 255, 28, 29, 30,
246 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
247 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
248 51, 52, 53, 255, 255, 255, 255, 255
249};
250#define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
251
252static void
253decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data)
254{
255 u_int8_t *bp = buffer;
256 u_int8_t *p = data;
257 u_int8_t c1, c2, c3, c4;
258 while (bp < buffer + len) {
259 c1 = CHAR64(*p);
260 c2 = CHAR64(*(p + 1));
261
262 /* Invalid data */
263 if (c1 == 255 || c2 == 255)
264 break;
265
266 *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
267 if (bp >= buffer + len)
268 break;
269
270 c3 = CHAR64(*(p + 2));
271 if (c3 == 255)
272 break;
273
274 *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
275 if (bp >= buffer + len)
276 break;
277
278 c4 = CHAR64(*(p + 3));
279 if (c4 == 255)
280 break;
281 *bp++ = ((c3 & 0x03) << 6) | c4;
282
283 p += 4;
284 }
285}
286
287static void 287static void
288encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) 288encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
289{ 289{
@@ -313,33 +313,3 @@ encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
313 } 313 }
314 *bp = '\0'; 314 *bp = '\0';
315} 315}
316#if 0
317void
318main()
319{
320 char blubber[73];
321 char salt[100];
322 char *p;
323 salt[0] = '$';
324 salt[1] = BCRYPT_VERSION;
325 salt[2] = '$';
326
327 snprintf(salt + 3, 4, "%2.2u$", 5);
328
329 printf("24 bytes of salt: ");
330 fgets(salt + 6, sizeof(salt) - 6, stdin);
331 salt[99] = 0;
332 printf("72 bytes of password: ");
333 fpurge(stdin);
334 fgets(blubber, sizeof(blubber), stdin);
335 blubber[72] = 0;
336
337 p = crypt(blubber, salt);
338 printf("Passwd entry: %s\n\n", p);
339
340 p = bcrypt_gensalt(5);
341 printf("Generated salt: %s\n", p);
342 p = crypt(blubber, p);
343 printf("Passwd entry: %s\n", p);
344}
345#endif