summaryrefslogtreecommitdiff
path: root/test-crypt-bcrypt.c
blob: e8ccdfd4c018fd39b28739b85d724327ea877c80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Written by Solar Designer <solar at openwall.com> in 2000-2014.
 * No copyright is claimed, and the software is hereby placed in the public
 * domain.  In case this attempt to disclaim copyright and place the software
 * in the public domain is deemed null and void, then the software is
 * Copyright (c) 2000-2014 Solar Designer and it is hereby released to the
 * general public under the following terms:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted.
 *
 * There's ABSOLUTELY NO WARRANTY, express or implied.
 *
 * See crypt_blowfish.c for more information.
 */

#include "crypt-port.h"
#include "crypt-base.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char *tests[][3] = {
  { "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW",
    "U*U" },
  { "$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK",
    "U*U*" },
  { "$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a",
    "U*U*U" },
  { "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui",
    "0123456789abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    "chars after 72 are ignored" },
  { "$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
    "\xa3" },
  { "$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
    "\xff\xff\xa3" },
  { "$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
    "\xff\xff\xa3" },
  { "$2a$05$/OK.fbVrR/bpIqNJ5ianF.nqd1wy.pTMdcvrRWxyiGL2eMz.2a85.",
    "\xff\xff\xa3" },
  { "$2b$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e",
    "\xff\xff\xa3" },
  { "$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
    "\xa3" },
  { "$2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
    "\xa3" },
  { "$2b$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq",
    "\xa3" },
  { "$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
    "1\xa3" "345" },
  { "$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
    "\xff\xa3" "345" },
  { "$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
    "\xff\xa3" "34" "\xff\xff\xff\xa3" "345" },
  { "$2y$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi",
    "\xff\xa3" "34" "\xff\xff\xff\xa3" "345" },
  { "$2a$05$/OK.fbVrR/bpIqNJ5ianF.ZC1JEJ8Z4gPfpe1JOr/oyPXTWl9EFd.",
    "\xff\xa3" "34" "\xff\xff\xff\xa3" "345" },
  { "$2y$05$/OK.fbVrR/bpIqNJ5ianF.nRht2l/HRhr6zmCp9vYUvvsqynflf9e",
    "\xff\xa3" "345" },
  { "$2a$05$/OK.fbVrR/bpIqNJ5ianF.nRht2l/HRhr6zmCp9vYUvvsqynflf9e",
    "\xff\xa3" "345" },
  { "$2a$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
    "\xa3" "ab" },
  { "$2x$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
    "\xa3" "ab" },
  { "$2y$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS",
    "\xa3" "ab" },
  { "$2x$05$6bNw2HLQYeqHYyBfLMsv/OiwqTymGIGzFsA4hOTWebfehXHNprcAS",
    "\xd1\x91" },
  { "$2x$05$6bNw2HLQYeqHYyBfLMsv/O9LIGgn8OMzuDoHfof8AQimSGfcSWxnS",
    "\xd0\xc1\xd2\xcf\xcc\xd8" },
  { "$2a$05$/OK.fbVrR/bpIqNJ5ianF.swQOIzjOiJ9GHEPuhEkvqrUyvWhEMx6",
    "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
    "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
    "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
    "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
    "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
    "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
    "chars after 72 are ignored as usual" },
  { "$2a$05$/OK.fbVrR/bpIqNJ5ianF.R9xrDjiycxMbQE2bp.vgqlYpW5wx2yy",
    "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
    "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
    "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
    "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
    "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55"
    "\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55\xaa\x55" },
  { "$2a$05$/OK.fbVrR/bpIqNJ5ianF.9tQZzcJfm3uj2NvJ/n5xkhpqLrMpWCe",
    "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
    "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
    "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
    "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
    "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff"
    "\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff\x55\xaa\xff" },
  { "$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy",
    "" },
  { "*0", "", "$2a$03$CCCCCCCCCCCCCCCCCCCCC." },
  { "*0", "", "$2a$32$CCCCCCCCCCCCCCCCCCCCC." },
  { "*0", "", "$2c$05$CCCCCCCCCCCCCCCCCCCCC." },
  { "*0", "", "$2z$05$CCCCCCCCCCCCCCCCCCCCC." },
  { "*0", "", "$2`$05$CCCCCCCCCCCCCCCCCCCCC." },
  { "*0", "", "$2{$05$CCCCCCCCCCCCCCCCCCCCC." },
  { "*1", "", "*0" },
  { 0 }
};

int
main (void)
{
  void *data = 0;
  int size = 0x12345678;
  int i;
  int status = 0;

  for (i = 0; tests[i][0]; i++)
    {
      const char *hash = tests[i][0];
      const char *key = tests[i][1];
      const char *setting = tests[i][2];
      const char *p;
      int ok = !setting || strlen (hash) >= 30;
      char s_buf[30];
      char o_buf[sizeof (struct crypt_data)];
      int errnm, match;

      if (!setting)
        {
          memcpy (s_buf, hash, sizeof (s_buf) - 1);
          s_buf[sizeof (s_buf) - 1] = 0;
          setting = s_buf;
        }

      errno = 0;
      p = crypt (key, setting);
      errnm = errno;
      match = strcmp (p, hash);
      if ((!ok && !errno) || strcmp (p, hash))
        {
          printf ("FAIL: %d/crypt.1: key=%s setting=%s: xhash=%s xerr=%d, "
                  "p=%s match=%d err=%s\n",
                  i, key, setting, hash, !ok, p, match==0, strerror (errnm));
          status = 1;
          continue;
        }

      if (ok)
        {
          p = crypt (key, hash);
          if (strcmp (p, hash))
            {
              printf ("FAIL: %d/crypt.2: key=%s hash=%s p=%s\n",
                      i, key, hash, p);
              status = 1;
              continue;
            }
        }

      strcpy (o_buf, "abc");
      const char *x = "*0";
      if (setting[0] == '*' && setting[1] == '0')
        x = "*1";
      errno = 0;
      p = crypt_rn (key, setting, o_buf, sizeof o_buf);
      errnm = errno;
      if (ok)
        match = p && !strcmp (p, hash);
      else
        match = !p && errnm && !strcmp (o_buf, x);

      if (!match)
        {
          printf ("FAIL: %d/crypt_rn: key=%s setting=%s: "
                  "xhash=%s xmagic=%s xerr=%d, p=%s obuf=%s err=%s\n",
                  i, key, setting, hash, x, !ok, p, o_buf,
                  strerror (errnm));
          status = 1;
          continue;
        }

      errno = 0;
      p = crypt_ra (key, setting, &data, &size);
      errnm = errno;

      if (ok)
        match = p && !strcmp (p, hash);
      else
        match = !p && errnm && !strcmp (data, hash);

      if (!match)
        {
          printf ("FAIL: %d/crypt_ra: key=%s setting=%s: xhash=%s xerr=%d, "
                  "p=%s data=%s err=%s\n",
                  i, key, setting, hash, !ok, p,
                  (char *)data, strerror (errnm));
          status = 1;
          continue;
        }
    }

  free (data);
  return status;
}