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