1 | /* $Id: openssl-sha512.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - SHA-512 hash functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 |
|
---|
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_SHA512_PRIVATE_CONTEXT
|
---|
48 | #include <iprt/sha.h>
|
---|
49 |
|
---|
50 | #include <iprt/assert.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 |
|
---|
53 |
|
---|
54 | AssertCompile(RT_SIZEOFMEMB(RTSHA512CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA512CONTEXT, Private));
|
---|
55 |
|
---|
56 |
|
---|
57 | RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE])
|
---|
58 | {
|
---|
59 | RTSHA512CONTEXT Ctx;
|
---|
60 | RTSha512Init(&Ctx);
|
---|
61 | RTSha512Update(&Ctx, pvBuf, cbBuf);
|
---|
62 | RTSha512Final(&Ctx, pabDigest);
|
---|
63 | }
|
---|
64 | RT_EXPORT_SYMBOL(RTSha512);
|
---|
65 |
|
---|
66 |
|
---|
67 | RTDECL(bool) RTSha512Check(const void *pvBuf, size_t cbBuf, uint8_t const pabDigest[RTSHA512_HASH_SIZE])
|
---|
68 | {
|
---|
69 | RTSHA512CONTEXT Ctx;
|
---|
70 | RTSha512Init(&Ctx);
|
---|
71 | RTSha512Update(&Ctx, pvBuf, cbBuf);
|
---|
72 | uint8_t abActualDigest[RTSHA512_HASH_SIZE];
|
---|
73 | RTSha512Final(&Ctx, abActualDigest);
|
---|
74 | bool fRet = memcmp(pabDigest, abActualDigest, RTSHA512_HASH_SIZE) == 0;
|
---|
75 | RT_ZERO(abActualDigest);
|
---|
76 | return fRet;
|
---|
77 | }
|
---|
78 | RT_EXPORT_SYMBOL(RTSha512Check);
|
---|
79 |
|
---|
80 |
|
---|
81 | RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx)
|
---|
82 | {
|
---|
83 | SHA512_Init(&pCtx->Private);
|
---|
84 | }
|
---|
85 | RT_EXPORT_SYMBOL(RTSha512Init);
|
---|
86 |
|
---|
87 |
|
---|
88 | RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
|
---|
89 | {
|
---|
90 | SHA512_Update(&pCtx->Private, pvBuf, cbBuf);
|
---|
91 | }
|
---|
92 | RT_EXPORT_SYMBOL(RTSha512Update);
|
---|
93 |
|
---|
94 |
|
---|
95 | RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[32])
|
---|
96 | {
|
---|
97 | SHA512_Final((unsigned char *)&pabDigest[0], &pCtx->Private);
|
---|
98 | }
|
---|
99 | RT_EXPORT_SYMBOL(RTSha512Final);
|
---|
100 |
|
---|
101 |
|
---|
102 | /*
|
---|
103 | * We have to expose the same API as alt-sha512.cpp, so the SHA-384,
|
---|
104 | * SHA-512/224 and SHA-512/256 implementations also live here. (They are all
|
---|
105 | * just truncted SHA-512 with different initial values.)
|
---|
106 | */
|
---|
107 |
|
---|
108 | RTDECL(void) RTSha384(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA384_HASH_SIZE])
|
---|
109 | {
|
---|
110 | RTSHA384CONTEXT Ctx;
|
---|
111 | RTSha384Init(&Ctx);
|
---|
112 | RTSha384Update(&Ctx, pvBuf, cbBuf);
|
---|
113 | RTSha384Final(&Ctx, pabDigest);
|
---|
114 | }
|
---|
115 | RT_EXPORT_SYMBOL(RTSha384);
|
---|
116 |
|
---|
117 |
|
---|
118 | RTDECL(bool) RTSha384Check(const void *pvBuf, size_t cbBuf, uint8_t const pabDigest[RTSHA384_HASH_SIZE])
|
---|
119 | {
|
---|
120 | RTSHA384CONTEXT Ctx;
|
---|
121 | RTSha384Init(&Ctx);
|
---|
122 | RTSha384Update(&Ctx, pvBuf, cbBuf);
|
---|
123 | uint8_t abActualDigest[RTSHA384_HASH_SIZE];
|
---|
124 | RTSha384Final(&Ctx, abActualDigest);
|
---|
125 | bool fRet = memcmp(pabDigest, abActualDigest, RTSHA384_HASH_SIZE) == 0;
|
---|
126 | RT_ZERO(abActualDigest);
|
---|
127 | return fRet;
|
---|
128 | }
|
---|
129 | RT_EXPORT_SYMBOL(RTSha384Check);
|
---|
130 |
|
---|
131 |
|
---|
132 | RTDECL(void) RTSha384Init(PRTSHA384CONTEXT pCtx)
|
---|
133 | {
|
---|
134 | SHA384_Init(&pCtx->Private);
|
---|
135 | }
|
---|
136 | RT_EXPORT_SYMBOL(RTSha384Init);
|
---|
137 |
|
---|
138 |
|
---|
139 | RTDECL(void) RTSha384Update(PRTSHA384CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
|
---|
140 | {
|
---|
141 | SHA384_Update(&pCtx->Private, pvBuf, cbBuf);
|
---|
142 | }
|
---|
143 | RT_EXPORT_SYMBOL(RTSha384Update);
|
---|
144 |
|
---|
145 |
|
---|
146 | RTDECL(void) RTSha384Final(PRTSHA384CONTEXT pCtx, uint8_t pabDigest[32])
|
---|
147 | {
|
---|
148 | SHA384_Final((unsigned char *)&pabDigest[0], &pCtx->Private);
|
---|
149 | }
|
---|
150 | RT_EXPORT_SYMBOL(RTSha384Final);
|
---|
151 |
|
---|