13
13
// limitations under the License.
14
14
15
15
using System ;
16
- using System . IO ;
17
16
using System . Linq ;
18
17
using System . Security . Cryptography ;
19
18
using Xunit ;
@@ -40,45 +39,48 @@ public void KeyAgree_Succeeds(
40
39
var ( testPublicKey , testPrivateKey ) = TestKeys . GetKeyPair ( keyType ) ;
41
40
var testDevice = IntegrationTestDeviceEnumeration . GetTestDevice ( testDeviceType ) ;
42
41
var privateKeyParameters = AsnPrivateKeyReader . CreateKeyParameters ( testPrivateKey . EncodedKey ) ;
43
- IPublicKeyParameters publicKeyPeer ;
42
+ IPublicKeyParameters peerPublicKey ;
43
+ var peerPrivateKeyEcParameters = new ECParameters ( ) ;
44
+
44
45
if ( keyType is KeyType . X25519 )
45
46
{
46
47
var testSelectedPublicKeyPeer = TestKeys . GetTestPublicKey ( keyType , 2 ) ;
47
- publicKeyPeer = AsnPublicKeyReader . CreateKeyParameters ( testSelectedPublicKeyPeer . EncodedKey ) ;
48
+ peerPublicKey = Curve25519PublicKeyParameters . CreateFromPkcs8 ( testSelectedPublicKeyPeer . EncodedKey ) ;
48
49
}
49
50
else
50
51
{
51
- var oid = keyType . ToCurveOid ( ) ! ;
52
- var curve = ECCurve . CreateFromValue ( oid ) ;
53
- publicKeyPeer = ECPublicKeyParameters . CreateFromParameters ( ECDsa . Create ( curve ) . ExportParameters ( false ) ) ;
52
+ var curve = ECCurve . CreateFromValue ( keyType . ToCurveOid ( ) ! ) ;
53
+ var ecDsa = ECDsa . Create ( curve ) ;
54
+ peerPrivateKeyEcParameters = ecDsa . ExportParameters ( true ) ;
55
+ var peerPublicKeyEcParameters = ecDsa . ExportParameters ( false ) ;
56
+ peerPublicKey = ECPublicKeyParameters . CreateFromParameters ( peerPublicKeyEcParameters ) ;
54
57
}
55
58
59
+ // -> Import Private Key
56
60
using var pivSession = GetSession ( testDevice ) ;
57
61
pivSession . ImportPrivateKey ( 0x85 , privateKeyParameters , pinPolicy , PivTouchPolicy . Never ) ;
58
- var metadata = pivSession . GetMetadata ( 0x85 ) ;
59
-
62
+
60
63
// Act
61
- var sharedSecret = pivSession . KeyAgree ( 0x85 , publicKeyPeer ) ;
64
+ var yubikeySecret = pivSession . KeyAgree ( 0x85 , peerPublicKey ) ;
62
65
63
66
// Assert
64
- var publicPoint = metadata . PublicKeyParameters switch
65
- {
66
- ECPublicKeyParameters ecDsa => ecDsa . PublicPoint . ToArray ( ) ,
67
- Curve25519PublicKeyParameters edDsa => edDsa . PublicPoint . ToArray ( ) ,
68
- _ => throw new ArgumentException ( "Invalid public key type" )
69
- } ;
70
-
71
- Assert . Equal ( testPublicKey . GetPublicPoint ( ) , publicPoint ) ;
72
67
if ( keyType is KeyType . X25519 )
73
68
{
69
+ // We have pre-generated shared secrets for X25519
74
70
const string keyAgreeFilename = "x25519_private_and_public2_shared_secret.bin" ;
75
71
var expectedSharedSecret = TestCrypto . ReadTestData ( keyAgreeFilename ) ;
76
- Assert . Equal ( expectedSharedSecret , sharedSecret ) ;
72
+ Assert . Equal ( expectedSharedSecret , yubikeySecret ) ;
77
73
}
78
74
else
79
75
{
80
- var expectedSecretLength = publicKeyPeer . KeyDefinition . LengthInBytes ;
81
- Assert . Equal ( expectedSecretLength , sharedSecret . Length ) ;
76
+ // Perform ECDH using generated key and the imported YK public key
77
+ using var peerEcdh = ECDiffieHellman . Create ( peerPrivateKeyEcParameters ) ;
78
+ var yubiKeyParametersPublic = testPublicKey . AsECDsa ( ) . ExportParameters ( false ) ;
79
+ using var yubikeyEcdh = ECDiffieHellman . Create ( yubiKeyParametersPublic ) ;
80
+ var peerSecret = peerEcdh . DeriveRawSecretAgreement ( yubikeyEcdh . PublicKey ) ;
81
+
82
+ Assert . Equal ( yubikeySecret . Length , peerSecret . Length ) ;
83
+ Assert . Equal ( yubikeySecret , peerSecret ) ;
82
84
}
83
85
}
84
86
0 commit comments