]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/katja/katja_export.c
remove md2 md4 md5 hashes
[zpackage] / libtomcrypt / src / pk / katja / katja_export.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_export.c
13   Export Katja PKCS-style keys, Tom St Denis
14 */
15
16 #ifdef LTC_MKAT
17
18 /**
19     This will export either an KatjaPublicKey or KatjaPrivateKey
20     @param out       [out] Destination of the packet
21     @param outlen    [in/out] The max size and resulting size of the packet
22     @param type      The type of exported key (PK_PRIVATE or PK_PUBLIC)
23     @param key       The Katja key to export
24     @return CRYPT_OK if successful
25 */
26 int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key)
27 {
28    int           err;
29    unsigned long zero=0;
30
31    LTC_ARGCHK(out    != NULL);
32    LTC_ARGCHK(outlen != NULL);
33    LTC_ARGCHK(key    != NULL);
34
35    /* type valid? */
36    if (!(key->type == PK_PRIVATE) && (type == PK_PRIVATE)) {
37       return CRYPT_PK_INVALID_TYPE;
38    }
39
40    if (type == PK_PRIVATE) {
41       /* private key */
42       /* output is
43             Version, n, d, p, q, d mod (p-1), d mod (q - 1), 1/q mod p, pq
44        */
45       if ((err = der_encode_sequence_multi(out, outlen,
46                           LTC_ASN1_SHORT_INTEGER, 1UL, &zero,
47                           LTC_ASN1_INTEGER, 1UL,  key->N,
48                           LTC_ASN1_INTEGER, 1UL,  key->d,
49                           LTC_ASN1_INTEGER, 1UL,  key->p,
50                           LTC_ASN1_INTEGER, 1UL,  key->q,
51                           LTC_ASN1_INTEGER, 1UL,  key->dP,
52                           LTC_ASN1_INTEGER, 1UL,  key->dQ,
53                           LTC_ASN1_INTEGER, 1UL,  key->qP,
54                           LTC_ASN1_INTEGER, 1UL,  key->pq,
55                           LTC_ASN1_EOL,     0UL, NULL)) != CRYPT_OK) {
56          return err;
57       }
58
59       /* clear zero and return */
60       return CRYPT_OK;
61    } else {
62       /* public key */
63       return der_encode_sequence_multi(out, outlen,
64                                  LTC_ASN1_INTEGER, 1UL, key->N,
65                                  LTC_ASN1_EOL,     0UL, NULL);
66    }
67 }
68
69 #endif /* LTC_MRSA */
70
71 /* ref:         $Format:%D$ */
72 /* git commit:  $Format:%H$ */
73 /* commit time: $Format:%ai$ */