|
1 | 1 | package keyconv_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/ecdh" |
4 | 5 | "crypto/ecdsa" |
| 6 | + "crypto/rand" |
5 | 7 | "crypto/rsa" |
6 | 8 | "testing" |
7 | 9 |
|
@@ -205,3 +207,126 @@ func TestKeyconv(t *testing.T) { |
205 | 207 | }) |
206 | 208 | }) |
207 | 209 | } |
| 210 | + |
| 211 | +func TestECDHToECDSA(t *testing.T) { |
| 212 | + curves := []struct { |
| 213 | + name string |
| 214 | + ecdhCurve ecdh.Curve |
| 215 | + jwaAlg jwa.EllipticCurveAlgorithm |
| 216 | + }{ |
| 217 | + {"P256", ecdh.P256(), jwa.P256()}, |
| 218 | + {"P384", ecdh.P384(), jwa.P384()}, |
| 219 | + {"P521", ecdh.P521(), jwa.P521()}, |
| 220 | + } |
| 221 | + |
| 222 | + for _, curve := range curves { |
| 223 | + t.Run(curve.name, func(t *testing.T) { |
| 224 | + // Generate an ECDSA key for comparison |
| 225 | + ecdsaKey, err := jwxtest.GenerateEcdsaKey(curve.jwaAlg) |
| 226 | + require.NoError(t, err, `ecdsa.GenerateKey should succeed`) |
| 227 | + |
| 228 | + // Convert ECDSA key to ECDH key |
| 229 | + ecdhPrivKey, err := ecdsaKey.ECDH() |
| 230 | + require.NoError(t, err, `ECDSA to ECDH conversion should succeed`) |
| 231 | + |
| 232 | + ecdhPubKey := ecdhPrivKey.PublicKey() |
| 233 | + |
| 234 | + t.Run("PrivateKey", func(t *testing.T) { |
| 235 | + testcases := []struct { |
| 236 | + name string |
| 237 | + src any |
| 238 | + error bool |
| 239 | + }{ |
| 240 | + {"*ecdh.PrivateKey", ecdhPrivKey, false}, |
| 241 | + {"invalid type", "not a key", true}, |
| 242 | + } |
| 243 | + |
| 244 | + for _, tc := range testcases { |
| 245 | + t.Run(tc.name, func(t *testing.T) { |
| 246 | + var dst *ecdsa.PrivateKey |
| 247 | + err := keyconv.ECDHToECDSA(&dst, tc.src) |
| 248 | + |
| 249 | + if tc.error { |
| 250 | + require.Error(t, err, `ECDHToECDSA should fail for invalid input`) |
| 251 | + } else { |
| 252 | + require.NoError(t, err, `ECDHToECDSA should succeed`) |
| 253 | + require.NotNil(t, dst, `destination should not be nil`) |
| 254 | + |
| 255 | + // Verify the converted key has the same curve |
| 256 | + require.Equal(t, ecdsaKey.Curve, dst.Curve, `curves should match`) |
| 257 | + |
| 258 | + // Verify the private key values match |
| 259 | + require.Equal(t, ecdsaKey.D, dst.D, `private key values should match`) |
| 260 | + |
| 261 | + // Verify the public key coordinates match |
| 262 | + require.Equal(t, ecdsaKey.PublicKey.X, dst.PublicKey.X, `X coordinates should match`) |
| 263 | + require.Equal(t, ecdsaKey.PublicKey.Y, dst.PublicKey.Y, `Y coordinates should match`) |
| 264 | + } |
| 265 | + }) |
| 266 | + } |
| 267 | + }) |
| 268 | + |
| 269 | + t.Run("PublicKey", func(t *testing.T) { |
| 270 | + testcases := []struct { |
| 271 | + name string |
| 272 | + src any |
| 273 | + error bool |
| 274 | + }{ |
| 275 | + {"*ecdh.PublicKey", ecdhPubKey, false}, |
| 276 | + {"ecdh.PublicKey", *ecdhPubKey, false}, |
| 277 | + {"invalid type", "not a key", true}, |
| 278 | + } |
| 279 | + |
| 280 | + for _, tc := range testcases { |
| 281 | + t.Run(tc.name, func(t *testing.T) { |
| 282 | + var dst *ecdsa.PublicKey |
| 283 | + err := keyconv.ECDHToECDSA(&dst, tc.src) |
| 284 | + |
| 285 | + if tc.error { |
| 286 | + require.Error(t, err, `ECDHToECDSA should fail for invalid input`) |
| 287 | + } else { |
| 288 | + require.NoError(t, err, `ECDHToECDSA should succeed`) |
| 289 | + require.NotNil(t, dst, `destination should not be nil`) |
| 290 | + |
| 291 | + // Verify the converted key has the same curve |
| 292 | + require.Equal(t, ecdsaKey.PublicKey.Curve, dst.Curve, `curves should match`) |
| 293 | + |
| 294 | + // Verify the public key coordinates match |
| 295 | + require.Equal(t, ecdsaKey.PublicKey.X, dst.X, `X coordinates should match`) |
| 296 | + require.Equal(t, ecdsaKey.PublicKey.Y, dst.Y, `Y coordinates should match`) |
| 297 | + } |
| 298 | + }) |
| 299 | + } |
| 300 | + }) |
| 301 | + |
| 302 | + t.Run("RoundTrip", func(t *testing.T) { |
| 303 | + // Test that ECDSA -> ECDH -> ECDSA produces the same key |
| 304 | + var convertedPrivKey *ecdsa.PrivateKey |
| 305 | + err := keyconv.ECDHToECDSA(&convertedPrivKey, ecdhPrivKey) |
| 306 | + require.NoError(t, err, `ECDHToECDSA should succeed`) |
| 307 | + |
| 308 | + var convertedPubKey *ecdsa.PublicKey |
| 309 | + err = keyconv.ECDHToECDSA(&convertedPubKey, ecdhPubKey) |
| 310 | + require.NoError(t, err, `ECDHToECDSA should succeed`) |
| 311 | + |
| 312 | + // Verify the keys are equivalent |
| 313 | + require.Equal(t, ecdsaKey.D, convertedPrivKey.D, `private key values should match`) |
| 314 | + require.Equal(t, ecdsaKey.PublicKey.X, convertedPrivKey.PublicKey.X, `private key X coordinates should match`) |
| 315 | + require.Equal(t, ecdsaKey.PublicKey.Y, convertedPrivKey.PublicKey.Y, `private key Y coordinates should match`) |
| 316 | + require.Equal(t, ecdsaKey.PublicKey.X, convertedPubKey.X, `public key X coordinates should match`) |
| 317 | + require.Equal(t, ecdsaKey.PublicKey.Y, convertedPubKey.Y, `public key Y coordinates should match`) |
| 318 | + }) |
| 319 | + }) |
| 320 | + } |
| 321 | + |
| 322 | + t.Run("UnsupportedCurve", func(t *testing.T) { |
| 323 | + // Create a mock ECDH key with X25519 curve (not supported for ECDSA) |
| 324 | + x25519Key, err := ecdh.X25519().GenerateKey(rand.Reader) |
| 325 | + require.NoError(t, err, `X25519 key generation should succeed`) |
| 326 | + |
| 327 | + var dst *ecdsa.PrivateKey |
| 328 | + err = keyconv.ECDHToECDSA(&dst, x25519Key) |
| 329 | + require.Error(t, err, `ECDHToECDSA should fail for unsupported curve`) |
| 330 | + require.Contains(t, err.Error(), "unsupported ECDH curve", `error should mention unsupported curve`) |
| 331 | + }) |
| 332 | +} |
0 commit comments