evp_extra_test.c 76 KB

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