-static struct ec_curve *ec_name_to_curve(const char *name, int len) {
- if (len > 11 && !memcmp(name, "ecdsa-sha2-", 11)) {
- name += 11;
- len -= 11;
- } else if (len > 10 && !memcmp(name, "ecdh-sha2-", 10)) {
- name += 10;
- len -= 10;
- } else if (len == 11 && !memcmp(name, "ssh-ed25519", 11)) {
- return ec_ed25519();
- }
-
- if (len == 8 && !memcmp(name, "nistp", 5)) {
- name += 5;
- if (!memcmp(name, "256", 3)) {
- return ec_p256();
- } else if (!memcmp(name, "384", 3)) {
- return ec_p384();
- } else if (!memcmp(name, "521", 3)) {
- return ec_p521();
- }
- }
-
- if (len == 28 && !memcmp(name, "curve25519-sha256@libssh.org", 28)) {
- return ec_curve25519();
- }
-
- return NULL;
-}
-
-/* Type enumeration for specifying the curve name */
-enum ec_name_type { EC_TYPE_DSA, EC_TYPE_DH, EC_TYPE_CURVE };
-
-static int ec_curve_to_name(enum ec_name_type type, const struct ec_curve *curve,
- unsigned char *name, int len) {
- if (curve->type == EC_WEIERSTRASS) {
- int length, loc;
- if (type == EC_TYPE_DSA) {
- length = 19;
- loc = 16;
- } else if (type == EC_TYPE_DH) {
- length = 18;
- loc = 15;
- } else {
- length = 8;
- loc = 5;
- }
-
- /* Return length of string */
- if (name == NULL) return length;
-
- /* Not enough space for the name */
- if (len < length) return 0;
-
- /* Put the name in the buffer */
- switch (curve->fieldBits) {
- case 256:
- memcpy(name+loc, "256", 3);
- break;
- case 384:
- memcpy(name+loc, "384", 3);
- break;
- case 521:
- memcpy(name+loc, "521", 3);
- break;
- default:
- return 0;
- }
-
- if (type == EC_TYPE_DSA) {
- memcpy(name, "ecdsa-sha2-nistp", 16);
- } else if (type == EC_TYPE_DH) {
- memcpy(name, "ecdh-sha2-nistp", 15);
- } else {
- memcpy(name, "nistp", 5);
- }
-
- return length;
- } else if (curve->type == EC_EDWARDS) {
- /* No DH for ed25519 - use Montgomery instead */
- if (type == EC_TYPE_DH) return 0;
-
- if (type == EC_TYPE_CURVE) {
- /* Return length of string */
- if (name == NULL) return 7;
-
- /* Not enough space for the name */
- if (len < 7) return 0;
-
- /* Unknown curve field */
- if (curve->fieldBits != 256) return 0;
-
- memcpy(name, "ed25519", 7);
- return 7;
-
- } else {
- /* Return length of string */
- if (name == NULL) return 11;
-
- /* Not enough space for the name */
- if (len < 11) return 0;
-
- /* Unknown curve field */
- if (curve->fieldBits != 256) return 0;
-
- memcpy(name, "ssh-ed25519", 11);
- return 11;
- }
- } else {
- /* No DSA for curve25519 */
- if (type == EC_TYPE_DSA || type == EC_TYPE_CURVE) return 0;
-
- /* Return length of string */
- if (name == NULL) return 28;
-
- /* Not enough space for the name */
- if (len < 28) return 0;
-
- /* Unknown curve field */
- if (curve->fieldBits != 256) return 0;
-
- memcpy(name, "curve25519-sha256@libssh.org", 28);
- return 28;
- }
-}
-