758fb3d6f922d29e3bdbceefb7040ab02fac0186
[platform/upstream/dotnet/runtime.git] /
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3
4 using Internal.Cryptography;
5
6 namespace System.Security.Cryptography
7 {
8     internal sealed partial class DesImplementation
9     {
10         private static UniversalCryptoTransform CreateTransformCore(
11             CipherMode cipherMode,
12             PaddingMode paddingMode,
13             byte[] key,
14             byte[]? iv,
15             int blockSize,
16             int feedbackSize,
17             int paddingSize,
18             bool encrypting)
19         {
20             // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
21             IntPtr algorithm = GetAlgorithm(cipherMode, feedbackSize);
22
23             Interop.Crypto.EnsureLegacyAlgorithmsRegistered();
24
25             BasicSymmetricCipher cipher = new OpenSslCipher(algorithm, cipherMode, blockSize, paddingSize, key, iv, encrypting);
26             return UniversalCryptoTransform.Create(paddingMode, cipher, encrypting);
27         }
28
29         private static ILiteSymmetricCipher CreateLiteCipher(
30             CipherMode cipherMode,
31             ReadOnlySpan<byte> key,
32             ReadOnlySpan<byte> iv,
33             int blockSize,
34             int feedbackSize,
35             int paddingSize,
36             bool encrypting)
37         {
38             // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
39             IntPtr algorithm = GetAlgorithm(cipherMode, feedbackSize);
40
41             Interop.Crypto.EnsureLegacyAlgorithmsRegistered();
42             return new OpenSslCipherLite(algorithm, blockSize, paddingSize, key, iv, encrypting);
43         }
44
45         private static IntPtr GetAlgorithm(CipherMode cipherMode, int feedbackSize)
46         {
47             return cipherMode switch
48             {
49                 CipherMode.CBC => Interop.Crypto.EvpDesCbc(),
50                 CipherMode.ECB => Interop.Crypto.EvpDesEcb(),
51                 CipherMode.CFB when feedbackSize == 1 => Interop.Crypto.EvpDesCfb8(),
52                 _ => throw new NotSupportedException(),
53             };
54         }
55     }
56 }