2
* This file is part of the SSH Library
4
* Copyright (c) 2009 by Aris Adamantiadis
6
* The SSH Library is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation; either version 2.1 of the License, or (at your
9
* option) any later version.
11
* The SSH Library is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14
* License for more details.
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with the SSH Library; see the file COPYING. If not, write to
18
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26
#include "libssh/priv.h"
27
#include "libssh/session.h"
28
#include "libssh/crypto.h"
29
#include "libssh/wrapper.h"
35
static int alloc_key(struct crypto_struct *cipher) {
36
cipher->key = malloc(cipher->keylen);
37
if (cipher->key == NULL) {
44
SHACTX sha1_init(void) {
46
gcry_md_open(&ctx, GCRY_MD_SHA1, 0);
51
void sha1_update(SHACTX c, const void *data, unsigned long len) {
52
gcry_md_write(c, data, len);
55
void sha1_final(unsigned char *md, SHACTX c) {
57
memcpy(md, gcry_md_read(c, 0), SHA_DIGEST_LEN);
61
void sha1(unsigned char *digest, int len, unsigned char *hash) {
62
gcry_md_hash_buffer(GCRY_MD_SHA1, hash, digest, len);
65
MD5CTX md5_init(void) {
67
gcry_md_open(&c, GCRY_MD_MD5, 0);
72
void md5_update(MD5CTX c, const void *data, unsigned long len) {
73
gcry_md_write(c,data,len);
76
void md5_final(unsigned char *md, MD5CTX c) {
78
memcpy(md, gcry_md_read(c, 0), MD5_DIGEST_LEN);
82
HMACCTX hmac_init(const void *key, int len, int type) {
87
gcry_md_open(&c, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
90
gcry_md_open(&c, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
96
gcry_md_setkey(c, key, len);
101
void hmac_update(HMACCTX c, const void *data, unsigned long len) {
102
gcry_md_write(c, data, len);
105
void hmac_final(HMACCTX c, unsigned char *hashmacbuf, unsigned int *len) {
106
*len = gcry_md_get_algo_dlen(gcry_md_get_algo(c));
107
memcpy(hashmacbuf, gcry_md_read(c, 0), *len);
111
/* the wrapper functions for blowfish */
112
static int blowfish_set_key(struct crypto_struct *cipher, void *key, void *IV){
113
if (cipher->key == NULL) {
114
if (alloc_key(cipher) < 0) {
118
if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_BLOWFISH,
119
GCRY_CIPHER_MODE_CBC, 0)) {
120
SAFE_FREE(cipher->key);
123
if (gcry_cipher_setkey(cipher->key[0], key, 16)) {
124
SAFE_FREE(cipher->key);
127
if (gcry_cipher_setiv(cipher->key[0], IV, 8)) {
128
SAFE_FREE(cipher->key);
136
static void blowfish_encrypt(struct crypto_struct *cipher, void *in,
137
void *out, unsigned long len) {
138
gcry_cipher_encrypt(cipher->key[0], out, len, in, len);
141
static void blowfish_decrypt(struct crypto_struct *cipher, void *in,
142
void *out, unsigned long len) {
143
gcry_cipher_decrypt(cipher->key[0], out, len, in, len);
146
static int aes_set_key(struct crypto_struct *cipher, void *key, void *IV) {
147
int mode=GCRY_CIPHER_MODE_CBC;
148
if (cipher->key == NULL) {
149
if (alloc_key(cipher) < 0) {
152
if(strstr(cipher->name,"-ctr"))
153
mode=GCRY_CIPHER_MODE_CTR;
154
switch (cipher->keysize) {
156
if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_AES128,
158
SAFE_FREE(cipher->key);
163
if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_AES192,
165
SAFE_FREE(cipher->key);
170
if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_AES256,
172
SAFE_FREE(cipher->key);
177
if (gcry_cipher_setkey(cipher->key[0], key, cipher->keysize / 8)) {
178
SAFE_FREE(cipher->key);
181
if(mode == GCRY_CIPHER_MODE_CBC){
182
if (gcry_cipher_setiv(cipher->key[0], IV, 16)) {
184
SAFE_FREE(cipher->key);
188
if(gcry_cipher_setctr(cipher->key[0],IV,16)){
189
SAFE_FREE(cipher->key);
198
static void aes_encrypt(struct crypto_struct *cipher, void *in, void *out,
200
gcry_cipher_encrypt(cipher->key[0], out, len, in, len);
203
static void aes_decrypt(struct crypto_struct *cipher, void *in, void *out,
205
gcry_cipher_decrypt(cipher->key[0], out, len, in, len);
208
static int des3_set_key(struct crypto_struct *cipher, void *key, void *IV) {
209
if (cipher->key == NULL) {
210
if (alloc_key(cipher) < 0) {
213
if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_3DES,
214
GCRY_CIPHER_MODE_CBC, 0)) {
215
SAFE_FREE(cipher->key);
218
if (gcry_cipher_setkey(cipher->key[0], key, 24)) {
219
SAFE_FREE(cipher->key);
222
if (gcry_cipher_setiv(cipher->key[0], IV, 8)) {
223
SAFE_FREE(cipher->key);
231
static void des3_encrypt(struct crypto_struct *cipher, void *in,
232
void *out, unsigned long len) {
233
gcry_cipher_encrypt(cipher->key[0], out, len, in, len);
236
static void des3_decrypt(struct crypto_struct *cipher, void *in,
237
void *out, unsigned long len) {
238
gcry_cipher_decrypt(cipher->key[0], out, len, in, len);
241
static int des3_1_set_key(struct crypto_struct *cipher, void *key, void *IV) {
242
if (cipher->key == NULL) {
243
if (alloc_key(cipher) < 0) {
246
if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_DES,
247
GCRY_CIPHER_MODE_CBC, 0)) {
248
SAFE_FREE(cipher->key);
251
if (gcry_cipher_setkey(cipher->key[0], key, 8)) {
252
SAFE_FREE(cipher->key);
255
if (gcry_cipher_setiv(cipher->key[0], IV, 8)) {
256
SAFE_FREE(cipher->key);
260
if (gcry_cipher_open(&cipher->key[1], GCRY_CIPHER_DES,
261
GCRY_CIPHER_MODE_CBC, 0)) {
262
SAFE_FREE(cipher->key);
265
if (gcry_cipher_setkey(cipher->key[1], (unsigned char *)key + 8, 8)) {
266
SAFE_FREE(cipher->key);
269
if (gcry_cipher_setiv(cipher->key[1], (unsigned char *)IV + 8, 8)) {
270
SAFE_FREE(cipher->key);
274
if (gcry_cipher_open(&cipher->key[2], GCRY_CIPHER_DES,
275
GCRY_CIPHER_MODE_CBC, 0)) {
276
SAFE_FREE(cipher->key);
279
if (gcry_cipher_setkey(cipher->key[2], (unsigned char *)key + 16, 8)) {
280
SAFE_FREE(cipher->key);
283
if (gcry_cipher_setiv(cipher->key[2], (unsigned char *)IV + 16, 8)) {
284
SAFE_FREE(cipher->key);
292
static void des3_1_encrypt(struct crypto_struct *cipher, void *in,
293
void *out, unsigned long len) {
294
gcry_cipher_encrypt(cipher->key[0], out, len, in, len);
295
gcry_cipher_decrypt(cipher->key[1], in, len, out, len);
296
gcry_cipher_encrypt(cipher->key[2], out, len, in, len);
299
static void des3_1_decrypt(struct crypto_struct *cipher, void *in,
300
void *out, unsigned long len) {
301
gcry_cipher_decrypt(cipher->key[2], out, len, in, len);
302
gcry_cipher_encrypt(cipher->key[1], in, len, out, len);
303
gcry_cipher_decrypt(cipher->key[0], out, len, in, len);
306
/* the table of supported ciphers */
307
static struct crypto_struct ssh_ciphertab[] = {
309
.name = "blowfish-cbc",
311
.keylen = sizeof(gcry_cipher_hd_t),
314
.set_encrypt_key = blowfish_set_key,
315
.set_decrypt_key = blowfish_set_key,
316
.cbc_encrypt = blowfish_encrypt,
317
.cbc_decrypt = blowfish_decrypt
320
.name = "aes128-ctr",
322
.keylen = sizeof(gcry_cipher_hd_t),
325
.set_encrypt_key = aes_set_key,
326
.set_decrypt_key = aes_set_key,
327
.cbc_encrypt = aes_encrypt,
328
.cbc_decrypt = aes_encrypt
331
.name = "aes192-ctr",
333
.keylen = sizeof(gcry_cipher_hd_t),
336
.set_encrypt_key = aes_set_key,
337
.set_decrypt_key = aes_set_key,
338
.cbc_encrypt = aes_encrypt,
339
.cbc_decrypt = aes_encrypt
342
.name = "aes256-ctr",
344
.keylen = sizeof(gcry_cipher_hd_t),
347
.set_encrypt_key = aes_set_key,
348
.set_decrypt_key = aes_set_key,
349
.cbc_encrypt = aes_encrypt,
350
.cbc_decrypt = aes_encrypt
353
.name = "aes128-cbc",
355
.keylen = sizeof(gcry_cipher_hd_t),
358
.set_encrypt_key = aes_set_key,
359
.set_decrypt_key = aes_set_key,
360
.cbc_encrypt = aes_encrypt,
361
.cbc_decrypt = aes_decrypt
364
.name = "aes192-cbc",
366
.keylen = sizeof(gcry_cipher_hd_t),
369
.set_encrypt_key = aes_set_key,
370
.set_decrypt_key = aes_set_key,
371
.cbc_encrypt = aes_encrypt,
372
.cbc_decrypt = aes_decrypt
375
.name = "aes256-cbc",
377
.keylen = sizeof(gcry_cipher_hd_t),
380
.set_encrypt_key = aes_set_key,
381
.set_decrypt_key = aes_set_key,
382
.cbc_encrypt = aes_encrypt,
383
.cbc_decrypt = aes_decrypt
388
.keylen = sizeof(gcry_cipher_hd_t),
391
.set_encrypt_key = des3_set_key,
392
.set_decrypt_key = des3_set_key,
393
.cbc_encrypt = des3_encrypt,
394
.cbc_decrypt = des3_decrypt
397
.name = "3des-cbc-ssh1",
399
.keylen = sizeof(gcry_cipher_hd_t) * 3,
402
.set_encrypt_key = des3_1_set_key,
403
.set_decrypt_key = des3_1_set_key,
404
.cbc_encrypt = des3_1_encrypt,
405
.cbc_decrypt = des3_1_decrypt
413
.set_encrypt_key = NULL,
414
.set_decrypt_key = NULL,
420
struct crypto_struct *ssh_get_ciphertab(){
421
return ssh_ciphertab;