saturnin_m3.s 41.3 KB
Newer Older
lwc-tester 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 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
@ =======================================================================
@ Combined Saturnin-CTR-Cascade and Saturnin-Hash implementation for
@ ARM Cortex-M3 CPU. This implements the API described in saturnin.h.
@ =======================================================================

	.syntax	unified
	.cpu	cortex-m3
	.file	"saturnin_m3.s"
	.text

@ =======================================================================
@ void saturnin_key_expand(uint32_t *keybuf, const uint8_t *key)
@
@   Read 32-byte key (possibly unaligned) and convert it to the internal
@   bitslice-32 representation (key bytes yield sixteen 16-bit registers
@   z0 to z15; internal representation is eight 32-bit words k0..k7
@   such that k_i = z_i | (z_{i+8} << 16)).
@   The words k_i are written in keybuf[]. Then they are written again,
@   this time with the extra "key rotation". In total, sixteen 32-bit
@   words are written in keybuf[].
@ =======================================================================
	.align	1
	.global	saturnin_key_expand
	.thumb
	.thumb_func
	.type	saturnin_key_expand, %function
saturnin_key_expand:
	push	{r4, r5, r6, r7, r8, lr}

	@ Set r12 = 0x001F001F and r8 = 0x07FF07FF
	movs	r2, #0x1F
	orr	r12, r2, r2, lsl #16
	mvn	r8, r12, lsl #11

	@ Make words q0...q3
	ldr	r2, [r1]
	ldr	r3, [r1, #16]
	movs	r4, r2
	bfi	r4, r3, #16, #16
	bfc	r3, #0, #16
	orr	r5, r3, r2, lsr #16
	ldr	r2, [r1, #4]
	ldr	r3, [r1, #20]
	movs	r6, r2
	bfi	r6, r3, #16, #16
	bfc	r3, #0, #16
	orr	r7, r3, r2, lsr #16
	stm	r0!, {r4, r5, r6, r7}

	@ Apply rotations on q0..q3
	and	r2, r8, r4, lsr #5
	and	r3, r12, r4
	orr	r4, r2, r3, lsl #11
	and	r2, r8, r5, lsr #5
	and	r3, r12, r5
	orr	r5, r2, r3, lsl #11
	and	r2, r8, r6, lsr #5
	and	r3, r12, r6
	orr	r6, r2, r3, lsl #11
	and	r2, r8, r7, lsr #5
	and	r3, r12, r7
	orr	r7, r2, r3, lsl #11
	adds	r0, #16
	stm	r0!, {r4, r5, r6, r7}

	@ Make words q4...q7
	ldr	r2, [r1, #8]
	ldr	r3, [r1, #24]
	movs	r4, r2
	bfi	r4, r3, #16, #16
	bfc	r3, #0, #16
	orr	r5, r3, r2, lsr #16
	ldr	r2, [r1, #12]
	ldr	r3, [r1, #28]
	movs	r6, r2
	bfi	r6, r3, #16, #16
	bfc	r3, #0, #16
	orr	r7, r3, r2, lsr #16
	subs	r0, #32
	stm	r0!, {r4, r5, r6, r7}

	@ Apply rotations on q4..q7
	and	r2, r8, r4, lsr #5
	and	r3, r12, r4
	orr	r4, r2, r3, lsl #11
	and	r2, r8, r5, lsr #5
	and	r3, r12, r5
	orr	r5, r2, r3, lsl #11
	and	r2, r8, r6, lsr #5
	and	r3, r12, r6
	orr	r6, r2, r3, lsl #11
	and	r2, r8, r7, lsr #5
	and	r3, r12, r7
	orr	r7, r2, r3, lsl #11
	add	r0, #16
	stm	r0!, {r4, r5, r6, r7}

	pop	{r4, r5, r6, r7, r8, pc}
	.size	saturnin_key_expand, .-saturnin_key_expand

@ =======================================================================
@ void saturnin_block_encrypt(int R, const uint32_t *rc,
@                             uint32_t *keybuf, const uint8_t *buf)
@
@   Perform one block encryption:
@     R        Number of super-rounds (typically 10 or 16); must be even.
@     rc       The round constants, in usage order.
@     keybuf   The key and the rotated key.
@     buf      The input/output block (possibly unaligned).
@
@   The key and rotated key are in internal representation, as output
@   by saturnin_key_expand(). Each round constant is a 32-bit word;
@   low 16 bits are RC0, high 16 bits are RC1.
@ =======================================================================
	.align	1
	.global	saturnin_block_encrypt
	.thumb
	.thumb_func
	.type	saturnin_block_encrypt, %function
saturnin_block_encrypt:
	push	{r0, r1, r2, r3, r4, r5, r6, r7, r8, r10, r11, lr}

	@ Conventions:
	@   r0..r7    state values q0..q7
	@   [sp]      super-round counter
	@   [sp+4]    pointer to next round constant
	@   [sp+8]    pointer to key buf
	@   [sp+12]   pointer to data block
	@ Reserved registers are r9, r13 (sp) and r15 (pc).
	@ Scratch registers are r8, r10, r11, r12 and r14.

	@ Read block into q0..q7
	ldr	r0, [r3, #8]
	ldr	r1, [r3, #24]
	movs	r4, r0
	bfi	r4, r1, #16, #16
	bfc	r1, #0, #16
	orr	r5, r1, r0, lsr #16
	ldr	r0, [r3, #12]
	ldr	r1, [r3, #28]
	movs	r6, r0
	bfi	r6, r1, #16, #16
	bfc	r1, #0, #16
	orr	r7, r1, r0, lsr #16
	ldr	r0, [r3, #4]
	ldr	r1, [r3, #20]
	movs	r2, r0
	bfi	r2, r1, #16, #16
	bfc	r1, #0, #16
	orr	r12, r1, r0, lsr #16
	ldr	r0, [r3, #0]
	ldr	r1, [r3, #16]
	movs	r14, r0
	bfi	r14, r1, #16, #16
	bfc	r1, #0, #16
	orr	r1, r1, r0, lsr #16
	movs	r3, r12
	movs	r0, r14

	@ XOR with key
	ldr	r8, [sp, #8]
	ldm	r8!, {r10, r11, r12, r14}
	eors	r0, r10
	eors	r1, r11
	eors	r2, r12
	eors	r3, r14
	ldm	r8!, {r10, r11, r12, r14}
	eors	r4, r10
	eors	r5, r11
	eors	r6, r12
	eors	r7, r14

.Lsaturnin_block_encrypt_loop:
	@ ============= Even round

	@ Apply Sbox
	ands	r8, r1, r2
	eors	r8, r0, r8     @ r8 r1 r2 r3
	orrs	r10, r8, r3
	eors	r10, r1, r10   @ r8 r10 r2 r3
	orrs	r11, r10, r2
	eors	r11, r3, r11   @ r8 r10 r2 r11
	ands	r1, r10, r11
	eors	r1, r2, r1     @ r8 r10 r1 r11
	orrs	r0, r8, r1
	eors	r0, r10, r0    @ r8 r0 r1 r11
	orrs	r3, r0, r11
	eors	r3, r8, r3     @ r3 r0 r1 r11
	movs	r2, r11        @ r3 r0 r1 r2

	ands	r8, r5, r6
	eors	r8, r4, r8     @ r8 r5 r6 r7
	orrs	r10, r8, r7
	eors	r5, r5, r10    @ r8 r5 r6 r7
	orrs	r4, r5, r6
	eors	r4, r7, r4     @ r8 r5 r6 r4
	ands	r7, r5, r4
	eors	r7, r6, r7     @ r8 r5 r7 r4
	orrs	r11, r8, r7
	eors	r5, r5, r11    @ r8 r5 r7 r4
	orrs	r6, r5, r4
	eors	r6, r8, r6     @ r6 r5 r7 r4

	@ Apply MDS
	@ Initial: state is: r0 r1 r2 r3 r4 r5 r6 r7

	@ q0 ^= q4; q1 ^= q5; q2 ^= q6; q3 ^= q7;
	eors	r0, r4
	eors	r1, r5
	eors	r2, r6
	eors	r3, r7

	@ MUL(q4, q5, q6, q7);
	eors	r4, r5     @ r0 r1 r2 r3 r5 r6 r7 r4

	@ q4 ^= SW(q0); q5 ^= SW(q1); q6 ^= SW(q2); q7 ^= SW(q3);
	eors	r5, r5, r0, ror #16
	eors	r6, r6, r1, ror #16
	eors	r7, r7, r2, ror #16
	eors	r4, r4, r3, ror #16

	@ MUL(q0, q1, q2, q3);
	eors	r0, r1     @ r1 r2 r3 r0 r5 r6 r7 r4
	@ MUL(q0, q1, q2, q3);
	eors	r1, r2     @ r2 r3 r0 r1 r5 r6 r7 r4

	@ q0 ^= q4; q1 ^= q5; q2 ^= q6; q3 ^= q7;
	eors	r2, r5
	eors	r3, r6
	eors	r0, r7
	eors	r1, r4

	@ q4 ^= SW(q0); q5 ^= SW(q1); q6 ^= SW(q2); q7 ^= SW(q3);
	eors	r5, r5, r2, ror #16
	eors	r6, r6, r3, ror #16
	eors	r7, r7, r0, ror #16
	eors	r4, r4, r1, ror #16

	@ At this point, we have the following mapping:
	@   q0  in  r2
	@   q1  in  r3
	@   q2  in  r0
	@   q3  in  r1
	@   q4  in  r5
	@   q5  in  r6
	@   q6  in  r7
	@   q7  in  r4

	@ ============= Odd round, r = 1 mod 4

	@ Apply Sbox
	ands	r8, r3, r0
	eors	r8, r2, r8     @ r8 r3 r0 r1
	orrs	r10, r8, r1
	eors	r10, r3, r10   @ r8 r10 r0 r1
	orrs	r11, r10, r0
	eors	r11, r1, r11   @ r8 r10 r0 r11
	ands	r3, r10, r11
	eors	r3, r0, r3     @ r8 r10 r3 r11
	orrs	r2, r8, r3
	eors	r2, r10, r2    @ r8 r2 r3 r11
	orrs	r1, r2, r11
	eors	r1, r8, r1     @ r1 r2 r3 r11
	movs	r0, r11        @ r1 r2 r3 r0

	ands	r8, r6, r7
	eors	r8, r5, r8     @ r8 r6 r7 r4
	orrs	r10, r8, r4
	eors	r6, r6, r10    @ r8 r6 r7 r4
	orrs	r5, r6, r7
	eors	r5, r4, r5     @ r8 r6 r7 r5
	ands	r4, r6, r5
	eors	r4, r7, r4     @ r8 r6 r4 r5
	orrs	r11, r8, r4
	eors	r6, r6, r11    @ r8 r6 r4 r5
	orrs	r7, r6, r5
	eors	r7, r8, r7     @ r7 r6 r4 r5

	@ Apply SR_slice
	movw	r8, #0x3333
	movw	r10, #0xCCCC
	ands	r11, r8, r2, lsr #18
	ands	r12, r10, r2, lsr #14
	orrs	r12, r11, r12
	bfi	r2, r12, #16, #16
	ands	r11, r8, r3, lsr #18
	ands	r12, r10, r3, lsr #14
	orrs	r12, r11, r12
	bfi	r3, r12, #16, #16
	ands	r11, r8, r0, lsr #18
	ands	r12, r10, r0, lsr #14
	orrs	r12, r11, r12
	bfi	r0, r12, #16, #16
	ands	r11, r8, r1, lsr #18
	ands	r12, r10, r1, lsr #14
	orrs	r12, r11, r12
	bfi	r1, r12, #16, #16
	movw	r8, #0x1111
	movw	r10, #0xEEEE
	ands	r11, r10, r5, lsl #1      @ (x & 0x00007777) << 1 -> r11
	ands	r12, r8, r5, lsr #3       @ (x >> 3) & 0x00001111 -> r12
	orrs	r14, r11, r12
	ands	r11, r8, r5, lsr #16      @ (x >> 16) & 0x00001111 -> r11
	ands	r12, r10, r5, lsr #16     @ (x >> 16) & 0x0000EEEE -> r12
	orrs	r5, r14, r11, lsl #19
	orrs	r5, r5, r12, lsl #15
	ands	r11, r10, r6, lsl #1      @ (x & 0x00007777) << 1 -> r11
	ands	r12, r8, r6, lsr #3       @ (x >> 3) & 0x00001111 -> r12
	orrs	r14, r11, r12
	ands	r11, r8, r6, lsr #16      @ (x >> 16) & 0x00001111 -> r11
	ands	r12, r10, r6, lsr #16     @ (x >> 16) & 0x0000EEEE -> r12
	orrs	r6, r14, r11, lsl #19
	orrs	r6, r6, r12, lsl #15
	ands	r11, r10, r7, lsl #1      @ (x & 0x00007777) << 1 -> r11
	ands	r12, r8, r7, lsr #3       @ (x >> 3) & 0x00001111 -> r12
	orrs	r14, r11, r12
	ands	r11, r8, r7, lsr #16      @ (x >> 16) & 0x00001111 -> r11
	ands	r12, r10, r7, lsr #16     @ (x >> 16) & 0x0000EEEE -> r12
	orrs	r7, r14, r11, lsl #19
	orrs	r7, r7, r12, lsl #15
	ands	r11, r10, r4, lsl #1      @ (x & 0x00007777) << 1 -> r11
	ands	r12, r8, r4, lsr #3       @ (x >> 3) & 0x00001111 -> r12
	orrs	r14, r11, r12
	ands	r11, r8, r4, lsr #16      @ (x >> 16) & 0x00001111 -> r11
	ands	r12, r10, r4, lsr #16     @ (x >> 16) & 0x0000EEEE -> r12
	orrs	r4, r14, r11, lsl #19
	orrs	r4, r4, r12, lsl #15

	@ Apply MDS
	@ Initial: state is: r2 r3 r0 r1 r5 r6 r7 r4

	@ q0 ^= q4; q1 ^= q5; q2 ^= q6; q3 ^= q7;
	eors	r2, r5
	eors	r3, r6
	eors	r0, r7
	eors	r1, r4

	@ MUL(q4, q5, q6, q7);
	eors	r5, r6     @ r2 r3 r0 r1 r6 r7 r4 r5

	@ q4 ^= SW(q0); q5 ^= SW(q1); q6 ^= SW(q2); q7 ^= SW(q3);
	eors	r6, r6, r2, ror #16
	eors	r7, r7, r3, ror #16
	eors	r4, r4, r0, ror #16
	eors	r5, r5, r1, ror #16

	@ MUL(q0, q1, q2, q3);
	eors	r2, r3     @ r3 r0 r1 r2 r6 r7 r4 r5
	@ MUL(q0, q1, q2, q3);
	eors	r3, r0     @ r0 r1 r2 r3 r6 r7 r4 r5

	@ q0 ^= q4; q1 ^= q5; q2 ^= q6; q3 ^= q7;
	eors	r0, r6
	eors	r1, r7
	eors	r2, r4
	eors	r3, r5

	@ q4 ^= SW(q0); q5 ^= SW(q1); q6 ^= SW(q2); q7 ^= SW(q3);
	eors	r6, r6, r0, ror #16
	eors	r7, r7, r1, ror #16
	eors	r4, r4, r2, ror #16
	eors	r5, r5, r3, ror #16

	@ At this point, we have the following mapping:
	@   q0  in  r0
	@   q1  in  r1
	@   q2  in  r2
	@   q3  in  r3
	@   q4  in  r6
	@   q5  in  r7
	@   q6  in  r4
	@   q7  in  r5

	@ Apply SR_slice_inv
	movw	r8, #0x3333
	movw	r10, #0xCCCC
	ands	r11, r8, r0, lsr #18
	ands	r12, r10, r0, lsr #14
	orrs	r12, r11, r12
	bfi	r0, r12, #16, #16
	ands	r11, r8, r1, lsr #18
	ands	r12, r10, r1, lsr #14
	orrs	r12, r11, r12
	bfi	r1, r12, #16, #16
	ands	r11, r8, r2, lsr #18
	ands	r12, r10, r2, lsr #14
	orrs	r12, r11, r12
	bfi	r2, r12, #16, #16
	ands	r11, r8, r3, lsr #18
	ands	r12, r10, r3, lsr #14
	orrs	r12, r11, r12
	bfi	r3, r12, #16, #16
	movw	r8, #0x7777
	movw	r10, #0x8888
	ands	r11, r10, r6, lsl #3      @ (x & 0x00001111) << 3 -> r11
	ands	r12, r8, r6, lsr #1       @ (x >> 1) & 0x00007777 -> r12
	orrs	r14, r11, r12
	ands	r11, r8, r6, lsr #16      @ (x >> 16) & 0x00007777 -> r11
	ands	r12, r10, r6, lsr #16     @ (x >> 16) & 0x00008888 -> r12
	orrs	r6, r14, r11, lsl #17
	orrs	r6, r6, r12, lsl #13
	ands	r11, r10, r7, lsl #3      @ (x & 0x00001111) << 3 -> r11
	ands	r12, r8, r7, lsr #1       @ (x >> 1) & 0x00007777 -> r12
	orrs	r14, r11, r12
	ands	r11, r8, r7, lsr #16      @ (x >> 16) & 0x00007777 -> r11
	ands	r12, r10, r7, lsr #16     @ (x >> 16) & 0x00008888 -> r12
	orrs	r7, r14, r11, lsl #17
	orrs	r7, r7, r12, lsl #13
	ands	r11, r10, r4, lsl #3      @ (x & 0x00001111) << 3 -> r11
	ands	r12, r8, r4, lsr #1       @ (x >> 1) & 0x00007777 -> r12
	orrs	r14, r11, r12
	ands	r11, r8, r4, lsr #16      @ (x >> 16) & 0x00007777 -> r11
	ands	r12, r10, r4, lsr #16     @ (x >> 16) & 0x00008888 -> r12
	orrs	r4, r14, r11, lsl #17
	orrs	r4, r4, r12, lsl #13
	ands	r11, r10, r5, lsl #3      @ (x & 0x00001111) << 3 -> r11
	ands	r12, r8, r5, lsr #1       @ (x >> 1) & 0x00007777 -> r12
	orrs	r14, r11, r12
	ands	r11, r8, r5, lsr #16      @ (x >> 16) & 0x00007777 -> r11
	ands	r12, r10, r5, lsr #16     @ (x >> 16) & 0x00008888 -> r12
	orrs	r5, r14, r11, lsl #17
	orrs	r5, r5, r12, lsl #13

	@ XOR round constant
	ldr	r8, [sp, #4]
	ldm	r8!, {r10}
	str	r8, [sp, #4]
	eors	r0, r10

	@ XOR rotated key
	ldr	r8, [sp, #8]
	adds	r8, #32
	ldm	r8!, {r10, r11, r12, r14}
	eors	r0, r10
	eors	r1, r11
	eors	r2, r12
	eors	r3, r14
	ldm	r8!, {r10, r11, r12, r14}
	eors	r6, r10
	eors	r7, r11
	eors	r4, r12
	eors	r5, r14

	@ ============= Even round

	@ Apply Sbox
	ands	r8, r1, r2
	eors	r8, r0, r8     @ r8 r1 r2 r3
	orrs	r10, r8, r3
	eors	r10, r1, r10   @ r8 r10 r2 r3
	orrs	r11, r10, r2
	eors	r11, r3, r11   @ r8 r10 r2 r11
	ands	r1, r10, r11
	eors	r1, r2, r1     @ r8 r10 r1 r11
	orrs	r0, r8, r1
	eors	r0, r10, r0    @ r8 r0 r1 r11
	orrs	r3, r0, r11
	eors	r3, r8, r3     @ r3 r0 r1 r11
	movs	r2, r11        @ r3 r0 r1 r2

	ands	r8, r7, r4
	eors	r8, r6, r8     @ r8 r7 r4 r5
	orrs	r10, r8, r5
	eors	r7, r7, r10    @ r8 r7 r4 r5
	orrs	r6, r7, r4
	eors	r6, r5, r6     @ r8 r7 r4 r6
	ands	r5, r7, r6
	eors	r5, r4, r5     @ r8 r7 r5 r6
	orrs	r11, r8, r5
	eors	r7, r7, r11    @ r8 r7 r5 r6
	orrs	r4, r7, r6
	eors	r4, r8, r4     @ r4 r7 r5 r6

	@ Apply MDS
	@ Initial: state is: r0 r1 r2 r3 r6 r7 r4 r5

	@ q0 ^= q4; q1 ^= q5; q2 ^= q6; q3 ^= q7;
	eors	r0, r6
	eors	r1, r7
	eors	r2, r4
	eors	r3, r5

	@ MUL(q4, q5, q6, q7);
	eors	r6, r7     @ r0 r1 r2 r3 r7 r4 r5 r6

	@ q4 ^= SW(q0); q5 ^= SW(q1); q6 ^= SW(q2); q7 ^= SW(q3);
	eors	r7, r7, r0, ror #16
	eors	r4, r4, r1, ror #16
	eors	r5, r5, r2, ror #16
	eors	r6, r6, r3, ror #16

	@ MUL(q0, q1, q2, q3);
	eors	r0, r1     @ r1 r2 r3 r0 r7 r4 r5 r6
	@ MUL(q0, q1, q2, q3);
	eors	r1, r2     @ r2 r3 r0 r1 r7 r4 r5 r6

	@ q0 ^= q4; q1 ^= q5; q2 ^= q6; q3 ^= q7;
	eors	r2, r7
	eors	r3, r4
	eors	r0, r5
	eors	r1, r6

	@ q4 ^= SW(q0); q5 ^= SW(q1); q6 ^= SW(q2); q7 ^= SW(q3);
	eors	r7, r7, r2, ror #16
	eors	r4, r4, r3, ror #16
	eors	r5, r5, r0, ror #16
	eors	r6, r6, r1, ror #16

	@ At this point, we have the following mapping:
	@   q0  in  r2
	@   q1  in  r3
	@   q2  in  r0
	@   q3  in  r1
	@   q4  in  r7
	@   q5  in  r4
	@   q6  in  r5
	@   q7  in  r6

	@ ============= Odd round, r = 3 mod 4

	@ Apply Sbox
	ands	r8, r3, r0
	eors	r8, r2, r8     @ r8 r3 r0 r1
	orrs	r10, r8, r1
	eors	r10, r3, r10   @ r8 r10 r0 r1
	orrs	r11, r10, r0
	eors	r11, r1, r11   @ r8 r10 r0 r11
	ands	r3, r10, r11
	eors	r3, r0, r3     @ r8 r10 r3 r11
	orrs	r2, r8, r3
	eors	r2, r10, r2    @ r8 r2 r3 r11
	orrs	r1, r2, r11
	eors	r1, r8, r1     @ r1 r2 r3 r11
	movs	r0, r11        @ r1 r2 r3 r0

	ands	r8, r4, r5
	eors	r8, r7, r8     @ r8 r4 r5 r6
	orrs	r10, r8, r6
	eors	r4, r4, r10    @ r8 r4 r5 r6
	orrs	r7, r4, r5
	eors	r7, r6, r7     @ r8 r4 r5 r7
	ands	r6, r4, r7
	eors	r6, r5, r6     @ r8 r4 r6 r7
	orrs	r11, r8, r6
	eors	r4, r4, r11    @ r8 r4 r6 r7
	orrs	r5, r4, r7
	eors	r5, r8, r5     @ r5 r4 r6 r7

	@ Apply SR_sheet
	rev	r8, r2
	bfi	r2, r8, #16, #16
	rev	r8, r3
	bfi	r3, r8, #16, #16
	rev	r8, r0
	bfi	r0, r8, #16, #16
	rev	r8, r1
	bfi	r1, r8, #16, #16

	uxth	r10, r7
	lsrs	r11, r7, #20
	lsls	r7, r7, #12
	bfi	r7, r11, #16, #12
	bfi	r7, r10, #4, #12
	orr	r7, r7, r10, lsr #12

	uxth	r10, r4
	lsrs	r11, r4, #20
	lsls	r4, r4, #12
	bfi	r4, r11, #16, #12
	bfi	r4, r10, #4, #12
	orr	r4, r4, r10, lsr #12

	uxth	r10, r5
	lsrs	r11, r5, #20
	lsls	r5, r5, #12
	bfi	r5, r11, #16, #12
	bfi	r5, r10, #4, #12
	orr	r5, r5, r10, lsr #12

	uxth	r10, r6
	lsrs	r11, r6, #20
	lsls	r6, r6, #12
	bfi	r6, r11, #16, #12
	bfi	r6, r10, #4, #12
	orr	r6, r6, r10, lsr #12

	@ Apply MDS
	@ Initial: state is: r2 r3 r0 r1 r7 r4 r5 r6

	@ q0 ^= q4; q1 ^= q5; q2 ^= q6; q3 ^= q7;
	eors	r2, r7
	eors	r3, r4
	eors	r0, r5
	eors	r1, r6

	@ MUL(q4, q5, q6, q7);
	eors	r7, r4     @ r2 r3 r0 r1 r4 r5 r6 r7

	@ q4 ^= SW(q0); q5 ^= SW(q1); q6 ^= SW(q2); q7 ^= SW(q3);
	eors	r4, r4, r2, ror #16
	eors	r5, r5, r3, ror #16
	eors	r6, r6, r0, ror #16
	eors	r7, r7, r1, ror #16

	@ MUL(q0, q1, q2, q3);
	eors	r2, r3     @ r3 r0 r1 r2 r4 r5 r6 r7
	@ MUL(q0, q1, q2, q3);
	eors	r3, r0     @ r0 r1 r2 r3 r4 r5 r6 r7

	@ q0 ^= q4; q1 ^= q5; q2 ^= q6; q3 ^= q7;
	eors	r0, r4
	eors	r1, r5
	eors	r2, r6
	eors	r3, r7

	@ q4 ^= SW(q0); q5 ^= SW(q1); q6 ^= SW(q2); q7 ^= SW(q3);
	eors	r4, r4, r0, ror #16
	eors	r5, r5, r1, ror #16
	eors	r6, r6, r2, ror #16
	eors	r7, r7, r3, ror #16

	@ At this point, we have the following mapping:
	@   q0  in  r0
	@   q1  in  r1
	@   q2  in  r2
	@   q3  in  r3
	@   q4  in  r4
	@   q5  in  r5
	@   q6  in  r6
	@   q7  in  r7

	@ Apply SR_sheet_inv
	rev	r8, r0
	bfi	r0, r8, #16, #16
	rev	r8, r1
	bfi	r1, r8, #16, #16
	rev	r8, r2
	bfi	r2, r8, #16, #16
	rev	r8, r3
	bfi	r3, r8, #16, #16

	lsrs	r10, r4, #4
	lsrs	r11, r4, #28
	bfi	r4, r4, #8, #4
	lsls	r4, r4, #4
	bfi	r4, r11, #16, #4
	bfi	r4, r10, #0, #12

	lsrs	r10, r5, #4
	lsrs	r11, r5, #28
	bfi	r5, r5, #8, #4
	lsls	r5, r5, #4
	bfi	r5, r11, #16, #4
	bfi	r5, r10, #0, #12

	lsrs	r10, r6, #4
	lsrs	r11, r6, #28
	bfi	r6, r6, #8, #4
	lsls	r6, r6, #4
	bfi	r6, r11, #16, #4
	bfi	r6, r10, #0, #12

	lsrs	r10, r7, #4
	lsrs	r11, r7, #28
	bfi	r7, r7, #8, #4
	lsls	r7, r7, #4
	bfi	r7, r11, #16, #4
	bfi	r7, r10, #0, #12

	@ XOR round constant
	ldr	r8, [sp, #4]
	ldm	r8!, {r10}
	str	r8, [sp, #4]
	eors	r0, r10

	@ XOR non-rotated key
	ldr	r8, [sp, #8]
	ldm	r8!, {r10, r11, r12, r14}
	eors	r0, r10
	eors	r1, r11
	eors	r2, r12
	eors	r3, r14
	ldm	r8!, {r10, r11, r12, r14}
	eors	r4, r10
	eors	r5, r11
	eors	r6, r12
	eors	r7, r14

	@ Loop for sufficiently many rounds.
	ldr	r8, [sp]
	subs	r8, #2
	str	r8, [sp]
	bne	.Lsaturnin_block_encrypt_loop

	@ Encode back the final state.
	ldr	r8, [sp, #12]
	strh	r0, [r8]
	strh	r1, [r8, #2]
	strh	r2, [r8, #4]
	strh	r3, [r8, #6]
	strh	r4, [r8, #8]
	strh	r5, [r8, #10]
	strh	r6, [r8, #12]
	strh	r7, [r8, #14]
	lsrs	r0, r0, #16
	lsrs	r1, r1, #16
	lsrs	r2, r2, #16
	lsrs	r3, r3, #16
	lsrs	r4, r4, #16
	lsrs	r5, r5, #16
	lsrs	r6, r6, #16
	lsrs	r7, r7, #16
	strh	r0, [r8, #16]
	strh	r1, [r8, #18]
	strh	r2, [r8, #20]
	strh	r3, [r8, #22]
	strh	r4, [r8, #24]
	strh	r5, [r8, #26]
	strh	r6, [r8, #28]
	strh	r7, [r8, #30]

	pop	{r0, r1, r2, r3, r4, r5, r6, r7, r8, r10, r11, pc}
	.size	saturnin_block_encrypt, .-saturnin_block_encrypt

@ =======================================================================
@ void xor32_aligned(uint8_t *dst, const uint8_t *src)
@
@   XOR the 32 bytes from src[] into the 32 bytes at dst[]. Both src[]
@   and dst[] MUST be 32-byte aligned.
@ =======================================================================
	.align	1
	.thumb
	.thumb_func
	.type	xor32_aligned, %function
xor32_aligned:
	push	{r0, r4, r5, r6, r7, lr}   @ r0 is for stack 8-byte alignment
	ldm	r0, {r2, r3, r4, r5}
	ldm	r1!, {r6, r7, r12, r14}
	eors	r2, r6
	eors	r3, r7
	eors	r4, r12
	eors	r5, r14
	stm	r0!, {r2, r3, r4, r5}
	ldm	r0, {r2, r3, r4, r5}
	ldm	r1!, {r6, r7, r12, r14}
	eors	r2, r6
	eors	r3, r7
	eors	r4, r12
	eors	r5, r14
	stm	r0!, {r2, r3, r4, r5}
	pop	{r0, r4, r5, r6, r7, pc}
	.size	xor32_aligned, .-xor32_aligned

@ =======================================================================
@ Saturnin-CTR-Cascade (AEAD mode).
@
@ The context is the following structure (declared in saturnin.h):
@
@   typedef struct {
@           uint32_t keybuf[16];
@           uint8_t cascade[32];
@           uint8_t ctr[32];
@           uint8_t buf[32];
@           size_t ptr;
@   } saturnin_aead_context;
@
@ =======================================================================

@ =======================================================================
@ void do_cascade(saturnin_aead_context *cc, const uint32_t *rc)
@
@   Apply the Cascade for one more block. The block is cc->buf[].
@   This uses the current cascade[] field as key for a Saturnin block
@   encryption (10 super-rounds); the input of that block encryption
@   is cc->buf[]; the XOR of the output with the input block becomes the
@   new cascade[] field.
@ =======================================================================
	.align	1
	.thumb
	.thumb_func
	.type	do_cascade_aligned, %function
do_cascade_aligned:
	push	{r1, r4, r5, lr}

	@ We set r4 to the cascade[] field within the context,
	@ and r5 to the buf[] address.
	adds	r5, r0, #128
	adds	r4, r0, #64

	@ Expand the current cascade[] as a key into a stack allocated
	@ keybuf[].
	sub	sp, sp, #64
	mov	r0, sp
	movs	r1, r4
	bl	saturnin_key_expand

	@ Copy the block[] into the cascade[] field. We assume that both
	@ are aligned.
	ldm	r5!, {r0, r1, r2, r3}
	stm	r4!, {r0, r1, r2, r3}
	ldm	r5!, {r0, r1, r2, r3}
	stm	r4!, {r0, r1, r2, r3}
	subs	r5, #32
	subs	r4, #32

	@ Perform the block encryption.
	movs	r0, #10
	ldr	r1, [sp, #64]
	mov	r2, sp
	movs	r3, r4
	bl	saturnin_block_encrypt

	@ XOR the input block with the encryption output.
	movs	r0, r4
	movs	r1, r5
	bl	xor32_aligned

	@ Exit. We must add 64 for the stack-allocated keybuf[], and 4 for
	@ the r1 which was saved.
	add	sp, sp, #68
	pop	{r4, r5, pc}
	.size	do_cascade_aligned, .-do_cascade_aligned

@ =======================================================================
@ void saturnin_aead_init(saturnin_aead_context *cc,
@                 const void *key, size_t key_len)
@
@   Initialize the context with the provided key. The 'key_len' field
@   is ignored (it is up to the caller to make sure that only 32-byte
@   keys are provided).
@ =======================================================================
	.align	1
	.global	saturnin_aead_init
	.thumb
	.thumb_func
	.type	saturnin_aead_init, %function
saturnin_aead_init:
	@ Initialization is just expanding the key into the keybuf[]
	@ field of the context, which is at offset 0.
	b	saturnin_key_expand
	.size	saturnin_aead_init, .-saturnin_aead_init

@ =======================================================================
@ Precomputed round constants.
@ =======================================================================
	.align	2

	.type	.RC_10_1, %object
	.size	.RC_10_1, 40
.RC_10_1:
	.word	0x4EB026C2
	.word	0x90595303
	.word	0xAA8FE632
	.word	0xFE928A92
	.word	0x4115A419
	.word	0x93539532
	.word	0x5DB1CC4E
	.word	0x541515CA
	.word	0xBD1F55A8
	.word	0x5A6E1A0D

	.type	.RC_10_2, %object
	.size	.RC_10_2, 40
.RC_10_2:
	.word	0x4E4526B5
	.word	0xA3565FF0
	.word	0x0F8F20D8
	.word	0x0B54BEE1
	.word	0x7D1A6C9D
	.word	0x17A6280A
	.word	0xAA46C986
	.word	0xC1199062
	.word	0x182C5CDE
	.word	0xA00D53FE

	.type	.RC_10_3, %object
	.size	.RC_10_3, 40
.RC_10_3:
	.word	0x4E162698
	.word	0xB2535BA1
	.word	0x6C8F9D65
	.word	0x5816AD30
	.word	0x691FD4FA
	.word	0x6BF5BCF9
	.word	0xF8EB3525
	.word	0xB21DECFA
	.word	0x7B3DA417
	.word	0xF62C94B4

	.type	.RC_10_4, %object
	.size	.RC_10_4, 40
.RC_10_4:
	.word	0x4FAF265B
	.word	0xC5484616
	.word	0x45DCAD21
	.word	0xE08BD607
	.word	0x0504FDB8
	.word	0x1E1F5257
	.word	0x45FBC216
	.word	0xEB529B1F
	.word	0x52194E32
	.word	0x5498C018

	.type	.RC_10_5, %object
	.size	.RC_10_5, 40
.RC_10_5:
	.word	0x4FFC2676
	.word	0xD44D4247
	.word	0x26DC109C
	.word	0xB3C9C5D6
	.word	0x110145DF
	.word	0x624CC6A4
	.word	0x17563EB5
	.word	0x9856E787
	.word	0x3108B6FB
	.word	0x02B90752

	.type	.RC_16_7, %object
	.size	.RC_16_7, 64
.RC_16_7:
	.word	0x3FBA180C
	.word	0x563AB9AB
	.word	0x125EA5EF
	.word	0x859DA26C
	.word	0xB8CF779B
	.word	0x7D4DE793
	.word	0x07EFB49F
	.word	0x8D525306
	.word	0x1E08E6AB
	.word	0x41729F87
	.word	0x8C4AEF0A
	.word	0x4AA0C9A7
	.word	0xD93A95EF
	.word	0xBB00D2AF
	.word	0xB62C5BF0
	.word	0x386D94D8

	.type	.RC_16_8, %object
	.size	.RC_16_8, 64
.RC_16_8:
	.word	0x3C9B19A7
	.word	0xA9098694
	.word	0x23F878DA
	.word	0xA7B647D3
	.word	0x74FC9D78
	.word	0xEACAAE11
	.word	0x2F31A677
	.word	0x4CC8C054
	.word	0x2F51CA05
	.word	0x5268F195
	.word	0x4F5B8A2B
	.word	0xF614B4AC
	.word	0xF1D95401
	.word	0x764D2568
	.word	0x6A493611
	.word	0x8EEF9C3E

@ =======================================================================
@ void saturnin_aead_reset(saturnin_aead_context *cc,
@                 const void *nonce, size_t nonce_len)
@
@   Start a new message, with the provided nonce. Nonce length is between
@   0 and 20 bytes (inclusive); the nonce[] buffer is not necessarily
@   aligned.
@ =======================================================================
	.align	1
	.global	saturnin_aead_reset
	.thumb
	.thumb_func
	.type	saturnin_aead_reset, %function
saturnin_aead_reset:
	push	{r4, r5, r6, lr}
	adds	r0, #96   @ offset of ctr[] in the context
	movs	r4, r0
	movs	r6, r2    @ nonce_len

	@ Copy the nonce into the ctr[] buffer, padded.
	bl	memcpy

	@ Pad the nonce.
	adds	r0, r4, r6
	movs	r1, #0x80
	strb	r1, [r0]
	adds	r0, #1
	eors	r1, r1
	movs	r2, #31
	subs	r2, r6
	bl	memset

	@ Copy the ctr[] field to the cascade[] field; both are aligned.
	subs	r5, r4, #32
	ldm	r4!, {r0, r1, r2, r3}
	stm	r5!, {r0, r1, r2, r3}
	ldm	r4!, {r0, r1, r2, r3}
	stm	r5!, {r0, r1, r2, r3}

	@ Start the cascade by encrypting the initial ctr[] value, then
	@ XOR of the output with that block.
	movs	r0, #10
	adr	r1, .RC_10_2
	subs	r4, r5, #32
	subs	r2, r4, #64
	movs	r3, r4
	bl	saturnin_block_encrypt
	movs	r0, r4
	movs	r1, r5
	bl	xor32_aligned

	@ Set the ptr field to 0
	movs	r0, #0
	str	r0, [r5, #64]

	pop	{r4, r5, r6, pc}
	.size	saturnin_aead_reset, .-saturnin_aead_reset

@ =======================================================================
@ void saturnin_aead_aad_inject(saturnin_aead_context *cc,
@                 const void *aad, size_t aad_len)
@
@   Inject some associated authenticated data.
@ =======================================================================
	.align	1
	.global	saturnin_aead_aad_inject
	.thumb
	.thumb_func
	.type	saturnin_aead_aad_inject, %function
saturnin_aead_aad_inject:
	push	{r0, r4, r5, r6, r7, lr}   @ r0 for alignment

	movs	r4, r0            @ context
	movs	r5, r1            @ aad[]
	movs    r6, r2            @ aad_len
	ldr	r7, [r4, #160]    @ ptr

	@ If the buffer is not empty, try to complete it with fresh AAD.
	@ If the provided AAD is not sufficient, simply exit.
	cbz	r7, .Laad_inject_step2
	adds	r0, #128
	adds	r0, r7            @ &cc->buf[ptr]
	movs	r2, #32
	subs	r2, r7
	cmp	r2, r6
	bhi	.Laad_inject_early_exit
	adds	r5, r2
	subs	r6, r2
	bl	memcpy
	movs	r0, r4
	adr	r1, .RC_10_2
	bl	do_cascade_aligned
	movs	r7, #0

.Laad_inject_step2:
.Laad_inject_loop:
	cmp	r6, #31
	bls	.Laad_inject_exit

	@ Copy the next block into buf[]. Source data is not necessarily
	@ aligned, so we use memcpy().
	adds	r0, r4, #128
	movs	r1, r5
	movs	r2, #32
	bl	memcpy

	@ Continue the cascade.
	movs	r0, r4
	adr	r1, .RC_10_2
	bl	do_cascade_aligned

	@ Consider the 32 bytes of AAD consumed.
	adds	r5, #32
	subs	r6, #32
	b	.Laad_inject_loop

.Laad_inject_exit:
	@ Copy the remaining of the data into buf[].
	adds	r0, r4, #128
	movs	r1, r5
	movs	r2, r6
	bl	memcpy
	str	r6, [r4, #160]
	pop	{r0, r4, r5, r6, r7, pc}

.Laad_inject_early_exit:
	movs	r2, r6
	adds	r7, r6
	bl	memcpy
	str	r7, [r4, #160]
	pop	{r0, r4, r5, r6, r7, pc}
	.size	saturnin_aead_aad_inject, .-saturnin_aead_aad_inject

@ =======================================================================
@ void pad_buffer(void *buf)
@
@     r0   pointer to the 32-byte buffer (must be followed by ptr)
@
@   r0, r1, r2 and r3 may be altered arbitrarily.
@
@   This rountine extract the ptr value at address r4+32 (i.e. immediately
@   after the buffer to pad); this is the length of the data in the
@   buffer.
@ =======================================================================
	.align	1
	.thumb
	.thumb_func
	.type	pad_buffer, %function
pad_buffer:
	ldr	r1, [r0, #32]    @ ptr
	adds	r0, r1
	movs	r2, #0x80
	strb	r2, [r0]
	adds	r0, #1
	movs	r2, #31
	subs	r2, r1
	movs	r1, #0
	b	memset           @ tail call to memset
	.size	pad_buffer, .-pad_buffer

@ =======================================================================
@ void saturnin_aead_flip(saturnin_aead_context *cc)
@
@   Finish processing of AAD, start things for encryption/decryption.
@ =======================================================================
	.align	1
	.global	saturnin_aead_flip
	.thumb
	.thumb_func
	.type	saturnin_aead_flip, %function
saturnin_aead_flip:
	push	{r4, lr}

	movs	r4, r0            @ context
	adds	r0, #128
	bl	pad_buffer

	@ Process the final block.
	movs	r0, r4
	adr	r1, .RC_10_3
	bl	do_cascade_aligned

	@ Clear the ptr field.
	movs	r0, #0
	str	r0, [r4, #160]
	pop	{r4, pc}
	.size	saturnin_aead_flip, .-saturnin_aead_flip

@ =======================================================================
@ aead_run_partial: internal routine, special call convention
@
@     r2   buf (pointer &cc->buf[ptr])
@     r5   encrypt (0 = decrypt, -1 = encrypt)
@     r6   data
@     r3   number of bytes to process (0 <= r3 <= 31)
@
@   r0 and r1 may be altered arbitrarily.
@   r2 is modified (new r2 is the sum of old r2 and old r3).
@   r6 is modified (new r6 is the sum of old r6 and old r3).
@   r3 is set to 0.
@
@   Encrypt (if r5 == -1) or decrypt (if r5 == 0) some data.
@   The encryption/decryption is done in place. The r2 parameter
@   points to some emplacement within the buf[] field of the context
@   (not necessarily at the start). All r3 bytes can fit in the
@   remaining of that buffer. This function performs only buffer
@   processing (XOR, keeping the ciphertext bytes), but does not
@   call the block encryption or the cascade.
@ =======================================================================
	.align	1
	.thumb
	.thumb_func
	.type	aead_run_partial, %function
aead_run_partial:
	cbz	r3, .Laead_run_partial_exit
.Laead_run_partial_loop:
	ldrb	r0, [r2]     @ CTR-stream byte
	ldrb	r1, [r6]     @ input byte
	eors	r1, r0       @ output byte
	bics	r0, r5
	eors	r0, r1       @ ciphertext byte
	strb	r1, [r6]
	strb	r0, [r2]
	adds	r2, #1
	adds	r6, #1
	subs	r3, #1
	bne	.Laead_run_partial_loop
.Laead_run_partial_exit:
	bx	lr
	.size	aead_run_partial, .-aead_run_partial

@ =======================================================================
@ aead_run_incr_ctr: internal routine, special call convention
@
@     r4   pointer &cc->buf[0]
@
@   r0, r1, r2 and r3 may be altered arbitrarily.
@
@   Increment the counter value (from ctr[]), and copy it to the buf[]
@   array.
@ =======================================================================
	.align	1
	.thumb
	.thumb_func
	.type	aead_run_incr_ctr, %function
aead_run_incr_ctr:
	movs	r0, r4
	subs	r0, #32              @ &cc->ctr[0]
	ldm	r0!, {r1, r2, r3}
	adds	r0, #20
	stm	r0!, {r1, r2, r3}
	subs	r0, #32
	ldm	r0!, {r1, r2, r3}
	adds	r0, #20
	stm	r0!, {r1, r2, r3}
	subs	r0, #32
	ldm	r0!, {r1, r2}
	rev	r1, r1
	rev	r2, r2
	movs	r3, #0
	adds	r2, #1
	adcs	r1, r3
	rev	r1, r1
	rev	r2, r2
	subs	r0, #8
	stm	r0!, {r1, r2}
	adds	r0, #24
	stm	r0!, {r1, r2}
	bx	lr
	.size	aead_run_incr_ctr, .-aead_run_incr_ctr

@ =======================================================================
@ void saturnin_aead_run(saturnin_aead_context *cc, int encrypt,
@                        void *data, size_t data_len)
@
@   Encrypt (if encrypt != 0) or decrypt (if encrypt == 0) some data.
@   The encryption/decryption is done in place.
@ =======================================================================
	.align	1
	.global	saturnin_aead_run
	.thumb
	.thumb_func
	.type	saturnin_aead_run, %function
saturnin_aead_run:
	@ If there is no data, then exit early.
	cbnz	r3, .Lsaturnin_aead_run_step1
	bx	lr

.Lsaturnin_aead_run_step1:
	push	{r0, r4, r5, r6, r7, lr}    @ r0 is for stack alignment

	@ Save arguments into preserved registers.
	movs	r4, r0
	movs	r5, r1
	movs	r6, r2
	movs	r7, r3

	@ Normalize the 'encrypt' flag (-1 or 0).
	@ We use the fact that x|-x has its high bit set iff x != 0.
	rsbs	r1, r1, #0    @ negate r1
	orrs	r5, r1
	asrs	r5, r5, #31

	@ Also normalize r4 to point to the buf[] field (this keeps
	@ offsets low, and thus allows the use of smaller encodings for
	@ ldr and str opcodes).
	adds	r4, #128

	@ If there is already a partial block, try to complete it.
	ldr	r2, [r4, #32]
	cbz	r2, .Lsaturnin_aead_run_step2

	@ Compute the number of bytes to inject in the first partial run.
	rsbs	r3, r2, #32
	cmp	r3, r7
	it hi
	movhi	r3, r7

	@ Pointer &cc->buf[ptr] -> into r2.
	adds	r2, r4

	@ The aead_run_partial call will consume r3 data bytes.
	subs	r7, r3

	@ Process the r3 next bytes.
	bl	aead_run_partial

	@ r2 now points to the new &cc->buf[ptr]. From this we can
	@ infer the new value of ptr. If this is less than 31, then
	@ we are finished; we just write back the new value, and exit.
	movs	r3, r2
	subs	r3, r4
	cmp	r3, #31
	bls	.Lsaturnin_aead_run_exit

	@ We made a complete block, so we must run the cascade on it.
	movs	r0, r4
	subs	r0, #128
	adr	r1, .RC_10_4
	bl	do_cascade_aligned

.Lsaturnin_aead_run_step2:
	@ At this point, the buffer is empty. We process all full
	@ blocks.
.Lsaturnin_aead_run_loop:
	@ Consume 32 bytes; if the result is negative, then there are not
	@ as many remaining bytes, and we go to step 3.
	subs	r7, #32
	bmi	.Lsaturnin_aead_run_step3

	@ Generate next ctr block and encrypt it. We use a 64-bit counter;
	@ as per the specification, the counter is big-endian.
	bl	aead_run_incr_ctr

	movs	r0, #10         @ 10 super-rounds
	adr	r1, .RC_10_1    @ round constants
	movs	r2, r4
	subs	r2, #128        @ keybuf[]
	movs	r3, r4          @ buf[]
	bl	saturnin_block_encrypt

	@ Read data block, update it, and also copy the ciphertext to
	@ the buf[] array. The data block may be unaligned.
	movs	r3, #28
.Lsaturnin_aead_run_loop2:
	ldr	r0, [r4, r3]     @ CTR-stream word
	ldr	r1, [r6, r3]     @ input word
	eors	r1, r0           @ output word
	bics	r0, r5
	eors	r0, r1           @ ciphertext word
	str	r1, [r6, r3]     @ store back output word
	str	r0, [r4, r3]     @ store ciphertext word in buf
	subs	r3, #4
	bpl	.Lsaturnin_aead_run_loop2

	@ Apply the cascade on the full ciphertext block.
	movs	r0, r4
	subs	r0, #128
	adr	r1, .RC_10_4
	bl	do_cascade_aligned

	@ Loop for next block.
	adds	r6, #32
	b	.Lsaturnin_aead_run_loop

.Lsaturnin_aead_run_step3:
	@ At that point, there is less than a full block, and the buffer
	@ is empty. The number of remaining bytes to process is r7+32.
	adds	r7, #32
	movs	r3, r7
	beq	.Lsaturnin_aead_run_exit

	@ Prepare next block of CTR-stream
	bl	aead_run_incr_ctr
	movs	r0, #10         @ 10 super-rounds
	adr	r1, .RC_10_1    @ round constants
	movs	r2, r4
	subs	r2, #128        @ keybuf[]
	movs	r3, r4          @ buf[]
	bl	saturnin_block_encrypt

	@ Process the remaining bytes.
	movs	r2, r4
	movs	r3, r7
	bl	aead_run_partial

	@ Set r3 to the new value of cc->ptr.
	movs	r3, r7

.Lsaturnin_aead_run_exit:
	@ We get here when we are finished, with just cc->ptr to update.
	@ The new value of ptr is in r3.
	str	r3, [r4, #32]
	pop	{r0, r4, r5, r6, r7, pc}
	.size	saturnin_aead_run, .-saturnin_aead_run

@ =======================================================================
@ void saturnin_aead_get_tag(saturnin_aead_context *cc,
@                            void *tag, size_t tag_len)
@
@   Finish computation of the authentication tag, and copy it into
@   the provided buffer. The caller ensures that tag_len is at
@   most 32.
@ =======================================================================
	.align	1
	.global	saturnin_aead_get_tag
	.thumb
	.thumb_func
	.type	saturnin_aead_get_tag, %function
saturnin_aead_get_tag:
	push	{r4, r5, r6, lr}

	movs	r4, r0           @ context
	movs	r5, r1           @ tag
	movs    r6, r2           @ tag_len

	@ Pad the current buffer contents.
	adds	r0, #128         @ &cc->buf[0]
	bl	pad_buffer

	@ Final Cascade block.
	movs	r0, r4
	adr	r1, .RC_10_5
	bl	do_cascade_aligned

	@ Copy the tag value.
	movs	r0, r5
	adds	r1, r4, #64
	movs	r2, r6
	bl	memcpy

	pop	{r4, r5, r6, pc}
	.size	saturnin_aead_get_tag, .-saturnin_aead_get_tag

@ =======================================================================
@ int saturnin_aead_check_tag(saturnin_aead_context *cc,
@                              const void *tag, size_t tag_len)
@
@   Finish computation of the authentication tag, and compare it
@   with the provided value; the caller ensures that tag_len is at
@   most 32. Returned value is 1 on exact match, 0 otherwise.
@ =======================================================================
	.align	1
	.global	saturnin_aead_check_tag
	.thumb
	.thumb_func
	.type	saturnin_aead_check_tag, %function
saturnin_aead_check_tag:
	push	{r4, r5, r6, lr}

	movs	r4, r0           @ context
	movs	r5, r1           @ tag
	movs    r6, r2           @ tag_len

	@ Compute tag value into a stack buffer.
	sub	sp, sp, #32
	mov	r1, sp
	bl	saturnin_aead_get_tag

	@ Do the comparison.
	movs	r2, #0
	mov	r3, sp
.Lsaturnin_aead_check_tag_loop:
	subs	r6, #1
	bmi	.Lsaturnin_aead_check_tag_step2
	ldrb	r0, [r3, r6]
	ldrb	r1, [r5, r6]
	eors	r0, r1
	orrs	r2, r0
	b	.Lsaturnin_aead_check_tag_loop
.Lsaturnin_aead_check_tag_step2:
	adds	r2, #0xFF
	lsrs	r2, r2, #8
	movs	r0, #1
	subs	r0, r2

	add	sp, sp, #32
	pop	{r4, r5, r6, pc}
	.size	saturnin_aead_check_tag, .-saturnin_aead_check_tag

@ =======================================================================
@ Saturnin-Hash.
@
@ The context is the following structure (declared in saturnin.h):
@
@   typedef struct {
@           uint8_t state[32];
@           uint8_t buf[32];
@           size_t ptr;
@   } saturnin_hash_context;
@
@ =======================================================================

@ =======================================================================
@ void saturnin_hash_init(saturnin_hash_context *hc)
@
@   Initialize a context for a new hash computation.
@ =======================================================================
	.align	1
	.global	saturnin_hash_init
	.thumb
	.thumb_func
	.type	saturnin_hash_init, %function
saturnin_hash_init:
	movs	r3, #0
	str	r3, [r0, #64]
	movs	r1, #0
	movs	r2, #32
	b	memset      @ tail call to memset
	.size	saturnin_hash_init, .-saturnin_hash_init

@ =======================================================================
@ void hash_process_block(const uint8_t *key, const uint32_t *rc,
@                         const uint8_t *in, const uint8_t *out)
@
@   Compute: in XOR E(key, in) -> out
@
@   out[] may be the same buffer as key[].
@   The in[] and out[] buffers MUST be 32-bit aligned.
@ =======================================================================
	.align	1
	.thumb
	.thumb_func
	.type	hash_process_block, %function
hash_process_block:
	push	{r4, r5, r6, lr}
	sub	sp, sp, #64

	@ Save parameters.
	movs	r4, r1
	movs	r5, r2
	movs	r6, r3

	@ Expand key into stack buffer.
	movs	r1, r0
	mov	r0, sp
	bl	saturnin_key_expand

	@ Copy the input block into the output block. We do it
	@ manually because this is more efficient than calling memcpy().
	ldm	r5!, {r0, r1, r2, r3}
	stm	r6!, {r0, r1, r2, r3}
	ldm	r5!, {r0, r1, r2, r3}
	stm	r6!, {r0, r1, r2, r3}
	subs	r5, #32
	subs	r6, #32

	@ Apply the block encryption.
	movs	r0, #16
	movs	r1, r4
	mov	r2, sp
	movs	r3, r6
	bl	saturnin_block_encrypt

	@ XOR input block into output block.
	movs	r0, r6
	movs	r1, r5
	bl	xor32_aligned

	add	sp, sp, #64
	pop	{r4, r5, r6, pc}
	.size	hash_process_block, .-hash_process_block

@ =======================================================================
@ void saturnin_hash_update(saturnin_hash_context *hc,
@                           const void *data, size_t data_len)
@
@   Inject more bytes in the context.
@ =======================================================================
	.align	1
	.global	saturnin_hash_update
	.thumb
	.thumb_func
	.type	saturnin_hash_update, %function
saturnin_hash_update:
	push	{r0, r4, r5, r6, r7, lr}    @ r0 is for stack alignment

	@ Save parameters.
	movs	r4, r0      @ context
	movs	r5, r1      @ data
	movs	r6, r2      @ data_len

	@ Read current ptr. If non-zero, then complete the current block.
	ldr	r7, [r4, #64]
	tst	r7, r7
	beq	.Lsaturnin_hash_update_step2

	@ Copy bytes into the buffer.
	adds	r0, #32
	adds	r0, r7      @ destination: &hc->buf[ptr]
	movs	r1, r5      @ source: data
	movs	r2, #32
	subs	r2, r7
	cmp	r2, r6
	it hi
	movhi	r2, r6      @ length: min(32-ptr, data_len)
	adds	r5, r2      @ consume data (pointer)
	subs	r6, r2      @ consume data (length)
	adds	r7, r2      @ new ptr value
	bl	memcpy

	@ If still not a full block, exit.
	cmp	r7, #31
	bls	.Lsaturnin_hash_update_exit

	@ Process the full block.
	movs	r0, r4           @ key: current state[]
	adr	r1, .RC_16_7     @ round constants
	movs	r2, r4
	adds	r2, #32          @ in: buf[]
	movs	r3, r4           @ out: state[]
	bl	hash_process_block

.Lsaturnin_hash_update_step2:
	@ Process full blocks.
.Lsaturnin_hash_update_loop:
	subs	r6, #32
	bmi	.Lsaturnin_hash_update_step3

	@ Copy next data block into buf[], in order to make it aligned.
	adds	r4, #32
	ldr	r0, [r5]
	ldr	r1, [r5, #4]
	ldr	r2, [r5, #8]
	ldr	r3, [r5, #12]
	stm	r4!, {r0, r1, r2, r3}
	ldr	r0, [r5, #16]
	ldr	r1, [r5, #20]
	ldr	r2, [r5, #24]
	ldr	r3, [r5, #28]
	stm	r4!, {r0, r1, r2, r3}
	subs	r4, #64
	adds	r5, #32

	@ Apply the block encryption.
	movs	r0, r4
	adr	r1, .RC_16_7     @ round constants
	movs	r2, r4
	adds	r2, #32          @ in: buf[]
	movs	r3, r4           @ out: state[]
	bl	hash_process_block

	@ Loop for next block.
	b	.Lsaturnin_hash_update_loop

.Lsaturnin_hash_update_step3:
	@ At this point, there are fewer remaining bytes than a full
	@ block; number of remaining bytes is r6+32. We simply buffer
	@ them.
	adds	r6, #32
	movs	r0, r4
	adds	r0, #32
	movs	r1, r5
	movs	r2, r6
	bl	memcpy
	movs	r7, r6

.Lsaturnin_hash_update_exit:
	str	r7, [r4, #64]    @ Store back ptr
	pop	{r0, r4, r5, r6, r7, pc}
	.size	saturnin_hash_update, .-saturnin_hash_update

@ =======================================================================
@ void saturnin_hash_out(const saturnin_hash_context *hc, void *out)
@
@   Complete hash computation.
@ =======================================================================
	.align	1
	.global	saturnin_hash_out
	.thumb
	.thumb_func
	.type	saturnin_hash_out, %function
saturnin_hash_out:
	push	{r4, r5, r6, r7, lr}
	sub	sp, sp, #68  @ two 32-byte stack buffers + alignment

	@ Save parameters.
	movs	r6, r0       @ context
	movs	r7, r1       @ out

	@ Copy the current contents of buf[] into the lower stack buffer.
	movs	r1, r6
	adds	r1, #32
	mov	r0, sp
	ldm	r1!, {r2, r3, r4, r5}
	stm	r0!, {r2, r3, r4, r5}
	ldm	r1!, {r2, r3, r4, r5}
	stm	r0!, {r2, r3, r4, r5}
	@ Copy also the ptr field (pad_buffer expects it just after the block).
	ldr	r2, [r1]
	str	r2, [r0]

	@ Pad current buffer. This also reads the ptr field (located
	@ just after the buffer).
	mov	r0, sp
	bl	pad_buffer

	@ Process the padded block.
	@ Output is into a stack buffer (we need an aligned buffer, and
	@ we cannot modify the context).
	movs	r0, r6           @ key: current state[]
	adr	r1, .RC_16_8     @ round constants
	mov	r2, sp           @ in: padded input
	mov	r3, sp
	adds	r3, #32          @ output: upper stack buffer
	bl	hash_process_block

	@ Copy the process block into the output.
	movs	r0, r7
	mov	r1, sp
	adds	r1, #32
	movs	r2, #32

	add	sp, sp, #68
	pop	{r4, r5, r6, r7, lr}
	b	memcpy           @ tail call
	.size	saturnin_hash_out, .-saturnin_hash_out