~ubuntu-branches/ubuntu/utopic/nettle/utopic

« back to all changes in this revision

Viewing changes to salsa20-crypt.c

  • Committer: Package Import Robot
  • Author(s): Magnus Holmgren
  • Date: 2013-03-24 11:38:21 UTC
  • mto: (1.4.6) (3.1.11 experimental)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20130324113821-3rsmekqi09c93lnh
Tags: upstream-2.6
ImportĀ upstreamĀ versionĀ 2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * 
20
20
 * You should have received a copy of the GNU Lesser General Public License
21
21
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
22
 
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23
 
 * MA 02111-1307, USA.
 
22
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
23
 * MA 02111-1301, USA.
24
24
 */
25
25
 
26
26
/* Based on:
40
40
#include "macros.h"
41
41
#include "memxor.h"
42
42
 
43
 
#ifdef WORDS_BIGENDIAN
44
 
#define LE_SWAP32(v)                            \
45
 
  ((ROTL32(8,  v) & 0x00FF00FFUL) |             \
46
 
   (ROTL32(24, v) & 0xFF00FF00UL))
47
 
#else
48
 
#define LE_SWAP32(v) (v)
49
 
#endif
50
 
 
51
 
#define QROUND(x0, x1, x2, x3) do { \
52
 
  x1 ^= ROTL32(7, x0 + x3);         \
53
 
  x2 ^= ROTL32(9, x1 + x0);         \
54
 
  x3 ^= ROTL32(13, x2 + x1);        \
55
 
  x0 ^= ROTL32(18, x3 + x2);        \
56
 
  } while(0)
57
 
 
58
43
void
59
44
salsa20_crypt(struct salsa20_ctx *ctx,
60
45
              unsigned length,
67
52
  for (;;)
68
53
    {
69
54
      uint32_t x[_SALSA20_INPUT_LENGTH];
70
 
      int i;
71
 
      memcpy (x, ctx->input, sizeof(x));
72
 
      for (i = 0;i < 10;i ++)
73
 
        {
74
 
          QROUND(x[0], x[4], x[8], x[12]);
75
 
          QROUND(x[5], x[9], x[13], x[1]);
76
 
          QROUND(x[10], x[14], x[2], x[6]);
77
 
          QROUND(x[15], x[3], x[7], x[11]);
78
 
 
79
 
          QROUND(x[0], x[1], x[2], x[3]);
80
 
          QROUND(x[5], x[6], x[7], x[4]);
81
 
          QROUND(x[10], x[11], x[8], x[9]);
82
 
          QROUND(x[15], x[12], x[13], x[14]);
83
 
        }
84
 
 
85
 
      for (i = 0;i < _SALSA20_INPUT_LENGTH;++i)
86
 
        {
87
 
          uint32_t t = x[i] + ctx->input[i];
88
 
          x[i] = LE_SWAP32 (t);
89
 
        }
 
55
 
 
56
      _salsa20_core (x, ctx->input, 20);
90
57
 
91
58
      ctx->input[9] += (++ctx->input[8] == 0);
92
59