aboutsummaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorPaul Selkirk <paul@psgd.org>2016-07-05 22:45:35 -0400
committerPaul Selkirk <paul@psgd.org>2016-07-05 22:45:35 -0400
commit30f8e4e85b6a337291b09d55d8edc15e422b6341 (patch)
tree19199dd47bb98e18a96281d34e35d1971565fc72 /hash.c
parente1c57eff41a57b8a3f16e5d652b5598d75887a21 (diff)
Attempt to add resource management, for multiple cores of the same type.
Find a suitable core, and mark it busy. Don't forget to release it as soon as you're done. This has a knock-on effect of un-const'ing core arguments and struct fields in a lot of places, and it moves some core checks around.
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c92
1 files changed, 33 insertions, 59 deletions
diff --git a/hash.c b/hash.c
index 5fface0..5709a6a 100644
--- a/hash.c
+++ b/hash.c
@@ -100,11 +100,11 @@ struct hal_hash_driver {
*/
struct hal_hash_state {
- const hal_core_t *core;
+ hal_core_t *core;
const hal_hash_descriptor_t *descriptor;
const hal_hash_driver_t *driver;
- uint64_t msg_length_high; /* Total data hashed in this message */
- uint64_t msg_length_low; /* (128 bits in SHA-512 cases) */
+ long long unsigned msg_length_high; /* Total data hashed in this message */
+ long long unsigned msg_length_low; /* (128 bits in SHA-512 cases) */
uint8_t block[HAL_MAX_HASH_BLOCK_LENGTH], /* Block we're accumulating */
core_state[HAL_MAX_HASH_DIGEST_LENGTH]; /* Saved core state */
size_t block_used; /* How much of the block we've used */
@@ -362,42 +362,10 @@ static inline const hal_hash_driver_t *check_driver(const hal_hash_descriptor_t
}
/*
- * Internal utility to check core against descriptor, including
- * attempting to locate an appropriate core if we weren't given one.
- */
-
-static inline hal_error_t check_core(const hal_core_t **core,
- const hal_hash_descriptor_t * const descriptor,
- unsigned *flags)
-{
- assert(descriptor != NULL && descriptor->driver != NULL);
-
-#if RPC_CLIENT == RPC_CLIENT_MIXED
- hal_error_t err = HAL_ERROR_CORE_NOT_FOUND;
-#else
- hal_error_t err = hal_core_check_name(core, descriptor->core_name);
-#endif
-
-#if HAL_ENABLE_SOFTWARE_HASH_CORES
-
- if (err == HAL_ERROR_CORE_NOT_FOUND && descriptor->driver->sw_core) {
-
- if (flags != NULL)
- *flags |= STATE_FLAG_SOFTWARE_CORE;
-
- err = HAL_OK;
- }
-
-#endif /* HAL_ENABLE_SOFTWARE_HASH_CORES */
-
- return err;
-}
-
-/*
* Initialize hash state.
*/
-hal_error_t hal_hash_initialize(const hal_core_t *core,
+hal_error_t hal_hash_initialize(hal_core_t *core,
const hal_hash_descriptor_t * const descriptor,
hal_hash_state_t **state_,
void *state_buffer, const size_t state_length)
@@ -405,7 +373,6 @@ hal_error_t hal_hash_initialize(const hal_core_t *core,
const hal_hash_driver_t * const driver = check_driver(descriptor);
hal_hash_state_t *state = state_buffer;
unsigned flags = 0;
- hal_error_t err;
if (driver == NULL || state_ == NULL)
return HAL_ERROR_BAD_ARGUMENTS;
@@ -413,9 +380,6 @@ hal_error_t hal_hash_initialize(const hal_core_t *core,
if (state_buffer != NULL && state_length < descriptor->hash_state_length)
return HAL_ERROR_BAD_ARGUMENTS;
- if ((err = check_core(&core, descriptor, &flags)) != HAL_OK)
- return err;
-
if (state_buffer == NULL && (state = alloc_static_hash_state()) == NULL)
return HAL_ERROR_ALLOCATION_FAILURE;
@@ -515,9 +479,6 @@ static hal_error_t hash_write_block(hal_hash_state_t * const state)
uint8_t ctrl_cmd[4];
hal_error_t err;
- if (HAL_ENABLE_SOFTWARE_HASH_CORES && (state->flags & STATE_FLAG_SOFTWARE_CORE) != 0)
- return state->driver->sw_core(state);
-
if ((err = hal_io_wait_ready(state->core)) != HAL_OK)
return err;
@@ -556,7 +517,7 @@ hal_error_t hal_hash_update(hal_hash_state_t *state, /* Opaque state
size_t data_buffer_length) /* Length of data_buffer */
{
const uint8_t *p = data_buffer;
- hal_error_t err;
+ hal_error_t err = HAL_OK;
size_t n;
if (state == NULL || data_buffer == NULL)
@@ -568,6 +529,11 @@ hal_error_t hal_hash_update(hal_hash_state_t *state, /* Opaque state
assert(state->descriptor != NULL && state->driver != NULL);
assert(state->descriptor->block_length <= sizeof(state->block));
+#if RPC_CLIENT != RPC_CLIENT_MIXED
+ if ((err = hal_core_alloc(state->descriptor->core_name, &state->core)) != HAL_OK)
+ return err;
+#endif
+
while ((n = state->descriptor->block_length - state->block_used) <= data_buffer_length) {
/*
* We have enough data for another complete block.
@@ -582,7 +548,7 @@ hal_error_t hal_hash_update(hal_hash_state_t *state, /* Opaque state
data_buffer_length -= n;
p += n;
if ((err = hash_write_block(state)) != HAL_OK)
- return err;
+ goto out;
state->block_count++;
}
@@ -600,7 +566,11 @@ hal_error_t hal_hash_update(hal_hash_state_t *state, /* Opaque state
state->block_used += data_buffer_length;
}
- return HAL_OK;
+out:
+#if RPC_CLIENT != RPC_CLIENT_MIXED
+ hal_core_free(state->core);
+#endif
+ return err;
}
/*
@@ -627,6 +597,11 @@ hal_error_t hal_hash_finalize(hal_hash_state_t *state, /* Opaqu
assert(state->descriptor->block_length <= sizeof(state->block));
+#if RPC_CLIENT != RPC_CLIENT_MIXED
+ if ((err = hal_core_alloc(NULL, &state->core)) != HAL_OK)
+ return err;
+#endif
+
/*
* Add padding, then pull result from the core
*/
@@ -646,7 +621,7 @@ hal_error_t hal_hash_finalize(hal_hash_state_t *state, /* Opaqu
if (n > 0)
memset(state->block + state->block_used, 0, n);
if ((err = hash_write_block(state)) != HAL_OK)
- return err;
+ goto out;
state->block_count++;
state->block_used = 0;
}
@@ -671,25 +646,27 @@ hal_error_t hal_hash_finalize(hal_hash_state_t *state, /* Opaqu
/* Push final block */
if ((err = hash_write_block(state)) != HAL_OK)
- return err;
+ goto out;
state->block_count++;
/* All data pushed to core, now we just need to read back the result */
- if (HAL_ENABLE_SOFTWARE_HASH_CORES && (state->flags & STATE_FLAG_SOFTWARE_CORE) != 0)
- swytebop(digest_buffer, state->core_state, state->descriptor->digest_length, state->driver->sw_word_size);
-#if RPC_CLIENT != RPC_CLIENT_MIXED
- else if ((err = hash_read_digest(state->core, state->driver, digest_buffer, state->descriptor->digest_length)) != HAL_OK)
+#if RPC_CLIENT == RPC_CLIENT_MIXED
+ swytebop(digest_buffer, state->core_state, state->descriptor->digest_length, state->driver->sw_word_size);
+out:
+ return err;
+#else
+ err = hash_read_digest(state->core, state->driver, digest_buffer, state->descriptor->digest_length);
+out:
+ hal_core_free(state->core);
+ return err;
#endif
- return err;
-
- return HAL_OK;
}
/*
* Initialize HMAC state.
*/
-hal_error_t hal_hmac_initialize(const hal_core_t *core,
+hal_error_t hal_hmac_initialize(hal_core_t *core,
const hal_hash_descriptor_t * const descriptor,
hal_hmac_state_t **state_,
void *state_buffer, const size_t state_length,
@@ -706,9 +683,6 @@ hal_error_t hal_hmac_initialize(const hal_core_t *core,
if (state_buffer != NULL && state_length < descriptor->hmac_state_length)
return HAL_ERROR_BAD_ARGUMENTS;
- if ((err = check_core(&core, descriptor, NULL)) != HAL_OK)
- return err;
-
if (state_buffer == NULL && (state = alloc_static_hmac_state()) == NULL)
return HAL_ERROR_ALLOCATION_FAILURE;
>
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223