aboutsummaryrefslogtreecommitdiff
path: root/rpc_misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'rpc_misc.c')
-rw-r--r--rpc_misc.c59
1 files changed, 31 insertions, 28 deletions
diff --git a/rpc_misc.c b/rpc_misc.c
index e9ff4c6..6e64af2 100644
--- a/rpc_misc.c
+++ b/rpc_misc.c
@@ -33,8 +33,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include <assert.h>
-
#include "hal.h"
#include "hal_internal.h"
@@ -46,7 +44,8 @@ static hal_error_t get_version(uint32_t *version)
static hal_error_t get_random(void *buffer, const size_t length)
{
- assert(buffer != NULL && length > 0);
+ if (buffer == NULL || length == 0)
+ return HAL_ERROR_IMPOSSIBLE;
return hal_get_random(NULL, buffer, length);
}
@@ -78,17 +77,25 @@ typedef struct {
} client_slot_t;
#ifndef HAL_PIN_MINIMUM_ITERATIONS
-#define HAL_PIN_MINIMUM_ITERATIONS 10000
+#define HAL_PIN_MINIMUM_ITERATIONS 1000
#endif
#ifndef HAL_PIN_DEFAULT_ITERATIONS
-#define HAL_PIN_DEFAULT_ITERATIONS 20000
+#define HAL_PIN_DEFAULT_ITERATIONS 2000
#endif
static uint32_t hal_pin_default_iterations = HAL_PIN_DEFAULT_ITERATIONS;
+/*
+ * Seconds to delay when given a bad PIN.
+ */
+
+#ifndef HAL_PIN_DELAY_ON_FAILURE
+#define HAL_PIN_DELAY_ON_FAILURE 5
+#endif
+
#ifndef HAL_STATIC_CLIENT_STATE_BLOCKS
-#define HAL_STATIC_CLIENT_STATE_BLOCKS 10
+#define HAL_STATIC_CLIENT_STATE_BLOCKS 10
#endif
#if HAL_STATIC_CLIENT_STATE_BLOCKS > 0
@@ -101,8 +108,8 @@ static client_slot_t client_handle[HAL_STATIC_CLIENT_STATE_BLOCKS];
* them. HAL_USER_NONE indicates an empty slot in the table.
*/
-static inline client_slot_t *alloc_slot(const hal_client_handle_t client,
- const hal_user_t user)
+static inline hal_error_t alloc_slot(const hal_client_handle_t client,
+ const hal_user_t user)
{
client_slot_t *slot = NULL;
hal_critical_section_start();
@@ -126,7 +133,7 @@ static inline client_slot_t *alloc_slot(const hal_client_handle_t client,
}
hal_critical_section_end();
- return slot;
+ return slot == NULL ? HAL_ERROR_NO_CLIENT_SLOTS_AVAILABLE : HAL_OK;
}
static inline hal_error_t clear_slot(client_slot_t *slot)
@@ -167,8 +174,8 @@ static hal_error_t login(const hal_client_handle_t client,
const hal_user_t user,
const char * const pin, const size_t pin_len)
{
- assert(pin != NULL && pin_len != 0);
- assert(user == HAL_USER_NORMAL || user == HAL_USER_SO || user == HAL_USER_WHEEL);
+ if (pin == NULL || pin_len == 0 || (user != HAL_USER_NORMAL && user != HAL_USER_SO && user != HAL_USER_WHEEL))
+ return HAL_ERROR_IMPOSSIBLE;
const hal_ks_pin_t *p;
hal_error_t err;
@@ -187,19 +194,19 @@ static hal_error_t login(const hal_client_handle_t client,
for (int i = 0; i < sizeof(buf); i++)
diff |= buf[i] ^ p->pin[i];
- if (diff != 0)
+ if (diff != 0) {
+ hal_sleep(HAL_PIN_DELAY_ON_FAILURE);
return HAL_ERROR_PIN_INCORRECT;
+ }
- if (alloc_slot(client, user) == NULL)
- return HAL_ERROR_NO_CLIENT_SLOTS_AVAILABLE;
-
- return HAL_OK;
+ return alloc_slot(client, user);
}
static hal_error_t is_logged_in(const hal_client_handle_t client,
const hal_user_t user)
{
- assert(user == HAL_USER_NORMAL || user == HAL_USER_SO || user == HAL_USER_WHEEL);
+ if (user != HAL_USER_NORMAL && user != HAL_USER_SO && user != HAL_USER_WHEEL)
+ return HAL_ERROR_IMPOSSIBLE;
client_slot_t *slot = find_handle(client);
@@ -216,34 +223,29 @@ static hal_error_t logout(const hal_client_handle_t client)
static hal_error_t logout_all(void)
{
- /*
- * This is a bit inefficient, but it lets us keep the control
- * structure simple.
- */
+#if HAL_STATIC_CLIENT_STATE_BLOCKS > 0
client_slot_t *slot;
hal_error_t err;
+ int i = 0;
do {
- slot = NULL;
-
-#if HAL_STATIC_CLIENT_STATE_BLOCKS > 0
hal_critical_section_start();
- for (int i = 0; slot == NULL && i < sizeof(client_handle)/sizeof(*client_handle); i++)
+ for (slot = NULL; slot == NULL && i < sizeof(client_handle)/sizeof(*client_handle); i++)
if (client_handle[i].logged_in != HAL_USER_NONE)
slot = &client_handle[i];
hal_critical_section_end();
-#endif
-
if ((err = clear_slot(slot)) != HAL_OK)
return err;
} while (slot != NULL);
+#endif
+
return HAL_OK;
}
@@ -251,7 +253,8 @@ static hal_error_t set_pin(const hal_client_handle_t client,
const hal_user_t user,
const char * const newpin, const size_t newpin_len)
{
- assert(newpin != NULL && newpin_len >= hal_rpc_min_pin_length && newpin_len <= hal_rpc_max_pin_length);
+ if (newpin == NULL || newpin_len < hal_rpc_min_pin_length || newpin_len > hal_rpc_max_pin_length)
+ return HAL_ERROR_IMPOSSIBLE;
if ((user != HAL_USER_NORMAL || is_logged_in(client, HAL_USER_SO) != HAL_OK) &&
is_logged_in(client, HAL_USER_WHEEL) != HAL_OK)