forked from besser82/libxcrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-des-obsolete.c
106 lines (90 loc) · 2.09 KB
/
test-des-obsolete.c
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
/*
* This crypt(3) validation program shipped with UFC-crypt
* is derived from one distributed with Phil Karns PD DES package.
*
* @(#)cert.c 1.8 11 Aug 1996
*/
#include "crypt-port.h"
#include "crypt-base.h"
#include "crypt-obsolete.h"
#include "test-des-cases.h"
#include <stdio.h>
symver_ref("encrypt", encrypt, SYMVER_FLOOR);
symver_ref("setkey", setkey, SYMVER_FLOOR);
static void
expand (unsigned char ex[64], const unsigned char pk[8])
{
int i, j;
unsigned int t;
for (i = 0; i < 8; i++)
{
t = pk[i];
for (j = 0; j < 8; j++)
ex[i*8 + j] = (t & (0x01u << (7 - j))) != 0;
}
}
static void
ex_print (const unsigned char ex[64])
{
int i, j;
unsigned int t;
for (i = 0; i < 8; i++)
{
t = 0;
for (j = 0; j < 8; j++)
t = (t << 1) | ex[i*8 + j];
printf ("%02x", t);
}
}
static void
pk_print (const unsigned char pk[8])
{
for (int i = 0; i < 8; i++)
printf ("%02x", (unsigned int)pk[i]);
}
static void
report_failure (size_t n, bool decrypt,
const struct des_testcase *tc, const unsigned char got[64])
{
printf ("FAIL: %zu/%s: k=", n, decrypt ? "de" : "en");
pk_print (tc->key);
fputs (" exp ", stdout);
if (decrypt)
pk_print (tc->plain);
else
pk_print (tc->answer);
fputs (" got ", stdout);
ex_print (got);
putchar ('\n');
}
int
main (void)
{
unsigned char key[64], plain[64], cipher[64], answer[64];
const struct des_testcase *tc;
size_t t;
int status = 0;
for (t = 0; t < N_DES_TESTCASES; t++)
{
tc = &des_testcases[t];
expand (key, tc->key);
expand (plain, tc->plain);
expand (answer, tc->answer);
setkey ((char *)key);
memcpy (cipher, plain, 64);
encrypt ((char *)cipher, 0);
if (memcmp (cipher, answer, 64) != 0)
{
status = 1;
report_failure (t, false, tc, cipher);
}
memcpy (cipher, answer, 64);
encrypt ((char *)cipher, 1);
if (memcmp (cipher, plain, 64) != 0)
{
status = 1;
report_failure (t, true, tc, cipher);
}
}
return status;
}