1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file x25519_import_x509.c
7   Import a X25519 key from a X.509 certificate, Steffen Jaeckel
8 */
9 
10 #ifdef LTC_CURVE25519
11 
s_x25519_decode(const unsigned char * in,unsigned long inlen,curve25519_key * key)12 static int s_x25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
13 {
14    if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE;
15    XMEMCPY(key->pub, in, sizeof(key->pub));
16    return CRYPT_OK;
17 }
18 
19 /**
20   Import a X25519 public key from a X.509 certificate
21   @param in     The DER encoded X.509 certificate
22   @param inlen  The length of the certificate
23   @param key    [out] Where to import the key to
24   @return CRYPT_OK if successful, on error all allocated memory is freed automatically
25 */
x25519_import_x509(const unsigned char * in,unsigned long inlen,curve25519_key * key)26 int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key)
27 {
28    int err;
29 
30    LTC_ARGCHK(in  != NULL);
31    LTC_ARGCHK(key != NULL);
32 
33    if ((err = x509_decode_public_key_from_certificate(in, inlen,
34                                                       LTC_OID_X25519,
35                                                       LTC_ASN1_EOL, NULL, NULL,
36                                                       (public_key_decode_cb)s_x25519_decode, key)) != CRYPT_OK) {
37       return err;
38    }
39    key->type = PK_PUBLIC;
40    key->algo = LTC_OID_X25519;
41 
42    return err;
43 }
44 
45 #endif
46