dhkem_test.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright 2022-2023 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. typedef struct {
  10. const char *curvename;
  11. /* seed */
  12. const unsigned char *ikm;
  13. size_t ikmlen;
  14. /* expected public key */
  15. const unsigned char *pub;
  16. size_t publen;
  17. /* expected private key */
  18. const unsigned char *priv;
  19. size_t privlen;
  20. } TEST_DERIVEKEY_DATA;
  21. typedef struct {
  22. const char *curve;
  23. /* The seed for the senders ephemeral key */
  24. const unsigned char *ikmE;
  25. size_t ikmElen;
  26. /* Recipient key */
  27. const unsigned char *rpub;
  28. size_t rpublen;
  29. const unsigned char *rpriv;
  30. size_t rprivlen;
  31. /* The senders generated ephemeral public key */
  32. const unsigned char *expected_enc;
  33. size_t expected_enclen;
  34. /* The generated shared secret */
  35. const unsigned char *expected_secret;
  36. size_t expected_secretlen;
  37. /* Senders Auth key */
  38. const unsigned char *spub;
  39. size_t spublen;
  40. const unsigned char *spriv;
  41. size_t sprivlen;
  42. } TEST_ENCAPDATA;
  43. static const char *dhkem_supported_curves[] = {
  44. "P-256",
  45. "P-384",
  46. "P-521",
  47. #ifndef OPENSSL_NO_ECX
  48. "X25519",
  49. "X448",
  50. #endif
  51. };
  52. /* TEST vectors extracted from RFC 9180 */
  53. /* Base test values */
  54. #ifndef OPENSSL_NO_ECX
  55. static const unsigned char x25519_ikme[] = {
  56. 0x72, 0x68, 0x60, 0x0d, 0x40, 0x3f, 0xce, 0x43,
  57. 0x15, 0x61, 0xae, 0xf5, 0x83, 0xee, 0x16, 0x13,
  58. 0x52, 0x7c, 0xff, 0x65, 0x5c, 0x13, 0x43, 0xf2,
  59. 0x98, 0x12, 0xe6, 0x67, 0x06, 0xdf, 0x32, 0x34
  60. };
  61. static const unsigned char x25519_ikme_priv[] = {
  62. 0x52, 0xc4, 0xa7, 0x58, 0xa8, 0x02, 0xcd, 0x8b,
  63. 0x93, 0x6e, 0xce, 0xea, 0x31, 0x44, 0x32, 0x79,
  64. 0x8d, 0x5b, 0xaf, 0x2d, 0x7e, 0x92, 0x35, 0xdc,
  65. 0x08, 0x4a, 0xb1, 0xb9, 0xcf, 0xa2, 0xf7, 0x36
  66. };
  67. static const unsigned char x25519_ikme_pub[] = {
  68. 0x37, 0xfd, 0xa3, 0x56, 0x7b, 0xdb, 0xd6, 0x28,
  69. 0xe8, 0x86, 0x68, 0xc3, 0xc8, 0xd7, 0xe9, 0x7d,
  70. 0x1d, 0x12, 0x53, 0xb6, 0xd4, 0xea, 0x6d, 0x44,
  71. 0xc1, 0x50, 0xf7, 0x41, 0xf1, 0xbf, 0x44, 0x31
  72. };
  73. static const unsigned char x25519_rpub[] = {
  74. 0x39, 0x48, 0xcf, 0xe0, 0xad, 0x1d, 0xdb, 0x69,
  75. 0x5d, 0x78, 0x0e, 0x59, 0x07, 0x71, 0x95, 0xda,
  76. 0x6c, 0x56, 0x50, 0x6b, 0x02, 0x73, 0x29, 0x79,
  77. 0x4a, 0xb0, 0x2b, 0xca, 0x80, 0x81, 0x5c, 0x4d
  78. };
  79. static const unsigned char x25519_rpriv[] = {
  80. 0x46, 0x12, 0xc5, 0x50, 0x26, 0x3f, 0xc8, 0xad,
  81. 0x58, 0x37, 0x5d, 0xf3, 0xf5, 0x57, 0xaa, 0xc5,
  82. 0x31, 0xd2, 0x68, 0x50, 0x90, 0x3e, 0x55, 0xa9,
  83. 0xf2, 0x3f, 0x21, 0xd8, 0x53, 0x4e, 0x8a, 0xc8
  84. };
  85. static const unsigned char x25519_expected_enc[] = {
  86. 0x37, 0xfd, 0xa3, 0x56, 0x7b, 0xdb, 0xd6, 0x28,
  87. 0xe8, 0x86, 0x68, 0xc3, 0xc8, 0xd7, 0xe9, 0x7d,
  88. 0x1d, 0x12, 0x53, 0xb6, 0xd4, 0xea, 0x6d, 0x44,
  89. 0xc1, 0x50, 0xf7, 0x41, 0xf1, 0xbf, 0x44, 0x31
  90. };
  91. static const unsigned char x25519_expected_secret[] = {
  92. 0xfe, 0x0e, 0x18, 0xc9, 0xf0, 0x24, 0xce, 0x43,
  93. 0x79, 0x9a, 0xe3, 0x93, 0xc7, 0xe8, 0xfe, 0x8f,
  94. 0xce, 0x9d, 0x21, 0x88, 0x75, 0xe8, 0x22, 0x7b,
  95. 0x01, 0x87, 0xc0, 0x4e, 0x7d, 0x2e, 0xa1, 0xfc
  96. };
  97. static const unsigned char x25519_auth_ikme[] = {
  98. 0x6e, 0x6d, 0x8f, 0x20, 0x0e, 0xa2, 0xfb, 0x20,
  99. 0xc3, 0x0b, 0x00, 0x3a, 0x8b, 0x4f, 0x43, 0x3d,
  100. 0x2f, 0x4e, 0xd4, 0xc2, 0x65, 0x8d, 0x5b, 0xc8,
  101. 0xce, 0x2f, 0xef, 0x71, 0x80, 0x59, 0xc9, 0xf7
  102. };
  103. static const unsigned char x25519_auth_rpub[] = {
  104. 0x16, 0x32, 0xd5, 0xc2, 0xf7, 0x1c, 0x2b, 0x38,
  105. 0xd0, 0xa8, 0xfc, 0xc3, 0x59, 0x35, 0x52, 0x00,
  106. 0xca, 0xa8, 0xb1, 0xff, 0xdf, 0x28, 0x61, 0x80,
  107. 0x80, 0x46, 0x6c, 0x90, 0x9c, 0xb6, 0x9b, 0x2e
  108. };
  109. static const unsigned char x25519_auth_rpriv[] = {
  110. 0xfd, 0xea, 0x67, 0xcf, 0x83, 0x1f, 0x1c, 0xa9,
  111. 0x8d, 0x8e, 0x27, 0xb1, 0xf6, 0xab, 0xeb, 0x5b,
  112. 0x77, 0x45, 0xe9, 0xd3, 0x53, 0x48, 0xb8, 0x0f,
  113. 0xa4, 0x07, 0xff, 0x69, 0x58, 0xf9, 0x13, 0x7e
  114. };
  115. static const unsigned char x25519_auth_spub[] = {
  116. 0x8b, 0x0c, 0x70, 0x87, 0x3d, 0xc5, 0xae, 0xcb,
  117. 0x7f, 0x9e, 0xe4, 0xe6, 0x24, 0x06, 0xa3, 0x97,
  118. 0xb3, 0x50, 0xe5, 0x70, 0x12, 0xbe, 0x45, 0xcf,
  119. 0x53, 0xb7, 0x10, 0x5a, 0xe7, 0x31, 0x79, 0x0b
  120. };
  121. static const unsigned char x25519_auth_spriv[] = {
  122. 0xdc, 0x4a, 0x14, 0x63, 0x13, 0xcc, 0xe6, 0x0a,
  123. 0x27, 0x8a, 0x53, 0x23, 0xd3, 0x21, 0xf0, 0x51,
  124. 0xc5, 0x70, 0x7e, 0x9c, 0x45, 0xba, 0x21, 0xa3,
  125. 0x47, 0x9f, 0xec, 0xdf, 0x76, 0xfc, 0x69, 0xdd
  126. };
  127. static const unsigned char x25519_auth_expected_enc[] = {
  128. 0x23, 0xfb, 0x95, 0x25, 0x71, 0xa1, 0x4a, 0x25,
  129. 0xe3, 0xd6, 0x78, 0x14, 0x0c, 0xd0, 0xe5, 0xeb,
  130. 0x47, 0xa0, 0x96, 0x1b, 0xb1, 0x8a, 0xfc, 0xf8,
  131. 0x58, 0x96, 0xe5, 0x45, 0x3c, 0x31, 0x2e, 0x76
  132. };
  133. static const unsigned char x25519_auth_expected_secret[] = {
  134. 0x2d, 0x6d, 0xb4, 0xcf, 0x71, 0x9d, 0xc7, 0x29,
  135. 0x3f, 0xcb, 0xf3, 0xfa, 0x64, 0x69, 0x07, 0x08,
  136. 0xe4, 0x4e, 0x2b, 0xeb, 0xc8, 0x1f, 0x84, 0x60,
  137. 0x86, 0x77, 0x95, 0x8c, 0x0d, 0x44, 0x48, 0xa7
  138. };
  139. #endif
  140. static const unsigned char p256_ikme[] = {
  141. 0x42, 0x70, 0xe5, 0x4f, 0xfd, 0x08, 0xd7, 0x9d,
  142. 0x59, 0x28, 0x02, 0x0a, 0xf4, 0x68, 0x6d, 0x8f,
  143. 0x6b, 0x7d, 0x35, 0xdb, 0xe4, 0x70, 0x26, 0x5f,
  144. 0x1f, 0x5a, 0xa2, 0x28, 0x16, 0xce, 0x86, 0x0e
  145. };
  146. static const unsigned char p256_ikme_pub[] = {
  147. 0x04, 0xa9, 0x27, 0x19, 0xc6, 0x19, 0x5d, 0x50,
  148. 0x85, 0x10, 0x4f, 0x46, 0x9a, 0x8b, 0x98, 0x14,
  149. 0xd5, 0x83, 0x8f, 0xf7, 0x2b, 0x60, 0x50, 0x1e,
  150. 0x2c, 0x44, 0x66, 0xe5, 0xe6, 0x7b, 0x32, 0x5a,
  151. 0xc9, 0x85, 0x36, 0xd7, 0xb6, 0x1a, 0x1a, 0xf4,
  152. 0xb7, 0x8e, 0x5b, 0x7f, 0x95, 0x1c, 0x09, 0x00,
  153. 0xbe, 0x86, 0x3c, 0x40, 0x3c, 0xe6, 0x5c, 0x9b,
  154. 0xfc, 0xb9, 0x38, 0x26, 0x57, 0x22, 0x2d, 0x18,
  155. 0xc4
  156. };
  157. static const unsigned char p256_ikme_priv[] = {
  158. 0x49, 0x95, 0x78, 0x8e, 0xf4, 0xb9, 0xd6, 0x13,
  159. 0x2b, 0x24, 0x9c, 0xe5, 0x9a, 0x77, 0x28, 0x14,
  160. 0x93, 0xeb, 0x39, 0xaf, 0x37, 0x3d, 0x23, 0x6a,
  161. 0x1f, 0xe4, 0x15, 0xcb, 0x0c, 0x2d, 0x7b, 0xeb
  162. };
  163. static const unsigned char p256_ikmr[] = {
  164. 0x66, 0x8b, 0x37, 0x17, 0x1f, 0x10, 0x72, 0xf3,
  165. 0xcf, 0x12, 0xea, 0x8a, 0x23, 0x6a, 0x45, 0xdf,
  166. 0x23, 0xfc, 0x13, 0xb8, 0x2a, 0xf3, 0x60, 0x9a,
  167. 0xd1, 0xe3, 0x54, 0xf6, 0xef, 0x81, 0x75, 0x50
  168. };
  169. static const unsigned char p256_ikmr_pub[] = {
  170. 0x04, 0xfe, 0x8c, 0x19, 0xce, 0x09, 0x05, 0x19,
  171. 0x1e, 0xbc, 0x29, 0x8a, 0x92, 0x45, 0x79, 0x25,
  172. 0x31, 0xf2, 0x6f, 0x0c, 0xec, 0xe2, 0x46, 0x06,
  173. 0x39, 0xe8, 0xbc, 0x39, 0xcb, 0x7f, 0x70, 0x6a,
  174. 0x82, 0x6a, 0x77, 0x9b, 0x4c, 0xf9, 0x69, 0xb8,
  175. 0xa0, 0xe5, 0x39, 0xc7, 0xf6, 0x2f, 0xb3, 0xd3,
  176. 0x0a, 0xd6, 0xaa, 0x8f, 0x80, 0xe3, 0x0f, 0x1d,
  177. 0x12, 0x8a, 0xaf, 0xd6, 0x8a, 0x2c, 0xe7, 0x2e,
  178. 0xa0
  179. };
  180. static const unsigned char p256_ikmr_priv[] = {
  181. 0xf3, 0xce, 0x7f, 0xda, 0xe5, 0x7e, 0x1a, 0x31,
  182. 0x0d, 0x87, 0xf1, 0xeb, 0xbd, 0xe6, 0xf3, 0x28,
  183. 0xbe, 0x0a, 0x99, 0xcd, 0xbc, 0xad, 0xf4, 0xd6,
  184. 0x58, 0x9c, 0xf2, 0x9d, 0xe4, 0xb8, 0xff, 0xd2
  185. };
  186. static const unsigned char p256_expected_enc[] = {
  187. 0x04, 0xa9, 0x27, 0x19, 0xc6, 0x19, 0x5d, 0x50,
  188. 0x85, 0x10, 0x4f, 0x46, 0x9a, 0x8b, 0x98, 0x14,
  189. 0xd5, 0x83, 0x8f, 0xf7, 0x2b, 0x60, 0x50, 0x1e,
  190. 0x2c, 0x44, 0x66, 0xe5, 0xe6, 0x7b, 0x32, 0x5a,
  191. 0xc9, 0x85, 0x36, 0xd7, 0xb6, 0x1a, 0x1a, 0xf4,
  192. 0xb7, 0x8e, 0x5b, 0x7f, 0x95, 0x1c, 0x09, 0x00,
  193. 0xbe, 0x86, 0x3c, 0x40, 0x3c, 0xe6, 0x5c, 0x9b,
  194. 0xfc, 0xb9, 0x38, 0x26, 0x57, 0x22, 0x2d, 0x18,
  195. 0xc4
  196. };
  197. static const unsigned char p256_expected_secret[] = {
  198. 0xc0, 0xd2, 0x6a, 0xea, 0xb5, 0x36, 0x60, 0x9a,
  199. 0x57, 0x2b, 0x07, 0x69, 0x5d, 0x93, 0x3b, 0x58,
  200. 0x9d, 0xcf, 0x36, 0x3f, 0xf9, 0xd9, 0x3c, 0x93,
  201. 0xad, 0xea, 0x53, 0x7a, 0xea, 0xbb, 0x8c, 0xb8
  202. };
  203. static const unsigned char p521_ikme[] = {
  204. 0x7f, 0x06, 0xab, 0x82, 0x15, 0x10, 0x5f, 0xc4,
  205. 0x6a, 0xce, 0xeb, 0x2e, 0x3d, 0xc5, 0x02, 0x8b,
  206. 0x44, 0x36, 0x4f, 0x96, 0x04, 0x26, 0xeb, 0x0d,
  207. 0x8e, 0x40, 0x26, 0xc2, 0xf8, 0xb5, 0xd7, 0xe7,
  208. 0xa9, 0x86, 0x68, 0x8f, 0x15, 0x91, 0xab, 0xf5,
  209. 0xab, 0x75, 0x3c, 0x35, 0x7a, 0x5d, 0x6f, 0x04,
  210. 0x40, 0x41, 0x4b, 0x4e, 0xd4, 0xed, 0xe7, 0x13,
  211. 0x17, 0x77, 0x2a, 0xc9, 0x8d, 0x92, 0x39, 0xf7,
  212. 0x09, 0x04
  213. };
  214. static const unsigned char p521_ikme_pub[] = {
  215. 0x04, 0x01, 0x38, 0xb3, 0x85, 0xca, 0x16, 0xbb,
  216. 0x0d, 0x5f, 0xa0, 0xc0, 0x66, 0x5f, 0xbb, 0xd7,
  217. 0xe6, 0x9e, 0x3e, 0xe2, 0x9f, 0x63, 0x99, 0x1d,
  218. 0x3e, 0x9b, 0x5f, 0xa7, 0x40, 0xaa, 0xb8, 0x90,
  219. 0x0a, 0xae, 0xed, 0x46, 0xed, 0x73, 0xa4, 0x90,
  220. 0x55, 0x75, 0x84, 0x25, 0xa0, 0xce, 0x36, 0x50,
  221. 0x7c, 0x54, 0xb2, 0x9c, 0xc5, 0xb8, 0x5a, 0x5c,
  222. 0xee, 0x6b, 0xae, 0x0c, 0xf1, 0xc2, 0x1f, 0x27,
  223. 0x31, 0xec, 0xe2, 0x01, 0x3d, 0xc3, 0xfb, 0x7c,
  224. 0x8d, 0x21, 0x65, 0x4b, 0xb1, 0x61, 0xb4, 0x63,
  225. 0x96, 0x2c, 0xa1, 0x9e, 0x8c, 0x65, 0x4f, 0xf2,
  226. 0x4c, 0x94, 0xdd, 0x28, 0x98, 0xde, 0x12, 0x05,
  227. 0x1f, 0x1e, 0xd0, 0x69, 0x22, 0x37, 0xfb, 0x02,
  228. 0xb2, 0xf8, 0xd1, 0xdc, 0x1c, 0x73, 0xe9, 0xb3,
  229. 0x66, 0xb5, 0x29, 0xeb, 0x43, 0x6e, 0x98, 0xa9,
  230. 0x96, 0xee, 0x52, 0x2a, 0xef, 0x86, 0x3d, 0xd5,
  231. 0x73, 0x9d, 0x2f, 0x29, 0xb0
  232. };
  233. static const unsigned char p521_ikme_priv[] = {
  234. 0x01, 0x47, 0x84, 0xc6, 0x92, 0xda, 0x35, 0xdf,
  235. 0x6e, 0xcd, 0xe9, 0x8e, 0xe4, 0x3a, 0xc4, 0x25,
  236. 0xdb, 0xdd, 0x09, 0x69, 0xc0, 0xc7, 0x2b, 0x42,
  237. 0xf2, 0xe7, 0x08, 0xab, 0x9d, 0x53, 0x54, 0x15,
  238. 0xa8, 0x56, 0x9b, 0xda, 0xcf, 0xcc, 0x0a, 0x11,
  239. 0x4c, 0x85, 0xb8, 0xe3, 0xf2, 0x6a, 0xcf, 0x4d,
  240. 0x68, 0x11, 0x5f, 0x8c, 0x91, 0xa6, 0x61, 0x78,
  241. 0xcd, 0xbd, 0x03, 0xb7, 0xbc, 0xc5, 0x29, 0x1e,
  242. 0x37, 0x4b
  243. };
  244. static const unsigned char p521_ikmr_pub[] = {
  245. 0x04, 0x01, 0xb4, 0x54, 0x98, 0xc1, 0x71, 0x4e,
  246. 0x2d, 0xce, 0x16, 0x7d, 0x3c, 0xaf, 0x16, 0x2e,
  247. 0x45, 0xe0, 0x64, 0x2a, 0xfc, 0x7e, 0xd4, 0x35,
  248. 0xdf, 0x79, 0x02, 0xcc, 0xae, 0x0e, 0x84, 0xba,
  249. 0x0f, 0x7d, 0x37, 0x3f, 0x64, 0x6b, 0x77, 0x38,
  250. 0xbb, 0xbd, 0xca, 0x11, 0xed, 0x91, 0xbd, 0xea,
  251. 0xe3, 0xcd, 0xcb, 0xa3, 0x30, 0x1f, 0x24, 0x57,
  252. 0xbe, 0x45, 0x2f, 0x27, 0x1f, 0xa6, 0x83, 0x75,
  253. 0x80, 0xe6, 0x61, 0x01, 0x2a, 0xf4, 0x95, 0x83,
  254. 0xa6, 0x2e, 0x48, 0xd4, 0x4b, 0xed, 0x35, 0x0c,
  255. 0x71, 0x18, 0xc0, 0xd8, 0xdc, 0x86, 0x1c, 0x23,
  256. 0x8c, 0x72, 0xa2, 0xbd, 0xa1, 0x7f, 0x64, 0x70,
  257. 0x4f, 0x46, 0x4b, 0x57, 0x33, 0x8e, 0x7f, 0x40,
  258. 0xb6, 0x09, 0x59, 0x48, 0x0c, 0x0e, 0x58, 0xe6,
  259. 0x55, 0x9b, 0x19, 0x0d, 0x81, 0x66, 0x3e, 0xd8,
  260. 0x16, 0xe5, 0x23, 0xb6, 0xb6, 0xa4, 0x18, 0xf6,
  261. 0x6d, 0x24, 0x51, 0xec, 0x64
  262. };
  263. static const unsigned char p521_ikmr_priv[] = {
  264. 0x01, 0x46, 0x26, 0x80, 0x36, 0x9a, 0xe3, 0x75,
  265. 0xe4, 0xb3, 0x79, 0x10, 0x70, 0xa7, 0x45, 0x8e,
  266. 0xd5, 0x27, 0x84, 0x2f, 0x6a, 0x98, 0xa7, 0x9f,
  267. 0xf5, 0xe0, 0xd4, 0xcb, 0xde, 0x83, 0xc2, 0x71,
  268. 0x96, 0xa3, 0x91, 0x69, 0x56, 0x65, 0x55, 0x23,
  269. 0xa6, 0xa2, 0x55, 0x6a, 0x7a, 0xf6, 0x2c, 0x5c,
  270. 0xad, 0xab, 0xe2, 0xef, 0x9d, 0xa3, 0x76, 0x0b,
  271. 0xb2, 0x1e, 0x00, 0x52, 0x02, 0xf7, 0xb2, 0x46,
  272. 0x28, 0x47
  273. };
  274. static const unsigned char p521_expected_enc[] = {
  275. 0x04, 0x01, 0x38, 0xb3, 0x85, 0xca, 0x16, 0xbb,
  276. 0x0d, 0x5f, 0xa0, 0xc0, 0x66, 0x5f, 0xbb, 0xd7,
  277. 0xe6, 0x9e, 0x3e, 0xe2, 0x9f, 0x63, 0x99, 0x1d,
  278. 0x3e, 0x9b, 0x5f, 0xa7, 0x40, 0xaa, 0xb8, 0x90,
  279. 0x0a, 0xae, 0xed, 0x46, 0xed, 0x73, 0xa4, 0x90,
  280. 0x55, 0x75, 0x84, 0x25, 0xa0, 0xce, 0x36, 0x50,
  281. 0x7c, 0x54, 0xb2, 0x9c, 0xc5, 0xb8, 0x5a, 0x5c,
  282. 0xee, 0x6b, 0xae, 0x0c, 0xf1, 0xc2, 0x1f, 0x27,
  283. 0x31, 0xec, 0xe2, 0x01, 0x3d, 0xc3, 0xfb, 0x7c,
  284. 0x8d, 0x21, 0x65, 0x4b, 0xb1, 0x61, 0xb4, 0x63,
  285. 0x96, 0x2c, 0xa1, 0x9e, 0x8c, 0x65, 0x4f, 0xf2,
  286. 0x4c, 0x94, 0xdd, 0x28, 0x98, 0xde, 0x12, 0x05,
  287. 0x1f, 0x1e, 0xd0, 0x69, 0x22, 0x37, 0xfb, 0x02,
  288. 0xb2, 0xf8, 0xd1, 0xdc, 0x1c, 0x73, 0xe9, 0xb3,
  289. 0x66, 0xb5, 0x29, 0xeb, 0x43, 0x6e, 0x98, 0xa9,
  290. 0x96, 0xee, 0x52, 0x2a, 0xef, 0x86, 0x3d, 0xd5,
  291. 0x73, 0x9d, 0x2f, 0x29, 0xb0
  292. };
  293. static const unsigned char p521_expected_secret[] = {
  294. 0x77, 0x6a, 0xb4, 0x21, 0x30, 0x2f, 0x6e, 0xff,
  295. 0x7d, 0x7c, 0xb5, 0xcb, 0x1a, 0xda, 0xea, 0x0c,
  296. 0xd5, 0x08, 0x72, 0xc7, 0x1c, 0x2d, 0x63, 0xc3,
  297. 0x0c, 0x4f, 0x1d, 0x5e, 0x43, 0x65, 0x33, 0x36,
  298. 0xfe, 0xf3, 0x3b, 0x10, 0x3c, 0x67, 0xe7, 0xa9,
  299. 0x8a, 0xdd, 0x2d, 0x3b, 0x66, 0xe2, 0xfd, 0xa9,
  300. 0x5b, 0x5b, 0x2a, 0x66, 0x7a, 0xa9, 0xda, 0xc7,
  301. 0xe5, 0x9c, 0xc1, 0xd4, 0x6d, 0x30, 0xe8, 0x18
  302. };
  303. static const unsigned char p521_auth_ikme[] = {
  304. 0xfe, 0x1c, 0x58, 0x9c, 0x2a, 0x05, 0x89, 0x38,
  305. 0x95, 0xa5, 0x37, 0xf3, 0x8c, 0x7c, 0xb4, 0x30,
  306. 0x0b, 0x5a, 0x7e, 0x8f, 0xef, 0x3d, 0x6c, 0xcb,
  307. 0x8f, 0x07, 0xa4, 0x98, 0x02, 0x9c, 0x61, 0xe9,
  308. 0x02, 0x62, 0xe0, 0x09, 0xdc, 0x25, 0x4c, 0x7f,
  309. 0x62, 0x35, 0xf9, 0xc6, 0xb2, 0xfd, 0x6a, 0xef,
  310. 0xf0, 0xa7, 0x14, 0xdb, 0x13, 0x1b, 0x09, 0x25,
  311. 0x8c, 0x16, 0xe2, 0x17, 0xb7, 0xbd, 0x2a, 0xa6,
  312. 0x19, 0xb0
  313. };
  314. static const unsigned char p521_auth_ikmr_pub[] = {
  315. 0x04, 0x00, 0x7d, 0x41, 0x9b, 0x88, 0x34, 0xe7,
  316. 0x51, 0x3d, 0x0e, 0x7c, 0xc6, 0x64, 0x24, 0xa1,
  317. 0x36, 0xec, 0x5e, 0x11, 0x39, 0x5a, 0xb3, 0x53,
  318. 0xda, 0x32, 0x4e, 0x35, 0x86, 0x67, 0x3e, 0xe7,
  319. 0x3d, 0x53, 0xab, 0x34, 0xf3, 0x0a, 0x0b, 0x42,
  320. 0xa9, 0x2d, 0x05, 0x4d, 0x0d, 0xb3, 0x21, 0xb8,
  321. 0x0f, 0x62, 0x17, 0xe6, 0x55, 0xe3, 0x04, 0xf7,
  322. 0x27, 0x93, 0x76, 0x7c, 0x42, 0x31, 0x78, 0x5c,
  323. 0x4a, 0x4a, 0x6e, 0x00, 0x8f, 0x31, 0xb9, 0x3b,
  324. 0x7a, 0x4f, 0x2b, 0x8c, 0xd1, 0x2e, 0x5f, 0xe5,
  325. 0xa0, 0x52, 0x3d, 0xc7, 0x13, 0x53, 0xc6, 0x6c,
  326. 0xbd, 0xad, 0x51, 0xc8, 0x6b, 0x9e, 0x0b, 0xdf,
  327. 0xcd, 0x9a, 0x45, 0x69, 0x8f, 0x2d, 0xab, 0x18,
  328. 0x09, 0xab, 0x1b, 0x0f, 0x88, 0xf5, 0x42, 0x27,
  329. 0x23, 0x2c, 0x85, 0x8a, 0xcc, 0xc4, 0x4d, 0x9a,
  330. 0x8d, 0x41, 0x77, 0x5a, 0xc0, 0x26, 0x34, 0x15,
  331. 0x64, 0xa2, 0xd7, 0x49, 0xf4
  332. };
  333. static const unsigned char p521_auth_ikmr_priv[] = {
  334. 0x01, 0x3e, 0xf3, 0x26, 0x94, 0x09, 0x98, 0x54,
  335. 0x4a, 0x89, 0x9e, 0x15, 0xe1, 0x72, 0x65, 0x48,
  336. 0xff, 0x43, 0xbb, 0xdb, 0x23, 0xa8, 0x58, 0x7a,
  337. 0xa3, 0xbe, 0xf9, 0xd1, 0xb8, 0x57, 0x33, 0x8d,
  338. 0x87, 0x28, 0x7d, 0xf5, 0x66, 0x70, 0x37, 0xb5,
  339. 0x19, 0xd6, 0xa1, 0x46, 0x61, 0xe9, 0x50, 0x3c,
  340. 0xfc, 0x95, 0xa1, 0x54, 0xd9, 0x35, 0x66, 0xd8,
  341. 0xc8, 0x4e, 0x95, 0xce, 0x93, 0xad, 0x05, 0x29,
  342. 0x3a, 0x0b
  343. };
  344. static const unsigned char p521_auth_ikms_pub[] = {
  345. 0x04, 0x01, 0x5c, 0xc3, 0x63, 0x66, 0x32, 0xea,
  346. 0x9a, 0x38, 0x79, 0xe4, 0x32, 0x40, 0xbe, 0xae,
  347. 0x5d, 0x15, 0xa4, 0x4f, 0xba, 0x81, 0x92, 0x82,
  348. 0xfa, 0xc2, 0x6a, 0x19, 0xc9, 0x89, 0xfa, 0xfd,
  349. 0xd0, 0xf3, 0x30, 0xb8, 0x52, 0x1d, 0xff, 0x7d,
  350. 0xc3, 0x93, 0x10, 0x1b, 0x01, 0x8c, 0x1e, 0x65,
  351. 0xb0, 0x7b, 0xe9, 0xf5, 0xfc, 0x9a, 0x28, 0xa1,
  352. 0xf4, 0x50, 0xd6, 0xa5, 0x41, 0xee, 0x0d, 0x76,
  353. 0x22, 0x11, 0x33, 0x00, 0x1e, 0x8f, 0x0f, 0x6a,
  354. 0x05, 0xab, 0x79, 0xf9, 0xb9, 0xbb, 0x9c, 0xcc,
  355. 0xe1, 0x42, 0xa4, 0x53, 0xd5, 0x9c, 0x5a, 0xbe,
  356. 0xbb, 0x56, 0x74, 0x83, 0x9d, 0x93, 0x5a, 0x3c,
  357. 0xa1, 0xa3, 0xfb, 0xc3, 0x28, 0x53, 0x9a, 0x60,
  358. 0xb3, 0xbc, 0x3c, 0x05, 0xfe, 0xd2, 0x28, 0x38,
  359. 0x58, 0x4a, 0x72, 0x6b, 0x9c, 0x17, 0x67, 0x96,
  360. 0xca, 0xd0, 0x16, 0x9b, 0xa4, 0x09, 0x33, 0x32,
  361. 0xcb, 0xd2, 0xdc, 0x3a, 0x9f
  362. };
  363. static const unsigned char p521_auth_ikms_priv[] = {
  364. 0x00, 0x10, 0x18, 0x58, 0x45, 0x99, 0x62, 0x5f,
  365. 0xf9, 0x95, 0x3b, 0x93, 0x05, 0x84, 0x98, 0x50,
  366. 0xd5, 0xe3, 0x4b, 0xd7, 0x89, 0xd4, 0xb8, 0x11,
  367. 0x01, 0x13, 0x96, 0x62, 0xfb, 0xea, 0x8b, 0x65,
  368. 0x08, 0xdd, 0xb9, 0xd0, 0x19, 0xb0, 0xd6, 0x92,
  369. 0xe7, 0x37, 0xf6, 0x6b, 0xea, 0xe3, 0xf1, 0xf7,
  370. 0x83, 0xe7, 0x44, 0x20, 0x2a, 0xaf, 0x6f, 0xea,
  371. 0x01, 0x50, 0x6c, 0x27, 0x28, 0x7e, 0x35, 0x9f,
  372. 0xe7, 0x76
  373. };
  374. static const unsigned char p521_auth_expected_enc[] = {
  375. 0x04, 0x01, 0x7d, 0xe1, 0x2e, 0xde, 0x7f, 0x72,
  376. 0xcb, 0x10, 0x1d, 0xab, 0x36, 0xa1, 0x11, 0x26,
  377. 0x5c, 0x97, 0xb3, 0x65, 0x48, 0x16, 0xdc, 0xd6,
  378. 0x18, 0x3f, 0x80, 0x9d, 0x4b, 0x3d, 0x11, 0x1f,
  379. 0xe7, 0x59, 0x49, 0x7f, 0x8a, 0xef, 0xdc, 0x5d,
  380. 0xbb, 0x40, 0xd3, 0xe6, 0xd2, 0x1d, 0xb1, 0x5b,
  381. 0xdc, 0x60, 0xf1, 0x5f, 0x2a, 0x42, 0x07, 0x61,
  382. 0xbc, 0xae, 0xef, 0x73, 0xb8, 0x91, 0xc2, 0xb1,
  383. 0x17, 0xe9, 0xcf, 0x01, 0xe2, 0x93, 0x20, 0xb7,
  384. 0x99, 0xbb, 0xc8, 0x6a, 0xfd, 0xc5, 0xea, 0x97,
  385. 0xd9, 0x41, 0xea, 0x1c, 0x5b, 0xd5, 0xeb, 0xee,
  386. 0xac, 0x7a, 0x78, 0x4b, 0x3b, 0xab, 0x52, 0x47,
  387. 0x46, 0xf3, 0xe6, 0x40, 0xec, 0x26, 0xee, 0x1b,
  388. 0xd9, 0x12, 0x55, 0xf9, 0x33, 0x0d, 0x97, 0x4f,
  389. 0x84, 0x50, 0x84, 0x63, 0x7e, 0xe0, 0xe6, 0xfe,
  390. 0x9f, 0x50, 0x5c, 0x5b, 0x87, 0xc8, 0x6a, 0x4e,
  391. 0x1a, 0x6c, 0x30, 0x96, 0xdd
  392. };
  393. static const unsigned char p521_auth_expected_secret[] = {
  394. 0x26, 0x64, 0x8f, 0xa2, 0xa2, 0xde, 0xb0, 0xbf,
  395. 0xc5, 0x63, 0x49, 0xa5, 0x90, 0xfd, 0x4c, 0xb7,
  396. 0x10, 0x8a, 0x51, 0x79, 0x7b, 0x63, 0x46, 0x94,
  397. 0xfc, 0x02, 0x06, 0x1e, 0x8d, 0x91, 0xb3, 0x57,
  398. 0x6a, 0xc7, 0x36, 0xa6, 0x8b, 0xf8, 0x48, 0xfe,
  399. 0x2a, 0x58, 0xdf, 0xb1, 0x95, 0x6d, 0x26, 0x6e,
  400. 0x68, 0x20, 0x9a, 0x4d, 0x63, 0x1e, 0x51, 0x3b,
  401. 0xad, 0xf8, 0xf4, 0xdc, 0xfc, 0x00, 0xf3, 0x0a
  402. };
  403. static const TEST_DERIVEKEY_DATA ec_derivekey_data[] = {
  404. {
  405. "P-256",
  406. p256_ikme, sizeof(p256_ikme),
  407. p256_ikme_pub, sizeof(p256_ikme_pub),
  408. p256_ikme_priv, sizeof(p256_ikme_priv)
  409. },
  410. {
  411. "P-256",
  412. p256_ikmr, sizeof(p256_ikmr),
  413. p256_ikmr_pub, sizeof(p256_ikmr_pub),
  414. p256_ikmr_priv, sizeof(p256_ikmr_priv)
  415. },
  416. {
  417. "P-521",
  418. p521_ikme, sizeof(p521_ikme),
  419. p521_ikme_pub, sizeof(p521_ikme_pub),
  420. p521_ikme_priv, sizeof(p521_ikme_priv)
  421. }
  422. };
  423. static const TEST_ENCAPDATA ec_encapdata[] = {
  424. {
  425. "P-256",
  426. p256_ikme, sizeof(p256_ikme),
  427. p256_ikmr_pub, sizeof(p256_ikmr_pub),
  428. p256_ikmr_priv, sizeof(p256_ikmr_priv),
  429. p256_expected_enc, sizeof(p256_expected_enc),
  430. p256_expected_secret, sizeof(p256_expected_secret),
  431. },
  432. #ifndef OPENSSL_NO_ECX
  433. {
  434. "X25519",
  435. x25519_ikme, sizeof(x25519_ikme),
  436. x25519_rpub, sizeof(x25519_rpub),
  437. x25519_rpriv, sizeof(x25519_rpriv),
  438. x25519_expected_enc, sizeof(x25519_expected_enc),
  439. x25519_expected_secret, sizeof(x25519_expected_secret),
  440. },
  441. #endif
  442. {
  443. "P-521",
  444. p521_ikme, sizeof(p521_ikme),
  445. p521_ikmr_pub, sizeof(p521_ikmr_pub),
  446. p521_ikmr_priv, sizeof(p521_ikmr_priv),
  447. p521_expected_enc, sizeof(p521_expected_enc),
  448. p521_expected_secret, sizeof(p521_expected_secret),
  449. },
  450. {
  451. "P-521",
  452. p521_auth_ikme, sizeof(p521_auth_ikme),
  453. p521_auth_ikmr_pub, sizeof(p521_auth_ikmr_pub),
  454. p521_auth_ikmr_priv, sizeof(p521_auth_ikmr_priv),
  455. p521_auth_expected_enc, sizeof(p521_auth_expected_enc),
  456. p521_auth_expected_secret, sizeof(p521_auth_expected_secret),
  457. p521_auth_ikms_pub, sizeof(p521_auth_ikms_pub),
  458. p521_auth_ikms_priv, sizeof(p521_auth_ikms_priv)
  459. },
  460. #ifndef OPENSSL_NO_ECX
  461. {
  462. "X25519",
  463. x25519_auth_ikme, sizeof(x25519_auth_ikme),
  464. x25519_auth_rpub, sizeof(x25519_auth_rpub),
  465. x25519_auth_rpriv, sizeof(x25519_auth_rpriv),
  466. x25519_auth_expected_enc, sizeof(x25519_auth_expected_enc),
  467. x25519_auth_expected_secret, sizeof(x25519_auth_expected_secret),
  468. x25519_auth_spub, sizeof(x25519_auth_spub),
  469. x25519_auth_spriv, sizeof(x25519_auth_spriv)
  470. }
  471. #endif
  472. };
  473. /* Test vector from https://github.com/cfrg/draft-irtf-cfrg-hpke */
  474. #ifndef OPENSSL_NO_ECX
  475. static const unsigned char x448_ikmr[] = {
  476. 0xd4, 0x5d, 0x16, 0x52, 0xdf, 0x74, 0x92, 0x0a,
  477. 0xbf, 0x94, 0xa2, 0x88, 0x3c, 0x83, 0x05, 0x0f,
  478. 0x50, 0x2f, 0xf5, 0x12, 0xff, 0xb5, 0x6f, 0x07,
  479. 0xb6, 0xd8, 0x33, 0xec, 0x8d, 0xda, 0x74, 0xb6,
  480. 0xa1, 0xc1, 0xcc, 0x4d, 0x42, 0xa2, 0x26, 0x41,
  481. 0xc0, 0x96, 0x3d, 0x3c, 0x21, 0xed, 0x82, 0x61,
  482. 0xf3, 0x44, 0xdc, 0x9e, 0x05, 0x01, 0xa8, 0x1c
  483. };
  484. static const unsigned char x448_ikmr_priv[] = {
  485. 0x27, 0xa4, 0x35, 0x46, 0x08, 0xf3, 0xbd, 0xd3,
  486. 0x8f, 0x1f, 0x5a, 0xf3, 0x05, 0xf3, 0xe0, 0x68,
  487. 0x2e, 0xfe, 0x4e, 0x25, 0x80, 0x82, 0x49, 0xd8,
  488. 0xfc, 0xb5, 0x59, 0x27, 0xf6, 0xa9, 0xf4, 0x46,
  489. 0xb8, 0xdc, 0x1d, 0x0a, 0x2c, 0x3b, 0x8c, 0xb1,
  490. 0x33, 0xa5, 0x67, 0x3b, 0x59, 0xa6, 0xd5, 0x5c,
  491. 0xe7, 0x54, 0xec, 0x0c, 0x9a, 0x55, 0x54, 0x01
  492. };
  493. static const unsigned char x448_ikmr_pub[] = {
  494. 0x14, 0x5d, 0x08, 0x3e, 0xa7, 0xa6, 0x37, 0x9d,
  495. 0xbb, 0x32, 0xdc, 0xbd, 0x8a, 0xff, 0x4c, 0x20,
  496. 0x6e, 0xa5, 0xd0, 0x69, 0xb7, 0x5e, 0x96, 0xc6,
  497. 0xdd, 0x2a, 0x3e, 0x38, 0xf4, 0x41, 0x47, 0x1a,
  498. 0xc9, 0x7a, 0xdc, 0xa6, 0x41, 0xfd, 0xad, 0x66,
  499. 0x68, 0x5a, 0x96, 0xf3, 0x2b, 0x7c, 0x3e, 0x06,
  500. 0x46, 0x35, 0xfa, 0xb3, 0xcc, 0x89, 0x23, 0x4e
  501. };
  502. static const TEST_DERIVEKEY_DATA ecx_derivekey_data[] = {
  503. {
  504. "X25519",
  505. x25519_ikme, sizeof(x25519_ikme),
  506. x25519_ikme_pub, sizeof(x25519_ikme_pub),
  507. x25519_ikme_priv, sizeof(x25519_ikme_priv)
  508. },
  509. {
  510. "X448",
  511. x448_ikmr, sizeof(x448_ikmr),
  512. x448_ikmr_pub, sizeof(x448_ikmr_pub),
  513. x448_ikmr_priv, sizeof(x448_ikmr_priv)
  514. },
  515. };
  516. #endif
  517. /*
  518. * Helper function to create a EC or ECX private key from bytes.
  519. * The public key can optionally be NULL.
  520. */
  521. static EVP_PKEY *new_raw_private_key(const char *curvename,
  522. const unsigned char *priv, size_t privlen,
  523. const unsigned char *pub, size_t publen)
  524. {
  525. int ok = 0;
  526. EVP_PKEY_CTX *ctx;
  527. EVP_PKEY *key = NULL;
  528. OSSL_PARAM *params = NULL;
  529. BIGNUM *privbn = NULL;
  530. OSSL_PARAM_BLD *bld = NULL;
  531. int ecx = (curvename[0] == 'X');
  532. if (ecx)
  533. ctx = EVP_PKEY_CTX_new_from_name(libctx, curvename, NULL);
  534. else
  535. ctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", NULL);
  536. if (ctx == NULL)
  537. return 0;
  538. bld = OSSL_PARAM_BLD_new();
  539. if (bld == NULL)
  540. goto err;
  541. if (ecx) {
  542. if (!OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  543. (char *)priv, privlen))
  544. goto err;
  545. } else {
  546. privbn = BN_bin2bn(priv, privlen, NULL);
  547. if (privbn == NULL)
  548. goto err;
  549. if (!OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
  550. curvename, 0))
  551. goto err;
  552. if (!OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, privbn))
  553. goto err;
  554. }
  555. if (pub != NULL) {
  556. if (!OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY,
  557. (char *)pub, publen))
  558. goto err;
  559. }
  560. params = OSSL_PARAM_BLD_to_param(bld);
  561. if (params == NULL)
  562. goto err;
  563. if (EVP_PKEY_fromdata_init(ctx) <= 0)
  564. goto err;
  565. if (EVP_PKEY_fromdata(ctx, &key, EVP_PKEY_KEYPAIR, params) <= 0)
  566. goto err;
  567. ok = 1;
  568. err:
  569. if (!ok) {
  570. EVP_PKEY_free(key);
  571. key = NULL;
  572. }
  573. BN_free(privbn);
  574. OSSL_PARAM_free(params);
  575. OSSL_PARAM_BLD_free(bld);
  576. EVP_PKEY_CTX_free(ctx);
  577. return key;
  578. }
  579. static EVP_PKEY *new_raw_public_key(const char *curvename,
  580. const unsigned char *pub, size_t publen)
  581. {
  582. int ok = 0;
  583. EVP_PKEY_CTX *ctx;
  584. EVP_PKEY *key = NULL;
  585. OSSL_PARAM params[3], *p = params;
  586. int ecx = (curvename[0] == 'X');
  587. if (ecx)
  588. ctx = EVP_PKEY_CTX_new_from_name(libctx, curvename, NULL);
  589. else
  590. ctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", NULL);
  591. if (ctx == NULL)
  592. return 0;
  593. if (!ecx)
  594. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  595. (char *)curvename, 0);
  596. *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  597. (char *)pub, publen);
  598. *p = OSSL_PARAM_construct_end();
  599. if (EVP_PKEY_fromdata_init(ctx) <= 0)
  600. goto err;
  601. if (EVP_PKEY_fromdata(ctx, &key, EVP_PKEY_PUBLIC_KEY, params) <= 0)
  602. goto err;
  603. ok = 1;
  604. err:
  605. if (!ok) {
  606. EVP_PKEY_free(key);
  607. key = NULL;
  608. }
  609. EVP_PKEY_CTX_free(ctx);
  610. return key;
  611. }
  612. /* Helper function to perform encapsulation */
  613. static int do_encap(const TEST_ENCAPDATA *t, EVP_PKEY *rpub, EVP_PKEY *spriv)
  614. {
  615. int ret = 0;
  616. unsigned char secret[256] = { 0, };
  617. unsigned char enc[256] = { 0, };
  618. size_t secretlen = 0, enclen = 0;
  619. EVP_PKEY_CTX *sctx = NULL;
  620. OSSL_PARAM params[3], *p = params;
  621. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION,
  622. (char *)OSSL_KEM_PARAM_OPERATION_DHKEM,
  623. 0);
  624. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KEM_PARAM_IKME,
  625. (char *)t->ikmE, t->ikmElen);
  626. *p = OSSL_PARAM_construct_end();
  627. if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(libctx, rpub, NULL)))
  628. goto err;
  629. if (t->spriv == NULL) {
  630. if (!TEST_int_eq(EVP_PKEY_encapsulate_init(sctx, params), 1))
  631. goto err;
  632. } else {
  633. if (!TEST_int_eq(EVP_PKEY_auth_encapsulate_init(sctx, spriv, params), 1))
  634. goto err;
  635. }
  636. ret = TEST_int_eq(EVP_PKEY_encapsulate(sctx, NULL, &enclen, NULL,
  637. &secretlen), 1)
  638. && TEST_int_eq(EVP_PKEY_encapsulate(sctx, enc, &enclen, secret,
  639. &secretlen), 1)
  640. && TEST_mem_eq(enc, enclen, t->expected_enc, t->expected_enclen)
  641. && TEST_mem_eq(secret, secretlen,
  642. t->expected_secret, t->expected_secretlen);
  643. err:
  644. EVP_PKEY_CTX_free(sctx);
  645. return ret;
  646. }
  647. /* Helper function to perform decapsulation */
  648. static int do_decap(const TEST_ENCAPDATA *t, EVP_PKEY *rpriv, EVP_PKEY *spub)
  649. {
  650. int ret = 0;
  651. EVP_PKEY_CTX *recipctx = NULL;
  652. unsigned char secret[256] = { 0, };
  653. size_t secretlen = 0;
  654. if (!TEST_ptr(recipctx = EVP_PKEY_CTX_new_from_pkey(libctx, rpriv, NULL)))
  655. goto err;
  656. if (t->spub == NULL) {
  657. if (!TEST_int_eq(EVP_PKEY_decapsulate_init(recipctx, opparam), 1))
  658. goto err;
  659. } else {
  660. if (!TEST_int_eq(EVP_PKEY_auth_decapsulate_init(recipctx, spub,
  661. opparam), 1))
  662. goto err;
  663. }
  664. ret = TEST_int_eq(EVP_PKEY_decapsulate(recipctx, NULL, &secretlen,
  665. t->expected_enc,
  666. t->expected_enclen), 1)
  667. && TEST_int_eq(EVP_PKEY_decapsulate(recipctx, secret, &secretlen,
  668. t->expected_enc,
  669. t->expected_enclen), 1)
  670. && TEST_mem_eq(secret, secretlen,
  671. t->expected_secret, t->expected_secretlen);
  672. err:
  673. EVP_PKEY_CTX_free(recipctx);
  674. return ret;
  675. }