aboutsummaryrefslogtreecommitdiff
path: root/hash.c
blob: 4a154910c0306787f5ea00c2d42a01c6005011a2 (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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* 
 * hashes.c
 * --------
 *
 * HAL interface to Cryptech hash cores.
 * 
 * Authors: Joachim Strömbergson, Paul Selkirk, Rob Austein
 * Copyright (c) 2014-2015, SUNET
 * 
 * Redistribution and use in source and binary forms, with or 
 * without modification, are permitted provided that the following 
 * conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 
 * 2. 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. 
 * 
 * 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 OWNER 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 <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include "cryptech.h"

/* Longest digest block we support at the moment */
#define MAX_BLOCK_LEN           SHA512_BLOCK_LEN

/* Hash state */
typedef struct {
  uint64_t msg_length_high;             /* Total data hashed in this message */
  uint64_t msg_length_low;              /* (128 bits in SHA-512 cases) */
  size_t block_length;                  /* Block length for this algorithm */
  uint8_t block[MAX_BLOCK_LEN];         /* Block we're accumulating */
  size_t block_used;                    /* How much of the block we've used */
  unsigned block_count;                 /* Blocks sent */
} hash_state_t;

static int debug = 0;

/*
 * Debugging control.
 */

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

/*
 * Tell caller how much space to allocate for a hash_state_t.  This
 * lets us hide details that are nobody else's business while letting
 * somebody else deal with memory allocation (and is the way
 * Cryptlib's HAL code works, not by coincidence).
 */

size_t hal_hash_state_size(void)
{
  return sizeof(hash_state_t);
}

void hal_hash_state_initialize(void *_state)
{
  hash_state_t *state = _state;
  assert(state != NULL);
  memset(state, 0, sizeof(*state));
}

/*
 * Report whether cores are present.
 */

hal_error_t hash_sha1_core_present(void)
{
  return hal_io_expected(SHA1_ADDR_NAME0, (const uint8_t *) (SHA1_NAME0 SHA1_NAME1), 8);
}

hal_error_t hash_sha256_core_present(void)
{
  return hal_io_expected(SHA256_ADDR_NAME0, (const uint8_t *) (SHA256_NAME0 SHA256_NAME1), 8);
}

hal_error_t hash_sha512_core_present(void)
{
  return hal_io_expected(SHA512_ADDR_NAME0, (const uint8_t *) (SHA512_NAME0 SHA512_NAME1), 8);
}

/*
 * Send one block to a core.
 */

static hal_error_t hash_write_block(const off_t block_addr,
                                    const off_t ctrl_addr,
                                    const off_t status_addr,
                                    const uint8_t ctrl_mode,
                                    const hash_state_t * const state)
{
  uint8_t ctrl_cmd[4];
  hal_error_t err;

  assert(state != NULL && state->block_length % 4 == 0);

  if (debug)
    fprintf(stderr, "[ %s ]\n", state->block_count == 0 ? "init" : "next");

  if ((err = hal_io_write(block_addr, state->block, state->block_length)) != HAL_OK)
    return err;

  ctrl_cmd[0] = ctrl_cmd[1] = ctrl_cmd[2] = 0;
  ctrl_cmd[3] = state->block_count == 0 ? CTRL_INIT : CTRL_NEXT;  
  ctrl_cmd[3] |= ctrl_mode;

  /*
   * Not sure why we're waiting for ready here, but it's what the old
   * (read: tested) code did, so keep that behavior for now.
   */

  if ((err = hal_io_write(ctrl_addr, ctrl_cmd, sizeof(ctrl_cmd))) != HAL_OK)
    return err;

  return hal_io_wait_valid(status_addr);
}

/*
 * Read hash result from core.
 */

static hal_error_t hash_read_digest(const uint8_t digest_addr,
                                    const uint8_t status_addr,
                                    uint8_t *digest,
                                    const size_t digest_length)
{
  hal_error_t err;

  assert(digest != NULL && digest_length % 4 == 0);

  if ((err = hal_io_wait_valid(status_addr)) != HAL_OK)
    return err;

  return hal_io_read(digest_addr, digest, digest_length);
}

/*
 * Hash data.  All supported hash algorithms use similar block
 * manipulations and padding algorithms, so all can use this method
 * with a few parameters which we handle via closures below.
 */

static hal_error_t hash_do_hash(hash_state_t *state,                    /* Opaque state block */
                                const uint8_t * const data_buffer,	/* Data to be hashed */
                                size_t data_buffer_length,              /* Length of data_buffer */
                                uint8_t *digest_buffer,                 /* Returned digest */
                                const size_t digest_buffer_length,      /* Length of digest_buffer */
                                const size_t block_length,              /* Length of a block */
                                const size_t digest_length,             /* Length of resulting digest */
                                const size_t length_length,             /* Length of the length field */
                                const off_t block_addr,                 /* Where to write hash blocks */
                                const off_t ctrl_addr,                  /* Control register */
                                const off_t status_addr,                /* Status register */
                                const off_t digest_addr,                /* Where to read digest */
                                const uint8_t ctrl_mode)                /* Digest mode, for cores that have modes */
{
  hal_error_t err;
  size_t n;
  int i;

  if (state == NULL ||
      (state->block_length != 0 && state->block_length != block_length) ||
      (data_buffer_length > 0 && data_buffer == NULL) ||
      (data_buffer_length == 0 && digest_buffer == NULL) ||
      (digest_buffer != NULL && digest_buffer_length < digest_length))
    return HAL_ERROR_BAD_ARGUMENTS;

  if (state->block_length == 0)
    state->block_length = block_length;

  assert(block_length <= sizeof(state->block));

  if (data_buffer_length > 0) {                            /* We have data to hash */

    const uint8_t *p = data_buffer;

    while ((n = state->block_length - state->block_used) <= data_buffer_length) {
      /*
       * We have enough data for another complete block.
       */
      if (debug)
        fprintf(stderr, "[ Full block, data_buffer_length %lu, used %lu, n %lu, msg_length %llu ]\n",
                (unsigned long) data_buffer_length, (unsigned long) state->block_used, (unsigned long) n, state->msg_length_low);
      memcpy(state->block + state->block_used, p, n);
      if ((state->msg_length_low += n) < n)
        state->msg_length_high++;
      state->block_used = 0;
      data_buffer_length -= n;
      p += n;
      if ((err = hash_write_block(block_addr, ctrl_addr, status_addr, ctrl_mode, state)) != HAL_OK)
        return err;
      state->block_count++;
    }

    if (data_buffer_length > 0) {
      /*
       * Data left over, but not enough for a full block, stash it.
       */
      if (debug)
        fprintf(stderr, "[ Partial block, data_buffer_length %lu, used %lu, n %lu, msg_length %llu ]\n",
                (unsigned long) data_buffer_length, (unsigned long) state->block_used, (unsigned long) n, state->msg_length_low);
      assert(data_buffer_length < n);
      memcpy(state->block + state->block_used, p, data_buffer_length);
      if ((state->msg_length_low += data_buffer_length) < data_buffer_length)
        state->msg_length_high++;
      state->block_used += data_buffer_length;
    }
  }

  else {           /* Done: add padding, then pull result from the core */

    uint64_t bit_length_low  = (state->msg_length_low  << 3);
    uint64_t bit_length_high = (state->msg_length_high << 3) | (state->msg_length_low >> 61);
    uint8_t *p;

    /* Initial pad byte */
    assert(state->block_used < state->block_length);
    state->block[state->block_used++] = 0x80;

    /* If not enough room for bit count, zero and push current block */
    if ((n = state->block_length - state->block_used) < length_length) {
      if (debug)
        fprintf(stderr, "[ Overflow block, data_buffer_length %lu, used %lu, n %lu, msg_length %llu ]\n",
                (unsigned long) data_buffer_length, (unsigned long) state->block_used, (unsigned long) n, state->msg_length_low);
      if (n > 0)
        memset(state->block + state->block_used, 0, n);
      if ((err = hash_write_block(block_addr, ctrl_addr, status_addr, ctrl_mode, state)) != HAL_OK)
        return err;
      state->block_count++;
      state->block_used = 0;
    }

    /* Pad final block */
    n = state->block_length - state->block_used;
    assert(n >= length_length);
    if (n > 0)
      memset(state->block + state->block_used, 0, n);
    if (debug)
      fprintf(stderr, "[ Final block, data_buffer_length %lu, used %lu, n %lu, msg_length %llu ]\n",
              (unsigned long) data_buffer_length, (unsigned long) state->block_used, (unsigned long) n, state->msg_length_low);
    p = state->block + state->block_length;
    for (i = 0; (bit_length_low || bit_length_high) && i < length_length; i++) {
      *--p = (uint8_t) (bit_length_low & 0xFF);
      bit_length_low >>= 8;
      if (bit_length_high) {
        bit_length_low |= ((bit_length_high & 0xFF) << 56);
        bit_length_high >>= 8;
      }
    }

    /* Push final block */
    if ((err = hash_write_block(block_addr, ctrl_addr, status_addr, ctrl_mode, state)) != HAL_OK)
      return err;
    state->block_count++;

    /* All data pushed to core, now we just need to read back the result */
    if ((err = hash_read_digest(digest_addr, status_addr, digest_buffer, digest_length)) != HAL_OK)
      return err;
  }

  return HAL_OK;
}

/*
 * Closures to provide the public API.
 */

hal_error_t hal_hash_sha1(void *state,
                          const uint8_t *data_buffer,
                          const size_t data_buffer_length,
                          uint8_t *digest_buffer,
                          const size_t digest_buffer_length)
{
  return hash_do_hash(state, data_buffer, data_buffer_length, digest_buffer, digest_buffer_length,
                      SHA1_BLOCK_LEN, SHA1_DIGEST_LEN, SHA1_LENGTH_LEN,
                      SHA1_ADDR_BLOCK, SHA1_ADDR_CTRL, SHA1_ADDR_STATUS, SHA1_ADDR_DIGEST, 0);
}

hal_error_t hal_hash_sha256(void *state,
                            const uint8_t *data_buffer,
                            const size_t data_buffer_length,
                            uint8_t *digest_buffer,
                            const size_t digest_buffer_length)
{
  return hash_do_hash(state, data_buffer, data_buffer_length, digest_buffer, digest_buffer_length,
                      SHA256_BLOCK_LEN, SHA256_DIGEST_LEN, SHA256_LENGTH_LEN,
                      SHA256_ADDR_BLOCK, SHA256_ADDR_CTRL, SHA256_ADDR_STATUS, SHA256_ADDR_DIGEST, 0);
}

hal_error_t hal_hash_sha512_224(void *state,
                                const uint8_t *data_buffer,
                                const size_t data_buffer_length,
                                uint8_t *digest_buffer,
                                const size_t digest_buffer_length)
{
  return hash_do_hash(state, data_buffer, data_buffer_length, digest_buffer, digest_buffer_length,
                      SHA512_BLOCK_LEN, SHA512_DIGEST_LEN, SHA512_LENGTH_LEN,
                      SHA512_ADDR_BLOCK, SHA512_ADDR_CTRL, SHA512_ADDR_STATUS, SHA512_ADDR_DIGEST,
                      MODE_SHA_512_224);
}

hal_error_t hal_hash_sha512_256(void *state,
                                const uint8_t *data_buffer,
                                const size_t data_buffer_length,
                                uint8_t *digest_buffer,
                                const size_t digest_buffer_length)
{
  return hash_do_hash(state, data_buffer, data_buffer_length, digest_buffer, digest_buffer_length,
                      SHA512_BLOCK_LEN, SHA512_DIGEST_LEN, SHA512_LENGTH_LEN,
                      SHA512_ADDR_BLOCK, SHA512_ADDR_CTRL, SHA512_ADDR_STATUS, SHA512_ADDR_DIGEST,
                      MODE_SHA_512_256);
}

hal_error_t hal_hash_sha384(void *state,
                            const uint8_t *data_buffer,
                            const size_t data_buffer_length,
                            uint8_t *digest_buffer,
                            const size_t digest_buffer_length)
{
  return hash_do_hash(state, data_buffer, data_buffer_length, digest_buffer, digest_buffer_length,
                      SHA512_BLOCK_LEN, SHA512_DIGEST_LEN, SHA512_LENGTH_LEN,
                      SHA512_ADDR_BLOCK, SHA512_ADDR_CTRL, SHA512_ADDR_STATUS, SHA512_ADDR_DIGEST,
                      MODE_SHA_384);
}

hal_error_t hal_hash_sha512(void *state,
                            const uint8_t *data_buffer,
                            const size_t data_buffer_length,
                            uint8_t *digest_buffer,
                            const size_t digest_buffer_length)
{
  return hash_do_hash(state, data_buffer, data_buffer_length, digest_buffer, digest_buffer_length,
                      SHA512_BLOCK_LEN, SHA512_DIGEST_LEN, SHA512_LENGTH_LEN,
                      SHA512_ADDR_BLOCK, SHA512_ADDR_CTRL, SHA512_ADDR_STATUS, SHA512_ADDR_DIGEST,
                      MODE_SHA_512);
}

/*
 * "Any programmer who fails to comply with the standard naming, formatting,
 *  or commenting conventions should be shot.  If it so happens that it is
 *  inconvenient to shoot him, then he is to be politely requested to recode
 *  his program in adherence to the above standard."
 *                      -- Michael Spier, Digital Equipment Corporation
 *
 * Local variables:
 * indent-tabs-mode: nil
 * End:
 */