44#include <stdlib.h>
55#include <string.h>
66
7+ #include <wolfssl/version.h>
78#include <wolfssl/wolfcrypt/hmac.h>
89#include <wolfssl/wolfcrypt/hash.h>
910#include <wolfssl/wolfcrypt/aes.h>
1617#define ODOH_LABEL_KEY "odoh key"
1718#define ODOH_LABEL_NONCE "odoh nonce"
1819
20+ #if defined(LIBWOLFSSL_VERSION_HEX ) && (LIBWOLFSSL_VERSION_HEX >= 0x05008000 )
21+ #define ODOH_HAVE_HPKE_CONTEXT_API 1
22+ #else
23+ #define ODOH_HAVE_HPKE_CONTEXT_API 0
24+ #endif
25+
26+ static int odoh_hpke_init_seal_context (Hpke * hpke , HpkeBaseContext * ctx ,
27+ void * eph , void * receiver , byte * info , word32 info_sz )
28+ {
29+ #if ODOH_HAVE_HPKE_CONTEXT_API
30+ return wc_HpkeInitSealContext (hpke , ctx , eph , receiver , info , info_sz );
31+ #else
32+ (void )hpke ; (void )ctx ; (void )eph ; (void )receiver ; (void )info ; (void )info_sz ;
33+ return -1 ;
34+ #endif
35+ }
36+
37+ static int odoh_hpke_context_seal_base (Hpke * hpke , HpkeBaseContext * ctx ,
38+ byte * aad , word32 aad_sz , byte * pt , word32 pt_sz , byte * out )
39+ {
40+ #if ODOH_HAVE_HPKE_CONTEXT_API
41+ return wc_HpkeContextSealBase (hpke , ctx , aad , aad_sz , pt , pt_sz , out );
42+ #else
43+ (void )hpke ; (void )ctx ; (void )aad ; (void )aad_sz ; (void )pt ; (void )pt_sz ; (void )out ;
44+ return -1 ;
45+ #endif
46+ }
47+
48+ static int odoh_hpke_init_open_context (Hpke * hpke , HpkeBaseContext * ctx ,
49+ void * receiver , const byte * enc , word16 enc_sz , byte * info , word32 info_sz )
50+ {
51+ #if ODOH_HAVE_HPKE_CONTEXT_API
52+ return wc_HpkeInitOpenContext (hpke , ctx , receiver , enc , enc_sz , info , info_sz );
53+ #else
54+ (void )hpke ; (void )ctx ; (void )receiver ; (void )enc ; (void )enc_sz ; (void )info ; (void )info_sz ;
55+ return -1 ;
56+ #endif
57+ }
58+
59+ static int odoh_hpke_context_open_base (Hpke * hpke , HpkeBaseContext * ctx ,
60+ byte * aad , word32 aad_sz , byte * ct , word32 ct_sz , byte * out )
61+ {
62+ #if ODOH_HAVE_HPKE_CONTEXT_API
63+ return wc_HpkeContextOpenBase (hpke , ctx , aad , aad_sz , ct , ct_sz , out );
64+ #else
65+ (void )hpke ; (void )ctx ; (void )aad ; (void )aad_sz ; (void )ct ; (void )ct_sz ; (void )out ;
66+ return -1 ;
67+ #endif
68+ }
69+
1970static uint16_t be16 (const uint8_t * p )
2071{
2172 return (uint16_t )((p [0 ] << 8 ) | p [1 ]);
@@ -363,6 +414,10 @@ int odoh_client_encrypt_query(const odoh_config *cfg,
363414 uint8_t * out , uint16_t * out_len ,
364415 odoh_client_ctx * client_ctx )
365416{
417+ #if !ODOH_HAVE_HPKE_CONTEXT_API
418+ (void )cfg ; (void )dns_msg ; (void )dns_len ; (void )out ; (void )out_len ; (void )client_ctx ;
419+ return -1 ;
420+ #else
366421 uint8_t plain [ODOH_MAX_MESSAGE ];
367422 uint16_t plain_len ;
368423 uint8_t aad [3 + ODOH_MAX_KEY_ID ];
@@ -399,7 +454,7 @@ int odoh_client_encrypt_query(const odoh_config *cfg,
399454 return -1 ;
400455 }
401456
402- if (wc_HpkeInitSealContext (& client_ctx -> hpke , & client_ctx -> hpke_ctx ,
457+ if (odoh_hpke_init_seal_context (& client_ctx -> hpke , & client_ctx -> hpke_ctx ,
403458 eph , receiver , (byte * )ODOH_INFO_QUERY , (word32 )strlen (ODOH_INFO_QUERY )) != 0 ) {
404459 wc_HpkeFreeKey (& client_ctx -> hpke , cfg -> kem_id , eph , NULL );
405460 wc_HpkeFreeKey (& client_ctx -> hpke , cfg -> kem_id , receiver , NULL );
@@ -422,7 +477,7 @@ int odoh_client_encrypt_query(const odoh_config *cfg,
422477 }
423478
424479 ct_len = plain_len + client_ctx -> hpke .Nt ;
425- if (wc_HpkeContextSealBase (& client_ctx -> hpke , & client_ctx -> hpke_ctx ,
480+ if (odoh_hpke_context_seal_base (& client_ctx -> hpke , & client_ctx -> hpke_ctx ,
426481 aad , aad_len , plain , plain_len , ct ) != 0 ) {
427482 wc_HpkeFreeKey (& client_ctx -> hpke , cfg -> kem_id , eph , NULL );
428483 wc_HpkeFreeKey (& client_ctx -> hpke , cfg -> kem_id , receiver , NULL );
@@ -460,13 +515,18 @@ int odoh_client_encrypt_query(const odoh_config *cfg,
460515 wc_HpkeFreeKey (& client_ctx -> hpke , cfg -> kem_id , receiver , NULL );
461516 wc_FreeRng (& rng );
462517 return 0 ;
518+ #endif
463519}
464520
465521int odoh_target_decrypt_query (odoh_target_ctx * target ,
466522 const uint8_t * in , uint16_t in_len ,
467523 uint8_t * dns_out , uint16_t * dns_out_len ,
468524 odoh_req_ctx * req_ctx )
469525{
526+ #if !ODOH_HAVE_HPKE_CONTEXT_API
527+ (void )target ; (void )in ; (void )in_len ; (void )dns_out ; (void )dns_out_len ; (void )req_ctx ;
528+ return -1 ;
529+ #else
470530 odoh_message_view msg ;
471531 uint8_t aad [3 + ODOH_MAX_KEY_ID ];
472532 uint16_t aad_len ;
@@ -502,15 +562,15 @@ int odoh_target_decrypt_query(odoh_target_ctx *target,
502562 ct = msg .encrypted + enc_len ;
503563 ct_len = (uint16_t )(msg .encrypted_len - enc_len );
504564
505- if (wc_HpkeInitOpenContext (& req_ctx -> hpke , & req_ctx -> hpke_ctx ,
565+ if (odoh_hpke_init_open_context (& req_ctx -> hpke , & req_ctx -> hpke_ctx ,
506566 & target -> priv , enc , enc_len ,
507567 (byte * )ODOH_INFO_QUERY , (word32 )strlen (ODOH_INFO_QUERY )) != 0 )
508568 return -1 ;
509569
510570 if (build_query_aad (target -> cfg .key_id , target -> cfg .key_id_len , aad , & aad_len ) != 0 )
511571 return -1 ;
512572
513- if (wc_HpkeContextOpenBase (& req_ctx -> hpke , & req_ctx -> hpke_ctx ,
573+ if (odoh_hpke_context_open_base (& req_ctx -> hpke , & req_ctx -> hpke_ctx ,
514574 aad , aad_len , (byte * )ct , ct_len , plain ) != 0 )
515575 return -1 ;
516576
@@ -522,12 +582,17 @@ int odoh_target_decrypt_query(odoh_target_ctx *target,
522582 req_ctx -> valid = 1 ;
523583
524584 return 0 ;
585+ #endif
525586}
526587
527588int odoh_target_encrypt_response (const odoh_req_ctx * req_ctx ,
528589 const uint8_t * dns_msg , uint16_t dns_len ,
529590 uint8_t * out , uint16_t * out_len )
530591{
592+ #if !ODOH_HAVE_HPKE_CONTEXT_API
593+ (void )req_ctx ; (void )dns_msg ; (void )dns_len ; (void )out ; (void )out_len ;
594+ return -1 ;
595+ #else
531596 uint8_t plain [ODOH_MAX_MESSAGE ];
532597 uint16_t plain_len ;
533598 uint8_t resp_nonce [64 ];
@@ -599,12 +664,17 @@ int odoh_target_encrypt_response(const odoh_req_ctx *req_ctx,
599664
600665 * out_len = (uint16_t )off ;
601666 return 0 ;
667+ #endif
602668}
603669
604670int odoh_client_decrypt_response (odoh_client_ctx * client_ctx ,
605671 const uint8_t * in , uint16_t in_len ,
606672 uint8_t * dns_out , uint16_t * dns_out_len )
607673{
674+ #if !ODOH_HAVE_HPKE_CONTEXT_API
675+ (void )client_ctx ; (void )in ; (void )in_len ; (void )dns_out ; (void )dns_out_len ;
676+ return -1 ;
677+ #else
608678 odoh_message_view msg ;
609679 uint8_t aad [3 + 64 ];
610680 uint16_t aad_len ;
@@ -656,4 +726,5 @@ int odoh_client_decrypt_response(odoh_client_ctx *client_ctx,
656726 wc_AesFree (& aes );
657727
658728 return ret == 0 ? 0 : -1 ;
729+ #endif
659730}
0 commit comments