~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/src/pk/dsa/dsa_import.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 
2
 *
 
3
 * LibTomCrypt is a library that provides various cryptographic
 
4
 * algorithms in a highly modular and flexible manner.
 
5
 *
 
6
 * The library is free for all purposes without any express
 
7
 * guarantee it works.
 
8
 *
 
9
 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
 
10
 */
 
11
#include "tomcrypt.h"
 
12
 
 
13
/**
 
14
   @file dsa_import.c
 
15
   DSA implementation, import a DSA key, Tom St Denis
 
16
*/
 
17
 
 
18
#ifdef MDSA
 
19
 
 
20
/**
 
21
   Import a DSA key 
 
22
   @param in       The binary packet to import from
 
23
   @param inlen    The length of the binary packet
 
24
   @param key      [out] Where to store the imported key
 
25
   @return CRYPT_OK if successful, upon error this function will free all allocated memory
 
26
*/
 
27
int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key)
 
28
{
 
29
   unsigned char flags[1];
 
30
   int           err;
 
31
 
 
32
   LTC_ARGCHK(in  != NULL);
 
33
   LTC_ARGCHK(key != NULL);
 
34
 
 
35
   /* init key */
 
36
   if (mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL) != MP_OKAY) {
 
37
      return CRYPT_MEM;
 
38
   }
 
39
 
 
40
   /* get key type */
 
41
   if ((err = der_decode_sequence_multi(in, inlen,
 
42
                                  LTC_ASN1_BIT_STRING, 1UL, flags,
 
43
                                  LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
 
44
      goto error;
 
45
   }
 
46
 
 
47
   if (flags[0] == 1) {
 
48
      if ((err = der_decode_sequence_multi(in, inlen,
 
49
                                 LTC_ASN1_BIT_STRING,   1UL, flags,
 
50
                                 LTC_ASN1_INTEGER,      1UL, &key->g,
 
51
                                 LTC_ASN1_INTEGER,      1UL, &key->p,
 
52
                                 LTC_ASN1_INTEGER,      1UL, &key->q,
 
53
                                 LTC_ASN1_INTEGER,      1UL, &key->y,
 
54
                                 LTC_ASN1_INTEGER,      1UL, &key->x,
 
55
                                 LTC_ASN1_EOL,          0UL, NULL)) != CRYPT_OK) {
 
56
         goto error;
 
57
      }
 
58
      key->type = PK_PRIVATE;
 
59
   } else {
 
60
      if ((err = der_decode_sequence_multi(in, inlen,
 
61
                                 LTC_ASN1_BIT_STRING,   1UL, flags,
 
62
                                 LTC_ASN1_INTEGER,      1UL, &key->g,
 
63
                                 LTC_ASN1_INTEGER,      1UL, &key->p,
 
64
                                 LTC_ASN1_INTEGER,      1UL, &key->q,
 
65
                                 LTC_ASN1_INTEGER,      1UL, &key->y,
 
66
                                 LTC_ASN1_EOL,          0UL, NULL)) != CRYPT_OK) {
 
67
         goto error;
 
68
      }
 
69
      key->type = PK_PUBLIC;
 
70
  }
 
71
  key->qord = mp_unsigned_bin_size(&key->q);
 
72
 
 
73
  if (key->qord >= MDSA_MAX_GROUP || key->qord <= 15 ||
 
74
      key->qord >= mp_unsigned_bin_size(&key->p) || (mp_unsigned_bin_size(&key->p) - key->qord) >= MDSA_DELTA) {
 
75
      err = CRYPT_INVALID_PACKET;
 
76
      goto error;
 
77
   }
 
78
 
 
79
  return CRYPT_OK;
 
80
error: 
 
81
   mp_clear_multi(&key->p, &key->g, &key->q, &key->x, &key->y, NULL);
 
82
   return err;
 
83
}
 
84
 
 
85
#endif
 
86
 
 
87
/* $Source: /cvs/libtom/libtomcrypt/src/pk/dsa/dsa_import.c,v $ */
 
88
/* $Revision: 1.7 $ */
 
89
/* $Date: 2005/06/08 23:31:17 $ */