1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/** @file
* VirtualBox - Cryptographic support functions Interface.
*/
/*
* Copyright (C) 2022-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
#ifndef VBOX_INCLUDED_VBoxCryptoIf_h
#define VBOX_INCLUDED_VBoxCryptoIf_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
#include <iprt/vfs.h>
#include <VBox/types.h>
/** An opaque VBox cryptographic context handle. */
typedef struct VBOXCRYPTOCTXINT *VBOXCRYPTOCTX;
/**Pointer to an opaque VBox cryptographic context handle. */
typedef VBOXCRYPTOCTX *PVBOXCRYPTOCTX;
/** Magic identifying the cryptographic interface (Charles Babbage). */
#define VBOXCRYPTOIF_MAGIC UINT32_C(0x17911226)
/** Pointer to const cryptographic interface. */
typedef const struct VBOXCRYPTOIF *PCVBOXCRYPTOIF;
/**
* The main cryptographic callbacks interface table.
*/
typedef struct VBOXCRYPTOIF
{
/** Interface magic, set to VBOXCRYPTOIF_MAGIC. */
uint32_t u32Magic;
/** Interface version.
* This is set to VBOXCRYPTOIF_VERSION. */
uint32_t u32Version;
/** Description string. */
const char *pszDesc;
/** @name Generic crytographic context operations.
* @{ */
/**
* Creates a new cryptographic context for encryption.
*
* @returns VBox status code.
* @param pszCipher The identifier of the cipher to use.
* @param pszPassword Password for encrypting the context.
* @param phCryptoCtx Where to store the handle to the crypto context on success.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxCreate, (const char *pszCipher, const char *pszPassword,
PVBOXCRYPTOCTX phCryptoCtx));
/**
* Creates a new cryptographic context for decryption from the given base-64 encoded context.
*
* @returns VBox status code.
* @param pszStoredCtx The base-64 encoded context to decrypt with the given password.
* @param pszPassword Password for encrypting the context.
* @param phCryptoCtx Where to store the handle to the crypto context on success.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxLoad, (const char *pszStoredCtx, const char *pszPassword,
PVBOXCRYPTOCTX phCryptoCtx));
/**
* Destroys a previously created cryptographic context.
*
* @returns VBox status code.
* @param hCryptoCtx Handle of crpytographic context to destroy.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxDestroy, (VBOXCRYPTOCTX hCryptoCtx));
/**
* Returns the given cryptographic context as a base-64 encoded string.
*
* @returns VBox status code.
* @param hCryptoCtx Handle of crpytographic context.
* @param ppszStoredCtx Where to store the base-64 encoded cryptographic context on success.
* Must be freed with RTMemFree() when not required anymore.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxSave, (VBOXCRYPTOCTX hCryptoCtx, char **ppszStoredCtx));
/**
* Changes the encryption password for the given context.
*
* @returns VBox status code.
* @param hCryptoCtx Handle of crpytographic context.
* @param pszPassword New password used for encrypting the DEK.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxPasswordChange, (VBOXCRYPTOCTX hCryptoCtx, const char *pszPassword));
/**
* Queries the required size of the output buffer for encrypted data. Depends on the cipher.
*
* @returns VBox status code.
* @param hCryptoCtx Handle of crpytographic context.
* @param cbPlainText The size of the data to be encrypted.
* @param pcbEncrypted Where to store the size in bytes of the encrypted data on success.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxQueryEncryptedSize, (VBOXCRYPTOCTX hCryptoCtx, size_t cbPlaintext,
size_t *pcbEncrypted));
/**
* Queries the required size of the output buffer for decrypted data. Depends on the cipher.
*
* @returns VBox status code.
* @param hCryptoCtx Handle of crpytographic context.
* @param cbEncrypted The size of the encrypted chunk before decryption.
* @param pcbPlaintext Where to store the size in bytes of the decrypted data on success.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxQueryDecryptedSize, (VBOXCRYPTOCTX hCryptoCtx, size_t cbEncrypted,
size_t *pcbPlaintext));
/**
* Encrypts data.
*
* @returns VBox status code.
* @param hCryptoCtx Handle of crpytographic context.
* @param fPartial Only part of data to be encrypted is specified. The encryption
* cipher context will not be closed. Set to false for last piece
* of data, or if data is specified completely.
* Only CTR mode supports partial encryption.
* @param pvIV Pointer to IV. If null it will be generated.
* @param cbIV Size of the IV.
* @param pvPlainText Data to encrypt.
* @param cbPlainText Size of the data in the pvPlainText.
* @param pvAuthData Data used for authenticate the pvPlainText
* @param cbAuthData Size of the pvAuthData
* @param pvEncrypted Buffer to store encrypted data
* @param cbEncrypted Size of the buffer in pvEncrypted
* @param pcbEncrypted Placeholder where the size of the encrypted data returned.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxEncrypt, (VBOXCRYPTOCTX hCryptoCtx, bool fPartial, void const *pvIV, size_t cbIV,
void const *pvPlainText, size_t cbPlainText,
void const *pvAuthData, size_t cbAuthData,
void *pvEncrypted, size_t cbEncrypted,
size_t *pcbEncrypted));
/**
* Decrypts data.
*
* @returns VBox status code.
* @param hCryptoCtx Handle of crpytographic context.
* @param fPartial Only part of data to be encrypted is specified. The encryption
* cipher context will not be closed. Set to false for last piece
* of data, or if data is specified completely.
* Only CTR mode supports partial encryption.
* @param pvEncrypted Data to decrypt.
* @param cbEncrypted Size of the data in the pvEncrypted.
* @param pvAuthData Data used for authenticate the pvEncrypted
* @param cbAuthData Size of the pvAuthData
* @param pvPlainText Buffer to store decrypted data
* @param cbPlainText Size of the buffer in pvPlainText
* @param pcbPlainText Placeholder where the size of the decrypted data returned.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoCtxDecrypt, (VBOXCRYPTOCTX hCryptoCtx, bool fPartial,
void const *pvEncrypted, size_t cbEncrypted,
void const *pvAuthData, size_t cbAuthData,
void *pvPlainText, size_t cbPlainText, size_t *pcbPlainText));
/** @} */
/** @name File based cryptographic operations.
* @{ */
/**
* Creates a new VFS file handle for an encrypted or to be encrypted file handle.
*
* @returns VBox status code.
* @param hVfsFile The input file handle, a new reference is retained upon success.
* @param pszKeyStore The key store containing the DEK used for encryption.
* @param pszPassword Password encrypting the DEK.
* @param phVfsFile Where to store the handle to the VFS file on success.
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoFileFromVfsFile, (RTVFSFILE hVfsFile, const char *pszKeyStore, const char *pszPassword,
PRTVFSFILE phVfsFile));
/**
* Opens a new encryption I/O stream.
*
* @returns VBox status code.
* @param hVfsIosDst The encrypted output stream (must be writeable).
* The reference is not consumed, instead another
* one is retained.
* @param pszKeyStore The key store containing the DEK used for encryption.
* @param pszPassword Password encrypting the DEK.
* @param phVfsIosCrypt Where to return the crypting input I/O stream handle
* (you write to this).
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoIoStrmFromVfsIoStrmEncrypt, (RTVFSIOSTREAM hVfsIosDst, const char *pszKeyStore,
const char *pszPassword, PRTVFSIOSTREAM phVfsIosCrypt));
/**
* Opens a new decryption I/O stream.
*
* @returns VBox status code.
* @param hVfsIosIn The encrypted input stream (must be readable).
* The reference is not consumed, instead another
* one is retained.
* @param pszKeyStore The key store containing the DEK used for encryption.
* @param pszPassword Password encrypting the DEK.
* @param phVfsIosOut Where to return the handle to the decrypted I/O stream (read).
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoIoStrmFromVfsIoStrmDecrypt, (RTVFSIOSTREAM hVfsIosIn, const char *pszKeyStore,
const char *pszPassword, PRTVFSIOSTREAM phVfsIosOut));
/** @} */
/** @name Keystore related functions.
* @{ */
/**
* Return the encryption parameters and DEK from the base64 encoded key store data.
*
* @returns VBox status code.
* @param pszEnc The base64 encoded key store data.
* @param pszPassword The password to use for key decryption.
* If the password is NULL only the cipher is returned.
* @param ppbKey Where to store the DEK on success.
* Must be freed with RTMemSaferFree().
* @param pcbKey Where to store the DEK size in bytes on success.
* @param ppszCipher Where to store the used cipher for the decrypted DEK.
* Must be freed with RTStrFree().
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoKeyStoreGetDekFromEncoded, (const char *pszEnc, const char *pszPassword,
uint8_t **ppbKey, size_t *pcbKey, char **ppszCipher));
/**
* Stores the given DEK in a key store protected by the given password.
*
* @returns VBox status code.
* @param pszPassword The password to protect the DEK.
* @param pbKey The DEK to protect.
* @param cbKey Size of the DEK to protect.
* @param pszCipher The cipher string associated with the DEK.
* @param ppszEnc Where to store the base64 encoded key store data on success.
* Must be freed with RTMemFree().
*/
DECLR3CALLBACKMEMBER(int, pfnCryptoKeyStoreCreate, (const char *pszPassword, const uint8_t *pbKey, size_t cbKey,
const char *pszCipher, char **ppszEnc));
/** @} */
DECLR3CALLBACKMEMBER(int, pfnReserved1,(void)); /**< Reserved for minor structure revisions. */
DECLR3CALLBACKMEMBER(int, pfnReserved2,(void)); /**< Reserved for minor structure revisions. */
DECLR3CALLBACKMEMBER(int, pfnReserved3,(void)); /**< Reserved for minor structure revisions. */
DECLR3CALLBACKMEMBER(int, pfnReserved4,(void)); /**< Reserved for minor structure revisions. */
DECLR3CALLBACKMEMBER(int, pfnReserved5,(void)); /**< Reserved for minor structure revisions. */
DECLR3CALLBACKMEMBER(int, pfnReserved6,(void)); /**< Reserved for minor structure revisions. */
/** Reserved for minor structure revisions. */
uint32_t uReserved7;
/** End of structure marker (VBOXCRYPTOIF_VERSION). */
uint32_t u32EndMarker;
} VBOXCRYPTOIF;
/** Current version of the VBOXCRYPTOIF structure. */
#define VBOXCRYPTOIF_VERSION RT_MAKE_U32(0, 1)
/**
* The VBoxCrypto entry callback function.
*
* @returns VBox status code.
* @param ppCryptoIf Where to store the pointer to the crypto module interface callback table
* on success.
*/
typedef DECLCALLBACKTYPE(int, FNVBOXCRYPTOENTRY,(PCVBOXCRYPTOIF *ppCryptoIf));
/** Pointer to a FNVBOXCRYPTOENTRY. */
typedef FNVBOXCRYPTOENTRY *PFNVBOXCRYPTOENTRY;
/** The name of the crypto module entry point. */
#define VBOX_CRYPTO_MOD_ENTRY_POINT "VBoxCryptoEntry"
/**
* Checks if cryptographic interface version is compatible.
*
* @returns true if the do, false if they don't.
* @param u32Provider The provider version.
* @param u32User The user version.
*/
#define VBOXCRYPTO_IS_VER_COMPAT(u32Provider, u32User) \
( VBOXCRYPTO_IS_MAJOR_VER_EQUAL(u32Provider, u32User) \
&& (int32_t)RT_LOWORD(u32Provider) >= (int32_t)RT_LOWORD(u32User) ) /* stupid casts to shut up gcc */
/**
* Check if two cryptographic interface versions have the same major version.
*
* @returns true if the do, false if they don't.
* @param u32Ver1 The first version number.
* @param u32Ver2 The second version number.
*/
#define VBOXCRYPTO_IS_MAJOR_VER_EQUAL(u32Ver1, u32Ver2) (RT_HIWORD(u32Ver1) == RT_HIWORD(u32Ver2))
#endif /* !VBOX_INCLUDED_VBoxCryptoIf_h */
|