2
0

evp_extra_test.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. /*
  2. * Copyright 2015-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/aes.h>
  13. #include <openssl/bio.h>
  14. #include <openssl/crypto.h>
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/rsa.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/pem.h>
  20. #include <openssl/kdf.h>
  21. #include <openssl/dh.h>
  22. #include <openssl/engine.h>
  23. #include "testutil.h"
  24. #include "internal/nelem.h"
  25. #include "crypto/evp.h"
  26. /*
  27. * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
  28. * should never use this key anywhere but in an example.
  29. */
  30. static const unsigned char kExampleRSAKeyDER[] = {
  31. 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
  32. 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
  33. 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
  34. 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
  35. 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
  36. 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
  37. 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
  38. 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
  39. 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
  40. 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
  41. 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
  42. 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  43. 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
  44. 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
  45. 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
  46. 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
  47. 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
  48. 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
  49. 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
  50. 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
  51. 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
  52. 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
  53. 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
  54. 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
  55. 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
  56. 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
  57. 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
  58. 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
  59. 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
  60. 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
  61. 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
  62. 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
  63. 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
  64. 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
  65. 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
  66. 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
  67. 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
  68. 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
  69. 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
  70. 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
  71. 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
  72. 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
  73. 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
  74. 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
  75. 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
  76. 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
  77. 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
  78. 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
  79. 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
  80. 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
  81. 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  82. };
  83. /*
  84. * kExampleBadRSAKeyDER is an RSA private key in ASN.1, DER format. The private
  85. * components are not correct.
  86. */
  87. static const unsigned char kExampleBadRSAKeyDER[] = {
  88. 0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  89. 0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef,
  90. 0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9,
  91. 0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4,
  92. 0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3,
  93. 0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05,
  94. 0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26,
  95. 0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0,
  96. 0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33,
  97. 0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66,
  98. 0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde,
  99. 0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa,
  100. 0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53,
  101. 0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c,
  102. 0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75,
  103. 0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7,
  104. 0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c,
  105. 0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a,
  106. 0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87,
  107. 0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd,
  108. 0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0,
  109. 0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18,
  110. 0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  111. 0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5,
  112. 0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06,
  113. 0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0,
  114. 0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75,
  115. 0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1,
  116. 0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00,
  117. 0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd,
  118. 0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29,
  119. 0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5,
  120. 0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05,
  121. 0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e,
  122. 0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf,
  123. 0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d,
  124. 0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb,
  125. 0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5,
  126. 0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef,
  127. 0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9,
  128. 0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2,
  129. 0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7,
  130. 0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff,
  131. 0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99,
  132. 0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78,
  133. 0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb,
  134. 0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35,
  135. 0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46,
  136. 0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6,
  137. 0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f,
  138. 0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56,
  139. 0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d,
  140. 0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55,
  141. 0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20,
  142. 0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6,
  143. 0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81,
  144. 0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a,
  145. 0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e,
  146. 0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3,
  147. 0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1,
  148. 0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6,
  149. 0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77,
  150. 0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a,
  151. 0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d,
  152. 0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b,
  153. 0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a,
  154. 0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf,
  155. 0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28,
  156. 0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12,
  157. 0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd,
  158. 0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a,
  159. 0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5,
  160. 0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e,
  161. 0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b,
  162. 0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84,
  163. 0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e,
  164. 0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97,
  165. 0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81,
  166. 0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b,
  167. 0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6,
  168. 0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74,
  169. 0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e,
  170. 0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9,
  171. 0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98,
  172. 0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05,
  173. 0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef,
  174. 0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf,
  175. 0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69,
  176. 0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce,
  177. };
  178. static const unsigned char kMsg[] = { 1, 2, 3, 4 };
  179. static const unsigned char kSignature[] = {
  180. 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
  181. 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
  182. 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
  183. 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
  184. 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
  185. 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
  186. 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
  187. 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
  188. 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
  189. 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
  190. 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
  191. };
  192. /*
  193. * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
  194. * PrivateKeyInfo.
  195. */
  196. static const unsigned char kExampleRSAKeyPKCS8[] = {
  197. 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  198. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
  199. 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
  200. 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
  201. 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
  202. 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
  203. 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
  204. 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
  205. 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
  206. 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
  207. 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
  208. 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
  209. 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
  210. 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
  211. 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
  212. 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
  213. 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
  214. 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
  215. 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
  216. 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
  217. 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
  218. 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
  219. 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
  220. 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
  221. 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
  222. 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
  223. 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
  224. 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
  225. 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
  226. 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
  227. 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
  228. 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
  229. 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
  230. 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
  231. 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
  232. 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
  233. 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
  234. 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
  235. 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
  236. 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
  237. 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
  238. 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
  239. 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
  240. 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
  241. 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
  242. 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
  243. 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
  244. 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
  245. 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
  246. 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
  247. 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
  248. 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
  249. 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  250. };
  251. #ifndef OPENSSL_NO_EC
  252. /*
  253. * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
  254. * structure.
  255. */
  256. static const unsigned char kExampleECKeyDER[] = {
  257. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
  258. 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
  259. 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
  260. 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  261. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
  262. 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
  263. 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
  264. 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
  265. 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
  266. 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
  267. 0xc1,
  268. };
  269. /*
  270. * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
  271. * structure. The private key is equal to the order and will fail to import
  272. */
  273. static const unsigned char kExampleBadECKeyDER[] = {
  274. 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
  275. 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
  276. 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
  277. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  278. 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
  279. 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
  280. 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  281. 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  282. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
  283. };
  284. /* prime256v1 */
  285. static const unsigned char kExampleECPubKeyDER[] = {
  286. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  287. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  288. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  289. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  290. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  291. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  292. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  293. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  294. };
  295. /*
  296. * kExampleBadECPubKeyDER is a sample EC public key with a wrong OID
  297. * 1.2.840.10045.2.2 instead of 1.2.840.10045.2.1 - EC Public Key
  298. */
  299. static const unsigned char kExampleBadECPubKeyDER[] = {
  300. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  301. 0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  302. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  303. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  304. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  305. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  306. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  307. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  308. };
  309. static const unsigned char pExampleECParamDER[] = {
  310. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  311. };
  312. #endif
  313. static const unsigned char kCFBDefaultKey[] = {
  314. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88,
  315. 0x09, 0xCF, 0x4F, 0x3C
  316. };
  317. static const unsigned char kGCMDefaultKey[32] = { 0 };
  318. static const unsigned char kGCMResetKey[] = {
  319. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94,
  320. 0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  321. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  322. };
  323. static const unsigned char iCFBIV[] = {
  324. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  325. 0x0C, 0x0D, 0x0E, 0x0F
  326. };
  327. static const unsigned char iGCMDefaultIV[12] = { 0 };
  328. static const unsigned char iGCMResetIV1[] = {
  329. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad
  330. };
  331. static const unsigned char iGCMResetIV2[] = {
  332. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88
  333. };
  334. static const unsigned char cfbPlaintext[] = {
  335. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11,
  336. 0x73, 0x93, 0x17, 0x2A
  337. };
  338. static const unsigned char gcmDefaultPlaintext[16] = { 0 };
  339. static const unsigned char gcmResetPlaintext[] = {
  340. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5,
  341. 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  342. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95,
  343. 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  344. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39
  345. };
  346. static const unsigned char cfbCiphertext[] = {
  347. 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8,
  348. 0xE8, 0x3C, 0xFB, 0x4A
  349. };
  350. static const unsigned char gcmDefaultCiphertext[] = {
  351. 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3,
  352. 0xba, 0xf3, 0x9d, 0x18
  353. };
  354. static const unsigned char gcmResetCiphertext1[] = {
  355. 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32, 0xae, 0x47, 0xc1, 0x3b,
  356. 0xf1, 0x98, 0x44, 0xcb, 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
  357. 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0, 0xfe, 0xb5, 0x82, 0xd3,
  358. 0x39, 0x34, 0xa4, 0xf0, 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
  359. 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99, 0xf4, 0x7c, 0x9b, 0x1f
  360. };
  361. static const unsigned char gcmResetCiphertext2[] = {
  362. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3,
  363. 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  364. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48,
  365. 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  366. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62
  367. };
  368. static const unsigned char gcmAAD[] = {
  369. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce,
  370. 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2
  371. };
  372. static const unsigned char gcmDefaultTag[] = {
  373. 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0, 0x26, 0x5b, 0x98, 0xb5,
  374. 0xd4, 0x8a, 0xb9, 0x19
  375. };
  376. static const unsigned char gcmResetTag1[] = {
  377. 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4, 0x5e, 0x45, 0x49, 0x13,
  378. 0xfe, 0x2e, 0xa8, 0xf2
  379. };
  380. static const unsigned char gcmResetTag2[] = {
  381. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68, 0xcd, 0xdf, 0x88, 0x53,
  382. 0xbb, 0x2d, 0x55, 0x1b
  383. };
  384. typedef struct APK_DATA_st {
  385. const unsigned char *kder;
  386. size_t size;
  387. int evptype;
  388. int check;
  389. int pub_check;
  390. int param_check;
  391. int type; /* 0 for private, 1 for public, 2 for params */
  392. } APK_DATA;
  393. typedef struct {
  394. const char *cipher;
  395. const unsigned char *key;
  396. const unsigned char *iv;
  397. const unsigned char *input;
  398. const unsigned char *expected;
  399. const unsigned char *tag;
  400. size_t ivlen; /* 0 if we do not need to set a specific IV len */
  401. size_t inlen;
  402. size_t expectedlen;
  403. size_t taglen;
  404. int keyfirst;
  405. int initenc;
  406. int finalenc;
  407. } EVP_INIT_TEST_st;
  408. static const EVP_INIT_TEST_st evp_init_tests[] = {
  409. {
  410. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
  411. cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
  412. 0, 1, 0, 1
  413. },
  414. {
  415. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
  416. gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  417. sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
  418. sizeof(gcmDefaultTag), 1, 0, 1
  419. },
  420. {
  421. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
  422. cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
  423. 0, 0, 0, 1
  424. },
  425. {
  426. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
  427. gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  428. sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
  429. sizeof(gcmDefaultTag), 0, 0, 1
  430. },
  431. {
  432. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
  433. cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
  434. 0, 1, 1, 0
  435. },
  436. {
  437. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
  438. gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  439. sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
  440. sizeof(gcmDefaultTag), 1, 1, 0
  441. },
  442. {
  443. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
  444. cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
  445. 0, 0, 1, 0
  446. },
  447. {
  448. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
  449. gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  450. sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
  451. sizeof(gcmDefaultTag), 0, 1, 0
  452. }
  453. };
  454. static int evp_init_seq_set_iv(EVP_CIPHER_CTX *ctx, const EVP_INIT_TEST_st *t)
  455. {
  456. int res = 0;
  457. if (t->ivlen != 0) {
  458. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen, NULL)))
  459. goto err;
  460. }
  461. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv, -1)))
  462. goto err;
  463. res = 1;
  464. err:
  465. return res;
  466. }
  467. /*
  468. * Test step-wise cipher initialization via EVP_CipherInit_ex where the
  469. * arguments are given one at a time and a final adjustment to the enc
  470. * parameter sets the correct operation.
  471. */
  472. static int test_evp_init_seq(int idx)
  473. {
  474. int outlen1, outlen2;
  475. int testresult = 0;
  476. unsigned char outbuf[1024];
  477. unsigned char tag[16];
  478. const EVP_INIT_TEST_st *t = &evp_init_tests[idx];
  479. EVP_CIPHER_CTX *ctx = NULL;
  480. const EVP_CIPHER *type = NULL;
  481. size_t taglen = sizeof(tag);
  482. char *errmsg = NULL;
  483. ctx = EVP_CIPHER_CTX_new();
  484. if (ctx == NULL) {
  485. errmsg = "CTX_ALLOC";
  486. goto err;
  487. }
  488. if (!TEST_ptr(type = EVP_get_cipherbyname(t->cipher))) {
  489. errmsg = "GET_CIPHERBYNAME";
  490. goto err;
  491. }
  492. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, t->initenc))) {
  493. errmsg = "EMPTY_ENC_INIT";
  494. goto err;
  495. }
  496. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  497. errmsg = "PADDING";
  498. goto err;
  499. }
  500. if (t->keyfirst && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
  501. errmsg = "KEY_INIT (before iv)";
  502. goto err;
  503. }
  504. if (!evp_init_seq_set_iv(ctx, t)) {
  505. errmsg = "IV_INIT";
  506. goto err;
  507. }
  508. if (t->keyfirst == 0 && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
  509. errmsg = "KEY_INIT (after iv)";
  510. goto err;
  511. }
  512. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, t->finalenc))) {
  513. errmsg = "FINAL_ENC_INIT";
  514. goto err;
  515. }
  516. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  517. errmsg = "CIPHER_UPDATE";
  518. goto err;
  519. }
  520. if (t->finalenc == 0 && t->tag != NULL) {
  521. /* Set expected tag */
  522. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  523. t->taglen, (void *)t->tag))) {
  524. errmsg = "SET_TAG";
  525. goto err;
  526. }
  527. }
  528. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  529. errmsg = "CIPHER_FINAL";
  530. goto err;
  531. }
  532. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  533. errmsg = "WRONG_RESULT";
  534. goto err;
  535. }
  536. if (t->finalenc != 0 && t->tag != NULL) {
  537. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag))) {
  538. errmsg = "GET_TAG";
  539. goto err;
  540. }
  541. if (!TEST_mem_eq(t->tag, t->taglen, tag, taglen)) {
  542. errmsg = "TAG_ERROR";
  543. goto err;
  544. }
  545. }
  546. testresult = 1;
  547. err:
  548. if (errmsg != NULL)
  549. TEST_info("evp_init_test %d: %s", idx, errmsg);
  550. EVP_CIPHER_CTX_free(ctx);
  551. return testresult;
  552. }
  553. typedef struct {
  554. const unsigned char *input;
  555. const unsigned char *expected;
  556. size_t inlen;
  557. size_t expectedlen;
  558. int enc;
  559. } EVP_RESET_TEST_st;
  560. static const EVP_RESET_TEST_st evp_reset_tests[] = {
  561. {
  562. cfbPlaintext, cfbCiphertext,
  563. sizeof(cfbPlaintext), sizeof(cfbCiphertext), 1
  564. },
  565. {
  566. cfbCiphertext, cfbPlaintext,
  567. sizeof(cfbCiphertext), sizeof(cfbPlaintext), 0
  568. }
  569. };
  570. /*
  571. * Test a reset of a cipher via EVP_CipherInit_ex after the cipher has already
  572. * been used.
  573. */
  574. static int test_evp_reset(int idx)
  575. {
  576. const EVP_RESET_TEST_st *t = &evp_reset_tests[idx];
  577. int outlen1, outlen2;
  578. int testresult = 0;
  579. unsigned char outbuf[1024];
  580. EVP_CIPHER_CTX *ctx = NULL;
  581. const EVP_CIPHER *type = NULL;
  582. char *errmsg = NULL;
  583. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  584. errmsg = "CTX_ALLOC";
  585. goto err;
  586. }
  587. if (!TEST_ptr(type = EVP_get_cipherbyname("aes-128-cfb"))) {
  588. errmsg = "GET_CIPHERBYNAME";
  589. goto err;
  590. }
  591. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
  592. errmsg = "CIPHER_INIT";
  593. goto err;
  594. }
  595. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  596. errmsg = "PADDING";
  597. goto err;
  598. }
  599. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  600. errmsg = "CIPHER_UPDATE";
  601. goto err;
  602. }
  603. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  604. errmsg = "CIPHER_FINAL";
  605. goto err;
  606. }
  607. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  608. errmsg = "WRONG_RESULT";
  609. goto err;
  610. }
  611. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1))) {
  612. errmsg = "CIPHER_REINIT";
  613. goto err;
  614. }
  615. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  616. errmsg = "CIPHER_UPDATE (reinit)";
  617. goto err;
  618. }
  619. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  620. errmsg = "CIPHER_FINAL (reinit)";
  621. goto err;
  622. }
  623. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  624. errmsg = "WRONG_RESULT (reinit)";
  625. goto err;
  626. }
  627. testresult = 1;
  628. err:
  629. if (errmsg != NULL)
  630. TEST_info("test_evp_reset %d: %s", idx, errmsg);
  631. EVP_CIPHER_CTX_free(ctx);
  632. return testresult;
  633. }
  634. typedef struct {
  635. const unsigned char *iv1;
  636. const unsigned char *iv2;
  637. const unsigned char *expected1;
  638. const unsigned char *expected2;
  639. const unsigned char *tag1;
  640. const unsigned char *tag2;
  641. size_t ivlen1;
  642. size_t ivlen2;
  643. size_t expectedlen1;
  644. size_t expectedlen2;
  645. } TEST_GCM_IV_REINIT_st;
  646. static const TEST_GCM_IV_REINIT_st gcm_reinit_tests[] = {
  647. {
  648. iGCMResetIV1, iGCMResetIV2, gcmResetCiphertext1, gcmResetCiphertext2,
  649. gcmResetTag1, gcmResetTag2, sizeof(iGCMResetIV1), sizeof(iGCMResetIV2),
  650. sizeof(gcmResetCiphertext1), sizeof(gcmResetCiphertext2)
  651. },
  652. {
  653. iGCMResetIV2, iGCMResetIV1, gcmResetCiphertext2, gcmResetCiphertext1,
  654. gcmResetTag2, gcmResetTag1, sizeof(iGCMResetIV2), sizeof(iGCMResetIV1),
  655. sizeof(gcmResetCiphertext2), sizeof(gcmResetCiphertext1)
  656. }
  657. };
  658. static int test_gcm_reinit(int idx)
  659. {
  660. int outlen1, outlen2, outlen3;
  661. int testresult = 0;
  662. unsigned char outbuf[1024];
  663. unsigned char tag[16];
  664. const TEST_GCM_IV_REINIT_st *t = &gcm_reinit_tests[idx];
  665. EVP_CIPHER_CTX *ctx = NULL;
  666. const EVP_CIPHER *type = NULL;
  667. size_t taglen = sizeof(tag);
  668. char *errmsg = NULL;
  669. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  670. errmsg = "CTX_ALLOC";
  671. goto err;
  672. }
  673. if (!TEST_ptr(type = EVP_get_cipherbyname("aes-256-gcm"))) {
  674. errmsg = "GET_CIPHERBYNAME";
  675. goto err;
  676. }
  677. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, 1))) {
  678. errmsg = "ENC_INIT";
  679. goto err;
  680. }
  681. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen1, NULL))) {
  682. errmsg = "SET_IVLEN1";
  683. goto err;
  684. }
  685. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, kGCMResetKey, t->iv1, 1))) {
  686. errmsg = "SET_IV1";
  687. goto err;
  688. }
  689. if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
  690. errmsg = "AAD1";
  691. goto err;
  692. }
  693. EVP_CIPHER_CTX_set_padding(ctx, 0);
  694. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
  695. sizeof(gcmResetPlaintext)))) {
  696. errmsg = "CIPHER_UPDATE1";
  697. goto err;
  698. }
  699. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  700. errmsg = "CIPHER_FINAL1";
  701. goto err;
  702. }
  703. if (!TEST_mem_eq(t->expected1, t->expectedlen1, outbuf, outlen1 + outlen2)) {
  704. errmsg = "WRONG_RESULT1";
  705. goto err;
  706. }
  707. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag))) {
  708. errmsg = "GET_TAG1";
  709. goto err;
  710. }
  711. if (!TEST_mem_eq(t->tag1, taglen, tag, taglen)) {
  712. errmsg = "TAG_ERROR1";
  713. goto err;
  714. }
  715. /* Now reinit */
  716. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen2, NULL))) {
  717. errmsg = "SET_IVLEN2";
  718. goto err;
  719. }
  720. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv2, -1))) {
  721. errmsg = "SET_IV2";
  722. goto err;
  723. }
  724. if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
  725. errmsg = "AAD2";
  726. goto err;
  727. }
  728. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
  729. sizeof(gcmResetPlaintext)))) {
  730. errmsg = "CIPHER_UPDATE2";
  731. goto err;
  732. }
  733. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  734. errmsg = "CIPHER_FINAL2";
  735. goto err;
  736. }
  737. if (!TEST_mem_eq(t->expected2, t->expectedlen2, outbuf, outlen1 + outlen2)) {
  738. errmsg = "WRONG_RESULT2";
  739. goto err;
  740. }
  741. if (!TEST_true(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag))) {
  742. errmsg = "GET_TAG2";
  743. goto err;
  744. }
  745. if (!TEST_mem_eq(t->tag2, taglen, tag, taglen)) {
  746. errmsg = "TAG_ERROR2";
  747. goto err;
  748. }
  749. testresult = 1;
  750. err:
  751. if (errmsg != NULL)
  752. TEST_info("evp_init_test %d: %s", idx, errmsg);
  753. EVP_CIPHER_CTX_free(ctx);
  754. return testresult;
  755. }
  756. typedef struct {
  757. const char *cipher;
  758. int enc;
  759. } EVP_UPDATED_IV_TEST_st;
  760. static const EVP_UPDATED_IV_TEST_st evp_updated_iv_tests[] = {
  761. {
  762. "aes-128-cfb", 1
  763. },
  764. {
  765. "aes-128-cfb", 0
  766. },
  767. {
  768. "aes-128-cfb1", 1
  769. },
  770. {
  771. "aes-128-cfb1", 0
  772. },
  773. {
  774. "aes-128-cfb128", 1
  775. },
  776. {
  777. "aes-128-cfb128", 0
  778. },
  779. {
  780. "aes-128-cfb8", 1
  781. },
  782. {
  783. "aes-128-cfb8", 0
  784. },
  785. {
  786. "aes-128-ofb", 1
  787. },
  788. {
  789. "aes-128-ofb", 0
  790. },
  791. {
  792. "aes-128-ctr", 1
  793. },
  794. {
  795. "aes-128-ctr", 0
  796. },
  797. {
  798. "aes-128-cbc", 1
  799. },
  800. {
  801. "aes-128-cbc", 0
  802. }
  803. };
  804. /*
  805. * Test that the IV in the context is updated during a crypto operation for CFB
  806. * and OFB.
  807. */
  808. static int test_evp_updated_iv(int idx)
  809. {
  810. const EVP_UPDATED_IV_TEST_st *t = &evp_updated_iv_tests[idx];
  811. int outlen1, outlen2;
  812. int testresult = 0;
  813. unsigned char outbuf[1024];
  814. EVP_CIPHER_CTX *ctx = NULL;
  815. const EVP_CIPHER *type = NULL;
  816. const unsigned char *updated_iv;
  817. int iv_len;
  818. char *errmsg = NULL;
  819. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  820. errmsg = "CTX_ALLOC";
  821. goto err;
  822. }
  823. if ((type = EVP_get_cipherbyname(t->cipher)) == NULL) {
  824. TEST_info("cipher %s not supported, skipping", t->cipher);
  825. goto ok;
  826. }
  827. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
  828. errmsg = "CIPHER_INIT";
  829. goto err;
  830. }
  831. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  832. errmsg = "PADDING";
  833. goto err;
  834. }
  835. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, cfbPlaintext, sizeof(cfbPlaintext)))) {
  836. errmsg = "CIPHER_UPDATE";
  837. goto err;
  838. }
  839. if (!TEST_ptr(updated_iv = EVP_CIPHER_CTX_iv(ctx))) {
  840. errmsg = "CIPHER_CTX_IV";
  841. goto err;
  842. }
  843. if (!TEST_true(iv_len = EVP_CIPHER_CTX_iv_length(ctx))) {
  844. errmsg = "CIPHER_CTX_IV_LEN";
  845. goto err;
  846. }
  847. if (!TEST_mem_ne(iCFBIV, sizeof(iCFBIV), updated_iv, iv_len)) {
  848. errmsg = "IV_NOT_UPDATED";
  849. goto err;
  850. }
  851. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  852. errmsg = "CIPHER_FINAL";
  853. goto err;
  854. }
  855. ok:
  856. testresult = 1;
  857. err:
  858. if (errmsg != NULL)
  859. TEST_info("test_evp_updated_iv %d: %s", idx, errmsg);
  860. EVP_CIPHER_CTX_free(ctx);
  861. return testresult;
  862. }
  863. static APK_DATA keydata[] = {
  864. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA},
  865. {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA},
  866. #ifndef OPENSSL_NO_EC
  867. {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC}
  868. #endif
  869. };
  870. static APK_DATA keycheckdata[] = {
  871. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA, 1, -2, -2, 0},
  872. {kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), EVP_PKEY_RSA,
  873. 0, -2, -2, 0},
  874. #ifndef OPENSSL_NO_EC
  875. {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC, 1, 1, 1, 0},
  876. /* group is also associated in our pub key */
  877. {kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), EVP_PKEY_EC, 0, 1, 1, 1},
  878. {pExampleECParamDER, sizeof(pExampleECParamDER), EVP_PKEY_EC, 0, 0, 1, 2}
  879. #endif
  880. };
  881. static EVP_PKEY *load_example_rsa_key(void)
  882. {
  883. EVP_PKEY *ret = NULL;
  884. const unsigned char *derp = kExampleRSAKeyDER;
  885. EVP_PKEY *pkey = NULL;
  886. RSA *rsa = NULL;
  887. if (!TEST_true(d2i_RSAPrivateKey(&rsa, &derp, sizeof(kExampleRSAKeyDER))))
  888. return NULL;
  889. if (!TEST_ptr(pkey = EVP_PKEY_new())
  890. || !TEST_true(EVP_PKEY_set1_RSA(pkey, rsa)))
  891. goto end;
  892. ret = pkey;
  893. pkey = NULL;
  894. end:
  895. EVP_PKEY_free(pkey);
  896. RSA_free(rsa);
  897. return ret;
  898. }
  899. static int test_EVP_Enveloped(void)
  900. {
  901. int ret = 0;
  902. EVP_CIPHER_CTX *ctx = NULL;
  903. EVP_PKEY *keypair = NULL;
  904. unsigned char *kek = NULL;
  905. unsigned char iv[EVP_MAX_IV_LENGTH];
  906. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  907. int len, kek_len, ciphertext_len, plaintext_len;
  908. unsigned char ciphertext[32], plaintext[16];
  909. const EVP_CIPHER *type = EVP_aes_256_cbc();
  910. if (!TEST_ptr(keypair = load_example_rsa_key())
  911. || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_size(keypair)))
  912. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  913. || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,
  914. &keypair, 1))
  915. || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,
  916. msg, sizeof(msg)))
  917. || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,
  918. &len)))
  919. goto err;
  920. ciphertext_len += len;
  921. if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))
  922. || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,
  923. ciphertext, ciphertext_len))
  924. || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))
  925. goto err;
  926. plaintext_len += len;
  927. if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))
  928. goto err;
  929. ret = 1;
  930. err:
  931. OPENSSL_free(kek);
  932. EVP_PKEY_free(keypair);
  933. EVP_CIPHER_CTX_free(ctx);
  934. return ret;
  935. }
  936. static int test_EVP_DigestSignInit(void)
  937. {
  938. int ret = 0;
  939. EVP_PKEY *pkey = NULL;
  940. unsigned char *sig = NULL;
  941. size_t sig_len = 0;
  942. EVP_MD_CTX *md_ctx, *md_ctx_verify = NULL;
  943. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
  944. || !TEST_ptr(md_ctx_verify = EVP_MD_CTX_new())
  945. || !TEST_ptr(pkey = load_example_rsa_key()))
  946. goto out;
  947. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
  948. || !TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  949. goto out;
  950. /* Determine the size of the signature. */
  951. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
  952. || !TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey)))
  953. goto out;
  954. if (!TEST_ptr(sig = OPENSSL_malloc(sig_len))
  955. || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  956. goto out;
  957. /* Ensure that the signature round-trips. */
  958. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sha256(),
  959. NULL, pkey))
  960. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify,
  961. kMsg, sizeof(kMsg)))
  962. || !TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  963. goto out;
  964. ret = 1;
  965. out:
  966. EVP_MD_CTX_free(md_ctx);
  967. EVP_MD_CTX_free(md_ctx_verify);
  968. EVP_PKEY_free(pkey);
  969. OPENSSL_free(sig);
  970. return ret;
  971. }
  972. static int test_EVP_DigestVerifyInit(void)
  973. {
  974. int ret = 0;
  975. EVP_PKEY *pkey = NULL;
  976. EVP_MD_CTX *md_ctx = NULL;
  977. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
  978. || !TEST_ptr(pkey = load_example_rsa_key()))
  979. goto out;
  980. if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
  981. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
  982. || !TEST_true(EVP_DigestVerifyFinal(md_ctx, kSignature,
  983. sizeof(kSignature))))
  984. goto out;
  985. ret = 1;
  986. out:
  987. EVP_MD_CTX_free(md_ctx);
  988. EVP_PKEY_free(pkey);
  989. return ret;
  990. }
  991. static int test_d2i_AutoPrivateKey(int i)
  992. {
  993. int ret = 0;
  994. const unsigned char *p;
  995. EVP_PKEY *pkey = NULL;
  996. const APK_DATA *ak = &keydata[i];
  997. const unsigned char *input = ak->kder;
  998. size_t input_len = ak->size;
  999. int expected_id = ak->evptype;
  1000. p = input;
  1001. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  1002. || !TEST_ptr_eq(p, input + input_len)
  1003. || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id))
  1004. goto done;
  1005. ret = 1;
  1006. done:
  1007. EVP_PKEY_free(pkey);
  1008. return ret;
  1009. }
  1010. #ifndef OPENSSL_NO_EC
  1011. static const unsigned char ec_public_sect163k1_validxy[] = {
  1012. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  1013. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  1014. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  1015. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b,
  1016. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  1017. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  1018. };
  1019. static const unsigned char ec_public_sect163k1_badx[] = {
  1020. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  1021. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  1022. 0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  1023. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b,
  1024. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  1025. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  1026. };
  1027. static const unsigned char ec_public_sect163k1_bady[] = {
  1028. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  1029. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  1030. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  1031. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b,
  1032. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  1033. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6
  1034. };
  1035. static struct ec_der_pub_keys_st {
  1036. const unsigned char *der;
  1037. size_t len;
  1038. int valid;
  1039. } ec_der_pub_keys[] = {
  1040. { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 },
  1041. { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 },
  1042. { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 },
  1043. };
  1044. /*
  1045. * Tests the range of the decoded EC char2 public point.
  1046. * See ec_GF2m_simple_oct2point().
  1047. */
  1048. static int test_invalide_ec_char2_pub_range_decode(int id)
  1049. {
  1050. int ret = 0;
  1051. BIO *bio = NULL;
  1052. EC_KEY *eckey = NULL;
  1053. if (!TEST_ptr(bio = BIO_new_mem_buf(ec_der_pub_keys[id].der,
  1054. ec_der_pub_keys[id].len)))
  1055. goto err;
  1056. eckey = d2i_EC_PUBKEY_bio(bio, NULL);
  1057. ret = (ec_der_pub_keys[id].valid && TEST_ptr(eckey))
  1058. || TEST_ptr_null(eckey);
  1059. err:
  1060. EC_KEY_free(eckey);
  1061. BIO_free(bio);
  1062. return ret;
  1063. }
  1064. /* Tests loading a bad key in PKCS8 format */
  1065. static int test_EVP_PKCS82PKEY(void)
  1066. {
  1067. int ret = 0;
  1068. const unsigned char *derp = kExampleBadECKeyDER;
  1069. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  1070. EVP_PKEY *pkey = NULL;
  1071. if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp,
  1072. sizeof(kExampleBadECKeyDER))))
  1073. goto done;
  1074. if (!TEST_ptr_eq(derp,
  1075. kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)))
  1076. goto done;
  1077. if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf)))
  1078. goto done;
  1079. ret = 1;
  1080. done:
  1081. PKCS8_PRIV_KEY_INFO_free(p8inf);
  1082. EVP_PKEY_free(pkey);
  1083. return ret;
  1084. }
  1085. #endif
  1086. #ifndef OPENSSL_NO_SM2
  1087. static int test_EVP_SM2_verify(void)
  1088. {
  1089. /* From https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02#appendix-A */
  1090. const char *pubkey =
  1091. "-----BEGIN PUBLIC KEY-----\n"
  1092. "MIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEAhULWnkwETxjouSQ1\n"
  1093. "v2/33kVyg5FcRVF9ci7biwjx38MwRAQgeHlotPoyw/0kF4Quc7v+/y88hItoMdfg\n"
  1094. "7GUiizk35JgEIGPkxtOyOwyEnPhCQUhL/kj2HVmlsWugbm4S0donxSSaBEEEQh3r\n"
  1095. "1hti6rZ0ZDTrw8wxXjIiCzut1QvcTE5sFH/t1D0GgFEry7QsB9RzSdIVO3DE5df9\n"
  1096. "/L+jbqGoWEG55G4JogIhAIVC1p5MBE8Y6LkkNb9v990pdyBjBIVijVrnTufDLnm3\n"
  1097. "AgEBA0IABArkx3mKoPEZRxvuEYJb5GICu3nipYRElel8BP9N8lSKfAJA+I8c1OFj\n"
  1098. "Uqc8F7fxbwc1PlOhdtaEqf4Ma7eY6Fc=\n"
  1099. "-----END PUBLIC KEY-----\n";
  1100. const char *msg = "message digest";
  1101. const char *id = "ALICE123@YAHOO.COM";
  1102. const uint8_t signature[] = {
  1103. 0x30, 0x44, 0x02, 0x20,
  1104. 0x40, 0xF1, 0xEC, 0x59, 0xF7, 0x93, 0xD9, 0xF4, 0x9E, 0x09, 0xDC,
  1105. 0xEF, 0x49, 0x13, 0x0D, 0x41, 0x94, 0xF7, 0x9F, 0xB1, 0xEE, 0xD2,
  1106. 0xCA, 0xA5, 0x5B, 0xAC, 0xDB, 0x49, 0xC4, 0xE7, 0x55, 0xD1,
  1107. 0x02, 0x20,
  1108. 0x6F, 0xC6, 0xDA, 0xC3, 0x2C, 0x5D, 0x5C, 0xF1, 0x0C, 0x77, 0xDF,
  1109. 0xB2, 0x0F, 0x7C, 0x2E, 0xB6, 0x67, 0xA4, 0x57, 0x87, 0x2F, 0xB0,
  1110. 0x9E, 0xC5, 0x63, 0x27, 0xA6, 0x7E, 0xC7, 0xDE, 0xEB, 0xE7
  1111. };
  1112. int rc = 0;
  1113. BIO *bio = NULL;
  1114. EVP_PKEY *pkey = NULL;
  1115. EVP_MD_CTX *mctx = NULL;
  1116. EVP_PKEY_CTX *pctx = NULL;
  1117. bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
  1118. if (!TEST_true(bio != NULL))
  1119. goto done;
  1120. pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
  1121. if (!TEST_true(pkey != NULL))
  1122. goto done;
  1123. if (!TEST_true(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)))
  1124. goto done;
  1125. if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
  1126. goto done;
  1127. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new(pkey, NULL)))
  1128. goto done;
  1129. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, (const uint8_t *)id,
  1130. strlen(id)), 0))
  1131. goto done;
  1132. EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
  1133. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, EVP_sm3(), NULL, pkey)))
  1134. goto done;
  1135. if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
  1136. goto done;
  1137. if (!TEST_true(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature))))
  1138. goto done;
  1139. rc = 1;
  1140. done:
  1141. BIO_free(bio);
  1142. EVP_PKEY_free(pkey);
  1143. EVP_PKEY_CTX_free(pctx);
  1144. EVP_MD_CTX_free(mctx);
  1145. return rc;
  1146. }
  1147. static int test_EVP_SM2(void)
  1148. {
  1149. int ret = 0;
  1150. EVP_PKEY *pkey = NULL;
  1151. EVP_PKEY *params = NULL;
  1152. EVP_PKEY_CTX *pctx = NULL;
  1153. EVP_PKEY_CTX *kctx = NULL;
  1154. EVP_PKEY_CTX *sctx = NULL;
  1155. size_t sig_len = 0;
  1156. unsigned char *sig = NULL;
  1157. EVP_MD_CTX *md_ctx = NULL;
  1158. EVP_MD_CTX *md_ctx_verify = NULL;
  1159. EVP_PKEY_CTX *cctx = NULL;
  1160. uint8_t ciphertext[128];
  1161. size_t ctext_len = sizeof(ciphertext);
  1162. uint8_t plaintext[8];
  1163. size_t ptext_len = sizeof(plaintext);
  1164. uint8_t sm2_id[] = {1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r'};
  1165. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  1166. if (!TEST_ptr(pctx))
  1167. goto done;
  1168. if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
  1169. goto done;
  1170. if (!TEST_true(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)))
  1171. goto done;
  1172. if (!TEST_true(EVP_PKEY_paramgen(pctx, &params)))
  1173. goto done;
  1174. kctx = EVP_PKEY_CTX_new(params, NULL);
  1175. if (!TEST_ptr(kctx))
  1176. goto done;
  1177. if (!TEST_true(EVP_PKEY_keygen_init(kctx)))
  1178. goto done;
  1179. if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
  1180. goto done;
  1181. if (!TEST_true(EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)))
  1182. goto done;
  1183. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  1184. goto done;
  1185. if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
  1186. goto done;
  1187. if (!TEST_ptr(sctx = EVP_PKEY_CTX_new(pkey, NULL)))
  1188. goto done;
  1189. EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
  1190. EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);
  1191. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  1192. goto done;
  1193. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, EVP_sm3(), NULL, pkey)))
  1194. goto done;
  1195. if(!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  1196. goto done;
  1197. /* Determine the size of the signature. */
  1198. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
  1199. goto done;
  1200. if (!TEST_size_t_eq(sig_len, (size_t)EVP_PKEY_size(pkey)))
  1201. goto done;
  1202. if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
  1203. goto done;
  1204. if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  1205. goto done;
  1206. /* Ensure that the signature round-trips. */
  1207. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sm3(), NULL, pkey)))
  1208. goto done;
  1209. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
  1210. goto done;
  1211. if (!TEST_true(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)))
  1212. goto done;
  1213. /* now check encryption/decryption */
  1214. if (!TEST_ptr(cctx = EVP_PKEY_CTX_new(pkey, NULL)))
  1215. goto done;
  1216. if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
  1217. goto done;
  1218. if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg, sizeof(kMsg))))
  1219. goto done;
  1220. if (!TEST_true(EVP_PKEY_decrypt_init(cctx)))
  1221. goto done;
  1222. if (!TEST_true(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext, ctext_len)))
  1223. goto done;
  1224. if (!TEST_true(ptext_len == sizeof(kMsg)))
  1225. goto done;
  1226. if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
  1227. goto done;
  1228. ret = 1;
  1229. done:
  1230. EVP_PKEY_CTX_free(pctx);
  1231. EVP_PKEY_CTX_free(kctx);
  1232. EVP_PKEY_CTX_free(sctx);
  1233. EVP_PKEY_CTX_free(cctx);
  1234. EVP_PKEY_free(pkey);
  1235. EVP_PKEY_free(params);
  1236. EVP_MD_CTX_free(md_ctx);
  1237. EVP_MD_CTX_free(md_ctx_verify);
  1238. OPENSSL_free(sig);
  1239. return ret;
  1240. }
  1241. #endif
  1242. static struct keys_st {
  1243. int type;
  1244. char *priv;
  1245. char *pub;
  1246. } keys[] = {
  1247. {
  1248. EVP_PKEY_HMAC, "0123456789", NULL
  1249. #ifndef OPENSSL_NO_POLY1305
  1250. }, {
  1251. EVP_PKEY_POLY1305, "01234567890123456789012345678901", NULL
  1252. #endif
  1253. #ifndef OPENSSL_NO_SIPHASH
  1254. }, {
  1255. EVP_PKEY_SIPHASH, "0123456789012345", NULL
  1256. #endif
  1257. },
  1258. #ifndef OPENSSL_NO_EC
  1259. {
  1260. EVP_PKEY_X25519, "01234567890123456789012345678901",
  1261. "abcdefghijklmnopqrstuvwxyzabcdef"
  1262. }, {
  1263. EVP_PKEY_ED25519, "01234567890123456789012345678901",
  1264. "abcdefghijklmnopqrstuvwxyzabcdef"
  1265. }, {
  1266. EVP_PKEY_X448,
  1267. "01234567890123456789012345678901234567890123456789012345",
  1268. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd"
  1269. }, {
  1270. EVP_PKEY_ED448,
  1271. "012345678901234567890123456789012345678901234567890123456",
  1272. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde"
  1273. }
  1274. #endif
  1275. };
  1276. static int test_set_get_raw_keys_int(int tst, int pub)
  1277. {
  1278. int ret = 0;
  1279. unsigned char buf[80];
  1280. unsigned char *in;
  1281. size_t inlen, len = 0;
  1282. EVP_PKEY *pkey;
  1283. /* Check if this algorithm supports public keys */
  1284. if (pub && keys[tst].pub == NULL)
  1285. return 1;
  1286. memset(buf, 0, sizeof(buf));
  1287. if (pub) {
  1288. #ifndef OPENSSL_NO_EC
  1289. inlen = strlen(keys[tst].pub);
  1290. in = (unsigned char *)keys[tst].pub;
  1291. pkey = EVP_PKEY_new_raw_public_key(keys[tst].type,
  1292. NULL,
  1293. in,
  1294. inlen);
  1295. #else
  1296. return 1;
  1297. #endif
  1298. } else {
  1299. inlen = strlen(keys[tst].priv);
  1300. in = (unsigned char *)keys[tst].priv;
  1301. pkey = EVP_PKEY_new_raw_private_key(keys[tst].type,
  1302. NULL,
  1303. in,
  1304. inlen);
  1305. }
  1306. if (!TEST_ptr(pkey)
  1307. || !TEST_int_eq(EVP_PKEY_cmp(pkey, pkey), 1)
  1308. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len)))
  1309. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len)))
  1310. || !TEST_true(len == inlen)
  1311. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len)))
  1312. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len)))
  1313. || !TEST_mem_eq(in, inlen, buf, len))
  1314. goto done;
  1315. ret = 1;
  1316. done:
  1317. EVP_PKEY_free(pkey);
  1318. return ret;
  1319. }
  1320. static int test_set_get_raw_keys(int tst)
  1321. {
  1322. return test_set_get_raw_keys_int(tst, 0)
  1323. && test_set_get_raw_keys_int(tst, 1);
  1324. }
  1325. static int pkey_custom_check(EVP_PKEY *pkey)
  1326. {
  1327. return 0xbeef;
  1328. }
  1329. static int pkey_custom_pub_check(EVP_PKEY *pkey)
  1330. {
  1331. return 0xbeef;
  1332. }
  1333. static int pkey_custom_param_check(EVP_PKEY *pkey)
  1334. {
  1335. return 0xbeef;
  1336. }
  1337. static EVP_PKEY_METHOD *custom_pmeth;
  1338. static int test_EVP_PKEY_check(int i)
  1339. {
  1340. int ret = 0;
  1341. const unsigned char *p;
  1342. EVP_PKEY *pkey = NULL;
  1343. #ifndef OPENSSL_NO_EC
  1344. EC_KEY *eckey = NULL;
  1345. #endif
  1346. EVP_PKEY_CTX *ctx = NULL;
  1347. EVP_PKEY_CTX *ctx2 = NULL;
  1348. const APK_DATA *ak = &keycheckdata[i];
  1349. const unsigned char *input = ak->kder;
  1350. size_t input_len = ak->size;
  1351. int expected_id = ak->evptype;
  1352. int expected_check = ak->check;
  1353. int expected_pub_check = ak->pub_check;
  1354. int expected_param_check = ak->param_check;
  1355. int type = ak->type;
  1356. BIO *pubkey = NULL;
  1357. p = input;
  1358. switch (type) {
  1359. case 0:
  1360. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  1361. || !TEST_ptr_eq(p, input + input_len)
  1362. || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id))
  1363. goto done;
  1364. break;
  1365. #ifndef OPENSSL_NO_EC
  1366. case 1:
  1367. if (!TEST_ptr(pubkey = BIO_new_mem_buf(input, input_len))
  1368. || !TEST_ptr(eckey = d2i_EC_PUBKEY_bio(pubkey, NULL))
  1369. || !TEST_ptr(pkey = EVP_PKEY_new())
  1370. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  1371. goto done;
  1372. break;
  1373. case 2:
  1374. if (!TEST_ptr(eckey = d2i_ECParameters(NULL, &p, input_len))
  1375. || !TEST_ptr_eq(p, input + input_len)
  1376. || !TEST_ptr(pkey = EVP_PKEY_new())
  1377. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  1378. goto done;
  1379. break;
  1380. #endif
  1381. default:
  1382. return 0;
  1383. }
  1384. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL)))
  1385. goto done;
  1386. if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check))
  1387. goto done;
  1388. if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check))
  1389. goto done;
  1390. if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check))
  1391. goto done;
  1392. ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL);
  1393. /* assign the pkey directly, as an internal test */
  1394. EVP_PKEY_up_ref(pkey);
  1395. ctx2->pkey = pkey;
  1396. if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef))
  1397. goto done;
  1398. if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef))
  1399. goto done;
  1400. if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef))
  1401. goto done;
  1402. ret = 1;
  1403. done:
  1404. EVP_PKEY_CTX_free(ctx);
  1405. EVP_PKEY_CTX_free(ctx2);
  1406. EVP_PKEY_free(pkey);
  1407. BIO_free(pubkey);
  1408. return ret;
  1409. }
  1410. static int test_HKDF(void)
  1411. {
  1412. EVP_PKEY_CTX *pctx;
  1413. unsigned char out[20];
  1414. size_t outlen;
  1415. int i, ret = 0;
  1416. unsigned char salt[] = "0123456789";
  1417. unsigned char key[] = "012345678901234567890123456789";
  1418. unsigned char info[] = "infostring";
  1419. const unsigned char expected[] = {
  1420. 0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5,
  1421. 0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca
  1422. };
  1423. size_t expectedlen = sizeof(expected);
  1424. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)))
  1425. goto done;
  1426. /* We do this twice to test reuse of the EVP_PKEY_CTX */
  1427. for (i = 0; i < 2; i++) {
  1428. outlen = sizeof(out);
  1429. memset(out, 0, outlen);
  1430. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  1431. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  1432. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  1433. sizeof(salt) - 1), 0)
  1434. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  1435. sizeof(key) - 1), 0)
  1436. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  1437. sizeof(info) - 1), 0)
  1438. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  1439. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  1440. goto done;
  1441. }
  1442. ret = 1;
  1443. done:
  1444. EVP_PKEY_CTX_free(pctx);
  1445. return ret;
  1446. }
  1447. #ifndef OPENSSL_NO_EC
  1448. static int test_X509_PUBKEY_inplace(void)
  1449. {
  1450. int ret = 0;
  1451. X509_PUBKEY *xp = NULL;
  1452. const unsigned char *p = kExampleECPubKeyDER;
  1453. size_t input_len = sizeof(kExampleECPubKeyDER);
  1454. if (!TEST_ptr(xp = d2i_X509_PUBKEY(NULL, &p, input_len)))
  1455. goto done;
  1456. if (!TEST_ptr(X509_PUBKEY_get0(xp)))
  1457. goto done;
  1458. p = kExampleBadECPubKeyDER;
  1459. input_len = sizeof(kExampleBadECPubKeyDER);
  1460. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
  1461. goto done;
  1462. if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
  1463. goto done;
  1464. ret = 1;
  1465. done:
  1466. X509_PUBKEY_free(xp);
  1467. return ret;
  1468. }
  1469. #endif
  1470. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  1471. static int test_decrypt_null_chunks(void)
  1472. {
  1473. EVP_CIPHER_CTX* ctx = NULL;
  1474. const unsigned char key[32] = {
  1475. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1476. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1477. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1
  1478. };
  1479. unsigned char iv[12] = {
  1480. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
  1481. };
  1482. unsigned char msg[] = "It was the best of times, it was the worst of times";
  1483. unsigned char ciphertext[80];
  1484. unsigned char plaintext[80];
  1485. /* We initialise tmp to a non zero value on purpose */
  1486. int ctlen, ptlen, tmp = 99;
  1487. int ret = 0;
  1488. const int enc_offset = 10, dec_offset = 20;
  1489. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  1490. || !TEST_true(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL,
  1491. key, iv))
  1492. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg,
  1493. enc_offset))
  1494. /* Deliberate add a zero length update */
  1495. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL,
  1496. 0))
  1497. || !TEST_int_eq(tmp, 0)
  1498. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp,
  1499. msg + enc_offset,
  1500. sizeof(msg) - enc_offset))
  1501. || !TEST_int_eq(ctlen += tmp, sizeof(msg))
  1502. || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp))
  1503. || !TEST_int_eq(tmp, 0))
  1504. goto err;
  1505. /* Deliberately initialise tmp to a non zero value */
  1506. tmp = 99;
  1507. if (!TEST_true(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, key,
  1508. iv))
  1509. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext,
  1510. dec_offset))
  1511. /*
  1512. * Deliberately add a zero length update. We also deliberately do
  1513. * this at a different offset than for encryption.
  1514. */
  1515. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL,
  1516. 0))
  1517. || !TEST_int_eq(tmp, 0)
  1518. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp,
  1519. ciphertext + dec_offset,
  1520. ctlen - dec_offset))
  1521. || !TEST_int_eq(ptlen += tmp, sizeof(msg))
  1522. || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp))
  1523. || !TEST_int_eq(tmp, 0)
  1524. || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen))
  1525. goto err;
  1526. ret = 1;
  1527. err:
  1528. EVP_CIPHER_CTX_free(ctx);
  1529. return ret;
  1530. }
  1531. #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */
  1532. #ifndef OPENSSL_NO_DH
  1533. static int test_EVP_PKEY_set1_DH(void)
  1534. {
  1535. DH *x942dh, *pkcs3dh;
  1536. EVP_PKEY *pkey1, *pkey2;
  1537. int ret = 0;
  1538. x942dh = DH_get_2048_256();
  1539. pkcs3dh = DH_new_by_nid(NID_ffdhe2048);
  1540. pkey1 = EVP_PKEY_new();
  1541. pkey2 = EVP_PKEY_new();
  1542. if (!TEST_ptr(x942dh)
  1543. || !TEST_ptr(pkcs3dh)
  1544. || !TEST_ptr(pkey1)
  1545. || !TEST_ptr(pkey2))
  1546. goto err;
  1547. if(!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh))
  1548. || !TEST_int_eq(EVP_PKEY_id(pkey1), EVP_PKEY_DHX))
  1549. goto err;
  1550. if(!TEST_true(EVP_PKEY_set1_DH(pkey2, pkcs3dh))
  1551. || !TEST_int_eq(EVP_PKEY_id(pkey2), EVP_PKEY_DH))
  1552. goto err;
  1553. ret = 1;
  1554. err:
  1555. EVP_PKEY_free(pkey1);
  1556. EVP_PKEY_free(pkey2);
  1557. DH_free(x942dh);
  1558. DH_free(pkcs3dh);
  1559. return ret;
  1560. }
  1561. #endif /* OPENSSL_NO_DH */
  1562. typedef struct {
  1563. int data;
  1564. } custom_dgst_ctx;
  1565. static int custom_md_init_called = 0;
  1566. static int custom_md_cleanup_called = 0;
  1567. static int custom_md_init(EVP_MD_CTX *ctx)
  1568. {
  1569. custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
  1570. if (p == NULL)
  1571. return 0;
  1572. custom_md_init_called++;
  1573. return 1;
  1574. }
  1575. static int custom_md_cleanup(EVP_MD_CTX *ctx)
  1576. {
  1577. custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
  1578. if (p == NULL)
  1579. /* Nothing to do */
  1580. return 1;
  1581. custom_md_cleanup_called++;
  1582. return 1;
  1583. }
  1584. static int test_custom_md_meth(void)
  1585. {
  1586. EVP_MD_CTX *mdctx = NULL;
  1587. EVP_MD *tmp = NULL;
  1588. char mess[] = "Test Message\n";
  1589. unsigned char md_value[EVP_MAX_MD_SIZE];
  1590. unsigned int md_len;
  1591. int testresult = 0;
  1592. int nid;
  1593. custom_md_init_called = custom_md_cleanup_called = 0;
  1594. nid = OBJ_create("1.3.6.1.4.1.16604.998866.1", "custom-md", "custom-md");
  1595. if (!TEST_int_ne(nid, NID_undef))
  1596. goto err;
  1597. tmp = EVP_MD_meth_new(nid, NID_undef);
  1598. if (!TEST_ptr(tmp))
  1599. goto err;
  1600. if (!TEST_true(EVP_MD_meth_set_init(tmp, custom_md_init))
  1601. || !TEST_true(EVP_MD_meth_set_cleanup(tmp, custom_md_cleanup))
  1602. || !TEST_true(EVP_MD_meth_set_app_datasize(tmp,
  1603. sizeof(custom_dgst_ctx))))
  1604. goto err;
  1605. mdctx = EVP_MD_CTX_new();
  1606. if (!TEST_ptr(mdctx)
  1607. /*
  1608. * Initing our custom md and then initing another md should
  1609. * result in the init and cleanup functions of the custom md
  1610. * from being called.
  1611. */
  1612. || !TEST_true(EVP_DigestInit_ex(mdctx, tmp, NULL))
  1613. || !TEST_true(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL))
  1614. || !TEST_true(EVP_DigestUpdate(mdctx, mess, strlen(mess)))
  1615. || !TEST_true(EVP_DigestFinal_ex(mdctx, md_value, &md_len))
  1616. || !TEST_int_eq(custom_md_init_called, 1)
  1617. || !TEST_int_eq(custom_md_cleanup_called, 1))
  1618. goto err;
  1619. testresult = 1;
  1620. err:
  1621. EVP_MD_CTX_free(mdctx);
  1622. EVP_MD_meth_free(tmp);
  1623. return testresult;
  1624. }
  1625. #if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
  1626. /* Test we can create a signature keys with an associated ENGINE */
  1627. static int test_signatures_with_engine(int tst)
  1628. {
  1629. ENGINE *e;
  1630. const char *engine_id = "dasync";
  1631. EVP_PKEY *pkey = NULL;
  1632. const unsigned char badcmackey[] = { 0x00, 0x01 };
  1633. const unsigned char cmackey[] = {
  1634. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1635. 0x0c, 0x0d, 0x0e, 0x0f
  1636. };
  1637. const unsigned char ed25519key[] = {
  1638. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1639. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  1640. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  1641. };
  1642. const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  1643. int testresult = 0;
  1644. EVP_MD_CTX *ctx = NULL;
  1645. unsigned char *mac = NULL;
  1646. size_t maclen = 0;
  1647. int ret;
  1648. # ifdef OPENSSL_NO_CMAC
  1649. /* Skip CMAC tests in a no-cmac build */
  1650. if (tst <= 1)
  1651. return 1;
  1652. # endif
  1653. if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
  1654. return 0;
  1655. if (!TEST_true(ENGINE_init(e))) {
  1656. ENGINE_free(e);
  1657. return 0;
  1658. }
  1659. switch (tst) {
  1660. case 0:
  1661. pkey = EVP_PKEY_new_CMAC_key(e, cmackey, sizeof(cmackey),
  1662. EVP_aes_128_cbc());
  1663. break;
  1664. case 1:
  1665. pkey = EVP_PKEY_new_CMAC_key(e, badcmackey, sizeof(badcmackey),
  1666. EVP_aes_128_cbc());
  1667. break;
  1668. case 2:
  1669. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, e, ed25519key,
  1670. sizeof(ed25519key));
  1671. break;
  1672. default:
  1673. TEST_error("Invalid test case");
  1674. goto err;
  1675. }
  1676. if (tst == 1) {
  1677. /*
  1678. * In 1.1.1 CMAC keys will fail to during EVP_PKEY_new_CMAC_key() if the
  1679. * key is bad. In later versions this isn't detected until later.
  1680. */
  1681. if (!TEST_ptr_null(pkey))
  1682. goto err;
  1683. } else {
  1684. if (!TEST_ptr(pkey))
  1685. goto err;
  1686. }
  1687. if (tst == 0 || tst == 1) {
  1688. /*
  1689. * We stop the test here for tests 0 and 1. The dasync engine doesn't
  1690. * actually support CMAC in 1.1.1.
  1691. */
  1692. testresult = 1;
  1693. goto err;
  1694. }
  1695. if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
  1696. goto err;
  1697. ret = EVP_DigestSignInit(ctx, NULL, tst == 2 ? NULL : EVP_sha256(), NULL,
  1698. pkey);
  1699. if (tst == 0) {
  1700. if (!TEST_true(ret))
  1701. goto err;
  1702. if (!TEST_true(EVP_DigestSignUpdate(ctx, msg, sizeof(msg)))
  1703. || !TEST_true(EVP_DigestSignFinal(ctx, NULL, &maclen)))
  1704. goto err;
  1705. if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
  1706. goto err;
  1707. if (!TEST_true(EVP_DigestSignFinal(ctx, mac, &maclen)))
  1708. goto err;
  1709. } else {
  1710. /* We used a bad key. We expect a failure here */
  1711. if (!TEST_false(ret))
  1712. goto err;
  1713. }
  1714. testresult = 1;
  1715. err:
  1716. EVP_MD_CTX_free(ctx);
  1717. OPENSSL_free(mac);
  1718. EVP_PKEY_free(pkey);
  1719. ENGINE_finish(e);
  1720. ENGINE_free(e);
  1721. return testresult;
  1722. }
  1723. static int test_cipher_with_engine(void)
  1724. {
  1725. ENGINE *e;
  1726. const char *engine_id = "dasync";
  1727. const unsigned char keyiv[] = {
  1728. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1729. 0x0c, 0x0d, 0x0e, 0x0f
  1730. };
  1731. const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  1732. int testresult = 0;
  1733. EVP_CIPHER_CTX *ctx = NULL, *ctx2 = NULL;
  1734. unsigned char buf[AES_BLOCK_SIZE];
  1735. int len = 0;
  1736. if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
  1737. return 0;
  1738. if (!TEST_true(ENGINE_init(e))) {
  1739. ENGINE_free(e);
  1740. return 0;
  1741. }
  1742. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  1743. || !TEST_ptr(ctx2 = EVP_CIPHER_CTX_new()))
  1744. goto err;
  1745. if (!TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), e, keyiv, keyiv)))
  1746. goto err;
  1747. /* Copy the ctx, and complete the operation with the new ctx */
  1748. if (!TEST_true(EVP_CIPHER_CTX_copy(ctx2, ctx)))
  1749. goto err;
  1750. if (!TEST_true(EVP_EncryptUpdate(ctx2, buf, &len, msg, sizeof(msg)))
  1751. || !TEST_true(EVP_EncryptFinal_ex(ctx2, buf + len, &len)))
  1752. goto err;
  1753. testresult = 1;
  1754. err:
  1755. EVP_CIPHER_CTX_free(ctx);
  1756. EVP_CIPHER_CTX_free(ctx2);
  1757. ENGINE_finish(e);
  1758. ENGINE_free(e);
  1759. return testresult;
  1760. }
  1761. #endif /* !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DYNAMIC_ENGINE) */
  1762. int setup_tests(void)
  1763. {
  1764. #if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
  1765. ENGINE_load_builtin_engines();
  1766. #endif
  1767. ADD_TEST(test_EVP_DigestSignInit);
  1768. ADD_TEST(test_EVP_DigestVerifyInit);
  1769. ADD_TEST(test_EVP_Enveloped);
  1770. ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
  1771. #ifndef OPENSSL_NO_EC
  1772. ADD_TEST(test_EVP_PKCS82PKEY);
  1773. #endif
  1774. #ifndef OPENSSL_NO_SM2
  1775. ADD_TEST(test_EVP_SM2);
  1776. ADD_TEST(test_EVP_SM2_verify);
  1777. #endif
  1778. ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
  1779. custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
  1780. if (!TEST_ptr(custom_pmeth))
  1781. return 0;
  1782. EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
  1783. EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
  1784. EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
  1785. if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
  1786. return 0;
  1787. ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
  1788. ADD_TEST(test_HKDF);
  1789. #ifndef OPENSSL_NO_EC
  1790. ADD_TEST(test_X509_PUBKEY_inplace);
  1791. ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
  1792. OSSL_NELEM(ec_der_pub_keys));
  1793. #endif
  1794. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  1795. ADD_TEST(test_decrypt_null_chunks);
  1796. #endif
  1797. #ifndef OPENSSL_NO_DH
  1798. ADD_TEST(test_EVP_PKEY_set1_DH);
  1799. #endif
  1800. ADD_ALL_TESTS(test_evp_init_seq, OSSL_NELEM(evp_init_tests));
  1801. ADD_ALL_TESTS(test_evp_reset, OSSL_NELEM(evp_reset_tests));
  1802. ADD_ALL_TESTS(test_gcm_reinit, OSSL_NELEM(gcm_reinit_tests));
  1803. ADD_ALL_TESTS(test_evp_updated_iv, OSSL_NELEM(evp_updated_iv_tests));
  1804. ADD_TEST(test_custom_md_meth);
  1805. #if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
  1806. # ifndef OPENSSL_NO_EC
  1807. ADD_ALL_TESTS(test_signatures_with_engine, 3);
  1808. # else
  1809. ADD_ALL_TESTS(test_signatures_with_engine, 2);
  1810. # endif
  1811. ADD_TEST(test_cipher_with_engine);
  1812. #endif
  1813. return 1;
  1814. }