~ubuntu-branches/ubuntu/precise/libssh/precise

« back to all changes in this revision

Viewing changes to src/libgcrypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2011-06-15 15:48:07 UTC
  • mfrom: (1.1.10 upstream) (4.1.12 sid)
  • Revision ID: james.westby@ubuntu.com-20110615154807-3muklcqfftr1vtch
Tags: 0.5.0-2
* debian/patches/0002-Check-for-NULL-pointers-in-string-c.patch:
  Consolidate patch (Should fix previous REJECT)
* Support multiarch spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the SSH Library
 
3
 *
 
4
 * Copyright (c) 2009 by Aris Adamantiadis
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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,
 
19
 * MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include <stdlib.h>
 
23
#include <stdio.h>
 
24
#include <string.h>
 
25
 
 
26
#include "libssh/priv.h"
 
27
#include "libssh/session.h"
 
28
#include "libssh/crypto.h"
 
29
#include "libssh/wrapper.h"
 
30
 
 
31
#ifdef HAVE_LIBGCRYPT
 
32
#include <gcrypt.h>
 
33
 
 
34
 
 
35
static int alloc_key(struct crypto_struct *cipher) {
 
36
    cipher->key = malloc(cipher->keylen);
 
37
    if (cipher->key == NULL) {
 
38
      return -1;
 
39
    }
 
40
 
 
41
    return 0;
 
42
}
 
43
 
 
44
SHACTX sha1_init(void) {
 
45
  SHACTX ctx = NULL;
 
46
  gcry_md_open(&ctx, GCRY_MD_SHA1, 0);
 
47
 
 
48
  return ctx;
 
49
}
 
50
 
 
51
void sha1_update(SHACTX c, const void *data, unsigned long len) {
 
52
  gcry_md_write(c, data, len);
 
53
}
 
54
 
 
55
void sha1_final(unsigned char *md, SHACTX c) {
 
56
  gcry_md_final(c);
 
57
  memcpy(md, gcry_md_read(c, 0), SHA_DIGEST_LEN);
 
58
  gcry_md_close(c);
 
59
}
 
60
 
 
61
void sha1(unsigned char *digest, int len, unsigned char *hash) {
 
62
  gcry_md_hash_buffer(GCRY_MD_SHA1, hash, digest, len);
 
63
}
 
64
 
 
65
MD5CTX md5_init(void) {
 
66
  MD5CTX c = NULL;
 
67
  gcry_md_open(&c, GCRY_MD_MD5, 0);
 
68
 
 
69
  return c;
 
70
}
 
71
 
 
72
void md5_update(MD5CTX c, const void *data, unsigned long len) {
 
73
    gcry_md_write(c,data,len);
 
74
}
 
75
 
 
76
void md5_final(unsigned char *md, MD5CTX c) {
 
77
  gcry_md_final(c);
 
78
  memcpy(md, gcry_md_read(c, 0), MD5_DIGEST_LEN);
 
79
  gcry_md_close(c);
 
80
}
 
81
 
 
82
HMACCTX hmac_init(const void *key, int len, int type) {
 
83
  HMACCTX c = NULL;
 
84
 
 
85
  switch(type) {
 
86
    case HMAC_SHA1:
 
87
      gcry_md_open(&c, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
 
88
      break;
 
89
    case HMAC_MD5:
 
90
      gcry_md_open(&c, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
 
91
      break;
 
92
    default:
 
93
      c = NULL;
 
94
  }
 
95
 
 
96
  gcry_md_setkey(c, key, len);
 
97
 
 
98
  return c;
 
99
}
 
100
 
 
101
void hmac_update(HMACCTX c, const void *data, unsigned long len) {
 
102
  gcry_md_write(c, data, len);
 
103
}
 
104
 
 
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);
 
108
  gcry_md_close(c);
 
109
}
 
110
 
 
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) {
 
115
      return -1;
 
116
    }
 
117
 
 
118
    if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_BLOWFISH,
 
119
        GCRY_CIPHER_MODE_CBC, 0)) {
 
120
      SAFE_FREE(cipher->key);
 
121
      return -1;
 
122
    }
 
123
    if (gcry_cipher_setkey(cipher->key[0], key, 16)) {
 
124
      SAFE_FREE(cipher->key);
 
125
      return -1;
 
126
    }
 
127
    if (gcry_cipher_setiv(cipher->key[0], IV, 8)) {
 
128
      SAFE_FREE(cipher->key);
 
129
      return -1;
 
130
    }
 
131
  }
 
132
 
 
133
  return 0;
 
134
}
 
135
 
 
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);
 
139
}
 
140
 
 
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);
 
144
}
 
145
 
 
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) {
 
150
      return -1;
 
151
    }
 
152
    if(strstr(cipher->name,"-ctr"))
 
153
      mode=GCRY_CIPHER_MODE_CTR;
 
154
    switch (cipher->keysize) {
 
155
      case 128:
 
156
        if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_AES128,
 
157
              mode, 0)) {
 
158
          SAFE_FREE(cipher->key);
 
159
          return -1;
 
160
        }
 
161
        break;
 
162
      case 192:
 
163
        if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_AES192,
 
164
              mode, 0)) {
 
165
          SAFE_FREE(cipher->key);
 
166
          return -1;
 
167
        }
 
168
        break;
 
169
      case 256:
 
170
        if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_AES256,
 
171
              mode, 0)) {
 
172
          SAFE_FREE(cipher->key);
 
173
          return -1;
 
174
        }
 
175
        break;
 
176
    }
 
177
    if (gcry_cipher_setkey(cipher->key[0], key, cipher->keysize / 8)) {
 
178
      SAFE_FREE(cipher->key);
 
179
      return -1;
 
180
    }
 
181
    if(mode == GCRY_CIPHER_MODE_CBC){
 
182
      if (gcry_cipher_setiv(cipher->key[0], IV, 16)) {
 
183
 
 
184
        SAFE_FREE(cipher->key);
 
185
        return -1;
 
186
      }
 
187
    } else {
 
188
      if(gcry_cipher_setctr(cipher->key[0],IV,16)){
 
189
        SAFE_FREE(cipher->key);
 
190
        return -1;
 
191
      }
 
192
    }
 
193
  }
 
194
 
 
195
  return 0;
 
196
}
 
197
 
 
198
static void aes_encrypt(struct crypto_struct *cipher, void *in, void *out,
 
199
    unsigned long len) {
 
200
  gcry_cipher_encrypt(cipher->key[0], out, len, in, len);
 
201
}
 
202
 
 
203
static void aes_decrypt(struct crypto_struct *cipher, void *in, void *out,
 
204
    unsigned long len) {
 
205
  gcry_cipher_decrypt(cipher->key[0], out, len, in, len);
 
206
}
 
207
 
 
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) {
 
211
      return -1;
 
212
    }
 
213
    if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_3DES,
 
214
          GCRY_CIPHER_MODE_CBC, 0)) {
 
215
      SAFE_FREE(cipher->key);
 
216
      return -1;
 
217
    }
 
218
    if (gcry_cipher_setkey(cipher->key[0], key, 24)) {
 
219
      SAFE_FREE(cipher->key);
 
220
      return -1;
 
221
    }
 
222
    if (gcry_cipher_setiv(cipher->key[0], IV, 8)) {
 
223
      SAFE_FREE(cipher->key);
 
224
      return -1;
 
225
    }
 
226
  }
 
227
 
 
228
  return 0;
 
229
}
 
230
 
 
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);
 
234
}
 
235
 
 
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);
 
239
}
 
240
 
 
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) {
 
244
      return -1;
 
245
    }
 
246
    if (gcry_cipher_open(&cipher->key[0], GCRY_CIPHER_DES,
 
247
          GCRY_CIPHER_MODE_CBC, 0)) {
 
248
      SAFE_FREE(cipher->key);
 
249
      return -1;
 
250
    }
 
251
    if (gcry_cipher_setkey(cipher->key[0], key, 8)) {
 
252
      SAFE_FREE(cipher->key);
 
253
      return -1;
 
254
    }
 
255
    if (gcry_cipher_setiv(cipher->key[0], IV, 8)) {
 
256
      SAFE_FREE(cipher->key);
 
257
      return -1;
 
258
    }
 
259
 
 
260
    if (gcry_cipher_open(&cipher->key[1], GCRY_CIPHER_DES,
 
261
          GCRY_CIPHER_MODE_CBC, 0)) {
 
262
      SAFE_FREE(cipher->key);
 
263
      return -1;
 
264
    }
 
265
    if (gcry_cipher_setkey(cipher->key[1], (unsigned char *)key + 8, 8)) {
 
266
      SAFE_FREE(cipher->key);
 
267
      return -1;
 
268
    }
 
269
    if (gcry_cipher_setiv(cipher->key[1], (unsigned char *)IV + 8, 8)) {
 
270
      SAFE_FREE(cipher->key);
 
271
      return -1;
 
272
    }
 
273
 
 
274
    if (gcry_cipher_open(&cipher->key[2], GCRY_CIPHER_DES,
 
275
          GCRY_CIPHER_MODE_CBC, 0)) {
 
276
      SAFE_FREE(cipher->key);
 
277
      return -1;
 
278
    }
 
279
    if (gcry_cipher_setkey(cipher->key[2], (unsigned char *)key + 16, 8)) {
 
280
      SAFE_FREE(cipher->key);
 
281
      return -1;
 
282
    }
 
283
    if (gcry_cipher_setiv(cipher->key[2], (unsigned char *)IV + 16, 8)) {
 
284
      SAFE_FREE(cipher->key);
 
285
      return -1;
 
286
    }
 
287
  }
 
288
 
 
289
  return 0;
 
290
}
 
291
 
 
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);
 
297
}
 
298
 
 
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);
 
304
}
 
305
 
 
306
/* the table of supported ciphers */
 
307
static struct crypto_struct ssh_ciphertab[] = {
 
308
  {
 
309
    .name            = "blowfish-cbc",
 
310
    .blocksize       = 8,
 
311
    .keylen          = sizeof(gcry_cipher_hd_t),
 
312
    .key             = NULL,
 
313
    .keysize         = 128,
 
314
    .set_encrypt_key = blowfish_set_key,
 
315
    .set_decrypt_key = blowfish_set_key,
 
316
    .cbc_encrypt     = blowfish_encrypt,
 
317
    .cbc_decrypt     = blowfish_decrypt
 
318
  },
 
319
  {
 
320
    .name            = "aes128-ctr",
 
321
    .blocksize       = 16,
 
322
    .keylen          = sizeof(gcry_cipher_hd_t),
 
323
    .key             = NULL,
 
324
    .keysize         = 128,
 
325
    .set_encrypt_key = aes_set_key,
 
326
    .set_decrypt_key = aes_set_key,
 
327
    .cbc_encrypt     = aes_encrypt,
 
328
    .cbc_decrypt     = aes_encrypt
 
329
  },
 
330
  {
 
331
      .name            = "aes192-ctr",
 
332
      .blocksize       = 16,
 
333
      .keylen          = sizeof(gcry_cipher_hd_t),
 
334
      .key             = NULL,
 
335
      .keysize         = 192,
 
336
      .set_encrypt_key = aes_set_key,
 
337
      .set_decrypt_key = aes_set_key,
 
338
      .cbc_encrypt     = aes_encrypt,
 
339
      .cbc_decrypt     = aes_encrypt
 
340
  },
 
341
  {
 
342
      .name            = "aes256-ctr",
 
343
      .blocksize       = 16,
 
344
      .keylen          = sizeof(gcry_cipher_hd_t),
 
345
      .key             = NULL,
 
346
      .keysize         = 256,
 
347
      .set_encrypt_key = aes_set_key,
 
348
      .set_decrypt_key = aes_set_key,
 
349
      .cbc_encrypt     = aes_encrypt,
 
350
      .cbc_decrypt     = aes_encrypt
 
351
  },
 
352
  {
 
353
    .name            = "aes128-cbc",
 
354
    .blocksize       = 16,
 
355
    .keylen          = sizeof(gcry_cipher_hd_t),
 
356
    .key             = NULL,
 
357
    .keysize         = 128,
 
358
    .set_encrypt_key = aes_set_key,
 
359
    .set_decrypt_key = aes_set_key,
 
360
    .cbc_encrypt     = aes_encrypt,
 
361
    .cbc_decrypt     = aes_decrypt
 
362
  },
 
363
  {
 
364
    .name            = "aes192-cbc",
 
365
    .blocksize       = 16,
 
366
    .keylen          = sizeof(gcry_cipher_hd_t),
 
367
    .key             = NULL,
 
368
    .keysize         = 192,
 
369
    .set_encrypt_key = aes_set_key,
 
370
    .set_decrypt_key = aes_set_key,
 
371
    .cbc_encrypt     = aes_encrypt,
 
372
    .cbc_decrypt     = aes_decrypt
 
373
  },
 
374
  {
 
375
    .name            = "aes256-cbc",
 
376
    .blocksize       = 16,
 
377
    .keylen          = sizeof(gcry_cipher_hd_t),
 
378
    .key             = NULL,
 
379
    .keysize         = 256,
 
380
    .set_encrypt_key = aes_set_key,
 
381
    .set_decrypt_key = aes_set_key,
 
382
    .cbc_encrypt     = aes_encrypt,
 
383
    .cbc_decrypt     = aes_decrypt
 
384
  },
 
385
  {
 
386
    .name            = "3des-cbc",
 
387
    .blocksize       = 8,
 
388
    .keylen          = sizeof(gcry_cipher_hd_t),
 
389
    .key             = NULL,
 
390
    .keysize         = 192,
 
391
    .set_encrypt_key = des3_set_key,
 
392
    .set_decrypt_key = des3_set_key,
 
393
    .cbc_encrypt     = des3_encrypt,
 
394
    .cbc_decrypt     = des3_decrypt
 
395
  },
 
396
  {
 
397
    .name            = "3des-cbc-ssh1",
 
398
    .blocksize       = 8,
 
399
    .keylen          = sizeof(gcry_cipher_hd_t) * 3,
 
400
    .key             = NULL,
 
401
    .keysize         = 192,
 
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
 
406
  },
 
407
  {
 
408
    .name            = NULL,
 
409
    .blocksize       = 0,
 
410
    .keylen          = 0,
 
411
    .key             = NULL,
 
412
    .keysize         = 0,
 
413
    .set_encrypt_key = NULL,
 
414
    .set_decrypt_key = NULL,
 
415
    .cbc_encrypt     = NULL,
 
416
    .cbc_decrypt     = NULL
 
417
  }
 
418
};
 
419
 
 
420
struct crypto_struct *ssh_get_ciphertab(){
 
421
  return ssh_ciphertab;
 
422
}
 
423
#endif