forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.c
More file actions
199 lines (154 loc) · 4.32 KB
/
Copy pathhash.c
File metadata and controls
199 lines (154 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// SPDX-License-Identifier: BSD-3-Clause
/*
* Copyright(c) 2017 Intel Corporation. All rights reserved.
*
* Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
* Keyon Jie <yang.jie@linux.intel.com>
* Adrian Warecki <adrian.warecki@intel.com>
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <rimage/rimage.h>
#include <rimage/manifest.h>
#include <rimage/hash.h>
#define DEBUG_HASH 0
#if OPENSSL_VERSION_NUMBER < 0x10100000L
void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
EVP_MD_CTX *EVP_MD_CTX_new(void);
static void *OPENSSL_zalloc(size_t num)
{
void *ret = OPENSSL_malloc(num);
if (ret)
memset(ret, 0, num);
return ret;
}
EVP_MD_CTX *EVP_MD_CTX_new(void)
{
return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
}
void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
{
EVP_MD_CTX_cleanup(ctx);
OPENSSL_free(ctx);
}
#endif
static int hash_error(struct hash_context *context, int errcode, const char *msg)
{
EVP_MD_CTX_free(context->context);
context->context = NULL;
context->state = HS_ERROR;
context->error = -errcode;
fprintf(stderr, "hash: %s\n", msg);
return context->error;
}
int hash_init(struct hash_context *context, const EVP_MD *algo)
{
assert(context);
assert(algo);
context->error = 0;
context->digest_length = 0;
context->algo = algo;
context->context = EVP_MD_CTX_new();
if (!context->context)
return hash_error(context, ENOMEM, "Unable to allocate hash context.");
if (!EVP_DigestInit_ex(context->context, context->algo, NULL)) {
EVP_MD_CTX_free(context->context);
return hash_error(context, ENOTRECOVERABLE, "Unable to initialize hash context.");
}
context->state = HS_UPDATE;
return 0;
}
int hash_sha256_init(struct hash_context *context)
{
return hash_init(context, EVP_sha256());
}
int hash_sha384_init(struct hash_context *context)
{
return hash_init(context, EVP_sha384());
}
int hash_update(struct hash_context *context, const void *data, size_t size)
{
assert(context);
assert(data);
if (context->error)
return context->error;
assert(context->state == HS_UPDATE);
if (!EVP_DigestUpdate(context->context, data, size))
return hash_error(context, EINVAL, "Unable to update hash context.");
return 0;
}
int hash_finalize(struct hash_context *context)
{
assert(context);
if (context->error)
return context->error;
assert(context->state == HS_UPDATE);
if (!EVP_DigestFinal_ex(context->context, context->digest, &context->digest_length))
return hash_error(context, EINVAL, "Unable to finalize hash context.");
context->state = HS_DONE;
#if DEBUG_HASH
fprintf(stdout, "Hash result is: ");
hash_print(context);
#endif
EVP_MD_CTX_free(context->context);
context->context = NULL;
return 0;
}
int hash_get_digest(struct hash_context *context, void *output, size_t output_len)
{
assert(context);
assert(output);
if (context->error)
return context->error;
assert(context->state == HS_DONE);
if (context->digest_length > output_len)
return -ENOBUFS;
memcpy(output, context->digest, context->digest_length);
return context->digest_length;
}
void hash_print(struct hash_context *context)
{
unsigned int i;
assert(context);
assert(context->state == HS_DONE);
assert(context->digest_length);
for (i = 0; i < context->digest_length; i++)
fprintf(stdout, "%02x", context->digest[i]);
fprintf(stdout, "\n");
}
int hash_single(const void *data, size_t size, const EVP_MD *algo, void *output, size_t output_len)
{
int algo_out_size;
assert(algo);
assert(data);
assert(output);
//algo_out_size = EVP_MD_get_size(algo);
algo_out_size = EVP_MD_size(algo);
if (algo_out_size <= 0)
return -EINVAL;
/* EVP_Digest writes algo_out_size bytes into output, so the buffer
* must be at least that large; reject an undersized output buffer
*/
if (output_len < (size_t)algo_out_size)
return -ENOBUFS;
if (!EVP_Digest(data, size, output, NULL, algo, NULL)) {
fprintf(stderr, "Unable to compute hash.");
return -ENOTRECOVERABLE;
}
return 0;
}
int hash_sha256(const void *data, size_t size, void *output, size_t output_len)
{
return hash_single(data, size, EVP_sha256(), output, output_len);
}
int hash_sha384(const void *data, size_t size, void *output, size_t output_len)
{
return hash_single(data, size, EVP_sha384(), output, output_len);
}