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 ed25519_shared_secret.c
14   Create an Ed25519 signature, Steffen Jaeckel
15 */
16 
17 #ifdef LTC_CURVE25519
18 
19 /**
20    Create an Ed25519 signature.
21    @param private_key     The private Ed25519 key in the pair
22    @param public_key      The public Ed25519 key in the pair
23    @param out             [out] The destination of the shared data
24    @param outlen          [in/out] The max size and resulting size of the shared data.
25    @return CRYPT_OK if successful
26 */
ed25519_sign(const unsigned char * msg,unsigned long msglen,unsigned char * sig,unsigned long * siglen,const curve25519_key * private_key)27 int ed25519_sign(const unsigned char  *msg, unsigned long msglen,
28                        unsigned char  *sig, unsigned long *siglen,
29                  const curve25519_key *private_key)
30 {
31    unsigned char *s;
32    unsigned long long smlen;
33    int err;
34 
35    LTC_ARGCHK(msg         != NULL);
36    LTC_ARGCHK(sig         != NULL);
37    LTC_ARGCHK(siglen      != NULL);
38    LTC_ARGCHK(private_key != NULL);
39 
40    if (private_key->algo != PKA_ED25519) return CRYPT_PK_INVALID_TYPE;
41    if (private_key->type != PK_PRIVATE) return CRYPT_PK_INVALID_TYPE;
42 
43    if (*siglen < 64uL) {
44       *siglen = 64uL;
45       return CRYPT_BUFFER_OVERFLOW;
46    }
47 
48    smlen = msglen + 64;
49    s = XMALLOC(smlen);
50    if (s == NULL) return CRYPT_MEM;
51 
52    err = tweetnacl_crypto_sign(s, &smlen,
53                                msg, msglen,
54                                private_key->priv, private_key->pub);
55 
56    XMEMCPY(sig, s, 64uL);
57    *siglen = 64uL;
58 
59 #ifdef LTC_CLEAN_STACK
60    zeromem(s, smlen);
61 #endif
62    XFREE(s);
63 
64    return err;
65 }
66 
67 #endif
68 
69 /* ref:         $Format:%D$ */
70 /* git commit:  $Format:%H$ */
71 /* commit time: $Format:%ai$ */
72