evp_extra_test.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. /*
  2. * Copyright 2015-2020 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/rsa.h>
  20. #include <openssl/x509.h>
  21. #include <openssl/pem.h>
  22. #include <openssl/kdf.h>
  23. #include <openssl/provider.h>
  24. #include <openssl/core_names.h>
  25. #include <openssl/params.h>
  26. #include <openssl/dsa.h>
  27. #include <openssl/dh.h>
  28. #include <openssl/aes.h>
  29. #include "testutil.h"
  30. #include "internal/nelem.h"
  31. #include "internal/sizes.h"
  32. #include "crypto/evp.h"
  33. #include "../e_os.h" /* strcasecmp */
  34. static OSSL_LIB_CTX *testctx = NULL;
  35. /*
  36. * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
  37. * should never use this key anywhere but in an example.
  38. */
  39. static const unsigned char kExampleRSAKeyDER[] = {
  40. 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
  41. 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
  42. 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
  43. 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
  44. 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
  45. 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
  46. 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
  47. 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
  48. 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
  49. 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
  50. 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
  51. 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  52. 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
  53. 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
  54. 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
  55. 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
  56. 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
  57. 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
  58. 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
  59. 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
  60. 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
  61. 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
  62. 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
  63. 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
  64. 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
  65. 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
  66. 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
  67. 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
  68. 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
  69. 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
  70. 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
  71. 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
  72. 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
  73. 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
  74. 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
  75. 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
  76. 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
  77. 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
  78. 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
  79. 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
  80. 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
  81. 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
  82. 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
  83. 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
  84. 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
  85. 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
  86. 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
  87. 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
  88. 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
  89. 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
  90. 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  91. };
  92. /*
  93. * kExampleDSAKeyDER is a DSA private key in ASN.1, DER format. Of course, you
  94. * should never use this key anywhere but in an example.
  95. */
  96. #ifndef OPENSSL_NO_DSA
  97. static const unsigned char kExampleDSAKeyDER[] = {
  98. 0x30, 0x82, 0x01, 0xba, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0x9a,
  99. 0x05, 0x6d, 0x33, 0xcd, 0x5d, 0x78, 0xa1, 0xbb, 0xcb, 0x7d, 0x5b, 0x8d,
  100. 0xb4, 0xcc, 0xbf, 0x03, 0x99, 0x64, 0xde, 0x38, 0x78, 0x06, 0x15, 0x2f,
  101. 0x86, 0x26, 0x77, 0xf3, 0xb1, 0x85, 0x00, 0xed, 0xfc, 0x28, 0x3a, 0x42,
  102. 0x4d, 0xab, 0xab, 0xdf, 0xbc, 0x9c, 0x16, 0xd0, 0x22, 0x50, 0xd1, 0x38,
  103. 0xdd, 0x3f, 0x64, 0x05, 0x9e, 0x68, 0x7a, 0x1e, 0xf1, 0x56, 0xbf, 0x1e,
  104. 0x2c, 0xc5, 0x97, 0x2a, 0xfe, 0x7a, 0x22, 0xdc, 0x6c, 0x68, 0xb8, 0x2e,
  105. 0x06, 0xdb, 0x41, 0xca, 0x98, 0xd8, 0x54, 0xc7, 0x64, 0x48, 0x24, 0x04,
  106. 0x20, 0xbc, 0x59, 0xe3, 0x6b, 0xea, 0x7e, 0xfc, 0x7e, 0xc5, 0x4e, 0xd4,
  107. 0xd8, 0x3a, 0xed, 0xcd, 0x5d, 0x99, 0xb8, 0x5c, 0xa2, 0x8b, 0xbb, 0x0b,
  108. 0xac, 0xe6, 0x8e, 0x25, 0x56, 0x22, 0x3a, 0x2d, 0x3a, 0x56, 0x41, 0x14,
  109. 0x1f, 0x1c, 0x8f, 0x53, 0x46, 0x13, 0x85, 0x02, 0x15, 0x00, 0x98, 0x7e,
  110. 0x92, 0x81, 0x88, 0xc7, 0x3f, 0x70, 0x49, 0x54, 0xf6, 0x76, 0xb4, 0xa3,
  111. 0x9e, 0x1d, 0x45, 0x98, 0x32, 0x7f, 0x02, 0x81, 0x80, 0x69, 0x4d, 0xef,
  112. 0x55, 0xff, 0x4d, 0x59, 0x2c, 0x01, 0xfa, 0x6a, 0x38, 0xe0, 0x70, 0x9f,
  113. 0x9e, 0x66, 0x8e, 0x3e, 0x8c, 0x52, 0x22, 0x9d, 0x15, 0x7e, 0x3c, 0xef,
  114. 0x4c, 0x7a, 0x61, 0x26, 0xe0, 0x2b, 0x81, 0x3f, 0xeb, 0xaf, 0x35, 0x38,
  115. 0x8d, 0xfe, 0xed, 0x46, 0xff, 0x5f, 0x03, 0x9b, 0x81, 0x92, 0xe7, 0x6f,
  116. 0x76, 0x4f, 0x1d, 0xd9, 0xbb, 0x89, 0xc9, 0x3e, 0xd9, 0x0b, 0xf9, 0xf4,
  117. 0x78, 0x11, 0x59, 0xc0, 0x1d, 0xcd, 0x0e, 0xa1, 0x6f, 0x15, 0xf1, 0x4d,
  118. 0xc1, 0xc9, 0x22, 0xed, 0x8d, 0xad, 0x67, 0xc5, 0x4b, 0x95, 0x93, 0x86,
  119. 0xa6, 0xaf, 0x8a, 0xee, 0x06, 0x89, 0x2f, 0x37, 0x7e, 0x64, 0xaa, 0xf6,
  120. 0xe7, 0xb1, 0x5a, 0x0a, 0x93, 0x95, 0x5d, 0x3e, 0x53, 0x9a, 0xde, 0x8a,
  121. 0xc2, 0x95, 0x45, 0x81, 0xbe, 0x5c, 0x2f, 0xc2, 0xb2, 0x92, 0x58, 0x19,
  122. 0x72, 0x80, 0xe9, 0x79, 0xa1, 0x02, 0x81, 0x80, 0x07, 0xd7, 0x62, 0xff,
  123. 0xdf, 0x1a, 0x3f, 0xed, 0x32, 0xd4, 0xd4, 0x88, 0x7b, 0x2c, 0x63, 0x7f,
  124. 0x97, 0xdc, 0x44, 0xd4, 0x84, 0xa2, 0xdd, 0x17, 0x16, 0x85, 0x13, 0xe0,
  125. 0xac, 0x51, 0x8d, 0x29, 0x1b, 0x75, 0x9a, 0xe4, 0xe3, 0x8a, 0x92, 0x69,
  126. 0x09, 0x03, 0xc5, 0x68, 0xae, 0x5e, 0x94, 0xfe, 0xc9, 0x92, 0x6c, 0x07,
  127. 0xb4, 0x1e, 0x64, 0x62, 0x87, 0xc6, 0xa4, 0xfd, 0x0d, 0x5f, 0xe5, 0xf9,
  128. 0x1b, 0x4f, 0x85, 0x5f, 0xae, 0xf3, 0x11, 0xe5, 0x18, 0xd4, 0x4d, 0x79,
  129. 0x9f, 0xc4, 0x79, 0x26, 0x04, 0x27, 0xf0, 0x0b, 0xee, 0x2b, 0x86, 0x9f,
  130. 0x86, 0x61, 0xe6, 0x51, 0xce, 0x04, 0x9b, 0x5d, 0x6b, 0x34, 0x43, 0x8c,
  131. 0x85, 0x3c, 0xf1, 0x51, 0x9b, 0x08, 0x23, 0x1b, 0xf5, 0x7e, 0x33, 0x12,
  132. 0xea, 0xab, 0x1f, 0xb7, 0x2d, 0xe2, 0x5f, 0xe6, 0x97, 0x99, 0xb5, 0x45,
  133. 0x16, 0x5b, 0xc3, 0x41, 0x02, 0x14, 0x61, 0xbf, 0x51, 0x60, 0xcf, 0xc8,
  134. 0xf1, 0x8c, 0x82, 0x97, 0xf2, 0xf4, 0x19, 0xba, 0x2b, 0xf3, 0x16, 0xbe,
  135. 0x40, 0x48
  136. };
  137. #endif
  138. /*
  139. * kExampleBadRSAKeyDER is an RSA private key in ASN.1, DER format. The private
  140. * components are not correct.
  141. */
  142. static const unsigned char kExampleBadRSAKeyDER[] = {
  143. 0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  144. 0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef,
  145. 0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9,
  146. 0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4,
  147. 0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3,
  148. 0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05,
  149. 0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26,
  150. 0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0,
  151. 0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33,
  152. 0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66,
  153. 0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde,
  154. 0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa,
  155. 0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53,
  156. 0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c,
  157. 0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75,
  158. 0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7,
  159. 0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c,
  160. 0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a,
  161. 0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87,
  162. 0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd,
  163. 0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0,
  164. 0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18,
  165. 0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  166. 0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5,
  167. 0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06,
  168. 0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0,
  169. 0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75,
  170. 0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1,
  171. 0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00,
  172. 0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd,
  173. 0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29,
  174. 0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5,
  175. 0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05,
  176. 0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e,
  177. 0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf,
  178. 0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d,
  179. 0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb,
  180. 0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5,
  181. 0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef,
  182. 0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9,
  183. 0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2,
  184. 0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7,
  185. 0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff,
  186. 0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99,
  187. 0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78,
  188. 0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb,
  189. 0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35,
  190. 0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46,
  191. 0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6,
  192. 0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f,
  193. 0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56,
  194. 0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d,
  195. 0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55,
  196. 0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20,
  197. 0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6,
  198. 0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81,
  199. 0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a,
  200. 0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e,
  201. 0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3,
  202. 0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1,
  203. 0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6,
  204. 0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77,
  205. 0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a,
  206. 0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d,
  207. 0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b,
  208. 0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a,
  209. 0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf,
  210. 0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28,
  211. 0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12,
  212. 0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd,
  213. 0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a,
  214. 0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5,
  215. 0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e,
  216. 0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b,
  217. 0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84,
  218. 0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e,
  219. 0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97,
  220. 0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81,
  221. 0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b,
  222. 0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6,
  223. 0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74,
  224. 0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e,
  225. 0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9,
  226. 0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98,
  227. 0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05,
  228. 0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef,
  229. 0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf,
  230. 0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69,
  231. 0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce,
  232. };
  233. static const unsigned char kMsg[] = { 1, 2, 3, 4 };
  234. static const unsigned char kSignature[] = {
  235. 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
  236. 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
  237. 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
  238. 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
  239. 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
  240. 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
  241. 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
  242. 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
  243. 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
  244. 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
  245. 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
  246. };
  247. /*
  248. * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
  249. * PrivateKeyInfo.
  250. */
  251. static const unsigned char kExampleRSAKeyPKCS8[] = {
  252. 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  253. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
  254. 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
  255. 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
  256. 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
  257. 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
  258. 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
  259. 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
  260. 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
  261. 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
  262. 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
  263. 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
  264. 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
  265. 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
  266. 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
  267. 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
  268. 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
  269. 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
  270. 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
  271. 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
  272. 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
  273. 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
  274. 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
  275. 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
  276. 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
  277. 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
  278. 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
  279. 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
  280. 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
  281. 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
  282. 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
  283. 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
  284. 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
  285. 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
  286. 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
  287. 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
  288. 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
  289. 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
  290. 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
  291. 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
  292. 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
  293. 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
  294. 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
  295. 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
  296. 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
  297. 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
  298. 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
  299. 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
  300. 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
  301. 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
  302. 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
  303. 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
  304. 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  305. };
  306. #ifndef OPENSSL_NO_EC
  307. /*
  308. * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
  309. * structure.
  310. */
  311. static const unsigned char kExampleECKeyDER[] = {
  312. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
  313. 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
  314. 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
  315. 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  316. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
  317. 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
  318. 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
  319. 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
  320. 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
  321. 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
  322. 0xc1,
  323. };
  324. /*
  325. * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
  326. * structure. The private key is equal to the order and will fail to import
  327. */
  328. static const unsigned char kExampleBadECKeyDER[] = {
  329. 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
  330. 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
  331. 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
  332. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  333. 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
  334. 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
  335. 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  336. 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  337. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
  338. };
  339. /* prime256v1 */
  340. static const unsigned char kExampleECPubKeyDER[] = {
  341. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  342. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  343. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  344. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  345. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  346. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  347. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  348. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  349. };
  350. /*
  351. * kExampleBadECPubKeyDER is a sample EC public key with a wrong OID
  352. * 1.2.840.10045.2.2 instead of 1.2.840.10045.2.1 - EC Public Key
  353. */
  354. static const unsigned char kExampleBadECPubKeyDER[] = {
  355. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  356. 0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  357. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  358. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  359. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  360. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  361. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  362. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  363. };
  364. static const unsigned char pExampleECParamDER[] = {
  365. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  366. };
  367. #endif
  368. typedef struct APK_DATA_st {
  369. const unsigned char *kder;
  370. size_t size;
  371. int evptype;
  372. int check;
  373. int pub_check;
  374. int param_check;
  375. int type; /* 0 for private, 1 for public, 2 for params */
  376. } APK_DATA;
  377. static APK_DATA keydata[] = {
  378. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA},
  379. {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA},
  380. #ifndef OPENSSL_NO_EC
  381. {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC}
  382. #endif
  383. };
  384. static APK_DATA keycheckdata[] = {
  385. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA, 1, 1, 1, 0},
  386. {kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), EVP_PKEY_RSA,
  387. 0, 1, 1, 0},
  388. #ifndef OPENSSL_NO_EC
  389. {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC, 1, 1, 1, 0},
  390. /* group is also associated in our pub key */
  391. {kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), EVP_PKEY_EC, 0, 1, 1, 1},
  392. {pExampleECParamDER, sizeof(pExampleECParamDER), EVP_PKEY_EC, 0, 0, 1, 2}
  393. #endif
  394. };
  395. static EVP_PKEY *load_example_rsa_key(void)
  396. {
  397. EVP_PKEY *ret = NULL;
  398. const unsigned char *derp = kExampleRSAKeyDER;
  399. EVP_PKEY *pkey = NULL;
  400. RSA *rsa = NULL;
  401. if (!TEST_true(d2i_RSAPrivateKey(&rsa, &derp, sizeof(kExampleRSAKeyDER))))
  402. return NULL;
  403. if (!TEST_ptr(pkey = EVP_PKEY_new())
  404. || !TEST_true(EVP_PKEY_set1_RSA(pkey, rsa)))
  405. goto end;
  406. ret = pkey;
  407. pkey = NULL;
  408. end:
  409. EVP_PKEY_free(pkey);
  410. RSA_free(rsa);
  411. return ret;
  412. }
  413. #ifndef OPENSSL_NO_DSA
  414. static EVP_PKEY *load_example_dsa_key(void)
  415. {
  416. EVP_PKEY *ret = NULL;
  417. const unsigned char *derp = kExampleDSAKeyDER;
  418. EVP_PKEY *pkey = NULL;
  419. DSA *dsa = NULL;
  420. if (!TEST_true(d2i_DSAPrivateKey(&dsa, &derp, sizeof(kExampleDSAKeyDER))))
  421. return NULL;
  422. if (!TEST_ptr(pkey = EVP_PKEY_new())
  423. || !TEST_true(EVP_PKEY_set1_DSA(pkey, dsa)))
  424. goto end;
  425. ret = pkey;
  426. pkey = NULL;
  427. end:
  428. EVP_PKEY_free(pkey);
  429. DSA_free(dsa);
  430. return ret;
  431. }
  432. #endif
  433. static EVP_PKEY *load_example_hmac_key(void)
  434. {
  435. EVP_PKEY *pkey = NULL;
  436. unsigned char key[] = {
  437. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  438. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  439. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  440. };
  441. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, key, sizeof(key));
  442. if (!TEST_ptr(pkey))
  443. return NULL;
  444. return pkey;
  445. }
  446. static int test_EVP_set_default_properties(void)
  447. {
  448. OSSL_LIB_CTX *ctx;
  449. EVP_MD *md = NULL;
  450. int res = 0;
  451. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
  452. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
  453. goto err;
  454. EVP_MD_free(md);
  455. md = NULL;
  456. if (!TEST_true(EVP_set_default_properties(ctx, "provider=fizzbang"))
  457. || !TEST_ptr_null(md = EVP_MD_fetch(ctx, "sha256", NULL))
  458. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", "-provider")))
  459. goto err;
  460. EVP_MD_free(md);
  461. md = NULL;
  462. if (!TEST_true(EVP_set_default_properties(ctx, NULL))
  463. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
  464. goto err;
  465. res = 1;
  466. err:
  467. EVP_MD_free(md);
  468. OSSL_LIB_CTX_free(ctx);
  469. return res;
  470. }
  471. static int test_EVP_Enveloped(void)
  472. {
  473. int ret = 0;
  474. EVP_CIPHER_CTX *ctx = NULL;
  475. EVP_PKEY *keypair = NULL;
  476. unsigned char *kek = NULL;
  477. unsigned char iv[EVP_MAX_IV_LENGTH];
  478. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  479. int len, kek_len, ciphertext_len, plaintext_len;
  480. unsigned char ciphertext[32], plaintext[16];
  481. const EVP_CIPHER *type = EVP_aes_256_cbc();
  482. if (!TEST_ptr(keypair = load_example_rsa_key())
  483. || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_size(keypair)))
  484. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  485. || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,
  486. &keypair, 1))
  487. || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,
  488. msg, sizeof(msg)))
  489. || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,
  490. &len)))
  491. goto err;
  492. ciphertext_len += len;
  493. if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))
  494. || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,
  495. ciphertext, ciphertext_len))
  496. || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))
  497. goto err;
  498. plaintext_len += len;
  499. if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))
  500. goto err;
  501. ret = 1;
  502. err:
  503. OPENSSL_free(kek);
  504. EVP_PKEY_free(keypair);
  505. EVP_CIPHER_CTX_free(ctx);
  506. return ret;
  507. }
  508. /*
  509. * Test 0: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, RSA)
  510. * Test 1: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, DSA)
  511. * Test 2: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, HMAC)
  512. * Test 3: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, RSA)
  513. * Test 4: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, DSA)
  514. * Test 5: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch diegst, HMAC)
  515. * Test 6: Use an MD BIO to do the Update calls instead (RSA)
  516. * Test 7: Use an MD BIO to do the Update calls instead (DSA)
  517. * Test 8: Use an MD BIO to do the Update calls instead (HMAC)
  518. */
  519. static int test_EVP_DigestSignInit(int tst)
  520. {
  521. int ret = 0;
  522. EVP_PKEY *pkey = NULL;
  523. unsigned char *sig = NULL;
  524. size_t sig_len = 0;
  525. EVP_MD_CTX *md_ctx = NULL, *md_ctx_verify = NULL;
  526. EVP_MD_CTX *a_md_ctx = NULL, *a_md_ctx_verify = NULL;
  527. BIO *mdbio = NULL, *membio = NULL;
  528. size_t written;
  529. const EVP_MD *md;
  530. EVP_MD *mdexp = NULL;
  531. if (tst >= 6) {
  532. membio = BIO_new(BIO_s_mem());
  533. mdbio = BIO_new(BIO_f_md());
  534. if (!TEST_ptr(membio) || !TEST_ptr(mdbio))
  535. goto out;
  536. BIO_push(mdbio, membio);
  537. if (!TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx), 0))
  538. goto out;
  539. } else {
  540. if (!TEST_ptr(a_md_ctx = md_ctx = EVP_MD_CTX_new())
  541. || !TEST_ptr(a_md_ctx_verify = md_ctx_verify = EVP_MD_CTX_new()))
  542. goto out;
  543. }
  544. if (tst == 0 || tst == 3 || tst == 6) {
  545. if (!TEST_ptr(pkey = load_example_rsa_key()))
  546. goto out;
  547. } else if (tst == 1 || tst == 4 || tst == 7) {
  548. #ifndef OPENSSL_NO_DSA
  549. if (!TEST_ptr(pkey = load_example_dsa_key()))
  550. goto out;
  551. #else
  552. ret = 1;
  553. goto out;
  554. #endif
  555. } else {
  556. if (!TEST_ptr(pkey = load_example_hmac_key()))
  557. goto out;
  558. }
  559. if (tst >= 3 && tst <= 5)
  560. md = mdexp = EVP_MD_fetch(NULL, "SHA256", NULL);
  561. else
  562. md = EVP_sha256();
  563. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, md, NULL, pkey)))
  564. goto out;
  565. if (tst >= 6) {
  566. if (!BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written))
  567. goto out;
  568. } else {
  569. if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  570. goto out;
  571. }
  572. /* Determine the size of the signature. */
  573. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
  574. || !TEST_ptr(sig = OPENSSL_malloc(sig_len))
  575. || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  576. goto out;
  577. if (tst >= 6) {
  578. if (!TEST_int_gt(BIO_reset(mdbio), 0)
  579. || !TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx_verify), 0))
  580. goto out;
  581. }
  582. /*
  583. * Ensure that the signature round-trips (Verification isn't supported for
  584. * HMAC via EVP_DigestVerify*)
  585. */
  586. if (tst != 2 && tst != 5 && tst != 8) {
  587. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, md,
  588. NULL, pkey)))
  589. goto out;
  590. if (tst >= 6) {
  591. if (!TEST_true(BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written)))
  592. goto out;
  593. } else {
  594. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg,
  595. sizeof(kMsg))))
  596. goto out;
  597. }
  598. if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  599. goto out;
  600. }
  601. ret = 1;
  602. out:
  603. BIO_free(membio);
  604. BIO_free(mdbio);
  605. EVP_MD_CTX_free(a_md_ctx);
  606. EVP_MD_CTX_free(a_md_ctx_verify);
  607. EVP_PKEY_free(pkey);
  608. OPENSSL_free(sig);
  609. EVP_MD_free(mdexp);
  610. return ret;
  611. }
  612. static int test_EVP_DigestVerifyInit(void)
  613. {
  614. int ret = 0;
  615. EVP_PKEY *pkey = NULL;
  616. EVP_MD_CTX *md_ctx = NULL;
  617. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
  618. || !TEST_ptr(pkey = load_example_rsa_key()))
  619. goto out;
  620. if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
  621. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
  622. || !TEST_true(EVP_DigestVerifyFinal(md_ctx, kSignature,
  623. sizeof(kSignature))))
  624. goto out;
  625. ret = 1;
  626. out:
  627. EVP_MD_CTX_free(md_ctx);
  628. EVP_PKEY_free(pkey);
  629. return ret;
  630. }
  631. static int test_d2i_AutoPrivateKey(int i)
  632. {
  633. int ret = 0;
  634. const unsigned char *p;
  635. EVP_PKEY *pkey = NULL;
  636. const APK_DATA *ak = &keydata[i];
  637. const unsigned char *input = ak->kder;
  638. size_t input_len = ak->size;
  639. int expected_id = ak->evptype;
  640. p = input;
  641. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  642. || !TEST_ptr_eq(p, input + input_len)
  643. || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id))
  644. goto done;
  645. ret = 1;
  646. done:
  647. EVP_PKEY_free(pkey);
  648. return ret;
  649. }
  650. #ifndef OPENSSL_NO_EC
  651. static const unsigned char ec_public_sect163k1_validxy[] = {
  652. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  653. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  654. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  655. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b,
  656. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  657. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  658. };
  659. static const unsigned char ec_public_sect163k1_badx[] = {
  660. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  661. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  662. 0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  663. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b,
  664. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  665. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  666. };
  667. static const unsigned char ec_public_sect163k1_bady[] = {
  668. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  669. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  670. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  671. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b,
  672. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  673. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6
  674. };
  675. static struct ec_der_pub_keys_st {
  676. const unsigned char *der;
  677. size_t len;
  678. int valid;
  679. } ec_der_pub_keys[] = {
  680. { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 },
  681. { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 },
  682. { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 },
  683. };
  684. /*
  685. * Tests the range of the decoded EC char2 public point.
  686. * See ec_GF2m_simple_oct2point().
  687. */
  688. static int test_invalide_ec_char2_pub_range_decode(int id)
  689. {
  690. int ret = 0;
  691. BIO *bio = NULL;
  692. EC_KEY *eckey = NULL;
  693. if (!TEST_ptr(bio = BIO_new_mem_buf(ec_der_pub_keys[id].der,
  694. ec_der_pub_keys[id].len)))
  695. goto err;
  696. eckey = d2i_EC_PUBKEY_bio(bio, NULL);
  697. ret = (ec_der_pub_keys[id].valid && TEST_ptr(eckey))
  698. || TEST_ptr_null(eckey);
  699. err:
  700. EC_KEY_free(eckey);
  701. BIO_free(bio);
  702. return ret;
  703. }
  704. /* Tests loading a bad key in PKCS8 format */
  705. static int test_EVP_PKCS82PKEY(void)
  706. {
  707. int ret = 0;
  708. const unsigned char *derp = kExampleBadECKeyDER;
  709. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  710. EVP_PKEY *pkey = NULL;
  711. if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp,
  712. sizeof(kExampleBadECKeyDER))))
  713. goto done;
  714. if (!TEST_ptr_eq(derp,
  715. kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)))
  716. goto done;
  717. if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf)))
  718. goto done;
  719. ret = 1;
  720. done:
  721. PKCS8_PRIV_KEY_INFO_free(p8inf);
  722. EVP_PKEY_free(pkey);
  723. return ret;
  724. }
  725. #endif
  726. /* This uses kExampleRSAKeyDER and kExampleRSAKeyPKCS8 to verify encoding */
  727. static int test_privatekey_to_pkcs8(void)
  728. {
  729. EVP_PKEY *pkey = NULL;
  730. BIO *membio = NULL;
  731. char *membuf = NULL;
  732. long membuf_len = 0;
  733. int ok = 0;
  734. if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
  735. || !TEST_ptr(pkey = load_example_rsa_key())
  736. || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
  737. NULL, 0, NULL, NULL),
  738. 0)
  739. || !TEST_int_gt(membuf_len = BIO_get_mem_data(membio, &membuf), 0)
  740. || !TEST_ptr(membuf)
  741. || !TEST_mem_eq(membuf, (size_t)membuf_len,
  742. kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8))
  743. /*
  744. * We try to write PEM as well, just to see that it doesn't err, but
  745. * assume that the result is correct.
  746. */
  747. || !TEST_int_gt(PEM_write_bio_PKCS8PrivateKey(membio, pkey, NULL,
  748. NULL, 0, NULL, NULL),
  749. 0))
  750. goto done;
  751. ok = 1;
  752. done:
  753. EVP_PKEY_free(pkey);
  754. BIO_free_all(membio);
  755. return ok;
  756. }
  757. #ifndef OPENSSL_NO_EC
  758. static const struct {
  759. int encoding;
  760. const char *encoding_name;
  761. } ec_encodings[] = {
  762. { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
  763. { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP }
  764. };
  765. static int ec_export_get_encoding_cb(const OSSL_PARAM params[], void *arg)
  766. {
  767. const OSSL_PARAM *p;
  768. const char *enc_name = NULL;
  769. int *enc = arg;
  770. size_t i;
  771. *enc = -1;
  772. if (!TEST_ptr(p = OSSL_PARAM_locate_const(params,
  773. OSSL_PKEY_PARAM_EC_ENCODING))
  774. || !TEST_true(OSSL_PARAM_get_utf8_string_ptr(p, &enc_name)))
  775. return 0;
  776. for (i = 0; i < OSSL_NELEM(ec_encodings); i++) {
  777. if (strcasecmp(enc_name, ec_encodings[i].encoding_name) == 0) {
  778. *enc = ec_encodings[i].encoding;
  779. break;
  780. }
  781. }
  782. return (*enc != -1);
  783. }
  784. static int test_EC_keygen_with_enc(int idx)
  785. {
  786. EVP_PKEY *params = NULL, *key = NULL;
  787. EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
  788. int enc;
  789. int ret = 0;
  790. enc = ec_encodings[idx].encoding;
  791. /* Create key parameters */
  792. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "EC", NULL))
  793. || !TEST_true(EVP_PKEY_paramgen_init(pctx))
  794. || !TEST_true(EVP_PKEY_CTX_set_group_name(pctx, "P-256"))
  795. || !TEST_true(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc))
  796. || !TEST_true(EVP_PKEY_paramgen(pctx, &params))
  797. || !TEST_ptr(params))
  798. goto done;
  799. /* Create key */
  800. if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx, params, NULL))
  801. || !TEST_true(EVP_PKEY_keygen_init(kctx))
  802. || !TEST_true(EVP_PKEY_keygen(kctx, &key))
  803. || !TEST_ptr(key))
  804. goto done;
  805. /* Check that the encoding got all the way into the key */
  806. if (!TEST_true(evp_keymgmt_util_export(key, OSSL_KEYMGMT_SELECT_ALL,
  807. ec_export_get_encoding_cb, &enc))
  808. || !TEST_int_eq(enc, ec_encodings[idx].encoding))
  809. goto done;
  810. ret = 1;
  811. done:
  812. EVP_PKEY_free(key);
  813. EVP_PKEY_free(params);
  814. EVP_PKEY_CTX_free(kctx);
  815. EVP_PKEY_CTX_free(pctx);
  816. return ret;
  817. }
  818. #endif
  819. #if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
  820. static int test_EVP_SM2_verify(void)
  821. {
  822. const char *pubkey =
  823. "-----BEGIN PUBLIC KEY-----\n"
  824. "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEp1KLWq1ZE2jmoAnnBJE1LBGxVr18\n"
  825. "YvvqECWCpXfAQ9qUJ+UmthnUPf0iM3SaXKHe6PlLIDyNlWMWb9RUh/yU3g==\n"
  826. "-----END PUBLIC KEY-----\n";
  827. const char *msg = "message digest";
  828. const char *id = "ALICE123@YAHOO.COM";
  829. const uint8_t signature[] = {
  830. 0x30, 0x44, 0x02, 0x20, 0x5b, 0xdb, 0xab, 0x81, 0x4f, 0xbb,
  831. 0x8b, 0x69, 0xb1, 0x05, 0x9c, 0x99, 0x3b, 0xb2, 0x45, 0x06,
  832. 0x4a, 0x30, 0x15, 0x59, 0x84, 0xcd, 0xee, 0x30, 0x60, 0x36,
  833. 0x57, 0x87, 0xef, 0x5c, 0xd0, 0xbe, 0x02, 0x20, 0x43, 0x8d,
  834. 0x1f, 0xc7, 0x77, 0x72, 0x39, 0xbb, 0x72, 0xe1, 0xfd, 0x07,
  835. 0x58, 0xd5, 0x82, 0xc8, 0x2d, 0xba, 0x3b, 0x2c, 0x46, 0x24,
  836. 0xe3, 0x50, 0xff, 0x04, 0xc7, 0xa0, 0x71, 0x9f, 0xa4, 0x70
  837. };
  838. int rc = 0;
  839. BIO *bio = NULL;
  840. EVP_PKEY *pkey = NULL;
  841. EVP_MD_CTX *mctx = NULL;
  842. EVP_PKEY_CTX *pctx = NULL;
  843. bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
  844. if (!TEST_true(bio != NULL))
  845. goto done;
  846. pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
  847. if (!TEST_true(pkey != NULL))
  848. goto done;
  849. if (!TEST_true(EVP_PKEY_is_a(pkey, "SM2")))
  850. goto done;
  851. if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
  852. goto done;
  853. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new(pkey, NULL)))
  854. goto done;
  855. EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
  856. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, EVP_sm3(), NULL, pkey)))
  857. goto done;
  858. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, id, strlen(id)), 0))
  859. goto done;
  860. if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
  861. goto done;
  862. if (!TEST_true(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature))))
  863. goto done;
  864. rc = 1;
  865. done:
  866. BIO_free(bio);
  867. EVP_PKEY_free(pkey);
  868. EVP_PKEY_CTX_free(pctx);
  869. EVP_MD_CTX_free(mctx);
  870. return rc;
  871. }
  872. static int test_EVP_SM2(void)
  873. {
  874. int ret = 0;
  875. EVP_PKEY *pkey = NULL;
  876. EVP_PKEY *pkeyparams = NULL;
  877. EVP_PKEY_CTX *pctx = NULL;
  878. EVP_PKEY_CTX *kctx = NULL;
  879. EVP_PKEY_CTX *sctx = NULL;
  880. size_t sig_len = 0;
  881. unsigned char *sig = NULL;
  882. EVP_MD_CTX *md_ctx = NULL;
  883. EVP_MD_CTX *md_ctx_verify = NULL;
  884. EVP_PKEY_CTX *cctx = NULL;
  885. uint8_t ciphertext[128];
  886. size_t ctext_len = sizeof(ciphertext);
  887. uint8_t plaintext[8];
  888. size_t ptext_len = sizeof(plaintext);
  889. uint8_t sm2_id[] = {1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r'};
  890. OSSL_PARAM sparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
  891. OSSL_PARAM gparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
  892. int i;
  893. char mdname[20];
  894. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL);
  895. if (!TEST_ptr(pctx))
  896. goto done;
  897. if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
  898. goto done;
  899. /* TODO is this even needed? */
  900. if (!TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)))
  901. goto done;
  902. if (!TEST_true(EVP_PKEY_paramgen(pctx, &pkeyparams)))
  903. goto done;
  904. kctx = EVP_PKEY_CTX_new(pkeyparams, NULL);
  905. if (!TEST_ptr(kctx))
  906. goto done;
  907. if (!TEST_true(EVP_PKEY_keygen_init(kctx)))
  908. goto done;
  909. if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
  910. goto done;
  911. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  912. goto done;
  913. if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
  914. goto done;
  915. if (!TEST_ptr(sctx = EVP_PKEY_CTX_new(pkey, NULL)))
  916. goto done;
  917. EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
  918. EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);
  919. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sm3(), NULL, pkey)))
  920. goto done;
  921. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  922. goto done;
  923. if(!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  924. goto done;
  925. /* Determine the size of the signature. */
  926. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
  927. goto done;
  928. if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
  929. goto done;
  930. if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  931. goto done;
  932. /* Ensure that the signature round-trips. */
  933. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sm3(), NULL, pkey)))
  934. goto done;
  935. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  936. goto done;
  937. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
  938. goto done;
  939. if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  940. goto done;
  941. /* now check encryption/decryption */
  942. gparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
  943. mdname, sizeof(mdname));
  944. for (i = 0; i < 2; i++) {
  945. EVP_PKEY_CTX_free(cctx);
  946. sparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
  947. i == 0 ? "SM3" : "SHA2-256",
  948. 0);
  949. if (!TEST_ptr(cctx = EVP_PKEY_CTX_new(pkey, NULL)))
  950. goto done;
  951. if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
  952. goto done;
  953. if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
  954. goto done;
  955. if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg,
  956. sizeof(kMsg))))
  957. goto done;
  958. if (!TEST_true(EVP_PKEY_decrypt_init(cctx)))
  959. goto done;
  960. if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
  961. goto done;
  962. if (!TEST_true(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext,
  963. ctext_len)))
  964. goto done;
  965. if (!TEST_true(EVP_PKEY_CTX_get_params(cctx, gparams)))
  966. goto done;
  967. /* Test we're still using the digest we think we are */
  968. if (i == 0 && !TEST_int_eq(strcmp(mdname, "SM3"), 0))
  969. goto done;
  970. if (i == 1 && !TEST_int_eq(strcmp(mdname, "SHA2-256"), 0))
  971. goto done;
  972. if (!TEST_true(ptext_len == sizeof(kMsg)))
  973. goto done;
  974. if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
  975. goto done;
  976. }
  977. ret = 1;
  978. done:
  979. EVP_PKEY_CTX_free(pctx);
  980. EVP_PKEY_CTX_free(kctx);
  981. EVP_PKEY_CTX_free(sctx);
  982. EVP_PKEY_CTX_free(cctx);
  983. EVP_PKEY_free(pkey);
  984. EVP_PKEY_free(pkeyparams);
  985. EVP_MD_CTX_free(md_ctx);
  986. EVP_MD_CTX_free(md_ctx_verify);
  987. OPENSSL_free(sig);
  988. return ret;
  989. }
  990. #endif
  991. static struct keys_st {
  992. int type;
  993. char *priv;
  994. char *pub;
  995. } keys[] = {
  996. {
  997. EVP_PKEY_HMAC, "0123456789", NULL
  998. }, {
  999. EVP_PKEY_POLY1305, "01234567890123456789012345678901", NULL
  1000. }, {
  1001. EVP_PKEY_SIPHASH, "0123456789012345", NULL
  1002. },
  1003. #ifndef OPENSSL_NO_EC
  1004. {
  1005. EVP_PKEY_X25519, "01234567890123456789012345678901",
  1006. "abcdefghijklmnopqrstuvwxyzabcdef"
  1007. }, {
  1008. EVP_PKEY_ED25519, "01234567890123456789012345678901",
  1009. "abcdefghijklmnopqrstuvwxyzabcdef"
  1010. }, {
  1011. EVP_PKEY_X448,
  1012. "01234567890123456789012345678901234567890123456789012345",
  1013. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd"
  1014. }, {
  1015. EVP_PKEY_ED448,
  1016. "012345678901234567890123456789012345678901234567890123456",
  1017. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde"
  1018. }
  1019. #endif
  1020. };
  1021. static int test_set_get_raw_keys_int(int tst, int pub, int uselibctx)
  1022. {
  1023. int ret = 0;
  1024. unsigned char buf[80];
  1025. unsigned char *in;
  1026. size_t inlen, len = 0;
  1027. EVP_PKEY *pkey;
  1028. /* Check if this algorithm supports public keys */
  1029. if (keys[tst].pub == NULL)
  1030. return 1;
  1031. memset(buf, 0, sizeof(buf));
  1032. if (pub) {
  1033. inlen = strlen(keys[tst].pub);
  1034. in = (unsigned char *)keys[tst].pub;
  1035. if (uselibctx) {
  1036. pkey = EVP_PKEY_new_raw_public_key_ex(
  1037. testctx,
  1038. OBJ_nid2sn(keys[tst].type),
  1039. NULL,
  1040. in,
  1041. inlen);
  1042. } else {
  1043. pkey = EVP_PKEY_new_raw_public_key(keys[tst].type,
  1044. NULL,
  1045. in,
  1046. inlen);
  1047. }
  1048. } else {
  1049. inlen = strlen(keys[tst].priv);
  1050. in = (unsigned char *)keys[tst].priv;
  1051. if (uselibctx) {
  1052. pkey = EVP_PKEY_new_raw_private_key_ex(
  1053. testctx, OBJ_nid2sn(keys[tst].type),
  1054. NULL,
  1055. in,
  1056. inlen);
  1057. } else {
  1058. pkey = EVP_PKEY_new_raw_private_key(keys[tst].type,
  1059. NULL,
  1060. in,
  1061. inlen);
  1062. }
  1063. }
  1064. if (!TEST_ptr(pkey)
  1065. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len)))
  1066. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len)))
  1067. || !TEST_true(len == inlen)
  1068. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len)))
  1069. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len)))
  1070. || !TEST_mem_eq(in, inlen, buf, len))
  1071. goto done;
  1072. ret = 1;
  1073. done:
  1074. EVP_PKEY_free(pkey);
  1075. return ret;
  1076. }
  1077. static int test_set_get_raw_keys(int tst)
  1078. {
  1079. return test_set_get_raw_keys_int(tst, 0, 0)
  1080. && test_set_get_raw_keys_int(tst, 0, 1)
  1081. && test_set_get_raw_keys_int(tst, 1, 0)
  1082. && test_set_get_raw_keys_int(tst, 1, 1);
  1083. }
  1084. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1085. static int pkey_custom_check(EVP_PKEY *pkey)
  1086. {
  1087. return 0xbeef;
  1088. }
  1089. static int pkey_custom_pub_check(EVP_PKEY *pkey)
  1090. {
  1091. return 0xbeef;
  1092. }
  1093. static int pkey_custom_param_check(EVP_PKEY *pkey)
  1094. {
  1095. return 0xbeef;
  1096. }
  1097. static EVP_PKEY_METHOD *custom_pmeth;
  1098. #endif
  1099. static int test_EVP_PKEY_check(int i)
  1100. {
  1101. int ret = 0;
  1102. const unsigned char *p;
  1103. EVP_PKEY *pkey = NULL;
  1104. #ifndef OPENSSL_NO_EC
  1105. EC_KEY *eckey = NULL;
  1106. #endif
  1107. EVP_PKEY_CTX *ctx = NULL;
  1108. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1109. EVP_PKEY_CTX *ctx2 = NULL;
  1110. #endif
  1111. const APK_DATA *ak = &keycheckdata[i];
  1112. const unsigned char *input = ak->kder;
  1113. size_t input_len = ak->size;
  1114. int expected_id = ak->evptype;
  1115. int expected_check = ak->check;
  1116. int expected_pub_check = ak->pub_check;
  1117. int expected_param_check = ak->param_check;
  1118. int type = ak->type;
  1119. BIO *pubkey = NULL;
  1120. p = input;
  1121. switch (type) {
  1122. case 0:
  1123. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  1124. || !TEST_ptr_eq(p, input + input_len)
  1125. || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id))
  1126. goto done;
  1127. break;
  1128. #ifndef OPENSSL_NO_EC
  1129. case 1:
  1130. if (!TEST_ptr(pubkey = BIO_new_mem_buf(input, input_len))
  1131. || !TEST_ptr(eckey = d2i_EC_PUBKEY_bio(pubkey, NULL))
  1132. || !TEST_ptr(pkey = EVP_PKEY_new())
  1133. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  1134. goto done;
  1135. break;
  1136. case 2:
  1137. if (!TEST_ptr(eckey = d2i_ECParameters(NULL, &p, input_len))
  1138. || !TEST_ptr_eq(p, input + input_len)
  1139. || !TEST_ptr(pkey = EVP_PKEY_new())
  1140. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  1141. goto done;
  1142. break;
  1143. #endif
  1144. default:
  1145. return 0;
  1146. }
  1147. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
  1148. goto done;
  1149. if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check))
  1150. goto done;
  1151. if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check))
  1152. goto done;
  1153. if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check))
  1154. goto done;
  1155. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1156. ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL);
  1157. /* assign the pkey directly, as an internal test */
  1158. EVP_PKEY_up_ref(pkey);
  1159. ctx2->pkey = pkey;
  1160. if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef))
  1161. goto done;
  1162. if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef))
  1163. goto done;
  1164. if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef))
  1165. goto done;
  1166. #endif
  1167. ret = 1;
  1168. done:
  1169. EVP_PKEY_CTX_free(ctx);
  1170. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1171. EVP_PKEY_CTX_free(ctx2);
  1172. #endif
  1173. EVP_PKEY_free(pkey);
  1174. BIO_free(pubkey);
  1175. return ret;
  1176. }
  1177. #ifndef OPENSSL_NO_CMAC
  1178. static int get_cmac_val(EVP_PKEY *pkey, unsigned char *mac)
  1179. {
  1180. EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  1181. const char msg[] = "Hello World";
  1182. size_t maclen;
  1183. int ret = 1;
  1184. if (!TEST_ptr(mdctx)
  1185. || !TEST_true(EVP_DigestSignInit(mdctx, NULL, NULL, NULL, pkey))
  1186. || !TEST_true(EVP_DigestSignUpdate(mdctx, msg, sizeof(msg)))
  1187. || !TEST_true(EVP_DigestSignFinal(mdctx, mac, &maclen))
  1188. || !TEST_size_t_eq(maclen, AES_BLOCK_SIZE))
  1189. ret = 0;
  1190. EVP_MD_CTX_free(mdctx);
  1191. return ret;
  1192. }
  1193. static int test_CMAC_keygen(void)
  1194. {
  1195. static unsigned char key[] = {
  1196. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1197. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1198. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  1199. };
  1200. /*
  1201. * This is a legacy method for CMACs, but should still work.
  1202. * This verifies that it works without an ENGINE.
  1203. */
  1204. EVP_PKEY_CTX *kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_CMAC, NULL);
  1205. int ret = 0;
  1206. EVP_PKEY *pkey = NULL;
  1207. unsigned char mac[AES_BLOCK_SIZE], mac2[AES_BLOCK_SIZE];
  1208. /* Test a CMAC key created using the "generated" method */
  1209. if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
  1210. || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
  1211. EVP_PKEY_CTRL_CIPHER,
  1212. 0, (void *)EVP_aes_256_ecb()), 0)
  1213. || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
  1214. EVP_PKEY_CTRL_SET_MAC_KEY,
  1215. sizeof(key), (void *)key), 0)
  1216. || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0)
  1217. || !TEST_ptr(pkey)
  1218. || !TEST_true(get_cmac_val(pkey, mac)))
  1219. goto done;
  1220. EVP_PKEY_free(pkey);
  1221. /*
  1222. * Test a CMAC key using the direct method, and compare with the mac
  1223. * created above.
  1224. */
  1225. pkey = EVP_PKEY_new_CMAC_key(NULL, key, sizeof(key), EVP_aes_256_ecb());
  1226. if (!TEST_ptr(pkey)
  1227. || !TEST_true(get_cmac_val(pkey, mac2))
  1228. || !TEST_mem_eq(mac, sizeof(mac), mac2, sizeof(mac2)))
  1229. goto done;
  1230. ret = 1;
  1231. done:
  1232. EVP_PKEY_free(pkey);
  1233. EVP_PKEY_CTX_free(kctx);
  1234. return ret;
  1235. }
  1236. #endif
  1237. static int test_HKDF(void)
  1238. {
  1239. EVP_PKEY_CTX *pctx;
  1240. unsigned char out[20];
  1241. size_t outlen;
  1242. int i, ret = 0;
  1243. unsigned char salt[] = "0123456789";
  1244. unsigned char key[] = "012345678901234567890123456789";
  1245. unsigned char info[] = "infostring";
  1246. const unsigned char expected[] = {
  1247. 0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5,
  1248. 0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca
  1249. };
  1250. size_t expectedlen = sizeof(expected);
  1251. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)))
  1252. goto done;
  1253. /* We do this twice to test reuse of the EVP_PKEY_CTX */
  1254. for (i = 0; i < 2; i++) {
  1255. outlen = sizeof(out);
  1256. memset(out, 0, outlen);
  1257. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  1258. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  1259. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  1260. sizeof(salt) - 1), 0)
  1261. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  1262. sizeof(key) - 1), 0)
  1263. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  1264. sizeof(info) - 1), 0)
  1265. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  1266. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  1267. goto done;
  1268. }
  1269. ret = 1;
  1270. done:
  1271. EVP_PKEY_CTX_free(pctx);
  1272. return ret;
  1273. }
  1274. static int test_emptyikm_HKDF(void)
  1275. {
  1276. EVP_PKEY_CTX *pctx;
  1277. unsigned char out[20];
  1278. size_t outlen;
  1279. int ret = 0;
  1280. unsigned char salt[] = "9876543210";
  1281. unsigned char key[] = "";
  1282. unsigned char info[] = "stringinfo";
  1283. const unsigned char expected[] = {
  1284. 0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47,
  1285. 0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30
  1286. };
  1287. size_t expectedlen = sizeof(expected);
  1288. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)))
  1289. goto done;
  1290. outlen = sizeof(out);
  1291. memset(out, 0, outlen);
  1292. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  1293. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  1294. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  1295. sizeof(salt) - 1), 0)
  1296. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  1297. sizeof(key) - 1), 0)
  1298. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  1299. sizeof(info) - 1), 0)
  1300. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  1301. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  1302. goto done;
  1303. ret = 1;
  1304. done:
  1305. EVP_PKEY_CTX_free(pctx);
  1306. return ret;
  1307. }
  1308. #ifndef OPENSSL_NO_EC
  1309. static int test_X509_PUBKEY_inplace(void)
  1310. {
  1311. int ret = 0;
  1312. X509_PUBKEY *xp = NULL;
  1313. const unsigned char *p = kExampleECPubKeyDER;
  1314. size_t input_len = sizeof(kExampleECPubKeyDER);
  1315. if (!TEST_ptr(xp = d2i_X509_PUBKEY(NULL, &p, input_len)))
  1316. goto done;
  1317. if (!TEST_ptr(X509_PUBKEY_get0(xp)))
  1318. goto done;
  1319. p = kExampleBadECPubKeyDER;
  1320. input_len = sizeof(kExampleBadECPubKeyDER);
  1321. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
  1322. goto done;
  1323. if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
  1324. goto done;
  1325. ret = 1;
  1326. done:
  1327. X509_PUBKEY_free(xp);
  1328. return ret;
  1329. }
  1330. #endif /* OPENSSL_NO_EC */
  1331. /* Test getting and setting parameters on an EVP_PKEY_CTX */
  1332. static int test_EVP_PKEY_CTX_get_set_params(EVP_PKEY *pkey)
  1333. {
  1334. EVP_MD_CTX *mdctx = NULL;
  1335. EVP_PKEY_CTX *ctx = NULL;
  1336. const OSSL_PARAM *params;
  1337. OSSL_PARAM ourparams[2], *param = ourparams, *param_md;
  1338. int ret = 0;
  1339. const EVP_MD *md;
  1340. char mdname[OSSL_MAX_NAME_SIZE];
  1341. char ssl3ms[48];
  1342. /* Initialise a sign operation */
  1343. ctx = EVP_PKEY_CTX_new(pkey, NULL);
  1344. if (!TEST_ptr(ctx)
  1345. || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0))
  1346. goto err;
  1347. /*
  1348. * We should be able to query the parameters now.
  1349. */
  1350. params = EVP_PKEY_CTX_settable_params(ctx);
  1351. if (!TEST_ptr(params)
  1352. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1353. OSSL_SIGNATURE_PARAM_DIGEST)))
  1354. goto err;
  1355. params = EVP_PKEY_CTX_gettable_params(ctx);
  1356. if (!TEST_ptr(params)
  1357. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1358. OSSL_SIGNATURE_PARAM_ALGORITHM_ID))
  1359. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  1360. OSSL_SIGNATURE_PARAM_DIGEST)))
  1361. goto err;
  1362. /*
  1363. * Test getting and setting params via EVP_PKEY_CTX_set_params() and
  1364. * EVP_PKEY_CTX_get_params()
  1365. */
  1366. strcpy(mdname, "SHA512");
  1367. param_md = param;
  1368. *param++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1369. mdname, 0);
  1370. *param++ = OSSL_PARAM_construct_end();
  1371. if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
  1372. goto err;
  1373. mdname[0] = '\0';
  1374. *param_md = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  1375. mdname, sizeof(mdname));
  1376. if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
  1377. || !TEST_str_eq(mdname, "SHA512"))
  1378. goto err;
  1379. /*
  1380. * Test the TEST_PKEY_CTX_set_signature_md() and
  1381. * TEST_PKEY_CTX_get_signature_md() functions
  1382. */
  1383. if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
  1384. || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
  1385. || !TEST_ptr_eq(md, EVP_sha256()))
  1386. goto err;
  1387. /*
  1388. * Test getting MD parameters via an associated EVP_PKEY_CTX
  1389. */
  1390. mdctx = EVP_MD_CTX_new();
  1391. if (!TEST_ptr(mdctx)
  1392. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", NULL, NULL,
  1393. pkey)))
  1394. goto err;
  1395. /*
  1396. * We now have an EVP_MD_CTX with an EVP_PKEY_CTX inside it. We should be
  1397. * able to obtain the digest's settable parameters from the provider.
  1398. */
  1399. params = EVP_MD_CTX_settable_params(mdctx);
  1400. if (!TEST_ptr(params)
  1401. || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0)
  1402. /* The final key should be NULL */
  1403. || !TEST_ptr_null(params[1].key))
  1404. goto err;
  1405. param = ourparams;
  1406. memset(ssl3ms, 0, sizeof(ssl3ms));
  1407. *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  1408. ssl3ms, sizeof(ssl3ms));
  1409. *param++ = OSSL_PARAM_construct_end();
  1410. if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams)))
  1411. goto err;
  1412. ret = 1;
  1413. err:
  1414. EVP_MD_CTX_free(mdctx);
  1415. EVP_PKEY_CTX_free(ctx);
  1416. return ret;
  1417. }
  1418. #ifndef OPENSSL_NO_DSA
  1419. static int test_DSA_get_set_params(void)
  1420. {
  1421. DSA *dsa = NULL;
  1422. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  1423. EVP_PKEY *pkey = NULL;
  1424. int ret = 0;
  1425. /*
  1426. * Setup the parameters for our DSA object. For our purposes they don't
  1427. * have to actually be *valid* parameters. We just need to set something.
  1428. */
  1429. dsa = DSA_new();
  1430. p = BN_new();
  1431. q = BN_new();
  1432. g = BN_new();
  1433. pub = BN_new();
  1434. priv = BN_new();
  1435. if (!TEST_ptr(dsa)
  1436. || !TEST_ptr(p)
  1437. || !TEST_ptr(q)
  1438. || !TEST_ptr(g)
  1439. || !TEST_ptr(pub)
  1440. || !DSA_set0_pqg(dsa, p, q, g)
  1441. || !DSA_set0_key(dsa, pub, priv))
  1442. goto err;
  1443. p = q = g = pub = priv = NULL;
  1444. pkey = EVP_PKEY_new();
  1445. if (!TEST_ptr(pkey)
  1446. || !TEST_true(EVP_PKEY_assign_DSA(pkey, dsa)))
  1447. goto err;
  1448. dsa = NULL;
  1449. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  1450. err:
  1451. EVP_PKEY_free(pkey);
  1452. DSA_free(dsa);
  1453. BN_free(p);
  1454. BN_free(q);
  1455. BN_free(g);
  1456. BN_free(pub);
  1457. BN_free(priv);
  1458. return ret;
  1459. }
  1460. #endif
  1461. static int test_RSA_get_set_params(void)
  1462. {
  1463. RSA *rsa = NULL;
  1464. BIGNUM *n = NULL, *e = NULL, *d = NULL;
  1465. EVP_PKEY *pkey = NULL;
  1466. int ret = 0;
  1467. /*
  1468. * Setup the parameters for our RSA object. For our purposes they don't
  1469. * have to actually be *valid* parameters. We just need to set something.
  1470. */
  1471. rsa = RSA_new();
  1472. n = BN_new();
  1473. e = BN_new();
  1474. d = BN_new();
  1475. if (!TEST_ptr(rsa)
  1476. || !TEST_ptr(n)
  1477. || !TEST_ptr(e)
  1478. || !TEST_ptr(d)
  1479. || !RSA_set0_key(rsa, n, e, d))
  1480. goto err;
  1481. n = e = d = NULL;
  1482. pkey = EVP_PKEY_new();
  1483. if (!TEST_ptr(pkey)
  1484. || !TEST_true(EVP_PKEY_assign_RSA(pkey, rsa)))
  1485. goto err;
  1486. rsa = NULL;
  1487. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  1488. err:
  1489. EVP_PKEY_free(pkey);
  1490. RSA_free(rsa);
  1491. BN_free(n);
  1492. BN_free(e);
  1493. BN_free(d);
  1494. return ret;
  1495. }
  1496. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  1497. static int test_decrypt_null_chunks(void)
  1498. {
  1499. EVP_CIPHER_CTX* ctx = NULL;
  1500. const unsigned char key[32] = {
  1501. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1502. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1503. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1
  1504. };
  1505. unsigned char iv[12] = {
  1506. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
  1507. };
  1508. unsigned char msg[] = "It was the best of times, it was the worst of times";
  1509. unsigned char ciphertext[80];
  1510. unsigned char plaintext[80];
  1511. /* We initialise tmp to a non zero value on purpose */
  1512. int ctlen, ptlen, tmp = 99;
  1513. int ret = 0;
  1514. const int enc_offset = 10, dec_offset = 20;
  1515. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  1516. || !TEST_true(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL,
  1517. key, iv))
  1518. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg,
  1519. enc_offset))
  1520. /* Deliberate add a zero length update */
  1521. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL,
  1522. 0))
  1523. || !TEST_int_eq(tmp, 0)
  1524. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp,
  1525. msg + enc_offset,
  1526. sizeof(msg) - enc_offset))
  1527. || !TEST_int_eq(ctlen += tmp, sizeof(msg))
  1528. || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp))
  1529. || !TEST_int_eq(tmp, 0))
  1530. goto err;
  1531. /* Deliberately initialise tmp to a non zero value */
  1532. tmp = 99;
  1533. if (!TEST_true(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, key,
  1534. iv))
  1535. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext,
  1536. dec_offset))
  1537. /*
  1538. * Deliberately add a zero length update. We also deliberately do
  1539. * this at a different offset than for encryption.
  1540. */
  1541. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL,
  1542. 0))
  1543. || !TEST_int_eq(tmp, 0)
  1544. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp,
  1545. ciphertext + dec_offset,
  1546. ctlen - dec_offset))
  1547. || !TEST_int_eq(ptlen += tmp, sizeof(msg))
  1548. || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp))
  1549. || !TEST_int_eq(tmp, 0)
  1550. || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen))
  1551. goto err;
  1552. ret = 1;
  1553. err:
  1554. EVP_CIPHER_CTX_free(ctx);
  1555. return ret;
  1556. }
  1557. #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */
  1558. #ifndef OPENSSL_NO_DH
  1559. static int test_EVP_PKEY_set1_DH(void)
  1560. {
  1561. DH *x942dh = NULL, *noqdh = NULL;
  1562. EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
  1563. int ret = 0;
  1564. BIGNUM *p, *g = NULL;
  1565. if (!TEST_ptr(p = BN_new())
  1566. || !TEST_ptr(g = BN_new())
  1567. || !BN_set_word(p, 9999)
  1568. || !BN_set_word(g, 2)
  1569. || !TEST_ptr(noqdh = DH_new())
  1570. || !DH_set0_pqg(noqdh, p, NULL, g))
  1571. goto err;
  1572. p = g = NULL;
  1573. x942dh = DH_get_2048_256();
  1574. pkey1 = EVP_PKEY_new();
  1575. pkey2 = EVP_PKEY_new();
  1576. if (!TEST_ptr(x942dh)
  1577. || !TEST_ptr(noqdh)
  1578. || !TEST_ptr(pkey1)
  1579. || !TEST_ptr(pkey2))
  1580. goto err;
  1581. if(!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh))
  1582. || !TEST_int_eq(EVP_PKEY_id(pkey1), EVP_PKEY_DHX))
  1583. goto err;
  1584. if(!TEST_true(EVP_PKEY_set1_DH(pkey2, noqdh))
  1585. || !TEST_int_eq(EVP_PKEY_id(pkey2), EVP_PKEY_DH))
  1586. goto err;
  1587. ret = 1;
  1588. err:
  1589. BN_free(p);
  1590. BN_free(g);
  1591. EVP_PKEY_free(pkey1);
  1592. EVP_PKEY_free(pkey2);
  1593. DH_free(x942dh);
  1594. DH_free(noqdh);
  1595. return ret;
  1596. }
  1597. #endif
  1598. /*
  1599. * We test what happens with an empty template. For the sake of this test,
  1600. * the template must be ignored, and we know that's the case for RSA keys
  1601. * (this might arguably be a misfeature, but that's what we currently do,
  1602. * even in provider code, since that's how the legacy RSA implementation
  1603. * does things)
  1604. */
  1605. static int test_keygen_with_empty_template(int n)
  1606. {
  1607. EVP_PKEY_CTX *ctx = NULL;
  1608. EVP_PKEY *pkey = NULL;
  1609. EVP_PKEY *tkey = NULL;
  1610. int ret = 0;
  1611. switch (n) {
  1612. case 0:
  1613. /* We do test with no template at all as well */
  1614. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)))
  1615. goto err;
  1616. break;
  1617. case 1:
  1618. /* Here we create an empty RSA key that serves as our template */
  1619. if (!TEST_ptr(tkey = EVP_PKEY_new())
  1620. || !TEST_true(EVP_PKEY_set_type(tkey, EVP_PKEY_RSA))
  1621. || !TEST_ptr(ctx = EVP_PKEY_CTX_new(tkey, NULL)))
  1622. goto err;
  1623. break;
  1624. }
  1625. if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  1626. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0))
  1627. goto err;
  1628. ret = 1;
  1629. err:
  1630. EVP_PKEY_CTX_free(ctx);
  1631. EVP_PKEY_free(pkey);
  1632. EVP_PKEY_free(tkey);
  1633. return ret;
  1634. }
  1635. /*
  1636. * Test that we fail if we attempt to use an algorithm that is not available
  1637. * in the current library context (unless we are using an algorithm that
  1638. * should be made available via legacy codepaths).
  1639. *
  1640. * 0: RSA
  1641. * 1: SM2
  1642. */
  1643. static int test_pkey_ctx_fail_without_provider(int tst)
  1644. {
  1645. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  1646. OSSL_PROVIDER *nullprov = NULL;
  1647. EVP_PKEY_CTX *pctx = NULL;
  1648. const char *keytype = NULL;
  1649. int expect_null = 0;
  1650. int ret = 0;
  1651. if (!TEST_ptr(tmpctx))
  1652. goto err;
  1653. nullprov = OSSL_PROVIDER_load(tmpctx, "null");
  1654. if (!TEST_ptr(nullprov))
  1655. goto err;
  1656. /*
  1657. * We check for certain algos in the null provider.
  1658. * If an algo is expected to have a provider keymgmt, contructing an
  1659. * EVP_PKEY_CTX is expected to fail (return NULL).
  1660. * Otherwise, if it's expected to have legacy support, contructing an
  1661. * EVP_PKEY_CTX is expected to succeed (return non-NULL).
  1662. */
  1663. switch (tst) {
  1664. case 0:
  1665. keytype = "RSA";
  1666. expect_null = 1;
  1667. break;
  1668. case 1:
  1669. keytype = "SM2";
  1670. expect_null = 1;
  1671. #ifdef OPENSSL_NO_EC
  1672. TEST_info("EC disable, skipping SM2 check...");
  1673. goto end;
  1674. #endif
  1675. #ifdef OPENSSL_NO_SM2
  1676. TEST_info("SM2 disable, skipping SM2 check...");
  1677. goto end;
  1678. #endif
  1679. break;
  1680. default:
  1681. TEST_error("No test for case %d", tst);
  1682. goto err;
  1683. }
  1684. pctx = EVP_PKEY_CTX_new_from_name(tmpctx, keytype, "");
  1685. if (expect_null ? !TEST_ptr_null(pctx) : !TEST_ptr(pctx))
  1686. goto err;
  1687. #if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SM2)
  1688. end:
  1689. #endif
  1690. ret = 1;
  1691. err:
  1692. EVP_PKEY_CTX_free(pctx);
  1693. OSSL_PROVIDER_unload(nullprov);
  1694. OSSL_LIB_CTX_free(tmpctx);
  1695. return ret;
  1696. }
  1697. static int test_rand_agglomeration(void)
  1698. {
  1699. EVP_RAND *rand;
  1700. EVP_RAND_CTX *ctx;
  1701. OSSL_PARAM params[3], *p = params;
  1702. int res;
  1703. unsigned int step = 7;
  1704. static unsigned char seed[] = "It does not matter how slowly you go "
  1705. "as long as you do not stop.";
  1706. unsigned char out[sizeof(seed)];
  1707. if (!TEST_int_ne(sizeof(seed) % step, 0)
  1708. || !TEST_ptr(rand = EVP_RAND_fetch(NULL, "TEST-RAND", NULL)))
  1709. return 0;
  1710. ctx = EVP_RAND_CTX_new(rand, NULL);
  1711. EVP_RAND_free(rand);
  1712. if (!TEST_ptr(ctx))
  1713. return 0;
  1714. memset(out, 0, sizeof(out));
  1715. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  1716. seed, sizeof(seed));
  1717. *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_MAX_REQUEST, &step);
  1718. *p = OSSL_PARAM_construct_end();
  1719. res = TEST_true(EVP_RAND_set_ctx_params(ctx, params))
  1720. && TEST_true(EVP_RAND_generate(ctx, out, sizeof(out), 0, 1, NULL, 0))
  1721. && TEST_mem_eq(seed, sizeof(seed), out, sizeof(out));
  1722. EVP_RAND_CTX_free(ctx);
  1723. return res;
  1724. }
  1725. /*
  1726. * Test that we correctly return the original or "running" IV after
  1727. * an encryption operation.
  1728. * Run multiple times for some different relevant algorithms/modes.
  1729. */
  1730. static int test_evp_iv(int idx)
  1731. {
  1732. int ret = 0;
  1733. EVP_CIPHER_CTX *ctx = NULL;
  1734. unsigned char key[16] = {0x4c, 0x43, 0xdb, 0xdd, 0x42, 0x73, 0x47, 0xd1,
  1735. 0xe5, 0x62, 0x7d, 0xcd, 0x4d, 0x76, 0x4d, 0x57};
  1736. unsigned char init_iv[EVP_MAX_IV_LENGTH] =
  1737. {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  1738. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34};
  1739. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  1740. 9, 10, 11, 12, 13, 14, 15, 16 };
  1741. unsigned char ciphertext[32], oiv[16], iv[16];
  1742. unsigned char *ref_iv;
  1743. unsigned char cbc_state[16] = {0x10, 0x2f, 0x05, 0xcc, 0xc2, 0x55, 0x72, 0xb9,
  1744. 0x88, 0xe6, 0x4a, 0x17, 0x10, 0x74, 0x22, 0x5e};
  1745. unsigned char ofb_state[16] = {0x76, 0xe6, 0x66, 0x61, 0xd0, 0x8a, 0xe4, 0x64,
  1746. 0xdd, 0x66, 0xbf, 0x00, 0xf0, 0xe3, 0x6f, 0xfd};
  1747. unsigned char gcm_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  1748. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  1749. unsigned char ccm_state[7] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98};
  1750. #ifndef OPENSSL_NO_OCB
  1751. unsigned char ocb_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  1752. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  1753. #endif
  1754. int len = sizeof(ciphertext);
  1755. size_t ivlen, ref_len;
  1756. const EVP_CIPHER *type = NULL;
  1757. switch(idx) {
  1758. case 0:
  1759. type = EVP_aes_128_cbc();
  1760. /* FALLTHROUGH */
  1761. case 5:
  1762. type = (type != NULL) ? type :
  1763. EVP_CIPHER_fetch(testctx, "aes-128-cbc", NULL);
  1764. ref_iv = cbc_state;
  1765. ref_len = sizeof(cbc_state);
  1766. break;
  1767. case 1:
  1768. type = EVP_aes_128_ofb();
  1769. /* FALLTHROUGH */
  1770. case 6:
  1771. type = (type != NULL) ? type :
  1772. EVP_CIPHER_fetch(testctx, "aes-128-ofb", NULL);
  1773. ref_iv = ofb_state;
  1774. ref_len = sizeof(ofb_state);
  1775. break;
  1776. case 2:
  1777. type = EVP_aes_128_gcm();
  1778. /* FALLTHROUGH */
  1779. case 7:
  1780. type = (type != NULL) ? type :
  1781. EVP_CIPHER_fetch(testctx, "aes-128-gcm", NULL);
  1782. ref_iv = gcm_state;
  1783. ref_len = sizeof(gcm_state);
  1784. break;
  1785. case 3:
  1786. type = EVP_aes_128_ccm();
  1787. /* FALLTHROUGH */
  1788. case 8:
  1789. type = (type != NULL) ? type :
  1790. EVP_CIPHER_fetch(testctx, "aes-128-ccm", NULL);
  1791. ref_iv = ccm_state;
  1792. ref_len = sizeof(ccm_state);
  1793. break;
  1794. #ifdef OPENSSL_NO_OCB
  1795. case 4:
  1796. case 9:
  1797. return 1;
  1798. #else
  1799. case 4:
  1800. type = EVP_aes_128_ocb();
  1801. /* FALLTHROUGH */
  1802. case 9:
  1803. type = (type != NULL) ? type :
  1804. EVP_CIPHER_fetch(testctx, "aes-128-ocb", NULL);
  1805. ref_iv = ocb_state;
  1806. ref_len = sizeof(ocb_state);
  1807. break;
  1808. #endif
  1809. default:
  1810. return 0;
  1811. }
  1812. if (!TEST_ptr(type)
  1813. || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
  1814. || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
  1815. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
  1816. (int)sizeof(msg)))
  1817. || !TEST_true(EVP_CIPHER_CTX_get_iv(ctx, oiv, sizeof(oiv)))
  1818. || !TEST_true(EVP_CIPHER_CTX_get_iv_state(ctx, iv, sizeof(iv)))
  1819. || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
  1820. goto err;
  1821. ivlen = EVP_CIPHER_CTX_iv_length(ctx);
  1822. if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
  1823. || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
  1824. goto err;
  1825. ret = 1;
  1826. err:
  1827. EVP_CIPHER_CTX_free(ctx);
  1828. if (idx >= 5)
  1829. EVP_CIPHER_free((EVP_CIPHER *)type);
  1830. return ret;
  1831. }
  1832. int setup_tests(void)
  1833. {
  1834. testctx = OSSL_LIB_CTX_new();
  1835. if (!TEST_ptr(testctx))
  1836. return 0;
  1837. ADD_TEST(test_EVP_set_default_properties);
  1838. ADD_ALL_TESTS(test_EVP_DigestSignInit, 9);
  1839. ADD_TEST(test_EVP_DigestVerifyInit);
  1840. ADD_TEST(test_EVP_Enveloped);
  1841. ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
  1842. ADD_TEST(test_privatekey_to_pkcs8);
  1843. #ifndef OPENSSL_NO_EC
  1844. ADD_TEST(test_EVP_PKCS82PKEY);
  1845. #endif
  1846. #ifndef OPENSSL_NO_EC
  1847. ADD_ALL_TESTS(test_EC_keygen_with_enc, OSSL_NELEM(ec_encodings));
  1848. #endif
  1849. #if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
  1850. ADD_TEST(test_EVP_SM2);
  1851. ADD_TEST(test_EVP_SM2_verify);
  1852. #endif
  1853. ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
  1854. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1855. custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
  1856. if (!TEST_ptr(custom_pmeth))
  1857. return 0;
  1858. EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
  1859. EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
  1860. EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
  1861. if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
  1862. return 0;
  1863. #endif
  1864. ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
  1865. #ifndef OPENSSL_NO_CMAC
  1866. ADD_TEST(test_CMAC_keygen);
  1867. #endif
  1868. ADD_TEST(test_HKDF);
  1869. ADD_TEST(test_emptyikm_HKDF);
  1870. #ifndef OPENSSL_NO_EC
  1871. ADD_TEST(test_X509_PUBKEY_inplace);
  1872. ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
  1873. OSSL_NELEM(ec_der_pub_keys));
  1874. #endif
  1875. #ifndef OPENSSL_NO_DSA
  1876. ADD_TEST(test_DSA_get_set_params);
  1877. #endif
  1878. ADD_TEST(test_RSA_get_set_params);
  1879. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  1880. ADD_TEST(test_decrypt_null_chunks);
  1881. #endif
  1882. #ifndef OPENSSL_NO_DH
  1883. ADD_TEST(test_EVP_PKEY_set1_DH);
  1884. #endif
  1885. ADD_ALL_TESTS(test_keygen_with_empty_template, 2);
  1886. ADD_ALL_TESTS(test_pkey_ctx_fail_without_provider, 2);
  1887. ADD_TEST(test_rand_agglomeration);
  1888. ADD_ALL_TESTS(test_evp_iv, 10);
  1889. return 1;
  1890. }
  1891. void cleanup_tests(void)
  1892. {
  1893. OSSL_LIB_CTX_free(testctx);
  1894. }