skinny_key_schedule3.c 5.35 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 228
/******************************************************************************
 * Copyright (c) 2020, NEC Corporation.
 *
 * THIS CODE IS FURNISHED TO YOU "AS IS" WITHOUT WARRANTY OF ANY KIND.
 *
 *****************************************************************************/

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

#include "skinny.h"

#define PERMUTATION_TK3(c0Val, c1Val)                                             \
                                                                                  \
  /* permutation */                                                               \
                                                                                  \
    PERMUTATION()                                                                 \
                                                                                  \
  /* LFSR(for TK3) (x7 x6 x5 x4 x3 x2 x1 x0) -> (x0^x6 x7 x6 x5 x4 x3 x2 x1) */   \
    w0 = ((w0 >> 1) & 0x7f7f7f7f) ^                                               \
         (((w0 << 7) ^ (w0 << 1)) & 0x80808080);                                  \
    w1 = ((w1 >> 1) & 0x7f7f7f7f) ^                                               \
         (((w1 << 7) ^ (w1 << 1)) & 0x80808080);                                  \
                                                                                  \
  /* K3^AC(c0 c1) */                                                              \
  /* store */                                                                     \
    *tk3++ = w0 ^ c0Val;                                                          \
    *tk3++ = w1 ^ c1Val;                                                          \
    tk3 += 2;

#ifndef ___SKINNY_LOOP

void RunEncryptionKeyScheduleTK3(uint32_t *roundKeys)
{
  uint32_t *tk3;
  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[8];
  w1 = roundKeys[9];

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

  // 1st round
  *tk3++ = w0 ^ 0x01;
  *tk3++ = w1;
  tk3 += 2;

  // 3rd,5th, ... ,37th,39th round
  PERMUTATION_TK3(0x7, 0x000);
  PERMUTATION_TK3(0xf, 0x100);
  PERMUTATION_TK3(0xd, 0x300);
  PERMUTATION_TK3(0x7, 0x300);
  PERMUTATION_TK3(0xe, 0x100);
  PERMUTATION_TK3(0x9, 0x300);
  PERMUTATION_TK3(0x7, 0x200);
  PERMUTATION_TK3(0xd, 0x100);
  PERMUTATION_TK3(0x5, 0x300);

  PERMUTATION_TK3(0x6, 0x100);
  PERMUTATION_TK3(0x8, 0x100);
  PERMUTATION_TK3(0x1, 0x200);
  PERMUTATION_TK3(0x5, 0x000);
  PERMUTATION_TK3(0x7, 0x100);
  PERMUTATION_TK3(0xc, 0x100);
  PERMUTATION_TK3(0x1, 0x300);
  PERMUTATION_TK3(0x6, 0x000);
  PERMUTATION_TK3(0xb, 0x100);
  PERMUTATION_TK3(0xd, 0x200);

#ifdef ___NUM_OF_ROUNDS_56

  // 41td,43th, ... ,53th,55th round
  PERMUTATION_TK3(0x4, 0x300);
  PERMUTATION_TK3(0x2, 0x100);
  PERMUTATION_TK3(0x8, 0x000);
  PERMUTATION_TK3(0x2, 0x200);
  PERMUTATION_TK3(0x9, 0x000);
  PERMUTATION_TK3(0x6, 0x200);
  PERMUTATION_TK3(0x9, 0x100);
  PERMUTATION_TK3(0x5, 0x200);

#endif

  // even

  // load master key
  w0 = roundKeys[10];
  w1 = roundKeys[11];


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

  // 2nd,4th, ... ,38th,40th round
  PERMUTATION_TK3(0x3, 0x000);
  PERMUTATION_TK3(0xf, 0x000);
  PERMUTATION_TK3(0xe, 0x300);
  PERMUTATION_TK3(0xb, 0x300);
  PERMUTATION_TK3(0xf, 0x200);
  PERMUTATION_TK3(0xc, 0x300);
  PERMUTATION_TK3(0x3, 0x300);
  PERMUTATION_TK3(0xe, 0x000);
  PERMUTATION_TK3(0xa, 0x300);
  PERMUTATION_TK3(0xb, 0x200);

  PERMUTATION_TK3(0xc, 0x200);
  PERMUTATION_TK3(0x0, 0x300);
  PERMUTATION_TK3(0x2, 0x000);
  PERMUTATION_TK3(0xb, 0x000);
  PERMUTATION_TK3(0xe, 0x200);
  PERMUTATION_TK3(0x8, 0x300);
  PERMUTATION_TK3(0x3, 0x200);
  PERMUTATION_TK3(0xd, 0x000);
  PERMUTATION_TK3(0x6, 0x300);
  PERMUTATION_TK3(0xa, 0x100);

#ifdef ___NUM_OF_ROUNDS_56

  // 42nd,44th, ... ,54th,56th round
  PERMUTATION_TK3(0x9, 0x200);
  PERMUTATION_TK3(0x4, 0x200);
  PERMUTATION_TK3(0x1, 0x100);
  PERMUTATION_TK3(0x4, 0x000);
  PERMUTATION_TK3(0x3, 0x100);
  PERMUTATION_TK3(0xc, 0x000);
  PERMUTATION_TK3(0x2, 0x300);
  PERMUTATION_TK3(0xa, 0x000);

#endif

}

#else

void RunEncryptionKeyScheduleTK3(uint32_t *roundKeys, unsigned char *pRC)
{
  uint32_t *tk3;
  uint32_t t0;         // used in MACRO
  uint32_t t1;         // used in MACRO
  uint32_t t2;         // used in MACRO
  uint32_t w0;
  uint32_t w1;
  uint16_t c0;
  uint16_t c1;

  // odd
  
  // load master key
  w0 = roundKeys[8];
  w1 = roundKeys[9];

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

  // 1st round
  *tk3++ = w0 ^ 0x01;
  *tk3++ = w1;
  tk3 += 2;

  pRC += 4;
  // 3rd,5th, ...
#ifndef ___NUM_OF_ROUNDS_56
  for(int i=0;i<19;i++)
#else
  for(int i=0;i<27;i++)
#endif
  {
    c0 = *pRC++;
    c1 = *pRC++;
    c1 <<= 8;
    pRC += 2;
    PERMUTATION_TK3(c0, c1);
  }

  // even

  // load master key
  w0 = roundKeys[10];
  w1 = roundKeys[11];

#ifndef ___NUM_OF_ROUNDS_56
  pRC -= 78;
  tk3 = &roundKeys[98];
#else
  pRC -= 110;
  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
  {
    c0 = *pRC++;
    c1 = *pRC++;
    c1 <<= 8;
    pRC += 2;
    PERMUTATION_TK3(c0, c1);
  }

}

#endif