round.h 11.8 KB
Newer Older
Martin Schläffer 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 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
#ifndef ROUND_H_
#define ROUND_H_

#include "ascon.h"
#include "printstate.h"

forceinline void ROUND_LOOP(state_t* s, const uint8_t* C, const uint8_t* E) {
  uint32_t tmp0, tmp1;
  __asm__ __volatile__(
      "@.syntax_unified\n\t"
      "rbegin_%=:;\n\t"
      "ldrb %[tmp2], [%[tmp1], #0]\n\t"
      "push  {%[tmp0]}\n\t"
      "eor %[x0_l], %[x0_l], %[x4_l]\n\t"
      "eor %[x4_l], %[x4_l], %[x3_l]\n\t"
      "eor %[x2_l], %[x2_l], %[x1_l]\n\t"
      "eor %[x2_l], %[x2_l], %[tmp2]\n\t"
      "ldrb %[tmp2], [%[tmp1], #1]\n\t"
      "add %[tmp1], %[tmp1], #2\n\t"
      "movs %[tmp0], %[x2_h]\n\t"
      "push  {%[tmp1]}\n\t"
      "eor %[tmp0], %[tmp0], %[tmp2]\n\t"
      "movs %[x2_h], %[tmp0]\n\t"
      "movs %[tmp0], %[x0_l]\n\t"
      "bic %[tmp0], %[tmp0], %[x4_l]\n\t"
      "movs %[tmp1], %[x2_l]\n\t"
      "bic %[tmp1], %[tmp1], %[x1_l]\n\t"
      "eor %[x0_l], %[x0_l], %[tmp1]\n\t"
      "movs %[tmp1], %[x4_l]\n\t"
      "bic %[tmp1], %[tmp1], %[x3_l]\n\t"
      "eor %[x2_l], %[x2_l], %[tmp1]\n\t"
      "movs %[tmp2], %[x1_l]\n\t"
      "bic %[tmp2], %[tmp2], %[x0_l]\n\t"
      "eor %[tmp2], %[x4_l], %[tmp2]\n\t"
      "movs %[tmp1], %[x3_l]\n\t"
      "bic %[tmp1], %[tmp1], %[x2_l]\n\t"
      "eor %[tmp1], %[x1_l], %[tmp1]\n\t"
      "eor %[tmp0], %[x3_l], %[tmp0]\n\t"
      "eor %[tmp0], %[tmp0], %[x2_l]\n\t"
      "eor %[tmp1], %[tmp1], %[x0_l]\n\t"
      "eor %[x0_l], %[x0_l], %[tmp2]\n\t"
      "movs %[x4_l], %[x4_h]\n\t"
      "movs %[x1_l], %[x1_h]\n\t"
      "movs %[x3_l], %[x3_h]\n\t"
      "movs %[x3_h], %[tmp0]\n\t"
      "movs %[x1_h], %[tmp1]\n\t"
      "movs %[tmp0], %[x0_h]\n\t"
      "movs %[tmp1], %[x2_h]\n\t"
      "movs %[x0_h], %[x0_l]\n\t"
      "movs %[x2_h], %[x2_l]\n\t"
      "eor %[tmp0], %[tmp0], %[x4_l]\n\t"
      "eor %[x4_l], %[x4_l], %[x3_l]\n\t"
      "eor %[tmp1], %[tmp1], %[x1_l]\n\t"
      "movs %[x0_l], %[tmp0] \n\t"
      "bic %[x0_l], %[x0_l], %[x4_l]\n\t"
      "movs %[x2_l], %[tmp1] \n\t"
      "bic %[x2_l], %[x2_l], %[x1_l]\n\t"
      "eor %[tmp0], %[tmp0], %[x2_l]\n\t"
      "movs %[x2_l], %[x4_l] \n\t"
      "bic %[x2_l], %[x2_l], %[x3_l]\n\t"
      "eor %[tmp1], %[tmp1], %[x2_l]\n\t"
      "movs %[x2_l], %[x1_l]\n\t"
      "bic %[x2_l], %[x2_l], %[tmp0]\n\t"
      "eor %[x4_l], %[x4_l], %[x2_l]\n\t"
      "movs %[x2_l], %[x3_l] \n\t"
      "bic %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x1_l], %[x2_l]\n\t"
      "eor %[x3_l], %[x3_l], %[x0_l]\n\t"
      "eor %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x1_l], %[tmp0]\n\t"
      "eor %[tmp0], %[tmp0], %[x4_l]\n\t"
      "movs %[x4_h], %[tmp1]\n\t"
      "movs %[x2_l], %[x3_h]\n\t"
      "movs %[x3_h], %[x1_l]\n\t"
      "movs %[tmp1], #17\n\t"
      "movs %[x0_l], %[tmp2]\n\t"
      "ror %[x0_l], %[x0_l], %[tmp1]\n\t"
      "eor %[x0_l], %[tmp2], %[x0_l]\n\t"
      "movs %[x1_l], %[x4_l]\n\t"
      "ror %[x1_l], %[x1_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x4_l], %[x1_l]\n\t"
      "movs %[tmp1], #3\n\t"
      "ror %[x1_l], %[x1_l], %[tmp1]\n\t"
      "eor %[tmp2], %[tmp2], %[x1_l]\n\t"
      "movs %[tmp1], #4\n\t"
      "ror %[x0_l], %[x0_l], %[tmp1]\n\t"
      "eor %[x4_l], %[x4_l], %[x0_l]\n\t"
      "movs %[x1_l], %[x2_l]\n\t"
      "ror %[x1_l], %[x1_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x3_l], %[x1_l]\n\t"
      "movs %[tmp1], #3\n\t"
      "movs %[x0_l], %[x3_l]\n\t"
      "ror %[x0_l], %[x0_l], %[tmp1]\n\t"
      "eor %[x0_l], %[x2_l], %[x0_l]\n\t"
      "movs %[tmp1], #5\n\t"
      "ror %[x0_l], %[x0_l], %[tmp1]\n\t"
      "eor %[x2_l], %[x2_l], %[x0_l]\n\t"
      "ror %[x1_l], %[x1_l], %[tmp1]\n\t"
      "eor %[x3_l], %[x3_l], %[x1_l]\n\t"
      "movs %[x0_l], %[x0_h]\n\t"
      "movs %[x1_l], %[x1_h]\n\t"
      "movs %[x1_h], %[x2_l]\n\t"
      "movs %[x0_h], %[tmp2]\n\t"
      "movs %[tmp2], %[x4_h]\n\t"
      "movs %[x4_h], %[x4_l]\n\t"
      "movs %[x4_l], %[x3_h]\n\t"
      "movs %[x3_h], %[x3_l]\n\t"
      "movs %[x3_l], %[x0_l]\n\t"
      "ror %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x3_l], %[tmp0], %[x3_l]\n\t"
      "movs %[tmp1], #4\n\t"
      "movs %[x2_l], %[tmp0]\n\t"
      "ror %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x2_l], %[x0_l], %[x2_l]\n\t"
      "movs %[tmp1], #9\n\t"
      "ror %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x0_l], %[x0_l], %[x3_l]\n\t"
      "movs %[tmp1], #10\n\t"
      "ror %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[tmp0], %[tmp0], %[x2_l]\n\t"
      "movs %[tmp1], #11\n\t"
      "movs %[x2_l], %[x1_l]\n\t"
      "ror %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x2_l], %[x1_l], %[x2_l]\n\t"
      "movs %[x3_l], %[x4_l]\n\t"
      "ror %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x3_l], %[x4_l], %[x3_l]\n\t"
      "movs %[tmp1], #19\n\t"
      "ror %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x1_l], %[x3_l]\n\t"
      "movs %[tmp1], #20\n\t"
      "ror %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x4_l], %[x4_l], %[x2_l]\n\t"
      "movs %[x2_l], %[x2_h]\n\t"
      "movs %[x3_l], %[x1_h]\n\t"
      "movs %[x1_h], %[x4_l]\n\t"
      "movs %[x2_h], %[tmp0]\n\t"
      "movs %[x4_l], #2\n\t"
      "mvn %[tmp0], %[tmp2]\n\t"
      "ror %[tmp0], %[tmp0], %[x4_l]\n\t"
      "eor %[tmp0], %[x2_l], %[tmp0]\n\t"
      "movs %[x4_l], #3\n\t"
      "mvn %[tmp1], %[x2_l]\n\t"
      "ror %[tmp1], %[tmp1], %[x4_l]\n\t"
      "eor %[tmp1], %[tmp2], %[tmp1]\n\t"
      "eor %[x2_l], %[x2_l], %[tmp1]\n\t"
      "movs %[x4_l], #1\n\t"
      "pop  {%[tmp1]}\n\t"
      "ror %[tmp0], %[tmp0], %[x4_l]\n\t"
      "eor %[tmp2], %[tmp2], %[tmp0]\n\t"
      "pop  {%[tmp0]}\n\t"
      "movs %[x4_l], %[x0_h]\n\t"
      "movs %[x0_h], %[x2_h]\n\t"
      "movs %[x2_h], %[tmp2]\n\t"
      "cmp %[tmp1], %[tmp0]\n\t"
      "beq rend_%=\n\t"
      "b rbegin_%=\n\t"
      "rend_%=:;\n\t"
      : [ x0_l ] "+l"(s->w[0][0]), [ x0_h ] "+r"(s->w[0][1]),
        [ x1_l ] "+l"(s->w[1][0]), [ x1_h ] "+r"(s->w[1][1]),
        [ x2_l ] "+l"(s->w[2][0]), [ x2_h ] "+r"(s->w[2][1]),
        [ x3_l ] "+l"(s->w[3][0]), [ x3_h ] "+r"(s->w[3][1]),
        [ x4_l ] "+l"(s->w[4][0]), [ x4_h ] "+r"(s->w[4][1]), [ tmp1 ] "+l"(C),
        [ tmp0 ] "+l"(E), [ tmp2 ] "=l"(tmp1)
      :
      :);
  printstate(" round output", s);
}

forceinline void ROUND(state_t* s, uint64_t C) {
  uint32_t tmp0, tmp1, tmp2;
  __asm__ __volatile__(
      "@.syntax_unified\n\t"
      "movs %[tmp1], %[C_e]\n\t"
      "eor %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x0_l], %[x0_l], %[x4_l]\n\t"
      "eor %[x4_l], %[x4_l], %[x3_l]\n\t"
      "eor %[x2_l], %[x2_l], %[x1_l]\n\t"
      "movs %[tmp0], %[x0_l]\n\t"
      "bic %[tmp0], %[tmp0], %[x4_l]\n\t"
      "movs %[tmp1], %[x2_l]\n\t"
      "bic %[tmp1], %[tmp1], %[x1_l]\n\t"
      "eor %[x0_l], %[x0_l], %[tmp1]\n\t"
      "movs %[tmp1], %[x4_l]\n\t"
      "bic %[tmp1], %[tmp1], %[x3_l]\n\t"
      "eor %[x2_l], %[x2_l], %[tmp1]\n\t"
      "movs %[tmp2], %[x1_l]\n\t"
      "bic %[tmp2], %[tmp2], %[x0_l]\n\t"
      "eor %[tmp2], %[x4_l], %[tmp2]\n\t"
      "movs %[tmp1], %[x3_l]\n\t"
      "bic %[tmp1], %[tmp1], %[x2_l]\n\t"
      "eor %[tmp1], %[x1_l], %[tmp1]\n\t"
      "eor %[tmp0], %[x3_l], %[tmp0]\n\t"
      "eor %[tmp0], %[tmp0], %[x2_l]\n\t"
      "eor %[tmp1], %[tmp1], %[x0_l]\n\t"
      "eor %[x0_l], %[x0_l], %[tmp2]\n\t"
      "movs %[x4_l], %[x4_h]\n\t"
      "movs %[x1_l], %[x1_h]\n\t"
      "movs %[x3_l], %[x3_h]\n\t"
      "movs %[x3_h], %[tmp0]\n\t"
      "movs %[x1_h], %[tmp1]\n\t"
      "movs %[tmp0], %[x0_h]\n\t"
      "movs %[tmp1], %[x2_h]\n\t"
      "movs %[x0_h], %[x0_l]\n\t"
      "movs %[x2_h], %[x2_l]\n\t"
      "movs %[x0_l], %[C_o]\n\t"
      "eor %[tmp1], %[tmp1], %[x0_l]\n\t"
      "eor %[tmp0], %[tmp0], %[x4_l]\n\t"
      "eor %[x4_l], %[x4_l], %[x3_l]\n\t"
      "eor %[tmp1], %[tmp1], %[x1_l]\n\t"
      "movs %[x0_l], %[tmp0] \n\t"
      "bic %[x0_l], %[x0_l], %[x4_l]\n\t"
      "movs %[x2_l], %[tmp1] \n\t"
      "bic %[x2_l], %[x2_l], %[x1_l]\n\t"
      "eor %[tmp0], %[tmp0], %[x2_l]\n\t"
      "movs %[x2_l], %[x4_l] \n\t"
      "bic %[x2_l], %[x2_l], %[x3_l]\n\t"
      "eor %[tmp1], %[tmp1], %[x2_l]\n\t"
      "movs %[x2_l], %[x1_l]\n\t"
      "bic %[x2_l], %[x2_l], %[tmp0]\n\t"
      "eor %[x4_l], %[x4_l], %[x2_l]\n\t"
      "movs %[x2_l], %[x3_l] \n\t"
      "bic %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x1_l], %[x2_l]\n\t"
      "eor %[x3_l], %[x3_l], %[x0_l]\n\t"
      "eor %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x1_l], %[tmp0]\n\t"
      "eor %[tmp0], %[tmp0], %[x4_l]\n\t"
      "movs %[x4_h], %[tmp1]\n\t"
      "movs %[x2_l], %[x3_h]\n\t"
      "movs %[x3_h], %[x1_l]\n\t"
      "movs %[tmp1], #17\n\t"
      "movs %[x0_l], %[tmp2]\n\t"
      "ror %[x0_l], %[x0_l], %[tmp1]\n\t"
      "eor %[x0_l], %[tmp2], %[x0_l]\n\t"
      "movs %[x1_l], %[x4_l]\n\t"
      "ror %[x1_l], %[x1_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x4_l], %[x1_l]\n\t"
      "movs %[tmp1], #3\n\t"
      "ror %[x1_l], %[x1_l], %[tmp1]\n\t"
      "eor %[tmp2], %[tmp2], %[x1_l]\n\t"
      "movs %[tmp1], #4\n\t"
      "ror %[x0_l], %[x0_l], %[tmp1]\n\t"
      "eor %[x4_l], %[x4_l], %[x0_l]\n\t"
      "movs %[x1_l], %[x2_l]\n\t"
      "ror %[x1_l], %[x1_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x3_l], %[x1_l]\n\t"
      "movs %[tmp1], #3\n\t"
      "movs %[x0_l], %[x3_l]\n\t"
      "ror %[x0_l], %[x0_l], %[tmp1]\n\t"
      "eor %[x0_l], %[x2_l], %[x0_l]\n\t"
      "movs %[tmp1], #5\n\t"
      "ror %[x0_l], %[x0_l], %[tmp1]\n\t"
      "eor %[x2_l], %[x2_l], %[x0_l]\n\t"
      "ror %[x1_l], %[x1_l], %[tmp1]\n\t"
      "eor %[x3_l], %[x3_l], %[x1_l]\n\t"
      "movs %[x0_l], %[x0_h]\n\t"
      "movs %[x1_l], %[x1_h]\n\t"
      "movs %[x1_h], %[x2_l]\n\t"
      "movs %[x0_h], %[tmp2]\n\t"
      "movs %[tmp2], %[x4_h]\n\t"
      "movs %[x4_h], %[x4_l]\n\t"
      "movs %[x4_l], %[x3_h]\n\t"
      "movs %[x3_h], %[x3_l]\n\t"
      "movs %[x3_l], %[x0_l]\n\t"
      "ror %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x3_l], %[tmp0], %[x3_l]\n\t"
      "movs %[tmp1], #4\n\t"
      "movs %[x2_l], %[tmp0]\n\t"
      "ror %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x2_l], %[x0_l], %[x2_l]\n\t"
      "movs %[tmp1], #9\n\t"
      "ror %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x0_l], %[x0_l], %[x3_l]\n\t"
      "movs %[tmp1], #10\n\t"
      "ror %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[tmp0], %[tmp0], %[x2_l]\n\t"
      "movs %[tmp1], #11\n\t"
      "movs %[x2_l], %[x1_l]\n\t"
      "ror %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x2_l], %[x1_l], %[x2_l]\n\t"
      "movs %[x3_l], %[x4_l]\n\t"
      "ror %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x3_l], %[x4_l], %[x3_l]\n\t"
      "movs %[tmp1], #19\n\t"
      "ror %[x3_l], %[x3_l], %[tmp1]\n\t"
      "eor %[x1_l], %[x1_l], %[x3_l]\n\t"
      "movs %[tmp1], #20\n\t"
      "ror %[x2_l], %[x2_l], %[tmp1]\n\t"
      "eor %[x4_l], %[x4_l], %[x2_l]\n\t"
      "movs %[x2_l], %[x2_h]\n\t"
      "movs %[x3_l], %[x1_h]\n\t"
      "movs %[x1_h], %[x4_l]\n\t"
      "movs %[x2_h], %[tmp0]\n\t"
      "movs %[x4_l], #2\n\t"
      "mvn %[tmp0], %[tmp2]\n\t"
      "ror %[tmp0], %[tmp0], %[x4_l]\n\t"
      "eor %[tmp0], %[x2_l], %[tmp0]\n\t"
      "movs %[x4_l], #3\n\t"
      "mvn %[tmp1], %[x2_l]\n\t"
      "ror %[tmp1], %[tmp1], %[x4_l]\n\t"
      "eor %[tmp1], %[tmp2], %[tmp1]\n\t"
      "eor %[x2_l], %[x2_l], %[tmp1]\n\t"
      "movs %[x4_l], #1\n\t"
      "ror %[tmp0], %[tmp0], %[x4_l]\n\t"
      "eor %[tmp2], %[tmp2], %[tmp0]\n\t"
      "movs %[x4_l], %[x0_h]\n\t"
      "movs %[x0_h], %[x2_h]\n\t"
      "movs %[x2_h], %[tmp2]\n\t"
      : [ x0_l ] "+l"(s->w[0][0]), [ x0_h ] "+r"(s->w[0][1]),
        [ x1_l ] "+l"(s->w[1][0]), [ x1_h ] "+r"(s->w[1][1]),
        [ x2_l ] "+l"(s->w[2][0]), [ x2_h ] "+r"(s->w[2][1]),
        [ x3_l ] "+l"(s->w[3][0]), [ x3_h ] "+r"(s->w[3][1]),
        [ x4_l ] "+l"(s->w[4][0]), [ x4_h ] "+r"(s->w[4][1]),
        [ tmp0 ] "=l"(tmp0), [ tmp1 ] "=l"(tmp1), [ tmp2 ] "=l"(tmp2)
      : [ C_e ] "ri"((uint32_t)C), [ C_o ] "ri"((uint32_t)(C >> 32))
      :);
  printstate(" round output", s);
}

forceinline void PROUNDS(state_t* s, int nr) {
  ROUND_LOOP(s, constants + START(nr), constants + 24);
}

#endif /* ROUND_H_ */