skinny_key_schedule2.c 5 KB
Newer Older
Enrico Pozzobon committed
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
/******************************************************************************
 * Copyright (c) 2020, NEC Corporation.
 *
 * THIS CODE IS FURNISHED TO YOU "AS IS" WITHOUT WARRANTY OF ANY KIND.
 *
 *****************************************************************************/

/*
 * SKINNY-128-384
 *
 * load * AC(c0 c1) ^ TK3
 * calc AC(c0 c1) ^ TK2 -> store
 * ART(TK2)
 *
 * number of rounds : 40 or 56
 */

#include "skinny.h"

#define PERMUTATION_TK2()                                                         \
                                                                                  \
  /* permutation */                                                               \
                                                                                  \
    PERMUTATION()                                                                 \
                                                                                  \
  /* LFSR(for TK2) (x7 x6 x5 x4 x3 x2 x1 x0) -> (x6 x5 x4 x3 x2 x1 x0 x7^x5) */   \
    w0 = ((w0 << 1) & 0xfefefefe) ^                                               \
         (((w0 >> 7) ^ (w0 >> 5)) & 0x01010101);                                  \
    w1 = ((w1 << 1) & 0xfefefefe) ^                                               \
         (((w1 >> 7) ^ (w1 >> 5)) & 0x01010101);                                  \
                                                                                  \
  /* Load TK3 */                                                                  \
  /* TK2^TK3^AC(c0 c1) */                                                         \
  /* store */                                                                     \
    *tk2++ = w0 ^ *tk3++;                                                         \
    *tk2++ = w1 ^ *tk3++;                                                         \
    tk2 += 2;                                                                     \
    tk3 += 2;

#ifndef ___SKINNY_LOOP

void RunEncryptionKeyScheduleTK2(uint32_t *roundKeys)
{
  uint32_t* tk2;  // used in MACRO
  uint32_t* tk3;  // used in MACRO
  uint32_t t0;    // used in MACRO
  uint32_t t1;    // used in MACRO
  uint32_t t2;    // used in MACRO
  uint32_t w0;
  uint32_t w1;

  // odd
  
  // load master key
  w0 = roundKeys[4];
  w1 = roundKeys[5];

  tk2 = &roundKeys[16];
#ifndef ___NUM_OF_ROUNDS_56
  tk3 = &roundKeys[96];
#else
  tk3 = &roundKeys[128];
#endif

  // 1st round
  *tk2++ = w0 ^ *tk3++;
  *tk2++ = w1 ^ *tk3++;

  tk2 += 2;
  tk3 += 2;

  // 3rd,5th, ... ,37th,39th round
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();

  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();

#ifdef ___NUM_OF_ROUNDS_56

  // 41th,43th, ... ,51th,53th round
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();

#endif

  // even

  // load master key
  w0 = roundKeys[6];
  w1 = roundKeys[7];

  tk2 = &roundKeys[18];
#ifndef ___NUM_OF_ROUNDS_56
  tk3 = &roundKeys[98];
#else
  tk3 = &roundKeys[130];
#endif

  // 2nd,4th, ... ,54th,56th round
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();

  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();

#ifdef ___NUM_OF_ROUNDS_56

  // 42nd,44th, ... ,54th,56th round
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();
  PERMUTATION_TK2();

#endif

}

#else

void RunEncryptionKeyScheduleTK2(uint32_t *roundKeys)
{
  uint32_t* tk2;  // used in MACRO
  uint32_t* tk3;  // used in MACRO
  uint32_t t0;    // used in MACRO
  uint32_t t1;    // used in MACRO
  uint32_t t2;    // used in MACRO
  uint32_t w0;
  uint32_t w1;

  // odd

  // load master key
  w0 = roundKeys[4];
  w1 = roundKeys[5];

  tk2 = &roundKeys[16];
#ifndef ___NUM_OF_ROUNDS_56
  tk3 = &roundKeys[96];
#else
  tk3 = &roundKeys[128];
#endif

  // 1st round
  *tk2++ = w0 ^ *tk3++;
  *tk2++ = w1 ^ *tk3++;

  tk2 += 2;
  tk3 += 2;

  // 3rd,5th, ...
#ifndef ___NUM_OF_ROUNDS_56
  for(int i=0;i<19;i++)
#else
  for(int i=0;i<27;i++)
#endif
  {
    PERMUTATION_TK2();
  }

  // even

  // load master key
  w0 = roundKeys[6];
  w1 = roundKeys[7];

  tk2 = &roundKeys[18];
#ifndef ___NUM_OF_ROUNDS_56
  tk3 = &roundKeys[98];
#else
  tk3 = &roundKeys[130];
#endif

  // 2nd,4th, ...
#ifndef ___NUM_OF_ROUNDS_56
  for(int i=0;i<20;i++)
#else
  for(int i=0;i<28;i++)
#endif
  {
    PERMUTATION_TK2();
  }

}

#endif