@samitouri / QOSamiQemu / commits / e3ba2fd89c

target/mips: add Octeon SNOW3G COP2 helpers

Add helper support for the Octeon SNOW3G START and MORE selectors. The engine state and result are represented through the architectural HSH IV and DAT register banks that SNOW3G aliases for save and restore. Signed-off-by: James Hilliard <james.hilliard1@gmail.com> Message-ID: <20260608-mips-octeon-missing-insns-v2-v16-7-daef7a0d8b04@gmail.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

James Hilliard committed Jun 8, 2026 at 12:59 UTC e3ba2fd89c90c6f69023065e7958da4ee8753444
2 files changed +284
target/mips/helper.h
+2
@@ -50,6 +50,8 @@ DEF_HELPER_2(octeon_cp2_mt_gfm_xormul1, void, env, i64)
50 DEF_HELPER_1(octeon_cp2_mt_sha3_startop, void, env)
51 DEF_HELPER_2(octeon_cp2_mt_zuc_start, void, env, i64)
52 DEF_HELPER_2(octeon_cp2_mt_zuc_more, void, env, i64)
53 +DEF_HELPER_2(octeon_cp2_mt_snow3g_start, void, env, i64)
54 +DEF_HELPER_2(octeon_cp2_mt_snow3g_more, void, env, i64)
55
56 /* microMIPS functions */
57 DEF_HELPER_4(lwm, void, env, tl, tl, i32)
target/mips/tcg/octeon_crypto.c
+282
@@ -251,6 +251,11 @@ static uint32_t octeon_crypto_lo32(uint64_t value)
251 return value;
252 }
253
254 +static uint64_t octeon_crypto_pack32(uint32_t hi, uint32_t lo)
255 +{
256 + return ((uint64_t)hi << 32) | lo;
257 +}
258 +
259 static const uint8_t octeon_zuc_s0[256] = {
260 0x3e, 0x72, 0x5b, 0x47, 0xca, 0xe0, 0x00, 0x33,
261 0x04, 0xd1, 0x54, 0x98, 0x09, 0xb9, 0x6d, 0xcb,
@@ -569,6 +574,283 @@ static void octeon_zuc_more(MIPSOcteonCryptoState *crypto, uint64_t data)
574 octeon_zuc_advance_window(crypto, z2);
575 }
576
577 +static const uint8_t octeon_snow3g_sr[256] = {
578 + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
579 + 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
580 + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
581 + 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
582 + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
583 + 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
584 + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
585 + 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
586 + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
587 + 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
588 + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
589 + 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
590 + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
591 + 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
592 + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
593 + 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
594 + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
595 + 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
596 + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
597 + 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
598 + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
599 + 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
600 + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
601 + 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
602 + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
603 + 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
604 + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
605 + 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
606 + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
607 + 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
608 + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
609 + 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
610 +};
611 +
612 +static const uint8_t octeon_snow3g_sq[256] = {
613 + 0x25, 0x24, 0x73, 0x67, 0xd7, 0xae, 0x5c, 0x30,
614 + 0xa4, 0xee, 0x6e, 0xcb, 0x7d, 0xb5, 0x82, 0xdb,
615 + 0xe4, 0x8e, 0x48, 0x49, 0x4f, 0x5d, 0x6a, 0x78,
616 + 0x70, 0x88, 0xe8, 0x5f, 0x5e, 0x84, 0x65, 0xe2,
617 + 0xd8, 0xe9, 0xcc, 0xed, 0x40, 0x2f, 0x11, 0x28,
618 + 0x57, 0xd2, 0xac, 0xe3, 0x4a, 0x15, 0x1b, 0xb9,
619 + 0xb2, 0x80, 0x85, 0xa6, 0x2e, 0x02, 0x47, 0x29,
620 + 0x07, 0x4b, 0x0e, 0xc1, 0x51, 0xaa, 0x89, 0xd4,
621 + 0xca, 0x01, 0x46, 0xb3, 0xef, 0xdd, 0x44, 0x7b,
622 + 0xc2, 0x7f, 0xbe, 0xc3, 0x9f, 0x20, 0x4c, 0x64,
623 + 0x83, 0xa2, 0x68, 0x42, 0x13, 0xb4, 0x41, 0xcd,
624 + 0xba, 0xc6, 0xbb, 0x6d, 0x4d, 0x71, 0x21, 0xf4,
625 + 0x8d, 0xb0, 0xe5, 0x93, 0xfe, 0x8f, 0xe6, 0xcf,
626 + 0x43, 0x45, 0x31, 0x22, 0x37, 0x36, 0x96, 0xfa,
627 + 0xbc, 0x0f, 0x08, 0x52, 0x1d, 0x55, 0x1a, 0xc5,
628 + 0x4e, 0x23, 0x69, 0x7a, 0x92, 0xff, 0x5b, 0x5a,
629 + 0xeb, 0x9a, 0x1c, 0xa9, 0xd1, 0x7e, 0x0d, 0xfc,
630 + 0x50, 0x8a, 0xb6, 0x62, 0xf5, 0x0a, 0xf8, 0xdc,
631 + 0x03, 0x3c, 0x0c, 0x39, 0xf1, 0xb8, 0xf3, 0x3d,
632 + 0xf2, 0xd5, 0x97, 0x66, 0x81, 0x32, 0xa0, 0x00,
633 + 0x06, 0xce, 0xf6, 0xea, 0xb7, 0x17, 0xf7, 0x8c,
634 + 0x79, 0xd6, 0xa7, 0xbf, 0x8b, 0x3f, 0x1f, 0x53,
635 + 0x63, 0x75, 0x35, 0x2c, 0x60, 0xfd, 0x27, 0xd3,
636 + 0x94, 0xa5, 0x7c, 0xa1, 0x05, 0x58, 0x2d, 0xbd,
637 + 0xd9, 0xc7, 0xaf, 0x6b, 0x54, 0x0b, 0xe0, 0x38,
638 + 0x04, 0xc8, 0x9d, 0xe7, 0x14, 0xb1, 0x87, 0x9c,
639 + 0xdf, 0x6f, 0xf9, 0xda, 0x2a, 0xc4, 0x59, 0x16,
640 + 0x74, 0x91, 0xab, 0x26, 0x61, 0x76, 0x34, 0x2b,
641 + 0xad, 0x99, 0xfb, 0x72, 0xec, 0x33, 0x12, 0xde,
642 + 0x98, 0x3b, 0xc0, 0x9b, 0x3e, 0x18, 0x10, 0x3a,
643 + 0x56, 0xe1, 0x77, 0xc9, 0x1e, 0x9e, 0x95, 0xa3,
644 + 0x90, 0x19, 0xa8, 0x6c, 0x09, 0xd0, 0xf0, 0x86,
645 +};
646 +
647 +static uint8_t octeon_snow3g_mulx(uint8_t v, uint8_t c)
648 +{
649 + return (v & 0x80) ? ((v << 1) ^ c) : (v << 1);
650 +}
651 +
652 +static uint8_t octeon_snow3g_mulxpow(uint8_t v, unsigned int n, uint8_t c)
653 +{
654 + while (n-- > 0) {
655 + v = octeon_snow3g_mulx(v, c);
656 + }
657 + return v;
658 +}
659 +
660 +static uint32_t octeon_snow3g_pack32(uint8_t b0, uint8_t b1,
661 + uint8_t b2, uint8_t b3)
662 +{
663 + return ((uint32_t)b0 << 24)
664 + | ((uint32_t)b1 << 16)
665 + | ((uint32_t)b2 << 8)
666 + | b3;
667 +}
668 +
669 +static uint32_t octeon_snow3g_mulalpha(uint8_t c)
670 +{
671 + return octeon_snow3g_pack32(octeon_snow3g_mulxpow(c, 23, 0xa9),
672 + octeon_snow3g_mulxpow(c, 245, 0xa9),
673 + octeon_snow3g_mulxpow(c, 48, 0xa9),
674 + octeon_snow3g_mulxpow(c, 239, 0xa9));
675 +}
676 +
677 +static uint32_t octeon_snow3g_divalpha(uint8_t c)
678 +{
679 + return octeon_snow3g_pack32(octeon_snow3g_mulxpow(c, 16, 0xa9),
680 + octeon_snow3g_mulxpow(c, 39, 0xa9),
681 + octeon_snow3g_mulxpow(c, 6, 0xa9),
682 + octeon_snow3g_mulxpow(c, 64, 0xa9));
683 +}
684 +
685 +static uint32_t octeon_snow3g_s1(uint32_t w)
686 +{
687 + uint8_t x0 = octeon_snow3g_sr[w >> 24];
688 + uint8_t x1 = octeon_snow3g_sr[(uint8_t)(w >> 16)];
689 + uint8_t x2 = octeon_snow3g_sr[(uint8_t)(w >> 8)];
690 + uint8_t x3 = octeon_snow3g_sr[(uint8_t)w];
691 + uint8_t r0 = octeon_snow3g_mulx(x0, 0x1b) ^ x1 ^ x2 ^
692 + octeon_snow3g_mulx(x3, 0x1b) ^ x3;
693 + uint8_t r1 = octeon_snow3g_mulx(x0, 0x1b) ^ x0 ^
694 + octeon_snow3g_mulx(x1, 0x1b) ^ x2 ^ x3;
695 + uint8_t r2 = x0 ^ octeon_snow3g_mulx(x1, 0x1b) ^ x1 ^
696 + octeon_snow3g_mulx(x2, 0x1b) ^ x3;
697 + uint8_t r3 = x0 ^ x1 ^ octeon_snow3g_mulx(x2, 0x1b) ^ x2 ^
698 + octeon_snow3g_mulx(x3, 0x1b);
699 +
700 + return octeon_snow3g_pack32(r0, r1, r2, r3);
701 +}
702 +
703 +static uint32_t octeon_snow3g_s2(uint32_t w)
704 +{
705 + uint8_t x0 = octeon_snow3g_sq[w >> 24];
706 + uint8_t x1 = octeon_snow3g_sq[(uint8_t)(w >> 16)];
707 + uint8_t x2 = octeon_snow3g_sq[(uint8_t)(w >> 8)];
708 + uint8_t x3 = octeon_snow3g_sq[(uint8_t)w];
709 + uint8_t r0 = octeon_snow3g_mulx(x0, 0x69) ^ x1 ^ x2 ^
710 + octeon_snow3g_mulx(x3, 0x69) ^ x3;
711 + uint8_t r1 = octeon_snow3g_mulx(x0, 0x69) ^ x0 ^
712 + octeon_snow3g_mulx(x1, 0x69) ^ x2 ^ x3;
713 + uint8_t r2 = x0 ^ octeon_snow3g_mulx(x1, 0x69) ^ x1 ^
714 + octeon_snow3g_mulx(x2, 0x69) ^ x3;
715 + uint8_t r3 = x0 ^ x1 ^ octeon_snow3g_mulx(x2, 0x69) ^ x2 ^
716 + octeon_snow3g_mulx(x3, 0x69);
717 +
718 + return octeon_snow3g_pack32(r0, r1, r2, r3);
719 +}
720 +
721 +static uint32_t octeon_snow3g_lfsr(const MIPSOcteonCryptoState *crypto,
722 + unsigned int index)
723 +{
724 + uint64_t pair = crypto->hsh_dat[index / 2];
725 +
726 + return index & 1 ? octeon_crypto_lo32(pair) : octeon_crypto_hi32(pair);
727 +}
728 +
729 +static void octeon_snow3g_set_lfsr(MIPSOcteonCryptoState *crypto,
730 + unsigned int index, uint32_t value)
731 +{
732 + uint32_t hi = octeon_crypto_hi32(crypto->hsh_dat[index / 2]);
733 + uint32_t lo = octeon_crypto_lo32(crypto->hsh_dat[index / 2]);
734 +
735 + if (index & 1) {
736 + lo = value;
737 + } else {
738 + hi = value;
739 + }
740 + crypto->hsh_dat[index / 2] = octeon_crypto_pack32(hi, lo);
741 +}
742 +
743 +static uint32_t octeon_snow3g_fsm(const MIPSOcteonCryptoState *crypto,
744 + unsigned int index)
745 +{
746 + return crypto->hsh_iv[1 + index];
747 +}
748 +
749 +static void octeon_snow3g_set_fsm(MIPSOcteonCryptoState *crypto,
750 + unsigned int index, uint32_t value)
751 +{
752 + crypto->hsh_iv[1 + index] = value;
753 +}
754 +
755 +static uint32_t octeon_snow3g_clock_fsm(MIPSOcteonCryptoState *crypto)
756 +{
757 + uint32_t fsm0 = octeon_snow3g_fsm(crypto, 0);
758 + uint32_t fsm1 = octeon_snow3g_fsm(crypto, 1);
759 + uint32_t fsm2 = octeon_snow3g_fsm(crypto, 2);
760 + uint32_t f = (uint32_t)(octeon_snow3g_lfsr(crypto, 15) + fsm0) ^ fsm1;
761 + uint32_t r = (uint32_t)(fsm1 + (fsm2 ^ octeon_snow3g_lfsr(crypto, 5)));
762 +
763 + octeon_snow3g_set_fsm(crypto, 2, octeon_snow3g_s2(fsm1));
764 + octeon_snow3g_set_fsm(crypto, 1, octeon_snow3g_s1(fsm0));
765 + octeon_snow3g_set_fsm(crypto, 0, r);
766 + return f;
767 +}
768 +
769 +static void octeon_snow3g_clock_lfsr(MIPSOcteonCryptoState *crypto,
770 + bool init_mode, uint32_t f)
771 +{
772 + uint32_t lfsr[16];
773 + uint32_t s0;
774 + uint32_t s11;
775 + uint32_t v;
776 + int i;
777 +
778 + for (i = 0; i < 16; i++) {
779 + lfsr[i] = octeon_snow3g_lfsr(crypto, i);
780 + }
781 +
782 + s0 = lfsr[0];
783 + s11 = lfsr[11];
784 + v = (s0 << 8) ^ octeon_snow3g_mulalpha(s0 >> 24) ^
785 + lfsr[2] ^ (s11 >> 8) ^ octeon_snow3g_divalpha((uint8_t)s11);
786 +
787 + if (init_mode) {
788 + v ^= f;
789 + }
790 +
791 + for (i = 0; i < 15; i++) {
792 + octeon_snow3g_set_lfsr(crypto, i, lfsr[i + 1]);
793 + }
794 + octeon_snow3g_set_lfsr(crypto, 15, v);
795 +}
796 +
797 +static uint32_t octeon_snow3g_generate_word(MIPSOcteonCryptoState *crypto)
798 +{
799 + uint32_t f = octeon_snow3g_clock_fsm(crypto);
800 + uint32_t z = f ^ octeon_snow3g_lfsr(crypto, 0);
801 +
802 + octeon_snow3g_clock_lfsr(crypto, false, 0);
803 + return z;
804 +}
805 +
806 +static void octeon_snow3g_queue_result(MIPSOcteonCryptoState *crypto)
807 +{
808 + uint32_t z0 = octeon_snow3g_generate_word(crypto);
809 + uint32_t z1 = octeon_snow3g_generate_word(crypto);
810 +
811 + crypto->hsh_iv[0] = octeon_crypto_pack32(z0, z1);
812 +}
813 +
814 +static void octeon_snow3g_start(MIPSOcteonCryptoState *crypto, uint64_t data)
815 +{
816 + int i;
817 +
818 + for (i = 0; i < 14; i++) {
819 + octeon_snow3g_set_lfsr(crypto, i, octeon_snow3g_lfsr(crypto, i));
820 + }
821 + octeon_snow3g_set_lfsr(crypto, 14, data >> 32);
822 + octeon_snow3g_set_lfsr(crypto, 15, data);
823 + for (i = 0; i < 3; i++) {
824 + octeon_snow3g_set_fsm(crypto, i, 0);
825 + }
826 +
827 + for (i = 0; i < 32; i++) {
828 + uint32_t f = octeon_snow3g_clock_fsm(crypto);
829 +
830 + octeon_snow3g_clock_lfsr(crypto, true, f);
831 + }
832 +
833 + (void)octeon_snow3g_clock_fsm(crypto);
834 + octeon_snow3g_clock_lfsr(crypto, false, 0);
835 + octeon_snow3g_queue_result(crypto);
836 +}
837 +
838 +static void octeon_snow3g_more(MIPSOcteonCryptoState *crypto)
839 +{
840 + octeon_snow3g_queue_result(crypto);
841 +}
842 +
843 +void helper_octeon_cp2_mt_snow3g_start(CPUMIPSState *env, uint64_t value)
844 +{
845 + octeon_snow3g_start(&env->octeon_crypto, value);
846 +}
847 +
848 +void helper_octeon_cp2_mt_snow3g_more(CPUMIPSState *env, uint64_t value)
849 +{
850 + (void)value;
851 + octeon_snow3g_more(&env->octeon_crypto);
852 +}
853 +
854 void helper_octeon_cp2_mt_zuc_start(CPUMIPSState *env, uint64_t value)
855 {
856 octeon_zuc_start(&env->octeon_crypto, value);