]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/dsa/dsa_decrypt_key.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / dsa / dsa_decrypt_key.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 dsa_decrypt_key.c
13   DSA Crypto, Tom St Denis
14 */
15
16 #ifdef LTC_MDSA
17
18 /**
19   Decrypt an DSA encrypted key
20   @param in       The ciphertext
21   @param inlen    The length of the ciphertext (octets)
22   @param out      [out] The plaintext
23   @param outlen   [in/out] The max size and resulting size of the plaintext
24   @param key      The corresponding private DSA key
25   @return CRYPT_OK if successful
26 */
27 int dsa_decrypt_key(const unsigned char *in,  unsigned long  inlen,
28                           unsigned char *out, unsigned long *outlen,
29                           dsa_key *key)
30 {
31    unsigned char  *skey, *expt;
32    void           *g_pub;
33    unsigned long  x, y;
34    unsigned long  hashOID[32] = { 0 };
35    int            hash, err;
36    ltc_asn1_list  decode[3];
37
38    LTC_ARGCHK(in     != NULL);
39    LTC_ARGCHK(out    != NULL);
40    LTC_ARGCHK(outlen != NULL);
41    LTC_ARGCHK(key    != NULL);
42
43    /* right key type? */
44    if (key->type != PK_PRIVATE) {
45       return CRYPT_PK_NOT_PRIVATE;
46    }
47
48    /* decode to find out hash */
49    LTC_SET_ASN1(decode, 0, LTC_ASN1_OBJECT_IDENTIFIER, hashOID, sizeof(hashOID)/sizeof(hashOID[0]));
50    err = der_decode_sequence(in, inlen, decode, 1);
51    if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {
52       return err;
53    }
54
55    hash = find_hash_oid(hashOID, decode[0].size);
56    if (hash_is_valid(hash) != CRYPT_OK) {
57       return CRYPT_INVALID_PACKET;
58    }
59
60    /* we now have the hash! */
61
62    if ((err = mp_init(&g_pub)) != CRYPT_OK) {
63       return err;
64    }
65
66    /* allocate memory */
67    expt   = XMALLOC(mp_unsigned_bin_size(key->p) + 1);
68    skey   = XMALLOC(MAXBLOCKSIZE);
69    if (expt == NULL || skey == NULL) {
70       if (expt != NULL) {
71          XFREE(expt);
72       }
73       if (skey != NULL) {
74          XFREE(skey);
75       }
76       mp_clear(g_pub);
77       return CRYPT_MEM;
78    }
79
80    LTC_SET_ASN1(decode, 1, LTC_ASN1_INTEGER,          g_pub,      1UL);
81    LTC_SET_ASN1(decode, 2, LTC_ASN1_OCTET_STRING,      skey,      MAXBLOCKSIZE);
82
83    /* read the structure in now */
84    if ((err = der_decode_sequence(in, inlen, decode, 3)) != CRYPT_OK) {
85       goto LBL_ERR;
86    }
87
88    /* make shared key */
89    x = mp_unsigned_bin_size(key->p) + 1;
90    if ((err = dsa_shared_secret(key->x, g_pub, key, expt, &x)) != CRYPT_OK) {
91       goto LBL_ERR;
92    }
93
94    y = mp_unsigned_bin_size(key->p) + 1;
95    y = MIN(y, MAXBLOCKSIZE);
96    if ((err = hash_memory(hash, expt, x, expt, &y)) != CRYPT_OK) {
97       goto LBL_ERR;
98    }
99
100    /* ensure the hash of the shared secret is at least as big as the encrypt itself */
101    if (decode[2].size > y) {
102       err = CRYPT_INVALID_PACKET;
103       goto LBL_ERR;
104    }
105
106    /* avoid buffer overflow */
107    if (*outlen < decode[2].size) {
108       *outlen = decode[2].size;
109       err = CRYPT_BUFFER_OVERFLOW;
110       goto LBL_ERR;
111    }
112
113    /* Decrypt the key */
114    for (x = 0; x < decode[2].size; x++) {
115      out[x] = expt[x] ^ skey[x];
116    }
117    *outlen = x;
118
119    err = CRYPT_OK;
120 LBL_ERR:
121 #ifdef LTC_CLEAN_STACK
122    zeromem(expt,   mp_unsigned_bin_size(key->p) + 1);
123    zeromem(skey,   MAXBLOCKSIZE);
124 #endif
125
126    XFREE(expt);
127    XFREE(skey);
128
129    mp_clear(g_pub);
130
131    return err;
132 }
133
134 #endif
135
136 /* ref:         $Format:%D$ */
137 /* git commit:  $Format:%H$ */
138 /* commit time: $Format:%ai$ */
139