~ubuntu-branches/ubuntu/vivid/nettle/vivid-proposed

« back to all changes in this revision

Viewing changes to sha3.c

  • Committer: Package Import Robot
  • Author(s): Magnus Holmgren
  • Date: 2013-03-24 11:38:21 UTC
  • mfrom: (1.5.2)
  • mto: (8.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20130324113821-47kc1q7ojsxmuevv
Tags: 2.6-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* sha3.c
 
2
 *
 
3
 * The sha3 hash function.
 
4
 */
 
5
 
 
6
/* nettle, low-level cryptographics library
 
7
 *
 
8
 * Copyright (C) 2012 Niels Möller
 
9
 *  
 
10
 * The nettle library is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU Lesser General Public License as published by
 
12
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 
13
 * option) any later version.
 
14
 * 
 
15
 * The nettle library is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
17
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
18
 * License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 
22
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
23
 * MA 02111-1301, USA.
 
24
 */
 
25
 
 
26
#if HAVE_CONFIG_H
 
27
# include "config.h"
 
28
#endif
 
29
 
 
30
#include <assert.h>
 
31
#include <string.h>
 
32
 
 
33
#include "sha3.h"
 
34
 
 
35
#include "macros.h"
 
36
#include "memxor.h"
 
37
 
 
38
static void
 
39
sha3_absorb (struct sha3_state *state, unsigned length, const uint8_t *data)
 
40
{
 
41
  assert ( (length & 7) == 0);
 
42
#if WORDS_BIGENDIAN
 
43
  {    
 
44
    uint64_t *p;
 
45
    for (p = state->a; length > 0; p++, length -= 8, data += 8)
 
46
      *p ^= LE_READ_UINT64 (data);
 
47
  }
 
48
#else /* !WORDS_BIGENDIAN */
 
49
  memxor ((uint8_t *) state->a, data, length);
 
50
#endif
 
51
 
 
52
  sha3_permute (state);
 
53
}
 
54
 
 
55
unsigned
 
56
_sha3_update (struct sha3_state *state,
 
57
              unsigned block_size, uint8_t *block,
 
58
              unsigned pos,
 
59
              unsigned length, const uint8_t *data)
 
60
{
 
61
  if (pos > 0)
 
62
    {
 
63
      unsigned left = block_size - pos;
 
64
      if (length < pos)
 
65
        {
 
66
          memcpy (block + pos, data, length);
 
67
          return pos + length;
 
68
        }
 
69
      else
 
70
        {
 
71
          memcpy (block + pos, data, left);
 
72
          data += left;
 
73
          length -= left;
 
74
          sha3_absorb (state, block_size, block);
 
75
        }
 
76
    }
 
77
  for (; length >= block_size; length -= block_size, data += block_size)
 
78
    sha3_absorb (state, block_size, data);
 
79
 
 
80
  memcpy (block, data, length);
 
81
  return length;
 
82
}
 
83
 
 
84
void
 
85
_sha3_pad (struct sha3_state *state,
 
86
           unsigned block_size, uint8_t *block, unsigned pos)
 
87
{
 
88
  assert (pos < block_size);
 
89
  block[pos++] = 1;
 
90
 
 
91
  memset (block + pos, 0, block_size - pos);
 
92
  block[block_size - 1] |= 0x80;
 
93
 
 
94
  sha3_absorb (state, block_size, block);  
 
95
}