Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c07e6cf

Browse files
committed
Merge pull request #1962 from bartonjs/add-cryptography-aes
Add System.Security.Cryptography.Encryption.Aes Source And Tests
2 parents c8155b6 + 480dccd commit c07e6cf

25 files changed

+3789
-0
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
using Microsoft.Win32.SafeHandles;
8+
9+
internal static partial class Interop
10+
{
11+
internal static partial class libcrypto
12+
{
13+
// This was computed by sizeof(EVP_CIPHER_CTX) on a 64-bit Ubuntu
14+
// machine running OpenSSL 1.0.1f. If we end up making a native
15+
// interop boundary library for OpenSSL then a very good candidate
16+
// method would be EVP_CIPHER_CTX_new, so the size can be computed
17+
// to match the platform.
18+
internal const int EVP_CIPHER_CTX_SIZE = 168;
19+
20+
[DllImport(Libraries.LibCrypto)]
21+
internal static extern void EVP_CIPHER_CTX_init(SafeEvpCipherCtxHandle ctx);
22+
23+
[DllImport(Libraries.LibCrypto)]
24+
[return: MarshalAs(UnmanagedType.Bool)]
25+
internal static extern bool EVP_CipherInit_ex(
26+
SafeEvpCipherCtxHandle ctx,
27+
IntPtr cipher,
28+
IntPtr engineNull,
29+
byte[] key,
30+
byte[] iv,
31+
int enc);
32+
33+
[DllImport(Libraries.LibCrypto)]
34+
[return: MarshalAs(UnmanagedType.Bool)]
35+
internal static extern bool EVP_CIPHER_CTX_set_padding(SafeEvpCipherCtxHandle x, int padding);
36+
37+
[DllImport(Libraries.LibCrypto)]
38+
[return: MarshalAs(UnmanagedType.Bool)]
39+
internal static unsafe extern bool EVP_CipherUpdate(
40+
SafeEvpCipherCtxHandle ctx,
41+
byte* @out,
42+
out int outl,
43+
byte* @in,
44+
int inl);
45+
46+
[DllImport(Libraries.LibCrypto)]
47+
[return: MarshalAs(UnmanagedType.Bool)]
48+
internal static extern unsafe bool EVP_CipherFinal_ex(
49+
SafeEvpCipherCtxHandle ctx,
50+
byte* outm,
51+
out int outl);
52+
53+
[DllImport(Libraries.LibCrypto)]
54+
[return: MarshalAs(UnmanagedType.Bool)]
55+
internal static extern bool EVP_CIPHER_CTX_cleanup(IntPtr ctx);
56+
57+
[DllImport(Libraries.LibCrypto)]
58+
internal static extern IntPtr EVP_aes_128_ecb();
59+
60+
[DllImport(Libraries.LibCrypto)]
61+
internal static extern IntPtr EVP_aes_128_cbc();
62+
63+
//[DllImport(Libraries.LibCrypto)]
64+
//internal static extern IntPtr EVP_aes_128_cfb1();
65+
66+
//[DllImport(Libraries.LibCrypto)]
67+
//internal static extern IntPtr EVP_aes_128_cfb8();
68+
69+
//[DllImport(Libraries.LibCrypto)]
70+
//internal static extern IntPtr EVP_aes_128_cfb128();
71+
72+
//[DllImport(Libraries.LibCrypto)]
73+
//internal static extern IntPtr EVP_aes_128_ofb();
74+
75+
//[DllImport(Libraries.LibCrypto)]
76+
//internal static extern IntPtr EVP_aes_128_ctr();
77+
78+
//[DllImport(Libraries.LibCrypto)]
79+
//internal static extern IntPtr EVP_aes_128_ccm();
80+
81+
//[DllImport(Libraries.LibCrypto)]
82+
//internal static extern IntPtr EVP_aes_128_gcm();
83+
84+
//[DllImport(Libraries.LibCrypto)]
85+
//internal static extern IntPtr EVP_aes_128_xts();
86+
87+
//[DllImport(Libraries.LibCrypto)]
88+
//internal static extern IntPtr EVP_aes_128_wrap();
89+
90+
//[DllImport(Libraries.LibCrypto)]
91+
//internal static extern IntPtr EVP_aes_128_wrap_pad();
92+
93+
//[DllImport(Libraries.LibCrypto)]
94+
//internal static extern IntPtr EVP_aes_128_ocb();
95+
96+
[DllImport(Libraries.LibCrypto)]
97+
internal static extern IntPtr EVP_aes_192_ecb();
98+
99+
[DllImport(Libraries.LibCrypto)]
100+
internal static extern IntPtr EVP_aes_192_cbc();
101+
102+
//[DllImport(Libraries.LibCrypto)]
103+
//internal static extern IntPtr EVP_aes_192_cfb1();
104+
105+
//[DllImport(Libraries.LibCrypto)]
106+
//internal static extern IntPtr EVP_aes_192_cfb8();
107+
108+
//[DllImport(Libraries.LibCrypto)]
109+
//internal static extern IntPtr EVP_aes_192_cfb128();
110+
111+
//[DllImport(Libraries.LibCrypto)]
112+
//internal static extern IntPtr EVP_aes_192_ofb();
113+
114+
//[DllImport(Libraries.LibCrypto)]
115+
//internal static extern IntPtr EVP_aes_192_ctr();
116+
117+
//[DllImport(Libraries.LibCrypto)]
118+
//internal static extern IntPtr EVP_aes_192_ccm();
119+
120+
//[DllImport(Libraries.LibCrypto)]
121+
//internal static extern IntPtr EVP_aes_192_gcm();
122+
123+
//[DllImport(Libraries.LibCrypto)]
124+
//internal static extern IntPtr EVP_aes_192_wrap();
125+
126+
//[DllImport(Libraries.LibCrypto)]
127+
//internal static extern IntPtr EVP_aes_192_wrap_pad();
128+
129+
//[DllImport(Libraries.LibCrypto)]
130+
//internal static extern IntPtr EVP_aes_192_ocb();
131+
132+
[DllImport(Libraries.LibCrypto)]
133+
internal static extern IntPtr EVP_aes_256_ecb();
134+
135+
[DllImport(Libraries.LibCrypto)]
136+
internal static extern IntPtr EVP_aes_256_cbc();
137+
138+
//[DllImport(Libraries.LibCrypto)]
139+
//internal static extern IntPtr EVP_aes_256_cfb1();
140+
141+
//[DllImport(Libraries.LibCrypto)]
142+
//internal static extern IntPtr EVP_aes_256_cfb8();
143+
144+
//[DllImport(Libraries.LibCrypto)]
145+
//internal static extern IntPtr EVP_aes_256_cfb128();
146+
147+
//[DllImport(Libraries.LibCrypto)]
148+
//internal static extern IntPtr EVP_aes_256_ofb();
149+
150+
//[DllImport(Libraries.LibCrypto)]
151+
//internal static extern IntPtr EVP_aes_256_ctr();
152+
153+
//[DllImport(Libraries.LibCrypto)]
154+
//internal static extern IntPtr EVP_aes_256_ccm();
155+
156+
//[DllImport(Libraries.LibCrypto)]
157+
//internal static extern IntPtr EVP_aes_256_gcm();
158+
159+
//[DllImport(Libraries.LibCrypto)]
160+
//internal static extern IntPtr EVP_aes_256_xts();
161+
162+
//[DllImport(Libraries.LibCrypto)]
163+
//internal static extern IntPtr EVP_aes_256_wrap();
164+
165+
//[DllImport(Libraries.LibCrypto)]
166+
//internal static extern IntPtr EVP_aes_256_wrap_pad();
167+
168+
//[DllImport(Libraries.LibCrypto)]
169+
//internal static extern IntPtr EVP_aes_256_ocb();
170+
}
171+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Security;
6+
using System.Runtime.InteropServices;
7+
8+
namespace Microsoft.Win32.SafeHandles
9+
{
10+
[SecurityCritical]
11+
internal sealed class SafeEvpCipherCtxHandle : SafeHandle
12+
{
13+
private SafeEvpCipherCtxHandle() :
14+
base(IntPtr.Zero, ownsHandle: true)
15+
{
16+
}
17+
18+
[SecurityCritical]
19+
protected override bool ReleaseHandle()
20+
{
21+
Interop.libcrypto.EVP_CIPHER_CTX_cleanup(handle);
22+
Marshal.FreeHGlobal(handle);
23+
return true;
24+
}
25+
26+
public override bool IsInvalid
27+
{
28+
[SecurityCritical]
29+
get { return handle == IntPtr.Zero; }
30+
}
31+
32+
internal static SafeEvpCipherCtxHandle Create()
33+
{
34+
IntPtr memPtr = IntPtr.Zero;
35+
bool succeeded = false;
36+
SafeEvpCipherCtxHandle safeHandle = null;
37+
38+
try
39+
{
40+
memPtr = Marshal.AllocHGlobal(Interop.libcrypto.EVP_CIPHER_CTX_SIZE);
41+
safeHandle = new SafeEvpCipherCtxHandle();
42+
safeHandle.SetHandle(memPtr);
43+
44+
Interop.libcrypto.EVP_CIPHER_CTX_init(safeHandle);
45+
46+
succeeded = true;
47+
return safeHandle;
48+
}
49+
finally
50+
{
51+
if (!succeeded)
52+
{
53+
// If we made it to SetHandle, and failed calling EVP_CIPHER_CTX_init
54+
// then OpenSSL hasn't built this object yet, and we shouldn't call
55+
// EVP_CIPHER_CTX_cleanup.
56+
if (safeHandle != null && !safeHandle.IsInvalid)
57+
{
58+
safeHandle.SetHandleAsInvalid();
59+
}
60+
61+
if (memPtr != IntPtr.Zero)
62+
{
63+
Marshal.FreeHGlobal(memPtr);
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.22911.2
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Security.Cryptography.Encryption.Aes", "src\System.Security.Cryptography.Encryption.Aes.csproj", "{2EEB552A-0293-4824-B8A5-6725C3055215}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Security.Cryptography.Encryption.Aes.Tests", "tests\System.Security.Cryptography.Encryption.Aes.Tests.csproj", "{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Linux_Debug|Any CPU = Linux_Debug|Any CPU
13+
Linux_Release|Any CPU = Linux_Release|Any CPU
14+
OSX_Debug|Any CPU = OSX_Debug|Any CPU
15+
OSX_Release|Any CPU = OSX_Release|Any CPU
16+
Windows_Debug|Any CPU = Windows_Debug|Any CPU
17+
Windows_Release|Any CPU = Windows_Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20+
{2EEB552A-0293-4824-B8A5-6725C3055215}.Linux_Debug|Any CPU.ActiveCfg = Linux_Debug|Any CPU
21+
{2EEB552A-0293-4824-B8A5-6725C3055215}.Linux_Debug|Any CPU.Build.0 = Linux_Debug|Any CPU
22+
{2EEB552A-0293-4824-B8A5-6725C3055215}.Linux_Release|Any CPU.ActiveCfg = Linux_Release|Any CPU
23+
{2EEB552A-0293-4824-B8A5-6725C3055215}.Linux_Release|Any CPU.Build.0 = Linux_Release|Any CPU
24+
{2EEB552A-0293-4824-B8A5-6725C3055215}.OSX_Debug|Any CPU.ActiveCfg = OSX_Debug|Any CPU
25+
{2EEB552A-0293-4824-B8A5-6725C3055215}.OSX_Debug|Any CPU.Build.0 = OSX_Debug|Any CPU
26+
{2EEB552A-0293-4824-B8A5-6725C3055215}.OSX_Release|Any CPU.ActiveCfg = OSX_Release|Any CPU
27+
{2EEB552A-0293-4824-B8A5-6725C3055215}.OSX_Release|Any CPU.Build.0 = OSX_Release|Any CPU
28+
{2EEB552A-0293-4824-B8A5-6725C3055215}.Windows_Debug|Any CPU.ActiveCfg = Windows_Debug|Any CPU
29+
{2EEB552A-0293-4824-B8A5-6725C3055215}.Windows_Debug|Any CPU.Build.0 = Windows_Debug|Any CPU
30+
{2EEB552A-0293-4824-B8A5-6725C3055215}.Windows_Release|Any CPU.ActiveCfg = Windows_Release|Any CPU
31+
{2EEB552A-0293-4824-B8A5-6725C3055215}.Windows_Release|Any CPU.Build.0 = Windows_Release|Any CPU
32+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.Linux_Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.Linux_Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.Linux_Release|Any CPU.ActiveCfg = Debug|Any CPU
35+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.Linux_Release|Any CPU.Build.0 = Debug|Any CPU
36+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.OSX_Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.OSX_Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.OSX_Release|Any CPU.ActiveCfg = Debug|Any CPU
39+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.OSX_Release|Any CPU.Build.0 = Debug|Any CPU
40+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.Windows_Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.Windows_Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.Windows_Release|Any CPU.ActiveCfg = Debug|Any CPU
43+
{07B07E50-BBE9-41AA-89A5-17C82AF82CAA}.Windows_Release|Any CPU.Build.0 = Debug|Any CPU
44+
EndGlobalSection
45+
GlobalSection(SolutionProperties) = preSolution
46+
HideSolutionNode = FALSE
47+
EndGlobalSection
48+
EndGlobal

0 commit comments

Comments
 (0)