From fe1021a025260d95cfddc2b379078bdd322c6b9b Mon Sep 17 00:00:00 2001
From: Michal Hrusecky <Michal@Hrusecky.net>
Date: Mon, 19 Mar 2018 14:17:50 +0100
Subject: [PATCH] musl: Make hack to disable crypto functions optional

Some devices have enough space to handle strong cryptography.

Signed-off-by: Michal Hrusecky <Michal@Hrusecky.net>
---
 toolchain/Config.in                              |  7 +++++++
 toolchain/musl/Makefile                          |  4 ++++
 toolchain/musl/patches/901-crypt_size_hack.patch | 22 ++++++++++++----------
 3 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/toolchain/Config.in b/toolchain/Config.in
index 96acf1e..d844496 100644
--- a/toolchain/Config.in
+++ b/toolchain/Config.in
@@ -293,6 +293,13 @@ config USE_EXTERNAL_LIBC
 	bool
 	default y if EXTERNAL_TOOLCHAIN || NATIVE_TOOLCHAIN
 
+config CRYPTO_SIZE_HACK
+	bool
+	prompt "Strip strong cryptography from musl" if TOOLCHAINOPTS
+	default y
+	help
+	  Enable if you want to weaken cryptography and get extra space
+
 source "toolchain/binutils/Config.version"
 source "toolchain/gcc/Config.version"
 
diff --git a/toolchain/musl/Makefile b/toolchain/musl/Makefile
index 2b9312b..4433434 100644
--- a/toolchain/musl/Makefile
+++ b/toolchain/musl/Makefile
@@ -11,6 +11,10 @@ MUSL_MAKEOPTS = -C $(HOST_BUILD_DIR) \
 	DESTDIR="$(TOOLCHAIN_DIR)/" \
 	LIBCC="$(subst libgcc.a,libgcc_initial.a,$(shell $(TARGET_CC) -print-libgcc-file-name))"
 
+ifeq ($(CRYPTO_SIZE_HACK),y)
+TARGET_CFLAGS += -DSIZE_HACK=y
+endif
+
 define Host/SetToolchainInfo
 	$(SED) 's,^\(LIBC_TYPE\)=.*,\1=$(PKG_NAME),' $(TOOLCHAIN_DIR)/info.mk
 	$(SED) 's,^\(LIBC_URL\)=.*,\1=http://www.musl-libc.org/,' $(TOOLCHAIN_DIR)/info.mk
diff --git a/toolchain/musl/patches/901-crypt_size_hack.patch b/toolchain/musl/patches/901-crypt_size_hack.patch
index 8cd7b19..f286a57 100644
--- a/toolchain/musl/patches/901-crypt_size_hack.patch
+++ b/toolchain/musl/patches/901-crypt_size_hack.patch
@@ -1,15 +1,17 @@
 --- a/src/crypt/crypt_r.c
 +++ b/src/crypt/crypt_r.c
-@@ -19,12 +19,6 @@ char *__crypt_r(const char *key, const c
+@@ -19,12 +19,14 @@ char *__crypt_r(const char *key, const c
  	if (salt[0] == '$' && salt[1] && salt[2]) {
  		if (salt[1] == '1' && salt[2] == '$')
  			return __crypt_md5(key, salt, output);
--		if (salt[1] == '2' && salt[3] == '$')
--			return __crypt_blowfish(key, salt, output);
--		if (salt[1] == '5' && salt[2] == '$')
--			return __crypt_sha256(key, salt, output);
--		if (salt[1] == '6' && salt[2] == '$')
--			return __crypt_sha512(key, salt, output);
++#ifndef SIZE_HACK
+ 		if (salt[1] == '2' && salt[3] == '$')
+ 			return __crypt_blowfish(key, salt, output);
+ 		if (salt[1] == '5' && salt[2] == '$')
+ 			return __crypt_sha256(key, salt, output);
+ 		if (salt[1] == '6' && salt[2] == '$')
+ 			return __crypt_sha512(key, salt, output);
++#endif
  	}
  	return __crypt_des(key, salt, output);
  }
@@ -19,7 +21,7 @@
  #include <stdio.h>
  #include <string.h>
  #include <stdint.h>
-+#if 0
++#ifndef SIZE_HACK
  
  /* public domain sha512 implementation based on fips180-3 */
  /* >=2^64 bits messages are not supported (about 2000 peta bytes) */
@@ -34,7 +36,7 @@
  #include <string.h>
  #include <stdint.h>
  
-+#if 0
++#ifndef SIZE_HACK
  typedef uint32_t BF_word;
  typedef int32_t BF_word_signed;
  
@@ -49,7 +51,7 @@
  #include <string.h>
  #include <stdint.h>
  
-+#if 0
++#ifndef SIZE_HACK
  /* public domain sha256 implementation based on fips180-3 */
  
  struct sha256 {
-- 
2.16.2