| 1 | #include <fe.h> |
| 2 | #include <sodium.h> |
| 3 | |
| 4 | #include <xeddsa.h> |
| 5 | |
| 6 | /** |
| 7 | * Convert a Curve25519 public key into an Ed25519 public key. |
| 8 | * |
| 9 | * @param ed25519_pub (out): The Ed25519 public key corresponding to the Curve25519 public key. |
| 10 | * @param curve25519_pub (in): The Curve25519 public key to convert into its Ed25519 equivalent. |
| 11 | * @param set_sign_bit: Whether to set the sign bit of the output Ed25519 public key. |
| 12 | */ |
| 13 | void curve25519_pub_to_ed25519_pub(uint8_t* ed25519_pub, |
| 14 | const uint8_t* curve25519_pub, |
| 15 | const bool set_sign_bit) { |
| 16 | // Apply the birational map defined in RFC 7748, section 4.1 "Curve25519" to calculate an Ed25519 public |
| 17 | // key from a Curve25519 public key. Because the serialization format of Curve25519 public keys only |
| 18 | // contains the u coordinate, the x coordinate of the corresponding Ed25519 public key can't be uniquely |
| 19 | // calculated as defined by the birational map. The x coordinate is represented in the serialization |
| 20 | // format of Ed25519 public keys only in a single sign bit. This function assumes that the sign bit is |
| 21 | // known to the user and is passed accordingly. |
| 22 | fe u, y; |
| 23 | fe one; |
| 24 | fe u_minus_one, u_plus_one, u_plus_one_inv; |
| 25 | |
| 26 | // Parse the Curve25519 public key input as a field element containing the u coordinate. RFC 7748, |
| 27 | // section 5 "The X25519 and X448 Functions", mandates that the most significant bit of the Curve25519 |
| 28 | // public key has to be zeroized. This is handled by fe_frombytes internally. |
| 29 | fe_frombytes(u, curve25519_pub); |
| 30 | |
| 31 | // Calculate the parameters (u - 1) and (u + 1) |
| 32 | fe_1(one); |
| 33 | fe_sub(u_minus_one, u, one); |
| 34 | fe_add(u_plus_one, u, one); |
| 35 | |
| 36 | // Invert u + 1 |
| 37 | fe_invert(u_plus_one_inv, u_plus_one); |
| 38 | |
| 39 | // Calculate y = (u - 1) * inv(u + 1) (mod p) |
| 40 | fe_mul(y, u_minus_one, u_plus_one_inv); |
| 41 | |
| 42 | // Serialize the field element containing the y coordinate to the Ed25519 public key output |
| 43 | fe_tobytes(ed25519_pub, y); |
| 44 | |
| 45 | // Set the sign bit to zero |
| 46 | ed25519_pub[31] &= 0x7f; |
| 47 | |
| 48 | // Set the sign bit to the requested value |
| 49 | ed25519_pub[31] |= !!set_sign_bit << 7; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Convert an Ed25519 public key into a Curve25519 public key. Re-export of libsodiums/ref10s |
| 54 | * `crypto_sign_ed25519_pk_to_curve25519` function for convenience. |
| 55 | * |
| 56 | * @param curve25519_pub (out): The Curve25519 public key corresponding to the Ed25519 public key. |
| 57 | * @param ed25519_pub (in): The Ed25519 public key to convert into its Curve25519 equivalent. |
| 58 | * @return 0 on success, -1 if the public key was rejected due to suboptimal security propierties. |
| 59 | */ |
| 60 | int ed25519_pub_to_curve25519_pub(uint8_t* curve25519_pub, const uint8_t* ed25519_pub) { |
| 61 | return crypto_sign_ed25519_pk_to_curve25519(curve25519_pub, ed25519_pub); |
| 62 | } |