|
24 | 24 | #include <tests/api/test_asn.h> |
25 | 25 |
|
26 | 26 | #include <wolfssl/wolfcrypt/asn.h> |
| 27 | +#include <wolfssl/wolfcrypt/rsa.h> |
27 | 28 |
|
28 | 29 | #if defined(WC_ENABLE_ASYM_KEY_EXPORT) && defined(HAVE_ED25519) |
29 | 30 | static int test_SetAsymKeyDer_once(byte* privKey, word32 privKeySz, byte* pubKey, |
@@ -787,3 +788,135 @@ int test_wolfssl_local_MatchBaseName(void) |
787 | 788 |
|
788 | 789 | return EXPECT_RESULT(); |
789 | 790 | } |
| 791 | + |
| 792 | +/* |
| 793 | + * Testing wc_DecodeRsaPssParams with known DER byte arrays. |
| 794 | + * Exercises both WOLFSSL_ASN_TEMPLATE and non-template paths. |
| 795 | + */ |
| 796 | +int test_wc_DecodeRsaPssParams(void) |
| 797 | +{ |
| 798 | + EXPECT_DECLS; |
| 799 | +#if defined(WC_RSA_PSS) && !defined(NO_RSA) && !defined(NO_ASN) |
| 800 | + enum wc_HashType hash; |
| 801 | + int mgf; |
| 802 | + int saltLen; |
| 803 | + |
| 804 | + /* SHA-256 / MGF1-SHA-256 / saltLen=32 */ |
| 805 | + static const byte pssParamsSha256[] = { |
| 806 | + 0x30, 0x34, |
| 807 | + 0xA0, 0x0F, |
| 808 | + 0x30, 0x0D, |
| 809 | + 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 810 | + 0x04, 0x02, 0x01, |
| 811 | + 0x05, 0x00, |
| 812 | + 0xA1, 0x1C, |
| 813 | + 0x30, 0x1A, |
| 814 | + 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, |
| 815 | + 0x01, 0x01, 0x08, |
| 816 | + 0x30, 0x0D, |
| 817 | + 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 818 | + 0x04, 0x02, 0x01, |
| 819 | + 0x05, 0x00, |
| 820 | + 0xA2, 0x03, |
| 821 | + 0x02, 0x01, 0x20, |
| 822 | + }; |
| 823 | + |
| 824 | + /* Hash-only: SHA-256 hash, defaults for MGF and salt */ |
| 825 | + static const byte pssParamsHashOnly[] = { |
| 826 | + 0x30, 0x11, |
| 827 | + 0xA0, 0x0F, |
| 828 | + 0x30, 0x0D, |
| 829 | + 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
| 830 | + 0x04, 0x02, 0x01, |
| 831 | + 0x05, 0x00, |
| 832 | + }; |
| 833 | + |
| 834 | + /* Salt-only: default hash/mgf, saltLen=48 */ |
| 835 | + static const byte pssParamsSaltOnly[] = { |
| 836 | + 0x30, 0x05, |
| 837 | + 0xA2, 0x03, |
| 838 | + 0x02, 0x01, 0x30, |
| 839 | + }; |
| 840 | + |
| 841 | + /* NULL tag (05 00) means all defaults */ |
| 842 | + static const byte pssParamsNull[] = { 0x05, 0x00 }; |
| 843 | + |
| 844 | + /* Empty SEQUENCE means all non-default fields omitted => defaults */ |
| 845 | + static const byte pssParamsEmptySeq[] = { 0x30, 0x00 }; |
| 846 | + |
| 847 | + /* --- Test 1: sz=0 => all defaults --- */ |
| 848 | + hash = WC_HASH_TYPE_NONE; |
| 849 | + mgf = 0; |
| 850 | + saltLen = 0; |
| 851 | + ExpectIntEQ(wc_DecodeRsaPssParams((const byte*)"", 0, |
| 852 | + &hash, &mgf, &saltLen), 0); |
| 853 | + ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA); |
| 854 | + ExpectIntEQ(mgf, WC_MGF1SHA1); |
| 855 | + ExpectIntEQ(saltLen, 20); |
| 856 | + |
| 857 | + /* --- Test 2: NULL tag => all defaults --- */ |
| 858 | + hash = WC_HASH_TYPE_NONE; |
| 859 | + mgf = 0; |
| 860 | + saltLen = 0; |
| 861 | + ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsNull, |
| 862 | + (word32)sizeof(pssParamsNull), &hash, &mgf, &saltLen), 0); |
| 863 | + ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA); |
| 864 | + ExpectIntEQ(mgf, WC_MGF1SHA1); |
| 865 | + ExpectIntEQ(saltLen, 20); |
| 866 | + |
| 867 | + /* --- Test 3: Empty SEQUENCE => all defaults --- */ |
| 868 | + hash = WC_HASH_TYPE_NONE; |
| 869 | + mgf = 0; |
| 870 | + saltLen = 0; |
| 871 | + ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsEmptySeq, |
| 872 | + (word32)sizeof(pssParamsEmptySeq), &hash, &mgf, &saltLen), 0); |
| 873 | + ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA); |
| 874 | + ExpectIntEQ(mgf, WC_MGF1SHA1); |
| 875 | + ExpectIntEQ(saltLen, 20); |
| 876 | + |
| 877 | +#ifndef NO_SHA256 |
| 878 | + /* --- Test 4: SHA-256 / MGF1-SHA-256 / salt=32 --- */ |
| 879 | + hash = WC_HASH_TYPE_NONE; |
| 880 | + mgf = 0; |
| 881 | + saltLen = 0; |
| 882 | + ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsSha256, |
| 883 | + (word32)sizeof(pssParamsSha256), &hash, &mgf, &saltLen), 0); |
| 884 | + ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA256); |
| 885 | + ExpectIntEQ(mgf, WC_MGF1SHA256); |
| 886 | + ExpectIntEQ(saltLen, 32); |
| 887 | + |
| 888 | + /* --- Test 5: Hash only => SHA-256, default MGF/salt --- */ |
| 889 | + hash = WC_HASH_TYPE_NONE; |
| 890 | + mgf = 0; |
| 891 | + saltLen = 0; |
| 892 | + ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsHashOnly, |
| 893 | + (word32)sizeof(pssParamsHashOnly), &hash, &mgf, &saltLen), 0); |
| 894 | + ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA256); |
| 895 | + ExpectIntEQ(mgf, WC_MGF1SHA1); |
| 896 | + ExpectIntEQ(saltLen, 20); |
| 897 | +#endif |
| 898 | + |
| 899 | + /* --- Test 6: Salt only => default hash/MGF, salt=48 --- */ |
| 900 | + hash = WC_HASH_TYPE_NONE; |
| 901 | + mgf = 0; |
| 902 | + saltLen = 0; |
| 903 | + ExpectIntEQ(wc_DecodeRsaPssParams(pssParamsSaltOnly, |
| 904 | + (word32)sizeof(pssParamsSaltOnly), &hash, &mgf, &saltLen), 0); |
| 905 | + ExpectIntEQ((int)hash, (int)WC_HASH_TYPE_SHA); |
| 906 | + ExpectIntEQ(mgf, WC_MGF1SHA1); |
| 907 | + ExpectIntEQ(saltLen, 48); |
| 908 | + |
| 909 | + /* --- Test 7: NULL pointer → BAD_FUNC_ARG --- */ |
| 910 | + ExpectIntEQ(wc_DecodeRsaPssParams(NULL, 10, &hash, &mgf, &saltLen), |
| 911 | + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); |
| 912 | + |
| 913 | + /* --- Test 8: Bad leading tag => ASN_PARSE_E --- */ |
| 914 | + { |
| 915 | + static const byte badTag[] = { 0x01, 0x00 }; |
| 916 | + ExpectIntEQ(wc_DecodeRsaPssParams(badTag, (word32)sizeof(badTag), |
| 917 | + &hash, &mgf, &saltLen), WC_NO_ERR_TRACE(ASN_PARSE_E)); |
| 918 | + } |
| 919 | + |
| 920 | +#endif /* WC_RSA_PSS && !NO_RSA && !NO_ASN */ |
| 921 | + return EXPECT_RESULT(); |
| 922 | +} |
0 commit comments