@@ -228,40 +228,40 @@ func encryptPassword(password string, seed []byte, pub *rsa.PublicKey) ([]byte,
228228 return rsa .EncryptOAEP (sha1 , rand .Reader , pub , plain , nil )
229229}
230230
231- // Derived from https://github.com/MariaDB/server/blob/d8e6bb00888b1f82c031938f4c8ac5d97f6874c3/plugin/auth_ed25519/ref10/sign.c
232- func doEd25519Auth (scramble []byte , password string ) ([]byte , error ) {
231+ // authEd25519 does ed25519 authentication used by MariaDB.
232+ func authEd25519 (scramble []byte , password string ) ([]byte , error ) {
233+ // Derived from https://github.com/MariaDB/server/blob/d8e6bb00888b1f82c031938f4c8ac5d97f6874c3/plugin/auth_ed25519/ref10/sign.c
234+ // Code style is from https://cs.opensource.google/go/go/+/refs/tags/go1.21.5:src/crypto/ed25519/ed25519.go;l=207
233235 h := sha512 .Sum512 ([]byte (password ))
234236
235237 s , err := edwards25519 .NewScalar ().SetBytesWithClamping (h [:32 ])
236238 if err != nil {
237239 return nil , err
238240 }
241+ A := (& edwards25519.Point {}).ScalarBaseMult (s )
239242
240- nonceHash := sha512 .New ()
241- nonceHash .Write (h [32 :])
242- nonceHash .Write (scramble )
243- nonce := nonceHash .Sum (nil )
244-
245- r , err := edwards25519 .NewScalar ().SetUniformBytes (nonce )
243+ mh := sha512 .New ()
244+ mh .Write (h [32 :])
245+ mh .Write (scramble )
246+ messageDigest := mh .Sum (nil )
247+ r , err := edwards25519 .NewScalar ().SetUniformBytes (messageDigest )
246248 if err != nil {
247249 return nil , err
248250 }
249- R := (& edwards25519.Point {}).ScalarBaseMult (r )
250251
251- A := (& edwards25519.Point {}).ScalarBaseMult (s )
252-
253- kHash := sha512 .New ()
254- kHash .Write (R .Bytes ())
255- kHash .Write (A .Bytes ())
256- kHash .Write (scramble )
257- k := kHash .Sum (nil )
252+ R := (& edwards25519.Point {}).ScalarBaseMult (r )
258253
259- K , err := edwards25519 .NewScalar ().SetUniformBytes (k )
254+ kh := sha512 .New ()
255+ kh .Write (R .Bytes ())
256+ kh .Write (A .Bytes ())
257+ kh .Write (scramble )
258+ hramDigest := kh .Sum (nil )
259+ k , err := edwards25519 .NewScalar ().SetUniformBytes (hramDigest )
260260 if err != nil {
261261 return nil , err
262262 }
263263
264- S := K .MultiplyAdd (K , s , r )
264+ S := k .MultiplyAdd (k , s , r )
265265
266266 return append (R .Bytes (), S .Bytes ()... ), nil
267267}
@@ -335,8 +335,7 @@ func (mc *mysqlConn) auth(authData []byte, plugin string) ([]byte, error) {
335335 if len (authData ) != 32 {
336336 return nil , ErrMalformPkt
337337 }
338-
339- return doEd25519Auth (authData , mc .cfg .Passwd )
338+ return authEd25519 (authData , mc .cfg .Passwd )
340339
341340 default :
342341 mc .cfg .Logger .Print ("unknown auth plugin:" , plugin )
0 commit comments