@@ -12,168 +12,134 @@ internal static partial class Interop
1212{ 
1313    internal  static   partial  class  Crypto 
1414    { 
15-         [ Experimental ( Experimentals . PostQuantumCryptographyDiagId ) ] 
16-         internal  static   partial  class  EvpPKeyMLDsa 
15+         [ LibraryImport ( Libraries . CryptoNative ,  StringMarshalling  =  StringMarshalling . Utf8 ) ] 
16+         private  static   partial  SafeEvpPKeyHandle  CryptoNative_MLDsaGenerateKey ( string  keyType ,  ReadOnlySpan < byte >  seed ,  int  seedLength ) ; 
17+ 
18+         internal  static   SafeEvpPKeyHandle  MLDsaGenerateKey ( string  algorithmName ,  ReadOnlySpan < byte >  seed ) 
1719        { 
18-             internal  static   string ?  MLDsa44  {  get ;  } 
19-             internal  static   string ?  MLDsa65  {  get ;  } 
20-             internal  static   string ?  MLDsa87  {  get ;  } 
20+             SafeEvpPKeyHandle  handle  =  CryptoNative_MLDsaGenerateKey ( algorithmName ,  seed ,  seed . Length ) ; 
21+             Debug . Assert ( handle  !=  null ,  "handle != null" ) ; 
2122
22-             static   EvpPKeyMLDsa ( ) 
23+             if   ( handle . IsInvalid ) 
2324            { 
24-                 CryptoInitializer . Initialize ( ) ; 
25- 
26-                 // Do not use property initializers for these because we need to ensure CryptoInitializer.Initialize 
27-                 // is called first. Property initializers happen before cctors, so instead set the property after the 
28-                 // initializer is run. 
29-                 MLDsa44  =  IsSignatureAlgorithmAvailable ( MLDsaAlgorithm . MLDsa44 . Name ) ; 
30-                 MLDsa65  =  IsSignatureAlgorithmAvailable ( MLDsaAlgorithm . MLDsa65 . Name ) ; 
31-                 MLDsa87  =  IsSignatureAlgorithmAvailable ( MLDsaAlgorithm . MLDsa87 . Name ) ; 
25+                 Exception  ex  =  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
26+                 handle . Dispose ( ) ; 
27+                 throw  ex ; 
3228            } 
3329
34-             [ LibraryImport ( Libraries . CryptoNative ,   StringMarshalling   =   StringMarshalling . Utf8 ) ] 
35-              private   static   partial   int   CryptoNative_IsSignatureAlgorithmAvailable ( string   algorithm ) ; 
30+             return   handle ; 
31+         } 
3632
37-             private  static   string ?  IsSignatureAlgorithmAvailable ( string  algorithm ) 
38-             { 
39-                 const  int  Available  =  1 ; 
40-                 const  int  NotAvailable  =  0 ; 
41- 
42-                 int  ret  =  CryptoNative_IsSignatureAlgorithmAvailable ( algorithm ) ; 
43-                 return  ret  switch 
44-                 { 
45-                     Available  =>  algorithm , 
46-                     NotAvailable  =>  null , 
47-                     int  other  =>  throw  Fail ( other ) , 
48-                 } ; 
49- 
50-                 static   CryptographicException  Fail ( int  result ) 
51-                 { 
52-                     Debug . Fail ( $ "Unexpected result { result }  from { nameof ( CryptoNative_IsSignatureAlgorithmAvailable ) } ") ; 
53-                     return  new  CryptographicException ( ) ; 
54-                 } 
55-             } 
33+         [ LibraryImport ( Libraries . CryptoNative ,  StringMarshalling  =  StringMarshalling . Utf8 ) ] 
34+         private  static   partial  SafeEvpPKeyHandle  CryptoNative_MLDsaImportSecretKey ( string  keyType ,  ReadOnlySpan < byte >  sk ,  int  skLength ) ; 
5635
57-             [ LibraryImport ( Libraries . CryptoNative ,  StringMarshalling  =  StringMarshalling . Utf8 ) ] 
58-             private  static   partial  SafeEvpPKeyHandle ?  CryptoNative_MLDsaGenerateKey ( string  keyType ,  ReadOnlySpan < byte >  seed ,  int  seedLength ) ; 
36+         internal  static   SafeEvpPKeyHandle  MLDsaImportSecretKey ( string  algorithmName ,  ReadOnlySpan < byte >  sk ) 
37+         { 
38+             SafeEvpPKeyHandle ?  handle  =  CryptoNative_MLDsaImportSecretKey ( algorithmName ,  sk ,  sk . Length ) ; 
39+             Debug . Assert ( handle  !=  null ,  "handle != null" ) ; 
5940
60-             public   static   SafeEvpPKeyHandle   MLDsaGenerateKey ( string   algorithmName ,   ReadOnlySpan < byte >   seed ) 
41+             if   ( handle . IsInvalid ) 
6142            { 
62-                 SafeEvpPKeyHandle ?  handle  =  CryptoNative_MLDsaGenerateKey ( algorithmName ,  seed ,  seed . Length ) ; 
63- 
64-                 if  ( handle  ==  null  ||  handle . IsInvalid ) 
65-                 { 
66-                     throw  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
67-                 } 
68- 
69-                 return  handle ; 
43+                 Exception  ex  =  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
44+                 handle . Dispose ( ) ; 
45+                 throw  ex ; 
7046            } 
7147
72-             [ LibraryImport ( Libraries . CryptoNative ,   StringMarshalling   =   StringMarshalling . Utf8 ) ] 
73-              private   static   partial   SafeEvpPKeyHandle ?   CryptoNative_MLDsaImportSecretKey ( string   keyType ,   ReadOnlySpan < byte >   sk ,   int   skLength ) ; 
48+             return   handle ; 
49+         } 
7450
75-             public  static   SafeEvpPKeyHandle  MLDsaImportSecretKey ( string  algorithmName ,  ReadOnlySpan < byte >  sk ) 
76-             { 
77-                 SafeEvpPKeyHandle ?  handle  =  CryptoNative_MLDsaImportSecretKey ( algorithmName ,  sk ,  sk . Length ) ; 
51+         [ LibraryImport ( Libraries . CryptoNative ,  StringMarshalling  =  StringMarshalling . Utf8 ) ] 
52+         private  static   partial  SafeEvpPKeyHandle  CryptoNative_MLDsaImportPublicKey ( string  keyType ,  ReadOnlySpan < byte >  pk ,  int  pkLength ) ; 
7853
79-                  if   ( handle   ==   null   ||   handle . IsInvalid ) 
80-                  { 
81-                      throw   Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
82-                  } 
54+         internal   static   SafeEvpPKeyHandle   MLDsaImportPublicKey ( string   algorithmName ,   ReadOnlySpan < byte >   pk ) 
55+         { 
56+             SafeEvpPKeyHandle   handle   =   CryptoNative_MLDsaImportPublicKey ( algorithmName ,   pk ,   pk . Length ) ; 
57+             Debug . Assert ( handle   !=   null ,   "handle != null" ) ; 
8358
84-                 return  handle ; 
59+             if  ( handle . IsInvalid ) 
60+             { 
61+                 Exception  ex  =  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
62+                 handle . Dispose ( ) ; 
63+                 throw  ex ; 
8564            } 
8665
87-             [ LibraryImport ( Libraries . CryptoNative ,  StringMarshalling  =  StringMarshalling . Utf8 ) ] 
88-             private  static   partial  SafeEvpPKeyHandle ?  CryptoNative_MLDsaImportPublicKey ( string  keyType ,  ReadOnlySpan < byte >  pk ,  int  pkLength ) ; 
66+             return  handle ; 
67+         } 
68+ 
69+         [ LibraryImport ( Libraries . CryptoNative ) ] 
70+         private  static   partial  int  CryptoNative_MLDsaSignPure ( 
71+             SafeEvpPKeyHandle  pkey ,  IntPtr  extraHandle , 
72+             ReadOnlySpan < byte >  msg ,  int  msgLength , 
73+             ReadOnlySpan < byte >  context ,  int  contextLength , 
74+             Span < byte >  destination ,  int  destinationLength ) ; 
75+ 
76+         internal  static   void  MLDsaSignPure ( 
77+             SafeEvpPKeyHandle  pkey , 
78+             ReadOnlySpan < byte >  msg , 
79+             ReadOnlySpan < byte >  context , 
80+             Span < byte >  destination ) 
81+         { 
82+             int  ret  =  CryptoNative_MLDsaSignPure ( 
83+                 pkey ,  pkey . ExtraHandle , 
84+                 msg ,  msg . Length , 
85+                 context ,  context . Length , 
86+                 destination ,  destination . Length ) ; 
8987
90-             public   static   SafeEvpPKeyHandle   MLDsaImportPublicKey ( string   algorithmName ,   ReadOnlySpan < byte >   pk ) 
88+             if   ( ret   !=   1 ) 
9189            { 
92-                 SafeEvpPKeyHandle ?  handle  =  CryptoNative_MLDsaImportPublicKey ( algorithmName ,  pk ,  pk . Length ) ; 
90+                 throw  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
91+             } 
92+         } 
9393
94-                 if  ( handle  ==  null  ||  handle . IsInvalid ) 
95-                 { 
96-                     throw  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
97-                 } 
94+         [ LibraryImport ( Libraries . CryptoNative ) ] 
95+         private  static   partial  int  CryptoNative_MLDsaVerifyPure ( 
96+             SafeEvpPKeyHandle  pkey ,  IntPtr  extraHandle , 
97+             ReadOnlySpan < byte >  msg ,  int  msgLength , 
98+             ReadOnlySpan < byte >  context ,  int  contextLength , 
99+             ReadOnlySpan < byte >  signature ,  int  signatureLength ) ; 
100+ 
101+         internal  static   bool  MLDsaVerifyPure ( 
102+             SafeEvpPKeyHandle  pkey , 
103+             ReadOnlySpan < byte >  msg , 
104+             ReadOnlySpan < byte >  context , 
105+             ReadOnlySpan < byte >  signature ) 
106+         { 
107+             int  ret  =  CryptoNative_MLDsaVerifyPure ( 
108+                 pkey ,  pkey . ExtraHandle , 
109+                 msg ,  msg . Length , 
110+                 context ,  context . Length , 
111+                 signature ,  signature . Length ) ; 
98112
99-                 return  handle ; 
113+             if  ( ret  ==  1 ) 
114+             { 
115+                 return  true ; 
100116            } 
101- 
102-             [ LibraryImport ( Libraries . CryptoNative ) ] 
103-             private  static   partial  int  CryptoNative_MLDsaSignPure ( 
104-                 SafeEvpPKeyHandle  pkey ,  IntPtr  extraHandle , 
105-                 ReadOnlySpan < byte >  msg ,  int  msgLength , 
106-                 ReadOnlySpan < byte >  context ,  int  contextLength , 
107-                 Span < byte >  destination ,  int  destinationLength ) ; 
108- 
109-             public  static   void  MLDsaSignPure ( 
110-                 SafeEvpPKeyHandle  pkey , 
111-                 ReadOnlySpan < byte >  msg , 
112-                 ReadOnlySpan < byte >  context , 
113-                 Span < byte >  destination ) 
117+             else  if  ( ret  ==  0 ) 
114118            { 
115-                 int  ret  =  CryptoNative_MLDsaSignPure ( 
116-                     pkey ,  pkey . ExtraHandle , 
117-                     msg ,  msg . Length , 
118-                     context ,  context . Length , 
119-                     destination ,  destination . Length ) ; 
120- 
121-                 if  ( ret  !=  1 ) 
122-                 { 
123-                     throw  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
124-                 } 
119+                 return  false ; 
125120            } 
126- 
127-             [ LibraryImport ( Libraries . CryptoNative ) ] 
128-             private  static   partial  int  CryptoNative_MLDsaVerifyPure ( 
129-                 SafeEvpPKeyHandle  pkey ,  IntPtr  extraHandle , 
130-                 ReadOnlySpan < byte >  msg ,  int  msgLength , 
131-                 ReadOnlySpan < byte >  context ,  int  contextLength , 
132-                 ReadOnlySpan < byte >  signature ,  int  signatureLength ) ; 
133- 
134-             public  static   bool  MLDsaVerifyPure ( 
135-                 SafeEvpPKeyHandle  pkey , 
136-                 ReadOnlySpan < byte >  msg , 
137-                 ReadOnlySpan < byte >  context , 
138-                 ReadOnlySpan < byte >  signature ) 
121+             else 
139122            { 
140-                 int  ret  =  CryptoNative_MLDsaVerifyPure ( 
141-                     pkey ,  pkey . ExtraHandle , 
142-                     msg ,  msg . Length , 
143-                     context ,  context . Length , 
144-                     signature ,  signature . Length ) ; 
145- 
146-                 if  ( ret  ==  1 ) 
147-                 { 
148-                     return  true ; 
149-                 } 
150-                 else  if  ( ret  ==  0 ) 
151-                 { 
152-                     return  false ; 
153-                 } 
154-                 else 
155-                 { 
156-                     throw  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
157-                 } 
123+                 throw  Interop . Crypto . CreateOpenSslCryptographicException ( ) ; 
158124            } 
125+         } 
159126
160-              [ LibraryImport ( Libraries . CryptoNative ) ] 
161-              private  static   partial  int  CryptoNative_MLDsaExportSecretKey ( SafeEvpPKeyHandle  pkey ,  Span < byte >  destination ,  int  destinationLength ) ; 
127+         [ LibraryImport ( Libraries . CryptoNative ) ] 
128+         private  static   partial  int  CryptoNative_MLDsaExportSecretKey ( SafeEvpPKeyHandle  pkey ,  Span < byte >  destination ,  int  destinationLength ) ; 
162129
163-              [ LibraryImport ( Libraries . CryptoNative ) ] 
164-              private  static   partial  int  CryptoNative_MLDsaExportSeed ( SafeEvpPKeyHandle  pkey ,  Span < byte >  destination ,  int  destinationLength ) ; 
130+         [ LibraryImport ( Libraries . CryptoNative ) ] 
131+         private  static   partial  int  CryptoNative_MLDsaExportSeed ( SafeEvpPKeyHandle  pkey ,  Span < byte >  destination ,  int  destinationLength ) ; 
165132
166-              [ LibraryImport ( Libraries . CryptoNative ) ] 
167-              private  static   partial  int  CryptoNative_MLDsaExportPublicKey ( SafeEvpPKeyHandle  pkey ,  Span < byte >  destination ,  int  destinationLength ) ; 
133+         [ LibraryImport ( Libraries . CryptoNative ) ] 
134+         private  static   partial  int  CryptoNative_MLDsaExportPublicKey ( SafeEvpPKeyHandle  pkey ,  Span < byte >  destination ,  int  destinationLength ) ; 
168135
169-              public  static   void  MLDsaExportSecretKey ( SafeEvpPKeyHandle  key ,  Span < byte >  destination )  => 
170-                  Interop . Crypto . ExportKeyContents ( key ,  destination ,  CryptoNative_MLDsaExportSecretKey ) ; 
136+         internal  static   void  MLDsaExportSecretKey ( SafeEvpPKeyHandle  key ,  Span < byte >  destination )  => 
137+             Interop . Crypto . ExportKeyContents ( key ,  destination ,  CryptoNative_MLDsaExportSecretKey ) ; 
171138
172-              public  static   void  MLDsaExportSeed ( SafeEvpPKeyHandle  key ,  Span < byte >  destination )  => 
173-                  Interop . Crypto . ExportKeyContents ( key ,  destination ,  CryptoNative_MLDsaExportSeed ) ; 
139+         internal  static   void  MLDsaExportSeed ( SafeEvpPKeyHandle  key ,  Span < byte >  destination )  => 
140+             Interop . Crypto . ExportKeyContents ( key ,  destination ,  CryptoNative_MLDsaExportSeed ) ; 
174141
175-             public  static   void  MLDsaExportPublicKey ( SafeEvpPKeyHandle  key ,  Span < byte >  destination )  => 
176-                 Interop . Crypto . ExportKeyContents ( key ,  destination ,  CryptoNative_MLDsaExportPublicKey ) ; 
177-         } 
142+         internal  static   void  MLDsaExportPublicKey ( SafeEvpPKeyHandle  key ,  Span < byte >  destination )  => 
143+             Interop . Crypto . ExportKeyContents ( key ,  destination ,  CryptoNative_MLDsaExportPublicKey ) ; 
178144    } 
179145} 
0 commit comments