| 1 |
|
| 2 |
/* |
| 3 |
* This crypt(3) validation program shipped with UFC-crypt |
| 4 |
* is derived from one distributed with Phil Karns PD DES package. |
| 5 |
* |
| 6 |
* @(#)cert.c 1.8 11 Aug 1996 |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "crypt-port.h" |
| 10 |
#include "crypt-obsolete.h" |
| 11 |
#include "des-cases.h" |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
#if ENABLE_OBSOLETE_API_ENOSYS |
| 16 |
#include <errno.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#if HAVE_SYMVER |
| 20 |
symver_ref("encrypt", encrypt, SYMVER_FLOOR); |
| 21 |
symver_ref("setkey", setkey, SYMVER_FLOOR); |
| 22 |
#endif |
| 23 |
|
| 24 |
static void |
| 25 |
expand (unsigned char ex[64], const unsigned char pk[8]) |
| 26 |
{ |
| 27 |
int i, j; |
| 28 |
unsigned int t; |
| 29 |
|
| 30 |
for (i = 0; i < 8; i++) |
| 31 |
{ |
| 32 |
t = pk[i]; |
| 33 |
for (j = 0; j < 8; j++) |
| 34 |
ex[i*8 + j] = (unsigned char)((t & (0x01u << (7 - j))) != 0); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
#if !ENABLE_OBSOLETE_API_ENOSYS |
| 39 |
|
| 40 |
static void |
| 41 |
ex_print (const unsigned char ex[64]) |
| 42 |
{ |
| 43 |
int i, j; |
| 44 |
unsigned int t; |
| 45 |
|
| 46 |
for (i = 0; i < 8; i++) |
| 47 |
{ |
| 48 |
t = 0; |
| 49 |
for (j = 0; j < 8; j++) |
| 50 |
t = (t << 1) | ex[i*8 + j]; |
| 51 |
printf ("%02x", t); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
pk_print (const unsigned char pk[8]) |
| 57 |
{ |
| 58 |
for (int i = 0; i < 8; i++) |
| 59 |
printf ("%02x", (unsigned int)pk[i]); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
report_failure (size_t n, bool decrypt, |
| 64 |
const struct des_testcase *tc, const unsigned char got[64]) |
| 65 |
{ |
| 66 |
printf ("FAIL: %zu/%s: k=", n, decrypt ? "de" : "en"); |
| 67 |
pk_print (tc->key); |
| 68 |
fputs (" exp ", stdout); |
| 69 |
if (decrypt) |
| 70 |
pk_print (tc->plain); |
| 71 |
else |
| 72 |
pk_print (tc->answer); |
| 73 |
fputs (" got ", stdout); |
| 74 |
ex_print (got); |
| 75 |
putchar ('\n'); |
| 76 |
} |
| 77 |
|
| 78 |
int |
| 79 |
main (void) |
| 80 |
{ |
| 81 |
unsigned char key[64], plain[64], cipher[64], answer[64]; |
| 82 |
const struct des_testcase *tc; |
| 83 |
size_t t; |
| 84 |
int status = 0; |
| 85 |
|
| 86 |
for (t = 0; t < N_DES_TESTCASES; t++) |
| 87 |
{ |
| 88 |
tc = &des_testcases[t]; |
| 89 |
expand (key, tc->key); |
| 90 |
expand (plain, tc->plain); |
| 91 |
expand (answer, tc->answer); |
| 92 |
|
| 93 |
setkey ((char *)key); |
| 94 |
memcpy (cipher, plain, 64); |
| 95 |
encrypt ((char *)cipher, 0); |
| 96 |
|
| 97 |
if (memcmp (cipher, answer, 64) != 0) |
| 98 |
{ |
| 99 |
status = 1; |
| 100 |
// coverity[sensitive_data_leak] |
| 101 |
report_failure (t, false, tc, cipher); |
| 102 |
} |
| 103 |
|
| 104 |
memcpy (cipher, answer, 64); |
| 105 |
encrypt ((char *)cipher, 1); |
| 106 |
if (memcmp (cipher, plain, 64) != 0) |
| 107 |
{ |
| 108 |
status = 1; |
| 109 |
report_failure (t, true, tc, cipher); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return status; |
| 114 |
} |
| 115 |
|
| 116 |
#else |
| 117 |
|
| 118 |
int |
| 119 |
main (void) |
| 120 |
{ |
| 121 |
unsigned char key[64], plain[64], cipher[64], answer[64]; |
| 122 |
const struct des_testcase *tc; |
| 123 |
size_t t; |
| 124 |
int status = 0; |
| 125 |
|
| 126 |
for (t = 0; t < N_DES_TESTCASES; t++) |
| 127 |
{ |
| 128 |
tc = &des_testcases[t]; |
| 129 |
expand (key, tc->key); |
| 130 |
expand (plain, tc->plain); |
| 131 |
expand (answer, tc->answer); |
| 132 |
|
| 133 |
/* Explicitly reset errno as required by POSIX. */ |
| 134 |
errno = 0; |
| 135 |
|
| 136 |
setkey ((char *)key); |
| 137 |
|
| 138 |
if (errno != ENOSYS) |
| 139 |
{ |
| 140 |
status = 1; |
| 141 |
printf ("FAIL: %s: errno does NOT equal ENOSYS.\n" |
| 142 |
"expected: %d, %s, got: %d, %s\n", "setkey", |
| 143 |
ENOSYS, strerror (ENOSYS), errno, strerror (errno)); |
| 144 |
} |
| 145 |
|
| 146 |
memcpy (cipher, plain, 64); |
| 147 |
|
| 148 |
/* Explicitly reset errno as required by POSIX. */ |
| 149 |
errno = 0; |
| 150 |
|
| 151 |
encrypt ((char *)cipher, 0); |
| 152 |
|
| 153 |
if (memcmp (cipher, answer, 64) == 0) |
| 154 |
{ |
| 155 |
status = 1; |
| 156 |
printf ("FAIL: %s: still performs correct operation.\n", |
| 157 |
"encrypt"); |
| 158 |
} |
| 159 |
|
| 160 |
if (memcmp (cipher, plain, 64) == 0) |
| 161 |
{ |
| 162 |
status = 1; |
| 163 |
printf ("FAIL: %s: data-block is has not changed.\n", |
| 164 |
"encrypt"); |
| 165 |
} |
| 166 |
|
| 167 |
if (errno != ENOSYS) |
| 168 |
{ |
| 169 |
status = 1; |
| 170 |
printf ("FAIL: %s: errno does NOT equal ENOSYS.\n" |
| 171 |
"expected: %d, %s, got: %d, %s\n", "encrypt", |
| 172 |
ENOSYS, strerror (ENOSYS), errno, strerror (errno)); |
| 173 |
} |
| 174 |
|
| 175 |
/* Explicitly reset errno as required by POSIX. */ |
| 176 |
errno = 0; |
| 177 |
|
| 178 |
encrypt ((char *)cipher, 1); |
| 179 |
|
| 180 |
if (memcmp (cipher, plain, 64) == 0) |
| 181 |
{ |
| 182 |
status = 1; |
| 183 |
printf ("FAIL: %s: still performs correct operation.\n", |
| 184 |
"encrypt (decrypt)"); |
| 185 |
} |
| 186 |
|
| 187 |
if (memcmp (cipher, answer, 64) == 0) |
| 188 |
{ |
| 189 |
status = 1; |
| 190 |
printf ("FAIL: %s: data-block is unchanged.\n", |
| 191 |
"encrypt (decrypt)"); |
| 192 |
} |
| 193 |
|
| 194 |
if (errno != ENOSYS) |
| 195 |
{ |
| 196 |
status = 1; |
| 197 |
printf ("FAIL: %s: errno does NOT equal ENOSYS.\n" |
| 198 |
"expected: %d, %s, got: %d, %s\n", "encrypt (decrypt)", |
| 199 |
ENOSYS, strerror (ENOSYS), errno, strerror (errno)); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return status; |
| 204 |
} |
| 205 |
|
| 206 |
#endif |