150a03
diff -up libgcrypt-1.8.5/cipher/camellia-aesni-avx2-amd64.S.intel-cet libgcrypt-1.8.5/cipher/camellia-aesni-avx2-amd64.S
150a03
--- libgcrypt-1.8.5/cipher/camellia-aesni-avx2-amd64.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/cipher/camellia-aesni-avx2-amd64.S	2020-01-23 15:36:44.148972045 +0100
150a03
@@ -18,8 +18,9 @@
150a03
  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
150a03
  */
150a03
 
150a03
-#ifdef __x86_64
150a03
 #include <config.h>
150a03
+
150a03
+#ifdef __x86_64
150a03
 #if (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
150a03
      defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) && \
150a03
     defined(ENABLE_AESNI_SUPPORT) && defined(ENABLE_AVX2_SUPPORT)
150a03
diff -up libgcrypt-1.8.5/cipher/camellia-aesni-avx-amd64.S.intel-cet libgcrypt-1.8.5/cipher/camellia-aesni-avx-amd64.S
150a03
--- libgcrypt-1.8.5/cipher/camellia-aesni-avx-amd64.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/cipher/camellia-aesni-avx-amd64.S	2020-01-23 15:36:44.145972088 +0100
150a03
@@ -18,8 +18,9 @@
150a03
  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
150a03
  */
150a03
 
150a03
-#ifdef __x86_64
150a03
 #include <config.h>
150a03
+
150a03
+#ifdef __x86_64
150a03
 #if (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
150a03
      defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) && \
150a03
     defined(ENABLE_AESNI_SUPPORT) && defined(ENABLE_AVX_SUPPORT)
150a03
diff -up libgcrypt-1.8.5/cipher/chacha20-avx2-amd64.S.intel-cet libgcrypt-1.8.5/cipher/chacha20-avx2-amd64.S
150a03
--- libgcrypt-1.8.5/cipher/chacha20-avx2-amd64.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/cipher/chacha20-avx2-amd64.S	2020-01-23 15:36:16.780250066 +0100
150a03
@@ -48,6 +48,9 @@
150a03
 .globl _gcry_chacha20_amd64_avx2_blocks
150a03
 ELF(.type  _gcry_chacha20_amd64_avx2_blocks,@function;)
150a03
 _gcry_chacha20_amd64_avx2_blocks:
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 .Lchacha_blocks_avx2_local:
150a03
 	vzeroupper
150a03
 	pushq %rbx
150a03
diff -up libgcrypt-1.8.5/cipher/chacha20-sse2-amd64.S.intel-cet libgcrypt-1.8.5/cipher/chacha20-sse2-amd64.S
150a03
--- libgcrypt-1.8.5/cipher/chacha20-sse2-amd64.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/cipher/chacha20-sse2-amd64.S	2020-01-23 15:36:16.783250095 +0100
150a03
@@ -41,6 +41,9 @@
150a03
 .globl _gcry_chacha20_amd64_sse2_blocks
150a03
 ELF(.type  _gcry_chacha20_amd64_sse2_blocks,@function;)
150a03
 _gcry_chacha20_amd64_sse2_blocks:
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 .Lchacha_blocks_sse2_local:
150a03
 	pushq %rbx
150a03
 	pushq %rbp
150a03
diff -up libgcrypt-1.8.5/cipher/poly1305-avx2-amd64.S.intel-cet libgcrypt-1.8.5/cipher/poly1305-avx2-amd64.S
150a03
--- libgcrypt-1.8.5/cipher/poly1305-avx2-amd64.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/cipher/poly1305-avx2-amd64.S	2020-01-23 15:36:16.784250105 +0100
150a03
@@ -43,6 +43,9 @@
150a03
 .globl _gcry_poly1305_amd64_avx2_init_ext
150a03
 ELF(.type  _gcry_poly1305_amd64_avx2_init_ext,@function;)
150a03
 _gcry_poly1305_amd64_avx2_init_ext:
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 .Lpoly1305_init_ext_avx2_local:
150a03
 	xor %edx, %edx
150a03
 	vzeroupper
150a03
@@ -406,6 +409,9 @@ ELF(.size _gcry_poly1305_amd64_avx2_init
150a03
 .globl _gcry_poly1305_amd64_avx2_blocks
150a03
 ELF(.type  _gcry_poly1305_amd64_avx2_blocks,@function;)
150a03
 _gcry_poly1305_amd64_avx2_blocks:
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 .Lpoly1305_blocks_avx2_local:
150a03
 	vzeroupper
150a03
 	pushq %rbp
150a03
@@ -732,6 +738,9 @@ ELF(.size _gcry_poly1305_amd64_avx2_bloc
150a03
 .globl _gcry_poly1305_amd64_avx2_finish_ext
150a03
 ELF(.type  _gcry_poly1305_amd64_avx2_finish_ext,@function;)
150a03
 _gcry_poly1305_amd64_avx2_finish_ext:
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 .Lpoly1305_finish_ext_avx2_local:
150a03
 	vzeroupper
150a03
 	pushq %rbp
150a03
diff -up libgcrypt-1.8.5/cipher/poly1305-sse2-amd64.S.intel-cet libgcrypt-1.8.5/cipher/poly1305-sse2-amd64.S
150a03
--- libgcrypt-1.8.5/cipher/poly1305-sse2-amd64.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/cipher/poly1305-sse2-amd64.S	2020-01-23 15:36:16.787250134 +0100
150a03
@@ -42,6 +42,9 @@
150a03
 .globl _gcry_poly1305_amd64_sse2_init_ext
150a03
 ELF(.type  _gcry_poly1305_amd64_sse2_init_ext,@function;)
150a03
 _gcry_poly1305_amd64_sse2_init_ext:
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 .Lpoly1305_init_ext_x86_local:
150a03
 	xor %edx, %edx
150a03
 	pushq %r12
150a03
@@ -288,6 +291,9 @@ ELF(.size _gcry_poly1305_amd64_sse2_init
150a03
 .globl _gcry_poly1305_amd64_sse2_finish_ext
150a03
 ELF(.type  _gcry_poly1305_amd64_sse2_finish_ext,@function;)
150a03
 _gcry_poly1305_amd64_sse2_finish_ext:
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 .Lpoly1305_finish_ext_x86_local:
150a03
 	pushq %rbp
150a03
 	movq %rsp, %rbp
150a03
@@ -439,6 +445,9 @@ ELF(.size _gcry_poly1305_amd64_sse2_fini
150a03
 .globl _gcry_poly1305_amd64_sse2_blocks
150a03
 ELF(.type  _gcry_poly1305_amd64_sse2_blocks,@function;)
150a03
 _gcry_poly1305_amd64_sse2_blocks:
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 .Lpoly1305_blocks_x86_local:
150a03
 	pushq %rbp
150a03
 	movq %rsp, %rbp
150a03
diff -up libgcrypt-1.8.5/cipher/serpent-avx2-amd64.S.intel-cet libgcrypt-1.8.5/cipher/serpent-avx2-amd64.S
150a03
--- libgcrypt-1.8.5/cipher/serpent-avx2-amd64.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/cipher/serpent-avx2-amd64.S	2020-01-23 15:36:44.151972003 +0100
150a03
@@ -18,8 +18,9 @@
150a03
  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
150a03
  */
150a03
 
150a03
-#ifdef __x86_64
150a03
 #include <config.h>
150a03
+
150a03
+#ifdef __x86_64
150a03
 #if (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
150a03
     defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) && defined(USE_SERPENT) && \
150a03
     defined(ENABLE_AVX2_SUPPORT)
150a03
diff -up libgcrypt-1.8.5/configure.ac.intel-cet libgcrypt-1.8.5/configure.ac
150a03
--- libgcrypt-1.8.5/configure.ac.intel-cet	2019-08-29 15:00:08.000000000 +0200
150a03
+++ libgcrypt-1.8.5/configure.ac	2020-01-23 15:35:28.147774463 +0100
150a03
@@ -95,6 +95,12 @@ AH_TOP([
150a03
 AH_BOTTOM([
150a03
 #define _GCRYPT_IN_LIBGCRYPT 1
150a03
 
150a03
+/* Add .note.gnu.property section for Intel CET in assembler sources
150a03
+   when CET is enabled.  */
150a03
+#if defined(__ASSEMBLER__) && defined(__CET__)
150a03
+# include <cet.h>
150a03
+#endif
150a03
+
150a03
 /* If the configure check for endianness has been disabled, get it from
150a03
    OS macros.  This is intended for making fat binary builds on OS X.  */
150a03
 #ifdef DISABLED_ENDIAN_CHECK
150a03
diff -up libgcrypt-1.8.5/mpi/config.links.intel-cet libgcrypt-1.8.5/mpi/config.links
150a03
--- libgcrypt-1.8.5/mpi/config.links.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/mpi/config.links	2020-01-23 15:35:46.398952954 +0100
150a03
@@ -382,6 +382,16 @@ if test x"$mpi_cpu_arch" = x ; then
150a03
     mpi_cpu_arch="unknown"
150a03
 fi
150a03
 
150a03
+# Add .note.gnu.property section for Intel CET in assembler sources
150a03
+# when CET is enabled.  */
150a03
+if test x"$mpi_cpu_arch" = xx86 ; then
150a03
+    cat <<EOF >> ./mpi/asm-syntax.h
150a03
+
150a03
+#if defined(__ASSEMBLER__) && defined(__CET__)
150a03
+# include <cet.h>
150a03
+#endif
150a03
+EOF
150a03
+fi
150a03
 
150a03
 # Make sysdep.h
150a03
 echo '/* created by config.links - do not edit */' >./mpi/sysdep.h
150a03
diff -up libgcrypt-1.8.5/mpi/i386/mpih-add1.S.intel-cet libgcrypt-1.8.5/mpi/i386/mpih-add1.S
150a03
--- libgcrypt-1.8.5/mpi/i386/mpih-add1.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/mpi/i386/mpih-add1.S	2020-01-23 15:37:40.470175379 +0100
150a03
@@ -52,6 +52,10 @@ C_SYMBOL_NAME(_gcry_mpih_add_n:)
150a03
 	movl 20(%esp),%edx		/* s2_ptr */
150a03
 	movl 24(%esp),%ecx		/* size */
150a03
 
150a03
+#if defined __CET__ && (__CET__ & 1) != 0
150a03
+	pushl	%ebx
150a03
+#endif
150a03
+
150a03
 	movl	%ecx,%eax
150a03
 	shrl	$3,%ecx 		/* compute count for unrolled loop */
150a03
 	negl	%eax
150a03
@@ -63,6 +67,9 @@ C_SYMBOL_NAME(_gcry_mpih_add_n:)
150a03
 	subl	%eax,%esi		/* ... by a constant when we ... */
150a03
 	subl	%eax,%edx		/* ... enter the loop */
150a03
 	shrl	$2,%eax 		/* restore previous value */
150a03
+#if defined __CET__ && (__CET__ & 1) != 0
150a03
+	leal	-4(,%eax,4),%ebx	/* Count for 4-byte endbr32 */
150a03
+#endif
150a03
 #ifdef PIC
150a03
 /* Calculate start address in loop for PIC.  Due to limitations in some
150a03
    assemblers, Loop-L0-3 cannot be put into the leal */
150a03
@@ -75,29 +82,53 @@ L0:	leal	(%eax,%eax,8),%eax
150a03
 /* Calculate start address in loop for non-PIC.  */
150a03
 	leal	(Loop - 3)(%eax,%eax,8),%eax
150a03
 #endif
150a03
+#if defined __CET__ && (__CET__ & 1) != 0
150a03
+	addl	%ebx,%eax		/* Adjust for endbr32 */
150a03
+#endif
150a03
 	jmp	*%eax			/* jump into loop */
150a03
 	ALIGN (3)
150a03
 Loop:	movl	(%esi),%eax
150a03
 	adcl	(%edx),%eax
150a03
 	movl	%eax,(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	4(%esi),%eax
150a03
 	adcl	4(%edx),%eax
150a03
 	movl	%eax,4(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	8(%esi),%eax
150a03
 	adcl	8(%edx),%eax
150a03
 	movl	%eax,8(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	12(%esi),%eax
150a03
 	adcl	12(%edx),%eax
150a03
 	movl	%eax,12(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	16(%esi),%eax
150a03
 	adcl	16(%edx),%eax
150a03
 	movl	%eax,16(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	20(%esi),%eax
150a03
 	adcl	20(%edx),%eax
150a03
 	movl	%eax,20(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	24(%esi),%eax
150a03
 	adcl	24(%edx),%eax
150a03
 	movl	%eax,24(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	28(%esi),%eax
150a03
 	adcl	28(%edx),%eax
150a03
 	movl	%eax,28(%edi)
150a03
@@ -110,6 +141,10 @@ Loop:	movl	(%esi),%eax
150a03
 	sbbl	%eax,%eax
150a03
 	negl	%eax
150a03
 
150a03
+#if defined __CET__ && (__CET__ & 1) != 0
150a03
+	popl	%ebx
150a03
+#endif
150a03
+
150a03
 	popl %esi
150a03
 	popl %edi
150a03
 	ret
150a03
diff -up libgcrypt-1.8.5/mpi/i386/mpih-sub1.S.intel-cet libgcrypt-1.8.5/mpi/i386/mpih-sub1.S
150a03
--- libgcrypt-1.8.5/mpi/i386/mpih-sub1.S.intel-cet	2017-11-23 19:16:58.000000000 +0100
150a03
+++ libgcrypt-1.8.5/mpi/i386/mpih-sub1.S	2020-01-23 15:37:40.472175351 +0100
150a03
@@ -53,6 +53,10 @@ C_SYMBOL_NAME(_gcry_mpih_sub_n:)
150a03
 	movl 20(%esp),%edx		/* s2_ptr */
150a03
 	movl 24(%esp),%ecx		/* size */
150a03
 
150a03
+#if defined __CET__ && (__CET__ & 1) != 0
150a03
+	pushl	%ebx
150a03
+#endif
150a03
+
150a03
 	movl	%ecx,%eax
150a03
 	shrl	$3,%ecx 		/* compute count for unrolled loop */
150a03
 	negl	%eax
150a03
@@ -64,6 +68,9 @@ C_SYMBOL_NAME(_gcry_mpih_sub_n:)
150a03
 	subl	%eax,%esi		/* ... by a constant when we ... */
150a03
 	subl	%eax,%edx		/* ... enter the loop */
150a03
 	shrl	$2,%eax 		/* restore previous value */
150a03
+#if defined __CET__ && (__CET__ & 1) != 0
150a03
+	leal	-4(,%eax,4),%ebx	/* Count for 4-byte endbr32 */
150a03
+#endif
150a03
 #ifdef PIC
150a03
 /* Calculate start address in loop for PIC.  Due to limitations in some
150a03
    assemblers, Loop-L0-3 cannot be put into the leal */
150a03
@@ -76,29 +83,53 @@ L0:	leal	(%eax,%eax,8),%eax
150a03
 /* Calculate start address in loop for non-PIC.  */
150a03
 	leal	(Loop - 3)(%eax,%eax,8),%eax
150a03
 #endif
150a03
+#if defined __CET__ && (__CET__ & 1) != 0
150a03
+	addl	%ebx,%eax		/* Adjust for endbr32 */
150a03
+#endif
150a03
 	jmp	*%eax			/* jump into loop */
150a03
 	ALIGN (3)
150a03
 Loop:	movl	(%esi),%eax
150a03
 	sbbl	(%edx),%eax
150a03
 	movl	%eax,(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	4(%esi),%eax
150a03
 	sbbl	4(%edx),%eax
150a03
 	movl	%eax,4(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	8(%esi),%eax
150a03
 	sbbl	8(%edx),%eax
150a03
 	movl	%eax,8(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	12(%esi),%eax
150a03
 	sbbl	12(%edx),%eax
150a03
 	movl	%eax,12(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	16(%esi),%eax
150a03
 	sbbl	16(%edx),%eax
150a03
 	movl	%eax,16(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	20(%esi),%eax
150a03
 	sbbl	20(%edx),%eax
150a03
 	movl	%eax,20(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	24(%esi),%eax
150a03
 	sbbl	24(%edx),%eax
150a03
 	movl	%eax,24(%edi)
150a03
+#ifdef _CET_ENDBR
150a03
+	_CET_ENDBR
150a03
+#endif
150a03
 	movl	28(%esi),%eax
150a03
 	sbbl	28(%edx),%eax
150a03
 	movl	%eax,28(%edi)
150a03
@@ -111,6 +142,10 @@ Loop:	movl	(%esi),%eax
150a03
 	sbbl	%eax,%eax
150a03
 	negl	%eax
150a03
 
150a03
+#if defined __CET__ && (__CET__ & 1) != 0
150a03
+	popl	%ebx
150a03
+#endif
150a03
+
150a03
 	popl %esi
150a03
 	popl %edi
150a03
 	ret