Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ppp: mppe: Use SHA-1 library instead of crypto_shash

Now that a SHA-1 library API is available, use it instead of
crypto_shash. This is simpler and faster.

Signed-off-by: Eric Biggers <ebiggers@kernel.org>
Link: https://patch.msgid.link/20250815020705.23055-1-ebiggers@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Eric Biggers and committed by
Jakub Kicinski
b55f7295 1fb39d4c

+19 -92
+1 -2
drivers/net/ppp/Kconfig
··· 85 85 config PPP_MPPE 86 86 tristate "PPP MPPE compression (encryption)" 87 87 depends on PPP 88 - select CRYPTO 89 - select CRYPTO_SHA1 90 88 select CRYPTO_LIB_ARC4 89 + select CRYPTO_LIB_SHA1 91 90 help 92 91 Support for the MPPE Encryption protocol, as employed by the 93 92 Microsoft Point-to-Point Tunneling Protocol.
+18 -90
drivers/net/ppp/ppp_mppe.c
··· 43 43 */ 44 44 45 45 #include <crypto/arc4.h> 46 - #include <crypto/hash.h> 46 + #include <crypto/sha1.h> 47 47 #include <linux/err.h> 48 48 #include <linux/fips.h> 49 49 #include <linux/module.h> ··· 55 55 #include <linux/mm.h> 56 56 #include <linux/ppp_defs.h> 57 57 #include <linux/ppp-comp.h> 58 - #include <linux/scatterlist.h> 59 58 #include <linux/unaligned.h> 60 59 61 60 #include "ppp_mppe.h" ··· 66 67 MODULE_VERSION("1.0.2"); 67 68 68 69 #define SHA1_PAD_SIZE 40 69 - 70 - /* 71 - * kernel crypto API needs its arguments to be in kmalloc'd memory, not in the module 72 - * static data area. That means sha_pad needs to be kmalloc'd. 73 - */ 74 - 75 - struct sha_pad { 76 - unsigned char sha_pad1[SHA1_PAD_SIZE]; 77 - unsigned char sha_pad2[SHA1_PAD_SIZE]; 78 - }; 79 - static struct sha_pad *sha_pad; 80 - 81 - static inline void sha_pad_init(struct sha_pad *shapad) 82 - { 83 - memset(shapad->sha_pad1, 0x00, sizeof(shapad->sha_pad1)); 84 - memset(shapad->sha_pad2, 0xF2, sizeof(shapad->sha_pad2)); 85 - } 70 + static const u8 sha_pad1[SHA1_PAD_SIZE] = { 0 }; 71 + static const u8 sha_pad2[SHA1_PAD_SIZE] = { [0 ... SHA1_PAD_SIZE - 1] = 0xF2 }; 86 72 87 73 /* 88 74 * State for an MPPE (de)compressor. 89 75 */ 90 76 struct ppp_mppe_state { 91 77 struct arc4_ctx arc4; 92 - struct shash_desc *sha1; 93 - unsigned char *sha1_digest; 78 + unsigned char sha1_digest[SHA1_DIGEST_SIZE]; 94 79 unsigned char master_key[MPPE_MAX_KEY_LEN]; 95 80 unsigned char session_key[MPPE_MAX_KEY_LEN]; 96 81 unsigned keylen; /* key length in bytes */ ··· 113 130 */ 114 131 static void get_new_key_from_sha(struct ppp_mppe_state * state) 115 132 { 116 - crypto_shash_init(state->sha1); 117 - crypto_shash_update(state->sha1, state->master_key, 118 - state->keylen); 119 - crypto_shash_update(state->sha1, sha_pad->sha_pad1, 120 - sizeof(sha_pad->sha_pad1)); 121 - crypto_shash_update(state->sha1, state->session_key, 122 - state->keylen); 123 - crypto_shash_update(state->sha1, sha_pad->sha_pad2, 124 - sizeof(sha_pad->sha_pad2)); 125 - crypto_shash_final(state->sha1, state->sha1_digest); 133 + struct sha1_ctx ctx; 134 + 135 + sha1_init(&ctx); 136 + sha1_update(&ctx, state->master_key, state->keylen); 137 + sha1_update(&ctx, sha_pad1, sizeof(sha_pad1)); 138 + sha1_update(&ctx, state->session_key, state->keylen); 139 + sha1_update(&ctx, sha_pad2, sizeof(sha_pad2)); 140 + sha1_final(&ctx, state->sha1_digest); 126 141 } 127 142 128 143 /* ··· 152 171 static void *mppe_alloc(unsigned char *options, int optlen) 153 172 { 154 173 struct ppp_mppe_state *state; 155 - struct crypto_shash *shash; 156 - unsigned int digestsize; 157 174 158 175 if (optlen != CILEN_MPPE + sizeof(state->master_key) || 159 176 options[0] != CI_MPPE || options[1] != CILEN_MPPE || 160 177 fips_enabled) 161 - goto out; 178 + return NULL; 162 179 163 180 state = kzalloc(sizeof(*state), GFP_KERNEL); 164 181 if (state == NULL) 165 - goto out; 166 - 167 - 168 - shash = crypto_alloc_shash("sha1", 0, 0); 169 - if (IS_ERR(shash)) 170 - goto out_free; 171 - 172 - state->sha1 = kmalloc(sizeof(*state->sha1) + 173 - crypto_shash_descsize(shash), 174 - GFP_KERNEL); 175 - if (!state->sha1) { 176 - crypto_free_shash(shash); 177 - goto out_free; 178 - } 179 - state->sha1->tfm = shash; 180 - 181 - digestsize = crypto_shash_digestsize(shash); 182 - if (digestsize < MPPE_MAX_KEY_LEN) 183 - goto out_free; 184 - 185 - state->sha1_digest = kmalloc(digestsize, GFP_KERNEL); 186 - if (!state->sha1_digest) 187 - goto out_free; 182 + return NULL; 188 183 189 184 /* Save keys. */ 190 185 memcpy(state->master_key, &options[CILEN_MPPE], ··· 174 217 */ 175 218 176 219 return (void *)state; 177 - 178 - out_free: 179 - kfree(state->sha1_digest); 180 - if (state->sha1) { 181 - crypto_free_shash(state->sha1->tfm); 182 - kfree_sensitive(state->sha1); 183 - } 184 - kfree(state); 185 - out: 186 - return NULL; 187 220 } 188 221 189 222 /* ··· 182 235 static void mppe_free(void *arg) 183 236 { 184 237 struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg; 185 - if (state) { 186 - kfree(state->sha1_digest); 187 - crypto_free_shash(state->sha1->tfm); 188 - kfree_sensitive(state->sha1); 189 - kfree_sensitive(state); 190 - } 238 + 239 + kfree_sensitive(state); 191 240 } 192 241 193 242 /* ··· 592 649 .comp_extra = MPPE_PAD, 593 650 }; 594 651 595 - /* 596 - * ppp_mppe_init() 597 - * 598 - * Prior to allowing load, try to load the arc4 and sha1 crypto 599 - * libraries. The actual use will be allocated later, but 600 - * this way the module will fail to insmod if they aren't available. 601 - */ 602 - 603 652 static int __init ppp_mppe_init(void) 604 653 { 605 654 int answer; 606 - if (fips_enabled || !crypto_has_ahash("sha1", 0, CRYPTO_ALG_ASYNC)) 607 - return -ENODEV; 608 655 609 - sha_pad = kmalloc(sizeof(struct sha_pad), GFP_KERNEL); 610 - if (!sha_pad) 611 - return -ENOMEM; 612 - sha_pad_init(sha_pad); 656 + if (fips_enabled) 657 + return -ENODEV; 613 658 614 659 answer = ppp_register_compressor(&ppp_mppe); 615 660 616 661 if (answer == 0) 617 662 printk(KERN_INFO "PPP MPPE Compression module registered\n"); 618 - else 619 - kfree(sha_pad); 620 663 621 664 return answer; 622 665 } ··· 610 681 static void __exit ppp_mppe_cleanup(void) 611 682 { 612 683 ppp_unregister_compressor(&ppp_mppe); 613 - kfree(sha_pad); 614 684 } 615 685 616 686 module_init(ppp_mppe_init);