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