1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 #ifdef LTC_MDH
7 
8 /**
9   Binary export a DH key to a buffer
10   @param out    [out] The destination for the key
11   @param outlen [in/out] The max size and resulting size of the DH key
12   @param type   Which type of key (PK_PRIVATE or PK_PUBLIC)
13   @param key    The key you wish to export
14   @return CRYPT_OK if successful
15 */
dh_export_key(void * out,unsigned long * outlen,int type,const dh_key * key)16 int dh_export_key(void *out, unsigned long *outlen, int type, const dh_key *key)
17 {
18    unsigned long len;
19    void *k;
20 
21    LTC_ARGCHK(out    != NULL);
22    LTC_ARGCHK(outlen != NULL);
23    LTC_ARGCHK(key    != NULL);
24 
25    k = (type == PK_PRIVATE) ? key->x : key->y;
26    len = mp_unsigned_bin_size(k);
27 
28    if (*outlen < len) {
29       *outlen = len;
30       return CRYPT_BUFFER_OVERFLOW;
31    }
32    *outlen = len;
33 
34    return mp_to_unsigned_bin(k, out);
35 }
36 
37 #endif /* LTC_MDH */
38