1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5 /**
6 @file ec25519_crypto_ctx.c
7 curve25519 crypto context helper
8 */
9
10 #ifdef LTC_CURVE25519
11
ec25519_crypto_ctx(unsigned char * out,unsigned long * outlen,unsigned char flag,const unsigned char * ctx,unsigned long ctxlen)12 int ec25519_crypto_ctx(unsigned char *out, unsigned long *outlen, unsigned char flag, const unsigned char *ctx, unsigned long ctxlen)
13 {
14 unsigned char *buf = out;
15
16 const char *prefix = "SigEd25519 no Ed25519 collisions";
17 const unsigned long prefix_len = XSTRLEN(prefix);
18 const unsigned char ctxlen8 = (unsigned char)ctxlen;
19
20 if (ctxlen > 255u) return CRYPT_INPUT_TOO_LONG;
21 if (*outlen < prefix_len + 2u + ctxlen) return CRYPT_BUFFER_OVERFLOW;
22
23 XMEMCPY(buf, prefix, prefix_len);
24 buf += prefix_len;
25 XMEMCPY(buf, &flag, 1);
26 buf++;
27 XMEMCPY(buf, &ctxlen8, 1);
28 buf++;
29
30 if (ctxlen > 0u) {
31 LTC_ARGCHK(ctx != NULL);
32 XMEMCPY(buf, ctx, ctxlen);
33 buf += ctxlen;
34 }
35
36 *outlen = buf-out;
37
38 return CRYPT_OK;
39 }
40
41 #endif
42