bcrypt 3.1.12-java → 3.1.16-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ /*
2
+ * Written by Solar Designer <solar at openwall.com> in 2000-2011.
3
+ * No copyright is claimed, and the software is hereby placed in the public
4
+ * domain. In case this attempt to disclaim copyright and place the software
5
+ * in the public domain is deemed null and void, then the software is
6
+ * Copyright (c) 2000-2011 Solar Designer and it is hereby released to the
7
+ * general public under the following terms:
8
+ *
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted.
11
+ *
12
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
13
+ *
14
+ * See crypt_blowfish.c for more information.
15
+ */
16
+
17
+ #ifndef _CRYPT_BLOWFISH_H
18
+ #define _CRYPT_BLOWFISH_H
19
+
20
+ extern int _crypt_output_magic(const char *setting, char *output, int size);
21
+ extern char *_crypt_blowfish_rn(const char *key, const char *setting,
22
+ char *output, int size);
23
+ extern char *_crypt_gensalt_blowfish_rn(const char *prefix,
24
+ unsigned long count,
25
+ const char *input, int size, char *output, int output_size);
26
+
27
+ #endif
@@ -1,5 +1,16 @@
1
1
  /*
2
- * Written by Solar Designer and placed in the public domain.
2
+ * Written by Solar Designer <solar at openwall.com> in 2000-2011.
3
+ * No copyright is claimed, and the software is hereby placed in the public
4
+ * domain. In case this attempt to disclaim copyright and place the software
5
+ * in the public domain is deemed null and void, then the software is
6
+ * Copyright (c) 2000-2011 Solar Designer and it is hereby released to the
7
+ * general public under the following terms:
8
+ *
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted.
11
+ *
12
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
13
+ *
3
14
  * See crypt_blowfish.c for more information.
4
15
  *
5
16
  * This file contains salt generation functions for the traditional and
@@ -14,19 +25,17 @@
14
25
  #define __set_errno(val) errno = (val)
15
26
  #endif
16
27
 
17
- #undef __CONST
18
- #ifdef __GNUC__
19
- #define __CONST __const
20
- #else
21
- #define __CONST
22
- #endif
28
+ /* Just to make sure the prototypes match the actual definitions */
29
+ #include "crypt_gensalt.h"
23
30
 
24
- unsigned char _crypt_itoa64[64 + 1] =
31
+ const unsigned char _crypt_itoa64[64 + 1] =
25
32
  "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
26
33
 
27
- char *_crypt_gensalt_traditional_rn(unsigned long count,
28
- __CONST char *input, int size, char *output, int output_size)
34
+ char *_crypt_gensalt_traditional_rn(const char *prefix, unsigned long count,
35
+ const char *input, int size, char *output, int output_size)
29
36
  {
37
+ (void) prefix;
38
+
30
39
  if (size < 2 || output_size < 2 + 1 || (count && count != 25)) {
31
40
  if (output_size > 0) output[0] = '\0';
32
41
  __set_errno((output_size < 2 + 1) ? ERANGE : EINVAL);
@@ -40,11 +49,13 @@ char *_crypt_gensalt_traditional_rn(unsigned long count,
40
49
  return output;
41
50
  }
42
51
 
43
- char *_crypt_gensalt_extended_rn(unsigned long count,
44
- __CONST char *input, int size, char *output, int output_size)
52
+ char *_crypt_gensalt_extended_rn(const char *prefix, unsigned long count,
53
+ const char *input, int size, char *output, int output_size)
45
54
  {
46
55
  unsigned long value;
47
56
 
57
+ (void) prefix;
58
+
48
59
  /* Even iteration counts make it easier to detect weak DES keys from a look
49
60
  * at the hash, so they should be avoided */
50
61
  if (size < 3 || output_size < 1 + 4 + 4 + 1 ||
@@ -73,11 +84,13 @@ char *_crypt_gensalt_extended_rn(unsigned long count,
73
84
  return output;
74
85
  }
75
86
 
76
- char *_crypt_gensalt_md5_rn(unsigned long count,
77
- __CONST char *input, int size, char *output, int output_size)
87
+ char *_crypt_gensalt_md5_rn(const char *prefix, unsigned long count,
88
+ const char *input, int size, char *output, int output_size)
78
89
  {
79
90
  unsigned long value;
80
91
 
92
+ (void) prefix;
93
+
81
94
  if (size < 3 || output_size < 3 + 4 + 1 || (count && count != 1000)) {
82
95
  if (output_size > 0) output[0] = '\0';
83
96
  __set_errno((output_size < 3 + 4 + 1) ? ERANGE : EINVAL);
@@ -0,0 +1,30 @@
1
+ /*
2
+ * Written by Solar Designer <solar at openwall.com> in 2000-2011.
3
+ * No copyright is claimed, and the software is hereby placed in the public
4
+ * domain. In case this attempt to disclaim copyright and place the software
5
+ * in the public domain is deemed null and void, then the software is
6
+ * Copyright (c) 2000-2011 Solar Designer and it is hereby released to the
7
+ * general public under the following terms:
8
+ *
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted.
11
+ *
12
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
13
+ *
14
+ * See crypt_blowfish.c for more information.
15
+ */
16
+
17
+ #ifndef _CRYPT_GENSALT_H
18
+ #define _CRYPT_GENSALT_H
19
+
20
+ extern const unsigned char _crypt_itoa64[];
21
+ extern char *_crypt_gensalt_traditional_rn(const char *prefix,
22
+ unsigned long count,
23
+ const char *input, int size, char *output, int output_size);
24
+ extern char *_crypt_gensalt_extended_rn(const char *prefix,
25
+ unsigned long count,
26
+ const char *input, int size, char *output, int output_size);
27
+ extern char *_crypt_gensalt_md5_rn(const char *prefix, unsigned long count,
28
+ const char *input, int size, char *output, int output_size);
29
+
30
+ #endif
@@ -11,6 +11,12 @@ if RUBY_PLATFORM == "java"
11
11
  exit 0
12
12
  else
13
13
  require "mkmf"
14
+
15
+ # From Openwall's crypt_blowfish Makefile.
16
+ # This is `bcrypt_ext` (our extension) + CRYPT_OBJS from that Makefile.
17
+ $objs = %w(bcrypt_ext.o crypt_blowfish.o x86.o crypt_gensalt.o wrapper.o)
18
+
19
+ $defs << "-D__SKIP_GNU"
14
20
  dir_config("bcrypt_ext")
15
21
  create_makefile("bcrypt_ext")
16
22
  end
@@ -1,35 +1,43 @@
1
1
  /*
2
- * Written by Solar Designer and placed in the public domain.
2
+ * Written by Solar Designer <solar at openwall.com> in 2000-2011.
3
+ * No copyright is claimed, and the software is hereby placed in the public
4
+ * domain. In case this attempt to disclaim copyright and place the software
5
+ * in the public domain is deemed null and void, then the software is
6
+ * Copyright (c) 2000-2011 Solar Designer and it is hereby released to the
7
+ * general public under the following terms:
8
+ *
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted.
11
+ *
12
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
13
+ *
3
14
  * See crypt_blowfish.c for more information.
4
15
  */
5
16
 
6
17
  #ifndef _OW_CRYPT_H
7
18
  #define _OW_CRYPT_H
8
19
 
9
- #undef __CONST
10
- #if defined __GNUC__
11
- #define __CONST __const
12
- #elif defined _MSC_VER
13
- #define __CONST const
14
- #else
20
+ #ifndef __GNUC__
21
+ #undef __const
22
+ #define __const const
15
23
  #endif
16
24
 
17
25
  #ifndef __SKIP_GNU
18
- extern char *crypt(__CONST char *key, __CONST char *setting);
19
- extern char *crypt_r(__CONST char *key, __CONST char *setting, void *data);
26
+ extern char *crypt(__const char *key, __const char *setting);
27
+ extern char *crypt_r(__const char *key, __const char *setting, void *data);
20
28
  #endif
21
29
 
22
30
  #ifndef __SKIP_OW
23
- extern char *crypt_rn(__CONST char *key, __CONST char *setting,
31
+ extern char *crypt_rn(__const char *key, __const char *setting,
24
32
  void *data, int size);
25
- extern char *crypt_ra(__CONST char *key, __CONST char *setting,
33
+ extern char *crypt_ra(__const char *key, __const char *setting,
26
34
  void **data, int *size);
27
- extern char *crypt_gensalt(__CONST char *prefix, unsigned long count,
28
- __CONST char *input, int size);
29
- extern char *crypt_gensalt_rn(__CONST char *prefix, unsigned long count,
30
- __CONST char *input, int size, char *output, int output_size);
31
- extern char *crypt_gensalt_ra(__CONST char *prefix, unsigned long count,
32
- __CONST char *input, int size);
35
+ extern char *crypt_gensalt(__const char *prefix, unsigned long count,
36
+ __const char *input, int size);
37
+ extern char *crypt_gensalt_rn(__const char *prefix, unsigned long count,
38
+ __const char *input, int size, char *output, int output_size);
39
+ extern char *crypt_gensalt_ra(__const char *prefix, unsigned long count,
40
+ __const char *input, int size);
33
41
  #endif
34
42
 
35
43
  #endif
@@ -1,11 +1,25 @@
1
1
  /*
2
- * Written by Solar Designer and placed in the public domain.
2
+ * Written by Solar Designer <solar at openwall.com> in 2000-2014.
3
+ * No copyright is claimed, and the software is hereby placed in the public
4
+ * domain. In case this attempt to disclaim copyright and place the software
5
+ * in the public domain is deemed null and void, then the software is
6
+ * Copyright (c) 2000-2014 Solar Designer and it is hereby released to the
7
+ * general public under the following terms:
8
+ *
9
+ * Redistribution and use in source and binary forms, with or without
10
+ * modification, are permitted.
11
+ *
12
+ * There's ABSOLUTELY NO WARRANTY, express or implied.
13
+ *
3
14
  * See crypt_blowfish.c for more information.
4
15
  */
5
16
 
6
17
  #include <stdlib.h>
7
18
  #include <string.h>
8
19
 
20
+ /* Redefine strdup to ruby_strdup in case string.h doesn't export it. */
21
+ #include <ruby/util.h>
22
+
9
23
  #include <errno.h>
10
24
  #ifndef __set_errno
11
25
  #define __set_errno(val) errno = (val)
@@ -23,13 +37,6 @@
23
37
  #endif
24
38
  #endif
25
39
 
26
- #include <ruby.h>
27
- #ifdef HAVE_RUBY_UTIL_H
28
- #include <ruby/util.h>
29
- #else
30
- #include <util.h>
31
- #endif
32
-
33
40
  #define CRYPT_OUTPUT_SIZE (7 + 22 + 31 + 1)
34
41
  #define CRYPT_GENSALT_OUTPUT_SIZE (7 + 22 + 1)
35
42
 
@@ -38,18 +45,8 @@
38
45
  #endif
39
46
  #include "ow-crypt.h"
40
47
 
41
- extern char *_crypt_blowfish_rn(__CONST char *key, __CONST char *setting,
42
- char *output, int size);
43
- extern char *_crypt_gensalt_blowfish_rn(unsigned long count,
44
- __CONST char *input, int size, char *output, int output_size);
45
-
46
- extern unsigned char _crypt_itoa64[];
47
- extern char *_crypt_gensalt_traditional_rn(unsigned long count,
48
- __CONST char *input, int size, char *output, int output_size);
49
- extern char *_crypt_gensalt_extended_rn(unsigned long count,
50
- __CONST char *input, int size, char *output, int output_size);
51
- extern char *_crypt_gensalt_md5_rn(unsigned long count,
52
- __CONST char *input, int size, char *output, int output_size);
48
+ #include "crypt_blowfish.h"
49
+ #include "crypt_gensalt.h"
53
50
 
54
51
  #if defined(__GLIBC__) && defined(_LIBC)
55
52
  /* crypt.h from glibc-crypt-2.1 will define struct crypt_data for us */
@@ -90,17 +87,14 @@ static int _crypt_data_alloc(void **data, int *size, int need)
90
87
  return 0;
91
88
  }
92
89
 
93
- static char *_crypt_retval_magic(char *retval, __CONST char *setting,
94
- char *output)
90
+ static char *_crypt_retval_magic(char *retval, const char *setting,
91
+ char *output, int size)
95
92
  {
96
- if (retval) return retval;
97
-
98
- output[0] = '*';
99
- output[1] = '0';
100
- output[2] = '\0';
93
+ if (retval)
94
+ return retval;
101
95
 
102
- if (setting[0] == '*' && setting[1] == '0')
103
- output[1] = '1';
96
+ if (_crypt_output_magic(setting, output, size))
97
+ return NULL; /* shouldn't happen */
104
98
 
105
99
  return output;
106
100
  }
@@ -162,22 +156,22 @@ char *__crypt_r(__const char *key, __const char *setting,
162
156
  {
163
157
  return _crypt_retval_magic(
164
158
  __crypt_rn(key, setting, data, sizeof(*data)),
165
- setting, (char *)data);
159
+ setting, (char *)data, sizeof(*data));
166
160
  }
167
161
 
168
162
  char *__crypt(__const char *key, __const char *setting)
169
163
  {
170
164
  return _crypt_retval_magic(
171
165
  __crypt_rn(key, setting, &_ufc_foobar, sizeof(_ufc_foobar)),
172
- setting, (char *)&_ufc_foobar);
166
+ setting, (char *)&_ufc_foobar, sizeof(_ufc_foobar));
173
167
  }
174
168
  #else
175
- char *crypt_rn(__CONST char *key, __CONST char *setting, void *data, int size)
169
+ char *crypt_rn(const char *key, const char *setting, void *data, int size)
176
170
  {
177
171
  return _crypt_blowfish_rn(key, setting, (char *)data, size);
178
172
  }
179
173
 
180
- char *crypt_ra(__CONST char *key, __CONST char *setting,
174
+ char *crypt_ra(const char *key, const char *setting,
181
175
  void **data, int *size)
182
176
  {
183
177
  if (_crypt_data_alloc(data, size, CRYPT_OUTPUT_SIZE))
@@ -185,11 +179,20 @@ char *crypt_ra(__CONST char *key, __CONST char *setting,
185
179
  return _crypt_blowfish_rn(key, setting, (char *)*data, *size);
186
180
  }
187
181
 
188
- char *crypt_r(__CONST char *key, __CONST char *setting, void *data)
182
+ char *crypt_r(const char *key, const char *setting, struct crypt_data *data)
189
183
  {
190
184
  return _crypt_retval_magic(
191
185
  crypt_rn(key, setting, data, CRYPT_OUTPUT_SIZE),
192
- setting, (char *)data);
186
+ setting, (char *)data, CRYPT_OUTPUT_SIZE);
187
+ }
188
+
189
+ char *crypt(const char *key, const char *setting)
190
+ {
191
+ static char output[CRYPT_OUTPUT_SIZE];
192
+
193
+ return _crypt_retval_magic(
194
+ crypt_rn(key, setting, output, sizeof(output)),
195
+ setting, output, sizeof(output));
193
196
  }
194
197
 
195
198
  #define __crypt_gensalt_rn crypt_gensalt_rn
@@ -197,11 +200,12 @@ char *crypt_r(__CONST char *key, __CONST char *setting, void *data)
197
200
  #define __crypt_gensalt crypt_gensalt
198
201
  #endif
199
202
 
200
- char *__crypt_gensalt_rn(__CONST char *prefix, unsigned long count,
201
- __CONST char *input, int size, char *output, int output_size)
203
+ char *__crypt_gensalt_rn(const char *prefix, unsigned long count,
204
+ const char *input, int size, char *output, int output_size)
202
205
  {
203
- char *(*use)(unsigned long count,
204
- __CONST char *input, int size, char *output, int output_size);
206
+ char *(*use)(const char *_prefix, unsigned long _count,
207
+ const char *_input, int _size,
208
+ char *_output, int _output_size);
205
209
 
206
210
  /* This may be supported on some platforms in the future */
207
211
  if (!input) {
@@ -209,7 +213,8 @@ char *__crypt_gensalt_rn(__CONST char *prefix, unsigned long count,
209
213
  return NULL;
210
214
  }
211
215
 
212
- if (!strncmp(prefix, "$2a$", 4))
216
+ if (!strncmp(prefix, "$2a$", 4) || !strncmp(prefix, "$2b$", 4) ||
217
+ !strncmp(prefix, "$2y$", 4))
213
218
  use = _crypt_gensalt_blowfish_rn;
214
219
  else
215
220
  if (!strncmp(prefix, "$1$", 3))
@@ -228,11 +233,11 @@ char *__crypt_gensalt_rn(__CONST char *prefix, unsigned long count,
228
233
  return NULL;
229
234
  }
230
235
 
231
- return use(count, input, size, output, output_size);
236
+ return use(prefix, count, input, size, output, output_size);
232
237
  }
233
238
 
234
- char *__crypt_gensalt_ra(__CONST char *prefix, unsigned long count,
235
- __CONST char *input, int size)
239
+ char *__crypt_gensalt_ra(const char *prefix, unsigned long count,
240
+ const char *input, int size)
236
241
  {
237
242
  char output[CRYPT_GENSALT_OUTPUT_SIZE];
238
243
  char *retval;
@@ -241,7 +246,7 @@ char *__crypt_gensalt_ra(__CONST char *prefix, unsigned long count,
241
246
  input, size, output, sizeof(output));
242
247
 
243
248
  if (retval) {
244
- retval = ruby_strdup(retval);
249
+ retval = strdup(retval);
245
250
  #ifndef __GLIBC__
246
251
  /* strdup(3) on glibc sets errno, so we don't need to bother */
247
252
  if (!retval)
@@ -252,11 +257,298 @@ char *__crypt_gensalt_ra(__CONST char *prefix, unsigned long count,
252
257
  return retval;
253
258
  }
254
259
 
255
- char *__crypt_gensalt(__CONST char *prefix, unsigned long count,
256
- __CONST char *input, int size)
260
+ char *__crypt_gensalt(const char *prefix, unsigned long count,
261
+ const char *input, int size)
257
262
  {
258
263
  static char output[CRYPT_GENSALT_OUTPUT_SIZE];
259
264
 
260
265
  return __crypt_gensalt_rn(prefix, count,
261
266
  input, size, output, sizeof(output));
262
267
  }
268
+
269
+ #if defined(__GLIBC__) && defined(_LIBC)
270
+ weak_alias(__crypt_rn, crypt_rn)
271
+ weak_alias(__crypt_ra, crypt_ra)
272
+ weak_alias(__crypt_r, crypt_r)
273
+ weak_alias(__crypt, crypt)
274
+ weak_alias(__crypt_gensalt_rn, crypt_gensalt_rn)
275
+ weak_alias(__crypt_gensalt_ra, crypt_gensalt_ra)
276
+ weak_alias(__crypt_gensalt, crypt_gensalt)
277
+ weak_alias(crypt, fcrypt)
278
+ #endif
279
+
280
+ #ifdef TEST
281
+ static const char *tests[][3] = {
282
+ {"$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW",
283
+ "U*U"},
284
+ {"$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK",
285
+ "U*U*"},
286
+ {"$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a",
287
+ "U*U*U"},
288
+ {"$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui",
289
+ "0123456789abcdefghijklmnopqrstuvwxyz"
290
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
291
+ "chars after 72 are ignored"},
292
+ {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
293
+ "\xa3"},
294
+ {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
295
+ "\xff\xff\xa3"},
296
+ {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
297
+ "\xff\xff\xa3"},
298
+ {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.nqd1wy.pTMdcvrRWxyiGL2eMz.2a85.",
299
+ "\xff\xff\xa3"},
300
+ {"$2b$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
301
+ "\xff\xff\xa3"},
302
+ {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
303
+ "\xa3"},
304
+ {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
305
+ "\xa3"},
306
+ {"$2b$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
307
+ "\xa3"},
308
+ {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
309
+ "1\xa3" "345"},
310
+ {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
311
+ "\xff\xa3" "345"},
312
+ {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
313
+ "\xff\xa3" "34" "\xff\xff\xff\xa3" "345"},
314
+ {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
315
+ "\xff\xa3" "34" "\xff\xff\xff\xa3" "345"},
316
+ {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.ZC1JEJ8Z4gPfpe1JOr/oyPXTWl9EFd.",
317
+ "\xff\xa3" "34" "\xff\xff\xff\xa3" "345"},
318
+ {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.nRht2l/HRhr6zmCp9vYUvvsqynflf9e",
319
+ "\xff\xa3" "345"},
320
+ {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.nRht2l/HRhr6zmCp9vYUvvsqynflf9e",
321
+ "\xff\xa3" "345"},
322
+ {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
323
+ "\xa3" "ab"},
324
+ {"$2x$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
325
+ "\xa3" "ab"},
326
+ {"$2y$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
327
+ "\xa3" "ab"},
328
+ {"$2x$05$6bNw2HLQYeqHYyBfLMsv/OiwqTymGIGzFsA4hOTWebfehXHNprcAS",
329
+ "\xd1\x91"},
330
+ {"$2x$05$6bNw2HLQYeqHYyBfLMsv/O9LIGgn8OMzuDoHfof8AQimSGfcSWxnS",
331
+ "\xd0\xc1\xd2\xcf\xcc\xd8"},
332
+ {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.swQOIzjOiJ9GHEPuhEkvqrUyvWhEMx6",
333
+ "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
334
+ "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
335
+ "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
336
+ "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
337
+ "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
338
+ "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
339
+ "chars after 72 are ignored as usual"},
340
+ {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.R9xrDjiycxMbQE2bp.vgqlYpW5wx2yy",
341
+ "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
342
+ "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
343
+ "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
344
+ "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
345
+ "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
346
+ "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"},
347
+ {"$2a$05$/OK.fbVrR/bpIqNJ5ianF.9tQZzcJfm3uj2NvJ/n5xkhpqLrMpWCe",
348
+ "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
349
+ "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
350
+ "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
351
+ "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
352
+ "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
353
+ "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"},
354
+ {"$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy",
355
+ ""},
356
+ {"*0", "", "$2a$03$CCCCCCCCCCCCCCCCCCCCC."},
357
+ {"*0", "", "$2a$32$CCCCCCCCCCCCCCCCCCCCC."},
358
+ {"*0", "", "$2c$05$CCCCCCCCCCCCCCCCCCCCC."},
359
+ {"*0", "", "$2z$05$CCCCCCCCCCCCCCCCCCCCC."},
360
+ {"*0", "", "$2`$05$CCCCCCCCCCCCCCCCCCCCC."},
361
+ {"*0", "", "$2{$05$CCCCCCCCCCCCCCCCCCCCC."},
362
+ {"*1", "", "*0"},
363
+ {NULL}
364
+ };
365
+
366
+ #define which tests[0]
367
+
368
+ static volatile sig_atomic_t running;
369
+
370
+ static void handle_timer(int signum)
371
+ {
372
+ (void) signum;
373
+ running = 0;
374
+ }
375
+
376
+ static void *run(void *arg)
377
+ {
378
+ unsigned long count = 0;
379
+ int i = 0;
380
+ void *data = NULL;
381
+ int size = 0x12345678;
382
+
383
+ do {
384
+ const char *hash = tests[i][0];
385
+ const char *key = tests[i][1];
386
+ const char *setting = tests[i][2];
387
+
388
+ if (!tests[++i][0])
389
+ i = 0;
390
+
391
+ if (setting && strlen(hash) < 30) /* not for benchmark */
392
+ continue;
393
+
394
+ if (strcmp(crypt_ra(key, hash, &data, &size), hash)) {
395
+ printf("%d: FAILED (crypt_ra/%d/%lu)\n",
396
+ (int)((char *)arg - (char *)0), i, count);
397
+ free(data);
398
+ return NULL;
399
+ }
400
+ count++;
401
+ } while (running);
402
+
403
+ free(data);
404
+ return count + (char *)0;
405
+ }
406
+
407
+ int main(void)
408
+ {
409
+ struct itimerval it;
410
+ struct tms buf;
411
+ clock_t clk_tck, start_real, start_virtual, end_real, end_virtual;
412
+ unsigned long count;
413
+ void *data;
414
+ int size;
415
+ char *setting1, *setting2;
416
+ int i;
417
+ #ifdef TEST_THREADS
418
+ pthread_t t[TEST_THREADS];
419
+ void *t_retval;
420
+ #endif
421
+
422
+ data = NULL;
423
+ size = 0x12345678;
424
+
425
+ for (i = 0; tests[i][0]; i++) {
426
+ const char *hash = tests[i][0];
427
+ const char *key = tests[i][1];
428
+ const char *setting = tests[i][2];
429
+ const char *p;
430
+ int ok = !setting || strlen(hash) >= 30;
431
+ int o_size;
432
+ char s_buf[30], o_buf[61];
433
+ if (!setting) {
434
+ memcpy(s_buf, hash, sizeof(s_buf) - 1);
435
+ s_buf[sizeof(s_buf) - 1] = 0;
436
+ setting = s_buf;
437
+ }
438
+
439
+ __set_errno(0);
440
+ p = crypt(key, setting);
441
+ if ((!ok && !errno) || strcmp(p, hash)) {
442
+ printf("FAILED (crypt/%d)\n", i);
443
+ return 1;
444
+ }
445
+
446
+ if (ok && strcmp(crypt(key, hash), hash)) {
447
+ printf("FAILED (crypt/%d)\n", i);
448
+ return 1;
449
+ }
450
+
451
+ for (o_size = -1; o_size <= (int)sizeof(o_buf); o_size++) {
452
+ int ok_n = ok && o_size == (int)sizeof(o_buf);
453
+ const char *x = "abc";
454
+ strcpy(o_buf, x);
455
+ if (o_size >= 3) {
456
+ x = "*0";
457
+ if (setting[0] == '*' && setting[1] == '0')
458
+ x = "*1";
459
+ }
460
+ __set_errno(0);
461
+ p = crypt_rn(key, setting, o_buf, o_size);
462
+ if ((ok_n && (!p || strcmp(p, hash))) ||
463
+ (!ok_n && (!errno || p || strcmp(o_buf, x)))) {
464
+ printf("FAILED (crypt_rn/%d)\n", i);
465
+ return 1;
466
+ }
467
+ }
468
+
469
+ __set_errno(0);
470
+ p = crypt_ra(key, setting, &data, &size);
471
+ if ((ok && (!p || strcmp(p, hash))) ||
472
+ (!ok && (!errno || p || strcmp((char *)data, hash)))) {
473
+ printf("FAILED (crypt_ra/%d)\n", i);
474
+ return 1;
475
+ }
476
+ }
477
+
478
+ setting1 = crypt_gensalt(which[0], 12, data, size);
479
+ if (!setting1 || strncmp(setting1, "$2a$12$", 7)) {
480
+ puts("FAILED (crypt_gensalt)\n");
481
+ return 1;
482
+ }
483
+
484
+ setting2 = crypt_gensalt_ra(setting1, 12, data, size);
485
+ if (strcmp(setting1, setting2)) {
486
+ puts("FAILED (crypt_gensalt_ra/1)\n");
487
+ return 1;
488
+ }
489
+
490
+ (*(char *)data)++;
491
+ setting1 = crypt_gensalt_ra(setting2, 12, data, size);
492
+ if (!strcmp(setting1, setting2)) {
493
+ puts("FAILED (crypt_gensalt_ra/2)\n");
494
+ return 1;
495
+ }
496
+
497
+ free(setting1);
498
+ free(setting2);
499
+ free(data);
500
+
501
+ #if defined(_SC_CLK_TCK) || !defined(CLK_TCK)
502
+ clk_tck = sysconf(_SC_CLK_TCK);
503
+ #else
504
+ clk_tck = CLK_TCK;
505
+ #endif
506
+
507
+ running = 1;
508
+ signal(SIGALRM, handle_timer);
509
+
510
+ memset(&it, 0, sizeof(it));
511
+ it.it_value.tv_sec = 5;
512
+ setitimer(ITIMER_REAL, &it, NULL);
513
+
514
+ start_real = times(&buf);
515
+ start_virtual = buf.tms_utime + buf.tms_stime;
516
+
517
+ count = (char *)run((char *)0) - (char *)0;
518
+
519
+ end_real = times(&buf);
520
+ end_virtual = buf.tms_utime + buf.tms_stime;
521
+ if (end_virtual == start_virtual) end_virtual++;
522
+
523
+ printf("%.1f c/s real, %.1f c/s virtual\n",
524
+ (float)count * clk_tck / (end_real - start_real),
525
+ (float)count * clk_tck / (end_virtual - start_virtual));
526
+
527
+ #ifdef TEST_THREADS
528
+ running = 1;
529
+ it.it_value.tv_sec = 60;
530
+ setitimer(ITIMER_REAL, &it, NULL);
531
+ start_real = times(&buf);
532
+
533
+ for (i = 0; i < TEST_THREADS; i++)
534
+ if (pthread_create(&t[i], NULL, run, i + (char *)0)) {
535
+ perror("pthread_create");
536
+ return 1;
537
+ }
538
+
539
+ for (i = 0; i < TEST_THREADS; i++) {
540
+ if (pthread_join(t[i], &t_retval)) {
541
+ perror("pthread_join");
542
+ continue;
543
+ }
544
+ if (!t_retval) continue;
545
+ count = (char *)t_retval - (char *)0;
546
+ end_real = times(&buf);
547
+ printf("%d: %.1f c/s real\n", i,
548
+ (float)count * clk_tck / (end_real - start_real));
549
+ }
550
+ #endif
551
+
552
+ return 0;
553
+ }
554
+ #endif