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 ecc_ssh_ecdsa_encode_name.c
14 Curve/OID to SSH+ECDSA name string mapping per RFC5656
15 Russ Williams
16 */
17
18 /**
19 Curve/OID to SSH+ECDSA name string mapping
20 @param buffer [out] The destination for the name
21 @param buflen [in/out] The max size and resulting size (including terminator) of the name
22 @param key A public or private ECC key
23 @return CRYPT_OK if successful
24 */
ecc_ssh_ecdsa_encode_name(char * buffer,unsigned long * buflen,const ecc_key * key)25 int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key)
26 {
27 char oidstr[64];
28 unsigned long oidlen = sizeof(oidstr);
29 unsigned long size = 0;
30 int err;
31
32 LTC_ARGCHK(buffer != NULL);
33 LTC_ARGCHK(buflen != NULL);
34 LTC_ARGCHK(key != NULL);
35
36 /* Get the OID of the curve */
37 if ((err = ecc_get_oid_str(oidstr, &oidlen, key)) != CRYPT_OK) goto error;
38
39 /* Check for three named curves: nistp256, nistp384, nistp521 */
40 if (XSTRCMP("1.2.840.10045.3.1.7", oidstr) == 0) {
41 /* nistp256 - secp256r1 - OID 1.2.840.10045.3.1.7 */
42 size = snprintf(buffer, *buflen, "ecdsa-sha2-nistp256");
43 }
44 else if (XSTRCMP("1.3.132.0.34", oidstr) == 0) {
45 /* nistp384 - secp384r1 - OID 1.3.132.0.34 */
46 size = snprintf(buffer, *buflen, "ecdsa-sha2-nistp384");
47 }
48 else if (XSTRCMP("1.3.132.0.35", oidstr) == 0) {
49 /* nistp521 - secp521r1 - OID 1.3.132.0.35 */
50 size = snprintf(buffer, *buflen, "ecdsa-sha2-nistp521");
51 } else {
52 /* Otherwise we use the OID... */
53 size = snprintf(buffer, *buflen, "ecdsa-sha2-%s", oidstr);
54 }
55
56 /* snprintf returns size that would have been written, but limits to buflen-1 chars plus terminator */
57 if (size >= *buflen) {
58 err = CRYPT_BUFFER_OVERFLOW;
59 } else {
60 err = CRYPT_OK;
61 }
62 *buflen = size + 1; /* the string length + NUL byte */
63
64 error:
65 return err;
66 }
67
68
69 /* ref: $Format:%D$ */
70 /* git commit: $Format:%H$ */
71 /* commit time: $Format:%ai$ */
72