# Analysis of Key Storage Mechanism of Asymmetric Key-Related Functions in CNG Crypto Library
## 3 Methodology  
### Research Question  
As described above, the CNG cannot obtain information on the actual key because it is not directly receiving the key information during the encryption and decryption processes due to the fact that the key is processed through the handle obtained from the import and export processes (Question 1). Thus, when given the handle address of a key, the goal of this paper is to analyze the location of the actual key from the key handle address, namely to analyze the key storage mechanism (Question 2).  
The functions related to encryption and decryption in CNG are classified into BCrypt, SSL, and NCrypt. The encryption and decryption functions of BCrypt are called BCryptEncrypt and BCryptDecrypt, respectively. The encryption and decryption functions of SSL are called SslEncryptPacket and SslDecryptPacket, respectively. The encryption and decryption functions of NCrypt are called NCryptEncrypt and NCryptDecrypt, respectively. In this paper, we analyze the key storage mechanism of NCrypt and the asymmetric-key-based RSA algorithm.  
The analysis environment is based on Intel(r) Core(tm) i5-2410M CPU 2.30 GHz, 4 GB RAM, a PC installed with Microsoft Windows 7 Home Premium K operating system, CNG SDK 2.0, Windows SDK 7.1, and.NET Framework 4 installation, Visual Studio 2005 to implement proof-of-concept tools, CNG Demo Sample Code installation for CNG sample source code reference, and OllyDbg v1.10 reverse engineering tool to analyze the key storage mechanism.  
To analyze the key location in NCrypt-related functions, we first implemented a sample program based on the RSA algorithm where the plain text is \"SignThis\" and the key is \"test2\" by referring to the example of Windows CNG Demo Sample Code. As a result, the implemented program executes encryption and decryption operations, as shown in Figure 3, and outputs the result, as shown in Figure 4.  
Figure 3: NCrypt sample program flow (RSA).  
To analyze the key location, we confirmed the related parameters of the NCryptDecrypt function after setting a breakpoint on the NCryptDecrypt function. As a result, we found that the first parameter is the address associated with the key handle, as shown in Figure 5. Nevertheless, there was no specific information related to the key, as shown in Figure 6, after checking the key handle address 0x0051A288 in detail.  
Figure 4: NCrypt sample program execution result (RSA).  
Figure 5: Analysis of the key handle address when calling the NCryptDecrypt function (RSA).  
Figure 6: Structure of the NCrypt key handle (RSA).  
Since there is no way to obtain key-related information through the key handle address, we, therefore, derived clues about the key location by checking the key-related information when the key is loaded.  
Figure 7 analyzes the key-related parameters after setting a breakpoint on the NCryptImportKey function and shows that the sixth parameter stores the key information, while the seventh parameter stores the key size. The sixth parameter for storing the key information is the address 0x004 BE80, and it stores a value that starts with \"RSA2\", as shown in Figure 8. Nevertheless, Figure 9 shows that as a result of inferring the location from the key handle based on the key information when the key is loaded, the exact location could not be analyzed (response to Question 1).