|
21 | 21 | #include <openssl/evp.h> |
22 | 22 | #include <openssl/hmac.h> |
23 | 23 | #include <openssl/kdf.h> |
24 | | - |
25 | | -#ifdef CONFIG_OPENSSL_3 |
26 | 24 | #include <openssl/core_names.h> |
27 | 25 | #include <openssl/params.h> |
28 | 26 | #endif |
29 | | -#endif |
30 | 27 |
|
31 | 28 | #ifdef CONFIG_KEYUTILS |
32 | 29 | #include <keyutils.h> |
@@ -812,149 +809,7 @@ static int derive_tls_key(int version, unsigned char cipher, |
812 | 809 |
|
813 | 810 | return key_len; |
814 | 811 | } |
815 | | -#endif /* CONFIG_OPENSSL */ |
816 | | - |
817 | | -#ifdef CONFIG_OPENSSL_1 |
818 | | -static DEFINE_CLEANUP_FUNC(cleanup_hmac_ctx, HMAC_CTX *, HMAC_CTX_free) |
819 | | -#define _cleanup_hmac_ctx_ __cleanup__(cleanup_hmac_ctx) |
820 | | - |
821 | | -int nvme_gen_dhchap_key(char *hostnqn, enum nvme_hmac_alg hmac, |
822 | | - unsigned int key_len, unsigned char *secret, |
823 | | - unsigned char *key) |
824 | | -{ |
825 | | - const char hmac_seed[] = "NVMe-over-Fabrics"; |
826 | | - _cleanup_hmac_ctx_ HMAC_CTX *hmac_ctx = NULL; |
827 | | - const EVP_MD *md; |
828 | | - |
829 | | - hmac_ctx = HMAC_CTX_new(); |
830 | | - if (!hmac_ctx) { |
831 | | - errno = ENOMEM; |
832 | | - return -1; |
833 | | - } |
834 | | - |
835 | | - switch (hmac) { |
836 | | - case NVME_HMAC_ALG_NONE: |
837 | | - memcpy(key, secret, key_len); |
838 | | - return 0; |
839 | | - case NVME_HMAC_ALG_SHA2_256: |
840 | | - md = EVP_sha256(); |
841 | | - break; |
842 | | - case NVME_HMAC_ALG_SHA2_384: |
843 | | - md = EVP_sha384(); |
844 | | - break; |
845 | | - case NVME_HMAC_ALG_SHA2_512: |
846 | | - md = EVP_sha512(); |
847 | | - break; |
848 | | - default: |
849 | | - errno = EINVAL; |
850 | | - return -1; |
851 | | - } |
852 | | - |
853 | | - if (!md) { |
854 | | - errno = EINVAL; |
855 | | - return -1; |
856 | | - } |
857 | | - |
858 | | - if (!HMAC_Init_ex(hmac_ctx, secret, key_len, md, NULL)) { |
859 | | - errno = ENOMEM; |
860 | | - return -1; |
861 | | - } |
862 | | - |
863 | | - if (!HMAC_Update(hmac_ctx, (unsigned char *)hostnqn, |
864 | | - strlen(hostnqn))) { |
865 | | - errno = ENOKEY; |
866 | | - return -1; |
867 | | - } |
868 | | - |
869 | | - if (!HMAC_Update(hmac_ctx, (unsigned char *)hmac_seed, |
870 | | - strlen(hmac_seed))) { |
871 | | - errno = ENOKEY; |
872 | | - return -1; |
873 | | - } |
874 | | - |
875 | | - if (!HMAC_Final(hmac_ctx, key, &key_len)) { |
876 | | - errno = ENOKEY; |
877 | | - return -1; |
878 | | - } |
879 | | - |
880 | | - return 0; |
881 | | -} |
882 | | - |
883 | | -static int derive_psk_digest(const char *hostnqn, const char *subsysnqn, |
884 | | - int version, int cipher, |
885 | | - unsigned char *retained, size_t key_len, |
886 | | - char *digest, size_t digest_len) |
887 | | -{ |
888 | | - static const char hmac_seed[] = "NVMe-over-Fabrics"; |
889 | | - _cleanup_hmac_ctx_ HMAC_CTX *hmac_ctx = NULL; |
890 | | - _cleanup_free_ unsigned char *psk_ctx = NULL; |
891 | | - const EVP_MD *md; |
892 | | - size_t hmac_len; |
893 | | - size_t len; |
894 | | - |
895 | | - hmac_ctx = HMAC_CTX_new(); |
896 | | - if (!hmac_ctx) { |
897 | | - errno = ENOMEM; |
898 | | - return -1; |
899 | | - } |
900 | | - md = select_hmac(cipher, &hmac_len); |
901 | | - if (!md || !hmac_len) { |
902 | | - errno = EINVAL; |
903 | | - return -1; |
904 | | - } |
905 | | - |
906 | | - psk_ctx = malloc(key_len); |
907 | | - if (!psk_ctx) { |
908 | | - errno = ENOMEM; |
909 | | - return -1; |
910 | | - } |
911 | | - if (!HMAC_Init_ex(hmac_ctx, retained, key_len, md, NULL)) { |
912 | | - errno = ENOMEM; |
913 | | - return -1; |
914 | | - } |
915 | | - if (!HMAC_Update(hmac_ctx, (unsigned char *)hostnqn, |
916 | | - strlen(hostnqn))) { |
917 | | - errno = ENOKEY; |
918 | | - return -1; |
919 | | - } |
920 | | - if (!HMAC_Update(hmac_ctx, (unsigned char *)" ", 1)) { |
921 | | - errno = ENOKEY; |
922 | | - return -1; |
923 | | - } |
924 | | - if (!HMAC_Update(hmac_ctx, (unsigned char *)subsysnqn, |
925 | | - strlen(subsysnqn))) { |
926 | | - errno = ENOKEY; |
927 | | - return -1; |
928 | | - } |
929 | | - if (!HMAC_Update(hmac_ctx, (unsigned char *)" ", 1)) { |
930 | | - errno = ENOKEY; |
931 | | - return -1; |
932 | | - } |
933 | | - if (!HMAC_Update(hmac_ctx, (unsigned char *)hmac_seed, |
934 | | - strlen(hmac_seed))) { |
935 | | - errno = ENOKEY; |
936 | | - return -1; |
937 | | - } |
938 | | - if (!HMAC_Final(hmac_ctx, psk_ctx, (unsigned int *)&key_len)) { |
939 | | - errno = ENOKEY; |
940 | | - return -1; |
941 | | - } |
942 | | - if (key_len * 2 > digest_len) { |
943 | | - errno = EINVAL; |
944 | | - return -1; |
945 | | - } |
946 | | - memset(digest, 0, digest_len); |
947 | | - len = base64_encode(psk_ctx, key_len, digest); |
948 | | - if (len < 0) { |
949 | | - errno = ENOKEY; |
950 | | - return len; |
951 | | - } |
952 | | - return strlen(digest); |
953 | | -} |
954 | | - |
955 | | -#endif /* !CONFIG_OPENSSL_1 */ |
956 | 812 |
|
957 | | -#ifdef CONFIG_OPENSSL_3 |
958 | 813 | static DEFINE_CLEANUP_FUNC( |
959 | 814 | cleanup_ossl_lib_ctx, OSSL_LIB_CTX *, OSSL_LIB_CTX_free) |
960 | 815 | #define _cleanup_ossl_lib_ctx_ __cleanup__(cleanup_ossl_lib_ctx) |
@@ -1148,7 +1003,7 @@ static int derive_psk_digest(const char *hostnqn, const char *subsysnqn, |
1148 | 1003 | } |
1149 | 1004 | return strlen(digest); |
1150 | 1005 | } |
1151 | | -#endif /* !CONFIG_OPENSSL_3 */ |
| 1006 | +#endif /* !CONFIG_OPENSSL */ |
1152 | 1007 |
|
1153 | 1008 | static int gen_tls_identity(const char *hostnqn, const char *subsysnqn, |
1154 | 1009 | int version, int cipher, char *digest, |
|
0 commit comments