VirtualBox

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

Last change on this file since 87203 was 85623, checked in by vboxsync, 4 years ago

IPRT: SHA-3 nits. bugref:9734

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