File: sshcrypto.c

package info (click to toggle)
dsniff 2.4b1+debian-29
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,020 kB
  • sloc: ansic: 10,803; sh: 152; makefile: 126
file content (205 lines) | stat: -rw-r--r-- 4,410 bytes parent folder | download
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
/*
 * sshcrypto.c
 *
 * SSH-1 crypto routines, adapted from OpenSSH.
 *
 * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
 * Copyright (c) 2000 Niels Provos <provos@monkey.org>
 * Copyright (c) 2000 Markus Friedl <markus@openbsd.org>
 *
 * $Id: sshcrypto.c,v 1.5 2001/03/15 08:33:04 dugsong Exp $
 */

#include "config.h"

#include <sys/types.h>
#include <openssl/ssl.h>
#include <openssl/blowfish.h>
#include <openssl/des.h>

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

#include "sshcrypto.h"

struct blowfish_state {
	struct bf_key_st	key;
	u_char			iv[8];
};

#if 0
struct des3_state {
	DES_key_schedule	k1, k2, k3;
	DES_cblock		iv1, iv2, iv3;
};
#endif

void
rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
{
	u_char *inbuf, *outbuf;
	int len, ilen, olen;

	const BIGNUM *n, *e;
	RSA_get0_key(key, &n, &e, NULL);
	if (BN_num_bits(e) < 2 || !BN_is_odd(e))
		errx(1, "rsa_public_encrypt() exponent too small or not odd");

	olen = BN_num_bytes(n);
	outbuf = malloc(olen);

	ilen = BN_num_bytes(in);
	inbuf = malloc(ilen);

	if (outbuf == NULL || inbuf == NULL)
		err(1, "malloc");
	
	BN_bn2bin(in, inbuf);
	
	if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
				      RSA_PKCS1_PADDING)) <= 0)
		errx(1, "rsa_public_encrypt() failed");

	BN_bin2bn(outbuf, len, out);

	memset(outbuf, 0, olen);
	memset(inbuf, 0, ilen);
	free(outbuf);
	free(inbuf);
}

void
rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
{
	u_char *inbuf, *outbuf;
	int len, ilen, olen;

	const BIGNUM *n;
	RSA_get0_key(key, &n, NULL, NULL);
	olen = BN_num_bytes(n);
	outbuf = malloc(olen);

	ilen = BN_num_bytes(in);
	inbuf = malloc(ilen);

	if (outbuf == NULL || inbuf == NULL)
		err(1, "malloc");
	
	BN_bn2bin(in, inbuf);

	if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
				       RSA_PKCS1_PADDING)) <= 0)
		errx(1, "rsa_private_decrypt() failed");
	
	BN_bin2bn(outbuf, len, out);

	memset(outbuf, 0, olen);
	memset(inbuf, 0, ilen);
	free(outbuf);
	free(inbuf);
}

/* XXX - SSH1's weirdo Blowfish... */
static void
swap_bytes(const u_char *src, u_char *dst, int n)
{
	char c[4];
	
	for (n = n / 4; n > 0; n--) {
		c[3] = *src++; c[2] = *src++;
		c[1] = *src++; c[0] = *src++;
		*dst++ = c[0]; *dst++ = c[1];
		*dst++ = c[2]; *dst++ = c[3];
	}
}

void *
blowfish_init(u_char *sesskey, int len)
{
	struct blowfish_state *state;

	if ((state = malloc(sizeof(*state))) == NULL)
		err(1, "malloc");
	
	BF_set_key(&state->key, len, sesskey);
	memset(state->iv, 0, 8);

	return (state);
}

void
blowfish_encrypt(u_char *src, u_char *dst, int len, void *state)
{
	struct blowfish_state *estate;

	estate = (struct blowfish_state *)state;
	swap_bytes(src, dst, len);
	BF_cbc_encrypt((void *)dst, dst, len, &estate->key, estate->iv,
		       BF_ENCRYPT);
	swap_bytes(dst, dst, len);
}

void
blowfish_decrypt(u_char *src, u_char *dst, int len, void *state)
{
	struct blowfish_state *dstate;

	dstate = (struct blowfish_state *)state;
	swap_bytes(src, dst, len);
	BF_cbc_encrypt((void *)dst, dst, len, &dstate->key, dstate->iv,
		       BF_DECRYPT);
	swap_bytes(dst, dst, len);
}

#if 0
/* XXX - SSH1's weirdo 3DES... */
void *
des3_init(u_char *sesskey, int len)
{
	struct des3_state *state;
	
	if ((state = malloc(sizeof(*state))) == NULL)
		err(1, "malloc");

	DES_set_key((void *)sesskey, &state->k1);
	DES_set_key((void *)(sesskey + 8), &state->k2);

	if (len <= 16)
		DES_set_key((void *)sesskey, &state->k3);
	else
		DES_set_key((void *)(sesskey + 16), &state->k3);
	
	memset(state->iv1, 0, 8);
	memset(state->iv2, 0, 8);
	memset(state->iv3, 0, 8);
	
	return (state);
}
void
des3_encrypt(u_char *src, u_char *dst, int len, void *state)
{
	struct des3_state *estate;

	estate = (struct des3_state *)state;
	memcpy(estate->iv1, estate->iv2, 8);
	
	DES_ncbc_encrypt(src, dst, len, &estate->k1, &estate->iv1, DES_ENCRYPT);
	DES_ncbc_encrypt(dst, dst, len, &estate->k2, &estate->iv2, DES_DECRYPT);
	DES_ncbc_encrypt(dst, dst, len, &estate->k3, &estate->iv3, DES_ENCRYPT);
}

void
des3_decrypt(u_char *src, u_char *dst, int len, void *state)
{
	struct des3_state *dstate;
	
	dstate = (struct des3_state *)state;
	memcpy(dstate->iv1, dstate->iv2, 8);
	
	DES_ncbc_encrypt(src, dst, len, &dstate->k3, &dstate->iv3, DES_DECRYPT);
	DES_ncbc_encrypt(dst, dst, len, &dstate->k2, &dstate->iv2, DES_ENCRYPT);
	DES_ncbc_encrypt(dst, dst, len, &dstate->k1, &dstate->iv1, DES_DECRYPT);
}
#endif