aboutsummaryrefslogtreecommitdiff
path: root/hal_io_fmc.c
blob: 7aa4b19e4ced76b86ebe35ed6fe11585a3366e28 (plain) (blame)
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
/*
 * hal_io_fmc.c
 * ------------
 * This module contains common code to talk to the FPGA over the FMC bus.
 *
 * Author: Paul Selkirk
 * Copyright (c) 2014-2016, NORDUnet A/S All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the NORDUnet nor the names of its contributors may
 *   be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdint.h>

#include "stm-fmc.h"

/* stm32f4xx_hal_def.h and hal.h both define HAL_OK as an enum value */
#define HAL_OK HAL_OKAY

#include "hal.h"
#include "hal_internal.h"

static int debug = 0;
static int inited = 0;

#ifndef FMC_IO_TIMEOUT
#define FMC_IO_TIMEOUT  100000000
#endif

static hal_error_t init(void)
{
  if (!inited) {
    fmc_init();
    inited = 1;
  }
  return HAL_OK;
}

/* Translate cryptech register number to FMC address.
 */
static hal_addr_t fmc_offset(hal_addr_t offset)
{
  return offset << 2;
}

void hal_io_set_debug(int onoff)
{
  debug = onoff;
}

static void dump(char *label, hal_addr_t offset, const uint8_t *buf, size_t len)
{
  if (debug) {
    size_t i;
    printf("%s %04x [", label, (unsigned int)offset);
    for (i = 0; i < len; ++i)
      printf(" %02x", buf[i]);
    printf(" ]\n");
  }
}

hal_error_t hal_io_write(const hal_core_t *core, hal_addr_t offset, const uint8_t *buf, size_t len)
{
  hal_error_t err;

  if (core == NULL)
    return HAL_ERROR_CORE_NOT_FOUND;

  if (len % 4 != 0)
    return HAL_ERROR_IO_BAD_COUNT;

  if ((err = init()) != HAL_OK)
    return err;

  dump("write ", offset + hal_core_base(core), buf, len);

  offset = fmc_offset(offset + hal_core_base(core));
  for (; len > 0; offset += 4, buf += 4, len -= 4) {
    uint32_t val;
    val = htonl(*(uint32_t *)buf);
    fmc_write_32(offset, &val);
  }

  return HAL_OK;
}

hal_error_t hal_io_read(const hal_core_t *core, hal_addr_t offset, uint8_t *buf, size_t len)
{
  uint8_t *rbuf = buf;
  int rlen = len;
  hal_addr_t orig_offset = offset;
  hal_error_t err;

  if (core == NULL)
    return HAL_ERROR_CORE_NOT_FOUND;

  if (len % 4 != 0)
    return HAL_ERROR_IO_BAD_COUNT;

  if ((err = init()) != HAL_OK)
    return err;

  offset = fmc_offset(offset + hal_core_base(core));
  for (; rlen > 0; offset += 4, rbuf += 4, rlen -= 4) {
    uint32_t val;
    fmc_read_32(offset, &val);
    *(uint32_t *)rbuf = ntohl(val);
  }

  dump("read  ", orig_offset + hal_core_base(core), buf, len);

  return HAL_OK;
}

hal_error_t hal_io_init(const hal_core_t *core)
{
  uint8_t buf[4] = { 0, 0, 0, CTRL_INIT };
  return hal_io_write(core, ADDR_CTRL, buf, sizeof(buf));
}

hal_error_t hal_io_next(const hal_core_t *core)
{
  uint8_t buf[4] = { 0, 0, 0, CTRL_NEXT };
  return hal_io_write(core, ADDR_CTRL, buf, sizeof(buf));
}

hal_error_t hal_io_wait(const hal_core_t *core, uint8_t status, int *count)
{
  hal_error_t err;
  uint8_t buf[4];
  int i;

  for (i = 1; ; ++i) {

    if (count && (*count > 0) && (i >= *count))
      return HAL_ERROR_IO_TIMEOUT;

    if ((err = hal_io_read(core, ADDR_STATUS, buf, sizeof(buf))) != HAL_OK)
      return err;

    if ((buf[3] & status) != 0) {
      if (count)
        *count = i;
      return HAL_OK;
    }
  }
}

hal_error_t hal_io_wait_ready(const hal_core_t *core)
{
  int limit = FMC_IO_TIMEOUT;
  return hal_io_wait(core, STATUS_READY, &limit);
}

hal_error_t hal_io_wait_valid(const hal_core_t *core)
{
  int limit = FMC_IO_TIMEOUT;
  return hal_io_wait(core, STATUS_VALID, &limit);
}

/*
 * Local variables:
 * indent-tabs-mode: nil
 * c-basic-offset: 2
 * End:
 */
ass="w"> != HAL_OK) return err; return HAL_OK; } static int find(const hal_ks_keydb_t * const db, const hal_key_type_t type, const uint8_t * const name, const size_t name_len, int *hint) { assert(db != NULL && name != NULL && acceptable_key_type(type)); if (hint != NULL && *hint >= 0 && *hint < sizeof(db->keys)/sizeof(*db->keys) && db->keys[*hint].in_use && db->keys[*hint].type == type && db->keys[*hint].name_len == name_len && memcmp(db->keys[*hint].name, name, name_len) == 0) return 1; for (int i = 0; i < sizeof(db->keys)/sizeof(*db->keys); i++) { if (!db->keys[i].in_use || (hint != NULL && i == *hint) || db->keys[i].type != type || db->keys[i].name_len != name_len || memcmp(db->keys[i].name, name, name_len) != 0) continue; if (hint != NULL) *hint = i; return 1; } return 0; } hal_error_t hal_ks_exists(const hal_key_type_t type, const uint8_t * const name, const size_t name_len, int *hint) { if (name == NULL || !acceptable_key_type(type)) return HAL_ERROR_BAD_ARGUMENTS; const hal_ks_keydb_t * const db = hal_ks_get_keydb(); if (db == NULL) return HAL_ERROR_KEYSTORE_ACCESS; if (find(db, type, name, name_len, hint)) return HAL_OK; else return HAL_ERROR_KEY_NOT_FOUND; } hal_error_t hal_ks_fetch(const hal_key_type_t type, const uint8_t * const name, const size_t name_len, hal_curve_name_t *curve, hal_key_flags_t *flags, uint8_t *der, size_t *der_len, const size_t der_max, int *hint) { if (name == NULL || !acceptable_key_type(type)) return HAL_ERROR_BAD_ARGUMENTS; const hal_ks_keydb_t * const db = hal_ks_get_keydb(); int hint_ = -1; if (db == NULL) return HAL_ERROR_KEYSTORE_ACCESS; if (hint == NULL) hint = &hint_; if (!find(db, type, name, name_len, hint)) return HAL_ERROR_KEY_NOT_FOUND; const hal_ks_key_t * const k = &db->keys[*hint]; if (curve != NULL) *curve = k->curve; if (flags != NULL) *flags = k->flags; if (der == NULL && der_len != NULL) *der_len = k->der_len; if (der != NULL) { #if USE_KEK uint8_t kek[KEK_LENGTH]; size_t kek_len, der_len_; hal_error_t err; if (der_len == NULL) der_len = &der_len_; *der_len = der_max; if ((err = hal_ks_get_kek(kek, &kek_len, sizeof(kek))) == HAL_OK) err = hal_aes_keyunwrap(NULL, kek, kek_len, k->der, k->der_len, der, der_len); memset(kek, 0, sizeof(kek)); if (err != HAL_OK) return err; #else /* USE_KEK */ if (k->der_len > der_max) return HAL_ERROR_RESULT_TOO_LONG; if (der_len != NULL) *der_len = k->der_len; memcpy(der, k->der, k->der_len); #endif /* USE_KEK */ } return HAL_OK; } hal_error_t hal_ks_delete(const hal_key_type_t type, const uint8_t * const name, const size_t name_len, int *hint) { if (name == NULL || !acceptable_key_type(type)) return HAL_ERROR_BAD_ARGUMENTS; const hal_ks_keydb_t * const db = hal_ks_get_keydb(); int hint_ = -1; if (db == NULL) return HAL_ERROR_KEYSTORE_ACCESS; if (hint == NULL) hint = &hint_; if (!find(db, type, name, name_len, hint)) return HAL_ERROR_KEY_NOT_FOUND; return hal_ks_del_keydb(*hint); } hal_error_t hal_ks_rename(const hal_key_type_t type, const uint8_t * const old_name, const size_t old_name_len, const uint8_t * const new_name, const size_t new_name_len, int *hint) { if (old_name == NULL || new_name == NULL || !acceptable_key_type(type)) return HAL_ERROR_BAD_ARGUMENTS; if (new_name_len > HAL_RPC_PKEY_NAME_MAX) return HAL_ERROR_KEY_NAME_TOO_LONG; const hal_ks_keydb_t * const db = hal_ks_get_keydb(); int hint_ = -1; if (db == NULL) return HAL_ERROR_KEYSTORE_ACCESS; if (find(db, type, new_name, new_name_len, NULL)) return HAL_ERROR_KEY_NAME_IN_USE; if (hint == NULL) hint = &hint_; if (!find(db, type, old_name, old_name_len, hint)) return HAL_ERROR_KEY_NOT_FOUND; hal_ks_key_t k = db->keys[*hint]; assert(new_name_len <= sizeof(k.name)); memcpy(k.name, new_name, new_name_len); k.name_len = new_name_len; return hal_ks_set_keydb(&k, *hint, 1); } hal_error_t hal_ks_list(hal_pkey_info_t *result, unsigned *result_len, const unsigned result_max) { if (result == NULL || result_len == NULL) return HAL_ERROR_BAD_ARGUMENTS; const hal_ks_keydb_t * const db = hal_ks_get_keydb(); if (db == NULL) return HAL_ERROR_KEYSTORE_ACCESS; *result_len = 0; for (int i = 0; i < sizeof(db->keys)/sizeof(*db->keys); i++) { if (!db->keys[i].in_use) continue; if (*result_len == result_max) return HAL_ERROR_RESULT_TOO_LONG; result[*result_len].type = db->keys[i].type; result[*result_len].curve = db->keys[i].curve; result[*result_len].flags = db->keys[i].flags; result[*result_len].name_len = db->keys[i].name_len; memcpy(result[*result_len].name, db->keys[i].name, db->keys[i].name_len); ++ *result_len; } return HAL_OK; } hal_error_t hal_ks_get_pin(const hal_user_t user, const hal_ks_pin_t **pin) { if (pin == NULL) return HAL_ERROR_BAD_ARGUMENTS; const hal_ks_keydb_t * const db = hal_ks_get_keydb(); if (db == NULL) return HAL_ERROR_KEYSTORE_ACCESS; switch (user) { case HAL_USER_WHEEL: *pin = &db->wheel_pin; break; case HAL_USER_SO: *pin = &db->so_pin; break; case HAL_USER_NORMAL: *pin = &db->user_pin; break; default: return HAL_ERROR_BAD_ARGUMENTS; } #warning Need better "Have we been initialized yet?" test /* * If we were looking for the WHEEL PIN and it appears to be * completely unset, return the compiled-in last-gasp PIN. This is * a terrible answer, but we need some kind of bootstrapping * mechanism. Feel free to suggest something better. * * We probably need some more general "have we been initialized?" * state somewhere, and might want to refuse to do things like * storing keys until we've been initialized and the appropriate * PINs have been set. * * Just to make things more fun, some drivers return all zeros for * "this has never been set", some return all ones to indicate the * same thing. REALLY need a flag somewhere. */ uint8_t u00 = 0x00, uFF = 0xFF; for (int i = 0; i < sizeof((*pin)->pin); i++) { u00 |= (*pin)->pin[i]; uFF &= (*pin)->pin[i]; } for (int i = 0; i < sizeof((*pin)->salt); i++) { u00 |= (*pin)->salt[i]; uFF &= (*pin)->salt[i]; } if (user == HAL_USER_WHEEL && ((u00 == 0x00 && (*pin)->iterations == 0x00000000) || (uFF == 0xFF && (*pin)->iterations == 0xFFFFFFFF))) *pin = &hal_last_gasp_pin; return HAL_OK; } /* * Local variables: * indent-tabs-mode: nil * End: */