hostapd: add mbedtls variant
[openwrt/openwrt.git] / package / network / services / hostapd / patches / 120-mbedtls-fips186_2_prf.patch
1 From c8dba4bd750269bcc80fed3d546e2077cb4cdf0e Mon Sep 17 00:00:00 2001
2 From: Glenn Strauss <gstrauss@gluelogic.com>
3 Date: Tue, 19 Jul 2022 20:02:21 -0400
4 Subject: [PATCH 2/7] mbedtls: fips186_2_prf()
5
6 Signed-off-by: Glenn Strauss <gstrauss@gluelogic.com>
7 ---
8 hostapd/Makefile | 4 ---
9 src/crypto/crypto_mbedtls.c | 60 +++++++++++++++++++++++++++++++++++++
10 wpa_supplicant/Makefile | 4 ---
11 3 files changed, 60 insertions(+), 8 deletions(-)
12
13 --- a/hostapd/Makefile
14 +++ b/hostapd/Makefile
15 @@ -759,10 +759,6 @@ endif
16 OBJS += ../src/crypto/crypto_$(CONFIG_CRYPTO).o
17 HOBJS += ../src/crypto/crypto_$(CONFIG_CRYPTO).o
18 SOBJS += ../src/crypto/crypto_$(CONFIG_CRYPTO).o
19 -ifdef NEED_FIPS186_2_PRF
20 -OBJS += ../src/crypto/fips_prf_internal.o
21 -SHA1OBJS += ../src/crypto/sha1-internal.o
22 -endif
23 ifeq ($(CONFIG_CRYPTO), mbedtls)
24 ifdef CONFIG_DPP
25 LIBS += -lmbedx509
26 --- a/src/crypto/crypto_mbedtls.c
27 +++ b/src/crypto/crypto_mbedtls.c
28 @@ -132,6 +132,12 @@
29 #define CRYPTO_MBEDTLS_HMAC_KDF_SHA512
30 #endif
31
32 +#if defined(EAP_SIM) || defined(EAP_SIM_DYNAMIC) || defined(EAP_SERVER_SIM) \
33 + || defined(EAP_AKA) || defined(EAP_AKA_DYNAMIC) || defined(EAP_SERVER_AKA)
34 +/* EAP_SIM=y EAP_AKA=y */
35 +#define CRYPTO_MBEDTLS_FIPS186_2_PRF
36 +#endif
37 +
38 #if defined(EAP_FAST) || defined(EAP_FAST_DYNAMIC) || defined(EAP_SERVER_FAST) \
39 || defined(EAP_TEAP) || defined(EAP_TEAP_DYNAMIC) || defined(EAP_SERVER_FAST)
40 #define CRYPTO_MBEDTLS_SHA1_T_PRF
41 @@ -813,6 +819,60 @@ int sha1_t_prf(const u8 *key, size_t key
42
43 #endif /* CRYPTO_MBEDTLS_SHA1_T_PRF */
44
45 +#ifdef CRYPTO_MBEDTLS_FIPS186_2_PRF
46 +
47 +/* fips_prf_internal.c sha1-internal.c */
48 +
49 +/* used only by src/eap_common/eap_sim_common.c:eap_sim_prf()
50 + * for eap_sim_derive_keys() and eap_sim_derive_keys_reauth()
51 + * where xlen is 160 */
52 +
53 +int fips186_2_prf(const u8 *seed, size_t seed_len, u8 *x, size_t xlen)
54 +{
55 + /* FIPS 186-2 + change notice 1 */
56 +
57 + mbedtls_sha1_context ctx;
58 + u8 * const xkey = ctx.MBEDTLS_PRIVATE(buffer);
59 + u32 * const xstate = ctx.MBEDTLS_PRIVATE(state);
60 + const u32 xstate_init[] =
61 + { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
62 +
63 + mbedtls_sha1_init(&ctx);
64 + os_memcpy(xkey, seed, seed_len < 64 ? seed_len : 64);
65 +
66 + /* note: does not fill extra bytes if (xlen % 20) (SHA1_MAC_LEN) */
67 + for (; xlen >= 20; xlen -= 20) {
68 + /* XSEED_j = 0 */
69 + /* XVAL = (XKEY + XSEED_j) mod 2^b */
70 +
71 + /* w_i = G(t, XVAL) */
72 + os_memcpy(xstate, xstate_init, sizeof(xstate_init));
73 + mbedtls_internal_sha1_process(&ctx, xkey);
74 +
75 + #if __BYTE_ORDER == __LITTLE_ENDIAN
76 + xstate[0] = host_to_be32(xstate[0]);
77 + xstate[1] = host_to_be32(xstate[1]);
78 + xstate[2] = host_to_be32(xstate[2]);
79 + xstate[3] = host_to_be32(xstate[3]);
80 + xstate[4] = host_to_be32(xstate[4]);
81 + #endif
82 + os_memcpy(x, xstate, 20);
83 + if (xlen == 20) /*(done; skip prep for next loop)*/
84 + break;
85 +
86 + /* XKEY = (1 + XKEY + w_i) mod 2^b */
87 + for (u32 carry = 1, k = 20; k-- > 0; carry >>= 8)
88 + xkey[k] = (carry += xkey[k] + x[k]) & 0xff;
89 + x += 20;
90 + /* x_j = w_0|w_1 (each pair of iterations through loop)*/
91 + }
92 +
93 + mbedtls_sha1_free(&ctx);
94 + return 0;
95 +}
96 +
97 +#endif /* CRYPTO_MBEDTLS_FIPS186_2_PRF */
98 +
99 #endif /* MBEDTLS_SHA1_C */
100
101
102 --- a/wpa_supplicant/Makefile
103 +++ b/wpa_supplicant/Makefile
104 @@ -1160,10 +1160,6 @@ endif
105 OBJS += ../src/crypto/crypto_$(CONFIG_CRYPTO).o
106 OBJS_p += ../src/crypto/crypto_$(CONFIG_CRYPTO).o
107 OBJS_priv += ../src/crypto/crypto_$(CONFIG_CRYPTO).o
108 -ifdef NEED_FIPS186_2_PRF
109 -OBJS += ../src/crypto/fips_prf_internal.o
110 -SHA1OBJS += ../src/crypto/sha1-internal.o
111 -endif
112 ifeq ($(CONFIG_CRYPTO), mbedtls)
113 LIBS += -lmbedcrypto
114 LIBS_p += -lmbedcrypto