summaryrefslogtreecommitdiff
path: root/board/cr50/dcrypto/hash_api.c
blob: 34b5d88d557b03aac76985ee84d40c6e9272446b (plain)
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
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "internal.h"

size_t DCRYPTO_hash_size(enum hashing_mode mode)
{
	if (!fips_crypto_allowed())
		return 0;

	switch (mode) {
	case HASH_SHA1:
		return SHA1_DIGEST_SIZE;
	case HASH_SHA256:
		return SHA256_DIGEST_SIZE;
#ifdef CONFIG_UPTO_SHA512
	case HASH_SHA384:
		return SHA384_DIGEST_SIZE;
	case HASH_SHA512:
		return SHA512_DIGEST_SIZE;
#endif
	default:
		return 0;
	}
	return 0;
}

enum dcrypto_result DCRYPTO_sw_hash_init(union hash_ctx *ctx,
					 enum hashing_mode mode)
{
	if (!fips_crypto_allowed())
		return DCRYPTO_FAIL;

	switch (mode) {
	case HASH_SHA1:
		SHA1_sw_init(&ctx->sha1);
		break;
	case HASH_SHA256:
		SHA256_sw_init(&ctx->sha256);
		break;
#ifdef CONFIG_UPTO_SHA512
	case HASH_SHA384:
		SHA384_sw_init(&ctx->sha384);
		break;
	case HASH_SHA512:
		SHA512_sw_init(&ctx->sha512);
		break;
#endif
	default:
		return DCRYPTO_FAIL;
	}
	return DCRYPTO_OK;
}

enum dcrypto_result DCRYPTO_hw_hash_init(union hash_ctx *ctx,
					 enum hashing_mode mode)
{
	if (!fips_crypto_allowed())
		return DCRYPTO_FAIL;

	switch (mode) {
	case HASH_SHA1:
		SHA1_hw_init(&ctx->sha1);
		break;
	case HASH_SHA256:
		SHA256_hw_init(&ctx->sha256);
		break;
#ifdef CONFIG_UPTO_SHA512
	case HASH_SHA384:
		SHA384_hw_init(&ctx->sha384);
		break;
	case HASH_SHA512:
		SHA512_hw_init(&ctx->sha512);
		break;
#endif
	default:
		return DCRYPTO_FAIL;
	}
	return DCRYPTO_OK;
}

enum dcrypto_result DCRYPTO_sw_hmac_init(union hmac_ctx *ctx, const void *key,
					 size_t len, enum hashing_mode mode)
{
	if (!fips_crypto_allowed())
		return DCRYPTO_FAIL;

	switch (mode) {
	case HASH_SHA1:
		SHA1_sw_init(&ctx->hmac_sha1.hash);
		break;
	case HASH_SHA256:
		SHA256_sw_init(&ctx->hmac_sha256.hash);
		break;
#ifdef CONFIG_UPTO_SHA512
	case HASH_SHA384:
		SHA384_sw_init(&ctx->hmac_sha384.hash);
		break;
	case HASH_SHA512:
		SHA512_sw_init(&ctx->hmac_sha512.hash);
		break;
#endif
	default:
		return DCRYPTO_FAIL;
	}
	HMAC_sw_init(ctx, key, len);
	return DCRYPTO_OK;
}

enum dcrypto_result DCRYPTO_hw_hmac_init(union hmac_ctx *ctx, const void *key,
					 size_t len, enum hashing_mode mode)
{
	if (!fips_crypto_allowed())
		return DCRYPTO_FAIL;

	switch (mode) {
	case HASH_SHA1:
		SHA1_hw_init(&ctx->hmac_sha1.hash);
		HMAC_sw_init(ctx, key, len);
		break;
	case HASH_SHA256:
		HMAC_SHA256_hw_init(&ctx->hmac_sha256, key, len);
		break;
#ifdef CONFIG_UPTO_SHA512
	case HASH_SHA384:
		SHA384_hw_init(&ctx->hmac_sha384.hash);
		HMAC_sw_init(ctx, key, len);
		break;
	case HASH_SHA512:
		SHA512_hw_init(&ctx->hmac_sha512.hash);
		HMAC_sw_init(ctx, key, len);
		break;
#endif
	default:
		return DCRYPTO_FAIL;
	}
	return DCRYPTO_OK;
}

const uint8_t *DCRYPTO_SHA1_hash(const void *data, size_t n, uint8_t *digest)
{
	if (!fips_crypto_allowed())
		return NULL;

	if (is_not_aligned(digest)) {
		struct sha1_digest d;

		SHA1_hw_hash(data, n, &d);
		memcpy(digest, d.b8, sizeof(d));
	} else
		SHA1_hw_hash(data, n, (struct sha1_digest *)digest);
	return digest;
}

const uint8_t *DCRYPTO_SHA256_hash(const void *data, size_t n, uint8_t *digest)
{
	if (!fips_crypto_allowed())
		return NULL;

	if (is_not_aligned(digest)) {
		struct sha256_digest d;

		SHA256_hw_hash(data, n, &d);
		memcpy(digest, d.b8, sizeof(d));
	} else
		SHA256_hw_hash(data, n, (struct sha256_digest *)digest);
	return digest;
}