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

« back to all changes in this revision

Viewing changes to libtomcrypt/src/pk/rsa/rsa_export.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 rsa_export.c
 
15
  Export RSA PKCS keys, Tom St Denis
 
16
*/  
 
17
 
 
18
#ifdef MRSA
 
19
 
 
20
/**
 
21
    This will export either an RSAPublicKey or RSAPrivateKey [defined in PKCS #1 v2.1] 
 
22
    @param out       [out] Destination of the packet
 
23
    @param outlen    [in/out] The max size and resulting size of the packet
 
24
    @param type      The type of exported key (PK_PRIVATE or PK_PUBLIC)
 
25
    @param key       The RSA key to export
 
26
    @return CRYPT_OK if successful
 
27
*/    
 
28
int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key)
 
29
{
 
30
   int           err;
 
31
   unsigned long zero=0;
 
32
 
 
33
   LTC_ARGCHK(out    != NULL);
 
34
   LTC_ARGCHK(outlen != NULL);
 
35
   LTC_ARGCHK(key    != NULL);
 
36
 
 
37
   /* type valid? */
 
38
   if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) {
 
39
      return CRYPT_PK_INVALID_TYPE;
 
40
   }
 
41
 
 
42
   if (type == PK_PRIVATE) {
 
43
      /* private key */
 
44
      /* output is 
 
45
            Version, n, e, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p
 
46
       */
 
47
      if ((err = der_encode_sequence_multi(out, outlen, 
 
48
                          LTC_ASN1_SHORT_INTEGER, 1UL, &zero, 
 
49
                          LTC_ASN1_INTEGER, 1UL, &key->N, 
 
50
                          LTC_ASN1_INTEGER, 1UL, &key->e,
 
51
                          LTC_ASN1_INTEGER, 1UL, &key->d, 
 
52
                          LTC_ASN1_INTEGER, 1UL, &key->p, 
 
53
                          LTC_ASN1_INTEGER, 1UL, &key->q, 
 
54
                          LTC_ASN1_INTEGER, 1UL, &key->dP,
 
55
                          LTC_ASN1_INTEGER, 1UL, &key->dQ, 
 
56
                          LTC_ASN1_INTEGER, 1UL, &key->qP, 
 
57
                          LTC_ASN1_EOL,     0UL, NULL)) != CRYPT_OK) {
 
58
         return err;
 
59
      }
 
60
 
 
61
      /* clear zero and return */
 
62
      return CRYPT_OK;
 
63
   } else {
 
64
      /* public key */
 
65
      return der_encode_sequence_multi(out, outlen, 
 
66
                                 LTC_ASN1_INTEGER, 1UL, &key->N, 
 
67
                                 LTC_ASN1_INTEGER, 1UL, &key->e, 
 
68
                                 LTC_ASN1_EOL,     0UL, NULL);
 
69
   }
 
70
}
 
71
 
 
72
#endif /* MRSA */
 
73
 
 
74
/* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_export.c,v $ */
 
75
/* $Revision: 1.11 $ */
 
76
/* $Date: 2005/06/04 01:42:48 $ */