pax_global_header00006660000000000000000000000064147473413140014522gustar00rootroot0000000000000052 comment=0b5e582eff2855ba755fbacffd16c8dc6762a707 dq-20250201/000077500000000000000000000000001474734131400123635ustar00rootroot00000000000000dq-20250201/.gitignore000066400000000000000000000000071474734131400143500ustar00rootroot00000000000000/build dq-20250201/LICENCE000066400000000000000000000156101474734131400133530ustar00rootroot00000000000000Creative Commons Legal Code CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: i. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; ii. moral rights retained by the original author(s) and/or performer(s); iii. publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; iv. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; v. rights protecting the extraction, dissemination, use and reuse of data in a Work; vi. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and vii. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 4. Limitations and Disclaimers. a. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. b. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. c. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. d. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. dq-20250201/Makefile000066400000000000000000000405001474734131400140220ustar00rootroot00000000000000CC?=cc CFLAGS+=-O3 -fno-strict-overflow -fwrapv -Wno-parentheses -Wundef -Wunused-value -Wmissing-prototypes -Wmissing-declarations -Wwrite-strings -Wdeclaration-after-statement -Wshadow -Wno-unused-function -Wno-overlength-strings -Wno-long-long -Wall -pedantic -Icryptoint LDFLAGS?= CPPFLAGS?= DESTDIR?= PREFIX?=/usr/local INSTALL?=install BINARIES=dq BINARIES+=dqcache BINARIES+=dqcache-makekey BINARIES+=dqcache-start all: $(BINARIES) alloc.o: alloc.c e.h cryptoint/crypto_uint64.h byte.h purge.h alloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c alloc.c base32decode.o: base32decode.c base32decode.h $(CC) $(CFLAGS) $(CPPFLAGS) -c base32decode.c blocking.o: blocking.c blocking.h $(CC) $(CFLAGS) $(CPPFLAGS) -c blocking.c buffer_2.o: buffer_2.c buffer.h $(CC) $(CFLAGS) $(CPPFLAGS) -c buffer_2.c buffer.o: buffer.c buffer.h $(CC) $(CFLAGS) $(CPPFLAGS) -c buffer.c buffer_put.o: buffer_put.c e.h str.h byte.h buffer.h $(CC) $(CFLAGS) $(CPPFLAGS) -c buffer_put.c buffer_write.o: buffer_write.c writeall.h buffer.h $(CC) $(CFLAGS) $(CPPFLAGS) -c buffer_write.c byte.o: byte.c byte.h $(CC) $(CFLAGS) $(CPPFLAGS) -c byte.c cache.o: cache.c alloc.h byte.h cryptoint/crypto_uint64.h \ cryptoint/crypto_uint32.h seconds.h die.h randombytes.h \ haslibrandombytes.h buffer.h open.h dns.h stralloc.h \ crypto_auth_siphash24.h e.h cache.h $(CC) $(CFLAGS) $(CPPFLAGS) -c cache.c case.o: case.c case.h $(CC) $(CFLAGS) $(CPPFLAGS) -c case.c crypto_auth_siphash24.o: crypto_auth_siphash24.c siphash.h \ crypto_verify_8.h crypto_auth_siphash24.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_auth_siphash24.c crypto_box_curve25519xsalsa20poly1305.o: \ crypto_box_curve25519xsalsa20poly1305.c crypto_core_hsalsa20.h \ crypto_scalarmult_curve25519.h haslib25519.h \ crypto_secretbox_xsalsa20poly1305.h randombytes.h haslibrandombytes.h \ crypto_box_curve25519xsalsa20poly1305.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_box_curve25519xsalsa20poly1305.c crypto_core_hsalsa20.o: crypto_core_hsalsa20.c salsa.h \ cryptoint/crypto_uint32.h crypto_core_hsalsa20.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_core_hsalsa20.c crypto_onetimeauth_poly1305.o: crypto_onetimeauth_poly1305.c \ cryptoint/crypto_int16.h cryptoint/crypto_uint32.h \ cryptoint/crypto_uint64.h crypto_onetimeauth_poly1305.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_onetimeauth_poly1305.c crypto_scalarmult_curve25519.o: crypto_scalarmult_curve25519.c \ crypto_scalarmult_curve25519.h haslib25519.h cryptoint/crypto_uint8.h \ cryptoint/crypto_uint32.h cryptoint/crypto_uint64.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_scalarmult_curve25519.c crypto_secretbox_xsalsa20poly1305.o: crypto_secretbox_xsalsa20poly1305.c \ crypto_onetimeauth_poly1305.h crypto_stream_xsalsa20.h \ crypto_secretbox_xsalsa20poly1305.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_secretbox_xsalsa20poly1305.c crypto_stream_salsa20.o: crypto_stream_salsa20.c salsa.h \ cryptoint/crypto_uint32.h crypto_stream_salsa20.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_stream_salsa20.c crypto_stream_xsalsa20.o: crypto_stream_xsalsa20.c crypto_core_hsalsa20.h \ crypto_stream_salsa20.h crypto_stream_xsalsa20.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_stream_xsalsa20.c crypto_verify_16.o: crypto_verify_16.c verify.h crypto_verify_16.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_verify_16.c crypto_verify_32.o: crypto_verify_32.c verify.h crypto_verify_32.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_verify_32.c crypto_verify_8.o: crypto_verify_8.c verify.h crypto_verify_8.h $(CC) $(CFLAGS) $(CPPFLAGS) -c crypto_verify_8.c die.o: die.c alloc.h writeall.h die.h $(CC) $(CFLAGS) $(CPPFLAGS) -c die.c dns_base32.o: dns_base32.c byte.h dns.h stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_base32.c dns_data.o: dns_data.c stralloc.h alloc.h byte.h dns.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_data.c dns_domain.o: dns_domain.c alloc.h byte.h case.h e.h dns.h stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_domain.c dns_dtda.o: dns_dtda.c stralloc.h dns.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_dtda.c dns_ip.o: dns_ip.c alloc.h byte.h cryptoint/crypto_uint16.h \ base32decode.h hexdecode.h case.h str.h stralloc.h strtoip.h \ milliseconds.h dns.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_ip.c dns_ipq.o: dns_ipq.c stralloc.h case.h byte.h str.h dns.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_ipq.c dns_iptoname.o: dns_iptoname.c byte.h numtostr.h dns.h stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_iptoname.c dns_keys.o: dns_keys.c crypto_stream_salsa20.h dns.h stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_keys.c dns_nonce.o: dns_nonce.c nanoseconds.h randombytes.h haslibrandombytes.h \ cryptoint/crypto_uint32.h cryptoint/crypto_uint64.h byte.h purge.h dns.h \ stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_nonce.c dns_packet.o: dns_packet.c e.h byte.h dns.h stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_packet.c dns_rcip.o: dns_rcip.c milliseconds.h openreadclose.h stralloc.h byte.h \ env.h strtoip.h strtomultiip.h xsocket.h dns.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_rcip.c dns_rcrw.o: dns_rcrw.c milliseconds.h env.h byte.h str.h openreadclose.h \ stralloc.h dns.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_rcrw.c dns_resolve.o: dns_resolve.c milliseconds.h byte.h e.h dns.h stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_resolve.c dns_sortip.o: dns_sortip.c randommod.h byte.h dns.h stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_sortip.c dns_transmit.o: dns_transmit.c alloc.h milliseconds.h xsocket.h e.h \ byte.h cryptoint/crypto_uint16.h randombytes.h haslibrandombytes.h \ randommod.h case.h str.h dns.h stralloc.h \ crypto_box_curve25519xsalsa20poly1305.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_transmit.c dns_verbosity.o: dns_verbosity.c stralloc.h writeall.h iptostr.h \ porttostr.h numtostr.h e.h cryptoint/crypto_uint16.h byte.h dns.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dns_verbosity.c dq.o: dq.c dns.h stralloc.h strtonum.h case.h die.h e.h randombytes.h \ haslibrandombytes.h byte.h printpacket.h writeall.h milliseconds.h str.h \ cryptoint/crypto_uint16.h portparse.h base32decode.h hexdecode.h \ strtoip.h keyparse.h typeparse.h purge.h \ crypto_box_curve25519xsalsa20poly1305.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dq.c dqcache.o: dqcache.c env.h byte.h xsocket.h strtoip.h randombytes.h \ haslibrandombytes.h cryptoint/crypto_uint64.h query.h dns.h stralloc.h \ cryptoint/crypto_uint32.h die.h warn.h e.h numtostr.h strtonum.h cache.h \ response.h log.h roots.h hexparse.h alloc.h milliseconds.h blocking.h \ cryptoint/crypto_uint16.h portparse.h droproot.h okclient.h purge.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dqcache.c dqcache-makekey.o: dqcache-makekey.c randombytes.h haslibrandombytes.h \ writeall.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dqcache-makekey.c dqcache-start.o: dqcache-start.c numtostr.h strtonum.h e.h die.h $(CC) $(CFLAGS) $(CPPFLAGS) -c dqcache-start.c droproot.o: droproot.c env.h die.h strtonum.h e.h droproot.h $(CC) $(CFLAGS) $(CPPFLAGS) -c droproot.c e.o: e.c e.h $(CC) $(CFLAGS) $(CPPFLAGS) -c e.c env.o: env.c str.h env.h $(CC) $(CFLAGS) $(CPPFLAGS) -c env.c hexdecode.o: hexdecode.c hexdecode.h $(CC) $(CFLAGS) $(CPPFLAGS) -c hexdecode.c hexparse.o: hexparse.c hexparse.h $(CC) $(CFLAGS) $(CPPFLAGS) -c hexparse.c int16_optblocker.o: int16_optblocker.c cryptoint/crypto_int16.h $(CC) $(CFLAGS) $(CPPFLAGS) -c int16_optblocker.c int32_optblocker.o: int32_optblocker.c cryptoint/crypto_int32.h $(CC) $(CFLAGS) $(CPPFLAGS) -c int32_optblocker.c int64_optblocker.o: int64_optblocker.c cryptoint/crypto_int64.h $(CC) $(CFLAGS) $(CPPFLAGS) -c int64_optblocker.c int8_optblocker.o: int8_optblocker.c cryptoint/crypto_int8.h $(CC) $(CFLAGS) $(CPPFLAGS) -c int8_optblocker.c iptostr.o: iptostr.c byte.h iptostr.h $(CC) $(CFLAGS) $(CPPFLAGS) -c iptostr.c keyparse.o: keyparse.c hexdecode.h base32decode.h byte.h str.h keyparse.h $(CC) $(CFLAGS) $(CPPFLAGS) -c keyparse.c log.o: log.c buffer.h cryptoint/crypto_uint32.h cryptoint/crypto_uint16.h \ e.h byte.h iptostr.h numtostr.h log.h cryptoint/crypto_uint64.h $(CC) $(CFLAGS) $(CPPFLAGS) -c log.c milliseconds.o: milliseconds.c milliseconds.h $(CC) $(CFLAGS) $(CPPFLAGS) -c milliseconds.c nanoseconds.o: nanoseconds.c nanoseconds.h $(CC) $(CFLAGS) $(CPPFLAGS) -c nanoseconds.c numtostr.o: numtostr.c numtostr.h $(CC) $(CFLAGS) $(CPPFLAGS) -c numtostr.c okclient.o: okclient.c str.h byte.h iptostr.h okclient.h $(CC) $(CFLAGS) $(CPPFLAGS) -c okclient.c open_read.o: open_read.c open.h $(CC) $(CFLAGS) $(CPPFLAGS) -c open_read.c openreadclose.o: openreadclose.c open.h e.h byte.h openreadclose.h \ stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c openreadclose.c open_trunc.o: open_trunc.c open.h $(CC) $(CFLAGS) $(CPPFLAGS) -c open_trunc.c portparse.o: portparse.c portparse.h $(CC) $(CFLAGS) $(CPPFLAGS) -c portparse.c porttostr.o: porttostr.c cryptoint/crypto_uint16.h porttostr.h $(CC) $(CFLAGS) $(CPPFLAGS) -c porttostr.c printpacket.o: printpacket.c cryptoint/crypto_uint16.h e.h byte.h dns.h \ stralloc.h printrecord.h printpacket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c printpacket.c printrecord.o: printrecord.c cryptoint/crypto_uint16.h \ cryptoint/crypto_uint32.h e.h byte.h dns.h stralloc.h printrecord.h \ iptostr.h $(CC) $(CFLAGS) $(CPPFLAGS) -c printrecord.c purge.o: purge.c purge.h $(CC) $(CFLAGS) $(CPPFLAGS) -c purge.c query.o: query.c e.h roots.h log.h cryptoint/crypto_uint64.h case.h \ cache.h byte.h dns.h stralloc.h cryptoint/crypto_uint32.h \ cryptoint/crypto_uint16.h alloc.h response.h query.h strtoip.h iptostr.h \ xsocket.h crypto_scalarmult_curve25519.h haslib25519.h \ crypto_box_curve25519xsalsa20poly1305.h purge.h $(CC) $(CFLAGS) $(CPPFLAGS) -c query.c randombytes.o: randombytes.c randombytes.h haslibrandombytes.h $(CC) $(CFLAGS) $(CPPFLAGS) -c randombytes.c randommod.o: randommod.c randombytes.h haslibrandombytes.h randommod.h $(CC) $(CFLAGS) $(CPPFLAGS) -c randommod.c response.o: response.c dns.h stralloc.h byte.h cryptoint/crypto_uint16.h \ cryptoint/crypto_uint32.h response.h $(CC) $(CFLAGS) $(CPPFLAGS) -c response.c roots.o: roots.c open.h e.h str.h byte.h direntry.h strtoip.h dns.h \ stralloc.h openreadclose.h roots.h keyparse.h $(CC) $(CFLAGS) $(CPPFLAGS) -c roots.c salsa.o: salsa.c cryptoint/crypto_uint64.h cryptoint/crypto_uint32.h \ salsa.h $(CC) $(CFLAGS) $(CPPFLAGS) -c salsa.c seconds.o: seconds.c seconds.h $(CC) $(CFLAGS) $(CPPFLAGS) -c seconds.c siphash.o: siphash.c cryptoint/crypto_uint64.h siphash.h $(CC) $(CFLAGS) $(CPPFLAGS) -c siphash.c stralloc.o: stralloc.c alloc.h e.h stralloc.h $(CC) $(CFLAGS) $(CPPFLAGS) -c stralloc.c str.o: str.c str.h $(CC) $(CFLAGS) $(CPPFLAGS) -c str.c strtoip.o: strtoip.c byte.h strtoip.h $(CC) $(CFLAGS) $(CPPFLAGS) -c strtoip.c strtomultiip.o: strtomultiip.c byte.h str.h strtoip.h strtomultiip.h $(CC) $(CFLAGS) $(CPPFLAGS) -c strtomultiip.c strtonum.o: strtonum.c e.h strtonum.h $(CC) $(CFLAGS) $(CPPFLAGS) -c strtonum.c typeparse.o: typeparse.c strtonum.h cryptoint/crypto_uint16.h case.h \ dns.h stralloc.h byte.h typeparse.h $(CC) $(CFLAGS) $(CPPFLAGS) -c typeparse.c uint16_optblocker.o: uint16_optblocker.c cryptoint/crypto_uint16.h $(CC) $(CFLAGS) $(CPPFLAGS) -c uint16_optblocker.c uint32_optblocker.o: uint32_optblocker.c cryptoint/crypto_uint32.h $(CC) $(CFLAGS) $(CPPFLAGS) -c uint32_optblocker.c uint64_optblocker.o: uint64_optblocker.c cryptoint/crypto_uint64.h $(CC) $(CFLAGS) $(CPPFLAGS) -c uint64_optblocker.c uint8_optblocker.o: uint8_optblocker.c cryptoint/crypto_uint8.h $(CC) $(CFLAGS) $(CPPFLAGS) -c uint8_optblocker.c verify.o: verify.c verify.h $(CC) $(CFLAGS) $(CPPFLAGS) -c verify.c warn.o: warn.c writeall.h warn.h $(CC) $(CFLAGS) $(CPPFLAGS) -c warn.c writeall.o: writeall.c e.h writeall.h $(CC) $(CFLAGS) $(CPPFLAGS) -c writeall.c xsocket_accept.o: xsocket_accept.c e.h byte.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_accept.c xsocket_bind.o: xsocket_bind.c e.h byte.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_bind.c xsocket_conn.o: xsocket_conn.c e.h byte.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_conn.c xsocket_listen.o: xsocket_listen.c e.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_listen.c xsocket_recv.o: xsocket_recv.c e.h byte.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_recv.c xsocket_send.o: xsocket_send.c e.h byte.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_send.c xsocket_tcp.o: xsocket_tcp.c blocking.h e.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_tcp.c xsocket_type.o: xsocket_type.c byte.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_type.c xsocket_udp.o: xsocket_udp.c blocking.h e.h xsocket.h $(CC) $(CFLAGS) $(CPPFLAGS) -c xsocket_udp.c OBJECTS=alloc.o OBJECTS+=base32decode.o OBJECTS+=blocking.o OBJECTS+=buffer_2.o OBJECTS+=buffer.o OBJECTS+=buffer_put.o OBJECTS+=buffer_write.o OBJECTS+=byte.o OBJECTS+=cache.o OBJECTS+=case.o OBJECTS+=crypto_auth_siphash24.o OBJECTS+=crypto_box_curve25519xsalsa20poly1305.o OBJECTS+=crypto_core_hsalsa20.o OBJECTS+=crypto_onetimeauth_poly1305.o OBJECTS+=crypto_scalarmult_curve25519.o OBJECTS+=crypto_secretbox_xsalsa20poly1305.o OBJECTS+=crypto_stream_salsa20.o OBJECTS+=crypto_stream_xsalsa20.o OBJECTS+=crypto_verify_16.o OBJECTS+=crypto_verify_32.o OBJECTS+=crypto_verify_8.o OBJECTS+=die.o OBJECTS+=dns_base32.o OBJECTS+=dns_data.o OBJECTS+=dns_domain.o OBJECTS+=dns_dtda.o OBJECTS+=dns_ip.o OBJECTS+=dns_ipq.o OBJECTS+=dns_iptoname.o OBJECTS+=dns_keys.o OBJECTS+=dns_nonce.o OBJECTS+=dns_packet.o OBJECTS+=dns_rcip.o OBJECTS+=dns_rcrw.o OBJECTS+=dns_resolve.o OBJECTS+=dns_sortip.o OBJECTS+=dns_transmit.o OBJECTS+=dns_verbosity.o OBJECTS+=droproot.o OBJECTS+=e.o OBJECTS+=env.o OBJECTS+=hexdecode.o OBJECTS+=hexparse.o OBJECTS+=int16_optblocker.o OBJECTS+=int32_optblocker.o OBJECTS+=int64_optblocker.o OBJECTS+=int8_optblocker.o OBJECTS+=iptostr.o OBJECTS+=keyparse.o OBJECTS+=log.o OBJECTS+=milliseconds.o OBJECTS+=nanoseconds.o OBJECTS+=numtostr.o OBJECTS+=okclient.o OBJECTS+=open_read.o OBJECTS+=openreadclose.o OBJECTS+=open_trunc.o OBJECTS+=portparse.o OBJECTS+=porttostr.o OBJECTS+=printpacket.o OBJECTS+=printrecord.o OBJECTS+=purge.o OBJECTS+=query.o OBJECTS+=randombytes.o OBJECTS+=randommod.o OBJECTS+=response.o OBJECTS+=roots.o OBJECTS+=salsa.o OBJECTS+=seconds.o OBJECTS+=siphash.o OBJECTS+=stralloc.o OBJECTS+=str.o OBJECTS+=strtoip.o OBJECTS+=strtomultiip.o OBJECTS+=strtonum.o OBJECTS+=typeparse.o OBJECTS+=uint16_optblocker.o OBJECTS+=uint32_optblocker.o OBJECTS+=uint64_optblocker.o OBJECTS+=uint8_optblocker.o OBJECTS+=verify.o OBJECTS+=warn.o OBJECTS+=writeall.o OBJECTS+=xsocket_accept.o OBJECTS+=xsocket_bind.o OBJECTS+=xsocket_conn.o OBJECTS+=xsocket_listen.o OBJECTS+=xsocket_recv.o OBJECTS+=xsocket_send.o OBJECTS+=xsocket_tcp.o OBJECTS+=xsocket_type.o OBJECTS+=xsocket_udp.o dq: dq.o $(OBJECTS) libs $(CC) $(CFLAGS) $(CPPFLAGS) -o dq dq.o $(OBJECTS) $(LDFLAGS) `cat libs` dqcache: dqcache.o $(OBJECTS) libs $(CC) $(CFLAGS) $(CPPFLAGS) -o dqcache dqcache.o $(OBJECTS) $(LDFLAGS) `cat libs` dqcache-makekey: dqcache-makekey.o $(OBJECTS) libs $(CC) $(CFLAGS) $(CPPFLAGS) -o dqcache-makekey dqcache-makekey.o $(OBJECTS) $(LDFLAGS) `cat libs` dqcache-start: dqcache-start.o $(OBJECTS) libs $(CC) $(CFLAGS) $(CPPFLAGS) -o dqcache-start dqcache-start.o $(OBJECTS) $(LDFLAGS) `cat libs` haslib25519.h: tryfeature.sh haslib25519.c libs env CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS) `cat libs`" ./tryfeature.sh haslib25519.c >haslib25519.h 2>haslib25519.log cat haslib25519.h haslibrandombytes.h: tryfeature.sh haslibrandombytes.c libs env CC="$(CC)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS) `cat libs`" ./tryfeature.sh haslibrandombytes.c >haslibrandombytes.h 2>haslibrandombytes.log cat haslibrandombytes.h libs: trylibs.sh env CC="$(CC)" ./trylibs.sh -lsocket -lnsl -lrandombytes -l25519 >libs 2>libs.log cat libs install: dq dqcache dqcache-makekey dqcache-start mkdir -p $(DESTDIR)$(PREFIX)/bin mkdir -p $(DESTDIR)$(PREFIX)/sbin mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1 mkdir -p $(DESTDIR)$(PREFIX)/share/man/man8 $(INSTALL) -m 0755 dq $(DESTDIR)$(PREFIX)/bin/dq $(INSTALL) -m 0755 dqcache $(DESTDIR)$(PREFIX)/sbin/dqcache $(INSTALL) -m 0755 dqcache-makekey $(DESTDIR)$(PREFIX)/sbin/dqcache-makekey $(INSTALL) -m 0755 dqcache-start $(DESTDIR)$(PREFIX)/sbin/dqcache-start $(INSTALL) -m 0644 man/dq.1 $(DESTDIR)$(PREFIX)/share/man/man1/dq.1 $(INSTALL) -m 0644 man/dqcache.8 $(DESTDIR)$(PREFIX)/share/man/man8/dqcache.8 $(INSTALL) -m 0644 man/dqcache-makekey.8 $(DESTDIR)$(PREFIX)/share/man/man8/dqcache-makekey.8 $(INSTALL) -m 0644 man/dqcache-start.8 $(DESTDIR)$(PREFIX)/share/man/man8/dqcache-start.8 clean: rm -f *.log has*.h $(OBJECTS) $(BINARIES) libs dq-20250201/README.cryptoint.md000066400000000000000000000005121474734131400156720ustar00rootroot00000000000000Cryptoint is a library created by Daniel J. Bernstein. It's public-domain code used in SUPERCOP/libntruprime/libmceliece/... To Dq is imported from https://libntruprime.cr.yp.to/libntruprime-20241021/cryptoint.html. Description from the author e.g. here: - https://cr.yp.to/talks/2024.07.22/slides-djb-20240722-snippets-4x3.pdf dq-20250201/README.md000066400000000000000000000017351474734131400136500ustar00rootroot00000000000000#### dq tool #### * based on dnsq and dnsqr from djbdns * added IPv6 support * added DNSCurve support (Streamlined/TXT) #### dqcache recursive server #### * based on dnscache from djbdns * added support for streamlined DNSCurve * added support for TXT DNSCurve * added support for combined DNSCurve (streamlined and TXT) * added support for DNS anchors with DNSCurve keys * added full IPv6 support * added support for IPv6 DNS anchors * added support for cache dumping/loading * used siphash24 instead of hash5381 in cache library * added SOA record caching * added authority record to DNS response * improved logs - IPs not printed in hex format * fixed CVE-2012-1191 * fixed CVE-2008-4392 #### examples #### - [examples page](examples.md) #### installation #### - [installation](install.md) #### licence #### * Dq is derived from public-domain djbdns-1.05 - see: https://cr.yp.to/distributors.html * Dq is released under CC0 licence http://creativecommons.org/publicdomain/zero/1.0/ dq-20250201/alloc.c000066400000000000000000000056631474734131400136330ustar00rootroot00000000000000/* 20130503 Jan Mojzis Public domain. */ #include #include #include #include "e.h" #include "crypto_uint64.h" #include "byte.h" #include "purge.h" #include "alloc.h" #define ALLOC_ALIGNMENT 16 #define ALLOC_SPACE 8192 #define ALLOC_LIMIT 4000000000LL typedef union { unsigned char irrelevant[ALLOC_ALIGNMENT]; double d; } aligned; static aligned realspace[ALLOC_SPACE / ALLOC_ALIGNMENT]; #define space ((unsigned char *) realspace) static long long avail = ALLOC_SPACE; static long long allocated = 0; static long long limit = -1; static long long getlimit(void) { struct rlimit r; if (limit >= 0) return limit; #ifdef RLIMIT_DATA if (getrlimit(RLIMIT_DATA, &r) == 0) { if (r.rlim_cur > r.rlim_max) r.rlim_cur = r.rlim_max; limit = (long long)r.rlim_cur; } #endif if (limit < 0 || limit > ALLOC_LIMIT) limit = ALLOC_LIMIT; return limit; } #ifdef TEST void alloc_setlimit(long long l) { limit = l; } long long alloc_getallocated(void) { return allocated; } long long alloc_getspace(void) { return ALLOC_SPACE; } #endif static void **ptr = 0; static long long ptrlen = 0; static long long ptralloc = 0; static int ptr_add(void *x) { void **newptr; if (ptrlen + 1 > ptralloc) { while (ptrlen + 1 > ptralloc) ptralloc = 2 * ptralloc + 1; newptr = (void **)malloc(ptralloc * sizeof(void *)); if (!newptr) return 0; if (ptr) { byte_copy(newptr, ptrlen * sizeof(void *), ptr); free(ptr); } ptr = newptr; } if (!x) return 1; ptr[ptrlen++] = x; return 1; } static int ptr_remove(void *x) { long long i; for (i = 0; i < ptrlen; ++i) { if (ptr[i] == x) goto ok; } return 0; ok: --ptrlen; ptr[i] = ptr[ptrlen]; return 1; } void *alloc(long long nn) { unsigned char *x; crypto_uint64 n; if (nn < 0 || nn > ALLOC_LIMIT) goto nomem; if (nn == 0) nn = 1; n = nn; n = ALLOC_ALIGNMENT + n - (n & (ALLOC_ALIGNMENT - 1)); if (n <= avail) { avail -= n; return (void *)(space + avail); } n += ALLOC_ALIGNMENT; if (allocated + n > getlimit()) goto nomem; x = (unsigned char *)malloc(n); if (!x) goto nomem; allocated += n; byte_zero(x, n); crypto_uint64_store(x, n); x += ALLOC_ALIGNMENT; if (!ptr_add(x)) goto nomem; return (void *)x; nomem: errno = ENOMEM; return (void *)0; } void alloc_free(void *xv) { crypto_uint64 n; unsigned char *x = xv; if (x >= space) if (x < space + ALLOC_SPACE) return; if (!ptr_remove(x)) return; x -= ALLOC_ALIGNMENT; n = crypto_uint64_load(x); allocated -= n; purge(x, n); free(x); } void alloc_freeall(void) { while (ptrlen > 0) { alloc_free(ptr[0]); } if (ptr) { free(ptr); ptr = 0; ptrlen = 0; ptralloc = 0; } purge(space, ALLOC_SPACE); } dq-20250201/alloc.h000066400000000000000000000005271474734131400136320ustar00rootroot00000000000000#ifndef _ALLOC_H____ #define _ALLOC_H____ #ifdef TEST extern void alloc_setlimit(long long); extern long long alloc_getallocated(void); extern long long alloc_getspace(void); #endif extern void *alloc(long long); extern void alloc_free(void *x); extern int alloc_re(void **x, long long, long long); extern void alloc_freeall(void); #endif dq-20250201/base32decode.c000066400000000000000000000030011474734131400147440ustar00rootroot00000000000000#include "base32decode.h" static const unsigned char base32values[128] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0xff, 0x0d, 0x0e, 0x0f, 0xff, 0x10, 0x11, 0x12, 0x13, 0x14, 0xff, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0xff, 0x0d, 0x0e, 0x0f, 0xff, 0x10, 0x11, 0x12, 0x13, 0x14, 0xff, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff }; int base32decode(unsigned char *out, long long outlen, const unsigned char *in, long long inlen) { long long i, j = 0; unsigned char u; unsigned long long v = 0, vbits = 0; for (i = 0; i < inlen; ++i) { if (in[j] & 0x80) return 0; u = base32values[in[i]]; if (u > 0x1f) return 0; v |= u << vbits; vbits += 5; if (vbits >= 8) { if (j >= outlen) return 0; out[j++] = v; vbits -= 8; v >>= 8; } } if (vbits) { if (j >= outlen) return 0; out[j++] = v; } return 1; } dq-20250201/base32decode.h000066400000000000000000000002301474734131400147520ustar00rootroot00000000000000#ifndef _BASE32DECODE_H____ #define _BASE32DECODE_H____ extern int base32decode(unsigned char *, long long, const unsigned char *, long long); #endif dq-20250201/blocking.c000066400000000000000000000004431474734131400143200ustar00rootroot00000000000000/* taken from nacl-20110221, from curvecp/blocking.c (public domain) */ #include #include "blocking.h" void blocking_enable(int fd) { fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) & ~O_NONBLOCK); } void blocking_disable(int fd) { fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) | O_NONBLOCK); } dq-20250201/blocking.h000066400000000000000000000001641474734131400143250ustar00rootroot00000000000000#ifndef BLOCKING_H #define BLOCKING_H extern void blocking_enable(int); extern void blocking_disable(int); #endif dq-20250201/buffer.c000066400000000000000000000003101474734131400137720ustar00rootroot00000000000000#include "buffer.h" void buffer_init(buffer *s, long long (*op)(), int fd, char *buf, long long len) { s->x = buf; s->fd = fd; s->op = op; s->p = 0; s->n = len; return; } dq-20250201/buffer.h000066400000000000000000000034331474734131400140100ustar00rootroot00000000000000#ifndef _BUFFER_H____ #define _BUFFER_H____ typedef struct buffer { char *x; long long p; long long n; int fd; long long (*op)(); } buffer; #define BUFFER_INIT(op, fd, buf, len) { (buf), 0, (len), (fd), (op) } #define BUFFER_INSIZE 8192 #define BUFFER_OUTSIZE 8192 #define BUFFER_ERRSIZE 256 extern void buffer_init(buffer *, long long (*op)(), int, char *, long long); extern int buffer_flush(buffer *); extern int buffer_putalign(buffer *, const char *, long long); extern int buffer_put(buffer *, const char *, long long); extern int buffer_putflush(buffer *, const char *, long long); extern int buffer_putsalign(buffer *, const char *); extern int buffer_puts(buffer *, const char *); extern int buffer_putsflush(buffer *, const char *); #define buffer_PUTC(s,c) \ ( ((s)->n != (s)->p) \ ? ( (s)->x[(s)->p++] = (c), 0 ) \ : buffer_put((s),&(c),1) \ ) extern long long buffer_get(buffer *, char *, long long); extern long long buffer_bget(buffer *, char *, long long); extern long long buffer_feed(buffer *); extern char *buffer_peek(buffer *); extern void buffer_seek(buffer *, long long); #define buffer_PEEK(s) ( (s)->x + (s)->n ) #define buffer_SEEK(s,len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) ) #define buffer_GETC(s,c) \ ( ((s)->p > 0) \ ? ( *(c) = (s)->x[(s)->n], buffer_SEEK((s),1), 1 ) \ : buffer_get((s),(c),1) \ ) extern int buffer_copy(buffer *,buffer *); extern long long buffer_unixread(int, char *, long long); extern long long buffer_unixwrite(int, const char *, long long); #define buffer_PEEK(s) ( (s)->x + (s)->n ) #define buffer_SEEK(s, len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) ) extern buffer *buffer_0; extern buffer *buffer_0small; extern buffer *buffer_1; extern buffer *buffer_1small; extern buffer *buffer_2; #endif dq-20250201/buffer_2.c000066400000000000000000000002561474734131400142240ustar00rootroot00000000000000#include "buffer.h" char buffer_2_space[BUFFER_ERRSIZE]; static buffer it = BUFFER_INIT(buffer_unixwrite, 2, buffer_2_space, sizeof buffer_2_space); buffer *buffer_2 = ⁢ dq-20250201/buffer_put.c000066400000000000000000000042321474734131400146710ustar00rootroot00000000000000#include "e.h" #include "str.h" #include "byte.h" #include "buffer.h" static int allwrite(long long (*op)(int, const char *, long long), int fd, const char *buf, long long len) { long long w; if (!buf || len < 0) { errno = EINVAL; return -1; } while (len > 0) { w = op(fd, buf, len); if (w == -1) { if (errno == EINTR) continue; if (errno == EAGAIN) continue; if (errno == EWOULDBLOCK) continue; return -1; } buf += w; len -= w; } return 0; } int buffer_flush(buffer *s) { long long p; p = s->p; if (!p) return 0; s->p = 0; return allwrite(s->op, s->fd, s->x, p); } int buffer_putalign(buffer *s, const char *buf, long long len) { long long n; if (!buf || len < 0) { errno = EINVAL; return -1; } while (len > (n = s->n - s->p)) { byte_copy(s->x + s->p, n, buf); s->p += n; buf += n; len -= n; s->p += n; buf += n; len -= n; if (buffer_flush(s) == -1) return -1; } /* now len <= s->n - s->p */ byte_copy(s->x + s->p, len, buf); s->p += len; return 0; } int buffer_put(buffer *s, const char *buf, long long len) { long long n; if (!buf || len < 0) { errno = EINVAL; return -1; } n = s->n; if (len > n - s->p) { if (buffer_flush(s) == -1) return -1; /* now s->p == 0 */ if (n < BUFFER_OUTSIZE) n = BUFFER_OUTSIZE; while (len > s->n) { if (n > len) n = len; if (allwrite(s->op, s->fd, buf, n) == -1) return -1; buf += n; len -= n; } } /* now len <= s->n - s->p */ byte_copy(s->x + s->p, len, buf); s->p += len; return 0; } int buffer_putflush(buffer *s, const char *buf, long long len) { if (buffer_flush(s) == -1) return -1; return allwrite(s->op, s->fd, buf, len); } int buffer_putsalign(buffer *s, const char *buf) { return buffer_putalign(s, buf, str_len(buf)); } int buffer_puts(buffer *s, const char *buf) { return buffer_put(s, buf, str_len(buf)); } int buffer_putsflush(buffer *s, const char *buf) { return buffer_putflush(s, buf, str_len(buf)); } dq-20250201/buffer_write.c000066400000000000000000000002641474734131400152140ustar00rootroot00000000000000#include "writeall.h" #include "buffer.h" long long buffer_unixwrite(int fd, const char *x, long long xlen) { if (writeall(fd, x, xlen) == -1) return -1; return xlen; } dq-20250201/byte.c000066400000000000000000000031401474734131400134700ustar00rootroot00000000000000/* 20130505 Jan Mojzis Public domain. */ #include "byte.h" void byte_copy(void *yv, long long ylen, const void *xv) { long long i; const char *x = xv; char *y = yv; for (i = 0; i < ylen; ++i) y[i] = x[i]; } void byte_copyr(void *yv, long long ylen, const void *xv) { long long i; const char *x = xv; char *y = yv; for (i = ylen - 1; i >= 0; --i) y[i] = x[i]; } long long byte_chr(const void *yv, long long ylen, int c) { long long i; const char *y = yv; char ch = c; if (ylen <= 0) return ylen; for (i = 0; i < ylen; ++i) if (y[i] == ch) break; return i; } long long byte_rchr(const void *yv, long long ylen, int c) { long long i, u = -1; const char *y = yv; char ch = c; if (ylen <= 0) return ylen; for (i = ylen - 1; i >= 0; --i) if (y[i] == ch) { u = i; break; } if (u == -1) return ylen; return u; } void byte_zero(void *yv, long long ylen) { long long i; char *y = yv; for (i = 0; i < ylen; ++i) y[i] = 0; } int byte_isequal(const void *yv, long long ylen, const void *xv) { long long i; const unsigned char *y = yv; const unsigned char *x = xv; unsigned char diff = 0; for (i = 0; i < ylen; ++i) diff |= x[i] ^ y[i]; return (256 - (unsigned int) diff) >> 8; } int byte_diff(const void *yv, long long ylen, const void *xv) { long long i; const unsigned char *y = yv; const unsigned char *x = xv; for (i = 0; i < ylen; ++i) { if (x[i] == y[i]) continue; return ((int)(unsigned int)y[i]) - ((int)(unsigned int)x[i]); } return 0; } dq-20250201/byte.h000066400000000000000000000006131474734131400134770ustar00rootroot00000000000000#ifndef _BYTE_H____ #define _BYTE_H____ void byte_copy(void *, long long, const void *); void byte_copyr(void *, long long, const void *); long long byte_chr(const void *, long long, int); long long byte_rchr(const void *, long long, int); void byte_zero(void *, long long); int byte_isequal(const void *, long long, const void *); int byte_diff(const void *, long long, const void *); #endif dq-20250201/cache.c000066400000000000000000000206021474734131400135720ustar00rootroot00000000000000#include #include #include #include #include "alloc.h" #include "byte.h" #include "crypto_uint64.h" #include "crypto_uint32.h" #include "seconds.h" #include "die.h" #include "randombytes.h" #include "buffer.h" #include "open.h" #include "dns.h" #include "crypto_auth_siphash24.h" #include "e.h" #include "cache.h" crypto_uint64 cache_motion = 0; crypto_uint64 cache_hit = 0; crypto_uint64 cache_miss = 0; static unsigned char *x = 0; static crypto_uint32 size; static crypto_uint32 hsize; static crypto_uint32 writer; static crypto_uint32 oldest; static crypto_uint32 unused; static unsigned char hashkey[crypto_auth_siphash24_KEYBYTES]; /* 100 <= size <= 3000000000. 4 <= hsize <= size/16. hsize is a power of 2. hsize <= writer <= oldest <= unused <= size. If oldest == unused then unused == size. x is a hash table with the following structure: x[0...hsize-1]: hsize/4 head links. x[hsize...writer-1]: consecutive entries, newest entry on the right. x[writer...oldest-1]: free space for new entries. x[oldest...unused-1]: consecutive entries, oldest entry on the left. x[unused...size-1]: unused. Each hash bucket is a linked list containing the following items: the head link, the newest entry, the second-newest entry, etc. Each link is a 4-byte number giving the xor of the positions of the adjacent items in the list. Entries are always inserted immediately after the head and removed at the tail. Each entry contains the following information: 4-byte link; 4-byte keylen; 4-byte datalen; 8-byte expire time; key; data. */ #define MAXKEYLEN 1000 #define MAXDATALEN 1000000 static void cache_impossible(void) { die_0(111); } static void set4(crypto_uint32 pos, crypto_uint32 u) { if (pos > size - 4) cache_impossible(); crypto_uint32_store(x + pos, u); } static crypto_uint32 get4(crypto_uint32 pos) { crypto_uint32 result; if (pos > size - 4) cache_impossible(); result = crypto_uint32_load(x + pos); return result; } #if 0 static crypto_uint32 hash(const unsigned char *key, crypto_uint32 keylen) { unsigned int result = 5381; while (keylen) { result = (result << 5) + result; result ^= (unsigned char) *key; ++key; --keylen; } result <<= 2; result &= hsize - 4; return result; } #else static crypto_uint32 hash(const unsigned char *key, crypto_uint32 keylen) { unsigned char a[crypto_auth_siphash24_BYTES]; crypto_auth_siphash24(a, key, keylen, hashkey); return (crypto_uint32_load(a) & (hsize - 4)); } #endif unsigned char *cache_get(const unsigned char *key, long long keylen, long long *datalen, long long *ttl, unsigned char *flags) { crypto_uint32 pos; crypto_uint32 prevpos; crypto_uint32 nextpos; crypto_uint32 u; long long loop; long long xttl; unsigned char dummy; unsigned char expirestr[8]; if (!flags) flags = &dummy; if (!x) return 0; if (keylen > MAXKEYLEN) return 0; prevpos = hash(key, keylen); pos = get4(prevpos); loop = 0; *ttl = 0; while (pos) { if (get4(pos + 4) == keylen) { if (pos + 20 + keylen > size) cache_impossible(); if (byte_isequal(key, keylen, x + pos + 20)) { byte_copy(expirestr, 8, x + pos + 12); *flags = expirestr[7]; expirestr[7] = 0; xttl = crypto_uint64_load(expirestr) - seconds(); if (xttl <= 0) return 0; if (xttl > 604800) xttl = 604800; *ttl = xttl; u = get4(pos + 8); if (u > size - pos - 20 - keylen) cache_impossible(); *datalen = u; ++cache_hit; return x + pos + 20 + keylen; } } nextpos = prevpos ^ get4(pos); prevpos = pos; pos = nextpos; if (++loop > 100) { ++cache_miss; return 0; } /* to protect against hash flooding */ } ++cache_miss; return 0; } void cache_set(const unsigned char *key, long long keylen, const unsigned char *data, long long datalen, long long ttl, unsigned char flags) { crypto_uint32 entrylen; crypto_uint32 keyhash; crypto_uint32 pos; if (!x) return; if (keylen > MAXKEYLEN || keylen < 0) return; if (datalen > MAXDATALEN || datalen < 0) return; if (ttl <= 0) return; if (ttl > 604800) ttl = 604800; entrylen = keylen + datalen + 20; while (writer + entrylen > oldest) { if (oldest == unused) { if (writer <= hsize) return; unused = writer; oldest = hsize; writer = hsize; } pos = get4(oldest); set4(pos,get4(pos) ^ oldest); oldest += get4(oldest + 4) + get4(oldest + 8) + 20; if (oldest > unused) cache_impossible(); if (oldest == unused) { unused = size; oldest = size; } } keyhash = hash(key, keylen); pos = get4(keyhash); if (pos) set4(pos,get4(pos) ^ keyhash ^ writer); set4(writer,pos ^ keyhash); set4(writer + 4, keylen); set4(writer + 8, datalen); crypto_uint64_store(x + writer + 12, seconds() + ttl); x[writer + 12 + 7] = flags; byte_copy(x + writer + 20, keylen, key); byte_copy(x + writer + 20 + keylen, datalen, data); set4(keyhash, writer); writer += entrylen; cache_motion += entrylen; } int cache_init(long long cachesize) { randombytes(hashkey, sizeof hashkey); if (x) { alloc_free(x); x = 0; } if (cachesize > 3000000000LL) cachesize = 3000000000LL; if (cachesize < 100) cachesize = 100; size = cachesize; hsize = 4; while (hsize <= (size >> 5)) hsize <<= 1; x = alloc(size); if (!x) return 0; byte_zero(x, size); writer = hsize; oldest = size; unused = size; return 1; } static const char fn[]="dump/dnsdata"; static const char fntmp[]="dump/dnsdata.tmp"; char bspace[8096]; buffer b; void cache_clean(void) { unlink(fn); die_0(0); } int cache_dump(void) { crypto_uint32 pos; long long len; int r; int fd; fd = open_trunc(fntmp); if (fd == -1) return -1; buffer_init(&b, buffer_unixwrite, fd, bspace, sizeof bspace); pos = oldest; while (pos < unused) { len = get4(pos + 4) + get4(pos + 8) + 16; if (byte_diff(x + pos + 20, 2, DNS_T_AXFR)){ if (byte_diff(x + pos + 20, 2, DNS_T_ANY)){ r = buffer_put(&b, (char *)x + pos + 4, len); if (r == -1) { close(fd); return -1; } } } pos += 4 + len; } pos = hsize; while (pos < writer) { len = get4(pos + 4) + get4(pos + 8) + 16; if (byte_diff(x + pos + 20, 2, DNS_T_AXFR)){ if (byte_diff(x + pos + 20, 2, DNS_T_ANY)){ r = buffer_put(&b, (char *)x + pos + 4, len); if (r == -1) { close(fd); return -1; } } } pos += 4 + len; } if (buffer_flush(&b) == -1) { close(fd); return -1; } if (fsync(fd) == -1) { close(fd); return -1; } if (close(fd) == -1) return -1; if (chmod(fntmp, 0600) == -1) return -1; if (rename(fntmp,fn) == -1) return -1; return 0; } int cache_load(void) { unsigned char *p, *xx; crypto_uint32 pos; long long len; crypto_uint32 keylen; crypto_uint32 datalen; int nb; struct stat st; int fd; int flags = 0; long long now, expire; unsigned char expirestr[8]; fd = open_read(fn); if (fd == -1) { if (errno == ENOENT) return 0; return -1; } if (fstat(fd,&st) == -1) { close(fd); return -1; } if (st.st_size == 0) { close(fd); return 0; } xx = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (xx == MAP_FAILED) {close(fd); return -1;} len = st.st_size; p = xx; now = seconds(); pos = 0; nb = 0; while (pos + 16 <= len) { keylen = crypto_uint32_load(p + pos); datalen = crypto_uint32_load(p + pos + 4); byte_copy(expirestr, 8, p + pos + 8); flags = expirestr[7]; expirestr[7] = 0; expire = crypto_uint64_load(expirestr) - now; pos += 16; if (pos + keylen + datalen > len) break; /* missing data */ if (expire > 0) { cache_set(p + pos, keylen, p + pos + keylen, datalen, expire, flags); } pos += keylen + datalen; nb++; } munmap(xx, st.st_size); close(fd); return 0; } dq-20250201/cache.h000066400000000000000000000006221474734131400135770ustar00rootroot00000000000000#ifndef _CACHE_H____ #define _CACHE_H____ extern int cache_init(long long); extern void cache_set(const unsigned char *, long long, const unsigned char *, long long, long long, unsigned char); extern unsigned char *cache_get(const unsigned char *, long long, long long *, long long *, unsigned char *); extern void cache_clean(void); extern int cache_dump(void); extern int cache_load(void); #endif dq-20250201/case.c000066400000000000000000000021021474734131400134350ustar00rootroot00000000000000#include "case.h" int case_diffb(const void *yv, long long ylen, const void *xv) { unsigned char a, b; const unsigned char *y = yv; const unsigned char *x = xv; while (ylen > 0) { a = *x++ - 'A'; b = *y++ - 'A'; --ylen; if (a <= 'Z' - 'A') a += 'a'; else a += 'A'; if (b <= 'Z' - 'A') b += 'a'; else b += 'A'; if (a != b) return ((int)(unsigned int)b) - ((int)(unsigned int)a); } return 0; } int case_diffs(const void *yv, const void *xv) { unsigned char a, b; const unsigned char *y = yv; const unsigned char *x = xv; for (;;) { a = *x++ - 'A'; b = *y++ - 'A'; if (a <= 'Z' - 'A') a += 'a'; else a += 'A'; if (b <= 'Z' - 'A') b += 'a'; else b += 'A'; if (a != b) break; if (!a) break; } return ((int)(unsigned int)b) - ((int)(unsigned int)a); } void case_lowerb(void *xv, long long len) { unsigned char *x = xv; unsigned char y; while (len > 0) { --len; y = *x - 'A'; if (y <= 'Z' - 'A') *x = y + 'a'; ++x; } } dq-20250201/case.h000066400000000000000000000004001474734131400134410ustar00rootroot00000000000000#ifndef _CASE_H____ #define _CASE_H____ extern int case_diffb(const void *, long long, const void *); extern int case_diffs(const void *, const void *); extern void case_lowerb(void *, long long); #define case_equals(s,t) (!case_diffs((s),(t))) #endif dq-20250201/crypto_auth_siphash24.c000066400000000000000000000010011474734131400167450ustar00rootroot00000000000000#include "siphash.h" #include "crypto_verify_8.h" #include "crypto_auth_siphash24.h" int crypto_auth_siphash24_tinynacl(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *k) { return siphash(out, in, inlen, k, 2, 4); } int crypto_auth_siphash24_tinynacl_verify(const unsigned char *h, const unsigned char *m, unsigned long long n, const unsigned char *k) { unsigned char x[8]; crypto_auth_siphash24_tinynacl(x, m, n, k); return crypto_verify_8(h, x); } dq-20250201/crypto_auth_siphash24.h000066400000000000000000000014611474734131400167640ustar00rootroot00000000000000#ifndef crypto_auth_siphash24_H #define crypto_auth_siphash24_H #define crypto_auth_siphash24_tinynacl_BYTES 8 #define crypto_auth_siphash24_tinynacl_KEYBYTES 16 extern int crypto_auth_siphash24_tinynacl(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *); extern int crypto_auth_siphash24_tinynacl_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *); #define crypto_auth_siphash24 crypto_auth_siphash24_tinynacl #define crypto_auth_siphash24_verify crypto_auth_siphash24_tinynacl_verify #define crypto_auth_siphash24_BYTES crypto_auth_siphash24_tinynacl_BYTES #define crypto_auth_siphash24_KEYBYTES crypto_auth_siphash24_tinynacl_KEYBYTES #define crypto_auth_siphash24_IMPLEMENTATION "tinynacl" #define crypto_auth_siphash24_VERSION "-" #endif dq-20250201/crypto_box_curve25519xsalsa20poly1305.c000066400000000000000000000037261474734131400213340ustar00rootroot00000000000000#include "crypto_core_hsalsa20.h" #include "crypto_scalarmult_curve25519.h" #include "crypto_secretbox_xsalsa20poly1305.h" #include "randombytes.h" #include "crypto_box_curve25519xsalsa20poly1305.h" static const unsigned char zero[16] = {0}; static const unsigned char sigma[16] = "expand 32-byte k"; int crypto_box_curve25519xsalsa20poly1305_tinynacl_beforenm( unsigned char *k, const unsigned char *pk, const unsigned char *sk ) { unsigned char s[32]; crypto_scalarmult_curve25519(s,sk,pk); return crypto_core_hsalsa20(k, zero, s, sigma); } int crypto_box_curve25519xsalsa20poly1305_tinynacl_afternm( unsigned char *c, const unsigned char *m,unsigned long long mlen, const unsigned char *n, const unsigned char *k ) { return crypto_secretbox_xsalsa20poly1305(c,m,mlen,n,k); } int crypto_box_curve25519xsalsa20poly1305_tinynacl_open_afternm( unsigned char *m, const unsigned char *c,unsigned long long clen, const unsigned char *n, const unsigned char *k ) { return crypto_secretbox_xsalsa20poly1305_open(m,c,clen,n,k); } int crypto_box_curve25519xsalsa20poly1305_tinynacl( unsigned char *c, const unsigned char *m,unsigned long long mlen, const unsigned char *n, const unsigned char *pk, const unsigned char *sk ) { unsigned char k[32]; crypto_box_curve25519xsalsa20poly1305_tinynacl_beforenm(k,pk,sk); return crypto_box_curve25519xsalsa20poly1305_tinynacl_afternm(c,m,mlen,n,k); } int crypto_box_curve25519xsalsa20poly1305_tinynacl_open( unsigned char *m, const unsigned char *c,unsigned long long clen, const unsigned char *n, const unsigned char *pk, const unsigned char *sk ) { unsigned char k[32]; crypto_box_curve25519xsalsa20poly1305_tinynacl_beforenm(k,pk,sk); return crypto_box_curve25519xsalsa20poly1305_tinynacl_open_afternm(m,c,clen,n,k); } int crypto_box_curve25519xsalsa20poly1305_tinynacl_keypair( unsigned char *pk, unsigned char *sk ) { randombytes(sk,32); return crypto_scalarmult_curve25519_base(pk,sk); } dq-20250201/crypto_box_curve25519xsalsa20poly1305.h000066400000000000000000000056231474734131400213370ustar00rootroot00000000000000#ifndef crypto_box_curve25519xsalsa20poly1305_H #define crypto_box_curve25519xsalsa20poly1305_H #define crypto_box_curve25519xsalsa20poly1305_tinynacl_PUBLICKEYBYTES 32 #define crypto_box_curve25519xsalsa20poly1305_tinynacl_SECRETKEYBYTES 32 #define crypto_box_curve25519xsalsa20poly1305_tinynacl_BEFORENMBYTES 32 #define crypto_box_curve25519xsalsa20poly1305_tinynacl_NONCEBYTES 24 #define crypto_box_curve25519xsalsa20poly1305_tinynacl_ZEROBYTES 32 #define crypto_box_curve25519xsalsa20poly1305_tinynacl_BOXZEROBYTES 16 extern int crypto_box_curve25519xsalsa20poly1305_tinynacl(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *); extern int crypto_box_curve25519xsalsa20poly1305_tinynacl_open(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *); extern int crypto_box_curve25519xsalsa20poly1305_tinynacl_keypair(unsigned char *,unsigned char *); extern int crypto_box_curve25519xsalsa20poly1305_tinynacl_beforenm(unsigned char *,const unsigned char *,const unsigned char *); extern int crypto_box_curve25519xsalsa20poly1305_tinynacl_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *); extern int crypto_box_curve25519xsalsa20poly1305_tinynacl_open_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *); #define crypto_box_curve25519xsalsa20poly1305 crypto_box_curve25519xsalsa20poly1305_tinynacl #define crypto_box_curve25519xsalsa20poly1305_open crypto_box_curve25519xsalsa20poly1305_tinynacl_open #define crypto_box_curve25519xsalsa20poly1305_keypair crypto_box_curve25519xsalsa20poly1305_tinynacl_keypair #define crypto_box_curve25519xsalsa20poly1305_beforenm crypto_box_curve25519xsalsa20poly1305_tinynacl_beforenm #define crypto_box_curve25519xsalsa20poly1305_afternm crypto_box_curve25519xsalsa20poly1305_tinynacl_afternm #define crypto_box_curve25519xsalsa20poly1305_open_afternm crypto_box_curve25519xsalsa20poly1305_tinynacl_open_afternm #define crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES crypto_box_curve25519xsalsa20poly1305_tinynacl_PUBLICKEYBYTES #define crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES crypto_box_curve25519xsalsa20poly1305_tinynacl_SECRETKEYBYTES #define crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES crypto_box_curve25519xsalsa20poly1305_tinynacl_BEFORENMBYTES #define crypto_box_curve25519xsalsa20poly1305_NONCEBYTES crypto_box_curve25519xsalsa20poly1305_tinynacl_NONCEBYTES #define crypto_box_curve25519xsalsa20poly1305_ZEROBYTES crypto_box_curve25519xsalsa20poly1305_tinynacl_ZEROBYTES #define crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES crypto_box_curve25519xsalsa20poly1305_tinynacl_BOXZEROBYTES #define crypto_box_curve25519xsalsa20poly1305_IMPLEMENTATION "tinynacl" #define crypto_box_curve25519xsalsa20poly1305_VERSION "-" #endif dq-20250201/crypto_core_hsalsa20.c000066400000000000000000000010061474734131400165510ustar00rootroot00000000000000#include "salsa.h" #include "crypto_uint32.h" #include "crypto_core_hsalsa20.h" int crypto_core_hsalsa20_tinynacl(unsigned char *o, const unsigned char *nn, const unsigned char *kk, const unsigned char *cc) { crypto_uint32 k[8], n[4], c[4]; long long i; for (i = 0; i < 8; ++i) k[i] = crypto_uint32_load(kk + 4 * i); for (i = 0; i < 4; ++i) n[i] = crypto_uint32_load(nn + 4 * i); for (i = 0; i < 4; ++i) c[i] = crypto_uint32_load(cc + 4 * i); salsa_core(o, n, k, c, 1, 20); return 0; } dq-20250201/crypto_core_hsalsa20.h000066400000000000000000000015611474734131400165640ustar00rootroot00000000000000#ifndef crypto_core_hsalsa20_H #define crypto_core_hsalsa20_H #define crypto_core_hsalsa20_tinynacl_OUTPUTBYTES 32 #define crypto_core_hsalsa20_tinynacl_INPUTBYTES 16 #define crypto_core_hsalsa20_tinynacl_KEYBYTES 32 #define crypto_core_hsalsa20_tinynacl_CONSTBYTES 16 extern int crypto_core_hsalsa20_tinynacl(unsigned char *,const unsigned char *,const unsigned char *,const unsigned char *); #define crypto_core_hsalsa20 crypto_core_hsalsa20_tinynacl #define crypto_core_hsalsa20_OUTPUTBYTES crypto_core_hsalsa20_tinynacl_OUTPUTBYTES #define crypto_core_hsalsa20_INPUTBYTES crypto_core_hsalsa20_tinynacl_INPUTBYTES #define crypto_core_hsalsa20_KEYBYTES crypto_core_hsalsa20_tinynacl_KEYBYTES #define crypto_core_hsalsa20_CONSTBYTES crypto_core_hsalsa20_tinynacl_CONSTBYTES #define crypto_core_hsalsa20_IMPLEMENTATION "tinynacl" #define crypto_core_hsalsa20_VERSION "-" #endif dq-20250201/crypto_onetimeauth_poly1305.c000066400000000000000000000130451474734131400200300ustar00rootroot00000000000000/* 20250126 */ /* Based on poly1305-donna (https://github.com/floodyberry/poly1305-donna) - modified for NaCl API - switched to cryptoint */ #include "crypto_int16.h" #include "crypto_uint32.h" #include "crypto_uint64.h" #include "crypto_onetimeauth_poly1305.h" /* clang-format off */ int crypto_onetimeauth_poly1305_tinynacl(unsigned char *o, const unsigned char *m, unsigned long long n, const unsigned char *k) { crypto_uint32 h0, h1, h2, h3, h4; crypto_uint32 r0, r1, r2, r3, r4; crypto_uint32 s1, s2, s3, s4; crypto_uint64 d0, d1, d2, d3, d4; crypto_uint32 c, mask; crypto_uint64 f; crypto_uint64 i; /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */ r0 = (crypto_uint32_load(k + 0) ) & 0x3ffffff; r1 = (crypto_uint32_load(k + 3) >> 2) & 0x3ffff03; r2 = (crypto_uint32_load(k + 6) >> 4) & 0x3ffc0ff; r3 = (crypto_uint32_load(k + 9) >> 6) & 0x3f03fff; r4 = (crypto_uint32_load(k + 12) >> 8) & 0x00fffff; s1 = r1 * 5; s2 = r2 * 5; s3 = r3 * 5; s4 = r4 * 5; /* h = 0 */ h0 = h1 = h2 = h3 = h4 = 0; while (n > 0) { /* h += m[i] */ if (n >= 16) { h0 += (crypto_uint32_load(m ) ) & 0x3ffffff; h1 += (crypto_uint32_load(m + 3) >> 2) & 0x3ffffff; h2 += (crypto_uint32_load(m + 6) >> 4) & 0x3ffffff; h3 += (crypto_uint32_load(m + 9) >> 6) & 0x3ffffff; h4 += (crypto_uint32_load(m + 12) >> 8) | 16777216; m += 16; n -= 16; } else { unsigned char mm[16]; for (i = 0; i < 16; ++i) mm[i] = 0; for (i = 0; i < n; ++i) mm[i] = m[i]; mm[i] = 1; h0 += (crypto_uint32_load(mm ) ) & 0x3ffffff; h1 += (crypto_uint32_load(mm + 3) >> 2) & 0x3ffffff; h2 += (crypto_uint32_load(mm + 6) >> 4) & 0x3ffffff; h3 += (crypto_uint32_load(mm + 9) >> 6) & 0x3ffffff; h4 += (crypto_uint32_load(mm + 12) >> 8); n = 0; } /* h *= r */ d0 = ((crypto_uint64)h0 * r0) + ((crypto_uint64)h1 * s4) + ((crypto_uint64)h2 * s3) + ((crypto_uint64)h3 * s2) + ((crypto_uint64)h4 * s1); d1 = ((crypto_uint64)h0 * r1) + ((crypto_uint64)h1 * r0) + ((crypto_uint64)h2 * s4) + ((crypto_uint64)h3 * s3) + ((crypto_uint64)h4 * s2); d2 = ((crypto_uint64)h0 * r2) + ((crypto_uint64)h1 * r1) + ((crypto_uint64)h2 * r0) + ((crypto_uint64)h3 * s4) + ((crypto_uint64)h4 * s3); d3 = ((crypto_uint64)h0 * r3) + ((crypto_uint64)h1 * r2) + ((crypto_uint64)h2 * r1) + ((crypto_uint64)h3 * r0) + ((crypto_uint64)h4 * s4); d4 = ((crypto_uint64)h0 * r4) + ((crypto_uint64)h1 * r3) + ((crypto_uint64)h2 * r2) + ((crypto_uint64)h3 * r1) + ((crypto_uint64)h4 * r0); /* (partial) h %= p */ c = (crypto_uint32)(d0 >> 26); h0 = (crypto_uint32)d0 & 0x3ffffff; d1 += c; c = (crypto_uint32)(d1 >> 26); h1 = (crypto_uint32)d1 & 0x3ffffff; d2 += c; c = (crypto_uint32)(d2 >> 26); h2 = (crypto_uint32)d2 & 0x3ffffff; d3 += c; c = (crypto_uint32)(d3 >> 26); h3 = (crypto_uint32)d3 & 0x3ffffff; d4 += c; c = (crypto_uint32)(d4 >> 26); h4 = (crypto_uint32)d4 & 0x3ffffff; h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff; h1 += c; } /* fully carry h */ c = h1 >> 26; h1 = h1 & 0x3ffffff; h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff; h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff; h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff; h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff; h1 += c; /* compute h + -p */ r0 = h0 + 5; c = r0 >> 26; r0 &= 0x3ffffff; r1 = h1 + c; c = r1 >> 26; r1 &= 0x3ffffff; r2 = h2 + c; c = r2 >> 26; r2 &= 0x3ffffff; r3 = h3 + c; c = r3 >> 26; r3 &= 0x3ffffff; r4 = h4 + c - (1 << 26); /* select h if h < p, or h + -p if h >= p */ mask = crypto_uint32_topbit_01(r4); mask -= 1; r0 ^= h0; r1 ^= h1; r2 ^= h2; r3 ^= h3; r4 ^= h4; h0 ^= mask & r0; h1 ^= mask & r1; h2 ^= mask & r2; h3 ^= mask & r3; h4 ^= mask & r4; /* h = h % (2^128) */ h0 = ((h0 ) | (h1 << 26)) & 0xffffffff; h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff; h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff; h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff; /* mac = (h + pad) % (2^128) */ f = (crypto_uint64)h0 + crypto_uint32_load(k + 16) ; h0 = (crypto_uint32)f; f = (crypto_uint64)h1 + crypto_uint32_load(k + 20) + (f >> 32); h1 = (crypto_uint32)f; f = (crypto_uint64)h2 + crypto_uint32_load(k + 24) + (f >> 32); h2 = (crypto_uint32)f; f = (crypto_uint64)h3 + crypto_uint32_load(k + 28) + (f >> 32); h3 = (crypto_uint32)f; crypto_uint32_store(o + 0, h0); crypto_uint32_store(o + 4, h1); crypto_uint32_store(o + 8, h2); crypto_uint32_store(o + 12, h3); return 0; } int crypto_onetimeauth_poly1305_tinynacl_verify(const unsigned char *h, const unsigned char *in, unsigned long long l, const unsigned char *k) { unsigned char correct[16]; crypto_int16 d = 0; long long i; crypto_onetimeauth_poly1305(correct, in, l, k); for (i = 0; i < 16; ++i) d |= correct[i] ^ h[i]; return crypto_int16_nonzero_mask(d); } /* clang-format on */ dq-20250201/crypto_onetimeauth_poly1305.h000066400000000000000000000025041474734131400200330ustar00rootroot00000000000000#ifndef crypto_onetimeauth_poly1305_H #define crypto_onetimeauth_poly1305_H #define crypto_onetimeauth_poly1305_tinynacl_BYTES 16 #define crypto_onetimeauth_poly1305_tinynacl_KEYBYTES 32 extern int crypto_onetimeauth_poly1305_tinynacl(unsigned char *, const unsigned char *, unsigned long long, const unsigned char *); extern int crypto_onetimeauth_poly1305_tinynacl_verify(const unsigned char *, const unsigned char *, unsigned long long, const unsigned char *); #define crypto_onetimeauth_poly1305 crypto_onetimeauth_poly1305_tinynacl #define crypto_onetimeauth_poly1305_verify \ crypto_onetimeauth_poly1305_tinynacl_verify #define crypto_onetimeauth_poly1305_BYTES \ crypto_onetimeauth_poly1305_tinynacl_BYTES #define crypto_onetimeauth_poly1305_KEYBYTES \ crypto_onetimeauth_poly1305_tinynacl_KEYBYTES #define crypto_onetimeauth_poly1305_IMPLEMENTATION "tinynacl" #define crypto_onetimeauth_poly1305_VERSION "-" #endif dq-20250201/crypto_scalarmult_curve25519.c000066400000000000000000000215011474734131400201070ustar00rootroot00000000000000/* 20241110 */ #include "crypto_scalarmult_curve25519.h" #ifndef HASLIB25519 #include "crypto_uint8.h" #include "crypto_uint32.h" #include "crypto_uint64.h" typedef crypto_uint32 fe[8]; /* o = x */ static void fe_copy(fe o, const fe x) { long long i; for (i = 0; i < 8; ++i) o[i] = x[i]; } /* o = 0 */ static void fe_0(fe o) { long long i; for (i = 0; i < 8; ++i) o[i] = 0; } /* o = 1 */ static void fe_1(fe o) { fe_0(o); o[0] = 1; } /* if (b) swap(f, g) */ static void fe_cswap(fe f, fe g, crypto_uint32 b) { long long i; fe t; b = -b; for (i = 0; i < 8; ++i) t[i] = b & (f[i] ^ g[i]); for (i = 0; i < 8; ++i) f[i] ^= t[i]; for (i = 0; i < 8; ++i) g[i] ^= t[i]; } static void fe_frombytes(fe out, const unsigned char *in) { long long i; /* unpack from little-endian format */ for (i = 0; i < 8; ++i) out[i] = crypto_uint32_load(in + 4 * i); out[7] &= 0x7fffffff; } /* select r if r < p, or r + -p if r >= p */ static void fe_squeeze(fe r) { crypto_uint64 pb = 0, b; long long i; fe t; const crypto_uint64 p[8] = { 0xffffffedULL, 0xffffffffULL, 0xffffffffULL, 0xffffffffULL, 0xffffffffULL, 0xffffffffULL, 0xffffffffULL, 0x7fffffffULL, }; for (i = 0; i < 8; ++i) { pb += p[i]; b = (crypto_uint64)r[i] - pb; b = crypto_uint64_topbit_01(b); t[i] = (crypto_uint64)r[i] - pb + (b << 32); pb = b; } b = -pb; b = crypto_uint64_topbit_01(b); b -= 1; for (i = 0; i < 8; ++i) r[i] ^= b & (r[i] ^ t[i]); } static void fe_tobytes(unsigned char *out, const fe in) { fe r; long long i; fe_copy(r, in); fe_squeeze(r); for (i = 0; i < 8; ++i) crypto_uint32_store(out + 4 * i, r[i]); } /* o = (x * c) % p */ static void fe_mulc(fe o, const fe x, const crypto_uint64 c) { crypto_uint64 u = 0; long long i; for (i = 0; i < 7; ++i) { u += (crypto_uint64)x[i] * c; o[i] = u & 0xffffffff; u >>= 32; } u += (crypto_uint64)x[i] * c; o[i] = u & 0x7fffffff; u >>= 31; u *= 19ULL; for (i = 0; i < 8; ++i) { u += o[i]; o[i] = u & 0xffffffff; u >>= 32; } } /* o = (a * b) % p */ static void fe_mul(fe o, const fe a, const fe b) { crypto_uint64 u, t[16]; long long i; for (i = 0; i < 16; ++i) t[i] = 0; /* Unrolled version of: for (i = 0; i < 8; ++i) for (j = 0; j < 8; ++j) { u = (crypto_uint64)a[i] * (crypto_uint64)b[j]; t[i + j ] += u & 0xffffffff; t[i + j + 1] += u >> 32; } */ #define M(i, j) u = (crypto_uint64)a[i] * (crypto_uint64)b[j]; \ t[i + j ] += u & 0xffffffff; \ t[i + j + 1] += u >> 32; M(0, 0); M(0, 1); M(0, 2); M(0, 3); M(0, 4); M(0, 5); M(0, 6); M(0, 7); M(1, 0); M(1, 1); M(1, 2); M(1, 3); M(1, 4); M(1, 5); M(1, 6); M(1, 7); M(2, 0); M(2, 1); M(2, 2); M(2, 3); M(2, 4); M(2, 5); M(2, 6); M(2, 7); M(3, 0); M(3, 1); M(3, 2); M(3, 3); M(3, 4); M(3, 5); M(3, 6); M(3, 7); M(4, 0); M(4, 1); M(4, 2); M(4, 3); M(4, 4); M(4, 5); M(4, 6); M(4, 7); M(5, 0); M(5, 1); M(5, 2); M(5, 3); M(5, 4); M(5, 5); M(5, 6); M(5, 7); M(6, 0); M(6, 1); M(6, 2); M(6, 3); M(6, 4); M(6, 5); M(6, 6); M(6, 7); M(7, 0); M(7, 1); M(7, 2); M(7, 3); M(7, 4); M(7, 5); M(7, 6); M(7, 7); #undef M u = 0; for (i = 0; i < 7; ++i) { u += t[i] + 38ULL * t[i + 8]; t[i] = u & 0xffffffff; u >>= 32; } u += t[i] + 38ULL * t[i + 8]; t[i] = u & 0x7fffffff; u >>= 31; u *= 19ULL; for (i = 0; i < 8; ++i) { u += t[i]; o[i] = u & 0xffffffff; u >>= 32; } } /* o = (x ^ 2) % p */ static void fe_sq(fe o, const fe a) { crypto_uint64 u, t[16]; long long i; for (i = 0; i < 16; ++i) t[i] = 0; /* Unrolled version of: for (i = 0; i < 8; ++i) for (j = i + 1; j < 8; ++j) { u = (crypto_uint64)a[i] * (crypto_uint64)a[j]; t[i + j ] += 2 * (u & 0xffffffff); t[i + j + 1] += 2 * (u >> 32); } for (i = 0; i < 8; ++i) { u = (crypto_uint64)a[i] * (crypto_uint64)a[i]; t[2 * i ] += u & 0xffffffff; t[2 * i + 1] += u >> 32; } */ #define M(i, j) u = (crypto_uint64)a[i] * (crypto_uint64)a[j]; \ t[i + j ] += (u & 0x00000000ffffffff) << 1; \ t[i + j + 1] += (u & 0xffffffff00000000) >> 31; M(0, 1); M(0, 2); M(0, 3); M(0, 4); M(0, 5); M(0, 6); M(0, 7); M(1, 2); M(1, 3); M(1, 4); M(1, 5); M(1, 6); M(1, 7); M(2, 3); M(2, 4); M(2, 5); M(2, 6); M(2, 7); M(3, 4); M(3, 5); M(3, 6); M(3, 7); M(4, 5); M(4, 6); M(4, 7); M(5, 6); M(5, 7); M(6, 7); #undef M #define S(i) u = (crypto_uint64)a[i] * (crypto_uint64)a[i]; \ t[2 * i ] += (u & 0xffffffff); \ t[2 * i + 1] += (u >> 32) S(0); S(1); S(2); S(3); S(4); S(5); S(6); S(7); #undef S u = 0; for (i = 0; i < 7; ++i) { u += t[i] + 38ULL * t[i + 8]; t[i] = u & 0xffffffff; u >>= 32; } u += t[i] + 38ULL * t[i + 8]; t[i] = u & 0x7fffffff; u >>= 31; u *= 19ULL; for (i = 0; i < 8; ++i) { u += t[i]; o[i] = u & 0xffffffff; u >>= 32; } } /* o = (x + y) % p */ static void fe_add(fe o, const fe x, const fe y) { crypto_uint64 u = 0; long long i; for (i = 0; i < 7; ++i) { u += (crypto_uint64)x[i] + (crypto_uint64)y[i]; o[i] = u & 0xffffffff; u >>= 32; } u += (crypto_uint64)x[i] + (crypto_uint64)y[i]; o[i] = u & 0x7fffffff; u >>= 31; u *= 19ULL; for (i = 0; i < 8; ++i) { u += o[i]; o[i] = u & 0xffffffff; u >>= 32; } } /* o = (x - y) % p */ static void fe_sub(fe o, const fe x, const fe y) { long long i; crypto_uint64 u = 0; const crypto_uint64 p4[8] = { 0x3ffffffb4ULL, 0x3fffffffcULL, 0x3fffffffcULL, 0x3fffffffcULL, 0x3fffffffcULL, 0x3fffffffcULL, 0x3fffffffcULL, 0x1fffffffcULL, }; for (i = 0; i < 7; ++i) { u += p4[i] - (crypto_uint64)y[i] + (crypto_uint64)x[i]; o[i] = u & 0xffffffff; u >>= 32; } u += p4[i] - (crypto_uint64)y[i] + (crypto_uint64)x[i]; o[i] = u & 0x7fffffff; u >>= 31; u *= 19ULL; for (i = 0; i < 8; ++i) { u += o[i]; o[i] = u & 0xffffffff; u >>= 32; } } /* o = (1 / z) % p */ static void fe_inv(fe o, const fe z) { fe t0, t1, t2, t3; long long i; fe_sq(t0, z); for (i = 1; i < 1; ++i) fe_sq(t0, t0); fe_sq(t1,t0); for (i = 1; i < 2; ++i) fe_sq(t1, t1); fe_mul(t1, z, t1); fe_mul(t0, t0, t1); fe_sq(t2, t0); for (i = 1; i < 1; ++i) fe_sq(t2, t2); fe_mul(t1, t1, t2); fe_sq(t2, t1); for (i = 1; i < 5; ++i) fe_sq(t2, t2); fe_mul(t1, t2, t1); fe_sq(t2, t1); for (i = 1; i < 10; ++i) fe_sq(t2, t2); fe_mul(t2, t2, t1); fe_sq(t3, t2); for (i = 1; i < 20; ++i) fe_sq(t3, t3); fe_mul(t2, t3, t2); fe_sq(t2, t2); for (i = 1; i < 10; ++i) fe_sq(t2, t2); fe_mul(t1, t2, t1); fe_sq(t2, t1); for (i = 1; i < 50; ++i) fe_sq(t2, t2); fe_mul(t2, t2, t1); fe_sq(t3, t2); for (i = 1; i < 100; ++i) fe_sq(t3, t3); fe_mul(t2, t3, t2); fe_sq(t2, t2); for (i = 1; i < 50; ++i) fe_sq(t2, t2); fe_mul(t1, t2, t1); fe_sq(t1, t1); for (i = 1; i < 5; ++i) fe_sq(t1, t1); fe_mul(o, t1, t0); } int crypto_scalarmult_curve25519_tinynacl(unsigned char *q, const unsigned char *n, const unsigned char *p) { unsigned char e[32]; fe x1, x2, z2, x3, z3, tmp0, tmp1; long long i, pos; crypto_uint32 swap, b; for (i = 0; i < 32; ++i) e[i] = n[i]; e[0] &= 248; e[31] &= 127; e[31] |= 64; fe_frombytes(x1, p); fe_1(x2); fe_0(z2); fe_copy(x3, x1); fe_1(z3); swap = 0; for (pos = 254; pos >= 0; --pos) { b = crypto_uint8_bitmod_01(e[pos / 8], pos); swap ^= b; fe_cswap(x2, x3, swap); fe_cswap(z2, z3, swap); swap = b; fe_sub(tmp0, x3, z3); fe_sub(tmp1, x2, z2); fe_add(x2, x2, z2); fe_add(z2, x3, z3); fe_mul(z3, tmp0, x2); fe_mul(z2, z2, tmp1); fe_sq(tmp0, tmp1); fe_sq(tmp1, x2); fe_add(x3, z3, z2); fe_sub(z2, z3, z2); fe_mul(x2, tmp1, tmp0); fe_sub(tmp1, tmp1, tmp0); fe_sq(z2, z2); fe_mulc(z3, tmp1, 121666); fe_sq(x3, x3); fe_add(tmp0, tmp0, z3); fe_mul(z3, x1, z2); fe_mul(z2, tmp1, tmp0); } fe_cswap(x2, x3, swap); fe_cswap(z2, z3, swap); fe_inv(z2, z2); fe_mul(x2, x2, z2); fe_tobytes(q, x2); return 0; } static const unsigned char basepoint[32] = {9}; int crypto_scalarmult_curve25519_tinynacl_base(unsigned char *q, const unsigned char *n) { return crypto_scalarmult_curve25519_tinynacl(q, n, basepoint); } #else #include int crypto_scalarmult_curve25519_lib25519(unsigned char *q, const unsigned char *n, const unsigned char *p) { lib25519_nP_montgomery25519(q, n, p); return 0; } int crypto_scalarmult_curve25519_lib25519_base(unsigned char *q, const unsigned char *n) { lib25519_nG_montgomery25519(q, n); return 0; } #endif dq-20250201/crypto_scalarmult_curve25519.h000066400000000000000000000031141474734131400201140ustar00rootroot00000000000000#ifndef crypto_scalarmult_curve25519_H #define crypto_scalarmult_curve25519_H #include "haslib25519.h" #ifndef HASLIB25519 #define crypto_scalarmult_curve25519_tinynacl_BYTES 32 #define crypto_scalarmult_curve25519_tinynacl_SCALARBYTES 32 extern int crypto_scalarmult_curve25519_tinynacl(unsigned char *,const unsigned char *,const unsigned char *); extern int crypto_scalarmult_curve25519_tinynacl_base(unsigned char *,const unsigned char *); #define crypto_scalarmult_curve25519 crypto_scalarmult_curve25519_tinynacl #define crypto_scalarmult_curve25519_base crypto_scalarmult_curve25519_tinynacl_base #define crypto_scalarmult_curve25519_BYTES crypto_scalarmult_curve25519_tinynacl_BYTES #define crypto_scalarmult_curve25519_SCALARBYTES crypto_scalarmult_curve25519_tinynacl_SCALARBYTES #define crypto_scalarmult_curve25519_IMPLEMENTATION "tinynacl" #define crypto_scalarmult_curve25519_VERSION "-" #else #include extern int crypto_scalarmult_curve25519_lib25519(unsigned char *,const unsigned char *,const unsigned char *); extern int crypto_scalarmult_curve25519_lib25519_base(unsigned char *,const unsigned char *); #define crypto_scalarmult_curve25519 crypto_scalarmult_curve25519_lib25519 #define crypto_scalarmult_curve25519_base crypto_scalarmult_curve25519_lib25519_base #define crypto_scalarmult_curve25519_BYTES lib25519_nG_montgomery25519_POINTBYTES #define crypto_scalarmult_curve25519_SCALARBYTES lib25519_nG_montgomery25519_SCALARBYTES #define crypto_scalarmult_curve25519_IMPLEMENTATION "lib25519" #define crypto_scalarmult_curve25519_VERSION lib25519_version #endif #endif dq-20250201/crypto_secretbox_xsalsa20poly1305.c000066400000000000000000000017471474734131400210700ustar00rootroot00000000000000#include "crypto_onetimeauth_poly1305.h" #include "crypto_stream_xsalsa20.h" #include "crypto_secretbox_xsalsa20poly1305.h" int crypto_secretbox_xsalsa20poly1305_tinynacl(unsigned char *c, const unsigned char *m, unsigned long long mlen, const unsigned char *n, const unsigned char *k) { long long i; if (mlen < 32) return -1; crypto_stream_xsalsa20_xor(c, m, mlen, n, k); crypto_onetimeauth_poly1305(c + 16, c + 32, mlen - 32, c); for (i = 0; i < 16; ++i) c[i] = 0; return 0; } int crypto_secretbox_xsalsa20poly1305_tinynacl_open(unsigned char *m, const unsigned char *c, unsigned long long clen, const unsigned char *n, const unsigned char *k) { long long i; unsigned char subkey[32]; if (clen < 32) return -1; crypto_stream_xsalsa20(subkey, 32, n, k); if (crypto_onetimeauth_poly1305_verify(c + 16, c + 32, clen - 32, subkey) != 0) return -1; crypto_stream_xsalsa20_xor(m, c, clen, n, k); for (i = 0; i < 32; ++i) m[i] = 0; return 0; } dq-20250201/crypto_secretbox_xsalsa20poly1305.h000066400000000000000000000026041474734131400210660ustar00rootroot00000000000000#ifndef crypto_secretbox_xsalsa20poly1305_H #define crypto_secretbox_xsalsa20poly1305_H #define crypto_secretbox_xsalsa20poly1305_tinynacl_KEYBYTES 32 #define crypto_secretbox_xsalsa20poly1305_tinynacl_NONCEBYTES 24 #define crypto_secretbox_xsalsa20poly1305_tinynacl_ZEROBYTES 32 #define crypto_secretbox_xsalsa20poly1305_tinynacl_BOXZEROBYTES 16 extern int crypto_secretbox_xsalsa20poly1305_tinynacl(unsigned char *, const unsigned char *, unsigned long long, const unsigned char *, const unsigned char *); extern int crypto_secretbox_xsalsa20poly1305_tinynacl_open(unsigned char *, const unsigned char *, unsigned long long, const unsigned char *, const unsigned char *); #define crypto_secretbox_xsalsa20poly1305 crypto_secretbox_xsalsa20poly1305_tinynacl #define crypto_secretbox_xsalsa20poly1305_open crypto_secretbox_xsalsa20poly1305_tinynacl_open #define crypto_secretbox_xsalsa20poly1305_KEYBYTES crypto_secretbox_xsalsa20poly1305_tinynacl_KEYBYTES #define crypto_secretbox_xsalsa20poly1305_NONCEBYTES crypto_secretbox_xsalsa20poly1305_tinynacl_NONCEBYTES #define crypto_secretbox_xsalsa20poly1305_ZEROBYTES crypto_secretbox_xsalsa20poly1305_tinynacl_ZEROBYTES #define crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES crypto_secretbox_xsalsa20poly1305_tinynacl_BOXZEROBYTES #define crypto_secretbox_xsalsa20poly1305_IMPLEMENTATION "tinynacl" #define crypto_secretbox_xsalsa20poly1305_VERSION "-" #endif dq-20250201/crypto_stream_salsa20.c000066400000000000000000000007341474734131400167530ustar00rootroot00000000000000/* 20140727 Jan Mojzis Public domain. */ #include "salsa.h" #include "crypto_stream_salsa20.h" int crypto_stream_salsa20_tinynacl_xor(unsigned char *c, const unsigned char *m, unsigned long long l, const unsigned char *n, const unsigned char *k) { return salsa_stream_xor(c, m, l, n, k, 20); } int crypto_stream_salsa20_tinynacl(unsigned char *c, unsigned long long l, const unsigned char *n, const unsigned char *k) { return salsa_stream_xor(c, 0, l, n, k, 20); } dq-20250201/crypto_stream_salsa20.h000066400000000000000000000015071474734131400167570ustar00rootroot00000000000000#ifndef crypto_stream_salsa20_H #define crypto_stream_salsa20_H #define crypto_stream_salsa20_tinynacl_KEYBYTES 32 #define crypto_stream_salsa20_tinynacl_NONCEBYTES 8 extern int crypto_stream_salsa20_tinynacl(unsigned char *,unsigned long long,const unsigned char *,const unsigned char *); extern int crypto_stream_salsa20_tinynacl_xor(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *); #define crypto_stream_salsa20 crypto_stream_salsa20_tinynacl #define crypto_stream_salsa20_xor crypto_stream_salsa20_tinynacl_xor #define crypto_stream_salsa20_KEYBYTES crypto_stream_salsa20_tinynacl_KEYBYTES #define crypto_stream_salsa20_NONCEBYTES crypto_stream_salsa20_tinynacl_NONCEBYTES #define crypto_stream_salsa20_IMPLEMENTATION "tinynacl" #define crypto_stream_salsa20_VERSION "-" #endif dq-20250201/crypto_stream_xsalsa20.c000066400000000000000000000013021474734131400171330ustar00rootroot00000000000000/* 20140727 Jan Mojzis Public domain. */ #include "crypto_core_hsalsa20.h" #include "crypto_stream_salsa20.h" #include "crypto_stream_xsalsa20.h" static const unsigned char sigma[16] = "expand 32-byte k"; int crypto_stream_xsalsa20_tinynacl_xor(unsigned char *c, const unsigned char *m, unsigned long long l, const unsigned char *n, const unsigned char *k) { unsigned char subkey[32]; crypto_core_hsalsa20(subkey, n, k, sigma); crypto_stream_salsa20_xor(c, m, l, n + 16, subkey); return 0; } int crypto_stream_xsalsa20_tinynacl(unsigned char *c, unsigned long long l, const unsigned char *n, const unsigned char *k) { return crypto_stream_xsalsa20_tinynacl_xor(c, 0, l, n, k); } dq-20250201/crypto_stream_xsalsa20.h000066400000000000000000000015301474734131400171430ustar00rootroot00000000000000#ifndef crypto_stream_xsalsa20_H #define crypto_stream_xsalsa20_H #define crypto_stream_xsalsa20_tinynacl_KEYBYTES 32 #define crypto_stream_xsalsa20_tinynacl_NONCEBYTES 24 extern int crypto_stream_xsalsa20_tinynacl(unsigned char *,unsigned long long,const unsigned char *,const unsigned char *); extern int crypto_stream_xsalsa20_tinynacl_xor(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *); #define crypto_stream_xsalsa20 crypto_stream_xsalsa20_tinynacl #define crypto_stream_xsalsa20_xor crypto_stream_xsalsa20_tinynacl_xor #define crypto_stream_xsalsa20_KEYBYTES crypto_stream_xsalsa20_tinynacl_KEYBYTES #define crypto_stream_xsalsa20_NONCEBYTES crypto_stream_xsalsa20_tinynacl_NONCEBYTES #define crypto_stream_xsalsa20_IMPLEMENTATION "tinynacl" #define crypto_stream_xsalsa20_VERSION "-" #endif dq-20250201/crypto_verify_16.c000066400000000000000000000002421474734131400157370ustar00rootroot00000000000000#include "verify.h" #include "crypto_verify_16.h" int crypto_verify_16_tinynacl(const unsigned char *x, const unsigned char *y) { return verify(x, y, 16); } dq-20250201/crypto_verify_16.h000066400000000000000000000006111474734131400157440ustar00rootroot00000000000000#ifndef crypto_verify_16_H #define crypto_verify_16_H #define crypto_verify_16_tinynacl_BYTES 16 extern int crypto_verify_16_tinynacl(const unsigned char *, const unsigned char *); #define crypto_verify_16 crypto_verify_16_tinynacl #define crypto_verify_16_BYTES crypto_verify_16_tinynacl_BYTES #define crypto_verify_16_IMPLEMENTATION "tinynacl" #define crypto_verify_16_VERSION "-" #endif dq-20250201/crypto_verify_32.c000066400000000000000000000002421474734131400157350ustar00rootroot00000000000000#include "verify.h" #include "crypto_verify_32.h" int crypto_verify_32_tinynacl(const unsigned char *x, const unsigned char *y) { return verify(x, y, 32); } dq-20250201/crypto_verify_32.h000066400000000000000000000006111474734131400157420ustar00rootroot00000000000000#ifndef crypto_verify_32_H #define crypto_verify_32_H #define crypto_verify_32_tinynacl_BYTES 32 extern int crypto_verify_32_tinynacl(const unsigned char *, const unsigned char *); #define crypto_verify_32 crypto_verify_32_tinynacl #define crypto_verify_32_BYTES crypto_verify_32_tinynacl_BYTES #define crypto_verify_32_IMPLEMENTATION "tinynacl" #define crypto_verify_32_VERSION "-" #endif dq-20250201/crypto_verify_8.c000066400000000000000000000002371474734131400156640ustar00rootroot00000000000000#include "verify.h" #include "crypto_verify_8.h" int crypto_verify_8_tinynacl(const unsigned char *x, const unsigned char *y) { return verify(x, y, 8); } dq-20250201/crypto_verify_8.h000066400000000000000000000005761474734131400156770ustar00rootroot00000000000000#ifndef crypto_verify_8_H #define crypto_verify_8_H #define crypto_verify_8_tinynacl_BYTES 8 extern int crypto_verify_8_tinynacl(const unsigned char *, const unsigned char *); #define crypto_verify_8 crypto_verify_8_tinynacl #define crypto_verify_8_BYTES crypto_verify_8_tinynacl_BYTES #define crypto_verify_8_IMPLEMENTATION "tinynacl" #define crypto_verify_8_VERSION "-" #endif dq-20250201/cryptoint/000077500000000000000000000000001474734131400144165ustar00rootroot00000000000000dq-20250201/cryptoint/autogen000077500000000000000000000000721474734131400160050ustar00rootroot00000000000000#!/bin/sh ./create.py ./specialize.py rm -rf __pycache__ dq-20250201/cryptoint/create.py000077500000000000000000000051671474734131400162470ustar00rootroot00000000000000#!/usr/bin/env python3 vars = 'k','l','p','q','r','s','x','y','z' with open('version') as f: version = f.read().strip() # ===== top csh = f'''/* auto-generated: cd cryptoint; ./autogen */ /* {version} */ #ifndef crypto_intN_h #define crypto_intN_h #include #define crypto_intN intN_t #define crypto_intN_unsigned uintN_t #define crypto_intN_optblocker namespace_intN_optblocker extern volatile crypto_intN crypto_intN_optblocker; ''' cuh = f'''/* auto-generated: cd cryptoint; ./autogen */ /* {version} */ #ifndef crypto_uintN_h #define crypto_uintN_h #include #define crypto_uintN uintN_t #define crypto_uintN_signed intN_t #define crypto_uintN_signed_optblocker namespace_uintN_signed_optblocker extern volatile crypto_uintN_signed crypto_uintN_signed_optblocker; ''' # ===== functions functions = [] with open('functions') as f: which,fun = 'both',None for line in f: if fun is None: if line.startswith('#'): continue if line.strip() == '': continue if line.strip() == 'SIGNED:': which = 'signed' continue if line.strip() == 'UNSIGNED:': which = 'unsigned' continue fun = '' fun += line if line.strip() == '}': functions += [(which,fun)] which,fun = 'both',None for which,fun in functions: if which in ('both','signed'): data = fun data = data.replace('TYPE','crypto_intN') data = data.replace('UNSIGNED','crypto_intN_unsigned') data = data.replace('SIGNED','crypto_intN') for v in vars: data = data.replace(v.upper(),'crypto_intN_'+v) csh += '__attribute__((unused))\n' csh += 'static inline\n' csh += data csh += '\n' if which in ('both','unsigned'): data = fun data = data.replace('TYPE','crypto_uintN') data = data.replace('UNSIGNED','crypto_uintN') data = data.replace('SIGNED','crypto_uintN_signed') for v in vars: data = data.replace(v.upper(),'crypto_uintN_'+v) cuh += '__attribute__((unused))\n' cuh += 'static inline\n' cuh += data cuh += '\n' # ===== bottom csh += '''#endif ''' cuh += '''#endif ''' # ===== ship it with open('crypto_intN.h','w') as f: f.write(csh) with open('crypto_uintN.h','w') as f: f.write(cuh) with open('intN_optblocker.c','w') as f: f.write(f'''/* auto-generated: cd cryptoint; ./autogen */ /* {version} */ #include "crypto_intN.h" volatile crypto_intN crypto_intN_optblocker = 0; ''') with open('uintN_optblocker.c','w') as f: f.write(f'''/* auto-generated: cd cryptoint; ./autogen */ /* {version} */ #include "crypto_uintN.h" volatile crypto_uintN_signed crypto_uintN_signed_optblocker = 0; ''') dq-20250201/cryptoint/crypto_int16.h000066400000000000000000000526601474734131400171410ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_int16_h #define crypto_int16_h #include #define crypto_int16 int16_t #define crypto_int16_unsigned uint16_t #define crypto_int16_optblocker dq_int16_optblocker extern volatile crypto_int16 crypto_int16_optblocker; __attribute__((unused)) static inline crypto_int16 crypto_int16_load(const unsigned char *crypto_int16_s) { crypto_int16 crypto_int16_z = 0; crypto_int16_z |= ((crypto_int16) (*crypto_int16_s++)) << 0; crypto_int16_z |= ((crypto_int16) (*crypto_int16_s++)) << 8; return crypto_int16_z; } __attribute__((unused)) static inline crypto_int16 crypto_int16_load_bigendian(const unsigned char *crypto_int16_s) { crypto_int16 crypto_int16_z = 0; crypto_int16_z |= ((crypto_int16) (*crypto_int16_s++)) << 8; crypto_int16_z |= ((crypto_int16) (*crypto_int16_s++)) << 0; return crypto_int16_z; } __attribute__((unused)) static inline void crypto_int16_store(unsigned char *crypto_int16_s,crypto_int16 crypto_int16_x) { *crypto_int16_s++ = crypto_int16_x >> 0; *crypto_int16_s++ = crypto_int16_x >> 8; } __attribute__((unused)) static inline void crypto_int16_store_bigendian(unsigned char *crypto_int16_s,crypto_int16 crypto_int16_x) { *crypto_int16_s++ = crypto_int16_x >> 8; *crypto_int16_s++ = crypto_int16_x >> 0; } __attribute__((unused)) static inline crypto_int16 crypto_int16_negative_mask(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarw $15,%0" : "+r"(crypto_int16_x) : : "cc"); return crypto_int16_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_y; __asm__ ("sbfx %w0,%w1,15,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : ); return crypto_int16_y; #else crypto_int16_x >>= 16-6; crypto_int16_x += crypto_int16_optblocker; crypto_int16_x >>= 5; return crypto_int16_x; #endif } __attribute__((unused)) static inline crypto_int16_unsigned crypto_int16_unsigned_topbit_01(crypto_int16_unsigned crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrw $15,%0" : "+r"(crypto_int16_x) : : "cc"); return crypto_int16_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_y; __asm__ ("ubfx %w0,%w1,15,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : ); return crypto_int16_y; #else crypto_int16_x >>= 16-6; crypto_int16_x += crypto_int16_optblocker; crypto_int16_x >>= 5; return crypto_int16_x; #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_negative_01(crypto_int16 crypto_int16_x) { return crypto_int16_unsigned_topbit_01(crypto_int16_x); } __attribute__((unused)) static inline crypto_int16 crypto_int16_topbit_mask(crypto_int16 crypto_int16_x) { return crypto_int16_negative_mask(crypto_int16_x); } __attribute__((unused)) static inline crypto_int16 crypto_int16_topbit_01(crypto_int16 crypto_int16_x) { return crypto_int16_unsigned_topbit_01(crypto_int16_x); } __attribute__((unused)) static inline crypto_int16 crypto_int16_bottombit_mask(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andw $1,%0" : "+r"(crypto_int16_x) : : "cc"); return -crypto_int16_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_y; __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : ); return crypto_int16_y; #else crypto_int16_x &= 1 + crypto_int16_optblocker; return -crypto_int16_x; #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_bottombit_01(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andw $1,%0" : "+r"(crypto_int16_x) : : "cc"); return crypto_int16_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_y; __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : ); return crypto_int16_y; #else crypto_int16_x &= 1 + crypto_int16_optblocker; return crypto_int16_x; #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_bitinrangepublicpos_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("sxth %w0,%w0\n asr %w0,%w0,%w1" : "+&r"(crypto_int16_x) : "r"(crypto_int16_s) : ); #else crypto_int16_x >>= crypto_int16_s ^ crypto_int16_optblocker; #endif return crypto_int16_bottombit_mask(crypto_int16_x); } __attribute__((unused)) static inline crypto_int16 crypto_int16_bitinrangepublicpos_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("sxth %w0,%w0\n asr %w0,%w0,%w1" : "+&r"(crypto_int16_x) : "r"(crypto_int16_s) : ); #else crypto_int16_x >>= crypto_int16_s ^ crypto_int16_optblocker; #endif return crypto_int16_bottombit_01(crypto_int16_x); } __attribute__((unused)) static inline crypto_int16 crypto_int16_shlmod(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16_s &= 15; __asm__ ("shlw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,15\n and %w1,%w1,65535\n lsl %w1,%w1,%w0" : "+&r"(crypto_int16_s), "+r"(crypto_int16_x) : : ); #else int crypto_int16_k, crypto_int16_l; for (crypto_int16_l = 0,crypto_int16_k = 1;crypto_int16_k < 16;++crypto_int16_l,crypto_int16_k *= 2) crypto_int16_x ^= (crypto_int16_x ^ (crypto_int16_x << crypto_int16_k)) & crypto_int16_bitinrangepublicpos_mask(crypto_int16_s,crypto_int16_l); #endif return crypto_int16_x; } __attribute__((unused)) static inline crypto_int16 crypto_int16_shrmod(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16_s &= 15; __asm__ ("sarw %%cl,%0" : "+r"(crypto_int16_x) : "c"(crypto_int16_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,15\n sxth %w1,%w1\n asr %w1,%w1,%w0" : "+&r"(crypto_int16_s), "+r"(crypto_int16_x) : : ); #else int crypto_int16_k, crypto_int16_l; for (crypto_int16_l = 0,crypto_int16_k = 1;crypto_int16_k < 16;++crypto_int16_l,crypto_int16_k *= 2) crypto_int16_x ^= (crypto_int16_x ^ (crypto_int16_x >> crypto_int16_k)) & crypto_int16_bitinrangepublicpos_mask(crypto_int16_s,crypto_int16_l); #endif return crypto_int16_x; } __attribute__((unused)) static inline crypto_int16 crypto_int16_bitmod_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) { crypto_int16_x = crypto_int16_shrmod(crypto_int16_x,crypto_int16_s); return crypto_int16_bottombit_mask(crypto_int16_x); } __attribute__((unused)) static inline crypto_int16 crypto_int16_bitmod_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_s) { crypto_int16_x = crypto_int16_shrmod(crypto_int16_x,crypto_int16_s); return crypto_int16_bottombit_01(crypto_int16_x); } __attribute__((unused)) static inline crypto_int16 crypto_int16_nonzero_mask(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n testw %2,%2\n cmovnew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("tst %w1,65535\n csetm %w0,ne" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #else crypto_int16_x |= -crypto_int16_x; return crypto_int16_negative_mask(crypto_int16_x); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_nonzero_01(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n testw %2,%2\n cmovnew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("tst %w1,65535\n cset %w0,ne" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #else crypto_int16_x |= -crypto_int16_x; return crypto_int16_unsigned_topbit_01(crypto_int16_x); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_positive_mask(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n testw %2,%2\n cmovgw %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("sxth %w0,%w1\n cmp %w0,0\n csetm %w0,gt" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #else crypto_int16 crypto_int16_z = -crypto_int16_x; crypto_int16_z ^= crypto_int16_x & crypto_int16_z; return crypto_int16_negative_mask(crypto_int16_z); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_positive_01(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n testw %2,%2\n cmovgw %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("sxth %w0,%w1\n cmp %w0,0\n cset %w0,gt" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #else crypto_int16 crypto_int16_z = -crypto_int16_x; crypto_int16_z ^= crypto_int16_x & crypto_int16_z; return crypto_int16_unsigned_topbit_01(crypto_int16_z); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_zero_mask(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n testw %2,%2\n cmovew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("tst %w1,65535\n csetm %w0,eq" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #else return ~crypto_int16_nonzero_mask(crypto_int16_x); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_zero_01(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n testw %2,%2\n cmovew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("tst %w1,65535\n cset %w0,eq" : "=r"(crypto_int16_z) : "r"(crypto_int16_x) : "cc"); return crypto_int16_z; #else return 1-crypto_int16_nonzero_01(crypto_int16_x); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_unequal_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovnew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n csetm %w0,ne" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #else return crypto_int16_nonzero_mask(crypto_int16_x ^ crypto_int16_y); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_unequal_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovnew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n cset %w0,ne" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #else return crypto_int16_nonzero_01(crypto_int16_x ^ crypto_int16_y); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_equal_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n csetm %w0,eq" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #else return ~crypto_int16_unequal_mask(crypto_int16_x,crypto_int16_y); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_equal_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n cset %w0,eq" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #else return 1-crypto_int16_unequal_01(crypto_int16_x,crypto_int16_y); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_min(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpw %1,%0\n cmovgw %1,%0" : "+r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc"); return crypto_int16_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("sxth %w0,%w0\n cmp %w0,%w1,sxth\n csel %w0,%w0,%w1,lt" : "+&r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc"); return crypto_int16_x; #else crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x; crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x; crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y); crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z); crypto_int16_z &= crypto_int16_r; return crypto_int16_x ^ crypto_int16_z; #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_max(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpw %1,%0\n cmovlw %1,%0" : "+r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc"); return crypto_int16_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("sxth %w0,%w0\n cmp %w0,%w1,sxth\n csel %w0,%w1,%w0,lt" : "+&r"(crypto_int16_x) : "r"(crypto_int16_y) : "cc"); return crypto_int16_x; #else crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x; crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x; crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y); crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z); crypto_int16_z &= crypto_int16_r; return crypto_int16_y ^ crypto_int16_z; #endif } __attribute__((unused)) static inline void crypto_int16_minmax(crypto_int16 *crypto_int16_p,crypto_int16 *crypto_int16_q) { crypto_int16 crypto_int16_x = *crypto_int16_p; crypto_int16 crypto_int16_y = *crypto_int16_q; #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_z; __asm__ ("cmpw %2,%1\n movw %1,%0\n cmovgw %2,%1\n cmovgw %0,%2" : "=&r"(crypto_int16_z), "+&r"(crypto_int16_x), "+r"(crypto_int16_y) : : "cc"); *crypto_int16_p = crypto_int16_x; *crypto_int16_q = crypto_int16_y; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_r, crypto_int16_s; __asm__ ("sxth %w0,%w0\n cmp %w0,%w3,sxth\n csel %w1,%w0,%w3,lt\n csel %w2,%w3,%w0,lt" : "+&r"(crypto_int16_x), "=&r"(crypto_int16_r), "=r"(crypto_int16_s) : "r"(crypto_int16_y) : "cc"); *crypto_int16_p = crypto_int16_r; *crypto_int16_q = crypto_int16_s; #else crypto_int16 crypto_int16_r = crypto_int16_y ^ crypto_int16_x; crypto_int16 crypto_int16_z = crypto_int16_y - crypto_int16_x; crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_y); crypto_int16_z = crypto_int16_negative_mask(crypto_int16_z); crypto_int16_z &= crypto_int16_r; crypto_int16_x ^= crypto_int16_z; crypto_int16_y ^= crypto_int16_z; *crypto_int16_p = crypto_int16_x; *crypto_int16_q = crypto_int16_y; #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_smaller_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovlw %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("sxth %w0,%w1\n cmp %w0,%w2,sxth\n csetm %w0,lt" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #else crypto_int16 crypto_int16_r = crypto_int16_x ^ crypto_int16_y; crypto_int16 crypto_int16_z = crypto_int16_x - crypto_int16_y; crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_x); return crypto_int16_negative_mask(crypto_int16_z); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_smaller_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovlw %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("sxth %w0,%w1\n cmp %w0,%w2,sxth\n cset %w0,lt" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #else crypto_int16 crypto_int16_r = crypto_int16_x ^ crypto_int16_y; crypto_int16 crypto_int16_z = crypto_int16_x - crypto_int16_y; crypto_int16_z ^= crypto_int16_r & (crypto_int16_z ^ crypto_int16_x); return crypto_int16_unsigned_topbit_01(crypto_int16_z); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_leq_mask(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovlew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("sxth %w0,%w1\n cmp %w0,%w2,sxth\n csetm %w0,le" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #else return ~crypto_int16_smaller_mask(crypto_int16_y,crypto_int16_x); #endif } __attribute__((unused)) static inline crypto_int16 crypto_int16_leq_01(crypto_int16 crypto_int16_x,crypto_int16 crypto_int16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 crypto_int16_q,crypto_int16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovlew %1,%0" : "=&r"(crypto_int16_z), "=&r"(crypto_int16_q) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int16 crypto_int16_z; __asm__ ("sxth %w0,%w1\n cmp %w0,%w2,sxth\n cset %w0,le" : "=&r"(crypto_int16_z) : "r"(crypto_int16_x), "r"(crypto_int16_y) : "cc"); return crypto_int16_z; #else return 1-crypto_int16_smaller_01(crypto_int16_y,crypto_int16_x); #endif } __attribute__((unused)) static inline int crypto_int16_ones_num(crypto_int16 crypto_int16_x) { crypto_int16_unsigned crypto_int16_y = crypto_int16_x; const crypto_int16 C0 = 0x5555; const crypto_int16 C1 = 0x3333; const crypto_int16 C2 = 0x0f0f; crypto_int16_y -= ((crypto_int16_y >> 1) & C0); crypto_int16_y = (crypto_int16_y & C1) + ((crypto_int16_y >> 2) & C1); crypto_int16_y = (crypto_int16_y + (crypto_int16_y >> 4)) & C2; crypto_int16_y = (crypto_int16_y + (crypto_int16_y >> 8)) & 0xff; return crypto_int16_y; } __attribute__((unused)) static inline int crypto_int16_bottomzeros_num(crypto_int16 crypto_int16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int16 fallback = 16; __asm__ ("bsfw %0,%0\n cmovew %1,%0" : "+&r"(crypto_int16_x) : "r"(fallback) : "cc"); return crypto_int16_x; #elif defined(__GNUC__) && defined(__aarch64__) int64_t crypto_int16_y; __asm__ ("orr %w0,%w1,-65536\n rbit %w0,%w0\n clz %w0,%w0" : "=r"(crypto_int16_y) : "r"(crypto_int16_x) : ); return crypto_int16_y; #else crypto_int16 crypto_int16_y = crypto_int16_x ^ (crypto_int16_x-1); crypto_int16_y = ((crypto_int16) crypto_int16_y) >> 1; crypto_int16_y &= ~(crypto_int16_x & (((crypto_int16) 1) << (16-1))); return crypto_int16_ones_num(crypto_int16_y); #endif } #endif dq-20250201/cryptoint/crypto_int32.h000066400000000000000000000526661474734131400171450ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_int32_h #define crypto_int32_h #include #define crypto_int32 int32_t #define crypto_int32_unsigned uint32_t #define crypto_int32_optblocker dq_int32_optblocker extern volatile crypto_int32 crypto_int32_optblocker; __attribute__((unused)) static inline crypto_int32 crypto_int32_load(const unsigned char *crypto_int32_s) { crypto_int32 crypto_int32_z = 0; crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 0; crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 8; crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 16; crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 24; return crypto_int32_z; } __attribute__((unused)) static inline crypto_int32 crypto_int32_load_bigendian(const unsigned char *crypto_int32_s) { crypto_int32 crypto_int32_z = 0; crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 24; crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 16; crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 8; crypto_int32_z |= ((crypto_int32) (*crypto_int32_s++)) << 0; return crypto_int32_z; } __attribute__((unused)) static inline void crypto_int32_store(unsigned char *crypto_int32_s,crypto_int32 crypto_int32_x) { *crypto_int32_s++ = crypto_int32_x >> 0; *crypto_int32_s++ = crypto_int32_x >> 8; *crypto_int32_s++ = crypto_int32_x >> 16; *crypto_int32_s++ = crypto_int32_x >> 24; } __attribute__((unused)) static inline void crypto_int32_store_bigendian(unsigned char *crypto_int32_s,crypto_int32 crypto_int32_x) { *crypto_int32_s++ = crypto_int32_x >> 24; *crypto_int32_s++ = crypto_int32_x >> 16; *crypto_int32_s++ = crypto_int32_x >> 8; *crypto_int32_s++ = crypto_int32_x >> 0; } __attribute__((unused)) static inline crypto_int32 crypto_int32_negative_mask(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarl $31,%0" : "+r"(crypto_int32_x) : : "cc"); return crypto_int32_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_y; __asm__ ("asr %w0,%w1,31" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : ); return crypto_int32_y; #else crypto_int32_x >>= 32-6; crypto_int32_x += crypto_int32_optblocker; crypto_int32_x >>= 5; return crypto_int32_x; #endif } __attribute__((unused)) static inline crypto_int32_unsigned crypto_int32_unsigned_topbit_01(crypto_int32_unsigned crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrl $31,%0" : "+r"(crypto_int32_x) : : "cc"); return crypto_int32_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_y; __asm__ ("lsr %w0,%w1,31" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : ); return crypto_int32_y; #else crypto_int32_x >>= 32-6; crypto_int32_x += crypto_int32_optblocker; crypto_int32_x >>= 5; return crypto_int32_x; #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_negative_01(crypto_int32 crypto_int32_x) { return crypto_int32_unsigned_topbit_01(crypto_int32_x); } __attribute__((unused)) static inline crypto_int32 crypto_int32_topbit_mask(crypto_int32 crypto_int32_x) { return crypto_int32_negative_mask(crypto_int32_x); } __attribute__((unused)) static inline crypto_int32 crypto_int32_topbit_01(crypto_int32 crypto_int32_x) { return crypto_int32_unsigned_topbit_01(crypto_int32_x); } __attribute__((unused)) static inline crypto_int32 crypto_int32_bottombit_mask(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andl $1,%0" : "+r"(crypto_int32_x) : : "cc"); return -crypto_int32_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_y; __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : ); return crypto_int32_y; #else crypto_int32_x &= 1 + crypto_int32_optblocker; return -crypto_int32_x; #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_bottombit_01(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andl $1,%0" : "+r"(crypto_int32_x) : : "cc"); return crypto_int32_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_y; __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : ); return crypto_int32_y; #else crypto_int32_x &= 1 + crypto_int32_optblocker; return crypto_int32_x; #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_bitinrangepublicpos_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : ); #else crypto_int32_x >>= crypto_int32_s ^ crypto_int32_optblocker; #endif return crypto_int32_bottombit_mask(crypto_int32_x); } __attribute__((unused)) static inline crypto_int32 crypto_int32_bitinrangepublicpos_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : ); #else crypto_int32_x >>= crypto_int32_s ^ crypto_int32_optblocker; #endif return crypto_int32_bottombit_01(crypto_int32_x); } __attribute__((unused)) static inline crypto_int32 crypto_int32_shlmod(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shll %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsl %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : ); #else int crypto_int32_k, crypto_int32_l; for (crypto_int32_l = 0,crypto_int32_k = 1;crypto_int32_k < 32;++crypto_int32_l,crypto_int32_k *= 2) crypto_int32_x ^= (crypto_int32_x ^ (crypto_int32_x << crypto_int32_k)) & crypto_int32_bitinrangepublicpos_mask(crypto_int32_s,crypto_int32_l); #endif return crypto_int32_x; } __attribute__((unused)) static inline crypto_int32 crypto_int32_shrmod(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarl %%cl,%0" : "+r"(crypto_int32_x) : "c"(crypto_int32_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("asr %w0,%w0,%w1" : "+r"(crypto_int32_x) : "r"(crypto_int32_s) : ); #else int crypto_int32_k, crypto_int32_l; for (crypto_int32_l = 0,crypto_int32_k = 1;crypto_int32_k < 32;++crypto_int32_l,crypto_int32_k *= 2) crypto_int32_x ^= (crypto_int32_x ^ (crypto_int32_x >> crypto_int32_k)) & crypto_int32_bitinrangepublicpos_mask(crypto_int32_s,crypto_int32_l); #endif return crypto_int32_x; } __attribute__((unused)) static inline crypto_int32 crypto_int32_bitmod_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) { crypto_int32_x = crypto_int32_shrmod(crypto_int32_x,crypto_int32_s); return crypto_int32_bottombit_mask(crypto_int32_x); } __attribute__((unused)) static inline crypto_int32 crypto_int32_bitmod_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_s) { crypto_int32_x = crypto_int32_shrmod(crypto_int32_x,crypto_int32_s); return crypto_int32_bottombit_01(crypto_int32_x); } __attribute__((unused)) static inline crypto_int32 crypto_int32_nonzero_mask(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n testl %2,%2\n cmovnel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,0\n csetm %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #else crypto_int32_x |= -crypto_int32_x; return crypto_int32_negative_mask(crypto_int32_x); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_nonzero_01(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n testl %2,%2\n cmovnel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,0\n cset %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #else crypto_int32_x |= -crypto_int32_x; return crypto_int32_unsigned_topbit_01(crypto_int32_x); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_positive_mask(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n testl %2,%2\n cmovgl %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,0\n csetm %w0,gt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #else crypto_int32 crypto_int32_z = -crypto_int32_x; crypto_int32_z ^= crypto_int32_x & crypto_int32_z; return crypto_int32_negative_mask(crypto_int32_z); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_positive_01(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n testl %2,%2\n cmovgl %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,0\n cset %w0,gt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #else crypto_int32 crypto_int32_z = -crypto_int32_x; crypto_int32_z ^= crypto_int32_x & crypto_int32_z; return crypto_int32_unsigned_topbit_01(crypto_int32_z); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_zero_mask(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n testl %2,%2\n cmovel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,0\n csetm %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #else return ~crypto_int32_nonzero_mask(crypto_int32_x); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_zero_01(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n testl %2,%2\n cmovel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,0\n cset %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x) : "cc"); return crypto_int32_z; #else return 1-crypto_int32_nonzero_01(crypto_int32_x); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_unequal_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovnel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,%w2\n csetm %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #else return crypto_int32_nonzero_mask(crypto_int32_x ^ crypto_int32_y); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_unequal_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovnel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,%w2\n cset %w0,ne" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #else return crypto_int32_nonzero_01(crypto_int32_x ^ crypto_int32_y); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_equal_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,%w2\n csetm %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #else return ~crypto_int32_unequal_mask(crypto_int32_x,crypto_int32_y); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_equal_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,%w2\n cset %w0,eq" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #else return 1-crypto_int32_unequal_01(crypto_int32_x,crypto_int32_y); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_min(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpl %1,%0\n cmovgl %1,%0" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc"); return crypto_int32_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("cmp %w0,%w1\n csel %w0,%w0,%w1,lt" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc"); return crypto_int32_x; #else crypto_int32 crypto_int32_r = crypto_int32_y ^ crypto_int32_x; crypto_int32 crypto_int32_z = crypto_int32_y - crypto_int32_x; crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y); crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z); crypto_int32_z &= crypto_int32_r; return crypto_int32_x ^ crypto_int32_z; #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_max(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpl %1,%0\n cmovll %1,%0" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc"); return crypto_int32_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("cmp %w0,%w1\n csel %w0,%w1,%w0,lt" : "+r"(crypto_int32_x) : "r"(crypto_int32_y) : "cc"); return crypto_int32_x; #else crypto_int32 crypto_int32_r = crypto_int32_y ^ crypto_int32_x; crypto_int32 crypto_int32_z = crypto_int32_y - crypto_int32_x; crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y); crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z); crypto_int32_z &= crypto_int32_r; return crypto_int32_y ^ crypto_int32_z; #endif } __attribute__((unused)) static inline void crypto_int32_minmax(crypto_int32 *crypto_int32_p,crypto_int32 *crypto_int32_q) { crypto_int32 crypto_int32_x = *crypto_int32_p; crypto_int32 crypto_int32_y = *crypto_int32_q; #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_z; __asm__ ("cmpl %2,%1\n movl %1,%0\n cmovgl %2,%1\n cmovgl %0,%2" : "=&r"(crypto_int32_z), "+&r"(crypto_int32_x), "+r"(crypto_int32_y) : : "cc"); *crypto_int32_p = crypto_int32_x; *crypto_int32_q = crypto_int32_y; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_r, crypto_int32_s; __asm__ ("cmp %w2,%w3\n csel %w0,%w2,%w3,lt\n csel %w1,%w3,%w2,lt" : "=&r"(crypto_int32_r), "=r"(crypto_int32_s) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); *crypto_int32_p = crypto_int32_r; *crypto_int32_q = crypto_int32_s; #else crypto_int32 crypto_int32_r = crypto_int32_y ^ crypto_int32_x; crypto_int32 crypto_int32_z = crypto_int32_y - crypto_int32_x; crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_y); crypto_int32_z = crypto_int32_negative_mask(crypto_int32_z); crypto_int32_z &= crypto_int32_r; crypto_int32_x ^= crypto_int32_z; crypto_int32_y ^= crypto_int32_z; *crypto_int32_p = crypto_int32_x; *crypto_int32_q = crypto_int32_y; #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_smaller_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovll %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,%w2\n csetm %w0,lt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #else crypto_int32 crypto_int32_r = crypto_int32_x ^ crypto_int32_y; crypto_int32 crypto_int32_z = crypto_int32_x - crypto_int32_y; crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_x); return crypto_int32_negative_mask(crypto_int32_z); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_smaller_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovll %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,%w2\n cset %w0,lt" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #else crypto_int32 crypto_int32_r = crypto_int32_x ^ crypto_int32_y; crypto_int32 crypto_int32_z = crypto_int32_x - crypto_int32_y; crypto_int32_z ^= crypto_int32_r & (crypto_int32_z ^ crypto_int32_x); return crypto_int32_unsigned_topbit_01(crypto_int32_z); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_leq_mask(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovlel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,%w2\n csetm %w0,le" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #else return ~crypto_int32_smaller_mask(crypto_int32_y,crypto_int32_x); #endif } __attribute__((unused)) static inline crypto_int32 crypto_int32_leq_01(crypto_int32 crypto_int32_x,crypto_int32 crypto_int32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 crypto_int32_q,crypto_int32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovlel %1,%0" : "=&r"(crypto_int32_z), "=&r"(crypto_int32_q) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int32 crypto_int32_z; __asm__ ("cmp %w1,%w2\n cset %w0,le" : "=r"(crypto_int32_z) : "r"(crypto_int32_x), "r"(crypto_int32_y) : "cc"); return crypto_int32_z; #else return 1-crypto_int32_smaller_01(crypto_int32_y,crypto_int32_x); #endif } __attribute__((unused)) static inline int crypto_int32_ones_num(crypto_int32 crypto_int32_x) { crypto_int32_unsigned crypto_int32_y = crypto_int32_x; const crypto_int32 C0 = 0x55555555; const crypto_int32 C1 = 0x33333333; const crypto_int32 C2 = 0x0f0f0f0f; crypto_int32_y -= ((crypto_int32_y >> 1) & C0); crypto_int32_y = (crypto_int32_y & C1) + ((crypto_int32_y >> 2) & C1); crypto_int32_y = (crypto_int32_y + (crypto_int32_y >> 4)) & C2; crypto_int32_y += crypto_int32_y >> 8; crypto_int32_y = (crypto_int32_y + (crypto_int32_y >> 16)) & 0xff; return crypto_int32_y; } __attribute__((unused)) static inline int crypto_int32_bottomzeros_num(crypto_int32 crypto_int32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int32 fallback = 32; __asm__ ("bsfl %0,%0\n cmovel %1,%0" : "+&r"(crypto_int32_x) : "r"(fallback) : "cc"); return crypto_int32_x; #elif defined(__GNUC__) && defined(__aarch64__) int64_t crypto_int32_y; __asm__ ("rbit %w0,%w1\n clz %w0,%w0" : "=r"(crypto_int32_y) : "r"(crypto_int32_x) : ); return crypto_int32_y; #else crypto_int32 crypto_int32_y = crypto_int32_x ^ (crypto_int32_x-1); crypto_int32_y = ((crypto_int32) crypto_int32_y) >> 1; crypto_int32_y &= ~(crypto_int32_x & (((crypto_int32) 1) << (32-1))); return crypto_int32_ones_num(crypto_int32_y); #endif } #endif dq-20250201/cryptoint/crypto_int64.h000066400000000000000000000544121474734131400171410ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_int64_h #define crypto_int64_h #include #define crypto_int64 int64_t #define crypto_int64_unsigned uint64_t #define crypto_int64_optblocker dq_int64_optblocker extern volatile crypto_int64 crypto_int64_optblocker; __attribute__((unused)) static inline crypto_int64 crypto_int64_load(const unsigned char *crypto_int64_s) { crypto_int64 crypto_int64_z = 0; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 0; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 8; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 16; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 24; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 32; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 40; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 48; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 56; return crypto_int64_z; } __attribute__((unused)) static inline crypto_int64 crypto_int64_load_bigendian(const unsigned char *crypto_int64_s) { crypto_int64 crypto_int64_z = 0; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 56; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 48; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 40; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 32; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 24; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 16; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 8; crypto_int64_z |= ((crypto_int64) (*crypto_int64_s++)) << 0; return crypto_int64_z; } __attribute__((unused)) static inline void crypto_int64_store(unsigned char *crypto_int64_s,crypto_int64 crypto_int64_x) { *crypto_int64_s++ = crypto_int64_x >> 0; *crypto_int64_s++ = crypto_int64_x >> 8; *crypto_int64_s++ = crypto_int64_x >> 16; *crypto_int64_s++ = crypto_int64_x >> 24; *crypto_int64_s++ = crypto_int64_x >> 32; *crypto_int64_s++ = crypto_int64_x >> 40; *crypto_int64_s++ = crypto_int64_x >> 48; *crypto_int64_s++ = crypto_int64_x >> 56; } __attribute__((unused)) static inline void crypto_int64_store_bigendian(unsigned char *crypto_int64_s,crypto_int64 crypto_int64_x) { *crypto_int64_s++ = crypto_int64_x >> 56; *crypto_int64_s++ = crypto_int64_x >> 48; *crypto_int64_s++ = crypto_int64_x >> 40; *crypto_int64_s++ = crypto_int64_x >> 32; *crypto_int64_s++ = crypto_int64_x >> 24; *crypto_int64_s++ = crypto_int64_x >> 16; *crypto_int64_s++ = crypto_int64_x >> 8; *crypto_int64_s++ = crypto_int64_x >> 0; } __attribute__((unused)) static inline crypto_int64 crypto_int64_negative_mask(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarq $63,%0" : "+r"(crypto_int64_x) : : "cc"); return crypto_int64_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_y; __asm__ ("asr %0,%1,63" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : ); return crypto_int64_y; #else crypto_int64_x >>= 64-6; crypto_int64_x += crypto_int64_optblocker; crypto_int64_x >>= 5; return crypto_int64_x; #endif } __attribute__((unused)) static inline crypto_int64_unsigned crypto_int64_unsigned_topbit_01(crypto_int64_unsigned crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrq $63,%0" : "+r"(crypto_int64_x) : : "cc"); return crypto_int64_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_y; __asm__ ("lsr %0,%1,63" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : ); return crypto_int64_y; #else crypto_int64_x >>= 64-6; crypto_int64_x += crypto_int64_optblocker; crypto_int64_x >>= 5; return crypto_int64_x; #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_negative_01(crypto_int64 crypto_int64_x) { return crypto_int64_unsigned_topbit_01(crypto_int64_x); } __attribute__((unused)) static inline crypto_int64 crypto_int64_topbit_mask(crypto_int64 crypto_int64_x) { return crypto_int64_negative_mask(crypto_int64_x); } __attribute__((unused)) static inline crypto_int64 crypto_int64_topbit_01(crypto_int64 crypto_int64_x) { return crypto_int64_unsigned_topbit_01(crypto_int64_x); } __attribute__((unused)) static inline crypto_int64 crypto_int64_bottombit_mask(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andq $1,%0" : "+r"(crypto_int64_x) : : "cc"); return -crypto_int64_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_y; __asm__ ("sbfx %0,%1,0,1" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : ); return crypto_int64_y; #else crypto_int64_x &= 1 + crypto_int64_optblocker; return -crypto_int64_x; #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_bottombit_01(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andq $1,%0" : "+r"(crypto_int64_x) : : "cc"); return crypto_int64_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_y; __asm__ ("ubfx %0,%1,0,1" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : ); return crypto_int64_y; #else crypto_int64_x &= 1 + crypto_int64_optblocker; return crypto_int64_x; #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_bitinrangepublicpos_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : ); #else crypto_int64_x >>= crypto_int64_s ^ crypto_int64_optblocker; #endif return crypto_int64_bottombit_mask(crypto_int64_x); } __attribute__((unused)) static inline crypto_int64 crypto_int64_bitinrangepublicpos_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : ); #else crypto_int64_x >>= crypto_int64_s ^ crypto_int64_optblocker; #endif return crypto_int64_bottombit_01(crypto_int64_x); } __attribute__((unused)) static inline crypto_int64 crypto_int64_shlmod(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shlq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsl %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : ); #else int crypto_int64_k, crypto_int64_l; for (crypto_int64_l = 0,crypto_int64_k = 1;crypto_int64_k < 64;++crypto_int64_l,crypto_int64_k *= 2) crypto_int64_x ^= (crypto_int64_x ^ (crypto_int64_x << crypto_int64_k)) & crypto_int64_bitinrangepublicpos_mask(crypto_int64_s,crypto_int64_l); #endif return crypto_int64_x; } __attribute__((unused)) static inline crypto_int64 crypto_int64_shrmod(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarq %%cl,%0" : "+r"(crypto_int64_x) : "c"(crypto_int64_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("asr %0,%0,%1" : "+r"(crypto_int64_x) : "r"(crypto_int64_s) : ); #else int crypto_int64_k, crypto_int64_l; for (crypto_int64_l = 0,crypto_int64_k = 1;crypto_int64_k < 64;++crypto_int64_l,crypto_int64_k *= 2) crypto_int64_x ^= (crypto_int64_x ^ (crypto_int64_x >> crypto_int64_k)) & crypto_int64_bitinrangepublicpos_mask(crypto_int64_s,crypto_int64_l); #endif return crypto_int64_x; } __attribute__((unused)) static inline crypto_int64 crypto_int64_bitmod_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) { crypto_int64_x = crypto_int64_shrmod(crypto_int64_x,crypto_int64_s); return crypto_int64_bottombit_mask(crypto_int64_x); } __attribute__((unused)) static inline crypto_int64 crypto_int64_bitmod_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_s) { crypto_int64_x = crypto_int64_shrmod(crypto_int64_x,crypto_int64_s); return crypto_int64_bottombit_01(crypto_int64_x); } __attribute__((unused)) static inline crypto_int64 crypto_int64_nonzero_mask(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n testq %2,%2\n cmovneq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,0\n csetm %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #else crypto_int64_x |= -crypto_int64_x; return crypto_int64_negative_mask(crypto_int64_x); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_nonzero_01(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n testq %2,%2\n cmovneq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,0\n cset %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #else crypto_int64_x |= -crypto_int64_x; return crypto_int64_unsigned_topbit_01(crypto_int64_x); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_positive_mask(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n testq %2,%2\n cmovgq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,0\n csetm %0,gt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #else crypto_int64 crypto_int64_z = -crypto_int64_x; crypto_int64_z ^= crypto_int64_x & crypto_int64_z; return crypto_int64_negative_mask(crypto_int64_z); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_positive_01(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n testq %2,%2\n cmovgq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,0\n cset %0,gt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #else crypto_int64 crypto_int64_z = -crypto_int64_x; crypto_int64_z ^= crypto_int64_x & crypto_int64_z; return crypto_int64_unsigned_topbit_01(crypto_int64_z); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_zero_mask(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n testq %2,%2\n cmoveq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,0\n csetm %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #else return ~crypto_int64_nonzero_mask(crypto_int64_x); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_zero_01(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n testq %2,%2\n cmoveq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,0\n cset %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x) : "cc"); return crypto_int64_z; #else return 1-crypto_int64_nonzero_01(crypto_int64_x); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_unequal_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovneq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,%2\n csetm %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #else return crypto_int64_nonzero_mask(crypto_int64_x ^ crypto_int64_y); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_unequal_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovneq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,%2\n cset %0,ne" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #else return crypto_int64_nonzero_01(crypto_int64_x ^ crypto_int64_y); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_equal_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmoveq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,%2\n csetm %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #else return ~crypto_int64_unequal_mask(crypto_int64_x,crypto_int64_y); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_equal_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmoveq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,%2\n cset %0,eq" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #else return 1-crypto_int64_unequal_01(crypto_int64_x,crypto_int64_y); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_min(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpq %1,%0\n cmovgq %1,%0" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc"); return crypto_int64_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("cmp %0,%1\n csel %0,%0,%1,lt" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc"); return crypto_int64_x; #else crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x; crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x; crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y); crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z); crypto_int64_z &= crypto_int64_r; return crypto_int64_x ^ crypto_int64_z; #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_max(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpq %1,%0\n cmovlq %1,%0" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc"); return crypto_int64_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("cmp %0,%1\n csel %0,%1,%0,lt" : "+r"(crypto_int64_x) : "r"(crypto_int64_y) : "cc"); return crypto_int64_x; #else crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x; crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x; crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y); crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z); crypto_int64_z &= crypto_int64_r; return crypto_int64_y ^ crypto_int64_z; #endif } __attribute__((unused)) static inline void crypto_int64_minmax(crypto_int64 *crypto_int64_p,crypto_int64 *crypto_int64_q) { crypto_int64 crypto_int64_x = *crypto_int64_p; crypto_int64 crypto_int64_y = *crypto_int64_q; #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_z; __asm__ ("cmpq %2,%1\n movq %1,%0\n cmovgq %2,%1\n cmovgq %0,%2" : "=&r"(crypto_int64_z), "+&r"(crypto_int64_x), "+r"(crypto_int64_y) : : "cc"); *crypto_int64_p = crypto_int64_x; *crypto_int64_q = crypto_int64_y; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_r, crypto_int64_s; __asm__ ("cmp %2,%3\n csel %0,%2,%3,lt\n csel %1,%3,%2,lt" : "=&r"(crypto_int64_r), "=r"(crypto_int64_s) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); *crypto_int64_p = crypto_int64_r; *crypto_int64_q = crypto_int64_s; #else crypto_int64 crypto_int64_r = crypto_int64_y ^ crypto_int64_x; crypto_int64 crypto_int64_z = crypto_int64_y - crypto_int64_x; crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_y); crypto_int64_z = crypto_int64_negative_mask(crypto_int64_z); crypto_int64_z &= crypto_int64_r; crypto_int64_x ^= crypto_int64_z; crypto_int64_y ^= crypto_int64_z; *crypto_int64_p = crypto_int64_x; *crypto_int64_q = crypto_int64_y; #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_smaller_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovlq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,%2\n csetm %0,lt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #else crypto_int64 crypto_int64_r = crypto_int64_x ^ crypto_int64_y; crypto_int64 crypto_int64_z = crypto_int64_x - crypto_int64_y; crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_x); return crypto_int64_negative_mask(crypto_int64_z); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_smaller_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovlq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,%2\n cset %0,lt" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #else crypto_int64 crypto_int64_r = crypto_int64_x ^ crypto_int64_y; crypto_int64 crypto_int64_z = crypto_int64_x - crypto_int64_y; crypto_int64_z ^= crypto_int64_r & (crypto_int64_z ^ crypto_int64_x); return crypto_int64_unsigned_topbit_01(crypto_int64_z); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_leq_mask(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovleq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,%2\n csetm %0,le" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #else return ~crypto_int64_smaller_mask(crypto_int64_y,crypto_int64_x); #endif } __attribute__((unused)) static inline crypto_int64 crypto_int64_leq_01(crypto_int64 crypto_int64_x,crypto_int64 crypto_int64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 crypto_int64_q,crypto_int64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovleq %1,%0" : "=&r"(crypto_int64_z), "=&r"(crypto_int64_q) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int64 crypto_int64_z; __asm__ ("cmp %1,%2\n cset %0,le" : "=r"(crypto_int64_z) : "r"(crypto_int64_x), "r"(crypto_int64_y) : "cc"); return crypto_int64_z; #else return 1-crypto_int64_smaller_01(crypto_int64_y,crypto_int64_x); #endif } __attribute__((unused)) static inline int crypto_int64_ones_num(crypto_int64 crypto_int64_x) { crypto_int64_unsigned crypto_int64_y = crypto_int64_x; const crypto_int64 C0 = 0x5555555555555555; const crypto_int64 C1 = 0x3333333333333333; const crypto_int64 C2 = 0x0f0f0f0f0f0f0f0f; crypto_int64_y -= ((crypto_int64_y >> 1) & C0); crypto_int64_y = (crypto_int64_y & C1) + ((crypto_int64_y >> 2) & C1); crypto_int64_y = (crypto_int64_y + (crypto_int64_y >> 4)) & C2; crypto_int64_y += crypto_int64_y >> 8; crypto_int64_y += crypto_int64_y >> 16; crypto_int64_y = (crypto_int64_y + (crypto_int64_y >> 32)) & 0xff; return crypto_int64_y; } __attribute__((unused)) static inline int crypto_int64_bottomzeros_num(crypto_int64 crypto_int64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int64 fallback = 64; __asm__ ("bsfq %0,%0\n cmoveq %1,%0" : "+&r"(crypto_int64_x) : "r"(fallback) : "cc"); return crypto_int64_x; #elif defined(__GNUC__) && defined(__aarch64__) int64_t crypto_int64_y; __asm__ ("rbit %0,%1\n clz %0,%0" : "=r"(crypto_int64_y) : "r"(crypto_int64_x) : ); return crypto_int64_y; #else crypto_int64 crypto_int64_y = crypto_int64_x ^ (crypto_int64_x-1); crypto_int64_y = ((crypto_int64) crypto_int64_y) >> 1; crypto_int64_y &= ~(crypto_int64_x & (((crypto_int64) 1) << (64-1))); return crypto_int64_ones_num(crypto_int64_y); #endif } #endif dq-20250201/cryptoint/crypto_int8.h000066400000000000000000000550461474734131400170630ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_int8_h #define crypto_int8_h #include #define crypto_int8 int8_t #define crypto_int8_unsigned uint8_t #define crypto_int8_optblocker dq_int8_optblocker extern volatile crypto_int8 crypto_int8_optblocker; __attribute__((unused)) static inline crypto_int8 crypto_int8_load(const unsigned char *crypto_int8_s) { crypto_int8 crypto_int8_z = 0; crypto_int8_z |= ((crypto_int8) (*crypto_int8_s++)) << 0; return crypto_int8_z; } __attribute__((unused)) static inline crypto_int8 crypto_int8_load_bigendian(const unsigned char *crypto_int8_s) { crypto_int8 crypto_int8_z = 0; crypto_int8_z |= ((crypto_int8) (*crypto_int8_s++)) << 0; return crypto_int8_z; } __attribute__((unused)) static inline void crypto_int8_store(unsigned char *crypto_int8_s,crypto_int8 crypto_int8_x) { *crypto_int8_s++ = crypto_int8_x >> 0; } __attribute__((unused)) static inline void crypto_int8_store_bigendian(unsigned char *crypto_int8_s,crypto_int8 crypto_int8_x) { *crypto_int8_s++ = crypto_int8_x >> 0; } __attribute__((unused)) static inline crypto_int8 crypto_int8_negative_mask(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarb $7,%0" : "+r"(crypto_int8_x) : : "cc"); return crypto_int8_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_y; __asm__ ("sbfx %w0,%w1,7,1" : "=r"(crypto_int8_y) : "r"(crypto_int8_x) : ); return crypto_int8_y; #else crypto_int8_x >>= 8-6; crypto_int8_x += crypto_int8_optblocker; crypto_int8_x >>= 5; return crypto_int8_x; #endif } __attribute__((unused)) static inline crypto_int8_unsigned crypto_int8_unsigned_topbit_01(crypto_int8_unsigned crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrb $7,%0" : "+r"(crypto_int8_x) : : "cc"); return crypto_int8_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_y; __asm__ ("ubfx %w0,%w1,7,1" : "=r"(crypto_int8_y) : "r"(crypto_int8_x) : ); return crypto_int8_y; #else crypto_int8_x >>= 8-6; crypto_int8_x += crypto_int8_optblocker; crypto_int8_x >>= 5; return crypto_int8_x; #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_negative_01(crypto_int8 crypto_int8_x) { return crypto_int8_unsigned_topbit_01(crypto_int8_x); } __attribute__((unused)) static inline crypto_int8 crypto_int8_topbit_mask(crypto_int8 crypto_int8_x) { return crypto_int8_negative_mask(crypto_int8_x); } __attribute__((unused)) static inline crypto_int8 crypto_int8_topbit_01(crypto_int8 crypto_int8_x) { return crypto_int8_unsigned_topbit_01(crypto_int8_x); } __attribute__((unused)) static inline crypto_int8 crypto_int8_bottombit_mask(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andb $1,%0" : "+r"(crypto_int8_x) : : "cc"); return -crypto_int8_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_y; __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_int8_y) : "r"(crypto_int8_x) : ); return crypto_int8_y; #else crypto_int8_x &= 1 + crypto_int8_optblocker; return -crypto_int8_x; #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_bottombit_01(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andb $1,%0" : "+r"(crypto_int8_x) : : "cc"); return crypto_int8_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_y; __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_int8_y) : "r"(crypto_int8_x) : ); return crypto_int8_y; #else crypto_int8_x &= 1 + crypto_int8_optblocker; return crypto_int8_x; #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_bitinrangepublicpos_mask(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarb %%cl,%0" : "+r"(crypto_int8_x) : "c"(crypto_int8_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("sxtb %w0,%w0\n asr %w0,%w0,%w1" : "+&r"(crypto_int8_x) : "r"(crypto_int8_s) : ); #else crypto_int8_x >>= crypto_int8_s ^ crypto_int8_optblocker; #endif return crypto_int8_bottombit_mask(crypto_int8_x); } __attribute__((unused)) static inline crypto_int8 crypto_int8_bitinrangepublicpos_01(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarb %%cl,%0" : "+r"(crypto_int8_x) : "c"(crypto_int8_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("sxtb %w0,%w0\n asr %w0,%w0,%w1" : "+&r"(crypto_int8_x) : "r"(crypto_int8_s) : ); #else crypto_int8_x >>= crypto_int8_s ^ crypto_int8_optblocker; #endif return crypto_int8_bottombit_01(crypto_int8_x); } __attribute__((unused)) static inline crypto_int8 crypto_int8_shlmod(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_s) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8_s &= 7; __asm__ ("shlb %%cl,%0" : "+r"(crypto_int8_x) : "c"(crypto_int8_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,7\n and %w1,%w1,255\n lsl %w1,%w1,%w0" : "+&r"(crypto_int8_s), "+r"(crypto_int8_x) : : ); #else int crypto_int8_k, crypto_int8_l; for (crypto_int8_l = 0,crypto_int8_k = 1;crypto_int8_k < 8;++crypto_int8_l,crypto_int8_k *= 2) crypto_int8_x ^= (crypto_int8_x ^ (crypto_int8_x << crypto_int8_k)) & crypto_int8_bitinrangepublicpos_mask(crypto_int8_s,crypto_int8_l); #endif return crypto_int8_x; } __attribute__((unused)) static inline crypto_int8 crypto_int8_shrmod(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_s) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8_s &= 7; __asm__ ("sarb %%cl,%0" : "+r"(crypto_int8_x) : "c"(crypto_int8_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,7\n sxtb %w1,%w1\n asr %w1,%w1,%w0" : "+&r"(crypto_int8_s), "+r"(crypto_int8_x) : : ); #else int crypto_int8_k, crypto_int8_l; for (crypto_int8_l = 0,crypto_int8_k = 1;crypto_int8_k < 8;++crypto_int8_l,crypto_int8_k *= 2) crypto_int8_x ^= (crypto_int8_x ^ (crypto_int8_x >> crypto_int8_k)) & crypto_int8_bitinrangepublicpos_mask(crypto_int8_s,crypto_int8_l); #endif return crypto_int8_x; } __attribute__((unused)) static inline crypto_int8 crypto_int8_bitmod_mask(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_s) { crypto_int8_x = crypto_int8_shrmod(crypto_int8_x,crypto_int8_s); return crypto_int8_bottombit_mask(crypto_int8_x); } __attribute__((unused)) static inline crypto_int8 crypto_int8_bitmod_01(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_s) { crypto_int8_x = crypto_int8_shrmod(crypto_int8_x,crypto_int8_s); return crypto_int8_bottombit_01(crypto_int8_x); } __attribute__((unused)) static inline crypto_int8 crypto_int8_nonzero_mask(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n testb %b2,%b2\n cmovnel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("tst %w1,255\n csetm %w0,ne" : "=r"(crypto_int8_z) : "r"(crypto_int8_x) : "cc"); return crypto_int8_z; #else crypto_int8_x |= -crypto_int8_x; return crypto_int8_negative_mask(crypto_int8_x); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_nonzero_01(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n testb %b2,%b2\n cmovnel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("tst %w1,255\n cset %w0,ne" : "=r"(crypto_int8_z) : "r"(crypto_int8_x) : "cc"); return crypto_int8_z; #else crypto_int8_x |= -crypto_int8_x; return crypto_int8_unsigned_topbit_01(crypto_int8_x); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_positive_mask(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n testb %b2,%b2\n cmovgl %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("sxtb %w0,%w1\n cmp %w0,0\n csetm %w0,gt" : "=r"(crypto_int8_z) : "r"(crypto_int8_x) : "cc"); return crypto_int8_z; #else crypto_int8 crypto_int8_z = -crypto_int8_x; crypto_int8_z ^= crypto_int8_x & crypto_int8_z; return crypto_int8_negative_mask(crypto_int8_z); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_positive_01(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n testb %b2,%b2\n cmovgl %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("sxtb %w0,%w1\n cmp %w0,0\n cset %w0,gt" : "=r"(crypto_int8_z) : "r"(crypto_int8_x) : "cc"); return crypto_int8_z; #else crypto_int8 crypto_int8_z = -crypto_int8_x; crypto_int8_z ^= crypto_int8_x & crypto_int8_z; return crypto_int8_unsigned_topbit_01(crypto_int8_z); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_zero_mask(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n testb %b2,%b2\n cmovel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("tst %w1,255\n csetm %w0,eq" : "=r"(crypto_int8_z) : "r"(crypto_int8_x) : "cc"); return crypto_int8_z; #else return ~crypto_int8_nonzero_mask(crypto_int8_x); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_zero_01(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n testb %b2,%b2\n cmovel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("tst %w1,255\n cset %w0,eq" : "=r"(crypto_int8_z) : "r"(crypto_int8_x) : "cc"); return crypto_int8_z; #else return 1-crypto_int8_nonzero_01(crypto_int8_x); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_unequal_mask(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpb %b3,%b2\n cmovnel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32), "q"(crypto_int8_y32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n csetm %w0,ne" : "=&r"(crypto_int8_z) : "r"(crypto_int8_x), "r"(crypto_int8_y) : "cc"); return crypto_int8_z; #else return crypto_int8_nonzero_mask(crypto_int8_x ^ crypto_int8_y); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_unequal_01(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpb %b3,%b2\n cmovnel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32), "q"(crypto_int8_y32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n cset %w0,ne" : "=&r"(crypto_int8_z) : "r"(crypto_int8_x), "r"(crypto_int8_y) : "cc"); return crypto_int8_z; #else return crypto_int8_nonzero_01(crypto_int8_x ^ crypto_int8_y); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_equal_mask(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpb %b3,%b2\n cmovel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32), "q"(crypto_int8_y32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n csetm %w0,eq" : "=&r"(crypto_int8_z) : "r"(crypto_int8_x), "r"(crypto_int8_y) : "cc"); return crypto_int8_z; #else return ~crypto_int8_unequal_mask(crypto_int8_x,crypto_int8_y); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_equal_01(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpb %b3,%b2\n cmovel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32), "q"(crypto_int8_y32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n cset %w0,eq" : "=&r"(crypto_int8_z) : "r"(crypto_int8_x), "r"(crypto_int8_y) : "cc"); return crypto_int8_z; #else return 1-crypto_int8_unequal_01(crypto_int8_x,crypto_int8_y); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_min(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y; __asm__ ("cmpb %b1,%b0\n cmovgl %1,%0" : "+q"(crypto_int8_x32) : "q"(crypto_int8_y32) : "cc"); crypto_int8_x = crypto_int8_x32; return crypto_int8_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("sxtb %w0,%w0\n cmp %w0,%w1,sxtb\n csel %w0,%w0,%w1,lt" : "+&r"(crypto_int8_x) : "r"(crypto_int8_y) : "cc"); return crypto_int8_x; #else crypto_int8 crypto_int8_r = crypto_int8_y ^ crypto_int8_x; crypto_int8 crypto_int8_z = crypto_int8_y - crypto_int8_x; crypto_int8_z ^= crypto_int8_r & (crypto_int8_z ^ crypto_int8_y); crypto_int8_z = crypto_int8_negative_mask(crypto_int8_z); crypto_int8_z &= crypto_int8_r; return crypto_int8_x ^ crypto_int8_z; #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_max(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y; __asm__ ("cmpb %b1,%b0\n cmovll %1,%0" : "+q"(crypto_int8_x32) : "q"(crypto_int8_y32) : "cc"); crypto_int8_x = crypto_int8_x32; return crypto_int8_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("sxtb %w0,%w0\n cmp %w0,%w1,sxtb\n csel %w0,%w1,%w0,lt" : "+&r"(crypto_int8_x) : "r"(crypto_int8_y) : "cc"); return crypto_int8_x; #else crypto_int8 crypto_int8_r = crypto_int8_y ^ crypto_int8_x; crypto_int8 crypto_int8_z = crypto_int8_y - crypto_int8_x; crypto_int8_z ^= crypto_int8_r & (crypto_int8_z ^ crypto_int8_y); crypto_int8_z = crypto_int8_negative_mask(crypto_int8_z); crypto_int8_z &= crypto_int8_r; return crypto_int8_y ^ crypto_int8_z; #endif } __attribute__((unused)) static inline void crypto_int8_minmax(crypto_int8 *crypto_int8_p,crypto_int8 *crypto_int8_q) { crypto_int8 crypto_int8_x = *crypto_int8_p; crypto_int8 crypto_int8_y = *crypto_int8_q; #if defined(__GNUC__) && defined(__x86_64__) int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_z32; __asm__ ("cmpb %b2,%b1\n movl %1,%0\n cmovgl %2,%1\n cmovgl %0,%2" : "=&r"(crypto_int8_z32), "+&q"(crypto_int8_x32), "+q"(crypto_int8_y32) : : "cc"); crypto_int8_x = crypto_int8_x32; crypto_int8_y = crypto_int8_y32; *crypto_int8_p = crypto_int8_x; *crypto_int8_q = crypto_int8_y; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_r, crypto_int8_s; __asm__ ("sxtb %w0,%w0\n cmp %w0,%w3,sxtb\n csel %w1,%w0,%w3,lt\n csel %w2,%w3,%w0,lt" : "+&r"(crypto_int8_x), "=&r"(crypto_int8_r), "=r"(crypto_int8_s) : "r"(crypto_int8_y) : "cc"); *crypto_int8_p = crypto_int8_r; *crypto_int8_q = crypto_int8_s; #else crypto_int8 crypto_int8_r = crypto_int8_y ^ crypto_int8_x; crypto_int8 crypto_int8_z = crypto_int8_y - crypto_int8_x; crypto_int8_z ^= crypto_int8_r & (crypto_int8_z ^ crypto_int8_y); crypto_int8_z = crypto_int8_negative_mask(crypto_int8_z); crypto_int8_z &= crypto_int8_r; crypto_int8_x ^= crypto_int8_z; crypto_int8_y ^= crypto_int8_z; *crypto_int8_p = crypto_int8_x; *crypto_int8_q = crypto_int8_y; #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_smaller_mask(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpb %b3,%b2\n cmovll %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32), "q"(crypto_int8_y32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("sxtb %w0,%w1\n cmp %w0,%w2,sxtb\n csetm %w0,lt" : "=&r"(crypto_int8_z) : "r"(crypto_int8_x), "r"(crypto_int8_y) : "cc"); return crypto_int8_z; #else crypto_int8 crypto_int8_r = crypto_int8_x ^ crypto_int8_y; crypto_int8 crypto_int8_z = crypto_int8_x - crypto_int8_y; crypto_int8_z ^= crypto_int8_r & (crypto_int8_z ^ crypto_int8_x); return crypto_int8_negative_mask(crypto_int8_z); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_smaller_01(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpb %b3,%b2\n cmovll %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32), "q"(crypto_int8_y32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("sxtb %w0,%w1\n cmp %w0,%w2,sxtb\n cset %w0,lt" : "=&r"(crypto_int8_z) : "r"(crypto_int8_x), "r"(crypto_int8_y) : "cc"); return crypto_int8_z; #else crypto_int8 crypto_int8_r = crypto_int8_x ^ crypto_int8_y; crypto_int8 crypto_int8_z = crypto_int8_x - crypto_int8_y; crypto_int8_z ^= crypto_int8_r & (crypto_int8_z ^ crypto_int8_x); return crypto_int8_unsigned_topbit_01(crypto_int8_z); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_leq_mask(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpb %b3,%b2\n cmovlel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32), "q"(crypto_int8_y32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("sxtb %w0,%w1\n cmp %w0,%w2,sxtb\n csetm %w0,le" : "=&r"(crypto_int8_z) : "r"(crypto_int8_x), "r"(crypto_int8_y) : "cc"); return crypto_int8_z; #else return ~crypto_int8_smaller_mask(crypto_int8_y,crypto_int8_x); #endif } __attribute__((unused)) static inline crypto_int8 crypto_int8_leq_01(crypto_int8 crypto_int8_x,crypto_int8 crypto_int8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_int8 crypto_int8_z; int32_t crypto_int8_x32 = crypto_int8_x,crypto_int8_y32 = crypto_int8_y,crypto_int8_q32,crypto_int8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpb %b3,%b2\n cmovlel %1,%0" : "=&r"(crypto_int8_z32), "=&r"(crypto_int8_q32) : "q"(crypto_int8_x32), "q"(crypto_int8_y32) : "cc"); crypto_int8_z = crypto_int8_z32; return crypto_int8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_int8 crypto_int8_z; __asm__ ("sxtb %w0,%w1\n cmp %w0,%w2,sxtb\n cset %w0,le" : "=&r"(crypto_int8_z) : "r"(crypto_int8_x), "r"(crypto_int8_y) : "cc"); return crypto_int8_z; #else return 1-crypto_int8_smaller_01(crypto_int8_y,crypto_int8_x); #endif } __attribute__((unused)) static inline int crypto_int8_ones_num(crypto_int8 crypto_int8_x) { crypto_int8_unsigned crypto_int8_y = crypto_int8_x; const crypto_int8 C0 = 0x55; const crypto_int8 C1 = 0x33; const crypto_int8 C2 = 0x0f; crypto_int8_y -= ((crypto_int8_y >> 1) & C0); crypto_int8_y = (crypto_int8_y & C1) + ((crypto_int8_y >> 2) & C1); crypto_int8_y = (crypto_int8_y + (crypto_int8_y >> 4)) & C2; return crypto_int8_y; } __attribute__((unused)) static inline int crypto_int8_bottomzeros_num(crypto_int8 crypto_int8_x) { #if defined(__GNUC__) && defined(__x86_64__) int32_t fallback = 8; int32_t crypto_int8_x32 = crypto_int8_x; __asm__ ("bsfl %0,%0\n cmovel %1,%0" : "+&r"(crypto_int8_x32) : "r"(fallback) : "cc"); crypto_int8_x = crypto_int8_x32; return crypto_int8_x; #elif defined(__GNUC__) && defined(__aarch64__) int64_t crypto_int8_y; __asm__ ("orr %w0,%w1,-256\n rbit %w0,%w0\n clz %w0,%w0" : "=r"(crypto_int8_y) : "r"(crypto_int8_x) : ); return crypto_int8_y; #else crypto_int8 crypto_int8_y = crypto_int8_x ^ (crypto_int8_x-1); crypto_int8_y = ((crypto_int8) crypto_int8_y) >> 1; crypto_int8_y &= ~(crypto_int8_x & (((crypto_int8) 1) << (8-1))); return crypto_int8_ones_num(crypto_int8_y); #endif } #endif dq-20250201/cryptoint/crypto_intN.h000066400000000000000000001407401474734131400171050ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_intN_h #define crypto_intN_h #include #define crypto_intN intN_t #define crypto_intN_unsigned uintN_t #define crypto_intN_optblocker namespace_intN_optblocker extern volatile crypto_intN crypto_intN_optblocker; __attribute__((unused)) static inline crypto_intN crypto_intN_load(const unsigned char *crypto_intN_s) { crypto_intN crypto_intN_z = 0; int crypto_intN_k; for (crypto_intN_k = 0;crypto_intN_k < N;crypto_intN_k += 8) crypto_intN_z |= ((crypto_intN) (*crypto_intN_s++)) << crypto_intN_k; return crypto_intN_z; } __attribute__((unused)) static inline crypto_intN crypto_intN_load_bigendian(const unsigned char *crypto_intN_s) { crypto_intN crypto_intN_z = 0; int crypto_intN_k; for (crypto_intN_k = N - 8;crypto_intN_k >= 0;crypto_intN_k -= 8) crypto_intN_z |= ((crypto_intN) (*crypto_intN_s++)) << crypto_intN_k; return crypto_intN_z; } __attribute__((unused)) static inline void crypto_intN_store(unsigned char *crypto_intN_s,crypto_intN crypto_intN_x) { int crypto_intN_k; for (crypto_intN_k = 0;crypto_intN_k < N;crypto_intN_k += 8) *crypto_intN_s++ = crypto_intN_x >> crypto_intN_k; } __attribute__((unused)) static inline void crypto_intN_store_bigendian(unsigned char *crypto_intN_s,crypto_intN crypto_intN_x) { int crypto_intN_k; for (crypto_intN_k = N - 8;crypto_intN_k >= 0;crypto_intN_k -= 8) *crypto_intN_s++ = crypto_intN_x >> crypto_intN_k; } __attribute__((unused)) static inline crypto_intN crypto_intN_negative_mask(crypto_intN crypto_intN_x) { #if amd64 8: readasm("amd64; int8 crypto_intN_x; crypto_intN_x signed>>= 7"); 16: readasm("amd64; int16 crypto_intN_x; crypto_intN_x signed>>= 15"); 32: readasm("amd64; int32 crypto_intN_x; crypto_intN_x signed>>= 31"); 64: readasm("amd64; int64 crypto_intN_x; crypto_intN_x signed>>= 63"); return crypto_intN_x; #elif arm64 crypto_intN crypto_intN_y; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y; crypto_intN_y = -(1 & (crypto_intN_x unsigned>> 7))"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y; crypto_intN_y = -(1 & (crypto_intN_x unsigned>> 15))"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y; crypto_intN_y = crypto_intN_x signed>> 31"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y; crypto_intN_y = crypto_intN_x signed>> 63"); return crypto_intN_y; #else crypto_intN_x >>= N-6; crypto_intN_x += crypto_intN_optblocker; crypto_intN_x >>= 5; return crypto_intN_x; #endif } __attribute__((unused)) static inline crypto_intN_unsigned crypto_intN_unsigned_topbit_01(crypto_intN_unsigned crypto_intN_x) { #if amd64 8: readasm("amd64; int8 crypto_intN_x; crypto_intN_x unsigned>>= 7"); 16: readasm("amd64; int16 crypto_intN_x; crypto_intN_x unsigned>>= 15"); 32: readasm("amd64; int32 crypto_intN_x; crypto_intN_x unsigned>>= 31"); 64: readasm("amd64; int64 crypto_intN_x; crypto_intN_x unsigned>>= 63"); return crypto_intN_x; #elif arm64 crypto_intN crypto_intN_y; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y; crypto_intN_y = 1 & (crypto_intN_x unsigned>> 7)"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y; crypto_intN_y = 1 & (crypto_intN_x unsigned>> 15)"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y; crypto_intN_y = crypto_intN_x unsigned>> 31"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y; crypto_intN_y = crypto_intN_x unsigned>> 63"); return crypto_intN_y; #else crypto_intN_x >>= N-6; crypto_intN_x += crypto_intN_optblocker; crypto_intN_x >>= 5; return crypto_intN_x; #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_negative_01(crypto_intN crypto_intN_x) { return crypto_intN_unsigned_topbit_01(crypto_intN_x); } __attribute__((unused)) static inline crypto_intN crypto_intN_topbit_mask(crypto_intN crypto_intN_x) { return crypto_intN_negative_mask(crypto_intN_x); } __attribute__((unused)) static inline crypto_intN crypto_intN_topbit_01(crypto_intN crypto_intN_x) { return crypto_intN_unsigned_topbit_01(crypto_intN_x); } __attribute__((unused)) static inline crypto_intN crypto_intN_bottombit_mask(crypto_intN crypto_intN_x) { #if amd64 8: readasm("amd64; int8 crypto_intN_x; crypto_intN_x &= 1"); 16: readasm("amd64; int16 crypto_intN_x; crypto_intN_x &= 1"); 32: readasm("amd64; int32 crypto_intN_x; crypto_intN_x &= 1"); 64: readasm("amd64; int64 crypto_intN_x; crypto_intN_x &= 1"); return -crypto_intN_x; #elif arm64 crypto_intN crypto_intN_y; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y; crypto_intN_y = -(1 & (crypto_intN_x unsigned>> 0))"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y; crypto_intN_y = -(1 & (crypto_intN_x unsigned>> 0))"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y; crypto_intN_y = -(1 & (crypto_intN_x unsigned>> 0))"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y; crypto_intN_y = -(1 & (crypto_intN_x unsigned>> 0))"); return crypto_intN_y; #else crypto_intN_x &= 1 + crypto_intN_optblocker; return -crypto_intN_x; #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_bottombit_01(crypto_intN crypto_intN_x) { #if amd64 8: readasm("amd64; int8 crypto_intN_x; crypto_intN_x &= 1"); 16: readasm("amd64; int16 crypto_intN_x; crypto_intN_x &= 1"); 32: readasm("amd64; int32 crypto_intN_x; crypto_intN_x &= 1"); 64: readasm("amd64; int64 crypto_intN_x; crypto_intN_x &= 1"); return crypto_intN_x; #elif arm64 crypto_intN crypto_intN_y; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y; crypto_intN_y = 1 & (crypto_intN_x unsigned>> 0)"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y; crypto_intN_y = 1 & (crypto_intN_x unsigned>> 0)"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y; crypto_intN_y = 1 & (crypto_intN_x unsigned>> 0)"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y; crypto_intN_y = 1 & (crypto_intN_x unsigned>> 0)"); return crypto_intN_y; #else crypto_intN_x &= 1 + crypto_intN_optblocker; return crypto_intN_x; #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_bitinrangepublicpos_mask(crypto_intN crypto_intN_x,crypto_intN crypto_intN_s) { #if amd64 8: readasm("amd64; int8 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 16: readasm("amd64; int16 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 32: readasm("amd64; int32 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 64: readasm("amd64; int64 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); #elif arm64 8: readasm("arm64; int8 crypto_intN_x crypto_intN_s; crypto_intN_x = (int8) crypto_intN_x; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_s; crypto_intN_x = (int16) crypto_intN_x; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_s; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_s; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); #else crypto_intN_x >>= crypto_intN_s ^ crypto_intN_optblocker; #endif return crypto_intN_bottombit_mask(crypto_intN_x); } __attribute__((unused)) static inline crypto_intN crypto_intN_bitinrangepublicpos_01(crypto_intN crypto_intN_x,crypto_intN crypto_intN_s) { #if amd64 8: readasm("amd64; int8 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 16: readasm("amd64; int16 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 32: readasm("amd64; int32 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 64: readasm("amd64; int64 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); #elif arm64 8: readasm("arm64; int8 crypto_intN_x crypto_intN_s; crypto_intN_x = (int8) crypto_intN_x; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_s; crypto_intN_x = (int16) crypto_intN_x; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_s; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_s; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); #else crypto_intN_x >>= crypto_intN_s ^ crypto_intN_optblocker; #endif return crypto_intN_bottombit_01(crypto_intN_x); } __attribute__((unused)) static inline crypto_intN crypto_intN_shlmod(crypto_intN crypto_intN_x,crypto_intN crypto_intN_s) { #if amd64 8: crypto_intN_s &= 7; 8: readasm("amd64; int8 crypto_intN_x crypto_intN_s; crypto_intN_x <<= crypto_intN_s"); 16: crypto_intN_s &= 15; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_s; crypto_intN_x <<= crypto_intN_s"); 32: readasm("amd64; int32 crypto_intN_x crypto_intN_s; crypto_intN_x <<= crypto_intN_s"); 64: readasm("amd64; int64 crypto_intN_x crypto_intN_s; crypto_intN_x <<= crypto_intN_s"); #elif arm64 8: readasm("arm64; int8 crypto_intN_x crypto_intN_s; crypto_intN_s = crypto_intN_s & 7; crypto_intN_x = (uint8) crypto_intN_x; crypto_intN_x = crypto_intN_x << crypto_intN_s"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_s; crypto_intN_s = crypto_intN_s & 15; crypto_intN_x = (uint16) crypto_intN_x; crypto_intN_x = crypto_intN_x << crypto_intN_s"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_s; crypto_intN_x = crypto_intN_x << crypto_intN_s"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_s; crypto_intN_x = crypto_intN_x << crypto_intN_s"); #else int crypto_intN_k, crypto_intN_l; for (crypto_intN_l = 0,crypto_intN_k = 1;crypto_intN_k < N;++crypto_intN_l,crypto_intN_k *= 2) crypto_intN_x ^= (crypto_intN_x ^ (crypto_intN_x << crypto_intN_k)) & crypto_intN_bitinrangepublicpos_mask(crypto_intN_s,crypto_intN_l); #endif return crypto_intN_x; } __attribute__((unused)) static inline crypto_intN crypto_intN_shrmod(crypto_intN crypto_intN_x,crypto_intN crypto_intN_s) { #if amd64 8: crypto_intN_s &= 7; 8: readasm("amd64; int8 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 16: crypto_intN_s &= 15; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 32: readasm("amd64; int32 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); 64: readasm("amd64; int64 crypto_intN_x crypto_intN_s; crypto_intN_x signed>>= crypto_intN_s"); #elif arm64 8: readasm("arm64; int8 crypto_intN_x crypto_intN_s; crypto_intN_s = crypto_intN_s & 7; crypto_intN_x = (int8) crypto_intN_x; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_s; crypto_intN_s = crypto_intN_s & 15; crypto_intN_x = (int16) crypto_intN_x; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_s; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_s; crypto_intN_x = crypto_intN_x signed>> crypto_intN_s"); #else int crypto_intN_k, crypto_intN_l; for (crypto_intN_l = 0,crypto_intN_k = 1;crypto_intN_k < N;++crypto_intN_l,crypto_intN_k *= 2) crypto_intN_x ^= (crypto_intN_x ^ (crypto_intN_x >> crypto_intN_k)) & crypto_intN_bitinrangepublicpos_mask(crypto_intN_s,crypto_intN_l); #endif return crypto_intN_x; } __attribute__((unused)) static inline crypto_intN crypto_intN_bitmod_mask(crypto_intN crypto_intN_x,crypto_intN crypto_intN_s) { crypto_intN_x = crypto_intN_shrmod(crypto_intN_x,crypto_intN_s); return crypto_intN_bottombit_mask(crypto_intN_x); } __attribute__((unused)) static inline crypto_intN crypto_intN_bitmod_01(crypto_intN crypto_intN_x,crypto_intN crypto_intN_s) { crypto_intN_x = crypto_intN_shrmod(crypto_intN_x,crypto_intN_s); return crypto_intN_bottombit_01(crypto_intN_x); } __attribute__((unused)) static inline crypto_intN crypto_intN_nonzero_mask(crypto_intN crypto_intN_x) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = -1; crypto_intN_x32 & (int8) crypto_intN_x32; crypto_intN_z32 = crypto_intN_q32 if !="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if !="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if !="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if !="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_z; crypto_intN_x & 255; crypto_intN_z = -1 if != else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_z; crypto_intN_x & 65535; crypto_intN_z = -1 if != else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = -1 if != else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = -1 if != else 0"); return crypto_intN_z; #else crypto_intN_x |= -crypto_intN_x; return crypto_intN_negative_mask(crypto_intN_x); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_nonzero_01(crypto_intN crypto_intN_x) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = 1; crypto_intN_x32 & (int8) crypto_intN_x32; crypto_intN_z32 = crypto_intN_q32 if !="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if !="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if !="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if !="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_z; crypto_intN_x & 255; crypto_intN_z = 1 if != else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_z; crypto_intN_x & 65535; crypto_intN_z = 1 if != else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = 1 if != else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = 1 if != else 0"); return crypto_intN_z; #else crypto_intN_x |= -crypto_intN_x; return crypto_intN_unsigned_topbit_01(crypto_intN_x); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_positive_mask(crypto_intN crypto_intN_x) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = -1; crypto_intN_x32 & (int8) crypto_intN_x32; crypto_intN_z32 = crypto_intN_q32 if signed>"); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if signed>"); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if signed>"); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if signed>"); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_z; crypto_intN_z = (int8) crypto_intN_x; crypto_intN_z - 0; crypto_intN_z = -1 if signed> else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_z; crypto_intN_z = (int16) crypto_intN_x; crypto_intN_z - 0; crypto_intN_z = -1 if signed> else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = -1 if signed> else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = -1 if signed> else 0"); return crypto_intN_z; #else crypto_intN crypto_intN_z = -crypto_intN_x; crypto_intN_z ^= crypto_intN_x & crypto_intN_z; return crypto_intN_negative_mask(crypto_intN_z); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_positive_01(crypto_intN crypto_intN_x) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = 1; crypto_intN_x32 & (int8) crypto_intN_x32; crypto_intN_z32 = crypto_intN_q32 if signed>"); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if signed>"); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if signed>"); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if signed>"); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_z; crypto_intN_z = (int8) crypto_intN_x; crypto_intN_z - 0; crypto_intN_z = 1 if signed> else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_z; crypto_intN_z = (int16) crypto_intN_x; crypto_intN_z - 0; crypto_intN_z = 1 if signed> else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = 1 if signed> else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = 1 if signed> else 0"); return crypto_intN_z; #else crypto_intN crypto_intN_z = -crypto_intN_x; crypto_intN_z ^= crypto_intN_x & crypto_intN_z; return crypto_intN_unsigned_topbit_01(crypto_intN_z); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_zero_mask(crypto_intN crypto_intN_x) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = -1; crypto_intN_x32 & (int8) crypto_intN_x32; crypto_intN_z32 = crypto_intN_q32 if ="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if ="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if ="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if ="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_z; crypto_intN_x & 255; crypto_intN_z = -1 if = else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_z; crypto_intN_x & 65535; crypto_intN_z = -1 if = else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = -1 if = else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = -1 if = else 0"); return crypto_intN_z; #else return ~crypto_intN_nonzero_mask(crypto_intN_x); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_zero_01(crypto_intN crypto_intN_x) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = 1; crypto_intN_x32 & (int8) crypto_intN_x32; crypto_intN_z32 = crypto_intN_q32 if ="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if ="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if ="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x & crypto_intN_x; crypto_intN_z = crypto_intN_q if ="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_z; crypto_intN_x & 255; crypto_intN_z = 1 if = else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_z; crypto_intN_x & 65535; crypto_intN_z = 1 if = else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = 1 if = else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_z; crypto_intN_x - 0; crypto_intN_z = 1 if = else 0"); return crypto_intN_z; #else return 1-crypto_intN_nonzero_01(crypto_intN_x); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_unequal_mask(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_y32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = -1; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_q32 if !="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if !="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if !="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if !="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (uint8) crypto_intN_x; crypto_intN_z - (uint8) crypto_intN_y; crypto_intN_z = -1 if != else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (uint16) crypto_intN_x; crypto_intN_z - (uint16) crypto_intN_y; crypto_intN_z = -1 if != else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = -1 if != else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = -1 if != else 0"); return crypto_intN_z; #else return crypto_intN_nonzero_mask(crypto_intN_x ^ crypto_intN_y); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_unequal_01(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_y32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = 1; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_q32 if !="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if !="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if !="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if !="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (uint8) crypto_intN_x; crypto_intN_z - (uint8) crypto_intN_y; crypto_intN_z = 1 if != else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (uint16) crypto_intN_x; crypto_intN_z - (uint16) crypto_intN_y; crypto_intN_z = 1 if != else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = 1 if != else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = 1 if != else 0"); return crypto_intN_z; #else return crypto_intN_nonzero_01(crypto_intN_x ^ crypto_intN_y); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_equal_mask(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_y32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = -1; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_q32 if ="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if ="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if ="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if ="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (uint8) crypto_intN_x; crypto_intN_z - (uint8) crypto_intN_y; crypto_intN_z = -1 if = else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (uint16) crypto_intN_x; crypto_intN_z - (uint16) crypto_intN_y; crypto_intN_z = -1 if = else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = -1 if = else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = -1 if = else 0"); return crypto_intN_z; #else return ~crypto_intN_unequal_mask(crypto_intN_x,crypto_intN_y); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_equal_01(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_y32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = 1; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_q32 if ="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if ="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if ="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if ="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (uint8) crypto_intN_x; crypto_intN_z - (uint8) crypto_intN_y; crypto_intN_z = 1 if = else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (uint16) crypto_intN_x; crypto_intN_z - (uint16) crypto_intN_y; crypto_intN_z = 1 if = else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = 1 if = else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = 1 if = else 0"); return crypto_intN_z; #else return 1-crypto_intN_unequal_01(crypto_intN_x,crypto_intN_y); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_min(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_y32; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_x32 = crypto_intN_y32 if signed>"); 8: crypto_intN_x = crypto_intN_x32; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_y if signed>"); 32: readasm("amd64; int32 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_y if signed>"); 64: readasm("amd64; int64 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_y if signed>"); return crypto_intN_x; #elif arm64 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y; crypto_intN_x = (int8) crypto_intN_x; crypto_intN_x - (int8) crypto_intN_y; crypto_intN_x = crypto_intN_x if signed< else crypto_intN_y"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y; crypto_intN_x = (int16) crypto_intN_x; crypto_intN_x - (int16) crypto_intN_y; crypto_intN_x = crypto_intN_x if signed< else crypto_intN_y"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_x if signed< else crypto_intN_y"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_x if signed< else crypto_intN_y"); return crypto_intN_x; #else crypto_intN crypto_intN_r = crypto_intN_y ^ crypto_intN_x; crypto_intN crypto_intN_z = crypto_intN_y - crypto_intN_x; crypto_intN_z ^= crypto_intN_r & (crypto_intN_z ^ crypto_intN_y); crypto_intN_z = crypto_intN_negative_mask(crypto_intN_z); crypto_intN_z &= crypto_intN_r; return crypto_intN_x ^ crypto_intN_z; #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_max(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_y32; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_x32 = crypto_intN_y32 if signed<"); 8: crypto_intN_x = crypto_intN_x32; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_y if signed<"); 32: readasm("amd64; int32 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_y if signed<"); 64: readasm("amd64; int64 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_y if signed<"); return crypto_intN_x; #elif arm64 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y; crypto_intN_x = (int8) crypto_intN_x; crypto_intN_x - (int8) crypto_intN_y; crypto_intN_x = crypto_intN_y if signed< else crypto_intN_x"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y; crypto_intN_x = (int16) crypto_intN_x; crypto_intN_x - (int16) crypto_intN_y; crypto_intN_x = crypto_intN_y if signed< else crypto_intN_x"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_y if signed< else crypto_intN_x"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y; crypto_intN_x - crypto_intN_y; crypto_intN_x = crypto_intN_y if signed< else crypto_intN_x"); return crypto_intN_x; #else crypto_intN crypto_intN_r = crypto_intN_y ^ crypto_intN_x; crypto_intN crypto_intN_z = crypto_intN_y - crypto_intN_x; crypto_intN_z ^= crypto_intN_r & (crypto_intN_z ^ crypto_intN_y); crypto_intN_z = crypto_intN_negative_mask(crypto_intN_z); crypto_intN_z &= crypto_intN_r; return crypto_intN_y ^ crypto_intN_z; #endif } __attribute__((unused)) static inline void crypto_intN_minmax(crypto_intN *crypto_intN_p,crypto_intN *crypto_intN_q) { crypto_intN crypto_intN_x = *crypto_intN_p; crypto_intN crypto_intN_y = *crypto_intN_q; #if amd64 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_y32 crypto_intN_z32; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_x32; crypto_intN_x32 = crypto_intN_y32 if signed>; crypto_intN_y32 = crypto_intN_z32 if signed>"); 8: crypto_intN_x = crypto_intN_x32; crypto_intN_y = crypto_intN_y32; 16: crypto_intN crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_x; crypto_intN_x = crypto_intN_y if signed>; crypto_intN_y = crypto_intN_z if signed>"); 32: crypto_intN crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_x; crypto_intN_x = crypto_intN_y if signed>; crypto_intN_y = crypto_intN_z if signed>"); 64: crypto_intN crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_x; crypto_intN_x = crypto_intN_y if signed>; crypto_intN_y = crypto_intN_z if signed>"); *crypto_intN_p = crypto_intN_x; *crypto_intN_q = crypto_intN_y; #elif arm64 crypto_intN crypto_intN_r, crypto_intN_s; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_r crypto_intN_s; crypto_intN_x = (int8) crypto_intN_x; crypto_intN_x - (int8) crypto_intN_y; crypto_intN_r = crypto_intN_x if signed< else crypto_intN_y; crypto_intN_s = crypto_intN_y if signed< else crypto_intN_x"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_r crypto_intN_s; crypto_intN_x = (int16) crypto_intN_x; crypto_intN_x - (int16) crypto_intN_y; crypto_intN_r = crypto_intN_x if signed< else crypto_intN_y; crypto_intN_s = crypto_intN_y if signed< else crypto_intN_x"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_r crypto_intN_s; crypto_intN_x - crypto_intN_y; crypto_intN_r = crypto_intN_x if signed< else crypto_intN_y; crypto_intN_s = crypto_intN_y if signed< else crypto_intN_x"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_r crypto_intN_s; crypto_intN_x - crypto_intN_y; crypto_intN_r = crypto_intN_x if signed< else crypto_intN_y; crypto_intN_s = crypto_intN_y if signed< else crypto_intN_x"); *crypto_intN_p = crypto_intN_r; *crypto_intN_q = crypto_intN_s; #else crypto_intN crypto_intN_r = crypto_intN_y ^ crypto_intN_x; crypto_intN crypto_intN_z = crypto_intN_y - crypto_intN_x; crypto_intN_z ^= crypto_intN_r & (crypto_intN_z ^ crypto_intN_y); crypto_intN_z = crypto_intN_negative_mask(crypto_intN_z); crypto_intN_z &= crypto_intN_r; crypto_intN_x ^= crypto_intN_z; crypto_intN_y ^= crypto_intN_z; *crypto_intN_p = crypto_intN_x; *crypto_intN_q = crypto_intN_y; #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_smaller_mask(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_y32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = -1; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_q32 if signed<"); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<"); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<"); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<"); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (int8) crypto_intN_x; crypto_intN_z - (int8) crypto_intN_y; crypto_intN_z = -1 if signed< else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (int16) crypto_intN_x; crypto_intN_z - (int16) crypto_intN_y; crypto_intN_z = -1 if signed< else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = -1 if signed< else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = -1 if signed< else 0"); return crypto_intN_z; #else crypto_intN crypto_intN_r = crypto_intN_x ^ crypto_intN_y; crypto_intN crypto_intN_z = crypto_intN_x - crypto_intN_y; crypto_intN_z ^= crypto_intN_r & (crypto_intN_z ^ crypto_intN_x); return crypto_intN_negative_mask(crypto_intN_z); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_smaller_01(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_y32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = 1; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_q32 if signed<"); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<"); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<"); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<"); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (int8) crypto_intN_x; crypto_intN_z - (int8) crypto_intN_y; crypto_intN_z = 1 if signed< else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (int16) crypto_intN_x; crypto_intN_z - (int16) crypto_intN_y; crypto_intN_z = 1 if signed< else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = 1 if signed< else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = 1 if signed< else 0"); return crypto_intN_z; #else crypto_intN crypto_intN_r = crypto_intN_x ^ crypto_intN_y; crypto_intN crypto_intN_z = crypto_intN_x - crypto_intN_y; crypto_intN_z ^= crypto_intN_r & (crypto_intN_z ^ crypto_intN_x); return crypto_intN_unsigned_topbit_01(crypto_intN_z); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_leq_mask(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_y32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = -1; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_q32 if signed<="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = -1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (int8) crypto_intN_x; crypto_intN_z - (int8) crypto_intN_y; crypto_intN_z = -1 if signed<= else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (int16) crypto_intN_x; crypto_intN_z - (int16) crypto_intN_y; crypto_intN_z = -1 if signed<= else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = -1 if signed<= else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = -1 if signed<= else 0"); return crypto_intN_z; #else return ~crypto_intN_smaller_mask(crypto_intN_y,crypto_intN_x); #endif } __attribute__((unused)) static inline crypto_intN crypto_intN_leq_01(crypto_intN crypto_intN_x,crypto_intN crypto_intN_y) { #if amd64 8: crypto_intN crypto_intN_z; 8: int32_t crypto_intN_x32 = crypto_intN_x,crypto_intN_y32 = crypto_intN_y,crypto_intN_q32,crypto_intN_z32; 8: readasm("amd64; int32 crypto_intN_x32 crypto_intN_q32 crypto_intN_y32 crypto_intN_z32; crypto_intN_z32 = 0; crypto_intN_q32 = 1; crypto_intN_x32 - (int8) crypto_intN_y32; crypto_intN_z32 = crypto_intN_q32 if signed<="); 8: crypto_intN_z = crypto_intN_z32; 16: crypto_intN crypto_intN_q,crypto_intN_z; 16: readasm("amd64; int16 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<="); 32: crypto_intN crypto_intN_q,crypto_intN_z; 32: readasm("amd64; int32 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<="); 64: crypto_intN crypto_intN_q,crypto_intN_z; 64: readasm("amd64; int64 crypto_intN_x crypto_intN_q crypto_intN_y crypto_intN_z; crypto_intN_z = 0; crypto_intN_q = 1; crypto_intN_x - crypto_intN_y; crypto_intN_z = crypto_intN_q if signed<="); return crypto_intN_z; #elif arm64 crypto_intN crypto_intN_z; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (int8) crypto_intN_x; crypto_intN_z - (int8) crypto_intN_y; crypto_intN_z = 1 if signed<= else 0"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_z = (int16) crypto_intN_x; crypto_intN_z - (int16) crypto_intN_y; crypto_intN_z = 1 if signed<= else 0"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = 1 if signed<= else 0"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y crypto_intN_z; crypto_intN_x - crypto_intN_y; crypto_intN_z = 1 if signed<= else 0"); return crypto_intN_z; #else return 1-crypto_intN_smaller_01(crypto_intN_y,crypto_intN_x); #endif } __attribute__((unused)) static inline int crypto_intN_ones_num(crypto_intN crypto_intN_x) { crypto_intN_unsigned crypto_intN_y = crypto_intN_x; 8: const crypto_intN C0 = 0x55; 16: const crypto_intN C0 = 0x5555; 32: const crypto_intN C0 = 0x55555555; 64: const crypto_intN C0 = 0x5555555555555555; 8: const crypto_intN C1 = 0x33; 16: const crypto_intN C1 = 0x3333; 32: const crypto_intN C1 = 0x33333333; 64: const crypto_intN C1 = 0x3333333333333333; 8: const crypto_intN C2 = 0x0f; 16: const crypto_intN C2 = 0x0f0f; 32: const crypto_intN C2 = 0x0f0f0f0f; 64: const crypto_intN C2 = 0x0f0f0f0f0f0f0f0f; crypto_intN_y -= ((crypto_intN_y >> 1) & C0); crypto_intN_y = (crypto_intN_y & C1) + ((crypto_intN_y >> 2) & C1); crypto_intN_y = (crypto_intN_y + (crypto_intN_y >> 4)) & C2; 16: crypto_intN_y = (crypto_intN_y + (crypto_intN_y >> 8)) & 0xff; 32: crypto_intN_y += crypto_intN_y >> 8; 32: crypto_intN_y = (crypto_intN_y + (crypto_intN_y >> 16)) & 0xff; 64: crypto_intN_y += crypto_intN_y >> 8; 64: crypto_intN_y += crypto_intN_y >> 16; 64: crypto_intN_y = (crypto_intN_y + (crypto_intN_y >> 32)) & 0xff; return crypto_intN_y; } __attribute__((unused)) static inline int crypto_intN_bottomzeros_num(crypto_intN crypto_intN_x) { #if amd64 8: int32_t fallback = N; 8: int32_t crypto_intN_x32 = crypto_intN_x; 8: readasm("amd64; int32 crypto_intN_x32 fallback; crypto_intN_x32 = numbottomzeros_tricky crypto_intN_x32; crypto_intN_x32 = fallback if ="); 8: crypto_intN_x = crypto_intN_x32; 16: crypto_intN fallback = N; 16: readasm("amd64; int16 crypto_intN_x fallback; crypto_intN_x = numbottomzeros_tricky crypto_intN_x; crypto_intN_x = fallback if ="); 32: crypto_intN fallback = N; 32: readasm("amd64; int32 crypto_intN_x fallback; crypto_intN_x = numbottomzeros_tricky crypto_intN_x; crypto_intN_x = fallback if ="); 64: crypto_intN fallback = N; 64: readasm("amd64; int64 crypto_intN_x fallback; crypto_intN_x = numbottomzeros_tricky crypto_intN_x; crypto_intN_x = fallback if ="); return crypto_intN_x; #elif arm64 int64_t crypto_intN_y; 8: readasm("arm64; int8 crypto_intN_x crypto_intN_y; crypto_intN_y = crypto_intN_x | -256; crypto_intN_y = bitrev32 crypto_intN_y; crypto_intN_y = numbottomzeros crypto_intN_y"); 16: readasm("arm64; int16 crypto_intN_x crypto_intN_y; crypto_intN_y = crypto_intN_x | -65536; crypto_intN_y = bitrev32 crypto_intN_y; crypto_intN_y = numbottomzeros crypto_intN_y"); 32: readasm("arm64; int32 crypto_intN_x crypto_intN_y; crypto_intN_y = bitrev32 crypto_intN_x; crypto_intN_y = numbottomzeros crypto_intN_y"); 64: readasm("arm64; int64 crypto_intN_x crypto_intN_y; crypto_intN_y = bitrev64 crypto_intN_x; crypto_intN_y = numbottomzeros crypto_intN_y"); return crypto_intN_y; #else crypto_intN crypto_intN_y = crypto_intN_x ^ (crypto_intN_x-1); crypto_intN_y = ((crypto_intN) crypto_intN_y) >> 1; crypto_intN_y &= ~(crypto_intN_x & (((crypto_intN) 1) << (N-1))); return crypto_intN_ones_num(crypto_intN_y); #endif } #endif dq-20250201/cryptoint/crypto_uint16.h000066400000000000000000000511261474734131400173220ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_uint16_h #define crypto_uint16_h #include #define crypto_uint16 uint16_t #define crypto_uint16_signed int16_t #define crypto_uint16_signed_optblocker dq_uint16_signed_optblocker extern volatile crypto_uint16_signed crypto_uint16_signed_optblocker; __attribute__((unused)) static inline crypto_uint16 crypto_uint16_load(const unsigned char *crypto_uint16_s) { crypto_uint16 crypto_uint16_z = 0; crypto_uint16_z |= ((crypto_uint16) (*crypto_uint16_s++)) << 0; crypto_uint16_z |= ((crypto_uint16) (*crypto_uint16_s++)) << 8; return crypto_uint16_z; } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_load_bigendian(const unsigned char *crypto_uint16_s) { crypto_uint16 crypto_uint16_z = 0; crypto_uint16_z |= ((crypto_uint16) (*crypto_uint16_s++)) << 8; crypto_uint16_z |= ((crypto_uint16) (*crypto_uint16_s++)) << 0; return crypto_uint16_z; } __attribute__((unused)) static inline void crypto_uint16_store(unsigned char *crypto_uint16_s,crypto_uint16 crypto_uint16_x) { *crypto_uint16_s++ = crypto_uint16_x >> 0; *crypto_uint16_s++ = crypto_uint16_x >> 8; } __attribute__((unused)) static inline void crypto_uint16_store_bigendian(unsigned char *crypto_uint16_s,crypto_uint16 crypto_uint16_x) { *crypto_uint16_s++ = crypto_uint16_x >> 8; *crypto_uint16_s++ = crypto_uint16_x >> 0; } __attribute__((unused)) static inline crypto_uint16_signed crypto_uint16_signed_negative_mask(crypto_uint16_signed crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarw $15,%0" : "+r"(crypto_uint16_x) : : "cc"); return crypto_uint16_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16_signed crypto_uint16_y; __asm__ ("sbfx %w0,%w1,15,1" : "=r"(crypto_uint16_y) : "r"(crypto_uint16_x) : ); return crypto_uint16_y; #else crypto_uint16_x >>= 16-6; crypto_uint16_x += crypto_uint16_signed_optblocker; crypto_uint16_x >>= 5; return crypto_uint16_x; #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_topbit_01(crypto_uint16 crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrw $15,%0" : "+r"(crypto_uint16_x) : : "cc"); return crypto_uint16_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16_signed crypto_uint16_y; __asm__ ("ubfx %w0,%w1,15,1" : "=r"(crypto_uint16_y) : "r"(crypto_uint16_x) : ); return crypto_uint16_y; #else crypto_uint16_x >>= 16-6; crypto_uint16_x += crypto_uint16_signed_optblocker; crypto_uint16_x >>= 5; return crypto_uint16_x; #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_topbit_mask(crypto_uint16 crypto_uint16_x) { return crypto_uint16_signed_negative_mask(crypto_uint16_x); } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_bottombit_mask(crypto_uint16 crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andw $1,%0" : "+r"(crypto_uint16_x) : : "cc"); return -crypto_uint16_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_y; __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_uint16_y) : "r"(crypto_uint16_x) : ); return crypto_uint16_y; #else crypto_uint16_x &= 1 + crypto_uint16_signed_optblocker; return -crypto_uint16_x; #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_bottombit_01(crypto_uint16 crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andw $1,%0" : "+r"(crypto_uint16_x) : : "cc"); return crypto_uint16_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_y; __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_uint16_y) : "r"(crypto_uint16_x) : ); return crypto_uint16_y; #else crypto_uint16_x &= 1 + crypto_uint16_signed_optblocker; return crypto_uint16_x; #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_bitinrangepublicpos_mask(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrw %%cl,%0" : "+r"(crypto_uint16_x) : "c"(crypto_uint16_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,65535\n lsr %w0,%w0,%w1" : "+&r"(crypto_uint16_x) : "r"(crypto_uint16_s) : ); #else crypto_uint16_x >>= crypto_uint16_s ^ crypto_uint16_signed_optblocker; #endif return crypto_uint16_bottombit_mask(crypto_uint16_x); } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_bitinrangepublicpos_01(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrw %%cl,%0" : "+r"(crypto_uint16_x) : "c"(crypto_uint16_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,65535\n lsr %w0,%w0,%w1" : "+&r"(crypto_uint16_x) : "r"(crypto_uint16_s) : ); #else crypto_uint16_x >>= crypto_uint16_s ^ crypto_uint16_signed_optblocker; #endif return crypto_uint16_bottombit_01(crypto_uint16_x); } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_shlmod(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_s) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16_s &= 15; __asm__ ("shlw %%cl,%0" : "+r"(crypto_uint16_x) : "c"(crypto_uint16_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,15\n and %w1,%w1,65535\n lsl %w1,%w1,%w0" : "+&r"(crypto_uint16_s), "+r"(crypto_uint16_x) : : ); #else int crypto_uint16_k, crypto_uint16_l; for (crypto_uint16_l = 0,crypto_uint16_k = 1;crypto_uint16_k < 16;++crypto_uint16_l,crypto_uint16_k *= 2) crypto_uint16_x ^= (crypto_uint16_x ^ (crypto_uint16_x << crypto_uint16_k)) & crypto_uint16_bitinrangepublicpos_mask(crypto_uint16_s,crypto_uint16_l); #endif return crypto_uint16_x; } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_shrmod(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_s) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16_s &= 15; __asm__ ("shrw %%cl,%0" : "+r"(crypto_uint16_x) : "c"(crypto_uint16_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,15\n and %w1,%w1,65535\n lsr %w1,%w1,%w0" : "+&r"(crypto_uint16_s), "+r"(crypto_uint16_x) : : ); #else int crypto_uint16_k, crypto_uint16_l; for (crypto_uint16_l = 0,crypto_uint16_k = 1;crypto_uint16_k < 16;++crypto_uint16_l,crypto_uint16_k *= 2) crypto_uint16_x ^= (crypto_uint16_x ^ (crypto_uint16_x >> crypto_uint16_k)) & crypto_uint16_bitinrangepublicpos_mask(crypto_uint16_s,crypto_uint16_l); #endif return crypto_uint16_x; } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_bitmod_mask(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_s) { crypto_uint16_x = crypto_uint16_shrmod(crypto_uint16_x,crypto_uint16_s); return crypto_uint16_bottombit_mask(crypto_uint16_x); } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_bitmod_01(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_s) { crypto_uint16_x = crypto_uint16_shrmod(crypto_uint16_x,crypto_uint16_s); return crypto_uint16_bottombit_01(crypto_uint16_x); } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_nonzero_mask(crypto_uint16 crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n testw %2,%2\n cmovnew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("tst %w1,65535\n csetm %w0,ne" : "=r"(crypto_uint16_z) : "r"(crypto_uint16_x) : "cc"); return crypto_uint16_z; #else crypto_uint16_x |= -crypto_uint16_x; return crypto_uint16_signed_negative_mask(crypto_uint16_x); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_nonzero_01(crypto_uint16 crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n testw %2,%2\n cmovnew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("tst %w1,65535\n cset %w0,ne" : "=r"(crypto_uint16_z) : "r"(crypto_uint16_x) : "cc"); return crypto_uint16_z; #else crypto_uint16_x |= -crypto_uint16_x; return crypto_uint16_topbit_01(crypto_uint16_x); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_zero_mask(crypto_uint16 crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n testw %2,%2\n cmovew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("tst %w1,65535\n csetm %w0,eq" : "=r"(crypto_uint16_z) : "r"(crypto_uint16_x) : "cc"); return crypto_uint16_z; #else return ~crypto_uint16_nonzero_mask(crypto_uint16_x); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_zero_01(crypto_uint16 crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n testw %2,%2\n cmovew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("tst %w1,65535\n cset %w0,eq" : "=r"(crypto_uint16_z) : "r"(crypto_uint16_x) : "cc"); return crypto_uint16_z; #else return 1-crypto_uint16_nonzero_01(crypto_uint16_x); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_unequal_mask(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovnew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n csetm %w0,ne" : "=&r"(crypto_uint16_z) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #else return crypto_uint16_nonzero_mask(crypto_uint16_x ^ crypto_uint16_y); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_unequal_01(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovnew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n cset %w0,ne" : "=&r"(crypto_uint16_z) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #else return crypto_uint16_nonzero_01(crypto_uint16_x ^ crypto_uint16_y); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_equal_mask(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n csetm %w0,eq" : "=&r"(crypto_uint16_z) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #else return ~crypto_uint16_unequal_mask(crypto_uint16_x,crypto_uint16_y); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_equal_01(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n cset %w0,eq" : "=&r"(crypto_uint16_z) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #else return 1-crypto_uint16_unequal_01(crypto_uint16_x,crypto_uint16_y); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_min(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpw %1,%0\n cmovaw %1,%0" : "+r"(crypto_uint16_x) : "r"(crypto_uint16_y) : "cc"); return crypto_uint16_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,65535\n cmp %w0,%w1,uxth\n csel %w0,%w0,%w1,lo" : "+&r"(crypto_uint16_x) : "r"(crypto_uint16_y) : "cc"); return crypto_uint16_x; #else crypto_uint16 crypto_uint16_r = crypto_uint16_y ^ crypto_uint16_x; crypto_uint16 crypto_uint16_z = crypto_uint16_y - crypto_uint16_x; crypto_uint16_z ^= crypto_uint16_r & (crypto_uint16_z ^ crypto_uint16_y ^ (((crypto_uint16) 1) << (16-1))); crypto_uint16_z = crypto_uint16_signed_negative_mask(crypto_uint16_z); crypto_uint16_z &= crypto_uint16_r; return crypto_uint16_x ^ crypto_uint16_z; #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_max(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpw %1,%0\n cmovbw %1,%0" : "+r"(crypto_uint16_x) : "r"(crypto_uint16_y) : "cc"); return crypto_uint16_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,65535\n cmp %w0,%w1,uxth\n csel %w0,%w1,%w0,lo" : "+&r"(crypto_uint16_x) : "r"(crypto_uint16_y) : "cc"); return crypto_uint16_x; #else crypto_uint16 crypto_uint16_r = crypto_uint16_y ^ crypto_uint16_x; crypto_uint16 crypto_uint16_z = crypto_uint16_y - crypto_uint16_x; crypto_uint16_z ^= crypto_uint16_r & (crypto_uint16_z ^ crypto_uint16_y ^ (((crypto_uint16) 1) << (16-1))); crypto_uint16_z = crypto_uint16_signed_negative_mask(crypto_uint16_z); crypto_uint16_z &= crypto_uint16_r; return crypto_uint16_y ^ crypto_uint16_z; #endif } __attribute__((unused)) static inline void crypto_uint16_minmax(crypto_uint16 *crypto_uint16_p,crypto_uint16 *crypto_uint16_q) { crypto_uint16 crypto_uint16_x = *crypto_uint16_p; crypto_uint16 crypto_uint16_y = *crypto_uint16_q; #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_z; __asm__ ("cmpw %2,%1\n movw %1,%0\n cmovaw %2,%1\n cmovaw %0,%2" : "=&r"(crypto_uint16_z), "+&r"(crypto_uint16_x), "+r"(crypto_uint16_y) : : "cc"); *crypto_uint16_p = crypto_uint16_x; *crypto_uint16_q = crypto_uint16_y; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_r, crypto_uint16_s; __asm__ ("and %w0,%w0,65535\n cmp %w0,%w3,uxth\n csel %w1,%w0,%w3,lo\n csel %w2,%w3,%w0,lo" : "+&r"(crypto_uint16_x), "=&r"(crypto_uint16_r), "=r"(crypto_uint16_s) : "r"(crypto_uint16_y) : "cc"); *crypto_uint16_p = crypto_uint16_r; *crypto_uint16_q = crypto_uint16_s; #else crypto_uint16 crypto_uint16_r = crypto_uint16_y ^ crypto_uint16_x; crypto_uint16 crypto_uint16_z = crypto_uint16_y - crypto_uint16_x; crypto_uint16_z ^= crypto_uint16_r & (crypto_uint16_z ^ crypto_uint16_y ^ (((crypto_uint16) 1) << (16-1))); crypto_uint16_z = crypto_uint16_signed_negative_mask(crypto_uint16_z); crypto_uint16_z &= crypto_uint16_r; crypto_uint16_x ^= crypto_uint16_z; crypto_uint16_y ^= crypto_uint16_z; *crypto_uint16_p = crypto_uint16_x; *crypto_uint16_q = crypto_uint16_y; #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_smaller_mask(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovbw %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n csetm %w0,lo" : "=&r"(crypto_uint16_z) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #else crypto_uint16 crypto_uint16_r = crypto_uint16_x ^ crypto_uint16_y; crypto_uint16 crypto_uint16_z = crypto_uint16_x - crypto_uint16_y; crypto_uint16_z ^= crypto_uint16_r & (crypto_uint16_z ^ crypto_uint16_x ^ (((crypto_uint16) 1) << (16-1))); return crypto_uint16_signed_negative_mask(crypto_uint16_z); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_smaller_01(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovbw %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n cset %w0,lo" : "=&r"(crypto_uint16_z) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #else crypto_uint16 crypto_uint16_r = crypto_uint16_x ^ crypto_uint16_y; crypto_uint16 crypto_uint16_z = crypto_uint16_x - crypto_uint16_y; crypto_uint16_z ^= crypto_uint16_r & (crypto_uint16_z ^ crypto_uint16_x ^ (((crypto_uint16) 1) << (16-1))); return crypto_uint16_topbit_01(crypto_uint16_z); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_leq_mask(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $-1,%1\n cmpw %3,%2\n cmovbew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n csetm %w0,ls" : "=&r"(crypto_uint16_z) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #else return ~crypto_uint16_smaller_mask(crypto_uint16_y,crypto_uint16_x); #endif } __attribute__((unused)) static inline crypto_uint16 crypto_uint16_leq_01(crypto_uint16 crypto_uint16_x,crypto_uint16 crypto_uint16_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 crypto_uint16_q,crypto_uint16_z; __asm__ ("xorw %0,%0\n movw $1,%1\n cmpw %3,%2\n cmovbew %1,%0" : "=&r"(crypto_uint16_z), "=&r"(crypto_uint16_q) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint16 crypto_uint16_z; __asm__ ("and %w0,%w1,65535\n cmp %w0,%w2,uxth\n cset %w0,ls" : "=&r"(crypto_uint16_z) : "r"(crypto_uint16_x), "r"(crypto_uint16_y) : "cc"); return crypto_uint16_z; #else return 1-crypto_uint16_smaller_01(crypto_uint16_y,crypto_uint16_x); #endif } __attribute__((unused)) static inline int crypto_uint16_ones_num(crypto_uint16 crypto_uint16_x) { crypto_uint16 crypto_uint16_y = crypto_uint16_x; const crypto_uint16 C0 = 0x5555; const crypto_uint16 C1 = 0x3333; const crypto_uint16 C2 = 0x0f0f; crypto_uint16_y -= ((crypto_uint16_y >> 1) & C0); crypto_uint16_y = (crypto_uint16_y & C1) + ((crypto_uint16_y >> 2) & C1); crypto_uint16_y = (crypto_uint16_y + (crypto_uint16_y >> 4)) & C2; crypto_uint16_y = (crypto_uint16_y + (crypto_uint16_y >> 8)) & 0xff; return crypto_uint16_y; } __attribute__((unused)) static inline int crypto_uint16_bottomzeros_num(crypto_uint16 crypto_uint16_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint16 fallback = 16; __asm__ ("bsfw %0,%0\n cmovew %1,%0" : "+&r"(crypto_uint16_x) : "r"(fallback) : "cc"); return crypto_uint16_x; #elif defined(__GNUC__) && defined(__aarch64__) int64_t crypto_uint16_y; __asm__ ("orr %w0,%w1,-65536\n rbit %w0,%w0\n clz %w0,%w0" : "=r"(crypto_uint16_y) : "r"(crypto_uint16_x) : ); return crypto_uint16_y; #else crypto_uint16 crypto_uint16_y = crypto_uint16_x ^ (crypto_uint16_x-1); crypto_uint16_y = ((crypto_uint16_signed) crypto_uint16_y) >> 1; crypto_uint16_y &= ~(crypto_uint16_x & (((crypto_uint16) 1) << (16-1))); return crypto_uint16_ones_num(crypto_uint16_y); #endif } #endif dq-20250201/cryptoint/crypto_uint32.h000066400000000000000000000511341474734131400173170ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_uint32_h #define crypto_uint32_h #include #define crypto_uint32 uint32_t #define crypto_uint32_signed int32_t #define crypto_uint32_signed_optblocker dq_uint32_signed_optblocker extern volatile crypto_uint32_signed crypto_uint32_signed_optblocker; __attribute__((unused)) static inline crypto_uint32 crypto_uint32_load(const unsigned char *crypto_uint32_s) { crypto_uint32 crypto_uint32_z = 0; crypto_uint32_z |= ((crypto_uint32) (*crypto_uint32_s++)) << 0; crypto_uint32_z |= ((crypto_uint32) (*crypto_uint32_s++)) << 8; crypto_uint32_z |= ((crypto_uint32) (*crypto_uint32_s++)) << 16; crypto_uint32_z |= ((crypto_uint32) (*crypto_uint32_s++)) << 24; return crypto_uint32_z; } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_load_bigendian(const unsigned char *crypto_uint32_s) { crypto_uint32 crypto_uint32_z = 0; crypto_uint32_z |= ((crypto_uint32) (*crypto_uint32_s++)) << 24; crypto_uint32_z |= ((crypto_uint32) (*crypto_uint32_s++)) << 16; crypto_uint32_z |= ((crypto_uint32) (*crypto_uint32_s++)) << 8; crypto_uint32_z |= ((crypto_uint32) (*crypto_uint32_s++)) << 0; return crypto_uint32_z; } __attribute__((unused)) static inline void crypto_uint32_store(unsigned char *crypto_uint32_s,crypto_uint32 crypto_uint32_x) { *crypto_uint32_s++ = crypto_uint32_x >> 0; *crypto_uint32_s++ = crypto_uint32_x >> 8; *crypto_uint32_s++ = crypto_uint32_x >> 16; *crypto_uint32_s++ = crypto_uint32_x >> 24; } __attribute__((unused)) static inline void crypto_uint32_store_bigendian(unsigned char *crypto_uint32_s,crypto_uint32 crypto_uint32_x) { *crypto_uint32_s++ = crypto_uint32_x >> 24; *crypto_uint32_s++ = crypto_uint32_x >> 16; *crypto_uint32_s++ = crypto_uint32_x >> 8; *crypto_uint32_s++ = crypto_uint32_x >> 0; } __attribute__((unused)) static inline crypto_uint32_signed crypto_uint32_signed_negative_mask(crypto_uint32_signed crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarl $31,%0" : "+r"(crypto_uint32_x) : : "cc"); return crypto_uint32_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32_signed crypto_uint32_y; __asm__ ("asr %w0,%w1,31" : "=r"(crypto_uint32_y) : "r"(crypto_uint32_x) : ); return crypto_uint32_y; #else crypto_uint32_x >>= 32-6; crypto_uint32_x += crypto_uint32_signed_optblocker; crypto_uint32_x >>= 5; return crypto_uint32_x; #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_topbit_01(crypto_uint32 crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrl $31,%0" : "+r"(crypto_uint32_x) : : "cc"); return crypto_uint32_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32_signed crypto_uint32_y; __asm__ ("lsr %w0,%w1,31" : "=r"(crypto_uint32_y) : "r"(crypto_uint32_x) : ); return crypto_uint32_y; #else crypto_uint32_x >>= 32-6; crypto_uint32_x += crypto_uint32_signed_optblocker; crypto_uint32_x >>= 5; return crypto_uint32_x; #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_topbit_mask(crypto_uint32 crypto_uint32_x) { return crypto_uint32_signed_negative_mask(crypto_uint32_x); } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_bottombit_mask(crypto_uint32 crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andl $1,%0" : "+r"(crypto_uint32_x) : : "cc"); return -crypto_uint32_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_y; __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_uint32_y) : "r"(crypto_uint32_x) : ); return crypto_uint32_y; #else crypto_uint32_x &= 1 + crypto_uint32_signed_optblocker; return -crypto_uint32_x; #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_bottombit_01(crypto_uint32 crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andl $1,%0" : "+r"(crypto_uint32_x) : : "cc"); return crypto_uint32_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_y; __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_uint32_y) : "r"(crypto_uint32_x) : ); return crypto_uint32_y; #else crypto_uint32_x &= 1 + crypto_uint32_signed_optblocker; return crypto_uint32_x; #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_bitinrangepublicpos_mask(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrl %%cl,%0" : "+r"(crypto_uint32_x) : "c"(crypto_uint32_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsr %w0,%w0,%w1" : "+r"(crypto_uint32_x) : "r"(crypto_uint32_s) : ); #else crypto_uint32_x >>= crypto_uint32_s ^ crypto_uint32_signed_optblocker; #endif return crypto_uint32_bottombit_mask(crypto_uint32_x); } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_bitinrangepublicpos_01(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrl %%cl,%0" : "+r"(crypto_uint32_x) : "c"(crypto_uint32_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsr %w0,%w0,%w1" : "+r"(crypto_uint32_x) : "r"(crypto_uint32_s) : ); #else crypto_uint32_x >>= crypto_uint32_s ^ crypto_uint32_signed_optblocker; #endif return crypto_uint32_bottombit_01(crypto_uint32_x); } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_shlmod(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shll %%cl,%0" : "+r"(crypto_uint32_x) : "c"(crypto_uint32_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsl %w0,%w0,%w1" : "+r"(crypto_uint32_x) : "r"(crypto_uint32_s) : ); #else int crypto_uint32_k, crypto_uint32_l; for (crypto_uint32_l = 0,crypto_uint32_k = 1;crypto_uint32_k < 32;++crypto_uint32_l,crypto_uint32_k *= 2) crypto_uint32_x ^= (crypto_uint32_x ^ (crypto_uint32_x << crypto_uint32_k)) & crypto_uint32_bitinrangepublicpos_mask(crypto_uint32_s,crypto_uint32_l); #endif return crypto_uint32_x; } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_shrmod(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrl %%cl,%0" : "+r"(crypto_uint32_x) : "c"(crypto_uint32_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsr %w0,%w0,%w1" : "+r"(crypto_uint32_x) : "r"(crypto_uint32_s) : ); #else int crypto_uint32_k, crypto_uint32_l; for (crypto_uint32_l = 0,crypto_uint32_k = 1;crypto_uint32_k < 32;++crypto_uint32_l,crypto_uint32_k *= 2) crypto_uint32_x ^= (crypto_uint32_x ^ (crypto_uint32_x >> crypto_uint32_k)) & crypto_uint32_bitinrangepublicpos_mask(crypto_uint32_s,crypto_uint32_l); #endif return crypto_uint32_x; } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_bitmod_mask(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_s) { crypto_uint32_x = crypto_uint32_shrmod(crypto_uint32_x,crypto_uint32_s); return crypto_uint32_bottombit_mask(crypto_uint32_x); } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_bitmod_01(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_s) { crypto_uint32_x = crypto_uint32_shrmod(crypto_uint32_x,crypto_uint32_s); return crypto_uint32_bottombit_01(crypto_uint32_x); } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_nonzero_mask(crypto_uint32 crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n testl %2,%2\n cmovnel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,0\n csetm %w0,ne" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x) : "cc"); return crypto_uint32_z; #else crypto_uint32_x |= -crypto_uint32_x; return crypto_uint32_signed_negative_mask(crypto_uint32_x); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_nonzero_01(crypto_uint32 crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n testl %2,%2\n cmovnel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,0\n cset %w0,ne" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x) : "cc"); return crypto_uint32_z; #else crypto_uint32_x |= -crypto_uint32_x; return crypto_uint32_topbit_01(crypto_uint32_x); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_zero_mask(crypto_uint32 crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n testl %2,%2\n cmovel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,0\n csetm %w0,eq" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x) : "cc"); return crypto_uint32_z; #else return ~crypto_uint32_nonzero_mask(crypto_uint32_x); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_zero_01(crypto_uint32 crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n testl %2,%2\n cmovel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,0\n cset %w0,eq" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x) : "cc"); return crypto_uint32_z; #else return 1-crypto_uint32_nonzero_01(crypto_uint32_x); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_unequal_mask(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovnel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,%w2\n csetm %w0,ne" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #else return crypto_uint32_nonzero_mask(crypto_uint32_x ^ crypto_uint32_y); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_unequal_01(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovnel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,%w2\n cset %w0,ne" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #else return crypto_uint32_nonzero_01(crypto_uint32_x ^ crypto_uint32_y); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_equal_mask(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,%w2\n csetm %w0,eq" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #else return ~crypto_uint32_unequal_mask(crypto_uint32_x,crypto_uint32_y); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_equal_01(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,%w2\n cset %w0,eq" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #else return 1-crypto_uint32_unequal_01(crypto_uint32_x,crypto_uint32_y); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_min(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpl %1,%0\n cmoval %1,%0" : "+r"(crypto_uint32_x) : "r"(crypto_uint32_y) : "cc"); return crypto_uint32_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("cmp %w0,%w1\n csel %w0,%w0,%w1,lo" : "+r"(crypto_uint32_x) : "r"(crypto_uint32_y) : "cc"); return crypto_uint32_x; #else crypto_uint32 crypto_uint32_r = crypto_uint32_y ^ crypto_uint32_x; crypto_uint32 crypto_uint32_z = crypto_uint32_y - crypto_uint32_x; crypto_uint32_z ^= crypto_uint32_r & (crypto_uint32_z ^ crypto_uint32_y ^ (((crypto_uint32) 1) << (32-1))); crypto_uint32_z = crypto_uint32_signed_negative_mask(crypto_uint32_z); crypto_uint32_z &= crypto_uint32_r; return crypto_uint32_x ^ crypto_uint32_z; #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_max(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpl %1,%0\n cmovbl %1,%0" : "+r"(crypto_uint32_x) : "r"(crypto_uint32_y) : "cc"); return crypto_uint32_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("cmp %w0,%w1\n csel %w0,%w1,%w0,lo" : "+r"(crypto_uint32_x) : "r"(crypto_uint32_y) : "cc"); return crypto_uint32_x; #else crypto_uint32 crypto_uint32_r = crypto_uint32_y ^ crypto_uint32_x; crypto_uint32 crypto_uint32_z = crypto_uint32_y - crypto_uint32_x; crypto_uint32_z ^= crypto_uint32_r & (crypto_uint32_z ^ crypto_uint32_y ^ (((crypto_uint32) 1) << (32-1))); crypto_uint32_z = crypto_uint32_signed_negative_mask(crypto_uint32_z); crypto_uint32_z &= crypto_uint32_r; return crypto_uint32_y ^ crypto_uint32_z; #endif } __attribute__((unused)) static inline void crypto_uint32_minmax(crypto_uint32 *crypto_uint32_p,crypto_uint32 *crypto_uint32_q) { crypto_uint32 crypto_uint32_x = *crypto_uint32_p; crypto_uint32 crypto_uint32_y = *crypto_uint32_q; #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmpl %2,%1\n movl %1,%0\n cmoval %2,%1\n cmoval %0,%2" : "=&r"(crypto_uint32_z), "+&r"(crypto_uint32_x), "+r"(crypto_uint32_y) : : "cc"); *crypto_uint32_p = crypto_uint32_x; *crypto_uint32_q = crypto_uint32_y; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_r, crypto_uint32_s; __asm__ ("cmp %w2,%w3\n csel %w0,%w2,%w3,lo\n csel %w1,%w3,%w2,lo" : "=&r"(crypto_uint32_r), "=r"(crypto_uint32_s) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); *crypto_uint32_p = crypto_uint32_r; *crypto_uint32_q = crypto_uint32_s; #else crypto_uint32 crypto_uint32_r = crypto_uint32_y ^ crypto_uint32_x; crypto_uint32 crypto_uint32_z = crypto_uint32_y - crypto_uint32_x; crypto_uint32_z ^= crypto_uint32_r & (crypto_uint32_z ^ crypto_uint32_y ^ (((crypto_uint32) 1) << (32-1))); crypto_uint32_z = crypto_uint32_signed_negative_mask(crypto_uint32_z); crypto_uint32_z &= crypto_uint32_r; crypto_uint32_x ^= crypto_uint32_z; crypto_uint32_y ^= crypto_uint32_z; *crypto_uint32_p = crypto_uint32_x; *crypto_uint32_q = crypto_uint32_y; #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_smaller_mask(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovbl %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,%w2\n csetm %w0,lo" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #else crypto_uint32 crypto_uint32_r = crypto_uint32_x ^ crypto_uint32_y; crypto_uint32 crypto_uint32_z = crypto_uint32_x - crypto_uint32_y; crypto_uint32_z ^= crypto_uint32_r & (crypto_uint32_z ^ crypto_uint32_x ^ (((crypto_uint32) 1) << (32-1))); return crypto_uint32_signed_negative_mask(crypto_uint32_z); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_smaller_01(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovbl %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,%w2\n cset %w0,lo" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #else crypto_uint32 crypto_uint32_r = crypto_uint32_x ^ crypto_uint32_y; crypto_uint32 crypto_uint32_z = crypto_uint32_x - crypto_uint32_y; crypto_uint32_z ^= crypto_uint32_r & (crypto_uint32_z ^ crypto_uint32_x ^ (((crypto_uint32) 1) << (32-1))); return crypto_uint32_topbit_01(crypto_uint32_z); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_leq_mask(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpl %3,%2\n cmovbel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,%w2\n csetm %w0,ls" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #else return ~crypto_uint32_smaller_mask(crypto_uint32_y,crypto_uint32_x); #endif } __attribute__((unused)) static inline crypto_uint32 crypto_uint32_leq_01(crypto_uint32 crypto_uint32_x,crypto_uint32 crypto_uint32_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 crypto_uint32_q,crypto_uint32_z; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpl %3,%2\n cmovbel %1,%0" : "=&r"(crypto_uint32_z), "=&r"(crypto_uint32_q) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint32 crypto_uint32_z; __asm__ ("cmp %w1,%w2\n cset %w0,ls" : "=r"(crypto_uint32_z) : "r"(crypto_uint32_x), "r"(crypto_uint32_y) : "cc"); return crypto_uint32_z; #else return 1-crypto_uint32_smaller_01(crypto_uint32_y,crypto_uint32_x); #endif } __attribute__((unused)) static inline int crypto_uint32_ones_num(crypto_uint32 crypto_uint32_x) { crypto_uint32 crypto_uint32_y = crypto_uint32_x; const crypto_uint32 C0 = 0x55555555; const crypto_uint32 C1 = 0x33333333; const crypto_uint32 C2 = 0x0f0f0f0f; crypto_uint32_y -= ((crypto_uint32_y >> 1) & C0); crypto_uint32_y = (crypto_uint32_y & C1) + ((crypto_uint32_y >> 2) & C1); crypto_uint32_y = (crypto_uint32_y + (crypto_uint32_y >> 4)) & C2; crypto_uint32_y += crypto_uint32_y >> 8; crypto_uint32_y = (crypto_uint32_y + (crypto_uint32_y >> 16)) & 0xff; return crypto_uint32_y; } __attribute__((unused)) static inline int crypto_uint32_bottomzeros_num(crypto_uint32 crypto_uint32_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint32 fallback = 32; __asm__ ("bsfl %0,%0\n cmovel %1,%0" : "+&r"(crypto_uint32_x) : "r"(fallback) : "cc"); return crypto_uint32_x; #elif defined(__GNUC__) && defined(__aarch64__) int64_t crypto_uint32_y; __asm__ ("rbit %w0,%w1\n clz %w0,%w0" : "=r"(crypto_uint32_y) : "r"(crypto_uint32_x) : ); return crypto_uint32_y; #else crypto_uint32 crypto_uint32_y = crypto_uint32_x ^ (crypto_uint32_x-1); crypto_uint32_y = ((crypto_uint32_signed) crypto_uint32_y) >> 1; crypto_uint32_y &= ~(crypto_uint32_x & (((crypto_uint32) 1) << (32-1))); return crypto_uint32_ones_num(crypto_uint32_y); #endif } #endif dq-20250201/cryptoint/crypto_uint64.h000066400000000000000000000527361474734131400173350ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_uint64_h #define crypto_uint64_h #include #define crypto_uint64 uint64_t #define crypto_uint64_signed int64_t #define crypto_uint64_signed_optblocker dq_uint64_signed_optblocker extern volatile crypto_uint64_signed crypto_uint64_signed_optblocker; __attribute__((unused)) static inline crypto_uint64 crypto_uint64_load(const unsigned char *crypto_uint64_s) { crypto_uint64 crypto_uint64_z = 0; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 0; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 8; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 16; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 24; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 32; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 40; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 48; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 56; return crypto_uint64_z; } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_load_bigendian(const unsigned char *crypto_uint64_s) { crypto_uint64 crypto_uint64_z = 0; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 56; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 48; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 40; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 32; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 24; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 16; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 8; crypto_uint64_z |= ((crypto_uint64) (*crypto_uint64_s++)) << 0; return crypto_uint64_z; } __attribute__((unused)) static inline void crypto_uint64_store(unsigned char *crypto_uint64_s,crypto_uint64 crypto_uint64_x) { *crypto_uint64_s++ = crypto_uint64_x >> 0; *crypto_uint64_s++ = crypto_uint64_x >> 8; *crypto_uint64_s++ = crypto_uint64_x >> 16; *crypto_uint64_s++ = crypto_uint64_x >> 24; *crypto_uint64_s++ = crypto_uint64_x >> 32; *crypto_uint64_s++ = crypto_uint64_x >> 40; *crypto_uint64_s++ = crypto_uint64_x >> 48; *crypto_uint64_s++ = crypto_uint64_x >> 56; } __attribute__((unused)) static inline void crypto_uint64_store_bigendian(unsigned char *crypto_uint64_s,crypto_uint64 crypto_uint64_x) { *crypto_uint64_s++ = crypto_uint64_x >> 56; *crypto_uint64_s++ = crypto_uint64_x >> 48; *crypto_uint64_s++ = crypto_uint64_x >> 40; *crypto_uint64_s++ = crypto_uint64_x >> 32; *crypto_uint64_s++ = crypto_uint64_x >> 24; *crypto_uint64_s++ = crypto_uint64_x >> 16; *crypto_uint64_s++ = crypto_uint64_x >> 8; *crypto_uint64_s++ = crypto_uint64_x >> 0; } __attribute__((unused)) static inline crypto_uint64_signed crypto_uint64_signed_negative_mask(crypto_uint64_signed crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarq $63,%0" : "+r"(crypto_uint64_x) : : "cc"); return crypto_uint64_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64_signed crypto_uint64_y; __asm__ ("asr %0,%1,63" : "=r"(crypto_uint64_y) : "r"(crypto_uint64_x) : ); return crypto_uint64_y; #else crypto_uint64_x >>= 64-6; crypto_uint64_x += crypto_uint64_signed_optblocker; crypto_uint64_x >>= 5; return crypto_uint64_x; #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_topbit_01(crypto_uint64 crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrq $63,%0" : "+r"(crypto_uint64_x) : : "cc"); return crypto_uint64_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64_signed crypto_uint64_y; __asm__ ("lsr %0,%1,63" : "=r"(crypto_uint64_y) : "r"(crypto_uint64_x) : ); return crypto_uint64_y; #else crypto_uint64_x >>= 64-6; crypto_uint64_x += crypto_uint64_signed_optblocker; crypto_uint64_x >>= 5; return crypto_uint64_x; #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_topbit_mask(crypto_uint64 crypto_uint64_x) { return crypto_uint64_signed_negative_mask(crypto_uint64_x); } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_bottombit_mask(crypto_uint64 crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andq $1,%0" : "+r"(crypto_uint64_x) : : "cc"); return -crypto_uint64_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_y; __asm__ ("sbfx %0,%1,0,1" : "=r"(crypto_uint64_y) : "r"(crypto_uint64_x) : ); return crypto_uint64_y; #else crypto_uint64_x &= 1 + crypto_uint64_signed_optblocker; return -crypto_uint64_x; #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_bottombit_01(crypto_uint64 crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andq $1,%0" : "+r"(crypto_uint64_x) : : "cc"); return crypto_uint64_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_y; __asm__ ("ubfx %0,%1,0,1" : "=r"(crypto_uint64_y) : "r"(crypto_uint64_x) : ); return crypto_uint64_y; #else crypto_uint64_x &= 1 + crypto_uint64_signed_optblocker; return crypto_uint64_x; #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_bitinrangepublicpos_mask(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrq %%cl,%0" : "+r"(crypto_uint64_x) : "c"(crypto_uint64_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsr %0,%0,%1" : "+r"(crypto_uint64_x) : "r"(crypto_uint64_s) : ); #else crypto_uint64_x >>= crypto_uint64_s ^ crypto_uint64_signed_optblocker; #endif return crypto_uint64_bottombit_mask(crypto_uint64_x); } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_bitinrangepublicpos_01(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrq %%cl,%0" : "+r"(crypto_uint64_x) : "c"(crypto_uint64_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsr %0,%0,%1" : "+r"(crypto_uint64_x) : "r"(crypto_uint64_s) : ); #else crypto_uint64_x >>= crypto_uint64_s ^ crypto_uint64_signed_optblocker; #endif return crypto_uint64_bottombit_01(crypto_uint64_x); } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_shlmod(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shlq %%cl,%0" : "+r"(crypto_uint64_x) : "c"(crypto_uint64_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsl %0,%0,%1" : "+r"(crypto_uint64_x) : "r"(crypto_uint64_s) : ); #else int crypto_uint64_k, crypto_uint64_l; for (crypto_uint64_l = 0,crypto_uint64_k = 1;crypto_uint64_k < 64;++crypto_uint64_l,crypto_uint64_k *= 2) crypto_uint64_x ^= (crypto_uint64_x ^ (crypto_uint64_x << crypto_uint64_k)) & crypto_uint64_bitinrangepublicpos_mask(crypto_uint64_s,crypto_uint64_l); #endif return crypto_uint64_x; } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_shrmod(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrq %%cl,%0" : "+r"(crypto_uint64_x) : "c"(crypto_uint64_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("lsr %0,%0,%1" : "+r"(crypto_uint64_x) : "r"(crypto_uint64_s) : ); #else int crypto_uint64_k, crypto_uint64_l; for (crypto_uint64_l = 0,crypto_uint64_k = 1;crypto_uint64_k < 64;++crypto_uint64_l,crypto_uint64_k *= 2) crypto_uint64_x ^= (crypto_uint64_x ^ (crypto_uint64_x >> crypto_uint64_k)) & crypto_uint64_bitinrangepublicpos_mask(crypto_uint64_s,crypto_uint64_l); #endif return crypto_uint64_x; } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_bitmod_mask(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_s) { crypto_uint64_x = crypto_uint64_shrmod(crypto_uint64_x,crypto_uint64_s); return crypto_uint64_bottombit_mask(crypto_uint64_x); } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_bitmod_01(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_s) { crypto_uint64_x = crypto_uint64_shrmod(crypto_uint64_x,crypto_uint64_s); return crypto_uint64_bottombit_01(crypto_uint64_x); } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_nonzero_mask(crypto_uint64 crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n testq %2,%2\n cmovneq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,0\n csetm %0,ne" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x) : "cc"); return crypto_uint64_z; #else crypto_uint64_x |= -crypto_uint64_x; return crypto_uint64_signed_negative_mask(crypto_uint64_x); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_nonzero_01(crypto_uint64 crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n testq %2,%2\n cmovneq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,0\n cset %0,ne" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x) : "cc"); return crypto_uint64_z; #else crypto_uint64_x |= -crypto_uint64_x; return crypto_uint64_topbit_01(crypto_uint64_x); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_zero_mask(crypto_uint64 crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n testq %2,%2\n cmoveq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,0\n csetm %0,eq" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x) : "cc"); return crypto_uint64_z; #else return ~crypto_uint64_nonzero_mask(crypto_uint64_x); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_zero_01(crypto_uint64 crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n testq %2,%2\n cmoveq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,0\n cset %0,eq" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x) : "cc"); return crypto_uint64_z; #else return 1-crypto_uint64_nonzero_01(crypto_uint64_x); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_unequal_mask(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovneq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,%2\n csetm %0,ne" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #else return crypto_uint64_nonzero_mask(crypto_uint64_x ^ crypto_uint64_y); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_unequal_01(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovneq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,%2\n cset %0,ne" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #else return crypto_uint64_nonzero_01(crypto_uint64_x ^ crypto_uint64_y); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_equal_mask(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmoveq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,%2\n csetm %0,eq" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #else return ~crypto_uint64_unequal_mask(crypto_uint64_x,crypto_uint64_y); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_equal_01(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmoveq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,%2\n cset %0,eq" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #else return 1-crypto_uint64_unequal_01(crypto_uint64_x,crypto_uint64_y); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_min(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpq %1,%0\n cmovaq %1,%0" : "+r"(crypto_uint64_x) : "r"(crypto_uint64_y) : "cc"); return crypto_uint64_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("cmp %0,%1\n csel %0,%0,%1,lo" : "+r"(crypto_uint64_x) : "r"(crypto_uint64_y) : "cc"); return crypto_uint64_x; #else crypto_uint64 crypto_uint64_r = crypto_uint64_y ^ crypto_uint64_x; crypto_uint64 crypto_uint64_z = crypto_uint64_y - crypto_uint64_x; crypto_uint64_z ^= crypto_uint64_r & (crypto_uint64_z ^ crypto_uint64_y ^ (((crypto_uint64) 1) << (64-1))); crypto_uint64_z = crypto_uint64_signed_negative_mask(crypto_uint64_z); crypto_uint64_z &= crypto_uint64_r; return crypto_uint64_x ^ crypto_uint64_z; #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_max(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("cmpq %1,%0\n cmovbq %1,%0" : "+r"(crypto_uint64_x) : "r"(crypto_uint64_y) : "cc"); return crypto_uint64_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("cmp %0,%1\n csel %0,%1,%0,lo" : "+r"(crypto_uint64_x) : "r"(crypto_uint64_y) : "cc"); return crypto_uint64_x; #else crypto_uint64 crypto_uint64_r = crypto_uint64_y ^ crypto_uint64_x; crypto_uint64 crypto_uint64_z = crypto_uint64_y - crypto_uint64_x; crypto_uint64_z ^= crypto_uint64_r & (crypto_uint64_z ^ crypto_uint64_y ^ (((crypto_uint64) 1) << (64-1))); crypto_uint64_z = crypto_uint64_signed_negative_mask(crypto_uint64_z); crypto_uint64_z &= crypto_uint64_r; return crypto_uint64_y ^ crypto_uint64_z; #endif } __attribute__((unused)) static inline void crypto_uint64_minmax(crypto_uint64 *crypto_uint64_p,crypto_uint64 *crypto_uint64_q) { crypto_uint64 crypto_uint64_x = *crypto_uint64_p; crypto_uint64 crypto_uint64_y = *crypto_uint64_q; #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmpq %2,%1\n movq %1,%0\n cmovaq %2,%1\n cmovaq %0,%2" : "=&r"(crypto_uint64_z), "+&r"(crypto_uint64_x), "+r"(crypto_uint64_y) : : "cc"); *crypto_uint64_p = crypto_uint64_x; *crypto_uint64_q = crypto_uint64_y; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_r, crypto_uint64_s; __asm__ ("cmp %2,%3\n csel %0,%2,%3,lo\n csel %1,%3,%2,lo" : "=&r"(crypto_uint64_r), "=r"(crypto_uint64_s) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); *crypto_uint64_p = crypto_uint64_r; *crypto_uint64_q = crypto_uint64_s; #else crypto_uint64 crypto_uint64_r = crypto_uint64_y ^ crypto_uint64_x; crypto_uint64 crypto_uint64_z = crypto_uint64_y - crypto_uint64_x; crypto_uint64_z ^= crypto_uint64_r & (crypto_uint64_z ^ crypto_uint64_y ^ (((crypto_uint64) 1) << (64-1))); crypto_uint64_z = crypto_uint64_signed_negative_mask(crypto_uint64_z); crypto_uint64_z &= crypto_uint64_r; crypto_uint64_x ^= crypto_uint64_z; crypto_uint64_y ^= crypto_uint64_z; *crypto_uint64_p = crypto_uint64_x; *crypto_uint64_q = crypto_uint64_y; #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_smaller_mask(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovbq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,%2\n csetm %0,lo" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #else crypto_uint64 crypto_uint64_r = crypto_uint64_x ^ crypto_uint64_y; crypto_uint64 crypto_uint64_z = crypto_uint64_x - crypto_uint64_y; crypto_uint64_z ^= crypto_uint64_r & (crypto_uint64_z ^ crypto_uint64_x ^ (((crypto_uint64) 1) << (64-1))); return crypto_uint64_signed_negative_mask(crypto_uint64_z); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_smaller_01(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovbq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,%2\n cset %0,lo" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #else crypto_uint64 crypto_uint64_r = crypto_uint64_x ^ crypto_uint64_y; crypto_uint64 crypto_uint64_z = crypto_uint64_x - crypto_uint64_y; crypto_uint64_z ^= crypto_uint64_r & (crypto_uint64_z ^ crypto_uint64_x ^ (((crypto_uint64) 1) << (64-1))); return crypto_uint64_topbit_01(crypto_uint64_z); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_leq_mask(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $-1,%1\n cmpq %3,%2\n cmovbeq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,%2\n csetm %0,ls" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #else return ~crypto_uint64_smaller_mask(crypto_uint64_y,crypto_uint64_x); #endif } __attribute__((unused)) static inline crypto_uint64 crypto_uint64_leq_01(crypto_uint64 crypto_uint64_x,crypto_uint64 crypto_uint64_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 crypto_uint64_q,crypto_uint64_z; __asm__ ("xorq %0,%0\n movq $1,%1\n cmpq %3,%2\n cmovbeq %1,%0" : "=&r"(crypto_uint64_z), "=&r"(crypto_uint64_q) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint64 crypto_uint64_z; __asm__ ("cmp %1,%2\n cset %0,ls" : "=r"(crypto_uint64_z) : "r"(crypto_uint64_x), "r"(crypto_uint64_y) : "cc"); return crypto_uint64_z; #else return 1-crypto_uint64_smaller_01(crypto_uint64_y,crypto_uint64_x); #endif } __attribute__((unused)) static inline int crypto_uint64_ones_num(crypto_uint64 crypto_uint64_x) { crypto_uint64 crypto_uint64_y = crypto_uint64_x; const crypto_uint64 C0 = 0x5555555555555555; const crypto_uint64 C1 = 0x3333333333333333; const crypto_uint64 C2 = 0x0f0f0f0f0f0f0f0f; crypto_uint64_y -= ((crypto_uint64_y >> 1) & C0); crypto_uint64_y = (crypto_uint64_y & C1) + ((crypto_uint64_y >> 2) & C1); crypto_uint64_y = (crypto_uint64_y + (crypto_uint64_y >> 4)) & C2; crypto_uint64_y += crypto_uint64_y >> 8; crypto_uint64_y += crypto_uint64_y >> 16; crypto_uint64_y = (crypto_uint64_y + (crypto_uint64_y >> 32)) & 0xff; return crypto_uint64_y; } __attribute__((unused)) static inline int crypto_uint64_bottomzeros_num(crypto_uint64 crypto_uint64_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint64 fallback = 64; __asm__ ("bsfq %0,%0\n cmoveq %1,%0" : "+&r"(crypto_uint64_x) : "r"(fallback) : "cc"); return crypto_uint64_x; #elif defined(__GNUC__) && defined(__aarch64__) int64_t crypto_uint64_y; __asm__ ("rbit %0,%1\n clz %0,%0" : "=r"(crypto_uint64_y) : "r"(crypto_uint64_x) : ); return crypto_uint64_y; #else crypto_uint64 crypto_uint64_y = crypto_uint64_x ^ (crypto_uint64_x-1); crypto_uint64_y = ((crypto_uint64_signed) crypto_uint64_y) >> 1; crypto_uint64_y &= ~(crypto_uint64_x & (((crypto_uint64) 1) << (64-1))); return crypto_uint64_ones_num(crypto_uint64_y); #endif } #endif dq-20250201/cryptoint/crypto_uint8.h000066400000000000000000000531621474734131400172450ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_uint8_h #define crypto_uint8_h #include #define crypto_uint8 uint8_t #define crypto_uint8_signed int8_t #define crypto_uint8_signed_optblocker dq_uint8_signed_optblocker extern volatile crypto_uint8_signed crypto_uint8_signed_optblocker; __attribute__((unused)) static inline crypto_uint8 crypto_uint8_load(const unsigned char *crypto_uint8_s) { crypto_uint8 crypto_uint8_z = 0; crypto_uint8_z |= ((crypto_uint8) (*crypto_uint8_s++)) << 0; return crypto_uint8_z; } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_load_bigendian(const unsigned char *crypto_uint8_s) { crypto_uint8 crypto_uint8_z = 0; crypto_uint8_z |= ((crypto_uint8) (*crypto_uint8_s++)) << 0; return crypto_uint8_z; } __attribute__((unused)) static inline void crypto_uint8_store(unsigned char *crypto_uint8_s,crypto_uint8 crypto_uint8_x) { *crypto_uint8_s++ = crypto_uint8_x >> 0; } __attribute__((unused)) static inline void crypto_uint8_store_bigendian(unsigned char *crypto_uint8_s,crypto_uint8 crypto_uint8_x) { *crypto_uint8_s++ = crypto_uint8_x >> 0; } __attribute__((unused)) static inline crypto_uint8_signed crypto_uint8_signed_negative_mask(crypto_uint8_signed crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("sarb $7,%0" : "+r"(crypto_uint8_x) : : "cc"); return crypto_uint8_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8_signed crypto_uint8_y; __asm__ ("sbfx %w0,%w1,7,1" : "=r"(crypto_uint8_y) : "r"(crypto_uint8_x) : ); return crypto_uint8_y; #else crypto_uint8_x >>= 8-6; crypto_uint8_x += crypto_uint8_signed_optblocker; crypto_uint8_x >>= 5; return crypto_uint8_x; #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_topbit_01(crypto_uint8 crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrb $7,%0" : "+r"(crypto_uint8_x) : : "cc"); return crypto_uint8_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8_signed crypto_uint8_y; __asm__ ("ubfx %w0,%w1,7,1" : "=r"(crypto_uint8_y) : "r"(crypto_uint8_x) : ); return crypto_uint8_y; #else crypto_uint8_x >>= 8-6; crypto_uint8_x += crypto_uint8_signed_optblocker; crypto_uint8_x >>= 5; return crypto_uint8_x; #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_topbit_mask(crypto_uint8 crypto_uint8_x) { return crypto_uint8_signed_negative_mask(crypto_uint8_x); } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_bottombit_mask(crypto_uint8 crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andb $1,%0" : "+r"(crypto_uint8_x) : : "cc"); return -crypto_uint8_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_y; __asm__ ("sbfx %w0,%w1,0,1" : "=r"(crypto_uint8_y) : "r"(crypto_uint8_x) : ); return crypto_uint8_y; #else crypto_uint8_x &= 1 + crypto_uint8_signed_optblocker; return -crypto_uint8_x; #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_bottombit_01(crypto_uint8 crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("andb $1,%0" : "+r"(crypto_uint8_x) : : "cc"); return crypto_uint8_x; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_y; __asm__ ("ubfx %w0,%w1,0,1" : "=r"(crypto_uint8_y) : "r"(crypto_uint8_x) : ); return crypto_uint8_y; #else crypto_uint8_x &= 1 + crypto_uint8_signed_optblocker; return crypto_uint8_x; #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_bitinrangepublicpos_mask(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrb %%cl,%0" : "+r"(crypto_uint8_x) : "c"(crypto_uint8_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,255\n lsr %w0,%w0,%w1" : "+&r"(crypto_uint8_x) : "r"(crypto_uint8_s) : ); #else crypto_uint8_x >>= crypto_uint8_s ^ crypto_uint8_signed_optblocker; #endif return crypto_uint8_bottombit_mask(crypto_uint8_x); } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_bitinrangepublicpos_01(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_s) { #if defined(__GNUC__) && defined(__x86_64__) __asm__ ("shrb %%cl,%0" : "+r"(crypto_uint8_x) : "c"(crypto_uint8_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,255\n lsr %w0,%w0,%w1" : "+&r"(crypto_uint8_x) : "r"(crypto_uint8_s) : ); #else crypto_uint8_x >>= crypto_uint8_s ^ crypto_uint8_signed_optblocker; #endif return crypto_uint8_bottombit_01(crypto_uint8_x); } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_shlmod(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_s) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8_s &= 7; __asm__ ("shlb %%cl,%0" : "+r"(crypto_uint8_x) : "c"(crypto_uint8_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,7\n and %w1,%w1,255\n lsl %w1,%w1,%w0" : "+&r"(crypto_uint8_s), "+r"(crypto_uint8_x) : : ); #else int crypto_uint8_k, crypto_uint8_l; for (crypto_uint8_l = 0,crypto_uint8_k = 1;crypto_uint8_k < 8;++crypto_uint8_l,crypto_uint8_k *= 2) crypto_uint8_x ^= (crypto_uint8_x ^ (crypto_uint8_x << crypto_uint8_k)) & crypto_uint8_bitinrangepublicpos_mask(crypto_uint8_s,crypto_uint8_l); #endif return crypto_uint8_x; } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_shrmod(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_s) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8_s &= 7; __asm__ ("shrb %%cl,%0" : "+r"(crypto_uint8_x) : "c"(crypto_uint8_s) : "cc"); #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,7\n and %w1,%w1,255\n lsr %w1,%w1,%w0" : "+&r"(crypto_uint8_s), "+r"(crypto_uint8_x) : : ); #else int crypto_uint8_k, crypto_uint8_l; for (crypto_uint8_l = 0,crypto_uint8_k = 1;crypto_uint8_k < 8;++crypto_uint8_l,crypto_uint8_k *= 2) crypto_uint8_x ^= (crypto_uint8_x ^ (crypto_uint8_x >> crypto_uint8_k)) & crypto_uint8_bitinrangepublicpos_mask(crypto_uint8_s,crypto_uint8_l); #endif return crypto_uint8_x; } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_bitmod_mask(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_s) { crypto_uint8_x = crypto_uint8_shrmod(crypto_uint8_x,crypto_uint8_s); return crypto_uint8_bottombit_mask(crypto_uint8_x); } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_bitmod_01(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_s) { crypto_uint8_x = crypto_uint8_shrmod(crypto_uint8_x,crypto_uint8_s); return crypto_uint8_bottombit_01(crypto_uint8_x); } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_nonzero_mask(crypto_uint8 crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n testb %b2,%b2\n cmovnel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("tst %w1,255\n csetm %w0,ne" : "=r"(crypto_uint8_z) : "r"(crypto_uint8_x) : "cc"); return crypto_uint8_z; #else crypto_uint8_x |= -crypto_uint8_x; return crypto_uint8_signed_negative_mask(crypto_uint8_x); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_nonzero_01(crypto_uint8 crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n testb %b2,%b2\n cmovnel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("tst %w1,255\n cset %w0,ne" : "=r"(crypto_uint8_z) : "r"(crypto_uint8_x) : "cc"); return crypto_uint8_z; #else crypto_uint8_x |= -crypto_uint8_x; return crypto_uint8_topbit_01(crypto_uint8_x); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_zero_mask(crypto_uint8 crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n testb %b2,%b2\n cmovel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("tst %w1,255\n csetm %w0,eq" : "=r"(crypto_uint8_z) : "r"(crypto_uint8_x) : "cc"); return crypto_uint8_z; #else return ~crypto_uint8_nonzero_mask(crypto_uint8_x); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_zero_01(crypto_uint8 crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n testb %b2,%b2\n cmovel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("tst %w1,255\n cset %w0,eq" : "=r"(crypto_uint8_z) : "r"(crypto_uint8_x) : "cc"); return crypto_uint8_z; #else return 1-crypto_uint8_nonzero_01(crypto_uint8_x); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_unequal_mask(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpb %b3,%b2\n cmovnel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32), "q"(crypto_uint8_y32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n csetm %w0,ne" : "=&r"(crypto_uint8_z) : "r"(crypto_uint8_x), "r"(crypto_uint8_y) : "cc"); return crypto_uint8_z; #else return crypto_uint8_nonzero_mask(crypto_uint8_x ^ crypto_uint8_y); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_unequal_01(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpb %b3,%b2\n cmovnel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32), "q"(crypto_uint8_y32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n cset %w0,ne" : "=&r"(crypto_uint8_z) : "r"(crypto_uint8_x), "r"(crypto_uint8_y) : "cc"); return crypto_uint8_z; #else return crypto_uint8_nonzero_01(crypto_uint8_x ^ crypto_uint8_y); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_equal_mask(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpb %b3,%b2\n cmovel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32), "q"(crypto_uint8_y32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n csetm %w0,eq" : "=&r"(crypto_uint8_z) : "r"(crypto_uint8_x), "r"(crypto_uint8_y) : "cc"); return crypto_uint8_z; #else return ~crypto_uint8_unequal_mask(crypto_uint8_x,crypto_uint8_y); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_equal_01(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpb %b3,%b2\n cmovel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32), "q"(crypto_uint8_y32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n cset %w0,eq" : "=&r"(crypto_uint8_z) : "r"(crypto_uint8_x), "r"(crypto_uint8_y) : "cc"); return crypto_uint8_z; #else return 1-crypto_uint8_unequal_01(crypto_uint8_x,crypto_uint8_y); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_min(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) uint32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y; __asm__ ("cmpb %b1,%b0\n cmoval %1,%0" : "+q"(crypto_uint8_x32) : "q"(crypto_uint8_y32) : "cc"); crypto_uint8_x = crypto_uint8_x32; return crypto_uint8_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,255\n cmp %w0,%w1,uxtb\n csel %w0,%w0,%w1,lo" : "+&r"(crypto_uint8_x) : "r"(crypto_uint8_y) : "cc"); return crypto_uint8_x; #else crypto_uint8 crypto_uint8_r = crypto_uint8_y ^ crypto_uint8_x; crypto_uint8 crypto_uint8_z = crypto_uint8_y - crypto_uint8_x; crypto_uint8_z ^= crypto_uint8_r & (crypto_uint8_z ^ crypto_uint8_y ^ (((crypto_uint8) 1) << (8-1))); crypto_uint8_z = crypto_uint8_signed_negative_mask(crypto_uint8_z); crypto_uint8_z &= crypto_uint8_r; return crypto_uint8_x ^ crypto_uint8_z; #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_max(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) uint32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y; __asm__ ("cmpb %b1,%b0\n cmovbl %1,%0" : "+q"(crypto_uint8_x32) : "q"(crypto_uint8_y32) : "cc"); crypto_uint8_x = crypto_uint8_x32; return crypto_uint8_x; #elif defined(__GNUC__) && defined(__aarch64__) __asm__ ("and %w0,%w0,255\n cmp %w0,%w1,uxtb\n csel %w0,%w1,%w0,lo" : "+&r"(crypto_uint8_x) : "r"(crypto_uint8_y) : "cc"); return crypto_uint8_x; #else crypto_uint8 crypto_uint8_r = crypto_uint8_y ^ crypto_uint8_x; crypto_uint8 crypto_uint8_z = crypto_uint8_y - crypto_uint8_x; crypto_uint8_z ^= crypto_uint8_r & (crypto_uint8_z ^ crypto_uint8_y ^ (((crypto_uint8) 1) << (8-1))); crypto_uint8_z = crypto_uint8_signed_negative_mask(crypto_uint8_z); crypto_uint8_z &= crypto_uint8_r; return crypto_uint8_y ^ crypto_uint8_z; #endif } __attribute__((unused)) static inline void crypto_uint8_minmax(crypto_uint8 *crypto_uint8_p,crypto_uint8 *crypto_uint8_q) { crypto_uint8 crypto_uint8_x = *crypto_uint8_p; crypto_uint8 crypto_uint8_y = *crypto_uint8_q; #if defined(__GNUC__) && defined(__x86_64__) uint32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_z32; __asm__ ("cmpb %b2,%b1\n movl %1,%0\n cmoval %2,%1\n cmoval %0,%2" : "=&r"(crypto_uint8_z32), "+&q"(crypto_uint8_x32), "+q"(crypto_uint8_y32) : : "cc"); crypto_uint8_x = crypto_uint8_x32; crypto_uint8_y = crypto_uint8_y32; *crypto_uint8_p = crypto_uint8_x; *crypto_uint8_q = crypto_uint8_y; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_r, crypto_uint8_s; __asm__ ("and %w0,%w0,255\n cmp %w0,%w3,uxtb\n csel %w1,%w0,%w3,lo\n csel %w2,%w3,%w0,lo" : "+&r"(crypto_uint8_x), "=&r"(crypto_uint8_r), "=r"(crypto_uint8_s) : "r"(crypto_uint8_y) : "cc"); *crypto_uint8_p = crypto_uint8_r; *crypto_uint8_q = crypto_uint8_s; #else crypto_uint8 crypto_uint8_r = crypto_uint8_y ^ crypto_uint8_x; crypto_uint8 crypto_uint8_z = crypto_uint8_y - crypto_uint8_x; crypto_uint8_z ^= crypto_uint8_r & (crypto_uint8_z ^ crypto_uint8_y ^ (((crypto_uint8) 1) << (8-1))); crypto_uint8_z = crypto_uint8_signed_negative_mask(crypto_uint8_z); crypto_uint8_z &= crypto_uint8_r; crypto_uint8_x ^= crypto_uint8_z; crypto_uint8_y ^= crypto_uint8_z; *crypto_uint8_p = crypto_uint8_x; *crypto_uint8_q = crypto_uint8_y; #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_smaller_mask(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpb %b3,%b2\n cmovbl %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32), "q"(crypto_uint8_y32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n csetm %w0,lo" : "=&r"(crypto_uint8_z) : "r"(crypto_uint8_x), "r"(crypto_uint8_y) : "cc"); return crypto_uint8_z; #else crypto_uint8 crypto_uint8_r = crypto_uint8_x ^ crypto_uint8_y; crypto_uint8 crypto_uint8_z = crypto_uint8_x - crypto_uint8_y; crypto_uint8_z ^= crypto_uint8_r & (crypto_uint8_z ^ crypto_uint8_x ^ (((crypto_uint8) 1) << (8-1))); return crypto_uint8_signed_negative_mask(crypto_uint8_z); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_smaller_01(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpb %b3,%b2\n cmovbl %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32), "q"(crypto_uint8_y32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n cset %w0,lo" : "=&r"(crypto_uint8_z) : "r"(crypto_uint8_x), "r"(crypto_uint8_y) : "cc"); return crypto_uint8_z; #else crypto_uint8 crypto_uint8_r = crypto_uint8_x ^ crypto_uint8_y; crypto_uint8 crypto_uint8_z = crypto_uint8_x - crypto_uint8_y; crypto_uint8_z ^= crypto_uint8_r & (crypto_uint8_z ^ crypto_uint8_x ^ (((crypto_uint8) 1) << (8-1))); return crypto_uint8_topbit_01(crypto_uint8_z); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_leq_mask(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $-1,%1\n cmpb %b3,%b2\n cmovbel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32), "q"(crypto_uint8_y32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n csetm %w0,ls" : "=&r"(crypto_uint8_z) : "r"(crypto_uint8_x), "r"(crypto_uint8_y) : "cc"); return crypto_uint8_z; #else return ~crypto_uint8_smaller_mask(crypto_uint8_y,crypto_uint8_x); #endif } __attribute__((unused)) static inline crypto_uint8 crypto_uint8_leq_01(crypto_uint8 crypto_uint8_x,crypto_uint8 crypto_uint8_y) { #if defined(__GNUC__) && defined(__x86_64__) crypto_uint8 crypto_uint8_z; int32_t crypto_uint8_x32 = crypto_uint8_x,crypto_uint8_y32 = crypto_uint8_y,crypto_uint8_q32,crypto_uint8_z32; __asm__ ("xorl %0,%0\n movl $1,%1\n cmpb %b3,%b2\n cmovbel %1,%0" : "=&r"(crypto_uint8_z32), "=&r"(crypto_uint8_q32) : "q"(crypto_uint8_x32), "q"(crypto_uint8_y32) : "cc"); crypto_uint8_z = crypto_uint8_z32; return crypto_uint8_z; #elif defined(__GNUC__) && defined(__aarch64__) crypto_uint8 crypto_uint8_z; __asm__ ("and %w0,%w1,255\n cmp %w0,%w2,uxtb\n cset %w0,ls" : "=&r"(crypto_uint8_z) : "r"(crypto_uint8_x), "r"(crypto_uint8_y) : "cc"); return crypto_uint8_z; #else return 1-crypto_uint8_smaller_01(crypto_uint8_y,crypto_uint8_x); #endif } __attribute__((unused)) static inline int crypto_uint8_ones_num(crypto_uint8 crypto_uint8_x) { crypto_uint8 crypto_uint8_y = crypto_uint8_x; const crypto_uint8 C0 = 0x55; const crypto_uint8 C1 = 0x33; const crypto_uint8 C2 = 0x0f; crypto_uint8_y -= ((crypto_uint8_y >> 1) & C0); crypto_uint8_y = (crypto_uint8_y & C1) + ((crypto_uint8_y >> 2) & C1); crypto_uint8_y = (crypto_uint8_y + (crypto_uint8_y >> 4)) & C2; return crypto_uint8_y; } __attribute__((unused)) static inline int crypto_uint8_bottomzeros_num(crypto_uint8 crypto_uint8_x) { #if defined(__GNUC__) && defined(__x86_64__) int32_t fallback = 8; int32_t crypto_uint8_x32 = crypto_uint8_x; __asm__ ("bsfl %0,%0\n cmovel %1,%0" : "+&r"(crypto_uint8_x32) : "r"(fallback) : "cc"); crypto_uint8_x = crypto_uint8_x32; return crypto_uint8_x; #elif defined(__GNUC__) && defined(__aarch64__) int64_t crypto_uint8_y; __asm__ ("orr %w0,%w1,-256\n rbit %w0,%w0\n clz %w0,%w0" : "=r"(crypto_uint8_y) : "r"(crypto_uint8_x) : ); return crypto_uint8_y; #else crypto_uint8 crypto_uint8_y = crypto_uint8_x ^ (crypto_uint8_x-1); crypto_uint8_y = ((crypto_uint8_signed) crypto_uint8_y) >> 1; crypto_uint8_y &= ~(crypto_uint8_x & (((crypto_uint8) 1) << (8-1))); return crypto_uint8_ones_num(crypto_uint8_y); #endif } #endif dq-20250201/cryptoint/crypto_uintN.h000066400000000000000000001355071474734131400172770ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #ifndef crypto_uintN_h #define crypto_uintN_h #include #define crypto_uintN uintN_t #define crypto_uintN_signed intN_t #define crypto_uintN_signed_optblocker namespace_uintN_signed_optblocker extern volatile crypto_uintN_signed crypto_uintN_signed_optblocker; __attribute__((unused)) static inline crypto_uintN crypto_uintN_load(const unsigned char *crypto_uintN_s) { crypto_uintN crypto_uintN_z = 0; int crypto_uintN_k; for (crypto_uintN_k = 0;crypto_uintN_k < N;crypto_uintN_k += 8) crypto_uintN_z |= ((crypto_uintN) (*crypto_uintN_s++)) << crypto_uintN_k; return crypto_uintN_z; } __attribute__((unused)) static inline crypto_uintN crypto_uintN_load_bigendian(const unsigned char *crypto_uintN_s) { crypto_uintN crypto_uintN_z = 0; int crypto_uintN_k; for (crypto_uintN_k = N - 8;crypto_uintN_k >= 0;crypto_uintN_k -= 8) crypto_uintN_z |= ((crypto_uintN) (*crypto_uintN_s++)) << crypto_uintN_k; return crypto_uintN_z; } __attribute__((unused)) static inline void crypto_uintN_store(unsigned char *crypto_uintN_s,crypto_uintN crypto_uintN_x) { int crypto_uintN_k; for (crypto_uintN_k = 0;crypto_uintN_k < N;crypto_uintN_k += 8) *crypto_uintN_s++ = crypto_uintN_x >> crypto_uintN_k; } __attribute__((unused)) static inline void crypto_uintN_store_bigendian(unsigned char *crypto_uintN_s,crypto_uintN crypto_uintN_x) { int crypto_uintN_k; for (crypto_uintN_k = N - 8;crypto_uintN_k >= 0;crypto_uintN_k -= 8) *crypto_uintN_s++ = crypto_uintN_x >> crypto_uintN_k; } __attribute__((unused)) static inline crypto_uintN_signed crypto_uintN_signed_negative_mask(crypto_uintN_signed crypto_uintN_x) { #if amd64 8: readasm("amd64; int8 crypto_uintN_x; crypto_uintN_x signed>>= 7"); 16: readasm("amd64; int16 crypto_uintN_x; crypto_uintN_x signed>>= 15"); 32: readasm("amd64; int32 crypto_uintN_x; crypto_uintN_x signed>>= 31"); 64: readasm("amd64; int64 crypto_uintN_x; crypto_uintN_x signed>>= 63"); return crypto_uintN_x; #elif arm64 crypto_uintN_signed crypto_uintN_y; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = -(1 & (crypto_uintN_x unsigned>> 7))"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = -(1 & (crypto_uintN_x unsigned>> 15))"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = crypto_uintN_x signed>> 31"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = crypto_uintN_x signed>> 63"); return crypto_uintN_y; #else crypto_uintN_x >>= N-6; crypto_uintN_x += crypto_uintN_signed_optblocker; crypto_uintN_x >>= 5; return crypto_uintN_x; #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_topbit_01(crypto_uintN crypto_uintN_x) { #if amd64 8: readasm("amd64; int8 crypto_uintN_x; crypto_uintN_x unsigned>>= 7"); 16: readasm("amd64; int16 crypto_uintN_x; crypto_uintN_x unsigned>>= 15"); 32: readasm("amd64; int32 crypto_uintN_x; crypto_uintN_x unsigned>>= 31"); 64: readasm("amd64; int64 crypto_uintN_x; crypto_uintN_x unsigned>>= 63"); return crypto_uintN_x; #elif arm64 crypto_uintN_signed crypto_uintN_y; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = 1 & (crypto_uintN_x unsigned>> 7)"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = 1 & (crypto_uintN_x unsigned>> 15)"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = crypto_uintN_x unsigned>> 31"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = crypto_uintN_x unsigned>> 63"); return crypto_uintN_y; #else crypto_uintN_x >>= N-6; crypto_uintN_x += crypto_uintN_signed_optblocker; crypto_uintN_x >>= 5; return crypto_uintN_x; #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_topbit_mask(crypto_uintN crypto_uintN_x) { return crypto_uintN_signed_negative_mask(crypto_uintN_x); } __attribute__((unused)) static inline crypto_uintN crypto_uintN_bottombit_mask(crypto_uintN crypto_uintN_x) { #if amd64 8: readasm("amd64; int8 crypto_uintN_x; crypto_uintN_x &= 1"); 16: readasm("amd64; int16 crypto_uintN_x; crypto_uintN_x &= 1"); 32: readasm("amd64; int32 crypto_uintN_x; crypto_uintN_x &= 1"); 64: readasm("amd64; int64 crypto_uintN_x; crypto_uintN_x &= 1"); return -crypto_uintN_x; #elif arm64 crypto_uintN crypto_uintN_y; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = -(1 & (crypto_uintN_x unsigned>> 0))"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = -(1 & (crypto_uintN_x unsigned>> 0))"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = -(1 & (crypto_uintN_x unsigned>> 0))"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = -(1 & (crypto_uintN_x unsigned>> 0))"); return crypto_uintN_y; #else crypto_uintN_x &= 1 + crypto_uintN_signed_optblocker; return -crypto_uintN_x; #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_bottombit_01(crypto_uintN crypto_uintN_x) { #if amd64 8: readasm("amd64; int8 crypto_uintN_x; crypto_uintN_x &= 1"); 16: readasm("amd64; int16 crypto_uintN_x; crypto_uintN_x &= 1"); 32: readasm("amd64; int32 crypto_uintN_x; crypto_uintN_x &= 1"); 64: readasm("amd64; int64 crypto_uintN_x; crypto_uintN_x &= 1"); return crypto_uintN_x; #elif arm64 crypto_uintN crypto_uintN_y; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = 1 & (crypto_uintN_x unsigned>> 0)"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = 1 & (crypto_uintN_x unsigned>> 0)"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = 1 & (crypto_uintN_x unsigned>> 0)"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = 1 & (crypto_uintN_x unsigned>> 0)"); return crypto_uintN_y; #else crypto_uintN_x &= 1 + crypto_uintN_signed_optblocker; return crypto_uintN_x; #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_bitinrangepublicpos_mask(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_s) { #if amd64 8: readasm("amd64; int8 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); #elif arm64 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = (uint8) crypto_uintN_x; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = (uint16) crypto_uintN_x; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); #else crypto_uintN_x >>= crypto_uintN_s ^ crypto_uintN_signed_optblocker; #endif return crypto_uintN_bottombit_mask(crypto_uintN_x); } __attribute__((unused)) static inline crypto_uintN crypto_uintN_bitinrangepublicpos_01(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_s) { #if amd64 8: readasm("amd64; int8 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); #elif arm64 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = (uint8) crypto_uintN_x; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = (uint16) crypto_uintN_x; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); #else crypto_uintN_x >>= crypto_uintN_s ^ crypto_uintN_signed_optblocker; #endif return crypto_uintN_bottombit_01(crypto_uintN_x); } __attribute__((unused)) static inline crypto_uintN crypto_uintN_shlmod(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_s) { #if amd64 8: crypto_uintN_s &= 7; 8: readasm("amd64; int8 crypto_uintN_x crypto_uintN_s; crypto_uintN_x <<= crypto_uintN_s"); 16: crypto_uintN_s &= 15; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_s; crypto_uintN_x <<= crypto_uintN_s"); 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_s; crypto_uintN_x <<= crypto_uintN_s"); 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_s; crypto_uintN_x <<= crypto_uintN_s"); #elif arm64 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_s; crypto_uintN_s = crypto_uintN_s & 7; crypto_uintN_x = (uint8) crypto_uintN_x; crypto_uintN_x = crypto_uintN_x << crypto_uintN_s"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_s; crypto_uintN_s = crypto_uintN_s & 15; crypto_uintN_x = (uint16) crypto_uintN_x; crypto_uintN_x = crypto_uintN_x << crypto_uintN_s"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = crypto_uintN_x << crypto_uintN_s"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = crypto_uintN_x << crypto_uintN_s"); #else int crypto_uintN_k, crypto_uintN_l; for (crypto_uintN_l = 0,crypto_uintN_k = 1;crypto_uintN_k < N;++crypto_uintN_l,crypto_uintN_k *= 2) crypto_uintN_x ^= (crypto_uintN_x ^ (crypto_uintN_x << crypto_uintN_k)) & crypto_uintN_bitinrangepublicpos_mask(crypto_uintN_s,crypto_uintN_l); #endif return crypto_uintN_x; } __attribute__((unused)) static inline crypto_uintN crypto_uintN_shrmod(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_s) { #if amd64 8: crypto_uintN_s &= 7; 8: readasm("amd64; int8 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 16: crypto_uintN_s &= 15; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_s; crypto_uintN_x unsigned>>= crypto_uintN_s"); #elif arm64 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_s; crypto_uintN_s = crypto_uintN_s & 7; crypto_uintN_x = (uint8) crypto_uintN_x; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_s; crypto_uintN_s = crypto_uintN_s & 15; crypto_uintN_x = (uint16) crypto_uintN_x; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_s; crypto_uintN_x = crypto_uintN_x unsigned>> crypto_uintN_s"); #else int crypto_uintN_k, crypto_uintN_l; for (crypto_uintN_l = 0,crypto_uintN_k = 1;crypto_uintN_k < N;++crypto_uintN_l,crypto_uintN_k *= 2) crypto_uintN_x ^= (crypto_uintN_x ^ (crypto_uintN_x >> crypto_uintN_k)) & crypto_uintN_bitinrangepublicpos_mask(crypto_uintN_s,crypto_uintN_l); #endif return crypto_uintN_x; } __attribute__((unused)) static inline crypto_uintN crypto_uintN_bitmod_mask(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_s) { crypto_uintN_x = crypto_uintN_shrmod(crypto_uintN_x,crypto_uintN_s); return crypto_uintN_bottombit_mask(crypto_uintN_x); } __attribute__((unused)) static inline crypto_uintN crypto_uintN_bitmod_01(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_s) { crypto_uintN_x = crypto_uintN_shrmod(crypto_uintN_x,crypto_uintN_s); return crypto_uintN_bottombit_01(crypto_uintN_x); } __attribute__((unused)) static inline crypto_uintN crypto_uintN_nonzero_mask(crypto_uintN crypto_uintN_x) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = -1; crypto_uintN_x32 & (int8) crypto_uintN_x32; crypto_uintN_z32 = crypto_uintN_q32 if !="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if !="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if !="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if !="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_z; crypto_uintN_x & 255; crypto_uintN_z = -1 if != else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_z; crypto_uintN_x & 65535; crypto_uintN_z = -1 if != else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_z; crypto_uintN_x - 0; crypto_uintN_z = -1 if != else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_z; crypto_uintN_x - 0; crypto_uintN_z = -1 if != else 0"); return crypto_uintN_z; #else crypto_uintN_x |= -crypto_uintN_x; return crypto_uintN_signed_negative_mask(crypto_uintN_x); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_nonzero_01(crypto_uintN crypto_uintN_x) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = 1; crypto_uintN_x32 & (int8) crypto_uintN_x32; crypto_uintN_z32 = crypto_uintN_q32 if !="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if !="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if !="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if !="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_z; crypto_uintN_x & 255; crypto_uintN_z = 1 if != else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_z; crypto_uintN_x & 65535; crypto_uintN_z = 1 if != else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_z; crypto_uintN_x - 0; crypto_uintN_z = 1 if != else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_z; crypto_uintN_x - 0; crypto_uintN_z = 1 if != else 0"); return crypto_uintN_z; #else crypto_uintN_x |= -crypto_uintN_x; return crypto_uintN_topbit_01(crypto_uintN_x); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_zero_mask(crypto_uintN crypto_uintN_x) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = -1; crypto_uintN_x32 & (int8) crypto_uintN_x32; crypto_uintN_z32 = crypto_uintN_q32 if ="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if ="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if ="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if ="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_z; crypto_uintN_x & 255; crypto_uintN_z = -1 if = else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_z; crypto_uintN_x & 65535; crypto_uintN_z = -1 if = else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_z; crypto_uintN_x - 0; crypto_uintN_z = -1 if = else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_z; crypto_uintN_x - 0; crypto_uintN_z = -1 if = else 0"); return crypto_uintN_z; #else return ~crypto_uintN_nonzero_mask(crypto_uintN_x); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_zero_01(crypto_uintN crypto_uintN_x) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = 1; crypto_uintN_x32 & (int8) crypto_uintN_x32; crypto_uintN_z32 = crypto_uintN_q32 if ="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if ="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if ="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x & crypto_uintN_x; crypto_uintN_z = crypto_uintN_q if ="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_z; crypto_uintN_x & 255; crypto_uintN_z = 1 if = else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_z; crypto_uintN_x & 65535; crypto_uintN_z = 1 if = else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_z; crypto_uintN_x - 0; crypto_uintN_z = 1 if = else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_z; crypto_uintN_x - 0; crypto_uintN_z = 1 if = else 0"); return crypto_uintN_z; #else return 1-crypto_uintN_nonzero_01(crypto_uintN_x); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_unequal_mask(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = -1; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_q32 if !="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if !="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if !="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if !="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint8) crypto_uintN_x; crypto_uintN_z - (uint8) crypto_uintN_y; crypto_uintN_z = -1 if != else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint16) crypto_uintN_x; crypto_uintN_z - (uint16) crypto_uintN_y; crypto_uintN_z = -1 if != else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = -1 if != else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = -1 if != else 0"); return crypto_uintN_z; #else return crypto_uintN_nonzero_mask(crypto_uintN_x ^ crypto_uintN_y); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_unequal_01(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = 1; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_q32 if !="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if !="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if !="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if !="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint8) crypto_uintN_x; crypto_uintN_z - (uint8) crypto_uintN_y; crypto_uintN_z = 1 if != else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint16) crypto_uintN_x; crypto_uintN_z - (uint16) crypto_uintN_y; crypto_uintN_z = 1 if != else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = 1 if != else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = 1 if != else 0"); return crypto_uintN_z; #else return crypto_uintN_nonzero_01(crypto_uintN_x ^ crypto_uintN_y); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_equal_mask(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = -1; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_q32 if ="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if ="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if ="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if ="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint8) crypto_uintN_x; crypto_uintN_z - (uint8) crypto_uintN_y; crypto_uintN_z = -1 if = else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint16) crypto_uintN_x; crypto_uintN_z - (uint16) crypto_uintN_y; crypto_uintN_z = -1 if = else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = -1 if = else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = -1 if = else 0"); return crypto_uintN_z; #else return ~crypto_uintN_unequal_mask(crypto_uintN_x,crypto_uintN_y); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_equal_01(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = 1; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_q32 if ="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if ="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if ="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if ="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint8) crypto_uintN_x; crypto_uintN_z - (uint8) crypto_uintN_y; crypto_uintN_z = 1 if = else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint16) crypto_uintN_x; crypto_uintN_z - (uint16) crypto_uintN_y; crypto_uintN_z = 1 if = else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = 1 if = else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = 1 if = else 0"); return crypto_uintN_z; #else return 1-crypto_uintN_unequal_01(crypto_uintN_x,crypto_uintN_y); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_min(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: uint32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_y32; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_x32 = crypto_uintN_y32 if unsigned>"); 8: crypto_uintN_x = crypto_uintN_x32; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned>"); 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned>"); 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned>"); return crypto_uintN_x; #elif arm64 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y; crypto_uintN_x = (uint8) crypto_uintN_x; crypto_uintN_x - (uint8) crypto_uintN_y; crypto_uintN_x = crypto_uintN_x if unsigned< else crypto_uintN_y"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_x = (uint16) crypto_uintN_x; crypto_uintN_x - (uint16) crypto_uintN_y; crypto_uintN_x = crypto_uintN_x if unsigned< else crypto_uintN_y"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_x if unsigned< else crypto_uintN_y"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_x if unsigned< else crypto_uintN_y"); return crypto_uintN_x; #else crypto_uintN crypto_uintN_r = crypto_uintN_y ^ crypto_uintN_x; crypto_uintN crypto_uintN_z = crypto_uintN_y - crypto_uintN_x; crypto_uintN_z ^= crypto_uintN_r & (crypto_uintN_z ^ crypto_uintN_y ^ (((crypto_uintN) 1) << (N-1))); crypto_uintN_z = crypto_uintN_signed_negative_mask(crypto_uintN_z); crypto_uintN_z &= crypto_uintN_r; return crypto_uintN_x ^ crypto_uintN_z; #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_max(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: uint32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_y32; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_x32 = crypto_uintN_y32 if unsigned<"); 8: crypto_uintN_x = crypto_uintN_x32; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned<"); 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned<"); 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned<"); return crypto_uintN_x; #elif arm64 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y; crypto_uintN_x = (uint8) crypto_uintN_x; crypto_uintN_x - (uint8) crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned< else crypto_uintN_x"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_x = (uint16) crypto_uintN_x; crypto_uintN_x - (uint16) crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned< else crypto_uintN_x"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned< else crypto_uintN_x"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_x - crypto_uintN_y; crypto_uintN_x = crypto_uintN_y if unsigned< else crypto_uintN_x"); return crypto_uintN_x; #else crypto_uintN crypto_uintN_r = crypto_uintN_y ^ crypto_uintN_x; crypto_uintN crypto_uintN_z = crypto_uintN_y - crypto_uintN_x; crypto_uintN_z ^= crypto_uintN_r & (crypto_uintN_z ^ crypto_uintN_y ^ (((crypto_uintN) 1) << (N-1))); crypto_uintN_z = crypto_uintN_signed_negative_mask(crypto_uintN_z); crypto_uintN_z &= crypto_uintN_r; return crypto_uintN_y ^ crypto_uintN_z; #endif } __attribute__((unused)) static inline void crypto_uintN_minmax(crypto_uintN *crypto_uintN_p,crypto_uintN *crypto_uintN_q) { crypto_uintN crypto_uintN_x = *crypto_uintN_p; crypto_uintN crypto_uintN_y = *crypto_uintN_q; #if amd64 8: uint32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_x32; crypto_uintN_x32 = crypto_uintN_y32 if unsigned>; crypto_uintN_y32 = crypto_uintN_z32 if unsigned>"); 8: crypto_uintN_x = crypto_uintN_x32; crypto_uintN_y = crypto_uintN_y32; 16: crypto_uintN crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_x; crypto_uintN_x = crypto_uintN_y if unsigned>; crypto_uintN_y = crypto_uintN_z if unsigned>"); 32: crypto_uintN crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_x; crypto_uintN_x = crypto_uintN_y if unsigned>; crypto_uintN_y = crypto_uintN_z if unsigned>"); 64: crypto_uintN crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_x; crypto_uintN_x = crypto_uintN_y if unsigned>; crypto_uintN_y = crypto_uintN_z if unsigned>"); *crypto_uintN_p = crypto_uintN_x; *crypto_uintN_q = crypto_uintN_y; #elif arm64 crypto_uintN crypto_uintN_r, crypto_uintN_s; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_r crypto_uintN_s; crypto_uintN_x = (uint8) crypto_uintN_x; crypto_uintN_x - (uint8) crypto_uintN_y; crypto_uintN_r = crypto_uintN_x if unsigned< else crypto_uintN_y; crypto_uintN_s = crypto_uintN_y if unsigned< else crypto_uintN_x"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_r crypto_uintN_s; crypto_uintN_x = (uint16) crypto_uintN_x; crypto_uintN_x - (uint16) crypto_uintN_y; crypto_uintN_r = crypto_uintN_x if unsigned< else crypto_uintN_y; crypto_uintN_s = crypto_uintN_y if unsigned< else crypto_uintN_x"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_r crypto_uintN_s; crypto_uintN_x - crypto_uintN_y; crypto_uintN_r = crypto_uintN_x if unsigned< else crypto_uintN_y; crypto_uintN_s = crypto_uintN_y if unsigned< else crypto_uintN_x"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_r crypto_uintN_s; crypto_uintN_x - crypto_uintN_y; crypto_uintN_r = crypto_uintN_x if unsigned< else crypto_uintN_y; crypto_uintN_s = crypto_uintN_y if unsigned< else crypto_uintN_x"); *crypto_uintN_p = crypto_uintN_r; *crypto_uintN_q = crypto_uintN_s; #else crypto_uintN crypto_uintN_r = crypto_uintN_y ^ crypto_uintN_x; crypto_uintN crypto_uintN_z = crypto_uintN_y - crypto_uintN_x; crypto_uintN_z ^= crypto_uintN_r & (crypto_uintN_z ^ crypto_uintN_y ^ (((crypto_uintN) 1) << (N-1))); crypto_uintN_z = crypto_uintN_signed_negative_mask(crypto_uintN_z); crypto_uintN_z &= crypto_uintN_r; crypto_uintN_x ^= crypto_uintN_z; crypto_uintN_y ^= crypto_uintN_z; *crypto_uintN_p = crypto_uintN_x; *crypto_uintN_q = crypto_uintN_y; #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_smaller_mask(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = -1; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_q32 if unsigned<"); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<"); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<"); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<"); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint8) crypto_uintN_x; crypto_uintN_z - (uint8) crypto_uintN_y; crypto_uintN_z = -1 if unsigned< else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint16) crypto_uintN_x; crypto_uintN_z - (uint16) crypto_uintN_y; crypto_uintN_z = -1 if unsigned< else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = -1 if unsigned< else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = -1 if unsigned< else 0"); return crypto_uintN_z; #else crypto_uintN crypto_uintN_r = crypto_uintN_x ^ crypto_uintN_y; crypto_uintN crypto_uintN_z = crypto_uintN_x - crypto_uintN_y; crypto_uintN_z ^= crypto_uintN_r & (crypto_uintN_z ^ crypto_uintN_x ^ (((crypto_uintN) 1) << (N-1))); return crypto_uintN_signed_negative_mask(crypto_uintN_z); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_smaller_01(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = 1; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_q32 if unsigned<"); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<"); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<"); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<"); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint8) crypto_uintN_x; crypto_uintN_z - (uint8) crypto_uintN_y; crypto_uintN_z = 1 if unsigned< else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint16) crypto_uintN_x; crypto_uintN_z - (uint16) crypto_uintN_y; crypto_uintN_z = 1 if unsigned< else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = 1 if unsigned< else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = 1 if unsigned< else 0"); return crypto_uintN_z; #else crypto_uintN crypto_uintN_r = crypto_uintN_x ^ crypto_uintN_y; crypto_uintN crypto_uintN_z = crypto_uintN_x - crypto_uintN_y; crypto_uintN_z ^= crypto_uintN_r & (crypto_uintN_z ^ crypto_uintN_x ^ (((crypto_uintN) 1) << (N-1))); return crypto_uintN_topbit_01(crypto_uintN_z); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_leq_mask(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = -1; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_q32 if unsigned<="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = -1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint8) crypto_uintN_x; crypto_uintN_z - (uint8) crypto_uintN_y; crypto_uintN_z = -1 if unsigned<= else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint16) crypto_uintN_x; crypto_uintN_z - (uint16) crypto_uintN_y; crypto_uintN_z = -1 if unsigned<= else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = -1 if unsigned<= else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = -1 if unsigned<= else 0"); return crypto_uintN_z; #else return ~crypto_uintN_smaller_mask(crypto_uintN_y,crypto_uintN_x); #endif } __attribute__((unused)) static inline crypto_uintN crypto_uintN_leq_01(crypto_uintN crypto_uintN_x,crypto_uintN crypto_uintN_y) { #if amd64 8: crypto_uintN crypto_uintN_z; 8: int32_t crypto_uintN_x32 = crypto_uintN_x,crypto_uintN_y32 = crypto_uintN_y,crypto_uintN_q32,crypto_uintN_z32; 8: readasm("amd64; int32 crypto_uintN_x32 crypto_uintN_q32 crypto_uintN_y32 crypto_uintN_z32; crypto_uintN_z32 = 0; crypto_uintN_q32 = 1; crypto_uintN_x32 - (int8) crypto_uintN_y32; crypto_uintN_z32 = crypto_uintN_q32 if unsigned<="); 8: crypto_uintN_z = crypto_uintN_z32; 16: crypto_uintN crypto_uintN_q,crypto_uintN_z; 16: readasm("amd64; int16 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<="); 32: crypto_uintN crypto_uintN_q,crypto_uintN_z; 32: readasm("amd64; int32 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<="); 64: crypto_uintN crypto_uintN_q,crypto_uintN_z; 64: readasm("amd64; int64 crypto_uintN_x crypto_uintN_q crypto_uintN_y crypto_uintN_z; crypto_uintN_z = 0; crypto_uintN_q = 1; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = crypto_uintN_q if unsigned<="); return crypto_uintN_z; #elif arm64 crypto_uintN crypto_uintN_z; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint8) crypto_uintN_x; crypto_uintN_z - (uint8) crypto_uintN_y; crypto_uintN_z = 1 if unsigned<= else 0"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_z = (uint16) crypto_uintN_x; crypto_uintN_z - (uint16) crypto_uintN_y; crypto_uintN_z = 1 if unsigned<= else 0"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = 1 if unsigned<= else 0"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y crypto_uintN_z; crypto_uintN_x - crypto_uintN_y; crypto_uintN_z = 1 if unsigned<= else 0"); return crypto_uintN_z; #else return 1-crypto_uintN_smaller_01(crypto_uintN_y,crypto_uintN_x); #endif } __attribute__((unused)) static inline int crypto_uintN_ones_num(crypto_uintN crypto_uintN_x) { crypto_uintN crypto_uintN_y = crypto_uintN_x; 8: const crypto_uintN C0 = 0x55; 16: const crypto_uintN C0 = 0x5555; 32: const crypto_uintN C0 = 0x55555555; 64: const crypto_uintN C0 = 0x5555555555555555; 8: const crypto_uintN C1 = 0x33; 16: const crypto_uintN C1 = 0x3333; 32: const crypto_uintN C1 = 0x33333333; 64: const crypto_uintN C1 = 0x3333333333333333; 8: const crypto_uintN C2 = 0x0f; 16: const crypto_uintN C2 = 0x0f0f; 32: const crypto_uintN C2 = 0x0f0f0f0f; 64: const crypto_uintN C2 = 0x0f0f0f0f0f0f0f0f; crypto_uintN_y -= ((crypto_uintN_y >> 1) & C0); crypto_uintN_y = (crypto_uintN_y & C1) + ((crypto_uintN_y >> 2) & C1); crypto_uintN_y = (crypto_uintN_y + (crypto_uintN_y >> 4)) & C2; 16: crypto_uintN_y = (crypto_uintN_y + (crypto_uintN_y >> 8)) & 0xff; 32: crypto_uintN_y += crypto_uintN_y >> 8; 32: crypto_uintN_y = (crypto_uintN_y + (crypto_uintN_y >> 16)) & 0xff; 64: crypto_uintN_y += crypto_uintN_y >> 8; 64: crypto_uintN_y += crypto_uintN_y >> 16; 64: crypto_uintN_y = (crypto_uintN_y + (crypto_uintN_y >> 32)) & 0xff; return crypto_uintN_y; } __attribute__((unused)) static inline int crypto_uintN_bottomzeros_num(crypto_uintN crypto_uintN_x) { #if amd64 8: int32_t fallback = N; 8: int32_t crypto_uintN_x32 = crypto_uintN_x; 8: readasm("amd64; int32 crypto_uintN_x32 fallback; crypto_uintN_x32 = numbottomzeros_tricky crypto_uintN_x32; crypto_uintN_x32 = fallback if ="); 8: crypto_uintN_x = crypto_uintN_x32; 16: crypto_uintN fallback = N; 16: readasm("amd64; int16 crypto_uintN_x fallback; crypto_uintN_x = numbottomzeros_tricky crypto_uintN_x; crypto_uintN_x = fallback if ="); 32: crypto_uintN fallback = N; 32: readasm("amd64; int32 crypto_uintN_x fallback; crypto_uintN_x = numbottomzeros_tricky crypto_uintN_x; crypto_uintN_x = fallback if ="); 64: crypto_uintN fallback = N; 64: readasm("amd64; int64 crypto_uintN_x fallback; crypto_uintN_x = numbottomzeros_tricky crypto_uintN_x; crypto_uintN_x = fallback if ="); return crypto_uintN_x; #elif arm64 int64_t crypto_uintN_y; 8: readasm("arm64; int8 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = crypto_uintN_x | -256; crypto_uintN_y = bitrev32 crypto_uintN_y; crypto_uintN_y = numbottomzeros crypto_uintN_y"); 16: readasm("arm64; int16 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = crypto_uintN_x | -65536; crypto_uintN_y = bitrev32 crypto_uintN_y; crypto_uintN_y = numbottomzeros crypto_uintN_y"); 32: readasm("arm64; int32 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = bitrev32 crypto_uintN_x; crypto_uintN_y = numbottomzeros crypto_uintN_y"); 64: readasm("arm64; int64 crypto_uintN_x crypto_uintN_y; crypto_uintN_y = bitrev64 crypto_uintN_x; crypto_uintN_y = numbottomzeros crypto_uintN_y"); return crypto_uintN_y; #else crypto_uintN crypto_uintN_y = crypto_uintN_x ^ (crypto_uintN_x-1); crypto_uintN_y = ((crypto_uintN_signed) crypto_uintN_y) >> 1; crypto_uintN_y &= ~(crypto_uintN_x & (((crypto_uintN) 1) << (N-1))); return crypto_uintN_ones_num(crypto_uintN_y); #endif } #endif dq-20250201/cryptoint/functions000066400000000000000000000773421474734131400163660ustar00rootroot00000000000000TYPE TYPE_load(const unsigned char *S) { TYPE Z = 0; int K; for (K = 0;K < N;K += 8) Z |= ((TYPE) (*S++)) << K; return Z; } TYPE TYPE_load_bigendian(const unsigned char *S) { TYPE Z = 0; int K; for (K = N - 8;K >= 0;K -= 8) Z |= ((TYPE) (*S++)) << K; return Z; } void TYPE_store(unsigned char *S,TYPE X) { int K; for (K = 0;K < N;K += 8) *S++ = X >> K; } void TYPE_store_bigendian(unsigned char *S,TYPE X) { int K; for (K = N - 8;K >= 0;K -= 8) *S++ = X >> K; } # could instead use X >>= (N-1) ^ SIGNED_optblocker SIGNED SIGNED_negative_mask(SIGNED X) { #if amd64 8: readasm("amd64; int8 X; X signed>>= 7"); 16: readasm("amd64; int16 X; X signed>>= 15"); 32: readasm("amd64; int32 X; X signed>>= 31"); 64: readasm("amd64; int64 X; X signed>>= 63"); return X; #elif arm64 SIGNED Y; 8: readasm("arm64; int8 X Y; Y = -(1 & (X unsigned>> 7))"); 16: readasm("arm64; int16 X Y; Y = -(1 & (X unsigned>> 15))"); 32: readasm("arm64; int32 X Y; Y = X signed>> 31"); 64: readasm("arm64; int64 X Y; Y = X signed>> 63"); return Y; #else X >>= N-6; X += SIGNED_optblocker; X >>= 5; return X; #endif } UNSIGNED UNSIGNED_topbit_01(UNSIGNED X) { #if amd64 8: readasm("amd64; int8 X; X unsigned>>= 7"); 16: readasm("amd64; int16 X; X unsigned>>= 15"); 32: readasm("amd64; int32 X; X unsigned>>= 31"); 64: readasm("amd64; int64 X; X unsigned>>= 63"); return X; #elif arm64 SIGNED Y; 8: readasm("arm64; int8 X Y; Y = 1 & (X unsigned>> 7)"); 16: readasm("arm64; int16 X Y; Y = 1 & (X unsigned>> 15)"); 32: readasm("arm64; int32 X Y; Y = X unsigned>> 31"); 64: readasm("arm64; int64 X Y; Y = X unsigned>> 63"); return Y; #else X >>= N-6; X += SIGNED_optblocker; X >>= 5; return X; #endif } SIGNED: TYPE TYPE_negative_01(TYPE X) { return UNSIGNED_topbit_01(X); } SIGNED: TYPE TYPE_topbit_mask(TYPE X) { return SIGNED_negative_mask(X); } SIGNED: TYPE TYPE_topbit_01(TYPE X) { return UNSIGNED_topbit_01(X); } UNSIGNED: TYPE TYPE_topbit_mask(TYPE X) { return SIGNED_negative_mask(X); } TYPE TYPE_bottombit_mask(TYPE X) { #if amd64 8: readasm("amd64; int8 X; X &= 1"); 16: readasm("amd64; int16 X; X &= 1"); 32: readasm("amd64; int32 X; X &= 1"); 64: readasm("amd64; int64 X; X &= 1"); return -X; #elif arm64 TYPE Y; 8: readasm("arm64; int8 X Y; Y = -(1 & (X unsigned>> 0))"); 16: readasm("arm64; int16 X Y; Y = -(1 & (X unsigned>> 0))"); 32: readasm("arm64; int32 X Y; Y = -(1 & (X unsigned>> 0))"); 64: readasm("arm64; int64 X Y; Y = -(1 & (X unsigned>> 0))"); return Y; #else X &= 1 + SIGNED_optblocker; return -X; #endif } TYPE TYPE_bottombit_01(TYPE X) { #if amd64 8: readasm("amd64; int8 X; X &= 1"); 16: readasm("amd64; int16 X; X &= 1"); 32: readasm("amd64; int32 X; X &= 1"); 64: readasm("amd64; int64 X; X &= 1"); return X; #elif arm64 TYPE Y; 8: readasm("arm64; int8 X Y; Y = 1 & (X unsigned>> 0)"); 16: readasm("arm64; int16 X Y; Y = 1 & (X unsigned>> 0)"); 32: readasm("arm64; int32 X Y; Y = 1 & (X unsigned>> 0)"); 64: readasm("arm64; int64 X Y; Y = 1 & (X unsigned>> 0)"); return Y; #else X &= 1 + SIGNED_optblocker; return X; #endif } # without optblocker here, seems possible that compiler will cause trouble, # especially if S is compile-time constant N-1 SIGNED: TYPE TYPE_bitinrangepublicpos_mask(TYPE X,TYPE S) { #if amd64 8: readasm("amd64; int8 X S; X signed>>= S"); 16: readasm("amd64; int16 X S; X signed>>= S"); 32: readasm("amd64; int32 X S; X signed>>= S"); 64: readasm("amd64; int64 X S; X signed>>= S"); #elif arm64 8: readasm("arm64; int8 X S; X = (int8) X; X = X signed>> S"); 16: readasm("arm64; int16 X S; X = (int16) X; X = X signed>> S"); 32: readasm("arm64; int32 X S; X = X signed>> S"); 64: readasm("arm64; int64 X S; X = X signed>> S"); #else X >>= S ^ SIGNED_optblocker; #endif return TYPE_bottombit_mask(X); } # without optblocker here, seems possible that compiler will cause trouble, # especially if S is compile-time constant N-1 SIGNED: TYPE TYPE_bitinrangepublicpos_01(TYPE X,TYPE S) { #if amd64 8: readasm("amd64; int8 X S; X signed>>= S"); 16: readasm("amd64; int16 X S; X signed>>= S"); 32: readasm("amd64; int32 X S; X signed>>= S"); 64: readasm("amd64; int64 X S; X signed>>= S"); #elif arm64 8: readasm("arm64; int8 X S; X = (int8) X; X = X signed>> S"); 16: readasm("arm64; int16 X S; X = (int16) X; X = X signed>> S"); 32: readasm("arm64; int32 X S; X = X signed>> S"); 64: readasm("arm64; int64 X S; X = X signed>> S"); #else X >>= S ^ SIGNED_optblocker; #endif return TYPE_bottombit_01(X); } # without optblocker here, seems possible that compiler will cause trouble, # especially if S is compile-time constant N-1 UNSIGNED: TYPE TYPE_bitinrangepublicpos_mask(TYPE X,TYPE S) { #if amd64 8: readasm("amd64; int8 X S; X unsigned>>= S"); 16: readasm("amd64; int16 X S; X unsigned>>= S"); 32: readasm("amd64; int32 X S; X unsigned>>= S"); 64: readasm("amd64; int64 X S; X unsigned>>= S"); #elif arm64 8: readasm("arm64; int8 X S; X = (uint8) X; X = X unsigned>> S"); 16: readasm("arm64; int16 X S; X = (uint16) X; X = X unsigned>> S"); 32: readasm("arm64; int32 X S; X = X unsigned>> S"); 64: readasm("arm64; int64 X S; X = X unsigned>> S"); #else X >>= S ^ SIGNED_optblocker; #endif return TYPE_bottombit_mask(X); } # without optblocker here, seems possible that compiler will cause trouble, # especially if S is compile-time constant N-1 UNSIGNED: TYPE TYPE_bitinrangepublicpos_01(TYPE X,TYPE S) { #if amd64 8: readasm("amd64; int8 X S; X unsigned>>= S"); 16: readasm("amd64; int16 X S; X unsigned>>= S"); 32: readasm("amd64; int32 X S; X unsigned>>= S"); 64: readasm("amd64; int64 X S; X unsigned>>= S"); #elif arm64 8: readasm("arm64; int8 X S; X = (uint8) X; X = X unsigned>> S"); 16: readasm("arm64; int16 X S; X = (uint16) X; X = X unsigned>> S"); 32: readasm("arm64; int32 X S; X = X unsigned>> S"); 64: readasm("arm64; int64 X S; X = X unsigned>> S"); #else X >>= S ^ SIGNED_optblocker; #endif return TYPE_bottombit_01(X); } # same as (X << (S % N)) but without assuming barrel shifter # for amd64, assumes constant-time barrel shifter TYPE TYPE_shlmod(TYPE X,TYPE S) { #if amd64 8: S &= 7; 8: readasm("amd64; int8 X S; X <<= S"); 16: S &= 15; 16: readasm("amd64; int16 X S; X <<= S"); 32: readasm("amd64; int32 X S; X <<= S"); 64: readasm("amd64; int64 X S; X <<= S"); #elif arm64 8: readasm("arm64; int8 X S; S = S & 7; X = (uint8) X; X = X << S"); 16: readasm("arm64; int16 X S; S = S & 15; X = (uint16) X; X = X << S"); 32: readasm("arm64; int32 X S; X = X << S"); 64: readasm("arm64; int64 X S; X = X << S"); #else int K, L; for (L = 0,K = 1;K < N;++L,K *= 2) X ^= (X ^ (X << K)) & TYPE_bitinrangepublicpos_mask(S,L); #endif return X; } # same as (X >> (S % N)) but without assuming barrel shifter SIGNED: TYPE TYPE_shrmod(TYPE X,TYPE S) { #if amd64 8: S &= 7; 8: readasm("amd64; int8 X S; X signed>>= S"); 16: S &= 15; 16: readasm("amd64; int16 X S; X signed>>= S"); 32: readasm("amd64; int32 X S; X signed>>= S"); 64: readasm("amd64; int64 X S; X signed>>= S"); #elif arm64 8: readasm("arm64; int8 X S; S = S & 7; X = (int8) X; X = X signed>> S"); 16: readasm("arm64; int16 X S; S = S & 15; X = (int16) X; X = X signed>> S"); 32: readasm("arm64; int32 X S; X = X signed>> S"); 64: readasm("arm64; int64 X S; X = X signed>> S"); #else int K, L; for (L = 0,K = 1;K < N;++L,K *= 2) X ^= (X ^ (X >> K)) & TYPE_bitinrangepublicpos_mask(S,L); #endif return X; } # same as (X >> (S % N)) but without assuming barrel shifter UNSIGNED: TYPE TYPE_shrmod(TYPE X,TYPE S) { #if amd64 8: S &= 7; 8: readasm("amd64; int8 X S; X unsigned>>= S"); 16: S &= 15; 16: readasm("amd64; int16 X S; X unsigned>>= S"); 32: readasm("amd64; int32 X S; X unsigned>>= S"); 64: readasm("amd64; int64 X S; X unsigned>>= S"); #elif arm64 8: readasm("arm64; int8 X S; S = S & 7; X = (uint8) X; X = X unsigned>> S"); 16: readasm("arm64; int16 X S; S = S & 15; X = (uint16) X; X = X unsigned>> S"); 32: readasm("arm64; int32 X S; X = X unsigned>> S"); 64: readasm("arm64; int64 X S; X = X unsigned>> S"); #else int K, L; for (L = 0,K = 1;K < N;++L,K *= 2) X ^= (X ^ (X >> K)) & TYPE_bitinrangepublicpos_mask(S,L); #endif return X; } TYPE TYPE_bitmod_mask(TYPE X,TYPE S) { X = TYPE_shrmod(X,S); return TYPE_bottombit_mask(X); } TYPE TYPE_bitmod_01(TYPE X,TYPE S) { X = TYPE_shrmod(X,S); return TYPE_bottombit_01(X); } TYPE TYPE_nonzero_mask(TYPE X) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Z32; Z32 = 0; Q32 = -1; X32 & (int8) X32; Z32 = Q32 if !="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Z; Z = 0; Q = -1; X & X; Z = Q if !="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Z; Z = 0; Q = -1; X & X; Z = Q if !="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Z; Z = 0; Q = -1; X & X; Z = Q if !="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Z; X & 255; Z = -1 if != else 0"); 16: readasm("arm64; int16 X Z; X & 65535; Z = -1 if != else 0"); 32: readasm("arm64; int32 X Z; X - 0; Z = -1 if != else 0"); 64: readasm("arm64; int64 X Z; X - 0; Z = -1 if != else 0"); return Z; #else X |= -X; return SIGNED_negative_mask(X); #endif } # XXX: for amd64, use set instead of cmov TYPE TYPE_nonzero_01(TYPE X) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Z32; Z32 = 0; Q32 = 1; X32 & (int8) X32; Z32 = Q32 if !="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Z; Z = 0; Q = 1; X & X; Z = Q if !="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Z; Z = 0; Q = 1; X & X; Z = Q if !="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Z; Z = 0; Q = 1; X & X; Z = Q if !="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Z; X & 255; Z = 1 if != else 0"); 16: readasm("arm64; int16 X Z; X & 65535; Z = 1 if != else 0"); 32: readasm("arm64; int32 X Z; X - 0; Z = 1 if != else 0"); 64: readasm("arm64; int64 X Z; X - 0; Z = 1 if != else 0"); return Z; #else X |= -X; return UNSIGNED_topbit_01(X); #endif } SIGNED: TYPE TYPE_positive_mask(TYPE X) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Z32; Z32 = 0; Q32 = -1; X32 & (int8) X32; Z32 = Q32 if signed>"); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Z; Z = 0; Q = -1; X & X; Z = Q if signed>"); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Z; Z = 0; Q = -1; X & X; Z = Q if signed>"); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Z; Z = 0; Q = -1; X & X; Z = Q if signed>"); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Z; Z = (int8) X; Z - 0; Z = -1 if signed> else 0"); 16: readasm("arm64; int16 X Z; Z = (int16) X; Z - 0; Z = -1 if signed> else 0"); 32: readasm("arm64; int32 X Z; X - 0; Z = -1 if signed> else 0"); 64: readasm("arm64; int64 X Z; X - 0; Z = -1 if signed> else 0"); return Z; #else TYPE Z = -X; Z ^= X & Z; return TYPE_negative_mask(Z); #endif } SIGNED: TYPE TYPE_positive_01(TYPE X) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Z32; Z32 = 0; Q32 = 1; X32 & (int8) X32; Z32 = Q32 if signed>"); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Z; Z = 0; Q = 1; X & X; Z = Q if signed>"); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Z; Z = 0; Q = 1; X & X; Z = Q if signed>"); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Z; Z = 0; Q = 1; X & X; Z = Q if signed>"); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Z; Z = (int8) X; Z - 0; Z = 1 if signed> else 0"); 16: readasm("arm64; int16 X Z; Z = (int16) X; Z - 0; Z = 1 if signed> else 0"); 32: readasm("arm64; int32 X Z; X - 0; Z = 1 if signed> else 0"); 64: readasm("arm64; int64 X Z; X - 0; Z = 1 if signed> else 0"); return Z; #else TYPE Z = -X; Z ^= X & Z; return UNSIGNED_topbit_01(Z); #endif } TYPE TYPE_zero_mask(TYPE X) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Z32; Z32 = 0; Q32 = -1; X32 & (int8) X32; Z32 = Q32 if ="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Z; Z = 0; Q = -1; X & X; Z = Q if ="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Z; Z = 0; Q = -1; X & X; Z = Q if ="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Z; Z = 0; Q = -1; X & X; Z = Q if ="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Z; X & 255; Z = -1 if = else 0"); 16: readasm("arm64; int16 X Z; X & 65535; Z = -1 if = else 0"); 32: readasm("arm64; int32 X Z; X - 0; Z = -1 if = else 0"); 64: readasm("arm64; int64 X Z; X - 0; Z = -1 if = else 0"); return Z; #else return ~TYPE_nonzero_mask(X); #endif } TYPE TYPE_zero_01(TYPE X) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Z32; Z32 = 0; Q32 = 1; X32 & (int8) X32; Z32 = Q32 if ="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Z; Z = 0; Q = 1; X & X; Z = Q if ="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Z; Z = 0; Q = 1; X & X; Z = Q if ="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Z; Z = 0; Q = 1; X & X; Z = Q if ="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Z; X & 255; Z = 1 if = else 0"); 16: readasm("arm64; int16 X Z; X & 65535; Z = 1 if = else 0"); 32: readasm("arm64; int32 X Z; X - 0; Z = 1 if = else 0"); 64: readasm("arm64; int64 X Z; X - 0; Z = 1 if = else 0"); return Z; #else return 1-TYPE_nonzero_01(X); #endif } TYPE TYPE_unequal_mask(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = -1; X32 - (int8) Y32; Z32 = Q32 if !="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if !="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if !="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if !="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (uint8) X; Z - (uint8) Y; Z = -1 if != else 0"); 16: readasm("arm64; int16 X Y Z; Z = (uint16) X; Z - (uint16) Y; Z = -1 if != else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = -1 if != else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = -1 if != else 0"); return Z; #else return TYPE_nonzero_mask(X ^ Y); #endif } TYPE TYPE_unequal_01(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = 1; X32 - (int8) Y32; Z32 = Q32 if !="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if !="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if !="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if !="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (uint8) X; Z - (uint8) Y; Z = 1 if != else 0"); 16: readasm("arm64; int16 X Y Z; Z = (uint16) X; Z - (uint16) Y; Z = 1 if != else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = 1 if != else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = 1 if != else 0"); return Z; #else return TYPE_nonzero_01(X ^ Y); #endif } TYPE TYPE_equal_mask(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = -1; X32 - (int8) Y32; Z32 = Q32 if ="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if ="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if ="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if ="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (uint8) X; Z - (uint8) Y; Z = -1 if = else 0"); 16: readasm("arm64; int16 X Y Z; Z = (uint16) X; Z - (uint16) Y; Z = -1 if = else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = -1 if = else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = -1 if = else 0"); return Z; #else return ~TYPE_unequal_mask(X,Y); #endif } TYPE TYPE_equal_01(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = 1; X32 - (int8) Y32; Z32 = Q32 if ="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if ="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if ="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if ="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (uint8) X; Z - (uint8) Y; Z = 1 if = else 0"); 16: readasm("arm64; int16 X Y Z; Z = (uint16) X; Z - (uint16) Y; Z = 1 if = else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = 1 if = else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = 1 if = else 0"); return Z; #else return 1-TYPE_unequal_01(X,Y); #endif } SIGNED: TYPE TYPE_min(TYPE X,TYPE Y) { #if amd64 8: int32_t X32 = X,Y32 = Y; 8: readasm("amd64; int32 X32 Y32; X32 - (int8) Y32; X32 = Y32 if signed>"); 8: X = X32; 16: readasm("amd64; int16 X Y; X - Y; X = Y if signed>"); 32: readasm("amd64; int32 X Y; X - Y; X = Y if signed>"); 64: readasm("amd64; int64 X Y; X - Y; X = Y if signed>"); return X; #elif arm64 8: readasm("arm64; int8 X Y; X = (int8) X; X - (int8) Y; X = X if signed< else Y"); 16: readasm("arm64; int16 X Y; X = (int16) X; X - (int16) Y; X = X if signed< else Y"); 32: readasm("arm64; int32 X Y; X - Y; X = X if signed< else Y"); 64: readasm("arm64; int64 X Y; X - Y; X = X if signed< else Y"); return X; #else TYPE R = Y ^ X; TYPE Z = Y - X; Z ^= R & (Z ^ Y); Z = TYPE_negative_mask(Z); Z &= R; return X ^ Z; #endif } UNSIGNED: TYPE TYPE_min(TYPE X,TYPE Y) { #if amd64 8: uint32_t X32 = X,Y32 = Y; 8: readasm("amd64; int32 X32 Y32; X32 - (int8) Y32; X32 = Y32 if unsigned>"); 8: X = X32; 16: readasm("amd64; int16 X Y; X - Y; X = Y if unsigned>"); 32: readasm("amd64; int32 X Y; X - Y; X = Y if unsigned>"); 64: readasm("amd64; int64 X Y; X - Y; X = Y if unsigned>"); return X; #elif arm64 8: readasm("arm64; int8 X Y; X = (uint8) X; X - (uint8) Y; X = X if unsigned< else Y"); 16: readasm("arm64; int16 X Y; X = (uint16) X; X - (uint16) Y; X = X if unsigned< else Y"); 32: readasm("arm64; int32 X Y; X - Y; X = X if unsigned< else Y"); 64: readasm("arm64; int64 X Y; X - Y; X = X if unsigned< else Y"); return X; #else TYPE R = Y ^ X; TYPE Z = Y - X; Z ^= R & (Z ^ Y ^ (((TYPE) 1) << (N-1))); Z = SIGNED_negative_mask(Z); Z &= R; return X ^ Z; #endif } SIGNED: TYPE TYPE_max(TYPE X,TYPE Y) { #if amd64 8: int32_t X32 = X,Y32 = Y; 8: readasm("amd64; int32 X32 Y32; X32 - (int8) Y32; X32 = Y32 if signed<"); 8: X = X32; 16: readasm("amd64; int16 X Y; X - Y; X = Y if signed<"); 32: readasm("amd64; int32 X Y; X - Y; X = Y if signed<"); 64: readasm("amd64; int64 X Y; X - Y; X = Y if signed<"); return X; #elif arm64 8: readasm("arm64; int8 X Y; X = (int8) X; X - (int8) Y; X = Y if signed< else X"); 16: readasm("arm64; int16 X Y; X = (int16) X; X - (int16) Y; X = Y if signed< else X"); 32: readasm("arm64; int32 X Y; X - Y; X = Y if signed< else X"); 64: readasm("arm64; int64 X Y; X - Y; X = Y if signed< else X"); return X; #else TYPE R = Y ^ X; TYPE Z = Y - X; Z ^= R & (Z ^ Y); Z = TYPE_negative_mask(Z); Z &= R; return Y ^ Z; #endif } UNSIGNED: TYPE TYPE_max(TYPE X,TYPE Y) { #if amd64 8: uint32_t X32 = X,Y32 = Y; 8: readasm("amd64; int32 X32 Y32; X32 - (int8) Y32; X32 = Y32 if unsigned<"); 8: X = X32; 16: readasm("amd64; int16 X Y; X - Y; X = Y if unsigned<"); 32: readasm("amd64; int32 X Y; X - Y; X = Y if unsigned<"); 64: readasm("amd64; int64 X Y; X - Y; X = Y if unsigned<"); return X; #elif arm64 8: readasm("arm64; int8 X Y; X = (uint8) X; X - (uint8) Y; X = Y if unsigned< else X"); 16: readasm("arm64; int16 X Y; X = (uint16) X; X - (uint16) Y; X = Y if unsigned< else X"); 32: readasm("arm64; int32 X Y; X - Y; X = Y if unsigned< else X"); 64: readasm("arm64; int64 X Y; X - Y; X = Y if unsigned< else X"); return X; #else TYPE R = Y ^ X; TYPE Z = Y - X; Z ^= R & (Z ^ Y ^ (((TYPE) 1) << (N-1))); Z = SIGNED_negative_mask(Z); Z &= R; return Y ^ Z; #endif } SIGNED: void TYPE_minmax(TYPE *P,TYPE *Q) { TYPE X = *P; TYPE Y = *Q; #if amd64 8: int32_t X32 = X,Y32 = Y,Z32; 8: readasm("amd64; int32 X32 Y32 Z32; X32 - (int8) Y32; Z32 = X32; X32 = Y32 if signed>; Y32 = Z32 if signed>"); 8: X = X32; Y = Y32; 16: TYPE Z; 16: readasm("amd64; int16 X Y Z; X - Y; Z = X; X = Y if signed>; Y = Z if signed>"); 32: TYPE Z; 32: readasm("amd64; int32 X Y Z; X - Y; Z = X; X = Y if signed>; Y = Z if signed>"); 64: TYPE Z; 64: readasm("amd64; int64 X Y Z; X - Y; Z = X; X = Y if signed>; Y = Z if signed>"); *P = X; *Q = Y; #elif arm64 TYPE R, S; 8: readasm("arm64; int8 X Y R S; X = (int8) X; X - (int8) Y; R = X if signed< else Y; S = Y if signed< else X"); 16: readasm("arm64; int16 X Y R S; X = (int16) X; X - (int16) Y; R = X if signed< else Y; S = Y if signed< else X"); 32: readasm("arm64; int32 X Y R S; X - Y; R = X if signed< else Y; S = Y if signed< else X"); 64: readasm("arm64; int64 X Y R S; X - Y; R = X if signed< else Y; S = Y if signed< else X"); *P = R; *Q = S; #else TYPE R = Y ^ X; TYPE Z = Y - X; Z ^= R & (Z ^ Y); Z = TYPE_negative_mask(Z); Z &= R; X ^= Z; Y ^= Z; *P = X; *Q = Y; #endif } UNSIGNED: void TYPE_minmax(TYPE *P,TYPE *Q) { TYPE X = *P; TYPE Y = *Q; #if amd64 8: uint32_t X32 = X,Y32 = Y,Z32; 8: readasm("amd64; int32 X32 Y32 Z32; X32 - (int8) Y32; Z32 = X32; X32 = Y32 if unsigned>; Y32 = Z32 if unsigned>"); 8: X = X32; Y = Y32; 16: TYPE Z; 16: readasm("amd64; int16 X Y Z; X - Y; Z = X; X = Y if unsigned>; Y = Z if unsigned>"); 32: TYPE Z; 32: readasm("amd64; int32 X Y Z; X - Y; Z = X; X = Y if unsigned>; Y = Z if unsigned>"); 64: TYPE Z; 64: readasm("amd64; int64 X Y Z; X - Y; Z = X; X = Y if unsigned>; Y = Z if unsigned>"); *P = X; *Q = Y; #elif arm64 TYPE R, S; 8: readasm("arm64; int8 X Y R S; X = (uint8) X; X - (uint8) Y; R = X if unsigned< else Y; S = Y if unsigned< else X"); 16: readasm("arm64; int16 X Y R S; X = (uint16) X; X - (uint16) Y; R = X if unsigned< else Y; S = Y if unsigned< else X"); 32: readasm("arm64; int32 X Y R S; X - Y; R = X if unsigned< else Y; S = Y if unsigned< else X"); 64: readasm("arm64; int64 X Y R S; X - Y; R = X if unsigned< else Y; S = Y if unsigned< else X"); *P = R; *Q = S; #else TYPE R = Y ^ X; TYPE Z = Y - X; Z ^= R & (Z ^ Y ^ (((TYPE) 1) << (N-1))); Z = SIGNED_negative_mask(Z); Z &= R; X ^= Z; Y ^= Z; *P = X; *Q = Y; #endif } SIGNED: TYPE TYPE_smaller_mask(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = -1; X32 - (int8) Y32; Z32 = Q32 if signed<"); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if signed<"); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if signed<"); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if signed<"); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (int8) X; Z - (int8) Y; Z = -1 if signed< else 0"); 16: readasm("arm64; int16 X Y Z; Z = (int16) X; Z - (int16) Y; Z = -1 if signed< else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = -1 if signed< else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = -1 if signed< else 0"); return Z; #else TYPE R = X ^ Y; TYPE Z = X - Y; Z ^= R & (Z ^ X); return TYPE_negative_mask(Z); #endif } SIGNED: TYPE TYPE_smaller_01(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = 1; X32 - (int8) Y32; Z32 = Q32 if signed<"); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if signed<"); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if signed<"); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if signed<"); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (int8) X; Z - (int8) Y; Z = 1 if signed< else 0"); 16: readasm("arm64; int16 X Y Z; Z = (int16) X; Z - (int16) Y; Z = 1 if signed< else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = 1 if signed< else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = 1 if signed< else 0"); return Z; #else TYPE R = X ^ Y; TYPE Z = X - Y; Z ^= R & (Z ^ X); return UNSIGNED_topbit_01(Z); #endif } UNSIGNED: TYPE TYPE_smaller_mask(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = -1; X32 - (int8) Y32; Z32 = Q32 if unsigned<"); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if unsigned<"); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if unsigned<"); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if unsigned<"); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (uint8) X; Z - (uint8) Y; Z = -1 if unsigned< else 0"); 16: readasm("arm64; int16 X Y Z; Z = (uint16) X; Z - (uint16) Y; Z = -1 if unsigned< else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = -1 if unsigned< else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = -1 if unsigned< else 0"); return Z; #else TYPE R = X ^ Y; TYPE Z = X - Y; Z ^= R & (Z ^ X ^ (((TYPE) 1) << (N-1))); return SIGNED_negative_mask(Z); #endif } UNSIGNED: TYPE TYPE_smaller_01(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = 1; X32 - (int8) Y32; Z32 = Q32 if unsigned<"); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if unsigned<"); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if unsigned<"); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if unsigned<"); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (uint8) X; Z - (uint8) Y; Z = 1 if unsigned< else 0"); 16: readasm("arm64; int16 X Y Z; Z = (uint16) X; Z - (uint16) Y; Z = 1 if unsigned< else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = 1 if unsigned< else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = 1 if unsigned< else 0"); return Z; #else TYPE R = X ^ Y; TYPE Z = X - Y; Z ^= R & (Z ^ X ^ (((TYPE) 1) << (N-1))); return UNSIGNED_topbit_01(Z); #endif } SIGNED: TYPE TYPE_leq_mask(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = -1; X32 - (int8) Y32; Z32 = Q32 if signed<="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if signed<="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if signed<="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if signed<="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (int8) X; Z - (int8) Y; Z = -1 if signed<= else 0"); 16: readasm("arm64; int16 X Y Z; Z = (int16) X; Z - (int16) Y; Z = -1 if signed<= else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = -1 if signed<= else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = -1 if signed<= else 0"); return Z; #else return ~TYPE_smaller_mask(Y,X); #endif } SIGNED: TYPE TYPE_leq_01(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = 1; X32 - (int8) Y32; Z32 = Q32 if signed<="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if signed<="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if signed<="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if signed<="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (int8) X; Z - (int8) Y; Z = 1 if signed<= else 0"); 16: readasm("arm64; int16 X Y Z; Z = (int16) X; Z - (int16) Y; Z = 1 if signed<= else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = 1 if signed<= else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = 1 if signed<= else 0"); return Z; #else return 1-TYPE_smaller_01(Y,X); #endif } UNSIGNED: TYPE TYPE_leq_mask(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = -1; X32 - (int8) Y32; Z32 = Q32 if unsigned<="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if unsigned<="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if unsigned<="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = -1; X - Y; Z = Q if unsigned<="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (uint8) X; Z - (uint8) Y; Z = -1 if unsigned<= else 0"); 16: readasm("arm64; int16 X Y Z; Z = (uint16) X; Z - (uint16) Y; Z = -1 if unsigned<= else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = -1 if unsigned<= else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = -1 if unsigned<= else 0"); return Z; #else return ~TYPE_smaller_mask(Y,X); #endif } UNSIGNED: TYPE TYPE_leq_01(TYPE X,TYPE Y) { #if amd64 8: TYPE Z; 8: int32_t X32 = X,Y32 = Y,Q32,Z32; 8: readasm("amd64; int32 X32 Q32 Y32 Z32; Z32 = 0; Q32 = 1; X32 - (int8) Y32; Z32 = Q32 if unsigned<="); 8: Z = Z32; 16: TYPE Q,Z; 16: readasm("amd64; int16 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if unsigned<="); 32: TYPE Q,Z; 32: readasm("amd64; int32 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if unsigned<="); 64: TYPE Q,Z; 64: readasm("amd64; int64 X Q Y Z; Z = 0; Q = 1; X - Y; Z = Q if unsigned<="); return Z; #elif arm64 TYPE Z; 8: readasm("arm64; int8 X Y Z; Z = (uint8) X; Z - (uint8) Y; Z = 1 if unsigned<= else 0"); 16: readasm("arm64; int16 X Y Z; Z = (uint16) X; Z - (uint16) Y; Z = 1 if unsigned<= else 0"); 32: readasm("arm64; int32 X Y Z; X - Y; Z = 1 if unsigned<= else 0"); 64: readasm("arm64; int64 X Y Z; X - Y; Z = 1 if unsigned<= else 0"); return Z; #else return 1-TYPE_smaller_01(Y,X); #endif } # XXX: consider *0x...10101 for the denouement _if_ mul is constant-time # XXX: use popcnt if __SSE4_2__ # XXX: use cnt if arm64 cssc # XXX: maybe use neon cnt if arm64 int TYPE_ones_num(TYPE X) { UNSIGNED Y = X; 8: const TYPE C0 = 0x55; 16: const TYPE C0 = 0x5555; 32: const TYPE C0 = 0x55555555; 64: const TYPE C0 = 0x5555555555555555; 8: const TYPE C1 = 0x33; 16: const TYPE C1 = 0x3333; 32: const TYPE C1 = 0x33333333; 64: const TYPE C1 = 0x3333333333333333; 8: const TYPE C2 = 0x0f; 16: const TYPE C2 = 0x0f0f; 32: const TYPE C2 = 0x0f0f0f0f; 64: const TYPE C2 = 0x0f0f0f0f0f0f0f0f; Y -= ((Y >> 1) & C0); Y = (Y & C1) + ((Y >> 2) & C1); Y = (Y + (Y >> 4)) & C2; 16: Y = (Y + (Y >> 8)) & 0xff; 32: Y += Y >> 8; 32: Y = (Y + (Y >> 16)) & 0xff; 64: Y += Y >> 8; 64: Y += Y >> 16; 64: Y = (Y + (Y >> 32)) & 0xff; return Y; } # XXX: use tzcnt if amd64 bmi1 # XXX: use ctz if arm64 cssc int TYPE_bottomzeros_num(TYPE X) { #if amd64 8: int32_t fallback = N; 8: int32_t X32 = X; 8: readasm("amd64; int32 X32 fallback; X32 = numbottomzeros_tricky X32; X32 = fallback if ="); 8: X = X32; 16: TYPE fallback = N; 16: readasm("amd64; int16 X fallback; X = numbottomzeros_tricky X; X = fallback if ="); 32: TYPE fallback = N; 32: readasm("amd64; int32 X fallback; X = numbottomzeros_tricky X; X = fallback if ="); 64: TYPE fallback = N; 64: readasm("amd64; int64 X fallback; X = numbottomzeros_tricky X; X = fallback if ="); return X; #elif arm64 int64_t Y; 8: readasm("arm64; int8 X Y; Y = X | -256; Y = bitrev32 Y; Y = numbottomzeros Y"); 16: readasm("arm64; int16 X Y; Y = X | -65536; Y = bitrev32 Y; Y = numbottomzeros Y"); 32: readasm("arm64; int32 X Y; Y = bitrev32 X; Y = numbottomzeros Y"); 64: readasm("arm64; int64 X Y; Y = bitrev64 X; Y = numbottomzeros Y"); return Y; #else TYPE Y = X ^ (X-1); Y = ((SIGNED) Y) >> 1; Y &= ~(X & (((TYPE) 1) << (N-1))); return TYPE_ones_num(Y); #endif } dq-20250201/cryptoint/int16_optblocker.c000066400000000000000000000002251474734131400177460ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_int16.h" volatile crypto_int16 crypto_int16_optblocker = 0; dq-20250201/cryptoint/int32_optblocker.c000066400000000000000000000002251474734131400177440ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_int32.h" volatile crypto_int32 crypto_int32_optblocker = 0; dq-20250201/cryptoint/int64_optblocker.c000066400000000000000000000002251474734131400177510ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_int64.h" volatile crypto_int64 crypto_int64_optblocker = 0; dq-20250201/cryptoint/int8_optblocker.c000066400000000000000000000002221474734131400176640ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_int8.h" volatile crypto_int8 crypto_int8_optblocker = 0; dq-20250201/cryptoint/intN_optblocker.c000066400000000000000000000002221474734131400177120ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_intN.h" volatile crypto_intN crypto_intN_optblocker = 0; dq-20250201/cryptoint/namespace000066400000000000000000000000031474734131400162660ustar00rootroot00000000000000dq dq-20250201/cryptoint/readasm.py000077500000000000000000000446631474734131400164240ustar00rootroot00000000000000#!/usr/bin/env python3 import re import sys machinedesc = {} # note for qhasm users: # the machine descriptions here have, e.g., # int8 meaning where the C ABI stores an int8, # such as a 64-bit integer register on amd64 # ===== amd64 machinedesc['amd64'] = r''' int8 r:var/r=int8: int16 r:var/r=int16: int32 r:var/r=int32: int64 r:var/r=int64: int8 r s:var/r=int8:var/s=int8: int16 r s:var/r=int16:var/s=int16: int32 r s:var/r=int32:var/s=int32: int64 r s:var/r=int64:var/s=int64: int8 r s t:var/r=int8:var/s=int8:var/t=int8: int16 r s t:var/r=int16:var/s=int16:var/t=int16: int32 r s t:var/r=int32:var/s=int32:var/t=int32: int64 r s t:var/r=int64:var/s=int64:var/t=int64: int8 r s t u:var/r=int8:var/s=int8:var/t=int8:var/u=int8: int16 r s t u:var/r=int16:var/s=int16:var/t=int16:var/u=int16: int32 r s t u:var/r=int32:var/s=int32:var/t=int32:var/u=int32: int64 r s t u:var/r=int64:var/s=int64:var/t=int64:var/u=int64: r = 0:>r=int8:asm/xorb >r,>r:>?cc: r = 0:>r=int16:asm/xorw >r,>r:>?cc: r = 0:>r=int32:asm/xorl >r,>r:>?cc: r = 0:>r=int64:asm/xorq >r,>r:>?cc: r = n:>r=int8:#n:asm/movb $#n,>r: r = n:>r=int16:#n:asm/movw $#n,>r: r = n:>r=int32:#n:asm/movl $#n,>r: r = n:>r=int64:#n:asm/movq $#n,>r: r = -n:>r=int8:#n:asm/movb $-#n,>r: r = -n:>r=int16:#n:asm/movw $-#n,>r: r = -n:>r=int32:#n:asm/movl $-#n,>r: r = -n:>r=int64:#n:asm/movq $-#n,>r: r signed>>= n:+r=int8:#n:asm/sarb $#n,+r:>?cc: r signed>>= n:+r=int16:#n:asm/sarw $#n,+r:>?cc: r signed>>= n:+r=int32:#n:asm/sarl $#n,+r:>?cc: r signed>>= n:+r=int64:#n:asm/sarq $#n,+r:>?cc: r signed>>= s:+r=int8:?cc: r signed>>= s:+r=int16:?cc: r signed>>= s:+r=int32:?cc: r signed>>= s:+r=int64:?cc: r unsigned>>= n:+r=int8:#n:asm/shrb $#n,+r:>?cc: r unsigned>>= n:+r=int16:#n:asm/shrw $#n,+r:>?cc: r unsigned>>= n:+r=int32:#n:asm/shrl $#n,+r:>?cc: r unsigned>>= n:+r=int64:#n:asm/shrq $#n,+r:>?cc: r unsigned>>= s:+r=int8:?cc: r unsigned>>= s:+r=int16:?cc: r unsigned>>= s:+r=int32:?cc: r unsigned>>= s:+r=int64:?cc: r <<= n:+r=int8:#n:asm/shlb $#n,+r:>?cc: r <<= n:+r=int16:#n:asm/shlw $#n,+r:>?cc: r <<= n:+r=int32:#n:asm/shll $#n,+r:>?cc: r <<= n:+r=int64:#n:asm/shlq $#n,+r:>?cc: r <<= s:+r=int8:?cc: r <<= s:+r=int16:?cc: r <<= s:+r=int32:?cc: r <<= s:+r=int64:?cc: r &= n:+r=int8:#n:asm/andb $#n,+r:>?cc: r &= n:+r=int16:#n:asm/andw $#n,+r:>?cc: r &= n:+r=int32:#n:asm/andl $#n,+r:>?cc: r &= n:+r=int64:#n:asm/andq $#n,+r:>?cc: r & (int8) s:?cc: r & s:?cc: r & s:?cc: r & s:?cc: r - (int8) s:?cc: r - s:?cc: r - s:?cc: r - s:?cc: r = s:>r=int16:r: r = s:>r=int32:r: r = s:>r=int64:r: r = s if signed<:+r=int16::+r=int16::+r=int32::+r=int64::+r=int16::+r=int32::+r=int64:r=int16:?cc:asm/bsfw r: r = numbottomzeros_tricky s:>r=int32:?cc:asm/bsfl r: r = numbottomzeros_tricky s:>r=int64:?cc:asm/bsfq r: ''' # ===== arm64 machinedesc['arm64'] = r''' int8 r:var/r=int8: int16 r:var/r=int16: int32 r:var/r=int32: int64 r:var/r=int64: int8 r s:var/r=int8:var/s=int8: int16 r s:var/r=int16:var/s=int16: int32 r s:var/r=int32:var/s=int32: int64 r s:var/r=int64:var/s=int64: int8 r s t:var/r=int8:var/s=int8:var/t=int8: int16 r s t:var/r=int16:var/s=int16:var/t=int16: int32 r s t:var/r=int32:var/s=int32:var/t=int32: int64 r s t:var/r=int64:var/s=int64:var/t=int64: int8 r s t u:var/r=int8:var/s=int8:var/t=int8:var/u=int8: int16 r s t u:var/r=int16:var/s=int16:var/t=int16:var/u=int16: int32 r s t u:var/r=int32:var/s=int32:var/t=int32:var/u=int32: int64 r s t u:var/r=int64:var/s=int64:var/t=int64:var/u=int64: r = s signed>> n:>r=int32:r,> n:>r=int64:r,> n:>r=int32:r,> n:>r=int64:r,> t:>r=int8:r,> t:>r=int16:r,> t:>r=int32:r,> t:>r=int64:r,> t:>r=int8:r,> t:>r=int16:r,> t:>r=int32:r,> t:>r=int64:r,r=int8:r,r=int16:r,r=int32:r,r=int64:r,r=int8:r,r=int16:r,r=int8:r,r=int16:r,r=int8:r,r=int8:r,r=int16:r,r=int16:r,r=int8:r,r=int16:r,?cc: r & n:?cc: r - n:?cc: r - n:?cc: r - n:?cc: r - n:?cc: r - s:?cc: r - s:?cc: r - s:?cc: r - s:?cc: r - (int8) s:?cc: r - (uint8) s:?cc: r - (int16) s:?cc: r - (uint16) s:?cc: r = -1 if != else 0:>r=int8:r,ne: r = -1 if != else 0:>r=int16:r,ne: r = -1 if != else 0:>r=int32:r,ne: r = -1 if != else 0:>r=int64:r,ne: r = -1 if = else 0:>r=int8:r,eq: r = -1 if = else 0:>r=int16:r,eq: r = -1 if = else 0:>r=int32:r,eq: r = -1 if = else 0:>r=int64:r,eq: r = -1 if signed< else 0:>r=int8:r,lt: r = -1 if signed< else 0:>r=int16:r,lt: r = -1 if signed< else 0:>r=int32:r,lt: r = -1 if signed< else 0:>r=int64:r,lt: r = -1 if signed> else 0:>r=int8:r,gt: r = -1 if signed> else 0:>r=int16:r,gt: r = -1 if signed> else 0:>r=int32:r,gt: r = -1 if signed> else 0:>r=int64:r,gt: r = -1 if signed<= else 0:>r=int8:r,le: r = -1 if signed<= else 0:>r=int16:r,le: r = -1 if signed<= else 0:>r=int32:r,le: r = -1 if signed<= else 0:>r=int64:r,le: r = -1 if unsigned< else 0:>r=int8:r,lo: r = -1 if unsigned< else 0:>r=int16:r,lo: r = -1 if unsigned< else 0:>r=int32:r,lo: r = -1 if unsigned< else 0:>r=int64:r,lo: r = -1 if unsigned<= else 0:>r=int8:r,ls: r = -1 if unsigned<= else 0:>r=int16:r,ls: r = -1 if unsigned<= else 0:>r=int32:r,ls: r = -1 if unsigned<= else 0:>r=int64:r,ls: r = 1 if != else 0:>r=int8:r,ne: r = 1 if != else 0:>r=int16:r,ne: r = 1 if != else 0:>r=int32:r,ne: r = 1 if != else 0:>r=int64:r,ne: r = 1 if = else 0:>r=int8:r,eq: r = 1 if = else 0:>r=int16:r,eq: r = 1 if = else 0:>r=int32:r,eq: r = 1 if = else 0:>r=int64:r,eq: r = 1 if signed< else 0:>r=int8:r,lt: r = 1 if signed< else 0:>r=int16:r,lt: r = 1 if signed< else 0:>r=int32:r,lt: r = 1 if signed< else 0:>r=int64:r,lt: r = 1 if signed> else 0:>r=int8:r,gt: r = 1 if signed> else 0:>r=int16:r,gt: r = 1 if signed> else 0:>r=int32:r,gt: r = 1 if signed> else 0:>r=int64:r,gt: r = 1 if signed<= else 0:>r=int8:r,le: r = 1 if signed<= else 0:>r=int16:r,le: r = 1 if signed<= else 0:>r=int32:r,le: r = 1 if signed<= else 0:>r=int64:r,le: r = 1 if unsigned< else 0:>r=int8:r,lo: r = 1 if unsigned< else 0:>r=int16:r,lo: r = 1 if unsigned< else 0:>r=int32:r,lo: r = 1 if unsigned< else 0:>r=int64:r,lo: r = 1 if unsigned<= else 0:>r=int8:r,ls: r = 1 if unsigned<= else 0:>r=int16:r,ls: r = 1 if unsigned<= else 0:>r=int32:r,ls: r = 1 if unsigned<= else 0:>r=int64:r,ls: r = s if signed< else t:>r=int8:r,r=int16:r,r=int32:r,r=int64:r,r=int8:r,r=int16:r,r=int32:r,r=int64:r,r=int8:r,r=int16:r,r=int32:r,r=int64:r,r=int8:r,r=int16:r,r=int32:r,r=int64:r,> n)):>r=int8:r,> n)):>r=int16:r,> n)):>r=int32:r,> n)):>r=int64:r,> n):>r=int8:r,> n):>r=int16:r,> n):>r=int32:r,> n):>r=int64:r,'): if x[1:] == '?cc': if x[0] != '<': insnwrite += ['?cc'] if x[0] != '>': insnread += ['?cc'] continue if x[0] != '<': insnwrite += [x[1]] if x[0] != '>': insnread += [x[1]] insnvars += [x[1]] assert x[2] == '=' insnvartype[x[1]] = x[3:].split('#')[0] if len(x[3:].split('#')) > 1: insnvarpos[x[1]] = int(x[3:].split('#')[1]) continue if x.startswith('var/'): insnvars += [x[4]] assert x[5] == '=' insnvartype[x[4]] = x[6:] insnmakevars += [x[4]] continue if x.startswith('asm/'): insnasm += [x[4:]] continue raise Exception(x) insn = tokens(insndesc[0]) insnpos = 0 linepos = 0 while True: if insnpos == len(insn) and linepos == len(line): foundinsn = True if insnpos == len(insn) or linepos == len(line): break x = insn[insnpos] y = line[linepos] if x in insnvars: if y in vartype: if vartype[y] != insnvartype[x]: break # line and insn both have variables, but different types if x in insnvar2line: if insnvar2line[x] != y: break # insn variable already assigned to something different in line else: insnvar2line[x] = y else: if not isvarname(y): break # not an acceptable variable name insnvar2line[x] = y else: if y in vartype: break # XXX: but maybe sometimes want to allow variable in line to match literal in insn if y != x: break # non-matching literals insnpos += 1 linepos += 1 if foundinsn: break if not foundinsn: print(line) assert foundinsn insnvar2line['?cc'] = '?cc' for v in insnmakevars: w = insnvar2line[v] vartype[w] = insnvartype[v] for v in insnvar2line: if v == '?cc': continue if insnvartype[v] == '#': continue w = insnvar2line[v] if w not in vartype: raise Exception(f'undefined variable {w}') for v in insnread: w = insnvar2line[v] if v in insnvarpos: assert insnvarpos[v] == 4 assert machine == 'amd64' rcxvars.add(w) if w not in outputvars: if w not in inputvars: inputvars += [w] for z in outputvars: earlyclobber.add(z) for v in insnwrite: w = insnvar2line[v] if w not in outputvars: outputvars += [w] for asm in insnasm: todo += [(asm,insnvar2line)] need8 = set() todo2 = [] for asm,insnvar2line in todo: todo2 += [('asm',)] while len(asm) > 0: if asm[0] in ('<','+','>'): if asm[2:].startswith('%8'): todo2 += [('var8',insnvar2line[asm[1]])] need8.add(insnvar2line[asm[1]]) asm = asm[4:] else: todo2 += [('var',insnvar2line[asm[1]])] asm = asm[2:] elif asm[0] == '#': todo2 += [('str',insnvar2line[asm[1]])] asm = asm[2:] else: todo2 += [('str',asm[0])] asm = asm[1:] varnextpos = 0 var2name = {} outputs = [] for w in outputvars: if w == '?cc': continue if machine == 'arm64' and vartype[w] != 'int64': # XXX: abstract this var2name[w] = f'%w{varnextpos}' else: var2name[w] = f'%{varnextpos}' varnextpos += 1 regtype = 'r' if w in need8: regtype = 'q' if w in rcxvars: regtype = 'c' if w in earlyclobber: regtype = '&'+regtype if w in inputvars: outputs += [f'"+{regtype}"({w})'] else: outputs += [f'"={regtype}"({w})'] inputs = [] for w in inputvars: if w in outputvars: continue assert w != '?cc' if machine == 'arm64' and vartype[w] != 'int64': # XXX: abstract this var2name[w] = f'%w{varnextpos}' else: var2name[w] = f'%{varnextpos}' varnextpos += 1 regtype = 'r' if w in need8: regtype = 'q' if w in rcxvars: regtype = 'c' inputs += [f'"{regtype}"({w})'] clobbers = [] for w in outputvars: if w != '?cc': continue clobbers += ['"cc"'] result = '' for T in todo2: if T[0] == 'asm': if result != '': result += '\\n ' elif T[0] == 'var8': result += var2name[T[1]].replace('%','%b') elif T[0] == 'var': result += var2name[T[1]] else: assert T[0] == 'str' result += T[1] outputs = ', '.join(outputs) if len(outputs) > 0: outputs += ' ' inputs = ', '.join(inputs) if len(inputs) > 0: inputs += ' ' clobbers = ', '.join(clobbers) result = f'__asm__ ("{result}" : {outputs}: {inputs}: {clobbers})' return result def substitute(program): result = '' for line in program.splitlines(): m = re.fullmatch(r'(.*)\breadasm\("([^"]*)"\)(.*)',line) if m: result += m.group(1)+process(m.group(2))+m.group(3)+'\n' else: result += line+'\n' return result def test(): print(process('amd64; int64 X; X signed>>= 63')) print(process('arm64; int8 X; int8 Y; X = (int8) X; Y = (int8) Y; X - Y; X = Y if signed< else X')) print(process('arm64; int8 X; int8 Y; int8 Z; Z = (uint8) X; Z - (uint8) Y; Z = -1 if unsigned<= else 0')) print(process('arm64; int64 X; int64 Y; int64 R; int64 S; X - Y; R = X if unsigned< else Y; S = Y if unsigned< else X')) if __name__ == '__main__': test() dq-20250201/cryptoint/specialize.py000077500000000000000000000050631474734131400171270ustar00rootroot00000000000000#!/usr/bin/env python3 import os import readasm with open('namespace') as f: projectnamespace = f.readline().strip() with open('crypto_intN.h') as f: intN = f.read() with open('crypto_uintN.h') as f: uintN = f.read() for bits in 8,16,32,64: for ui in 'int','uint': TYPE = f'crypto_{ui}{bits}' with open(f'{TYPE}.h','w') as f: contents = intN if ui == 'int' else uintN contents = contents.replace('N',str(bits)) contents = contents.replace('namespace',projectnamespace) contents = readasm.substitute(contents) contents = contents.replace('amd64','defined(__GNUC__) && defined(__x86_64__)') contents = contents.replace('arm64','defined(__GNUC__) && defined(__aarch64__)') newcontents = '' for line in contents.splitlines(): if ':' in line: condbits = line.split(':')[0].strip() if condbits.isnumeric(): if int(condbits) != bits: continue line = ':'.join(line.split(':')[1:]) newcontents += line+'\n' contents = newcontents tounroll = f' int {TYPE}_k;\n for ({TYPE}_k = 0;{TYPE}_k < {bits};{TYPE}_k += 8)\n {TYPE}_z |= (({TYPE}) (*{TYPE}_s++)) << {TYPE}_k;' unrolled = '\n'.join(f' {TYPE}_z |= (({TYPE}) (*{TYPE}_s++)) << {k};' for k in range(0,bits,8)) contents = contents.replace(tounroll,unrolled) tounroll = f' int {TYPE}_k;\n for ({TYPE}_k = {bits} - 8;{TYPE}_k >= 0;{TYPE}_k -= 8)\n {TYPE}_z |= (({TYPE}) (*{TYPE}_s++)) << {TYPE}_k;' unrolled = '\n'.join(f' {TYPE}_z |= (({TYPE}) (*{TYPE}_s++)) << {k};' for k in reversed(range(0,bits,8))) contents = contents.replace(tounroll,unrolled) tounroll = f' int {TYPE}_k;\n for ({TYPE}_k = 0;{TYPE}_k < {bits};{TYPE}_k += 8)\n *{TYPE}_s++ = {TYPE}_x >> {TYPE}_k;' unrolled = '\n'.join(f' *{TYPE}_s++ = {TYPE}_x >> {k};' for k in range(0,bits,8)) contents = contents.replace(tounroll,unrolled) tounroll = f' int {TYPE}_k;\n for ({TYPE}_k = {bits} - 8;{TYPE}_k >= 0;{TYPE}_k -= 8)\n *{TYPE}_s++ = {TYPE}_x >> {TYPE}_k;' unrolled = '\n'.join(f' *{TYPE}_s++ = {TYPE}_x >> {k};' for k in reversed(range(0,bits,8))) contents = contents.replace(tounroll,unrolled) f.write(contents) for intc in sorted(os.listdir('.')): if not intc.endswith('.c'): continue if not (intc.startswith('int') or intc.startswith('uint')): continue targetintc = intc.replace('N',str(bits)) with open(intc) as f: contents = f.read() with open(targetintc,'w') as f: f.write(contents.replace('N',str(bits))) dq-20250201/cryptoint/uint16_optblocker.c000066400000000000000000000002461474734131400201360ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_uint16.h" volatile crypto_uint16_signed crypto_uint16_signed_optblocker = 0; dq-20250201/cryptoint/uint32_optblocker.c000066400000000000000000000002461474734131400201340ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_uint32.h" volatile crypto_uint32_signed crypto_uint32_signed_optblocker = 0; dq-20250201/cryptoint/uint64_optblocker.c000066400000000000000000000002461474734131400201410ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_uint64.h" volatile crypto_uint64_signed crypto_uint64_signed_optblocker = 0; dq-20250201/cryptoint/uint8_optblocker.c000066400000000000000000000002431474734131400200540ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_uint8.h" volatile crypto_uint8_signed crypto_uint8_signed_optblocker = 0; dq-20250201/cryptoint/uintN_optblocker.c000066400000000000000000000002431474734131400201020ustar00rootroot00000000000000/* auto-generated: cd cryptoint; ./autogen */ /* cryptoint 20241003 */ #include "crypto_uintN.h" volatile crypto_uintN_signed crypto_uintN_signed_optblocker = 0; dq-20250201/cryptoint/version000066400000000000000000000000231474734131400160210ustar00rootroot00000000000000cryptoint 20241003 dq-20250201/die.c000066400000000000000000000013211474734131400132650ustar00rootroot00000000000000#include #include "alloc.h" #include "writeall.h" #include "die.h" void die_9(int e ,const char *s0 ,const char *s1 ,const char *s2 ,const char *s3 ,const char *s4 ,const char *s5 ,const char *s6 ,const char *s7 ,const char *s8 ) { const char *s[9]; const char *x; char buf[256]; long long buflen = 0; long long i; s[0] = s0; s[1] = s1; s[2] = s2; s[3] = s3; s[4] = s4; s[5] = s5; s[6] = s6; s[7] = s7; s[8] = s8; for (i = 0;i < 9;++i) { x = s[i]; if (!x) continue; while (*x) { if (buflen == sizeof buf) { writeall(2,buf,buflen); buflen = 0; } buf[buflen++] = *x++; } } writeall(2,buf,buflen); alloc_freeall(); _exit(e); } dq-20250201/die.h000066400000000000000000000010771474734131400133020ustar00rootroot00000000000000#ifndef DIE_H #define DIE_H extern void die_9(int,const char *,const char *,const char *,const char *,const char *,const char *,const char *,const char *,const char *); #define die_8(x,a,b,c,d,e,f,g,h) die_9(x,a,b,c,d,e,f,g,h,0) #define die_7(x,a,b,c,d,e,f,g) die_8(x,a,b,c,d,e,f,g,0) #define die_6(x,a,b,c,d,e,f) die_7(x,a,b,c,d,e,f,0) #define die_5(x,a,b,c,d,e) die_6(x,a,b,c,d,e,0) #define die_4(x,a,b,c,d) die_5(x,a,b,c,d,0) #define die_3(x,a,b,c) die_4(x,a,b,c,0) #define die_2(x,a,b) die_3(x,a,b,0) #define die_1(x,a) die_2(x,a,0) #define die_0(x) die_1(x,0) #endif dq-20250201/direntry.h000066400000000000000000000002601474734131400143720ustar00rootroot00000000000000/* Public domain. */ #ifndef _DIRENTRY_H____ #define _DIRENTRY_H____ #include #include #define direntry struct dirent #endif /* _DIRENTRY_H____ */ dq-20250201/dns.h000066400000000000000000000136671474734131400133350ustar00rootroot00000000000000#ifndef _DNS_H____ #define _DNS_H____ #include #include "stralloc.h" #define DNS_C_IN (unsigned char *)"\0\1" #define DNS_C_ANY (unsigned char *)"\0\377" #define DNS_T_A (unsigned char *)"\0\1" #define DNS_T_NS (unsigned char *)"\0\2" #define DNS_T_CNAME (unsigned char *)"\0\5" #define DNS_T_SOA (unsigned char *)"\0\6" #define DNS_T_PTR (unsigned char *)"\0\14" #define DNS_T_MX (unsigned char *)"\0\17" #define DNS_T_TXT (unsigned char *)"\0\20" #define DNS_T_AAAA (unsigned char *)"\0\34" #define DNS_T_SRV (unsigned char *)"\0\41" #define DNS_T_AXFR (unsigned char *)"\0\374" #define DNS_T_ANY (unsigned char *)"\0\377" #define DNS_MAGICQ (unsigned char *)"Q6fnvWj8" #define DNS_MAGICR (unsigned char *)"R6fnvWJ8" struct dns_transmit { unsigned char *query; /* 0, or dynamically allocated */ long long querylen; unsigned char *packet; /* 0, or dynamically allocated */ long long packetlen; int s1; /* 0, or 1 + an open file descriptor */ int s1type; long long tcpstate; long long udploop; long long curserver; long long deadline; long long pos; const unsigned char *servers; unsigned char localip[32]; unsigned char qtype[2]; unsigned char port[2]; long long scope_id; long long paddinglen; unsigned char id[2]; unsigned char nonce[12]; const unsigned char *keys; const unsigned char *pk; const unsigned char *suffix; const unsigned char *name; int flagrecursive; int flagipv4only; }; #define DNS_HASKEY(d) (d->keys && *(d->keys + 33 * d->curserver)) #define DNS_ISTXT(d) (d->keys && (*(d->keys + 33 * d->curserver) == 2)) #define DNS_KEYPTR(d) d->keys + 33 * d->curserver + 1 /* dns_domain */ extern void dns_domain_free(unsigned char **); extern int dns_domain_copy(unsigned char **, const unsigned char *); extern long long dns_domain_length(const unsigned char *); extern int dns_domain_equal(const unsigned char *, const unsigned char *); extern int dns_domain_suffix(const unsigned char *, const unsigned char *); extern long long dns_domain_suffixpos(const unsigned char *, const unsigned char *); extern int dns_domain_fromdot(unsigned char **, const unsigned char *, long long); extern int dns_domain_fromdot_static(unsigned char *, const unsigned char *, long long); extern int dns_domain_todot_cat(stralloc *, const unsigned char *); /* dns_packet */ extern long long dns_packet_copy(const unsigned char *,long long,long long,unsigned char *,long long); extern long long dns_packet_getname(const unsigned char *,long long,long long,unsigned char **); extern long long dns_packet_skipname(const unsigned char *,long long,long long); extern long long dns_packet_getname_static(const unsigned char *,long long,long long,unsigned char *); /* dns_transmit */ extern int dns_transmit_start(struct dns_transmit *d, const unsigned char servers[256], int flagrecursive, const unsigned char *q, const unsigned char qtype[2], const unsigned char localip[32]); extern int dns_transmit_startext(struct dns_transmit *d, const unsigned char servers[256], int flagrecursive, int flagtcp, int flagipv4only, const unsigned char *q, const unsigned char qtype[2], const unsigned char localip[32], const unsigned char port[2], const unsigned char keys[528], const unsigned char pk[32], const unsigned char *suffix); extern void dns_transmit_free(struct dns_transmit *); extern void dns_transmit_io(struct dns_transmit *,struct pollfd *,long long *); extern int dns_transmit_get(struct dns_transmit *,const struct pollfd *,const long long); extern void dns_transmit_magic(const char *, const char *); /* dns_data */ struct dns_data { /* ---- OUTPUT */ /* name */ unsigned char *name; stralloc fqdn; /* result */ stralloc result; /* keys */ unsigned char *curvecpkey; unsigned char *dnscurvekey; /* ---- INPUT */ /* options */ char curvecpselector; }; extern void dns_data_free(struct dns_data *); extern int dns_resolvconfip(unsigned char *); extern int dns_resolve(const unsigned char *q, const unsigned char qtype[2]); extern struct dns_transmit dns_resolve_tx; extern int dns_ip(struct dns_data *, const char *); extern int dns_ip4(struct dns_data *, const char *); extern int dns_ip6(struct dns_data *, const char *); extern int dns_resolvconfrewrite(stralloc *); extern int dns_ip_qualify(struct dns_data *, const char *); extern int dns_ip4_qualify(struct dns_data *, const char *); extern int dns_ip6_qualify(struct dns_data *, const char *); #define DNS_IPTONAME_LEN 73 extern char *dns_iptoname(char *, const unsigned char *); /* utils */ extern void dns_sortip4(unsigned char *, long long); extern void dns_sortip(unsigned char *, long long); extern void dns_sortipkey(unsigned char *, unsigned char *, long long); /*base32 */ extern long long dns_base32_bytessize(long long); extern void dns_base32_encodebytes(unsigned char *, const unsigned char *, long long); extern void dns_base32_encodekey(unsigned char *, const unsigned char *); extern long long base32_decode(unsigned char *, const unsigned char *, long long, int); /* nonce */ extern void dns_nonce_purge(void); extern int dns_nonce_init(const char *, const unsigned char *); extern void dns_nonce(unsigned char *); /* verbose */ extern int dns_verbosity_flag; extern const char *dns_verbosity_message; extern void dns_verbosity_setflag(int); extern void dns_verbosity_setmessage(const char *); extern void dns_verbosity_writehex(const char *, const unsigned char *, long long); extern void dns_verbosity_writedomain(const char *, unsigned char *); extern void dns_verbosity_resolving(const char *); extern void dns_verbosity_resolved(struct dns_data *, const char *); extern void dns_verbosity_querysent(struct dns_transmit *, int); extern void dns_verbosity_queryfailed(struct dns_transmit *, int); extern void dns_verbosity_queryfailedtc(struct dns_transmit *); extern void dns_verbosity_queryok(struct dns_transmit *); /* keys */ extern void dns_keys_derive(unsigned char *, long long, unsigned char *); #endif dq-20250201/dns_base32.c000066400000000000000000000054071474734131400144600ustar00rootroot00000000000000#include "byte.h" #include "dns.h" static const char base32_digits[32] = "0123456789bcdfghjklmnpqrstuvwxyz"; long long dns_base32_bytessize(long long len) { if (len < 0) return 0; len = (8 * len + 4) / 5; return len + (len + 49) / 50; } void dns_base32_encodebytes(unsigned char *out, const unsigned char *in, long long len) { unsigned long long i, x, v, vbits; if (len < 0) return; x = v = vbits = 0; for (i = 0; i < len; ++i) { v |= ((unsigned long long)in[i]) << vbits; vbits += 8; do { out[++x] = base32_digits[v & 31]; v >>= 5; vbits -= 5; if (x == 50) { *out = x; out += 1 + x; x = 0; } } while (vbits >= 5); } if (vbits) out[++x] = base32_digits[v & 31]; if (x) *out = x; } void dns_base32_encodekey(unsigned char *out, const unsigned char *key) { unsigned long long i, v, vbits; byte_copy(out, 4, "\66x1a"); out += 4; v = vbits = 0; for (i = 0; i < 32; ++i) { v |= ((unsigned long long)key[i]) << vbits; vbits += 8; do { *out++ = base32_digits[v & 31]; v >>= 5; vbits -= 5; } while (vbits >= 5); } } static const unsigned char val[128] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0xff, 0x0d, 0x0e, 0x0f, 0xff, 0x10, 0x11, 0x12, 0x13, 0x14, 0xff, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0x0b, 0x0c, 0xff, 0x0d, 0x0e, 0x0f, 0xff, 0x10, 0x11, 0x12, 0x13, 0x14, 0xff, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff }; long long base32_decode(unsigned char *out, const unsigned char *in, long long len, int mode) { long long i; unsigned long long x, v, vbits; unsigned char *out0 = out; if (len < 0) return 0; v = vbits = 0; for (i = 0; i < len; ++i) { if (in[i] & 0x80) return 0; x = val[in[i]]; if (x > 0x1f) return 0; v |= x << vbits; vbits += 5; if (vbits >= 8) { *out++ = v; v >>= 8; vbits -= 8; } } if (mode) { if (vbits) *out++ = v; } else if (vbits >= 5 || v) return 0; return out - out0; } dq-20250201/dns_data.c000066400000000000000000000005621474734131400143070ustar00rootroot00000000000000#include "stralloc.h" #include "alloc.h" #include "byte.h" #include "dns.h" void dns_data_free(struct dns_data *r) { dns_domain_free(&r->name); stralloc_free(&r->result); stralloc_free(&r->fqdn); if (r->curvecpkey) alloc_free(r->curvecpkey); if (r->dnscurvekey) alloc_free(r->dnscurvekey); byte_zero(r, sizeof(struct dns_data)); return; } dq-20250201/dns_domain.c000066400000000000000000000103671474734131400146510ustar00rootroot00000000000000#include "alloc.h" #include "byte.h" #include "case.h" #include "e.h" #include "dns.h" long long dns_domain_length(const unsigned char *dn) { const unsigned char *x; unsigned char c; x = dn; while (c = *x++) { x += c; } return (x - dn); } void dns_domain_free(unsigned char **out) { if (*out) { alloc_free(*out); *out = 0; } } int dns_domain_copy(unsigned char **out, const unsigned char *in) { long long len; unsigned char *x; len = dns_domain_length(in); x = alloc(len); if (!x) return 0; byte_copy(x, len, in); if (*out) alloc_free(*out); *out = x; return 1; } int dns_domain_equal(const unsigned char *dn1, const unsigned char *dn2) { long long len; len = dns_domain_length(dn1); if (len != dns_domain_length(dn2)) return 0; return !case_diffb(dn1, len, dn2); } int dns_domain_suffix(const unsigned char *big, const unsigned char *little) { unsigned char c; for (;;) { if (dns_domain_equal(big, little)) return 1; c = *big++; if (!c) return 0; big += c; } } long long dns_domain_suffixpos(const unsigned char *big, const unsigned char *little) { const unsigned char *orig = big; unsigned char c; for (;;) { if (dns_domain_equal(big, little)) return (big - orig); c = *big++; if (!c) return 0; big += c; } } int dns_domain_fromdot(unsigned char **out,const unsigned char *buf,long long n) { unsigned char label[63]; long long labellen = 0; /* <= sizeof label */ unsigned char name[255]; long long namelen = 0; /* <= sizeof name */ unsigned char ch; unsigned char *x; errno = EPROTO; if (n < 0) return 0; for (;;) { if (!n) break; ch = *buf++; --n; if (ch == '.') { if (labellen > 0) { if (namelen + labellen + 1 > sizeof name) return 0; name[namelen++] = labellen; byte_copy(name + namelen,labellen,label); namelen += labellen; labellen = 0; } continue; } if (ch == '\\') { if (!n) break; ch = *buf++; --n; if ((ch >= '0') && (ch <= '7')) { ch -= '0'; if (n && (*buf >= '0') && (*buf <= '7')) { ch <<= 3; ch += *buf - '0'; ++buf; --n; if (n && (*buf >= '0') && (*buf <= '7')) { ch <<= 3; ch += *buf - '0'; ++buf; --n; } } } } if (labellen >= sizeof label) return 0; label[labellen++] = ch; } if (labellen > 0) { if (namelen + labellen + 1 > sizeof name) return 0; name[namelen++] = labellen; byte_copy(name + namelen,labellen,label); namelen += labellen; labellen = 0; } if (namelen + 1 > sizeof name) return 0; name[namelen++] = 0; x = alloc(namelen); if (!x) return 0; byte_copy(x,namelen,name); if (*out) alloc_free(*out); *out = x; return 1; } int dns_domain_fromdot_static(unsigned char *name,const unsigned char *buf,long long n) { unsigned char label[63]; long long labellen = 0; /* <= sizeof label */ long long namelen = 0; /* <= sizeof name */ unsigned char ch; errno = EPROTO; if (n < 0) return 0; byte_zero(name, 256); for (;;) { if (!n) break; ch = *buf++; --n; if (ch == '.') { if (labellen > 0) { if (namelen + labellen + 1 > 255) return 0; name[namelen++] = labellen; byte_copy(name + namelen,labellen,label); namelen += labellen; labellen = 0; } continue; } if (ch == '\\') { if (!n) break; ch = *buf++; --n; if ((ch >= '0') && (ch <= '7')) { ch -= '0'; if (n && (*buf >= '0') && (*buf <= '7')) { ch <<= 3; ch += *buf - '0'; ++buf; --n; if (n && (*buf >= '0') && (*buf <= '7')) { ch <<= 3; ch += *buf - '0'; ++buf; --n; } } } } if (labellen >= sizeof label) return 0; label[labellen++] = ch; } if (labellen > 0) { if (namelen + labellen + 1 > 255) return 0; name[namelen++] = labellen; byte_copy(name + namelen,labellen,label); namelen += labellen; labellen = 0; } if (namelen + 1 > 255) return 0; name[namelen++] = 0; return 1; } dq-20250201/dns_dtda.c000066400000000000000000000017201474734131400143070ustar00rootroot00000000000000#include "stralloc.h" #include "dns.h" int dns_domain_todot_cat(stralloc *out, const unsigned char *d) { char ch; char ch2; unsigned char ch3; unsigned char buf[4]; if (!*d) return stralloc_append(out, "."); for (;;) { ch = *d++; while (ch--) { ch2 = *d++; if ((ch2 >= 'A') && (ch2 <= 'Z')) ch2 += 32; if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_')) { if (!stralloc_append(out,&ch2)) return 0; } else { ch3 = ch2; buf[3] = '0' + (ch3 & 7); ch3 >>= 3; buf[2] = '0' + (ch3 & 7); ch3 >>= 3; buf[1] = '0' + (ch3 & 7); buf[0] = '\\'; if (!stralloc_catb(out, buf, 4)) return 0; } } if (!*d) return 1; if (!stralloc_append(out,".")) return 0; } } dq-20250201/dns_ip.c000066400000000000000000000217321474734131400140100ustar00rootroot00000000000000#include "alloc.h" #include "byte.h" #include "crypto_uint16.h" #include "base32decode.h" #include "hexdecode.h" #include "case.h" #include "str.h" #include "stralloc.h" #include "strtoip.h" #include "milliseconds.h" #include "dns.h" static int dns_curvecpkey_parse(struct dns_data *r, const unsigned char *d) { long long pos = 0; long long i; unsigned char c; unsigned char key[32]; unsigned char ext[16]; if (r->curvecpkey) return 0; for (;;) { c = d[pos++]; if (!c) break; if (c == 54 && byte_isequal(d + pos, 3, "uz7") && base32decode(key, sizeof key, d + pos + 3, 51)) { pos += c; c = d[pos++]; if (!c) break; if (c >= 32 && hexdecode(ext, sizeof ext, d + pos, 32)) { if (c == 32 || r->curvecpselector == 0) { r->curvecpkey = alloc(48); if (!r->curvecpkey) return -1; byte_copy(r->curvecpkey, 32, key); byte_copy(r->curvecpkey + 32, 16, ext); return 0; } if (d[pos + 32] == '/') { for (i = 33; i < c; ++i) { if (!case_diffb(d + pos + i, 1, &r->curvecpselector)) { r->curvecpkey = alloc(48); if (!r->curvecpkey) return -1; byte_copy(r->curvecpkey, 32, key); byte_copy(r->curvecpkey + 32, 16, ext); return 0; } } } } } pos += c; } return 0; } static int dns_dnscurvekey_parse(struct dns_data *r, const unsigned char *d) { long long pos = 0; unsigned char c; unsigned char key[32]; if (r->dnscurvekey) return 0; for (;;) { c = d[pos++]; if (!c) break; if (c == 54 && byte_isequal(d + pos, 3, "uz5") && base32decode(key, sizeof key, d + pos + 3, 51)) { r->dnscurvekey = alloc(32); if (!r->dnscurvekey) return -1; byte_copy(r->dnscurvekey, 32, key); return 0; } pos += c; } return 0; } static int ip_packet(struct dns_data *r, unsigned char *buf, long long len) { crypto_uint16 numanswers; crypto_uint16 numauthority; crypto_uint16 datalen; long long pos, newpos; unsigned char data[16]; unsigned char d[256]; /* header */ pos = dns_packet_copy(buf, len, 0, data, 12); if (!pos) return -1; numanswers = crypto_uint16_load_bigendian(data + 6); numauthority = crypto_uint16_load_bigendian(data + 8); pos = dns_packet_getname_static(buf, len, pos, d); if (!pos) return -1; pos += 4; if (dns_dnscurvekey_parse(r, d) == -1) return -1; if (dns_curvecpkey_parse(r, d) == -1) return -1; while (numanswers--) { pos = dns_packet_skipname(buf, len, pos); if (!pos) return -1; pos = dns_packet_copy(buf, len, pos, data, 10); if (!pos) return -1; datalen = crypto_uint16_load_bigendian(data + 8); newpos = pos + datalen; /* CNAME answers */ if (byte_isequal(data, 2, DNS_T_CNAME)) { if (byte_isequal(data + 2, 2, DNS_C_IN)) { if (!dns_packet_getname_static(buf, len, pos, d)) return -1; if (dns_curvecpkey_parse(r, d) == -1) return -1; } } /* A answers */ else if (byte_isequal(data, 2, DNS_T_A)) { if (byte_isequal(data + 2, 2, DNS_C_IN)) { if (datalen == 4) { if (!dns_packet_copy(buf, len, pos, data + 12, 4)) return -1; byte_copy(data, 12, "\0\0\0\0\0\0\0\0\0\0\377\377"); if (!stralloc_catb(&r->result, data, 16)) return -1; } } } /* AAAA answers */ else if (byte_isequal(data, 2, DNS_T_AAAA)) { if (byte_isequal(data + 2, 2, DNS_C_IN)) { if (datalen == 16) { if (!dns_packet_copy(buf, len, pos, data, 16)) return -1; if (!stralloc_catb(&r->result, data, 16)) return -1; } } } pos = newpos; } while (numauthority--) { pos = dns_packet_skipname(buf, len, pos); if (!pos) return -1; pos = dns_packet_copy(buf, len, pos, data, 10); if (!pos) return -1; datalen = crypto_uint16_load_bigendian(data + 8); newpos = pos + datalen; /* NS authority */ if (byte_isequal(data, 2, DNS_T_NS)) { if (byte_isequal(data + 2, 2, DNS_C_IN)) { if (!dns_packet_getname_static(buf, len, pos, d)) return -1; if (dns_curvecpkey_parse(r, d) == -1) return -1; } } pos = newpos; } return 0; } struct dns_transmit dns_resolve_tx0 = {0}; struct dns_transmit dns_resolve_tx1 = {0}; static int resolve2(struct dns_data *d, const unsigned char *q, const unsigned char qtype0[2], const unsigned char qtype1[2]) { long long deadline, stamp, timeout, max; unsigned char servers[256]; struct pollfd x[2]; int r; if (dns_resolvconfip(servers) == -1) return -1; if (dns_transmit_start(&dns_resolve_tx0, servers, 1, q, qtype0, 0) == -1) return -1; if (dns_transmit_start(&dns_resolve_tx1, servers, 1, q, qtype1, 0) == -1) return -1; for (;;) { stamp = milliseconds(); deadline = 120000 + stamp; dns_transmit_io(&dns_resolve_tx0, &x[0], &deadline); dns_transmit_io(&dns_resolve_tx1, &x[1], &deadline); timeout = deadline - stamp; if (timeout <= 0) timeout = 20; poll(x, 2, timeout); r = dns_transmit_get(&dns_resolve_tx0, &x[0], stamp); if (r == -1) return -1; if (r == 1) { if (ip_packet(d, dns_resolve_tx0.packet, dns_resolve_tx0.packetlen) == -1) return -1; dns_transmit_free(&dns_resolve_tx0); byte_copy(&dns_resolve_tx0, sizeof (struct dns_transmit), &dns_resolve_tx1); byte_zero(&dns_resolve_tx1, sizeof (struct dns_transmit)); break; } r = dns_transmit_get(&dns_resolve_tx1, &x[1], stamp); if (r == -1) return -1; if (r == 1) { if (ip_packet(d, dns_resolve_tx1.packet, dns_resolve_tx1.packetlen) == -1) return -1; break; } } max = 3000 + milliseconds(); for (;;) { stamp = milliseconds(); if (stamp > max) return 0; deadline = max; dns_transmit_io(&dns_resolve_tx0, x, &deadline); timeout = deadline - stamp; if (timeout <= 0) timeout = 20; poll(x, 1, timeout); r = dns_transmit_get(&dns_resolve_tx0, x, stamp); if (r == -1) return -1; if (r == 1) { if (ip_packet(d, dns_resolve_tx0.packet, dns_resolve_tx0.packetlen) == -1) return -1; return 0; } } } int dns_ip(struct dns_data *r, const char *name) { unsigned char ip[16]; if (!dns_domain_fromdot(&r->name, (unsigned char *)name, str_len(name))) return -1; if (strtoip(ip, name)) { if (!stralloc_copyb(&r->result, ip, 16)) return -1; return 0; } if (!stralloc_copys(&r->result, "")) return -1; if (resolve2(r, r->name, DNS_T_AAAA, DNS_T_A) == -1) return -1; dns_transmit_free(&dns_resolve_tx0); dns_transmit_free(&dns_resolve_tx1); if (r->fqdn.len == 0) { if (!stralloc_copys(&r->fqdn, name)) return -1; if (!stralloc_0(&r->fqdn)) return -1; } dns_sortip(r->result.s, r->result.len); return 0; } int dns_ip4(struct dns_data *r, const char *name) { unsigned char ip[16]; if (!dns_domain_fromdot(&r->name, (unsigned char *)name, str_len(name))) return -1; if (strtoip4(ip, name)) { if (!stralloc_copyb(&r->result, ip, 16)) return -1; return 0; } if (!stralloc_copys(&r->result, "")) return -1; /* A */ if (dns_resolve(r->name, DNS_T_A) == -1) return -1; if (ip_packet(r, dns_resolve_tx.packet, dns_resolve_tx.packetlen) == -1) return -1; dns_transmit_free(&dns_resolve_tx); if (r->fqdn.len == 0) { if (!stralloc_copys(&r->fqdn, name)) return -1; if (!stralloc_0(&r->fqdn)) return -1; } dns_sortip(r->result.s, r->result.len); return 0; } int dns_ip6(struct dns_data *r, const char *name) { unsigned char ip[16]; if (!dns_domain_fromdot(&r->name, (unsigned char *)name, str_len(name))) return -1; if (strtoip6(ip, name)) { if (!stralloc_copyb(&r->result, ip, 16)) return -1; return 0; } if (!stralloc_copys(&r->result, "")) return -1; /* AAAA */ if (dns_resolve(r->name, DNS_T_AAAA) == -1) return -1; if (ip_packet(r, dns_resolve_tx.packet, dns_resolve_tx.packetlen) == -1) return -1; dns_transmit_free(&dns_resolve_tx); if (r->fqdn.len == 0) { if (!stralloc_copys(&r->fqdn, name)) return -1; if (!stralloc_0(&r->fqdn)) return -1; } dns_sortip(r->result.s, r->result.len); return 0; } dq-20250201/dns_ipq.c000066400000000000000000000054001474734131400141630ustar00rootroot00000000000000#include "stralloc.h" #include "case.h" #include "byte.h" #include "str.h" #include "dns.h" static int doit(stralloc *work,const unsigned char *rule) { unsigned char ch; long long colon; long long prefixlen; ch = *rule++; if ((ch != '?') && (ch != '=') && (ch != '*') && (ch != '-')) return 1; colon = str_chr((char *)rule,':'); if (!rule[colon]) return 1; if (work->len < colon) return 1; prefixlen = work->len - colon; if ((ch == '=') && prefixlen) return 1; if (case_diffb(rule,colon,work->s + prefixlen)) return 1; if (ch == '?') { if (byte_chr(work->s,prefixlen,':') < prefixlen) return 1; /* IPv6 */ if (byte_chr(work->s,prefixlen,'.') < prefixlen) return 1; if (byte_chr(work->s,prefixlen,'[') < prefixlen) return 1; if (byte_chr(work->s,prefixlen,']') < prefixlen) return 1; } work->len = prefixlen; if (ch == '-') work->len = 0; return stralloc_cats(work,rule + colon + 1); } static int dns_ip_qualify_rules(struct dns_data *out,stralloc *fqdn,const char *in,const stralloc *rules, int (*op)(struct dns_data *, const char *)) { long long i; long long j; long long plus; long long fqdnlen; if (!stralloc_copys(fqdn,in)) return -1; for (j = i = 0;j < rules->len;++j) if (!rules->s[j]) { if (!doit(fqdn,rules->s + i)) return -1; i = j + 1; } fqdnlen = fqdn->len; plus = byte_chr(fqdn->s,fqdnlen,'+'); fqdn->s[fqdn->len] = 0; if (plus >= fqdnlen) return op(out,(char *)fqdn->s); i = plus + 1; for (;;) { j = byte_chr(fqdn->s + i,fqdnlen - i,'+'); byte_copy(fqdn->s + plus,j,fqdn->s + i); fqdn->len = plus + j; fqdn->s[fqdn->len] = 0; if (op(out,(char *)fqdn->s) == -1) return -1; if (out->result.len) return 0; i += j; if (i >= fqdnlen) return 0; ++i; } } int dns_ip_qualify(struct dns_data *out, const char *in) { stralloc rules = {0}; int r; dns_verbosity_resolving(in); if (dns_resolvconfrewrite(&rules) == -1) return -1; r = dns_ip_qualify_rules(out, &out->fqdn, in, &rules, dns_ip); stralloc_free(&rules); dns_verbosity_resolved(out, in); return r; } int dns_ip4_qualify(struct dns_data *out, const char *in) { stralloc rules = {0}; int r; dns_verbosity_resolving(in); if (dns_resolvconfrewrite(&rules) == -1) return -1; r = dns_ip_qualify_rules(out, &out->fqdn, in, &rules, dns_ip4); stralloc_free(&rules); dns_verbosity_resolved(out, in); return r; } int dns_ip6_qualify(struct dns_data *out, const char *in) { stralloc rules = {0}; int r; dns_verbosity_resolving(in); if (dns_resolvconfrewrite(&rules) == -1) return -1; r = dns_ip_qualify_rules(out, &out->fqdn, in, &rules, dns_ip6); stralloc_free(&rules); dns_verbosity_resolved(out, in); return r; } dq-20250201/dns_iptoname.c000066400000000000000000000020551474734131400152110ustar00rootroot00000000000000#include "byte.h" #include "numtostr.h" #include "dns.h" static char *dns_iptoname4(char *name, const unsigned char *ip) { long long i,j; char strnum[NUMTOSTR_LEN], *x; j = 0; for (i = 3; i >= 0; --i) { x = numtostr(strnum, ip[i]); while (*x) name[j++] = *x++; name[j++] = '.'; } byte_copy(name + j, 13, "in-addr.arpa"); return name; } static char *dns_iptoname6(char *name, const unsigned char *ip) { long long i,j; j = 0; for (i = 15; i >= 0; --i) { name[j++] = "0123456789abcdef"[(ip[i] >> 0) & 15]; name[j++] = '.'; name[j++] = "0123456789abcdef"[(ip[i] >> 4) & 15]; name[j++] = '.'; } byte_copy(name + j, 9, "ip6.arpa"); return name; } char *dns_iptoname(char *namebuf, const unsigned char *ip) { static char staticbuf[DNS_IPTONAME_LEN]; if (!namebuf) namebuf = staticbuf; /* not thread-safe */ if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { return dns_iptoname4(namebuf, ip + 12); } return dns_iptoname6(namebuf, ip); } dq-20250201/dns_keys.c000066400000000000000000000003571474734131400143530ustar00rootroot00000000000000#include "crypto_stream_salsa20.h" #include "dns.h" static const unsigned char zero[8] = {0}; void dns_keys_derive(unsigned char *out, long long outlen, unsigned char *skseed) { crypto_stream_salsa20(out, outlen, zero, skseed); } dq-20250201/dns_nonce.c000066400000000000000000000046161474734131400145040ustar00rootroot00000000000000/* version 20130522 20250136 - use cryptoint Jan Mojzis Public domain. */ #include "nanoseconds.h" #include "randombytes.h" #include "crypto_uint32.h" #include "crypto_uint64.h" #include "byte.h" #include "purge.h" #include "dns.h" #define NSS 1 /* nonce separation space, 1 - 3 bytes */ static unsigned char noncekey[16] = {0}; static crypto_uint64 noncecounter = 0; static unsigned char noncemask[NSS]; static unsigned char noncedata[NSS]; void dns_nonce_purge(void) { purge(noncekey, sizeof noncekey); } int dns_nonce_init(const char *ns, const unsigned char *nk) { long long i; noncecounter = nanoseconds(); for (i = 0; i < NSS; ++i) noncemask[i] = 0xff; for (i = 0; i < NSS; ++i) noncedata[i] = 0x00; if (!ns) ns = ""; i = 0; while (i < 8 * NSS) { if (ns[i] != '0' && ns[i] != '1') break; noncemask[i/8] = noncemask[i/8] * 2; noncedata[i/8] = noncedata[i/8] * 2 + ns[i] - '0'; ++i; } if (ns[i] == '0' || ns[i] == '1') return 0; while (i < 8 * NSS) { noncemask[i/8] = noncemask[i/8] * 2 + 1; noncedata[i/8] = noncedata[i/8] * 2; ++i; } if (nk) byte_copy(noncekey, sizeof noncekey, nk); else randombytes(noncekey, sizeof noncekey); return 1; } static void dns_nonce_encrypt(unsigned char *out, crypto_uint64 in, const unsigned char *k) { long long i; crypto_uint32 v0, v1, k0, k1, k2, k3; crypto_uint32 sum = 0; crypto_uint32 delta = 0x9e3779b9; v0 = in; in >>= 32; v1 = in; k0 = crypto_uint32_load(k + 0); k1 = crypto_uint32_load(k + 4); k2 = crypto_uint32_load(k + 8); k3 = crypto_uint32_load(k + 12); for (i = 0; i < 32; i++) { sum += delta; v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1); v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3); } crypto_uint32_store(out + 0, v0); crypto_uint32_store(out + 4, v1); } /* n is 12-byte nonce with the following structure: n[0...NSS-1]: random or nonce-separation bits n[NSS...4]: random n[4...11]: TEA encrypted counter */ void dns_nonce(unsigned char *n) { long long x; if (!noncecounter) dns_nonce_init(0, 0); x = nanoseconds(); if (x > noncecounter) noncecounter = x; randombytes(n, 4); for(x = 0; x < NSS; ++x) { n[x] &= noncemask[x]; n[x] += noncedata[x]; } dns_nonce_encrypt((n + 4), ++noncecounter, noncekey); } dq-20250201/dns_packet.c000066400000000000000000000062411474734131400146450ustar00rootroot00000000000000#include "e.h" #include "byte.h" #include "dns.h" long long dns_packet_copy(const unsigned char *buf, long long len, long long pos, unsigned char *out, long long outlen) { while (outlen > 0) { if (pos >= len) { errno = EPROTO; return 0; } *out = buf[pos++]; ++out; --outlen; } return pos; } long long dns_packet_skipname(const unsigned char *buf, long long len, long long pos) { unsigned char ch; for (;;) { if (pos >= len) break; ch = buf[pos++]; if (ch >= 192) return pos + 1; if (ch >= 64) break; if (!ch) return pos; pos += ch; } errno = EPROTO; return 0; } long long dns_packet_getname(const unsigned char *buf, long long len, long long pos, unsigned char **d) { long long loop = 0; long long state = 0; long long firstcompress = 0; long long where; unsigned char ch; unsigned char name[255]; long long namelen = 0; for (;;) { if (pos >= len) goto PROTO; ch = buf[pos++]; if (++loop >= 1000) goto PROTO; if (state > 0) { if (namelen + 1 > sizeof name) goto PROTO; name[namelen++] = ch; --state; } else { while (ch >= 192) { where = ch; where -= 192; where <<= 8; if (pos >= len) goto PROTO; ch = buf[pos++]; if (!firstcompress) firstcompress = pos; pos = where + ch; if (pos >= len) goto PROTO; ch = buf[pos++]; if (++loop >= 1000) goto PROTO; } if (ch >= 64) goto PROTO; if (namelen + 1 > sizeof name) goto PROTO; name[namelen++] = ch; if (!ch) break; state = ch; } } if (!dns_domain_copy(d, name)) return 0; if (firstcompress) return firstcompress; return pos; PROTO: errno = EPROTO; return 0; } long long dns_packet_getname_static(const unsigned char *buf, long long len, long long pos, unsigned char *name) { long long loop = 0; long long state = 0; long long firstcompress = 0; long long where; unsigned char ch; long long namelen = 0; byte_zero(name, 256); for (;;) { if (pos >= len) goto PROTO; ch = buf[pos++]; if (++loop >= 1000) goto PROTO; if (state > 0) { if (namelen + 1 > 255) goto PROTO; name[namelen++] = ch; --state; } else { while (ch >= 192) { where = ch; where -= 192; where <<= 8; if (pos >= len) goto PROTO; ch = buf[pos++]; if (!firstcompress) firstcompress = pos; pos = where + ch; if (pos >= len) goto PROTO; ch = buf[pos++]; if (++loop >= 1000) goto PROTO; } if (ch >= 64) goto PROTO; if (namelen + 1 > 255) goto PROTO; name[namelen++] = ch; if (!ch) break; state = ch; } } if (firstcompress) return firstcompress; return pos; PROTO: errno = EPROTO; return 0; } dq-20250201/dns_rcip.c000066400000000000000000000051341474734131400143330ustar00rootroot00000000000000#include "milliseconds.h" #include "openreadclose.h" #include "byte.h" #include "env.h" #include "stralloc.h" #include "strtoip.h" #include "strtomultiip.h" #include "xsocket.h" #include "dns.h" static stralloc data = {0}; static int init(unsigned char ip[256]) { long long i, j, iplen = 0; long long k = 0; byte_zero(ip, 256); iplen = strtomultiip(ip, 256, env_get("DNSCACHEIP")); if (!iplen) { i = openreadclose("/etc/resolv.conf", &data, 64); if (i == -1) return -1; if (i) { if (!stralloc_append(&data, "\n")) return -1; i = 0; for (j = 0; j < data.len; ++j) { if (data.s[j] == '\n') { k = j; while (k >= 0 && ((data.s[k] == ' ') || (data.s[k] == '\t') || (data.s[k] == '\n'))) { data.s[k--] = 0; } if (byte_isequal("nameserver ", 11, data.s + i) || byte_isequal("nameserver\t", 11, data.s + i)) { i += 10; while ((data.s[i] == ' ') || (data.s[i] == '\t')) ++i; if (iplen + 16 <= 256) { if (strtoip(ip + iplen, (char *)data.s + i)) { if (byte_isequal(ip + iplen, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) { byte_copy(ip + iplen, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"); } if (byte_isequal(ip + iplen, 16, "\0\0\0\0\0\0\0\0\0\0\377\377\0\0\0\0")) { byte_copy(ip + iplen, 16, "\0\0\0\0\0\0\0\0\0\0\377\377\177\0\0\1"); } iplen += 16; } } } i = j + 1; } } } } if (!iplen) { byte_copy(ip + iplen, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"); iplen += 16; byte_copy(ip + iplen, 16, "\0\0\0\0\0\0\0\0\0\0\377\377\177\0\0\1"); iplen += 16; } return 0; } static int ok = 0; static long long uses = 0; static long long deadline = 0; static unsigned char ip[256]; /* defined if ok */ int dns_resolvconfip(unsigned char *s) { long long now; now = milliseconds(); if (deadline < now) ok = 0; if (uses <= 0) ok = 0; if (!ok) { if (init(ip) == -1) return -1; deadline = 600000 + now; uses = 10000; ok = 1; } --uses; byte_copy(s, sizeof ip, ip); return 0; } dq-20250201/dns_rcrw.c000066400000000000000000000066461474734131400143640ustar00rootroot00000000000000#include #include "milliseconds.h" #include "env.h" #include "byte.h" #include "str.h" #include "openreadclose.h" #include "dns.h" static stralloc data = {0}; static int init(stralloc *rules) { char host[256]; const char *x; int i; int j; int k; int flagds = 0; if (!stralloc_copys(rules,"")) return -1; x = env_get("DNSREWRITEFILE"); if (!x) x = "/etc/dnsrewrite"; i = openreadclose(x,&data,64); if (i == -1) return -1; if (i) { if (!stralloc_append(&data,"\n")) return -1; i = 0; for (j = 0;j < data.len;++j) if (data.s[j] == '\n') { if (!stralloc_catb(rules,data.s + i,j - i)) return -1; while (rules->len) { if (rules->s[rules->len - 1] != ' ') if (rules->s[rules->len - 1] != '\t') if (rules->s[rules->len - 1] != '\r') break; --rules->len; } if (!stralloc_0(rules)) return -1; i = j + 1; } return 0; } x = env_get("LOCALDOMAIN"); if (x) { if (!stralloc_copys(&data,x)) return -1; if (!stralloc_append(&data," ")) return -1; if (!stralloc_copys(rules,"?:")) return -1; i = 0; for (j = 0;j < data.len;++j) if (data.s[j] == ' ') { if (!stralloc_cats(rules,"+.")) return -1; if (!stralloc_catb(rules,data.s + i,j - i)) return -1; i = j + 1; } if (!stralloc_0(rules)) return -1; if (!stralloc_cats(rules,"*.:")) return -1; if (!stralloc_0(rules)) return -1; return 0; } i = openreadclose("/etc/resolv.conf",&data,64); if (i == -1) return -1; if (i) { if (!stralloc_append(&data,"\n")) return -1; i = 0; for (j = 0;j < data.len;++j) if (data.s[j] == '\n') { if (byte_isequal("domain ",7,data.s + i) || byte_isequal("domain\t",7,data.s + i)) { flagds = 1; } if (byte_isequal("search ",7,data.s + i) || byte_isequal("search\t",7,data.s + i)) { flagds = 2; } if (flagds) { if (!stralloc_copys(rules,"?:")) return -1; i += 7; while (i < j) { k = byte_chr(data.s + i,j - i,' '); k = byte_chr(data.s + i,k,'\t'); if (!k) { ++i; continue; } if (!stralloc_cats(rules,"+.")) return -1; if (!stralloc_catb(rules,data.s + i,k)) return -1; i += k; } if (!stralloc_0(rules)) return -1; if (!stralloc_cats(rules,"*.:")) return -1; if (!stralloc_0(rules)) return -1; if (flagds > 1) return 0; } i = j + 1; } if (flagds > 0) return 0; } host[0] = 0; if (gethostname(host,sizeof host) == -1) return -1; host[(sizeof host) - 1] = 0; i = str_chr(host,'.'); if (host[i]) { if (!stralloc_copys(rules,"?:")) return -1; if (!stralloc_cats(rules,host + i)) return -1; if (!stralloc_0(rules)) return -1; } if (!stralloc_cats(rules,"*.:")) return -1; if (!stralloc_0(rules)) return -1; return 0; } static int ok = 0; static long long uses; static long long deadline; static stralloc rules = {0}; /* defined if ok */ int dns_resolvconfrewrite(stralloc *out) { long long now; now = milliseconds(); if (deadline < now) ok = 0; if (uses <= 0) ok = 0; if (!ok) { if (init(&rules) == -1) return -1; deadline = 600000 + now; uses = 10000; ok = 1; } --uses; if (!stralloc_copy(out, &rules)) return -1; return 0; } dq-20250201/dns_resolve.c000066400000000000000000000014761474734131400150620ustar00rootroot00000000000000#include #include "milliseconds.h" #include "byte.h" #include "e.h" #include "dns.h" struct dns_transmit dns_resolve_tx = {0}; int dns_resolve(const unsigned char *q, const unsigned char qtype[2]) { long long deadline, stamp, timeout; unsigned char servers[256]; struct pollfd x[1]; int r; if (dns_resolvconfip(servers) == -1) return -1; if (dns_transmit_start(&dns_resolve_tx, servers, 1, q, qtype, 0) == -1) return -1; for (;;) { stamp = milliseconds(); deadline = 120000 + stamp;; dns_transmit_io(&dns_resolve_tx, x, &deadline); timeout = deadline - stamp; if (timeout <= 0) timeout = 20; poll(x, 1, timeout); r = dns_transmit_get(&dns_resolve_tx, x, stamp); if (r == -1) return -1; if (r == 1) return 0; } } dq-20250201/dns_sortip.c000066400000000000000000000054011474734131400147130ustar00rootroot00000000000000#include "randommod.h" #include "byte.h" #include "dns.h" static void swap(unsigned char *x, long long len, unsigned char *y) { unsigned char t[33]; byte_copy(t, len, x); byte_copy(x, len, y); byte_copy(y, len, t); } void dns_sortip4(unsigned char *s, long long n) { long long i; if (n < 0) return; n >>= 2; while (n > 1) { i = randommod(n); --n; swap(s + (i << 2), 4, s + (n << 2)); } } void dns_sortip(unsigned char *s, long long nn) { long long i; long long n = nn; if (nn < 0) return; n >>= 4; while (n > 1) { i = randommod(n); --n; swap(s + (i << 4), 16, s + (n << 4)); } for (i = 0; i + 16 <= nn; i += 16) { if (!byte_isequal(s + i, 12, "\0\0\0\0\0\0\0\0\0\0\377\377")) { swap(s + i, 16, s); break; } } } void dns_sortipkey(unsigned char *s, unsigned char *t, long long nn) { long long i, j, k; long long n; unsigned char *key; nn >>=4; n = nn; while (n > 1) { i = randommod(n); --n; swap(s + 16 * i, 16, s + 16 * n); swap(t + 33 * i, 33, t + 33 * n); } n = nn; j = 0; k = 0; for (i = k; i < n; ++i) { key = t + 33 * i; if (key[0] == 1) { swap(s + 16 * i, 16, s + 16 * j); swap(t + 33 * i, 33, t + 33 * j); ++j; } } for (i = k; i < j; ++i) { key = t + 33 * i; if (key[0] != 1) continue; if (!byte_isequal(s + 16 * i, 12, "\0\0\0\0\0\0\0\0\0\0\377\377")) { swap(s + 16 * i, 16, s + 16 * k); swap(t + 33 * i, 33, t + 33 * k); break; } } k = j; for (i = k; i < n; ++i) { key = t + 33 * i; if (key[0] == 2) { swap(s + 16 * i, 16, s + 16 * j); swap(t + 33 * i, 33, t + 33 * j); ++j; } } for (i = k; i < j; ++i) { key = t + 33 * i; if (key[0] != 2) continue; if (!byte_isequal(s + 16 * i, 12, "\0\0\0\0\0\0\0\0\0\0\377\377")) { swap(s + 16 * i, 16, s + 16 * k); swap(t + 33 * i, 33, t + 33 * k); break; } } k = j; for (i = k; i < n; ++i) { key = t + 33 * i; if (!byte_isequal(s + 16 * i, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) { swap(s + 16 * i, 16, s + 16 * j); swap(t + 33 * i, 33, t + 33 * j); ++j; } } for (i = k; i < j; ++i) { key = t + 33 * i; if (key[0] != 0) continue; if (!byte_isequal(s + 16 * i, 12, "\0\0\0\0\0\0\0\0\0\0\377\377")) { swap(s + 16 * i, 16, s + 16 * k); swap(t + 33 * i, 33, t + 33 * k); break; } } } dq-20250201/dns_transmit.c000066400000000000000000000422461474734131400152440ustar00rootroot00000000000000#include #include "alloc.h" #include "milliseconds.h" #include "xsocket.h" #include "e.h" #include "byte.h" #include "crypto_uint16.h" #include "randombytes.h" #include "randommod.h" #include "case.h" #include "str.h" #include "dns.h" #include "crypto_box_curve25519xsalsa20poly1305.h" unsigned char *magicq = DNS_MAGICQ; unsigned char *magicr = DNS_MAGICR; void dns_transmit_magic(const char *mq, const char *mr) { if (mq && str_len(mq) == 8) magicq = (unsigned char *)mq; if (mr && str_len(mr) == 8) magicr = (unsigned char *)mr; } static void basequery(struct dns_transmit *d, unsigned char *query) { long long len; len = dns_domain_length(d->name); byte_copy(query, 2, d->id); byte_copy(query + 2, 10, d->flagrecursive ? "\1\0\0\1\0\0\0\0\0\0" : "\0\0\0\1\0\0\0\0\0\0gcc-bug-workaround"); byte_copy(query + 12, len, d->name); byte_copy(query + 12 + len, 2, d->qtype); byte_copy(query + 14 + len, 2, DNS_C_IN); if (d->paddinglen > 0) { byte_zero(query + 16 + len, d->paddinglen); query[16 + len + d->paddinglen - 1] = 0x80; } } static void regularquery(struct dns_transmit *d) { long long len; d->paddinglen = 0; len = dns_domain_length(d->name) + d->paddinglen; d->querylen = len + 18; crypto_uint16_store_bigendian(d->query, d->querylen - 2); randombytes(d->id, 2); basequery(d, d->query + 2); d->name = d->query + 14; } static void streamlinedquery(struct dns_transmit *d) { long long len; unsigned char nonce[24]; d->paddinglen = (2 + randommod(2)) * 64 - (dns_domain_length(d->name) + 16) % 64; len = dns_domain_length(d->name) + d->paddinglen; d->querylen = len + 86; dns_nonce(d->nonce); byte_copy(nonce, 12, d->nonce); byte_zero(nonce + 12, 12); dns_verbosity_writehex("DNSCurve pk: ", d->pk, 32); dns_verbosity_writehex("DNSCurve nonce: ", nonce, 24); byte_zero(d->query + 38, 32); randombytes(d->id, 2); basequery(d, d->query + 38 + 32); crypto_box_curve25519xsalsa20poly1305_afternm(d->query + 38, d->query + 38, len + 48, nonce, DNS_KEYPTR(d)); crypto_uint16_store_bigendian(d->query, d->querylen - 2); byte_copy(d->query + 2, 8, magicq); byte_copy(d->query + 10, 32, d->pk); byte_copy(d->query + 42, 12, nonce); } static void txtquery(struct dns_transmit *d) { long long len, suffixlen, m; unsigned char nonce[24]; d->paddinglen = 0; len = dns_domain_length(d->name) + d->paddinglen; suffixlen = dns_domain_length(d->suffix); m = dns_base32_bytessize(len + 44); d->querylen = m + suffixlen + 73; dns_nonce(d->nonce); byte_copy(nonce, 12, d->nonce); byte_zero(nonce + 12, 12); dns_verbosity_writehex("DNSCurve pk: ", d->pk, 32); dns_verbosity_writehex("DNSCurve nonce: ", nonce, 24); byte_zero(d->query, 32); randombytes(d->id, 2); basequery(d, d->query + 32); crypto_box_curve25519xsalsa20poly1305_afternm(d->query, d->query, len + 48, nonce, DNS_KEYPTR(d)); byte_copyr(d->query + d->querylen - len - 32, len + 32, d->query + 16); byte_copy(d->query + d->querylen - len - 44, 12, nonce); crypto_uint16_store_bigendian(d->query, d->querylen - 2); randombytes(d->query + 2, 2); byte_copy(d->query + 4, 10, "\0\0\0\1\0\0\0\0\0\0"); dns_base32_encodebytes(d->query + 14,d->query + d->querylen - len - 44, len + 44); dns_base32_encodekey(d->query + 14 + m, d->pk); byte_copy(d->query + 69 + m, suffixlen, d->suffix); dns_verbosity_writedomain("DNSCurve txt: ", d->query + 14); byte_copy(d->query + 69 + m + suffixlen, 2, DNS_T_TXT); byte_copy(d->query + 69 + m + suffixlen + 2, 2, DNS_C_IN); } static void makequery(struct dns_transmit *d) { if (!DNS_HASKEY(d)) { regularquery(d); return; } if (!DNS_ISTXT(d)) { streamlinedquery(d); return; } txtquery(d); } static int getquery(const struct dns_transmit *d, unsigned char *buf, long long *lenp) { long long len; unsigned char nonce[24]; long long pos; unsigned char out[16]; long long namelen; long long txtlen; long long i, j; unsigned char ch; crypto_uint16 datalen; if (!DNS_HASKEY(d)) return 0; len = *lenp; if (!DNS_ISTXT(d)) { if (len < 48) return 1; if (!byte_isequal(buf, 8, magicr)) return 1; if (!byte_isequal(buf + 8, 12, d->nonce)) return 1; byte_copy(nonce, 24, buf + 8); dns_verbosity_writehex("DNSCurve nonce: ", nonce, 24); byte_zero(buf + 16, 16); if (crypto_box_curve25519xsalsa20poly1305_open_afternm(buf + 16, buf + 16, len - 16, nonce, DNS_KEYPTR(d))) return 1; byte_copy(buf, len - 48, buf + 48); *lenp = len - 48; return 0; } pos = dns_packet_copy(buf, len, 0, out, 12); if (!pos) return 1; if (!byte_isequal(out, 2, d->query + 2)) return 1; if (!byte_isequal(out + 2, 10, "\204\0\0\1\0\1\0\0\0\0")) return 1; /* query name might be >255 bytes, so can't use dns_packet_getname */ namelen = dns_domain_length(d->query + 14); if (namelen > len - pos) return 1; if (case_diffb(buf + pos, namelen, d->query + 14)) return 1; pos += namelen; pos = dns_packet_copy(buf, len, pos, out, 16); if (!pos) return 1; if (!byte_isequal(out, 14, "\0\20\0\1\300\14\0\20\0\1\0\0\0\0")) return 1; datalen = crypto_uint16_load_bigendian(out + 14); if (datalen > len - pos) return 1; j = 4; txtlen = 0; for (i = 0; i < datalen; ++i) { ch = buf[pos + i]; if (!txtlen) txtlen = ch; else { --txtlen; buf[j++] = ch; } } if (txtlen) return 1; if (j < 32) return 1; byte_copy(nonce, 12, d->nonce); byte_copy(nonce + 12, 12, buf + 4); dns_verbosity_writehex("DNSCurve nonce: ", nonce, 24); byte_zero(buf, 16); if (crypto_box_curve25519xsalsa20poly1305_open_afternm(buf, buf, j, nonce, DNS_KEYPTR(d))) return 1; byte_copy(buf, j - 32, buf + 32); *lenp = j - 32; return 0; } static int serverwantstcp(const unsigned char *buf, long long len) { unsigned char out[12]; if (!dns_packet_copy(buf, len, 0, out, 12)) return 1; if (out[2] & 2) return 1; return 0; } static int serverfailed(const unsigned char *buf, long long len) { unsigned char out[12]; unsigned long long rcode; if (!dns_packet_copy(buf, len, 0, out, 12)) return 1; rcode = out[3]; rcode &= 15; if (rcode && (rcode != 3)) { errno = EAGAIN; return 1; } return 0; } static int irrelevant(const struct dns_transmit *d, const unsigned char *buf, long long len) { unsigned char out[12]; unsigned char *dn; long long pos; pos = dns_packet_copy(buf, len, 0, out, 12); if (!pos) return 1; if (!byte_isequal(out, 2, d->id)) return 1; if (out[4] != 0) return 1; if (out[5] != 1) return 1; dn = 0; pos = dns_packet_getname(buf, len, pos, &dn); if (!pos) return 1; if (!dns_domain_equal(dn, d->name)) { alloc_free(dn); return 1; } alloc_free(dn); pos = dns_packet_copy(buf, len, pos, out, 4); if (!pos) return 1; if (!byte_isequal(out, 2, d->qtype)) return 1; if (!byte_isequal(out + 2, 2, DNS_C_IN)) return 1; return 0; } static void packetfree(struct dns_transmit *d) { if (!d->packet) return; alloc_free(d->packet); d->packet = 0; } static void queryfree(struct dns_transmit *d) { if (!d->query) return; alloc_free(d->query); d->query = 0; } static void socketfree(struct dns_transmit *d) { if (!d->s1) return; close(d->s1 - 1); d->s1 = 0; d->s1type = 0; } void dns_transmit_free(struct dns_transmit *d) { queryfree(d); socketfree(d); packetfree(d); } static int randombind(struct dns_transmit *d) { long long j; unsigned char port[2]; long long pos = 0; if (d->s1type == XSOCKET_V4) pos = 0; if (d->s1type == XSOCKET_V6) pos = 16; for (j = 0;j < 10;++j) { crypto_uint16_store_bigendian(port, randommod(64510) + 1025); if (xsocket_bind(d->s1 - 1, d->s1type, d->localip + pos, port, d->scope_id) == 0) return 0; } byte_zero(port, 2); if (xsocket_bind(d->s1 - 1, d->s1type, d->localip + pos, port, d->scope_id) == 0) return 0; return -1; } static const long long timeouts[4] = { 1000, 3000, 11000, 45000 }; static int thisudp(struct dns_transmit *d) { const unsigned char *ip; socketfree(d); while (d->udploop < 4) { for (;d->curserver < 16; ++d->curserver) { ip = d->servers + 16 * d->curserver; if (!byte_isequal(ip, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) { makequery(d); d->s1type = XSOCKET_V6; if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { d->s1type = XSOCKET_V4; } if (d->s1type == XSOCKET_V6 && d->flagipv4only) continue; d->s1 = 1 + xsocket_udp(d->s1type); if (!d->s1) { if (errno == EPROTONOSUPPORT) { dns_verbosity_queryfailed(d, 0); continue; } dns_transmit_free(d); return -1; } if (randombind(d) == -1) { if (errno == EPROTONOSUPPORT) { dns_verbosity_queryfailed(d, 0); continue; } dns_transmit_free(d); return -1; } if (xsocket_send(d->s1 - 1, d->s1type, d->query + 2, d->querylen - 2, ip, d->port, d->scope_id) == d->querylen - 2) { dns_verbosity_querysent(d, 0); d->deadline = milliseconds() + timeouts[d->udploop]; d->tcpstate = 0; return 0; } dns_verbosity_queryfailed(d, 0); socketfree(d); } } ++d->udploop; d->curserver = 0; } dns_transmit_free(d); return -1; } static int firstudp(struct dns_transmit *d) { d->curserver = 0; return thisudp(d); } static int nextudp(struct dns_transmit *d) { dns_verbosity_queryfailed(d, 0); ++d->curserver; return thisudp(d); } static int thistcp(struct dns_transmit *d) { const unsigned char *ip; socketfree(d); packetfree(d); for (;d->curserver < 16; ++d->curserver) { ip = d->servers + 16 * d->curserver; if (!byte_isequal(ip, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) { makequery(d); d->s1type = XSOCKET_V6; if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { d->s1type = XSOCKET_V4; } if (d->s1type == XSOCKET_V6 && d->flagipv4only) continue; d->s1 = 1 + xsocket_tcp(d->s1type); if (!d->s1) { if (errno == EPROTONOSUPPORT) { dns_verbosity_queryfailed(d, 1); continue; } dns_transmit_free(d); return -1; } if (randombind(d) == -1) { if (errno == EPROTONOSUPPORT) { dns_verbosity_queryfailed(d, 1); continue; } dns_transmit_free(d); return -1; } d->deadline = milliseconds() + 10000; if (xsocket_connect(d->s1 - 1, d->s1type, ip, d->port, d->scope_id) == 0) { d->pos = 0; d->tcpstate = 2; dns_verbosity_querysent(d, 1); return 0; } if ((errno == EINPROGRESS) || (errno == EWOULDBLOCK)) { d->tcpstate = 1; dns_verbosity_querysent(d, 1); return 0; } dns_verbosity_queryfailed(d, 1); socketfree(d); } } dns_transmit_free(d); return -1; } static int firsttcp(struct dns_transmit *d) { d->curserver = 0; return thistcp(d); } static int nexttcp(struct dns_transmit *d) { dns_verbosity_queryfailed(d, 1); ++d->curserver; return thistcp(d); } int dns_transmit_startext(struct dns_transmit *d, const unsigned char servers[256], int flagrecursive, int flagtcp, int flagipv4only, const unsigned char *q, const unsigned char qtype[2], const unsigned char localip[32], const unsigned char port[2], const unsigned char keys[528], const unsigned char pk[32], const unsigned char *suffix) { long long len, suffixlen = 0; dns_transmit_free(d); errno = EIO; /* suffix length */ if (!suffix) suffix = (unsigned char *)""; suffixlen = dns_domain_length(suffix); /* length */ len = dns_domain_length(q); /* allocate enough space */ if (!keys) { d->paddinglen = 0; d->query = alloc(len + 18 + d->paddinglen); } else { d->paddinglen = 3 * 64 - (len + 16) % 64; /* padding MAX */ d->query = alloc(dns_base32_bytessize(len + d->paddinglen + 44) + suffixlen + 73); } if (!d->query) return -1; /* init structure */ byte_copy(d->qtype, 2, qtype); d->servers = servers; if (!localip) { byte_copy(d->localip, 16, xsocket_ANYIP4); byte_copy(d->localip + 16, 16, xsocket_ANYIP6); } else { byte_copy(d->localip, 32, localip); } d->udploop = flagrecursive ? 1 : 0; d->flagrecursive = flagrecursive; d->flagipv4only = flagipv4only; d->name = q; d->keys = keys; d->pk = pk; d->suffix = suffix; if (!port) { crypto_uint16_store_bigendian(d->port, 53); } else { byte_copy(d->port, 2, port); } if (len + 16 > 512 || flagtcp) return firsttcp(d); return firstudp(d); } int dns_transmit_start(struct dns_transmit *d, const unsigned char servers[256], int flagrecursive, const unsigned char *q, const unsigned char qtype[2], const unsigned char localip[32]) { return dns_transmit_startext(d, servers, flagrecursive, 0, 0, q, qtype, localip, 0, 0, 0, 0); } void dns_transmit_io(struct dns_transmit *d, struct pollfd *x, long long *deadline) { x->fd = d->s1 - 1; switch(d->tcpstate) { case 0: case 3: case 4: case 5: x->events = POLLIN; break; case 1: case 2: x->events = POLLOUT; break; } if (d->deadline < *deadline) *deadline = d->deadline; } int dns_transmit_get(struct dns_transmit *d, const struct pollfd *x, const long long when) { unsigned char udpbuf[4097]; unsigned char ch; long long r; int fd; unsigned char ip[16]; unsigned char port[2]; errno = EIO; fd = d->s1 - 1; if (!x->revents) { if (when < d->deadline) return 0; errno = ETIMEDOUT; if (d->tcpstate == 0) return nextudp(d); return nexttcp(d); } if (d->tcpstate == 0) { /* have attempted to send UDP query to each server udploop times have sent query to curserver on UDP socket s */ r = xsocket_recv(fd,d->s1type,udpbuf,sizeof udpbuf,ip,port,0); if (r <= 0) { /* if (errno == ECONNREFUSED) if (d->udploop == 2) return 0; */ return nextudp(d); } if (r + 1 > sizeof udpbuf) return 0; if (getquery(d, udpbuf, &r)) return 0; if (irrelevant(d,udpbuf,r)) return 0; if (serverwantstcp(udpbuf,r)) { dns_verbosity_queryfailedtc(d); return firsttcp(d); } if (serverfailed(udpbuf,r)) { /* if (d->udploop == 2) return 0; */ return nextudp(d); } socketfree(d); d->packetlen = r; d->packet = alloc(d->packetlen); if (!d->packet) { dns_transmit_free(d); return -1; } byte_copy(d->packet,d->packetlen,udpbuf); queryfree(d); dns_verbosity_queryok(d); return 1; } if (d->tcpstate == 1) { /* have sent connection attempt to curserver on TCP socket s pos not defined */ if (!xsocket_connected(fd)) return nexttcp(d); d->pos = 0; d->tcpstate = 2; return 0; } if (d->tcpstate == 2) { /* have connection to curserver on TCP socket s have sent pos bytes of query */ r = write(fd,d->query + d->pos,d->querylen - d->pos); if (r <= 0) return nexttcp(d); d->pos += r; if (d->pos == d->querylen) { d->deadline = milliseconds() + 10000; d->tcpstate = 3; } return 0; } if (d->tcpstate == 3) { /* have sent entire query to curserver on TCP socket s pos not defined */ r = read(fd,&ch,1); if (r <= 0) return nexttcp(d); d->packetlen = ch; d->tcpstate = 4; return 0; } if (d->tcpstate == 4) { /* have sent entire query to curserver on TCP socket s pos not defined have received one byte of packet length into packetlen */ r = read(fd,&ch,1); if (r <= 0) return nexttcp(d); d->packetlen <<= 8; d->packetlen += ch; d->tcpstate = 5; d->pos = 0; d->packet = alloc(d->packetlen); if (!d->packet) { dns_transmit_free(d); return -1; } return 0; } if (d->tcpstate == 5) { /* have sent entire query to curserver on TCP socket s have received entire packet length into packetlen packet is allocated have received pos bytes of packet */ r = read(fd,d->packet + d->pos,d->packetlen - d->pos); if (r <= 0) return nexttcp(d); d->pos += r; if (d->pos < d->packetlen) return 0; socketfree(d); if (getquery(d,d->packet,&d->packetlen)) return nexttcp(d); if (irrelevant(d,d->packet,d->packetlen)) return nexttcp(d); if (serverwantstcp(d->packet,d->packetlen)) { dns_verbosity_queryfailedtc(d); return nexttcp(d); } if (serverfailed(d->packet,d->packetlen)) return nexttcp(d); queryfree(d); dns_verbosity_queryok(d); return 1; } return 0; } dq-20250201/dns_verbosity.c000066400000000000000000000161471474734131400154320ustar00rootroot00000000000000#include "stralloc.h" #include "writeall.h" #include "iptostr.h" #include "porttostr.h" #include "numtostr.h" #include "e.h" #include "crypto_uint16.h" #include "byte.h" #include "dns.h" int dns_verbosity_flag = 1; const char *dns_verbosity_message = "dns: info: "; void dns_verbosity_setflag(int x) { dns_verbosity_flag = x; } void dns_verbosity_setmessage(const char *x) { dns_verbosity_message = x; } static stralloc out = {0}; void dns_verbosity_writehex(const char *message, const unsigned char *x, long long xlen) { if (dns_verbosity_flag < 3) return; if (!stralloc_copys(&out, dns_verbosity_message)) return; if (!stralloc_cats(&out, message)) return; while (xlen > 0) { if (!stralloc_catb(&out, "0123456789abcdef" + ((*x >> 4) & 15), 1)) return; if (!stralloc_catb(&out, "0123456789abcdef" + (*x & 15), 1)) return; ++x; --xlen; } if (!stralloc_cats(&out, "\n")) return; writeall(2, out.s, out.len); } void dns_verbosity_writedomain(const char *message, unsigned char *x) { if (dns_verbosity_flag < 3) return; if (!stralloc_copys(&out, dns_verbosity_message)) return; if (!stralloc_cats(&out, message)) return; if (!dns_domain_todot_cat(&out, x)) return; if (!stralloc_cats(&out, "\n")) return; writeall(2, out.s, out.len); } static const unsigned char *qtypetostr(const unsigned char *qtype) { crypto_uint16 u16; if (byte_isequal(qtype, 2, DNS_T_A)) return (unsigned char *)"A"; if (byte_isequal(qtype, 2, DNS_T_NS)) return (unsigned char *)"NS"; if (byte_isequal(qtype, 2, DNS_T_MX)) return (unsigned char *)"MX"; if (byte_isequal(qtype, 2, DNS_T_ANY)) return (unsigned char *)"ANY"; if (byte_isequal(qtype, 2, DNS_T_PTR)) return (unsigned char *)"PTR"; if (byte_isequal(qtype, 2, DNS_T_TXT)) return (unsigned char *)"TXT"; if (byte_isequal(qtype, 2, DNS_T_SOA)) return (unsigned char *)"SOA"; if (byte_isequal(qtype, 2, DNS_T_SRV)) return (unsigned char *)"SRV"; if (byte_isequal(qtype, 2, DNS_T_AAAA)) return (unsigned char *)"AAAA"; if (byte_isequal(qtype, 2, DNS_T_PTR)) return (unsigned char *)"PTR"; if (byte_isequal(qtype, 2, DNS_T_CNAME)) return (unsigned char *)"CNAME"; u16 = crypto_uint16_load_bigendian(qtype); return (const unsigned char *)numtostr(0, u16); } void dns_verbosity_resolving(const char *x) { if (dns_verbosity_flag < 3) return; if (!stralloc_copys(&out, dns_verbosity_message)) return; if (!stralloc_cats(&out, "resolving: ")) return; if (!stralloc_cats(&out, x)) return; if (!stralloc_cats(&out, "\n")) return; writeall(2, out.s, out.len); } void dns_verbosity_resolved(struct dns_data *r, const char *x) { long long j; if (dns_verbosity_flag < 3) return; if (!stralloc_copys(&out, dns_verbosity_message)) return; if (!stralloc_cats(&out, "resolved: ")) return; if (!stralloc_cats(&out, x)) return; if (!stralloc_cats(&out, ": ")) return; if (!stralloc_cat(&out, &r->fqdn)) return; if (!stralloc_cats(&out, " ")) return; for (j = 0; j + 16 <= r->result.len; j += 16) { if (!stralloc_cats(&out, iptostr(0, r->result.s + j))) return; if (!stralloc_cats(&out, ",")) return; } out.len -= 1; if (!stralloc_cats(&out, "\n")) return; writeall(2, out.s, out.len); } void dns_verbosity_querysent(struct dns_transmit *d, int flagtcp) { if (dns_verbosity_flag < 3) return; if (!stralloc_copys(&out, dns_verbosity_message)) return; if (DNS_HASKEY(d)) { if (!stralloc_cats(&out, "DNSCurve query: ")) return; } else { if (!stralloc_cats(&out, "DNS query: ")) return; } if (!dns_domain_todot_cat(&out, d->name)) return; if (!stralloc_cats(&out, " ")) return; if (!stralloc_cats(&out, qtypetostr(d->qtype))) return; if (!stralloc_cats(&out, " ")) return; if (flagtcp) { if (!stralloc_cats(&out, "TCP ")) return; } else { if (!stralloc_cats(&out, "UDP ")) return; } if (!stralloc_cats(&out, iptostr(0, d->servers + 16 * d->curserver))) return; if (!stralloc_cats(&out, " ")) return; if (!stralloc_cats(&out, porttostr(0, d->port))) return; if (!stralloc_cats(&out, ": sent\n")) return; writeall(2, out.s, out.len); } void dns_verbosity_queryfailed(struct dns_transmit *d, int flagtcp) { if (dns_verbosity_flag < 2) return; if (!stralloc_copys(&out, dns_verbosity_message)) return; if (DNS_HASKEY(d)) { if (!stralloc_cats(&out, "DNSCurve query: ")) return; } else { if (!stralloc_cats(&out, "DNS query: ")) return; } if (!dns_domain_todot_cat(&out, d->name)) return; if (!stralloc_cats(&out, " ")) return; if (!stralloc_cats(&out, qtypetostr(d->qtype))) return; if (!stralloc_cats(&out, " ")) return; if (flagtcp) { if (!stralloc_cats(&out, "TCP ")) return; } else { if (!stralloc_cats(&out, "UDP ")) return; } if (!stralloc_cats(&out, iptostr(0, d->servers + 16 * d->curserver))) return; if (!stralloc_cats(&out, " ")) return; if (!stralloc_cats(&out, porttostr(0, d->port))) return; if (!stralloc_cats(&out, ": failed: ")) return; if (!stralloc_cats(&out, e_str(errno))) return; if (!stralloc_cats(&out, "\n")) return; writeall(2, out.s, out.len); } void dns_verbosity_queryfailedtc(struct dns_transmit *d) { if (dns_verbosity_flag < 2) return; if (!stralloc_copys(&out, dns_verbosity_message)) return; if (DNS_HASKEY(d)) { if (!stralloc_cats(&out, "DNSCurve query: ")) return; } else { if (!stralloc_cats(&out, "DNS query: ")) return; } if (!dns_domain_todot_cat(&out, d->name)) return; if (!stralloc_cats(&out, " ")) return; if (!stralloc_cats(&out, qtypetostr(d->qtype))) return; if (!stralloc_cats(&out, " ")) return; if (d->tcpstate > 0) { if (!stralloc_cats(&out, "TCP ")) return; } else { if (!stralloc_cats(&out, "UDP ")) return; } if (!stralloc_cats(&out, iptostr(0, d->servers + 16 * d->curserver))) return; if (!stralloc_cats(&out, " ")) return; if (!stralloc_cats(&out, porttostr(0, d->port))) return; if (!stralloc_cats(&out, ": failed: truncated\n")) return; writeall(2, out.s, out.len); } void dns_verbosity_queryok(struct dns_transmit *d) { if (dns_verbosity_flag < 3) return; if (!stralloc_copys(&out, dns_verbosity_message)) return; if (DNS_HASKEY(d)) { if (!stralloc_cats(&out, "DNSCurve query: ")) return; } else { if (!stralloc_cats(&out, "DNS query: ")) return; } if (!dns_domain_todot_cat(&out, d->name)) return; if (!stralloc_cats(&out, " ")) return; if (!stralloc_cats(&out, qtypetostr(d->qtype))) return; if (!stralloc_cats(&out, " ")) return; if (d->tcpstate > 0) { if (!stralloc_cats(&out, "TCP ")) return; } else { if (!stralloc_cats(&out, "UDP ")) return; } if (!stralloc_cats(&out, iptostr(0, d->servers + 16 * d->curserver))) return; if (!stralloc_cats(&out, " ")) return; if (!stralloc_cats(&out, porttostr(0, d->port))) return; if (!stralloc_cats(&out, ": received\n")) return; writeall(2, out.s, out.len); } dq-20250201/dq.c000066400000000000000000000226331474734131400131410ustar00rootroot00000000000000/* * 20130521 * Jan Mojzis * Public domain. */ #include #include "dns.h" #include "strtonum.h" #include "case.h" #include "die.h" #include "e.h" #include "randombytes.h" #include "byte.h" #include "stralloc.h" #include "printpacket.h" #include "writeall.h" #include "milliseconds.h" #include "str.h" #include "crypto_uint16.h" #include "portparse.h" #include "base32decode.h" #include "hexdecode.h" #include "strtoip.h" #include "keyparse.h" #include "typeparse.h" #include "purge.h" #include "crypto_box_curve25519xsalsa20poly1305.h" #define USAGE "\ \n\ dq: usage:\n\ \n\ name:\n\ dq - DNS/DNSCurve query tool\n\ \n\ synopsis:\n\ dq [options] type fqdn [host]\n\ dq -a [options] type fqdn host\n\ \n\ options:\n\ -v (optional): print extra information\n\ -r (optional): send recursive query (default)\n\ -a (optional): send authoritative query\n\ -u (optional): use UDP (default)\n\ -t (optional): use TCP\n\ -s (optional): send DNSCurve query in streamlined format (default), ignored for regular DNS queries\n\ -S suffix (optional): send DNSCurve query in TXT format using suffix suffix, ignored for regular DNS queries\n\ -T timeout (optional): give up on the DNS/DNSCurve query attempt after timeout seconds <1-60> (default 60)\n\ -p port (optional): send query to port port (default 53)\n\ -k key (optional): send DNSCurve query and use servers public-key key\n\ type: DNS query type (A, NS, MX, ANY, PTR, TXT, SOA, SRV, AAAA, AXFR, CNAME or numeric type)\n\ fqdn: fully qualified domain name\n\ host: DNS server, hostname or IP address\n\ \n\ environment:\n\ DNSCACHEIP: use IP address $DNSCACHEIP instead of 'nameserver' lines from /etc/resolv.conf\n\ LOCALDOMAIN: use space separated names from $LOCALDOMAIN instead of 'search' line from /etc/resolv.conf\n\ DNSREWRITEFILE: use $DNSREWRITEFILE file instead of /etc/dnsrewrite\n\ \n\ notes:\n\ dq rewrites IP address to *.in-addr.arpa or *.ip6.arpa for PTR queries e.g.:\n\ 127.0.0.1 -> 1.0.0.127.in-addr.arpa\n\ ::1 -> 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa\n\ \n\ recursive examples:\n\ dq any dnscurve.cz\n\ dq any dnscurve.cz 8.8.8.8\n\ env DNSCACHEIP=8.8.8.8 dq any dnscurve.cz\n\ dq ptr 1.0.0.127.in-addr.arpa\n\ dq ptr 127.0.0.1\n\ dq ptr ::1\n\ dq ptr 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa\n\ \n\ authoritative examples:\n\ dq -a any dnscurve.cz uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq.ns.dnscurve.cz\n\ dq -a -S cz any dnscurve.cz uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq.ns.dnscurve.cz\n\ dq -a -k uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq any dnscurve.cz 2a02:2b88:2:1::127d:1\n\ \n\ " #define FATAL "dq: fatal: " #define DEBUG "dq: debug: " static int flagverbose = 1; static int flagrecursive = 1; static int flagtcp = 0; #define NUMIP 16 static struct global { int x; unsigned char servers[16 * NUMIP]; unsigned char keys[33 * NUMIP]; unsigned char pk[32]; unsigned char sk[32]; stralloc out; unsigned char qtype[2]; unsigned char q[256]; struct dns_transmit tx; struct dns_data r; } g = {0}; static unsigned char *suffix = 0; static const char *transport = "regular DNS"; static const char *portstr = "53"; static unsigned char port[2]; static char *keystr = 0; static const char *timeoutstr = "60"; static long long maxtimeout; static void die_usage(const char *s) { if (s) die_4(100, USAGE, FATAL, s, "\n"); die_1(100, USAGE); } static void die_fatal(const char *trouble, const char *fn) { purge(&g, sizeof g); if (errno) { if (fn) die_7(111, FATAL, trouble, " ", fn, ": ", e_str(errno), "\n"); die_5(111, FATAL, trouble, ": ", e_str(errno), "\n"); } if (fn) die_5(111, FATAL, trouble, " ", fn, "\n"); die_3(111, FATAL, trouble, "\n"); } static int resolve(void) { long long deadline, stamp, timeout, max; struct pollfd x[1]; int r; if (dns_transmit_startext(&g.tx, g.servers, flagrecursive, flagtcp, 0, g.q, g.qtype, 0, port, g.keys, g.pk, suffix) == -1) return -1; max = maxtimeout * 1000 + milliseconds(); for (;;) { stamp = milliseconds(); if (stamp > max) { errno = ETIMEDOUT; dns_verbosity_queryfailed(&g.tx, flagtcp); return -1; } deadline = max; dns_transmit_io(&g.tx, x, &deadline); timeout = deadline - stamp; if (timeout <= 0) timeout = 20; poll(x, 1, timeout); r = dns_transmit_get(&g.tx, x, stamp); if (r == -1) return -1; if (r == 1) break; } return 0; } static int nameparse(unsigned char *s, const char *x) { unsigned char ip[16]; if (!x) return 0; if (byte_isequal(g.qtype, 2, DNS_T_PTR)) { if (strtoip(ip, x)) x = dns_iptoname(0, ip); } return dns_domain_fromdot_static(s, (unsigned char *)x, str_len(x)); } static int ipget(const char *host) { if (host) { if (dns_ip_qualify(&g.r, host) == -1) return 0; if (g.r.result.len == 0) return 0; if (g.r.result.len > sizeof g.servers) g.r.result.len = sizeof g.servers; byte_copy(g.servers, g.r.result.len, g.r.result.s); if (g.r.dnscurvekey && !g.keys[0]) { byte_copy(g.keys + 1, 32, g.r.dnscurvekey); g.keys[0] = 1; } } else { if (!flagrecursive) return 0; if (dns_resolvconfip(g.servers) == -1) return 0; } return 1; } static void oops(void) { die_fatal("unable to parse", 0); } int main(int argc, char **argv) { crypto_uint16 u16; char *x; long long i; if (!argv[0]) die_usage(0); for (;;) { if (!argv[1]) break; if (argv[1][0] != '-') break; x = *++argv; if (x[0] == '-' && x[1] == 0) break; if (x[0] == '-' && x[1] == '-' && x[2] == 0) break; while (*++x) { if (*x == 'q') { flagverbose = 0; continue; } if (*x == 'Q') { flagverbose = 1; continue; } if (*x == 'v') { if (flagverbose >= 2) flagverbose = 3; else flagverbose = 2; continue; } if (*x == 'a') { flagrecursive = 0; continue; } if (*x == 'r') { flagrecursive = 1; continue; } if (*x == 't') { flagtcp = 1; continue; } if (*x == 'u') { flagtcp = 0; continue; } if (*x == 's') { dns_domain_free(&suffix); continue; } if (*x == 'S') { if (x[1]) { dns_domain_fromdot(&suffix, (unsigned char *)x + 1, str_len(x + 1)); break; } if (argv[1]) { ++argv; dns_domain_fromdot(&suffix, (unsigned char *)*argv, str_len(*argv)); break; } } if (*x == 'p') { if (x[1]) { portstr = x + 1; break; } if (argv[1]) { portstr = *++argv; break; } } if (*x == 'k') { if (x[1]) { keystr = x + 1; ; break; } if (argv[1]) { keystr = *++argv; break; } } if (*x == 'T') { if (x[1]) { timeoutstr = x + 1; break; } if (argv[1]) { timeoutstr = *++argv; break; } } die_usage(0); } } if (!strtonum(&maxtimeout, timeoutstr) || maxtimeout < 1 || maxtimeout > 60) die_usage("unable to parse timeout, timeout must be an integer between 1 and 60"); dns_verbosity_setflag(flagverbose); dns_verbosity_setmessage(DEBUG); if (!portparse(port, portstr)) die_usage("unable to parse port"); if (keystr) { if (!keyparse(g.keys + 1, 32, keystr)) die_usage("unable to parse key"); g.keys[0] = 1; } if (!typeparse(g.qtype, *++argv)) die_usage("unable to parse type"); if (!nameparse(g.q, *++argv)) die_usage("unable to parse fqdn/IP"); if (!*++argv) { if (!flagrecursive) die_usage("missing host"); byte_zero(g.keys, sizeof g.keys); } if (!ipget(*argv)) die_usage("unable to figure out IP from host"); if (g.keys[0] > 0) { crypto_box_curve25519xsalsa20poly1305_keypair(g.pk, g.sk); crypto_box_curve25519xsalsa20poly1305_beforenm(g.keys + 1, g.keys + 1, g.sk); for (i = 0; i + 33 < sizeof g.keys; ++i) g.keys[i + 33] = g.keys[i]; if (suffix) { for (i = 0; i < NUMIP; ++i) g.keys[33 * i] = 2; transport = "txt DNSCurve"; } else { transport = "streamlined DNSCurve"; } } if (!stralloc_copys(&g.out, "")) oops(); u16 = crypto_uint16_load_bigendian(g.qtype); if (!stralloc_catnum(&g.out, u16)) oops(); if (!stralloc_cats(&g.out, " ")) oops(); if (!dns_domain_todot_cat(&g.out, g.q)) oops(); if (!stralloc_cats(&g.out, " - ")) oops(); if (!stralloc_cats(&g.out, transport)) oops(); if (!stralloc_cats(&g.out, ":\n")) oops(); if (byte_isequal(g.qtype, 2, DNS_T_AXFR)) { if (!stralloc_cats(&g.out, "axfr not supported, use axfr-get\n")) oops(); } else if (resolve() == -1) { if (!stralloc_cats(&g.out, e_str(errno))) oops(); if (!stralloc_cats(&g.out, "\n")) oops(); } else { if (g.tx.packetlen < 4) oops(); if (flagrecursive) { g.tx.packet[2] &= ~1; g.tx.packet[3] &= ~128; } if (!printpacket_cat(&g.out, g.tx.packet, g.tx.packetlen)) oops(); } if (writeall(1, g.out.s, g.out.len) == -1) die_fatal("unable to write output", 0); purge(&g, sizeof g); die_0(0); return 111; } dq-20250201/dqcache-makekey.c000066400000000000000000000010361474734131400155430ustar00rootroot00000000000000/* 20160325 Jan Mojzis Public domain. */ #include #include "randombytes.h" #include "writeall.h" static unsigned char sk[32]; static unsigned char out[65]; int main(int argc, char **argv) { long long i; randombytes(sk, sizeof sk); for (i = 0; i < 32; ++i) { out[2 * i + 0] = "0123456789abcdef"[15 & (int) (sk[i] >> 4)]; out[2 * i + 1] = "0123456789abcdef"[15 & (int) (sk[i] >> 0)]; } out[2 * i] = '\n'; if (writeall(1, out, sizeof out) == -1) _exit(111); fsync(1); _exit(0); } dq-20250201/dqcache-start.c000066400000000000000000000031351474734131400152540ustar00rootroot00000000000000#include #include #include /* workaround for *BSD #include */ extern int setenv(const char *, const char *, int); #include "numtostr.h" #include "strtonum.h" #include "e.h" #include "die.h" static const char *account; static const char *root; static struct passwd *pw; static long long uid, gid; #define USAGE "dqcache-start: usage: dqcache-start root-directory account child\n" #define FATAL "dqcache-start: fatal: " int main(int argc, char **argv) { root = *++argv; if (!root) die_1(100, USAGE); account = *++argv; if (!account || !*++argv) die_1(100, USAGE); pw = getpwnam(account); if (pw) { uid = pw->pw_uid; gid = pw->pw_gid; } else { if (!strtonum(&uid, account) || uid < 0) die_4(111, FATAL, "unknown account ", account, "\n"); gid = uid; } if (chdir(root) == -1) die_6(111, FATAL, "unable to change directory to ", root, ": ", e_str(errno), "\n"); if (chown("dump", uid, gid) == -1) die_6(111, FATAL, "unable to change owner on ", root, "/dump: ", e_str(errno), "\n"); if (setenv("ROOT", root, 1) == -1) die_4(111, FATAL, "unable to set env. variable ROOT: ", e_str(errno), "\n"); if (setenv("GID", numtostr(0, gid), 1) == -1) die_4(111, FATAL, "unable to set env. variable GID: ", e_str(errno), "\n"); if (setenv("UID", numtostr(0, uid), 1) == -1) die_4(111, FATAL, "unable to set env. variable UID: ", e_str(errno), "\n"); execvp(*argv, argv); die_6(111, FATAL, "unable to run ", *argv, ": ", e_str(errno), "\n"); return 111; } dq-20250201/dqcache.c000066400000000000000000000344621474734131400141300ustar00rootroot00000000000000#include #include #include #include "env.h" #include "byte.h" #include "xsocket.h" #include "strtoip.h" #include "randombytes.h" #include "crypto_uint64.h" #include "query.h" #include "die.h" #include "warn.h" #include "e.h" #include "numtostr.h" #include "strtonum.h" #include "cache.h" #include "response.h" #include "log.h" #include "roots.h" #include "hexparse.h" #include "alloc.h" #include "milliseconds.h" #include "blocking.h" #include "crypto_uint16.h" #include "portparse.h" #include "droproot.h" #include "okclient.h" #include "purge.h" static int flagokclient = 0; static int packetquery(unsigned char *buf, long long len, unsigned char **q, unsigned char qtype[2], unsigned char qclass[2], unsigned char id[2]) { long long pos; unsigned char header[12]; errno = EPROTO; pos = dns_packet_copy(buf, len, 0, header, 12); if (!pos) return 0; if (header[2] & 128) return 0; /* must not respond to responses */ if (!(header[2] & 1)) return 0; /* do not respond to non-recursive queries */ if (header[2] & 120) return 0; if (header[2] & 2) return 0; if (!byte_isequal(header + 4, 2, "\0\1")) return 0; pos = dns_packet_getname(buf, len, pos, q); if (!pos) return 0; pos = dns_packet_copy(buf, len, pos, qtype, 2); if (!pos) return 0; pos = dns_packet_copy(buf, len, pos, qclass, 2); if (!pos) return 0; if (!byte_isequal(qclass, 2, DNS_C_IN) && !byte_isequal(qclass, 2, DNS_C_ANY)) return 0; byte_copy(id, 2, header); return 1; } static unsigned char myport[2] = {0, 53}; static unsigned char myipoutgoing[32]; static unsigned char myipincoming[16]; static int mytypeincoming = XSOCKET_V6; static unsigned char buf[1024]; static int udp53; #define MAXUDPDUPLICATED 10 #define MAXUDP 200 static struct udpclient { struct query q; long long start; crypto_uint64 active; /* query number, if active; otherwise 0 */ struct pollfd *io; unsigned char ip[16]; unsigned char port[2]; long long scope_id; unsigned char id[2]; } u[MAXUDP]; static void u_drop(long long j) { if (!u[j].active) return; log_querydrop(&u[j].active); u[j].active = 0; --log_uactive; } static void u_respond(long long j) { if (!u[j].active) return; response_id(u[j].id); if (response_len > 512) response_tc(); xsocket_send(udp53, mytypeincoming, response, response_len, u[j].ip, u[j].port, u[j].scope_id); log_querydone(&u[j].active, response_len); u[j].active = 0; --log_uactive; } static long long u_duplicatequerycount(unsigned char *q, unsigned char *qtype) { long long j, c = 0; struct query *z; for (j = 0; j < MAXUDP; ++j) { if (!u[j].active) continue; z = &u[j].q; if (!byte_isequal(z->type, 2, qtype)) continue; if (!dns_domain_equal(z->name[0], q)) continue; ++c; } return c; } static void u_new(void) { long long j; long long i; struct udpclient *x; long long len; static unsigned char *q = 0; unsigned char qtype[2]; unsigned char qclass[2]; crypto_uint16 port; for (j = 0; j < MAXUDP; ++j) if (!u[j].active) break; if (j >= MAXUDP) { j = 0; for (i = 1;i < MAXUDP;++i) if (u[i].start < u[j].start) j = i; errno = ETIMEDOUT; u_drop(j); } x = u + j; x->start = milliseconds(); len = xsocket_recv(udp53, mytypeincoming, buf, sizeof buf, x->ip, x->port, &x->scope_id); if (len == -1) return; if (len >= sizeof buf) return; port = crypto_uint16_load_bigendian(x->port); if (port < 1024) if (port != 53) return; if (!flagokclient && !okclient(x->ip)) { log_queryreject(x->ip, x->port, 0, 0, 0, "IP address not allowed"); return; } if (!packetquery(buf, len, &q, qtype, qclass, x->id)) { log_queryreject(x->ip, x->port, x->id, q, qtype, "bad query"); return; } if (u_duplicatequerycount(q, qtype) >= MAXUDPDUPLICATED) { log_queryreject(x->ip, x->port, x->id, q, qtype, "too many duplicate queries"); return; } x->active = ++log_numqueries; ++log_uactive; log_query(&x->active, x->ip, x->port, x->id, q, qtype); switch(query_start(&x->q, q, qtype, qclass, myipoutgoing)) { case -1: u_drop(j); return; case 1: u_respond(j); } } static int tcp53; #define MAXTCP 20 struct tcpclient { struct query q; long long start; long long timeout; crypto_uint64 active; /* query number or 1, if active; otherwise 0 */ struct pollfd *io; unsigned char ip[16]; /* send response to this address */ unsigned char port[2]; /* send response to this port */ long long scope_id; int type; unsigned char id[2]; int tcp; /* open TCP socket, if active */ int state; unsigned char *buf; /* 0, or dynamically allocated of length len */ long long len; long long pos; } t[MAXTCP]; /* state 1: buf 0; normal state at beginning of TCP connection state 2: buf 0; have read 1 byte of query packet length into len state 3: buf allocated; have read pos bytes of buf state 0: buf 0; handling query in q state -1: buf allocated; have written pos bytes */ static void t_free(long long j) { if (!t[j].buf) return; alloc_free(t[j].buf); t[j].buf = 0; } static void t_timeout(long long j) { if (!t[j].active) return; t[j].timeout = milliseconds() + 10000; } static void t_close(long long j) { if (!t[j].active) return; t_free(j); log_tcpclose(t[j].ip, t[j].port); close(t[j].tcp); t[j].active = 0; --log_tactive; } static void t_drop(long long j) { log_querydrop(&t[j].active); errno = EPIPE; t_close(j); } static void t_respond(long long j) { if (!t[j].active) return; log_querydone(&t[j].active, response_len); response_id(t[j].id); t[j].len = response_len + 2; t_free(j); t[j].buf = alloc(response_len + 2); if (!t[j].buf) { t_close(j); return; } crypto_uint16_store_bigendian(t[j].buf, response_len); byte_copy(t[j].buf + 2, response_len, response); t[j].pos = 0; t[j].state = -1; } static void t_rw(long long j) { struct tcpclient *x; unsigned char ch; static unsigned char *q = 0; unsigned char qtype[2]; unsigned char qclass[2]; long long r; x = t + j; if (x->state == -1) { r = write(x->tcp, x->buf + x->pos, x->len - x->pos); if (r <= 0) { t_close(j); return; } x->pos += r; if (x->pos == x->len) { t_free(j); x->state = 1; /* could drop connection immediately */ } return; } if (x->state == 1) { r = read(x->tcp, &ch, 1); if (r == 0) { errno = EPIPE; t_close(j); return; } if (r < 0) { t_close(j); return; } x->len = (unsigned char) ch; x->len <<= 8; x->state = 2; return; } if (x->state == 2) { r = read(x->tcp, &ch, 1); if (r == 0) { errno = EPIPE; t_close(j); return; } if (r < 0) { t_close(j); return; } x->len += (unsigned char) ch; if (!x->len) { errno = EPROTO; t_close(j); return; } x->buf = alloc(x->len); if (!x->buf) { t_close(j); return; } x->pos = 0; x->state = 3; return; } if (x->state != 3) return; /* impossible */ r = read(x->tcp, x->buf + x->pos, x->len - x->pos); if (r == 0) { errno = EPIPE; t_close(j); return; } if (r < 0) { t_close(j); return; } x->pos += r; if (x->pos < x->len) return; if (!packetquery(x->buf, x->len, &q, qtype, qclass, x->id)) { log_queryreject(x->ip, x->port, x->id, q, qtype, "bad query"); t_close(j); return; } x->active = ++log_numqueries; log_query(&x->active, x->ip, x->port, x->id, q, qtype); switch(query_start(&x->q,q,qtype,qclass,myipoutgoing)) { case -1: t_drop(j); return; case 1: t_respond(j); return; } t_free(j); x->state = 0; } static void t_new(void) { long long i; long long j; struct tcpclient *x; crypto_uint16 port; for (j = 0;j < MAXTCP;++j) if (!t[j].active) break; if (j >= MAXTCP) { j = 0; for (i = 1;i < MAXTCP;++i) if (t[i].start < t[j].start) j = i; errno = ETIMEDOUT; if (t[j].state == 0) t_drop(j); else t_close(j); } x = t + j; x->start = milliseconds(); x->tcp = xsocket_accept(tcp53, mytypeincoming, x->ip, x->port, &x->scope_id); if (x->tcp == -1) return; port = crypto_uint16_load_bigendian(x->port); if (port < 1024) if (port != 53) { close(x->tcp); return; } if (!flagokclient && !okclient(x->ip)) { log_queryreject(x->ip, x->port, 0, 0, 0, "IP address not allowed"); close(x->tcp); return; } blocking_disable(x->tcp); x->active = 1; ++log_tactive; x->state = 1; t_timeout(j); log_tcpopen(x->ip,x->port); } static struct pollfd io[3 + MAXUDP + MAXTCP]; static struct pollfd *udp53io; static struct pollfd *tcp53io; static void doit(void) { long long j; long long deadline; long long stamp; long long timeout; long long iolen; int r; for (;;) { stamp = milliseconds(); deadline = stamp + 120000; iolen = 0; udp53io = io + iolen++; udp53io->fd = udp53; udp53io->events = POLLIN; tcp53io = io + iolen++; tcp53io->fd = tcp53; tcp53io->events = POLLIN; for (j = 0;j < MAXUDP;++j) { if (u[j].active) { u[j].io = io + iolen++; query_io(&u[j].q,u[j].io,&deadline); } } for (j = 0;j < MAXTCP;++j) { if (t[j].active) { t[j].io = io + iolen++; if (t[j].state == 0) query_io(&t[j].q,t[j].io,&deadline); else { if (t[j].timeout < deadline) deadline = t[j].timeout; t[j].io->fd = t[j].tcp; t[j].io->events = (t[j].state > 0) ? POLLIN : POLLOUT; } } } timeout = deadline - stamp; if (timeout < 0) timeout = 10; poll(io, iolen, timeout); for (j = 0;j < MAXUDP;++j) if (u[j].active) { r = query_get(&u[j].q,u[j].io,stamp); if (r == -1) u_drop(j); if (r == 1) u_respond(j); } for (j = 0;j < MAXTCP;++j) if (t[j].active) { if (t[j].io->revents) t_timeout(j); if (t[j].state == 0) { r = query_get(&t[j].q,t[j].io,stamp); if (r == -1) t_drop(j); if (r == 1) t_respond(j); } else if (t[j].io->revents || (t[j].timeout < stamp)) t_rw(j); } if (udp53io) if (udp53io->revents) u_new(); if (tcp53io) if (tcp53io->revents) t_new(); } } static unsigned char skseed[32]; static unsigned char sk[32 + 16]; #define FATAL "dqcache: fatal: " #define WARNING "dqcache: warning: " static void removesecrets(void) { query_purge(); dns_nonce_purge(); purge(skseed, sizeof skseed); purge(sk, sizeof sk); } static void die_fatal(const char *trouble, const char *fn) { removesecrets(); if (errno) { if (fn) die_7(111, FATAL, trouble, " ", fn, ": ", e_str(errno), "\n"); die_5(111, FATAL, trouble, ": ", e_str(errno), "\n"); } if (fn) die_5(111, FATAL, trouble, " ", fn, "\n"); die_3(111, FATAL, trouble, "\n"); } static char *dnscurvetype = 0; static void reload(int sig) { if (!roots_init(dnscurvetype)) die_fatal("unable to read servers", 0); } static void dump(int sig){ if (cache_dump() == -1) warn_4(WARNING, "unable to dump cache: ", e_str(errno), "\n"); } static void exitasap(int sig){ removesecrets(); dump(0); die_0(0); } static void clean(int sig){ cache_clean(); } int main(int argc, char **argv) { long long cachesize, ll; unsigned char port[2]; char *x; signal(SIGPIPE, SIG_IGN); signal(SIGHUP, reload); signal(SIGALRM, dump); signal(SIGINT, clean); signal(SIGTERM, exitasap); if (!strtoip(myipincoming, env_get("IP"))) { byte_copy(myipincoming, 16, xsocket_ANYIP); } mytypeincoming = xsocket_type(myipincoming); udp53 = xsocket_udp(mytypeincoming); if (udp53 == -1) die_fatal("unable to create UDP socket", 0); if (xsocket_bind_reuse(udp53, mytypeincoming, myipincoming, myport, 0) == -1) die_fatal("unable to bind UDP socket", 0); tcp53 = xsocket_tcp(mytypeincoming); if (tcp53 == -1) die_fatal("unable to create TCP socket", 0); if (xsocket_bind_reuse(tcp53, mytypeincoming, myipincoming, myport, 0) == -1) die_fatal("unable to bind TCP socket", 0); randombytes(skseed, sizeof skseed); x = env_get("SECRETKEY"); if (x) { if (!hexparse(skseed, sizeof skseed, x)) { warn_2(WARNING, "unable to parse $SECRETKEY\n"); randombytes(skseed, sizeof skseed); } while (*x) { *x = 0; ++x; } } droproot(FATAL); dns_keys_derive(sk, sizeof sk, skseed); query_init(sk); x = env_get("NONCESTART"); if (!dns_nonce_init(x, sk + 32)) die_fatal("too long $NONCESTART", x); purge(skseed, sizeof skseed); purge(sk, sizeof sk); dns_transmit_magic(env_get("QUERYMAGIC"), env_get("RESPONSEMAGIC")); xsocket_tryreservein(udp53, 131072); if (!strtoip(myipoutgoing, env_get("IPSEND4"))) { byte_copy(myipoutgoing, 16, xsocket_ANYIP4); } if (!strtoip(myipoutgoing + 16, env_get("IPSEND6"))) { byte_copy(myipoutgoing + 16, 16, xsocket_ANYIP6); } if (portparse(port, env_get("REMOTEPORT"))) { query_remoteport(port); } if (!strtonum(&cachesize, env_get("CACHESIZE"))) { cachesize = 10000000; } if (!cache_init(cachesize)) die_fatal("not enough memory for cache of size", numtostr(0, cachesize)); if (env_get("HIDETTL")) response_hidettl(); if (env_get("FORWARDONLY")) query_forwardonly(); if (env_get("TCPONLY")) query_tcponly(); if (env_get("DISABLEIPV6")) query_ipv4only(); if (strtonum(&ll, env_get("MINTTL"))) query_minttl(ll); if (env_get("OKCLIENT")) flagokclient = 1; dnscurvetype = env_get("DNSCURVETYPE"); query_dnscurvetype(dnscurvetype); if (!roots_init(dnscurvetype)) die_fatal("unable to read servers", 0); if (xsocket_listen(tcp53, 20) == -1) die_fatal("unable to listen on TCP socket", 0); if (cache_load() == -1) warn_4(WARNING, "unable to load cache: ", e_str(errno), "\n"); log_startup(); doit(); return 111; } dq-20250201/droproot.c000066400000000000000000000030771474734131400144060ustar00rootroot00000000000000#include #include #include #include "env.h" #include "die.h" #include "strtonum.h" #include "e.h" #include "droproot.h" static void die_fatal(const char *fatal, const char *trouble, const char *fn) { if (errno) { if (fn) die_7(111, fatal, trouble, " ", fn, ": ", e_str(errno), "\n"); die_5(111, fatal, trouble, ": ", e_str(errno), "\n"); } if (fn) die_5(111, fatal, trouble, " ", fn, "\n"); die_3(111, fatal, trouble, "\n"); } void droproot(const char *fatal) { char *x; long long id; gid_t gid; uid_t uid; x = env_get("ROOT"); if (!x) die_fatal(fatal, "$ROOT not set", 0); if (chdir(x) == -1) die_fatal(fatal, "unable to chdir to", x); if (chroot(".") == -1) die_fatal(fatal, "unable to chroot to", x); x = env_get("GID"); if (!x) die_fatal(fatal, "$GID not set", 0); if (!strtonum(&id, x)) die_fatal(fatal, "unable to parse $GID", 0); gid = id; if (id != (long long)gid) die_fatal(fatal, "bad $GID", 0); if (setgroups(1, &gid) == -1) die_fatal(fatal, "unable to setgid", 0); if (setgid(gid) == -1) die_fatal(fatal, "unable to setgid", 0); if (getgid() != gid) die_fatal(fatal, "unable to setgid", 0); x = env_get("UID"); if (!x) die_fatal(fatal, "$UID not set", 0); if (!strtonum(&id, x)) die_fatal(fatal, "unable to parse $UID", 0); uid = id; if (id != (long long)uid) die_fatal(fatal, "bad $UID", 0); if (setuid(uid) == -1) die_fatal(fatal, "unable to setuid", 0); if (getuid() != uid) die_fatal(fatal, "unable to setuid", 0); } dq-20250201/droproot.h000066400000000000000000000001351474734131400144030ustar00rootroot00000000000000#ifndef _DROPROOT_H____ #define _DROPROOT_H____ extern void droproot(const char *); #endif dq-20250201/e.c000066400000000000000000000070341474734131400127570ustar00rootroot00000000000000#include "e.h" #define X(e,s) if (i == e) return s; const char *e_str(int i) { X(0,"no error"); X(EINTR,"interrupted system call") X(ENOMEM,"out of memory") X(ENOENT,"file does not exist") X(ETXTBSY,"text busy") X(EIO,"input/output error") X(EEXIST,"file already exists") X(ETIMEDOUT,"timed out") X(EINPROGRESS,"operation in progress") X(EAGAIN,"temporary failure") X(EWOULDBLOCK,"input/output would block") X(EPIPE,"broken pipe") X(EPERM,"permission denied") X(EACCES,"access denied") X(ENODEV,"device not configured") X(EPROTO,"protocol error") X(EISDIR,"is a directory") X(ESRCH,"no such process") X(E2BIG,"argument list too long") X(ENOEXEC,"exec format error") X(EBADF,"file descriptor not open") X(ECHILD,"no child processes") X(EDEADLK,"operation would cause deadlock") X(EFAULT,"bad address") X(ENOTBLK,"not a block device") X(EBUSY,"device busy") X(EXDEV,"cross-device link") X(ENODEV,"device does not support operation") X(ENOTDIR,"not a directory") X(EINVAL,"invalid argument") X(ENFILE,"system cannot open more files") X(EMFILE,"process cannot open more files") X(ENOTTY,"not a tty") X(EFBIG,"file too big") X(ENOSPC,"out of disk space") X(ESPIPE,"unseekable descriptor") X(EROFS,"read-only file system") X(EMLINK,"too many links") X(EDOM,"input out of range") X(ERANGE,"output out of range") X(EALREADY,"operation already in progress") X(ENOTSOCK,"not a socket") X(EDESTADDRREQ,"destination address required") X(EMSGSIZE,"message too long") X(EPROTOTYPE,"incorrect protocol type") X(ENOPROTOOPT,"protocol not available") X(EPROTONOSUPPORT,"protocol not supported") X(ESOCKTNOSUPPORT,"socket type not supported") X(EOPNOTSUPP,"operation not supported") X(EPFNOSUPPORT,"protocol family not supported") X(EAFNOSUPPORT,"address family not supported") X(EADDRINUSE,"address already used") X(EADDRNOTAVAIL,"address not available") X(ENETDOWN,"network down") X(ENETUNREACH,"network unreachable") X(ENETRESET,"network reset") X(ECONNABORTED,"connection aborted") X(ECONNRESET,"connection reset") X(ENOBUFS,"out of buffer space") X(EISCONN,"already connected") X(ENOTCONN,"not connected") X(ESHUTDOWN,"socket shut down") X(ETOOMANYREFS,"too many references") X(ECONNREFUSED,"connection refused") X(ELOOP,"symbolic link loop") X(ENAMETOOLONG,"file name too long") X(EHOSTDOWN,"host down") X(EHOSTUNREACH,"host unreachable") X(ENOTEMPTY,"directory not empty") X(EPROCLIM,"too many processes") X(EUSERS,"too many users") X(EDQUOT,"disk quota exceeded") X(ESTALE,"stale NFS file handle") X(EREMOTE,"too many levels of remote in path") X(EBADRPC,"RPC structure is bad") X(ERPCMISMATCH,"RPC version mismatch") X(EPROGUNAVAIL,"RPC program unavailable") X(EPROGMISMATCH,"program version mismatch") X(EPROCUNAVAIL,"bad procedure for program") X(ENOLCK,"no locks available") X(ENOSYS,"system call not available") X(EFTYPE,"bad file type") X(EAUTH,"authentication error") X(ENEEDAUTH,"not authenticated") X(ENOSTR,"not a stream device") X(ETIME,"timer expired") X(ENOSR,"out of stream resources") X(ENOMSG,"no message of desired type") X(EBADMSG,"bad message type") X(EIDRM,"identifier removed") X(ENONET,"machine not on network") X(EREMOTE,"object not local") X(ENOLINK,"link severed") X(EADV,"advertise error") X(ESRMNT,"srmount error") X(ECOMM,"communication error") X(EMULTIHOP,"multihop attempted") X(EREMCHG,"remote address changed") /* XXX */ X(DNSNOENT,"dns record doesn't exist") return "unknown error"; } dq-20250201/e.h000066400000000000000000000156021474734131400127640ustar00rootroot00000000000000#ifndef E_H #define E_H #include extern const char *e_str(int); #ifndef EPERM #define EPERM (-5001) #endif #ifndef ENOENT #define ENOENT (-5002) #endif #ifndef ESRCH #define ESRCH (-5003) #endif #ifndef EINTR #define EINTR (-5004) #endif #ifndef EIO #define EIO (-5005) #endif #ifndef ENXIO #define ENXIO (-5006) #endif #ifndef E2BIG #define E2BIG (-5007) #endif #ifndef ENOEXEC #define ENOEXEC (-5008) #endif #ifndef EBADF #define EBADF (-5009) #endif #ifndef ECHILD #define ECHILD (-5010) #endif #ifndef EAGAIN #define EAGAIN (-5011) #endif #ifndef EWOULDBLOCK #define EWOULDBLOCK (-7011) #endif #ifndef ENOMEM #define ENOMEM (-5012) #endif #ifndef EACCES #define EACCES (-5013) #endif #ifndef EFAULT #define EFAULT (-5014) #endif #ifndef ENOTBLK #define ENOTBLK (-5015) #endif #ifndef EBUSY #define EBUSY (-5016) #endif #ifndef EEXIST #define EEXIST (-5017) #endif #ifndef EXDEV #define EXDEV (-5018) #endif #ifndef ENODEV #define ENODEV (-5019) #endif #ifndef ENOTDIR #define ENOTDIR (-5020) #endif #ifndef EISDIR #define EISDIR (-5021) #endif #ifndef EINVAL #define EINVAL (-5022) #endif #ifndef ENFILE #define ENFILE (-5023) #endif #ifndef EMFILE #define EMFILE (-5024) #endif #ifndef ENOTTY #define ENOTTY (-5025) #endif #ifndef ETXTBSY #define ETXTBSY (-5026) #endif #ifndef EFBIG #define EFBIG (-5027) #endif #ifndef ENOSPC #define ENOSPC (-5028) #endif #ifndef ESPIPE #define ESPIPE (-5029) #endif #ifndef EROFS #define EROFS (-5030) #endif #ifndef EMLINK #define EMLINK (-5031) #endif #ifndef EPIPE #define EPIPE (-5032) #endif #ifndef EDOM #define EDOM (-5033) #endif #ifndef ERANGE #define ERANGE (-5034) #endif #ifndef EDEADLK #define EDEADLK (-5035) #endif #ifndef EDEADLOCK #define EDEADLOCK (-7035) #endif #ifndef ENAMETOOLONG #define ENAMETOOLONG (-5036) #endif #ifndef ENOLCK #define ENOLCK (-5037) #endif #ifndef ENOSYS #define ENOSYS (-5038) #endif #ifndef ENOTEMPTY #define ENOTEMPTY (-5039) #endif #ifndef ELOOP #define ELOOP (-5040) #endif #ifndef ENOMSG #define ENOMSG (-5042) #endif #ifndef EIDRM #define EIDRM (-5043) #endif #ifndef ECHRNG #define ECHRNG (-5044) #endif #ifndef EL2NSYNC #define EL2NSYNC (-5045) #endif #ifndef EL3HLT #define EL3HLT (-5046) #endif #ifndef EL3RST #define EL3RST (-5047) #endif #ifndef ELNRNG #define ELNRNG (-5048) #endif #ifndef EUNATCH #define EUNATCH (-5049) #endif #ifndef ENOCSI #define ENOCSI (-5050) #endif #ifndef EL2HLT #define EL2HLT (-5051) #endif #ifndef EBADE #define EBADE (-5052) #endif #ifndef EBADR #define EBADR (-5053) #endif #ifndef EXFULL #define EXFULL (-5054) #endif #ifndef ENOANO #define ENOANO (-5055) #endif #ifndef EBADRQC #define EBADRQC (-5056) #endif #ifndef EBADSLT #define EBADSLT (-5057) #endif #ifndef EBFONT #define EBFONT (-5059) #endif #ifndef ENOSTR #define ENOSTR (-5060) #endif #ifndef ENODATA #define ENODATA (-5061) #endif #ifndef ETIME #define ETIME (-5062) #endif #ifndef ENOSR #define ENOSR (-5063) #endif #ifndef ENONET #define ENONET (-5064) #endif #ifndef ENOPKG #define ENOPKG (-5065) #endif #ifndef EREMOTE #define EREMOTE (-5066) #endif #ifndef ENOLINK #define ENOLINK (-5067) #endif #ifndef EADV #define EADV (-5068) #endif #ifndef ESRMNT #define ESRMNT (-5069) #endif #ifndef ECOMM #define ECOMM (-5070) #endif #ifndef EPROTO #define EPROTO (-5071) #endif #ifndef EMULTIHOP #define EMULTIHOP (-5072) #endif #ifndef EDOTDOT #define EDOTDOT (-5073) #endif #ifndef EBADMSG #define EBADMSG (-5074) #endif #ifndef EOVERFLOW #define EOVERFLOW (-5075) #endif #ifndef ENOTUNIQ #define ENOTUNIQ (-5076) #endif #ifndef EBADFD #define EBADFD (-5077) #endif #ifndef EREMCHG #define EREMCHG (-5078) #endif #ifndef ELIBACC #define ELIBACC (-5079) #endif #ifndef ELIBBAD #define ELIBBAD (-5080) #endif #ifndef ELIBSCN #define ELIBSCN (-5081) #endif #ifndef ELIBMAX #define ELIBMAX (-5082) #endif #ifndef ELIBEXEC #define ELIBEXEC (-5083) #endif #ifndef EILSEQ #define EILSEQ (-5084) #endif #ifndef ERESTART #define ERESTART (-5085) #endif #ifndef ESTRPIPE #define ESTRPIPE (-5086) #endif #ifndef EUSERS #define EUSERS (-5087) #endif #ifndef ENOTSOCK #define ENOTSOCK (-5088) #endif #ifndef EDESTADDRREQ #define EDESTADDRREQ (-5089) #endif #ifndef EMSGSIZE #define EMSGSIZE (-5090) #endif #ifndef EPROTOTYPE #define EPROTOTYPE (-5091) #endif #ifndef ENOPROTOOPT #define ENOPROTOOPT (-5092) #endif #ifndef EPROTONOSUPPORT #define EPROTONOSUPPORT (-5093) #endif #ifndef ESOCKTNOSUPPORT #define ESOCKTNOSUPPORT (-5094) #endif #ifndef EOPNOTSUPP #define EOPNOTSUPP (-5095) #endif #ifndef EPFNOSUPPORT #define EPFNOSUPPORT (-5096) #endif #ifndef EAFNOSUPPORT #define EAFNOSUPPORT (-5097) #endif #ifndef EADDRINUSE #define EADDRINUSE (-5098) #endif #ifndef EADDRNOTAVAIL #define EADDRNOTAVAIL (-5099) #endif #ifndef ENETDOWN #define ENETDOWN (-5100) #endif #ifndef ENETUNREACH #define ENETUNREACH (-5101) #endif #ifndef ENETRESET #define ENETRESET (-5102) #endif #ifndef ECONNABORTED #define ECONNABORTED (-5103) #endif #ifndef ECONNRESET #define ECONNRESET (-5104) #endif #ifndef ENOBUFS #define ENOBUFS (-5105) #endif #ifndef EISCONN #define EISCONN (-5106) #endif #ifndef ENOTCONN #define ENOTCONN (-5107) #endif #ifndef ESHUTDOWN #define ESHUTDOWN (-5108) #endif #ifndef ETOOMANYREFS #define ETOOMANYREFS (-5109) #endif #ifndef ETIMEDOUT #define ETIMEDOUT (-5110) #endif #ifndef ECONNREFUSED #define ECONNREFUSED (-5111) #endif #ifndef EHOSTDOWN #define EHOSTDOWN (-5112) #endif #ifndef EHOSTUNREACH #define EHOSTUNREACH (-5113) #endif #ifndef EALREADY #define EALREADY (-5114) #endif #ifndef EINPROGRESS #define EINPROGRESS (-5115) #endif #ifndef ESTALE #define ESTALE (-5116) #endif #ifndef EUCLEAN #define EUCLEAN (-5117) #endif #ifndef ENOTNAM #define ENOTNAM (-5118) #endif #ifndef ENAVAIL #define ENAVAIL (-5119) #endif #ifndef EISNAM #define EISNAM (-5120) #endif #ifndef EREMOTEIO #define EREMOTEIO (-5121) #endif #ifndef EDQUOT #define EDQUOT (-5122) #endif #ifndef ENOMEDIUM #define ENOMEDIUM (-5123) #endif #ifndef EMEDIUMTYPE #define EMEDIUMTYPE (-5124) #endif #ifndef ECANCELED #define ECANCELED (-5125) #endif #ifndef ENOKEY #define ENOKEY (-5126) #endif #ifndef EKEYEXPIRED #define EKEYEXPIRED (-5127) #endif #ifndef EKEYREVOKED #define EKEYREVOKED (-5128) #endif #ifndef EKEYREJECTED #define EKEYREJECTED (-5129) #endif #ifndef EOWNERDEAD #define EOWNERDEAD (-5130) #endif #ifndef ENOTRECOVERABLE #define ENOTRECOVERABLE (-5131) #endif #ifndef ERFKILL #define ERFKILL (-5132) #endif #ifndef EPROCLIM #define EPROCLIM (-6067) #endif #ifndef EBADRPC #define EBADRPC (-6072) #endif #ifndef ERPCMISMATCH #define ERPCMISMATCH (-6073) #endif #ifndef EPROGUNAVAIL #define EPROGUNAVAIL (-6074) #endif #ifndef EPROGMISMATCH #define EPROGMISMATCH (-6075) #endif #ifndef EPROCUNAVAIL #define EPROCUNAVAIL (-6076) #endif #ifndef EFTYPE #define EFTYPE (-6079) #endif #ifndef EAUTH #define EAUTH (-6080) #endif #ifndef ENEEDAUTH #define ENEEDAUTH (-6081) #endif #ifndef ENOATTR #define ENOATTR (-6087) #endif #ifndef ENOTCAPABLE #define ENOTCAPABLE (-6093) #endif /* XXX */ #ifndef DNSNOENT #define DNSNOENT (-7000) #endif #endif dq-20250201/env.c000066400000000000000000000006761474734131400133300ustar00rootroot00000000000000#include "str.h" #include "env.h" /* The 'env_get(name)' function returns the value of the first environment variable whose name is 'name', or 0 if there is no such variable. */ char *env_get(const char *name) { long long len, i; if (!name) return 0; len = str_len(name); for (i = 0; environ[i]; ++i) if (str_start(environ[i], name) && (environ[i][len] == '=')) return environ[i] + len + 1; return 0; } dq-20250201/env.h000066400000000000000000000004201474734131400133200ustar00rootroot00000000000000#ifndef _ENV_H____ #define _ENV_H____ /* The environment 'environ', is a 0-terminated array of 0-terminated strings, called environment variables. Each environment variable is of the form name=value. */ extern char **environ; extern char *env_get(const char *); #endif dq-20250201/examples.md000066400000000000000000000210171474734131400145240ustar00rootroot00000000000000### Authoritative example - streamlined DNSCurve query (executed 23.11.2013): ### ~~~ $ dq -a a dnscurve.org uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org 1 dnscurve.org - streamlined DNSCurve: 320 bytes, 1+2+3+3 records, response, authoritative, noerror query: 1 dnscurve.org answer: dnscurve.org 600 A 131.193.32.142 answer: dnscurve.org 600 A 131.193.32.147 authority: dnscurve.org 259200 NS uz5hjgptn63q5qlch6xlrw63tf6vhvvu6mjwn0s31buw1lhmlk14kd.ns.dnscurve.org authority: dnscurve.org 259200 NS uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org authority: dnscurve.org 259200 NS uz5uu2c7j228ujjccp3ustnfmr4pgcg5ylvt16kmd0qzw7bbjgd5xq.ns.dnscurve.org additional: uz5hjgptn63q5qlch6xlrw63tf6vhvvu6mjwn0s31buw1lhmlk14kd.ns.dnscurve.org 259200 A 131.193.32.142 additional: uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org 259200 A 131.155.71.143 additional: uz5uu2c7j228ujjccp3ustnfmr4pgcg5ylvt16kmd0qzw7bbjgd5xq.ns.dnscurve.org 259200 A 80.101.159.118 ~~~ ### Authoritative example - TXT DNSCurve query, using suffix 'dnscurve.org' (executed 23.11.2013): ### ~~~ $ dq -S 'dnscurve.org' -a a dnscurve.org uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org 1 dnscurve.org - txt DNSCurve: 320 bytes, 1+2+3+3 records, response, authoritative, noerror query: 1 dnscurve.org answer: dnscurve.org 600 A 131.193.32.142 answer: dnscurve.org 600 A 131.193.32.147 authority: dnscurve.org 259200 NS uz5hjgptn63q5qlch6xlrw63tf6vhvvu6mjwn0s31buw1lhmlk14kd.ns.dnscurve.org authority: dnscurve.org 259200 NS uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org authority: dnscurve.org 259200 NS uz5uu2c7j228ujjccp3ustnfmr4pgcg5ylvt16kmd0qzw7bbjgd5xq.ns.dnscurve.org additional: uz5hjgptn63q5qlch6xlrw63tf6vhvvu6mjwn0s31buw1lhmlk14kd.ns.dnscurve.org 259200 A 131.193.32.142 additional: uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org 259200 A 131.155.71.143 additional: uz5uu2c7j228ujjccp3ustnfmr4pgcg5ylvt16kmd0qzw7bbjgd5xq.ns.dnscurve.org 259200 A 80.101.159.118 ~~~ ### Authoritative example - streamlined DNSCurve query, using IPv4 and key (executed 23.11.2013): ### ~~~ dq -k uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090 -a a dnscurve.org 131.155.71.143 1 dnscurve.org - streamlined DNSCurve: 320 bytes, 1+2+3+3 records, response, authoritative, noerror query: 1 dnscurve.org answer: dnscurve.org 600 A 131.193.32.147 answer: dnscurve.org 600 A 131.193.32.142 authority: dnscurve.org 259200 NS uz5hjgptn63q5qlch6xlrw63tf6vhvvu6mjwn0s31buw1lhmlk14kd.ns.dnscurve.org authority: dnscurve.org 259200 NS uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org authority: dnscurve.org 259200 NS uz5uu2c7j228ujjccp3ustnfmr4pgcg5ylvt16kmd0qzw7bbjgd5xq.ns.dnscurve.org additional: uz5hjgptn63q5qlch6xlrw63tf6vhvvu6mjwn0s31buw1lhmlk14kd.ns.dnscurve.org 259200 A 131.193.32.142 additional: uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org 259200 A 131.155.71.143 additional: uz5uu2c7j228ujjccp3ustnfmr4pgcg5ylvt16kmd0qzw7bbjgd5xq.ns.dnscurve.org 259200 A 80.101.159.118 ~~~ ### Authoritative example - streamlined DNSCurve query, using IPv6 and key (executed 23.11.2013): ### ~~~ $ dq -a -k uz52zufj724bjt11s3pgk2dtcpw8ww1dr8g3ssm3hs3jkmwr8l090p a blinkenlights.ch 2a01:4f8:120:43c1::a 1 blinkenlights.ch - streamlined DNSCurve: 341 bytes, 1+1+3+3 records, response, authoritative, noerror query: 1 blinkenlights.ch answer: blinkenlights.ch 86400 A 78.46.72.150 authority: blinkenlights.ch 259200 NS uz5crn6x92t4vb4k3z68du7rmwmnnvkbdd29t79yzg9fr2s2rx5pk0.nsde1.eqmx.net authority: blinkenlights.ch 259200 NS uz5mnv8n4dzrp95zl50jryb4wgf45my27q6pvx8f540l9sspkcwvtm.nszh1.eqmx.net authority: blinkenlights.ch 259200 NS uz52zufj724bjt11s3pgk2dtcpw8ww1dr8g3ssm3hs3jkmwr8l090p.dns1.workaround.ch additional: uz5crn6x92t4vb4k3z68du7rmwmnnvkbdd29t79yzg9fr2s2rx5pk0.nsde1.eqmx.net 86400 A 178.209.50.169 additional: uz5mnv8n4dzrp95zl50jryb4wgf45my27q6pvx8f540l9sspkcwvtm.nszh1.eqmx.net 86400 A 46.246.93.162 additional: uz52zufj724bjt11s3pgk2dtcpw8ww1dr8g3ssm3hs3jkmwr8l090p.dns1.workaround.ch 86400 A 78.46.72.150 ~~~ ### Recursive example (executed 23.11.2013): ### ~~~ $ dq a dnscurve.org 1 dnscurve.org - regular DNS: 272 bytes, 1+2+3+0 records, response, noerror query: 1 dnscurve.org answer: dnscurve.org 600 A 131.193.32.147 answer: dnscurve.org 600 A 131.193.32.142 authority: dnscurve.org 259200 NS uz5hjgptn63q5qlch6xlrw63tf6vhvvu6mjwn0s31buw1lhmlk14kd.ns.dnscurve.org authority: dnscurve.org 259200 NS uz5dz39x8xk8wyq3dzn7vpt670qmvzx0zd9zg4ldwldkv6kx9ft090.ns.dnscurve.org authority: dnscurve.org 259200 NS uz5uu2c7j228ujjccp3ustnfmr4pgcg5ylvt16kmd0qzw7bbjgd5xq.ns.dnscurve.org ~~~ ### Recursive example, using OpenDNS resolver - IPv6 (executed 23.11.2013): ### ~~~ $ dq a dnscurve.org 2620:0:ccc::2 1 dnscurve.org - regular DNS: 62 bytes, 1+2+0+0 records, response, noerror query: 1 dnscurve.org answer: dnscurve.org 600 A 131.193.32.147 answer: dnscurve.org 600 A 131.193.32.142 ~~~ ### Recursive example, using OpenDNS resolver - IPv4 (executed 23.11.2013): ### ~~~ $ dq a dnscurve.org 208.67.222.222 1 dnscurve.org - regular DNS: 62 bytes, 1+2+0+0 records, response, noerror query: 1 dnscurve.org answer: dnscurve.org 600 A 131.193.32.142 answer: dnscurve.org 600 A 131.193.32.147 ~~~ ### Recursive example, using OpenDNS resolver - IPv4, dnsqr backward compatibility (executed 23.11.2013): ### ~~~ env DNSCACHEIP=208.67.222.222 dq a dnscurve.org 1 dnscurve.org - regular DNS: 62 bytes, 1+2+0+0 records, response, noerror query: 1 dnscurve.org answer: dnscurve.org 600 A 131.193.32.142 answer: dnscurve.org 600 A 131.193.32.147 ~~~ ### Recursive example, PTR query (executed 23.11.2013): ### ~~~ $ dq ptr 1.0.0.127.in-addr.arpa 12 1.0.0.127.in-addr.arpa - regular DNS: 63 bytes, 1+1+0+0 records, response, noerror query: 12 1.0.0.127.in-addr.arpa answer: 1.0.0.127.in-addr.arpa 655360 PTR localhost ~~~ ### Recursive example, PTR query, auto-rewrite to *.in-addr.arpa (executed 23.11.2013): ### ~~~ $ dq ptr 127.0.0.1 12 1.0.0.127.in-addr.arpa - regular DNS: 63 bytes, 1+1+0+0 records, response, noerror query: 12 1.0.0.127.in-addr.arpa answer: 1.0.0.127.in-addr.arpa 655360 PTR localhost ~~~ ### Recursive example, PTR query, auto-rewrite to *.ip6.arpa (executed 23.11.2013): ### ~~~ $ dq ptr 2a01:4f8:120:43c1::aa 12 a.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.c.3.4.0.2.1.0.8.f.4.0.1.0.a.2.ip6.arpa - regular DNS: 90 bytes, 1+0+0+0 records, response, authoritative, nxdomain query: 12 a.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.c.3.4.0.2.1.0.8.f.4.0.1.0.a.2.ip6.arpa ~~~ ### Authoritative example, verbose mode, truncated answer (executed 23.11.2013): ### ~~~ $ dq -a -vv any google.com ns4.google.com dq: debug: resolving: ns4.google.com dq: debug: DNS query: ns4.google.com AAAA UDP 127.10.10.10 53: sent dq: debug: DNS query: ns4.google.com A UDP 127.10.10.10 53: sent dq: debug: DNS query: ns4.google.com A UDP 127.10.10.10 53: received dq: debug: DNS query: ns4.google.com AAAA UDP 127.10.10.10 53: received dq: debug: resolved: ns4.google.com: ns4.google.com 216.239.38.10 dq: debug: DNS query: google.com ANY UDP 216.239.38.10 53: sent dq: debug: DNS query: google.com ANY UDP 216.239.38.10 53: failed: truncated dq: debug: DNS query: google.com ANY TCP 216.239.38.10 53: sent dq: debug: DNS query: google.com ANY TCP 216.239.38.10 53: received 255 google.com - regular DNS: 577 bytes, 1+24+0+0 records, response, authoritative, noerror query: 255 google.com answer: google.com 300 A 173.194.112.97 answer: google.com 300 A 173.194.112.96 answer: google.com 300 A 173.194.112.104 answer: google.com 300 A 173.194.112.98 answer: google.com 300 A 173.194.112.99 answer: google.com 300 A 173.194.112.103 answer: google.com 300 A 173.194.112.110 answer: google.com 300 A 173.194.112.100 answer: google.com 300 A 173.194.112.102 answer: google.com 300 A 173.194.112.105 answer: google.com 300 A 173.194.112.101 answer: google.com 300 AAAA 2a00:1450:4001:803::1006 answer: google.com 86400 SOA ns1.google.com dns-admin.google.com 2013100300 7200 1800 1209600 300 answer: google.com 600 MX 10 aspmx.l.google.com answer: google.com 86400 257 \000\005issuesymantec.com answer: google.com 345600 NS ns3.google.com answer: google.com 345600 NS ns2.google.com answer: google.com 345600 NS ns1.google.com answer: google.com 600 MX 20 alt1.aspmx.l.google.com answer: google.com 600 MX 30 alt2.aspmx.l.google.com answer: google.com 345600 NS ns4.google.com answer: google.com 600 MX 50 alt4.aspmx.l.google.com answer: google.com 600 MX 40 alt3.aspmx.l.google.com answer: google.com 3600 TXT v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all ~~~ dq-20250201/haslib25519.c000066400000000000000000000041301474734131400143750ustar00rootroot00000000000000#include #if lib25519_nP_montgomery25519_SCALARBYTES != lib25519_dh_SECRETKEYBYTES #error #endif #if lib25519_nP_montgomery25519_POINTBYTES != lib25519_dh_PUBLICKEYBYTES #error #endif #if lib25519_nP_montgomery25519_POINTBYTES != lib25519_dh_BYTES #error #endif #if lib25519_nG_montgomery25519_SCALARBYTES != lib25519_dh_SECRETKEYBYTES #error #endif #if lib25519_nG_montgomery25519_POINTBYTES != lib25519_dh_PUBLICKEYBYTES #error #endif #if lib25519_nG_montgomery25519_POINTBYTES != lib25519_dh_BYTES #error #endif static unsigned char k[lib25519_dh_BYTES]; static unsigned char k2[lib25519_dh_BYTES]; static unsigned char pk[lib25519_dh_PUBLICKEYBYTES]; static unsigned char pk2[lib25519_dh_PUBLICKEYBYTES]; static unsigned char sk[lib25519_dh_SECRETKEYBYTES]; static unsigned char signsk[lib25519_sign_SECRETKEYBYTES]; static unsigned char signpk[lib25519_sign_PUBLICKEYBYTES]; static unsigned char signs[lib25519_sign_BYTES + /*m*/ lib25519_dh_BYTES]; static long long signslen; static long long klen; /* return zero for all inputs */ static unsigned char z(unsigned char x) { unsigned long long z = (unsigned long long) x + 1ULL; unsigned long long t = z; long long i; for (i = 6; i >= 0; --i) { t = (t * t) % 257; t = (t * z) % 257; } t = (t * z) % 257; return t - 1; } int main(int argc, char **argv) { unsigned char ret = 0; long long i; (void) argc; (void) argv; lib25519_dh_keypair(pk, sk); lib25519_nG_montgomery25519(pk2, sk); for (i = 0; i < lib25519_dh_PUBLICKEYBYTES; ++i) ret |= pk[i] ^ pk2[i]; lib25519_dh(k, pk, sk); lib25519_nP_montgomery25519(k2, sk, pk); for (i = 0; i < lib25519_dh_BYTES; ++i) ret |= k[i] ^ k2[i]; lib25519_sign_keypair(signpk, signsk); lib25519_sign(signs, &signslen, k, sizeof k, signsk); if (signslen != sizeof signs) ret |= 1; ret |= lib25519_sign_open(k2, &klen, signs, signslen, signpk); if (klen != sizeof k2) ret |= 1; for (i = 0; i < lib25519_dh_BYTES; ++i) ret |= k[i] ^ k2[i]; for (i = 0; i < sizeof signs; ++i) ret |= z(signs[i]); return ret; } dq-20250201/haslibrandombytes.c000066400000000000000000000013311474734131400162370ustar00rootroot00000000000000#include /* return zero for all inputs */ static unsigned char z(unsigned char x) { unsigned long long z = (unsigned long long) x + 1ULL; unsigned long long t = z; long long i; for (i = 6; i >= 0; --i) { t = (t * t) % 257; t = (t * z) % 257; } t = (t * z) % 257; return (unsigned char) t - 1; } int main(int argc, char **argv) { unsigned char buf[32], ret = 0; const char *source; unsigned long long i; (void) argc; (void) argv; randombytes(buf, sizeof buf); for (i = 0; i < sizeof buf; ++i) ret |= z(buf[i]); source = randombytes_source(); for (i = 0; source[i]; ++i) ret |= z((unsigned char) source[i]); return z(ret); } dq-20250201/hexdecode.c000066400000000000000000000012301474734131400144530ustar00rootroot00000000000000#include "hexdecode.h" static int hexdigit(char x) { if (x >= '0' && x <= '9') return x - '0'; if (x >= 'a' && x <= 'f') return 10 + (x - 'a'); if (x >= 'A' && x <= 'F') return 10 + (x - 'A'); return -1; } int hexdecode(unsigned char *y, long long len, const unsigned char *x, long long xlen) { int digit0; int digit1; if (!x) return 0; while (len > 0 && xlen >= 2) { digit0 = hexdigit(x[0]); if (digit0 == -1) return 0; digit1 = hexdigit(x[1]); if (digit1 == -1) return 0; *y++ = digit1 + 16 * digit0; --len; x += 2; xlen -= 2; } if (xlen != 0) return 0; return 1; } dq-20250201/hexdecode.h000066400000000000000000000002171474734131400144640ustar00rootroot00000000000000#ifndef _HEXDECODE_H____ #define _HEXDECODE_H____ extern int hexdecode(unsigned char *, long long, const unsigned char *, long long); #endif dq-20250201/hexparse.c000066400000000000000000000010561474734131400143500ustar00rootroot00000000000000#include "hexparse.h" static int hexdigit(char x) { if (x >= '0' && x <= '9') return x - '0'; if (x >= 'a' && x <= 'f') return 10 + (x - 'a'); if (x >= 'A' && x <= 'F') return 10 + (x - 'A'); return -1; } int hexparse(unsigned char *y,long long len,const char *x) { if (!x) return 0; while (len > 0) { int digit0; int digit1; digit0 = hexdigit(x[0]); if (digit0 == -1) return 0; digit1 = hexdigit(x[1]); if (digit1 == -1) return 0; *y++ = digit1 + 16 * digit0; --len; x += 2; } if (x[0]) return 0; return 1; } dq-20250201/hexparse.h000066400000000000000000000001541474734131400143530ustar00rootroot00000000000000#ifndef HEXPARSE_H #define HEXPARSE_H extern int hexparse(unsigned char *,long long,const char *); #endif dq-20250201/install.md000066400000000000000000000027211474734131400143550ustar00rootroot00000000000000### Debian installation ### ~~~ apt-get install dq apt-get install dqcache ~~~ ###Unix installation ### #### download #### ~~~ wget https://github.com/janmojzis/dq/releases/download/20250201/20250201.tar.gz.asc wget https://github.com/janmojzis/dq/archive/20250201.tar.gz gpg --verify 20250201.tar.gz.asc 20250201.tar.gz gunzip < 20250201.tar.gz | tar -xf - cd dq-20250201 ~~~ #### compile and install binaries #### ~~~ make sudo make install ~~~ #### run dqcache #### ~~~ #under root - create dqcache root directory sudo mkdir -p /etc/dqcache/root/servers /etc/dqcache/env sudo echo 10000000 > /etc/dqcache/env/CACHESIZE sudo echo 127.0.0.1 > /etc/dqcache/env/IP sudo echo "/etc/dqcache/root" > /etc/dqcache/env/ROOT ~~~ ~~~ #under root - setup dqcache root servers sudo sh -c '( echo "198.41.0.4" echo "2001:503:ba3e::2:30" echo "192.228.79.201" echo "2001:500:84::b" echo "192.33.4.12" echo "2001:500:2::c" echo "199.7.91.13" echo "2001:500:2d::d" echo "192.203.230.10" echo "192.5.5.241" echo "2001:500:2f::f" echo "192.112.36.4" echo "198.97.190.53" echo "2001:500:1::53" echo "192.36.148.17" echo "2001:7fe::53" echo "192.58.128.30" echo "2001:503:c27::2:30" echo "193.0.14.129" echo "2001:7fd::1" echo "199.7.83.42" echo "2001:500:9f::42" echo "202.12.27.33" echo "2001:dc3::35" ) > /etc/dqcache/root/servers/@' ~~~ ~~~ #under root - create dqcache user sudo useradd dqcache ~~~ ~~~ #under root - run dqcache server sudo envuidgid dqcache envdir /etc/dqcache/env dqcache ~~~ dq-20250201/int16_optblocker.c000077700000000000000000000000001474734131400232732cryptoint/int16_optblocker.custar00rootroot00000000000000dq-20250201/int32_optblocker.c000077700000000000000000000000001474734131400232672cryptoint/int32_optblocker.custar00rootroot00000000000000dq-20250201/int64_optblocker.c000077700000000000000000000000001474734131400233012cryptoint/int64_optblocker.custar00rootroot00000000000000dq-20250201/int8_optblocker.c000077700000000000000000000000001474734131400231352cryptoint/int8_optblocker.custar00rootroot00000000000000dq-20250201/iptostr.c000066400000000000000000000076101474734131400142370ustar00rootroot00000000000000/* 20130604 Jan Mojzis Public domain. */ #include "byte.h" #include "iptostr.h" static char *iptostr4(char *, const unsigned char *); static char *iptostr6(char *, const unsigned char *); static char *iptostr6x(char *, const unsigned char *); /* The 'iptostr(strbuf,ip)' function converts IP address 'ip' from network byte order into the 0-terminated string. The 'ip' length is always 16 bytes. The caller must allocate at least IPTOSTR_LEN bytes for 'strbuf'. */ char *iptostr(char *strbuf, const unsigned char *ip) { static char staticbuf[IPTOSTR_LEN]; if (!strbuf) strbuf = staticbuf; /* not thread-safe */ if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { return iptostr4(strbuf, ip + 12); } return iptostr6(strbuf, ip); } char *iptostrx(char *strbuf, const unsigned char *ip) { static char staticbuf[IPTOSTR_LEN]; if (!strbuf) strbuf = staticbuf; /* not thread-safe */ if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { return iptostr4(strbuf, ip + 12); } return iptostr6x(strbuf, ip); } /* convert IPv4 address */ static char *iptostr4(char *strbuf, const unsigned char *ip) { long long i, len = 0; unsigned long long num; for (i = 3; i >= 0; --i) { num = ip[i]; do { num /= 10; ++len; } while (num); if (i > 0) ++len; } strbuf += len; for (i = 3; i >= 0; --i) { num = ip[i]; do { *--strbuf = '0' + (num % 10); num /= 10; } while (num); if (i > 0) *--strbuf = '.'; } while (len < IPTOSTR_LEN) strbuf[len++] = 0; return strbuf; } /* The 'ip' is splited into 8 16-bit blocks and the 'countz' function counts successive zeros and searches for largest zero-block. 'first' ... first position of the zero-block 'last' ... last position of the zero-block */ static void countz(long long *first, long long *last, const unsigned long long *ip) { long long i, j, e; long long count[8]; for (i = 7; i >= 0; --i) count[i] = 0; e = 8; for (i = 7; i >= 0; --i) { if (!ip[i]) { for (j = i; j < e; ++j) ++count[j]; } else { e = i; } } e = 0; j = 0; for (i = 7; i >= 0; --i) { if (count[i]) { if (count[i] >= e) { e = count[i]; j = i; } } } *first = j - count[j] + 1; *last = j; } /* convert IPv6 address */ static char *iptostr6(char *strbuf, const unsigned char *ip) { long long first, last, i; unsigned long long ip2[8]; char *s = strbuf; for (i = 7; i >= 0; --i) { ip2[i] = ip[2 * i]; ip2[i] <<= 8; ip2[i] += ip[2 * i + 1]; } countz(&first, &last, ip2); strbuf += IPTOSTR_LEN - 1; *strbuf = 0; for (i = 7; i >= 0; --i) { if (i <= last && i >= first && first != last) { if (i == last) *--strbuf = ':'; if (i == 7) *--strbuf = ':'; continue; } do { *--strbuf = "0123456789abcdef"[ip2[i] & 15]; ip2[i] >>= 4; } while (ip2[i]); if (i > 0) *--strbuf = ':'; } i = strbuf - s; byte_copy(s, IPTOSTR_LEN - i, strbuf); byte_zero(s + IPTOSTR_LEN - i, i); return s; } /* convert IPv6 address without '::' compression */ static char *iptostr6x(char *strbuf, const unsigned char *ip) { long long i; unsigned long long ip2[8]; char *s = strbuf; for (i = 7; i >= 0; --i) { ip2[i] = ip[2 * i]; ip2[i] <<= 8; ip2[i] += ip[2 * i + 1]; } strbuf += IPTOSTR_LEN - 1; *strbuf = 0; for (i = 7; i >= 0; --i) { do { *--strbuf = "0123456789abcdef"[ip2[i] & 15]; ip2[i] >>= 4; } while (ip2[i]); if (i > 0) *--strbuf = ':'; } i = strbuf - s; byte_copy(s, IPTOSTR_LEN - i, strbuf); byte_zero(s + IPTOSTR_LEN - i, i); return s; } dq-20250201/iptostr.h000066400000000000000000000002721474734131400142410ustar00rootroot00000000000000#ifndef _IPTOSTR_H____ #define _IPTOSTR_H____ #define IPTOSTR_LEN 46 extern char *iptostr(char *, const unsigned char *); extern char *iptostrx(char *, const unsigned char *); #endif dq-20250201/keyparse.c000066400000000000000000000014451474734131400143560ustar00rootroot00000000000000/* * 20131204 * Jan Mojzis * Public domain. */ #include "hexdecode.h" #include "base32decode.h" #include "byte.h" #include "str.h" #include "keyparse.h" int keyparse(unsigned char *keys, long long keyslen, const char *xx) { long long len; const unsigned char *x = (const unsigned char *)xx; if (!xx) return 0; if (keyslen != 32) return 0; len = str_len(xx); switch (len) { case 54: if (!byte_isequal(x, 3, "uz5")) return 0; if (!base32decode(keys, 32, x + 3, 51)) return 0; break; case 51: if (!base32decode(keys, 32, x, 51)) return 0; break; case 64: if (!hexdecode(keys, 32, x, 64)) return 0; break; default: return 0; } return 1; } dq-20250201/keyparse.h000066400000000000000000000001701474734131400143550ustar00rootroot00000000000000#ifndef _KEYPARSE_H____ #define _KEYPARSE_H____ extern int keyparse(unsigned char *, long long, const char *); #endif dq-20250201/log.c000066400000000000000000000226461474734131400133220ustar00rootroot00000000000000#include "buffer.h" #include "crypto_uint32.h" #include "crypto_uint16.h" #include "e.h" #include "byte.h" #include "iptostr.h" #include "numtostr.h" #include "log.h" /* work around gcc 2.95.2 bug */ #define number(x) ( (u64 = (x)), u64_print() ) static crypto_uint64 u64; static void u64_print(void) { char buf[20]; long long pos; pos = sizeof buf; do { if (pos <= 0) break; buf[--pos] = '0' + (u64 % 10); u64 /= 10; } while(u64); buffer_put(buffer_2, buf + pos, sizeof buf - pos); } static void hex(unsigned char c) { buffer_put(buffer_2, "0123456789abcdef" + (c >> 4), 1); buffer_put(buffer_2, "0123456789abcdef" + (c & 15), 1); } static void string(const char *s) { buffer_puts(buffer_2, s); } static void line(void) { string("\n"); buffer_flush(buffer_2); } static void space(void) { string(" "); } static void ip(const unsigned char i[16]) { #if 1 string(iptostr(0, i)); #else hex(i[0]); hex(i[1]); hex(i[2]); hex(i[3]); hex(i[4]); hex(i[5]); hex(i[6]); hex(i[7]); hex(i[8]); hex(i[9]); hex(i[10]); hex(i[11]); hex(i[12]); hex(i[13]); hex(i[14]); hex(i[15]); #endif } static void dctype(unsigned char x) { switch(x) { case 1: string("S"); break; case 2: string("T"); break; default: string("R"); break; } } static void logid(const unsigned char id[2]) { #if 1 string(numtostr(0, crypto_uint16_load_bigendian(id))); #else hex(id[0]); hex(id[1]); #endif } static void logtype(const unsigned char type[2]) { crypto_uint16 u; u = crypto_uint16_load_bigendian(type); number(u); } static void name(const unsigned char *q) { unsigned char ch; int state; if (!*q) { string("."); return; } while(state = *q++) { while (state) { ch = *q++; --state; if ((ch <= 32) || (ch > 126)) ch = '?'; if ((ch >= 'A') && (ch <= 'Z')) ch += 32; buffer_put(buffer_2, (char *)&ch, 1); } string("."); } } void log_startup(void) { string("starting"); line(); } void log_dnscurvekey(const unsigned char *key) { long long i; string("dnscurve public-key "); for(i = 0; i < 32; ++i) { hex(key[i]); } line(); } void log_query(crypto_uint64 *qnum, const unsigned char client[16], unsigned char port[2], const unsigned char id[2], const unsigned char *q, const unsigned char qtype[2]) { string("query "); number(*qnum); space(); ip(client); string(":"); string(numtostr(0, crypto_uint16_load_bigendian(port))); string(":"); logid(id); space(); logtype(qtype); space(); name(q); line(); } void log_queryreject(const unsigned char *client, unsigned char *port, const unsigned char *id, const unsigned char *q, const unsigned char *qtype, const char *x) { string("reject "); ip(client); string(":"); string(numtostr(0, crypto_uint16_load_bigendian(port))); string(":"); if (id) { logid(id); } else { string("?"); } space(); if (qtype) { logtype(qtype); } else { string("?"); } space(); if (q) { name(q); } else { string("?"); } space(); string(x); line(); } void log_querydone(crypto_uint64 *qnum, long long len) { string("sent "); number(*qnum); space(); number(len); line(); } void log_querydrop(crypto_uint64 *qnum) { const char *x = e_str(errno); string("drop "); number(*qnum); space(); string(x); line(); } void log_tcpopen(const unsigned char client[16], unsigned char port[2]) { string("tcpopen "); ip(client); string(":"); hex(port[0]); hex(port[1]); line(); } void log_tcpclose(const unsigned char client[16],unsigned char port[2]) { const char *x = e_str(errno); string("tcpclose "); ip(client); string(":"); hex(port[0]); hex(port[1]); space(); string(x); line(); } /* XXX */ void log_tx(const unsigned char *q, const unsigned char qtype[2], const unsigned char *control, const unsigned char servers[256], const unsigned char keys[528], int flaghavekeys, unsigned int gluelessness) { long long i, j; const unsigned char *k; string("tx "); number(gluelessness); space(); logtype(qtype); space(); name(q); space(); name(control); string(flaghavekeys ? " +" : " -"); for (i = 0; i < 256; i += 16) { j = i >> 4; if (!byte_isequal(servers + i, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) { k = keys + 33 * j; space(); dctype(k[0]); ip(servers + i); } } line(); } void log_cachedanswer(const unsigned char *q,const unsigned char type[2]) { string("cached "); logtype(type); space(); name(q); line(); } void log_cachedcname(const unsigned char *dn,const unsigned char *dn2) { string("cached cname "); name(dn); space(); name(dn2); line(); } void log_cachedns(const unsigned char *control,const unsigned char *ns) { string("cached ns "); name(control); space(); name(ns); line(); } void log_cachednxdomain(const unsigned char *dn) { string("cached nxdomain "); name(dn); line(); } void log_nxdomain(const unsigned char *server,const unsigned char *q,unsigned int ttl) { string("nxdomain "); ip(server); space(); number(ttl); space(); name(q); line(); } void log_nodata(const unsigned char *server,const unsigned char *q,const unsigned char qtype[2],unsigned int ttl) { string("nodata "); ip(server); space(); number(ttl); space(); logtype(qtype); space(); name(q); line(); } void log_lame(const unsigned char *server,const unsigned char *control,const unsigned char *referral) { string("lame "); ip(server); space(); name(control); space(); name(referral); line(); } void log_ignore_referral(const unsigned char *server, const unsigned char *control, const unsigned char *referral) { string("ignored referral "); ip(server); space(); name(control); space(); name(referral); line(); } void log_servfail(const unsigned char *dn) { const char *x = e_str(errno); string("servfail "); name(dn); space(); string(x); line(); } void log_cachedservfail(const unsigned char *dn, const unsigned char *dt) { string("cached servfail "); name(dn); space(); logtype(dt); line(); } void log_rr(const unsigned char *server,const unsigned char *q,const unsigned char type[2],const unsigned char *buf,unsigned int len,unsigned int ttl, unsigned char flagkey) { int i; string("rr "); dctype(flagkey); ip(server); space(); number(ttl); space(); logtype(type); space(); name(q); space(); for (i = 0;i < len;++i) { hex(buf[i]); if (i > 30) { string("..."); break; } } line(); } void log_rra(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey) { unsigned char i[16]; byte_copy(i, 12, "\0\0\0\0\0\0\0\0\0\0\377\377"); byte_copy(i + 12, 4, data); string("rr "); dctype(flagkey); ip(server); space(); number(ttl); string(" a "); name(q); space(); string(iptostr(0, i)); line(); } void log_rraaaa(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey) { string("rr "); dctype(flagkey); ip(server); space(); number(ttl); string(" aaaa "); name(q); space(); string(iptostr(0, data)); line(); } void log_rrns(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey) { string("rr "); dctype(flagkey); ip(server); space(); number(ttl); string(" ns "); name(q); space(); name(data); line(); } void log_rrcname(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey) { string("rr "); dctype(flagkey); ip(server); space(); number(ttl); string(" cname "); name(q); space(); name(data); line(); } void log_rrptr(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey) { string("rr "); dctype(flagkey); ip(server); space(); number(ttl); string(" ptr "); name(q); space(); name(data); line(); } void log_rrmx(const unsigned char *server,const unsigned char *q,const unsigned char *mx,const unsigned char pref[2],unsigned int ttl, unsigned char flagkey) { crypto_uint16 u; string("rr "); dctype(flagkey); ip(server); space(); number(ttl); string(" mx "); name(q); space(); u = crypto_uint16_load_bigendian(pref); number(u); space(); name(mx); line(); } void log_rrsoa(const unsigned char *server,const unsigned char *q,const unsigned char *n1,const unsigned char *n2,const unsigned char misc[20],unsigned int ttl,unsigned char flagkey) { crypto_uint32 u; int i; string("rr "); dctype(flagkey); ip(server); space(); number(ttl); string(" soa "); name(q); space(); name(n1); space(); name(n2); for (i = 0;i < 20;i += 4) { u = crypto_uint32_load_bigendian(misc + i); space(); number(u); } line(); } crypto_uint64 log_numqueries = 0; long long log_uactive = 0; long long log_tactive = 0; void log_stats(void) { extern crypto_uint64 cache_motion; extern crypto_uint64 cache_hit; extern crypto_uint64 cache_miss; extern crypto_uint64 tx4; extern crypto_uint64 tx6; string("stats "); number(log_numqueries); space(); number(cache_motion); space(); number(log_uactive); space(); number(log_tactive); space(); number(cache_hit); space(); number(cache_miss); space(); number(tx4); space(); number(tx6); line(); } dq-20250201/log.h000066400000000000000000000053701474734131400133220ustar00rootroot00000000000000#ifndef LOG_H #define LOG_H #include "crypto_uint64.h" extern void log_startup(void); extern void log_dnscurvekey(const unsigned char *key); extern void log_query(crypto_uint64 *,const unsigned char [16],unsigned char [2],const unsigned char [2],const unsigned char *,const unsigned char [2]); extern void log_queryreject(const unsigned char *, unsigned char *, const unsigned char *, const unsigned char *, const unsigned char *, const char *); extern void log_querydrop(crypto_uint64 *); extern void log_querydone(crypto_uint64 *,long long); extern void log_tcpopen(const unsigned char [16],unsigned char [2]); extern void log_tcpclose(const unsigned char [16],unsigned char [2]); extern void log_cachedanswer(const unsigned char *,const unsigned char [2]); extern void log_cachedcname(const unsigned char *,const unsigned char *); extern void log_cachednxdomain(const unsigned char *); extern void log_cachedns(const unsigned char *,const unsigned char *); extern void log_tx(const unsigned char *,const unsigned char [2],const unsigned char *,const unsigned char [256],const unsigned char [528],int,unsigned int); extern void log_nxdomain(const unsigned char *,const unsigned char *,unsigned int); extern void log_nodata(const unsigned char *,const unsigned char *,const unsigned char [2],unsigned int); extern void log_servfail(const unsigned char *); extern void log_cachedservfail(const unsigned char *, const unsigned char *); extern void log_lame(const unsigned char *,const unsigned char *,const unsigned char *); extern void log_ignore_referral(const unsigned char *,const unsigned char *,const unsigned char *); extern void log_rr(const unsigned char *,const unsigned char *,const unsigned char [2],const unsigned char *,unsigned int,unsigned int,unsigned char); extern void log_rra(const unsigned char *,const unsigned char *,const unsigned char *,unsigned int,unsigned char); extern void log_rraaaa(const unsigned char *,const unsigned char *,const unsigned char *,unsigned int,unsigned char); extern void log_rrns(const unsigned char *,const unsigned char *,const unsigned char *,unsigned int,unsigned char); extern void log_rrcname(const unsigned char *,const unsigned char *,const unsigned char *,unsigned int,unsigned char); extern void log_rrptr(const unsigned char *,const unsigned char *,const unsigned char *,unsigned int,unsigned char); extern void log_rrmx(const unsigned char *,const unsigned char *,const unsigned char *,const unsigned char [2],unsigned int,unsigned char); extern void log_rrsoa(const unsigned char *,const unsigned char *,const unsigned char *,const unsigned char *,const unsigned char [20],unsigned int,unsigned char); extern void log_stats(void); extern crypto_uint64 log_numqueries; extern long long log_uactive; extern long long log_tactive; #endif dq-20250201/makefilegen.sh000066400000000000000000000067071474734131400152000ustar00rootroot00000000000000#!/bin/sh ( ( echo "CC?=cc" echo "CFLAGS+=-O3 -fno-strict-overflow -fwrapv -Wno-parentheses -Wundef -Wunused-value -Wmissing-prototypes -Wmissing-declarations -Wwrite-strings -Wdeclaration-after-statement -Wshadow -Wno-unused-function -Wno-overlength-strings -Wno-long-long -Wall -pedantic -Icryptoint" echo "LDFLAGS?=" echo "CPPFLAGS?=" echo echo "DESTDIR?=" echo "PREFIX?=/usr/local" echo echo "INSTALL?=install" echo # binaries i=0 for file in `ls -1 *.c | grep -v '^has'`; do if grep '^int main(' "${file}" >/dev/null; then x=`echo "${file}" | sed 's/\.c$//'` if [ $i -eq 0 ]; then echo "BINARIES=${x}" else echo "BINARIES+=${x}" fi i=`expr $i + 1` fi done echo echo "all: \$(BINARIES)" echo for file in `ls -1 has*.c`; do hfile=`echo ${file} | sed 's/\.c/.h/'` touch "${hfile}" done for file in `ls -1 *.c | grep -v '^has'`; do ( gcc -Icryptoint -MM "${file}" echo " \$(CC) \$(CFLAGS) \$(CPPFLAGS) -c ${file}" echo ) done for file in `ls -1 has*.c`; do hfile=`echo ${file} | sed 's/\.c/.h/'` rm -f "${hfile}" done i=0 for file in `ls *.c`; do if ! grep '^int main(' "${file}" >/dev/null; then x=`echo "${file}" | sed 's/\.c$/.o/'` if [ $i -eq 0 ]; then echo "OBJECTS=${x}" else echo "OBJECTS+=${x}" fi i=`expr $i + 1` fi done echo for file in `ls *.c | grep -v '^has'`; do if grep '^int main(' "${file}" >/dev/null; then x=`echo "${file}" | sed 's/\.c$//'` echo "${x}: ${x}.o \$(OBJECTS) libs" echo " \$(CC) \$(CFLAGS) \$(CPPFLAGS) -o ${x} ${x}.o \$(OBJECTS) \$(LDFLAGS) \`cat libs\`" echo fi done echo for cfile in `ls -1 has*.c`; do hfile=`echo ${cfile} | sed 's/\.c/.h/'` lfile=`echo ${cfile} | sed 's/\.c/.log/'` touch "${hfile}" echo "${hfile}: tryfeature.sh ${cfile} libs" echo " env CC=\"\$(CC)\" CFLAGS=\"\$(CFLAGS)\" LDFLAGS=\"\$(LDFLAGS) \`cat libs\`\" ./tryfeature.sh ${cfile} >${hfile} 2>${lfile}" echo " cat ${hfile}" echo done echo "libs: trylibs.sh" echo " env CC=\"\$(CC)\" ./trylibs.sh -lsocket -lnsl -lrandombytes -l25519 >libs 2>libs.log" echo " cat libs" echo echo "install: dq dqcache dqcache-makekey dqcache-start" echo " mkdir -p \$(DESTDIR)\$(PREFIX)/bin" echo " mkdir -p \$(DESTDIR)\$(PREFIX)/sbin" echo " mkdir -p \$(DESTDIR)\$(PREFIX)/share/man/man1" echo " mkdir -p \$(DESTDIR)\$(PREFIX)/share/man/man8" echo " \$(INSTALL) -m 0755 dq \$(DESTDIR)\$(PREFIX)/bin/dq" echo " \$(INSTALL) -m 0755 dqcache \$(DESTDIR)\$(PREFIX)/sbin/dqcache" echo " \$(INSTALL) -m 0755 dqcache-makekey \$(DESTDIR)\$(PREFIX)/sbin/dqcache-makekey" echo " \$(INSTALL) -m 0755 dqcache-start \$(DESTDIR)\$(PREFIX)/sbin/dqcache-start" echo " \$(INSTALL) -m 0644 man/dq.1 \$(DESTDIR)\$(PREFIX)/share/man/man1/dq.1" echo " \$(INSTALL) -m 0644 man/dqcache.8 \$(DESTDIR)\$(PREFIX)/share/man/man8/dqcache.8" echo " \$(INSTALL) -m 0644 man/dqcache-makekey.8 \$(DESTDIR)\$(PREFIX)/share/man/man8/dqcache-makekey.8" echo " \$(INSTALL) -m 0644 man/dqcache-start.8 \$(DESTDIR)\$(PREFIX)/share/man/man8/dqcache-start.8" echo echo "clean:" echo " rm -f *.log has*.h \$(OBJECTS) \$(BINARIES) libs" echo ) > Makefile ) dq-20250201/man/000077500000000000000000000000001474734131400131365ustar00rootroot00000000000000dq-20250201/man/dq.1000066400000000000000000000050201474734131400136210ustar00rootroot00000000000000.TH dq 1 .SH NAME dq \- DNS/DNSCurve query tool .SH SYNOPSIS .B dq [ options ] .I type .I fqdn [ .I host ] .TP .B dq \-a [ options ] .I type .I fqdn .I host .SH DESCRIPTION .B dq is commandline tool derived from .B dnsq / .B dnsqr (https://cr.yp.to/djbdns/debugging.html) .sp .B dq asks for records of type .I type under the domain name .I fqdn .TP .B dq \-a sends a non\-recursive DNS/DNSCurve query to DNS/DNSCurve server .I host for records of type .I type under the domain name .I fqdn .SH OPTIONS .TP .B \-v print extra information .TP .B \-r send recursive query (default) .TP .B \-a send authoritative query .TP .B \-u use UDP (default) .TP .B \-t use TCP .TP .B \-s send DNSCurve query in streamlined format (default), ignored for regular DNS queries .TP .B \-S \fIsuffix send DNSCurve query in TXT format using suffix .IR suffix , ignored for regular DNS queries .TP .B \-T \fItimeout give up on the DNS/DNSCurve query attempt after .I timeout seconds <1\-60> (default 60) .TP .B \-p \fIport send query to port .I port (default 53) .TP .B \-k \fIkey send DNSCurve query and use servers public\-key .I key .TP .I type DNS query type (A, NS, MX, ANY, PTR, TXT, SOA, SRV, AAAA, AXFR, CNAME or numeric type) .TP .I fqdn fully qualified domain name .TP .I host DNS server (hostname or IP address) .SH ENVIRONMENT .TP .B DNSCACHEIP use IP address $DNSCACHEIP instead of 'nameserver' lines from /etc/resolv.conf .TP .B LOCALDOMAIN use space separated names from $LOCALDOMAIN instead of 'search' line from /etc/resolv.conf .TP .B DNSREWRITEFILE use $DNSREWRITEFILE file instead of /etc/dnsrewrite .SH NOTES .B dq rewrites IP address to *.in\-addr.arpa or *.ip6.arpa for PTR queries e.g.: .nf .RS 2 127.0.0.1 \-> 1.0.0.127.in\-addr.arpa ::1 \-> 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa .RE .SH RECURSIVE EXAMPLES .nf dq any dnscurve.cz dq any dnscurve.cz 8.8.8.8 env DNSCACHEIP=8.8.8.8 dq any dnscurve.cz dq ptr 1.0.0.127.in\-addr.arpa dq ptr 127.0.0.1 dq ptr ::1 dq ptr 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa .fi .SH AUTHORITATIVE EXAMPLES .nf dq \-a any dnscurve.cz uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq.ns.dnscurve.cz dq \-a \-S cz any dnscurve.cz uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq.ns.dnscurve.cz dq \-a \-k uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq any dnscurve.cz 2a02:2b88:2:1::127d:1 .fi .SH SEE ALSO .BR dnsq (1), .BR dnsqr (1), .BR dqcache (8) .sp .nf https://mojzis.com/software/dq https://dnscurve.org/ https://cr.yp.to/djbdns/debugging.html .fi dq-20250201/man/dqcache-makekey.8000066400000000000000000000006541474734131400162500ustar00rootroot00000000000000.TH dqcache\-makekey 8 .SH NAME dqcache\-makekey \- make DNSCurve secret\-key for dqcache .SH SYNOPSIS .B dqcache\-makekey .SH DESCRIPTION .B dqcache\-makekey creates secret\-key (in hex format) for dqcache .SH EXAMPLE .nf ( cd /etc/dqcache/env/ umask 077 dqcache\-makekey > SECRETKEY.tmp mv \-f SECRETKEY.tmp SECRETKEY ) .fi .SH SEE ALSO .BR dqcache (8) .sp .nf https://mojzis.com/software/dq https://dnscurve.org/ .fi dq-20250201/man/dqcache-start.8000066400000000000000000000010611474734131400157500ustar00rootroot00000000000000.TH dqcache\-start 8 .SH NAME dqcache\-start \- wrapper for dqcache daemon .SH SYNOPSIS .B dqcache\-start root\-directory account child .SH DESCRIPTION .B dqcache is designed to run under daemontools. .B dqcache\-start is a wrapper and allows running under e.g. systemd. .sp .B dqcache\-start sets $ROOT,$UID,$GID env. variables, sets appropriate permissions in $ROOT directory and runs .B dqcache .SH EXAMPLE .nf dqcache\-start /etc/dqcache/root dqcache dqcache .fi .SH SEE ALSO .BR dqcache (8) .sp .nf https://mojzis.com/software/dq https://dnscurve.org/ .fi dq-20250201/man/dqcache.8000066400000000000000000000110501474734131400146140ustar00rootroot00000000000000.TH dqcache 8 .SH NAME dqcache \- DNS cache with DNSCurve support .SH DESCRIPTION .B dqcache is recursive DNS server derived from .B dnscache (https://cr.yp.to/djbdns/dnscache.html) .SH CONFIGURATION .B dqcache runs chrooted in the directory specified by the $ROOT environment variable, under the uid and gid specified by the $UID and $GID environment variables. .sp .B dqcache listens for incoming UDP packets and TCP connections addressed to port 53 of $IP. Typically $IP is 127.0.0.1, but it can also be an externally accessible IP address. .sp .B dqcache accepts connections from localhost (127.0.0.0/8 or ::1/128). For external connections .B dqcache has file\-style accesslist. .RS 4 .nf e.g. for IPv4 address 1.2.3.4 dqcache allows connection when sees a file named: ip4/1.2.3.4 or ip4/1.2.3 or ip4/1.2 or ip4/1 e.g. for IPv6 address 2a02:598:7000:101:12dd:b1ff:fede:3476 dqcache allows connection when sees a file named: ip6/2a02:598:7000:101:12dd:b1ff:fede:3476 or ip6/2a02:598:7000:101:12dd:b1ff:fede or ip6/2a02:598:7000:101:12dd:b1ff or ip6/2a02:598:7000:101:12dd or ip6/2a02:598:7000:101 or ip6/2a02:598:7000 or ip6/2a02:598 or ip6/2a02 .fi .RE when environment variable $OKCLIENT is set, .B dqcache accepts all connections. .sp .B Warning , .B dqcache encrypts DNS packets and should run on client's machine. If You run .B dqcache on external IP (not on localhost), You're probably doing it wrong. .sp .B dqcache sends outgoing IPv4 packets from high ports of $IPSEND4. Typically $IPSEND4 is 0.0.0.0, meaning the machine's primary IPv4 address. .sp .B dqcache sends outgoing IPv6 packets from high ports of $IPSEND6. Typically $IPSEND6 is ::, meaning the machine's primary IPv6 address. .sp If $HIDETTL is set, .B dqcache always uses a TTL of 0 in its responses .sp .B dqcache reads a list of root servers as an anchor, one record per line, from servers/@. The record has format {IP} or {IP}|{DNSCURVEPUBLICKEY} e.g.: .RS 4 .nf servers/@: 198.41.0.4 2001:503:ba3e::2:30 192.228.79.201 2001:500:84::b etc. .fi .RE .sp .B dqcache also scans the servers directory for anchors for other domains, one record per line. The record has format {IP} or {IP}|{DNSCURVEPUBLICKEY} e.g.: .RS 4 .nf servers/dnscurve.cz: 185.66.36.55|uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq 37.157.196.86|uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq 2a02:2b88:2:1::127d:1|uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq 2a03:1e20:0:5::2|uz5mj60yr9tnudkkpcglw1y0w6dlh78j1l4gk4z7t8bsf1u4d48wlq .fi .RE .sp .B dqcache supports forward\-only mode. If $FORWARDONLY is set, dqcache treats servers/@ as a list of IP addresses for other caches, not root servers. It forwards queries to those caches the same way that a client does, rather than contacting a chain of servers according to NS records. .sp .B dqcache uses a fixed\-size cache, as controlled by the $CACHESIZE environment variable. Typically $CACHESIZE is 10000000 (10MB). .sp .B dqcache typically generates new secret key at start of program. If environment variable $SECRETKEY is set, then .B dqcache loads this secret key. $SECRETKEY must be safely generated using .BR dqcache\-makekey (8). .sp Note that .B dqcache uses internally two secret keys, one used as the discrete logarithm of the public key (256bit DNSCurve key) and one to encrypt the nonce (128bit TEA symmetric key). Both of these keys are actually derived from $SECRETKEY using crypto_stream_salsa20(). .sp .B dqcache also supports nonce\-separation via environment variable $NONCESTART. This means that several caches share a key but use separate nonces: .RS 2 .nf e.g.: NONCESTART=100 ... the first of four caches is configured to use top bits 100 NONCESTART=101 ... the second is configured to use top bits 101 NONCESTART=110 ... the third is configured to use top bits 110 NONCESTART=111 ... and the fourth is configured to use top bits 111. etc. .fi .RE .sp .B dqcache typically supports DNSCurve in 'combined mode' (meaning: if Streamlined query fails, .B dqcache tries also TXT query). Environment variable $DNSCURVETYPE selects specific DNSCurve type. .RS 2 .nf DNSCURVETYPE=1 ... Streamlined only DNSCurve DNSCURVETYPE=2 ... TXT only DNSCurve .fi .RE .sp .B dqcache caches records for at least $MINTTL seconds, when MINTTL is set. It's limited <0\-300> seconds. Use only if you really know what you're doing. .RE .sp .B dqcache sends only IPv4 queries, when DISABLEIPV6 is set. Use only if you really know what you're doing. .SH SEE ALSO .BR dqcache\-makekey (8), .BR dq (1), .BR dnscache (8) .sp .nf https://mojzis.com/software/dq https://dnscurve.org/ https://cr.yp.to/djbdns/dnscache.html .fi dq-20250201/milliseconds.c000066400000000000000000000004061474734131400152140ustar00rootroot00000000000000/* 20130505 Jan Mojzis Public domain. */ #include #include #include "milliseconds.h" long long milliseconds(void) { struct timeval t; gettimeofday(&t, (struct timezone *)0); return t.tv_sec * 1000LL + t.tv_usec / 1000LL; } dq-20250201/milliseconds.h000066400000000000000000000001461474734131400152220ustar00rootroot00000000000000#ifndef _MILLISECONDS_H____ #define _MILLISECONDS_H____ extern long long milliseconds(void); #endif dq-20250201/nanoseconds.c000066400000000000000000000010051474734131400150350ustar00rootroot00000000000000#include #include #include "nanoseconds.h" /* XXX: Y2036 problems; should upgrade to a 128-bit type for this */ /* XXX: nanosecond granularity limits users to 1 terabyte per second */ long long nanoseconds(void) { #ifdef CLOCK_REALTIME struct timespec t; if (clock_gettime(CLOCK_REALTIME,&t) != 0) return -1; return t.tv_sec * 1000000000LL + t.tv_nsec; #else struct timeval t; gettimeofday(&t,(struct timezone *) 0); return t.tv_sec * 1000000000LL + t.tv_usec * 1000LL; #endif } dq-20250201/nanoseconds.h000066400000000000000000000001311474734131400150410ustar00rootroot00000000000000#ifndef NANOSECONDS_H #define NANOSECONDS_H extern long long nanoseconds(void); #endif dq-20250201/numtostr.c000066400000000000000000000016321474734131400144240ustar00rootroot00000000000000/* 20130604 Jan Mojzis Public domain. */ #include "numtostr.h" /* The 'numtostr(strbuf,n)' converts number 'n' into the 0-terminated string. The caller must allocate at least NUMTOSTR_LEN bytes for 'strbuf'. The 'numtostr' function is ready for 128-bit integer. */ char *numtostr(char *strbuf, long long n) { long long len = 0; unsigned long long n1, n2; static char staticbuf[NUMTOSTR_LEN]; int flagsign = 0; if (!strbuf) strbuf = staticbuf; /* not thread-safe */ if (n < 0) { n1 = n2 = -(unsigned long long)n; flagsign = 1; } else { n1 = n2 = (unsigned long long)n; } do { n1 /= 10; ++len; } while (n1); if (flagsign) ++len; strbuf += len; do { *--strbuf = '0' + (n2 % 10); n2 /= 10; } while (n2); if (flagsign) *--strbuf = '-'; while (len < NUMTOSTR_LEN) strbuf[len++] = 0; return strbuf; } dq-20250201/numtostr.h000066400000000000000000000001741474734131400144310ustar00rootroot00000000000000#ifndef _NUMTOSTR_H____ #define _NUMTOSTR_H____ #define NUMTOSTR_LEN 41 extern char *numtostr(char *, long long); #endif dq-20250201/okclient.c000066400000000000000000000017721474734131400143460ustar00rootroot00000000000000#include #include #include "str.h" #include "byte.h" #include "iptostr.h" #include "okclient.h" static char fn[4 + IPTOSTR_LEN]; int okclient(unsigned char *ip) { struct stat st; long long i; char sep; /* allow ::1/128 */ if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1", 16, ip)) return 1; /* allow 127.0.0.0/8 */ if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377\177", 13, ip)) return 1; if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { fn[0] = 'i'; fn[1] = 'p'; fn[2] = '4'; fn[3] = '/'; iptostrx(fn + 4, ip); sep = '.'; } else { fn[0] = 'i'; fn[1] = 'p'; fn[2] = '6'; fn[3] = '/'; iptostrx(fn + 4, ip); sep = ':'; } for (;;) { if (stat(fn, &st) == 0) return 1; /* treat temporary error as rejection */ i = str_rchr(fn, sep); if (!fn[i]) return 0; fn[i] = 0; } return 0; } dq-20250201/okclient.h000066400000000000000000000001251474734131400143420ustar00rootroot00000000000000#ifndef OKCLIENT_H #define OKCLIENT_H extern int okclient(unsigned char *); #endif dq-20250201/open.h000066400000000000000000000003621474734131400134760ustar00rootroot00000000000000#ifndef OPEN_H #define OPEN_H extern int open_read(const char *); extern int open_write(const char *); extern int open_lock(const char *); extern int open_cwd(void); extern int open_pipe(int *); extern int open_trunc(const char *); #endif dq-20250201/open_read.c000066400000000000000000000005061474734131400144640ustar00rootroot00000000000000#include #include #include #include #include "open.h" int open_read(const char *fn) { #ifdef O_CLOEXEC return open(fn,O_RDONLY | O_NONBLOCK | O_CLOEXEC); #else int fd = open(fn,O_RDONLY | O_NONBLOCK); if (fd == -1) return -1; fcntl(fd,F_SETFD,1); return fd; #endif } dq-20250201/open_trunc.c000066400000000000000000000005711474734131400147060ustar00rootroot00000000000000#include #include #include #include #include "open.h" int open_trunc(const char *fn) { #ifdef O_CLOEXEC return open(fn,O_CREAT | O_WRONLY | O_NONBLOCK | O_TRUNC | O_CLOEXEC,0644); #else int fd = open(fn,O_CREAT | O_WRONLY | O_NONBLOCK | O_TRUNC,0644); if (fd == -1) return -1; fcntl(fd,F_SETFD,1); return fd; #endif } dq-20250201/openreadclose.c000066400000000000000000000020051474734131400153470ustar00rootroot00000000000000#include #include #include #include "open.h" #include "e.h" #include "byte.h" #include "openreadclose.h" int openreadclose(const char *fn, stralloc *sa, long long bufsize) { int fd; long long r; struct stat st; if (bufsize <= 1) bufsize = 32; fd = open_read(fn); if (fd == -1) { if (errno == ENOENT) return 0; return -1; } if (fstat(fd, &st) == -1) { close(fd); return -1; } if (!stralloc_readyplus(sa, st.st_size)) { close(fd); return -1; } if (!stralloc_copys(sa, "")) { close(fd); return -1; } for (;;) { if (!stralloc_readyplus(sa, bufsize)) { close(fd); return -1; } r = read(fd, sa->s + sa->len, bufsize); if (r == 0) break; if (r == -1) { if (errno == EINTR) continue; if (errno == EAGAIN) continue; if (errno == EWOULDBLOCK) continue; close(fd); return -1; } sa->len += r; } close(fd); return 1; } dq-20250201/openreadclose.h000066400000000000000000000002311474734131400153530ustar00rootroot00000000000000#ifndef _OPENREADCLOSE_H____ #define _OPENREADCLOSE_H____ #include "stralloc.h" extern int openreadclose(const char *, stralloc *, long long); #endif dq-20250201/portparse.c000066400000000000000000000005101474734131400145420ustar00rootroot00000000000000#include "portparse.h" int portparse(unsigned char *y,const char *x) { long long d = 0; long long j; if (!x) return 0; for (j = 0;j < 5 && x[j] >= '0' && x[j] <= '9';++j) d = d * 10 + (x[j] - '0'); if (j == 0) return 0; if (x[j]) return 0; if (d > 65535) return 0; y[0] = d >> 8; y[1] = d; return 1; } dq-20250201/portparse.h000066400000000000000000000001451474734131400145530ustar00rootroot00000000000000#ifndef PORTPARSE_H #define PORTPARSE_H extern int portparse(unsigned char *,const char *); #endif dq-20250201/porttostr.c000066400000000000000000000015041474734131400146070ustar00rootroot00000000000000/* 20130604 Jan Mojzis Public domain. */ #include "crypto_uint16.h" #include "porttostr.h" /* The 'porttostr(strbuf,port)' converts 'port' from network byte order into the 0-terminated string. The 'port' length is always 2 bytes. The caller must allocate at least PORTTOSTR_LEN bytes for 'strbuf'. */ char *porttostr(char *strbuf, const unsigned char *port) { long long len = 0; crypto_uint16 num; static char staticbuf[PORTTOSTR_LEN]; if (!strbuf) strbuf = staticbuf; /* not thread-safe */ num = port[0]; num <<= 8; num |= port[1]; do { num /= 10; ++len; } while (num); strbuf += len; num = port[0]; num <<= 8; num |= port[1]; do { *--strbuf = '0' + (num % 10); num /= 10; } while (num); while (len < PORTTOSTR_LEN) strbuf[len++] = 0; return strbuf; } dq-20250201/porttostr.h000066400000000000000000000002131474734131400146100ustar00rootroot00000000000000#ifndef _PORTTOSTR_H____ #define _PORTTOSTR_H____ #define PORTTOSTR_LEN 6 extern char *porttostr(char *, const unsigned char *); #endif dq-20250201/printpacket.c000066400000000000000000000042131474734131400150530ustar00rootroot00000000000000#include "crypto_uint16.h" #include "e.h" #include "byte.h" #include "dns.h" #include "printrecord.h" #include "printpacket.h" static unsigned char *d = 0; #define X(s) if (!stralloc_cats(out,s)) return 0; #define NUM(u) if (!stralloc_catnum(out,u)) return 0; int printpacket_cat(stralloc *out, unsigned char *buf, long long len) { crypto_uint16 xnumqueries; crypto_uint16 numanswers; crypto_uint16 numauthority; crypto_uint16 numglue; long long pos; unsigned char data[12]; crypto_uint16 type; pos = dns_packet_copy(buf,len,0,data,12); if (!pos) return 0; xnumqueries = crypto_uint16_load_bigendian(data + 4); numanswers = crypto_uint16_load_bigendian(data + 6); numauthority = crypto_uint16_load_bigendian(data + 8); numglue = crypto_uint16_load_bigendian(data + 10); NUM(len) X(" bytes, ") NUM(xnumqueries) X("+") NUM(numanswers) X("+") NUM(numauthority) X("+") NUM(numglue) X(" records") if (data[2] & 128) X(", response") if (data[2] & 120) X(", weird op") if (data[2] & 4) X(", authoritative") if (data[2] & 2) X(", truncated") if (data[2] & 1) X(", weird rd") if (data[3] & 128) X(", weird ra") switch(data[3] & 15) { case 0: X(", noerror"); break; case 3: X(", nxdomain"); break; case 4: X(", notimp"); break; case 5: X(", refused"); break; default: X(", weird rcode"); } if (data[3] & 112) X(", weird z") X("\n") while (xnumqueries) { --xnumqueries; X("query: ") pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; pos = dns_packet_copy(buf,len,pos,data,4); if (!pos) return 0; if (!byte_isequal(data + 2,2,DNS_C_IN)) { X("weird class") } else { type = crypto_uint16_load_bigendian(data); NUM(type) X(" ") if (!dns_domain_todot_cat(out,d)) return 0; } X("\n") } for (;;) { if (numanswers) { --numanswers; X("answer: ") } else if (numauthority) { --numauthority; X("authority: ") } else if (numglue) { --numglue; X("additional: ") } else break; pos = printrecord_cat(out,buf,len,pos,0,0); if (!pos) return 0; } if (pos != len) { errno = EPROTO; return 0; } return 1; } dq-20250201/printpacket.h000066400000000000000000000002321474734131400150550ustar00rootroot00000000000000#ifndef PRINTPACKET_H #define PRINTPACKET_H #include "stralloc.h" extern int printpacket_cat(stralloc *out, unsigned char *buf, long long len); #endif dq-20250201/printrecord.c000066400000000000000000000127231474734131400150670ustar00rootroot00000000000000#include "crypto_uint16.h" #include "crypto_uint32.h" #include "e.h" #include "byte.h" #include "dns.h" #include "printrecord.h" #include "iptostr.h" static unsigned char *d = 0; long long printrecord_cat(stralloc *out,const unsigned char *buf,long long len,long long pos,const unsigned char *q,const unsigned char qtype[2]) { const unsigned char *x; unsigned char misc[20]; crypto_uint16 datalen; crypto_uint16 u16; crypto_uint32 u32; long long newpos; long long i; unsigned char ch; long long txtlen; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; pos = dns_packet_copy(buf,len,pos,misc,10); if (!pos) return 0; datalen = crypto_uint16_load_bigendian(misc + 8); newpos = pos + datalen; if (q) { if (!dns_domain_equal(d,q)) return newpos; if (!byte_isequal(qtype,2,misc) && !byte_isequal(qtype,2,DNS_T_ANY)) return newpos; } if (!dns_domain_todot_cat(out,d)) return 0; if (!stralloc_cats(out," ")) return 0; u32 = crypto_uint32_load_bigendian(misc + 4); if (!stralloc_catnum(out,u32)) return 0; if (!byte_isequal(misc + 2,2,DNS_C_IN)) { if (!stralloc_cats(out," weird class\n")) return 0; return newpos; } x = 0; if (byte_isequal(misc,2,DNS_T_NS)) x = (unsigned char *)" NS "; if (byte_isequal(misc,2,DNS_T_PTR)) x = (unsigned char *)" PTR "; if (byte_isequal(misc,2,DNS_T_CNAME)) x = (unsigned char *)" CNAME "; if (x) { pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; if (!stralloc_cats(out,x)) return 0; if (!dns_domain_todot_cat(out,d)) return 0; } else if (byte_isequal(misc,2,DNS_T_MX)) { if (!stralloc_cats(out," MX ")) return 0; pos = dns_packet_copy(buf,len,pos,misc,2); if (!pos) return 0; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; u16 = crypto_uint16_load_bigendian(misc); if (!stralloc_catnum(out,u16)) return 0; if (!stralloc_cats(out," ")) return 0; if (!dns_domain_todot_cat(out,d)) return 0; } else if (byte_isequal(misc,2,DNS_T_SOA)) { if (!stralloc_cats(out," SOA ")) return 0; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; if (!dns_domain_todot_cat(out,d)) return 0; if (!stralloc_cats(out," ")) return 0; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; if (!dns_domain_todot_cat(out,d)) return 0; pos = dns_packet_copy(buf,len,pos,misc,20); if (!pos) return 0; for (i = 0;i < 5;++i) { if (!stralloc_cats(out," ")) return 0; u32 = crypto_uint32_load_bigendian(misc + 4 * i); if (!stralloc_catnum(out,u32)) return 0; } } else if (byte_isequal(misc,2,DNS_T_AAAA)) { if (datalen != 16) { errno = EPROTO; return 0; } if (!stralloc_cats(out," AAAA ")) return 0; pos = dns_packet_copy(buf,len,pos,misc,16); if (!pos) return 0; if (!stralloc_cats(out,iptostr(0, misc))) return 0; } else if (byte_isequal(misc,2,DNS_T_TXT)) { if (pos + datalen > len) return 0; if (!stralloc_cats(out," TXT ")) return 0; txtlen = 0; for (i = 0;i < datalen;++i) { ch = buf[pos + i]; if (!txtlen) txtlen = ch; else { --txtlen; if (ch < 32 || ch > 126) { misc[3] = '0' + (7 & ch); ch >>= 3; misc[2] = '0' + (7 & ch); ch >>= 3; misc[1] = '0' + (7 & ch); misc[0] = '\\'; if (!stralloc_catb(out,misc,4)) return 0; } else { if (!stralloc_append(out,&ch)) return 0; } } } pos += datalen; } else if (byte_isequal(misc,2,DNS_T_SRV)) { if (!stralloc_cats(out," SRV ")) return 0; pos = dns_packet_copy(buf,len,pos,misc,6); if (!pos) return 0; u16 = crypto_uint16_load_bigendian(misc); if (!stralloc_catnum(out,u16)) return 0; if (!stralloc_cats(out," ")) return 0; u16 = crypto_uint16_load_bigendian(misc + 2); if (!stralloc_catnum(out,u16)) return 0; if (!stralloc_cats(out," ")) return 0; u16 = crypto_uint16_load_bigendian(misc + 4); if (!stralloc_catnum(out,u16)) return 0; if (!stralloc_cats(out," ")) return 0; pos = dns_packet_getname(buf,len,pos,&d); if (!pos) return 0; if (!dns_domain_todot_cat(out,d)) return 0; } else if (byte_isequal(misc,2,DNS_T_A)) { if (datalen != 4) { errno = EPROTO; return 0; } if (!stralloc_cats(out," A ")) return 0; pos = dns_packet_copy(buf,len,pos,misc,4); if (!pos) return 0; for (i = 0;i < 4;++i) { ch = misc[i]; if (i) if (!stralloc_cats(out,".")) return 0; if (!stralloc_catnum(out,ch)) return 0; } } else { if (!stralloc_cats(out," ")) return 0; u16 = crypto_uint16_load_bigendian(misc); if (!stralloc_catnum(out,u16)) return 0; if (!stralloc_cats(out," ")) return 0; while (datalen--) { pos = dns_packet_copy(buf,len,pos,misc,1); if (!pos) return 0; if ((misc[0] >= 33) && (misc[0] <= 126) && (misc[0] != '\\')) { if (!stralloc_catb(out,misc,1)) return 0; } else { ch = misc[0]; misc[3] = '0' + (7 & ch); ch >>= 3; misc[2] = '0' + (7 & ch); ch >>= 3; misc[1] = '0' + (7 & ch); misc[0] = '\\'; if (!stralloc_catb(out,misc,4)) return 0; } } } if (!stralloc_cats(out,"\n")) return 0; if (pos != newpos) { errno = EPROTO; return 0; } return newpos; } long long printrecord(stralloc *out,const unsigned char *buf,long long len,long long pos,const unsigned char *q,const unsigned char qtype[2]) { if (!stralloc_copys(out,"")) return 0; return printrecord_cat(out,buf,len,pos,q,qtype); } dq-20250201/printrecord.h000066400000000000000000000005751474734131400150760ustar00rootroot00000000000000#ifndef PRINTRECORD_H #define PRINTRECORD_H #include "stralloc.h" extern long long printrecord_cat(stralloc *out,const unsigned char *buf,long long len,long long pos,const unsigned char *q,const unsigned char qtype[2]); extern long long printrecord(stralloc *out,const unsigned char *buf,long long len,long long pos,const unsigned char *q,const unsigned char qtype[2]); #endif dq-20250201/purge.c000066400000000000000000000003111474734131400136440ustar00rootroot00000000000000#include "purge.h" void purge(void *yv, long long ylen) { volatile char *y = (volatile char *)yv; while (ylen > 0) { *y++ = 0; --ylen; } __asm__ __volatile__("" : : "r"(yv) : "memory"); } dq-20250201/purge.h000066400000000000000000000001311474734131400136510ustar00rootroot00000000000000#ifndef _PURGE_H____ #define _PURGE_H____ extern void purge(void *, long long); #endif dq-20250201/query.c000066400000000000000000001117321474734131400137010ustar00rootroot00000000000000#include "e.h" #include "roots.h" #include "log.h" #include "case.h" #include "cache.h" #include "byte.h" #include "dns.h" #include "crypto_uint32.h" #include "crypto_uint16.h" #include "alloc.h" #include "response.h" #include "query.h" #include "strtoip.h" #include "iptostr.h" #include "xsocket.h" #include "crypto_scalarmult_curve25519.h" #include "crypto_box_curve25519xsalsa20poly1305.h" #include "purge.h" static unsigned char secretkey[32]; static unsigned char publickey[32]; void query_init(const unsigned char *sk) { byte_copy(secretkey,32,sk); crypto_scalarmult_curve25519_base(publickey, secretkey); log_dnscurvekey(publickey); return; } void query_purge(void) { purge(secretkey, sizeof secretkey); purge(publickey, sizeof publickey); } static int flagforwardonly = 0; void query_forwardonly(void) { flagforwardonly = 1; } static int flagtcponly = 0; void query_tcponly(void) { flagtcponly = 1; } static int flagipv4only = 0; void query_ipv4only(void) { flagipv4only = 1; } static crypto_uint32 minttl = 0; void query_minttl(long long x) { if (x < 0) x = 0; if (x > 300) x = 300; minttl = x; } unsigned char remoteport[2] = { 0, 53 }; void query_remoteport(unsigned char *port) { byte_copy(remoteport, 2, port); } static int flagdnscurvetype1 = 1; static int flagdnscurvetype2 = 2; void query_dnscurvetype(char *x) { if (!x) return; if (*x == '1') { /* streamlined only */ flagdnscurvetype1 = 1; flagdnscurvetype2 = 0; } else if (*x == '2') { /* txt only */ flagdnscurvetype1 = 2; flagdnscurvetype2 = 0; } else { /* mixed */ flagdnscurvetype1 = 1; flagdnscurvetype2 = 2; } } static void cachegeneric(const unsigned char type[2],const unsigned char *d,const unsigned char *data,long long datalen,crypto_uint32 ttl, unsigned char flagns) { long long len; unsigned char key[257]; len = dns_domain_length(d); if (len > 255) return; byte_copy(key,2,type); byte_copy(key + 2,len,d); case_lowerb(key + 2,len); cache_set(key,len + 2,data,datalen,ttl,flagns); } static void cachegeneric2(const unsigned char type[2],const unsigned char dtype[2], const unsigned char *d,const unsigned char *data,long long datalen,crypto_uint32 ttl, unsigned char flagns) { long long len; unsigned char key[259]; len = dns_domain_length(d); if (len > 255) return; byte_copy(key,2,type); byte_copy(key + 2,len,d); byte_copy(key + 2 + len,2,dtype); case_lowerb(key + 2,len); cache_set(key,len + 4,data,datalen,ttl,flagns); } static unsigned char save_buf[8192]; static long long save_len; static long long save_ok; static void save_start(void) { save_len = 0; save_ok = 1; } static void save_data(const unsigned char *buf,long long len) { if (!save_ok) return; if (len > (sizeof save_buf) - save_len) { save_ok = 0; return; } byte_copy(save_buf + save_len,len,buf); save_len += len; } static void save_finish(const unsigned char type[2],const unsigned char *d,crypto_uint32 ttl,unsigned char flagns) { if (!save_ok) return; cachegeneric(type,d,save_buf,save_len,ttl,flagns); } static int typematch(const unsigned char rtype[2],const unsigned char qtype[2]) { return byte_isequal(qtype,2,rtype) || byte_isequal(qtype,2,DNS_T_ANY); } static long long ttlget(unsigned char buf[4]) { crypto_uint32 ttl; ttl=crypto_uint32_load_bigendian(buf); if (ttl < minttl) ttl = minttl; if (ttl > 1000000000) return 0; if (ttl > 604800) return 604800; return ttl; } static void cleanup(struct query *z) { int j; int k; dns_transmit_free(&z->dt); for (j = 0;j < QUERY_MAXALIAS;++j) dns_domain_free(&z->alias[j]); for (j = 0;j < QUERY_MAXLEVEL;++j) { dns_domain_free(&z->name[j]); for (k = 0;k < QUERY_MAXNS;++k) dns_domain_free(&z->ns[j][k]); } } static int rqa(struct query *z) { int i; for (i = QUERY_MAXALIAS - 1;i >= 0;--i) if (z->alias[i]) { if (!response_query(z->alias[i],z->type,z->class)) return 0; while (i > 0) { if (!response_cname(z->alias[i],z->alias[i - 1],z->aliasttl[i])) return 0; --i; } if (!response_cname(z->alias[0],z->name[0],z->aliasttl[0])) return 0; return 1; } if (!response_query(z->name[0],z->type,z->class)) return 0; return 1; } static int dtis(char *out, long long outlen, const unsigned char *d) { long long pos = 0; char ch; char ch2; if (!*d) return 0; for (;;) { ch = *d++; while (ch--) { ch2 = *d++; if (((ch2 >= '0') && (ch2 <= '9')) || (ch2 == ':')) { if (pos >= outlen) return 0; out[pos++] = ch2; continue; } return 0; } if (!*d) { if (pos >= outlen) return 0; out[pos++] = 0; return 1; } if (pos >= outlen) return 0; out[pos++] = '.'; } return 0; } static int globalip(unsigned char *d,unsigned char ip[16]) { char xbuf[100]; if (dns_domain_equal(d,(unsigned char *)"\011localhost\0")) { byte_copy(ip,16,"\0\0\0\0\0\0\0\0\0\0\377\377\177\0\0\1"); return 1; } if (!dtis(xbuf, sizeof xbuf, d)) return 0; return strtoip4(ip, xbuf); } static int globalip6(unsigned char *d,unsigned char ip[16]) { char xbuf[100]; if (dns_domain_equal(d,(unsigned char *)"\011localhost\0")) { byte_copy(ip,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1"); return 1; } if (!dtis(xbuf, sizeof xbuf, d)) return 0; return strtoip6(ip, xbuf); } static int findkey(const unsigned char *dn,unsigned char key[32]) { unsigned char c; while (c = *dn++) { if (c == 54) if (!case_diffb(dn,3,"uz5")) if (base32_decode(key,dn + 3,51,1) == 32) return 1; dn += (unsigned int) c; } return 0; } static unsigned char *t1 = 0; static unsigned char *t2 = 0; static unsigned char *t3 = 0; static unsigned char *cname = 0; static unsigned char *referral = 0; static long long *records = 0; static int smaller(unsigned char *buf,long long len,long long pos1,long long pos2) { unsigned char header1[12]; unsigned char header2[12]; int r; long long len1; long long len2; pos1 = dns_packet_getname(buf,len,pos1,&t1); dns_packet_copy(buf,len,pos1,header1,10); pos2 = dns_packet_getname(buf,len,pos2,&t2); dns_packet_copy(buf,len,pos2,header2,10); r = byte_diff(header1,4,header2); if (r < 0) return 1; if (r > 0) return 0; len1 = dns_domain_length(t1); len2 = dns_domain_length(t2); if (len1 < len2) return 1; if (len1 > len2) return 0; r = case_diffb(t1,len1,t2); if (r < 0) return 1; if (r > 0) return 0; if (pos1 < pos2) return 1; return 0; } static void addserver(struct query *z,const unsigned char *addr,const unsigned char *key) { int k; int i; unsigned char *kk; if (key) z->flaghavekeys[z->level - 1] = 1; for (k = 0;k < 256;k += 16) { i = k >> 4; if (byte_isequal(z->servers[z->level - 1] + k,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) { byte_copy(z->servers[z->level - 1] + k,16,addr); kk = z->keys[z->level - 1] + 33 * i; if (key) { byte_copy(kk + 1,32,key); kk[0] = flagdnscurvetype1; } else { kk[0] = 0; } break; } } /* add txt */ if (!key || !flagdnscurvetype2) return; for (k = 0;k < 256;k += 16) { i = k >> 4; kk = z->keys[z->level - 1] + 33 * i; if (byte_isequal(z->servers[z->level - 1] + k,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) { byte_copy(z->servers[z->level - 1] + k,16,addr); byte_copy(kk + 1,32,key); kk[0] = flagdnscurvetype2; break; } } } crypto_uint64 tx4 = 0; crypto_uint64 tx6 = 0; static int doit(struct query *z,int state) { unsigned char key[259]; unsigned char *cached; long long cachedlen; unsigned char *buf; long long len; unsigned char *whichserver; unsigned char *whichkey; unsigned char header[16]; unsigned char misc[20]; unsigned char pubkey[32]; int flaghaskey; long long rcode; long long posanswers; crypto_uint16 numanswers; long long posauthority; crypto_uint16 numauthority; /* long long posglue; */ crypto_uint16 numglue; long long pos; long long pos2; crypto_uint16 datalen; unsigned char *control; unsigned char *d; const unsigned char *dtype; long long dlen; int flagout; int flagcname; int flagreferral; unsigned char flagns; int flagsoa; long long ttl; long long soattl; long long cnamettl; long long cachedttl; unsigned char cachedflag; long long i; long long j; long long k; long long p; long long q; int flaghavekeys; errno = EIO; if (state == 1) goto HAVEPACKET; if (state == -1) { cachegeneric2(DNS_T_AXFR, z->type, z->name[z->level], (unsigned char *)"", 0, 10, 0); log_servfail(z->name[z->level]); goto SERVFAIL; } NEWNAME: if (++z->loop == QUERY_MAXLOOP) goto DIE; d = z->name[z->level]; /* dtype = z->level ? DNS_T_A : z->type; */ dtype = z->level ? (z->ipv6[z->level] ? DNS_T_AAAA : DNS_T_A) : z->type; dlen = dns_domain_length(d); if (globalip(d,misc) && typematch(DNS_T_A,dtype)) { if (z->level) { addserver(z,misc,0); goto LOWERLEVEL; } if (!rqa(z)) goto DIE; if (typematch(DNS_T_A,dtype)) { if (!response_rstart(d,DNS_T_A,655360)) goto DIE; if (!response_addbytes(misc + 12,4)) goto DIE; response_rfinish(RESPONSE_ANSWER); } cleanup(z); return 1; } if (globalip6(d,misc) && typematch(DNS_T_AAAA,dtype)) { if (z->level) { addserver(z,misc,0); goto LOWERLEVEL; } if (!rqa(z)) goto DIE; if (typematch(DNS_T_AAAA,dtype)) { if (!response_rstart(d,DNS_T_AAAA,655360)) goto DIE; if (!response_addbytes(misc,16)) goto DIE; response_rfinish(RESPONSE_ANSWER); } cleanup(z); return 1; } if (dns_domain_equal(d,(unsigned char *)"\0011\0010\0010\003127\7in-addr\4arpa\0")) { if (z->level) goto LOWERLEVEL; if (!rqa(z)) goto DIE; if (typematch(DNS_T_PTR,dtype)) { if (!response_rstart(d,DNS_T_PTR,655360)) goto DIE; if (!response_addname((unsigned char *)"\011localhost\0")) goto DIE; response_rfinish(RESPONSE_ANSWER); } cleanup(z); log_stats(); return 1; } if (dlen <= 255) { byte_copy(key,2,DNS_T_ANY); byte_copy(key + 2,dlen,d); case_lowerb(key + 2,dlen); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached) { log_cachednxdomain(d); goto NXDOMAIN; } byte_copy(key,2,DNS_T_AXFR); byte_copy(key + 2 + dlen,2,dtype); cached = cache_get(key,dlen + 4,&cachedlen,&ttl,0); if (cached && cachedlen == 0) { log_cachedservfail(d, dtype); goto SERVFAIL; } byte_copy(key,2,DNS_T_CNAME); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached) { if (typematch(DNS_T_CNAME,dtype)) { log_cachedanswer(d,DNS_T_CNAME); if (!rqa(z)) goto DIE; if (!response_cname(z->name[0],cached,ttl)) goto DIE; cleanup(z); return 1; } log_cachedcname(d,cached); if (!dns_domain_copy(&cname,cached)) goto DIE; goto CNAME; } if (typematch(DNS_T_NS,dtype)) { byte_copy(key,2,DNS_T_NS); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { log_cachedanswer(d,DNS_T_NS); if (!rqa(z)) goto DIE; pos = 0; while (pos = dns_packet_getname(cached,cachedlen,pos,&t2)) { if (!response_rstart(d,DNS_T_NS,ttl)) goto DIE; if (!response_addname(t2)) goto DIE; response_rfinish(RESPONSE_ANSWER); } cleanup(z); return 1; } } if (typematch(DNS_T_PTR,dtype)) { byte_copy(key,2,DNS_T_PTR); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { log_cachedanswer(d,DNS_T_PTR); if (!rqa(z)) goto DIE; pos = 0; while (pos = dns_packet_getname(cached,cachedlen,pos,&t2)) { if (!response_rstart(d,DNS_T_PTR,ttl)) goto DIE; if (!response_addname(t2)) goto DIE; response_rfinish(RESPONSE_ANSWER); } cleanup(z); return 1; } } if (typematch(DNS_T_MX,dtype)) { byte_copy(key,2,DNS_T_MX); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { log_cachedanswer(d,DNS_T_MX); if (!rqa(z)) goto DIE; pos = 0; while (pos = dns_packet_copy(cached,cachedlen,pos,misc,2)) { pos = dns_packet_getname(cached,cachedlen,pos,&t2); if (!pos) break; if (!response_rstart(d,DNS_T_MX,ttl)) goto DIE; if (!response_addbytes(misc,2)) goto DIE; if (!response_addname(t2)) goto DIE; response_rfinish(RESPONSE_ANSWER); } cleanup(z); return 1; } } if (typematch(DNS_T_SOA,dtype)) { byte_copy(key,2,DNS_T_SOA); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached && (cachedlen || byte_diff(dtype,2,DNS_T_ANY))) { log_cachedanswer(d,DNS_T_SOA); if (!rqa(z)) goto DIE; pos = 0; while (pos = dns_packet_copy(cached,cachedlen,pos,misc,20)) { pos = dns_packet_getname(cached,cachedlen,pos,&t2); if (!pos) break; pos = dns_packet_getname(cached,cachedlen,pos,&t3); if (!pos) break; if (!response_rstart(d,DNS_T_SOA,ttl)) goto DIE; if (!response_addname(t2)) goto DIE; if (!response_addname(t3)) goto DIE; if (!response_addbytes(misc,20)) goto DIE; response_rfinish(RESPONSE_ANSWER); } cleanup(z); return 1; } } if (typematch(DNS_T_A,dtype)) { byte_copy(key,2,DNS_T_A); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,&flagns); if (cached && !cachedlen && z->level) { /* if we were looking the A record up to find an NS, try IPv6 too */ z->ipv6[z->level]=1; goto NEWNAME; } if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { if (z->level) { flaghaskey = findkey(d,pubkey); dns_sortip4(cached, cachedlen); log_cachedanswer(d,DNS_T_A); while (cachedlen >= 4) { byte_copy(misc, 12, "\0\0\0\0\0\0\0\0\0\0\377\377"); byte_copy(misc + 12, 4, cached); addserver(z,misc,flaghaskey ? pubkey : 0); cached += 4; cachedlen -= 4; } /* if we were looking the A record up to find an NS, try IPv6 too */ byte_copy(key,2,DNS_T_AAAA); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,&flagns); if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { flaghaskey = findkey(d,pubkey); dns_sortip(cached, cachedlen); log_cachedanswer(d,DNS_T_AAAA); while (cachedlen >= 16) { addserver(z,cached,flaghaskey ? pubkey : 0); cached += 16; cachedlen -= 16; } } goto LOWERLEVEL; } dns_sortip4(cached, cachedlen); log_cachedanswer(d,DNS_T_A); if (!rqa(z)) goto DIE; while (cachedlen >= 4) { if (!response_rstart(d,DNS_T_A,ttl)) goto DIE; if (!response_addbytes(cached,4)) goto DIE; response_rfinish(RESPONSE_ANSWER); cached += 4; cachedlen -= 4; } if (!flagns){ cleanup(z); return 1; } byte_copy(key,2,DNS_T_NS); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { pos = 0; while (pos = dns_packet_getname(cached,cachedlen,pos,&t2)) { if (!response_rstart(d,DNS_T_NS,ttl)) goto DIE; if (!response_addname(t2)) goto DIE; response_rfinish(RESPONSE_AUTHORITY); } cleanup(z); return 1; } } } if (typematch(DNS_T_AAAA,dtype)) { byte_copy(key,2,DNS_T_AAAA); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,&flagns); if (cached && !cachedlen && z->level) { /* if we were looking the AAAA record up to find an NS, go to LOWERLEVEL */ goto LOWERLEVEL; } if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { if (z->level) { flaghaskey = findkey(d,pubkey); dns_sortip(cached, cachedlen); log_cachedanswer(d,DNS_T_AAAA); while (cachedlen >= 16) { byte_copy(misc, 16, cached); addserver(z,misc,flaghaskey ? pubkey : 0); cached += 16; cachedlen -= 16; } goto LOWERLEVEL; } dns_sortip(cached, cachedlen); log_cachedanswer(d,DNS_T_AAAA); if (!rqa(z)) goto DIE; while (cachedlen >= 16) { if (!response_rstart(d,DNS_T_AAAA,ttl)) goto DIE; if (!response_addbytes(cached,16)) goto DIE; response_rfinish(RESPONSE_ANSWER); cached += 16; cachedlen -= 16; } if (!flagns){ cleanup(z); return 1; } byte_copy(key,2,DNS_T_NS); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { pos = 0; while (pos = dns_packet_getname(cached,cachedlen,pos,&t2)) { if (!response_rstart(d,DNS_T_NS,ttl)) goto DIE; if (!response_addname(t2)) goto DIE; response_rfinish(RESPONSE_AUTHORITY); } cleanup(z); return 1; } } } if (!typematch(DNS_T_ANY,dtype) && !typematch(DNS_T_AXFR,dtype) && !typematch(DNS_T_CNAME,dtype) && !typematch(DNS_T_NS,dtype) && !typematch(DNS_T_PTR,dtype) && !typematch(DNS_T_A,dtype) && !typematch(DNS_T_MX,dtype) && !typematch(DNS_T_AAAA,dtype) && !typematch(DNS_T_SOA,dtype)) { byte_copy(key,2,dtype); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,&flagns); if (cached && (cachedlen || !byte_isequal(dtype,2,DNS_T_ANY))) { log_cachedanswer(d,dtype); if (!rqa(z)) goto DIE; while (cachedlen >= 2) { datalen = crypto_uint16_load_bigendian(cached); cached += 2; cachedlen -= 2; if (datalen > cachedlen) goto DIE; if (!response_rstart(d,dtype,ttl)) goto DIE; if (!response_addbytes(cached,datalen)) goto DIE; response_rfinish(RESPONSE_ANSWER); cached += datalen; cachedlen -= datalen; } cleanup(z); return 1; } } } for (;;) { if (roots(z->servers[z->level],z->keys[z->level],&flaghavekeys,d)) { z->flaghavekeys[z->level] = flaghavekeys; for (j = 0;j < QUERY_MAXNS;++j) dns_domain_free(&z->ns[z->level][j]); z->control[z->level] = d; break; } if (!flagforwardonly && (z->level < 2)) if (dlen < 255) { byte_copy(key,2,DNS_T_NS); byte_copy(key + 2,dlen,d); case_lowerb(key + 2,dlen); cached = cache_get(key,dlen + 2,&cachedlen,&ttl,0); if (cached && cachedlen) { z->control[z->level] = d; byte_zero(z->servers[z->level],256); byte_zero(z->keys[z->level],528); z->flaghavekeys[z->level] = 0; for (j = 0;j < QUERY_MAXNS;++j) dns_domain_free(&z->ns[z->level][j]); pos = 0; j = 0; while (pos = dns_packet_getname(cached,cachedlen,pos,&t1)) { log_cachedns(d,t1); if (j < QUERY_MAXNS) if (!dns_domain_copy(&z->ns[z->level][j++],t1)) goto DIE; } break; } } if (!*d) goto DIE; j = 1 + (unsigned int) (unsigned char) *d; dlen -= j; d += j; } HAVENS: for (j = 0;j < QUERY_MAXNS;++j) if (z->ns[z->level][j]) { if (z->level + 1 < QUERY_MAXLEVEL) { if (!dns_domain_copy(&z->name[z->level + 1],z->ns[z->level][j])) goto DIE; dns_domain_free(&z->ns[z->level][j]); ++z->level; z->ipv6[z->level]=0; goto NEWNAME; } dns_domain_free(&z->ns[z->level][j]); } for (j = 0;j < 256;j += 16) if (!byte_isequal(z->servers[z->level] + j,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) break; if (j == 256) { log_servfail(z->name[z->level]); goto SERVFAIL; } byte_copy(key,2,DNS_T_AXFR); for (j = 0;j < 256;j += 16) { k = j >> 4; if (!byte_isequal(z->servers[z->level] + j,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) { whichkey = z->keys[z->level] + 33 * k; if (whichkey[0]) { byte_copy(key + 2,32,whichkey + 1); cached = cache_get(key,34,&cachedlen,&ttl,0); if (cached && (cachedlen == 32)) { byte_copy(whichkey + 1,32,cached); continue; } crypto_box_curve25519xsalsa20poly1305_beforenm((unsigned char *) whichkey + 1,(const unsigned char *) whichkey + 1,(const unsigned char *) secretkey); cache_set(key,34,whichkey + 1,32,655360,0); } } } dns_sortipkey(z->servers[z->level],z->keys[z->level],256); /* dtype = z->level ? DNS_T_A : z->type; */ dtype = z->level ? (z->ipv6[z->level] ? DNS_T_AAAA : DNS_T_A) : z->type; log_tx(z->name[z->level],dtype,z->control[z->level],z->servers[z->level],z->keys[z->level],z->flaghavekeys[z->level],z->level); if (dns_transmit_startext(&z->dt,z->servers[z->level],flagforwardonly,flagtcponly,flagipv4only,z->name[z->level],dtype,z->localip,remoteport,z->keys[z->level],publickey,z->control[z->level]) == -1) goto DIE; return 0; LOWERLEVEL: dns_domain_free(&z->name[z->level]); for (j = 0;j < QUERY_MAXNS;++j) dns_domain_free(&z->ns[z->level][j]); --z->level; goto HAVENS; HAVEPACKET: if (++z->loop == QUERY_MAXLOOP) goto DIE; buf = z->dt.packet; len = z->dt.packetlen; whichserver = (unsigned char *)z->dt.servers + 16 * z->dt.curserver; whichkey = (unsigned char *)z->dt.keys + 33 * z->dt.curserver; if (xsocket_type(whichserver) == XSOCKET_V4) ++tx4; if (xsocket_type(whichserver) == XSOCKET_V6) ++tx6; control = z->control[z->level]; d = z->name[z->level]; /* dtype = z->level ? DNS_T_A : z->type; */ dtype = z->level ? (z->ipv6[z->level] ? DNS_T_AAAA : DNS_T_A) : z->type; pos = dns_packet_copy(buf,len,0,header,12); if (!pos) goto DIE; pos = dns_packet_skipname(buf,len,pos); if (!pos) goto DIE; pos += 4; posanswers = pos; numanswers = crypto_uint16_load_bigendian(header + 6); numauthority = crypto_uint16_load_bigendian(header + 8); numglue = crypto_uint16_load_bigendian(header + 10); rcode = header[3] & 15; if (rcode && (rcode != 3)) goto DIE; /* impossible; see irrelevant() */ flagout = 0; flagcname = 0; flagreferral = 0; flagns = 0; flagsoa = 0; soattl = 0; cnamettl = 0; for (j = 0;j < numanswers;++j) { pos = dns_packet_getname(buf,len,pos,&t1); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; if (dns_domain_equal(t1,d)) if (byte_isequal(header + 2,2,DNS_C_IN)) { /* should always be true */ if (byte_isequal(dtype,2,DNS_T_ANY)) if (byte_isequal(header,2,DNS_T_NS)) flagns = 1; if (typematch(header,dtype)) flagout = 1; else if (typematch(header,DNS_T_CNAME)) { if (!dns_packet_getname(buf,len,pos,&cname)) goto DIE; flagcname = 1; cnamettl = ttlget(header + 4); } } datalen=crypto_uint16_load_bigendian(header + 8); pos += datalen; } posauthority = pos; for (j = 0;j < numauthority;++j) { pos = dns_packet_getname(buf,len,pos,&t1); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; if (typematch(header,DNS_T_SOA)) { flagsoa = 1; soattl = ttlget(header + 4); if (soattl > 3600) soattl = 3600; } else if (typematch(header,DNS_T_NS)) { flagreferral = 1; if (dns_domain_equal(t1,d)) flagns = 1; if (!dns_domain_copy(&referral,t1)) goto DIE; } datalen=crypto_uint16_load_bigendian(header + 8); pos += datalen; } /* posglue = pos; */ if (!flagcname && !rcode && !flagout && flagreferral && !flagsoa) if (dns_domain_equal(referral,control) || !dns_domain_suffix(referral,control)) { log_lame(whichserver,control,referral); byte_zero(whichserver,16); goto HAVENS; } if (records) { alloc_free(records); records = 0; } k = numanswers + numauthority + numglue; records = (long long *) alloc(k * sizeof(long long)); if (!records) goto DIE; pos = posanswers; for (j = 0;j < k;++j) { records[j] = pos; pos = dns_packet_getname(buf,len,pos,&t1); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; datalen=crypto_uint16_load_bigendian(header + 8); pos += datalen; } i = j = k; while (j > 1) { if (i > 1) { --i; pos = records[i - 1]; } else { pos = records[j - 1]; records[j - 1] = records[i - 1]; --j; } q = i; while ((p = q * 2) < j) { if (!smaller(buf,len,records[p],records[p - 1])) ++p; records[q - 1] = records[p - 1]; q = p; } if (p == j) { records[q - 1] = records[p - 1]; q = p; } while ((q > i) && smaller(buf,len,records[(p = q/2) - 1],pos)) { records[q - 1] = records[p - 1]; q = p; } records[q - 1] = pos; } i = 0; while (i < k) { unsigned char type[2]; pos = dns_packet_getname(buf,len,records[i],&t1); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; ttl = ttlget(header + 4); byte_copy(type,2,header); if (!byte_isequal(header + 2,2,DNS_C_IN)) { ++i; continue; } for (j = i + 1;j < k;++j) { pos = dns_packet_getname(buf,len,records[j],&t2); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; if (!dns_domain_equal(t1,t2)) break; if (!byte_isequal(header,2,type)) break; if (!byte_isequal(header + 2,2,DNS_C_IN)) break; } if (!dns_domain_suffix(t1,control)) { i = j; continue; } if (!roots_same(t1,control)) { i = j; continue; } if (byte_isequal(type,2,DNS_T_ANY)) ; else if (byte_isequal(type,2,DNS_T_AXFR)) ; else if (byte_isequal(type,2,DNS_T_SOA)) { int non_authority = 0; save_start(); while (i < j) { pos = dns_packet_skipname(buf,len,records[i]); if (!pos) goto DIE; pos = dns_packet_getname(buf,len,pos + 10,&t2); if (!pos) goto DIE; pos = dns_packet_getname(buf,len,pos,&t3); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,misc,20); if (!pos) goto DIE; if (records[i] < posauthority) { log_rrsoa(whichserver,t1,t2,t3,misc,ttl,whichkey[0]); save_data(misc,20); save_data(t2,dns_domain_length(t2)); save_data(t3,dns_domain_length(t3)); non_authority++; } ++i; } if (non_authority) save_finish(DNS_T_SOA,t1,ttl,0); } else if (byte_isequal(type,2,DNS_T_CNAME)) { pos = dns_packet_skipname(buf,len,records[j - 1]); if (!pos) goto DIE; pos = dns_packet_getname(buf,len,pos + 10,&t2); if (!pos) goto DIE; log_rrcname(whichserver,t1,t2,ttl,whichkey[0]); cachegeneric(DNS_T_CNAME,t1,t2,dns_domain_length(t2),ttl,0); } else if (byte_isequal(type,2,DNS_T_PTR)) { save_start(); while (i < j) { pos = dns_packet_skipname(buf,len,records[i]); if (!pos) goto DIE; pos = dns_packet_getname(buf,len,pos + 10,&t2); if (!pos) goto DIE; log_rrptr(whichserver,t1,t2,ttl,whichkey[0]); save_data(t2,dns_domain_length(t2)); ++i; } save_finish(DNS_T_PTR,t1,ttl,0); } else if (byte_isequal(type,2,DNS_T_NS)) { cachedflag = 0; if (posauthority <= records[i]) { dlen = dns_domain_length(t1); byte_copy(key,2,DNS_T_NS); byte_copy(key + 2,dlen,t1); case_lowerb(key + 2,dlen); if (dns_domain_equal(t1,control)) { if (cache_get(key,dlen + 2,&cachedlen,&cachedttl,&cachedflag)) { if (cachedflag) if (cachedttl < ttl) ttl = cachedttl; } } } save_start(); while (i < j) { pos = dns_packet_skipname(buf,len,records[i]); if (!pos) goto DIE; pos = dns_packet_getname(buf,len,pos + 10,&t2); if (!pos) goto DIE; log_rrns(whichserver,t1,t2,ttl,whichkey[0]); save_data(t2,dns_domain_length(t2)); ++i; } save_finish(DNS_T_NS,t1,ttl,dns_domain_equal(t1,control)); } else if (byte_isequal(type,2,DNS_T_MX)) { save_start(); while (i < j) { pos = dns_packet_skipname(buf,len,records[i]); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos + 10,misc,2); if (!pos) goto DIE; pos = dns_packet_getname(buf,len,pos,&t2); if (!pos) goto DIE; log_rrmx(whichserver,t1,t2,misc,ttl,whichkey[0]); save_data(misc,2); save_data(t2,dns_domain_length(t2)); ++i; } save_finish(DNS_T_MX,t1,ttl,0); } else if (byte_isequal(type,2,DNS_T_A)) { save_start(); while (i < j) { pos = dns_packet_skipname(buf,len,records[i]); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; if (byte_isequal(header + 8,2,"\0\4")) { pos = dns_packet_copy(buf,len,pos,header,4); if (!pos) goto DIE; save_data(header,4); log_rra(whichserver,t1,header,ttl,whichkey[0]); } ++i; } save_finish(DNS_T_A,t1,ttl,flagns); } else if (byte_isequal(type,2,DNS_T_AAAA)) { save_start(); while (i < j) { pos = dns_packet_skipname(buf,len,records[i]); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; if (crypto_uint16_load_bigendian(header + 8) == 16) { pos = dns_packet_copy(buf,len,pos,header,16); if (!pos) goto DIE; save_data(header,16); log_rraaaa(whichserver,t1,header,ttl,whichkey[0]); } ++i; } save_finish(DNS_T_AAAA,t1,ttl,flagns); } else { save_start(); while (i < j) { pos = dns_packet_skipname(buf,len,records[i]); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; datalen=crypto_uint16_load_bigendian(header + 8); if (datalen > len - pos) goto DIE; save_data(header + 8,2); save_data(buf + pos,datalen); log_rr(whichserver,t1,type,buf + pos,datalen,ttl,whichkey[0]); ++i; } save_finish(type,t1,ttl,0); } i = j; } alloc_free(records); records = 0; if (flagcname) { ttl = cnamettl; CNAME: if (!z->level) { if (z->alias[QUERY_MAXALIAS - 1]) goto DIE; for (j = QUERY_MAXALIAS - 1;j > 0;--j) z->alias[j] = z->alias[j - 1]; for (j = QUERY_MAXALIAS - 1;j > 0;--j) z->aliasttl[j] = z->aliasttl[j - 1]; z->alias[0] = z->name[0]; z->aliasttl[0] = ttl; z->name[0] = 0; } if (!dns_domain_copy(&z->name[z->level],cname)) goto DIE; goto NEWNAME; } if (rcode == 3) { log_nxdomain(whichserver,d,soattl); cachegeneric(DNS_T_ANY,d,(unsigned char *)"",0,soattl,0); NXDOMAIN: if (z->level) goto LOWERLEVEL; if (!rqa(z)) goto DIE; response_nxdomain(); cleanup(z); return 1; } if (!flagout && flagsoa) if (!byte_isequal(DNS_T_ANY,2,dtype)) if (!byte_isequal(DNS_T_AXFR,2,dtype)) if (!byte_isequal(DNS_T_CNAME,2,dtype)) { save_start(); save_finish(dtype,d,soattl,0); log_nodata(whichserver,d,dtype,soattl); if (z->level && byte_isequal(DNS_T_A,2,dtype)) { d = z->name[z->level]; z->ipv6[z->level] = 1; goto NEWNAME; /* retry, will ask for AAAA next */ } } log_stats(); if (flagout || flagsoa || !flagreferral) { if (z->level) { flaghaskey = findkey(d,pubkey); pos = posanswers; for (j = 0;j < numanswers;++j) { pos = dns_packet_getname(buf,len,pos,&t1); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; datalen=crypto_uint16_load_bigendian(header + 8); if (dns_domain_equal(t1,d)) { if (typematch(header,DNS_T_A)) if (byte_isequal(header + 2,2,DNS_C_IN)) /* should always be true */ if (datalen == 4) { byte_copy(misc, 12, "\0\0\0\0\0\0\0\0\0\0\377\377"); if (!dns_packet_copy(buf,len,pos,misc+12,4)) goto DIE; addserver(z,misc,flaghaskey ? pubkey : 0); } if (typematch(header,DNS_T_AAAA)) if (byte_isequal(header + 2,2,DNS_C_IN)) /* should always be true */ if (datalen == 16) { if (!dns_packet_copy(buf,len,pos,misc,16)) goto DIE; addserver(z,misc,flaghaskey ? pubkey : 0); } } pos += datalen; } goto LOWERLEVEL; } if (!rqa(z)) goto DIE; pos = posanswers; for (j = 0;j < numanswers;++j) { pos = dns_packet_getname(buf,len,pos,&t1); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; ttl = ttlget(header + 4); datalen=crypto_uint16_load_bigendian(header + 8); if (dns_domain_equal(t1,d)) if (byte_isequal(header + 2,2,DNS_C_IN)) /* should always be true */ if (typematch(header,dtype)) { if (!response_rstart(t1,header,ttl)) goto DIE; if (typematch(header,DNS_T_NS) || typematch(header,DNS_T_CNAME) || typematch(header,DNS_T_PTR)) { if (!dns_packet_getname(buf,len,pos,&t2)) goto DIE; if (!response_addname(t2)) goto DIE; } else if (typematch(header,DNS_T_MX)) { pos2 = dns_packet_copy(buf,len,pos,misc,2); if (!pos2) goto DIE; if (!response_addbytes(misc,2)) goto DIE; if (!dns_packet_getname(buf,len,pos2,&t2)) goto DIE; if (!response_addname(t2)) goto DIE; } else if (typematch(header,DNS_T_SOA)) { pos2 = dns_packet_getname(buf,len,pos,&t2); if (!pos2) goto DIE; if (!response_addname(t2)) goto DIE; pos2 = dns_packet_getname(buf,len,pos2,&t3); if (!pos2) goto DIE; if (!response_addname(t3)) goto DIE; pos2 = dns_packet_copy(buf,len,pos2,misc,20); if (!pos2) goto DIE; if (!response_addbytes(misc,20)) goto DIE; } else { if (pos + datalen > len) goto DIE; if (!response_addbytes(buf + pos,datalen)) goto DIE; } response_rfinish(RESPONSE_ANSWER); } pos += datalen; } pos = posauthority; for (j = 0;j < numauthority;++j) { pos = dns_packet_getname(buf,len,pos,&t1); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; ttl = ttlget(header + 4); datalen = crypto_uint16_load_bigendian(header + 8); if (dns_domain_equal(t1,d)) if (byte_isequal(header + 2,2,DNS_C_IN)) /* should always be true */ if (typematch(header,DNS_T_NS)) { if (!response_rstart(t1,header,ttl)) goto DIE; if (!dns_packet_getname(buf,len,pos,&t2)) goto DIE; if (!response_addname(t2)) goto DIE; response_rfinish(RESPONSE_AUTHORITY); } pos += datalen; } cleanup(z); return 1; } if (!dns_domain_suffix(d,referral)) goto DIE; /* In strict "forwardonly" mode, we don't, as the manual states, ** contact a chain of servers according to "NS" resource records. ** We don't obey any referral responses, therefore. Instead, we ** eliminate the server from the list and try the next one. */ if (flagforwardonly) { log_ignore_referral(whichserver,control,referral); byte_zero(whichserver,16); goto HAVENS; } control = d + dns_domain_suffixpos(d,referral); z->control[z->level] = control; byte_zero(z->servers[z->level],256); z->flaghavekeys[z->level] = 0; for (j = 0;j < QUERY_MAXNS;++j) dns_domain_free(&z->ns[z->level][j]); k = 0; pos = posauthority; for (j = 0;j < numauthority;++j) { pos = dns_packet_getname(buf,len,pos,&t1); if (!pos) goto DIE; pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) goto DIE; datalen = crypto_uint16_load_bigendian(header + 8); if (dns_domain_equal(referral,t1)) /* should always be true */ if (typematch(header,DNS_T_NS)) /* should always be true */ if (byte_isequal(header + 2,2,DNS_C_IN)) /* should always be true */ if (k < QUERY_MAXNS) if (!dns_packet_getname(buf,len,pos,&z->ns[z->level][k++])) goto DIE; pos += datalen; } goto HAVENS; SERVFAIL: if (z->level) goto LOWERLEVEL; if (!rqa(z)) goto DIE; response_servfail(); cleanup(z); return 1; DIE: cleanup(z); if (records) { alloc_free(records); records = 0; } return -1; } int query_start(struct query *z,unsigned char *dn,unsigned char type[2],unsigned char class[2],unsigned char localip[32]) { if (byte_isequal(type,2,DNS_T_AXFR)) { errno = EPERM; return -1; } cleanup(z); z->level = 0; z->loop = 0; if (!dns_domain_copy(&z->name[0],dn)) return -1; byte_copy(z->type,2,type); byte_copy(z->class,2,class); byte_copy(z->localip,32,localip); z->ipv6[0]=0; return doit(z,0); } int query_get(struct query *z,struct pollfd *x,long long stamp) { switch(dns_transmit_get(&z->dt,x,stamp)) { case 1: return doit(z,1); case -1: return doit(z,-1); } return 0; } void query_io(struct query *z,struct pollfd *x,long long *deadline) { dns_transmit_io(&z->dt,x,deadline); } dq-20250201/query.h000066400000000000000000000024211474734131400137000ustar00rootroot00000000000000#ifndef QUERY_H #define QUERY_H #include "dns.h" #include "crypto_uint32.h" #define QUERY_MAXLEVEL 5 #define QUERY_MAXALIAS 16 #define QUERY_MAXNS 16 #define QUERY_MAXLOOP 500 struct query { unsigned int loop; unsigned int level; unsigned char *name[QUERY_MAXLEVEL]; unsigned char *control[QUERY_MAXLEVEL]; /* pointing inside name */ unsigned char *ns[QUERY_MAXLEVEL][QUERY_MAXNS]; unsigned char servers[QUERY_MAXLEVEL][256]; unsigned char keys[QUERY_MAXLEVEL][528]; int flaghavekeys[QUERY_MAXLEVEL]; int ipv6[QUERY_MAXLEVEL]; unsigned char *alias[QUERY_MAXALIAS]; crypto_uint32 aliasttl[QUERY_MAXALIAS]; unsigned char localip[32]; unsigned char type[2]; unsigned char class[2]; struct dns_transmit dt; } ; extern int query_start(struct query *,unsigned char *,unsigned char [2],unsigned char [2],unsigned char [32]); extern void query_io(struct query *,struct pollfd *,long long *); extern int query_get(struct query *,struct pollfd *,long long); extern void query_init(const unsigned char *); extern void query_purge(void); extern void query_forwardonly(void); extern void query_tcponly(void); extern void query_ipv4only(void); extern void query_minttl(long long); extern void query_remoteport(unsigned char *); extern void query_dnscurvetype(char *); #endif dq-20250201/randombytes.c000066400000000000000000000020461474734131400150600ustar00rootroot00000000000000#include "randombytes.h" #include "haslibrandombytes.h" #ifndef HASLIBRANDOMBYTES #include #include #include #include static int fd = -1; __attribute__((constructor)) static void init(void) { if (fd == -1) { for (;;) { #ifdef O_CLOEXEC fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); #else fd = open("/dev/urandom", O_RDONLY); fcntl(fd, F_SETFD, 1); #endif if (fd != -1) break; sleep(1); } } } void randombytes(void *xv, long long xlen) { long long i; unsigned char *x = xv; if (fd == -1) init(); while (xlen > 0) { if (xlen < 1048576) i = xlen; else i = 1048576; i = read(fd, x, i); if (i < 1) { sleep(1); continue; } x += i; xlen -= i; } #ifdef __GNUC__ __asm__ __volatile__("" : : "r"(xv) : "memory"); #endif } const char *randombytes_source(void) { return "kernel-devurandom"; } #endif dq-20250201/randombytes.h000066400000000000000000000005531474734131400150660ustar00rootroot00000000000000#ifndef RANDOMBYTES_H____ #define RANDOMBYTES_H____ #include "haslibrandombytes.h" #ifdef HASLIBRANDOMBYTES #include #else #define randombytes randombytes_internal_void_voidstar_longlong #define randombytes_source randombytes_internal_source extern void randombytes(void *, long long); extern const char *randombytes_source(void); #endif #endif dq-20250201/randommod.c000066400000000000000000000006451474734131400145140ustar00rootroot00000000000000/* taken from nacl-20110221, from curvecp/randommod.c (public-domain) */ #include "randombytes.h" #include "randommod.h" /* XXX: current implementation is limited to n<2^55 */ long long randommod(long long n) { long long result = 0; long long j; unsigned char r[32]; if (n <= 1) return 0; randombytes(r,32); for (j = 0;j < 32;++j) result = (result * 256 + (unsigned long long) r[j]) % n; return result; } dq-20250201/randommod.h000066400000000000000000000001421474734131400145110ustar00rootroot00000000000000#ifndef _RANDOMMOD_H____ #define _RANDOMMOD_H____ extern long long randommod(long long); #endif dq-20250201/response.c000066400000000000000000000060151474734131400143670ustar00rootroot00000000000000#include "dns.h" #include "byte.h" #include "crypto_uint16.h" #include "crypto_uint32.h" #include "response.h" unsigned char response[65535]; long long response_len = 0; /* <= 65535 */ static long long tctarget; #define NAMES 100 static unsigned char name[NAMES][255]; static long long name_ptr[NAMES]; /* each < 16384 */ static long long name_num; int response_addbytes(const unsigned char *buf, long long len) { if (len < 0) return 0; if (len > 65535 - response_len) return 0; byte_copy(response + response_len, len, buf); response_len += len; return 1; } int response_addname(const unsigned char *d) { long long dlen; long long i; unsigned char buf[2]; dlen = dns_domain_length(d); while (*d) { for (i = 0; i < name_num; ++i) if (dns_domain_equal(d, name[i])) { crypto_uint16_store_bigendian(buf, 49152 + name_ptr[i]); return response_addbytes(buf, 2); } if ((dlen <= 255) && (response_len < 16384)) if (name_num < NAMES) { byte_copy(name[name_num], dlen, d); name_ptr[name_num] = response_len; ++name_num; } i = *d; ++i; if (!response_addbytes(d, i)) return 0; d += i; dlen -= i; } return response_addbytes(d, 1); } int response_query(const unsigned char *q, const unsigned char qtype[2], const unsigned char qclass[2]) { response_len = 0; name_num = 0; if (!response_addbytes((unsigned char *)"\0\0\201\200\0\1\0\0\0\0\0\0", 12)) return 0; if (!response_addname(q)) return 0; if (!response_addbytes(qtype, 2)) return 0; if (!response_addbytes(qclass, 2)) return 0; tctarget = response_len; return 1; } static long long dpos; static int flaghidettl = 0; void response_hidettl(void) { flaghidettl = 1; } int response_rstart(const unsigned char *d, const unsigned char type[2], crypto_uint32 ttl) { unsigned char ttlstr[4]; if (!response_addname(d)) return 0; if (!response_addbytes(type, 2)) return 0; if (!response_addbytes(DNS_C_IN, 2)) return 0; if (flaghidettl) ttl = 0; crypto_uint32_store_bigendian(ttlstr, ttl); if (!response_addbytes(ttlstr, 4)) return 0; if (!response_addbytes((unsigned char *)"\0\0", 2)) return 0; dpos = response_len; return 1; } void response_rfinish(int x) { crypto_uint16_store_bigendian(response + dpos - 2, response_len - dpos); if (!++response[x + 1]) ++response[x]; } int response_cname(const unsigned char *c, const unsigned char *d, crypto_uint32 ttl) { if (!response_rstart(c, DNS_T_CNAME, ttl)) return 0; if (!response_addname(d)) return 0; response_rfinish(RESPONSE_ANSWER); return 1; } void response_nxdomain(void) { response[3] |= 3; response[2] |= 4; } void response_servfail(void) { response[3] |= 2; } void response_id(const unsigned char id[2]) { byte_copy(response, 2, id); } void response_tc(void) { response[2] |= 2; response_len = tctarget; byte_zero(response + 6, 6); } dq-20250201/response.h000066400000000000000000000015221474734131400143720ustar00rootroot00000000000000#ifndef RESPONSE_H #define RESPONSE_H #include "crypto_uint32.h" extern unsigned char response[]; extern long long response_len; extern int response_query(const unsigned char *, const unsigned char [2], const unsigned char [2]); extern void response_nxdomain(void); extern void response_servfail(void); extern void response_id(const unsigned char [2]); extern void response_tc(void); extern int response_addbytes(const unsigned char *, long long); extern int response_addname(const unsigned char *); extern void response_hidettl(void); extern int response_rstart(const unsigned char *, const unsigned char [2], crypto_uint32); extern void response_rfinish(int); #define RESPONSE_ANSWER 6 #define RESPONSE_AUTHORITY 8 #define RESPONSE_ADDITIONAL 10 extern int response_cname(const unsigned char *, const unsigned char *, crypto_uint32); #endif dq-20250201/roots.c000066400000000000000000000122251474734131400136770ustar00rootroot00000000000000#include #include "open.h" #include "e.h" #include "str.h" #include "byte.h" #include "direntry.h" #include "strtoip.h" #include "dns.h" #include "openreadclose.h" #include "roots.h" #include "keyparse.h" static int flagdnscurvetype1 = 1; static int flagdnscurvetype2 = 2; static stralloc data; static stralloc text; static long long roots_find(unsigned char *q) { long long i,j; i = 0; while (i < data.len) { j = dns_domain_length(data.s + i); if (dns_domain_equal(data.s + i, q)) return i + j; i += j; i += 256 + 528 + 1; } return -1; } static long long roots_search(unsigned char *q) { long long r; for (;;) { r = roots_find(q); if (r >= 0) return r; if (!*q) return -1; /* user misconfiguration */ q += *q; q += 1; } } int roots(unsigned char servers[256], unsigned char keys[528], int *flaghavekeys, unsigned char *q) { long long r; r = roots_find(q); if (r == -1) return 0; byte_copy(servers, 256, data.s + r); byte_copy(keys, 528, data.s + r + 256); *flaghavekeys = 0; if (data.s[r + 256 + 528]) *flaghavekeys = 1; return 1; } int roots_same(unsigned char *q, unsigned char *q2) { return roots_search(q) == roots_search(q2); } static int init2(DIR *dir) { direntry *d; const char *fqdn; static unsigned char *q; unsigned char servers[256]; unsigned char keys[528]; int flaghavekeys; long long serverslen; long long keyslen; long long i; long long j; long long k; unsigned char *kk; byte_zero(keys, 528); for (;;) { errno = 0; d = readdir(dir); if (!d) { if (errno) return 0; return 1; } if (d->d_name[0] != '.') { if (openreadclose(d->d_name, &text, 32) != 1) return 0; if (!stralloc_append(&text,"\n")) return 0; fqdn = d->d_name; if (str_equal(fqdn,"@")) fqdn = "."; if (!dns_domain_fromdot(&q,(unsigned char *)fqdn,str_len(fqdn))) return 0; serverslen = 0; keyslen = 0; j = 0; k = 0; flaghavekeys = 0; if (byte_chr(text.s, text.len, '|') != text.len) { flaghavekeys = 1; } for (i = 0;i < text.len;++i) { if (text.s[i] == '|') { k = i + 1; text.s[i] = 0; continue; } if (text.s[i] == '\n') { text.s[i] = 0; if (serverslen <= 240) { if (strtoip(servers + serverslen, (char *)text.s + j)) { kk = keys + keyslen; kk[0] = 0; if (k && keyparse(kk + 1, 32, (char *)text.s + k)) { kk[0] = flagdnscurvetype1; serverslen += 16; keyslen += 33; /* add txt */ if (flagdnscurvetype2 && serverslen <= 240) { byte_copy(servers + serverslen, 16, servers + serverslen - 16); byte_copy(keys + keyslen, 33, keys + keyslen - 33); kk = keys + keyslen; kk[0] = flagdnscurvetype2; serverslen += 16; keyslen += 33; } } else { serverslen += 16; keyslen += 33; } } } j = i + 1; k = 0; } } byte_zero(servers + serverslen,256 - serverslen); byte_zero(keys + keyslen,528 - keyslen); if (!stralloc_catb(&data,q,dns_domain_length(q))) return 0; if (!stralloc_catb(&data,servers,256)) return 0; if (!stralloc_catb(&data,keys,528)) return 0; if (flaghavekeys) { if (!stralloc_catb(&data,"1",1)) return 0; } else { if (!stralloc_0(&data)) return 0; } } } } static int init1(void) { DIR *dir; int r; if (chdir("servers") == -1) return 0; dir = opendir("."); if (!dir) return 0; r = init2(dir); closedir(dir); return r; } int roots_init(char *x) { int fddir; int r; if (x) { if (*x == '1') { /* streamlined only */ flagdnscurvetype1 = 1; flagdnscurvetype2 = 0; } else if (*x == '2') { /* txt only */ flagdnscurvetype1 = 2; flagdnscurvetype2 = 0; } else { /* mixed */ flagdnscurvetype1 = 1; flagdnscurvetype2 = 2; } } if (!stralloc_copys(&data, "")) return 0; fddir = open_read("."); if (fddir == -1) return 0; r = init1(); if (fchdir(fddir) == -1) r = 0; close(fddir); return r; } dq-20250201/roots.h000066400000000000000000000003251474734131400137020ustar00rootroot00000000000000#ifndef ROOTS_H #define ROOTS_H extern int roots(unsigned char [256], unsigned char [528], int *, unsigned char *); extern int roots_same(unsigned char *, unsigned char *); extern int roots_init(char *); #endif dq-20250201/salsa.c000066400000000000000000000047461474734131400136450ustar00rootroot00000000000000/* 20140727 20250136 - use cryptoint Jan Mojzis Public domain. */ #include "crypto_uint64.h" #include "crypto_uint32.h" #include "salsa.h" #define ROTATE(x, c) ((x) << (c)) | ((x) >> (32 - (c))) /* sigma: "expand 32-byte k" */ static const crypto_uint32 sigma[4] = { 0x61707865, 0x3320646E, 0x79622D32, 0x6B206574 }; void salsa_core(unsigned char *out, crypto_uint32 *n, const crypto_uint32 *k, const crypto_uint32 *c, int h, long long r) { long long i, j, m; crypto_uint32 x[16], y[16], t[4], w[16]; for (i = 0; i < 4; ++i) { x[i * 5] = c[i ]; x[i + 1] = k[i ]; x[i + 6] = n[i ]; x[i + 11] = k[i + 4]; } for (i = 0; i < 16; ++i) y[i] = x[i]; for (i = 0; i < r; ++i) { for (j = 0; j < 4; ++j) { for (m = 0; m < 4; ++m) t[m] = x[(5 * j + 4 * m) % 16]; t[1] ^= ROTATE(t[0] + t[3], 7); t[2] ^= ROTATE(t[1] + t[0], 9); t[3] ^= ROTATE(t[2] + t[1], 13); t[0] ^= ROTATE(t[3] + t[2], 18); for (m = 0; m < 4; ++m) w[4 * j + (j + m) % 4] = t[m]; } for (j = 0; j < 16; ++j) x[j] = w[j]; } if (h) { for (i = 0; i < 16; ++i) x[i] += y[i]; for (i = 0; i < 4; ++i) { x[5 * i] -= c[i]; x[6 + i] -= n[i]; } for (i = 0; i < 4; ++i) { crypto_uint32_store(out + 4 * i, x[5 * i]); crypto_uint32_store(out + 16 + 4 * i, x[6 + i]); } } else { for (i = 0; i < 16; ++i) crypto_uint32_store(out + 4 * i, x[i] + y[i]); } } int salsa_stream_xor(unsigned char *c, const unsigned char *m, unsigned long long l, const unsigned char *nn, const unsigned char *kk, long long r) { unsigned char x[64]; crypto_uint32 k[8], n[4]; long long i; crypto_uint64 u; if (!l) return 0; for (i = 0; i < 8; ++i) k[i ] = crypto_uint32_load(kk + 4 * i); for (i = 0; i < 2; ++i) n[i + 2] = 0; for (i = 0; i < 2; ++i) n[i ] = crypto_uint32_load(nn + 4 * i); while (l >= 64) { salsa_core(m ? x : c, n, k, sigma, 0, r); if (m) for (i = 0; i < 64; ++i) c[i] = m[i] ^ x[i]; u = 1; for (i = 0; i < 2; ++i) { u += (crypto_uint64)n[i + 2]; n[i + 2] = u; u >>= 32; } l -= 64; c += 64; if (m) m += 64; } if (l) { salsa_core(x, n, k, sigma, 0, r); for (i = 0; i < l; ++i) c[i] = (m ? m[i] : 0) ^ x[i]; } return 0; } dq-20250201/salsa.h000066400000000000000000000005311474734131400136360ustar00rootroot00000000000000#ifndef _SALSA_H____ #define _SALSA_H____ #include "crypto_uint32.h" extern void salsa_core(unsigned char *, crypto_uint32 *, const crypto_uint32 *, const crypto_uint32 *, int, long long); extern int salsa_stream_xor(unsigned char *, const unsigned char *, unsigned long long, const unsigned char *, const unsigned char *, long long); #endif dq-20250201/seconds.c000066400000000000000000000001431474734131400141630ustar00rootroot00000000000000#include "seconds.h" #include long long seconds(void) { return time((time_t *)0); } dq-20250201/seconds.h000066400000000000000000000001271474734131400141720ustar00rootroot00000000000000#ifndef _SECONDS_H____ #define _SECONDS_H____ extern long long seconds(void); #endif dq-20250201/siphash.c000066400000000000000000000026451474734131400141750ustar00rootroot00000000000000/* - based on crypto_auth/siphash24/little2 from supercop-20140622 */ #include "crypto_uint64.h" #include "siphash.h" #define ROTATE(x,b) x = (x << b) | (x >> (64 - b)) #define ROUND \ do { \ v0 += v1; v2 += v3; \ ROTATE(v1,13); ROTATE(v3,16); \ v1 ^= v0; v3 ^= v2; \ ROTATE(v0,32); \ v2 += v1; v0 += v3; \ ROTATE(v1,17); ROTATE(v3,21); \ v1 ^= v2; v3 ^= v0; \ ROTATE(v2,32); \ } while(0); int siphash(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *k, long long rounds, long long finalrounds) { crypto_uint64 v0, v1, v2, v3, lastblock = inlen; unsigned char block[8]; long long i; v0 = v2 = crypto_uint64_load(k + 0); v1 = v3 = crypto_uint64_load(k + 8); v0 ^= 0x736f6d6570736575; v1 ^= 0x646f72616e646f6d; v2 ^= 0x6c7967656e657261; v3 ^= 0x7465646279746573; while (inlen >= 8) { crypto_uint64 mi = crypto_uint64_load(in); in += 8; v3 ^= mi; for (i = 0; i < rounds; ++i) ROUND v0 ^= mi; inlen -= 8; } for (i = 0; i < 7; ++i) block[i] = 0; for (i = 0; i < inlen; ++i) block[i] = in[i]; block[7] = lastblock; lastblock = crypto_uint64_load(block); v3 ^= lastblock; for (i = 0; i < rounds; ++i) ROUND v0 ^= lastblock; v2 ^= 0xff; for (i = 0; i < finalrounds; ++i) ROUND crypto_uint64_store(out, (v0 ^ v1) ^ (v2 ^ v3)); return 0; } dq-20250201/siphash.h000066400000000000000000000002661474734131400141770ustar00rootroot00000000000000#ifndef _SIPHASH_H_____ #define _SIPHASH_H_____ extern int siphash(unsigned char *, const unsigned char *, unsigned long long, const unsigned char *, long long, long long); #endif dq-20250201/str.c000066400000000000000000000023431474734131400133410ustar00rootroot00000000000000/* 20130505 Jan Mojzis Public domain. */ #include "str.h" long long str_len(const char *s) { long long i; for (i = 0; s[i]; ++i); return i; } long long str_chr(const char *s, int c) { long long i; char ch = c; for (i = 0; s[i]; ++i) if (s[i] == ch) break; return i; } long long str_rchr(const char *s, int c) { long long i, u = -1; char ch = c; for (i = 0; s[i]; ++i) if (s[i] == ch) u = i; if (u != -1) return u; return i; } int str_diff(const char *s, const char *t) { register char x; for (;;) { x = *s; if (x != *t) break; if (!x) break; ++s; ++t; x = *s; if (x != *t) break; if (!x) break; ++s; ++t; x = *s; if (x != *t) break; if (!x) break; ++s; ++t; x = *s; if (x != *t) break; if (!x) break; ++s; ++t; } return ((int)(unsigned int)(unsigned char) x) - ((int)(unsigned int)(unsigned char) *t); } int str_start(const char *s, const char *t) { char x; for (;;) { x = *t++; if (!x) return 1; if (x != *s++) return 0; x = *t++; if (!x) return 1; if (x != *s++) return 0; x = *t++; if (!x) return 1; if (x != *s++) return 0; x = *t++; if (!x) return 1; if (x != *s++) return 0; } } dq-20250201/str.h000066400000000000000000000005011474734131400133400ustar00rootroot00000000000000#ifndef _STR_H____ #define _STR_H____ extern long long str_len(const char *); extern long long str_chr(const char *, int); extern long long str_rchr(const char *, int); extern int str_diff(const char *, const char *); extern int str_start(const char *, const char *); #define str_equal(s,t) (!str_diff((s),(t))) #endif dq-20250201/stralloc.c000066400000000000000000000055161474734131400143610ustar00rootroot00000000000000/* 20130505 Jan Mojzis Public domain. */ #include "alloc.h" #include "e.h" #include "stralloc.h" int stralloc_readyplus(stralloc *r, long long len) { unsigned char *newdata; long long i; if (!r || len < 0) { errno = EINVAL; return 0; } if (len == 0) return 1; if (r->len + len + 1 > r->alloc) { while (r->len + len + 1 > r->alloc) r->alloc = 2 * r->alloc + 1; newdata = alloc(r->alloc); if (!newdata) return 0; if (r->s) { for (i = 0; i < r->len; ++i) newdata[i] = r->s[i]; alloc_free(r->s); } r->s = newdata; } return 1; } int stralloc_catb(stralloc *r, const void *xv, long long xlen) { const unsigned char *x = xv; long long i; if (!r || !xv || xlen < 0) { errno = EINVAL; return 0; } if (xlen == 0) return 1; if (!stralloc_readyplus(r, xlen)) return 0; for (i = 0; i < xlen; ++i) r->s[r->len + i] = x[i]; r->len += xlen; return 1; } int stralloc_cats(stralloc *r, const void *xv) { const unsigned char *x = xv; long long xlen; if (!r || !xv) { errno = EINVAL; return 0; } for (xlen = 0; x[xlen]; ++xlen); return stralloc_catb(r, x, xlen); } int stralloc_cat(stralloc *x, stralloc *y) { if (!y) { errno = EINVAL; return 0; } return stralloc_catb(x, y->s, y->len); } int stralloc_copyb(stralloc *r, const void *xv, long long xlen) { if (!r) { errno = EINVAL; return 0; } r->len = 0; return stralloc_catb(r, xv, xlen); } int stralloc_copys(stralloc *r, const void *xv) { if (!r) { errno = EINVAL; return 0; } r->len = 0; return stralloc_cats(r, xv); } int stralloc_copy(stralloc *x, stralloc *y) { if (!x || !y) { errno = EINVAL; return 0; } x->len = 0; return stralloc_cat(x, y); } int stralloc_append(stralloc *r, const void *xv) { return stralloc_catb(r, xv, 1); } int stralloc_0(stralloc *r) { return stralloc_append(r, ""); } static int stralloc_catunum0(stralloc *sa, unsigned long long u, long long n) { long long len; unsigned long long q; unsigned char *s; if (!sa) { errno = EINVAL; return 0; } len = 1; q = u; while (q > 9) { ++len; q /= 10; } if (len < n) len = n; if (!stralloc_readyplus(sa, len)) return 0; s = sa->s + sa->len; sa->len += len; while (len) { s[--len] = '0' + (u % 10); u /= 10; } return 1; } int stralloc_catnum0(stralloc *sa, long long l, long long n) { if (!sa) { errno = EINVAL; return 0; } if (l < 0) { if (!stralloc_append(sa,"-")) return 0; l = -l; } return stralloc_catunum0(sa, l, n); } int stralloc_catnum(stralloc *r, long long num) { return stralloc_catnum0(r, num, 0); } void stralloc_free(stralloc *r) { if (!r) return; if (r->s) alloc_free(r->s); r->s = 0; r->len = 0; r->alloc = 0; } dq-20250201/stralloc.h000066400000000000000000000014211474734131400143550ustar00rootroot00000000000000#ifndef _STRALLOC_H____ #define _STRALLOC_H____ typedef struct stralloc { unsigned char *s; long long len; long long alloc; } stralloc; extern int stralloc_readyplus(stralloc *, long long); extern int stralloc_catb(stralloc *, const void *, long long); extern int stralloc_cats(stralloc *, const void *); extern int stralloc_cat(stralloc *, stralloc *); extern int stralloc_copyb(stralloc *, const void *, long long); extern int stralloc_copys(stralloc *, const void *); extern int stralloc_copy(stralloc *, stralloc *); extern int stralloc_append(stralloc *, const void *); extern int stralloc_0(stralloc *); extern void stralloc_free(stralloc *); extern int stralloc_catnum(stralloc *, long long); extern int stralloc_catnum0(stralloc *, long long, long long); #endif dq-20250201/strtoip.c000066400000000000000000000021411474734131400142310ustar00rootroot00000000000000/* 20130604 Jan Mojzis Public domain. */ #include #include #include "byte.h" #include "strtoip.h" /* taken from nacl-20110221, from curvecp/curvecpserver.c (public-domain) */ int strtoip4(unsigned char *ip, const char *x) { unsigned char *y = ip + 12; long long j; long long k; long long d; if (!x) return 0; for (k = 0; k < 4; ++k) y[k] = 0; for (k = 0; k < 4; ++k) { d = 0; for (j = 0; j < 3 && x[j] >= '0' && x[j] <= '9'; ++j) d = d * 10 + (x[j] - '0'); if (j == 0) return 0; x += j; if (k >= 0 && k < 4) y[k] = d; if (k < 3) { if (*x != '.') return 0; ++x; } } if (*x) return 0; byte_copy(ip, 12, "\0\0\0\0\0\0\0\0\0\0\377\377"); return 1; } /* XXX TODO rewrite without inet_pton */ int strtoip6(unsigned char *ip, const char *x) { if (!x) return 0; if (inet_pton(AF_INET6, x, ip) != 1) return 0; return 1; } int strtoip(unsigned char *ip, const char *x) { if (strtoip4(ip, x)) return 1; if (strtoip6(ip, x)) return 1; return 0; } dq-20250201/strtoip.h000066400000000000000000000003221474734131400142350ustar00rootroot00000000000000#ifndef _STRTOIP_H____ #define _STRTOIP_H____ extern int strtoip4(unsigned char *, const char *); extern int strtoip6(unsigned char *, const char *); extern int strtoip(unsigned char *, const char *); #endif dq-20250201/strtomultiip.c000066400000000000000000000034731474734131400153150ustar00rootroot00000000000000/* 20130607 Jan Mojzis Public domain. */ #include "byte.h" #include "str.h" #include "strtoip.h" #include "strtomultiip.h" static char separators[] = { ',', ' ', '\t', '\n', 0 }; static long long _strtomultiip(unsigned char *ip, long long ipmax, const char *x, int (*op)(unsigned char *, const char *)) { unsigned char data[STRTOMULTIIP_BUFSIZE]; long long i, j, k; long long iplen = 0; long long len; if (!x) return 0; byte_zero(ip, ipmax); len = str_len(x); if (len > STRTOMULTIIP_BUFSIZE - 1) { len = STRTOMULTIIP_BUFSIZE - 1; for (j = 0; separators[j]; ++j) len = byte_rchr(x, len, separators[j]); } byte_copy(data, len, x); data[len++] = 0; /* separators ',' ' ' '\t' '\n' */ for (i = 0; i < len; ++i) { for (j = 0; separators[j]; ++j) if (data[i] == separators[j]) data[i] = 0; } /* separator '.' - IPv4 backward compatibility */ j = byte_chr(data, len, ':'); if (j == len) { k = 0; for (i = 0; i < len; ++i) { if (data[i] == 0) k = 0; if (data[i] == '.') if (++k % 4 == 0) data[i] = 0; } } /* parse ip */ i = 0; for (j = 0; j < len; ++j) { if (data[j] == 0) { if (iplen + 16 <= ipmax) { if (op(ip + iplen, (char *)data + i)) { iplen += 16; } } i = j + 1; } } return iplen; } long long strtomultiip4(unsigned char *ip, long long ipmax, const char *x) { return _strtomultiip(ip, ipmax, x, strtoip4); } long long strtomultiip6(unsigned char *ip, long long ipmax, const char *x) { return _strtomultiip(ip, ipmax, x, strtoip6); } long long strtomultiip(unsigned char *ip, long long ipmax, const char *x) { return _strtomultiip(ip, ipmax, x, strtoip); } dq-20250201/strtomultiip.h000066400000000000000000000005001474734131400153060ustar00rootroot00000000000000#ifndef _STRTOMULTIIP_H____ #define _STRTOMULTIIP_H____ #define STRTOMULTIIP_BUFSIZE 800 extern long long strtomultiip(unsigned char *, long long, const char *); extern long long strtomultiip4(unsigned char *, long long, const char *); extern long long strtomultiip6(unsigned char *, long long, const char *); #endif dq-20250201/strtonum.c000066400000000000000000000021061474734131400144210ustar00rootroot00000000000000/* 20130605 Jan Mojzis Public domain. */ #include "e.h" #include "strtonum.h" /* The 'strnum(r,buf)' converts 0-terminated string 'buf' into number 'r'. */ int strtonum(long long *r, const char *buf) { char *bufpos = (char *)buf; int flagsign = 0; long long i; unsigned long long c, ret = 0; if (!buf) goto failed; switch (buf[0]) { case 0: goto failed; break; case '+': ++bufpos; break; case '-': flagsign = 1; ++bufpos; break; default: break; } for(i = 0; bufpos[i]; ++i) { c = bufpos[i] - '0'; if (c > 9) break; c += 10 * (ret); if (ret > c) goto failed; /* overflow */ ret = c; } if (i == 0) goto failed; /* "+..." or "-..." */ if (flagsign) { *r = -ret; if (*r > 0) goto failed; /* overflow */ } else { *r = ret; if (*r < 0) goto failed; /* overflow */ } return 1; failed: *r = 0; errno = EINVAL; return 0; } dq-20250201/strtonum.h000066400000000000000000000001511474734131400144240ustar00rootroot00000000000000#ifndef _STRTONUM_H____ #define _STRTONUM_H____ extern int strtonum(long long *, const char *); #endif dq-20250201/tryfeature.sh000077500000000000000000000014751474734131400151230ustar00rootroot00000000000000#!/bin/sh # version 20241107 scriptname="`basename $0`" if [ x"${CC}" = x ]; then echo "usage: env CC= ${scriptname} feature.c" echo '$CC not set' exit 1 fi if [ x"$1" = x ]; then echo "usage: env CC= ${scriptname} feature.c" echo 'missing feature.c argument' exit 1 fi # input tryname="$1" # output headermacro=`echo ${tryname} | sed 's/\.c//' | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'` # temporary binname="${tryname}.tmp.bin" logname="${tryname}.tmp.log" cleanup() { ex=$? rm -f "${binname}" "${logname}" exit "${ex}" } trap "cleanup" EXIT TERM INT "${CC}" ${CFLAGS} -O0 -o "${binname}" "${tryname}" ${LDFLAGS} 1>"${logname}" 2>&1 if [ $? -eq 0 ]; then echo "#define ${headermacro} 1" else echo "#undef ${headermacro}" cat "${logname}" >&2 fi exit 0 dq-20250201/trylibs.sh000077500000000000000000000017301474734131400144130ustar00rootroot00000000000000#!/bin/sh # version 20241109 scriptname="`basename $0`" if [ x"${CC}" = x ]; then echo "usage: env CC= ${scriptname} -l -l ..." echo '$CC not set' exit 1 fi if [ x"$1" = x ]; then echo "usage: env CC= ${scriptname} -l -l ..." echo 'missing -l argument' exit 1 fi # temporary name="${scriptname}" tryname="${name}.tmp.C" binname="${tryname}.tmp.bin" logname="${tryname}.tmp.log" cleanup() { ex=$? rm -f "${binname}" "${tryname}" "${logname}" exit "${ex}" } trap "cleanup" EXIT TERM INT cat < "${tryname}" int main(int argc, char **argv) { (void) argc; (void) argv; return 0; } EOF while true; do [ x"$1" = x ] && break "${CC}" -O0 -o "${binname}" "${tryname}" "$1" 1>"${logname}" 2>&1 if [ $? -eq 0 ]; then echo "${scriptname}: '$1' detected" >&2 echo "$1" else echo "${scriptname}: '$1' not detected" >&2 cat "${logname}" >&2 fi shift done exit 0 dq-20250201/typeparse.c000066400000000000000000000021101474734131400145350ustar00rootroot00000000000000#include "strtonum.h" #include "crypto_uint16.h" #include "case.h" #include "dns.h" #include "byte.h" #include "typeparse.h" int typeparse(unsigned char *type, const char *s) { long long l; if (!s) return 0; if (strtonum(&l, s) && l > 0 && l <= 65535) { crypto_uint16_store_bigendian(type, l); } else if (case_equals(s, "a")) byte_copy(type, 2, DNS_T_A); else if (case_equals(s, "ns")) byte_copy(type, 2, DNS_T_NS); else if (case_equals(s, "mx")) byte_copy(type, 2, DNS_T_MX); else if (case_equals(s, "any")) byte_copy(type, 2, DNS_T_ANY); else if (case_equals(s, "ptr")) byte_copy(type, 2, DNS_T_PTR); else if (case_equals(s, "txt")) byte_copy(type, 2, DNS_T_TXT); else if (case_equals(s, "soa")) byte_copy(type, 2, DNS_T_SOA); else if (case_equals(s, "srv")) byte_copy(type, 2, DNS_T_SRV); else if (case_equals(s, "aaaa")) byte_copy(type, 2, DNS_T_AAAA); else if (case_equals(s, "axfr")) byte_copy(type, 2, DNS_T_AXFR); else if (case_equals(s, "cname")) byte_copy(type, 2, DNS_T_CNAME); else return 0; return 1; } dq-20250201/typeparse.h000066400000000000000000000001601474734131400145450ustar00rootroot00000000000000#ifndef _TYPEPARSE_H____ #define _TYPEPARSE_H____ extern int typeparse(unsigned char *, const char *); #endif dq-20250201/uint16_optblocker.c000077700000000000000000000000001474734131400236452cryptoint/uint16_optblocker.custar00rootroot00000000000000dq-20250201/uint32_optblocker.c000077700000000000000000000000001474734131400236412cryptoint/uint32_optblocker.custar00rootroot00000000000000dq-20250201/uint64_optblocker.c000077700000000000000000000000001474734131400236532cryptoint/uint64_optblocker.custar00rootroot00000000000000dq-20250201/uint8_optblocker.c000077700000000000000000000000001474734131400235072cryptoint/uint8_optblocker.custar00rootroot00000000000000dq-20250201/verify.c000066400000000000000000000003371474734131400140360ustar00rootroot00000000000000#include "verify.h" int verify(const unsigned char *x, const unsigned char *y, long long n) { unsigned int d = 0; long long i; for (i = 0; i < n; ++i) d |= x[i] ^ y[i]; return (1 & ((d - 1) >> 8)) - 1; } dq-20250201/verify.h000066400000000000000000000002011474734131400140310ustar00rootroot00000000000000#ifndef _VERIFY_H____ #define _VERIFY_H____ extern int verify(const unsigned char *, const unsigned char *, long long); #endif dq-20250201/warn.c000066400000000000000000000012461474734131400135010ustar00rootroot00000000000000#include #include "writeall.h" #include "warn.h" void warn_9( const char *s0 ,const char *s1 ,const char *s2 ,const char *s3 ,const char *s4 ,const char *s5 ,const char *s6 ,const char *s7 ,const char *s8 ) { const char *s[9]; const char *x; char buf[256]; long long buflen = 0; long long i; s[0] = s0; s[1] = s1; s[2] = s2; s[3] = s3; s[4] = s4; s[5] = s5; s[6] = s6; s[7] = s7; s[8] = s8; for (i = 0;i < 9;++i) { x = s[i]; if (!x) continue; while (*x) { if (buflen == sizeof buf) { writeall(2,buf,buflen); buflen = 0; } buf[buflen++] = *x++; } } writeall(2,buf,buflen); return; } dq-20250201/warn.h000066400000000000000000000010341474734131400135010ustar00rootroot00000000000000#ifndef _WARN_H____ #define _WARN_H____ extern void warn_9(const char *,const char *,const char *,const char *,const char *,const char *,const char *,const char *,const char *); #define warn_8(a,b,c,d,e,f,g,h) warn_9(a,b,c,d,e,f,g,h,0) #define warn_7(a,b,c,d,e,f,g) warn_8(a,b,c,d,e,f,g,0) #define warn_6(a,b,c,d,e,f) warn_7(a,b,c,d,e,f,0) #define warn_5(a,b,c,d,e) warn_6(a,b,c,d,e,0) #define warn_4(a,b,c,d) warn_5(a,b,c,d,0) #define warn_3(a,b,c) warn_4(a,b,c,0) #define warn_2(a,b) warn_3(a,b,0) #define warn_1(a) warn_2(a,0) #endif dq-20250201/writeall.c000066400000000000000000000011531474734131400143520ustar00rootroot00000000000000/* taken from nacl-20110221, from curvecp/writeall.c (public domain) */ #include #include #include "e.h" #include "writeall.h" int writeall(int fd,const void *xv,long long xlen) { char *x = (char *)xv; long long w; while (xlen > 0) { w = xlen; if (w > 1048576) w = 1048576; w = write(fd,x,w); if (w < 0) { if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) { struct pollfd p; p.fd = fd; p.events = POLLOUT | POLLERR; poll(&p,1,-1); continue; } return -1; } x += w; xlen -= w; } return 0; } dq-20250201/writeall.h000066400000000000000000000001401474734131400143520ustar00rootroot00000000000000#ifndef WRITEALL_H #define WRITEALL_H extern int writeall(int,const void *,long long); #endif dq-20250201/xsocket.h000066400000000000000000000027701474734131400142220ustar00rootroot00000000000000#ifndef _XSOCKET_H____ #define _XSOCKET_H____ #define HASIPV6 1 #define XSOCKET_V4 4 #define XSOCKET_V6 6 extern int xsocket_tcp(int); extern int xsocket_udp(int); extern long long xsocket_send(int, int, const unsigned char *, long long, const unsigned char *, const unsigned char *, long long); extern long long xsocket_recv(int, int, unsigned char *, long long, unsigned char *, unsigned char *, long long *); extern int xsocket_bind(int, int, const unsigned char *, const unsigned char *, long long); extern int xsocket_bind_reuse(int, int, const unsigned char *, const unsigned char *, long long); extern void xsocket_tryreservein(int, int); extern int xsocket_listen(int, long long); extern int xsocket_accept(int, int, unsigned char *, unsigned char *, long long *); extern int xsocket_local(int, int, unsigned char *, unsigned char *, long long *); extern int xsocket_connect(int, int, const unsigned char *, const unsigned char *, long long); extern int xsocket_connected(int); extern int xsocket_ipoptionskill(int); extern int xsocket_tcpnodelay(int); extern long long xsocket_getscopeid(const char *); extern int xsocket_type(const unsigned char *); #define xsocket_ANYIP6 (const unsigned char *)"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" #define xsocket_ANYIP4 (const unsigned char *)"\0\0\0\0\0\0\0\0\0\0\377\377\0\0\0\0" #define xsocket_PREFIX4 (const unsigned char *)"\0\0\0\0\0\0\0\0\0\0\377\377" #ifdef HASIPV6 #define xsocket_ANYIP xsocket_ANYIP6 #else #define xsocket_ANYIP xsocket_ANYIP4 #endif #endif dq-20250201/xsocket_accept.c000066400000000000000000000032341474734131400155300ustar00rootroot00000000000000/* 20131206 Jan Mojzis Public domain. */ #include #include #include #include #include #include "e.h" #include "byte.h" #include "xsocket.h" static int xsocket_accept6(int s, unsigned char *ip, unsigned char *port, long long *id) { #ifdef HASIPV6 struct sockaddr_in6 sa; socklen_t salen = sizeof sa; int fd; byte_zero(&sa, sizeof sa); fd = accept(s, (struct sockaddr *)&sa, &salen); if (fd == -1) return -1; if (((struct sockaddr *)&sa)->sa_family != PF_INET6) { close(fd); errno = EPROTO; return -1; } if (ip) byte_copy(ip, 16, &sa.sin6_addr); if (port) byte_copy(port, 2, &sa.sin6_port); if (id) *id = sa.sin6_scope_id; return fd; #else errno = EPROTONOSUPPORT; return -1; #endif } static int xsocket_accept4(int s, unsigned char *ip, unsigned char *port, long long *id) { struct sockaddr_in sa; socklen_t salen = sizeof sa; int fd; byte_zero(&sa, sizeof sa); fd = accept(s, (struct sockaddr *)&sa, &salen); if (fd == -1) return -1; if (((struct sockaddr *)&sa)->sa_family != PF_INET) { close(fd); errno = EPROTO; return -1; } if (ip) byte_copy(ip, 12, "\0\0\0\0\0\0\0\0\0\0\377\377"); if (ip) byte_copy(ip + 12, 4, &sa.sin_addr); if (port) byte_copy(port, 2, &sa.sin_port); if (id) *id = 0; return fd; } int xsocket_accept(int fd, int type, unsigned char *ip, unsigned char *port, long long *id) { if (type == XSOCKET_V4) { return xsocket_accept4(fd, ip, port, id); } if (type == XSOCKET_V6) { return xsocket_accept6(fd, ip, port, id); } errno = EPROTO; return -1; } dq-20250201/xsocket_bind.c000066400000000000000000000035351474734131400152110ustar00rootroot00000000000000/* 20131117 Jan Mojzis Public domain. */ #include #include #include #include #include "e.h" #include "byte.h" #include "xsocket.h" static int xsocket_bind6(int fd, const unsigned char *ip, const unsigned char *port, long long id) { #ifdef HASIPV6 struct sockaddr_in6 sa; if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { errno = EPROTO; return -1; } byte_zero(&sa, sizeof sa); sa.sin6_family = PF_INET6; byte_copy(&sa.sin6_addr, 16, ip); byte_copy(&sa.sin6_port, 2, port); sa.sin6_scope_id = id; return bind(fd, (struct sockaddr *)&sa, sizeof sa); #else errno = EPROTONOSUPPORT; return -1; #endif } static int xsocket_bind4(int fd, const unsigned char *ip, const unsigned char *port, long long id) { struct sockaddr_in sa; if (!byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { errno = EPROTO; return -1; } byte_zero(&sa, sizeof sa); sa.sin_family = PF_INET; byte_copy(&sa.sin_addr, 4, ip + 12); byte_copy(&sa.sin_port, 2, port); return bind(fd, (struct sockaddr *)&sa, sizeof sa); } int xsocket_bind(int fd, int type, const unsigned char *ip, const unsigned char *port, long long id) { if (type == XSOCKET_V4) { return xsocket_bind4(fd, ip, port, id); } if (type == XSOCKET_V6) { return xsocket_bind6(fd, ip, port, id); } errno = EPROTO; return -1; } int xsocket_bind_reuse(int fd, int type, const unsigned char *ip, const unsigned char *port, long long id) { int opt = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt); return xsocket_bind(fd, type, ip, port, id); } void xsocket_tryreservein(int fd, int size) { while (size >= 1024) { if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof size) == 0) return; size -= (size >> 5); } } dq-20250201/xsocket_conn.c000066400000000000000000000033061474734131400152260ustar00rootroot00000000000000/* 20130505 Jan Mojzis Public domain. */ #include #include #include #include #include #include "e.h" #include "byte.h" #include "xsocket.h" static int xsocket_connect6(int s, const unsigned char *ip, const unsigned char *port, long long id) { #ifdef HASIPV6 struct sockaddr_in6 sa; if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { errno = EPROTO; return -1; } byte_zero(&sa, sizeof sa); sa.sin6_family = PF_INET6; byte_copy(&sa.sin6_addr, 16, ip); byte_copy(&sa.sin6_port, 2, port); sa.sin6_scope_id = id; return connect(s, (struct sockaddr *)&sa, sizeof sa); #else errno = EPROTONOSUPPORT; return -1; #endif } static int xsocket_connect4(int s, const unsigned char *ip, const unsigned char *port, long long id) { struct sockaddr_in sa; if (!byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { errno = EPROTO; return -1; } byte_zero(&sa, sizeof sa); sa.sin_family = PF_INET; byte_copy(&sa.sin_addr, 4, ip + 12); byte_copy(&sa.sin_port, 2, port); return connect(s, (struct sockaddr *)&sa, sizeof sa); } int xsocket_connect(int s, int type, const unsigned char *ip, const unsigned char *port, long long id) { if (type == XSOCKET_V4) { return xsocket_connect4(s, ip, port, id); } if (type == XSOCKET_V6) { return xsocket_connect6(s, ip, port, id); } errno = EPROTO; return -1; } int xsocket_connected(int s) { struct sockaddr sa; socklen_t dummy; char ch; dummy = sizeof sa; if (getpeername(s, &sa, &dummy) == -1) { if (read(s, &ch, 1) == -1) {}; /* sets errno */ return 0; } return 1; } dq-20250201/xsocket_listen.c000066400000000000000000000004361474734131400155700ustar00rootroot00000000000000#include #include #include #include #include "e.h" #include "xsocket.h" int xsocket_listen(int fd, long long backlog) { if (backlog < 0 || backlog > 1048576) { errno = EPROTO; return -1; } return listen(fd, backlog); } dq-20250201/xsocket_recv.c000066400000000000000000000035561474734131400152370ustar00rootroot00000000000000/* 20131117 Jan Mojzis Public domain. */ #include #include #include #include #include "e.h" #include "byte.h" #include "xsocket.h" static long long xsocket_recv6(int fd, unsigned char *x, long long xlen, unsigned char *ip, unsigned char *port, long long *id) { #ifdef HASIPV6 struct sockaddr_in6 sa; socklen_t salen = sizeof sa; long long r; byte_zero(&sa, sizeof sa); r = recvfrom(fd, x, xlen, 0, (struct sockaddr *) &sa, &salen); if (r == -1) return -1; if (((struct sockaddr *)&sa)->sa_family != PF_INET6) { errno = EPROTO; return -1; } if (ip) byte_copy(ip, 16, &sa.sin6_addr); if (port) byte_copy(port, 2, &sa.sin6_port); if (id) *id = sa.sin6_scope_id; return r; #else errno = EPROTONOSUPPORT; return -1; #endif } static long long xsocket_recv4(int fd, unsigned char *x, long long xlen, unsigned char *ip, unsigned char *port, long long *id) { struct sockaddr_in sa; socklen_t salen = sizeof sa; long long r; byte_zero(&sa, sizeof sa); r = recvfrom(fd, x, xlen, 0, (struct sockaddr *) &sa, &salen); if (r == -1) return -1; if (((struct sockaddr *)&sa)->sa_family != PF_INET) { errno = EPROTO; return -1; } if (ip) byte_copy(ip, 12, "\0\0\0\0\0\0\0\0\0\0\377\377"); if (ip) byte_copy(ip + 12, 4, &sa.sin_addr); if (port) byte_copy(port, 2, &sa.sin_port); if (id) *id = 0; return r; } long long xsocket_recv(int fd, int type, unsigned char *x, long long xlen, unsigned char *ip, unsigned char *port, long long *id) { if (xlen < 0) { errno = EPROTO; return -1; } if (xlen > 1048576) xlen = 1048576; if (type == XSOCKET_V4) { return xsocket_recv4(fd, x, xlen, ip, port, id); } if (type == XSOCKET_V6) { return xsocket_recv6(fd, x, xlen, ip, port, id); } errno = EPROTO; return -1; } dq-20250201/xsocket_send.c000066400000000000000000000032431474734131400152220ustar00rootroot00000000000000/* 20131117 Jan Mojzis Public domain. */ #include #include #include #include #include "e.h" #include "byte.h" #include "xsocket.h" static long long xsocket_send6(int fd, const unsigned char *x, long long xlen, const unsigned char *ip, const unsigned char *port, long long id) { #ifdef HASIPV6 struct sockaddr_in6 sa; if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { errno = EPROTO; return -1; } byte_zero(&sa, sizeof sa); sa.sin6_family = PF_INET6; byte_copy(&sa.sin6_addr, 16, ip); byte_copy(&sa.sin6_port, 2, port); sa.sin6_scope_id = id; return sendto(fd, x, xlen, 0, (struct sockaddr *)&sa, sizeof sa); #else errno = EPROTONOSUPPORT; return -1; #endif } static long long xsocket_send4(int fd, const unsigned char *x, long long xlen, const unsigned char *ip, const unsigned char *port, long long id) { struct sockaddr_in sa; if (!byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { errno = EPROTO; return -1; } byte_zero(&sa, sizeof sa); sa.sin_family = PF_INET; byte_copy(&sa.sin_addr, 4, ip + 12); byte_copy(&sa.sin_port, 2, port); return sendto(fd, x, xlen, 0, (struct sockaddr *)&sa, sizeof sa); } long long xsocket_send(int fd, int type, const unsigned char *x, long long xlen, const unsigned char *ip, const unsigned char *port, long long id) { if (xlen < 0 || xlen > 1048576) { errno = EPROTO; return -1; } if (type == XSOCKET_V4) { return xsocket_send4(fd, x, xlen, ip, port, id); } if (type == XSOCKET_V6) { return xsocket_send6(fd, x, xlen, ip, port, id); } errno = EPROTO; return -1; } dq-20250201/xsocket_tcp.c000066400000000000000000000017251474734131400150620ustar00rootroot00000000000000/* 20131117 Jan Mojzis Public domain. */ #include #include #include #include #include #include #include "blocking.h" #include "e.h" #include "xsocket.h" int xsocket_tcp(int type) { int s; #ifdef HASIPV6 int opt = 1; #endif if (type == XSOCKET_V6) { #ifdef HASIPV6 s = socket(PF_INET6, SOCK_STREAM, 0); if (s == -1) return -1; if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; } if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof opt) == -1) { close(s); return -1; } blocking_disable(s); return s; #endif errno = EPROTONOSUPPORT; return -1; } if (type == XSOCKET_V4) { s = socket(PF_INET, SOCK_STREAM, 0); if (s == -1) return -1; if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; } blocking_disable(s); return s; } errno = EPROTO; return -1; } dq-20250201/xsocket_type.c000066400000000000000000000003451474734131400152520ustar00rootroot00000000000000#include "byte.h" #include "xsocket.h" int xsocket_type(const unsigned char *ip) { if (byte_isequal("\0\0\0\0\0\0\0\0\0\0\377\377", 12, ip)) { return XSOCKET_V4; } else { return XSOCKET_V6; } } dq-20250201/xsocket_udp.c000066400000000000000000000017231474734131400150620ustar00rootroot00000000000000/* 20131117 Jan Mojzis Public domain. */ #include #include #include #include #include #include #include "blocking.h" #include "e.h" #include "xsocket.h" int xsocket_udp(int type) { int s; #ifdef HASIPV6 int opt = 1; #endif if (type == XSOCKET_V6) { #ifdef HASIPV6 s = socket(PF_INET6, SOCK_DGRAM, 0); if (s == -1) return -1; if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; } if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof opt) == -1) { close(s); return -1; } blocking_disable(s); return s; #endif errno = EPROTONOSUPPORT; return -1; } if (type == XSOCKET_V4) { s = socket(PF_INET, SOCK_DGRAM, 0); if (s == -1) return -1; if (fcntl(s, F_SETFD, 1) == -1) { close(s); return -1; } blocking_disable(s); return s; } errno = EPROTO; return -1; }