srtp_driver.c 123 KB

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