all repos — TinyCrypT @ 4b15e3a5ec0b46baab79396424d88b89809cc355

Short and sweet classical cryptographic primitives

tinycrypt/kangarootwelve128.c (view raw)

 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
#include <stdbool.h>
#include <stdint.h>

#include "tinycrypt/kangarootwelve128.h"

static uint64_t
rotl64 (uint64_t x, int c)
{
  return (x << c) | ((x & 0xffffffffffffffff) >> (64 - c));
}

static uint64_t
from_le64 (const uint8_t *x)
{
  uint64_t u = 0x0;
  for (unsigned int i = 0; i < 8; ++i)
    {
      u <<= 8;
      u |= x[7 - i];
    }
  return u;
}

static void
to_le64 (uint64_t u, uint8_t *x)
{
  for (unsigned int i = 0; i < 8; ++i)
    {
      x[i] = u & 0xFF;
      u >>= 8;
    }
}

static const uint64_t RC[12] = {
  0x8000808B,
  0x800000000000008B,
  0x8000000000008089,
  0x8000000000008003,
  0x8000000000008002,
  0x8000000000000080,
  0x800A,
  0x800000008000000A,
  0x8000000080008081,
  0x8000000000008080,
  0x0000000080000001,
  0x8000000080008008,
};

static void
kp (uint8_t state[200])
{
  uint64_t lanes[5][5];
  for (unsigned int x = 0; x < 5; ++x)
    {
      for (unsigned int y = 0; y < 5; ++y)
        {
          lanes[x][y] = from_le64 (&state[8 * (x + 5 * y)]);
        }
    }
  for (unsigned int round = 0; round < 12; ++round)
    {
      uint64_t C[5];
      uint64_t D[5];
      for (unsigned int x = 0; x < 5; ++x)
        {
          C[x] = lanes[x][0];
          C[x] ^= lanes[x][1];
          C[x] ^= lanes[x][2];
          C[x] ^= lanes[x][3];
          C[x] ^= lanes[x][4];
        }
      for (unsigned int x = 0; x < 5; ++x)
        {
          D[x] = C[(x + 4) % 5] ^ rotl64 (C[(x + 1) % 5], 1);
        }
      for (unsigned int y = 0; y < 5; ++y)
        {
          for (unsigned int x = 0; x < 5; ++x)
            {
              lanes[x][y] ^= D[x];
            }
        }
      uint64_t x = 1;
      uint64_t y = 0;
      uint64_t current = lanes[x][y];
      for (unsigned int t = 0; t < 24; ++t)
        {
          uint64_t tmp = x;
          x = y;
          y = (2 * tmp + 3 * y) % 5;
          tmp = current;
          current = lanes[x][y];
          lanes[x][y] = rotl64 (tmp, (t + 1) * (t + 2) / 2);
        }
      uint64_t T[5];
      for (unsigned int y = 0; y < 5; ++y)
        {
          for (unsigned int x = 0; x < 5; ++x)
            {
              T[x] = lanes[x][y];
            }
          for (unsigned int x = 0; x < 5; ++x)
            {
              lanes[x][y] = T[x] ^ ((~T[(x + 1) % 5]) & T[(x + 2) % 5]);
            }
        }
      lanes[0][0] ^= RC[round];
    }
  unsigned int cursor = 0;
  for (unsigned int y = 0; y < 5; ++y)
    {
      for (unsigned int x = 0; x < 5; ++x)
        {
          to_le64 (lanes[x][y], state + cursor);
          cursor += 8;
        }
    }
}

static void
turboshake128_init (uint8_t *state)
{
  for (unsigned int i = 0; i < 200; ++i)
    {
      state[i] = 0x0;
    }
}

static void
turboshake128_absorb (uint8_t *state, const uint8_t *data,
                      const uint64_t data_len, const uint8_t separation_byte)
{
  for (unsigned int i = 0; i < data_len; ++i)
    {
      state[i] ^= data[i];
    }
  if (data_len < 168)
    {
      state[data_len] ^= separation_byte;
      state[167] ^= 0x80;
    }
  kp (state);
}

static void
turboshake128_squeeze_destructive (uint8_t *state, uint8_t *output,
                                   const uint64_t output_len)
{
  for (uint64_t i = 0; i < output_len; i += 168)
    {
      for (unsigned int j = 0;
           j < ((output_len - i < 168) ? output_len - i : 168); ++j)
        {
          output[i + j] = state[j];
        }
      kp (state);
    }
}

static void
length_encode (const uint64_t n, uint8_t *out, uint64_t *out_len)
{
  (*out_len) = 1;
  for (uint64_t i = n; i > 0; i /= 256)
    {
      (*out_len)++;
    }
  for (uint64_t i = 0; i < (*out_len) - 1; ++i)
    {
      out[(*out_len) - 2 - i] = (n >> (8 * i)) & 0xFF;
    }
  out[(*out_len) - 1] = ((*out_len) - 1) & 0xFF;
}

void
tct_kangarootwelve128 (const uint8_t *input, const uint64_t input_len,
                       const uint8_t *custom_str, const uint64_t cs_len,
                       uint8_t *output, const uint64_t output_len)
{
  uint8_t buf[168];
  uint8_t state[200];
  turboshake128_init (state);
  uint8_t encoded[8];
  uint64_t encoded_len;
  length_encode (cs_len, encoded, &encoded_len);
  const uint64_t s_size = input_len + cs_len + encoded_len;
  if (s_size <= 8192)
    {
      for (uint64_t i = 0; i < s_size; i += 168)
        {
          uint64_t buf_len = (s_size - i < 168) ? s_size - i : 168;
          for (uint64_t j = 0; j < buf_len; ++j)
            {
              if (i + j < input_len)
                {
                  buf[j] = input[i + j];
                }
              else if (i + j < input_len + cs_len)
                {
                  buf[j] = custom_str[i + j - input_len];
                }
              else
                {
                  buf[j] = encoded[i + j - input_len - cs_len];
                }
            }
          turboshake128_absorb (state, buf, buf_len, 0x07);
        }
      turboshake128_squeeze_destructive (state, output, output_len);
      return;
    }
  uint64_t buf_len;
  for (unsigned int i = 0; i < 8200; i += 168)
    {
      buf_len = (8200 - i < 168) ? 8200 - i : 168;
      for (unsigned int j = 0; j < buf_len; ++j)
        {
          if (i + j < 8192)
            {
              if (i + j < input_len)
                {
                  buf[j] = input[i + j];
                }
              else if (i + j < input_len + cs_len)
                {
                  buf[j] = custom_str[i + j - input_len];
                }
              else
                {
                  buf[j] = encoded[i + j - input_len - cs_len];
                }
            }
          else if (i + j < 8193)
            {
              buf[j] = 0x03;
            }
          else
            {
              buf[j] = 0x0;
            }
        }
      if (buf_len == 168)
        {
          turboshake128_absorb (state, buf, 168, 0x06);
        }
    }
  uint64_t offset = 8192;
  uint64_t num_block = 0;
  while (offset < s_size)
    {
      uint64_t block_size = (8192 < s_size - offset) ? 8192 : s_size - offset;
      uint8_t cv[32];
      uint8_t cv_buf[168];
      uint8_t cv_state[200];
      turboshake128_init (cv_state);
      for (uint64_t i = offset; i < offset + block_size; i += 168)
        {
          uint64_t cv_block_size = (168 < offset + block_size - i)
                                       ? 168
                                       : offset + block_size - i;
          for (uint64_t j = 0; j < cv_block_size; ++j)
            {
              if (i + j < input_len)
                {
                  cv_buf[j] = input[i + j];
                }
              else if (i + j < input_len + cs_len)
                {
                  cv_buf[j] = custom_str[i + j - input_len];
                }
              else
                {
                  cv_buf[j] = encoded[i + j - input_len - cs_len];
                }
            }
          turboshake128_absorb (cv_state, cv_buf, cv_block_size, 0x0B);
        }
      turboshake128_squeeze_destructive (cv_state, cv, 32);
      if (buf_len + 32 >= 168)
        {
          for (unsigned int i = buf_len; i < 168; ++i)
            {
              buf[i] = cv[i - buf_len];
            }
          turboshake128_absorb (state, buf, 168, 0x06);
          for (unsigned int i = (buf_len + 32) - 168; i < 32; ++i)
            {
              buf[i] = cv[i];
            }
        }
      else
        {
          for (unsigned int i = buf_len; i < buf_len + 32; ++i)
            {
              buf[i] = cv[i - buf_len];
            }
        }
      buf_len += 32;
      buf_len %= 168;
      num_block++;
      offset += block_size;
    }
  uint8_t nb_encoded[8];
  uint64_t nb_encoded_size;
  length_encode (num_block, nb_encoded, &nb_encoded_size);
  if (buf_len + nb_encoded_size + 2 >= 168)
    {
      for (unsigned int i = buf_len; i < 168; ++i)
        {
          if (i - buf_len < nb_encoded_size)
            {
              buf[i] = nb_encoded[i - buf_len];
            }
          else
            {
              buf[i] = 0xFF;
            }
        }
      turboshake128_absorb (state, buf, 168, 0x06);

      for (unsigned int i = (buf_len + nb_encoded_size + 2) - 168;
           i < nb_encoded_size + 2; ++i)
        {
          if (i < nb_encoded_size)
            {
              buf[i] = nb_encoded[i];
            }
          else
            {
              buf[i] = 0xFF;
            }
        }
      buf_len = (buf_len + nb_encoded_size + 2) - 168;
    }
  else
    {
      for (unsigned int i = buf_len; i < buf_len + nb_encoded_size + 2; ++i)
        {
          if (i - buf_len < nb_encoded_size)
            {
              buf[i] = nb_encoded[i - buf_len];
            }
          else
            {
              buf[i] = 0xFF;
            }
        }
      buf_len += nb_encoded_size + 2;
    }
  turboshake128_absorb (state, buf, buf_len, 0x06);
  turboshake128_squeeze_destructive (state, output, output_len);
}