|
10 | 10 | #include <kunit/visibility.h> |
11 | 11 |
|
12 | 12 | #include <linux/kernel.h> |
| 13 | +#include <crypto/hash.h> |
13 | 14 |
|
14 | 15 | #include <linux/sunrpc/xdr.h> |
15 | 16 | #include <linux/sunrpc/gss_krb5.h> |
@@ -64,13 +65,66 @@ static void kdf_case(struct kunit *test) |
64 | 65 | "key mismatch"); |
65 | 66 | } |
66 | 67 |
|
| 68 | +static void checksum_case(struct kunit *test) |
| 69 | +{ |
| 70 | + const struct gss_krb5_test_param *param = test->param_value; |
| 71 | + struct xdr_buf buf = { |
| 72 | + .head[0].iov_base = param->plaintext->data, |
| 73 | + .head[0].iov_len = param->plaintext->len, |
| 74 | + .len = param->plaintext->len, |
| 75 | + }; |
| 76 | + const struct gss_krb5_enctype *gk5e; |
| 77 | + struct xdr_netobj Kc, checksum; |
| 78 | + struct crypto_ahash *tfm; |
| 79 | + int err; |
| 80 | + |
| 81 | + /* Arrange */ |
| 82 | + gk5e = gss_krb5_lookup_enctype(param->enctype); |
| 83 | + KUNIT_ASSERT_NOT_NULL(test, gk5e); |
| 84 | + |
| 85 | + Kc.len = gk5e->Kc_length; |
| 86 | + Kc.data = kunit_kzalloc(test, Kc.len, GFP_KERNEL); |
| 87 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, Kc.data); |
| 88 | + err = gk5e->derive_key(gk5e, param->base_key, &Kc, |
| 89 | + param->usage, GFP_KERNEL); |
| 90 | + KUNIT_ASSERT_EQ(test, err, 0); |
| 91 | + |
| 92 | + tfm = crypto_alloc_ahash(gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC); |
| 93 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tfm); |
| 94 | + err = crypto_ahash_setkey(tfm, Kc.data, Kc.len); |
| 95 | + KUNIT_ASSERT_EQ(test, err, 0); |
| 96 | + |
| 97 | + checksum.len = gk5e->cksumlength; |
| 98 | + checksum.data = kunit_kzalloc(test, checksum.len, GFP_KERNEL); |
| 99 | + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, checksum.data); |
| 100 | + |
| 101 | + /* Act */ |
| 102 | + err = gss_krb5_checksum(tfm, NULL, 0, &buf, 0, &checksum); |
| 103 | + KUNIT_ASSERT_EQ(test, err, 0); |
| 104 | + |
| 105 | + /* Assert */ |
| 106 | + KUNIT_EXPECT_EQ_MSG(test, |
| 107 | + memcmp(param->expected_result->data, |
| 108 | + checksum.data, checksum.len), 0, |
| 109 | + "checksum mismatch"); |
| 110 | + |
| 111 | + crypto_free_ahash(tfm); |
| 112 | +} |
| 113 | + |
67 | 114 | #define DEFINE_HEX_XDR_NETOBJ(name, hex_array...) \ |
68 | 115 | static const u8 name ## _data[] = { hex_array }; \ |
69 | 116 | static const struct xdr_netobj name = { \ |
70 | 117 | .data = (u8 *)name##_data, \ |
71 | 118 | .len = sizeof(name##_data), \ |
72 | 119 | } |
73 | 120 |
|
| 121 | +#define DEFINE_STR_XDR_NETOBJ(name, string) \ |
| 122 | + static const u8 name ## _str[] = string; \ |
| 123 | + static const struct xdr_netobj name = { \ |
| 124 | + .data = (u8 *)name##_str, \ |
| 125 | + .len = sizeof(name##_str) - 1, \ |
| 126 | + } |
| 127 | + |
74 | 128 | /* |
75 | 129 | * RFC 3961 Appendix A.1. n-fold |
76 | 130 | * |
@@ -836,12 +890,126 @@ static const struct gss_krb5_test_param rfc6803_kdf_test_params[] = { |
836 | 890 | /* Creates the function rfc6803_kdf_gen_params */ |
837 | 891 | KUNIT_ARRAY_PARAM(rfc6803_kdf, rfc6803_kdf_test_params, gss_krb5_get_desc); |
838 | 892 |
|
| 893 | +/* |
| 894 | + * From RFC 6803 Section 10. Test vectors |
| 895 | + * |
| 896 | + * Sample checksums. |
| 897 | + * |
| 898 | + * Copyright (c) 2012 IETF Trust and the persons identified as the |
| 899 | + * document authors. All rights reserved. |
| 900 | + * |
| 901 | + * XXX: These tests are likely to fail on EBCDIC or Unicode platforms. |
| 902 | + */ |
| 903 | +DEFINE_STR_XDR_NETOBJ(rfc6803_checksum_test1_plaintext, |
| 904 | + "abcdefghijk"); |
| 905 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test1_basekey, |
| 906 | + 0x1d, 0xc4, 0x6a, 0x8d, 0x76, 0x3f, 0x4f, 0x93, |
| 907 | + 0x74, 0x2b, 0xcb, 0xa3, 0x38, 0x75, 0x76, 0xc3 |
| 908 | +); |
| 909 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test1_usage, |
| 910 | + 0x00, 0x00, 0x00, 0x07, KEY_USAGE_SEED_CHECKSUM |
| 911 | +); |
| 912 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test1_expected_result, |
| 913 | + 0x11, 0x78, 0xe6, 0xc5, 0xc4, 0x7a, 0x8c, 0x1a, |
| 914 | + 0xe0, 0xc4, 0xb9, 0xc7, 0xd4, 0xeb, 0x7b, 0x6b |
| 915 | +); |
| 916 | + |
| 917 | +DEFINE_STR_XDR_NETOBJ(rfc6803_checksum_test2_plaintext, |
| 918 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
| 919 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test2_basekey, |
| 920 | + 0x50, 0x27, 0xbc, 0x23, 0x1d, 0x0f, 0x3a, 0x9d, |
| 921 | + 0x23, 0x33, 0x3f, 0x1c, 0xa6, 0xfd, 0xbe, 0x7c |
| 922 | +); |
| 923 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test2_usage, |
| 924 | + 0x00, 0x00, 0x00, 0x08, KEY_USAGE_SEED_CHECKSUM |
| 925 | +); |
| 926 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test2_expected_result, |
| 927 | + 0xd1, 0xb3, 0x4f, 0x70, 0x04, 0xa7, 0x31, 0xf2, |
| 928 | + 0x3a, 0x0c, 0x00, 0xbf, 0x6c, 0x3f, 0x75, 0x3a |
| 929 | +); |
| 930 | + |
| 931 | +DEFINE_STR_XDR_NETOBJ(rfc6803_checksum_test3_plaintext, |
| 932 | + "123456789"); |
| 933 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test3_basekey, |
| 934 | + 0xb6, 0x1c, 0x86, 0xcc, 0x4e, 0x5d, 0x27, 0x57, |
| 935 | + 0x54, 0x5a, 0xd4, 0x23, 0x39, 0x9f, 0xb7, 0x03, |
| 936 | + 0x1e, 0xca, 0xb9, 0x13, 0xcb, 0xb9, 0x00, 0xbd, |
| 937 | + 0x7a, 0x3c, 0x6d, 0xd8, 0xbf, 0x92, 0x01, 0x5b |
| 938 | +); |
| 939 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test3_usage, |
| 940 | + 0x00, 0x00, 0x00, 0x09, KEY_USAGE_SEED_CHECKSUM |
| 941 | +); |
| 942 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test3_expected_result, |
| 943 | + 0x87, 0xa1, 0x2c, 0xfd, 0x2b, 0x96, 0x21, 0x48, |
| 944 | + 0x10, 0xf0, 0x1c, 0x82, 0x6e, 0x77, 0x44, 0xb1 |
| 945 | +); |
| 946 | + |
| 947 | +DEFINE_STR_XDR_NETOBJ(rfc6803_checksum_test4_plaintext, |
| 948 | + "!@#$%^&*()!@#$%^&*()!@#$%^&*()"); |
| 949 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test4_basekey, |
| 950 | + 0x32, 0x16, 0x4c, 0x5b, 0x43, 0x4d, 0x1d, 0x15, |
| 951 | + 0x38, 0xe4, 0xcf, 0xd9, 0xbe, 0x80, 0x40, 0xfe, |
| 952 | + 0x8c, 0x4a, 0xc7, 0xac, 0xc4, 0xb9, 0x3d, 0x33, |
| 953 | + 0x14, 0xd2, 0x13, 0x36, 0x68, 0x14, 0x7a, 0x05 |
| 954 | +); |
| 955 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test4_usage, |
| 956 | + 0x00, 0x00, 0x00, 0x0a, KEY_USAGE_SEED_CHECKSUM |
| 957 | +); |
| 958 | +DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test4_expected_result, |
| 959 | + 0x3f, 0xa0, 0xb4, 0x23, 0x55, 0xe5, 0x2b, 0x18, |
| 960 | + 0x91, 0x87, 0x29, 0x4a, 0xa2, 0x52, 0xab, 0x64 |
| 961 | +); |
| 962 | + |
| 963 | +static const struct gss_krb5_test_param rfc6803_checksum_test_params[] = { |
| 964 | + { |
| 965 | + .desc = "camellia128-cts-cmac checksum test 1", |
| 966 | + .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC, |
| 967 | + .base_key = &rfc6803_checksum_test1_basekey, |
| 968 | + .usage = &rfc6803_checksum_test1_usage, |
| 969 | + .plaintext = &rfc6803_checksum_test1_plaintext, |
| 970 | + .expected_result = &rfc6803_checksum_test1_expected_result, |
| 971 | + }, |
| 972 | + { |
| 973 | + .desc = "camellia128-cts-cmac checksum test 2", |
| 974 | + .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC, |
| 975 | + .base_key = &rfc6803_checksum_test2_basekey, |
| 976 | + .usage = &rfc6803_checksum_test2_usage, |
| 977 | + .plaintext = &rfc6803_checksum_test2_plaintext, |
| 978 | + .expected_result = &rfc6803_checksum_test2_expected_result, |
| 979 | + }, |
| 980 | + { |
| 981 | + .desc = "camellia256-cts-cmac checksum test 3", |
| 982 | + .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC, |
| 983 | + .base_key = &rfc6803_checksum_test3_basekey, |
| 984 | + .usage = &rfc6803_checksum_test3_usage, |
| 985 | + .plaintext = &rfc6803_checksum_test3_plaintext, |
| 986 | + .expected_result = &rfc6803_checksum_test3_expected_result, |
| 987 | + }, |
| 988 | + { |
| 989 | + .desc = "camellia256-cts-cmac checksum test 4", |
| 990 | + .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC, |
| 991 | + .base_key = &rfc6803_checksum_test4_basekey, |
| 992 | + .usage = &rfc6803_checksum_test4_usage, |
| 993 | + .plaintext = &rfc6803_checksum_test4_plaintext, |
| 994 | + .expected_result = &rfc6803_checksum_test4_expected_result, |
| 995 | + }, |
| 996 | +}; |
| 997 | + |
| 998 | +/* Creates the function rfc6803_checksum_gen_params */ |
| 999 | +KUNIT_ARRAY_PARAM(rfc6803_checksum, rfc6803_checksum_test_params, |
| 1000 | + gss_krb5_get_desc); |
| 1001 | + |
839 | 1002 | static struct kunit_case rfc6803_test_cases[] = { |
840 | 1003 | { |
841 | 1004 | .name = "RFC 6803 key derivation", |
842 | 1005 | .run_case = kdf_case, |
843 | 1006 | .generate_params = rfc6803_kdf_gen_params, |
844 | 1007 | }, |
| 1008 | + { |
| 1009 | + .name = "RFC 6803 checksum", |
| 1010 | + .run_case = checksum_case, |
| 1011 | + .generate_params = rfc6803_checksum_gen_params, |
| 1012 | + }, |
845 | 1013 | }; |
846 | 1014 |
|
847 | 1015 | static struct kunit_suite rfc6803_suite = { |
|
0 commit comments