gss_krb5_test.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2022 Oracle and/or its affiliates.
  4. *
  5. * KUnit test of SunRPC's GSS Kerberos mechanism. Subsystem
  6. * name is "rpcsec_gss_krb5".
  7. */
  8. #include <kunit/test.h>
  9. #include <kunit/visibility.h>
  10. #include <linux/kernel.h>
  11. #include <crypto/hash.h>
  12. #include <linux/sunrpc/xdr.h>
  13. #include <linux/sunrpc/gss_krb5.h>
  14. #include "gss_krb5_internal.h"
  15. MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
  16. struct gss_krb5_test_param {
  17. const char *desc;
  18. u32 enctype;
  19. u32 nfold;
  20. u32 constant;
  21. const struct xdr_netobj *base_key;
  22. const struct xdr_netobj *Ke;
  23. const struct xdr_netobj *usage;
  24. const struct xdr_netobj *plaintext;
  25. const struct xdr_netobj *confounder;
  26. const struct xdr_netobj *expected_result;
  27. const struct xdr_netobj *expected_hmac;
  28. const struct xdr_netobj *next_iv;
  29. };
  30. static inline void gss_krb5_get_desc(const struct gss_krb5_test_param *param,
  31. char *desc)
  32. {
  33. strscpy(desc, param->desc, KUNIT_PARAM_DESC_SIZE);
  34. }
  35. static void kdf_case(struct kunit *test)
  36. {
  37. const struct gss_krb5_test_param *param = test->param_value;
  38. const struct gss_krb5_enctype *gk5e;
  39. struct xdr_netobj derivedkey;
  40. int err;
  41. /* Arrange */
  42. gk5e = gss_krb5_lookup_enctype(param->enctype);
  43. if (!gk5e)
  44. kunit_skip(test, "Encryption type is not available");
  45. derivedkey.data = kunit_kzalloc(test, param->expected_result->len,
  46. GFP_KERNEL);
  47. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, derivedkey.data);
  48. derivedkey.len = param->expected_result->len;
  49. /* Act */
  50. err = gk5e->derive_key(gk5e, param->base_key, &derivedkey,
  51. param->usage, GFP_KERNEL);
  52. KUNIT_ASSERT_EQ(test, err, 0);
  53. /* Assert */
  54. KUNIT_EXPECT_EQ_MSG(test,
  55. memcmp(param->expected_result->data,
  56. derivedkey.data, derivedkey.len), 0,
  57. "key mismatch");
  58. }
  59. static void checksum_case(struct kunit *test)
  60. {
  61. const struct gss_krb5_test_param *param = test->param_value;
  62. struct xdr_buf buf = {
  63. .head[0].iov_len = param->plaintext->len,
  64. .len = param->plaintext->len,
  65. };
  66. const struct gss_krb5_enctype *gk5e;
  67. struct xdr_netobj Kc, checksum;
  68. struct crypto_ahash *tfm;
  69. int err;
  70. /* Arrange */
  71. gk5e = gss_krb5_lookup_enctype(param->enctype);
  72. if (!gk5e)
  73. kunit_skip(test, "Encryption type is not available");
  74. Kc.len = gk5e->Kc_length;
  75. Kc.data = kunit_kzalloc(test, Kc.len, GFP_KERNEL);
  76. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, Kc.data);
  77. err = gk5e->derive_key(gk5e, param->base_key, &Kc,
  78. param->usage, GFP_KERNEL);
  79. KUNIT_ASSERT_EQ(test, err, 0);
  80. tfm = crypto_alloc_ahash(gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
  81. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, tfm);
  82. err = crypto_ahash_setkey(tfm, Kc.data, Kc.len);
  83. KUNIT_ASSERT_EQ(test, err, 0);
  84. buf.head[0].iov_base = kunit_kzalloc(test, buf.head[0].iov_len, GFP_KERNEL);
  85. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf.head[0].iov_base);
  86. memcpy(buf.head[0].iov_base, param->plaintext->data, buf.head[0].iov_len);
  87. checksum.len = gk5e->cksumlength;
  88. checksum.data = kunit_kzalloc(test, checksum.len, GFP_KERNEL);
  89. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, checksum.data);
  90. /* Act */
  91. err = gss_krb5_checksum(tfm, NULL, 0, &buf, 0, &checksum);
  92. KUNIT_ASSERT_EQ(test, err, 0);
  93. /* Assert */
  94. KUNIT_EXPECT_EQ_MSG(test,
  95. memcmp(param->expected_result->data,
  96. checksum.data, checksum.len), 0,
  97. "checksum mismatch");
  98. crypto_free_ahash(tfm);
  99. }
  100. #define DEFINE_HEX_XDR_NETOBJ(name, hex_array...) \
  101. static const u8 name ## _data[] = { hex_array }; \
  102. static const struct xdr_netobj name = { \
  103. .data = (u8 *)name##_data, \
  104. .len = sizeof(name##_data), \
  105. }
  106. #define DEFINE_STR_XDR_NETOBJ(name, string) \
  107. static const u8 name ## _str[] = string; \
  108. static const struct xdr_netobj name = { \
  109. .data = (u8 *)name##_str, \
  110. .len = sizeof(name##_str) - 1, \
  111. }
  112. /*
  113. * RFC 3961 Appendix A.1. n-fold
  114. *
  115. * The n-fold function is defined in section 5.1 of RFC 3961.
  116. *
  117. * This test material is copyright (C) The Internet Society (2005).
  118. */
  119. DEFINE_HEX_XDR_NETOBJ(nfold_test1_plaintext,
  120. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35
  121. );
  122. DEFINE_HEX_XDR_NETOBJ(nfold_test1_expected_result,
  123. 0xbe, 0x07, 0x26, 0x31, 0x27, 0x6b, 0x19, 0x55
  124. );
  125. DEFINE_HEX_XDR_NETOBJ(nfold_test2_plaintext,
  126. 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64
  127. );
  128. DEFINE_HEX_XDR_NETOBJ(nfold_test2_expected_result,
  129. 0x78, 0xa0, 0x7b, 0x6c, 0xaf, 0x85, 0xfa
  130. );
  131. DEFINE_HEX_XDR_NETOBJ(nfold_test3_plaintext,
  132. 0x52, 0x6f, 0x75, 0x67, 0x68, 0x20, 0x43, 0x6f,
  133. 0x6e, 0x73, 0x65, 0x6e, 0x73, 0x75, 0x73, 0x2c,
  134. 0x20, 0x61, 0x6e, 0x64, 0x20, 0x52, 0x75, 0x6e,
  135. 0x6e, 0x69, 0x6e, 0x67, 0x20, 0x43, 0x6f, 0x64,
  136. 0x65
  137. );
  138. DEFINE_HEX_XDR_NETOBJ(nfold_test3_expected_result,
  139. 0xbb, 0x6e, 0xd3, 0x08, 0x70, 0xb7, 0xf0, 0xe0
  140. );
  141. DEFINE_HEX_XDR_NETOBJ(nfold_test4_plaintext,
  142. 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64
  143. );
  144. DEFINE_HEX_XDR_NETOBJ(nfold_test4_expected_result,
  145. 0x59, 0xe4, 0xa8, 0xca, 0x7c, 0x03, 0x85, 0xc3,
  146. 0xc3, 0x7b, 0x3f, 0x6d, 0x20, 0x00, 0x24, 0x7c,
  147. 0xb6, 0xe6, 0xbd, 0x5b, 0x3e
  148. );
  149. DEFINE_HEX_XDR_NETOBJ(nfold_test5_plaintext,
  150. 0x4d, 0x41, 0x53, 0x53, 0x41, 0x43, 0x48, 0x56,
  151. 0x53, 0x45, 0x54, 0x54, 0x53, 0x20, 0x49, 0x4e,
  152. 0x53, 0x54, 0x49, 0x54, 0x56, 0x54, 0x45, 0x20,
  153. 0x4f, 0x46, 0x20, 0x54, 0x45, 0x43, 0x48, 0x4e,
  154. 0x4f, 0x4c, 0x4f, 0x47, 0x59
  155. );
  156. DEFINE_HEX_XDR_NETOBJ(nfold_test5_expected_result,
  157. 0xdb, 0x3b, 0x0d, 0x8f, 0x0b, 0x06, 0x1e, 0x60,
  158. 0x32, 0x82, 0xb3, 0x08, 0xa5, 0x08, 0x41, 0x22,
  159. 0x9a, 0xd7, 0x98, 0xfa, 0xb9, 0x54, 0x0c, 0x1b
  160. );
  161. DEFINE_HEX_XDR_NETOBJ(nfold_test6_plaintext,
  162. 0x51
  163. );
  164. DEFINE_HEX_XDR_NETOBJ(nfold_test6_expected_result,
  165. 0x51, 0x8a, 0x54, 0xa2, 0x15, 0xa8, 0x45, 0x2a,
  166. 0x51, 0x8a, 0x54, 0xa2, 0x15, 0xa8, 0x45, 0x2a,
  167. 0x51, 0x8a, 0x54, 0xa2, 0x15
  168. );
  169. DEFINE_HEX_XDR_NETOBJ(nfold_test7_plaintext,
  170. 0x62, 0x61
  171. );
  172. DEFINE_HEX_XDR_NETOBJ(nfold_test7_expected_result,
  173. 0xfb, 0x25, 0xd5, 0x31, 0xae, 0x89, 0x74, 0x49,
  174. 0x9f, 0x52, 0xfd, 0x92, 0xea, 0x98, 0x57, 0xc4,
  175. 0xba, 0x24, 0xcf, 0x29, 0x7e
  176. );
  177. DEFINE_HEX_XDR_NETOBJ(nfold_test_kerberos,
  178. 0x6b, 0x65, 0x72, 0x62, 0x65, 0x72, 0x6f, 0x73
  179. );
  180. DEFINE_HEX_XDR_NETOBJ(nfold_test8_expected_result,
  181. 0x6b, 0x65, 0x72, 0x62, 0x65, 0x72, 0x6f, 0x73
  182. );
  183. DEFINE_HEX_XDR_NETOBJ(nfold_test9_expected_result,
  184. 0x6b, 0x65, 0x72, 0x62, 0x65, 0x72, 0x6f, 0x73,
  185. 0x7b, 0x9b, 0x5b, 0x2b, 0x93, 0x13, 0x2b, 0x93
  186. );
  187. DEFINE_HEX_XDR_NETOBJ(nfold_test10_expected_result,
  188. 0x83, 0x72, 0xc2, 0x36, 0x34, 0x4e, 0x5f, 0x15,
  189. 0x50, 0xcd, 0x07, 0x47, 0xe1, 0x5d, 0x62, 0xca,
  190. 0x7a, 0x5a, 0x3b, 0xce, 0xa4
  191. );
  192. DEFINE_HEX_XDR_NETOBJ(nfold_test11_expected_result,
  193. 0x6b, 0x65, 0x72, 0x62, 0x65, 0x72, 0x6f, 0x73,
  194. 0x7b, 0x9b, 0x5b, 0x2b, 0x93, 0x13, 0x2b, 0x93,
  195. 0x5c, 0x9b, 0xdc, 0xda, 0xd9, 0x5c, 0x98, 0x99,
  196. 0xc4, 0xca, 0xe4, 0xde, 0xe6, 0xd6, 0xca, 0xe4
  197. );
  198. static const struct gss_krb5_test_param rfc3961_nfold_test_params[] = {
  199. {
  200. .desc = "64-fold(\"012345\")",
  201. .nfold = 64,
  202. .plaintext = &nfold_test1_plaintext,
  203. .expected_result = &nfold_test1_expected_result,
  204. },
  205. {
  206. .desc = "56-fold(\"password\")",
  207. .nfold = 56,
  208. .plaintext = &nfold_test2_plaintext,
  209. .expected_result = &nfold_test2_expected_result,
  210. },
  211. {
  212. .desc = "64-fold(\"Rough Consensus, and Running Code\")",
  213. .nfold = 64,
  214. .plaintext = &nfold_test3_plaintext,
  215. .expected_result = &nfold_test3_expected_result,
  216. },
  217. {
  218. .desc = "168-fold(\"password\")",
  219. .nfold = 168,
  220. .plaintext = &nfold_test4_plaintext,
  221. .expected_result = &nfold_test4_expected_result,
  222. },
  223. {
  224. .desc = "192-fold(\"MASSACHVSETTS INSTITVTE OF TECHNOLOGY\")",
  225. .nfold = 192,
  226. .plaintext = &nfold_test5_plaintext,
  227. .expected_result = &nfold_test5_expected_result,
  228. },
  229. {
  230. .desc = "168-fold(\"Q\")",
  231. .nfold = 168,
  232. .plaintext = &nfold_test6_plaintext,
  233. .expected_result = &nfold_test6_expected_result,
  234. },
  235. {
  236. .desc = "168-fold(\"ba\")",
  237. .nfold = 168,
  238. .plaintext = &nfold_test7_plaintext,
  239. .expected_result = &nfold_test7_expected_result,
  240. },
  241. {
  242. .desc = "64-fold(\"kerberos\")",
  243. .nfold = 64,
  244. .plaintext = &nfold_test_kerberos,
  245. .expected_result = &nfold_test8_expected_result,
  246. },
  247. {
  248. .desc = "128-fold(\"kerberos\")",
  249. .nfold = 128,
  250. .plaintext = &nfold_test_kerberos,
  251. .expected_result = &nfold_test9_expected_result,
  252. },
  253. {
  254. .desc = "168-fold(\"kerberos\")",
  255. .nfold = 168,
  256. .plaintext = &nfold_test_kerberos,
  257. .expected_result = &nfold_test10_expected_result,
  258. },
  259. {
  260. .desc = "256-fold(\"kerberos\")",
  261. .nfold = 256,
  262. .plaintext = &nfold_test_kerberos,
  263. .expected_result = &nfold_test11_expected_result,
  264. },
  265. };
  266. /* Creates the function rfc3961_nfold_gen_params */
  267. KUNIT_ARRAY_PARAM(rfc3961_nfold, rfc3961_nfold_test_params, gss_krb5_get_desc);
  268. static void rfc3961_nfold_case(struct kunit *test)
  269. {
  270. const struct gss_krb5_test_param *param = test->param_value;
  271. u8 *result;
  272. /* Arrange */
  273. result = kunit_kzalloc(test, 4096, GFP_KERNEL);
  274. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, result);
  275. /* Act */
  276. krb5_nfold(param->plaintext->len * 8, param->plaintext->data,
  277. param->expected_result->len * 8, result);
  278. /* Assert */
  279. KUNIT_EXPECT_EQ_MSG(test,
  280. memcmp(param->expected_result->data,
  281. result, param->expected_result->len), 0,
  282. "result mismatch");
  283. }
  284. static struct kunit_case rfc3961_test_cases[] = {
  285. {
  286. .name = "RFC 3961 n-fold",
  287. .run_case = rfc3961_nfold_case,
  288. .generate_params = rfc3961_nfold_gen_params,
  289. },
  290. {}
  291. };
  292. static struct kunit_suite rfc3961_suite = {
  293. .name = "RFC 3961 tests",
  294. .test_cases = rfc3961_test_cases,
  295. };
  296. /*
  297. * From RFC 3962 Appendix B: Sample Test Vectors
  298. *
  299. * Some test vectors for CBC with ciphertext stealing, using an
  300. * initial vector of all-zero.
  301. *
  302. * This test material is copyright (C) The Internet Society (2005).
  303. */
  304. DEFINE_HEX_XDR_NETOBJ(rfc3962_encryption_key,
  305. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  306. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  307. );
  308. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test1_plaintext,
  309. 0x49, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20,
  310. 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x74, 0x68, 0x65,
  311. 0x20
  312. );
  313. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test1_expected_result,
  314. 0xc6, 0x35, 0x35, 0x68, 0xf2, 0xbf, 0x8c, 0xb4,
  315. 0xd8, 0xa5, 0x80, 0x36, 0x2d, 0xa7, 0xff, 0x7f,
  316. 0x97
  317. );
  318. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test1_next_iv,
  319. 0xc6, 0x35, 0x35, 0x68, 0xf2, 0xbf, 0x8c, 0xb4,
  320. 0xd8, 0xa5, 0x80, 0x36, 0x2d, 0xa7, 0xff, 0x7f
  321. );
  322. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test2_plaintext,
  323. 0x49, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20,
  324. 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x74, 0x68, 0x65,
  325. 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c,
  326. 0x20, 0x47, 0x61, 0x75, 0x27, 0x73, 0x20
  327. );
  328. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test2_expected_result,
  329. 0xfc, 0x00, 0x78, 0x3e, 0x0e, 0xfd, 0xb2, 0xc1,
  330. 0xd4, 0x45, 0xd4, 0xc8, 0xef, 0xf7, 0xed, 0x22,
  331. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  332. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5
  333. );
  334. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test2_next_iv,
  335. 0xfc, 0x00, 0x78, 0x3e, 0x0e, 0xfd, 0xb2, 0xc1,
  336. 0xd4, 0x45, 0xd4, 0xc8, 0xef, 0xf7, 0xed, 0x22
  337. );
  338. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test3_plaintext,
  339. 0x49, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20,
  340. 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x74, 0x68, 0x65,
  341. 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c,
  342. 0x20, 0x47, 0x61, 0x75, 0x27, 0x73, 0x20, 0x43
  343. );
  344. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test3_expected_result,
  345. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  346. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8,
  347. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  348. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84
  349. );
  350. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test3_next_iv,
  351. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  352. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8
  353. );
  354. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test4_plaintext,
  355. 0x49, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20,
  356. 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x74, 0x68, 0x65,
  357. 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c,
  358. 0x20, 0x47, 0x61, 0x75, 0x27, 0x73, 0x20, 0x43,
  359. 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x2c, 0x20,
  360. 0x70, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2c
  361. );
  362. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test4_expected_result,
  363. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  364. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84,
  365. 0xb3, 0xff, 0xfd, 0x94, 0x0c, 0x16, 0xa1, 0x8c,
  366. 0x1b, 0x55, 0x49, 0xd2, 0xf8, 0x38, 0x02, 0x9e,
  367. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  368. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5
  369. );
  370. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test4_next_iv,
  371. 0xb3, 0xff, 0xfd, 0x94, 0x0c, 0x16, 0xa1, 0x8c,
  372. 0x1b, 0x55, 0x49, 0xd2, 0xf8, 0x38, 0x02, 0x9e
  373. );
  374. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test5_plaintext,
  375. 0x49, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20,
  376. 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x74, 0x68, 0x65,
  377. 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c,
  378. 0x20, 0x47, 0x61, 0x75, 0x27, 0x73, 0x20, 0x43,
  379. 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x2c, 0x20,
  380. 0x70, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2c, 0x20
  381. );
  382. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test5_expected_result,
  383. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  384. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84,
  385. 0x9d, 0xad, 0x8b, 0xbb, 0x96, 0xc4, 0xcd, 0xc0,
  386. 0x3b, 0xc1, 0x03, 0xe1, 0xa1, 0x94, 0xbb, 0xd8,
  387. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  388. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8
  389. );
  390. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test5_next_iv,
  391. 0x9d, 0xad, 0x8b, 0xbb, 0x96, 0xc4, 0xcd, 0xc0,
  392. 0x3b, 0xc1, 0x03, 0xe1, 0xa1, 0x94, 0xbb, 0xd8
  393. );
  394. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test6_plaintext,
  395. 0x49, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20,
  396. 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x74, 0x68, 0x65,
  397. 0x20, 0x47, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c,
  398. 0x20, 0x47, 0x61, 0x75, 0x27, 0x73, 0x20, 0x43,
  399. 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x2c, 0x20,
  400. 0x70, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x2c, 0x20,
  401. 0x61, 0x6e, 0x64, 0x20, 0x77, 0x6f, 0x6e, 0x74,
  402. 0x6f, 0x6e, 0x20, 0x73, 0x6f, 0x75, 0x70, 0x2e
  403. );
  404. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test6_expected_result,
  405. 0x97, 0x68, 0x72, 0x68, 0xd6, 0xec, 0xcc, 0xc0,
  406. 0xc0, 0x7b, 0x25, 0xe2, 0x5e, 0xcf, 0xe5, 0x84,
  407. 0x39, 0x31, 0x25, 0x23, 0xa7, 0x86, 0x62, 0xd5,
  408. 0xbe, 0x7f, 0xcb, 0xcc, 0x98, 0xeb, 0xf5, 0xa8,
  409. 0x48, 0x07, 0xef, 0xe8, 0x36, 0xee, 0x89, 0xa5,
  410. 0x26, 0x73, 0x0d, 0xbc, 0x2f, 0x7b, 0xc8, 0x40,
  411. 0x9d, 0xad, 0x8b, 0xbb, 0x96, 0xc4, 0xcd, 0xc0,
  412. 0x3b, 0xc1, 0x03, 0xe1, 0xa1, 0x94, 0xbb, 0xd8
  413. );
  414. DEFINE_HEX_XDR_NETOBJ(rfc3962_enc_test6_next_iv,
  415. 0x48, 0x07, 0xef, 0xe8, 0x36, 0xee, 0x89, 0xa5,
  416. 0x26, 0x73, 0x0d, 0xbc, 0x2f, 0x7b, 0xc8, 0x40
  417. );
  418. static const struct gss_krb5_test_param rfc3962_encrypt_test_params[] = {
  419. {
  420. .desc = "Encrypt with aes128-cts-hmac-sha1-96 case 1",
  421. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
  422. .Ke = &rfc3962_encryption_key,
  423. .plaintext = &rfc3962_enc_test1_plaintext,
  424. .expected_result = &rfc3962_enc_test1_expected_result,
  425. .next_iv = &rfc3962_enc_test1_next_iv,
  426. },
  427. {
  428. .desc = "Encrypt with aes128-cts-hmac-sha1-96 case 2",
  429. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
  430. .Ke = &rfc3962_encryption_key,
  431. .plaintext = &rfc3962_enc_test2_plaintext,
  432. .expected_result = &rfc3962_enc_test2_expected_result,
  433. .next_iv = &rfc3962_enc_test2_next_iv,
  434. },
  435. {
  436. .desc = "Encrypt with aes128-cts-hmac-sha1-96 case 3",
  437. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
  438. .Ke = &rfc3962_encryption_key,
  439. .plaintext = &rfc3962_enc_test3_plaintext,
  440. .expected_result = &rfc3962_enc_test3_expected_result,
  441. .next_iv = &rfc3962_enc_test3_next_iv,
  442. },
  443. {
  444. .desc = "Encrypt with aes128-cts-hmac-sha1-96 case 4",
  445. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
  446. .Ke = &rfc3962_encryption_key,
  447. .plaintext = &rfc3962_enc_test4_plaintext,
  448. .expected_result = &rfc3962_enc_test4_expected_result,
  449. .next_iv = &rfc3962_enc_test4_next_iv,
  450. },
  451. {
  452. .desc = "Encrypt with aes128-cts-hmac-sha1-96 case 5",
  453. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
  454. .Ke = &rfc3962_encryption_key,
  455. .plaintext = &rfc3962_enc_test5_plaintext,
  456. .expected_result = &rfc3962_enc_test5_expected_result,
  457. .next_iv = &rfc3962_enc_test5_next_iv,
  458. },
  459. {
  460. .desc = "Encrypt with aes128-cts-hmac-sha1-96 case 6",
  461. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
  462. .Ke = &rfc3962_encryption_key,
  463. .plaintext = &rfc3962_enc_test6_plaintext,
  464. .expected_result = &rfc3962_enc_test6_expected_result,
  465. .next_iv = &rfc3962_enc_test6_next_iv,
  466. },
  467. };
  468. /* Creates the function rfc3962_encrypt_gen_params */
  469. KUNIT_ARRAY_PARAM(rfc3962_encrypt, rfc3962_encrypt_test_params,
  470. gss_krb5_get_desc);
  471. /*
  472. * This tests the implementation of the encryption part of the mechanism.
  473. * It does not apply a confounder or test the result of HMAC over the
  474. * plaintext.
  475. */
  476. static void rfc3962_encrypt_case(struct kunit *test)
  477. {
  478. const struct gss_krb5_test_param *param = test->param_value;
  479. struct crypto_sync_skcipher *cts_tfm, *cbc_tfm;
  480. const struct gss_krb5_enctype *gk5e;
  481. struct xdr_buf buf;
  482. void *iv, *text;
  483. u32 err;
  484. /* Arrange */
  485. gk5e = gss_krb5_lookup_enctype(param->enctype);
  486. if (!gk5e)
  487. kunit_skip(test, "Encryption type is not available");
  488. cbc_tfm = crypto_alloc_sync_skcipher(gk5e->aux_cipher, 0, 0);
  489. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cbc_tfm);
  490. err = crypto_sync_skcipher_setkey(cbc_tfm, param->Ke->data, param->Ke->len);
  491. KUNIT_ASSERT_EQ(test, err, 0);
  492. cts_tfm = crypto_alloc_sync_skcipher(gk5e->encrypt_name, 0, 0);
  493. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cts_tfm);
  494. err = crypto_sync_skcipher_setkey(cts_tfm, param->Ke->data, param->Ke->len);
  495. KUNIT_ASSERT_EQ(test, err, 0);
  496. iv = kunit_kzalloc(test, crypto_sync_skcipher_ivsize(cts_tfm), GFP_KERNEL);
  497. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, iv);
  498. text = kunit_kzalloc(test, param->plaintext->len, GFP_KERNEL);
  499. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, text);
  500. memcpy(text, param->plaintext->data, param->plaintext->len);
  501. memset(&buf, 0, sizeof(buf));
  502. buf.head[0].iov_base = text;
  503. buf.head[0].iov_len = param->plaintext->len;
  504. buf.len = buf.head[0].iov_len;
  505. /* Act */
  506. err = krb5_cbc_cts_encrypt(cts_tfm, cbc_tfm, 0, &buf, NULL,
  507. iv, crypto_sync_skcipher_ivsize(cts_tfm));
  508. KUNIT_ASSERT_EQ(test, err, 0);
  509. /* Assert */
  510. KUNIT_EXPECT_EQ_MSG(test,
  511. param->expected_result->len, buf.len,
  512. "ciphertext length mismatch");
  513. KUNIT_EXPECT_EQ_MSG(test,
  514. memcmp(param->expected_result->data,
  515. text, param->expected_result->len), 0,
  516. "ciphertext mismatch");
  517. KUNIT_EXPECT_EQ_MSG(test,
  518. memcmp(param->next_iv->data, iv,
  519. param->next_iv->len), 0,
  520. "IV mismatch");
  521. crypto_free_sync_skcipher(cts_tfm);
  522. crypto_free_sync_skcipher(cbc_tfm);
  523. }
  524. static struct kunit_case rfc3962_test_cases[] = {
  525. {
  526. .name = "RFC 3962 encryption",
  527. .run_case = rfc3962_encrypt_case,
  528. .generate_params = rfc3962_encrypt_gen_params,
  529. },
  530. {}
  531. };
  532. static struct kunit_suite rfc3962_suite = {
  533. .name = "RFC 3962 suite",
  534. .test_cases = rfc3962_test_cases,
  535. };
  536. /*
  537. * From RFC 6803 Section 10. Test vectors
  538. *
  539. * Sample results for key derivation
  540. *
  541. * Copyright (c) 2012 IETF Trust and the persons identified as the
  542. * document authors. All rights reserved.
  543. */
  544. DEFINE_HEX_XDR_NETOBJ(camellia128_cts_cmac_basekey,
  545. 0x57, 0xd0, 0x29, 0x72, 0x98, 0xff, 0xd9, 0xd3,
  546. 0x5d, 0xe5, 0xa4, 0x7f, 0xb4, 0xbd, 0xe2, 0x4b
  547. );
  548. DEFINE_HEX_XDR_NETOBJ(camellia128_cts_cmac_Kc,
  549. 0xd1, 0x55, 0x77, 0x5a, 0x20, 0x9d, 0x05, 0xf0,
  550. 0x2b, 0x38, 0xd4, 0x2a, 0x38, 0x9e, 0x5a, 0x56
  551. );
  552. DEFINE_HEX_XDR_NETOBJ(camellia128_cts_cmac_Ke,
  553. 0x64, 0xdf, 0x83, 0xf8, 0x5a, 0x53, 0x2f, 0x17,
  554. 0x57, 0x7d, 0x8c, 0x37, 0x03, 0x57, 0x96, 0xab
  555. );
  556. DEFINE_HEX_XDR_NETOBJ(camellia128_cts_cmac_Ki,
  557. 0x3e, 0x4f, 0xbd, 0xf3, 0x0f, 0xb8, 0x25, 0x9c,
  558. 0x42, 0x5c, 0xb6, 0xc9, 0x6f, 0x1f, 0x46, 0x35
  559. );
  560. DEFINE_HEX_XDR_NETOBJ(camellia256_cts_cmac_basekey,
  561. 0xb9, 0xd6, 0x82, 0x8b, 0x20, 0x56, 0xb7, 0xbe,
  562. 0x65, 0x6d, 0x88, 0xa1, 0x23, 0xb1, 0xfa, 0xc6,
  563. 0x82, 0x14, 0xac, 0x2b, 0x72, 0x7e, 0xcf, 0x5f,
  564. 0x69, 0xaf, 0xe0, 0xc4, 0xdf, 0x2a, 0x6d, 0x2c
  565. );
  566. DEFINE_HEX_XDR_NETOBJ(camellia256_cts_cmac_Kc,
  567. 0xe4, 0x67, 0xf9, 0xa9, 0x55, 0x2b, 0xc7, 0xd3,
  568. 0x15, 0x5a, 0x62, 0x20, 0xaf, 0x9c, 0x19, 0x22,
  569. 0x0e, 0xee, 0xd4, 0xff, 0x78, 0xb0, 0xd1, 0xe6,
  570. 0xa1, 0x54, 0x49, 0x91, 0x46, 0x1a, 0x9e, 0x50
  571. );
  572. DEFINE_HEX_XDR_NETOBJ(camellia256_cts_cmac_Ke,
  573. 0x41, 0x2a, 0xef, 0xc3, 0x62, 0xa7, 0x28, 0x5f,
  574. 0xc3, 0x96, 0x6c, 0x6a, 0x51, 0x81, 0xe7, 0x60,
  575. 0x5a, 0xe6, 0x75, 0x23, 0x5b, 0x6d, 0x54, 0x9f,
  576. 0xbf, 0xc9, 0xab, 0x66, 0x30, 0xa4, 0xc6, 0x04
  577. );
  578. DEFINE_HEX_XDR_NETOBJ(camellia256_cts_cmac_Ki,
  579. 0xfa, 0x62, 0x4f, 0xa0, 0xe5, 0x23, 0x99, 0x3f,
  580. 0xa3, 0x88, 0xae, 0xfd, 0xc6, 0x7e, 0x67, 0xeb,
  581. 0xcd, 0x8c, 0x08, 0xe8, 0xa0, 0x24, 0x6b, 0x1d,
  582. 0x73, 0xb0, 0xd1, 0xdd, 0x9f, 0xc5, 0x82, 0xb0
  583. );
  584. DEFINE_HEX_XDR_NETOBJ(usage_checksum,
  585. 0x00, 0x00, 0x00, 0x02, KEY_USAGE_SEED_CHECKSUM
  586. );
  587. DEFINE_HEX_XDR_NETOBJ(usage_encryption,
  588. 0x00, 0x00, 0x00, 0x02, KEY_USAGE_SEED_ENCRYPTION
  589. );
  590. DEFINE_HEX_XDR_NETOBJ(usage_integrity,
  591. 0x00, 0x00, 0x00, 0x02, KEY_USAGE_SEED_INTEGRITY
  592. );
  593. static const struct gss_krb5_test_param rfc6803_kdf_test_params[] = {
  594. {
  595. .desc = "Derive Kc subkey for camellia128-cts-cmac",
  596. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  597. .base_key = &camellia128_cts_cmac_basekey,
  598. .usage = &usage_checksum,
  599. .expected_result = &camellia128_cts_cmac_Kc,
  600. },
  601. {
  602. .desc = "Derive Ke subkey for camellia128-cts-cmac",
  603. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  604. .base_key = &camellia128_cts_cmac_basekey,
  605. .usage = &usage_encryption,
  606. .expected_result = &camellia128_cts_cmac_Ke,
  607. },
  608. {
  609. .desc = "Derive Ki subkey for camellia128-cts-cmac",
  610. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  611. .base_key = &camellia128_cts_cmac_basekey,
  612. .usage = &usage_integrity,
  613. .expected_result = &camellia128_cts_cmac_Ki,
  614. },
  615. {
  616. .desc = "Derive Kc subkey for camellia256-cts-cmac",
  617. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  618. .base_key = &camellia256_cts_cmac_basekey,
  619. .usage = &usage_checksum,
  620. .expected_result = &camellia256_cts_cmac_Kc,
  621. },
  622. {
  623. .desc = "Derive Ke subkey for camellia256-cts-cmac",
  624. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  625. .base_key = &camellia256_cts_cmac_basekey,
  626. .usage = &usage_encryption,
  627. .expected_result = &camellia256_cts_cmac_Ke,
  628. },
  629. {
  630. .desc = "Derive Ki subkey for camellia256-cts-cmac",
  631. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  632. .base_key = &camellia256_cts_cmac_basekey,
  633. .usage = &usage_integrity,
  634. .expected_result = &camellia256_cts_cmac_Ki,
  635. },
  636. };
  637. /* Creates the function rfc6803_kdf_gen_params */
  638. KUNIT_ARRAY_PARAM(rfc6803_kdf, rfc6803_kdf_test_params, gss_krb5_get_desc);
  639. /*
  640. * From RFC 6803 Section 10. Test vectors
  641. *
  642. * Sample checksums.
  643. *
  644. * Copyright (c) 2012 IETF Trust and the persons identified as the
  645. * document authors. All rights reserved.
  646. *
  647. * XXX: These tests are likely to fail on EBCDIC or Unicode platforms.
  648. */
  649. DEFINE_STR_XDR_NETOBJ(rfc6803_checksum_test1_plaintext,
  650. "abcdefghijk");
  651. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test1_basekey,
  652. 0x1d, 0xc4, 0x6a, 0x8d, 0x76, 0x3f, 0x4f, 0x93,
  653. 0x74, 0x2b, 0xcb, 0xa3, 0x38, 0x75, 0x76, 0xc3
  654. );
  655. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test1_usage,
  656. 0x00, 0x00, 0x00, 0x07, KEY_USAGE_SEED_CHECKSUM
  657. );
  658. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test1_expected_result,
  659. 0x11, 0x78, 0xe6, 0xc5, 0xc4, 0x7a, 0x8c, 0x1a,
  660. 0xe0, 0xc4, 0xb9, 0xc7, 0xd4, 0xeb, 0x7b, 0x6b
  661. );
  662. DEFINE_STR_XDR_NETOBJ(rfc6803_checksum_test2_plaintext,
  663. "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  664. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test2_basekey,
  665. 0x50, 0x27, 0xbc, 0x23, 0x1d, 0x0f, 0x3a, 0x9d,
  666. 0x23, 0x33, 0x3f, 0x1c, 0xa6, 0xfd, 0xbe, 0x7c
  667. );
  668. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test2_usage,
  669. 0x00, 0x00, 0x00, 0x08, KEY_USAGE_SEED_CHECKSUM
  670. );
  671. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test2_expected_result,
  672. 0xd1, 0xb3, 0x4f, 0x70, 0x04, 0xa7, 0x31, 0xf2,
  673. 0x3a, 0x0c, 0x00, 0xbf, 0x6c, 0x3f, 0x75, 0x3a
  674. );
  675. DEFINE_STR_XDR_NETOBJ(rfc6803_checksum_test3_plaintext,
  676. "123456789");
  677. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test3_basekey,
  678. 0xb6, 0x1c, 0x86, 0xcc, 0x4e, 0x5d, 0x27, 0x57,
  679. 0x54, 0x5a, 0xd4, 0x23, 0x39, 0x9f, 0xb7, 0x03,
  680. 0x1e, 0xca, 0xb9, 0x13, 0xcb, 0xb9, 0x00, 0xbd,
  681. 0x7a, 0x3c, 0x6d, 0xd8, 0xbf, 0x92, 0x01, 0x5b
  682. );
  683. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test3_usage,
  684. 0x00, 0x00, 0x00, 0x09, KEY_USAGE_SEED_CHECKSUM
  685. );
  686. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test3_expected_result,
  687. 0x87, 0xa1, 0x2c, 0xfd, 0x2b, 0x96, 0x21, 0x48,
  688. 0x10, 0xf0, 0x1c, 0x82, 0x6e, 0x77, 0x44, 0xb1
  689. );
  690. DEFINE_STR_XDR_NETOBJ(rfc6803_checksum_test4_plaintext,
  691. "!@#$%^&*()!@#$%^&*()!@#$%^&*()");
  692. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test4_basekey,
  693. 0x32, 0x16, 0x4c, 0x5b, 0x43, 0x4d, 0x1d, 0x15,
  694. 0x38, 0xe4, 0xcf, 0xd9, 0xbe, 0x80, 0x40, 0xfe,
  695. 0x8c, 0x4a, 0xc7, 0xac, 0xc4, 0xb9, 0x3d, 0x33,
  696. 0x14, 0xd2, 0x13, 0x36, 0x68, 0x14, 0x7a, 0x05
  697. );
  698. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test4_usage,
  699. 0x00, 0x00, 0x00, 0x0a, KEY_USAGE_SEED_CHECKSUM
  700. );
  701. DEFINE_HEX_XDR_NETOBJ(rfc6803_checksum_test4_expected_result,
  702. 0x3f, 0xa0, 0xb4, 0x23, 0x55, 0xe5, 0x2b, 0x18,
  703. 0x91, 0x87, 0x29, 0x4a, 0xa2, 0x52, 0xab, 0x64
  704. );
  705. static const struct gss_krb5_test_param rfc6803_checksum_test_params[] = {
  706. {
  707. .desc = "camellia128-cts-cmac checksum test 1",
  708. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  709. .base_key = &rfc6803_checksum_test1_basekey,
  710. .usage = &rfc6803_checksum_test1_usage,
  711. .plaintext = &rfc6803_checksum_test1_plaintext,
  712. .expected_result = &rfc6803_checksum_test1_expected_result,
  713. },
  714. {
  715. .desc = "camellia128-cts-cmac checksum test 2",
  716. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  717. .base_key = &rfc6803_checksum_test2_basekey,
  718. .usage = &rfc6803_checksum_test2_usage,
  719. .plaintext = &rfc6803_checksum_test2_plaintext,
  720. .expected_result = &rfc6803_checksum_test2_expected_result,
  721. },
  722. {
  723. .desc = "camellia256-cts-cmac checksum test 3",
  724. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  725. .base_key = &rfc6803_checksum_test3_basekey,
  726. .usage = &rfc6803_checksum_test3_usage,
  727. .plaintext = &rfc6803_checksum_test3_plaintext,
  728. .expected_result = &rfc6803_checksum_test3_expected_result,
  729. },
  730. {
  731. .desc = "camellia256-cts-cmac checksum test 4",
  732. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  733. .base_key = &rfc6803_checksum_test4_basekey,
  734. .usage = &rfc6803_checksum_test4_usage,
  735. .plaintext = &rfc6803_checksum_test4_plaintext,
  736. .expected_result = &rfc6803_checksum_test4_expected_result,
  737. },
  738. };
  739. /* Creates the function rfc6803_checksum_gen_params */
  740. KUNIT_ARRAY_PARAM(rfc6803_checksum, rfc6803_checksum_test_params,
  741. gss_krb5_get_desc);
  742. /*
  743. * From RFC 6803 Section 10. Test vectors
  744. *
  745. * Sample encryptions (all using the default cipher state)
  746. *
  747. * Copyright (c) 2012 IETF Trust and the persons identified as the
  748. * document authors. All rights reserved.
  749. *
  750. * Key usage values are from errata 4326 against RFC 6803.
  751. */
  752. static const struct xdr_netobj rfc6803_enc_empty_plaintext = {
  753. .len = 0,
  754. };
  755. DEFINE_STR_XDR_NETOBJ(rfc6803_enc_1byte_plaintext, "1");
  756. DEFINE_STR_XDR_NETOBJ(rfc6803_enc_9byte_plaintext, "9 bytesss");
  757. DEFINE_STR_XDR_NETOBJ(rfc6803_enc_13byte_plaintext, "13 bytes byte");
  758. DEFINE_STR_XDR_NETOBJ(rfc6803_enc_30byte_plaintext,
  759. "30 bytes bytes bytes bytes byt"
  760. );
  761. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test1_confounder,
  762. 0xb6, 0x98, 0x22, 0xa1, 0x9a, 0x6b, 0x09, 0xc0,
  763. 0xeb, 0xc8, 0x55, 0x7d, 0x1f, 0x1b, 0x6c, 0x0a
  764. );
  765. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test1_basekey,
  766. 0x1d, 0xc4, 0x6a, 0x8d, 0x76, 0x3f, 0x4f, 0x93,
  767. 0x74, 0x2b, 0xcb, 0xa3, 0x38, 0x75, 0x76, 0xc3
  768. );
  769. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test1_expected_result,
  770. 0xc4, 0x66, 0xf1, 0x87, 0x10, 0x69, 0x92, 0x1e,
  771. 0xdb, 0x7c, 0x6f, 0xde, 0x24, 0x4a, 0x52, 0xdb,
  772. 0x0b, 0xa1, 0x0e, 0xdc, 0x19, 0x7b, 0xdb, 0x80,
  773. 0x06, 0x65, 0x8c, 0xa3, 0xcc, 0xce, 0x6e, 0xb8
  774. );
  775. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test2_confounder,
  776. 0x6f, 0x2f, 0xc3, 0xc2, 0xa1, 0x66, 0xfd, 0x88,
  777. 0x98, 0x96, 0x7a, 0x83, 0xde, 0x95, 0x96, 0xd9
  778. );
  779. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test2_basekey,
  780. 0x50, 0x27, 0xbc, 0x23, 0x1d, 0x0f, 0x3a, 0x9d,
  781. 0x23, 0x33, 0x3f, 0x1c, 0xa6, 0xfd, 0xbe, 0x7c
  782. );
  783. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test2_expected_result,
  784. 0x84, 0x2d, 0x21, 0xfd, 0x95, 0x03, 0x11, 0xc0,
  785. 0xdd, 0x46, 0x4a, 0x3f, 0x4b, 0xe8, 0xd6, 0xda,
  786. 0x88, 0xa5, 0x6d, 0x55, 0x9c, 0x9b, 0x47, 0xd3,
  787. 0xf9, 0xa8, 0x50, 0x67, 0xaf, 0x66, 0x15, 0x59,
  788. 0xb8
  789. );
  790. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test3_confounder,
  791. 0xa5, 0xb4, 0xa7, 0x1e, 0x07, 0x7a, 0xee, 0xf9,
  792. 0x3c, 0x87, 0x63, 0xc1, 0x8f, 0xdb, 0x1f, 0x10
  793. );
  794. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test3_basekey,
  795. 0xa1, 0xbb, 0x61, 0xe8, 0x05, 0xf9, 0xba, 0x6d,
  796. 0xde, 0x8f, 0xdb, 0xdd, 0xc0, 0x5c, 0xde, 0xa0
  797. );
  798. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test3_expected_result,
  799. 0x61, 0x9f, 0xf0, 0x72, 0xe3, 0x62, 0x86, 0xff,
  800. 0x0a, 0x28, 0xde, 0xb3, 0xa3, 0x52, 0xec, 0x0d,
  801. 0x0e, 0xdf, 0x5c, 0x51, 0x60, 0xd6, 0x63, 0xc9,
  802. 0x01, 0x75, 0x8c, 0xcf, 0x9d, 0x1e, 0xd3, 0x3d,
  803. 0x71, 0xdb, 0x8f, 0x23, 0xaa, 0xbf, 0x83, 0x48,
  804. 0xa0
  805. );
  806. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test4_confounder,
  807. 0x19, 0xfe, 0xe4, 0x0d, 0x81, 0x0c, 0x52, 0x4b,
  808. 0x5b, 0x22, 0xf0, 0x18, 0x74, 0xc6, 0x93, 0xda
  809. );
  810. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test4_basekey,
  811. 0x2c, 0xa2, 0x7a, 0x5f, 0xaf, 0x55, 0x32, 0x24,
  812. 0x45, 0x06, 0x43, 0x4e, 0x1c, 0xef, 0x66, 0x76
  813. );
  814. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test4_expected_result,
  815. 0xb8, 0xec, 0xa3, 0x16, 0x7a, 0xe6, 0x31, 0x55,
  816. 0x12, 0xe5, 0x9f, 0x98, 0xa7, 0xc5, 0x00, 0x20,
  817. 0x5e, 0x5f, 0x63, 0xff, 0x3b, 0xb3, 0x89, 0xaf,
  818. 0x1c, 0x41, 0xa2, 0x1d, 0x64, 0x0d, 0x86, 0x15,
  819. 0xc9, 0xed, 0x3f, 0xbe, 0xb0, 0x5a, 0xb6, 0xac,
  820. 0xb6, 0x76, 0x89, 0xb5, 0xea
  821. );
  822. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test5_confounder,
  823. 0xca, 0x7a, 0x7a, 0xb4, 0xbe, 0x19, 0x2d, 0xab,
  824. 0xd6, 0x03, 0x50, 0x6d, 0xb1, 0x9c, 0x39, 0xe2
  825. );
  826. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test5_basekey,
  827. 0x78, 0x24, 0xf8, 0xc1, 0x6f, 0x83, 0xff, 0x35,
  828. 0x4c, 0x6b, 0xf7, 0x51, 0x5b, 0x97, 0x3f, 0x43
  829. );
  830. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test5_expected_result,
  831. 0xa2, 0x6a, 0x39, 0x05, 0xa4, 0xff, 0xd5, 0x81,
  832. 0x6b, 0x7b, 0x1e, 0x27, 0x38, 0x0d, 0x08, 0x09,
  833. 0x0c, 0x8e, 0xc1, 0xf3, 0x04, 0x49, 0x6e, 0x1a,
  834. 0xbd, 0xcd, 0x2b, 0xdc, 0xd1, 0xdf, 0xfc, 0x66,
  835. 0x09, 0x89, 0xe1, 0x17, 0xa7, 0x13, 0xdd, 0xbb,
  836. 0x57, 0xa4, 0x14, 0x6c, 0x15, 0x87, 0xcb, 0xa4,
  837. 0x35, 0x66, 0x65, 0x59, 0x1d, 0x22, 0x40, 0x28,
  838. 0x2f, 0x58, 0x42, 0xb1, 0x05, 0xa5
  839. );
  840. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test6_confounder,
  841. 0x3c, 0xbb, 0xd2, 0xb4, 0x59, 0x17, 0x94, 0x10,
  842. 0x67, 0xf9, 0x65, 0x99, 0xbb, 0x98, 0x92, 0x6c
  843. );
  844. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test6_basekey,
  845. 0xb6, 0x1c, 0x86, 0xcc, 0x4e, 0x5d, 0x27, 0x57,
  846. 0x54, 0x5a, 0xd4, 0x23, 0x39, 0x9f, 0xb7, 0x03,
  847. 0x1e, 0xca, 0xb9, 0x13, 0xcb, 0xb9, 0x00, 0xbd,
  848. 0x7a, 0x3c, 0x6d, 0xd8, 0xbf, 0x92, 0x01, 0x5b
  849. );
  850. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test6_expected_result,
  851. 0x03, 0x88, 0x6d, 0x03, 0x31, 0x0b, 0x47, 0xa6,
  852. 0xd8, 0xf0, 0x6d, 0x7b, 0x94, 0xd1, 0xdd, 0x83,
  853. 0x7e, 0xcc, 0xe3, 0x15, 0xef, 0x65, 0x2a, 0xff,
  854. 0x62, 0x08, 0x59, 0xd9, 0x4a, 0x25, 0x92, 0x66
  855. );
  856. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test7_confounder,
  857. 0xde, 0xf4, 0x87, 0xfc, 0xeb, 0xe6, 0xde, 0x63,
  858. 0x46, 0xd4, 0xda, 0x45, 0x21, 0xbb, 0xa2, 0xd2
  859. );
  860. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test7_basekey,
  861. 0x1b, 0x97, 0xfe, 0x0a, 0x19, 0x0e, 0x20, 0x21,
  862. 0xeb, 0x30, 0x75, 0x3e, 0x1b, 0x6e, 0x1e, 0x77,
  863. 0xb0, 0x75, 0x4b, 0x1d, 0x68, 0x46, 0x10, 0x35,
  864. 0x58, 0x64, 0x10, 0x49, 0x63, 0x46, 0x38, 0x33
  865. );
  866. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test7_expected_result,
  867. 0x2c, 0x9c, 0x15, 0x70, 0x13, 0x3c, 0x99, 0xbf,
  868. 0x6a, 0x34, 0xbc, 0x1b, 0x02, 0x12, 0x00, 0x2f,
  869. 0xd1, 0x94, 0x33, 0x87, 0x49, 0xdb, 0x41, 0x35,
  870. 0x49, 0x7a, 0x34, 0x7c, 0xfc, 0xd9, 0xd1, 0x8a,
  871. 0x12
  872. );
  873. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test8_confounder,
  874. 0xad, 0x4f, 0xf9, 0x04, 0xd3, 0x4e, 0x55, 0x53,
  875. 0x84, 0xb1, 0x41, 0x00, 0xfc, 0x46, 0x5f, 0x88
  876. );
  877. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test8_basekey,
  878. 0x32, 0x16, 0x4c, 0x5b, 0x43, 0x4d, 0x1d, 0x15,
  879. 0x38, 0xe4, 0xcf, 0xd9, 0xbe, 0x80, 0x40, 0xfe,
  880. 0x8c, 0x4a, 0xc7, 0xac, 0xc4, 0xb9, 0x3d, 0x33,
  881. 0x14, 0xd2, 0x13, 0x36, 0x68, 0x14, 0x7a, 0x05
  882. );
  883. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test8_expected_result,
  884. 0x9c, 0x6d, 0xe7, 0x5f, 0x81, 0x2d, 0xe7, 0xed,
  885. 0x0d, 0x28, 0xb2, 0x96, 0x35, 0x57, 0xa1, 0x15,
  886. 0x64, 0x09, 0x98, 0x27, 0x5b, 0x0a, 0xf5, 0x15,
  887. 0x27, 0x09, 0x91, 0x3f, 0xf5, 0x2a, 0x2a, 0x9c,
  888. 0x8e, 0x63, 0xb8, 0x72, 0xf9, 0x2e, 0x64, 0xc8,
  889. 0x39
  890. );
  891. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test9_confounder,
  892. 0xcf, 0x9b, 0xca, 0x6d, 0xf1, 0x14, 0x4e, 0x0c,
  893. 0x0a, 0xf9, 0xb8, 0xf3, 0x4c, 0x90, 0xd5, 0x14
  894. );
  895. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test9_basekey,
  896. 0xb0, 0x38, 0xb1, 0x32, 0xcd, 0x8e, 0x06, 0x61,
  897. 0x22, 0x67, 0xfa, 0xb7, 0x17, 0x00, 0x66, 0xd8,
  898. 0x8a, 0xec, 0xcb, 0xa0, 0xb7, 0x44, 0xbf, 0xc6,
  899. 0x0d, 0xc8, 0x9b, 0xca, 0x18, 0x2d, 0x07, 0x15
  900. );
  901. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test9_expected_result,
  902. 0xee, 0xec, 0x85, 0xa9, 0x81, 0x3c, 0xdc, 0x53,
  903. 0x67, 0x72, 0xab, 0x9b, 0x42, 0xde, 0xfc, 0x57,
  904. 0x06, 0xf7, 0x26, 0xe9, 0x75, 0xdd, 0xe0, 0x5a,
  905. 0x87, 0xeb, 0x54, 0x06, 0xea, 0x32, 0x4c, 0xa1,
  906. 0x85, 0xc9, 0x98, 0x6b, 0x42, 0xaa, 0xbe, 0x79,
  907. 0x4b, 0x84, 0x82, 0x1b, 0xee
  908. );
  909. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test10_confounder,
  910. 0x64, 0x4d, 0xef, 0x38, 0xda, 0x35, 0x00, 0x72,
  911. 0x75, 0x87, 0x8d, 0x21, 0x68, 0x55, 0xe2, 0x28
  912. );
  913. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test10_basekey,
  914. 0xcc, 0xfc, 0xd3, 0x49, 0xbf, 0x4c, 0x66, 0x77,
  915. 0xe8, 0x6e, 0x4b, 0x02, 0xb8, 0xea, 0xb9, 0x24,
  916. 0xa5, 0x46, 0xac, 0x73, 0x1c, 0xf9, 0xbf, 0x69,
  917. 0x89, 0xb9, 0x96, 0xe7, 0xd6, 0xbf, 0xbb, 0xa7
  918. );
  919. DEFINE_HEX_XDR_NETOBJ(rfc6803_enc_test10_expected_result,
  920. 0x0e, 0x44, 0x68, 0x09, 0x85, 0x85, 0x5f, 0x2d,
  921. 0x1f, 0x18, 0x12, 0x52, 0x9c, 0xa8, 0x3b, 0xfd,
  922. 0x8e, 0x34, 0x9d, 0xe6, 0xfd, 0x9a, 0xda, 0x0b,
  923. 0xaa, 0xa0, 0x48, 0xd6, 0x8e, 0x26, 0x5f, 0xeb,
  924. 0xf3, 0x4a, 0xd1, 0x25, 0x5a, 0x34, 0x49, 0x99,
  925. 0xad, 0x37, 0x14, 0x68, 0x87, 0xa6, 0xc6, 0x84,
  926. 0x57, 0x31, 0xac, 0x7f, 0x46, 0x37, 0x6a, 0x05,
  927. 0x04, 0xcd, 0x06, 0x57, 0x14, 0x74
  928. );
  929. static const struct gss_krb5_test_param rfc6803_encrypt_test_params[] = {
  930. {
  931. .desc = "Encrypt empty plaintext with camellia128-cts-cmac",
  932. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  933. .constant = 0,
  934. .base_key = &rfc6803_enc_test1_basekey,
  935. .plaintext = &rfc6803_enc_empty_plaintext,
  936. .confounder = &rfc6803_enc_test1_confounder,
  937. .expected_result = &rfc6803_enc_test1_expected_result,
  938. },
  939. {
  940. .desc = "Encrypt 1 byte with camellia128-cts-cmac",
  941. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  942. .constant = 1,
  943. .base_key = &rfc6803_enc_test2_basekey,
  944. .plaintext = &rfc6803_enc_1byte_plaintext,
  945. .confounder = &rfc6803_enc_test2_confounder,
  946. .expected_result = &rfc6803_enc_test2_expected_result,
  947. },
  948. {
  949. .desc = "Encrypt 9 bytes with camellia128-cts-cmac",
  950. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  951. .constant = 2,
  952. .base_key = &rfc6803_enc_test3_basekey,
  953. .plaintext = &rfc6803_enc_9byte_plaintext,
  954. .confounder = &rfc6803_enc_test3_confounder,
  955. .expected_result = &rfc6803_enc_test3_expected_result,
  956. },
  957. {
  958. .desc = "Encrypt 13 bytes with camellia128-cts-cmac",
  959. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  960. .constant = 3,
  961. .base_key = &rfc6803_enc_test4_basekey,
  962. .plaintext = &rfc6803_enc_13byte_plaintext,
  963. .confounder = &rfc6803_enc_test4_confounder,
  964. .expected_result = &rfc6803_enc_test4_expected_result,
  965. },
  966. {
  967. .desc = "Encrypt 30 bytes with camellia128-cts-cmac",
  968. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  969. .constant = 4,
  970. .base_key = &rfc6803_enc_test5_basekey,
  971. .plaintext = &rfc6803_enc_30byte_plaintext,
  972. .confounder = &rfc6803_enc_test5_confounder,
  973. .expected_result = &rfc6803_enc_test5_expected_result,
  974. },
  975. {
  976. .desc = "Encrypt empty plaintext with camellia256-cts-cmac",
  977. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  978. .constant = 0,
  979. .base_key = &rfc6803_enc_test6_basekey,
  980. .plaintext = &rfc6803_enc_empty_plaintext,
  981. .confounder = &rfc6803_enc_test6_confounder,
  982. .expected_result = &rfc6803_enc_test6_expected_result,
  983. },
  984. {
  985. .desc = "Encrypt 1 byte with camellia256-cts-cmac",
  986. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  987. .constant = 1,
  988. .base_key = &rfc6803_enc_test7_basekey,
  989. .plaintext = &rfc6803_enc_1byte_plaintext,
  990. .confounder = &rfc6803_enc_test7_confounder,
  991. .expected_result = &rfc6803_enc_test7_expected_result,
  992. },
  993. {
  994. .desc = "Encrypt 9 bytes with camellia256-cts-cmac",
  995. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  996. .constant = 2,
  997. .base_key = &rfc6803_enc_test8_basekey,
  998. .plaintext = &rfc6803_enc_9byte_plaintext,
  999. .confounder = &rfc6803_enc_test8_confounder,
  1000. .expected_result = &rfc6803_enc_test8_expected_result,
  1001. },
  1002. {
  1003. .desc = "Encrypt 13 bytes with camellia256-cts-cmac",
  1004. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  1005. .constant = 3,
  1006. .base_key = &rfc6803_enc_test9_basekey,
  1007. .plaintext = &rfc6803_enc_13byte_plaintext,
  1008. .confounder = &rfc6803_enc_test9_confounder,
  1009. .expected_result = &rfc6803_enc_test9_expected_result,
  1010. },
  1011. {
  1012. .desc = "Encrypt 30 bytes with camellia256-cts-cmac",
  1013. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  1014. .constant = 4,
  1015. .base_key = &rfc6803_enc_test10_basekey,
  1016. .plaintext = &rfc6803_enc_30byte_plaintext,
  1017. .confounder = &rfc6803_enc_test10_confounder,
  1018. .expected_result = &rfc6803_enc_test10_expected_result,
  1019. },
  1020. };
  1021. /* Creates the function rfc6803_encrypt_gen_params */
  1022. KUNIT_ARRAY_PARAM(rfc6803_encrypt, rfc6803_encrypt_test_params,
  1023. gss_krb5_get_desc);
  1024. static void rfc6803_encrypt_case(struct kunit *test)
  1025. {
  1026. const struct gss_krb5_test_param *param = test->param_value;
  1027. struct crypto_sync_skcipher *cts_tfm, *cbc_tfm;
  1028. const struct gss_krb5_enctype *gk5e;
  1029. struct xdr_netobj Ke, Ki, checksum;
  1030. u8 usage_data[GSS_KRB5_K5CLENGTH];
  1031. struct xdr_netobj usage = {
  1032. .data = usage_data,
  1033. .len = sizeof(usage_data),
  1034. };
  1035. struct crypto_ahash *ahash_tfm;
  1036. unsigned int blocksize;
  1037. struct xdr_buf buf;
  1038. void *text;
  1039. size_t len;
  1040. u32 err;
  1041. /* Arrange */
  1042. gk5e = gss_krb5_lookup_enctype(param->enctype);
  1043. if (!gk5e)
  1044. kunit_skip(test, "Encryption type is not available");
  1045. memset(usage_data, 0, sizeof(usage_data));
  1046. usage.data[3] = param->constant;
  1047. Ke.len = gk5e->Ke_length;
  1048. Ke.data = kunit_kzalloc(test, Ke.len, GFP_KERNEL);
  1049. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, Ke.data);
  1050. usage.data[4] = KEY_USAGE_SEED_ENCRYPTION;
  1051. err = gk5e->derive_key(gk5e, param->base_key, &Ke, &usage, GFP_KERNEL);
  1052. KUNIT_ASSERT_EQ(test, err, 0);
  1053. cbc_tfm = crypto_alloc_sync_skcipher(gk5e->aux_cipher, 0, 0);
  1054. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cbc_tfm);
  1055. err = crypto_sync_skcipher_setkey(cbc_tfm, Ke.data, Ke.len);
  1056. KUNIT_ASSERT_EQ(test, err, 0);
  1057. cts_tfm = crypto_alloc_sync_skcipher(gk5e->encrypt_name, 0, 0);
  1058. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cts_tfm);
  1059. err = crypto_sync_skcipher_setkey(cts_tfm, Ke.data, Ke.len);
  1060. KUNIT_ASSERT_EQ(test, err, 0);
  1061. blocksize = crypto_sync_skcipher_blocksize(cts_tfm);
  1062. len = param->confounder->len + param->plaintext->len + blocksize;
  1063. text = kunit_kzalloc(test, len, GFP_KERNEL);
  1064. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, text);
  1065. memcpy(text, param->confounder->data, param->confounder->len);
  1066. memcpy(text + param->confounder->len, param->plaintext->data,
  1067. param->plaintext->len);
  1068. memset(&buf, 0, sizeof(buf));
  1069. buf.head[0].iov_base = text;
  1070. buf.head[0].iov_len = param->confounder->len + param->plaintext->len;
  1071. buf.len = buf.head[0].iov_len;
  1072. checksum.len = gk5e->cksumlength;
  1073. checksum.data = kunit_kzalloc(test, checksum.len, GFP_KERNEL);
  1074. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, checksum.data);
  1075. Ki.len = gk5e->Ki_length;
  1076. Ki.data = kunit_kzalloc(test, Ki.len, GFP_KERNEL);
  1077. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, Ki.data);
  1078. usage.data[4] = KEY_USAGE_SEED_INTEGRITY;
  1079. err = gk5e->derive_key(gk5e, param->base_key, &Ki,
  1080. &usage, GFP_KERNEL);
  1081. KUNIT_ASSERT_EQ(test, err, 0);
  1082. ahash_tfm = crypto_alloc_ahash(gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
  1083. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ahash_tfm);
  1084. err = crypto_ahash_setkey(ahash_tfm, Ki.data, Ki.len);
  1085. KUNIT_ASSERT_EQ(test, err, 0);
  1086. /* Act */
  1087. err = gss_krb5_checksum(ahash_tfm, NULL, 0, &buf, 0, &checksum);
  1088. KUNIT_ASSERT_EQ(test, err, 0);
  1089. err = krb5_cbc_cts_encrypt(cts_tfm, cbc_tfm, 0, &buf, NULL, NULL, 0);
  1090. KUNIT_ASSERT_EQ(test, err, 0);
  1091. /* Assert */
  1092. KUNIT_EXPECT_EQ_MSG(test, param->expected_result->len,
  1093. buf.len + checksum.len,
  1094. "ciphertext length mismatch");
  1095. KUNIT_EXPECT_EQ_MSG(test,
  1096. memcmp(param->expected_result->data,
  1097. buf.head[0].iov_base, buf.len), 0,
  1098. "encrypted result mismatch");
  1099. KUNIT_EXPECT_EQ_MSG(test,
  1100. memcmp(param->expected_result->data +
  1101. (param->expected_result->len - checksum.len),
  1102. checksum.data, checksum.len), 0,
  1103. "HMAC mismatch");
  1104. crypto_free_ahash(ahash_tfm);
  1105. crypto_free_sync_skcipher(cts_tfm);
  1106. crypto_free_sync_skcipher(cbc_tfm);
  1107. }
  1108. static struct kunit_case rfc6803_test_cases[] = {
  1109. {
  1110. .name = "RFC 6803 key derivation",
  1111. .run_case = kdf_case,
  1112. .generate_params = rfc6803_kdf_gen_params,
  1113. },
  1114. {
  1115. .name = "RFC 6803 checksum",
  1116. .run_case = checksum_case,
  1117. .generate_params = rfc6803_checksum_gen_params,
  1118. },
  1119. {
  1120. .name = "RFC 6803 encryption",
  1121. .run_case = rfc6803_encrypt_case,
  1122. .generate_params = rfc6803_encrypt_gen_params,
  1123. },
  1124. {}
  1125. };
  1126. static struct kunit_suite rfc6803_suite = {
  1127. .name = "RFC 6803 suite",
  1128. .test_cases = rfc6803_test_cases,
  1129. };
  1130. /*
  1131. * From RFC 8009 Appendix A. Test Vectors
  1132. *
  1133. * Sample results for SHA-2 enctype key derivation
  1134. *
  1135. * This test material is copyright (c) 2016 IETF Trust and the
  1136. * persons identified as the document authors. All rights reserved.
  1137. */
  1138. DEFINE_HEX_XDR_NETOBJ(aes128_cts_hmac_sha256_128_basekey,
  1139. 0x37, 0x05, 0xd9, 0x60, 0x80, 0xc1, 0x77, 0x28,
  1140. 0xa0, 0xe8, 0x00, 0xea, 0xb6, 0xe0, 0xd2, 0x3c
  1141. );
  1142. DEFINE_HEX_XDR_NETOBJ(aes128_cts_hmac_sha256_128_Kc,
  1143. 0xb3, 0x1a, 0x01, 0x8a, 0x48, 0xf5, 0x47, 0x76,
  1144. 0xf4, 0x03, 0xe9, 0xa3, 0x96, 0x32, 0x5d, 0xc3
  1145. );
  1146. DEFINE_HEX_XDR_NETOBJ(aes128_cts_hmac_sha256_128_Ke,
  1147. 0x9b, 0x19, 0x7d, 0xd1, 0xe8, 0xc5, 0x60, 0x9d,
  1148. 0x6e, 0x67, 0xc3, 0xe3, 0x7c, 0x62, 0xc7, 0x2e
  1149. );
  1150. DEFINE_HEX_XDR_NETOBJ(aes128_cts_hmac_sha256_128_Ki,
  1151. 0x9f, 0xda, 0x0e, 0x56, 0xab, 0x2d, 0x85, 0xe1,
  1152. 0x56, 0x9a, 0x68, 0x86, 0x96, 0xc2, 0x6a, 0x6c
  1153. );
  1154. DEFINE_HEX_XDR_NETOBJ(aes256_cts_hmac_sha384_192_basekey,
  1155. 0x6d, 0x40, 0x4d, 0x37, 0xfa, 0xf7, 0x9f, 0x9d,
  1156. 0xf0, 0xd3, 0x35, 0x68, 0xd3, 0x20, 0x66, 0x98,
  1157. 0x00, 0xeb, 0x48, 0x36, 0x47, 0x2e, 0xa8, 0xa0,
  1158. 0x26, 0xd1, 0x6b, 0x71, 0x82, 0x46, 0x0c, 0x52
  1159. );
  1160. DEFINE_HEX_XDR_NETOBJ(aes256_cts_hmac_sha384_192_Kc,
  1161. 0xef, 0x57, 0x18, 0xbe, 0x86, 0xcc, 0x84, 0x96,
  1162. 0x3d, 0x8b, 0xbb, 0x50, 0x31, 0xe9, 0xf5, 0xc4,
  1163. 0xba, 0x41, 0xf2, 0x8f, 0xaf, 0x69, 0xe7, 0x3d
  1164. );
  1165. DEFINE_HEX_XDR_NETOBJ(aes256_cts_hmac_sha384_192_Ke,
  1166. 0x56, 0xab, 0x22, 0xbe, 0xe6, 0x3d, 0x82, 0xd7,
  1167. 0xbc, 0x52, 0x27, 0xf6, 0x77, 0x3f, 0x8e, 0xa7,
  1168. 0xa5, 0xeb, 0x1c, 0x82, 0x51, 0x60, 0xc3, 0x83,
  1169. 0x12, 0x98, 0x0c, 0x44, 0x2e, 0x5c, 0x7e, 0x49
  1170. );
  1171. DEFINE_HEX_XDR_NETOBJ(aes256_cts_hmac_sha384_192_Ki,
  1172. 0x69, 0xb1, 0x65, 0x14, 0xe3, 0xcd, 0x8e, 0x56,
  1173. 0xb8, 0x20, 0x10, 0xd5, 0xc7, 0x30, 0x12, 0xb6,
  1174. 0x22, 0xc4, 0xd0, 0x0f, 0xfc, 0x23, 0xed, 0x1f
  1175. );
  1176. static const struct gss_krb5_test_param rfc8009_kdf_test_params[] = {
  1177. {
  1178. .desc = "Derive Kc subkey for aes128-cts-hmac-sha256-128",
  1179. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1180. .base_key = &aes128_cts_hmac_sha256_128_basekey,
  1181. .usage = &usage_checksum,
  1182. .expected_result = &aes128_cts_hmac_sha256_128_Kc,
  1183. },
  1184. {
  1185. .desc = "Derive Ke subkey for aes128-cts-hmac-sha256-128",
  1186. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1187. .base_key = &aes128_cts_hmac_sha256_128_basekey,
  1188. .usage = &usage_encryption,
  1189. .expected_result = &aes128_cts_hmac_sha256_128_Ke,
  1190. },
  1191. {
  1192. .desc = "Derive Ki subkey for aes128-cts-hmac-sha256-128",
  1193. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1194. .base_key = &aes128_cts_hmac_sha256_128_basekey,
  1195. .usage = &usage_integrity,
  1196. .expected_result = &aes128_cts_hmac_sha256_128_Ki,
  1197. },
  1198. {
  1199. .desc = "Derive Kc subkey for aes256-cts-hmac-sha384-192",
  1200. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1201. .base_key = &aes256_cts_hmac_sha384_192_basekey,
  1202. .usage = &usage_checksum,
  1203. .expected_result = &aes256_cts_hmac_sha384_192_Kc,
  1204. },
  1205. {
  1206. .desc = "Derive Ke subkey for aes256-cts-hmac-sha384-192",
  1207. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1208. .base_key = &aes256_cts_hmac_sha384_192_basekey,
  1209. .usage = &usage_encryption,
  1210. .expected_result = &aes256_cts_hmac_sha384_192_Ke,
  1211. },
  1212. {
  1213. .desc = "Derive Ki subkey for aes256-cts-hmac-sha384-192",
  1214. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1215. .base_key = &aes256_cts_hmac_sha384_192_basekey,
  1216. .usage = &usage_integrity,
  1217. .expected_result = &aes256_cts_hmac_sha384_192_Ki,
  1218. },
  1219. };
  1220. /* Creates the function rfc8009_kdf_gen_params */
  1221. KUNIT_ARRAY_PARAM(rfc8009_kdf, rfc8009_kdf_test_params, gss_krb5_get_desc);
  1222. /*
  1223. * From RFC 8009 Appendix A. Test Vectors
  1224. *
  1225. * These sample checksums use the above sample key derivation results,
  1226. * including use of the same base-key and key usage values.
  1227. *
  1228. * This test material is copyright (c) 2016 IETF Trust and the
  1229. * persons identified as the document authors. All rights reserved.
  1230. */
  1231. DEFINE_HEX_XDR_NETOBJ(rfc8009_checksum_plaintext,
  1232. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1233. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1234. 0x10, 0x11, 0x12, 0x13, 0x14
  1235. );
  1236. DEFINE_HEX_XDR_NETOBJ(rfc8009_checksum_test1_expected_result,
  1237. 0xd7, 0x83, 0x67, 0x18, 0x66, 0x43, 0xd6, 0x7b,
  1238. 0x41, 0x1c, 0xba, 0x91, 0x39, 0xfc, 0x1d, 0xee
  1239. );
  1240. DEFINE_HEX_XDR_NETOBJ(rfc8009_checksum_test2_expected_result,
  1241. 0x45, 0xee, 0x79, 0x15, 0x67, 0xee, 0xfc, 0xa3,
  1242. 0x7f, 0x4a, 0xc1, 0xe0, 0x22, 0x2d, 0xe8, 0x0d,
  1243. 0x43, 0xc3, 0xbf, 0xa0, 0x66, 0x99, 0x67, 0x2a
  1244. );
  1245. static const struct gss_krb5_test_param rfc8009_checksum_test_params[] = {
  1246. {
  1247. .desc = "Checksum with aes128-cts-hmac-sha256-128",
  1248. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1249. .base_key = &aes128_cts_hmac_sha256_128_basekey,
  1250. .usage = &usage_checksum,
  1251. .plaintext = &rfc8009_checksum_plaintext,
  1252. .expected_result = &rfc8009_checksum_test1_expected_result,
  1253. },
  1254. {
  1255. .desc = "Checksum with aes256-cts-hmac-sha384-192",
  1256. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1257. .base_key = &aes256_cts_hmac_sha384_192_basekey,
  1258. .usage = &usage_checksum,
  1259. .plaintext = &rfc8009_checksum_plaintext,
  1260. .expected_result = &rfc8009_checksum_test2_expected_result,
  1261. },
  1262. };
  1263. /* Creates the function rfc8009_checksum_gen_params */
  1264. KUNIT_ARRAY_PARAM(rfc8009_checksum, rfc8009_checksum_test_params,
  1265. gss_krb5_get_desc);
  1266. /*
  1267. * From RFC 8009 Appendix A. Test Vectors
  1268. *
  1269. * Sample encryptions (all using the default cipher state):
  1270. * --------------------------------------------------------
  1271. *
  1272. * These sample encryptions use the above sample key derivation results,
  1273. * including use of the same base-key and key usage values.
  1274. *
  1275. * This test material is copyright (c) 2016 IETF Trust and the
  1276. * persons identified as the document authors. All rights reserved.
  1277. */
  1278. static const struct xdr_netobj rfc8009_enc_empty_plaintext = {
  1279. .len = 0,
  1280. };
  1281. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_short_plaintext,
  1282. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05
  1283. );
  1284. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_block_plaintext,
  1285. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1286. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  1287. );
  1288. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_long_plaintext,
  1289. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1290. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1291. 0x10, 0x11, 0x12, 0x13, 0x14
  1292. );
  1293. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test1_confounder,
  1294. 0x7e, 0x58, 0x95, 0xea, 0xf2, 0x67, 0x24, 0x35,
  1295. 0xba, 0xd8, 0x17, 0xf5, 0x45, 0xa3, 0x71, 0x48
  1296. );
  1297. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test1_expected_result,
  1298. 0xef, 0x85, 0xfb, 0x89, 0x0b, 0xb8, 0x47, 0x2f,
  1299. 0x4d, 0xab, 0x20, 0x39, 0x4d, 0xca, 0x78, 0x1d
  1300. );
  1301. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test1_expected_hmac,
  1302. 0xad, 0x87, 0x7e, 0xda, 0x39, 0xd5, 0x0c, 0x87,
  1303. 0x0c, 0x0d, 0x5a, 0x0a, 0x8e, 0x48, 0xc7, 0x18
  1304. );
  1305. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test2_confounder,
  1306. 0x7b, 0xca, 0x28, 0x5e, 0x2f, 0xd4, 0x13, 0x0f,
  1307. 0xb5, 0x5b, 0x1a, 0x5c, 0x83, 0xbc, 0x5b, 0x24
  1308. );
  1309. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test2_expected_result,
  1310. 0x84, 0xd7, 0xf3, 0x07, 0x54, 0xed, 0x98, 0x7b,
  1311. 0xab, 0x0b, 0xf3, 0x50, 0x6b, 0xeb, 0x09, 0xcf,
  1312. 0xb5, 0x54, 0x02, 0xce, 0xf7, 0xe6
  1313. );
  1314. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test2_expected_hmac,
  1315. 0x87, 0x7c, 0xe9, 0x9e, 0x24, 0x7e, 0x52, 0xd1,
  1316. 0x6e, 0xd4, 0x42, 0x1d, 0xfd, 0xf8, 0x97, 0x6c
  1317. );
  1318. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test3_confounder,
  1319. 0x56, 0xab, 0x21, 0x71, 0x3f, 0xf6, 0x2c, 0x0a,
  1320. 0x14, 0x57, 0x20, 0x0f, 0x6f, 0xa9, 0x94, 0x8f
  1321. );
  1322. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test3_expected_result,
  1323. 0x35, 0x17, 0xd6, 0x40, 0xf5, 0x0d, 0xdc, 0x8a,
  1324. 0xd3, 0x62, 0x87, 0x22, 0xb3, 0x56, 0x9d, 0x2a,
  1325. 0xe0, 0x74, 0x93, 0xfa, 0x82, 0x63, 0x25, 0x40,
  1326. 0x80, 0xea, 0x65, 0xc1, 0x00, 0x8e, 0x8f, 0xc2
  1327. );
  1328. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test3_expected_hmac,
  1329. 0x95, 0xfb, 0x48, 0x52, 0xe7, 0xd8, 0x3e, 0x1e,
  1330. 0x7c, 0x48, 0xc3, 0x7e, 0xeb, 0xe6, 0xb0, 0xd3
  1331. );
  1332. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test4_confounder,
  1333. 0xa7, 0xa4, 0xe2, 0x9a, 0x47, 0x28, 0xce, 0x10,
  1334. 0x66, 0x4f, 0xb6, 0x4e, 0x49, 0xad, 0x3f, 0xac
  1335. );
  1336. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test4_expected_result,
  1337. 0x72, 0x0f, 0x73, 0xb1, 0x8d, 0x98, 0x59, 0xcd,
  1338. 0x6c, 0xcb, 0x43, 0x46, 0x11, 0x5c, 0xd3, 0x36,
  1339. 0xc7, 0x0f, 0x58, 0xed, 0xc0, 0xc4, 0x43, 0x7c,
  1340. 0x55, 0x73, 0x54, 0x4c, 0x31, 0xc8, 0x13, 0xbc,
  1341. 0xe1, 0xe6, 0xd0, 0x72, 0xc1
  1342. );
  1343. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test4_expected_hmac,
  1344. 0x86, 0xb3, 0x9a, 0x41, 0x3c, 0x2f, 0x92, 0xca,
  1345. 0x9b, 0x83, 0x34, 0xa2, 0x87, 0xff, 0xcb, 0xfc
  1346. );
  1347. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test5_confounder,
  1348. 0xf7, 0x64, 0xe9, 0xfa, 0x15, 0xc2, 0x76, 0x47,
  1349. 0x8b, 0x2c, 0x7d, 0x0c, 0x4e, 0x5f, 0x58, 0xe4
  1350. );
  1351. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test5_expected_result,
  1352. 0x41, 0xf5, 0x3f, 0xa5, 0xbf, 0xe7, 0x02, 0x6d,
  1353. 0x91, 0xfa, 0xf9, 0xbe, 0x95, 0x91, 0x95, 0xa0
  1354. );
  1355. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test5_expected_hmac,
  1356. 0x58, 0x70, 0x72, 0x73, 0xa9, 0x6a, 0x40, 0xf0,
  1357. 0xa0, 0x19, 0x60, 0x62, 0x1a, 0xc6, 0x12, 0x74,
  1358. 0x8b, 0x9b, 0xbf, 0xbe, 0x7e, 0xb4, 0xce, 0x3c
  1359. );
  1360. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test6_confounder,
  1361. 0xb8, 0x0d, 0x32, 0x51, 0xc1, 0xf6, 0x47, 0x14,
  1362. 0x94, 0x25, 0x6f, 0xfe, 0x71, 0x2d, 0x0b, 0x9a
  1363. );
  1364. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test6_expected_result,
  1365. 0x4e, 0xd7, 0xb3, 0x7c, 0x2b, 0xca, 0xc8, 0xf7,
  1366. 0x4f, 0x23, 0xc1, 0xcf, 0x07, 0xe6, 0x2b, 0xc7,
  1367. 0xb7, 0x5f, 0xb3, 0xf6, 0x37, 0xb9
  1368. );
  1369. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test6_expected_hmac,
  1370. 0xf5, 0x59, 0xc7, 0xf6, 0x64, 0xf6, 0x9e, 0xab,
  1371. 0x7b, 0x60, 0x92, 0x23, 0x75, 0x26, 0xea, 0x0d,
  1372. 0x1f, 0x61, 0xcb, 0x20, 0xd6, 0x9d, 0x10, 0xf2
  1373. );
  1374. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test7_confounder,
  1375. 0x53, 0xbf, 0x8a, 0x0d, 0x10, 0x52, 0x65, 0xd4,
  1376. 0xe2, 0x76, 0x42, 0x86, 0x24, 0xce, 0x5e, 0x63
  1377. );
  1378. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test7_expected_result,
  1379. 0xbc, 0x47, 0xff, 0xec, 0x79, 0x98, 0xeb, 0x91,
  1380. 0xe8, 0x11, 0x5c, 0xf8, 0xd1, 0x9d, 0xac, 0x4b,
  1381. 0xbb, 0xe2, 0xe1, 0x63, 0xe8, 0x7d, 0xd3, 0x7f,
  1382. 0x49, 0xbe, 0xca, 0x92, 0x02, 0x77, 0x64, 0xf6
  1383. );
  1384. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test7_expected_hmac,
  1385. 0x8c, 0xf5, 0x1f, 0x14, 0xd7, 0x98, 0xc2, 0x27,
  1386. 0x3f, 0x35, 0xdf, 0x57, 0x4d, 0x1f, 0x93, 0x2e,
  1387. 0x40, 0xc4, 0xff, 0x25, 0x5b, 0x36, 0xa2, 0x66
  1388. );
  1389. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test8_confounder,
  1390. 0x76, 0x3e, 0x65, 0x36, 0x7e, 0x86, 0x4f, 0x02,
  1391. 0xf5, 0x51, 0x53, 0xc7, 0xe3, 0xb5, 0x8a, 0xf1
  1392. );
  1393. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test8_expected_result,
  1394. 0x40, 0x01, 0x3e, 0x2d, 0xf5, 0x8e, 0x87, 0x51,
  1395. 0x95, 0x7d, 0x28, 0x78, 0xbc, 0xd2, 0xd6, 0xfe,
  1396. 0x10, 0x1c, 0xcf, 0xd5, 0x56, 0xcb, 0x1e, 0xae,
  1397. 0x79, 0xdb, 0x3c, 0x3e, 0xe8, 0x64, 0x29, 0xf2,
  1398. 0xb2, 0xa6, 0x02, 0xac, 0x86
  1399. );
  1400. DEFINE_HEX_XDR_NETOBJ(rfc8009_enc_test8_expected_hmac,
  1401. 0xfe, 0xf6, 0xec, 0xb6, 0x47, 0xd6, 0x29, 0x5f,
  1402. 0xae, 0x07, 0x7a, 0x1f, 0xeb, 0x51, 0x75, 0x08,
  1403. 0xd2, 0xc1, 0x6b, 0x41, 0x92, 0xe0, 0x1f, 0x62
  1404. );
  1405. static const struct gss_krb5_test_param rfc8009_encrypt_test_params[] = {
  1406. {
  1407. .desc = "Encrypt empty plaintext with aes128-cts-hmac-sha256-128",
  1408. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1409. .plaintext = &rfc8009_enc_empty_plaintext,
  1410. .confounder = &rfc8009_enc_test1_confounder,
  1411. .base_key = &aes128_cts_hmac_sha256_128_basekey,
  1412. .expected_result = &rfc8009_enc_test1_expected_result,
  1413. .expected_hmac = &rfc8009_enc_test1_expected_hmac,
  1414. },
  1415. {
  1416. .desc = "Encrypt short plaintext with aes128-cts-hmac-sha256-128",
  1417. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1418. .plaintext = &rfc8009_enc_short_plaintext,
  1419. .confounder = &rfc8009_enc_test2_confounder,
  1420. .base_key = &aes128_cts_hmac_sha256_128_basekey,
  1421. .expected_result = &rfc8009_enc_test2_expected_result,
  1422. .expected_hmac = &rfc8009_enc_test2_expected_hmac,
  1423. },
  1424. {
  1425. .desc = "Encrypt block plaintext with aes128-cts-hmac-sha256-128",
  1426. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1427. .plaintext = &rfc8009_enc_block_plaintext,
  1428. .confounder = &rfc8009_enc_test3_confounder,
  1429. .base_key = &aes128_cts_hmac_sha256_128_basekey,
  1430. .expected_result = &rfc8009_enc_test3_expected_result,
  1431. .expected_hmac = &rfc8009_enc_test3_expected_hmac,
  1432. },
  1433. {
  1434. .desc = "Encrypt long plaintext with aes128-cts-hmac-sha256-128",
  1435. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1436. .plaintext = &rfc8009_enc_long_plaintext,
  1437. .confounder = &rfc8009_enc_test4_confounder,
  1438. .base_key = &aes128_cts_hmac_sha256_128_basekey,
  1439. .expected_result = &rfc8009_enc_test4_expected_result,
  1440. .expected_hmac = &rfc8009_enc_test4_expected_hmac,
  1441. },
  1442. {
  1443. .desc = "Encrypt empty plaintext with aes256-cts-hmac-sha384-192",
  1444. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1445. .plaintext = &rfc8009_enc_empty_plaintext,
  1446. .confounder = &rfc8009_enc_test5_confounder,
  1447. .base_key = &aes256_cts_hmac_sha384_192_basekey,
  1448. .expected_result = &rfc8009_enc_test5_expected_result,
  1449. .expected_hmac = &rfc8009_enc_test5_expected_hmac,
  1450. },
  1451. {
  1452. .desc = "Encrypt short plaintext with aes256-cts-hmac-sha384-192",
  1453. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1454. .plaintext = &rfc8009_enc_short_plaintext,
  1455. .confounder = &rfc8009_enc_test6_confounder,
  1456. .base_key = &aes256_cts_hmac_sha384_192_basekey,
  1457. .expected_result = &rfc8009_enc_test6_expected_result,
  1458. .expected_hmac = &rfc8009_enc_test6_expected_hmac,
  1459. },
  1460. {
  1461. .desc = "Encrypt block plaintext with aes256-cts-hmac-sha384-192",
  1462. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1463. .plaintext = &rfc8009_enc_block_plaintext,
  1464. .confounder = &rfc8009_enc_test7_confounder,
  1465. .base_key = &aes256_cts_hmac_sha384_192_basekey,
  1466. .expected_result = &rfc8009_enc_test7_expected_result,
  1467. .expected_hmac = &rfc8009_enc_test7_expected_hmac,
  1468. },
  1469. {
  1470. .desc = "Encrypt long plaintext with aes256-cts-hmac-sha384-192",
  1471. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1472. .plaintext = &rfc8009_enc_long_plaintext,
  1473. .confounder = &rfc8009_enc_test8_confounder,
  1474. .base_key = &aes256_cts_hmac_sha384_192_basekey,
  1475. .expected_result = &rfc8009_enc_test8_expected_result,
  1476. .expected_hmac = &rfc8009_enc_test8_expected_hmac,
  1477. },
  1478. };
  1479. /* Creates the function rfc8009_encrypt_gen_params */
  1480. KUNIT_ARRAY_PARAM(rfc8009_encrypt, rfc8009_encrypt_test_params,
  1481. gss_krb5_get_desc);
  1482. static void rfc8009_encrypt_case(struct kunit *test)
  1483. {
  1484. const struct gss_krb5_test_param *param = test->param_value;
  1485. struct crypto_sync_skcipher *cts_tfm, *cbc_tfm;
  1486. const struct gss_krb5_enctype *gk5e;
  1487. struct xdr_netobj Ke, Ki, checksum;
  1488. u8 usage_data[GSS_KRB5_K5CLENGTH];
  1489. struct xdr_netobj usage = {
  1490. .data = usage_data,
  1491. .len = sizeof(usage_data),
  1492. };
  1493. struct crypto_ahash *ahash_tfm;
  1494. struct xdr_buf buf;
  1495. void *text;
  1496. size_t len;
  1497. u32 err;
  1498. /* Arrange */
  1499. gk5e = gss_krb5_lookup_enctype(param->enctype);
  1500. if (!gk5e)
  1501. kunit_skip(test, "Encryption type is not available");
  1502. *(__be32 *)usage.data = cpu_to_be32(2);
  1503. Ke.len = gk5e->Ke_length;
  1504. Ke.data = kunit_kzalloc(test, Ke.len, GFP_KERNEL);
  1505. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, Ke.data);
  1506. usage.data[4] = KEY_USAGE_SEED_ENCRYPTION;
  1507. err = gk5e->derive_key(gk5e, param->base_key, &Ke,
  1508. &usage, GFP_KERNEL);
  1509. KUNIT_ASSERT_EQ(test, err, 0);
  1510. cbc_tfm = crypto_alloc_sync_skcipher(gk5e->aux_cipher, 0, 0);
  1511. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cbc_tfm);
  1512. err = crypto_sync_skcipher_setkey(cbc_tfm, Ke.data, Ke.len);
  1513. KUNIT_ASSERT_EQ(test, err, 0);
  1514. cts_tfm = crypto_alloc_sync_skcipher(gk5e->encrypt_name, 0, 0);
  1515. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cts_tfm);
  1516. err = crypto_sync_skcipher_setkey(cts_tfm, Ke.data, Ke.len);
  1517. KUNIT_ASSERT_EQ(test, err, 0);
  1518. len = param->confounder->len + param->plaintext->len;
  1519. text = kunit_kzalloc(test, len, GFP_KERNEL);
  1520. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, text);
  1521. memcpy(text, param->confounder->data, param->confounder->len);
  1522. memcpy(text + param->confounder->len, param->plaintext->data,
  1523. param->plaintext->len);
  1524. memset(&buf, 0, sizeof(buf));
  1525. buf.head[0].iov_base = text;
  1526. buf.head[0].iov_len = param->confounder->len + param->plaintext->len;
  1527. buf.len = buf.head[0].iov_len;
  1528. checksum.len = gk5e->cksumlength;
  1529. checksum.data = kunit_kzalloc(test, checksum.len, GFP_KERNEL);
  1530. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, checksum.data);
  1531. Ki.len = gk5e->Ki_length;
  1532. Ki.data = kunit_kzalloc(test, Ki.len, GFP_KERNEL);
  1533. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, Ki.data);
  1534. usage.data[4] = KEY_USAGE_SEED_INTEGRITY;
  1535. err = gk5e->derive_key(gk5e, param->base_key, &Ki,
  1536. &usage, GFP_KERNEL);
  1537. KUNIT_ASSERT_EQ(test, err, 0);
  1538. ahash_tfm = crypto_alloc_ahash(gk5e->cksum_name, 0, CRYPTO_ALG_ASYNC);
  1539. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ahash_tfm);
  1540. err = crypto_ahash_setkey(ahash_tfm, Ki.data, Ki.len);
  1541. KUNIT_ASSERT_EQ(test, err, 0);
  1542. /* Act */
  1543. err = krb5_cbc_cts_encrypt(cts_tfm, cbc_tfm, 0, &buf, NULL, NULL, 0);
  1544. KUNIT_ASSERT_EQ(test, err, 0);
  1545. err = krb5_etm_checksum(cts_tfm, ahash_tfm, &buf, 0, &checksum);
  1546. KUNIT_ASSERT_EQ(test, err, 0);
  1547. /* Assert */
  1548. KUNIT_EXPECT_EQ_MSG(test,
  1549. param->expected_result->len, buf.len,
  1550. "ciphertext length mismatch");
  1551. KUNIT_EXPECT_EQ_MSG(test,
  1552. memcmp(param->expected_result->data,
  1553. buf.head[0].iov_base,
  1554. param->expected_result->len), 0,
  1555. "ciphertext mismatch");
  1556. KUNIT_EXPECT_EQ_MSG(test, memcmp(param->expected_hmac->data,
  1557. checksum.data,
  1558. checksum.len), 0,
  1559. "HMAC mismatch");
  1560. crypto_free_ahash(ahash_tfm);
  1561. crypto_free_sync_skcipher(cts_tfm);
  1562. crypto_free_sync_skcipher(cbc_tfm);
  1563. }
  1564. static struct kunit_case rfc8009_test_cases[] = {
  1565. {
  1566. .name = "RFC 8009 key derivation",
  1567. .run_case = kdf_case,
  1568. .generate_params = rfc8009_kdf_gen_params,
  1569. },
  1570. {
  1571. .name = "RFC 8009 checksum",
  1572. .run_case = checksum_case,
  1573. .generate_params = rfc8009_checksum_gen_params,
  1574. },
  1575. {
  1576. .name = "RFC 8009 encryption",
  1577. .run_case = rfc8009_encrypt_case,
  1578. .generate_params = rfc8009_encrypt_gen_params,
  1579. },
  1580. {}
  1581. };
  1582. static struct kunit_suite rfc8009_suite = {
  1583. .name = "RFC 8009 suite",
  1584. .test_cases = rfc8009_test_cases,
  1585. };
  1586. /*
  1587. * Encryption self-tests
  1588. */
  1589. DEFINE_STR_XDR_NETOBJ(encrypt_selftest_plaintext,
  1590. "This is the plaintext for the encryption self-test.");
  1591. static const struct gss_krb5_test_param encrypt_selftest_params[] = {
  1592. {
  1593. .desc = "aes128-cts-hmac-sha1-96 encryption self-test",
  1594. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA1_96,
  1595. .Ke = &rfc3962_encryption_key,
  1596. .plaintext = &encrypt_selftest_plaintext,
  1597. },
  1598. {
  1599. .desc = "aes256-cts-hmac-sha1-96 encryption self-test",
  1600. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA1_96,
  1601. .Ke = &rfc3962_encryption_key,
  1602. .plaintext = &encrypt_selftest_plaintext,
  1603. },
  1604. {
  1605. .desc = "camellia128-cts-cmac encryption self-test",
  1606. .enctype = ENCTYPE_CAMELLIA128_CTS_CMAC,
  1607. .Ke = &camellia128_cts_cmac_Ke,
  1608. .plaintext = &encrypt_selftest_plaintext,
  1609. },
  1610. {
  1611. .desc = "camellia256-cts-cmac encryption self-test",
  1612. .enctype = ENCTYPE_CAMELLIA256_CTS_CMAC,
  1613. .Ke = &camellia256_cts_cmac_Ke,
  1614. .plaintext = &encrypt_selftest_plaintext,
  1615. },
  1616. {
  1617. .desc = "aes128-cts-hmac-sha256-128 encryption self-test",
  1618. .enctype = ENCTYPE_AES128_CTS_HMAC_SHA256_128,
  1619. .Ke = &aes128_cts_hmac_sha256_128_Ke,
  1620. .plaintext = &encrypt_selftest_plaintext,
  1621. },
  1622. {
  1623. .desc = "aes256-cts-hmac-sha384-192 encryption self-test",
  1624. .enctype = ENCTYPE_AES256_CTS_HMAC_SHA384_192,
  1625. .Ke = &aes256_cts_hmac_sha384_192_Ke,
  1626. .plaintext = &encrypt_selftest_plaintext,
  1627. },
  1628. };
  1629. /* Creates the function encrypt_selftest_gen_params */
  1630. KUNIT_ARRAY_PARAM(encrypt_selftest, encrypt_selftest_params,
  1631. gss_krb5_get_desc);
  1632. /*
  1633. * Encrypt and decrypt plaintext, and ensure the input plaintext
  1634. * matches the output plaintext. A confounder is not added in this
  1635. * case.
  1636. */
  1637. static void encrypt_selftest_case(struct kunit *test)
  1638. {
  1639. const struct gss_krb5_test_param *param = test->param_value;
  1640. struct crypto_sync_skcipher *cts_tfm, *cbc_tfm;
  1641. const struct gss_krb5_enctype *gk5e;
  1642. struct xdr_buf buf;
  1643. void *text;
  1644. int err;
  1645. /* Arrange */
  1646. gk5e = gss_krb5_lookup_enctype(param->enctype);
  1647. if (!gk5e)
  1648. kunit_skip(test, "Encryption type is not available");
  1649. cbc_tfm = crypto_alloc_sync_skcipher(gk5e->aux_cipher, 0, 0);
  1650. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cbc_tfm);
  1651. err = crypto_sync_skcipher_setkey(cbc_tfm, param->Ke->data, param->Ke->len);
  1652. KUNIT_ASSERT_EQ(test, err, 0);
  1653. cts_tfm = crypto_alloc_sync_skcipher(gk5e->encrypt_name, 0, 0);
  1654. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cts_tfm);
  1655. err = crypto_sync_skcipher_setkey(cts_tfm, param->Ke->data, param->Ke->len);
  1656. KUNIT_ASSERT_EQ(test, err, 0);
  1657. text = kunit_kzalloc(test, roundup(param->plaintext->len,
  1658. crypto_sync_skcipher_blocksize(cbc_tfm)),
  1659. GFP_KERNEL);
  1660. KUNIT_ASSERT_NOT_ERR_OR_NULL(test, text);
  1661. memcpy(text, param->plaintext->data, param->plaintext->len);
  1662. memset(&buf, 0, sizeof(buf));
  1663. buf.head[0].iov_base = text;
  1664. buf.head[0].iov_len = param->plaintext->len;
  1665. buf.len = buf.head[0].iov_len;
  1666. /* Act */
  1667. err = krb5_cbc_cts_encrypt(cts_tfm, cbc_tfm, 0, &buf, NULL, NULL, 0);
  1668. KUNIT_ASSERT_EQ(test, err, 0);
  1669. err = krb5_cbc_cts_decrypt(cts_tfm, cbc_tfm, 0, &buf);
  1670. KUNIT_ASSERT_EQ(test, err, 0);
  1671. /* Assert */
  1672. KUNIT_EXPECT_EQ_MSG(test,
  1673. param->plaintext->len, buf.len,
  1674. "length mismatch");
  1675. KUNIT_EXPECT_EQ_MSG(test,
  1676. memcmp(param->plaintext->data,
  1677. buf.head[0].iov_base, buf.len), 0,
  1678. "plaintext mismatch");
  1679. crypto_free_sync_skcipher(cts_tfm);
  1680. crypto_free_sync_skcipher(cbc_tfm);
  1681. }
  1682. static struct kunit_case encryption_test_cases[] = {
  1683. {
  1684. .name = "Encryption self-tests",
  1685. .run_case = encrypt_selftest_case,
  1686. .generate_params = encrypt_selftest_gen_params,
  1687. },
  1688. {}
  1689. };
  1690. static struct kunit_suite encryption_test_suite = {
  1691. .name = "Encryption test suite",
  1692. .test_cases = encryption_test_cases,
  1693. };
  1694. kunit_test_suites(&rfc3961_suite,
  1695. &rfc3962_suite,
  1696. &rfc6803_suite,
  1697. &rfc8009_suite,
  1698. &encryption_test_suite);
  1699. MODULE_DESCRIPTION("Test RPCSEC GSS Kerberos 5 functions");
  1700. MODULE_LICENSE("GPL");