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

selftests/net: Refactor xfrm_fill_key() to use array of structs

A TODO in net/ipsec.c asks to refactor the code in xfrm_fill_key() to
use set/map to avoid manually comparing each algorithm with the "name"
parameter passed to the function as an argument. This patch refactors
the code to create an array of structs where each struct contains the
algorithm name and its corresponding key length.

Signed-off-by: Gautam Menghani <gautammenghani201@gmail.com>
Signed-off-by: Steffen Klassert <steffen.klassert@secunet.com>

authored by

Gautam Menghani and committed by
Steffen Klassert
93d7c52a 7ebfc85e

+45 -59
+45 -59
tools/testing/selftests/net/ipsec.c
··· 58 58 #define VETH_FMT "ktst-%d" 59 59 #define VETH_LEN 12 60 60 61 + #define XFRM_ALGO_NR_KEYS 29 62 + 61 63 static int nsfd_parent = -1; 62 64 static int nsfd_childa = -1; 63 65 static int nsfd_childb = -1; ··· 76 74 const unsigned int ping_timeout = 300; 77 75 const unsigned int ping_count = 100; 78 76 const unsigned int ping_success = 80; 77 + 78 + struct xfrm_key_entry { 79 + char algo_name[35]; 80 + int key_len; 81 + }; 82 + 83 + struct xfrm_key_entry xfrm_key_entries[] = { 84 + {"digest_null", 0}, 85 + {"ecb(cipher_null)", 0}, 86 + {"cbc(des)", 64}, 87 + {"hmac(md5)", 128}, 88 + {"cmac(aes)", 128}, 89 + {"xcbc(aes)", 128}, 90 + {"cbc(cast5)", 128}, 91 + {"cbc(serpent)", 128}, 92 + {"hmac(sha1)", 160}, 93 + {"hmac(rmd160)", 160}, 94 + {"cbc(des3_ede)", 192}, 95 + {"hmac(sha256)", 256}, 96 + {"cbc(aes)", 256}, 97 + {"cbc(camellia)", 256}, 98 + {"cbc(twofish)", 256}, 99 + {"rfc3686(ctr(aes))", 288}, 100 + {"hmac(sha384)", 384}, 101 + {"cbc(blowfish)", 448}, 102 + {"hmac(sha512)", 512}, 103 + {"rfc4106(gcm(aes))-128", 160}, 104 + {"rfc4543(gcm(aes))-128", 160}, 105 + {"rfc4309(ccm(aes))-128", 152}, 106 + {"rfc4106(gcm(aes))-192", 224}, 107 + {"rfc4543(gcm(aes))-192", 224}, 108 + {"rfc4309(ccm(aes))-192", 216}, 109 + {"rfc4106(gcm(aes))-256", 288}, 110 + {"rfc4543(gcm(aes))-256", 288}, 111 + {"rfc4309(ccm(aes))-256", 280}, 112 + {"rfc7539(chacha20,poly1305)-128", 0} 113 + }; 79 114 80 115 static void randomize_buffer(void *buf, size_t buflen) 81 116 { ··· 806 767 static int xfrm_fill_key(char *name, char *buf, 807 768 size_t buf_len, unsigned int *key_len) 808 769 { 809 - /* TODO: use set/map instead */ 810 - if (strncmp(name, "digest_null", ALGO_LEN) == 0) 811 - *key_len = 0; 812 - else if (strncmp(name, "ecb(cipher_null)", ALGO_LEN) == 0) 813 - *key_len = 0; 814 - else if (strncmp(name, "cbc(des)", ALGO_LEN) == 0) 815 - *key_len = 64; 816 - else if (strncmp(name, "hmac(md5)", ALGO_LEN) == 0) 817 - *key_len = 128; 818 - else if (strncmp(name, "cmac(aes)", ALGO_LEN) == 0) 819 - *key_len = 128; 820 - else if (strncmp(name, "xcbc(aes)", ALGO_LEN) == 0) 821 - *key_len = 128; 822 - else if (strncmp(name, "cbc(cast5)", ALGO_LEN) == 0) 823 - *key_len = 128; 824 - else if (strncmp(name, "cbc(serpent)", ALGO_LEN) == 0) 825 - *key_len = 128; 826 - else if (strncmp(name, "hmac(sha1)", ALGO_LEN) == 0) 827 - *key_len = 160; 828 - else if (strncmp(name, "hmac(rmd160)", ALGO_LEN) == 0) 829 - *key_len = 160; 830 - else if (strncmp(name, "cbc(des3_ede)", ALGO_LEN) == 0) 831 - *key_len = 192; 832 - else if (strncmp(name, "hmac(sha256)", ALGO_LEN) == 0) 833 - *key_len = 256; 834 - else if (strncmp(name, "cbc(aes)", ALGO_LEN) == 0) 835 - *key_len = 256; 836 - else if (strncmp(name, "cbc(camellia)", ALGO_LEN) == 0) 837 - *key_len = 256; 838 - else if (strncmp(name, "cbc(twofish)", ALGO_LEN) == 0) 839 - *key_len = 256; 840 - else if (strncmp(name, "rfc3686(ctr(aes))", ALGO_LEN) == 0) 841 - *key_len = 288; 842 - else if (strncmp(name, "hmac(sha384)", ALGO_LEN) == 0) 843 - *key_len = 384; 844 - else if (strncmp(name, "cbc(blowfish)", ALGO_LEN) == 0) 845 - *key_len = 448; 846 - else if (strncmp(name, "hmac(sha512)", ALGO_LEN) == 0) 847 - *key_len = 512; 848 - else if (strncmp(name, "rfc4106(gcm(aes))-128", ALGO_LEN) == 0) 849 - *key_len = 160; 850 - else if (strncmp(name, "rfc4543(gcm(aes))-128", ALGO_LEN) == 0) 851 - *key_len = 160; 852 - else if (strncmp(name, "rfc4309(ccm(aes))-128", ALGO_LEN) == 0) 853 - *key_len = 152; 854 - else if (strncmp(name, "rfc4106(gcm(aes))-192", ALGO_LEN) == 0) 855 - *key_len = 224; 856 - else if (strncmp(name, "rfc4543(gcm(aes))-192", ALGO_LEN) == 0) 857 - *key_len = 224; 858 - else if (strncmp(name, "rfc4309(ccm(aes))-192", ALGO_LEN) == 0) 859 - *key_len = 216; 860 - else if (strncmp(name, "rfc4106(gcm(aes))-256", ALGO_LEN) == 0) 861 - *key_len = 288; 862 - else if (strncmp(name, "rfc4543(gcm(aes))-256", ALGO_LEN) == 0) 863 - *key_len = 288; 864 - else if (strncmp(name, "rfc4309(ccm(aes))-256", ALGO_LEN) == 0) 865 - *key_len = 280; 866 - else if (strncmp(name, "rfc7539(chacha20,poly1305)-128", ALGO_LEN) == 0) 867 - *key_len = 0; 770 + int i; 771 + 772 + for (i = 0; i < XFRM_ALGO_NR_KEYS; i++) { 773 + if (strncmp(name, xfrm_key_entries[i].algo_name, ALGO_LEN) == 0) 774 + *key_len = xfrm_key_entries[i].key_len; 775 + } 868 776 869 777 if (*key_len > buf_len) { 870 778 printk("Can't pack a key - too big for buffer");