Skip to content

Commit 170fd3d

Browse files
authored
Replace Marshal.SizeOf with sizeof operator
1 parent 53956ab commit 170fd3d

File tree

4 files changed

+23
-34
lines changed

4 files changed

+23
-34
lines changed

src/libraries/Common/src/System/Security/Cryptography/ECCng.ImportExport.NamedCurve.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,40 +152,29 @@ internal static SafeNCryptKeyHandle ImportKeyBlob(
152152

153153
using (SafeUnicodeStringHandle safeCurveName = new SafeUnicodeStringHandle(curveName))
154154
{
155-
Interop.BCrypt.BCryptBufferDesc desc = default;
156-
Interop.BCrypt.BCryptBuffer buff = default;
157-
158-
IntPtr descPtr = IntPtr.Zero;
159-
IntPtr buffPtr = IntPtr.Zero;
160-
try
155+
unsafe
161156
{
162-
descPtr = Marshal.AllocHGlobal(Marshal.SizeOf(desc));
163-
buffPtr = Marshal.AllocHGlobal(Marshal.SizeOf(buff));
157+
Interop.BCrypt.BCryptBufferDesc desc = default;
158+
Interop.BCrypt.BCryptBuffer buff = default;
159+
164160
buff.cbBuffer = (curveName.Length + 1) * 2; // Add 1 for null terminator
165161
buff.BufferType = Interop.BCrypt.CngBufferDescriptors.NCRYPTBUFFER_ECC_CURVE_NAME;
166162
buff.pvBuffer = safeCurveName.DangerousGetHandle();
167-
Marshal.StructureToPtr(buff, buffPtr, false);
168163

169164
desc.cBuffers = 1;
170-
desc.pBuffers = buffPtr;
165+
desc.pBuffers = (IntPtr)(&buff);
171166
desc.ulVersion = Interop.BCrypt.BCRYPTBUFFER_VERSION;
172-
Marshal.StructureToPtr(desc, descPtr, false);
173167

174168
errorCode = Interop.NCrypt.NCryptImportKey(
175169
provider,
176170
IntPtr.Zero,
177171
blobType,
178-
descPtr,
172+
(IntPtr)(&desc),
179173
out keyHandle,
180174
ref MemoryMarshal.GetReference(keyBlob),
181175
keyBlob.Length,
182176
0);
183177
}
184-
finally
185-
{
186-
Marshal.FreeHGlobal(descPtr);
187-
Marshal.FreeHGlobal(buffPtr);
188-
}
189178
}
190179

191180
if (errorCode != ErrorCode.ERROR_SUCCESS)

src/libraries/Common/src/System/Security/Cryptography/MLKem.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private protected unsafe void ReadCngMLKemBlob(
2626
throw new CryptographicException();
2727
}
2828

29-
int blobHeaderSize = Marshal.SizeOf<BCRYPT_MLKEM_KEY_BLOB>();
29+
int blobHeaderSize = sizeof(BCRYPT_MLKEM_KEY_BLOB);
3030
int keySize = checked((int)blob->cbKey);
3131

3232
if (keySize != destination.Length)

src/libraries/Common/src/System/Security/Cryptography/PqcBlobHelpers.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ internal delegate TReturn EncodeMLKemBlobCallback<TState, TReturn>(
216216
string blobKind,
217217
ReadOnlySpan<byte> blob);
218218

219-
internal static TReturn EncodeMLKemBlob<TState, TReturn>(
219+
internal static unsafe TReturn EncodeMLKemBlob<TState, TReturn>(
220220
KeyBlobMagicNumber kind,
221221
MLKemAlgorithm algorithm,
222222
ReadOnlySpan<byte> key,
@@ -230,7 +230,7 @@ internal static TReturn EncodeMLKemBlob<TState, TReturn>(
230230
// try to accommodate them.
231231
const int MaxKeyStackSize = 128;
232232
string parameterSet = GetMLKemParameterSet(algorithm);
233-
int blobHeaderSize = Marshal.SizeOf<BCRYPT_MLKEM_KEY_BLOB>();
233+
int blobHeaderSize = sizeof(BCRYPT_MLKEM_KEY_BLOB);
234234
int parameterSetMarshalLength = (parameterSet.Length + 1) * 2;
235235
int blobSize =
236236
blobHeaderSize +
@@ -246,15 +246,12 @@ internal static TReturn EncodeMLKemBlob<TState, TReturn>(
246246
{
247247
buffer.Clear();
248248

249-
unsafe
249+
fixed (byte* pBuffer = buffer)
250250
{
251-
fixed (byte* pBuffer = buffer)
252-
{
253-
BCRYPT_MLKEM_KEY_BLOB* blob = (BCRYPT_MLKEM_KEY_BLOB*)pBuffer;
254-
blob->dwMagic = kind;
255-
blob->cbParameterSet = (uint)parameterSetMarshalLength;
256-
blob->cbKey = (uint)key.Length;
257-
}
251+
BCRYPT_MLKEM_KEY_BLOB* blob = (BCRYPT_MLKEM_KEY_BLOB*)pBuffer;
252+
blob->dwMagic = kind;
253+
blob->cbParameterSet = (uint)parameterSetMarshalLength;
254+
blob->cbKey = (uint)key.Length;
258255
}
259256

260257
// This won't write the null byte, but we zeroed the whole buffer earlier.

src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/ChainPal.Windows.BuildChain.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal sealed partial class ChainPal : IDisposable, IChainPal
3636
using (SafeCertStoreHandle extraStoreHandle = ConvertStoreToSafeHandle(extraStore))
3737
{
3838
Interop.Crypt32.CERT_CHAIN_PARA chainPara = default;
39-
chainPara.cbSize = Marshal.SizeOf<Interop.Crypt32.CERT_CHAIN_PARA>();
39+
chainPara.cbSize = sizeof(Interop.Crypt32.CERT_CHAIN_PARA);
4040

4141
int applicationPolicyCount;
4242
using (SafeHandle applicationPolicyOids = applicationPolicy!.ToLpstrArray(out applicationPolicyCount))
@@ -88,12 +88,15 @@ private static SafeChainEngineHandle GetChainEngine(
8888
if (trustMode == X509ChainTrustMode.CustomRootTrust)
8989
{
9090
// Need to get a valid SafeCertStoreHandle otherwise the default stores will be trusted
91-
using (SafeCertStoreHandle customTrustStoreHandle = ConvertStoreToSafeHandle(customTrustStore, true))
91+
unsafe
9292
{
93-
Interop.Crypt32.CERT_CHAIN_ENGINE_CONFIG customChainEngine = default;
94-
customChainEngine.cbSize = Marshal.SizeOf<Interop.Crypt32.CERT_CHAIN_ENGINE_CONFIG>();
95-
customChainEngine.hExclusiveRoot = customTrustStoreHandle.DangerousGetHandle();
96-
chainEngineHandle = Interop.crypt32.CertCreateCertificateChainEngine(ref customChainEngine);
93+
using (SafeCertStoreHandle customTrustStoreHandle = ConvertStoreToSafeHandle(customTrustStore, true))
94+
{
95+
Interop.Crypt32.CERT_CHAIN_ENGINE_CONFIG customChainEngine = default;
96+
customChainEngine.cbSize = sizeof(Interop.Crypt32.CERT_CHAIN_ENGINE_CONFIG);
97+
customChainEngine.hExclusiveRoot = customTrustStoreHandle.DangerousGetHandle();
98+
chainEngineHandle = Interop.crypt32.CertCreateCertificateChainEngine(ref customChainEngine);
99+
}
97100
}
98101
}
99102
else

0 commit comments

Comments
 (0)