1 //
2 // Mono.Security.Cryptography.CryptoAPI
3 //
4 // Authors:
5 //	Sebastien Pouliot (spouliot@motus.com)
6 //
7 // Copyright (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9 
10 using System;
11 using System.Runtime.InteropServices;
12 
13 namespace Mono.Security.Cryptography {
14 
15 internal class CryptoAPI {
16 
17 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptAcquireContextA(ref IntPtr phProv, string pszContainer, string pszProvider, int dwProvType, uint dwFlags)18 	public static extern bool CryptAcquireContextA (ref IntPtr phProv, string pszContainer, string pszProvider, int dwProvType, uint dwFlags);
19 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, ref IntPtr phHash)20 	public static extern bool CryptCreateHash (IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, ref IntPtr phHash);
21 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptDecrypt(IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen)22 	public static extern bool CryptDecrypt (IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen);
23 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptDestroyHash(IntPtr hHash)24 	public static extern bool CryptDestroyHash (IntPtr hHash);
25 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptDestroyKey(IntPtr hKey)26 	public static extern bool CryptDestroyKey (IntPtr hKey);
27 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptEncrypt(IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen, uint dwBufLen)28 	public static extern bool CryptEncrypt (IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, byte[] pbData, ref uint pdwDataLen, uint dwBufLen);
29 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptGenKey(IntPtr hProv, uint Algid, uint dwFlags, ref IntPtr phKey)30 	public static extern bool CryptGenKey (IntPtr hProv, uint Algid, uint dwFlags, ref IntPtr phKey);
31 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptGenRandom(IntPtr hProv, uint dwLen, byte[] pbBuffer)32 	public static extern bool CryptGenRandom (IntPtr hProv, uint dwLen, byte[] pbBuffer);
33 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptGetHashParam(IntPtr hHash, uint dwParam, byte[] pbData, ref uint pdwDataLen, uint dwFlags)34 	public static extern bool CryptGetHashParam (IntPtr hHash, uint dwParam, byte[] pbData, ref uint pdwDataLen, uint dwFlags);
35 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptHashData(IntPtr hHash, byte[] pbData, uint dwDataLen, uint dwFlags)36 	public static extern bool CryptHashData (IntPtr hHash, byte[] pbData, uint dwDataLen, uint dwFlags);
37 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptImportKey(IntPtr hProv, byte[] pbData, uint dwDataLen, IntPtr hPubKey, uint dwFlags, ref IntPtr phKey)38 	public static extern bool CryptImportKey (IntPtr hProv, byte[] pbData, uint dwDataLen, IntPtr hPubKey, uint dwFlags, ref IntPtr phKey);
39 	[DllImport ("advapi32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
CryptReleaseContext(IntPtr hProv, uint dwFlags)40 	public static extern bool CryptReleaseContext (IntPtr hProv, uint dwFlags);
41 
42 	public static readonly uint CRYPT_VERIFYCONTEXT  = 0xF0000000;
43 	public static readonly uint CRYPT_NEWKEYSET      = 0x00000008;
44 	public static readonly uint CRYPT_DELETEKEYSET   = 0x00000010;
45 	public static readonly uint CRYPT_MACHINE_KEYSET = 0x00000020;
46 	public static readonly uint CRYPT_SILENT         = 0x00000040;
47 
48 	public static readonly int PROV_RSA_FULL         = 1;
49 
50 	public static readonly uint HP_HASHVAL           = 0x0002;
51 
52 	public static readonly int CALG_MD2  = 0x8001;
53 	public static readonly int CALG_MD4  = 0x8002;
54 	public static readonly int CALG_MD5  = 0x8003;
55 	public static readonly int CALG_SHA1 = 0x8004;
56 
57 	// just so we don't have to add System.Runtime.InteropServices
58 	// in every file
GetLastError()59 	static public int GetLastError ()
60 	{
61 		return Marshal.GetLastWin32Error ();
62 	}
63 }
64 
65 }
66