VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/openssl-sha3.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 KB
Line 
1/* $Id: openssl-sha3.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - SHA-3 hash functions, OpenSSL based implementation.
4 */
5
6/*
7 * Copyright (C) 2020-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#if 1 /* For now: */
38# include "alt-sha3.cpp"
39
40#else
41
42
43/*********************************************************************************************************************************
44* Header Files *
45*********************************************************************************************************************************/
46#include "internal/iprt.h"
47#include <iprt/assert.h>
48#include <iprt/err.h>
49#include <iprt/string.h>
50
51#include "internal/openssl-pre.h"
52#include <openssl/evp.h>
53#include "internal/openssl-post.h"
54
55
56/*********************************************************************************************************************************
57* Defined Constants And Macros *
58*********************************************************************************************************************************/
59#define RTSHA3PRIVATECTX_MAGIC UINT64_C(0xb6362d323c56b758)
60#define RTSHA3PRIVATECTX_MAGIC_FINAL UINT64_C(0x40890fe0e474215d)
61#define RTSHA3PRIVATECTX_MAGIC_DEAD UINT64_C(0xdead7a05081cbeef)
62
63
64/*********************************************************************************************************************************
65* Structures and Typedefs *
66*********************************************************************************************************************************/
67/* Internal EVP structure that we fake here to avoid lots of casting. */
68struct evp_md_ctx_st
69{
70 void *apvWhatever[10];
71};
72
73/** The OpenSSL private context structure. */
74typedef struct RTSHA3PRIVATECTX
75{
76 /** RTSHA3PRIVATECTX_MAGIC / RTSHA3PRIVATECTX_MAGIC_FINAL / RTSHA3PRIVATECTX_MAGIC_DEAD */
77 uint64_t u64Magic;
78 /** The OpenSSL context. We cheat to avoid EVP_MD_CTX_new/free. */
79 struct evp_md_ctx_st MdCtx;
80} RTSHA3PRIVATECTX;
81
82#define RT_SHA3_PRIVATE_CONTEXT
83#include <iprt/sha.h>
84AssertCompile(RT_SIZEOFMEMB(RTSHA3CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA3CONTEXT, Private));
85
86
87
88static int rtSha3Init(PRTSHA3CONTEXT pCtx, const EVP_MD *pMdType)
89{
90 RT_ZERO(*pCtx); /* This is what EVP_MD_CTX_new does. */
91 pCtx->Private.u64Magic = RTSHA3PRIVATECTX_MAGIC;
92
93 AssertReturnStmt(EVP_DigestInit_ex(&pCtx->Private.MdCtx, pMdType, NULL /*engine*/),
94 pCtx->Private.u64Magic = RTSHA3PRIVATECTX_MAGIC_DEAD,
95 VERR_CR_DIGEST_OSSL_DIGEST_INIT_ERROR);
96 return VINF_SUCCESS;
97}
98
99
100static int rtSha3Update(PRTSHA3CONTEXT pCtx, uint8_t const *pbData, size_t cbData)
101{
102 AssertMsgReturn(pCtx->Private.u64Magic == RTSHA3PRIVATECTX_MAGIC, ("u64Magic=%RX64\n", pCtx->Private.u64Magic),
103 VERR_INVALID_CONTEXT);
104 AssertReturn(EVP_DigestUpdate(&pCtx->Private.MdCtx, pbData, cbData), VERR_GENERAL_FAILURE);
105 return VINF_SUCCESS;
106}
107
108
109static int rtSha3Final(PRTSHA3CONTEXT pCtx, uint8_t *pbDigest, size_t cbDigest)
110{
111 RT_BZERO(pbDigest, cbDigest);
112 AssertMsgReturn(pCtx->Private.u64Magic == RTSHA3PRIVATECTX_MAGIC, ("u64Magic=%RX64\n", pCtx->Private.u64Magic),
113 VERR_INVALID_CONTEXT);
114 AssertReturn(EVP_DigestFinal_ex(&pCtx->Private.MdCtx, pbDigest, NULL), VERR_GENERAL_FAILURE);
115
116 /* Implicit cleanup. */
117 EVP_MD_CTX_reset(&pCtx->Private.MdCtx);
118 pCtx->Private.u64Magic = RTSHA3PRIVATECTX_MAGIC_FINAL;
119 return VINF_SUCCESS;
120}
121
122
123static int rtSha3Cleanup(PRTSHA3CONTEXT pCtx)
124{
125 if (pCtx)
126 {
127 if (pCtx->Private.u64Magic == RTSHA3PRIVATECTX_MAGIC_FINAL)
128 { /* likely */ }
129 else if (pCtx->Private.u64Magic == RTSHA3PRIVATECTX_MAGIC)
130 EVP_MD_CTX_reset(&pCtx->Private.MdCtx);
131 else
132 AssertMsgFailedReturn(("u64Magic=%RX64\n", pCtx->Private.u64Magic), VERR_INVALID_CONTEXT);
133 pCtx->Private.u64Magic = RTSHA3PRIVATECTX_MAGIC_DEAD;
134 }
135 return VINF_SUCCESS;
136}
137
138
139static int rtSha3Clone(PRTSHA3CONTEXT pCtx, RTSHA3CONTEXT const *pCtxSrc)
140{
141 Assert(pCtx->Private.u64Magic != RTSHA3PRIVATECTX_MAGIC);
142 RT_ZERO(*pCtx); /* This is what EVP_MD_CTX_new does. */
143
144 AssertReturn(pCtxSrc->Private.u64Magic == RTSHA3PRIVATECTX_MAGIC, VERR_INVALID_CONTEXT);
145
146 pCtx->Private.u64Magic = RTSHA3PRIVATECTX_MAGIC;
147 AssertReturnStmt(EVP_MD_CTX_copy_ex(&pCtx->Private.MdCtx, &pCtxSrc->Private.MdCtx),
148 pCtx->Private.u64Magic = RTSHA3PRIVATECTX_MAGIC_DEAD,
149 VERR_CR_DIGEST_OSSL_DIGEST_CTX_COPY_ERROR);
150 return VINF_SUCCESS;
151}
152
153
154static int rtSha3(const void *pvData, size_t cbData, const EVP_MD *pMdType, uint8_t *pabHash, size_t cbHash)
155{
156 RT_BZERO(pabHash, cbHash);
157
158 int rc;
159 EVP_MD_CTX *pCtx = EVP_MD_CTX_new();
160 if (pCtx)
161 {
162 if (EVP_DigestInit_ex(pCtx, pMdType, NULL /*engine*/))
163 {
164 if (EVP_DigestUpdate(pCtx, pvData, cbData))
165 {
166 if (EVP_DigestFinal_ex(pCtx, pabHash, NULL))
167 rc = VINF_SUCCESS;
168 else
169 AssertFailedStmt(rc = VERR_GENERAL_FAILURE);
170 }
171 else
172 AssertFailedStmt(rc = VERR_GENERAL_FAILURE);
173 }
174 else
175 AssertFailedStmt(rc = VERR_CR_DIGEST_OSSL_DIGEST_INIT_ERROR);
176 EVP_MD_CTX_free(pCtx);
177 }
178 else
179 AssertFailedStmt(rc = VERR_NO_MEMORY);
180 return rc;
181}
182
183
184static bool rtSha3Check(const void *pvData, size_t cbData, const EVP_MD *pMdType,
185 const uint8_t *pabHash, uint8_t *pabHashTmp, size_t cbHash)
186{
187 int rc = rtSha3(pvData, cbData, pMdType, pabHashTmp, cbHash);
188 return RT_SUCCESS(rc) && memcmp(pabHash, pabHashTmp, cbHash) == 0;
189}
190
191
192/** Macro for declaring the interface for a SHA3 variation.
193 * @internal */
194#define RTSHA3_DEFINE_VARIANT(a_cBits, a_pMdType) \
195AssertCompile((a_cBits / 8) == RT_CONCAT3(RTSHA3_,a_cBits,_HASH_SIZE)); \
196\
197RTDECL(int) RT_CONCAT(RTSha3t,a_cBits)(const void *pvBuf, size_t cbBuf, uint8_t pabHash[RT_CONCAT3(RTSHA3_,a_cBits,_HASH_SIZE)]) \
198{ \
199 return rtSha3(pvBuf, cbBuf, a_pMdType, pabHash, (a_cBits) / 8); \
200} \
201RT_EXPORT_SYMBOL(RT_CONCAT(RTSha3t,a_cBits)); \
202\
203\
204RTDECL(bool) RT_CONCAT3(RTSha3t,a_cBits,Check)(const void *pvBuf, size_t cbBuf, \
205 uint8_t const pabHash[RT_CONCAT3(RTSHA3_,a_cBits,_HASH_SIZE)]) \
206{ \
207 uint8_t abHashTmp[(a_cBits) / 8]; \
208 return rtSha3Check(pvBuf, cbBuf, a_pMdType, pabHash, abHashTmp, (a_cBits) / 8); \
209} \
210RT_EXPORT_SYMBOL(RT_CONCAT3(RTSha3t,a_cBits,Check)); \
211\
212\
213RTDECL(int) RT_CONCAT3(RTSha3t,a_cBits,Init)(RT_CONCAT3(PRTSHA3T,a_cBits,CONTEXT) pCtx) \
214{ \
215 return rtSha3Init(&pCtx->Sha3, a_pMdType); \
216} \
217RT_EXPORT_SYMBOL(RT_CONCAT3(RTSha3t,a_cBits,Init)); \
218\
219\
220RTDECL(int) RT_CONCAT3(RTSha3t,a_cBits,Update)(RT_CONCAT3(PRTSHA3T,a_cBits,CONTEXT) pCtx, const void *pvBuf, size_t cbBuf) \
221{ \
222 return rtSha3Update(&pCtx->Sha3, (uint8_t const *)pvBuf, cbBuf); \
223} \
224RT_EXPORT_SYMBOL(RT_CONCAT3(RTSha3t,a_cBits,Update)); \
225\
226\
227RTDECL(int) RT_CONCAT3(RTSha3t,a_cBits,Final)(RT_CONCAT3(PRTSHA3T,a_cBits,CONTEXT) pCtx, \
228 uint8_t pabHash[RT_CONCAT3(RTSHA3_,a_cBits,_HASH_SIZE)]) \
229{ \
230 return rtSha3Final(&pCtx->Sha3, pabHash, (a_cBits) / 8); \
231} \
232RT_EXPORT_SYMBOL(RT_CONCAT3(RTSha3t,a_cBits,Final)); \
233\
234\
235RTDECL(int) RT_CONCAT3(RTSha3t,a_cBits,Cleanup)(RT_CONCAT3(PRTSHA3T,a_cBits,CONTEXT) pCtx) \
236{ \
237 return rtSha3Cleanup(&pCtx->Sha3); \
238} \
239RT_EXPORT_SYMBOL(RT_CONCAT3(RTSha3t,a_cBits,Cleanup)); \
240\
241\
242RTDECL(int) RT_CONCAT3(RTSha3t,a_cBits,Clone)(RT_CONCAT3(PRTSHA3T,a_cBits,CONTEXT) pCtx, \
243 RT_CONCAT3(RTSHA3T,a_cBits,CONTEXT) const *pCtxSrc) \
244{ \
245 return rtSha3Clone(&pCtx->Sha3, &pCtxSrc->Sha3); \
246} \
247RT_EXPORT_SYMBOL(RT_CONCAT3(RTSha3t,a_cBits,Clone)); \
248\
249\
250RTDECL(int) RT_CONCAT3(RTSha3t,a_cBits,ToString)(uint8_t const pabHash[RT_CONCAT3(RTSHA3_,a_cBits,_HASH_SIZE)], \
251 char *pszDigest, size_t cchDigest) \
252{ \
253 return RTStrPrintHexBytes(pszDigest, cchDigest, pabHash, (a_cBits) / 8, 0 /*fFlags*/); \
254} \
255RT_EXPORT_SYMBOL(RT_CONCAT3(RTSha3t,a_cBits,ToString)); \
256\
257\
258RTDECL(int) RT_CONCAT3(RTSha3t,a_cBits,FromString)(char const *pszDigest, uint8_t pabHash[RT_CONCAT3(RTSHA3_,a_cBits,_HASH_SIZE)]) \
259{ \
260 return RTStrConvertHexBytes(RTStrStripL(pszDigest), &pabHash[0], (a_cBits) / 8, 0 /*fFlags*/); \
261} \
262RT_EXPORT_SYMBOL(RT_CONCAT3(RTSha3t,a_cBits,FromString))
263
264
265RTSHA3_DEFINE_VARIANT(224, EVP_sha3_224());
266RTSHA3_DEFINE_VARIANT(256, EVP_sha3_256());
267RTSHA3_DEFINE_VARIANT(384, EVP_sha3_384());
268RTSHA3_DEFINE_VARIANT(512, EVP_sha3_512());
269
270#endif /* !alt-sha3.cpp */
Note: See TracBrowser for help on using the repository browser.

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