]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/katja/katja_import.c
remove md2 md4 md5 hashes
[zpackage] / libtomcrypt / src / pk / katja / katja_import.c
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 #include "tomcrypt.h"
10
11 /**
12   @file katja_import.c
13   Import a PKCS-style Katja key, Tom St Denis
14 */
15
16 #ifdef LTC_MKAT
17
18 /**
19   Import an KatjaPublicKey or KatjaPrivateKey [two-prime only, only support >= 1024-bit keys, defined in PKCS #1 v2.1]
20   @param in      The packet to import from
21   @param inlen   It's length (octets)
22   @param key     [out] Destination for newly imported key
23   @return CRYPT_OK if successful, upon error allocated memory is freed
24 */
25 int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key)
26 {
27    int           err;
28    void         *zero;
29
30    LTC_ARGCHK(in  != NULL);
31    LTC_ARGCHK(key != NULL);
32    LTC_ARGCHK(ltc_mp.name != NULL);
33
34    /* init key */
35    if ((err = mp_init_multi(&zero, &key->d, &key->N, &key->dQ,
36                             &key->dP, &key->qP, &key->p, &key->q, &key->pq, NULL)) != CRYPT_OK) {
37       return err;
38    }
39
40    if ((err = der_decode_sequence_multi(in, inlen,
41                                   LTC_ASN1_INTEGER, 1UL, key->N,
42                                   LTC_ASN1_EOL,     0UL, NULL)) != CRYPT_OK) {
43       goto LBL_ERR;
44    }
45
46    if (mp_cmp_d(key->N, 0) == LTC_MP_EQ) {
47       /* it's a private key */
48       if ((err = der_decode_sequence_multi(in, inlen,
49                           LTC_ASN1_INTEGER, 1UL, zero,
50                           LTC_ASN1_INTEGER, 1UL, key->N,
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_INTEGER, 1UL, key->pq,
58                           LTC_ASN1_EOL,     0UL, NULL)) != CRYPT_OK) {
59          goto LBL_ERR;
60       }
61       key->type = PK_PRIVATE;
62    } else {
63       /* public we have N */
64       key->type = PK_PUBLIC;
65    }
66    mp_clear(zero);
67    return CRYPT_OK;
68 LBL_ERR:
69    mp_clear_multi(zero,    key->d, key->N, key->dQ, key->dP,
70                   key->qP, key->p, key->q, key->pq, NULL);
71    return err;
72 }
73
74 #endif /* LTC_MRSA */
75
76
77 /* ref:         $Format:%D$ */
78 /* git commit:  $Format:%H$ */
79 /* commit time: $Format:%ai$ */