evp_extra_test.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829
  1. /*
  2. * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* We need to use some deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. #include <openssl/pem.h>
  21. #include <openssl/kdf.h>
  22. #include <openssl/provider.h>
  23. #include <openssl/core_names.h>
  24. #include <openssl/params.h>
  25. #include <openssl/param_build.h>
  26. #include <openssl/dsa.h>
  27. #include <openssl/dh.h>
  28. #include <openssl/aes.h>
  29. #include <openssl/decoder.h>
  30. #include <openssl/rsa.h>
  31. #include "testutil.h"
  32. #include "internal/nelem.h"
  33. #include "internal/sizes.h"
  34. #include "crypto/evp.h"
  35. #include "../e_os.h" /* strcasecmp */
  36. static OSSL_LIB_CTX *testctx = NULL;
  37. static char *testpropq = NULL;
  38. static OSSL_PROVIDER *nullprov = NULL;
  39. static OSSL_PROVIDER *deflprov = NULL;
  40. static OSSL_PROVIDER *lgcyprov = NULL;
  41. /*
  42. * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
  43. * should never use this key anywhere but in an example.
  44. */
  45. static const unsigned char kExampleRSAKeyDER[] = {
  46. 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
  47. 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
  48. 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
  49. 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
  50. 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
  51. 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
  52. 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
  53. 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
  54. 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
  55. 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
  56. 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
  57. 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  58. 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
  59. 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
  60. 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
  61. 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
  62. 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
  63. 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
  64. 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
  65. 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
  66. 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
  67. 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
  68. 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
  69. 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
  70. 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
  71. 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
  72. 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
  73. 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
  74. 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
  75. 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
  76. 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
  77. 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
  78. 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
  79. 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
  80. 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
  81. 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
  82. 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
  83. 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
  84. 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
  85. 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
  86. 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
  87. 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
  88. 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
  89. 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
  90. 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
  91. 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
  92. 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
  93. 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
  94. 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
  95. 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
  96. 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  97. };
  98. /*
  99. * kExampleDSAKeyDER is a DSA private key in ASN.1, DER format. Of course, you
  100. * should never use this key anywhere but in an example.
  101. */
  102. #ifndef OPENSSL_NO_DSA
  103. static const unsigned char kExampleDSAKeyDER[] = {
  104. 0x30, 0x82, 0x01, 0xba, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0x9a,
  105. 0x05, 0x6d, 0x33, 0xcd, 0x5d, 0x78, 0xa1, 0xbb, 0xcb, 0x7d, 0x5b, 0x8d,
  106. 0xb4, 0xcc, 0xbf, 0x03, 0x99, 0x64, 0xde, 0x38, 0x78, 0x06, 0x15, 0x2f,
  107. 0x86, 0x26, 0x77, 0xf3, 0xb1, 0x85, 0x00, 0xed, 0xfc, 0x28, 0x3a, 0x42,
  108. 0x4d, 0xab, 0xab, 0xdf, 0xbc, 0x9c, 0x16, 0xd0, 0x22, 0x50, 0xd1, 0x38,
  109. 0xdd, 0x3f, 0x64, 0x05, 0x9e, 0x68, 0x7a, 0x1e, 0xf1, 0x56, 0xbf, 0x1e,
  110. 0x2c, 0xc5, 0x97, 0x2a, 0xfe, 0x7a, 0x22, 0xdc, 0x6c, 0x68, 0xb8, 0x2e,
  111. 0x06, 0xdb, 0x41, 0xca, 0x98, 0xd8, 0x54, 0xc7, 0x64, 0x48, 0x24, 0x04,
  112. 0x20, 0xbc, 0x59, 0xe3, 0x6b, 0xea, 0x7e, 0xfc, 0x7e, 0xc5, 0x4e, 0xd4,
  113. 0xd8, 0x3a, 0xed, 0xcd, 0x5d, 0x99, 0xb8, 0x5c, 0xa2, 0x8b, 0xbb, 0x0b,
  114. 0xac, 0xe6, 0x8e, 0x25, 0x56, 0x22, 0x3a, 0x2d, 0x3a, 0x56, 0x41, 0x14,
  115. 0x1f, 0x1c, 0x8f, 0x53, 0x46, 0x13, 0x85, 0x02, 0x15, 0x00, 0x98, 0x7e,
  116. 0x92, 0x81, 0x88, 0xc7, 0x3f, 0x70, 0x49, 0x54, 0xf6, 0x76, 0xb4, 0xa3,
  117. 0x9e, 0x1d, 0x45, 0x98, 0x32, 0x7f, 0x02, 0x81, 0x80, 0x69, 0x4d, 0xef,
  118. 0x55, 0xff, 0x4d, 0x59, 0x2c, 0x01, 0xfa, 0x6a, 0x38, 0xe0, 0x70, 0x9f,
  119. 0x9e, 0x66, 0x8e, 0x3e, 0x8c, 0x52, 0x22, 0x9d, 0x15, 0x7e, 0x3c, 0xef,
  120. 0x4c, 0x7a, 0x61, 0x26, 0xe0, 0x2b, 0x81, 0x3f, 0xeb, 0xaf, 0x35, 0x38,
  121. 0x8d, 0xfe, 0xed, 0x46, 0xff, 0x5f, 0x03, 0x9b, 0x81, 0x92, 0xe7, 0x6f,
  122. 0x76, 0x4f, 0x1d, 0xd9, 0xbb, 0x89, 0xc9, 0x3e, 0xd9, 0x0b, 0xf9, 0xf4,
  123. 0x78, 0x11, 0x59, 0xc0, 0x1d, 0xcd, 0x0e, 0xa1, 0x6f, 0x15, 0xf1, 0x4d,
  124. 0xc1, 0xc9, 0x22, 0xed, 0x8d, 0xad, 0x67, 0xc5, 0x4b, 0x95, 0x93, 0x86,
  125. 0xa6, 0xaf, 0x8a, 0xee, 0x06, 0x89, 0x2f, 0x37, 0x7e, 0x64, 0xaa, 0xf6,
  126. 0xe7, 0xb1, 0x5a, 0x0a, 0x93, 0x95, 0x5d, 0x3e, 0x53, 0x9a, 0xde, 0x8a,
  127. 0xc2, 0x95, 0x45, 0x81, 0xbe, 0x5c, 0x2f, 0xc2, 0xb2, 0x92, 0x58, 0x19,
  128. 0x72, 0x80, 0xe9, 0x79, 0xa1, 0x02, 0x81, 0x80, 0x07, 0xd7, 0x62, 0xff,
  129. 0xdf, 0x1a, 0x3f, 0xed, 0x32, 0xd4, 0xd4, 0x88, 0x7b, 0x2c, 0x63, 0x7f,
  130. 0x97, 0xdc, 0x44, 0xd4, 0x84, 0xa2, 0xdd, 0x17, 0x16, 0x85, 0x13, 0xe0,
  131. 0xac, 0x51, 0x8d, 0x29, 0x1b, 0x75, 0x9a, 0xe4, 0xe3, 0x8a, 0x92, 0x69,
  132. 0x09, 0x03, 0xc5, 0x68, 0xae, 0x5e, 0x94, 0xfe, 0xc9, 0x92, 0x6c, 0x07,
  133. 0xb4, 0x1e, 0x64, 0x62, 0x87, 0xc6, 0xa4, 0xfd, 0x0d, 0x5f, 0xe5, 0xf9,
  134. 0x1b, 0x4f, 0x85, 0x5f, 0xae, 0xf3, 0x11, 0xe5, 0x18, 0xd4, 0x4d, 0x79,
  135. 0x9f, 0xc4, 0x79, 0x26, 0x04, 0x27, 0xf0, 0x0b, 0xee, 0x2b, 0x86, 0x9f,
  136. 0x86, 0x61, 0xe6, 0x51, 0xce, 0x04, 0x9b, 0x5d, 0x6b, 0x34, 0x43, 0x8c,
  137. 0x85, 0x3c, 0xf1, 0x51, 0x9b, 0x08, 0x23, 0x1b, 0xf5, 0x7e, 0x33, 0x12,
  138. 0xea, 0xab, 0x1f, 0xb7, 0x2d, 0xe2, 0x5f, 0xe6, 0x97, 0x99, 0xb5, 0x45,
  139. 0x16, 0x5b, 0xc3, 0x41, 0x02, 0x14, 0x61, 0xbf, 0x51, 0x60, 0xcf, 0xc8,
  140. 0xf1, 0x8c, 0x82, 0x97, 0xf2, 0xf4, 0x19, 0xba, 0x2b, 0xf3, 0x16, 0xbe,
  141. 0x40, 0x48
  142. };
  143. #endif
  144. /*
  145. * kExampleBadRSAKeyDER is an RSA private key in ASN.1, DER format. The private
  146. * components are not correct.
  147. */
  148. static const unsigned char kExampleBadRSAKeyDER[] = {
  149. 0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  150. 0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef,
  151. 0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9,
  152. 0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4,
  153. 0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3,
  154. 0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05,
  155. 0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26,
  156. 0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0,
  157. 0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33,
  158. 0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66,
  159. 0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde,
  160. 0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa,
  161. 0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53,
  162. 0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c,
  163. 0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75,
  164. 0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7,
  165. 0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c,
  166. 0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a,
  167. 0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87,
  168. 0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd,
  169. 0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0,
  170. 0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18,
  171. 0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  172. 0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5,
  173. 0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06,
  174. 0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0,
  175. 0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75,
  176. 0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1,
  177. 0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00,
  178. 0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd,
  179. 0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29,
  180. 0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5,
  181. 0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05,
  182. 0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e,
  183. 0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf,
  184. 0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d,
  185. 0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb,
  186. 0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5,
  187. 0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef,
  188. 0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9,
  189. 0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2,
  190. 0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7,
  191. 0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff,
  192. 0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99,
  193. 0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78,
  194. 0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb,
  195. 0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35,
  196. 0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46,
  197. 0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6,
  198. 0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f,
  199. 0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56,
  200. 0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d,
  201. 0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55,
  202. 0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20,
  203. 0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6,
  204. 0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81,
  205. 0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a,
  206. 0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e,
  207. 0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3,
  208. 0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1,
  209. 0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6,
  210. 0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77,
  211. 0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a,
  212. 0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d,
  213. 0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b,
  214. 0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a,
  215. 0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf,
  216. 0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28,
  217. 0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12,
  218. 0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd,
  219. 0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a,
  220. 0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5,
  221. 0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e,
  222. 0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b,
  223. 0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84,
  224. 0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e,
  225. 0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97,
  226. 0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81,
  227. 0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b,
  228. 0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6,
  229. 0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74,
  230. 0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e,
  231. 0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9,
  232. 0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98,
  233. 0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05,
  234. 0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef,
  235. 0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf,
  236. 0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69,
  237. 0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce,
  238. };
  239. static const unsigned char kMsg[] = { 1, 2, 3, 4 };
  240. static const unsigned char kSignature[] = {
  241. 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
  242. 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
  243. 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
  244. 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
  245. 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
  246. 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
  247. 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
  248. 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
  249. 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
  250. 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
  251. 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
  252. };
  253. /*
  254. * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
  255. * PrivateKeyInfo.
  256. */
  257. static const unsigned char kExampleRSAKeyPKCS8[] = {
  258. 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  259. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
  260. 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
  261. 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
  262. 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
  263. 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
  264. 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
  265. 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
  266. 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
  267. 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
  268. 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
  269. 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
  270. 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
  271. 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
  272. 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
  273. 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
  274. 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
  275. 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
  276. 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
  277. 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
  278. 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
  279. 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
  280. 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
  281. 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
  282. 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
  283. 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
  284. 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
  285. 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
  286. 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
  287. 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
  288. 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
  289. 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
  290. 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
  291. 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
  292. 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
  293. 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
  294. 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
  295. 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
  296. 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
  297. 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
  298. 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
  299. 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
  300. 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
  301. 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
  302. 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
  303. 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
  304. 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
  305. 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
  306. 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
  307. 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
  308. 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
  309. 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
  310. 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  311. };
  312. #ifndef OPENSSL_NO_EC
  313. /*
  314. * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
  315. * structure.
  316. */
  317. static const unsigned char kExampleECKeyDER[] = {
  318. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
  319. 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
  320. 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
  321. 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  322. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
  323. 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
  324. 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
  325. 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
  326. 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
  327. 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
  328. 0xc1,
  329. };
  330. /*
  331. * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
  332. * structure. The private key is equal to the order and will fail to import
  333. */
  334. static const unsigned char kExampleBadECKeyDER[] = {
  335. 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
  336. 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
  337. 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
  338. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  339. 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
  340. 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
  341. 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  342. 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  343. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
  344. };
  345. /* prime256v1 */
  346. static const unsigned char kExampleECPubKeyDER[] = {
  347. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  348. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  349. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  350. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  351. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  352. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  353. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  354. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  355. };
  356. /*
  357. * kExampleBadECPubKeyDER is a sample EC public key with a wrong OID
  358. * 1.2.840.10045.2.2 instead of 1.2.840.10045.2.1 - EC Public Key
  359. */
  360. static const unsigned char kExampleBadECPubKeyDER[] = {
  361. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  362. 0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  363. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  364. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  365. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  366. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  367. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  368. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  369. };
  370. static const unsigned char pExampleECParamDER[] = {
  371. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  372. };
  373. static const unsigned char kExampleED25519KeyDER[] = {
  374. 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,
  375. 0x04, 0x22, 0x04, 0x20, 0xba, 0x7b, 0xba, 0x20, 0x1b, 0x02, 0x75, 0x3a,
  376. 0xe8, 0x88, 0xfe, 0x00, 0xcd, 0x8b, 0xc6, 0xf4, 0x5c, 0x47, 0x09, 0x46,
  377. 0x66, 0xe4, 0x72, 0x85, 0x25, 0x26, 0x5e, 0x12, 0x33, 0x48, 0xf6, 0x50
  378. };
  379. static const unsigned char kExampleED25519PubKeyDER[] = {
  380. 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
  381. 0xf5, 0xc5, 0xeb, 0x52, 0x3e, 0x7d, 0x07, 0x86, 0xb2, 0x55, 0x07, 0x45,
  382. 0xef, 0x5b, 0x7c, 0x20, 0xe8, 0x66, 0x28, 0x30, 0x3c, 0x8a, 0x82, 0x40,
  383. 0x97, 0xa3, 0x08, 0xdc, 0x65, 0x80, 0x39, 0x29
  384. };
  385. #endif
  386. typedef struct APK_DATA_st {
  387. const unsigned char *kder;
  388. size_t size;
  389. const char *keytype;
  390. int evptype;
  391. int check;
  392. int pub_check;
  393. int param_check;
  394. int type; /* 0 for private, 1 for public, 2 for params */
  395. } APK_DATA;
  396. static APK_DATA keydata[] = {
  397. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA},
  398. {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), "RSA", EVP_PKEY_RSA},
  399. #ifndef OPENSSL_NO_EC
  400. {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC}
  401. #endif
  402. };
  403. static APK_DATA keycheckdata[] = {
  404. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA, 1, 1, 1,
  405. 0},
  406. {kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), "RSA", EVP_PKEY_RSA,
  407. 0, 1, 1, 0},
  408. #ifndef OPENSSL_NO_EC
  409. {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC, 1, 1, 1, 0},
  410. /* group is also associated in our pub key */
  411. {kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), "EC", EVP_PKEY_EC, 0, 1,
  412. 1, 1},
  413. {pExampleECParamDER, sizeof(pExampleECParamDER), "EC", EVP_PKEY_EC, 0, 0, 1,
  414. 2},
  415. {kExampleED25519KeyDER, sizeof(kExampleED25519KeyDER), "ED25519",
  416. EVP_PKEY_ED25519, 1, 1, 1, 0},
  417. {kExampleED25519PubKeyDER, sizeof(kExampleED25519PubKeyDER), "ED25519",
  418. EVP_PKEY_ED25519, 0, 1, 1, 1},
  419. #endif
  420. };
  421. static EVP_PKEY *load_example_key(const char *keytype,
  422. const unsigned char *data, size_t data_len)
  423. {
  424. const unsigned char **pdata = &data;
  425. EVP_PKEY *pkey = NULL;
  426. OSSL_DECODER_CTX *dctx =
  427. OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, keytype, 0,
  428. testctx, testpropq);
  429. /* |pkey| will be NULL on error */
  430. (void)OSSL_DECODER_from_data(dctx, pdata, &data_len);
  431. OSSL_DECODER_CTX_free(dctx);
  432. return pkey;
  433. }
  434. static EVP_PKEY *load_example_rsa_key(void)
  435. {
  436. return load_example_key("RSA", kExampleRSAKeyDER,
  437. sizeof(kExampleRSAKeyDER));
  438. }
  439. #ifndef OPENSSL_NO_DSA
  440. static EVP_PKEY *load_example_dsa_key(void)
  441. {
  442. return load_example_key("DSA", kExampleDSAKeyDER,
  443. sizeof(kExampleDSAKeyDER));
  444. }
  445. #endif
  446. static EVP_PKEY *load_example_hmac_key(void)
  447. {
  448. EVP_PKEY *pkey = NULL;
  449. unsigned char key[] = {
  450. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  451. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  452. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  453. };
  454. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, key, sizeof(key));
  455. if (!TEST_ptr(pkey))
  456. return NULL;
  457. return pkey;
  458. }
  459. static int test_EVP_set_default_properties(void)
  460. {
  461. OSSL_LIB_CTX *ctx;
  462. EVP_MD *md = NULL;
  463. int res = 0;
  464. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
  465. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
  466. goto err;
  467. EVP_MD_free(md);
  468. md = NULL;
  469. if (!TEST_true(EVP_set_default_properties(ctx, "provider=fizzbang"))
  470. || !TEST_ptr_null(md = EVP_MD_fetch(ctx, "sha256", NULL))
  471. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", "-provider")))
  472. goto err;
  473. EVP_MD_free(md);
  474. md = NULL;
  475. if (!TEST_true(EVP_set_default_properties(ctx, NULL))
  476. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
  477. goto err;
  478. res = 1;
  479. err:
  480. EVP_MD_free(md);
  481. OSSL_LIB_CTX_free(ctx);
  482. return res;
  483. }
  484. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
  485. static int test_fromdata(char *keytype, OSSL_PARAM *params)
  486. {
  487. EVP_PKEY_CTX *pctx = NULL;
  488. EVP_PKEY *pkey = NULL;
  489. int testresult = 0;
  490. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, keytype, testpropq)))
  491. goto err;
  492. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  493. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  494. params), 0))
  495. goto err;
  496. if (!TEST_ptr(pkey))
  497. goto err;
  498. testresult = 1;
  499. err:
  500. EVP_PKEY_free(pkey);
  501. EVP_PKEY_CTX_free(pctx);
  502. return testresult;
  503. }
  504. #endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA || !OPENSSL_NO_EC */
  505. /*
  506. * Test combinations of private, public, missing and private + public key
  507. * params to ensure they are all accepted
  508. */
  509. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
  510. static int test_EVP_PKEY_ffc_priv_pub(char *keytype)
  511. {
  512. OSSL_PARAM_BLD *bld = NULL;
  513. OSSL_PARAM *params = NULL;
  514. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  515. int ret = 0;
  516. /*
  517. * Setup the parameters for our pkey object. For our purposes they don't
  518. * have to actually be *valid* parameters. We just need to set something.
  519. */
  520. if (!TEST_ptr(p = BN_new())
  521. || !TEST_ptr(q = BN_new())
  522. || !TEST_ptr(g = BN_new())
  523. || !TEST_ptr(pub = BN_new())
  524. || !TEST_ptr(priv = BN_new()))
  525. goto err;
  526. /* Test !priv and !pub */
  527. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  528. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  529. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  530. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g)))
  531. goto err;
  532. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  533. goto err;
  534. if (!test_fromdata(keytype, params))
  535. goto err;
  536. OSSL_PARAM_free(params);
  537. OSSL_PARAM_BLD_free(bld);
  538. /* Test priv and !pub */
  539. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  540. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  541. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  542. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  543. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  544. priv)))
  545. goto err;
  546. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  547. goto err;
  548. if (!test_fromdata(keytype, params))
  549. goto err;
  550. OSSL_PARAM_free(params);
  551. OSSL_PARAM_BLD_free(bld);
  552. /* Test !priv and pub */
  553. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  554. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  555. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  556. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  557. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  558. pub)))
  559. goto err;
  560. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  561. goto err;
  562. if (!test_fromdata(keytype, params))
  563. goto err;
  564. OSSL_PARAM_free(params);
  565. OSSL_PARAM_BLD_free(bld);
  566. /* Test priv and pub */
  567. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  568. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  569. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  570. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  571. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  572. pub))
  573. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  574. priv)))
  575. goto err;
  576. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  577. goto err;
  578. if (!test_fromdata(keytype, params))
  579. goto err;
  580. ret = 1;
  581. err:
  582. OSSL_PARAM_free(params);
  583. OSSL_PARAM_BLD_free(bld);
  584. BN_free(p);
  585. BN_free(q);
  586. BN_free(g);
  587. BN_free(pub);
  588. BN_free(priv);
  589. return ret;
  590. }
  591. #endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA */
  592. /*
  593. * Test combinations of private, public, missing and private + public key
  594. * params to ensure they are all accepted for EC keys
  595. */
  596. #ifndef OPENSSL_NO_EC
  597. static unsigned char ec_priv[] = {
  598. 0xe9, 0x25, 0xf7, 0x66, 0x58, 0xa4, 0xdd, 0x99, 0x61, 0xe7, 0xe8, 0x23,
  599. 0x85, 0xc2, 0xe8, 0x33, 0x27, 0xc5, 0x5c, 0xeb, 0xdb, 0x43, 0x9f, 0xd5,
  600. 0xf2, 0x5a, 0x75, 0x55, 0xd0, 0x2e, 0x6d, 0x16
  601. };
  602. static unsigned char ec_pub[] = {
  603. 0x04, 0xad, 0x11, 0x90, 0x77, 0x4b, 0x46, 0xee, 0x72, 0x51, 0x15, 0x97,
  604. 0x4a, 0x6a, 0xa7, 0xaf, 0x59, 0xfa, 0x4b, 0xf2, 0x41, 0xc8, 0x3a, 0x81,
  605. 0x23, 0xb6, 0x90, 0x04, 0x6c, 0x67, 0x66, 0xd0, 0xdc, 0xf2, 0x15, 0x1d,
  606. 0x41, 0x61, 0xb7, 0x95, 0x85, 0x38, 0x5a, 0x84, 0x56, 0xe8, 0xb3, 0x0e,
  607. 0xf5, 0xc6, 0x5d, 0xa4, 0x54, 0x26, 0xb0, 0xf7, 0xa5, 0x4a, 0x33, 0xf1,
  608. 0x08, 0x09, 0xb8, 0xdb, 0x03
  609. };
  610. static int test_EC_priv_pub(void)
  611. {
  612. OSSL_PARAM_BLD *bld = NULL;
  613. OSSL_PARAM *params = NULL;
  614. BIGNUM *priv = NULL;
  615. int ret = 0;
  616. /*
  617. * Setup the parameters for our pkey object. For our purposes they don't
  618. * have to actually be *valid* parameters. We just need to set something.
  619. */
  620. if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
  621. goto err;
  622. /* Test !priv and !pub */
  623. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  624. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  625. OSSL_PKEY_PARAM_GROUP_NAME,
  626. "P-256", 0)))
  627. goto err;
  628. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  629. goto err;
  630. if (!test_fromdata("EC", params))
  631. goto err;
  632. OSSL_PARAM_free(params);
  633. OSSL_PARAM_BLD_free(bld);
  634. /* Test priv and !pub */
  635. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  636. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  637. OSSL_PKEY_PARAM_GROUP_NAME,
  638. "P-256", 0))
  639. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  640. priv)))
  641. goto err;
  642. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  643. goto err;
  644. if (!test_fromdata("EC", params))
  645. goto err;
  646. OSSL_PARAM_free(params);
  647. OSSL_PARAM_BLD_free(bld);
  648. /* Test !priv and pub */
  649. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  650. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  651. OSSL_PKEY_PARAM_GROUP_NAME,
  652. "P-256", 0))
  653. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  654. OSSL_PKEY_PARAM_PUB_KEY,
  655. ec_pub, sizeof(ec_pub))))
  656. goto err;
  657. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  658. goto err;
  659. if (!test_fromdata("EC", params))
  660. goto err;
  661. OSSL_PARAM_free(params);
  662. OSSL_PARAM_BLD_free(bld);
  663. /* Test priv and pub */
  664. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  665. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  666. OSSL_PKEY_PARAM_GROUP_NAME,
  667. "P-256", 0))
  668. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  669. OSSL_PKEY_PARAM_PUB_KEY,
  670. ec_pub, sizeof(ec_pub)))
  671. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  672. priv)))
  673. goto err;
  674. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  675. goto err;
  676. if (!test_fromdata("EC", params))
  677. goto err;
  678. ret = 1;
  679. err:
  680. OSSL_PARAM_free(params);
  681. OSSL_PARAM_BLD_free(bld);
  682. BN_free(priv);
  683. return ret;
  684. }
  685. /* Test that using a legacy EC key with only a private key in it works */
  686. # ifndef OPENSSL_NO_DEPRECATED_3_0
  687. static int test_EC_priv_only_legacy(void)
  688. {
  689. BIGNUM *priv = NULL;
  690. int ret = 0;
  691. EC_KEY *eckey = NULL;
  692. EVP_PKEY *pkey = NULL, *dup_pk = NULL;
  693. EVP_MD_CTX *ctx = NULL;
  694. /* Create the low level EC_KEY */
  695. if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
  696. goto err;
  697. eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  698. if (!TEST_ptr(eckey))
  699. goto err;
  700. if (!TEST_true(EC_KEY_set_private_key(eckey, priv)))
  701. goto err;
  702. pkey = EVP_PKEY_new();
  703. if (!TEST_ptr(pkey))
  704. goto err;
  705. if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  706. goto err;
  707. eckey = NULL;
  708. while (dup_pk == NULL) {
  709. ret = 0;
  710. ctx = EVP_MD_CTX_new();
  711. if (!TEST_ptr(ctx))
  712. goto err;
  713. /*
  714. * The EVP_DigestSignInit function should create the key on the
  715. * provider side which is sufficient for this test.
  716. */
  717. if (!TEST_true(EVP_DigestSignInit_ex(ctx, NULL, NULL, testctx,
  718. testpropq, pkey, NULL)))
  719. goto err;
  720. EVP_MD_CTX_free(ctx);
  721. ctx = NULL;
  722. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)))
  723. goto err;
  724. /* EVP_PKEY_eq() returns -2 with missing public keys */
  725. ret = TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), -2);
  726. EVP_PKEY_free(pkey);
  727. pkey = dup_pk;
  728. if (!ret)
  729. goto err;
  730. }
  731. err:
  732. EVP_MD_CTX_free(ctx);
  733. EVP_PKEY_free(pkey);
  734. EC_KEY_free(eckey);
  735. BN_free(priv);
  736. return ret;
  737. }
  738. # endif /* OPENSSL_NO_DEPRECATED_3_0 */
  739. #endif /* OPENSSL_NO_EC */
  740. static int test_EVP_Enveloped(void)
  741. {
  742. int ret = 0;
  743. EVP_CIPHER_CTX *ctx = NULL;
  744. EVP_PKEY *keypair = NULL;
  745. unsigned char *kek = NULL;
  746. unsigned char iv[EVP_MAX_IV_LENGTH];
  747. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  748. int len, kek_len, ciphertext_len, plaintext_len;
  749. unsigned char ciphertext[32], plaintext[16];
  750. const EVP_CIPHER *type = NULL;
  751. if (nullprov != NULL)
  752. return TEST_skip("Test does not support a non-default library context");
  753. type = EVP_aes_256_cbc();
  754. if (!TEST_ptr(keypair = load_example_rsa_key())
  755. || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_size(keypair)))
  756. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  757. || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,
  758. &keypair, 1))
  759. || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,
  760. msg, sizeof(msg)))
  761. || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,
  762. &len)))
  763. goto err;
  764. ciphertext_len += len;
  765. if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))
  766. || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,
  767. ciphertext, ciphertext_len))
  768. || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))
  769. goto err;
  770. plaintext_len += len;
  771. if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))
  772. goto err;
  773. ret = 1;
  774. err:
  775. OPENSSL_free(kek);
  776. EVP_PKEY_free(keypair);
  777. EVP_CIPHER_CTX_free(ctx);
  778. return ret;
  779. }
  780. /*
  781. * Test 0: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, RSA)
  782. * Test 1: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, DSA)
  783. * Test 2: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, HMAC)
  784. * Test 3: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, RSA)
  785. * Test 4: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, DSA)
  786. * Test 5: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch diegst, HMAC)
  787. * Test 6: Use an MD BIO to do the Update calls instead (RSA)
  788. * Test 7: Use an MD BIO to do the Update calls instead (DSA)
  789. * Test 8: Use an MD BIO to do the Update calls instead (HMAC)
  790. */
  791. static int test_EVP_DigestSignInit(int tst)
  792. {
  793. int ret = 0;
  794. EVP_PKEY *pkey = NULL;
  795. unsigned char *sig = NULL;
  796. size_t sig_len = 0;
  797. EVP_MD_CTX *md_ctx = NULL, *md_ctx_verify = NULL;
  798. EVP_MD_CTX *a_md_ctx = NULL, *a_md_ctx_verify = NULL;
  799. BIO *mdbio = NULL, *membio = NULL;
  800. size_t written;
  801. const EVP_MD *md;
  802. EVP_MD *mdexp = NULL;
  803. if (nullprov != NULL)
  804. return TEST_skip("Test does not support a non-default library context");
  805. if (tst >= 6) {
  806. membio = BIO_new(BIO_s_mem());
  807. mdbio = BIO_new(BIO_f_md());
  808. if (!TEST_ptr(membio) || !TEST_ptr(mdbio))
  809. goto out;
  810. BIO_push(mdbio, membio);
  811. if (!TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx), 0))
  812. goto out;
  813. } else {
  814. if (!TEST_ptr(a_md_ctx = md_ctx = EVP_MD_CTX_new())
  815. || !TEST_ptr(a_md_ctx_verify = md_ctx_verify = EVP_MD_CTX_new()))
  816. goto out;
  817. }
  818. if (tst == 0 || tst == 3 || tst == 6) {
  819. if (!TEST_ptr(pkey = load_example_rsa_key()))
  820. goto out;
  821. } else if (tst == 1 || tst == 4 || tst == 7) {
  822. #ifndef OPENSSL_NO_DSA
  823. if (!TEST_ptr(pkey = load_example_dsa_key()))
  824. goto out;
  825. #else
  826. ret = 1;
  827. goto out;
  828. #endif
  829. } else {
  830. if (!TEST_ptr(pkey = load_example_hmac_key()))
  831. goto out;
  832. }
  833. if (tst >= 3 && tst <= 5)
  834. md = mdexp = EVP_MD_fetch(NULL, "SHA256", NULL);
  835. else
  836. md = EVP_sha256();
  837. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, md, NULL, pkey)))
  838. goto out;
  839. if (tst >= 6) {
  840. if (!BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written))
  841. goto out;
  842. } else {
  843. if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  844. goto out;
  845. }
  846. /* Determine the size of the signature. */
  847. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
  848. || !TEST_ptr(sig = OPENSSL_malloc(sig_len))
  849. || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  850. goto out;
  851. if (tst >= 6) {
  852. if (!TEST_int_gt(BIO_reset(mdbio), 0)
  853. || !TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx_verify), 0))
  854. goto out;
  855. }
  856. /*
  857. * Ensure that the signature round-trips (Verification isn't supported for
  858. * HMAC via EVP_DigestVerify*)
  859. */
  860. if (tst != 2 && tst != 5 && tst != 8) {
  861. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, md,
  862. NULL, pkey)))
  863. goto out;
  864. if (tst >= 6) {
  865. if (!TEST_true(BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written)))
  866. goto out;
  867. } else {
  868. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg,
  869. sizeof(kMsg))))
  870. goto out;
  871. }
  872. if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  873. goto out;
  874. }
  875. ret = 1;
  876. out:
  877. BIO_free(membio);
  878. BIO_free(mdbio);
  879. EVP_MD_CTX_free(a_md_ctx);
  880. EVP_MD_CTX_free(a_md_ctx_verify);
  881. EVP_PKEY_free(pkey);
  882. OPENSSL_free(sig);
  883. EVP_MD_free(mdexp);
  884. return ret;
  885. }
  886. static int test_EVP_DigestVerifyInit(void)
  887. {
  888. int ret = 0;
  889. EVP_PKEY *pkey = NULL;
  890. EVP_MD_CTX *md_ctx = NULL;
  891. if (nullprov != NULL)
  892. return TEST_skip("Test does not support a non-default library context");
  893. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
  894. || !TEST_ptr(pkey = load_example_rsa_key()))
  895. goto out;
  896. if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
  897. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
  898. || !TEST_true(EVP_DigestVerifyFinal(md_ctx, kSignature,
  899. sizeof(kSignature))))
  900. goto out;
  901. ret = 1;
  902. out:
  903. EVP_MD_CTX_free(md_ctx);
  904. EVP_PKEY_free(pkey);
  905. return ret;
  906. }
  907. /*
  908. * Test corner cases of EVP_DigestInit/Update/Final API call behavior.
  909. */
  910. static int test_EVP_Digest(void)
  911. {
  912. int ret = 0;
  913. EVP_MD_CTX *md_ctx = NULL;
  914. unsigned char md[EVP_MAX_MD_SIZE];
  915. EVP_MD *sha256 = NULL;
  916. EVP_MD *shake256 = NULL;
  917. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  918. goto out;
  919. if (!TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", testpropq))
  920. || !TEST_ptr(shake256 = EVP_MD_fetch(testctx, "shake256", testpropq)))
  921. goto out;
  922. if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
  923. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  924. || !TEST_true(EVP_DigestFinal(md_ctx, md, NULL))
  925. /* EVP_DigestFinal resets the EVP_MD_CTX. */
  926. || !TEST_ptr_eq(EVP_MD_CTX_md(md_ctx), NULL))
  927. goto out;
  928. if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
  929. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  930. || !TEST_true(EVP_DigestFinal_ex(md_ctx, md, NULL))
  931. /* EVP_DigestFinal_ex does not reset the EVP_MD_CTX. */
  932. || !TEST_ptr(EVP_MD_CTX_md(md_ctx))
  933. /*
  934. * EVP_DigestInit_ex with NULL type should work on
  935. * pre-initialized context.
  936. */
  937. || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
  938. goto out;
  939. if (!TEST_true(EVP_DigestInit_ex(md_ctx, shake256, NULL))
  940. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  941. || !TEST_true(EVP_DigestFinalXOF(md_ctx, md, sizeof(md)))
  942. /* EVP_DigestFinalXOF does not reset the EVP_MD_CTX. */
  943. || !TEST_ptr(EVP_MD_CTX_md(md_ctx))
  944. || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
  945. goto out;
  946. ret = 1;
  947. out:
  948. EVP_MD_CTX_free(md_ctx);
  949. EVP_MD_free(sha256);
  950. EVP_MD_free(shake256);
  951. return ret;
  952. }
  953. static int test_d2i_AutoPrivateKey(int i)
  954. {
  955. int ret = 0;
  956. const unsigned char *p;
  957. EVP_PKEY *pkey = NULL;
  958. const APK_DATA *ak = &keydata[i];
  959. const unsigned char *input = ak->kder;
  960. size_t input_len = ak->size;
  961. int expected_id = ak->evptype;
  962. p = input;
  963. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  964. || !TEST_ptr_eq(p, input + input_len)
  965. || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id))
  966. goto done;
  967. ret = 1;
  968. done:
  969. EVP_PKEY_free(pkey);
  970. return ret;
  971. }
  972. #ifndef OPENSSL_NO_EC
  973. static const unsigned char ec_public_sect163k1_validxy[] = {
  974. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  975. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  976. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  977. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b,
  978. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  979. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  980. };
  981. static const unsigned char ec_public_sect163k1_badx[] = {
  982. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  983. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  984. 0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  985. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b,
  986. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  987. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  988. };
  989. static const unsigned char ec_public_sect163k1_bady[] = {
  990. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  991. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  992. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  993. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b,
  994. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  995. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6
  996. };
  997. static struct ec_der_pub_keys_st {
  998. const unsigned char *der;
  999. size_t len;
  1000. int valid;
  1001. } ec_der_pub_keys[] = {
  1002. { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 },
  1003. { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 },
  1004. { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 },
  1005. };
  1006. /*
  1007. * Tests the range of the decoded EC char2 public point.
  1008. * See ec_GF2m_simple_oct2point().
  1009. */
  1010. static int test_invalide_ec_char2_pub_range_decode(int id)
  1011. {
  1012. int ret = 0;
  1013. EVP_PKEY *pkey;
  1014. pkey = load_example_key("EC", ec_der_pub_keys[id].der,
  1015. ec_der_pub_keys[id].len);
  1016. ret = (ec_der_pub_keys[id].valid && TEST_ptr(pkey))
  1017. || TEST_ptr_null(pkey);
  1018. EVP_PKEY_free(pkey);
  1019. return ret;
  1020. }
  1021. /* Tests loading a bad key in PKCS8 format */
  1022. static int test_EVP_PKCS82PKEY(void)
  1023. {
  1024. int ret = 0;
  1025. const unsigned char *derp = kExampleBadECKeyDER;
  1026. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  1027. EVP_PKEY *pkey = NULL;
  1028. if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp,
  1029. sizeof(kExampleBadECKeyDER))))
  1030. goto done;
  1031. if (!TEST_ptr_eq(derp,
  1032. kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)))
  1033. goto done;
  1034. if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf)))
  1035. goto done;
  1036. ret = 1;
  1037. done:
  1038. PKCS8_PRIV_KEY_INFO_free(p8inf);
  1039. EVP_PKEY_free(pkey);
  1040. return ret;
  1041. }
  1042. #endif
  1043. /* This uses kExampleRSAKeyDER and kExampleRSAKeyPKCS8 to verify encoding */
  1044. static int test_privatekey_to_pkcs8(void)
  1045. {
  1046. EVP_PKEY *pkey = NULL;
  1047. BIO *membio = NULL;
  1048. char *membuf = NULL;
  1049. long membuf_len = 0;
  1050. int ok = 0;
  1051. if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
  1052. || !TEST_ptr(pkey = load_example_rsa_key())
  1053. || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
  1054. NULL, 0, NULL, NULL),
  1055. 0)
  1056. || !TEST_int_gt(membuf_len = BIO_get_mem_data(membio, &membuf), 0)
  1057. || !TEST_ptr(membuf)
  1058. || !TEST_mem_eq(membuf, (size_t)membuf_len,
  1059. kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8))
  1060. /*
  1061. * We try to write PEM as well, just to see that it doesn't err, but
  1062. * assume that the result is correct.
  1063. */
  1064. || !TEST_int_gt(PEM_write_bio_PKCS8PrivateKey(membio, pkey, NULL,
  1065. NULL, 0, NULL, NULL),
  1066. 0))
  1067. goto done;
  1068. ok = 1;
  1069. done:
  1070. EVP_PKEY_free(pkey);
  1071. BIO_free_all(membio);
  1072. return ok;
  1073. }
  1074. #ifndef OPENSSL_NO_EC
  1075. static const struct {
  1076. int encoding;
  1077. const char *encoding_name;
  1078. } ec_encodings[] = {
  1079. { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
  1080. { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP }
  1081. };
  1082. static int ec_export_get_encoding_cb(const OSSL_PARAM params[], void *arg)
  1083. {
  1084. const OSSL_PARAM *p;
  1085. const char *enc_name = NULL;
  1086. int *enc = arg;
  1087. size_t i;
  1088. *enc = -1;
  1089. if (!TEST_ptr(p = OSSL_PARAM_locate_const(params,
  1090. OSSL_PKEY_PARAM_EC_ENCODING))
  1091. || !TEST_true(OSSL_PARAM_get_utf8_string_ptr(p, &enc_name)))
  1092. return 0;
  1093. for (i = 0; i < OSSL_NELEM(ec_encodings); i++) {
  1094. if (strcasecmp(enc_name, ec_encodings[i].encoding_name) == 0) {
  1095. *enc = ec_encodings[i].encoding;
  1096. break;
  1097. }
  1098. }
  1099. return (*enc != -1);
  1100. }
  1101. static int test_EC_keygen_with_enc(int idx)
  1102. {
  1103. EVP_PKEY *params = NULL, *key = NULL;
  1104. EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
  1105. int enc;
  1106. int ret = 0;
  1107. enc = ec_encodings[idx].encoding;
  1108. /* Create key parameters */
  1109. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "EC", NULL))
  1110. || !TEST_true(EVP_PKEY_paramgen_init(pctx))
  1111. || !TEST_true(EVP_PKEY_CTX_set_group_name(pctx, "P-256"))
  1112. || !TEST_true(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc))
  1113. || !TEST_true(EVP_PKEY_paramgen(pctx, &params))
  1114. || !TEST_ptr(params))
  1115. goto done;
  1116. /* Create key */
  1117. if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx, params, NULL))
  1118. || !TEST_true(EVP_PKEY_keygen_init(kctx))
  1119. || !TEST_true(EVP_PKEY_keygen(kctx, &key))
  1120. || !TEST_ptr(key))
  1121. goto done;
  1122. /* Check that the encoding got all the way into the key */
  1123. if (!TEST_true(evp_keymgmt_util_export(key, OSSL_KEYMGMT_SELECT_ALL,
  1124. ec_export_get_encoding_cb, &enc))
  1125. || !TEST_int_eq(enc, ec_encodings[idx].encoding))
  1126. goto done;
  1127. ret = 1;
  1128. done:
  1129. EVP_PKEY_free(key);
  1130. EVP_PKEY_free(params);
  1131. EVP_PKEY_CTX_free(kctx);
  1132. EVP_PKEY_CTX_free(pctx);
  1133. return ret;
  1134. }
  1135. #endif
  1136. #if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
  1137. static int test_EVP_SM2_verify(void)
  1138. {
  1139. const char *pubkey =
  1140. "-----BEGIN PUBLIC KEY-----\n"
  1141. "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEp1KLWq1ZE2jmoAnnBJE1LBGxVr18\n"
  1142. "YvvqECWCpXfAQ9qUJ+UmthnUPf0iM3SaXKHe6PlLIDyNlWMWb9RUh/yU3g==\n"
  1143. "-----END PUBLIC KEY-----\n";
  1144. const char *msg = "message digest";
  1145. const char *id = "ALICE123@YAHOO.COM";
  1146. const uint8_t signature[] = {
  1147. 0x30, 0x44, 0x02, 0x20, 0x5b, 0xdb, 0xab, 0x81, 0x4f, 0xbb,
  1148. 0x8b, 0x69, 0xb1, 0x05, 0x9c, 0x99, 0x3b, 0xb2, 0x45, 0x06,
  1149. 0x4a, 0x30, 0x15, 0x59, 0x84, 0xcd, 0xee, 0x30, 0x60, 0x36,
  1150. 0x57, 0x87, 0xef, 0x5c, 0xd0, 0xbe, 0x02, 0x20, 0x43, 0x8d,
  1151. 0x1f, 0xc7, 0x77, 0x72, 0x39, 0xbb, 0x72, 0xe1, 0xfd, 0x07,
  1152. 0x58, 0xd5, 0x82, 0xc8, 0x2d, 0xba, 0x3b, 0x2c, 0x46, 0x24,
  1153. 0xe3, 0x50, 0xff, 0x04, 0xc7, 0xa0, 0x71, 0x9f, 0xa4, 0x70
  1154. };
  1155. int rc = 0;
  1156. BIO *bio = NULL;
  1157. EVP_PKEY *pkey = NULL;
  1158. EVP_MD_CTX *mctx = NULL;
  1159. EVP_PKEY_CTX *pctx = NULL;
  1160. EVP_MD *sm3 = NULL;
  1161. bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
  1162. if (!TEST_true(bio != NULL))
  1163. goto done;
  1164. pkey = PEM_read_bio_PUBKEY_ex(bio, NULL, NULL, NULL, testctx, testpropq);
  1165. if (!TEST_true(pkey != NULL))
  1166. goto done;
  1167. if (!TEST_true(EVP_PKEY_is_a(pkey, "SM2")))
  1168. goto done;
  1169. if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
  1170. goto done;
  1171. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1172. goto done;
  1173. EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
  1174. if (!TEST_ptr(sm3 = EVP_MD_fetch(testctx, "sm3", testpropq)))
  1175. goto done;
  1176. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, sm3, NULL, pkey)))
  1177. goto done;
  1178. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, id, strlen(id)), 0))
  1179. goto done;
  1180. if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
  1181. goto done;
  1182. if (!TEST_true(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature))))
  1183. goto done;
  1184. rc = 1;
  1185. done:
  1186. BIO_free(bio);
  1187. EVP_PKEY_free(pkey);
  1188. EVP_PKEY_CTX_free(pctx);
  1189. EVP_MD_CTX_free(mctx);
  1190. EVP_MD_free(sm3);
  1191. return rc;
  1192. }
  1193. static int test_EVP_SM2(void)
  1194. {
  1195. int ret = 0;
  1196. EVP_PKEY *pkey = NULL;
  1197. EVP_PKEY *pkeyparams = NULL;
  1198. EVP_PKEY_CTX *pctx = NULL;
  1199. EVP_PKEY_CTX *kctx = NULL;
  1200. EVP_PKEY_CTX *sctx = NULL;
  1201. size_t sig_len = 0;
  1202. unsigned char *sig = NULL;
  1203. EVP_MD_CTX *md_ctx = NULL;
  1204. EVP_MD_CTX *md_ctx_verify = NULL;
  1205. EVP_PKEY_CTX *cctx = NULL;
  1206. EVP_MD *sm3 = NULL;
  1207. uint8_t ciphertext[128];
  1208. size_t ctext_len = sizeof(ciphertext);
  1209. uint8_t plaintext[8];
  1210. size_t ptext_len = sizeof(plaintext);
  1211. uint8_t sm2_id[] = {1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r'};
  1212. OSSL_PARAM sparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
  1213. OSSL_PARAM gparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
  1214. int i;
  1215. char mdname[20];
  1216. pctx = EVP_PKEY_CTX_new_from_name(testctx, "SM2", testpropq);
  1217. if (!TEST_ptr(pctx))
  1218. goto done;
  1219. if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
  1220. goto done;
  1221. if (!TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)))
  1222. goto done;
  1223. if (!TEST_true(EVP_PKEY_paramgen(pctx, &pkeyparams)))
  1224. goto done;
  1225. kctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkeyparams, testpropq);
  1226. if (!TEST_ptr(kctx))
  1227. goto done;
  1228. if (!TEST_true(EVP_PKEY_keygen_init(kctx)))
  1229. goto done;
  1230. if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
  1231. goto done;
  1232. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  1233. goto done;
  1234. if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
  1235. goto done;
  1236. if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1237. goto done;
  1238. EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
  1239. EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);
  1240. if (!TEST_ptr(sm3 = EVP_MD_fetch(testctx, "sm3", testpropq)))
  1241. goto done;
  1242. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, sm3, NULL, pkey)))
  1243. goto done;
  1244. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  1245. goto done;
  1246. if(!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  1247. goto done;
  1248. /* Determine the size of the signature. */
  1249. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
  1250. goto done;
  1251. if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
  1252. goto done;
  1253. if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  1254. goto done;
  1255. /* Ensure that the signature round-trips. */
  1256. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, sm3, NULL, pkey)))
  1257. goto done;
  1258. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  1259. goto done;
  1260. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
  1261. goto done;
  1262. if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  1263. goto done;
  1264. /* now check encryption/decryption */
  1265. gparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
  1266. mdname, sizeof(mdname));
  1267. for (i = 0; i < 2; i++) {
  1268. EVP_PKEY_CTX_free(cctx);
  1269. sparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
  1270. i == 0 ? "SM3" : "SHA2-256",
  1271. 0);
  1272. if (!TEST_ptr(cctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1273. goto done;
  1274. if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
  1275. goto done;
  1276. if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
  1277. goto done;
  1278. if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg,
  1279. sizeof(kMsg))))
  1280. goto done;
  1281. if (!TEST_true(EVP_PKEY_decrypt_init(cctx)))
  1282. goto done;
  1283. if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
  1284. goto done;
  1285. if (!TEST_true(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext,
  1286. ctext_len)))
  1287. goto done;
  1288. if (!TEST_true(EVP_PKEY_CTX_get_params(cctx, gparams)))
  1289. goto done;
  1290. /* Test we're still using the digest we think we are */
  1291. if (i == 0 && !TEST_int_eq(strcmp(mdname, "SM3"), 0))
  1292. goto done;
  1293. if (i == 1 && !TEST_int_eq(strcmp(mdname, "SHA2-256"), 0))
  1294. goto done;
  1295. if (!TEST_true(ptext_len == sizeof(kMsg)))
  1296. goto done;
  1297. if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
  1298. goto done;
  1299. }
  1300. ret = 1;
  1301. done:
  1302. EVP_PKEY_CTX_free(pctx);
  1303. EVP_PKEY_CTX_free(kctx);
  1304. EVP_PKEY_CTX_free(sctx);
  1305. EVP_PKEY_CTX_free(cctx);
  1306. EVP_PKEY_free(pkey);
  1307. EVP_PKEY_free(pkeyparams);
  1308. EVP_MD_CTX_free(md_ctx);
  1309. EVP_MD_CTX_free(md_ctx_verify);
  1310. EVP_MD_free(sm3);
  1311. OPENSSL_free(sig);
  1312. return ret;
  1313. }
  1314. #endif
  1315. static struct keys_st {
  1316. int type;
  1317. char *priv;
  1318. char *pub;
  1319. } keys[] = {
  1320. {
  1321. EVP_PKEY_HMAC, "0123456789", NULL
  1322. }, {
  1323. EVP_PKEY_POLY1305, "01234567890123456789012345678901", NULL
  1324. }, {
  1325. EVP_PKEY_SIPHASH, "0123456789012345", NULL
  1326. },
  1327. #ifndef OPENSSL_NO_EC
  1328. {
  1329. EVP_PKEY_X25519, "01234567890123456789012345678901",
  1330. "abcdefghijklmnopqrstuvwxyzabcdef"
  1331. }, {
  1332. EVP_PKEY_ED25519, "01234567890123456789012345678901",
  1333. "abcdefghijklmnopqrstuvwxyzabcdef"
  1334. }, {
  1335. EVP_PKEY_X448,
  1336. "01234567890123456789012345678901234567890123456789012345",
  1337. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd"
  1338. }, {
  1339. EVP_PKEY_ED448,
  1340. "012345678901234567890123456789012345678901234567890123456",
  1341. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde"
  1342. }
  1343. #endif
  1344. };
  1345. static int test_set_get_raw_keys_int(int tst, int pub, int uselibctx)
  1346. {
  1347. int ret = 0;
  1348. unsigned char buf[80];
  1349. unsigned char *in;
  1350. size_t inlen, len = 0;
  1351. EVP_PKEY *pkey;
  1352. if (nullprov != NULL)
  1353. return TEST_skip("Test does not support a non-default library context");
  1354. /* Check if this algorithm supports public keys */
  1355. if (keys[tst].pub == NULL)
  1356. return 1;
  1357. memset(buf, 0, sizeof(buf));
  1358. if (pub) {
  1359. inlen = strlen(keys[tst].pub);
  1360. in = (unsigned char *)keys[tst].pub;
  1361. if (uselibctx) {
  1362. pkey = EVP_PKEY_new_raw_public_key_ex(
  1363. testctx,
  1364. OBJ_nid2sn(keys[tst].type),
  1365. NULL,
  1366. in,
  1367. inlen);
  1368. } else {
  1369. pkey = EVP_PKEY_new_raw_public_key(keys[tst].type,
  1370. NULL,
  1371. in,
  1372. inlen);
  1373. }
  1374. } else {
  1375. inlen = strlen(keys[tst].priv);
  1376. in = (unsigned char *)keys[tst].priv;
  1377. if (uselibctx) {
  1378. pkey = EVP_PKEY_new_raw_private_key_ex(
  1379. testctx, OBJ_nid2sn(keys[tst].type),
  1380. NULL,
  1381. in,
  1382. inlen);
  1383. } else {
  1384. pkey = EVP_PKEY_new_raw_private_key(keys[tst].type,
  1385. NULL,
  1386. in,
  1387. inlen);
  1388. }
  1389. }
  1390. if (!TEST_ptr(pkey)
  1391. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len)))
  1392. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len)))
  1393. || !TEST_true(len == inlen)
  1394. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len)))
  1395. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len)))
  1396. || !TEST_mem_eq(in, inlen, buf, len))
  1397. goto done;
  1398. ret = 1;
  1399. done:
  1400. EVP_PKEY_free(pkey);
  1401. return ret;
  1402. }
  1403. static int test_set_get_raw_keys(int tst)
  1404. {
  1405. return test_set_get_raw_keys_int(tst, 0, 0)
  1406. && test_set_get_raw_keys_int(tst, 0, 1)
  1407. && test_set_get_raw_keys_int(tst, 1, 0)
  1408. && test_set_get_raw_keys_int(tst, 1, 1);
  1409. }
  1410. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1411. static int pkey_custom_check(EVP_PKEY *pkey)
  1412. {
  1413. return 0xbeef;
  1414. }
  1415. static int pkey_custom_pub_check(EVP_PKEY *pkey)
  1416. {
  1417. return 0xbeef;
  1418. }
  1419. static int pkey_custom_param_check(EVP_PKEY *pkey)
  1420. {
  1421. return 0xbeef;
  1422. }
  1423. static EVP_PKEY_METHOD *custom_pmeth;
  1424. #endif
  1425. static int test_EVP_PKEY_check(int i)
  1426. {
  1427. int ret = 0;
  1428. EVP_PKEY *pkey = NULL;
  1429. EVP_PKEY_CTX *ctx = NULL;
  1430. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1431. EVP_PKEY_CTX *ctx2 = NULL;
  1432. #endif
  1433. const APK_DATA *ak = &keycheckdata[i];
  1434. const unsigned char *input = ak->kder;
  1435. size_t input_len = ak->size;
  1436. int expected_id = ak->evptype;
  1437. int expected_check = ak->check;
  1438. int expected_pub_check = ak->pub_check;
  1439. int expected_param_check = ak->param_check;
  1440. int type = ak->type;
  1441. if (!TEST_ptr(pkey = load_example_key(ak->keytype, input, input_len)))
  1442. goto done;
  1443. if (type == 0
  1444. && !TEST_int_eq(EVP_PKEY_id(pkey), expected_id))
  1445. goto done;
  1446. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1447. goto done;
  1448. if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check))
  1449. goto done;
  1450. if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check))
  1451. goto done;
  1452. if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check))
  1453. goto done;
  1454. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1455. ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL);
  1456. /* assign the pkey directly, as an internal test */
  1457. EVP_PKEY_up_ref(pkey);
  1458. ctx2->pkey = pkey;
  1459. if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef))
  1460. goto done;
  1461. if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef))
  1462. goto done;
  1463. if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef))
  1464. goto done;
  1465. #endif
  1466. ret = 1;
  1467. done:
  1468. EVP_PKEY_CTX_free(ctx);
  1469. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1470. EVP_PKEY_CTX_free(ctx2);
  1471. #endif
  1472. EVP_PKEY_free(pkey);
  1473. return ret;
  1474. }
  1475. #ifndef OPENSSL_NO_CMAC
  1476. static int get_cmac_val(EVP_PKEY *pkey, unsigned char *mac)
  1477. {
  1478. EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  1479. const char msg[] = "Hello World";
  1480. size_t maclen;
  1481. int ret = 1;
  1482. if (!TEST_ptr(mdctx)
  1483. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, NULL, testctx,
  1484. testpropq, pkey, NULL))
  1485. || !TEST_true(EVP_DigestSignUpdate(mdctx, msg, sizeof(msg)))
  1486. || !TEST_true(EVP_DigestSignFinal(mdctx, mac, &maclen))
  1487. || !TEST_size_t_eq(maclen, AES_BLOCK_SIZE))
  1488. ret = 0;
  1489. EVP_MD_CTX_free(mdctx);
  1490. return ret;
  1491. }
  1492. static int test_CMAC_keygen(void)
  1493. {
  1494. static unsigned char key[] = {
  1495. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1496. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1497. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  1498. };
  1499. EVP_PKEY_CTX *kctx = NULL;
  1500. int ret = 0;
  1501. EVP_PKEY *pkey = NULL;
  1502. unsigned char mac[AES_BLOCK_SIZE];
  1503. # if !defined(OPENSSL_NO_DEPRECATED_3_0)
  1504. unsigned char mac2[AES_BLOCK_SIZE];
  1505. # endif
  1506. if (nullprov != NULL)
  1507. return TEST_skip("Test does not support a non-default library context");
  1508. /*
  1509. * This is a legacy method for CMACs, but should still work.
  1510. * This verifies that it works without an ENGINE.
  1511. */
  1512. kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_CMAC, NULL);
  1513. /* Test a CMAC key created using the "generated" method */
  1514. if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
  1515. || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
  1516. EVP_PKEY_CTRL_CIPHER,
  1517. 0, (void *)EVP_aes_256_ecb()), 0)
  1518. || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
  1519. EVP_PKEY_CTRL_SET_MAC_KEY,
  1520. sizeof(key), (void *)key), 0)
  1521. || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0)
  1522. || !TEST_ptr(pkey)
  1523. || !TEST_true(get_cmac_val(pkey, mac)))
  1524. goto done;
  1525. # if !defined(OPENSSL_NO_DEPRECATED_3_0)
  1526. EVP_PKEY_free(pkey);
  1527. /*
  1528. * Test a CMAC key using the direct method, and compare with the mac
  1529. * created above.
  1530. */
  1531. pkey = EVP_PKEY_new_CMAC_key(NULL, key, sizeof(key), EVP_aes_256_ecb());
  1532. if (!TEST_ptr(pkey)
  1533. || !TEST_true(get_cmac_val(pkey, mac2))
  1534. || !TEST_mem_eq(mac, sizeof(mac), mac2, sizeof(mac2)))
  1535. goto done;
  1536. # endif
  1537. ret = 1;
  1538. done:
  1539. EVP_PKEY_free(pkey);
  1540. EVP_PKEY_CTX_free(kctx);
  1541. return ret;
  1542. }
  1543. #endif
  1544. static int test_HKDF(void)
  1545. {
  1546. EVP_PKEY_CTX *pctx;
  1547. unsigned char out[20];
  1548. size_t outlen;
  1549. int i, ret = 0;
  1550. unsigned char salt[] = "0123456789";
  1551. unsigned char key[] = "012345678901234567890123456789";
  1552. unsigned char info[] = "infostring";
  1553. const unsigned char expected[] = {
  1554. 0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5,
  1555. 0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca
  1556. };
  1557. size_t expectedlen = sizeof(expected);
  1558. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
  1559. goto done;
  1560. /* We do this twice to test reuse of the EVP_PKEY_CTX */
  1561. for (i = 0; i < 2; i++) {
  1562. outlen = sizeof(out);
  1563. memset(out, 0, outlen);
  1564. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  1565. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  1566. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  1567. sizeof(salt) - 1), 0)
  1568. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  1569. sizeof(key) - 1), 0)
  1570. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  1571. sizeof(info) - 1), 0)
  1572. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  1573. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  1574. goto done;
  1575. }
  1576. ret = 1;
  1577. done:
  1578. EVP_PKEY_CTX_free(pctx);
  1579. return ret;
  1580. }
  1581. static int test_emptyikm_HKDF(void)
  1582. {
  1583. EVP_PKEY_CTX *pctx;
  1584. unsigned char out[20];
  1585. size_t outlen;
  1586. int ret = 0;
  1587. unsigned char salt[] = "9876543210";
  1588. unsigned char key[] = "";
  1589. unsigned char info[] = "stringinfo";
  1590. const unsigned char expected[] = {
  1591. 0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47,
  1592. 0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30
  1593. };
  1594. size_t expectedlen = sizeof(expected);
  1595. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
  1596. goto done;
  1597. outlen = sizeof(out);
  1598. memset(out, 0, outlen);
  1599. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  1600. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  1601. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  1602. sizeof(salt) - 1), 0)
  1603. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  1604. sizeof(key) - 1), 0)
  1605. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  1606. sizeof(info) - 1), 0)
  1607. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  1608. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  1609. goto done;
  1610. ret = 1;
  1611. done:
  1612. EVP_PKEY_CTX_free(pctx);
  1613. return ret;
  1614. }
  1615. #ifndef OPENSSL_NO_EC
  1616. static int test_X509_PUBKEY_inplace(void)
  1617. {
  1618. int ret = 0;
  1619. X509_PUBKEY *xp = NULL;
  1620. const unsigned char *p = kExampleECPubKeyDER;
  1621. size_t input_len = sizeof(kExampleECPubKeyDER);
  1622. if (!TEST_ptr(xp = d2i_X509_PUBKEY(NULL, &p, input_len)))
  1623. goto done;
  1624. if (!TEST_ptr(X509_PUBKEY_get0(xp)))
  1625. goto done;
  1626. p = kExampleBadECPubKeyDER;
  1627. input_len = sizeof(kExampleBadECPubKeyDER);
  1628. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
  1629. goto done;
  1630. if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
  1631. goto done;
  1632. ret = 1;
  1633. done:
  1634. X509_PUBKEY_free(xp);
  1635. return ret;
  1636. }
  1637. static int test_X509_PUBKEY_dup(void)
  1638. {
  1639. int ret = 0;
  1640. X509_PUBKEY *xp = NULL, *xq = NULL;
  1641. const unsigned char *p = kExampleECPubKeyDER;
  1642. size_t input_len = sizeof(kExampleECPubKeyDER);
  1643. if (!TEST_ptr(xp = d2i_X509_PUBKEY(NULL, &p, input_len))
  1644. || !TEST_ptr(xq = X509_PUBKEY_dup(xp))
  1645. || !TEST_ptr_ne(xp, xq))
  1646. goto done;
  1647. if (!TEST_ptr(X509_PUBKEY_get0(xq))
  1648. || !TEST_ptr(X509_PUBKEY_get0(xp))
  1649. || !TEST_ptr_eq(X509_PUBKEY_get0(xq), X509_PUBKEY_get0(xp)))
  1650. goto done;
  1651. X509_PUBKEY_free(xq);
  1652. xq = NULL;
  1653. p = kExampleBadECPubKeyDER;
  1654. input_len = sizeof(kExampleBadECPubKeyDER);
  1655. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len))
  1656. || !TEST_ptr(xq = X509_PUBKEY_dup(xp)))
  1657. goto done;
  1658. X509_PUBKEY_free(xp);
  1659. xp = NULL;
  1660. if (!TEST_true(X509_PUBKEY_get0(xq) == NULL))
  1661. goto done;
  1662. ret = 1;
  1663. done:
  1664. X509_PUBKEY_free(xp);
  1665. X509_PUBKEY_free(xq);
  1666. return ret;
  1667. }
  1668. #endif /* OPENSSL_NO_EC */
  1669. /* Test getting and setting parameters on an EVP_PKEY_CTX */
  1670. static int test_EVP_PKEY_CTX_get_set_params(EVP_PKEY *pkey)
  1671. {
  1672. EVP_MD_CTX *mdctx = NULL;
  1673. EVP_PKEY_CTX *ctx = NULL;
  1674. const OSSL_PARAM *params;
  1675. OSSL_PARAM ourparams[2], *param = ourparams, *param_md;
  1676. int ret = 0;
  1677. const EVP_MD *md;
  1678. char mdname[OSSL_MAX_NAME_SIZE];
  1679. char ssl3ms[48];
  1680. /* Initialise a sign operation */
  1681. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq);
  1682. if (!TEST_ptr(ctx)
  1683. || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0))
  1684. goto err;
  1685. /*
  1686. * We should be able to query the parameters now.
  1687. */
  1688. params = EVP_PKEY_CTX_settable_params(ctx);
  1689. if (!TEST_ptr(params)
  1690. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1691. OSSL_SIGNATURE_PARAM_DIGEST)))
  1692. goto err;
  1693. params = EVP_PKEY_CTX_gettable_params(ctx);
  1694. if (!TEST_ptr(params)
  1695. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1696. OSSL_SIGNATURE_PARAM_ALGORITHM_ID))
  1697. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1698. OSSL_SIGNATURE_PARAM_DIGEST)))
  1699. goto err;
  1700. /*
  1701. * Test getting and setting params via EVP_PKEY_CTX_set_params() and
  1702. * EVP_PKEY_CTX_get_params()
  1703. */
  1704. strcpy(mdname, "SHA512");
  1705. param_md = param;
  1706. *param++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1707. mdname, 0);
  1708. *param++ = OSSL_PARAM_construct_end();
  1709. if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
  1710. goto err;
  1711. mdname[0] = '\0';
  1712. *param_md = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1713. mdname, sizeof(mdname));
  1714. if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
  1715. || !TEST_str_eq(mdname, "SHA512"))
  1716. goto err;
  1717. /*
  1718. * Test the TEST_PKEY_CTX_set_signature_md() and
  1719. * TEST_PKEY_CTX_get_signature_md() functions
  1720. */
  1721. if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
  1722. || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
  1723. || !TEST_ptr_eq(md, EVP_sha256()))
  1724. goto err;
  1725. /*
  1726. * Test getting MD parameters via an associated EVP_PKEY_CTX
  1727. */
  1728. mdctx = EVP_MD_CTX_new();
  1729. if (!TEST_ptr(mdctx)
  1730. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", testctx, testpropq,
  1731. pkey, NULL)))
  1732. goto err;
  1733. /*
  1734. * We now have an EVP_MD_CTX with an EVP_PKEY_CTX inside it. We should be
  1735. * able to obtain the digest's settable parameters from the provider.
  1736. */
  1737. params = EVP_MD_CTX_settable_params(mdctx);
  1738. if (!TEST_ptr(params)
  1739. || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0)
  1740. /* The final key should be NULL */
  1741. || !TEST_ptr_null(params[1].key))
  1742. goto err;
  1743. param = ourparams;
  1744. memset(ssl3ms, 0, sizeof(ssl3ms));
  1745. *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  1746. ssl3ms, sizeof(ssl3ms));
  1747. *param++ = OSSL_PARAM_construct_end();
  1748. if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams)))
  1749. goto err;
  1750. ret = 1;
  1751. err:
  1752. EVP_MD_CTX_free(mdctx);
  1753. EVP_PKEY_CTX_free(ctx);
  1754. return ret;
  1755. }
  1756. #ifndef OPENSSL_NO_DSA
  1757. static int test_DSA_get_set_params(void)
  1758. {
  1759. OSSL_PARAM_BLD *bld = NULL;
  1760. OSSL_PARAM *params = NULL;
  1761. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  1762. EVP_PKEY_CTX *pctx = NULL;
  1763. EVP_PKEY *pkey = NULL;
  1764. int ret = 0;
  1765. /*
  1766. * Setup the parameters for our DSA object. For our purposes they don't
  1767. * have to actually be *valid* parameters. We just need to set something.
  1768. */
  1769. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "DSA", NULL))
  1770. || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
  1771. || !TEST_ptr(p = BN_new())
  1772. || !TEST_ptr(q = BN_new())
  1773. || !TEST_ptr(g = BN_new())
  1774. || !TEST_ptr(pub = BN_new())
  1775. || !TEST_ptr(priv = BN_new()))
  1776. goto err;
  1777. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  1778. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  1779. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  1780. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  1781. pub))
  1782. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  1783. priv)))
  1784. goto err;
  1785. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  1786. goto err;
  1787. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  1788. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  1789. params), 0))
  1790. goto err;
  1791. if (!TEST_ptr(pkey))
  1792. goto err;
  1793. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  1794. err:
  1795. EVP_PKEY_free(pkey);
  1796. EVP_PKEY_CTX_free(pctx);
  1797. OSSL_PARAM_free(params);
  1798. OSSL_PARAM_BLD_free(bld);
  1799. BN_free(p);
  1800. BN_free(q);
  1801. BN_free(g);
  1802. BN_free(pub);
  1803. BN_free(priv);
  1804. return ret;
  1805. }
  1806. /*
  1807. * Test combinations of private, public, missing and private + public key
  1808. * params to ensure they are all accepted
  1809. */
  1810. static int test_DSA_priv_pub(void)
  1811. {
  1812. return test_EVP_PKEY_ffc_priv_pub("DSA");
  1813. }
  1814. #endif /* !OPENSSL_NO_DSA */
  1815. static int test_RSA_get_set_params(void)
  1816. {
  1817. OSSL_PARAM_BLD *bld = NULL;
  1818. OSSL_PARAM *params = NULL;
  1819. BIGNUM *n = NULL, *e = NULL, *d = NULL;
  1820. EVP_PKEY_CTX *pctx = NULL;
  1821. EVP_PKEY *pkey = NULL;
  1822. int ret = 0;
  1823. /*
  1824. * Setup the parameters for our RSA object. For our purposes they don't
  1825. * have to actually be *valid* parameters. We just need to set something.
  1826. */
  1827. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", NULL))
  1828. || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
  1829. || !TEST_ptr(n = BN_new())
  1830. || !TEST_ptr(e = BN_new())
  1831. || !TEST_ptr(d = BN_new()))
  1832. goto err;
  1833. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
  1834. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
  1835. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)))
  1836. goto err;
  1837. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  1838. goto err;
  1839. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  1840. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  1841. params), 0))
  1842. goto err;
  1843. if (!TEST_ptr(pkey))
  1844. goto err;
  1845. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  1846. err:
  1847. EVP_PKEY_free(pkey);
  1848. EVP_PKEY_CTX_free(pctx);
  1849. OSSL_PARAM_free(params);
  1850. OSSL_PARAM_BLD_free(bld);
  1851. BN_free(n);
  1852. BN_free(e);
  1853. BN_free(d);
  1854. return ret;
  1855. }
  1856. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  1857. static int test_decrypt_null_chunks(void)
  1858. {
  1859. EVP_CIPHER_CTX* ctx = NULL;
  1860. EVP_CIPHER *cipher = NULL;
  1861. const unsigned char key[32] = {
  1862. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1863. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1864. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1
  1865. };
  1866. unsigned char iv[12] = {
  1867. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
  1868. };
  1869. unsigned char msg[] = "It was the best of times, it was the worst of times";
  1870. unsigned char ciphertext[80];
  1871. unsigned char plaintext[80];
  1872. /* We initialise tmp to a non zero value on purpose */
  1873. int ctlen, ptlen, tmp = 99;
  1874. int ret = 0;
  1875. const int enc_offset = 10, dec_offset = 20;
  1876. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "ChaCha20-Poly1305", testpropq))
  1877. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  1878. || !TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL,
  1879. key, iv))
  1880. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg,
  1881. enc_offset))
  1882. /* Deliberate add a zero length update */
  1883. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL,
  1884. 0))
  1885. || !TEST_int_eq(tmp, 0)
  1886. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp,
  1887. msg + enc_offset,
  1888. sizeof(msg) - enc_offset))
  1889. || !TEST_int_eq(ctlen += tmp, sizeof(msg))
  1890. || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp))
  1891. || !TEST_int_eq(tmp, 0))
  1892. goto err;
  1893. /* Deliberately initialise tmp to a non zero value */
  1894. tmp = 99;
  1895. if (!TEST_true(EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv))
  1896. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext,
  1897. dec_offset))
  1898. /*
  1899. * Deliberately add a zero length update. We also deliberately do
  1900. * this at a different offset than for encryption.
  1901. */
  1902. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL,
  1903. 0))
  1904. || !TEST_int_eq(tmp, 0)
  1905. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp,
  1906. ciphertext + dec_offset,
  1907. ctlen - dec_offset))
  1908. || !TEST_int_eq(ptlen += tmp, sizeof(msg))
  1909. || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp))
  1910. || !TEST_int_eq(tmp, 0)
  1911. || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen))
  1912. goto err;
  1913. ret = 1;
  1914. err:
  1915. EVP_CIPHER_CTX_free(ctx);
  1916. EVP_CIPHER_free(cipher);
  1917. return ret;
  1918. }
  1919. #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */
  1920. #ifndef OPENSSL_NO_DH
  1921. /*
  1922. * Test combinations of private, public, missing and private + public key
  1923. * params to ensure they are all accepted
  1924. */
  1925. static int test_DH_priv_pub(void)
  1926. {
  1927. return test_EVP_PKEY_ffc_priv_pub("DH");
  1928. }
  1929. # ifndef OPENSSL_NO_DEPRECATED_3_0
  1930. static int test_EVP_PKEY_set1_DH(void)
  1931. {
  1932. DH *x942dh = NULL, *noqdh = NULL;
  1933. EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
  1934. int ret = 0;
  1935. BIGNUM *p, *g = NULL;
  1936. if (!TEST_ptr(p = BN_new())
  1937. || !TEST_ptr(g = BN_new())
  1938. || !BN_set_word(p, 9999)
  1939. || !BN_set_word(g, 2)
  1940. || !TEST_ptr(noqdh = DH_new())
  1941. || !DH_set0_pqg(noqdh, p, NULL, g))
  1942. goto err;
  1943. p = g = NULL;
  1944. x942dh = DH_get_2048_256();
  1945. pkey1 = EVP_PKEY_new();
  1946. pkey2 = EVP_PKEY_new();
  1947. if (!TEST_ptr(x942dh)
  1948. || !TEST_ptr(noqdh)
  1949. || !TEST_ptr(pkey1)
  1950. || !TEST_ptr(pkey2))
  1951. goto err;
  1952. if(!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh))
  1953. || !TEST_int_eq(EVP_PKEY_id(pkey1), EVP_PKEY_DHX))
  1954. goto err;
  1955. if(!TEST_true(EVP_PKEY_set1_DH(pkey2, noqdh))
  1956. || !TEST_int_eq(EVP_PKEY_id(pkey2), EVP_PKEY_DH))
  1957. goto err;
  1958. ret = 1;
  1959. err:
  1960. BN_free(p);
  1961. BN_free(g);
  1962. EVP_PKEY_free(pkey1);
  1963. EVP_PKEY_free(pkey2);
  1964. DH_free(x942dh);
  1965. DH_free(noqdh);
  1966. return ret;
  1967. }
  1968. # endif /* !OPENSSL_NO_DEPRECATED_3_0 */
  1969. #endif /* !OPENSSL_NO_DH */
  1970. /*
  1971. * We test what happens with an empty template. For the sake of this test,
  1972. * the template must be ignored, and we know that's the case for RSA keys
  1973. * (this might arguably be a misfeature, but that's what we currently do,
  1974. * even in provider code, since that's how the legacy RSA implementation
  1975. * does things)
  1976. */
  1977. static int test_keygen_with_empty_template(int n)
  1978. {
  1979. EVP_PKEY_CTX *ctx = NULL;
  1980. EVP_PKEY *pkey = NULL;
  1981. EVP_PKEY *tkey = NULL;
  1982. int ret = 0;
  1983. if (nullprov != NULL)
  1984. return TEST_skip("Test does not support a non-default library context");
  1985. switch (n) {
  1986. case 0:
  1987. /* We do test with no template at all as well */
  1988. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)))
  1989. goto err;
  1990. break;
  1991. case 1:
  1992. /* Here we create an empty RSA key that serves as our template */
  1993. if (!TEST_ptr(tkey = EVP_PKEY_new())
  1994. || !TEST_true(EVP_PKEY_set_type(tkey, EVP_PKEY_RSA))
  1995. || !TEST_ptr(ctx = EVP_PKEY_CTX_new(tkey, NULL)))
  1996. goto err;
  1997. break;
  1998. }
  1999. if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  2000. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0))
  2001. goto err;
  2002. ret = 1;
  2003. err:
  2004. EVP_PKEY_CTX_free(ctx);
  2005. EVP_PKEY_free(pkey);
  2006. EVP_PKEY_free(tkey);
  2007. return ret;
  2008. }
  2009. /*
  2010. * Test that we fail if we attempt to use an algorithm that is not available
  2011. * in the current library context (unless we are using an algorithm that
  2012. * should be made available via legacy codepaths).
  2013. *
  2014. * 0: RSA
  2015. * 1: SM2
  2016. */
  2017. static int test_pkey_ctx_fail_without_provider(int tst)
  2018. {
  2019. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  2020. OSSL_PROVIDER *tmpnullprov = NULL;
  2021. EVP_PKEY_CTX *pctx = NULL;
  2022. const char *keytype = NULL;
  2023. int expect_null = 0;
  2024. int ret = 0;
  2025. if (!TEST_ptr(tmpctx))
  2026. goto err;
  2027. tmpnullprov = OSSL_PROVIDER_load(tmpctx, "null");
  2028. if (!TEST_ptr(tmpnullprov))
  2029. goto err;
  2030. /*
  2031. * We check for certain algos in the null provider.
  2032. * If an algo is expected to have a provider keymgmt, contructing an
  2033. * EVP_PKEY_CTX is expected to fail (return NULL).
  2034. * Otherwise, if it's expected to have legacy support, contructing an
  2035. * EVP_PKEY_CTX is expected to succeed (return non-NULL).
  2036. */
  2037. switch (tst) {
  2038. case 0:
  2039. keytype = "RSA";
  2040. expect_null = 1;
  2041. break;
  2042. case 1:
  2043. keytype = "SM2";
  2044. expect_null = 1;
  2045. #ifdef OPENSSL_NO_EC
  2046. TEST_info("EC disable, skipping SM2 check...");
  2047. goto end;
  2048. #endif
  2049. #ifdef OPENSSL_NO_SM2
  2050. TEST_info("SM2 disable, skipping SM2 check...");
  2051. goto end;
  2052. #endif
  2053. break;
  2054. default:
  2055. TEST_error("No test for case %d", tst);
  2056. goto err;
  2057. }
  2058. pctx = EVP_PKEY_CTX_new_from_name(tmpctx, keytype, "");
  2059. if (expect_null ? !TEST_ptr_null(pctx) : !TEST_ptr(pctx))
  2060. goto err;
  2061. #if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SM2)
  2062. end:
  2063. #endif
  2064. ret = 1;
  2065. err:
  2066. EVP_PKEY_CTX_free(pctx);
  2067. OSSL_PROVIDER_unload(tmpnullprov);
  2068. OSSL_LIB_CTX_free(tmpctx);
  2069. return ret;
  2070. }
  2071. static int test_rand_agglomeration(void)
  2072. {
  2073. EVP_RAND *rand;
  2074. EVP_RAND_CTX *ctx;
  2075. OSSL_PARAM params[3], *p = params;
  2076. int res;
  2077. unsigned int step = 7;
  2078. static unsigned char seed[] = "It does not matter how slowly you go "
  2079. "as long as you do not stop.";
  2080. unsigned char out[sizeof(seed)];
  2081. if (!TEST_int_ne(sizeof(seed) % step, 0)
  2082. || !TEST_ptr(rand = EVP_RAND_fetch(testctx, "TEST-RAND", testpropq)))
  2083. return 0;
  2084. ctx = EVP_RAND_CTX_new(rand, NULL);
  2085. EVP_RAND_free(rand);
  2086. if (!TEST_ptr(ctx))
  2087. return 0;
  2088. memset(out, 0, sizeof(out));
  2089. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  2090. seed, sizeof(seed));
  2091. *p++ = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_MAX_REQUEST, &step);
  2092. *p = OSSL_PARAM_construct_end();
  2093. res = TEST_true(EVP_RAND_CTX_set_params(ctx, params))
  2094. && TEST_true(EVP_RAND_generate(ctx, out, sizeof(out), 0, 1, NULL, 0))
  2095. && TEST_mem_eq(seed, sizeof(seed), out, sizeof(out));
  2096. EVP_RAND_CTX_free(ctx);
  2097. return res;
  2098. }
  2099. /*
  2100. * Test that we correctly return the original or "running" IV after
  2101. * an encryption operation.
  2102. * Run multiple times for some different relevant algorithms/modes.
  2103. */
  2104. static int test_evp_iv(int idx)
  2105. {
  2106. int ret = 0;
  2107. EVP_CIPHER_CTX *ctx = NULL;
  2108. unsigned char key[16] = {0x4c, 0x43, 0xdb, 0xdd, 0x42, 0x73, 0x47, 0xd1,
  2109. 0xe5, 0x62, 0x7d, 0xcd, 0x4d, 0x76, 0x4d, 0x57};
  2110. unsigned char init_iv[EVP_MAX_IV_LENGTH] =
  2111. {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  2112. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34};
  2113. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  2114. 9, 10, 11, 12, 13, 14, 15, 16 };
  2115. unsigned char ciphertext[32], oiv[16], iv[16];
  2116. unsigned char *ref_iv;
  2117. unsigned char cbc_state[16] = {0x10, 0x2f, 0x05, 0xcc, 0xc2, 0x55, 0x72, 0xb9,
  2118. 0x88, 0xe6, 0x4a, 0x17, 0x10, 0x74, 0x22, 0x5e};
  2119. unsigned char ofb_state[16] = {0x76, 0xe6, 0x66, 0x61, 0xd0, 0x8a, 0xe4, 0x64,
  2120. 0xdd, 0x66, 0xbf, 0x00, 0xf0, 0xe3, 0x6f, 0xfd};
  2121. unsigned char cfb_state[16] = {0x77, 0xe4, 0x65, 0x65, 0xd5, 0x8c, 0xe3, 0x6c,
  2122. 0xd4, 0x6c, 0xb4, 0x0c, 0xfd, 0xed, 0x60, 0xed};
  2123. unsigned char gcm_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  2124. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  2125. unsigned char ccm_state[7] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98};
  2126. #ifndef OPENSSL_NO_OCB
  2127. unsigned char ocb_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  2128. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  2129. #endif
  2130. int len = sizeof(ciphertext);
  2131. size_t ivlen, ref_len;
  2132. const EVP_CIPHER *type = NULL;
  2133. int iv_reset = 0;
  2134. if (nullprov != NULL && idx < 6)
  2135. return TEST_skip("Test does not support a non-default library context");
  2136. switch(idx) {
  2137. case 0:
  2138. type = EVP_aes_128_cbc();
  2139. /* FALLTHROUGH */
  2140. case 6:
  2141. type = (type != NULL) ? type :
  2142. EVP_CIPHER_fetch(testctx, "aes-128-cbc", testpropq);
  2143. ref_iv = cbc_state;
  2144. ref_len = sizeof(cbc_state);
  2145. iv_reset = 1;
  2146. break;
  2147. case 1:
  2148. type = EVP_aes_128_ofb();
  2149. /* FALLTHROUGH */
  2150. case 7:
  2151. type = (type != NULL) ? type :
  2152. EVP_CIPHER_fetch(testctx, "aes-128-ofb", testpropq);
  2153. ref_iv = ofb_state;
  2154. ref_len = sizeof(ofb_state);
  2155. iv_reset = 1;
  2156. break;
  2157. case 2:
  2158. type = EVP_aes_128_cfb();
  2159. /* FALLTHROUGH */
  2160. case 8:
  2161. type = (type != NULL) ? type :
  2162. EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq);
  2163. ref_iv = cfb_state;
  2164. ref_len = sizeof(cfb_state);
  2165. iv_reset = 1;
  2166. break;
  2167. case 3:
  2168. type = EVP_aes_128_gcm();
  2169. /* FALLTHROUGH */
  2170. case 9:
  2171. type = (type != NULL) ? type :
  2172. EVP_CIPHER_fetch(testctx, "aes-128-gcm", testpropq);
  2173. ref_iv = gcm_state;
  2174. ref_len = sizeof(gcm_state);
  2175. break;
  2176. case 4:
  2177. type = EVP_aes_128_ccm();
  2178. /* FALLTHROUGH */
  2179. case 10:
  2180. type = (type != NULL) ? type :
  2181. EVP_CIPHER_fetch(testctx, "aes-128-ccm", testpropq);
  2182. ref_iv = ccm_state;
  2183. ref_len = sizeof(ccm_state);
  2184. break;
  2185. #ifdef OPENSSL_NO_OCB
  2186. case 5:
  2187. case 11:
  2188. return 1;
  2189. #else
  2190. case 5:
  2191. type = EVP_aes_128_ocb();
  2192. /* FALLTHROUGH */
  2193. case 11:
  2194. type = (type != NULL) ? type :
  2195. EVP_CIPHER_fetch(testctx, "aes-128-ocb", testpropq);
  2196. ref_iv = ocb_state;
  2197. ref_len = sizeof(ocb_state);
  2198. break;
  2199. #endif
  2200. default:
  2201. return 0;
  2202. }
  2203. if (!TEST_ptr(type)
  2204. || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
  2205. || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
  2206. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
  2207. (int)sizeof(msg)))
  2208. || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
  2209. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
  2210. || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
  2211. goto err;
  2212. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  2213. if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
  2214. || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
  2215. goto err;
  2216. /* CBC, OFB, and CFB modes: the updated iv must be reset after reinit */
  2217. if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
  2218. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
  2219. goto err;
  2220. if (iv_reset) {
  2221. if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
  2222. goto err;
  2223. } else {
  2224. if (!TEST_mem_eq(ref_iv, ivlen, iv, ivlen))
  2225. goto err;
  2226. }
  2227. ret = 1;
  2228. err:
  2229. EVP_CIPHER_CTX_free(ctx);
  2230. if (idx >= 6)
  2231. EVP_CIPHER_free((EVP_CIPHER *)type);
  2232. return ret;
  2233. }
  2234. #ifndef OPENSSL_NO_EC
  2235. static int ecpub_nids[] = { NID_brainpoolP256r1, NID_X9_62_prime256v1,
  2236. NID_secp384r1, NID_secp521r1, NID_sect233k1, NID_sect233r1, NID_sect283r1,
  2237. NID_sect409k1, NID_sect409r1, NID_sect571k1, NID_sect571r1,
  2238. NID_brainpoolP384r1, NID_brainpoolP512r1};
  2239. static int test_ecpub(int idx)
  2240. {
  2241. int ret = 0, len, savelen;
  2242. int nid;
  2243. unsigned char buf[1024];
  2244. unsigned char *p;
  2245. EVP_PKEY *pkey = NULL;
  2246. EVP_PKEY_CTX *ctx = NULL;
  2247. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2248. const unsigned char *q;
  2249. EVP_PKEY *pkey2 = NULL;
  2250. EC_KEY *ec = NULL;
  2251. # endif
  2252. if (nullprov != NULL)
  2253. return TEST_skip("Test does not support a non-default library context");
  2254. nid = ecpub_nids[idx];
  2255. ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  2256. if (!TEST_ptr(ctx)
  2257. || !TEST_true(EVP_PKEY_keygen_init(ctx))
  2258. || !TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid))
  2259. || !TEST_true(EVP_PKEY_keygen(ctx, &pkey)))
  2260. goto done;
  2261. len = i2d_PublicKey(pkey, NULL);
  2262. savelen = len;
  2263. if (!TEST_int_ge(len, 1)
  2264. || !TEST_int_lt(len, 1024))
  2265. goto done;
  2266. p = buf;
  2267. len = i2d_PublicKey(pkey, &p);
  2268. if (!TEST_int_ge(len, 1)
  2269. || !TEST_int_eq(len, savelen))
  2270. goto done;
  2271. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2272. /* Now try to decode the just-created DER. */
  2273. q = buf;
  2274. if (!TEST_ptr((pkey2 = EVP_PKEY_new()))
  2275. || !TEST_ptr((ec = EC_KEY_new_by_curve_name(nid)))
  2276. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey2, ec)))
  2277. goto done;
  2278. /* EC_KEY ownership transferred */
  2279. ec = NULL;
  2280. if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_EC, &pkey2, &q, savelen)))
  2281. goto done;
  2282. /* The keys should match. */
  2283. if (!TEST_int_eq(EVP_PKEY_cmp(pkey, pkey2), 1))
  2284. goto done;
  2285. # endif
  2286. ret = 1;
  2287. done:
  2288. EVP_PKEY_CTX_free(ctx);
  2289. EVP_PKEY_free(pkey);
  2290. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2291. EVP_PKEY_free(pkey2);
  2292. EC_KEY_free(ec);
  2293. # endif
  2294. return ret;
  2295. }
  2296. #endif
  2297. static int test_EVP_rsa_pss_with_keygen_bits(void)
  2298. {
  2299. int ret = 0;
  2300. EVP_PKEY_CTX *ctx = NULL;
  2301. EVP_PKEY *pkey = NULL;
  2302. EVP_MD *md;
  2303. md = EVP_MD_fetch(testctx, "sha256", testpropq);
  2304. ret = TEST_ptr(md)
  2305. && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", testpropq)))
  2306. && TEST_true(EVP_PKEY_keygen_init(ctx))
  2307. && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 512), 0)
  2308. && TEST_true(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md))
  2309. && TEST_true(EVP_PKEY_keygen(ctx, &pkey));
  2310. EVP_MD_free(md);
  2311. EVP_PKEY_free(pkey);
  2312. EVP_PKEY_CTX_free(ctx);
  2313. return ret;
  2314. }
  2315. static int success = 1;
  2316. static void md_names(const char *name, void *vctx)
  2317. {
  2318. OSSL_LIB_CTX *ctx = (OSSL_LIB_CTX *)vctx;
  2319. /* Force a namemap update */
  2320. EVP_CIPHER *aes128 = EVP_CIPHER_fetch(ctx, "AES-128-CBC", NULL);
  2321. if (!TEST_ptr(aes128))
  2322. success = 0;
  2323. EVP_CIPHER_free(aes128);
  2324. }
  2325. /*
  2326. * Test that changing the namemap in a user callback works in a names_do_all
  2327. * function.
  2328. */
  2329. static int test_names_do_all(void)
  2330. {
  2331. /* We use a custom libctx so that we know the state of the namemap */
  2332. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  2333. EVP_MD *sha256 = NULL;
  2334. int testresult = 0;
  2335. if (!TEST_ptr(ctx))
  2336. goto err;
  2337. sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
  2338. if (!TEST_ptr(sha256))
  2339. goto err;
  2340. /*
  2341. * We loop through all the names for a given digest. This should still work
  2342. * even if the namemap changes part way through.
  2343. */
  2344. if (!TEST_true(EVP_MD_names_do_all(sha256, md_names, ctx)))
  2345. goto err;
  2346. if (!TEST_true(success))
  2347. goto err;
  2348. testresult = 1;
  2349. err:
  2350. EVP_MD_free(sha256);
  2351. OSSL_LIB_CTX_free(ctx);
  2352. return testresult;
  2353. }
  2354. typedef enum OPTION_choice {
  2355. OPT_ERR = -1,
  2356. OPT_EOF = 0,
  2357. OPT_CONTEXT,
  2358. OPT_TEST_ENUM
  2359. } OPTION_CHOICE;
  2360. const OPTIONS *test_get_options(void)
  2361. {
  2362. static const OPTIONS options[] = {
  2363. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  2364. { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" },
  2365. { NULL }
  2366. };
  2367. return options;
  2368. }
  2369. int setup_tests(void)
  2370. {
  2371. OPTION_CHOICE o;
  2372. while ((o = opt_next()) != OPT_EOF) {
  2373. switch (o) {
  2374. case OPT_CONTEXT:
  2375. /* Set up an alternate library context */
  2376. testctx = OSSL_LIB_CTX_new();
  2377. if (!TEST_ptr(testctx))
  2378. return 0;
  2379. /* Swap the libctx to test non-default context only */
  2380. nullprov = OSSL_PROVIDER_load(NULL, "null");
  2381. deflprov = OSSL_PROVIDER_load(testctx, "default");
  2382. lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
  2383. break;
  2384. case OPT_TEST_CASES:
  2385. break;
  2386. default:
  2387. return 0;
  2388. }
  2389. }
  2390. ADD_TEST(test_EVP_set_default_properties);
  2391. ADD_ALL_TESTS(test_EVP_DigestSignInit, 9);
  2392. ADD_TEST(test_EVP_DigestVerifyInit);
  2393. ADD_TEST(test_EVP_Digest);
  2394. ADD_TEST(test_EVP_Enveloped);
  2395. ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
  2396. ADD_TEST(test_privatekey_to_pkcs8);
  2397. #ifndef OPENSSL_NO_EC
  2398. ADD_TEST(test_EVP_PKCS82PKEY);
  2399. #endif
  2400. #ifndef OPENSSL_NO_EC
  2401. ADD_ALL_TESTS(test_EC_keygen_with_enc, OSSL_NELEM(ec_encodings));
  2402. #endif
  2403. #if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
  2404. ADD_TEST(test_EVP_SM2);
  2405. ADD_TEST(test_EVP_SM2_verify);
  2406. #endif
  2407. ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
  2408. #ifndef OPENSSL_NO_DEPRECATED_3_0
  2409. custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
  2410. if (!TEST_ptr(custom_pmeth))
  2411. return 0;
  2412. EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
  2413. EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
  2414. EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
  2415. if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
  2416. return 0;
  2417. #endif
  2418. ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
  2419. #ifndef OPENSSL_NO_CMAC
  2420. ADD_TEST(test_CMAC_keygen);
  2421. #endif
  2422. ADD_TEST(test_HKDF);
  2423. ADD_TEST(test_emptyikm_HKDF);
  2424. #ifndef OPENSSL_NO_EC
  2425. ADD_TEST(test_X509_PUBKEY_inplace);
  2426. ADD_TEST(test_X509_PUBKEY_dup);
  2427. ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
  2428. OSSL_NELEM(ec_der_pub_keys));
  2429. #endif
  2430. #ifndef OPENSSL_NO_DSA
  2431. ADD_TEST(test_DSA_get_set_params);
  2432. ADD_TEST(test_DSA_priv_pub);
  2433. #endif
  2434. ADD_TEST(test_RSA_get_set_params);
  2435. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  2436. ADD_TEST(test_decrypt_null_chunks);
  2437. #endif
  2438. #ifndef OPENSSL_NO_DH
  2439. ADD_TEST(test_DH_priv_pub);
  2440. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2441. ADD_TEST(test_EVP_PKEY_set1_DH);
  2442. # endif
  2443. #endif
  2444. #ifndef OPENSSL_NO_EC
  2445. ADD_TEST(test_EC_priv_pub);
  2446. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2447. ADD_TEST(test_EC_priv_only_legacy);
  2448. # endif
  2449. #endif
  2450. ADD_ALL_TESTS(test_keygen_with_empty_template, 2);
  2451. ADD_ALL_TESTS(test_pkey_ctx_fail_without_provider, 2);
  2452. ADD_TEST(test_rand_agglomeration);
  2453. ADD_ALL_TESTS(test_evp_iv, 12);
  2454. ADD_TEST(test_EVP_rsa_pss_with_keygen_bits);
  2455. #ifndef OPENSSL_NO_EC
  2456. ADD_ALL_TESTS(test_ecpub, OSSL_NELEM(ecpub_nids));
  2457. #endif
  2458. ADD_TEST(test_names_do_all);
  2459. return 1;
  2460. }
  2461. void cleanup_tests(void)
  2462. {
  2463. OSSL_PROVIDER_unload(nullprov);
  2464. OSSL_PROVIDER_unload(deflprov);
  2465. OSSL_PROVIDER_unload(lgcyprov);
  2466. OSSL_LIB_CTX_free(testctx);
  2467. }