srtp_driver.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837
  1. /*
  2. * srtp_driver.c
  3. *
  4. * a test driver for libSRTP
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2017, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #include <string.h> /* for memcpy() */
  45. #include <time.h> /* for clock() */
  46. #include <stdlib.h> /* for malloc(), free() */
  47. #include <stdio.h> /* for print(), fflush() */
  48. #include "getopt_s.h" /* for local getopt() */
  49. #include "srtp_priv.h"
  50. #include "util.h"
  51. #ifdef HAVE_NETINET_IN_H
  52. #include <netinet/in.h>
  53. #elif defined HAVE_WINSOCK2_H
  54. #include <winsock2.h>
  55. #endif
  56. #define PRINT_REFERENCE_PACKET 1
  57. srtp_err_status_t srtp_validate(void);
  58. #ifdef GCM
  59. srtp_err_status_t srtp_validate_gcm(void);
  60. #endif
  61. srtp_err_status_t srtp_validate_encrypted_extensions_headers(void);
  62. #ifdef GCM
  63. srtp_err_status_t srtp_validate_encrypted_extensions_headers_gcm(void);
  64. #endif
  65. srtp_err_status_t srtp_validate_aes_256(void);
  66. srtp_err_status_t srtp_create_big_policy(srtp_policy_t **list);
  67. srtp_err_status_t srtp_dealloc_big_policy(srtp_policy_t *list);
  68. srtp_err_status_t srtp_test_empty_payload(void);
  69. #ifdef GCM
  70. srtp_err_status_t srtp_test_empty_payload_gcm(void);
  71. #endif
  72. srtp_err_status_t srtp_test_remove_stream(void);
  73. srtp_err_status_t srtp_test_update(void);
  74. srtp_err_status_t srtp_test_protect_trailer_length(void);
  75. srtp_err_status_t srtp_test_protect_rtcp_trailer_length(void);
  76. srtp_err_status_t srtp_test_get_roc(void);
  77. srtp_err_status_t srtp_test_set_receiver_roc(void);
  78. srtp_err_status_t srtp_test_set_sender_roc(void);
  79. double srtp_bits_per_second(int msg_len_octets, const srtp_policy_t *policy);
  80. double srtp_rejections_per_second(int msg_len_octets,
  81. const srtp_policy_t *policy);
  82. void srtp_do_timing(const srtp_policy_t *policy);
  83. void srtp_do_rejection_timing(const srtp_policy_t *policy);
  84. srtp_err_status_t srtp_test(const srtp_policy_t *policy,
  85. int extension_header,
  86. int mki_index);
  87. srtp_err_status_t srtcp_test(const srtp_policy_t *policy, int mki_index);
  88. srtp_err_status_t srtp_session_print_policy(srtp_t srtp);
  89. srtp_err_status_t srtp_print_policy(const srtp_policy_t *policy);
  90. char *srtp_packet_to_string(srtp_hdr_t *hdr, int packet_len);
  91. double mips_estimate(int num_trials, int *ignore);
  92. #define TEST_MKI_ID_SIZE 4
  93. extern uint8_t test_key[46];
  94. extern uint8_t test_key_2[46];
  95. extern uint8_t test_mki_id[TEST_MKI_ID_SIZE];
  96. extern uint8_t test_mki_id_2[TEST_MKI_ID_SIZE];
  97. // clang-format off
  98. srtp_master_key_t master_key_1 = {
  99. test_key,
  100. test_mki_id,
  101. TEST_MKI_ID_SIZE
  102. };
  103. srtp_master_key_t master_key_2 = {
  104. test_key_2,
  105. test_mki_id_2,
  106. TEST_MKI_ID_SIZE
  107. };
  108. srtp_master_key_t *test_keys[2] = {
  109. &master_key_1,
  110. &master_key_2
  111. };
  112. // clang-format on
  113. void usage(char *prog_name)
  114. {
  115. printf("usage: %s [ -t ][ -c ][ -v ][ -o ][-d <debug_module> ]* [ -l ]\n"
  116. " -t run timing test\n"
  117. " -r run rejection timing test\n"
  118. " -c run codec timing test\n"
  119. " -v run validation tests\n"
  120. " -o output logging to stdout\n"
  121. " -d <mod> turn on debugging module <mod>\n"
  122. " -l list debugging modules\n",
  123. prog_name);
  124. exit(1);
  125. }
  126. void log_handler(srtp_log_level_t level, const char *msg, void *data)
  127. {
  128. char level_char = '?';
  129. switch (level) {
  130. case srtp_log_level_error:
  131. level_char = 'e';
  132. break;
  133. case srtp_log_level_warning:
  134. level_char = 'w';
  135. break;
  136. case srtp_log_level_info:
  137. level_char = 'i';
  138. break;
  139. case srtp_log_level_debug:
  140. level_char = 'd';
  141. break;
  142. }
  143. printf("SRTP-LOG [%c]: %s\n", level_char, msg);
  144. }
  145. /*
  146. * The policy_array is a null-terminated array of policy structs. it
  147. * is declared at the end of this file
  148. */
  149. extern const srtp_policy_t *policy_array[];
  150. /* the wildcard_policy is declared below; it has a wildcard ssrc */
  151. extern const srtp_policy_t wildcard_policy;
  152. /*
  153. * mod_driver debug module - debugging module for this test driver
  154. *
  155. * we use the crypto_kernel debugging system in this driver, which
  156. * makes the interface uniform and increases portability
  157. */
  158. srtp_debug_module_t mod_driver = {
  159. 0, /* debugging is off by default */
  160. "driver" /* printable name for module */
  161. };
  162. int main(int argc, char *argv[])
  163. {
  164. int q;
  165. unsigned do_timing_test = 0;
  166. unsigned do_rejection_test = 0;
  167. unsigned do_codec_timing = 0;
  168. unsigned do_validation = 0;
  169. unsigned do_list_mods = 0;
  170. unsigned do_log_stdout = 0;
  171. srtp_err_status_t status;
  172. /*
  173. * verify that the compiler has interpreted the header data
  174. * structure srtp_hdr_t correctly
  175. */
  176. if (sizeof(srtp_hdr_t) != 12) {
  177. printf("error: srtp_hdr_t has incorrect size"
  178. "(size is %ld bytes, expected 12)\n",
  179. (long)sizeof(srtp_hdr_t));
  180. exit(1);
  181. }
  182. /* initialize srtp library */
  183. status = srtp_init();
  184. if (status) {
  185. printf("error: srtp init failed with error code %d\n", status);
  186. exit(1);
  187. }
  188. /* load srtp_driver debug module */
  189. status = srtp_crypto_kernel_load_debug_module(&mod_driver);
  190. if (status) {
  191. printf("error: load of srtp_driver debug module failed "
  192. "with error code %d\n",
  193. status);
  194. exit(1);
  195. }
  196. /* process input arguments */
  197. while (1) {
  198. q = getopt_s(argc, argv, "trcvold:");
  199. if (q == -1) {
  200. break;
  201. }
  202. switch (q) {
  203. case 't':
  204. do_timing_test = 1;
  205. break;
  206. case 'r':
  207. do_rejection_test = 1;
  208. break;
  209. case 'c':
  210. do_codec_timing = 1;
  211. break;
  212. case 'v':
  213. do_validation = 1;
  214. break;
  215. case 'o':
  216. do_log_stdout = 1;
  217. break;
  218. case 'l':
  219. do_list_mods = 1;
  220. break;
  221. case 'd':
  222. status = srtp_set_debug_module(optarg_s, 1);
  223. if (status) {
  224. printf("error: set debug module (%s) failed\n", optarg_s);
  225. exit(1);
  226. }
  227. break;
  228. default:
  229. usage(argv[0]);
  230. }
  231. }
  232. if (!do_validation && !do_timing_test && !do_codec_timing &&
  233. !do_list_mods && !do_rejection_test) {
  234. usage(argv[0]);
  235. }
  236. if (do_log_stdout) {
  237. status = srtp_install_log_handler(log_handler, NULL);
  238. if (status) {
  239. printf("error: install log handler failed\n");
  240. exit(1);
  241. }
  242. }
  243. if (do_list_mods) {
  244. status = srtp_list_debug_modules();
  245. if (status) {
  246. printf("error: list of debug modules failed\n");
  247. exit(1);
  248. }
  249. }
  250. if (do_validation) {
  251. const srtp_policy_t **policy = policy_array;
  252. srtp_policy_t *big_policy;
  253. /* loop over policy array, testing srtp and srtcp for each policy */
  254. while (*policy != NULL) {
  255. printf("testing srtp_protect and srtp_unprotect\n");
  256. if (srtp_test(*policy, 0, -1) == srtp_err_status_ok) {
  257. printf("passed\n\n");
  258. } else {
  259. printf("failed\n");
  260. exit(1);
  261. }
  262. printf("testing srtp_protect and srtp_unprotect with encrypted "
  263. "extensions headers\n");
  264. if (srtp_test(*policy, 1, -1) == srtp_err_status_ok) {
  265. printf("passed\n\n");
  266. } else {
  267. printf("failed\n");
  268. exit(1);
  269. }
  270. printf("testing srtp_protect_rtcp and srtp_unprotect_rtcp\n");
  271. if (srtcp_test(*policy, -1) == srtp_err_status_ok) {
  272. printf("passed\n\n");
  273. } else {
  274. printf("failed\n");
  275. exit(1);
  276. }
  277. printf("testing srtp_protect_rtp and srtp_unprotect_rtp with MKI "
  278. "index set to 0\n");
  279. if (srtp_test(*policy, 0, 0) == srtp_err_status_ok) {
  280. printf("passed\n\n");
  281. } else {
  282. printf("failed\n");
  283. exit(1);
  284. }
  285. printf("testing srtp_protect_rtp and srtp_unprotect_rtp with MKI "
  286. "index set to 1\n");
  287. if (srtp_test(*policy, 0, 1) == srtp_err_status_ok) {
  288. printf("passed\n\n");
  289. } else {
  290. printf("failed\n");
  291. exit(1);
  292. }
  293. printf("testing srtp_protect_rtcp and srtp_unprotect_rtcp with MKI "
  294. "index set to 0\n");
  295. if (srtcp_test(*policy, 0) == srtp_err_status_ok) {
  296. printf("passed\n\n");
  297. } else {
  298. printf("failed\n");
  299. exit(1);
  300. }
  301. printf("testing srtp_protect_rtcp and srtp_unprotect_rtcp with MKI "
  302. "index set to 1\n");
  303. if (srtcp_test(*policy, 1) == srtp_err_status_ok) {
  304. printf("passed\n\n");
  305. } else {
  306. printf("failed\n");
  307. exit(1);
  308. }
  309. policy++;
  310. }
  311. /* create a big policy list and run tests on it */
  312. status = srtp_create_big_policy(&big_policy);
  313. if (status) {
  314. printf("unexpected failure with error code %d\n", status);
  315. exit(1);
  316. }
  317. printf("testing srtp_protect and srtp_unprotect with big policy\n");
  318. if (srtp_test(big_policy, 0, -1) == srtp_err_status_ok) {
  319. printf("passed\n\n");
  320. } else {
  321. printf("failed\n");
  322. exit(1);
  323. }
  324. printf("testing srtp_protect and srtp_unprotect with big policy and "
  325. "encrypted extensions headers\n");
  326. if (srtp_test(big_policy, 1, -1) == srtp_err_status_ok) {
  327. printf("passed\n\n");
  328. } else {
  329. printf("failed\n");
  330. exit(1);
  331. }
  332. status = srtp_dealloc_big_policy(big_policy);
  333. if (status) {
  334. printf("unexpected failure with error code %d\n", status);
  335. exit(1);
  336. }
  337. /* run test on wildcard policy */
  338. printf("testing srtp_protect and srtp_unprotect on "
  339. "wildcard ssrc policy\n");
  340. if (srtp_test(&wildcard_policy, 0, -1) == srtp_err_status_ok) {
  341. printf("passed\n\n");
  342. } else {
  343. printf("failed\n");
  344. exit(1);
  345. }
  346. printf("testing srtp_protect and srtp_unprotect on "
  347. "wildcard ssrc policy and encrypted extensions headers\n");
  348. if (srtp_test(&wildcard_policy, 1, -1) == srtp_err_status_ok) {
  349. printf("passed\n\n");
  350. } else {
  351. printf("failed\n");
  352. exit(1);
  353. }
  354. /*
  355. * run validation test against the reference packets - note
  356. * that this test only covers the default policy
  357. */
  358. printf("testing srtp_protect and srtp_unprotect against "
  359. "reference packet\n");
  360. if (srtp_validate() == srtp_err_status_ok) {
  361. printf("passed\n\n");
  362. } else {
  363. printf("failed\n");
  364. exit(1);
  365. }
  366. #ifdef GCM
  367. printf("testing srtp_protect and srtp_unprotect against "
  368. "reference packet using GCM\n");
  369. if (srtp_validate_gcm() == srtp_err_status_ok) {
  370. printf("passed\n\n");
  371. } else {
  372. printf("failed\n");
  373. exit(1);
  374. }
  375. #endif
  376. printf("testing srtp_protect and srtp_unprotect against "
  377. "reference packet with encrypted extensions headers\n");
  378. if (srtp_validate_encrypted_extensions_headers() == srtp_err_status_ok)
  379. printf("passed\n\n");
  380. else {
  381. printf("failed\n");
  382. exit(1);
  383. }
  384. #ifdef GCM
  385. printf("testing srtp_protect and srtp_unprotect against "
  386. "reference packet with encrypted extension headers (GCM)\n");
  387. if (srtp_validate_encrypted_extensions_headers_gcm() ==
  388. srtp_err_status_ok) {
  389. printf("passed\n\n");
  390. } else {
  391. printf("failed\n");
  392. exit(1);
  393. }
  394. #endif
  395. /*
  396. * run validation test against the reference packets for
  397. * AES-256
  398. */
  399. printf("testing srtp_protect and srtp_unprotect against "
  400. "reference packet (AES-256)\n");
  401. if (srtp_validate_aes_256() == srtp_err_status_ok) {
  402. printf("passed\n\n");
  403. } else {
  404. printf("failed\n");
  405. exit(1);
  406. }
  407. /*
  408. * test packets with empty payload
  409. */
  410. printf("testing srtp_protect and srtp_unprotect against "
  411. "packet with empty payload\n");
  412. if (srtp_test_empty_payload() == srtp_err_status_ok) {
  413. printf("passed\n");
  414. } else {
  415. printf("failed\n");
  416. exit(1);
  417. }
  418. #ifdef GCM
  419. printf("testing srtp_protect and srtp_unprotect against "
  420. "packet with empty payload (GCM)\n");
  421. if (srtp_test_empty_payload_gcm() == srtp_err_status_ok) {
  422. printf("passed\n");
  423. } else {
  424. printf("failed\n");
  425. exit(1);
  426. }
  427. #endif
  428. /*
  429. * test the function srtp_remove_stream()
  430. */
  431. printf("testing srtp_remove_stream()...");
  432. if (srtp_test_remove_stream() == srtp_err_status_ok) {
  433. printf("passed\n");
  434. } else {
  435. printf("failed\n");
  436. exit(1);
  437. }
  438. /*
  439. * test the function srtp_update()
  440. */
  441. printf("testing srtp_update()...");
  442. if (srtp_test_update() == srtp_err_status_ok) {
  443. printf("passed\n");
  444. } else {
  445. printf("failed\n");
  446. exit(1);
  447. }
  448. /*
  449. * test the functions srtp_get_protect_trailer_length
  450. * and srtp_get_protect_rtcp_trailer_length
  451. */
  452. printf("testing srtp_get_protect_trailer_length()...");
  453. if (srtp_test_protect_trailer_length() == srtp_err_status_ok) {
  454. printf("passed\n");
  455. } else {
  456. printf("failed\n");
  457. exit(1);
  458. }
  459. printf("testing srtp_get_protect_rtcp_trailer_length()...");
  460. if (srtp_test_protect_rtcp_trailer_length() == srtp_err_status_ok) {
  461. printf("passed\n");
  462. } else {
  463. printf("failed\n");
  464. exit(1);
  465. }
  466. printf("testing srtp_test_get_roc()...");
  467. if (srtp_test_get_roc() == srtp_err_status_ok) {
  468. printf("passed\n");
  469. } else {
  470. printf("failed\n");
  471. exit(1);
  472. }
  473. printf("testing srtp_test_set_receiver_roc()...");
  474. if (srtp_test_set_receiver_roc() == srtp_err_status_ok) {
  475. printf("passed\n");
  476. } else {
  477. printf("failed\n");
  478. exit(1);
  479. }
  480. printf("testing srtp_test_set_sender_roc()...");
  481. if (srtp_test_set_sender_roc() == srtp_err_status_ok) {
  482. printf("passed\n");
  483. } else {
  484. printf("failed\n");
  485. exit(1);
  486. }
  487. }
  488. if (do_timing_test) {
  489. const srtp_policy_t **policy = policy_array;
  490. /* loop over policies, run timing test for each */
  491. while (*policy != NULL) {
  492. srtp_print_policy(*policy);
  493. srtp_do_timing(*policy);
  494. policy++;
  495. }
  496. }
  497. if (do_rejection_test) {
  498. const srtp_policy_t **policy = policy_array;
  499. /* loop over policies, run rejection timing test for each */
  500. while (*policy != NULL) {
  501. srtp_print_policy(*policy);
  502. srtp_do_rejection_timing(*policy);
  503. policy++;
  504. }
  505. }
  506. if (do_codec_timing) {
  507. srtp_policy_t policy;
  508. int ignore;
  509. double mips_value = mips_estimate(1000000000, &ignore);
  510. memset(&policy, 0, sizeof(policy));
  511. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  512. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  513. policy.ssrc.type = ssrc_specific;
  514. policy.ssrc.value = 0xdecafbad;
  515. policy.key = test_key;
  516. policy.ekt = NULL;
  517. policy.window_size = 128;
  518. policy.allow_repeat_tx = 0;
  519. policy.next = NULL;
  520. printf("mips estimate: %e\n", mips_value);
  521. printf("testing srtp processing time for voice codecs:\n");
  522. printf("codec\t\tlength (octets)\t\tsrtp instructions/second\n");
  523. printf("G.711\t\t%d\t\t\t%e\n", 80,
  524. (double)mips_value * (80 * 8) /
  525. srtp_bits_per_second(80, &policy) / .01);
  526. printf("G.711\t\t%d\t\t\t%e\n", 160,
  527. (double)mips_value * (160 * 8) /
  528. srtp_bits_per_second(160, &policy) / .02);
  529. printf("G.726-32\t%d\t\t\t%e\n", 40,
  530. (double)mips_value * (40 * 8) /
  531. srtp_bits_per_second(40, &policy) / .01);
  532. printf("G.726-32\t%d\t\t\t%e\n", 80,
  533. (double)mips_value * (80 * 8) /
  534. srtp_bits_per_second(80, &policy) / .02);
  535. printf("G.729\t\t%d\t\t\t%e\n", 10,
  536. (double)mips_value * (10 * 8) /
  537. srtp_bits_per_second(10, &policy) / .01);
  538. printf("G.729\t\t%d\t\t\t%e\n", 20,
  539. (double)mips_value * (20 * 8) /
  540. srtp_bits_per_second(20, &policy) / .02);
  541. printf("Wideband\t%d\t\t\t%e\n", 320,
  542. (double)mips_value * (320 * 8) /
  543. srtp_bits_per_second(320, &policy) / .01);
  544. printf("Wideband\t%d\t\t\t%e\n", 640,
  545. (double)mips_value * (640 * 8) /
  546. srtp_bits_per_second(640, &policy) / .02);
  547. }
  548. status = srtp_shutdown();
  549. if (status) {
  550. printf("error: srtp shutdown failed with error code %d\n", status);
  551. exit(1);
  552. }
  553. return 0;
  554. }
  555. /*
  556. * srtp_create_test_packet(len, ssrc) returns a pointer to a
  557. * (malloced) example RTP packet whose data field has the length given
  558. * by pkt_octet_len and the SSRC value ssrc. The total length of the
  559. * packet is twelve octets longer, since the header is at the
  560. * beginning. There is room at the end of the packet for a trailer,
  561. * and the four octets following the packet are filled with 0xff
  562. * values to enable testing for overwrites.
  563. *
  564. * note that the location of the test packet can (and should) be
  565. * deallocated with the free() call once it is no longer needed.
  566. */
  567. srtp_hdr_t *srtp_create_test_packet(int pkt_octet_len,
  568. uint32_t ssrc,
  569. int *pkt_len)
  570. {
  571. int i;
  572. uint8_t *buffer;
  573. srtp_hdr_t *hdr;
  574. int bytes_in_hdr = 12;
  575. /* allocate memory for test packet */
  576. hdr = (srtp_hdr_t *)malloc(pkt_octet_len + bytes_in_hdr +
  577. SRTP_MAX_TRAILER_LEN + 4);
  578. if (!hdr) {
  579. return NULL;
  580. }
  581. hdr->version = 2; /* RTP version two */
  582. hdr->p = 0; /* no padding needed */
  583. hdr->x = 0; /* no header extension */
  584. hdr->cc = 0; /* no CSRCs */
  585. hdr->m = 0; /* marker bit */
  586. hdr->pt = 0xf; /* payload type */
  587. hdr->seq = htons(0x1234); /* sequence number */
  588. hdr->ts = htonl(0xdecafbad); /* timestamp */
  589. hdr->ssrc = htonl(ssrc); /* synch. source */
  590. buffer = (uint8_t *)hdr;
  591. buffer += bytes_in_hdr;
  592. /* set RTP data to 0xab */
  593. for (i = 0; i < pkt_octet_len; i++) {
  594. *buffer++ = 0xab;
  595. }
  596. /* set post-data value to 0xffff to enable overrun checking */
  597. for (i = 0; i < SRTP_MAX_TRAILER_LEN + 4; i++) {
  598. *buffer++ = 0xff;
  599. }
  600. *pkt_len = bytes_in_hdr + pkt_octet_len;
  601. return hdr;
  602. }
  603. static srtp_hdr_t *srtp_create_test_packet_extended(int pkt_octet_len,
  604. uint32_t ssrc,
  605. uint16_t seq,
  606. uint32_t ts,
  607. int *pkt_len)
  608. {
  609. srtp_hdr_t *hdr;
  610. hdr = srtp_create_test_packet(pkt_octet_len, ssrc, pkt_len);
  611. if (hdr == NULL)
  612. return hdr;
  613. hdr->seq = htons(seq);
  614. hdr->ts = htonl(ts);
  615. return hdr;
  616. }
  617. srtp_hdr_t *srtp_create_test_packet_ext_hdr(int pkt_octet_len,
  618. uint32_t ssrc,
  619. int *pkt_len)
  620. {
  621. int i;
  622. uint8_t *buffer;
  623. srtp_hdr_t *hdr;
  624. int bytes_in_hdr = 12;
  625. uint8_t extension_header[12] = { /* one-byte header */
  626. 0xbe, 0xde,
  627. /* size */
  628. 0x00, 0x02,
  629. /* id 1, length 1 (i.e. 2 bytes) */
  630. 0x11,
  631. /* payload */
  632. 0xca, 0xfe,
  633. /* padding */
  634. 0x00,
  635. /* id 2, length 0 (i.e. 1 byte) */
  636. 0x20,
  637. /* payload */
  638. 0xba,
  639. /* padding */
  640. 0x00, 0x00
  641. };
  642. /* allocate memory for test packet */
  643. hdr = (srtp_hdr_t *)malloc(pkt_octet_len + bytes_in_hdr +
  644. sizeof(extension_header) + SRTP_MAX_TRAILER_LEN +
  645. 4);
  646. if (!hdr)
  647. return NULL;
  648. hdr->version = 2; /* RTP version two */
  649. hdr->p = 0; /* no padding needed */
  650. hdr->x = 1; /* no header extension */
  651. hdr->cc = 0; /* no CSRCs */
  652. hdr->m = 0; /* marker bit */
  653. hdr->pt = 0xf; /* payload type */
  654. hdr->seq = htons(0x1234); /* sequence number */
  655. hdr->ts = htonl(0xdecafbad); /* timestamp */
  656. hdr->ssrc = htonl(ssrc); /* synch. source */
  657. buffer = (uint8_t *)hdr;
  658. buffer += bytes_in_hdr;
  659. memcpy(buffer, extension_header, sizeof(extension_header));
  660. buffer += sizeof(extension_header);
  661. /* set RTP data to 0xab */
  662. for (i = 0; i < pkt_octet_len; i++)
  663. *buffer++ = 0xab;
  664. /* set post-data value to 0xffff to enable overrun checking */
  665. for (i = 0; i < SRTP_MAX_TRAILER_LEN + 4; i++)
  666. *buffer++ = 0xff;
  667. *pkt_len = bytes_in_hdr + sizeof(extension_header) + pkt_octet_len;
  668. return hdr;
  669. }
  670. void srtp_do_timing(const srtp_policy_t *policy)
  671. {
  672. int len;
  673. /*
  674. * note: the output of this function is formatted so that it
  675. * can be used in gnuplot. '#' indicates a comment, and "\r\n"
  676. * terminates a record
  677. */
  678. printf("# testing srtp throughput:\r\n");
  679. printf("# mesg length (octets)\tthroughput (megabits per second)\r\n");
  680. for (len = 16; len <= 2048; len *= 2) {
  681. printf("%d\t\t\t%f\r\n", len,
  682. srtp_bits_per_second(len, policy) / 1.0E6);
  683. }
  684. /* these extra linefeeds let gnuplot know that a dataset is done */
  685. printf("\r\n\r\n");
  686. }
  687. void srtp_do_rejection_timing(const srtp_policy_t *policy)
  688. {
  689. int len;
  690. /*
  691. * note: the output of this function is formatted so that it
  692. * can be used in gnuplot. '#' indicates a comment, and "\r\n"
  693. * terminates a record
  694. */
  695. printf("# testing srtp rejection throughput:\r\n");
  696. printf("# mesg length (octets)\trejections per second\r\n");
  697. for (len = 8; len <= 2048; len *= 2) {
  698. printf("%d\t\t\t%e\r\n", len, srtp_rejections_per_second(len, policy));
  699. }
  700. /* these extra linefeeds let gnuplot know that a dataset is done */
  701. printf("\r\n\r\n");
  702. }
  703. #define MAX_MSG_LEN 1024
  704. double srtp_bits_per_second(int msg_len_octets, const srtp_policy_t *policy)
  705. {
  706. srtp_t srtp;
  707. srtp_hdr_t *mesg;
  708. int i;
  709. clock_t timer;
  710. int num_trials = 100000;
  711. int input_len, len;
  712. uint32_t ssrc;
  713. srtp_err_status_t status;
  714. /*
  715. * allocate and initialize an srtp session
  716. */
  717. status = srtp_create(&srtp, policy);
  718. if (status) {
  719. printf("error: srtp_create() failed with error code %d\n", status);
  720. exit(1);
  721. }
  722. /*
  723. * if the ssrc is unspecified, use a predetermined one
  724. */
  725. if (policy->ssrc.type != ssrc_specific) {
  726. ssrc = 0xdeadbeef;
  727. } else {
  728. ssrc = policy->ssrc.value;
  729. }
  730. /*
  731. * create a test packet
  732. */
  733. mesg = srtp_create_test_packet(msg_len_octets, ssrc, &input_len);
  734. if (mesg == NULL) {
  735. return 0.0; /* indicate failure by returning zero */
  736. }
  737. timer = clock();
  738. for (i = 0; i < num_trials; i++) {
  739. len = input_len;
  740. /* srtp protect message */
  741. status = srtp_protect(srtp, mesg, &len);
  742. if (status) {
  743. printf("error: srtp_protect() failed with error code %d\n", status);
  744. exit(1);
  745. }
  746. /* increment message number */
  747. {
  748. /* hack sequence to avoid problems with macros for htons/ntohs on
  749. * some systems */
  750. short new_seq = ntohs(mesg->seq) + 1;
  751. mesg->seq = htons(new_seq);
  752. }
  753. }
  754. timer = clock() - timer;
  755. free(mesg);
  756. status = srtp_dealloc(srtp);
  757. if (status) {
  758. printf("error: srtp_dealloc() failed with error code %d\n", status);
  759. exit(1);
  760. }
  761. return (double)(msg_len_octets)*8 * num_trials * CLOCKS_PER_SEC / timer;
  762. }
  763. double srtp_rejections_per_second(int msg_len_octets,
  764. const srtp_policy_t *policy)
  765. {
  766. srtp_ctx_t *srtp;
  767. srtp_hdr_t *mesg;
  768. int i;
  769. int len;
  770. clock_t timer;
  771. int num_trials = 1000000;
  772. uint32_t ssrc = policy->ssrc.value;
  773. srtp_err_status_t status;
  774. /*
  775. * allocate and initialize an srtp session
  776. */
  777. status = srtp_create(&srtp, policy);
  778. if (status) {
  779. printf("error: srtp_create() failed with error code %d\n", status);
  780. exit(1);
  781. }
  782. mesg = srtp_create_test_packet(msg_len_octets, ssrc, &len);
  783. if (mesg == NULL) {
  784. return 0.0; /* indicate failure by returning zero */
  785. }
  786. srtp_protect(srtp, (srtp_hdr_t *)mesg, &len);
  787. timer = clock();
  788. for (i = 0; i < num_trials; i++) {
  789. len = msg_len_octets;
  790. srtp_unprotect(srtp, (srtp_hdr_t *)mesg, &len);
  791. }
  792. timer = clock() - timer;
  793. free(mesg);
  794. status = srtp_dealloc(srtp);
  795. if (status) {
  796. printf("error: srtp_dealloc() failed with error code %d\n", status);
  797. exit(1);
  798. }
  799. return (double)num_trials * CLOCKS_PER_SEC / timer;
  800. }
  801. void err_check(srtp_err_status_t s)
  802. {
  803. if (s == srtp_err_status_ok) {
  804. return;
  805. } else {
  806. fprintf(stderr, "error: unexpected srtp failure (code %d)\n", s);
  807. }
  808. exit(1);
  809. }
  810. srtp_err_status_t srtp_test_call_protect(srtp_t srtp_sender,
  811. srtp_hdr_t *hdr,
  812. int *len,
  813. int mki_index)
  814. {
  815. if (mki_index == -1) {
  816. return srtp_protect(srtp_sender, hdr, len);
  817. } else {
  818. return srtp_protect_mki(srtp_sender, hdr, len, 1, mki_index);
  819. }
  820. }
  821. srtp_err_status_t srtp_test_call_protect_rtcp(srtp_t srtp_sender,
  822. srtp_hdr_t *hdr,
  823. int *len,
  824. int mki_index)
  825. {
  826. if (mki_index == -1) {
  827. return srtp_protect_rtcp(srtp_sender, hdr, len);
  828. } else {
  829. return srtp_protect_rtcp_mki(srtp_sender, hdr, len, 1, mki_index);
  830. }
  831. }
  832. srtp_err_status_t srtp_test_call_unprotect(srtp_t srtp_sender,
  833. srtp_hdr_t *hdr,
  834. int *len,
  835. int use_mki)
  836. {
  837. if (use_mki == -1) {
  838. return srtp_unprotect(srtp_sender, hdr, len);
  839. } else {
  840. return srtp_unprotect_mki(srtp_sender, hdr, len, use_mki);
  841. }
  842. }
  843. srtp_err_status_t srtp_test_call_unprotect_rtcp(srtp_t srtp_sender,
  844. srtp_hdr_t *hdr,
  845. int *len,
  846. int use_mki)
  847. {
  848. if (use_mki == -1) {
  849. return srtp_unprotect_rtcp(srtp_sender, hdr, len);
  850. } else {
  851. return srtp_unprotect_rtcp_mki(srtp_sender, hdr, len, use_mki);
  852. }
  853. }
  854. srtp_err_status_t srtp_test(const srtp_policy_t *policy,
  855. int extension_header,
  856. int mki_index)
  857. {
  858. int i;
  859. srtp_t srtp_sender;
  860. srtp_t srtp_rcvr;
  861. srtp_err_status_t status = srtp_err_status_ok;
  862. srtp_hdr_t *hdr, *hdr2;
  863. uint8_t hdr_enc[64];
  864. uint8_t *pkt_end;
  865. int msg_len_octets, msg_len_enc, msg_len;
  866. int len, len2;
  867. uint32_t tag_length;
  868. uint32_t ssrc;
  869. srtp_policy_t *rcvr_policy;
  870. srtp_policy_t tmp_policy;
  871. int header = 1;
  872. int use_mki = 0;
  873. if (mki_index >= 0)
  874. use_mki = 1;
  875. if (extension_header) {
  876. memcpy(&tmp_policy, policy, sizeof(srtp_policy_t));
  877. tmp_policy.enc_xtn_hdr = &header;
  878. tmp_policy.enc_xtn_hdr_count = 1;
  879. err_check(srtp_create(&srtp_sender, &tmp_policy));
  880. } else {
  881. err_check(srtp_create(&srtp_sender, policy));
  882. }
  883. /* print out policy */
  884. err_check(srtp_session_print_policy(srtp_sender));
  885. /*
  886. * initialize data buffer, using the ssrc in the policy unless that
  887. * value is a wildcard, in which case we'll just use an arbitrary
  888. * one
  889. */
  890. if (policy->ssrc.type != ssrc_specific) {
  891. ssrc = 0xdecafbad;
  892. } else {
  893. ssrc = policy->ssrc.value;
  894. }
  895. msg_len_octets = 28;
  896. if (extension_header) {
  897. hdr = srtp_create_test_packet_ext_hdr(msg_len_octets, ssrc, &len);
  898. hdr2 = srtp_create_test_packet_ext_hdr(msg_len_octets, ssrc, &len2);
  899. } else {
  900. hdr = srtp_create_test_packet(msg_len_octets, ssrc, &len);
  901. hdr2 = srtp_create_test_packet(msg_len_octets, ssrc, &len2);
  902. }
  903. /* save original msg len */
  904. msg_len = len;
  905. if (hdr == NULL) {
  906. free(hdr2);
  907. return srtp_err_status_alloc_fail;
  908. }
  909. if (hdr2 == NULL) {
  910. free(hdr);
  911. return srtp_err_status_alloc_fail;
  912. }
  913. debug_print(mod_driver, "before protection:\n%s",
  914. srtp_packet_to_string(hdr, len));
  915. #if PRINT_REFERENCE_PACKET
  916. debug_print(mod_driver, "reference packet before protection:\n%s",
  917. octet_string_hex_string((uint8_t *)hdr, len));
  918. #endif
  919. err_check(srtp_test_call_protect(srtp_sender, hdr, &len, mki_index));
  920. debug_print(mod_driver, "after protection:\n%s",
  921. srtp_packet_to_string(hdr, len));
  922. #if PRINT_REFERENCE_PACKET
  923. debug_print(mod_driver, "after protection:\n%s",
  924. octet_string_hex_string((uint8_t *)hdr, len));
  925. #endif
  926. /* save protected message and length */
  927. memcpy(hdr_enc, hdr, len);
  928. msg_len_enc = len;
  929. /*
  930. * check for overrun of the srtp_protect() function
  931. *
  932. * The packet is followed by a value of 0xfffff; if the value of the
  933. * data following the packet is different, then we know that the
  934. * protect function is overwriting the end of the packet.
  935. */
  936. err_check(srtp_get_protect_trailer_length(srtp_sender, use_mki, mki_index,
  937. &tag_length));
  938. pkt_end = (uint8_t *)hdr + msg_len + tag_length;
  939. for (i = 0; i < 4; i++) {
  940. if (pkt_end[i] != 0xff) {
  941. fprintf(stdout, "overwrite in srtp_protect() function "
  942. "(expected %x, found %x in trailing octet %d)\n",
  943. 0xff, ((uint8_t *)hdr)[i], i);
  944. free(hdr);
  945. free(hdr2);
  946. return srtp_err_status_algo_fail;
  947. }
  948. }
  949. /*
  950. * if the policy includes confidentiality, check that ciphertext is
  951. * different than plaintext
  952. *
  953. * Note that this check will give false negatives, with some small
  954. * probability, especially if the packets are short. For that
  955. * reason, we skip this check if the plaintext is less than four
  956. * octets long.
  957. */
  958. if ((policy->rtp.sec_serv & sec_serv_conf) && (msg_len_octets >= 4)) {
  959. printf("testing that ciphertext is distinct from plaintext...");
  960. status = srtp_err_status_algo_fail;
  961. for (i = 12; i < msg_len_octets + 12; i++) {
  962. if (((uint8_t *)hdr)[i] != ((uint8_t *)hdr2)[i]) {
  963. status = srtp_err_status_ok;
  964. }
  965. }
  966. if (status) {
  967. printf("failed\n");
  968. free(hdr);
  969. free(hdr2);
  970. return status;
  971. }
  972. printf("passed\n");
  973. }
  974. /*
  975. * if the policy uses a 'wildcard' ssrc, then we need to make a copy
  976. * of the policy that changes the direction to inbound
  977. *
  978. * we always copy the policy into the rcvr_policy, since otherwise
  979. * the compiler would fret about the constness of the policy
  980. */
  981. rcvr_policy = (srtp_policy_t *)malloc(sizeof(srtp_policy_t));
  982. if (rcvr_policy == NULL) {
  983. free(hdr);
  984. free(hdr2);
  985. return srtp_err_status_alloc_fail;
  986. }
  987. if (extension_header) {
  988. memcpy(rcvr_policy, &tmp_policy, sizeof(srtp_policy_t));
  989. if (tmp_policy.ssrc.type == ssrc_any_outbound) {
  990. rcvr_policy->ssrc.type = ssrc_any_inbound;
  991. }
  992. } else {
  993. memcpy(rcvr_policy, policy, sizeof(srtp_policy_t));
  994. if (policy->ssrc.type == ssrc_any_outbound) {
  995. rcvr_policy->ssrc.type = ssrc_any_inbound;
  996. }
  997. }
  998. err_check(srtp_create(&srtp_rcvr, rcvr_policy));
  999. err_check(srtp_test_call_unprotect(srtp_rcvr, hdr, &len, use_mki));
  1000. debug_print(mod_driver, "after unprotection:\n%s",
  1001. srtp_packet_to_string(hdr, len));
  1002. /* verify that the unprotected packet matches the origial one */
  1003. for (i = 0; i < len; i++) {
  1004. if (((uint8_t *)hdr)[i] != ((uint8_t *)hdr2)[i]) {
  1005. fprintf(stdout, "mismatch at octet %d\n", i);
  1006. status = srtp_err_status_algo_fail;
  1007. }
  1008. }
  1009. if (status) {
  1010. free(hdr);
  1011. free(hdr2);
  1012. free(rcvr_policy);
  1013. return status;
  1014. }
  1015. /*
  1016. * if the policy includes authentication, then test for false positives
  1017. */
  1018. if (policy->rtp.sec_serv & sec_serv_auth) {
  1019. char *data = ((char *)hdr) + (extension_header ? 24 : 12);
  1020. printf("testing for false positives in replay check...");
  1021. /* unprotect a second time - should fail with a replay error */
  1022. status =
  1023. srtp_test_call_unprotect(srtp_rcvr, hdr, &msg_len_enc, use_mki);
  1024. if (status != srtp_err_status_replay_fail) {
  1025. printf("failed with error code %d\n", status);
  1026. free(hdr);
  1027. free(hdr2);
  1028. free(rcvr_policy);
  1029. return status;
  1030. } else {
  1031. printf("passed\n");
  1032. }
  1033. printf("testing for false positives in auth check...");
  1034. /* increment sequence number in header */
  1035. hdr->seq++;
  1036. /* apply protection */
  1037. err_check(srtp_test_call_protect(srtp_sender, hdr, &len, mki_index));
  1038. /* flip bits in packet */
  1039. data[0] ^= 0xff;
  1040. /* unprotect, and check for authentication failure */
  1041. status = srtp_test_call_unprotect(srtp_rcvr, hdr, &len, use_mki);
  1042. if (status != srtp_err_status_auth_fail) {
  1043. printf("failed\n");
  1044. free(hdr);
  1045. free(hdr2);
  1046. free(rcvr_policy);
  1047. return status;
  1048. } else {
  1049. printf("passed\n");
  1050. }
  1051. }
  1052. err_check(srtp_dealloc(srtp_sender));
  1053. err_check(srtp_dealloc(srtp_rcvr));
  1054. free(hdr);
  1055. free(hdr2);
  1056. free(rcvr_policy);
  1057. return srtp_err_status_ok;
  1058. }
  1059. srtp_err_status_t srtcp_test(const srtp_policy_t *policy, int mki_index)
  1060. {
  1061. int i;
  1062. srtp_t srtcp_sender;
  1063. srtp_t srtcp_rcvr;
  1064. srtp_err_status_t status = srtp_err_status_ok;
  1065. srtp_hdr_t *hdr, *hdr2;
  1066. uint8_t hdr_enc[64];
  1067. uint8_t *pkt_end;
  1068. int msg_len_octets, msg_len_enc, msg_len;
  1069. int len, len2;
  1070. uint32_t tag_length;
  1071. uint32_t ssrc;
  1072. srtp_policy_t *rcvr_policy;
  1073. int use_mki = 0;
  1074. if (mki_index >= 0)
  1075. use_mki = 1;
  1076. err_check(srtp_create(&srtcp_sender, policy));
  1077. /* print out policy */
  1078. err_check(srtp_session_print_policy(srtcp_sender));
  1079. /*
  1080. * initialize data buffer, using the ssrc in the policy unless that
  1081. * value is a wildcard, in which case we'll just use an arbitrary
  1082. * one
  1083. */
  1084. if (policy->ssrc.type != ssrc_specific) {
  1085. ssrc = 0xdecafbad;
  1086. } else {
  1087. ssrc = policy->ssrc.value;
  1088. }
  1089. msg_len_octets = 28;
  1090. hdr = srtp_create_test_packet(msg_len_octets, ssrc, &len);
  1091. /* save message len */
  1092. msg_len = len;
  1093. if (hdr == NULL) {
  1094. return srtp_err_status_alloc_fail;
  1095. }
  1096. hdr2 = srtp_create_test_packet(msg_len_octets, ssrc, &len2);
  1097. if (hdr2 == NULL) {
  1098. free(hdr);
  1099. return srtp_err_status_alloc_fail;
  1100. }
  1101. debug_print(mod_driver, "before protection:\n%s",
  1102. srtp_packet_to_string(hdr, len));
  1103. #if PRINT_REFERENCE_PACKET
  1104. debug_print(mod_driver, "reference packet before protection:\n%s",
  1105. octet_string_hex_string((uint8_t *)hdr, len));
  1106. #endif
  1107. err_check(srtp_test_call_protect_rtcp(srtcp_sender, hdr, &len, mki_index));
  1108. debug_print(mod_driver, "after protection:\n%s",
  1109. srtp_packet_to_string(hdr, len));
  1110. #if PRINT_REFERENCE_PACKET
  1111. debug_print(mod_driver, "after protection:\n%s",
  1112. octet_string_hex_string((uint8_t *)hdr, len));
  1113. #endif
  1114. /* save protected message and length */
  1115. memcpy(hdr_enc, hdr, len);
  1116. msg_len_enc = len;
  1117. /*
  1118. * check for overrun of the srtp_protect() function
  1119. *
  1120. * The packet is followed by a value of 0xfffff; if the value of the
  1121. * data following the packet is different, then we know that the
  1122. * protect function is overwriting the end of the packet.
  1123. */
  1124. srtp_get_protect_rtcp_trailer_length(srtcp_sender, use_mki, mki_index,
  1125. &tag_length);
  1126. pkt_end = (uint8_t *)hdr + msg_len + tag_length;
  1127. for (i = 0; i < 4; i++) {
  1128. if (pkt_end[i] != 0xff) {
  1129. fprintf(stdout, "overwrite in srtp_protect_rtcp() function "
  1130. "(expected %x, found %x in trailing octet %d)\n",
  1131. 0xff, ((uint8_t *)hdr)[i], i);
  1132. free(hdr);
  1133. free(hdr2);
  1134. return srtp_err_status_algo_fail;
  1135. }
  1136. }
  1137. /*
  1138. * if the policy includes confidentiality, check that ciphertext is
  1139. * different than plaintext
  1140. *
  1141. * Note that this check will give false negatives, with some small
  1142. * probability, especially if the packets are short. For that
  1143. * reason, we skip this check if the plaintext is less than four
  1144. * octets long.
  1145. */
  1146. if ((policy->rtcp.sec_serv & sec_serv_conf) && (msg_len_octets >= 4)) {
  1147. printf("testing that ciphertext is distinct from plaintext...");
  1148. status = srtp_err_status_algo_fail;
  1149. for (i = 12; i < msg_len_octets + 12; i++) {
  1150. if (((uint8_t *)hdr)[i] != ((uint8_t *)hdr2)[i]) {
  1151. status = srtp_err_status_ok;
  1152. }
  1153. }
  1154. if (status) {
  1155. printf("failed\n");
  1156. free(hdr);
  1157. free(hdr2);
  1158. return status;
  1159. }
  1160. printf("passed\n");
  1161. }
  1162. /*
  1163. * if the policy uses a 'wildcard' ssrc, then we need to make a copy
  1164. * of the policy that changes the direction to inbound
  1165. *
  1166. * we always copy the policy into the rcvr_policy, since otherwise
  1167. * the compiler would fret about the constness of the policy
  1168. */
  1169. rcvr_policy = (srtp_policy_t *)malloc(sizeof(srtp_policy_t));
  1170. if (rcvr_policy == NULL) {
  1171. free(hdr);
  1172. free(hdr2);
  1173. return srtp_err_status_alloc_fail;
  1174. }
  1175. memcpy(rcvr_policy, policy, sizeof(srtp_policy_t));
  1176. if (policy->ssrc.type == ssrc_any_outbound) {
  1177. rcvr_policy->ssrc.type = ssrc_any_inbound;
  1178. }
  1179. err_check(srtp_create(&srtcp_rcvr, rcvr_policy));
  1180. err_check(srtp_test_call_unprotect_rtcp(srtcp_rcvr, hdr, &len, use_mki));
  1181. debug_print(mod_driver, "after unprotection:\n%s",
  1182. srtp_packet_to_string(hdr, len));
  1183. /* verify that the unprotected packet matches the origial one */
  1184. for (i = 0; i < len; i++) {
  1185. if (((uint8_t *)hdr)[i] != ((uint8_t *)hdr2)[i]) {
  1186. fprintf(stdout, "mismatch at octet %d\n", i);
  1187. status = srtp_err_status_algo_fail;
  1188. }
  1189. }
  1190. if (status) {
  1191. free(hdr);
  1192. free(hdr2);
  1193. free(rcvr_policy);
  1194. return status;
  1195. }
  1196. /*
  1197. * if the policy includes authentication, then test for false positives
  1198. */
  1199. if (policy->rtp.sec_serv & sec_serv_auth) {
  1200. char *data = ((char *)hdr) + 12;
  1201. printf("testing for false positives in replay check...");
  1202. /* unprotect a second time - should fail with a replay error */
  1203. status = srtp_test_call_unprotect_rtcp(srtcp_rcvr, hdr, &msg_len_enc,
  1204. use_mki);
  1205. if (status != srtp_err_status_replay_fail) {
  1206. printf("failed with error code %d\n", status);
  1207. free(hdr);
  1208. free(hdr2);
  1209. free(rcvr_policy);
  1210. return status;
  1211. } else {
  1212. printf("passed\n");
  1213. }
  1214. printf("testing for false positives in auth check...");
  1215. /* increment sequence number in header */
  1216. hdr->seq++;
  1217. /* apply protection */
  1218. err_check(
  1219. srtp_test_call_protect_rtcp(srtcp_sender, hdr, &len, mki_index));
  1220. /* flip bits in packet */
  1221. data[0] ^= 0xff;
  1222. /* unprotect, and check for authentication failure */
  1223. status = srtp_test_call_unprotect_rtcp(srtcp_rcvr, hdr, &len, use_mki);
  1224. if (status != srtp_err_status_auth_fail) {
  1225. printf("failed\n");
  1226. free(hdr);
  1227. free(hdr2);
  1228. free(rcvr_policy);
  1229. return status;
  1230. } else {
  1231. printf("passed\n");
  1232. }
  1233. }
  1234. err_check(srtp_dealloc(srtcp_sender));
  1235. err_check(srtp_dealloc(srtcp_rcvr));
  1236. free(hdr);
  1237. free(hdr2);
  1238. free(rcvr_policy);
  1239. return srtp_err_status_ok;
  1240. }
  1241. srtp_err_status_t srtp_session_print_policy(srtp_t srtp)
  1242. {
  1243. char *serv_descr[4] = { "none", "confidentiality", "authentication",
  1244. "confidentiality and authentication" };
  1245. char *direction[3] = { "unknown", "outbound", "inbound" };
  1246. srtp_stream_t stream;
  1247. srtp_session_keys_t *session_keys = NULL;
  1248. /* sanity checking */
  1249. if (srtp == NULL) {
  1250. return srtp_err_status_fail;
  1251. }
  1252. /* if there's a template stream, print it out */
  1253. if (srtp->stream_template != NULL) {
  1254. stream = srtp->stream_template;
  1255. session_keys = &stream->session_keys[0];
  1256. printf("# SSRC: any %s\r\n"
  1257. "# rtp cipher: %s\r\n"
  1258. "# rtp auth: %s\r\n"
  1259. "# rtp services: %s\r\n"
  1260. "# rtcp cipher: %s\r\n"
  1261. "# rtcp auth: %s\r\n"
  1262. "# rtcp services: %s\r\n"
  1263. "# window size: %lu\r\n"
  1264. "# tx rtx allowed:%s\r\n",
  1265. direction[stream->direction],
  1266. session_keys->rtp_cipher->type->description,
  1267. session_keys->rtp_auth->type->description,
  1268. serv_descr[stream->rtp_services],
  1269. session_keys->rtcp_cipher->type->description,
  1270. session_keys->rtcp_auth->type->description,
  1271. serv_descr[stream->rtcp_services],
  1272. srtp_rdbx_get_window_size(&stream->rtp_rdbx),
  1273. stream->allow_repeat_tx ? "true" : "false");
  1274. printf("# Encrypted extension headers: ");
  1275. if (stream->enc_xtn_hdr && stream->enc_xtn_hdr_count > 0) {
  1276. int *enc_xtn_hdr = stream->enc_xtn_hdr;
  1277. int count = stream->enc_xtn_hdr_count;
  1278. while (count > 0) {
  1279. printf("%d ", *enc_xtn_hdr);
  1280. enc_xtn_hdr++;
  1281. count--;
  1282. }
  1283. printf("\n");
  1284. } else {
  1285. printf("none\n");
  1286. }
  1287. }
  1288. /* loop over streams in session, printing the policy of each */
  1289. stream = srtp->stream_list;
  1290. while (stream != NULL) {
  1291. if (stream->rtp_services > sec_serv_conf_and_auth) {
  1292. return srtp_err_status_bad_param;
  1293. }
  1294. session_keys = &stream->session_keys[0];
  1295. printf("# SSRC: 0x%08x\r\n"
  1296. "# rtp cipher: %s\r\n"
  1297. "# rtp auth: %s\r\n"
  1298. "# rtp services: %s\r\n"
  1299. "# rtcp cipher: %s\r\n"
  1300. "# rtcp auth: %s\r\n"
  1301. "# rtcp services: %s\r\n"
  1302. "# window size: %lu\r\n"
  1303. "# tx rtx allowed:%s\r\n",
  1304. stream->ssrc, session_keys->rtp_cipher->type->description,
  1305. session_keys->rtp_auth->type->description,
  1306. serv_descr[stream->rtp_services],
  1307. session_keys->rtcp_cipher->type->description,
  1308. session_keys->rtcp_auth->type->description,
  1309. serv_descr[stream->rtcp_services],
  1310. srtp_rdbx_get_window_size(&stream->rtp_rdbx),
  1311. stream->allow_repeat_tx ? "true" : "false");
  1312. printf("# Encrypted extension headers: ");
  1313. if (stream->enc_xtn_hdr && stream->enc_xtn_hdr_count > 0) {
  1314. int *enc_xtn_hdr = stream->enc_xtn_hdr;
  1315. int count = stream->enc_xtn_hdr_count;
  1316. while (count > 0) {
  1317. printf("%d ", *enc_xtn_hdr);
  1318. enc_xtn_hdr++;
  1319. count--;
  1320. }
  1321. printf("\n");
  1322. } else {
  1323. printf("none\n");
  1324. }
  1325. /* advance to next stream in the list */
  1326. stream = stream->next;
  1327. }
  1328. return srtp_err_status_ok;
  1329. }
  1330. srtp_err_status_t srtp_print_policy(const srtp_policy_t *policy)
  1331. {
  1332. srtp_err_status_t status;
  1333. srtp_t session;
  1334. status = srtp_create(&session, policy);
  1335. if (status) {
  1336. return status;
  1337. }
  1338. status = srtp_session_print_policy(session);
  1339. if (status) {
  1340. return status;
  1341. }
  1342. status = srtp_dealloc(session);
  1343. if (status) {
  1344. return status;
  1345. }
  1346. return srtp_err_status_ok;
  1347. }
  1348. /*
  1349. * srtp_print_packet(...) is for debugging only
  1350. * it prints an RTP packet to the stdout
  1351. *
  1352. * note that this function is *not* threadsafe
  1353. */
  1354. #include <stdio.h>
  1355. #define MTU 2048
  1356. char packet_string[MTU];
  1357. char *srtp_packet_to_string(srtp_hdr_t *hdr, int pkt_octet_len)
  1358. {
  1359. int octets_in_rtp_header = 12;
  1360. uint8_t *data = ((uint8_t *)hdr) + octets_in_rtp_header;
  1361. int hex_len = pkt_octet_len - octets_in_rtp_header;
  1362. /* sanity checking */
  1363. if ((hdr == NULL) || (pkt_octet_len > MTU)) {
  1364. return NULL;
  1365. }
  1366. /* write packet into string */
  1367. sprintf(packet_string, "(s)rtp packet: {\n"
  1368. " version:\t%d\n"
  1369. " p:\t\t%d\n"
  1370. " x:\t\t%d\n"
  1371. " cc:\t\t%d\n"
  1372. " m:\t\t%d\n"
  1373. " pt:\t\t%x\n"
  1374. " seq:\t\t%x\n"
  1375. " ts:\t\t%x\n"
  1376. " ssrc:\t%x\n"
  1377. " data:\t%s\n"
  1378. "} (%d octets in total)\n",
  1379. hdr->version, hdr->p, hdr->x, hdr->cc, hdr->m, hdr->pt, hdr->seq,
  1380. hdr->ts, hdr->ssrc, octet_string_hex_string(data, hex_len),
  1381. pkt_octet_len);
  1382. return packet_string;
  1383. }
  1384. /*
  1385. * mips_estimate() is a simple function to estimate the number of
  1386. * instructions per second that the host can perform. note that this
  1387. * function can be grossly wrong; you may want to have a manual sanity
  1388. * check of its output!
  1389. *
  1390. * the 'ignore' pointer is there to convince the compiler to not just
  1391. * optimize away the function
  1392. */
  1393. double mips_estimate(int num_trials, int *ignore)
  1394. {
  1395. clock_t t;
  1396. volatile int i, sum;
  1397. sum = 0;
  1398. t = clock();
  1399. for (i = 0; i < num_trials; i++) {
  1400. sum += i;
  1401. }
  1402. t = clock() - t;
  1403. if (t < 1) {
  1404. t = 1;
  1405. }
  1406. /* printf("%d\n", sum); */
  1407. *ignore = sum;
  1408. return (double)num_trials * CLOCKS_PER_SEC / t;
  1409. }
  1410. /*
  1411. * srtp_validate() verifies the correctness of libsrtp by comparing
  1412. * some computed packets against some pre-computed reference values.
  1413. * These packets were made with the default SRTP policy.
  1414. */
  1415. srtp_err_status_t srtp_validate()
  1416. {
  1417. // clang-format off
  1418. uint8_t srtp_plaintext_ref[28] = {
  1419. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1420. 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
  1421. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1422. 0xab, 0xab, 0xab, 0xab
  1423. };
  1424. uint8_t srtp_plaintext[38] = {
  1425. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1426. 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
  1427. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1428. 0xab, 0xab, 0xab, 0xab, 0x00, 0x00, 0x00, 0x00,
  1429. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1430. };
  1431. uint8_t srtp_ciphertext[38] = {
  1432. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1433. 0xca, 0xfe, 0xba, 0xbe, 0x4e, 0x55, 0xdc, 0x4c,
  1434. 0xe7, 0x99, 0x78, 0xd8, 0x8c, 0xa4, 0xd2, 0x15,
  1435. 0x94, 0x9d, 0x24, 0x02, 0xb7, 0x8d, 0x6a, 0xcc,
  1436. 0x99, 0xea, 0x17, 0x9b, 0x8d, 0xbb
  1437. };
  1438. uint8_t rtcp_plaintext_ref[24] = {
  1439. 0x81, 0xc8, 0x00, 0x0b, 0xca, 0xfe, 0xba, 0xbe,
  1440. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1441. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1442. };
  1443. uint8_t rtcp_plaintext[38] = {
  1444. 0x81, 0xc8, 0x00, 0x0b, 0xca, 0xfe, 0xba, 0xbe,
  1445. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1446. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1447. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1448. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1449. };
  1450. uint8_t srtcp_ciphertext[38] = {
  1451. 0x81, 0xc8, 0x00, 0x0b, 0xca, 0xfe, 0xba, 0xbe,
  1452. 0x71, 0x28, 0x03, 0x5b, 0xe4, 0x87, 0xb9, 0xbd,
  1453. 0xbe, 0xf8, 0x90, 0x41, 0xf9, 0x77, 0xa5, 0xa8,
  1454. 0x80, 0x00, 0x00, 0x01, 0x99, 0x3e, 0x08, 0xcd,
  1455. 0x54, 0xd6, 0xc1, 0x23, 0x07, 0x98
  1456. };
  1457. // clang-format on
  1458. srtp_t srtp_snd, srtp_recv;
  1459. srtp_err_status_t status;
  1460. int len;
  1461. srtp_policy_t policy;
  1462. /*
  1463. * create a session with a single stream using the default srtp
  1464. * policy and with the SSRC value 0xcafebabe
  1465. */
  1466. memset(&policy, 0, sizeof(policy));
  1467. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  1468. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  1469. policy.ssrc.type = ssrc_specific;
  1470. policy.ssrc.value = 0xcafebabe;
  1471. policy.key = test_key;
  1472. policy.ekt = NULL;
  1473. policy.window_size = 128;
  1474. policy.allow_repeat_tx = 0;
  1475. policy.next = NULL;
  1476. status = srtp_create(&srtp_snd, &policy);
  1477. if (status) {
  1478. return status;
  1479. }
  1480. /*
  1481. * protect plaintext, then compare with ciphertext
  1482. */
  1483. len = 28;
  1484. status = srtp_protect(srtp_snd, srtp_plaintext, &len);
  1485. if (status || (len != 38)) {
  1486. return srtp_err_status_fail;
  1487. }
  1488. debug_print(mod_driver, "ciphertext:\n %s",
  1489. octet_string_hex_string(srtp_plaintext, len));
  1490. debug_print(mod_driver, "ciphertext reference:\n %s",
  1491. octet_string_hex_string(srtp_ciphertext, len));
  1492. if (srtp_octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len)) {
  1493. return srtp_err_status_fail;
  1494. }
  1495. /*
  1496. * protect plaintext rtcp, then compare with srtcp ciphertext
  1497. */
  1498. len = 24;
  1499. status = srtp_protect_rtcp(srtp_snd, rtcp_plaintext, &len);
  1500. if (status || (len != 38)) {
  1501. return srtp_err_status_fail;
  1502. }
  1503. debug_print(mod_driver, "srtcp ciphertext:\n %s",
  1504. octet_string_hex_string(rtcp_plaintext, len));
  1505. debug_print(mod_driver, "srtcp ciphertext reference:\n %s",
  1506. octet_string_hex_string(srtcp_ciphertext, len));
  1507. if (srtp_octet_string_is_eq(rtcp_plaintext, srtcp_ciphertext, len)) {
  1508. return srtp_err_status_fail;
  1509. }
  1510. /*
  1511. * create a receiver session context comparable to the one created
  1512. * above - we need to do this so that the replay checking doesn't
  1513. * complain
  1514. */
  1515. status = srtp_create(&srtp_recv, &policy);
  1516. if (status) {
  1517. return status;
  1518. }
  1519. /*
  1520. * unprotect ciphertext, then compare with plaintext
  1521. */
  1522. status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
  1523. if (status || (len != 28)) {
  1524. return status;
  1525. }
  1526. if (srtp_octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len)) {
  1527. return srtp_err_status_fail;
  1528. }
  1529. /*
  1530. * unprotect srtcp ciphertext, then compare with rtcp plaintext
  1531. */
  1532. len = 38;
  1533. status = srtp_unprotect_rtcp(srtp_recv, srtcp_ciphertext, &len);
  1534. if (status || (len != 24)) {
  1535. return status;
  1536. }
  1537. if (srtp_octet_string_is_eq(srtcp_ciphertext, rtcp_plaintext_ref, len)) {
  1538. return srtp_err_status_fail;
  1539. }
  1540. status = srtp_dealloc(srtp_snd);
  1541. if (status) {
  1542. return status;
  1543. }
  1544. status = srtp_dealloc(srtp_recv);
  1545. if (status) {
  1546. return status;
  1547. }
  1548. return srtp_err_status_ok;
  1549. }
  1550. #ifdef GCM
  1551. /*
  1552. * srtp_validate_gcm() verifies the correctness of libsrtp by comparing
  1553. * an computed packet against the known ciphertext for the plaintext.
  1554. */
  1555. srtp_err_status_t srtp_validate_gcm()
  1556. {
  1557. // clang-format off
  1558. unsigned char test_key_gcm[28] = {
  1559. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  1560. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  1561. 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  1562. 0xa8, 0xa9, 0xaa, 0xab
  1563. };
  1564. uint8_t rtp_plaintext_ref[28] = {
  1565. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1566. 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
  1567. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1568. 0xab, 0xab, 0xab, 0xab
  1569. };
  1570. uint8_t rtp_plaintext[44] = {
  1571. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1572. 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
  1573. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1574. 0xab, 0xab, 0xab, 0xab, 0x00, 0x00, 0x00, 0x00,
  1575. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1576. 0x00, 0x00, 0x00, 0x00
  1577. };
  1578. uint8_t srtp_ciphertext[44] = {
  1579. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1580. 0xca, 0xfe, 0xba, 0xbe, 0xc5, 0x00, 0x2e, 0xde,
  1581. 0x04, 0xcf, 0xdd, 0x2e, 0xb9, 0x11, 0x59, 0xe0,
  1582. 0x88, 0x0a, 0xa0, 0x6e, 0xd2, 0x97, 0x68, 0x26,
  1583. 0xf7, 0x96, 0xb2, 0x01, 0xdf, 0x31, 0x31, 0xa1,
  1584. 0x27, 0xe8, 0xa3, 0x92
  1585. };
  1586. uint8_t rtcp_plaintext_ref[24] = {
  1587. 0x81, 0xc8, 0x00, 0x0b, 0xca, 0xfe, 0xba, 0xbe,
  1588. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1589. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1590. };
  1591. uint8_t rtcp_plaintext[44] = {
  1592. 0x81, 0xc8, 0x00, 0x0b, 0xca, 0xfe, 0xba, 0xbe,
  1593. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1594. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1595. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1596. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1597. 0x00, 0x00, 0x00, 0x00
  1598. };
  1599. uint8_t srtcp_ciphertext[44] = {
  1600. 0x81, 0xc8, 0x00, 0x0b, 0xca, 0xfe, 0xba, 0xbe,
  1601. 0xc9, 0x8b, 0x8b, 0x5d, 0xf0, 0x39, 0x2a, 0x55,
  1602. 0x85, 0x2b, 0x6c, 0x21, 0xac, 0x8e, 0x70, 0x25,
  1603. 0xc5, 0x2c, 0x6f, 0xbe, 0xa2, 0xb3, 0xb4, 0x46,
  1604. 0xea, 0x31, 0x12, 0x3b, 0xa8, 0x8c, 0xe6, 0x1e,
  1605. 0x80, 0x00, 0x00, 0x01
  1606. };
  1607. // clang-format on
  1608. srtp_t srtp_snd, srtp_recv;
  1609. srtp_err_status_t status;
  1610. int len;
  1611. srtp_policy_t policy;
  1612. /*
  1613. * create a session with a single stream using the default srtp
  1614. * policy and with the SSRC value 0xcafebabe
  1615. */
  1616. memset(&policy, 0, sizeof(policy));
  1617. srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtp);
  1618. srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy.rtcp);
  1619. policy.ssrc.type = ssrc_specific;
  1620. policy.ssrc.value = 0xcafebabe;
  1621. policy.key = test_key_gcm;
  1622. policy.ekt = NULL;
  1623. policy.window_size = 128;
  1624. policy.allow_repeat_tx = 0;
  1625. policy.next = NULL;
  1626. status = srtp_create(&srtp_snd, &policy);
  1627. if (status) {
  1628. return status;
  1629. }
  1630. /*
  1631. * protect plaintext rtp, then compare with srtp ciphertext
  1632. */
  1633. len = 28;
  1634. status = srtp_protect(srtp_snd, rtp_plaintext, &len);
  1635. if (status || (len != 44)) {
  1636. return srtp_err_status_fail;
  1637. }
  1638. debug_print(mod_driver, "srtp ciphertext:\n %s",
  1639. octet_string_hex_string(rtp_plaintext, len));
  1640. debug_print(mod_driver, "srtp ciphertext reference:\n %s",
  1641. octet_string_hex_string(srtp_ciphertext, len));
  1642. if (srtp_octet_string_is_eq(rtp_plaintext, srtp_ciphertext, len)) {
  1643. return srtp_err_status_fail;
  1644. }
  1645. /*
  1646. * protect plaintext rtcp, then compare with srtcp ciphertext
  1647. */
  1648. len = 24;
  1649. status = srtp_protect_rtcp(srtp_snd, rtcp_plaintext, &len);
  1650. if (status || (len != 44)) {
  1651. return srtp_err_status_fail;
  1652. }
  1653. debug_print(mod_driver, "srtcp ciphertext:\n %s",
  1654. octet_string_hex_string(rtcp_plaintext, len));
  1655. debug_print(mod_driver, "srtcp ciphertext reference:\n %s",
  1656. octet_string_hex_string(srtcp_ciphertext, len));
  1657. if (srtp_octet_string_is_eq(rtcp_plaintext, srtcp_ciphertext, len)) {
  1658. return srtp_err_status_fail;
  1659. }
  1660. /*
  1661. * create a receiver session context comparable to the one created
  1662. * above - we need to do this so that the replay checking doesn't
  1663. * complain
  1664. */
  1665. status = srtp_create(&srtp_recv, &policy);
  1666. if (status) {
  1667. return status;
  1668. }
  1669. /*
  1670. * unprotect srtp ciphertext, then compare with rtp plaintext
  1671. */
  1672. len = 44;
  1673. status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
  1674. if (status || (len != 28)) {
  1675. return status;
  1676. }
  1677. if (srtp_octet_string_is_eq(srtp_ciphertext, rtp_plaintext_ref, len)) {
  1678. return srtp_err_status_fail;
  1679. }
  1680. /*
  1681. * unprotect srtcp ciphertext, then compare with rtcp plaintext
  1682. */
  1683. len = 44;
  1684. status = srtp_unprotect_rtcp(srtp_recv, srtcp_ciphertext, &len);
  1685. if (status || (len != 24)) {
  1686. return status;
  1687. }
  1688. if (srtp_octet_string_is_eq(srtcp_ciphertext, rtcp_plaintext_ref, len)) {
  1689. return srtp_err_status_fail;
  1690. }
  1691. status = srtp_dealloc(srtp_snd);
  1692. if (status) {
  1693. return status;
  1694. }
  1695. status = srtp_dealloc(srtp_recv);
  1696. if (status) {
  1697. return status;
  1698. }
  1699. return srtp_err_status_ok;
  1700. }
  1701. #endif
  1702. /*
  1703. * Test vectors taken from RFC 6904, Appendix A
  1704. */
  1705. srtp_err_status_t srtp_validate_encrypted_extensions_headers()
  1706. {
  1707. // clang-format off
  1708. unsigned char test_key_ext_headers[30] = {
  1709. 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
  1710. 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
  1711. 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
  1712. 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
  1713. };
  1714. uint8_t srtp_plaintext_ref[56] = {
  1715. 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1716. 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
  1717. 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
  1718. 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
  1719. 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
  1720. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1721. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab
  1722. };
  1723. uint8_t srtp_plaintext[66] = {
  1724. 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1725. 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
  1726. 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
  1727. 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
  1728. 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
  1729. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1730. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1731. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1732. 0x00, 0x00
  1733. };
  1734. uint8_t srtp_ciphertext[66] = {
  1735. 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1736. 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
  1737. 0x17, 0x58, 0x8A, 0x92, 0x70, 0xF4, 0xE1, 0x5E,
  1738. 0x1C, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x95, 0x46,
  1739. 0xA9, 0x94, 0xF0, 0xBC, 0x54, 0x78, 0x97, 0x00,
  1740. 0x4e, 0x55, 0xdc, 0x4c, 0xe7, 0x99, 0x78, 0xd8,
  1741. 0x8c, 0xa4, 0xd2, 0x15, 0x94, 0x9d, 0x24, 0x02,
  1742. 0x5a, 0x46, 0xb3, 0xca, 0x35, 0xc5, 0x35, 0xa8,
  1743. 0x91, 0xc7
  1744. };
  1745. // clang-format on
  1746. srtp_t srtp_snd, srtp_recv;
  1747. srtp_err_status_t status;
  1748. int len;
  1749. srtp_policy_t policy;
  1750. int headers[3] = { 1, 3, 4 };
  1751. /*
  1752. * create a session with a single stream using the default srtp
  1753. * policy and with the SSRC value 0xcafebabe
  1754. */
  1755. memset(&policy, 0, sizeof(policy));
  1756. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  1757. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  1758. policy.ssrc.type = ssrc_specific;
  1759. policy.ssrc.value = 0xcafebabe;
  1760. policy.key = test_key_ext_headers;
  1761. policy.ekt = NULL;
  1762. policy.window_size = 128;
  1763. policy.allow_repeat_tx = 0;
  1764. policy.enc_xtn_hdr = headers;
  1765. policy.enc_xtn_hdr_count = sizeof(headers) / sizeof(headers[0]);
  1766. policy.next = NULL;
  1767. status = srtp_create(&srtp_snd, &policy);
  1768. if (status)
  1769. return status;
  1770. /*
  1771. * protect plaintext, then compare with ciphertext
  1772. */
  1773. len = sizeof(srtp_plaintext_ref);
  1774. status = srtp_protect(srtp_snd, srtp_plaintext, &len);
  1775. if (status || (len != sizeof(srtp_plaintext)))
  1776. return srtp_err_status_fail;
  1777. debug_print(mod_driver, "ciphertext:\n %s",
  1778. srtp_octet_string_hex_string(srtp_plaintext, len));
  1779. debug_print(mod_driver, "ciphertext reference:\n %s",
  1780. srtp_octet_string_hex_string(srtp_ciphertext, len));
  1781. if (srtp_octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len))
  1782. return srtp_err_status_fail;
  1783. /*
  1784. * create a receiver session context comparable to the one created
  1785. * above - we need to do this so that the replay checking doesn't
  1786. * complain
  1787. */
  1788. status = srtp_create(&srtp_recv, &policy);
  1789. if (status)
  1790. return status;
  1791. /*
  1792. * unprotect ciphertext, then compare with plaintext
  1793. */
  1794. status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
  1795. if (status) {
  1796. return status;
  1797. } else if (len != sizeof(srtp_plaintext_ref)) {
  1798. return srtp_err_status_fail;
  1799. }
  1800. if (srtp_octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len))
  1801. return srtp_err_status_fail;
  1802. status = srtp_dealloc(srtp_snd);
  1803. if (status)
  1804. return status;
  1805. status = srtp_dealloc(srtp_recv);
  1806. if (status)
  1807. return status;
  1808. return srtp_err_status_ok;
  1809. }
  1810. #ifdef GCM
  1811. /*
  1812. * Headers of test vectors taken from RFC 6904, Appendix A
  1813. */
  1814. srtp_err_status_t srtp_validate_encrypted_extensions_headers_gcm()
  1815. {
  1816. // clang-format off
  1817. unsigned char test_key_ext_headers[30] = {
  1818. 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
  1819. 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
  1820. 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
  1821. 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
  1822. };
  1823. uint8_t srtp_plaintext_ref[56] = {
  1824. 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1825. 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
  1826. 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
  1827. 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
  1828. 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
  1829. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1830. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab
  1831. };
  1832. uint8_t srtp_plaintext[64] = {
  1833. 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1834. 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
  1835. 0x17, 0x41, 0x42, 0x73, 0xA4, 0x75, 0x26, 0x27,
  1836. 0x48, 0x22, 0x00, 0x00, 0xC8, 0x30, 0x8E, 0x46,
  1837. 0x55, 0x99, 0x63, 0x86, 0xB3, 0x95, 0xFB, 0x00,
  1838. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1839. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1840. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1841. };
  1842. uint8_t srtp_ciphertext[64] = {
  1843. 0x90, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1844. 0xca, 0xfe, 0xba, 0xbe, 0xBE, 0xDE, 0x00, 0x06,
  1845. 0x17, 0x12, 0xe0, 0x20, 0x5b, 0xfa, 0x94, 0x9b,
  1846. 0x1C, 0x22, 0x00, 0x00, 0xC8, 0x30, 0xbb, 0x46,
  1847. 0x73, 0x27, 0x78, 0xd9, 0x92, 0x9a, 0xab, 0x00,
  1848. 0x0e, 0xca, 0x0c, 0xf9, 0x5e, 0xe9, 0x55, 0xb2,
  1849. 0x6c, 0xd3, 0xd2, 0x88, 0xb4, 0x9f, 0x6c, 0xa9,
  1850. 0xf4, 0xb1, 0xb7, 0x59, 0x71, 0x9e, 0xb5, 0xbc
  1851. };
  1852. // clang-format on
  1853. srtp_t srtp_snd, srtp_recv;
  1854. srtp_err_status_t status;
  1855. int len;
  1856. srtp_policy_t policy;
  1857. int headers[3] = { 1, 3, 4 };
  1858. /*
  1859. * create a session with a single stream using the default srtp
  1860. * policy and with the SSRC value 0xcafebabe
  1861. */
  1862. memset(&policy, 0, sizeof(policy));
  1863. srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp);
  1864. srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp);
  1865. policy.ssrc.type = ssrc_specific;
  1866. policy.ssrc.value = 0xcafebabe;
  1867. policy.key = test_key_ext_headers;
  1868. policy.ekt = NULL;
  1869. policy.window_size = 128;
  1870. policy.allow_repeat_tx = 0;
  1871. policy.enc_xtn_hdr = headers;
  1872. policy.enc_xtn_hdr_count = sizeof(headers) / sizeof(headers[0]);
  1873. policy.next = NULL;
  1874. status = srtp_create(&srtp_snd, &policy);
  1875. if (status)
  1876. return status;
  1877. /*
  1878. * protect plaintext, then compare with ciphertext
  1879. */
  1880. len = sizeof(srtp_plaintext_ref);
  1881. status = srtp_protect(srtp_snd, srtp_plaintext, &len);
  1882. if (status || (len != sizeof(srtp_plaintext)))
  1883. return srtp_err_status_fail;
  1884. debug_print(mod_driver, "ciphertext:\n %s",
  1885. srtp_octet_string_hex_string(srtp_plaintext, len));
  1886. debug_print(mod_driver, "ciphertext reference:\n %s",
  1887. srtp_octet_string_hex_string(srtp_ciphertext, len));
  1888. if (srtp_octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len))
  1889. return srtp_err_status_fail;
  1890. /*
  1891. * create a receiver session context comparable to the one created
  1892. * above - we need to do this so that the replay checking doesn't
  1893. * complain
  1894. */
  1895. status = srtp_create(&srtp_recv, &policy);
  1896. if (status)
  1897. return status;
  1898. /*
  1899. * unprotect ciphertext, then compare with plaintext
  1900. */
  1901. status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
  1902. if (status) {
  1903. return status;
  1904. } else if (len != sizeof(srtp_plaintext_ref)) {
  1905. return srtp_err_status_fail;
  1906. }
  1907. if (srtp_octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len))
  1908. return srtp_err_status_fail;
  1909. status = srtp_dealloc(srtp_snd);
  1910. if (status)
  1911. return status;
  1912. status = srtp_dealloc(srtp_recv);
  1913. if (status)
  1914. return status;
  1915. return srtp_err_status_ok;
  1916. }
  1917. #endif
  1918. /*
  1919. * srtp_validate_aes_256() verifies the correctness of libsrtp by comparing
  1920. * some computed packets against some pre-computed reference values.
  1921. * These packets were made with the AES-CM-256/HMAC-SHA-1-80 policy.
  1922. */
  1923. srtp_err_status_t srtp_validate_aes_256()
  1924. {
  1925. // clang-format off
  1926. unsigned char aes_256_test_key[46] = {
  1927. 0xf0, 0xf0, 0x49, 0x14, 0xb5, 0x13, 0xf2, 0x76,
  1928. 0x3a, 0x1b, 0x1f, 0xa1, 0x30, 0xf1, 0x0e, 0x29,
  1929. 0x98, 0xf6, 0xf6, 0xe4, 0x3e, 0x43, 0x09, 0xd1,
  1930. 0xe6, 0x22, 0xa0, 0xe3, 0x32, 0xb9, 0xf1, 0xb6,
  1931. 0x3b, 0x04, 0x80, 0x3d, 0xe5, 0x1e, 0xe7, 0xc9,
  1932. 0x64, 0x23, 0xab, 0x5b, 0x78, 0xd2
  1933. };
  1934. uint8_t srtp_plaintext_ref[28] = {
  1935. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1936. 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
  1937. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1938. 0xab, 0xab, 0xab, 0xab
  1939. };
  1940. uint8_t srtp_plaintext[38] = {
  1941. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1942. 0xca, 0xfe, 0xba, 0xbe, 0xab, 0xab, 0xab, 0xab,
  1943. 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab, 0xab,
  1944. 0xab, 0xab, 0xab, 0xab, 0x00, 0x00, 0x00, 0x00,
  1945. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1946. };
  1947. uint8_t srtp_ciphertext[38] = {
  1948. 0x80, 0x0f, 0x12, 0x34, 0xde, 0xca, 0xfb, 0xad,
  1949. 0xca, 0xfe, 0xba, 0xbe, 0xf1, 0xd9, 0xde, 0x17,
  1950. 0xff, 0x25, 0x1f, 0xf1, 0xaa, 0x00, 0x77, 0x74,
  1951. 0xb0, 0xb4, 0xb4, 0x0d, 0xa0, 0x8d, 0x9d, 0x9a,
  1952. 0x5b, 0x3a, 0x55, 0xd8, 0x87, 0x3b
  1953. };
  1954. // clang-format on
  1955. srtp_t srtp_snd, srtp_recv;
  1956. srtp_err_status_t status;
  1957. int len;
  1958. srtp_policy_t policy;
  1959. /*
  1960. * create a session with a single stream using the default srtp
  1961. * policy and with the SSRC value 0xcafebabe
  1962. */
  1963. memset(&policy, 0, sizeof(policy));
  1964. srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtp);
  1965. srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy.rtcp);
  1966. policy.ssrc.type = ssrc_specific;
  1967. policy.ssrc.value = 0xcafebabe;
  1968. policy.key = aes_256_test_key;
  1969. policy.ekt = NULL;
  1970. policy.window_size = 128;
  1971. policy.allow_repeat_tx = 0;
  1972. policy.next = NULL;
  1973. status = srtp_create(&srtp_snd, &policy);
  1974. if (status) {
  1975. return status;
  1976. }
  1977. /*
  1978. * protect plaintext, then compare with ciphertext
  1979. */
  1980. len = 28;
  1981. status = srtp_protect(srtp_snd, srtp_plaintext, &len);
  1982. if (status || (len != 38)) {
  1983. return srtp_err_status_fail;
  1984. }
  1985. debug_print(mod_driver, "ciphertext:\n %s",
  1986. octet_string_hex_string(srtp_plaintext, len));
  1987. debug_print(mod_driver, "ciphertext reference:\n %s",
  1988. octet_string_hex_string(srtp_ciphertext, len));
  1989. if (srtp_octet_string_is_eq(srtp_plaintext, srtp_ciphertext, len)) {
  1990. return srtp_err_status_fail;
  1991. }
  1992. /*
  1993. * create a receiver session context comparable to the one created
  1994. * above - we need to do this so that the replay checking doesn't
  1995. * complain
  1996. */
  1997. status = srtp_create(&srtp_recv, &policy);
  1998. if (status) {
  1999. return status;
  2000. }
  2001. /*
  2002. * unprotect ciphertext, then compare with plaintext
  2003. */
  2004. status = srtp_unprotect(srtp_recv, srtp_ciphertext, &len);
  2005. if (status || (len != 28)) {
  2006. return status;
  2007. }
  2008. if (srtp_octet_string_is_eq(srtp_ciphertext, srtp_plaintext_ref, len)) {
  2009. return srtp_err_status_fail;
  2010. }
  2011. status = srtp_dealloc(srtp_snd);
  2012. if (status) {
  2013. return status;
  2014. }
  2015. status = srtp_dealloc(srtp_recv);
  2016. if (status) {
  2017. return status;
  2018. }
  2019. return srtp_err_status_ok;
  2020. }
  2021. srtp_err_status_t srtp_create_big_policy(srtp_policy_t **list)
  2022. {
  2023. extern const srtp_policy_t *policy_array[];
  2024. srtp_policy_t *p, *tmp;
  2025. int i = 0;
  2026. uint32_t ssrc = 0;
  2027. /* sanity checking */
  2028. if ((list == NULL) || (policy_array[0] == NULL)) {
  2029. return srtp_err_status_bad_param;
  2030. }
  2031. /*
  2032. * loop over policy list, mallocing a new list and copying values
  2033. * into it (and incrementing the SSRC value as we go along)
  2034. */
  2035. tmp = NULL;
  2036. while (policy_array[i] != NULL) {
  2037. p = (srtp_policy_t *)malloc(sizeof(srtp_policy_t));
  2038. if (p == NULL) {
  2039. return srtp_err_status_bad_param;
  2040. }
  2041. memcpy(p, policy_array[i], sizeof(srtp_policy_t));
  2042. p->ssrc.type = ssrc_specific;
  2043. p->ssrc.value = ssrc++;
  2044. p->next = tmp;
  2045. tmp = p;
  2046. i++;
  2047. }
  2048. *list = p;
  2049. return srtp_err_status_ok;
  2050. }
  2051. srtp_err_status_t srtp_dealloc_big_policy(srtp_policy_t *list)
  2052. {
  2053. srtp_policy_t *p, *next;
  2054. for (p = list; p != NULL; p = next) {
  2055. next = p->next;
  2056. free(p);
  2057. }
  2058. return srtp_err_status_ok;
  2059. }
  2060. srtp_err_status_t srtp_test_empty_payload()
  2061. {
  2062. srtp_t srtp_snd, srtp_recv;
  2063. srtp_err_status_t status;
  2064. int len;
  2065. srtp_policy_t policy;
  2066. srtp_hdr_t *mesg;
  2067. /*
  2068. * create a session with a single stream using the default srtp
  2069. * policy and with the SSRC value 0xcafebabe
  2070. */
  2071. memset(&policy, 0, sizeof(policy));
  2072. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  2073. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  2074. policy.ssrc.type = ssrc_specific;
  2075. policy.ssrc.value = 0xcafebabe;
  2076. policy.key = test_key;
  2077. policy.ekt = NULL;
  2078. policy.window_size = 128;
  2079. policy.allow_repeat_tx = 0;
  2080. policy.next = NULL;
  2081. status = srtp_create(&srtp_snd, &policy);
  2082. if (status) {
  2083. return status;
  2084. }
  2085. mesg = srtp_create_test_packet(0, policy.ssrc.value, &len);
  2086. if (mesg == NULL) {
  2087. return srtp_err_status_fail;
  2088. }
  2089. status = srtp_protect(srtp_snd, mesg, &len);
  2090. if (status) {
  2091. return status;
  2092. } else if (len != 12 + 10) {
  2093. return srtp_err_status_fail;
  2094. }
  2095. /*
  2096. * create a receiver session context comparable to the one created
  2097. * above - we need to do this so that the replay checking doesn't
  2098. * complain
  2099. */
  2100. status = srtp_create(&srtp_recv, &policy);
  2101. if (status) {
  2102. return status;
  2103. }
  2104. /*
  2105. * unprotect ciphertext, then compare with plaintext
  2106. */
  2107. status = srtp_unprotect(srtp_recv, mesg, &len);
  2108. if (status) {
  2109. return status;
  2110. } else if (len != 12) {
  2111. return srtp_err_status_fail;
  2112. }
  2113. status = srtp_dealloc(srtp_snd);
  2114. if (status) {
  2115. return status;
  2116. }
  2117. status = srtp_dealloc(srtp_recv);
  2118. if (status) {
  2119. return status;
  2120. }
  2121. free(mesg);
  2122. return srtp_err_status_ok;
  2123. }
  2124. #ifdef GCM
  2125. srtp_err_status_t srtp_test_empty_payload_gcm()
  2126. {
  2127. srtp_t srtp_snd, srtp_recv;
  2128. srtp_err_status_t status;
  2129. int len;
  2130. srtp_policy_t policy;
  2131. srtp_hdr_t *mesg;
  2132. /*
  2133. * create a session with a single stream using the default srtp
  2134. * policy and with the SSRC value 0xcafebabe
  2135. */
  2136. memset(&policy, 0, sizeof(policy));
  2137. srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtp);
  2138. srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy.rtcp);
  2139. policy.ssrc.type = ssrc_specific;
  2140. policy.ssrc.value = 0xcafebabe;
  2141. policy.key = test_key;
  2142. policy.ekt = NULL;
  2143. policy.window_size = 128;
  2144. policy.allow_repeat_tx = 0;
  2145. policy.next = NULL;
  2146. status = srtp_create(&srtp_snd, &policy);
  2147. if (status) {
  2148. return status;
  2149. }
  2150. mesg = srtp_create_test_packet(0, policy.ssrc.value, &len);
  2151. if (mesg == NULL) {
  2152. return srtp_err_status_fail;
  2153. }
  2154. status = srtp_protect(srtp_snd, mesg, &len);
  2155. if (status) {
  2156. return status;
  2157. } else if (len != 12 + 8) {
  2158. return srtp_err_status_fail;
  2159. }
  2160. /*
  2161. * create a receiver session context comparable to the one created
  2162. * above - we need to do this so that the replay checking doesn't
  2163. * complain
  2164. */
  2165. status = srtp_create(&srtp_recv, &policy);
  2166. if (status) {
  2167. return status;
  2168. }
  2169. /*
  2170. * unprotect ciphertext, then compare with plaintext
  2171. */
  2172. status = srtp_unprotect(srtp_recv, mesg, &len);
  2173. if (status) {
  2174. return status;
  2175. } else if (len != 12) {
  2176. return srtp_err_status_fail;
  2177. }
  2178. status = srtp_dealloc(srtp_snd);
  2179. if (status) {
  2180. return status;
  2181. }
  2182. status = srtp_dealloc(srtp_recv);
  2183. if (status) {
  2184. return status;
  2185. }
  2186. free(mesg);
  2187. return srtp_err_status_ok;
  2188. }
  2189. #endif // GCM
  2190. srtp_err_status_t srtp_test_remove_stream()
  2191. {
  2192. srtp_err_status_t status;
  2193. srtp_policy_t *policy_list, policy;
  2194. srtp_t session;
  2195. srtp_stream_t stream;
  2196. /*
  2197. * srtp_get_stream() is a libSRTP internal function that we declare
  2198. * here so that we can use it to verify the correct operation of the
  2199. * library
  2200. */
  2201. extern srtp_stream_t srtp_get_stream(srtp_t srtp, uint32_t ssrc);
  2202. status = srtp_create_big_policy(&policy_list);
  2203. if (status) {
  2204. return status;
  2205. }
  2206. status = srtp_create(&session, policy_list);
  2207. if (status) {
  2208. return status;
  2209. }
  2210. /*
  2211. * check for false positives by trying to remove a stream that's not
  2212. * in the session
  2213. */
  2214. status = srtp_remove_stream(session, htonl(0xaaaaaaaa));
  2215. if (status != srtp_err_status_no_ctx) {
  2216. return srtp_err_status_fail;
  2217. }
  2218. /*
  2219. * check for false negatives by removing stream 0x1, then
  2220. * searching for streams 0x0 and 0x2
  2221. */
  2222. status = srtp_remove_stream(session, htonl(0x1));
  2223. if (status != srtp_err_status_ok) {
  2224. return srtp_err_status_fail;
  2225. }
  2226. stream = srtp_get_stream(session, htonl(0x0));
  2227. if (stream == NULL) {
  2228. return srtp_err_status_fail;
  2229. }
  2230. stream = srtp_get_stream(session, htonl(0x2));
  2231. if (stream == NULL) {
  2232. return srtp_err_status_fail;
  2233. }
  2234. status = srtp_dealloc(session);
  2235. if (status != srtp_err_status_ok) {
  2236. return status;
  2237. }
  2238. status = srtp_dealloc_big_policy(policy_list);
  2239. if (status != srtp_err_status_ok) {
  2240. return status;
  2241. }
  2242. /* Now test adding and removing a single stream */
  2243. memset(&policy, 0, sizeof(policy));
  2244. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  2245. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  2246. policy.ssrc.type = ssrc_specific;
  2247. policy.ssrc.value = 0xcafebabe;
  2248. policy.key = test_key;
  2249. policy.ekt = NULL;
  2250. policy.window_size = 128;
  2251. policy.allow_repeat_tx = 0;
  2252. policy.next = NULL;
  2253. status = srtp_create(&session, NULL);
  2254. if (status != srtp_err_status_ok) {
  2255. return status;
  2256. }
  2257. status = srtp_add_stream(session, &policy);
  2258. if (status != srtp_err_status_ok) {
  2259. return status;
  2260. }
  2261. status = srtp_remove_stream(session, htonl(0xcafebabe));
  2262. if (status != srtp_err_status_ok) {
  2263. return status;
  2264. }
  2265. status = srtp_dealloc(session);
  2266. if (status != srtp_err_status_ok) {
  2267. return status;
  2268. }
  2269. return srtp_err_status_ok;
  2270. }
  2271. // clang-format off
  2272. unsigned char test_alt_key[46] = {
  2273. 0xe5, 0x19, 0x6f, 0x01, 0x5e, 0xf1, 0x9b, 0xe1,
  2274. 0xd7, 0x47, 0xa7, 0x27, 0x07, 0xd7, 0x47, 0x33,
  2275. 0x01, 0xc2, 0x35, 0x4d, 0x59, 0x6a, 0xf7, 0x84,
  2276. 0x96, 0x98, 0xeb, 0xaa, 0xac, 0xf6, 0xa1, 0x45,
  2277. 0xc7, 0x15, 0xe2, 0xea, 0xfe, 0x55, 0x67, 0x96,
  2278. 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
  2279. };
  2280. // clang-format on
  2281. /*
  2282. * srtp_test_update() verifies updating/rekeying exsisting streams.
  2283. * As stated in https://tools.ietf.org/html/rfc3711#section-3.3.1
  2284. * the value of the ROC must not be reset after a rekey, this test
  2285. * atempts to prove that srtp_update does not reset the ROC.
  2286. */
  2287. srtp_err_status_t srtp_test_update()
  2288. {
  2289. srtp_err_status_t status;
  2290. uint32_t ssrc = 0x12121212;
  2291. int msg_len_octets = 32;
  2292. int protected_msg_len_octets;
  2293. srtp_hdr_t *msg;
  2294. srtp_t srtp_snd, srtp_recv;
  2295. srtp_policy_t policy;
  2296. memset(&policy, 0, sizeof(policy));
  2297. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  2298. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  2299. policy.ekt = NULL;
  2300. policy.window_size = 128;
  2301. policy.allow_repeat_tx = 0;
  2302. policy.next = NULL;
  2303. policy.ssrc.type = ssrc_any_outbound;
  2304. policy.key = test_key;
  2305. /* create a send and recive ctx with defualt profile and test_key */
  2306. status = srtp_create(&srtp_recv, &policy);
  2307. if (status)
  2308. return status;
  2309. policy.ssrc.type = ssrc_any_inbound;
  2310. status = srtp_create(&srtp_snd, &policy);
  2311. if (status)
  2312. return status;
  2313. /* protect and unprotect two msg's that will cause the ROC to be equal to 1
  2314. */
  2315. msg = srtp_create_test_packet(msg_len_octets, ssrc,
  2316. &protected_msg_len_octets);
  2317. if (msg == NULL)
  2318. return srtp_err_status_alloc_fail;
  2319. msg->seq = htons(65535);
  2320. status = srtp_protect(srtp_snd, msg, &protected_msg_len_octets);
  2321. if (status)
  2322. return srtp_err_status_fail;
  2323. status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
  2324. if (status)
  2325. return status;
  2326. free(msg);
  2327. msg = srtp_create_test_packet(msg_len_octets, ssrc,
  2328. &protected_msg_len_octets);
  2329. if (msg == NULL)
  2330. return srtp_err_status_alloc_fail;
  2331. msg->seq = htons(1);
  2332. status = srtp_protect(srtp_snd, msg, &protected_msg_len_octets);
  2333. if (status)
  2334. return srtp_err_status_fail;
  2335. status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
  2336. if (status)
  2337. return status;
  2338. free(msg);
  2339. /* update send ctx with same test_key t verify update works*/
  2340. policy.ssrc.type = ssrc_any_outbound;
  2341. policy.key = test_key;
  2342. status = srtp_update(srtp_snd, &policy);
  2343. if (status)
  2344. return status;
  2345. msg = srtp_create_test_packet(msg_len_octets, ssrc,
  2346. &protected_msg_len_octets);
  2347. if (msg == NULL)
  2348. return srtp_err_status_alloc_fail;
  2349. msg->seq = htons(2);
  2350. status = srtp_protect(srtp_snd, msg, &protected_msg_len_octets);
  2351. if (status)
  2352. return srtp_err_status_fail;
  2353. status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
  2354. if (status)
  2355. return status;
  2356. free(msg);
  2357. /* update send ctx to use test_alt_key */
  2358. policy.ssrc.type = ssrc_any_outbound;
  2359. policy.key = test_alt_key;
  2360. status = srtp_update(srtp_snd, &policy);
  2361. if (status)
  2362. return status;
  2363. /* create and protect msg with new key and ROC still equal to 1 */
  2364. msg = srtp_create_test_packet(msg_len_octets, ssrc,
  2365. &protected_msg_len_octets);
  2366. if (msg == NULL)
  2367. return srtp_err_status_alloc_fail;
  2368. msg->seq = htons(3);
  2369. status = srtp_protect(srtp_snd, msg, &protected_msg_len_octets);
  2370. if (status)
  2371. return srtp_err_status_fail;
  2372. /* verify that recive ctx will fail to unprotect as it still uses test_key
  2373. */
  2374. status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
  2375. if (status == srtp_err_status_ok)
  2376. return srtp_err_status_fail;
  2377. /* create a new recvieve ctx with test_alt_key but since it is new it will
  2378. * have ROC equal to 1
  2379. * and therefore should fail to unprotected */
  2380. {
  2381. srtp_t srtp_recv_roc_0;
  2382. policy.ssrc.type = ssrc_any_inbound;
  2383. policy.key = test_alt_key;
  2384. status = srtp_create(&srtp_recv_roc_0, &policy);
  2385. if (status)
  2386. return status;
  2387. status =
  2388. srtp_unprotect(srtp_recv_roc_0, msg, &protected_msg_len_octets);
  2389. if (status == srtp_err_status_ok)
  2390. return srtp_err_status_fail;
  2391. status = srtp_dealloc(srtp_recv_roc_0);
  2392. if (status)
  2393. return status;
  2394. }
  2395. /* update recive ctx to use test_alt_key */
  2396. policy.ssrc.type = ssrc_any_inbound;
  2397. policy.key = test_alt_key;
  2398. status = srtp_update(srtp_recv, &policy);
  2399. if (status)
  2400. return status;
  2401. /* verify that can still unprotect, therfore key is updated and ROC value is
  2402. * preserved */
  2403. status = srtp_unprotect(srtp_recv, msg, &protected_msg_len_octets);
  2404. if (status)
  2405. return status;
  2406. free(msg);
  2407. status = srtp_dealloc(srtp_snd);
  2408. if (status)
  2409. return status;
  2410. status = srtp_dealloc(srtp_recv);
  2411. if (status)
  2412. return status;
  2413. return srtp_err_status_ok;
  2414. }
  2415. srtp_err_status_t srtp_test_setup_protect_trailer_streams(
  2416. srtp_t *srtp_send,
  2417. srtp_t *srtp_send_mki,
  2418. srtp_t *srtp_send_aes_gcm,
  2419. srtp_t *srtp_send_aes_gcm_mki)
  2420. {
  2421. srtp_err_status_t status;
  2422. srtp_policy_t policy;
  2423. srtp_policy_t policy_mki;
  2424. #ifdef GCM
  2425. srtp_policy_t policy_aes_gcm;
  2426. srtp_policy_t policy_aes_gcm_mki;
  2427. #endif // GCM
  2428. memset(&policy, 0, sizeof(policy));
  2429. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  2430. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  2431. policy.ekt = NULL;
  2432. policy.window_size = 128;
  2433. policy.allow_repeat_tx = 0;
  2434. policy.next = NULL;
  2435. policy.ssrc.type = ssrc_any_outbound;
  2436. policy.key = test_key;
  2437. memset(&policy_mki, 0, sizeof(policy_mki));
  2438. srtp_crypto_policy_set_rtp_default(&policy_mki.rtp);
  2439. srtp_crypto_policy_set_rtcp_default(&policy_mki.rtcp);
  2440. policy_mki.ekt = NULL;
  2441. policy_mki.window_size = 128;
  2442. policy_mki.allow_repeat_tx = 0;
  2443. policy_mki.next = NULL;
  2444. policy_mki.ssrc.type = ssrc_any_outbound;
  2445. policy_mki.key = NULL;
  2446. policy_mki.keys = test_keys;
  2447. policy_mki.num_master_keys = 2;
  2448. #ifdef GCM
  2449. memset(&policy_aes_gcm, 0, sizeof(policy_aes_gcm));
  2450. srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy_aes_gcm.rtp);
  2451. srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy_aes_gcm.rtcp);
  2452. policy_aes_gcm.ekt = NULL;
  2453. policy_aes_gcm.window_size = 128;
  2454. policy_aes_gcm.allow_repeat_tx = 0;
  2455. policy_aes_gcm.next = NULL;
  2456. policy_aes_gcm.ssrc.type = ssrc_any_outbound;
  2457. policy_aes_gcm.key = test_key;
  2458. memset(&policy_aes_gcm_mki, 0, sizeof(policy_aes_gcm_mki));
  2459. srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy_aes_gcm_mki.rtp);
  2460. srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy_aes_gcm_mki.rtcp);
  2461. policy_aes_gcm_mki.ekt = NULL;
  2462. policy_aes_gcm_mki.window_size = 128;
  2463. policy_aes_gcm_mki.allow_repeat_tx = 0;
  2464. policy_aes_gcm_mki.next = NULL;
  2465. policy_aes_gcm_mki.ssrc.type = ssrc_any_outbound;
  2466. policy_aes_gcm_mki.key = NULL;
  2467. policy_aes_gcm_mki.keys = test_keys;
  2468. policy_aes_gcm_mki.num_master_keys = 2;
  2469. #endif // GCM
  2470. /* create a send ctx with defualt profile and test_key */
  2471. status = srtp_create(srtp_send, &policy);
  2472. if (status)
  2473. return status;
  2474. status = srtp_create(srtp_send_mki, &policy_mki);
  2475. if (status)
  2476. return status;
  2477. #ifdef GCM
  2478. status = srtp_create(srtp_send_aes_gcm, &policy_aes_gcm);
  2479. if (status)
  2480. return status;
  2481. status = srtp_create(srtp_send_aes_gcm_mki, &policy_aes_gcm_mki);
  2482. if (status)
  2483. return status;
  2484. #endif // GCM
  2485. return srtp_err_status_ok;
  2486. }
  2487. srtp_err_status_t srtp_test_protect_trailer_length()
  2488. {
  2489. srtp_t srtp_send;
  2490. srtp_t srtp_send_mki;
  2491. srtp_t srtp_send_aes_gcm;
  2492. srtp_t srtp_send_aes_gcm_mki;
  2493. uint32_t length = 0;
  2494. srtp_err_status_t status;
  2495. srtp_test_setup_protect_trailer_streams(
  2496. &srtp_send, &srtp_send_mki, &srtp_send_aes_gcm, &srtp_send_aes_gcm_mki);
  2497. status = srtp_get_protect_trailer_length(srtp_send, 0, 0, &length);
  2498. if (status)
  2499. return status;
  2500. /* TAG Length: 10 bytes */
  2501. if (length != 10)
  2502. return srtp_err_status_fail;
  2503. status = srtp_get_protect_trailer_length(srtp_send_mki, 1, 1, &length);
  2504. if (status)
  2505. return status;
  2506. /* TAG Length: 10 bytes + MKI length: 4 bytes*/
  2507. if (length != 14)
  2508. return srtp_err_status_fail;
  2509. #ifdef GCM
  2510. status = srtp_get_protect_trailer_length(srtp_send_aes_gcm, 0, 0, &length);
  2511. if (status)
  2512. return status;
  2513. /* TAG Length: 16 bytes */
  2514. if (length != 16)
  2515. return srtp_err_status_fail;
  2516. status =
  2517. srtp_get_protect_trailer_length(srtp_send_aes_gcm_mki, 1, 1, &length);
  2518. if (status)
  2519. return status;
  2520. /* TAG Length: 16 bytes + MKI length: 4 bytes*/
  2521. if (length != 20)
  2522. return srtp_err_status_fail;
  2523. #endif // GCM
  2524. srtp_dealloc(srtp_send);
  2525. srtp_dealloc(srtp_send_mki);
  2526. #ifdef GCM
  2527. srtp_dealloc(srtp_send_aes_gcm);
  2528. srtp_dealloc(srtp_send_aes_gcm_mki);
  2529. #endif
  2530. return srtp_err_status_ok;
  2531. }
  2532. srtp_err_status_t srtp_test_protect_rtcp_trailer_length()
  2533. {
  2534. srtp_t srtp_send;
  2535. srtp_t srtp_send_mki;
  2536. srtp_t srtp_send_aes_gcm;
  2537. srtp_t srtp_send_aes_gcm_mki;
  2538. uint32_t length = 0;
  2539. srtp_err_status_t status;
  2540. srtp_test_setup_protect_trailer_streams(
  2541. &srtp_send, &srtp_send_mki, &srtp_send_aes_gcm, &srtp_send_aes_gcm_mki);
  2542. status = srtp_get_protect_rtcp_trailer_length(srtp_send, 0, 0, &length);
  2543. if (status)
  2544. return status;
  2545. /* TAG Length: 10 bytes + SRTCP Trailer 4 bytes*/
  2546. if (length != 14)
  2547. return srtp_err_status_fail;
  2548. status = srtp_get_protect_rtcp_trailer_length(srtp_send_mki, 1, 1, &length);
  2549. if (status)
  2550. return status;
  2551. /* TAG Length: 10 bytes + SRTCP Trailer 4 bytes + MKI 4 bytes*/
  2552. if (length != 18)
  2553. return srtp_err_status_fail;
  2554. #ifdef GCM
  2555. status =
  2556. srtp_get_protect_rtcp_trailer_length(srtp_send_aes_gcm, 0, 0, &length);
  2557. if (status)
  2558. return status;
  2559. /* TAG Length: 16 bytes + SRTCP Trailer 4 bytes*/
  2560. if (length != 20)
  2561. return srtp_err_status_fail;
  2562. status = srtp_get_protect_rtcp_trailer_length(srtp_send_aes_gcm_mki, 1, 1,
  2563. &length);
  2564. if (status)
  2565. return status;
  2566. /* TAG Length: 16 bytes + SRTCP Trailer 4 bytes + MKI 4 bytes*/
  2567. if (length != 24)
  2568. return srtp_err_status_fail;
  2569. #endif // GCM
  2570. srtp_dealloc(srtp_send);
  2571. srtp_dealloc(srtp_send_mki);
  2572. #ifdef GCM
  2573. srtp_dealloc(srtp_send_aes_gcm);
  2574. srtp_dealloc(srtp_send_aes_gcm_mki);
  2575. #endif
  2576. return srtp_err_status_ok;
  2577. }
  2578. srtp_err_status_t srtp_test_get_roc()
  2579. {
  2580. srtp_err_status_t status;
  2581. srtp_policy_t policy;
  2582. srtp_t session;
  2583. srtp_hdr_t *pkt;
  2584. uint32_t i;
  2585. uint32_t roc;
  2586. uint32_t ts;
  2587. uint16_t seq;
  2588. int msg_len_octets = 32;
  2589. int protected_msg_len_octets;
  2590. memset(&policy, 0, sizeof(policy));
  2591. srtp_crypto_policy_set_rtp_default(&policy.rtp);
  2592. srtp_crypto_policy_set_rtcp_default(&policy.rtcp);
  2593. policy.ssrc.type = ssrc_specific;
  2594. policy.ssrc.value = 0xcafebabe;
  2595. policy.key = test_key;
  2596. policy.window_size = 128;
  2597. /* Create a sender session */
  2598. status = srtp_create(&session, &policy);
  2599. if (status) {
  2600. return status;
  2601. }
  2602. /* Set start sequence so we roll over */
  2603. seq = 65535;
  2604. ts = 0;
  2605. for (i = 0; i < 2; i++) {
  2606. pkt = srtp_create_test_packet_extended(msg_len_octets,
  2607. policy.ssrc.value, seq, ts,
  2608. &protected_msg_len_octets);
  2609. status = srtp_protect(session, pkt, &protected_msg_len_octets);
  2610. free(pkt);
  2611. if (status) {
  2612. return status;
  2613. }
  2614. status = srtp_get_stream_roc(session, policy.ssrc.value, &roc);
  2615. if (status) {
  2616. return status;
  2617. }
  2618. if (roc != i) {
  2619. return srtp_err_status_fail;
  2620. }
  2621. seq++;
  2622. ts++;
  2623. }
  2624. /* Cleanup */
  2625. status = srtp_dealloc(session);
  2626. if (status) {
  2627. return status;
  2628. }
  2629. return srtp_err_status_ok;
  2630. }
  2631. static srtp_err_status_t test_set_receiver_roc(uint32_t packets,
  2632. uint32_t roc_to_set)
  2633. {
  2634. srtp_err_status_t status;
  2635. srtp_policy_t sender_policy;
  2636. srtp_t sender_session;
  2637. srtp_policy_t receiver_policy;
  2638. srtp_t receiver_session;
  2639. srtp_hdr_t *pkt_1;
  2640. unsigned char *recv_pkt_1;
  2641. srtp_hdr_t *pkt_2;
  2642. unsigned char *recv_pkt_2;
  2643. uint32_t i;
  2644. uint32_t ts;
  2645. uint16_t seq;
  2646. int msg_len_octets = 32;
  2647. int protected_msg_len_octets_1;
  2648. int protected_msg_len_octets_2;
  2649. /* Create sender */
  2650. memset(&sender_policy, 0, sizeof(sender_policy));
  2651. srtp_crypto_policy_set_rtp_default(&sender_policy.rtp);
  2652. srtp_crypto_policy_set_rtcp_default(&sender_policy.rtcp);
  2653. sender_policy.ssrc.type = ssrc_specific;
  2654. sender_policy.ssrc.value = 0xcafebabe;
  2655. sender_policy.key = test_key;
  2656. sender_policy.window_size = 128;
  2657. status = srtp_create(&sender_session, &sender_policy);
  2658. if (status) {
  2659. return status;
  2660. }
  2661. /* Create and protect packets */
  2662. seq = 0;
  2663. ts = 0;
  2664. for (i = 0; i < packets; i++) {
  2665. srtp_hdr_t *tmp_pkt;
  2666. int tmp_len;
  2667. tmp_pkt = srtp_create_test_packet_extended(
  2668. msg_len_octets, sender_policy.ssrc.value, seq, ts, &tmp_len);
  2669. status = srtp_protect(sender_session, tmp_pkt, &tmp_len);
  2670. free(tmp_pkt);
  2671. if (status) {
  2672. return status;
  2673. }
  2674. seq++;
  2675. ts++;
  2676. }
  2677. /* Create the first packet to decrypt and test for ROC change */
  2678. pkt_1 = srtp_create_test_packet_extended(msg_len_octets,
  2679. sender_policy.ssrc.value, seq, ts,
  2680. &protected_msg_len_octets_1);
  2681. status = srtp_protect(sender_session, pkt_1, &protected_msg_len_octets_1);
  2682. if (status) {
  2683. return status;
  2684. }
  2685. /* Create the second packet to decrypt and test for ROC change */
  2686. seq++;
  2687. ts++;
  2688. pkt_2 = srtp_create_test_packet_extended(msg_len_octets,
  2689. sender_policy.ssrc.value, seq, ts,
  2690. &protected_msg_len_octets_2);
  2691. status = srtp_protect(sender_session, pkt_2, &protected_msg_len_octets_2);
  2692. if (status) {
  2693. return status;
  2694. }
  2695. /* Create the receiver */
  2696. memset(&receiver_policy, 0, sizeof(receiver_policy));
  2697. srtp_crypto_policy_set_rtp_default(&receiver_policy.rtp);
  2698. srtp_crypto_policy_set_rtcp_default(&receiver_policy.rtcp);
  2699. receiver_policy.ssrc.type = ssrc_specific;
  2700. receiver_policy.ssrc.value = sender_policy.ssrc.value;
  2701. receiver_policy.key = test_key;
  2702. receiver_policy.window_size = 128;
  2703. status = srtp_create(&receiver_session, &receiver_policy);
  2704. if (status) {
  2705. return status;
  2706. }
  2707. /* Make a copy of the first sent protected packet */
  2708. recv_pkt_1 = malloc(protected_msg_len_octets_1);
  2709. if (recv_pkt_1 == NULL) {
  2710. return srtp_err_status_fail;
  2711. }
  2712. memcpy(recv_pkt_1, pkt_1, protected_msg_len_octets_1);
  2713. /* Make a copy of the second sent protected packet */
  2714. recv_pkt_2 = malloc(protected_msg_len_octets_2);
  2715. if (recv_pkt_2 == NULL) {
  2716. return srtp_err_status_fail;
  2717. }
  2718. memcpy(recv_pkt_2, pkt_2, protected_msg_len_octets_2);
  2719. /* Set the ROC to the wanted value */
  2720. status = srtp_set_stream_roc(receiver_session, receiver_policy.ssrc.value,
  2721. roc_to_set);
  2722. if (status) {
  2723. return status;
  2724. }
  2725. /* Unprotect the first packet */
  2726. status = srtp_unprotect(receiver_session, recv_pkt_1,
  2727. &protected_msg_len_octets_1);
  2728. if (status) {
  2729. return status;
  2730. }
  2731. /* Unprotect the second packet */
  2732. status = srtp_unprotect(receiver_session, recv_pkt_2,
  2733. &protected_msg_len_octets_2);
  2734. if (status) {
  2735. return status;
  2736. }
  2737. /* Cleanup */
  2738. status = srtp_dealloc(sender_session);
  2739. if (status) {
  2740. return status;
  2741. }
  2742. status = srtp_dealloc(receiver_session);
  2743. if (status) {
  2744. return status;
  2745. }
  2746. free(pkt_1);
  2747. free(recv_pkt_1);
  2748. free(pkt_2);
  2749. free(recv_pkt_2);
  2750. return srtp_err_status_ok;
  2751. }
  2752. static srtp_err_status_t test_set_sender_roc(uint16_t seq, uint32_t roc_to_set)
  2753. {
  2754. srtp_err_status_t status;
  2755. srtp_policy_t sender_policy;
  2756. srtp_t sender_session;
  2757. srtp_policy_t receiver_policy;
  2758. srtp_t receiver_session;
  2759. srtp_hdr_t *pkt;
  2760. unsigned char *recv_pkt;
  2761. uint32_t ts;
  2762. int msg_len_octets = 32;
  2763. int protected_msg_len_octets;
  2764. /* Create sender */
  2765. memset(&sender_policy, 0, sizeof(sender_policy));
  2766. srtp_crypto_policy_set_rtp_default(&sender_policy.rtp);
  2767. srtp_crypto_policy_set_rtcp_default(&sender_policy.rtcp);
  2768. sender_policy.ssrc.type = ssrc_specific;
  2769. sender_policy.ssrc.value = 0xcafebabe;
  2770. sender_policy.key = test_key;
  2771. sender_policy.window_size = 128;
  2772. status = srtp_create(&sender_session, &sender_policy);
  2773. if (status) {
  2774. return status;
  2775. }
  2776. /* Set the ROC before encrypting the first packet */
  2777. status = srtp_set_stream_roc(sender_session, sender_policy.ssrc.value,
  2778. roc_to_set);
  2779. if (status != srtp_err_status_ok) {
  2780. return status;
  2781. }
  2782. /* Create the packet to decrypt */
  2783. ts = 0;
  2784. pkt = srtp_create_test_packet_extended(msg_len_octets,
  2785. sender_policy.ssrc.value, seq, ts,
  2786. &protected_msg_len_octets);
  2787. status = srtp_protect(sender_session, pkt, &protected_msg_len_octets);
  2788. if (status) {
  2789. return status;
  2790. }
  2791. /* Create the receiver */
  2792. memset(&receiver_policy, 0, sizeof(receiver_policy));
  2793. srtp_crypto_policy_set_rtp_default(&receiver_policy.rtp);
  2794. srtp_crypto_policy_set_rtcp_default(&receiver_policy.rtcp);
  2795. receiver_policy.ssrc.type = ssrc_specific;
  2796. receiver_policy.ssrc.value = sender_policy.ssrc.value;
  2797. receiver_policy.key = test_key;
  2798. receiver_policy.window_size = 128;
  2799. status = srtp_create(&receiver_session, &receiver_policy);
  2800. if (status) {
  2801. return status;
  2802. }
  2803. /* Make a copy of the sent protected packet */
  2804. recv_pkt = malloc(protected_msg_len_octets);
  2805. if (recv_pkt == NULL) {
  2806. return srtp_err_status_fail;
  2807. }
  2808. memcpy(recv_pkt, pkt, protected_msg_len_octets);
  2809. /* Set the ROC to the wanted value */
  2810. status = srtp_set_stream_roc(receiver_session, receiver_policy.ssrc.value,
  2811. roc_to_set);
  2812. if (status) {
  2813. return status;
  2814. }
  2815. status =
  2816. srtp_unprotect(receiver_session, recv_pkt, &protected_msg_len_octets);
  2817. if (status) {
  2818. return status;
  2819. }
  2820. /* Cleanup */
  2821. status = srtp_dealloc(sender_session);
  2822. if (status) {
  2823. return status;
  2824. }
  2825. status = srtp_dealloc(receiver_session);
  2826. if (status) {
  2827. return status;
  2828. }
  2829. free(pkt);
  2830. free(recv_pkt);
  2831. return srtp_err_status_ok;
  2832. }
  2833. srtp_err_status_t srtp_test_set_receiver_roc()
  2834. {
  2835. int packets;
  2836. uint32_t roc;
  2837. srtp_err_status_t status;
  2838. /* First test does not rollover */
  2839. packets = 1;
  2840. roc = 0;
  2841. status = test_set_receiver_roc(packets - 1, roc);
  2842. if (status) {
  2843. return status;
  2844. }
  2845. status = test_set_receiver_roc(packets, roc);
  2846. if (status) {
  2847. return status;
  2848. }
  2849. status = test_set_receiver_roc(packets + 1, roc);
  2850. if (status) {
  2851. return status;
  2852. }
  2853. status = test_set_receiver_roc(packets + 60000, roc);
  2854. if (status) {
  2855. return status;
  2856. }
  2857. /* Second test should rollover */
  2858. packets = 65535;
  2859. roc = 0;
  2860. status = test_set_receiver_roc(packets - 1, roc);
  2861. if (status) {
  2862. return status;
  2863. }
  2864. status = test_set_receiver_roc(packets, roc);
  2865. if (status) {
  2866. return status;
  2867. }
  2868. /* Now the rollover counter should be 1 */
  2869. roc = 1;
  2870. status = test_set_receiver_roc(packets + 1, roc);
  2871. if (status) {
  2872. return status;
  2873. }
  2874. status = test_set_receiver_roc(packets + 60000, roc);
  2875. if (status) {
  2876. return status;
  2877. }
  2878. return srtp_err_status_ok;
  2879. }
  2880. srtp_err_status_t srtp_test_set_sender_roc()
  2881. {
  2882. uint32_t roc;
  2883. uint16_t seq;
  2884. srtp_err_status_t status;
  2885. seq = 43210;
  2886. roc = 0;
  2887. status = test_set_sender_roc(seq, roc);
  2888. if (status) {
  2889. return status;
  2890. }
  2891. roc = 65535;
  2892. status = test_set_sender_roc(seq, roc);
  2893. if (status) {
  2894. return status;
  2895. }
  2896. roc = 0xffff;
  2897. status = test_set_sender_roc(seq, roc);
  2898. if (status) {
  2899. return status;
  2900. }
  2901. roc = 0xffff00;
  2902. status = test_set_sender_roc(seq, roc);
  2903. if (status) {
  2904. return status;
  2905. }
  2906. roc = 0xfffffff0;
  2907. status = test_set_sender_roc(seq, roc);
  2908. if (status) {
  2909. return status;
  2910. }
  2911. return srtp_err_status_ok;
  2912. }
  2913. /*
  2914. * srtp policy definitions - these definitions are used above
  2915. */
  2916. // clang-format off
  2917. unsigned char test_key[46] = {
  2918. 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
  2919. 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
  2920. 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
  2921. 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6, 0xc1, 0x73,
  2922. 0xc3, 0x17, 0xf2, 0xda, 0xbe, 0x35, 0x77, 0x93,
  2923. 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
  2924. };
  2925. unsigned char test_key_2[46] = {
  2926. 0xf0, 0xf0, 0x49, 0x14, 0xb5, 0x13, 0xf2, 0x76,
  2927. 0x3a, 0x1b, 0x1f, 0xa1, 0x30, 0xf1, 0x0e, 0x29,
  2928. 0x98, 0xf6, 0xf6, 0xe4, 0x3e, 0x43, 0x09, 0xd1,
  2929. 0xe6, 0x22, 0xa0, 0xe3, 0x32, 0xb9, 0xf1, 0xb6,
  2930. 0xc3, 0x17, 0xf2, 0xda, 0xbe, 0x35, 0x77, 0x93,
  2931. 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6
  2932. };
  2933. unsigned char test_mki_id[TEST_MKI_ID_SIZE] = {
  2934. 0xe1, 0xf9, 0x7a, 0x0d
  2935. };
  2936. unsigned char test_mki_id_2[TEST_MKI_ID_SIZE] = {
  2937. 0xf3, 0xa1, 0x46, 0x71
  2938. };
  2939. // clang-format on
  2940. const srtp_policy_t default_policy = {
  2941. { ssrc_any_outbound, 0 }, /* SSRC */
  2942. {
  2943. /* SRTP policy */
  2944. SRTP_AES_ICM_128, /* cipher type */
  2945. SRTP_AES_ICM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  2946. SRTP_HMAC_SHA1, /* authentication func type */
  2947. 16, /* auth key length in octets */
  2948. 10, /* auth tag length in octets */
  2949. sec_serv_conf_and_auth /* security services flag */
  2950. },
  2951. {
  2952. /* SRTCP policy */
  2953. SRTP_AES_ICM_128, /* cipher type */
  2954. SRTP_AES_ICM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  2955. SRTP_HMAC_SHA1, /* authentication func type */
  2956. 16, /* auth key length in octets */
  2957. 10, /* auth tag length in octets */
  2958. sec_serv_conf_and_auth /* security services flag */
  2959. },
  2960. NULL,
  2961. (srtp_master_key_t **)test_keys,
  2962. 2, /* indicates the number of Master keys */
  2963. NULL, /* indicates that EKT is not in use */
  2964. 128, /* replay window size */
  2965. 0, /* retransmission not allowed */
  2966. NULL, /* no encrypted extension headers */
  2967. 0, /* list of encrypted extension headers is empty */
  2968. NULL
  2969. };
  2970. const srtp_policy_t aes_only_policy = {
  2971. { ssrc_any_outbound, 0 }, /* SSRC */
  2972. {
  2973. SRTP_AES_ICM_128, /* cipher type */
  2974. SRTP_AES_ICM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  2975. SRTP_NULL_AUTH, /* authentication func type */
  2976. 0, /* auth key length in octets */
  2977. 0, /* auth tag length in octets */
  2978. sec_serv_conf /* security services flag */
  2979. },
  2980. {
  2981. SRTP_AES_ICM_128, /* cipher type */
  2982. SRTP_AES_ICM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  2983. SRTP_NULL_AUTH, /* authentication func type */
  2984. 0, /* auth key length in octets */
  2985. 0, /* auth tag length in octets */
  2986. sec_serv_conf /* security services flag */
  2987. },
  2988. NULL,
  2989. (srtp_master_key_t **)test_keys,
  2990. 2, /* indicates the number of Master keys */
  2991. NULL, /* indicates that EKT is not in use */
  2992. 128, /* replay window size */
  2993. 0, /* retransmission not allowed */
  2994. NULL, /* no encrypted extension headers */
  2995. 0, /* list of encrypted extension headers is empty */
  2996. NULL
  2997. };
  2998. const srtp_policy_t hmac_only_policy = {
  2999. { ssrc_any_outbound, 0 }, /* SSRC */
  3000. {
  3001. SRTP_NULL_CIPHER, /* cipher type */
  3002. 0, /* cipher key length in octets */
  3003. SRTP_HMAC_SHA1, /* authentication func type */
  3004. 20, /* auth key length in octets */
  3005. 4, /* auth tag length in octets */
  3006. sec_serv_auth /* security services flag */
  3007. },
  3008. {
  3009. SRTP_NULL_CIPHER, /* cipher type */
  3010. 0, /* cipher key length in octets */
  3011. SRTP_HMAC_SHA1, /* authentication func type */
  3012. 20, /* auth key length in octets */
  3013. 4, /* auth tag length in octets */
  3014. sec_serv_auth /* security services flag */
  3015. },
  3016. NULL,
  3017. (srtp_master_key_t **)test_keys,
  3018. 2, /* Number of Master keys associated with the policy */
  3019. NULL, /* indicates that EKT is not in use */
  3020. 128, /* replay window size */
  3021. 0, /* retransmission not allowed */
  3022. NULL, /* no encrypted extension headers */
  3023. 0, /* list of encrypted extension headers is empty */
  3024. NULL
  3025. };
  3026. #ifdef GCM
  3027. const srtp_policy_t aes128_gcm_8_policy = {
  3028. { ssrc_any_outbound, 0 }, /* SSRC */
  3029. {
  3030. /* SRTP policy */
  3031. SRTP_AES_GCM_128, /* cipher type */
  3032. SRTP_AES_GCM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  3033. SRTP_NULL_AUTH, /* authentication func type */
  3034. 0, /* auth key length in octets */
  3035. 8, /* auth tag length in octets */
  3036. sec_serv_conf_and_auth /* security services flag */
  3037. },
  3038. {
  3039. /* SRTCP policy */
  3040. SRTP_AES_GCM_128, /* cipher type */
  3041. SRTP_AES_GCM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  3042. SRTP_NULL_AUTH, /* authentication func type */
  3043. 0, /* auth key length in octets */
  3044. 8, /* auth tag length in octets */
  3045. sec_serv_conf_and_auth /* security services flag */
  3046. },
  3047. NULL,
  3048. (srtp_master_key_t **)test_keys,
  3049. 2, /* indicates the number of Master keys */
  3050. NULL, /* indicates that EKT is not in use */
  3051. 128, /* replay window size */
  3052. 0, /* retransmission not allowed */
  3053. NULL, /* no encrypted extension headers */
  3054. 0, /* list of encrypted extension headers is empty */
  3055. NULL
  3056. };
  3057. const srtp_policy_t aes128_gcm_8_cauth_policy = {
  3058. { ssrc_any_outbound, 0 }, /* SSRC */
  3059. {
  3060. /* SRTP policy */
  3061. SRTP_AES_GCM_128, /* cipher type */
  3062. SRTP_AES_GCM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  3063. SRTP_NULL_AUTH, /* authentication func type */
  3064. 0, /* auth key length in octets */
  3065. 8, /* auth tag length in octets */
  3066. sec_serv_conf_and_auth /* security services flag */
  3067. },
  3068. {
  3069. /* SRTCP policy */
  3070. SRTP_AES_GCM_128, /* cipher type */
  3071. SRTP_AES_GCM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  3072. SRTP_NULL_AUTH, /* authentication func type */
  3073. 0, /* auth key length in octets */
  3074. 8, /* auth tag length in octets */
  3075. sec_serv_auth /* security services flag */
  3076. },
  3077. NULL,
  3078. (srtp_master_key_t **)test_keys,
  3079. 2, /* indicates the number of Master keys */
  3080. NULL, /* indicates that EKT is not in use */
  3081. 128, /* replay window size */
  3082. 0, /* retransmission not allowed */
  3083. NULL, /* no encrypted extension headers */
  3084. 0, /* list of encrypted extension headers is empty */
  3085. NULL
  3086. };
  3087. const srtp_policy_t aes256_gcm_8_policy = {
  3088. { ssrc_any_outbound, 0 }, /* SSRC */
  3089. {
  3090. /* SRTP policy */
  3091. SRTP_AES_GCM_256, /* cipher type */
  3092. SRTP_AES_GCM_256_KEY_LEN_WSALT, /* cipher key length in octets */
  3093. SRTP_NULL_AUTH, /* authentication func type */
  3094. 0, /* auth key length in octets */
  3095. 8, /* auth tag length in octets */
  3096. sec_serv_conf_and_auth /* security services flag */
  3097. },
  3098. {
  3099. /* SRTCP policy */
  3100. SRTP_AES_GCM_256, /* cipher type */
  3101. SRTP_AES_GCM_256_KEY_LEN_WSALT, /* cipher key length in octets */
  3102. SRTP_NULL_AUTH, /* authentication func type */
  3103. 0, /* auth key length in octets */
  3104. 8, /* auth tag length in octets */
  3105. sec_serv_conf_and_auth /* security services flag */
  3106. },
  3107. NULL,
  3108. (srtp_master_key_t **)test_keys,
  3109. 2, /* indicates the number of Master keys */
  3110. NULL, /* indicates that EKT is not in use */
  3111. 128, /* replay window size */
  3112. 0, /* retransmission not allowed */
  3113. NULL, /* no encrypted extension headers */
  3114. 0, /* list of encrypted extension headers is empty */
  3115. NULL
  3116. };
  3117. const srtp_policy_t aes256_gcm_8_cauth_policy = {
  3118. { ssrc_any_outbound, 0 }, /* SSRC */
  3119. {
  3120. /* SRTP policy */
  3121. SRTP_AES_GCM_256, /* cipher type */
  3122. SRTP_AES_GCM_256_KEY_LEN_WSALT, /* cipher key length in octets */
  3123. SRTP_NULL_AUTH, /* authentication func type */
  3124. 0, /* auth key length in octets */
  3125. 8, /* auth tag length in octets */
  3126. sec_serv_conf_and_auth /* security services flag */
  3127. },
  3128. {
  3129. /* SRTCP policy */
  3130. SRTP_AES_GCM_256, /* cipher type */
  3131. SRTP_AES_GCM_256_KEY_LEN_WSALT, /* cipher key length in octets */
  3132. SRTP_NULL_AUTH, /* authentication func type */
  3133. 0, /* auth key length in octets */
  3134. 8, /* auth tag length in octets */
  3135. sec_serv_auth /* security services flag */
  3136. },
  3137. NULL,
  3138. (srtp_master_key_t **)test_keys,
  3139. 2, /* indicates the number of Master keys */
  3140. NULL, /* indicates that EKT is not in use */
  3141. 128, /* replay window size */
  3142. 0, /* retransmission not allowed */
  3143. NULL, /* no encrypted extension headers */
  3144. 0, /* list of encrypted extension headers is empty */
  3145. NULL
  3146. };
  3147. #endif
  3148. const srtp_policy_t null_policy = {
  3149. { ssrc_any_outbound, 0 }, /* SSRC */
  3150. {
  3151. SRTP_NULL_CIPHER, /* cipher type */
  3152. 0, /* cipher key length in octets */
  3153. SRTP_NULL_AUTH, /* authentication func type */
  3154. 0, /* auth key length in octets */
  3155. 0, /* auth tag length in octets */
  3156. sec_serv_none /* security services flag */
  3157. },
  3158. {
  3159. SRTP_NULL_CIPHER, /* cipher type */
  3160. 0, /* cipher key length in octets */
  3161. SRTP_NULL_AUTH, /* authentication func type */
  3162. 0, /* auth key length in octets */
  3163. 0, /* auth tag length in octets */
  3164. sec_serv_none /* security services flag */
  3165. },
  3166. NULL,
  3167. (srtp_master_key_t **)test_keys,
  3168. 2, /* indicates the number of Master keys */
  3169. NULL, /* indicates that EKT is not in use */
  3170. 128, /* replay window size */
  3171. 0, /* retransmission not allowed */
  3172. NULL, /* no encrypted extension headers */
  3173. 0, /* list of encrypted extension headers is empty */
  3174. NULL
  3175. };
  3176. // clang-format off
  3177. unsigned char test_256_key[46] = {
  3178. 0xf0, 0xf0, 0x49, 0x14, 0xb5, 0x13, 0xf2, 0x76,
  3179. 0x3a, 0x1b, 0x1f, 0xa1, 0x30, 0xf1, 0x0e, 0x29,
  3180. 0x98, 0xf6, 0xf6, 0xe4, 0x3e, 0x43, 0x09, 0xd1,
  3181. 0xe6, 0x22, 0xa0, 0xe3, 0x32, 0xb9, 0xf1, 0xb6,
  3182. 0x3b, 0x04, 0x80, 0x3d, 0xe5, 0x1e, 0xe7, 0xc9,
  3183. 0x64, 0x23, 0xab, 0x5b, 0x78, 0xd2
  3184. };
  3185. unsigned char test_256_key_2[46] = {
  3186. 0xe1, 0xf9, 0x7a, 0x0d, 0x3e, 0x01, 0x8b, 0xe0,
  3187. 0xd6, 0x4f, 0xa3, 0x2c, 0x06, 0xde, 0x41, 0x39,
  3188. 0x0e, 0xc6, 0x75, 0xad, 0x49, 0x8a, 0xfe, 0xeb,
  3189. 0xb6, 0x96, 0x0b, 0x3a, 0xab, 0xe6, 0xc1, 0x73,
  3190. 0x3b, 0x04, 0x80, 0x3d, 0xe5, 0x1e, 0xe7, 0xc9,
  3191. 0x64, 0x23, 0xab, 0x5b, 0x78, 0xd2
  3192. };
  3193. srtp_master_key_t master_256_key_1 = {
  3194. test_256_key,
  3195. test_mki_id,
  3196. TEST_MKI_ID_SIZE
  3197. };
  3198. srtp_master_key_t master_256_key_2 = {
  3199. test_256_key_2,
  3200. test_mki_id_2,
  3201. TEST_MKI_ID_SIZE
  3202. };
  3203. srtp_master_key_t *test_256_keys[2] = {
  3204. &master_key_1,
  3205. &master_key_2
  3206. };
  3207. // clang-format on
  3208. const srtp_policy_t aes_256_hmac_policy = {
  3209. { ssrc_any_outbound, 0 }, /* SSRC */
  3210. {
  3211. /* SRTP policy */
  3212. SRTP_AES_ICM_256, /* cipher type */
  3213. SRTP_AES_ICM_256_KEY_LEN_WSALT, /* cipher key length in octets */
  3214. SRTP_HMAC_SHA1, /* authentication func type */
  3215. 20, /* auth key length in octets */
  3216. 10, /* auth tag length in octets */
  3217. sec_serv_conf_and_auth /* security services flag */
  3218. },
  3219. {
  3220. /* SRTCP policy */
  3221. SRTP_AES_ICM_256, /* cipher type */
  3222. SRTP_AES_ICM_256_KEY_LEN_WSALT, /* cipher key length in octets */
  3223. SRTP_HMAC_SHA1, /* authentication func type */
  3224. 20, /* auth key length in octets */
  3225. 10, /* auth tag length in octets */
  3226. sec_serv_conf_and_auth /* security services flag */
  3227. },
  3228. NULL,
  3229. (srtp_master_key_t **)test_256_keys,
  3230. 2, /* indicates the number of Master keys */
  3231. NULL, /* indicates that EKT is not in use */
  3232. 128, /* replay window size */
  3233. 0, /* retransmission not allowed */
  3234. NULL, /* no encrypted extension headers */
  3235. 0, /* list of encrypted extension headers is empty */
  3236. NULL
  3237. };
  3238. // clang-format off
  3239. uint8_t ekt_test_key[16] = {
  3240. 0x77, 0x26, 0x9d, 0xac, 0x16, 0xa3, 0x28, 0xca,
  3241. 0x8e, 0xc9, 0x68, 0x4b, 0xcc, 0xc4, 0xd2, 0x1b
  3242. };
  3243. // clang-format on
  3244. #include "ekt.h"
  3245. // clang-format off
  3246. srtp_ekt_policy_ctx_t ekt_test_policy = {
  3247. 0xa5a5, /* SPI */
  3248. SRTP_EKT_CIPHER_AES_128_ECB,
  3249. ekt_test_key,
  3250. NULL
  3251. };
  3252. // clang-format on
  3253. const srtp_policy_t hmac_only_with_ekt_policy = {
  3254. { ssrc_any_outbound, 0 }, /* SSRC */
  3255. {
  3256. SRTP_NULL_CIPHER, /* cipher type */
  3257. 0, /* cipher key length in octets */
  3258. SRTP_HMAC_SHA1, /* authentication func type */
  3259. 20, /* auth key length in octets */
  3260. 4, /* auth tag length in octets */
  3261. sec_serv_auth /* security services flag */
  3262. },
  3263. {
  3264. SRTP_NULL_CIPHER, /* cipher type */
  3265. 0, /* cipher key length in octets */
  3266. SRTP_HMAC_SHA1, /* authentication func type */
  3267. 20, /* auth key length in octets */
  3268. 4, /* auth tag length in octets */
  3269. sec_serv_auth /* security services flag */
  3270. },
  3271. NULL,
  3272. (srtp_master_key_t **)test_keys,
  3273. 2, /* indicates the number of Master keys */
  3274. &ekt_test_policy, /* indicates that EKT is not in use */
  3275. 128, /* replay window size */
  3276. 0, /* retransmission not allowed */
  3277. NULL, /* no encrypted extension headers */
  3278. 0, /* list of encrypted extension headers is empty */
  3279. NULL
  3280. };
  3281. /*
  3282. * an array of pointers to the policies listed above
  3283. *
  3284. * This array is used to test various aspects of libSRTP for
  3285. * different cryptographic policies. The order of the elements
  3286. * matters - the timing test generates output that can be used
  3287. * in a plot (see the gnuplot script file 'timing'). If you
  3288. * add to this list, you should do it at the end.
  3289. */
  3290. // clang-format off
  3291. const srtp_policy_t *policy_array[] = {
  3292. &hmac_only_policy,
  3293. &aes_only_policy,
  3294. &default_policy,
  3295. #ifdef GCM
  3296. &aes128_gcm_8_policy,
  3297. &aes128_gcm_8_cauth_policy,
  3298. &aes256_gcm_8_policy,
  3299. &aes256_gcm_8_cauth_policy,
  3300. #endif
  3301. &null_policy,
  3302. &aes_256_hmac_policy,
  3303. &hmac_only_with_ekt_policy,
  3304. NULL
  3305. };
  3306. // clang-format on
  3307. const srtp_policy_t wildcard_policy = {
  3308. { ssrc_any_outbound, 0 }, /* SSRC */
  3309. {
  3310. /* SRTP policy */
  3311. SRTP_AES_ICM_128, /* cipher type */
  3312. SRTP_AES_ICM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  3313. SRTP_HMAC_SHA1, /* authentication func type */
  3314. 16, /* auth key length in octets */
  3315. 10, /* auth tag length in octets */
  3316. sec_serv_conf_and_auth /* security services flag */
  3317. },
  3318. {
  3319. /* SRTCP policy */
  3320. SRTP_AES_ICM_128, /* cipher type */
  3321. SRTP_AES_ICM_128_KEY_LEN_WSALT, /* cipher key length in octets */
  3322. SRTP_HMAC_SHA1, /* authentication func type */
  3323. 16, /* auth key length in octets */
  3324. 10, /* auth tag length in octets */
  3325. sec_serv_conf_and_auth /* security services flag */
  3326. },
  3327. test_key,
  3328. NULL,
  3329. 0,
  3330. NULL,
  3331. 128, /* replay window size */
  3332. 0, /* retransmission not allowed */
  3333. NULL, /* no encrypted extension headers */
  3334. 0, /* list of encrypted extension headers is empty */
  3335. NULL
  3336. };