Home | History | Annotate | Download | only in katja
      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 (at) gmail.com, http://libtomcrypt.com
     10  */
     11 #include "tomcrypt.h"
     12 
     13 /**
     14   @file katja_import.c
     15   Import a PKCS-style Katja key, Tom St Denis
     16 */
     17 
     18 #ifdef MKAT
     19 
     20 /**
     21   Import an KatjaPublicKey or KatjaPrivateKey [two-prime only, only support >= 1024-bit keys, defined in PKCS #1 v2.1]
     22   @param in      The packet to import from
     23   @param inlen   It's length (octets)
     24   @param key     [out] Destination for newly imported key
     25   @return CRYPT_OK if successful, upon error allocated memory is freed
     26 */
     27 int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key)
     28 {
     29    int           err;
     30    void         *zero;
     31 
     32    LTC_ARGCHK(in  != NULL);
     33    LTC_ARGCHK(key != NULL);
     34    LTC_ARGCHK(ltc_mp.name != NULL);
     35 
     36    /* init key */
     37    if ((err = mp_init_multi(&zero, &key->d, &key->N, &key->dQ,
     38                             &key->dP, &key->qP, &key->p, &key->q, &key->pq, NULL)) != CRYPT_OK) {
     39       return err;
     40    }
     41 
     42    if ((err = der_decode_sequence_multi(in, inlen,
     43                                   LTC_ASN1_INTEGER, 1UL, key->N,
     44                                   LTC_ASN1_EOL,     0UL, NULL)) != CRYPT_OK) {
     45       goto LBL_ERR;
     46    }
     47 
     48    if (mp_cmp_d(key->N, 0) == LTC_MP_EQ) {
     49       /* it's a private key */
     50       if ((err = der_decode_sequence_multi(in, inlen,
     51                           LTC_ASN1_INTEGER, 1UL, zero,
     52                           LTC_ASN1_INTEGER, 1UL, key->N,
     53                           LTC_ASN1_INTEGER, 1UL, key->d,
     54                           LTC_ASN1_INTEGER, 1UL, key->p,
     55                           LTC_ASN1_INTEGER, 1UL, key->q,
     56                           LTC_ASN1_INTEGER, 1UL, key->dP,
     57                           LTC_ASN1_INTEGER, 1UL, key->dQ,
     58                           LTC_ASN1_INTEGER, 1UL, key->qP,
     59                           LTC_ASN1_INTEGER, 1UL, key->pq,
     60                           LTC_ASN1_EOL,     0UL, NULL)) != CRYPT_OK) {
     61          goto LBL_ERR;
     62       }
     63       key->type = PK_PRIVATE;
     64    } else {
     65       /* public we have N */
     66       key->type = PK_PUBLIC;
     67    }
     68    mp_clear(zero);
     69    return CRYPT_OK;
     70 LBL_ERR:
     71    mp_clear_multi(zero,    key->d, key->N, key->dQ, key->dP,
     72                   key->qP, key->p, key->q, key->pq, NULL);
     73    return err;
     74 }
     75 
     76 #endif /* MRSA */
     77 
     78 
     79 /* $Source: /cvs/libtom/libtomcrypt/src/pk/katja/katja_import.c,v $ */
     80 /* $Revision: 1.3 $ */
     81 /* $Date: 2006/03/31 14:15:35 $ */
     82