1/*      $OpenBSD: des3.c,v 1.10 2021/12/13 16:56:49 deraadt Exp $  */
2
3/*
4 * Copyright (c) 2002 Markus Friedl.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <openssl/des.h>
28#include <err.h>
29#include <fcntl.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
35/* Stubs */
36
37u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **);
38
39u_int32_t
40deflate_global(u_int8_t *data, u_int32_t size, int comp, u_int8_t **out)
41{
42	return 0;
43}
44
45void	explicit_bzero(void *, size_t);
46
47void
48explicit_bzero(void *b, size_t len)
49{
50	bzero(b, len);
51}
52
53
54/* Simulate CBC mode */
55
56static int
57docrypt(const unsigned char *key, size_t klen, const unsigned char *iv0,
58    const unsigned char *in, unsigned char *out, size_t len, int encrypt)
59{
60	u_int8_t block[8], iv[8], iv2[8], *ivp = iv, *nivp;
61	u_int8_t ctx[384];
62	int i, j, error = 0;
63
64	memcpy(iv, iv0, 8);
65	memset(ctx, 0, sizeof(ctx));
66	error = des3_setkey(ctx, key, klen);
67	if (error)
68		return -1;
69	for (i = 0; i < len / 8; i ++) {
70		bcopy(in, block, 8);
71		in += 8;
72		if (encrypt) {
73			for (j = 0; j < 8; j++)
74				block[j] ^= ivp[j];
75			des3_encrypt(ctx, block);
76			memcpy(ivp, block, 8);
77		} else {
78			nivp = ivp == iv ? iv2 : iv;
79			memcpy(nivp, block, 8);
80			des3_decrypt(ctx, block);
81			for (j = 0; j < 8; j++)
82				block[j] ^= ivp[j];
83			ivp = nivp;
84		}
85		bcopy(block, out, 8);
86		out += 8;
87	}
88	return 0;
89}
90
91static int
92match(unsigned char *a, unsigned char *b, size_t len)
93{
94	int i;
95
96	if (memcmp(a, b, len) == 0)
97		return (1);
98
99	warnx("decrypt/plaintext mismatch");
100
101	for (i = 0; i < len; i++)
102		printf("%2.2x", a[i]);
103	printf("\n");
104	for (i = 0; i < len; i++)
105		printf("%2.2x", b[i]);
106	printf("\n");
107
108	return (0);
109}
110
111#define SZ 16
112
113int
114main(int argc, char **argv)
115{
116	DES_key_schedule ks1, ks2, ks3;
117	unsigned char iv0[8], iv[8], key[24] = "012345670123456701234567";
118	unsigned char b1[SZ], b2[SZ];
119	int i, fail = 0;
120	u_int32_t rand = 0;
121
122	/* setup data and iv */
123	for (i = 0; i < sizeof(b1); i++ ) {
124		if (i % 4 == 0)
125                        rand = arc4random();
126		b1[i] = rand;
127		rand >>= 8;
128	}
129	for (i = 0; i < sizeof(iv0); i++ ) {
130		if (i % 4 == 0)
131                        rand = arc4random();
132		iv0[i] = rand;
133		rand >>= 8;
134	}
135	memset(b2, 0, sizeof(b2));
136
137	/* keysetup for software */
138        DES_set_key((void *) key, &ks1);
139        DES_set_key((void *) (key+8), &ks2);
140        DES_set_key((void *) (key+16), &ks3);
141
142	/* encrypt with software, decrypt with /dev/crypto */
143	memcpy(iv, iv0, sizeof(iv0));
144        DES_ede3_cbc_encrypt((void *)b1, (void*)b2, sizeof(b1), &ks1, &ks2,
145	    &ks3, (void*)iv, DES_ENCRYPT);
146	memcpy(iv, iv0, sizeof(iv0));
147	if (docrypt(key, sizeof(key), iv, b2, b2, sizeof(b1), 0) < 0) {
148		warnx("decryption failed");
149		fail++;
150	}
151	if (!match(b1, b2, sizeof(b1)))
152		fail++;
153	else
154		printf("ok, decrypted\n");
155
156	/* encrypt with kernel functions, decrypt with openssl */
157	memset(b2, 0, sizeof(b2));
158	memcpy(iv, iv0, sizeof(iv0));
159	if (docrypt(key, sizeof(key), iv, b1, b2, sizeof(b1), 1) < 0) {
160		warnx("encryption failed");
161		fail++;
162	}
163	memcpy(iv, iv0, sizeof(iv0));
164        DES_ede3_cbc_encrypt((void *)b2, (void*)b2, sizeof(b1), &ks1, &ks2,
165	    &ks3, (void*)iv, DES_DECRYPT);
166	if (!match(b1, b2, sizeof(b1)))
167		fail++;
168	else
169		printf("ok, encrypted\n");
170
171	exit((fail > 0) ? 1 : 0);
172}
173