VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstVBoxCrypto.cpp@ 94980

Last change on this file since 94980 was 94980, checked in by vboxsync, 3 years ago

Main/testcase: Skip the testcase if the crpytographic module is not available, bugref:9955

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: tstVBoxCrypto.cpp 94980 2022-05-10 10:31:25Z vboxsync $ */
2/** @file
3 * tstVBoxCrypto - Testcase for the cryptographic support module.
4 */
5
6/*
7 * Copyright (C) 2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/VBoxCryptoIf.h>
23#include <VBox/err.h>
24
25#include <iprt/test.h>
26#include <iprt/ldr.h>
27#include <iprt/mem.h>
28#include <iprt/memsafer.h>
29#include <iprt/string.h>
30#include <iprt/vfs.h>
31
32
33/*********************************************************************************************************************************
34* Global Variables *
35*********************************************************************************************************************************/
36static RTTEST g_hTest;
37static const uint8_t g_abDek[64] = { 0x42 };
38static const char g_szPassword[] = "testtesttest";
39static const char g_szPasswordWrong[] = "testtest";
40
41static const char *g_aCiphers[] =
42{
43 "AES-XTS128-PLAIN64",
44 "AES-GCM128",
45 "AES-CTR128",
46
47 "AES-XTS256-PLAIN64",
48 "AES-GCM256",
49 "AES-CTR256"
50};
51
52#define CHECK_STR(str1, str2) do { if (strcmp(str1, str2)) { RTTestIFailed("line %u: '%s' != '%s' (*)", __LINE__, str1, str2); } } while (0)
53#define CHECK_BYTES(bytes1, bytes2, size) do { if (memcmp(bytes1, bytes2, size)) { RTTestIFailed("line %u: '%s' != '%s' (*)", __LINE__, #bytes1, bytes2); } } while (0)
54
55
56/**
57 * Testing some basics of the crypto keystore code.
58 *
59 * @returns nothing.
60 * @param pCryptoIf Pointer to the callback table.
61 */
62static void tstCryptoKeyStoreBasics(PCVBOXCRYPTOIF pCryptoIf)
63{
64 RTTestISub("Crypto Keystore - Basics");
65
66 RTTestDisableAssertions(g_hTest);
67
68 for (uint32_t i = 0; i < RT_ELEMENTS(g_aCiphers); i++)
69 {
70 RTTestISubF("Creating a new keystore for cipher '%s'", g_aCiphers[i]);
71
72 char *pszKeystoreEnc = NULL; /**< The encoded keystore. */
73 int rc = pCryptoIf->pfnCryptoKeyStoreCreate(g_szPassword, &g_abDek[0], sizeof(g_abDek),
74 g_aCiphers[i], &pszKeystoreEnc);
75 if (RT_SUCCESS(rc))
76 {
77 uint8_t *pbKey = NULL;
78 size_t cbKey = 0;
79 char *pszCipher = NULL;
80
81 RTTestSub(g_hTest, "Trying to unlock DEK with wrong password");
82 rc = pCryptoIf->pfnCryptoKeyStoreGetDekFromEncoded(pszKeystoreEnc, g_szPasswordWrong,
83 &pbKey, &cbKey, &pszCipher);
84 RTTESTI_CHECK_RC(rc, VERR_VD_PASSWORD_INCORRECT);
85
86 RTTestSub(g_hTest, "Trying to unlock DEK with correct password");
87 rc = pCryptoIf->pfnCryptoKeyStoreGetDekFromEncoded(pszKeystoreEnc, g_szPassword,
88 &pbKey, &cbKey, &pszCipher);
89 RTTESTI_CHECK_RC_OK(rc);
90 if (RT_SUCCESS(rc))
91 {
92 RTTESTI_CHECK(cbKey == sizeof(g_abDek));
93 CHECK_STR(pszCipher, g_aCiphers[i]);
94 CHECK_BYTES(pbKey, &g_abDek[0], sizeof(g_abDek));
95
96 RTMemSaferFree(pbKey, cbKey);
97 }
98
99 RTMemFree(pszKeystoreEnc);
100 }
101 else
102 RTTestIFailed("Creating a new keystore failed with %Rrc", rc);
103 }
104
105 RTTestRestoreAssertions(g_hTest);
106}
107
108
109int main(int argc, char *argv[])
110{
111 /*
112 * Initialization.
113 */
114 RTEXITCODE rcExit = RTTestInitAndCreate("tstVBoxCrypto", &g_hTest);
115 if (rcExit != RTEXITCODE_SUCCESS)
116 return rcExit;
117 RTTestBanner(g_hTest);
118
119 RTTestSub(g_hTest, "Loading the cryptographic support module");
120 const char *pszModCrypto = NULL;
121 if (argc == 2)
122 {
123 /* The module to load is given on the command line. */
124 pszModCrypto = argv[1];
125 }
126 else
127 {
128 /* Try find it in the extension pack. */
129 /** @todo */
130 RTTestSkipped(g_hTest, "Getting the module from the extension pack is not implemented yet, skipping testcase");
131 }
132
133 if (pszModCrypto)
134 {
135 RTLDRMOD hLdrModCrypto = NIL_RTLDRMOD;
136 int rc = RTLdrLoad(pszModCrypto, &hLdrModCrypto);
137 if (RT_SUCCESS(rc))
138 {
139 PFNVBOXCRYPTOENTRY pfnCryptoEntry = NULL;
140 rc = RTLdrGetSymbol(hLdrModCrypto, VBOX_CRYPTO_MOD_ENTRY_POINT, (void **)&pfnCryptoEntry);
141 if (RT_SUCCESS(rc))
142 {
143 PCVBOXCRYPTOIF pCryptoIf = NULL;
144 rc = pfnCryptoEntry(&pCryptoIf);
145 if (RT_SUCCESS(rc))
146 {
147 /* Loading succeeded, now we can start real testing. */
148 tstCryptoKeyStoreBasics(pCryptoIf);
149 }
150 else
151 RTTestIFailed("Calling '%s' failed with %Rrc", VBOX_CRYPTO_MOD_ENTRY_POINT, rc);
152 }
153 else
154 RTTestIFailed("Failed to resolve entry point '%s' with %Rrc", VBOX_CRYPTO_MOD_ENTRY_POINT, rc);
155 }
156 else
157 RTTestIFailed("Failed to load the crypto module '%s' with %Rrc", pszModCrypto, rc);
158 }
159
160 return RTTestSummaryAndDestroy(g_hTest);
161}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette