1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file x25519_import_x509.c
14   Import a X25519 key from a X.509 certificate, Steffen Jaeckel
15 */
16 
17 #ifdef LTC_CURVE25519
18 
_x25519_decode(const unsigned char * in,unsigned long inlen,curve25519_key * key)19 static int _x25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
20 {
21    if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE;
22    XMEMCPY(key->pub, in, sizeof(key->pub));
23    return CRYPT_OK;
24 }
25 
26 /**
27   Import a X25519 public key from a X.509 certificate
28   @param in     The DER encoded X.509 certificate
29   @param inlen  The length of the certificate
30   @param key    [out] Where to import the key to
31   @return CRYPT_OK if successful, on error all allocated memory is freed automatically
32 */
x25519_import_x509(const unsigned char * in,unsigned long inlen,curve25519_key * key)33 int x25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key)
34 {
35    int err;
36 
37    LTC_ARGCHK(in  != NULL);
38    LTC_ARGCHK(key != NULL);
39 
40    if ((err = x509_decode_public_key_from_certificate(in, inlen,
41                                                       PKA_X25519,
42                                                       LTC_ASN1_EOL, NULL, NULL,
43                                                       (public_key_decode_cb)_x25519_decode, key)) != CRYPT_OK) {
44       return err;
45    }
46    key->type = PK_PUBLIC;
47    key->algo = PKA_X25519;
48 
49    return err;
50 }
51 
52 #endif
53 
54 /* ref:         $Format:%D$ */
55 /* git commit:  $Format:%H$ */
56 /* commit time: $Format:%ai$ */
57