@@ -10,7 +10,7 @@ namespace MySqlConnector.Authentication.Ed25519;
10
10
/// Provides an implementation of the <c>client_ed25519</c> authentication plugin for MariaDB.
11
11
/// </summary>
12
12
/// <remarks>See <a href="https://mariadb.com/kb/en/library/authentication-plugin-ed25519/">Authentication Plugin - ed25519</a>.</remarks>
13
- public sealed class Ed25519AuthenticationPlugin : IAuthenticationPlugin
13
+ public sealed class Ed25519AuthenticationPlugin : IAuthenticationPlugin2
14
14
{
15
15
/// <summary>
16
16
/// Registers the Ed25519 authentication plugin with MySqlConnector. You must call this method once before
@@ -31,6 +31,24 @@ public static void Install()
31
31
/// Creates the authentication response.
32
32
/// </summary>
33
33
public byte [ ] CreateResponse ( string password , ReadOnlySpan < byte > authenticationData )
34
+ {
35
+ CreateResponseAndHash ( password , authenticationData , out _ , out var authenticationResponse ) ;
36
+ return authenticationResponse ;
37
+ }
38
+
39
+ /// <summary>
40
+ /// Creates the Ed25519 password hash.
41
+ /// </summary>
42
+ public byte [ ] CreatePasswordHash ( string password , ReadOnlySpan < byte > authenticationData )
43
+ {
44
+ CreateResponseAndHash ( password , authenticationData , out var passwordHash , out _ ) ;
45
+ return passwordHash ;
46
+ }
47
+
48
+ /// <summary>
49
+ /// Creates the authentication response.
50
+ /// </summary>
51
+ private static void CreateResponseAndHash ( string password , ReadOnlySpan < byte > authenticationData , out byte [ ] passwordHash , out byte [ ] authenticationResponse )
34
52
{
35
53
// Java reference: https://github.com/MariaDB/mariadb-connector-j/blob/master/src/main/java/org/mariadb/jdbc/internal/com/send/authentication/Ed25519PasswordPlugin.java
36
54
// C reference: https://github.com/MariaDB/server/blob/592fe954ef82be1bc08b29a8e54f7729eb1e1343/plugin/auth_ed25519/ref10/sign.c#L7
@@ -109,6 +127,9 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
109
127
GroupOperations . ge_scalarmult_base ( out var A , az , 0 ) ;
110
128
GroupOperations . ge_p3_tobytes ( sm , 32 , ref A ) ;
111
129
130
+ passwordHash = new byte [ 32 ] ;
131
+ Array . Copy ( sm , 32 , passwordHash , 0 , 32 ) ;
132
+
112
133
/*** Java
113
134
nonce = scalar.reduce(nonce);
114
135
GroupElement elementRvalue = spec.getB().scalarMultiply(nonce);
@@ -152,30 +173,7 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
152
173
153
174
var result = new byte [ 64 ] ;
154
175
Buffer . BlockCopy ( sm , 0 , result , 0 , result . Length ) ;
155
- return result ;
156
- }
157
-
158
- /// <summary>
159
- /// Creates the ed25519 password hash.
160
- /// </summary>
161
- public byte [ ] CreatePasswordHash ( string password , ReadOnlySpan < byte > authenticationData )
162
- {
163
- byte [ ] passwordBytes = Encoding . UTF8 . GetBytes ( password ) ;
164
- using var sha512 = SHA512 . Create ( ) ;
165
- byte [ ] az = sha512 . ComputeHash ( passwordBytes ) ;
166
- ScalarOperations . sc_clamp ( az , 0 ) ;
167
-
168
- byte [ ] sm = new byte [ 64 + authenticationData . Length ] ;
169
- authenticationData . CopyTo ( sm . AsSpan ( ) . Slice ( 64 ) ) ;
170
- Buffer . BlockCopy ( az , 32 , sm , 32 , 32 ) ;
171
- sha512 . ComputeHash ( sm , 32 , authenticationData . Length + 32 ) ;
172
-
173
- GroupOperations . ge_scalarmult_base ( out var A , az , 0 ) ;
174
- GroupOperations . ge_p3_tobytes ( sm , 32 , ref A ) ;
175
-
176
- byte [ ] res = new byte [ 32 ] ;
177
- Array . Copy ( sm , 32 , res , 0 , 32 ) ;
178
- return res ;
176
+ authenticationResponse = result ;
179
177
}
180
178
181
179
private Ed25519AuthenticationPlugin ( )
0 commit comments