dsniff  2.4b2
About: A collection of tools for network auditing
  Fossies Dox: dsniff-2.4b2.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

sshcrypto.c
Go to the documentation of this file.
1 /*
2  * sshcrypto.c
3  *
4  * SSH-1 crypto routines, adapted from OpenSSH.
5  *
6  * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
7  * Copyright (c) 2000 Niels Provos <provos@monkey.org>
8  * Copyright (c) 2000 Markus Friedl <markus@openbsd.org>
9  *
10  * $Id: sshcrypto.c,v 1.5 2001/03/15 08:33:04 dugsong Exp $
11  */
12 
13 #include "config.h"
14 
15 #include <sys/types.h>
16 #include <openssl/ssl.h>
17 
18 #include <err.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include "sshcrypto.h"
23 
25  struct bf_key_st key;
26  u_char iv[8];
27 };
28 
29 struct des3_state {
30  des_key_schedule k1, k2, k3;
31  des_cblock iv1, iv2, iv3;
32 };
33 
34 void
35 rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
36 {
37  u_char *inbuf, *outbuf;
38  int len, ilen, olen;
39 
40  if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
41  errx(1, "rsa_public_encrypt() exponent too small or not odd");
42 
43  olen = BN_num_bytes(key->n);
44  outbuf = malloc(olen);
45 
46  ilen = BN_num_bytes(in);
47  inbuf = malloc(ilen);
48 
49  if (outbuf == NULL || inbuf == NULL)
50  err(1, "malloc");
51 
52  BN_bn2bin(in, inbuf);
53 
54  if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
55  RSA_PKCS1_PADDING)) <= 0)
56  errx(1, "rsa_public_encrypt() failed");
57 
58  BN_bin2bn(outbuf, len, out);
59 
60  memset(outbuf, 0, olen);
61  memset(inbuf, 0, ilen);
62  free(outbuf);
63  free(inbuf);
64 }
65 
66 void
67 rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
68 {
69  u_char *inbuf, *outbuf;
70  int len, ilen, olen;
71 
72  olen = BN_num_bytes(key->n);
73  outbuf = malloc(olen);
74 
75  ilen = BN_num_bytes(in);
76  inbuf = malloc(ilen);
77 
78  if (outbuf == NULL || inbuf == NULL)
79  err(1, "malloc");
80 
81  BN_bn2bin(in, inbuf);
82 
83  if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
84  RSA_PKCS1_PADDING)) <= 0)
85  errx(1, "rsa_private_decrypt() failed");
86 
87  BN_bin2bn(outbuf, len, out);
88 
89  memset(outbuf, 0, olen);
90  memset(inbuf, 0, ilen);
91  free(outbuf);
92  free(inbuf);
93 }
94 
95 /* XXX - SSH1's weirdo Blowfish... */
96 static void
97 swap_bytes(const u_char *src, u_char *dst, int n)
98 {
99  char c[4];
100 
101  for (n = n / 4; n > 0; n--) {
102  c[3] = *src++; c[2] = *src++;
103  c[1] = *src++; c[0] = *src++;
104  *dst++ = c[0]; *dst++ = c[1];
105  *dst++ = c[2]; *dst++ = c[3];
106  }
107 }
108 
109 void *
110 blowfish_init(u_char *sesskey, int len)
111 {
112  struct blowfish_state *state;
113 
114  if ((state = malloc(sizeof(*state))) == NULL)
115  err(1, "malloc");
116 
117  BF_set_key(&state->key, len, sesskey);
118  memset(state->iv, 0, 8);
119 
120  return (state);
121 }
122 
123 void
124 blowfish_encrypt(u_char *src, u_char *dst, int len, void *state)
125 {
126  struct blowfish_state *estate;
127 
128  estate = (struct blowfish_state *)state;
129  swap_bytes(src, dst, len);
130  BF_cbc_encrypt((void *)dst, dst, len, &estate->key, estate->iv,
131  BF_ENCRYPT);
132  swap_bytes(dst, dst, len);
133 }
134 
135 void
136 blowfish_decrypt(u_char *src, u_char *dst, int len, void *state)
137 {
138  struct blowfish_state *dstate;
139 
140  dstate = (struct blowfish_state *)state;
141  swap_bytes(src, dst, len);
142  BF_cbc_encrypt((void *)dst, dst, len, &dstate->key, dstate->iv,
143  BF_DECRYPT);
144  swap_bytes(dst, dst, len);
145 }
146 
147 /* XXX - SSH1's weirdo 3DES... */
148 void *
149 des3_init(u_char *sesskey, int len)
150 {
151  struct des3_state *state;
152 
153  if ((state = malloc(sizeof(*state))) == NULL)
154  err(1, "malloc");
155 
156  des_set_key((void *)sesskey, state->k1);
157  des_set_key((void *)(sesskey + 8), state->k2);
158 
159  if (len <= 16)
160  des_set_key((void *)sesskey, state->k3);
161  else
162  des_set_key((void *)(sesskey + 16), state->k3);
163 
164  memset(state->iv1, 0, 8);
165  memset(state->iv2, 0, 8);
166  memset(state->iv3, 0, 8);
167 
168  return (state);
169 }
170 void
171 des3_encrypt(u_char *src, u_char *dst, int len, void *state)
172 {
173  struct des3_state *estate;
174 
175  estate = (struct des3_state *)state;
176  memcpy(estate->iv1, estate->iv2, 8);
177 
178  des_ncbc_encrypt(src, dst, len, estate->k1, &estate->iv1, DES_ENCRYPT);
179  des_ncbc_encrypt(dst, dst, len, estate->k2, &estate->iv2, DES_DECRYPT);
180  des_ncbc_encrypt(dst, dst, len, estate->k3, &estate->iv3, DES_ENCRYPT);
181 }
182 
183 void
184 des3_decrypt(u_char *src, u_char *dst, int len, void *state)
185 {
186  struct des3_state *dstate;
187 
188  dstate = (struct des3_state *)state;
189  memcpy(dstate->iv1, dstate->iv2, 8);
190 
191  des_ncbc_encrypt(src, dst, len, dstate->k3, &dstate->iv3, DES_DECRYPT);
192  des_ncbc_encrypt(dst, dst, len, dstate->k2, &dstate->iv2, DES_ENCRYPT);
193  des_ncbc_encrypt(dst, dst, len, dstate->k1, &dstate->iv1, DES_DECRYPT);
194 }
des3_encrypt
void des3_encrypt(u_char *src, u_char *dst, int len, void *state)
Definition: sshcrypto.c:171
blowfish_state
Definition: sshcrypto.c:24
blowfish_encrypt
void blowfish_encrypt(u_char *src, u_char *dst, int len, void *state)
Definition: sshcrypto.c:124
des3_state::iv2
des_cblock iv2
Definition: sshcrypto.c:31
des3_state::k3
des_key_schedule k3
Definition: sshcrypto.c:30
rsa_public_encrypt
void rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
Definition: sshcrypto.c:35
blowfish_init
void * blowfish_init(u_char *sesskey, int len)
Definition: sshcrypto.c:110
des3_init
void * des3_init(u_char *sesskey, int len)
Definition: sshcrypto.c:149
sshcrypto.h
rsa_private_decrypt
void rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
Definition: sshcrypto.c:67
des3_state::k2
des_key_schedule k2
Definition: sshcrypto.c:30
blowfish_state::key
struct bf_key_st key
Definition: sshcrypto.c:25
err.h
des3_state::iv1
des_cblock iv1
Definition: sshcrypto.c:31
err
void err(int eval, const char *fmt,...)
Definition: err.c:47
des3_state::k1
des_key_schedule k1
Definition: sshcrypto.c:30
blowfish_state::iv
u_char iv[8]
Definition: sshcrypto.c:26
des3_state::iv3
des_cblock iv3
Definition: sshcrypto.c:31
swap_bytes
static void swap_bytes(const u_char *src, u_char *dst, int n)
Definition: sshcrypto.c:97
errx
void errx(int eval, const char *fmt,...)
Definition: err.c:76
des3_state
Definition: sshcrypto.c:29
des3_decrypt
void des3_decrypt(u_char *src, u_char *dst, int len, void *state)
Definition: sshcrypto.c:184
config.h
blowfish_decrypt
void blowfish_decrypt(u_char *src, u_char *dst, int len, void *state)
Definition: sshcrypto.c:136