VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/openssl-sha256.cpp@ 106580

Last change on this file since 106580 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1/* $Id: openssl-sha256.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - SHA-256 hash functions.
4 */
5
6/*
7 * Copyright (C) 2009-2024 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
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42
43#include "internal/openssl-pre.h"
44#include <openssl/sha.h>
45#include "internal/openssl-post.h"
46
47#define RT_SHA256_PRIVATE_CONTEXT
48#include <iprt/sha.h>
49
50#include <iprt/assert.h>
51#include <iprt/string.h>
52
53
54AssertCompile(RT_SIZEOFMEMB(RTSHA256CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA256CONTEXT, Private));
55
56
57RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE])
58{
59 RTSHA256CONTEXT Ctx;
60 RTSha256Init(&Ctx);
61 RTSha256Update(&Ctx, pvBuf, cbBuf);
62 RTSha256Final(&Ctx, pabDigest);
63}
64RT_EXPORT_SYMBOL(RTSha256);
65
66
67RTDECL(bool) RTSha256Check(const void *pvBuf, size_t cbBuf, uint8_t const pabDigest[RTSHA256_HASH_SIZE])
68{
69 RTSHA256CONTEXT Ctx;
70 RTSha256Init(&Ctx);
71 RTSha256Update(&Ctx, pvBuf, cbBuf);
72 uint8_t abActualDigest[RTSHA256_HASH_SIZE];
73 RTSha256Final(&Ctx, abActualDigest);
74 bool fRet = memcmp(pabDigest, abActualDigest, RTSHA256_HASH_SIZE) == 0;
75 RT_ZERO(abActualDigest);
76 return fRet;
77}
78RT_EXPORT_SYMBOL(RTSha256Check);
79
80
81RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx)
82{
83 SHA256_Init(&pCtx->Private);
84}
85RT_EXPORT_SYMBOL(RTSha256Init);
86
87
88RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
89{
90 SHA256_Update(&pCtx->Private, pvBuf, cbBuf);
91}
92RT_EXPORT_SYMBOL(RTSha256Update);
93
94
95RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE])
96{
97 SHA256_Final((unsigned char *)&pabDigest[0], &pCtx->Private);
98}
99RT_EXPORT_SYMBOL(RTSha256Final);
100
101
102/*
103 * We have to expose the same API as alt-sha256.cpp, so the SHA-224
104 * implementation also lives here. (SHA-224 is just a truncated SHA-256 with
105 * different initial values.)
106 */
107RTDECL(void) RTSha224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA224_HASH_SIZE])
108{
109 RTSHA224CONTEXT Ctx;
110 RTSha224Init(&Ctx);
111 RTSha224Update(&Ctx, pvBuf, cbBuf);
112 RTSha224Final(&Ctx, pabDigest);
113}
114RT_EXPORT_SYMBOL(RTSha224);
115
116
117RTDECL(bool) RTSha224Check(const void *pvBuf, size_t cbBuf, uint8_t const pabDigest[RTSHA224_HASH_SIZE])
118{
119 RTSHA224CONTEXT Ctx;
120 RTSha224Init(&Ctx);
121 RTSha224Update(&Ctx, pvBuf, cbBuf);
122 uint8_t abActualDigest[RTSHA224_HASH_SIZE];
123 RTSha224Final(&Ctx, abActualDigest);
124 bool fRet = memcmp(pabDigest, abActualDigest, RTSHA224_HASH_SIZE) == 0;
125 RT_ZERO(abActualDigest);
126 return fRet;
127}
128RT_EXPORT_SYMBOL(RTSha224Check);
129
130
131RTDECL(void) RTSha224Init(PRTSHA224CONTEXT pCtx)
132{
133 SHA224_Init(&pCtx->Private);
134}
135RT_EXPORT_SYMBOL(RTSha224Init);
136
137
138RTDECL(void) RTSha224Update(PRTSHA224CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
139{
140 SHA224_Update(&pCtx->Private, pvBuf, cbBuf);
141}
142RT_EXPORT_SYMBOL(RTSha224Update);
143
144
145RTDECL(void) RTSha224Final(PRTSHA224CONTEXT pCtx, uint8_t pabDigest[RTSHA224_HASH_SIZE])
146{
147 SHA224_Final((unsigned char *)&pabDigest[0], &pCtx->Private);
148}
149RT_EXPORT_SYMBOL(RTSha224Final);
150
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