File: crypt3-check.c

package info (click to toggle)
ccrypt 1.11-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,148 kB
  • sloc: ansic: 22,340; sh: 5,691; makefile: 664; lisp: 637; yacc: 291; sed: 35
file content (92 lines) | stat: -rw-r--r-- 2,228 bytes parent folder | download | duplicates (4)
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
/* Copyright (C) 2000-2018 Peter Selinger.
   This file is part of ccrypt. It is free software and it is covered
   by the GNU general public license. See the file COPYING for details. */

/* test the crypt(3) replacement against the library crypt(3). Note
   that on many systems, crypt(3) does not exist, and even on those
   systems where it does, it is often buggy. 

   crypt(3) only looks at the lower 7 bits of the characters in a key,
   and only at the first 8 characters. Some implementations differ in
   whether they consider 128 as an end-of-string character or not
   (FreeBSD does, SunOS and GNU do not). The character 128 is unlikely
   to appear in a password, and we only check compliance for
   characters 1-127 here. */

#ifdef HAVE_CONFIG_H
#include <config.h>  /* generated by configure */
#endif

#ifndef HAVE_LIBCRYPT      /* this check doesn't make sense if the
			      reference crypt(3) is not available */
int main() {
  return 77;
}

#else

#define _XOPEN_SOURCE 500
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

#ifdef __CYGWIN__
#include <crypt.h>  /* for crypt() */
#endif

#include "../src/unixcrypt3.h"

#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')

int main() {
  int seed = time(0);
  int total = 0;
  int i,j,l,n,k;
  char salt[2];
  char key[8];
  char res1[13];
  char res2[13];
  char *p;

  printf("Random seed: %d\n", seed);
  srand(seed);

  /* give it a good spin */
  for (i=0; i<64; i++) {
    salt[0] = bin_to_ascii(i);

    for (k=0; k<20; k++) {
      j = rand() % 64;
      salt[1] = bin_to_ascii(j);

      l = rand() % 9;
      for (n=0; n<l; n++) {
	key[n] = rand() % 127 + 1;
      }
      if (n<8) {
	key[n] = 0;
      }
      p = crypt_replacement(key, salt);
      strncpy(res1, p, 13);
      p = crypt(key, salt);
      strncpy(res2, p, 13);
      if (strncmp(res1, res2, 13)!=0) {
        printf("Discrepancy for salt %c%c, password length %d\n",
               salt[0], salt[1], l);
        total++;
      }
    }
  }
  
  if (total) {
    printf("Failed: %d discrepancies.\n", total);
    return 1;
  } else {
    printf("Passed.\n");
    return 0;
  }
}

#endif /* HAVE_LIBCRYPT */