cjson
.github
workflows CI.yml ci-fuzz.yml
CONTRIBUTING.md
fuzzing
inputs test1 test10 test11 test2 test3 test3.bu test3.uf test3.uu test4 test5 test6 test7 test8 test9
.gitignore CMakeLists.txt afl-prepare-linux.sh afl.c afl.sh cjson_read_fuzzer.c fuzz_main.c json.dict ossfuzz.sh
library_config cJSONConfig.cmake.in cJSONConfigVersion.cmake.in libcjson.pc.in libcjson_utils.pc.in uninstall.cmake
tests
inputs test1 test1.expected test10 test10.expected test11 test11.expected test2 test2.expected test3 test3.expected test4 test4.expected test5 test5.expected test6 test7 test7.expected test8 test8.expected test9 test9.expected
json-patch-tests .editorconfig .gitignore .npmignore README.md cjson-utils-tests.json package.json spec_tests.json tests.json
unity
auto colour_prompt.rb colour_reporter.rb generate_config.yml generate_module.rb generate_test_runner.rb parse_output.rb stylize_as_junit.rb test_file_filter.rb type_sanitizer.rb unity_test_summary.py unity_test_summary.rb unity_to_junit.py
docs ThrowTheSwitchCodingStandard.md UnityAssertionsCheatSheetSuitableforPrintingandPossiblyFraming.pdf UnityAssertionsReference.md UnityConfigurationGuide.md UnityGettingStartedGuide.md UnityHelperScriptsGuide.md license.txt
examples
example_1
src ProductionCode.c ProductionCode.h ProductionCode2.c ProductionCode2.h
makefile readme.txt
example_2
src ProductionCode.c ProductionCode.h ProductionCode2.c ProductionCode2.h
makefile readme.txt
example_3
helper UnityHelper.c UnityHelper.h
src ProductionCode.c ProductionCode.h ProductionCode2.c ProductionCode2.h
rakefile.rb rakefile_helper.rb readme.txt target_gcc_32.yml
unity_config.h
extras
eclipse error_parsers.txt
fixture
src unity_fixture.c unity_fixture.h unity_fixture_internals.h unity_fixture_malloc_overrides.h
rakefile.rb rakefile_helper.rb readme.txt
release build.info version.info
src unity.c unity.h unity_internals.h
.gitattributes .gitignore .travis.yml README.md
CMakeLists.txt cjson_add.c common.h compare_tests.c json_patch_tests.c minify_tests.c misc_tests.c misc_utils_tests.c old_utils_tests.c parse_array.c parse_examples.c parse_hex4.c parse_number.c parse_object.c parse_string.c parse_value.c parse_with_opts.c print_array.c print_number.c print_object.c print_string.c print_value.c readme_examples.c unity_setup.c
.editorconfig .gitattributes .gitignore .travis.yml CHANGELOG.md CMakeLists.txt CONTRIBUTORS.md LICENSE Makefile README.md SECURITY.md appveyor.yml cJSON.c cJSON.h cJSON_Utils.c cJSON_Utils.h test.c valgrind.supp
curl
.circleci config.yml
.github
ISSUE_TEMPLATE bug_report.yml config.yml docs.yml
scripts cleancmd.pl cmp-config.pl cmp-pkg-config.sh codespell-ignore.words codespell.sh distfiles.sh pyspelling.words pyspelling.yaml randcurl.pl requirements-docs.txt requirements-proselint.txt requirements.txt shellcheck-ci.sh shellcheck.sh spellcheck.curl trimmarkdownheader.pl typos.sh typos.toml verify-examples.pl verify-synopsis.pl yamlcheck.sh yamlcheck.yaml
workflows appveyor-status.yml checkdocs.yml checksrc.yml checkurls.yml codeql.yml configure-vs-cmake.yml curl-for-win.yml distcheck.yml fuzz.yml http3-linux.yml label.yml linux-old.yml linux.yml macos.yml non-native.yml windows.yml
CODEOWNERS CONTRIBUTING.md FUNDING.yml dependabot.yml labeler.yml lock.yml stale.yml
CMake CurlSymbolHiding.cmake CurlTests.c FindBrotli.cmake FindCares.cmake FindGSS.cmake FindGnuTLS.cmake FindLDAP.cmake FindLibbacktrace.cmake FindLibgsasl.cmake FindLibidn2.cmake FindLibpsl.cmake FindLibssh.cmake FindLibssh2.cmake FindLibuv.cmake FindMbedTLS.cmake FindNGHTTP2.cmake FindNGHTTP3.cmake FindNGTCP2.cmake FindNettle.cmake FindQuiche.cmake FindRustls.cmake FindWolfSSL.cmake FindZstd.cmake Macros.cmake OtherTests.cmake PickyWarnings.cmake Utilities.cmake cmake_uninstall.in.cmake curl-config.in.cmake unix-cache.cmake win32-cache.cmake
LICENSES BSD-4-Clause-UC.txt ISC.txt curl.txt
docs
cmdline-opts .gitignore CMakeLists.txt MANPAGE.md Makefile.am Makefile.inc _AUTHORS.md _BUGS.md _DESCRIPTION.md _ENVIRONMENT.md _EXITCODES.md _FILES.md _GLOBBING.md _NAME.md _OPTIONS.md _OUTPUT.md _PROGRESS.md _PROTOCOLS.md _PROXYPREFIX.md _SEEALSO.md _SYNOPSIS.md _URL.md _VARIABLES.md _VERSION.md _WWW.md abstract-unix-socket.md alt-svc.md anyauth.md append.md aws-sigv4.md basic.md ca-native.md cacert.md capath.md cert-status.md cert-type.md cert.md ciphers.md compressed-ssh.md compressed.md config.md connect-timeout.md connect-to.md continue-at.md cookie-jar.md cookie.md create-dirs.md create-file-mode.md crlf.md crlfile.md curves.md data-ascii.md data-binary.md data-raw.md data-urlencode.md data.md delegation.md digest.md disable-eprt.md disable-epsv.md disable.md disallow-username-in-url.md dns-interface.md dns-ipv4-addr.md dns-ipv6-addr.md dns-servers.md doh-cert-status.md doh-insecure.md doh-url.md dump-ca-embed.md dump-header.md ech.md egd-file.md engine.md etag-compare.md etag-save.md expect100-timeout.md fail-early.md fail-with-body.md fail.md false-start.md follow.md form-escape.md form-string.md form.md ftp-account.md ftp-alternative-to-user.md ftp-create-dirs.md ftp-method.md ftp-pasv.md ftp-port.md ftp-pret.md ftp-skip-pasv-ip.md ftp-ssl-ccc-mode.md ftp-ssl-ccc.md ftp-ssl-control.md get.md globoff.md happy-eyeballs-timeout-ms.md haproxy-clientip.md haproxy-protocol.md head.md header.md help.md hostpubmd5.md hostpubsha256.md hsts.md http0.9.md http1.0.md http1.1.md http2-prior-knowledge.md http2.md http3-only.md http3.md ignore-content-length.md insecure.md interface.md ip-tos.md ipfs-gateway.md ipv4.md ipv6.md json.md junk-session-cookies.md keepalive-cnt.md keepalive-time.md key-type.md key.md knownhosts.md krb.md libcurl.md limit-rate.md list-only.md local-port.md location-trusted.md location.md login-options.md mail-auth.md mail-from.md mail-rcpt-allowfails.md mail-rcpt.md mainpage.idx manual.md max-filesize.md max-redirs.md max-time.md metalink.md mptcp.md negotiate.md netrc-file.md netrc-optional.md netrc.md next.md no-alpn.md no-buffer.md no-clobber.md no-keepalive.md no-npn.md no-progress-meter.md no-sessionid.md noproxy.md ntlm-wb.md ntlm.md oauth2-bearer.md out-null.md output-dir.md output.md parallel-immediate.md parallel-max-host.md parallel-max.md parallel.md pass.md path-as-is.md pinnedpubkey.md post301.md post302.md post303.md preproxy.md progress-bar.md proto-default.md proto-redir.md proto.md proxy-anyauth.md proxy-basic.md proxy-ca-native.md proxy-cacert.md proxy-capath.md proxy-cert-type.md proxy-cert.md proxy-ciphers.md proxy-crlfile.md proxy-digest.md proxy-header.md proxy-http2.md proxy-insecure.md proxy-key-type.md proxy-key.md proxy-negotiate.md proxy-ntlm.md proxy-pass.md proxy-pinnedpubkey.md proxy-service-name.md proxy-ssl-allow-beast.md proxy-ssl-auto-client-cert.md proxy-tls13-ciphers.md proxy-tlsauthtype.md proxy-tlspassword.md proxy-tlsuser.md proxy-tlsv1.md proxy-user.md proxy.md proxy1.0.md proxytunnel.md pubkey.md quote.md random-file.md range.md rate.md raw.md referer.md remote-header-name.md remote-name-all.md remote-name.md remote-time.md remove-on-error.md request-target.md request.md resolve.md retry-all-errors.md retry-connrefused.md retry-delay.md retry-max-time.md retry.md sasl-authzid.md sasl-ir.md service-name.md show-error.md show-headers.md sigalgs.md silent.md skip-existing.md socks4.md socks4a.md socks5-basic.md socks5-gssapi-nec.md socks5-gssapi-service.md socks5-gssapi.md socks5-hostname.md socks5.md speed-limit.md speed-time.md ssl-allow-beast.md ssl-auto-client-cert.md ssl-no-revoke.md ssl-reqd.md ssl-revoke-best-effort.md ssl-sessions.md ssl.md sslv2.md sslv3.md stderr.md styled-output.md suppress-connect-headers.md tcp-fastopen.md tcp-nodelay.md telnet-option.md tftp-blksize.md tftp-no-options.md time-cond.md tls-earlydata.md tls-max.md tls13-ciphers.md tlsauthtype.md tlspassword.md tlsuser.md tlsv1.0.md tlsv1.1.md tlsv1.2.md tlsv1.3.md tlsv1.md tr-encoding.md trace-ascii.md trace-config.md trace-ids.md trace-time.md trace.md unix-socket.md upload-file.md upload-flags.md url-query.md url.md use-ascii.md user-agent.md user.md variable.md verbose.md version.md vlan-priority.md write-out.md xattr.md
examples .checksrc .gitignore 10-at-a-time.c CMakeLists.txt Makefile.am Makefile.example Makefile.inc README.md adddocsref.pl address-scope.c altsvc.c anyauthput.c block_ip.c cacertinmem.c certinfo.c chkspeed.c connect-to.c cookie_interface.c crawler.c debug.c default-scheme.c ephiperfifo.c evhiperfifo.c externalsocket.c fileupload.c ftp-delete.c ftp-wildcard.c ftpget.c ftpgetinfo.c ftpgetresp.c ftpsget.c ftpupload.c ftpuploadfrommem.c ftpuploadresume.c getinfo.c getinmemory.c getredirect.c getreferrer.c ghiper.c headerapi.c hiperfifo.c hsts-preload.c htmltidy.c htmltitle.cpp http-options.c http-post.c http2-download.c http2-pushinmemory.c http2-serverpush.c http2-upload.c http3-present.c http3.c httpcustomheader.c httpput-postfields.c httpput.c https.c imap-append.c imap-authzid.c imap-copy.c imap-create.c imap-delete.c imap-examine.c imap-fetch.c imap-list.c imap-lsub.c imap-multi.c imap-noop.c imap-search.c imap-ssl.c imap-store.c imap-tls.c interface.c ipv6.c keepalive.c localport.c log_failed_transfers.c maxconnects.c multi-app.c multi-debugcallback.c multi-double.c multi-event.c multi-formadd.c multi-legacy.c multi-post.c multi-single.c multi-uv.c netrc.c parseurl.c persistent.c pop3-authzid.c pop3-dele.c pop3-list.c pop3-multi.c pop3-noop.c pop3-retr.c pop3-ssl.c pop3-stat.c pop3-tls.c pop3-top.c pop3-uidl.c post-callback.c postinmemory.c postit2-formadd.c postit2.c progressfunc.c protofeats.c range.c resolve.c rtsp-options.c sendrecv.c sepheaders.c sessioninfo.c sftpget.c sftpuploadresume.c shared-connection-cache.c simple.c simplepost.c simplessl.c smooth-gtk-thread.c smtp-authzid.c smtp-expn.c smtp-mail.c smtp-mime.c smtp-multi.c smtp-ssl.c smtp-tls.c smtp-vrfy.c sslbackend.c synctime.c threaded.c unixsocket.c url2file.c urlapi.c usercertinmem.c version-check.pl websocket-cb.c websocket-updown.c websocket.c xmlstream.c
internals BUFQ.md BUFREF.md CHECKSRC.md CLIENT-READERS.md CLIENT-WRITERS.md CODE_STYLE.md CONNECTION-FILTERS.md CREDENTIALS.md CURLX.md DYNBUF.md HASH.md LLIST.md MID.md MQTT.md MULTI-EV.md NEW-PROTOCOL.md PEERS.md PORTING.md RATELIMITS.md README.md SCORECARD.md SPLAY.md STRPARSE.md THRDPOOL-AND-QUEUE.md TIME-KEEPING.md TLS-SESSIONS.md UINT_SETS.md WEBSOCKET.md
libcurl
opts CMakeLists.txt CURLINFO_ACTIVESOCKET.md CURLINFO_APPCONNECT_TIME.md CURLINFO_APPCONNECT_TIME_T.md CURLINFO_CAINFO.md CURLINFO_CAPATH.md CURLINFO_CERTINFO.md CURLINFO_CONDITION_UNMET.md CURLINFO_CONNECT_TIME.md CURLINFO_CONNECT_TIME_T.md CURLINFO_CONN_ID.md CURLINFO_CONTENT_LENGTH_DOWNLOAD.md CURLINFO_CONTENT_LENGTH_DOWNLOAD_T.md CURLINFO_CONTENT_LENGTH_UPLOAD.md CURLINFO_CONTENT_LENGTH_UPLOAD_T.md CURLINFO_CONTENT_TYPE.md CURLINFO_COOKIELIST.md CURLINFO_EARLYDATA_SENT_T.md CURLINFO_EFFECTIVE_METHOD.md CURLINFO_EFFECTIVE_URL.md CURLINFO_FILETIME.md CURLINFO_FILETIME_T.md CURLINFO_FTP_ENTRY_PATH.md CURLINFO_HEADER_SIZE.md CURLINFO_HTTPAUTH_AVAIL.md CURLINFO_HTTPAUTH_USED.md CURLINFO_HTTP_CONNECTCODE.md CURLINFO_HTTP_VERSION.md CURLINFO_LASTSOCKET.md CURLINFO_LOCAL_IP.md CURLINFO_LOCAL_PORT.md CURLINFO_NAMELOOKUP_TIME.md CURLINFO_NAMELOOKUP_TIME_T.md CURLINFO_NUM_CONNECTS.md CURLINFO_OS_ERRNO.md CURLINFO_POSTTRANSFER_TIME_T.md CURLINFO_PRETRANSFER_TIME.md CURLINFO_PRETRANSFER_TIME_T.md CURLINFO_PRIMARY_IP.md CURLINFO_PRIMARY_PORT.md CURLINFO_PRIVATE.md CURLINFO_PROTOCOL.md CURLINFO_PROXYAUTH_AVAIL.md CURLINFO_PROXYAUTH_USED.md CURLINFO_PROXY_ERROR.md CURLINFO_PROXY_SSL_VERIFYRESULT.md CURLINFO_QUEUE_TIME_T.md CURLINFO_REDIRECT_COUNT.md CURLINFO_REDIRECT_TIME.md CURLINFO_REDIRECT_TIME_T.md CURLINFO_REDIRECT_URL.md CURLINFO_REFERER.md CURLINFO_REQUEST_SIZE.md CURLINFO_RESPONSE_CODE.md CURLINFO_RETRY_AFTER.md CURLINFO_RTSP_CLIENT_CSEQ.md CURLINFO_RTSP_CSEQ_RECV.md CURLINFO_RTSP_SERVER_CSEQ.md CURLINFO_RTSP_SESSION_ID.md CURLINFO_SCHEME.md CURLINFO_SIZE_DELIVERED.md CURLINFO_SIZE_DOWNLOAD.md CURLINFO_SIZE_DOWNLOAD_T.md CURLINFO_SIZE_UPLOAD.md CURLINFO_SIZE_UPLOAD_T.md CURLINFO_SPEED_DOWNLOAD.md CURLINFO_SPEED_DOWNLOAD_T.md CURLINFO_SPEED_UPLOAD.md CURLINFO_SPEED_UPLOAD_T.md CURLINFO_SSL_ENGINES.md CURLINFO_SSL_VERIFYRESULT.md CURLINFO_STARTTRANSFER_TIME.md CURLINFO_STARTTRANSFER_TIME_T.md CURLINFO_TLS_SESSION.md CURLINFO_TLS_SSL_PTR.md CURLINFO_TOTAL_TIME.md CURLINFO_TOTAL_TIME_T.md CURLINFO_USED_PROXY.md CURLINFO_XFER_ID.md CURLMINFO_XFERS_ADDED.md CURLMINFO_XFERS_CURRENT.md CURLMINFO_XFERS_DONE.md CURLMINFO_XFERS_PENDING.md CURLMINFO_XFERS_RUNNING.md CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE.md CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE.md CURLMOPT_MAXCONNECTS.md CURLMOPT_MAX_CONCURRENT_STREAMS.md CURLMOPT_MAX_HOST_CONNECTIONS.md CURLMOPT_MAX_PIPELINE_LENGTH.md CURLMOPT_MAX_TOTAL_CONNECTIONS.md CURLMOPT_NETWORK_CHANGED.md CURLMOPT_NOTIFYDATA.md CURLMOPT_NOTIFYFUNCTION.md CURLMOPT_PIPELINING.md CURLMOPT_PIPELINING_SERVER_BL.md CURLMOPT_PIPELINING_SITE_BL.md CURLMOPT_PUSHDATA.md CURLMOPT_PUSHFUNCTION.md CURLMOPT_QUICK_EXIT.md CURLMOPT_RESOLVE_THREADS_MAX.md CURLMOPT_SOCKETDATA.md CURLMOPT_SOCKETFUNCTION.md CURLMOPT_TIMERDATA.md CURLMOPT_TIMERFUNCTION.md CURLOPT_ABSTRACT_UNIX_SOCKET.md CURLOPT_ACCEPTTIMEOUT_MS.md CURLOPT_ACCEPT_ENCODING.md CURLOPT_ADDRESS_SCOPE.md CURLOPT_ALTSVC.md CURLOPT_ALTSVC_CTRL.md CURLOPT_APPEND.md CURLOPT_AUTOREFERER.md CURLOPT_AWS_SIGV4.md CURLOPT_BUFFERSIZE.md CURLOPT_CAINFO.md CURLOPT_CAINFO_BLOB.md CURLOPT_CAPATH.md CURLOPT_CA_CACHE_TIMEOUT.md CURLOPT_CERTINFO.md CURLOPT_CHUNK_BGN_FUNCTION.md CURLOPT_CHUNK_DATA.md CURLOPT_CHUNK_END_FUNCTION.md CURLOPT_CLOSESOCKETDATA.md CURLOPT_CLOSESOCKETFUNCTION.md CURLOPT_CONNECTTIMEOUT.md CURLOPT_CONNECTTIMEOUT_MS.md CURLOPT_CONNECT_ONLY.md CURLOPT_CONNECT_TO.md CURLOPT_CONV_FROM_NETWORK_FUNCTION.md CURLOPT_CONV_FROM_UTF8_FUNCTION.md CURLOPT_CONV_TO_NETWORK_FUNCTION.md CURLOPT_COOKIE.md CURLOPT_COOKIEFILE.md CURLOPT_COOKIEJAR.md CURLOPT_COOKIELIST.md CURLOPT_COOKIESESSION.md CURLOPT_COPYPOSTFIELDS.md CURLOPT_CRLF.md CURLOPT_CRLFILE.md CURLOPT_CURLU.md CURLOPT_CUSTOMREQUEST.md CURLOPT_DEBUGDATA.md CURLOPT_DEBUGFUNCTION.md CURLOPT_DEFAULT_PROTOCOL.md CURLOPT_DIRLISTONLY.md CURLOPT_DISALLOW_USERNAME_IN_URL.md CURLOPT_DNS_CACHE_TIMEOUT.md CURLOPT_DNS_INTERFACE.md CURLOPT_DNS_LOCAL_IP4.md CURLOPT_DNS_LOCAL_IP6.md CURLOPT_DNS_SERVERS.md CURLOPT_DNS_SHUFFLE_ADDRESSES.md CURLOPT_DNS_USE_GLOBAL_CACHE.md CURLOPT_DOH_SSL_VERIFYHOST.md CURLOPT_DOH_SSL_VERIFYPEER.md CURLOPT_DOH_SSL_VERIFYSTATUS.md CURLOPT_DOH_URL.md CURLOPT_ECH.md CURLOPT_EGDSOCKET.md CURLOPT_ERRORBUFFER.md CURLOPT_EXPECT_100_TIMEOUT_MS.md CURLOPT_FAILONERROR.md CURLOPT_FILETIME.md CURLOPT_FNMATCH_DATA.md CURLOPT_FNMATCH_FUNCTION.md CURLOPT_FOLLOWLOCATION.md CURLOPT_FORBID_REUSE.md CURLOPT_FRESH_CONNECT.md CURLOPT_FTPPORT.md CURLOPT_FTPSSLAUTH.md CURLOPT_FTP_ACCOUNT.md CURLOPT_FTP_ALTERNATIVE_TO_USER.md CURLOPT_FTP_CREATE_MISSING_DIRS.md CURLOPT_FTP_FILEMETHOD.md CURLOPT_FTP_SKIP_PASV_IP.md CURLOPT_FTP_SSL_CCC.md CURLOPT_FTP_USE_EPRT.md CURLOPT_FTP_USE_EPSV.md CURLOPT_FTP_USE_PRET.md CURLOPT_GSSAPI_DELEGATION.md CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS.md CURLOPT_HAPROXYPROTOCOL.md CURLOPT_HAPROXY_CLIENT_IP.md CURLOPT_HEADER.md CURLOPT_HEADERDATA.md CURLOPT_HEADERFUNCTION.md CURLOPT_HEADEROPT.md CURLOPT_HSTS.md CURLOPT_HSTSREADDATA.md CURLOPT_HSTSREADFUNCTION.md CURLOPT_HSTSWRITEDATA.md CURLOPT_HSTSWRITEFUNCTION.md CURLOPT_HSTS_CTRL.md CURLOPT_HTTP09_ALLOWED.md CURLOPT_HTTP200ALIASES.md CURLOPT_HTTPAUTH.md CURLOPT_HTTPGET.md CURLOPT_HTTPHEADER.md CURLOPT_HTTPPOST.md CURLOPT_HTTPPROXYTUNNEL.md CURLOPT_HTTP_CONTENT_DECODING.md CURLOPT_HTTP_TRANSFER_DECODING.md CURLOPT_HTTP_VERSION.md CURLOPT_IGNORE_CONTENT_LENGTH.md CURLOPT_INFILESIZE.md CURLOPT_INFILESIZE_LARGE.md CURLOPT_INTERFACE.md CURLOPT_INTERLEAVEDATA.md CURLOPT_INTERLEAVEFUNCTION.md CURLOPT_IOCTLDATA.md CURLOPT_IOCTLFUNCTION.md CURLOPT_IPRESOLVE.md CURLOPT_ISSUERCERT.md CURLOPT_ISSUERCERT_BLOB.md CURLOPT_KEEP_SENDING_ON_ERROR.md CURLOPT_KEYPASSWD.md CURLOPT_KRBLEVEL.md CURLOPT_LOCALPORT.md CURLOPT_LOCALPORTRANGE.md CURLOPT_LOGIN_OPTIONS.md CURLOPT_LOW_SPEED_LIMIT.md CURLOPT_LOW_SPEED_TIME.md CURLOPT_MAIL_AUTH.md CURLOPT_MAIL_FROM.md CURLOPT_MAIL_RCPT.md CURLOPT_MAIL_RCPT_ALLOWFAILS.md CURLOPT_MAXAGE_CONN.md CURLOPT_MAXCONNECTS.md CURLOPT_MAXFILESIZE.md CURLOPT_MAXFILESIZE_LARGE.md CURLOPT_MAXLIFETIME_CONN.md CURLOPT_MAXREDIRS.md CURLOPT_MAX_RECV_SPEED_LARGE.md CURLOPT_MAX_SEND_SPEED_LARGE.md CURLOPT_MIMEPOST.md CURLOPT_MIME_OPTIONS.md CURLOPT_NETRC.md CURLOPT_NETRC_FILE.md CURLOPT_NEW_DIRECTORY_PERMS.md CURLOPT_NEW_FILE_PERMS.md CURLOPT_NOBODY.md CURLOPT_NOPROGRESS.md CURLOPT_NOPROXY.md CURLOPT_NOSIGNAL.md CURLOPT_OPENSOCKETDATA.md CURLOPT_OPENSOCKETFUNCTION.md CURLOPT_PASSWORD.md CURLOPT_PATH_AS_IS.md CURLOPT_PINNEDPUBLICKEY.md CURLOPT_PIPEWAIT.md CURLOPT_PORT.md CURLOPT_POST.md CURLOPT_POSTFIELDS.md CURLOPT_POSTFIELDSIZE.md CURLOPT_POSTFIELDSIZE_LARGE.md CURLOPT_POSTQUOTE.md CURLOPT_POSTREDIR.md CURLOPT_PREQUOTE.md CURLOPT_PREREQDATA.md CURLOPT_PREREQFUNCTION.md CURLOPT_PRE_PROXY.md CURLOPT_PRIVATE.md CURLOPT_PROGRESSDATA.md CURLOPT_PROGRESSFUNCTION.md CURLOPT_PROTOCOLS.md CURLOPT_PROTOCOLS_STR.md CURLOPT_PROXY.md CURLOPT_PROXYAUTH.md CURLOPT_PROXYHEADER.md CURLOPT_PROXYPASSWORD.md CURLOPT_PROXYPORT.md CURLOPT_PROXYTYPE.md CURLOPT_PROXYUSERNAME.md CURLOPT_PROXYUSERPWD.md CURLOPT_PROXY_CAINFO.md CURLOPT_PROXY_CAINFO_BLOB.md CURLOPT_PROXY_CAPATH.md CURLOPT_PROXY_CRLFILE.md CURLOPT_PROXY_ISSUERCERT.md CURLOPT_PROXY_ISSUERCERT_BLOB.md CURLOPT_PROXY_KEYPASSWD.md CURLOPT_PROXY_PINNEDPUBLICKEY.md CURLOPT_PROXY_SERVICE_NAME.md CURLOPT_PROXY_SSLCERT.md CURLOPT_PROXY_SSLCERTTYPE.md CURLOPT_PROXY_SSLCERT_BLOB.md CURLOPT_PROXY_SSLKEY.md CURLOPT_PROXY_SSLKEYTYPE.md CURLOPT_PROXY_SSLKEY_BLOB.md CURLOPT_PROXY_SSLVERSION.md CURLOPT_PROXY_SSL_CIPHER_LIST.md CURLOPT_PROXY_SSL_OPTIONS.md CURLOPT_PROXY_SSL_VERIFYHOST.md CURLOPT_PROXY_SSL_VERIFYPEER.md CURLOPT_PROXY_TLS13_CIPHERS.md CURLOPT_PROXY_TLSAUTH_PASSWORD.md CURLOPT_PROXY_TLSAUTH_TYPE.md CURLOPT_PROXY_TLSAUTH_USERNAME.md CURLOPT_PROXY_TRANSFER_MODE.md CURLOPT_PUT.md CURLOPT_QUICK_EXIT.md CURLOPT_QUOTE.md CURLOPT_RANDOM_FILE.md CURLOPT_RANGE.md CURLOPT_READDATA.md CURLOPT_READFUNCTION.md CURLOPT_REDIR_PROTOCOLS.md CURLOPT_REDIR_PROTOCOLS_STR.md CURLOPT_REFERER.md CURLOPT_REQUEST_TARGET.md CURLOPT_RESOLVE.md CURLOPT_RESOLVER_START_DATA.md CURLOPT_RESOLVER_START_FUNCTION.md CURLOPT_RESUME_FROM.md CURLOPT_RESUME_FROM_LARGE.md CURLOPT_RTSP_CLIENT_CSEQ.md CURLOPT_RTSP_REQUEST.md CURLOPT_RTSP_SERVER_CSEQ.md CURLOPT_RTSP_SESSION_ID.md CURLOPT_RTSP_STREAM_URI.md CURLOPT_RTSP_TRANSPORT.md CURLOPT_SASL_AUTHZID.md CURLOPT_SASL_IR.md CURLOPT_SEEKDATA.md CURLOPT_SEEKFUNCTION.md CURLOPT_SERVER_RESPONSE_TIMEOUT.md CURLOPT_SERVER_RESPONSE_TIMEOUT_MS.md CURLOPT_SERVICE_NAME.md CURLOPT_SHARE.md CURLOPT_SOCKOPTDATA.md CURLOPT_SOCKOPTFUNCTION.md CURLOPT_SOCKS5_AUTH.md CURLOPT_SOCKS5_GSSAPI_NEC.md CURLOPT_SOCKS5_GSSAPI_SERVICE.md CURLOPT_SSH_AUTH_TYPES.md CURLOPT_SSH_COMPRESSION.md CURLOPT_SSH_HOSTKEYDATA.md CURLOPT_SSH_HOSTKEYFUNCTION.md CURLOPT_SSH_HOST_PUBLIC_KEY_MD5.md CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256.md CURLOPT_SSH_KEYDATA.md CURLOPT_SSH_KEYFUNCTION.md CURLOPT_SSH_KNOWNHOSTS.md CURLOPT_SSH_PRIVATE_KEYFILE.md CURLOPT_SSH_PUBLIC_KEYFILE.md CURLOPT_SSLCERT.md CURLOPT_SSLCERTTYPE.md CURLOPT_SSLCERT_BLOB.md CURLOPT_SSLENGINE.md CURLOPT_SSLENGINE_DEFAULT.md CURLOPT_SSLKEY.md CURLOPT_SSLKEYTYPE.md CURLOPT_SSLKEY_BLOB.md CURLOPT_SSLVERSION.md CURLOPT_SSL_CIPHER_LIST.md CURLOPT_SSL_CTX_DATA.md CURLOPT_SSL_CTX_FUNCTION.md CURLOPT_SSL_EC_CURVES.md CURLOPT_SSL_ENABLE_ALPN.md CURLOPT_SSL_ENABLE_NPN.md CURLOPT_SSL_FALSESTART.md CURLOPT_SSL_OPTIONS.md CURLOPT_SSL_SESSIONID_CACHE.md CURLOPT_SSL_SIGNATURE_ALGORITHMS.md CURLOPT_SSL_VERIFYHOST.md CURLOPT_SSL_VERIFYPEER.md CURLOPT_SSL_VERIFYSTATUS.md CURLOPT_STDERR.md CURLOPT_STREAM_DEPENDS.md CURLOPT_STREAM_DEPENDS_E.md CURLOPT_STREAM_WEIGHT.md CURLOPT_SUPPRESS_CONNECT_HEADERS.md CURLOPT_TCP_FASTOPEN.md CURLOPT_TCP_KEEPALIVE.md CURLOPT_TCP_KEEPCNT.md CURLOPT_TCP_KEEPIDLE.md CURLOPT_TCP_KEEPINTVL.md CURLOPT_TCP_NODELAY.md CURLOPT_TELNETOPTIONS.md CURLOPT_TFTP_BLKSIZE.md CURLOPT_TFTP_NO_OPTIONS.md CURLOPT_TIMECONDITION.md CURLOPT_TIMEOUT.md CURLOPT_TIMEOUT_MS.md CURLOPT_TIMEVALUE.md CURLOPT_TIMEVALUE_LARGE.md CURLOPT_TLS13_CIPHERS.md CURLOPT_TLSAUTH_PASSWORD.md CURLOPT_TLSAUTH_TYPE.md CURLOPT_TLSAUTH_USERNAME.md CURLOPT_TRAILERDATA.md CURLOPT_TRAILERFUNCTION.md CURLOPT_TRANSFERTEXT.md CURLOPT_TRANSFER_ENCODING.md CURLOPT_UNIX_SOCKET_PATH.md CURLOPT_UNRESTRICTED_AUTH.md CURLOPT_UPKEEP_INTERVAL_MS.md CURLOPT_UPLOAD.md CURLOPT_UPLOAD_BUFFERSIZE.md CURLOPT_UPLOAD_FLAGS.md CURLOPT_URL.md CURLOPT_USERAGENT.md CURLOPT_USERNAME.md CURLOPT_USERPWD.md CURLOPT_USE_SSL.md CURLOPT_VERBOSE.md CURLOPT_WILDCARDMATCH.md CURLOPT_WRITEDATA.md CURLOPT_WRITEFUNCTION.md CURLOPT_WS_OPTIONS.md CURLOPT_XFERINFODATA.md CURLOPT_XFERINFOFUNCTION.md CURLOPT_XOAUTH2_BEARER.md CURLSHOPT_LOCKFUNC.md CURLSHOPT_SHARE.md CURLSHOPT_UNLOCKFUNC.md CURLSHOPT_UNSHARE.md CURLSHOPT_USERDATA.md Makefile.am Makefile.inc
.gitignore ABI.md CMakeLists.txt Makefile.am Makefile.inc curl_easy_cleanup.md curl_easy_duphandle.md curl_easy_escape.md curl_easy_getinfo.md curl_easy_header.md curl_easy_init.md curl_easy_nextheader.md curl_easy_option_by_id.md curl_easy_option_by_name.md curl_easy_option_next.md curl_easy_pause.md curl_easy_perform.md curl_easy_recv.md curl_easy_reset.md curl_easy_send.md curl_easy_setopt.md curl_easy_ssls_export.md curl_easy_ssls_import.md curl_easy_strerror.md curl_easy_unescape.md curl_easy_upkeep.md curl_escape.md curl_formadd.md curl_formfree.md curl_formget.md curl_free.md curl_getdate.md curl_getenv.md curl_global_cleanup.md curl_global_init.md curl_global_init_mem.md curl_global_sslset.md curl_global_trace.md curl_mime_addpart.md curl_mime_data.md curl_mime_data_cb.md curl_mime_encoder.md curl_mime_filedata.md curl_mime_filename.md curl_mime_free.md curl_mime_headers.md curl_mime_init.md curl_mime_name.md curl_mime_subparts.md curl_mime_type.md curl_mprintf.md curl_multi_add_handle.md curl_multi_assign.md curl_multi_cleanup.md curl_multi_fdset.md curl_multi_get_handles.md curl_multi_get_offt.md curl_multi_info_read.md curl_multi_init.md curl_multi_notify_disable.md curl_multi_notify_enable.md curl_multi_perform.md curl_multi_poll.md curl_multi_remove_handle.md curl_multi_setopt.md curl_multi_socket.md curl_multi_socket_action.md curl_multi_socket_all.md curl_multi_strerror.md curl_multi_timeout.md curl_multi_wait.md curl_multi_waitfds.md curl_multi_wakeup.md curl_pushheader_byname.md curl_pushheader_bynum.md curl_share_cleanup.md curl_share_init.md curl_share_setopt.md curl_share_strerror.md curl_slist_append.md curl_slist_free_all.md curl_strequal.md curl_strnequal.md curl_unescape.md curl_url.md curl_url_cleanup.md curl_url_dup.md curl_url_get.md curl_url_set.md curl_url_strerror.md curl_version.md curl_version_info.md curl_ws_meta.md curl_ws_recv.md curl_ws_send.md curl_ws_start_frame.md libcurl-easy.md libcurl-env-dbg.md libcurl-env.md libcurl-errors.md libcurl-multi.md libcurl-security.md libcurl-share.md libcurl-thread.md libcurl-tutorial.md libcurl-url.md libcurl-ws.md libcurl.m4 libcurl.md mksymbolsmanpage.pl symbols-in-versions symbols.pl
tests CI.md FILEFORMAT.md HTTP.md TEST-SUITE.md
.gitignore ALTSVC.md BINDINGS.md BUG-BOUNTY.md BUGS.md CIPHERS-TLS12.md CIPHERS.md CMakeLists.txt CODE_OF_CONDUCT.md CODE_REVIEW.md CONTRIBUTE.md CURL-DISABLE.md CURLDOWN.md DEPRECATE.md DISTROS.md EARLY-RELEASE.md ECH.md EXPERIMENTAL.md FAQ.md FEATURES.md GOVERNANCE.md HELP-US.md HISTORY.md HSTS.md HTTP-COOKIES.md HTTP3.md HTTPSRR.md INFRASTRUCTURE.md INSTALL-CMAKE.md INSTALL.md INTERNALS.md IPFS.md KNOWN_BUGS.md KNOWN_RISKS.md MAIL-ETIQUETTE.md MANUAL.md Makefile.am README.md RELEASE-PROCEDURE.md ROADMAP.md RUSTLS.md SECURITY-ADVISORY.md SPONSORS.md SSL-PROBLEMS.md SSLCERTS.md THANKS THANKS-filter TODO.md TheArtOfHttpScripting.md URL-SYNTAX.md VERIFY.md VERSIONS.md VULN-DISCLOSURE-POLICY.md curl-config.md mk-ca-bundle.md options-in-versions runtests.md testcurl.md wcurl.md
include
curl Makefile.am curl.h curlver.h easy.h header.h mprintf.h multi.h options.h stdcheaders.h system.h typecheck-gcc.h urlapi.h websockets.h
Makefile.am README.md
lib
curlx base64.c base64.h basename.c basename.h dynbuf.c dynbuf.h fopen.c fopen.h inet_ntop.c inet_ntop.h inet_pton.c inet_pton.h multibyte.c multibyte.h nonblock.c nonblock.h snprintf.c snprintf.h strcopy.c strcopy.h strdup.c strdup.h strerr.c strerr.h strparse.c strparse.h timediff.c timediff.h timeval.c timeval.h version_win32.c version_win32.h wait.c wait.h warnless.c warnless.h winapi.c winapi.h
vauth cleartext.c cram.c digest.c digest.h digest_sspi.c gsasl.c krb5_gssapi.c krb5_sspi.c ntlm.c ntlm_sspi.c oauth2.c spnego_gssapi.c spnego_sspi.c vauth.c vauth.h
vquic curl_ngtcp2.c curl_ngtcp2.h curl_quiche.c curl_quiche.h vquic-tls.c vquic-tls.h vquic.c vquic.h vquic_int.h
vssh libssh.c libssh2.c ssh.h vssh.c vssh.h
vtls apple.c apple.h cipher_suite.c cipher_suite.h gtls.c gtls.h hostcheck.c hostcheck.h keylog.c keylog.h mbedtls.c mbedtls.h openssl.c openssl.h rustls.c rustls.h schannel.c schannel.h schannel_int.h schannel_verify.c vtls.c vtls.h vtls_int.h vtls_scache.c vtls_scache.h vtls_spack.c vtls_spack.h wolfssl.c wolfssl.h x509asn1.c x509asn1.h
.gitignore CMakeLists.txt Makefile.am Makefile.inc Makefile.soname altsvc.c altsvc.h amigaos.c amigaos.h arpa_telnet.h asyn-ares.c asyn-base.c asyn-thrdd.c asyn.h bufq.c bufq.h bufref.c bufref.h cf-dns.c cf-dns.h cf-h1-proxy.c cf-h1-proxy.h cf-h2-proxy.c cf-h2-proxy.h cf-haproxy.c cf-haproxy.h cf-https-connect.c cf-https-connect.h cf-ip-happy.c cf-ip-happy.h cf-socket.c cf-socket.h cfilters.c cfilters.h config-mac.h config-os400.h config-riscos.h config-win32.h conncache.c conncache.h connect.c connect.h content_encoding.c content_encoding.h cookie.c cookie.h creds.c creds.h cshutdn.c cshutdn.h curl_addrinfo.c curl_addrinfo.h curl_config-cmake.h.in curl_ctype.h curl_endian.c curl_endian.h curl_fnmatch.c curl_fnmatch.h curl_fopen.c curl_fopen.h curl_get_line.c curl_get_line.h curl_gethostname.c curl_gethostname.h curl_gssapi.c curl_gssapi.h curl_hmac.h curl_ldap.h curl_md4.h curl_md5.h curl_memrchr.c curl_memrchr.h curl_ntlm_core.c curl_ntlm_core.h curl_printf.h curl_range.c curl_range.h curl_sasl.c curl_sasl.h curl_setup.h curl_sha256.h curl_sha512_256.c curl_sha512_256.h curl_share.c curl_share.h curl_sspi.c curl_sspi.h curl_threads.c curl_threads.h curl_trc.c curl_trc.h cw-out.c cw-out.h cw-pause.c cw-pause.h dict.c dict.h dllmain.c dnscache.c dnscache.h doh.c doh.h dynhds.c dynhds.h easy.c easy_lock.h easygetopt.c easyif.h easyoptions.c easyoptions.h escape.c escape.h fake_addrinfo.c fake_addrinfo.h file.c file.h fileinfo.c fileinfo.h formdata.c formdata.h ftp-int.h ftp.c ftp.h ftplistparser.c ftplistparser.h functypes.h getenv.c getinfo.c getinfo.h gopher.c gopher.h hash.c hash.h headers.c headers.h hmac.c hostip.c hostip.h hostip4.c hostip6.c hsts.c hsts.h http.c http.h http1.c http1.h http2.c http2.h http_aws_sigv4.c http_aws_sigv4.h http_chunks.c http_chunks.h http_digest.c http_digest.h http_negotiate.c http_negotiate.h http_ntlm.c http_ntlm.h http_proxy.c http_proxy.h httpsrr.c httpsrr.h idn.c idn.h if2ip.c if2ip.h imap.c imap.h ldap.c libcurl.def libcurl.rc libcurl.vers.in llist.c llist.h macos.c macos.h md4.c md5.c memdebug.c mime.c mime.h mprintf.c mqtt.c mqtt.h multi.c multi_ev.c multi_ev.h multi_ntfy.c multi_ntfy.h multihandle.h multiif.h netrc.c netrc.h noproxy.c noproxy.h openldap.c optiontable.pl parsedate.c parsedate.h peer.c peer.h pingpong.c pingpong.h pop3.c pop3.h progress.c progress.h protocol.c protocol.h psl.c psl.h rand.c rand.h ratelimit.c ratelimit.h request.c request.h rtsp.c rtsp.h select.c select.h sendf.c sendf.h setopt.c setopt.h setup-os400.h setup-vms.h setup-win32.h sha256.c sigpipe.h slist.c slist.h smb.c smb.h smtp.c smtp.h sockaddr.h socketpair.c socketpair.h socks.c socks.h socks_gssapi.c socks_sspi.c splay.c splay.h strcase.c strcase.h strequal.c strerror.c strerror.h system_win32.c system_win32.h telnet.c telnet.h tftp.c tftp.h thrdpool.c thrdpool.h thrdqueue.c thrdqueue.h transfer.c transfer.h uint-bset.c uint-bset.h uint-hash.c uint-hash.h uint-spbset.c uint-spbset.h uint-table.c uint-table.h url.c url.h urlapi-int.h urlapi.c urldata.h version.c ws.c ws.h
m4 .gitignore curl-amissl.m4 curl-apple-sectrust.m4 curl-compilers.m4 curl-confopts.m4 curl-functions.m4 curl-gnutls.m4 curl-mbedtls.m4 curl-openssl.m4 curl-override.m4 curl-reentrant.m4 curl-rustls.m4 curl-schannel.m4 curl-sysconfig.m4 curl-wolfssl.m4 xc-am-iface.m4 xc-cc-check.m4 xc-lt-iface.m4 xc-val-flgs.m4 zz40-xc-ovr.m4 zz50-xc-ovr.m4
projects
OS400
rpg-examples HEADERAPI HTTPPOST INMEMORY SIMPLE1 SIMPLE2 SMTPSRCMBR
.checksrc README.OS400 ccsidcurl.c ccsidcurl.h config400.default curl.cmd curl.inc.in curlcl.c curlmain.c initscript.sh make-docs.sh make-include.sh make-lib.sh make-src.sh make-tests.sh makefile.sh os400sys.c os400sys.h
Windows
tmpl .gitattributes README.txt curl-all.sln curl.sln curl.vcxproj curl.vcxproj.filters libcurl.sln libcurl.vcxproj libcurl.vcxproj.filters
.gitignore README.md generate.bat
vms Makefile.am backup_gnv_curl_src.com build_curl-config_script.com build_gnv_curl.com build_gnv_curl_pcsi_desc.com build_gnv_curl_pcsi_text.com build_gnv_curl_release_notes.com build_libcurl_pc.com build_vms.com clean_gnv_curl.com compare_curl_source.com config_h.com curl_crtl_init.c curl_gnv_build_steps.txt curl_release_note_start.txt curl_startup.com curlmsg.h curlmsg.msg curlmsg.sdl curlmsg_vms.h generate_config_vms_h_curl.com generate_vax_transfer.com gnv_conftest.c_first gnv_curl_configure.sh gnv_libcurl_symbols.opt gnv_link_curl.com macro32_exactcase.patch make_gnv_curl_install.sh make_pcsi_curl_kit_name.com pcsi_gnv_curl_file_list.txt pcsi_product_gnv_curl.com readme report_openssl_version.c setup_gnv_curl_build.com stage_curl_install.com vms_eco_level.h
Makefile.am README.md
scripts .checksrc CMakeLists.txt Makefile.am badwords badwords-all badwords.txt cd2cd cd2nroff cdall checksrc-all.pl checksrc.pl cmakelint.sh completion.pl contributors.sh contrithanks.sh coverage.sh delta dmaketgz extract-unit-protos firefox-db2pem.sh installcheck.sh maketgz managen mdlinkcheck mk-ca-bundle.pl mk-unity.pl nroff2cd perlcheck.sh pythonlint.sh randdisable release-notes.pl release-tools.sh schemetable.c singleuse.pl spacecheck.pl top-complexity top-length verify-release wcurl
src
toolx tool_time.c tool_time.h
.checksrc .gitignore CMakeLists.txt Makefile.am Makefile.inc config2setopts.c config2setopts.h curl.rc curlinfo.c mk-file-embed.pl mkhelp.pl slist_wc.c slist_wc.h terminal.c terminal.h tool_cb_dbg.c tool_cb_dbg.h tool_cb_hdr.c tool_cb_hdr.h tool_cb_prg.c tool_cb_prg.h tool_cb_rea.c tool_cb_rea.h tool_cb_see.c tool_cb_see.h tool_cb_soc.c tool_cb_soc.h tool_cb_wrt.c tool_cb_wrt.h tool_cfgable.c tool_cfgable.h tool_dirhie.c tool_dirhie.h tool_doswin.c tool_doswin.h tool_easysrc.c tool_easysrc.h tool_filetime.c tool_filetime.h tool_findfile.c tool_findfile.h tool_formparse.c tool_formparse.h tool_getparam.c tool_getparam.h tool_getpass.c tool_getpass.h tool_help.c tool_help.h tool_helpers.c tool_helpers.h tool_hugehelp.h tool_ipfs.c tool_ipfs.h tool_libinfo.c tool_libinfo.h tool_listhelp.c tool_main.c tool_main.h tool_msgs.c tool_msgs.h tool_operate.c tool_operate.h tool_operhlp.c tool_operhlp.h tool_paramhlp.c tool_paramhlp.h tool_parsecfg.c tool_parsecfg.h tool_progress.c tool_progress.h tool_sdecls.h tool_setopt.c tool_setopt.h tool_setup.h tool_ssls.c tool_ssls.h tool_stderr.c tool_stderr.h tool_urlglob.c tool_urlglob.h tool_util.c tool_util.h tool_version.h tool_vms.c tool_vms.h tool_writeout.c tool_writeout.h tool_writeout_json.c tool_writeout_json.h tool_xattr.c tool_xattr.h var.c var.h
tests
certs .gitignore CMakeLists.txt Makefile.am Makefile.inc genserv.pl srp-verifier-conf srp-verifier-db test-ca.cnf test-ca.prm test-client-cert.prm test-client-eku-only.prm test-localhost-san-first.prm test-localhost-san-last.prm test-localhost.nn.prm test-localhost.prm test-localhost0h.prm
cmake CMakeLists.txt test.c test.cpp test.sh
data .gitignore DISABLED Makefile.am data-xml1 data1400.c data1401.c data1402.c data1403.c data1404.c data1405.c data1406.c data1407.c data1420.c data1461.txt data1463.txt data1465.c data1481.c data1705-1.md data1705-2.md data1705-3.md data1705-4.md data1705-stdout.1 data1706-1.md data1706-2.md data1706-3.md data1706-4.md data1706-stdout.txt data320.html test1 test10 test100 test1000 test1001 test1002 test1003 test1004 test1005 test1006 test1007 test1008 test1009 test101 test1010 test1011 test1012 test1013 test1014 test1015 test1016 test1017 test1018 test1019 test102 test1020 test1021 test1022 test1023 test1024 test1025 test1026 test1027 test1028 test1029 test103 test1030 test1031 test1032 test1033 test1034 test1035 test1036 test1037 test1038 test1039 test104 test1040 test1041 test1042 test1043 test1044 test1045 test1046 test1047 test1048 test1049 test105 test1050 test1051 test1052 test1053 test1054 test1055 test1056 test1057 test1058 test1059 test106 test1060 test1061 test1062 test1063 test1064 test1065 test1066 test1067 test1068 test1069 test107 test1070 test1071 test1072 test1073 test1074 test1075 test1076 test1077 test1078 test1079 test108 test1080 test1081 test1082 test1083 test1084 test1085 test1086 test1087 test1088 test1089 test109 test1090 test1091 test1092 test1093 test1094 test1095 test1096 test1097 test1098 test1099 test11 test110 test1100 test1101 test1102 test1103 test1104 test1105 test1106 test1107 test1108 test1109 test111 test1110 test1111 test1112 test1113 test1114 test1115 test1116 test1117 test1118 test1119 test112 test1120 test1121 test1122 test1123 test1124 test1125 test1126 test1127 test1128 test1129 test113 test1130 test1131 test1132 test1133 test1134 test1135 test1136 test1137 test1138 test1139 test114 test1140 test1141 test1142 test1143 test1144 test1145 test1146 test1147 test1148 test1149 test115 test1150 test1151 test1152 test1153 test1154 test1155 test1156 test1157 test1158 test1159 test116 test1160 test1161 test1162 test1163 test1164 test1165 test1166 test1167 test1168 test1169 test117 test1170 test1171 test1172 test1173 test1174 test1175 test1176 test1177 test1178 test1179 test118 test1180 test1181 test1182 test1183 test1184 test1185 test1186 test1187 test1188 test1189 test119 test1190 test1191 test1192 test1193 test1194 test1195 test1196 test1197 test1198 test1199 test12 test120 test1200 test1201 test1202 test1203 test1204 test1205 test1206 test1207 test1208 test1209 test121 test1210 test1211 test1212 test1213 test1214 test1215 test1216 test1217 test1218 test1219 test122 test1220 test1221 test1222 test1223 test1224 test1225 test1226 test1227 test1228 test1229 test123 test1230 test1231 test1232 test1233 test1234 test1235 test1236 test1237 test1238 test1239 test124 test1240 test1241 test1242 test1243 test1244 test1245 test1246 test1247 test1248 test1249 test125 test1250 test1251 test1252 test1253 test1254 test1255 test1256 test1257 test1258 test1259 test126 test1260 test1261 test1262 test1263 test1264 test1265 test1266 test1267 test1268 test1269 test127 test1270 test1271 test1272 test1273 test1274 test1275 test1276 test1277 test1278 test1279 test128 test1280 test1281 test1282 test1283 test1284 test1285 test1286 test1287 test1288 test1289 test129 test1290 test1291 test1292 test1293 test1294 test1295 test1296 test1297 test1298 test1299 test13 test130 test1300 test1301 test1302 test1303 test1304 test1305 test1306 test1307 test1308 test1309 test131 test1310 test1311 test1312 test1313 test1314 test1315 test1316 test1317 test1318 test1319 test132 test1320 test1321 test1322 test1323 test1324 test1325 test1326 test1327 test1328 test1329 test133 test1330 test1331 test1332 test1333 test1334 test1335 test1336 test1337 test1338 test1339 test134 test1340 test1341 test1342 test1343 test1344 test1345 test1346 test1347 test1348 test1349 test135 test1350 test1351 test1352 test1353 test1354 test1355 test1356 test1357 test1358 test1359 test136 test1360 test1361 test1362 test1363 test1364 test1365 test1366 test1367 test1368 test1369 test137 test1370 test1371 test1372 test1373 test1374 test1375 test1376 test1377 test1378 test1379 test138 test1380 test1381 test1382 test1383 test1384 test1385 test1386 test1387 test1388 test1389 test139 test1390 test1391 test1392 test1393 test1394 test1395 test1396 test1397 test1398 test1399 test14 test140 test1400 test1401 test1402 test1403 test1404 test1405 test1406 test1407 test1408 test1409 test141 test1410 test1411 test1412 test1413 test1414 test1415 test1416 test1417 test1418 test1419 test142 test1420 test1421 test1422 test1423 test1424 test1425 test1426 test1427 test1428 test1429 test143 test1430 test1431 test1432 test1433 test1434 test1435 test1436 test1437 test1438 test1439 test144 test1440 test1441 test1442 test1443 test1444 test1445 test1446 test1447 test1448 test1449 test145 test1450 test1451 test1452 test1453 test1454 test1455 test1456 test1457 test1458 test1459 test146 test1460 test1461 test1462 test1463 test1464 test1465 test1466 test1467 test1468 test1469 test147 test1470 test1471 test1472 test1473 test1474 test1475 test1476 test1477 test1478 test1479 test148 test1480 test1481 test1482 test1483 test1484 test1485 test1486 test1487 test1488 test1489 test149 test1490 test1491 test1492 test1493 test1494 test1495 test1496 test1497 test1498 test1499 test15 test150 test1500 test1501 test1502 test1503 test1504 test1505 test1506 test1507 test1508 test1509 test151 test1510 test1511 test1512 test1513 test1514 test1515 test1516 test1517 test1518 test1519 test152 test1520 test1521 test1522 test1523 test1524 test1525 test1526 test1527 test1528 test1529 test153 test1530 test1531 test1532 test1533 test1534 test1535 test1536 test1537 test1538 test1539 test154 test1540 test1541 test1542 test1543 test1544 test1545 test1546 test1547 test1548 test1549 test155 test1550 test1551 test1552 test1553 test1554 test1555 test1556 test1557 test1558 test1559 test156 test1560 test1561 test1562 test1563 test1564 test1565 test1566 test1567 test1568 test1569 test157 test1570 test1571 test1572 test1573 test1574 test1575 test1576 test1577 test1578 test1579 test158 test1580 test1581 test1582 test1583 test1584 test1585 test1586 test1587 test1588 test1589 test159 test1590 test1591 test1592 test1593 test1594 test1595 test1596 test1597 test1598 test1599 test16 test160 test1600 test1601 test1602 test1603 test1604 test1605 test1606 test1607 test1608 test1609 test161 test1610 test1611 test1612 test1613 test1614 test1615 test1616 test1617 test1618 test1619 test162 test1620 test1621 test1622 test1623 test1624 test1625 test1626 test1627 test1628 test1629 test163 test1630 test1631 test1632 test1633 test1634 test1635 test1636 test1637 test1638 test1639 test164 test1640 test1641 test1642 test1643 test1644 test1645 test165 test1650 test1651 test1652 test1653 test1654 test1655 test1656 test1657 test1658 test1659 test166 test1660 test1661 test1662 test1663 test1664 test1665 test1666 test1667 test1668 test1669 test167 test1670 test1671 test1672 test1673 test1674 test1675 test1676 test168 test1680 test1681 test1682 test1683 test1684 test1685 test169 test17 test170 test1700 test1701 test1702 test1703 test1704 test1705 test1706 test1707 test1708 test1709 test171 test1710 test1711 test1712 test1713 test1714 test1715 test172 test1720 test1721 test173 test174 test175 test176 test177 test178 test179 test18 test180 test1800 test1801 test1802 test181 test182 test183 test184 test1847 test1848 test1849 test185 test1850 test1851 test186 test187 test188 test189 test19 test190 test1900 test1901 test1902 test1903 test1904 test1905 test1906 test1907 test1908 test1909 test191 test1910 test1911 test1912 test1913 test1914 test1915 test1916 test1917 test1918 test1919 test192 test1920 test1921 test193 test1933 test1934 test1935 test1936 test1937 test1938 test1939 test194 test1940 test1941 test1942 test1943 test1944 test1945 test1946 test1947 test1948 test195 test1955 test1956 test1957 test1958 test1959 test196 test1960 test1964 test1965 test1966 test197 test1970 test1971 test1972 test1973 test1974 test1975 test1976 test1977 test1978 test1979 test198 test1980 test1981 test1982 test1983 test1984 test199 test2 test20 test200 test2000 test2001 test2002 test2003 test2004 test2005 test2006 test2007 test2008 test2009 test201 test2010 test2011 test2012 test2013 test2014 test202 test2023 test2024 test2025 test2026 test2027 test2028 test2029 test203 test2030 test2031 test2032 test2033 test2034 test2035 test2037 test2038 test2039 test204 test2040 test2041 test2042 test2043 test2044 test2045 test2046 test2047 test2048 test2049 test205 test2050 test2051 test2052 test2053 test2054 test2055 test2056 test2057 test2058 test2059 test206 test2060 test2061 test2062 test2063 test2064 test2065 test2066 test2067 test2068 test2069 test207 test2070 test2071 test2072 test2073 test2074 test2075 test2076 test2077 test2078 test2079 test208 test2080 test2081 test2082 test2083 test2084 test2085 test2086 test2087 test2088 test2089 test209 test2090 test2091 test2092 test21 test210 test2100 test2101 test2102 test2103 test2104 test211 test212 test213 test214 test215 test216 test217 test218 test219 test22 test220 test2200 test2201 test2202 test2203 test2204 test2205 test2206 test2207 test221 test222 test223 test224 test225 test226 test227 test228 test229 test23 test230 test2300 test2301 test2302 test2303 test2304 test2306 test2307 test2308 test2309 test231 test232 test233 test234 test235 test236 test237 test238 test239 test24 test240 test2400 test2401 test2402 test2403 test2404 test2405 test2406 test2407 test2408 test2409 test241 test2410 test2411 test242 test243 test244 test245 test246 test247 test248 test249 test25 test250 test2500 test2501 test2502 test2503 test2504 test2505 test2506 test251 test252 test253 test254 test255 test256 test257 test258 test259 test26 test260 test2600 test2601 test2602 test2603 test2604 test2605 test261 test262 test263 test264 test265 test266 test267 test268 test269 test27 test270 test2700 test2701 test2702 test2703 test2704 test2705 test2706 test2707 test2708 test2709 test271 test2710 test2711 test2712 test2713 test2714 test2715 test2716 test2717 test2718 test2719 test272 test2720 test2721 test2722 test2723 test273 test274 test275 test276 test277 test278 test279 test28 test280 test281 test282 test283 test284 test285 test286 test287 test288 test289 test29 test290 test291 test292 test293 test294 test295 test296 test297 test298 test299 test3 test30 test300 test3000 test3001 test3002 test3003 test3004 test3005 test3006 test3007 test3008 test3009 test301 test3010 test3011 test3012 test3013 test3014 test3015 test3016 test3017 test3018 test3019 test302 test3020 test3021 test3022 test3023 test3024 test3025 test3026 test3027 test3028 test3029 test303 test3030 test3031 test3032 test3033 test3034 test3035 test3036 test304 test305 test306 test307 test308 test309 test31 test310 test3100 test3101 test3102 test3103 test3104 test3105 test3106 test311 test312 test313 test314 test315 test316 test317 test318 test319 test32 test320 test3200 test3201 test3202 test3203 test3204 test3205 test3206 test3207 test3208 test3209 test321 test3210 test3211 test3212 test3213 test3214 test3215 test3216 test3217 test3218 test3219 test322 test3220 test323 test324 test325 test326 test327 test328 test329 test33 test330 test3300 test3301 test3302 test331 test332 test333 test334 test335 test336 test337 test338 test339 test34 test340 test341 test342 test343 test344 test345 test346 test347 test348 test349 test35 test350 test351 test352 test353 test354 test355 test356 test357 test358 test359 test36 test360 test361 test362 test363 test364 test365 test366 test367 test368 test369 test37 test370 test371 test372 test373 test374 test375 test376 test378 test379 test38 test380 test381 test383 test384 test385 test386 test387 test388 test389 test39 test390 test391 test392 test393 test394 test395 test396 test397 test398 test399 test4 test40 test400 test4000 test4001 test401 test402 test403 test404 test405 test406 test407 test408 test409 test41 test410 test411 test412 test413 test414 test415 test416 test417 test418 test419 test42 test420 test421 test422 test423 test424 test425 test426 test427 test428 test429 test43 test430 test431 test432 test433 test434 test435 test436 test437 test438 test439 test44 test440 test441 test442 test443 test444 test445 test446 test447 test448 test449 test45 test450 test451 test452 test453 test454 test455 test456 test457 test458 test459 test46 test460 test461 test462 test463 test467 test468 test469 test47 test470 test471 test472 test473 test474 test475 test476 test477 test478 test479 test48 test480 test481 test482 test483 test484 test485 test486 test487 test488 test489 test49 test490 test491 test492 test493 test494 test495 test496 test497 test498 test499 test5 test50 test500 test501 test502 test503 test504 test505 test506 test507 test508 test509 test51 test510 test511 test512 test513 test514 test515 test516 test517 test518 test519 test52 test520 test521 test522 test523 test524 test525 test526 test527 test528 test529 test53 test530 test531 test532 test533 test534 test535 test536 test537 test538 test539 test54 test540 test541 test542 test543 test544 test545 test546 test547 test548 test549 test55 test550 test551 test552 test553 test554 test555 test556 test557 test558 test559 test56 test560 test561 test562 test563 test564 test565 test566 test567 test568 test569 test57 test570 test571 test572 test573 test574 test575 test576 test577 test578 test579 test58 test580 test581 test582 test583 test584 test585 test586 test587 test588 test589 test59 test590 test591 test592 test593 test594 test595 test596 test597 test598 test599 test6 test60 test600 test601 test602 test603 test604 test605 test606 test607 test608 test609 test61 test610 test611 test612 test613 test614 test615 test616 test617 test618 test619 test62 test620 test621 test622 test623 test624 test625 test626 test627 test628 test629 test63 test630 test631 test632 test633 test634 test635 test636 test637 test638 test639 test64 test640 test641 test642 test643 test644 test645 test646 test647 test648 test649 test65 test650 test651 test652 test653 test654 test655 test656 test658 test659 test66 test660 test661 test662 test663 test664 test665 test666 test667 test668 test669 test67 test670 test671 test672 test673 test674 test675 test676 test677 test678 test679 test68 test680 test681 test682 test683 test684 test685 test686 test687 test688 test689 test69 test690 test691 test692 test693 test694 test695 test696 test697 test698 test699 test7 test70 test700 test701 test702 test703 test704 test705 test706 test707 test708 test709 test71 test710 test711 test712 test713 test714 test715 test716 test717 test718 test719 test72 test720 test721 test722 test723 test724 test725 test726 test727 test728 test729 test73 test730 test731 test732 test733 test734 test735 test736 test737 test738 test739 test74 test740 test741 test742 test743 test744 test745 test746 test747 test748 test749 test75 test750 test751 test752 test753 test754 test755 test756 test757 test758 test759 test76 test760 test761 test762 test763 test764 test765 test766 test767 test768 test769 test77 test770 test771 test772 test773 test774 test775 test776 test777 test778 test779 test78 test780 test781 test782 test783 test784 test785 test786 test787 test788 test789 test79 test790 test791 test792 test793 test794 test795 test796 test797 test798 test799 test8 test80 test800 test801 test802 test803 test804 test805 test806 test807 test808 test809 test81 test810 test811 test812 test813 test814 test815 test816 test817 test818 test819 test82 test820 test821 test822 test823 test824 test825 test826 test827 test828 test829 test83 test830 test831 test832 test833 test834 test835 test836 test837 test838 test839 test84 test840 test841 test842 test843 test844 test845 test846 test847 test848 test849 test85 test850 test851 test852 test853 test854 test855 test856 test857 test858 test859 test86 test860 test861 test862 test863 test864 test865 test866 test867 test868 test869 test87 test870 test871 test872 test873 test874 test875 test876 test877 test878 test879 test88 test880 test881 test882 test883 test884 test885 test886 test887 test888 test889 test89 test890 test891 test892 test893 test894 test895 test896 test897 test898 test899 test9 test90 test900 test901 test902 test903 test904 test905 test906 test907 test908 test909 test91 test910 test911 test912 test913 test914 test915 test916 test917 test918 test919 test92 test920 test921 test922 test923 test924 test925 test926 test927 test928 test929 test93 test930 test931 test932 test933 test934 test935 test936 test937 test938 test939 test94 test940 test941 test942 test943 test944 test945 test946 test947 test948 test949 test95 test950 test951 test952 test953 test954 test955 test956 test957 test958 test959 test96 test960 test961 test962 test963 test964 test965 test966 test967 test968 test969 test97 test970 test971 test972 test973 test974 test975 test976 test977 test978 test979 test98 test980 test981 test982 test983 test984 test985 test986 test987 test988 test989 test99 test990 test991 test992 test993 test994 test995 test996 test997 test998 test999
http
testenv
mod_curltest .gitignore mod_curltest.c
__init__.py caddy.py certs.py client.py curl.py dante.py dnsd.py env.py httpd.py nghttpx.py ports.py sshd.py vsftpd.py ws_echo_server.py
.gitignore CMakeLists.txt Makefile.am config.ini.in conftest.py requirements.txt scorecard.py test_01_basic.py test_02_download.py test_03_goaway.py test_04_stuttered.py test_05_errors.py test_06_eyeballs.py test_07_upload.py test_08_caddy.py test_09_push.py test_10_proxy.py test_11_unix.py test_12_reuse.py test_13_proxy_auth.py test_14_auth.py test_15_tracing.py test_16_info.py test_17_ssl_use.py test_18_methods.py test_19_shutdown.py test_20_websockets.py test_21_resolve.py test_22_httpsrr.py test_30_vsftpd.py test_31_vsftpds.py test_32_ftps_vsftpd.py test_40_socks.py test_50_scp.py test_51_sftp.py
libtest .gitignore CMakeLists.txt Makefile.am Makefile.inc cli_ftp_upload.c cli_h2_pausing.c cli_h2_serverpush.c cli_h2_upgrade_extreme.c cli_hx_download.c cli_hx_upload.c cli_tls_session_reuse.c cli_upload_pausing.c cli_ws_data.c cli_ws_pingpong.c first.c first.h lib1156.c lib1301.c lib1308.c lib1485.c lib1500.c lib1501.c lib1502.c lib1506.c lib1507.c lib1508.c lib1509.c lib1510.c lib1511.c lib1512.c lib1513.c lib1514.c lib1515.c lib1517.c lib1518.c lib1520.c lib1522.c lib1523.c lib1525.c lib1526.c lib1527.c lib1528.c lib1529.c lib1530.c lib1531.c lib1532.c lib1533.c lib1534.c lib1535.c lib1536.c lib1537.c lib1538.c lib1540.c lib1541.c lib1542.c lib1545.c lib1549.c lib1550.c lib1551.c lib1552.c lib1553.c lib1554.c lib1555.c lib1556.c lib1557.c lib1558.c lib1559.c lib1560.c lib1564.c lib1565.c lib1567.c lib1568.c lib1569.c lib1571.c lib1576.c lib1582.c lib1587.c lib1588.c lib1589.c lib1591.c lib1592.c lib1593.c lib1594.c lib1597.c lib1598.c lib1599.c lib1662.c lib1900.c lib1901.c lib1902.c lib1903.c lib1905.c lib1906.c lib1907.c lib1908.c lib1910.c lib1911.c lib1912.c lib1913.c lib1915.c lib1916.c lib1918.c lib1919.c lib1920.c lib1921.c lib1933.c lib1934.c lib1935.c lib1936.c lib1937.c lib1938.c lib1939.c lib1940.c lib1945.c lib1947.c lib1948.c lib1955.c lib1956.c lib1957.c lib1958.c lib1959.c lib1960.c lib1964.c lib1965.c lib1970.c lib1971.c lib1972.c lib1973.c lib1974.c lib1975.c lib1977.c lib1978.c lib2023.c lib2032.c lib2082.c lib2301.c lib2302.c lib2304.c lib2306.c lib2308.c lib2309.c lib2402.c lib2404.c lib2405.c lib2502.c lib2504.c lib2505.c lib2506.c lib2700.c lib3010.c lib3025.c lib3026.c lib3027.c lib3033.c lib3034.c lib3100.c lib3101.c lib3102.c lib3103.c lib3104.c lib3105.c lib3207.c lib3208.c lib500.c lib501.c lib502.c lib503.c lib504.c lib505.c lib506.c lib507.c lib508.c lib509.c lib510.c lib511.c lib512.c lib513.c lib514.c lib515.c lib516.c lib517.c lib518.c lib519.c lib520.c lib521.c lib523.c lib524.c lib525.c lib526.c lib530.c lib533.c lib536.c lib537.c lib539.c lib540.c lib541.c lib542.c lib543.c lib544.c lib547.c lib549.c lib552.c lib553.c lib554.c lib555.c lib556.c lib557.c lib558.c lib559.c lib560.c lib562.c lib564.c lib566.c lib567.c lib568.c lib569.c lib570.c lib571.c lib572.c lib573.c lib574.c lib575.c lib576.c lib578.c lib579.c lib582.c lib583.c lib586.c lib589.c lib590.c lib591.c lib597.c lib598.c lib599.c lib643.c lib650.c lib651.c lib652.c lib653.c lib654.c lib655.c lib658.c lib659.c lib661.c lib666.c lib667.c lib668.c lib670.c lib674.c lib676.c lib677.c lib678.c lib694.c lib695.c lib751.c lib753.c lib757.c lib758.c lib766.c memptr.c mk-lib1521.pl test1013.pl test1022.pl test307.pl test610.pl test613.pl testtrace.c testtrace.h testutil.c testutil.h unitcheck.h
server .checksrc .gitignore CMakeLists.txt Makefile.am Makefile.inc dnsd.c first.c first.h getpart.c mqttd.c resolve.c rtspd.c sockfilt.c socksd.c sws.c tftpd.c util.c
tunit .gitignore CMakeLists.txt Makefile.am Makefile.inc README.md tool1394.c tool1604.c tool1621.c tool1622.c tool1623.c tool1720.c
unit .gitignore CMakeLists.txt Makefile.am Makefile.inc README.md unit1300.c unit1302.c unit1303.c unit1304.c unit1305.c unit1307.c unit1309.c unit1323.c unit1330.c unit1395.c unit1396.c unit1397.c unit1398.c unit1399.c unit1600.c unit1601.c unit1602.c unit1603.c unit1605.c unit1606.c unit1607.c unit1608.c unit1609.c unit1610.c unit1611.c unit1612.c unit1614.c unit1615.c unit1616.c unit1620.c unit1625.c unit1626.c unit1627.c unit1636.c unit1650.c unit1651.c unit1652.c unit1653.c unit1654.c unit1655.c unit1656.c unit1657.c unit1658.c unit1660.c unit1661.c unit1663.c unit1664.c unit1666.c unit1667.c unit1668.c unit1669.c unit1674.c unit1675.c unit1676.c unit1979.c unit1980.c unit2600.c unit2601.c unit2602.c unit2603.c unit2604.c unit2605.c unit3200.c unit3205.c unit3211.c unit3212.c unit3213.c unit3214.c unit3216.c unit3219.c unit3300.c unit3301.c unit3302.c
.gitignore CMakeLists.txt Makefile.am allversions.pm appveyor.pm azure.pm config.in configurehelp.pm.in devtest.pl dictserver.py directories.pm ech_combos.py ech_tests.sh ftpserver.pl getpart.pm globalconfig.pm http-server.pl http2-server.pl http3-server.pl memanalyze.pl memanalyzer.pm negtelnetserver.py nghttpx.conf pathhelp.pm processhelp.pm requirements.txt rtspserver.pl runner.pm runtests.pl secureserver.pl serverhelp.pm servers.pm smbserver.py sshhelp.pm sshserver.pl test1119.pl test1135.pl test1139.pl test1140.pl test1165.pl test1167.pl test1173.pl test1175.pl test1177.pl test1222.pl test1275.pl test1276.pl test1477.pl test1486.pl test1488.pl test1544.pl test1707.pl test745.pl test971.pl testcurl.pl testutil.pm tftpserver.pl util.py valgrind.pm valgrind.supp
.clang-tidy.yml .dir-locals.el .editorconfig .git-blame-ignore-revs .gitattributes .gitignore .mailmap CHANGES.md CMakeLists.txt COPYING Dockerfile GIT-INFO.md Makefile.am README README.md RELEASE-NOTES REUSE.toml SECURITY.md acinclude.m4 appveyor.sh appveyor.yml configure.ac curl-config.in libcurl.pc.in renovate.json
examples .env config.ini crypto_test.lua env_test.lua fs_example.lua http_server.lua https_test.lua ini_example.lua json.lua log.lua path_fs_example.lua process_example.lua request_download.lua request_test.lua run_all.lua sqlite_example.lua sqlite_http_template.lua stash_test.lua template_test.lua timer.lua websocket.lua
iniparser
.github
ISSUE_TEMPLATE config.yml
workflows disable-pull-requests.yml trigger-gitlab-ci.yml
cmake JoinPaths.cmake config.cmake.in pc.in
example iniexample.c iniwrite.c parse.c twisted-errors.ini twisted-genhuge.py twisted-ofkey.ini twisted-ofval.ini twisted.ini
src dictionary.c dictionary.h iniparser.c iniparser.h
test
ressources
bad_ini ends_well.ini twisted-errors.ini twisted-ofkey.ini twisted-ofval.ini
good_ini empty.ini spaced.ini spaced2.ini twisted.ini
gruezi.ini old.ini quotes.ini utf8.ini
CMakeLists.txt test_dictionary.c test_iniparser.c unity-config.yml unity_config.h
.cmake-format.py .gitignore .gitlab-ci.yml .gitmessage .travis.yml AUTHORS CMakeLists.txt FAQ-en.md FAQ-zhcn.md INSTALL LICENSE README.md compile_commands.json
jinjac
example CMakeLists.txt example.c
jinjac_test_app CMakeLists.txt jinjac_test_app.c
libjinjac
include jinjac.h
src CMakeLists.txt ast.c ast.h block_statement.c block_statement.h buffer.c buffer.h buildin.c buildin.h common.h convert.c convert.h flex_decl.h jfunction.c jfunction.h jinja_expression.l jinja_expression.y jinjac_parse.c jinjac_parse.h jinjac_stream.c jinjac_stream.h jlist.c jlist.h jobject.c jobject.h parameter.c parameter.h str_obj.c str_obj.h trace.c trace.h
CMakeLists.txt
test .gitignore CMakeLists.txt autotest.rb test_01.expected test_01.jinja test_01b.expected test_01b.jinja test_01c.expected test_01c.jinja test_01d.expected test_01d.jinja test_02.expected test_02.jinja test_03.expected test_03.jinja test_04.expected test_04.jinja test_05.expected test_05.jinja test_06.expected test_06.jinja test_07.expected test_07.jinja test_08.expected test_08.jinja test_08b.expected test_08b.jinja test_09.expected test_09.jinja test_10.expected test_10.jinja test_11.expected test_11.jinja test_12.expected test_12.jinja test_13.expected test_13.jinja test_14.expected test_14.jinja test_15.expected test_15.jinja test_16.expected test_16.jinja test_17.expected test_17.jinja test_18.expected test_18.jinja test_18b.expected test_18b.jinja test_18c.expected test_18c.jinja test_19.expected test_19.jinja test_19b.expected test_19b.jinja test_19c.expected test_19c.jinja test_19d.expected test_19d.jinja test_19e.expected test_19e.jinja test_19f.expected test_19f.jinja test_20.expected test_20.jinja test_21.expected test_21.jinja test_22.expected test_22.jinja test_22a.expected test_22a.jinja test_22b.expected test_22b.jinja test_23.expected test_23.jinja test_24.expected test_24.jinja
.gitignore CMakeLists.txt LICENSE.txt README.md build_coverage.sh build_debug.sh build_release.sh cppcheck_analysis.sh
libev Changes LICENSE Makefile Makefile.am Makefile.in README Symbols.ev Symbols.event aclocal.m4 autogen.sh compile config.guess config.h config.h.in config.status config.sub configure configure.ac depcomp ev++.h ev.3 ev.c ev.h ev.pod ev_epoll.c ev_kqueue.c ev_poll.c ev_port.c ev_select.c ev_vars.h ev_win32.c ev_wrap.h event.c event.h install-sh libev.m4 libtool ltmain.sh missing mkinstalldirs stamp-h1
luajit
doc
img contact.png
bluequad-print.css bluequad.css contact.html ext_buffer.html ext_c_api.html ext_ffi.html ext_ffi_api.html ext_ffi_semantics.html ext_ffi_tutorial.html ext_jit.html ext_profiler.html extensions.html install.html luajit.html running.html
dynasm dasm_arm.h dasm_arm.lua dasm_arm64.h dasm_arm64.lua dasm_mips.h dasm_mips.lua dasm_mips64.lua dasm_ppc.h dasm_ppc.lua dasm_proto.h dasm_x64.lua dasm_x86.h dasm_x86.lua dynasm.lua
etc luajit.1 luajit.pc
src
host .gitignore README buildvm.c buildvm.h buildvm_asm.c buildvm_fold.c buildvm_lib.c buildvm_libbc.h buildvm_peobj.c genlibbc.lua genminilua.lua genversion.lua minilua.c
jit .gitignore bc.lua bcsave.lua dis_arm.lua dis_arm64.lua dis_arm64be.lua dis_mips.lua dis_mips64.lua dis_mips64el.lua dis_mips64r6.lua dis_mips64r6el.lua dis_mipsel.lua dis_ppc.lua dis_x64.lua dis_x86.lua dump.lua p.lua v.lua zone.lua
.gitignore Makefile Makefile.dep lauxlib.h lib_aux.c lib_base.c lib_bit.c lib_buffer.c lib_debug.c lib_ffi.c lib_init.c lib_io.c lib_jit.c lib_math.c lib_os.c lib_package.c lib_string.c lib_table.c lj_alloc.c lj_alloc.h lj_api.c lj_arch.h lj_asm.c lj_asm.h lj_asm_arm.h lj_asm_arm64.h lj_asm_mips.h lj_asm_ppc.h lj_asm_x86.h lj_assert.c lj_bc.c lj_bc.h lj_bcdump.h lj_bcread.c lj_bcwrite.c lj_buf.c lj_buf.h lj_carith.c lj_carith.h lj_ccall.c lj_ccall.h lj_ccallback.c lj_ccallback.h lj_cconv.c lj_cconv.h lj_cdata.c lj_cdata.h lj_char.c lj_char.h lj_clib.c lj_clib.h lj_cparse.c lj_cparse.h lj_crecord.c lj_crecord.h lj_ctype.c lj_ctype.h lj_debug.c lj_debug.h lj_def.h lj_dispatch.c lj_dispatch.h lj_emit_arm.h lj_emit_arm64.h lj_emit_mips.h lj_emit_ppc.h lj_emit_x86.h lj_err.c lj_err.h lj_errmsg.h lj_ff.h lj_ffrecord.c lj_ffrecord.h lj_frame.h lj_func.c lj_func.h lj_gc.c lj_gc.h lj_gdbjit.c lj_gdbjit.h lj_ir.c lj_ir.h lj_ircall.h lj_iropt.h lj_jit.h lj_lex.c lj_lex.h lj_lib.c lj_lib.h lj_load.c lj_mcode.c lj_mcode.h lj_meta.c lj_meta.h lj_obj.c lj_obj.h lj_opt_dce.c lj_opt_fold.c lj_opt_loop.c lj_opt_mem.c lj_opt_narrow.c lj_opt_sink.c lj_opt_split.c lj_parse.c lj_parse.h lj_prng.c lj_prng.h lj_profile.c lj_profile.h lj_record.c lj_record.h lj_serialize.c lj_serialize.h lj_snap.c lj_snap.h lj_state.c lj_state.h lj_str.c lj_str.h lj_strfmt.c lj_strfmt.h lj_strfmt_num.c lj_strscan.c lj_strscan.h lj_tab.c lj_tab.h lj_target.h lj_target_arm.h lj_target_arm64.h lj_target_mips.h lj_target_ppc.h lj_target_x86.h lj_trace.c lj_trace.h lj_traceerr.h lj_udata.c lj_udata.h lj_vm.h lj_vmevent.c lj_vmevent.h lj_vmmath.c ljamalg.c lua.h lua.hpp luaconf.h luajit.c luajit_rolling.h lualib.h msvcbuild.bat nxbuild.bat ps4build.bat ps5build.bat psvitabuild.bat vm_arm.dasc vm_arm64.dasc vm_mips.dasc vm_mips64.dasc vm_ppc.dasc vm_x64.dasc vm_x86.dasc xb1build.bat xedkbuild.bat
.gitattributes .gitignore .relver COPYRIGHT Makefile README
sqlite shell.c sqlite3.c sqlite3.h sqlite3ext.h
wolfssl
.github
ISSUE_TEMPLATE bug_report.yaml other.yaml
actions
install-apt-deps action.yml
scripts
zephyr-4.x external_libc.conf zephyr-test.sh
openssl-ech.sh tls-anvil-test.sh
workflows
disabled haproxy.yml hitch.yml hostap.yml
hostap-files
configs
07c9f183ea744ac04585fb6dd10220c75a5e2e74 hostapd.config tests wpa_supplicant.config
b607d2723e927a3446d89aed813f1aa6068186bb hostapd.config tests wpa_supplicant.config
hostap_2_10 extra.patch hostapd.config tests wpa_supplicant.config
Makefile README dbus-wpa_supplicant.conf
ada.yml arduino.yml async-examples.yml async.yml atecc608-sim.yml bind.yml cmake-autoconf.yml cmake.yml codespell.yml coverity-scan-fixes.yml cryptocb-only.yml curl.yml cyrus-sasl.yml disable-pk-algs.yml docker-Espressif.yml docker-OpenWrt.yml emnet-nonblock.yml fil-c.yml freertos-mem-track.yml gencertbuf.yml grpc.yml haproxy.yml hostap-vm.yml intelasm-c-fallback.yml ipmitool.yml jwt-cpp.yml krb5.yml libspdm.yml libssh2.yml libvncserver.yml linuxkm.yml macos-apple-native-cert-validation.yml mbedtls.sh mbedtls.yml membrowse-comment.yml membrowse-onboard.yml membrowse-report.yml memcached.sh memcached.yml mono.yml mosquitto.yml msmtp.yml msys2.yml multi-arch.yml multi-compiler.yml net-snmp.yml nginx.yml no-malloc.yml no-tls.yml nss.sh nss.yml ntp.yml ocsp.yml openldap.yml openssh.yml openssl-ech.yml opensslcoexist.yml openvpn.yml os-check.yml packaging.yml pam-ipmi.yml pq-all.yml pr-commit-check.yml psk.yml puf.yml python.yml rng-tools.yml rust-wrapper.yml se050-sim.yml smallStackSize.yml socat.yml softhsm.yml sssd.yml stm32-sim.yml stsafe-a120-sim.yml stunnel.yml symbol-prefixes.yml threadx.yml tls-anvil.yml trackmemory.yml watcomc.yml win-csharp-test.yml wolfCrypt-Wconversion.yml wolfboot-integration.yml wolfsm.yml xcode.yml zephyr-4.x.yml zephyr.yml
PULL_REQUEST_TEMPLATE.md SECURITY.md membrowse-targets.json
Docker
OpenWrt Dockerfile README.md runTests.sh
packaging
debian Dockerfile
fedora Dockerfile
wolfCLU Dockerfile README.md
yocto Dockerfile buildAndPush.sh
Dockerfile Dockerfile.cross-compiler README.md buildAndPush.sh include.am run.sh
IDE
ARDUINO
sketches
wolfssl_client README.md
wolfssl_server README.md
wolfssl_version README.md
README.md
Arduino_README_prepend.md README.md include.am keywords.txt library.properties.template wolfssl-arduino.cpp wolfssl-arduino.sh wolfssl.h
AURIX Cpu0_Main.c README.md include.am user_settings.h wolf_main.c
Android Android.bp README.md include.am user_settings.h
CRYPTOCELL README.md include.am main.c user_settings.h
CSBENCH include.am user_settings.h
ECLIPSE
DEOS
deos_wolfssl .options
README.md deos_malloc.c include.am tls_wolfssl.c tls_wolfssl.h user_settings.h
MICRIUM README.md client_wolfssl.c client_wolfssl.h include.am server_wolfssl.c server_wolfssl.h user_settings.h wolfsslRunTests.c
RTTHREAD README.md include.am user_settings.h wolfssl_test.c
SIFIVE README.md include.am
Espressif
ESP-IDF
examples
template
VisualGDB wolfssl_template_IDF_v5.1_ESP32.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include main.h
CMakeLists.txt Kconfig.projbuild component.mk main.c
CMakeLists.txt Makefile README.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp8266
wolfssl_benchmark
VisualGDB wolfssl_benchmark_IDF_v4.4_ESP32.sln wolfssl_benchmark_IDF_v4.4_ESP32.vgdbproj wolfssl_benchmark_IDF_v5_ESP32.sln wolfssl_benchmark_IDF_v5_ESP32.vgdbproj wolfssl_benchmark_IDF_v5_ESP32C3.sln wolfssl_benchmark_IDF_v5_ESP32C3.vgdbproj wolfssl_benchmark_IDF_v5_ESP32S3.sln wolfssl_benchmark_IDF_v5_ESP32S3.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include main.h
CMakeLists.txt Kconfig.projbuild component.mk main.c
CMakeLists.txt Makefile README.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp8266
wolfssl_client
VisualGDB README.md wolfssl_client_IDF_v5_ESP32.sln wolfssl_client_IDF_v5_ESP32.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include client-tls.h main.h time_helper.h wifi_connect.h
CMakeLists.txt Kconfig.projbuild client-tls.c component.mk main.c time_helper.c wifi_connect.c
CMakeLists.txt Makefile README.md README_server_sm.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp32c2 sdkconfig.defaults.esp8266 wolfssl_client_ESP8266.vgdbproj
wolfssl_server
VisualGDB README.md wolfssl_server_IDF_v5_ESP32.sln wolfssl_server_IDF_v5_ESP32.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include main.h server-tls.h time_helper.h wifi_connect.h
CMakeLists.txt Kconfig.projbuild component.mk main.c server-tls.c time_helper.c wifi_connect.c
CMakeLists.txt Makefile README.md README_server_sm.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp32c2 sdkconfig.defaults.esp8266 wolfssl_server_ESP8266.vgdbproj
wolfssl_test
VisualGDB wolfssl_test-IDF_v5_ESP32.sln wolfssl_test-IDF_v5_ESP32.vgdbproj wolfssl_test-IDF_v5_ESP32C3.sln wolfssl_test-IDF_v5_ESP32C3.vgdbproj wolfssl_test-IDF_v5_ESP32C6.sln wolfssl_test-IDF_v5_ESP32C6.vgdbproj wolfssl_test_IDF_v5_ESP32S3.sln wolfssl_test_IDF_v5_ESP32S3.vgdbproj
components
wolfssl
include user_settings.h
CMakeLists.txt Kconfig README.md component.mk
main
include main.h
CMakeLists.txt Kconfig.projbuild component.mk main.c
CMakeLists.txt Makefile README.md partitions_singleapp_large.csv sdkconfig.defaults sdkconfig.defaults.esp32 sdkconfig.defaults.esp32c3 sdkconfig.defaults.esp32c6 sdkconfig.defaults.esp32h2 sdkconfig.defaults.esp32s2 sdkconfig.defaults.esp32s3 sdkconfig.defaults.esp8266 testAll.sh testMonitor.sh wolfssl_test_ESP8266.sln wolfssl_test_ESP8266.vgdbproj
wolfssl_test_idf
VisualGDB VisualGDB_wolfssl_test_idf.sln VisualGDB_wolfssl_test_idf.vgdbproj
main CMakeLists.txt Kconfig.projbuild component.mk main.c main_wip.c.ex time_helper.c time_helper.h
CMakeLists.txt Kconfig.projbuild README.md component.mk sdkconfig.defaults
README.md
libs CMakeLists.txt README.md component.mk tigard.cfg
test CMakeLists.txt README.md component.mk test_wolfssl.c
README.md README_32se.md UPDATE.md compileAllExamples.sh dummy_config_h dummy_test_paths.h setup.sh setup_win.bat user_settings.h
README.md include.am
GCC-ARM
Header user_settings.h
Source armtarget.c benchmark_main.c test_main.c tls_client.c tls_server.c wolf_main.c
Makefile Makefile.bench Makefile.client Makefile.common Makefile.server Makefile.static Makefile.test README.md include.am linker.ld linker_fips.ld
Gaisler-BCC README.md include.am
HEXAGON
DSP Makefile wolfssl_dsp.idl
Makefile README.md build.sh ecc-verify-benchmark.c ecc-verify.c include.am user_settings.h
HEXIWEAR
wolfSSL_HW .cwGeneratedFileSetLog user_settings.h
IAR-EWARM
Projects
benchmark benchmark-main.c current_time.c wolfCrypt-benchmark.ewd wolfCrypt-benchmark.ewp
common minimum-startup.c wolfssl.icf
lib wolfSSL-Lib.ewd wolfSSL-Lib.ewp
test test-main.c wolfCrypt-test.ewd wolfCrypt-test.ewp
user_settings.h wolfssl.eww
embOS
SAMV71_XULT
embOS_SAMV71_XULT_Linker_Script samv71q21_wolfssl.icf
embOS_SAMV71_XULT_user_settings user_settings.h user_settings_simple_example.h user_settings_verbose_example.h
embOS_wolfcrypt_benchmark_SAMV71_XULT
Application runBenchmarks.c
README_wolfcrypt_benchmark wolfcrypt_benchmark.ewd wolfcrypt_benchmark.ewp
embOS_wolfcrypt_lib_SAMV71_XULT README_wolfcrypt_lib wolfcrypt_lib.ewd wolfcrypt_lib.ewp
embOS_wolfcrypt_test_SAMV71_XULT
Application runWolfcryptTests.c
README_wolfcrypt_test wolfcrypt_test.ewd wolfcrypt_test.ewp
README_SAMV71
custom_port
custom_port_Linker_Script samv71q21_wolfssl.icf
custom_port_user_settings user_settings.h
wolfcrypt_benchmark_custom_port
Application runBenchmarks.c
wolfcrypt_test_custom_port
Application runWolfcryptTests.c
README_custom_port
extract_trial_here README_extract_trial_here
README
.gitignore README
IAR-MSP430 Makefile README.md include.am main.c user_settings.h
INTIME-RTOS Makefile README.md include.am libwolfssl.c libwolfssl.vcxproj user_settings.h wolfExamples.c wolfExamples.h wolfExamples.sln wolfExamples.vcxproj wolfssl-lib.sln wolfssl-lib.vcxproj
Infineon README.md include.am user_settings.h
KDS
config user_settings.h
include.am
LINUX-SGX README.md build.sh clean.sh include.am sgx_t_static.mk
LPCXPRESSO
lib_wolfssl lpc_18xx_port.c user_settings.h
wolf_example
src lpc_18xx_startup.c wolfssl_example.c
readme.txt
README.md
M68K
benchmark Makefile main.cpp
testwolfcrypt Makefile main.cpp
Makefile README.md include.am user_settings.h
MCUEXPRESSO
RT1170 fsl_caam_c.patch fsl_caam_h.patch user_settings.h
benchmark
source run_benchmark.c
wolfssl liblinks.xml
README.md include.am user_settings.h wolfcrypt_test.c
MDK-ARM
LPC43xx time-LCP43xx.c
MDK-ARM
wolfSSL Retarget.c cert_data.c cert_data.h config-BARE-METAL.h config-FS.h config-RTX-TCP-FS.h config-WOLFLIB.h main.c shell.c time-CortexM3-4.c time-dummy.c wolfssl_MDK_ARM.c wolfssl_MDK_ARM.h
STM32F2xx_StdPeriph_Lib time-STM32F2xx.c
MDK5-ARM
Conf user_settings.h
Inc wolfssl_MDK_ARM.h
Projects
CryptBenchmark Abstract.txt CryptBenchmark.sct CryptBenchmark.uvoptx CryptBenchmark.uvprojx main.c
CryptTest Abstract.txt CryptTest.sct CryptTest.uvoptx CryptTest.uvprojx main.c
EchoClient Abstract.txt EchoClient.uvoptx EchoClient.uvprojx main.c wolfssl-link.sct
EchoServer Abstract.txt EchoServer.uvoptx EchoServer.uvprojx main.c wolfssl-link.sct
SimpleClient Abstract.txt SimpleClient.uvoptx SimpleClient.uvprojx main.c wolfssl-link.sct
SimpleServer Abstract.txt SimpleServer.uvoptx SimpleServer.uvprojx main.c wolfssl-link.sct
wolfSSL-Full Abstract.txt main.c shell.c time-CortexM3-4.c wolfsslFull.uvoptx wolfsslFull.uvprojx
wolfSSL-Lib Abstract.txt wolfSSL-Lib.uvoptx wolfSSL-Lib.uvprojx
Src ssl-dummy.c
README.md include.am
MPLABX16
wolfcrypt_test.X
nbproject
private configurations.xml private.xml
configurations.xml include.am project.xml
Makefile
wolfssl.X
nbproject configurations.xml include.am project.xml
Makefile
README.md include.am main.c user_settings.h
MQX Makefile README-jp.md README.md client-tls.c include.am server-tls.c user_config.h user_settings.h
MSVS-2019-AZSPHERE
client client.c client.h
server server.c server.h
shared util.h
wolfssl_new_azsphere
HardwareDefinitions
avnet_mt3620_sk
inc
hw template_appliance.h
template_appliance.json
mt3620_rdb
inc
hw template_appliance.h
template_appliance.json
seeed_mt3620_mdb
inc
hw template_appliance.h
template_appliance.json
.gitignore CMakeLists.txt CMakeSettings.json app_manifest.json applibs_versions.h launch.vs.json main.c
README.md include.am user_settings.h
MYSQL CMakeLists_wolfCrypt.txt CMakeLists_wolfSSL.txt do.sh
NDS README.md
NETOS Makefile.wolfcrypt.inc README.md include.am user_settings.h user_settings.h-cert2425 user_settings.h-cert3389 wolfssl_netos_custom.c
OPENSTM32 README.md
PlatformIO
examples
wolfssl_benchmark
include README main.h
lib README
src CMakeLists.txt main.c
test README
CMakeLists.txt README.md platformio.ini sdkconfig.defaults wolfssl_benchmark.code-workspace
wolfssl_test
include README main.h
lib README
src CMakeLists.txt main.c
test README
CMakeLists.txt README.md platformio.ini sdkconfig.defaults wolfssl_test.code-workspace
README.md wolfssl_platformio.code-workspace
README.md include.am
QNX
CAAM-DRIVER Makefile
example-client Makefile client-tls.c
example-cmac Makefile cmac-test.c
example-server Makefile server-tls.c
testwolfcrypt Makefile
wolfssl Makefile user_settings.h
README.md include.am
RISCV
SIFIVE-HIFIVE1 Makefile README.md include.am main.c user_settings.h
SIFIVE-UNLEASHED README.md include.am
include.am
ROWLEY-CROSSWORKS-ARM Kinetis_FlashPlacement.xml README.md arm_startup.c benchmark_main.c hw.h include.am kinetis_hw.c retarget.c test_main.c user_settings.h wolfssl.hzp wolfssl_ltc.hzp
Renesas
cs+
Projects
common strings.h unistd.h user_settings.h wolfssl_dummy.c
t4_demo README_en.txt README_jp.txt t4_demo.mtpj wolf_client.c wolf_demo.h wolf_main.c wolf_server.c
test test.mtpj test_main.c
wolfssl_lib wolfssl_lib.mtpj
README include.am
e2studio
DK-S7G2
benchmark-template
src app_entry.c
example_server-template
src app_entry.c
wolfcrypttest-template
src app_entry.c
wolfssl-template-project configuration.xml
README.md include.am user_settings.h
Projects
common strings.h unistd.h user_settings.h wolfssl_dummy.c
test
src key_data.c key_data.h test_main.c wolf_client.c wolf_server.c wolfssl_demo.h
tools generate_rsa_keypair.sh genhexbuf.pl rsa_pss_sign.sh
wolfssl
src .gitkeep
wolfcrypt
src .gitkeep
README include.am
RA6M3
benchmark-wolfcrypt
common .gitkeep
script .gitkeep
src wolfssl_thread_entry.c
client-wolfssl
common
src .gitkeep
script .gitkeep
src wolfssl_thread_entry.c
wolfssl_thread_entry.h
common
ra6m3g README.md
src freertos_tcp_port.c
user_settings.h util.h
server-wolfssl
common
src .gitkeep
script .gitkeep
src wolfssl_thread_entry.c
wolfssl_thread_entry.h
test-wolfcrypt
common .gitkeep
script .gitkeep
src wolfssl_thread_entry.c
wolfssl
src .gitkeep
wolfcrypt .gitkeep
README.md README_APRA6M_en.md README_APRA6M_jp.md include.am
RA6M3G README.md
RA6M4
common user_settings.h wolfssl_demo.h
test
key_data key_data.h key_data_sce.c
src
SEGGER_RTT myprint.c
common .gitignore
test_main.c wolf_client.c wolfssl_sce_unit_test.c
test_RA6M4Debug.launch
tools
example_keys generate_SignedCA.sh rsa_private.pem rsa_public.pem
README.md
README.md include.am
RX65N
GR-ROSE
common strings.h unistd.h user_settings.h wolfssl_dummy.c
smc smc.scfg
test
src key_data.c key_data.h test_main.c wolf_client.c wolf_server.c wolfssl_demo.h
test.rcpc test_HardwareDebug.launch
tools
example_keys generate_SignedCA.sh rsa_private.pem rsa_public.pem
README.md
wolfssl wolfssl.rcpc
README_EN.md README_JP.md include.am
RSK
resource section.esi
wolfssl wolfssl.rcpc
wolfssl_demo key_data.c key_data.h user_settings.h wolfssl_demo.c wolfssl_demo.h
InstructionManualForExample_RSK+RX65N-2MB_EN.pdf InstructionManualForExample_RSK+RX65N-2MB_JP.pdf README_EN.md README_JP.md include.am
RX72N
EnvisionKit
Simple
common sectioninfo.esi wolfssl_dummy.c
test
src
client simple_tcp_client.c simple_tls_tsip_client.c
server simple_tcp_server.c simple_tls_server.c
test_main.c wolfssl_simple_demo.h
test.rcpc test.scfg test_HardwareDebug.launch
wolfssl wolfssl.rcpc
README_EN.md README_JP.md
resource section.esi
tools
example_keys generate_SignedCA.sh rsa_private.pem rsa_public.pem
README.md
wolfssl wolfssl.rcpc
wolfssl_demo key_data.c key_data.h user_settings.h wolfssl_demo.c wolfssl_demo.h wolfssl_tsip_unit_test.c
InstructionManualForExample_RX72N_EnvisonKit_EN.pdf InstructionManualForExample_RX72N_EnvisonKit_JP.pdf README_EN.md README_JP.md include.am
RZN2L
common user_settings.h wolfssl_demo.h
test
src
serial_io app_print.c
test wolf_client.c wolf_server.c wolfssl_rsip_unit_test.c
wolfCrypt .gitignore
wolfSSL .gitignore
local_system_init.c rzn2l_tst_thread_entry.c wolfssl_dummy.c
README.md include.am
SK-S7G2
common user_settings.h
wolfssl_lib configuration.xml
.gitignore README.md include.am
STARCORE README.txt include.am starcore_test.c user_settings.h
STM32Cube README.md STM32_Benchmarks.md default_conf.ftl include.am main.c wolfssl_example.c wolfssl_example.h
SimplicityStudio README.md include.am test_wolf.c user_settings.h
TRUESTUDIO
wolfssl user_settings.h
README include.am
VS-ARM README.md include.am user_settings.h wolfssl.sln wolfssl.vcxproj
VS-AZURE-SPHERE
client app_manifest.json client.c client.h client.vcxproj
server app_manifest.json server.c server.h server.vcxproj
shared util.h
wolfcrypt_test app_manifest.json wolfcrypt_test.vcxproj
README.md include.am user_settings.h wolfssl.sln wolfssl.vcxproj
VisualDSP include.am user_settings.h wolf_tasks.c
WICED-STUDIO README include.am user_settings.h
WIN README.txt include.am test.vcxproj user_settings.h user_settings_dtls.h wolfssl-fips.sln wolfssl-fips.vcxproj
WIN-SGX ReadMe.txt include.am wolfSSL_SGX.edl wolfSSL_SGX.sln wolfSSL_SGX.vcxproj
WIN-SRTP-KDF-140-3 README.txt include.am resource.h test.vcxproj user_settings.h wolfssl-fips.rc wolfssl-fips.sln wolfssl-fips.vcxproj
WIN10 README.txt include.am resource.h test.vcxproj user_settings.h wolfssl-fips.rc wolfssl-fips.sln wolfssl-fips.vcxproj
WINCE README.md include.am user_settings.h user_settings.h.140-2-deprecated
WORKBENCH README.md include.am
XCODE
Benchmark
wolfBench
Assets.xcassets
AppIcon.appiconset Contents.json
Base.lproj LaunchScreen.storyboard Main.storyboard
AppDelegate.h AppDelegate.m Info.plist ViewController.h ViewController.m main.m
wolfBench.xcodeproj project.pbxproj
include.am
wolfssl-FIPS.xcodeproj project.pbxproj
wolfssl.xcodeproj project.pbxproj
wolfssl_testsuite.xcodeproj project.pbxproj
README.md build-for-i386.sh include.am user_settings.h
XCODE-FIPSv2
macOS-C++
Intel user_settings.h
M1 user_settings.h
include.am user_settings.h
XCODE-FIPSv5 README include.am user_settings.h
XCODE-FIPSv6 README include.am user_settings.h
XilinxSDK
2018_2 lscript.ld
2019_2
wolfCrypt_example
src lscript.ld
wolfCrypt_example_system wolfCrypt_example_system.sprj
2022_1
wolfCrypt_FreeRTOS_example wolfCrypt_FreeRTOS_example.prj
wolfCrypt_FreeRTOS_example_system wolfCrypt_FreeRTOS_example_system.sprj
wolfCrypt_example wolfCrypt_example.prj
wolfCrypt_example_system wolfCrypt_example_system.sprj
.gitignore
README.md bench.sh combine.sh eclipse_formatter_profile.xml graph.sh include.am user_settings.h wolfssl_example.c
apple-universal
wolfssl-multiplatform
wolfssl-multiplatform
Assets.xcassets
AccentColor.colorset Contents.json
AppIcon.appiconset Contents.json
Contents.json
ContentView.swift simple_client_example.c simple_client_example.h wolfssl-multiplatform-Bridging-Header.h wolfssl_multiplatform.entitlements wolfssl_multiplatformApp.swift wolfssl_test_driver.c wolfssl_test_driver.h
wolfssl-multiplatform.xcodeproj project.pbxproj
.gitignore README.md build-wolfssl-framework.sh include.am
iotsafe Makefile README.md ca-cert.c devices.c devices.h include.am main.c memory-tls.c startup.c target.ld user_settings.h
iotsafe-raspberrypi Makefile README.md client-tls13.c include.am main.c
mynewt README.md apps.wolfcrypttest.pkg.yml crypto.wolfssl.pkg.yml crypto.wolfssl.syscfg.yml include.am setup.sh
zephyr README.md include.am
include.am
RTOS
nuttx
wolfssl .gitignore Kconfig Make.defs Makefile README.md setup-wolfssl.sh user_settings.h
include.am
bsdkm Makefile README.md bsdkm_wc_port.h include.am wolfkmod.c wolfkmod_aes.c x86_vecreg.c
certs
1024 ca-cert.der ca-cert.pem ca-key.der ca-key.pem client-cert.der client-cert.pem client-key.der client-key.pem client-keyPub.der dh1024.der dh1024.pem dsa-pub-1024.pem dsa1024.der dsa1024.pem include.am rsa1024.der server-cert.der server-cert.pem server-key.der server-key.pem
3072 client-cert.der client-cert.pem client-key.der client-key.pem client-keyPub.der include.am
4096 client-cert.der client-cert.pem client-key.der client-key.pem client-keyPub.der include.am
acert
rsa_pss acert.pem acert_ietf.pem acert_ietf_pubkey.pem acert_pubkey.pem
acert.pem acert_ietf.pem acert_ietf_pubkey.pem acert_pubkey.pem include.am
aia ca-issuers-cert.pem multi-aia-cert.pem overflow-aia-cert.pem
crl
extra-crls ca-int-cert-revoked.pem claim-root.pem crl_critical_entry.pem crlnum_57oct.pem crlnum_64oct.pem general-server-crl.pem large_crlnum.pem large_crlnum2.pem
hash_der 0fdb2da4.r0
hash_pem 0fdb2da4.r0
bad_time_fmt.pem ca-int-ecc.pem ca-int.pem ca-int2-ecc.pem ca-int2.pem caEcc384Crl.pem caEccCrl.der caEccCrl.pem cliCrl.pem client-int-ecc.pem client-int.pem crl.der crl.pem crl.revoked crl2.der crl2.pem crl_reason.pem crl_rsapss.pem eccCliCRL.pem eccSrvCRL.pem gencrls.sh include.am server-goodaltCrl.pem server-goodaltwildCrl.pem server-goodcnCrl.pem server-goodcnwildCrl.pem server-int-ecc.pem server-int.pem wolfssl.cnf
dilithium bench_dilithium_level2_key.der bench_dilithium_level3_key.der bench_dilithium_level5_key.der include.am
ecc bp256r1-key.der bp256r1-key.pem ca-secp256k1-cert.pem ca-secp256k1-key.pem client-bp256r1-cert.der client-bp256r1-cert.pem client-secp256k1-cert.der client-secp256k1-cert.pem genecc.sh include.am secp256k1-key.der secp256k1-key.pem secp256k1-param.pem secp256k1-privkey.der secp256k1-privkey.pem server-bp256r1-cert.der server-bp256r1-cert.pem server-secp256k1-cert.der server-secp256k1-cert.pem server2-secp256k1-cert.der server2-secp256k1-cert.pem wolfssl.cnf wolfssl_384.cnf
ed25519 ca-ed25519-key.der ca-ed25519-key.pem ca-ed25519-priv.der ca-ed25519-priv.pem ca-ed25519.der ca-ed25519.pem client-ed25519-key.der client-ed25519-key.pem client-ed25519-priv.der client-ed25519-priv.pem client-ed25519.der client-ed25519.pem eddsa-ed25519.der eddsa-ed25519.pem gen-ed25519-certs.sh gen-ed25519-keys.sh gen-ed25519.sh include.am root-ed25519-key.der root-ed25519-key.pem root-ed25519-priv.der root-ed25519-priv.pem root-ed25519.der root-ed25519.pem server-ed25519-cert.pem server-ed25519-key.der server-ed25519-key.pem server-ed25519-priv.der server-ed25519-priv.pem server-ed25519.der server-ed25519.pem
ed448 ca-ed448-key.der ca-ed448-key.pem ca-ed448-priv.der ca-ed448-priv.pem ca-ed448.der ca-ed448.pem client-ed448-key.der client-ed448-key.pem client-ed448-priv.der client-ed448-priv.pem client-ed448.der client-ed448.pem gen-ed448-certs.sh gen-ed448-keys.sh include.am root-ed448-key.der root-ed448-key.pem root-ed448-priv.der root-ed448-priv.pem root-ed448.der root-ed448.pem server-ed448-cert.pem server-ed448-key.der server-ed448-key.pem server-ed448-priv.der server-ed448-priv.pem server-ed448.der server-ed448.pem
external DigiCertGlobalRootCA.pem README.txt ca-digicert-ev.pem ca-globalsign-root.pem ca-google-root.pem ca_collection.pem include.am
falcon bench_falcon_level1_key.der bench_falcon_level5_key.der include.am
intermediate
ca_false_intermediate gentestcert.sh int_ca.key server.key test_ca.key test_ca.pem test_int_not_cacert.pem test_sign_bynoca_srv.pem wolfssl_base.conf wolfssl_srv.conf
ca-ecc-bad-aki.der ca-ecc-bad-aki.pem ca-int-cert.der ca-int-cert.pem ca-int-ecc-cert.der ca-int-ecc-cert.pem ca-int-ecc-key.der ca-int-ecc-key.pem ca-int-key.der ca-int-key.pem ca-int2-cert.der ca-int2-cert.pem ca-int2-ecc-cert.der ca-int2-ecc-cert.pem ca-int2-ecc-key.der ca-int2-ecc-key.pem ca-int2-key.der ca-int2-key.pem client-chain-alt-ecc.pem client-chain-alt.pem client-chain-ecc.der client-chain-ecc.pem client-chain.der client-chain.pem client-int-cert.der client-int-cert.pem client-int-ecc-cert.der client-int-ecc-cert.pem genintcerts.sh include.am server-chain-alt-ecc.pem server-chain-alt.pem server-chain-ecc.der server-chain-ecc.pem server-chain-short.pem server-chain.der server-chain.pem server-int-cert.der server-int-cert.pem server-int-ecc-cert.der server-int-ecc-cert.pem
lms bc_hss_L2_H5_W8_root.der bc_hss_L3_H5_W4_root.der bc_lms_chain_ca.der bc_lms_chain_leaf.der bc_lms_native_bc_root.der bc_lms_sha256_h10_w8_root.der bc_lms_sha256_h5_w4_root.der include.am
mldsa README.txt include.am mldsa44-cert.der mldsa44-cert.pem mldsa44-key.pem mldsa44_bare-priv.der mldsa44_bare-seed.der mldsa44_oqskeypair.der mldsa44_priv-only.der mldsa44_pub-spki.der mldsa44_seed-only.der mldsa44_seed-priv.der mldsa65-cert.der mldsa65-cert.pem mldsa65-key.pem mldsa65_bare-priv.der mldsa65_bare-seed.der mldsa65_oqskeypair.der mldsa65_priv-only.der mldsa65_pub-spki.der mldsa65_seed-only.der mldsa65_seed-priv.der mldsa87-cert.der mldsa87-cert.pem mldsa87-key.pem mldsa87_bare-priv.der mldsa87_bare-seed.der mldsa87_oqskeypair.der mldsa87_priv-only.der mldsa87_pub-spki.der mldsa87_seed-only.der mldsa87_seed-priv.der
ocsp imposter-root-ca-cert.der imposter-root-ca-cert.pem imposter-root-ca-key.der imposter-root-ca-key.pem include.am index-ca-and-intermediate-cas.txt index-ca-and-intermediate-cas.txt.attr index-intermediate1-ca-issued-certs.txt index-intermediate1-ca-issued-certs.txt.attr index-intermediate2-ca-issued-certs.txt index-intermediate2-ca-issued-certs.txt.attr index-intermediate3-ca-issued-certs.txt index-intermediate3-ca-issued-certs.txt.attr intermediate1-ca-cert.der intermediate1-ca-cert.pem intermediate1-ca-key.der intermediate1-ca-key.pem intermediate2-ca-cert.der intermediate2-ca-cert.pem intermediate2-ca-key.der intermediate2-ca-key.pem intermediate3-ca-cert.der intermediate3-ca-cert.pem intermediate3-ca-key.der intermediate3-ca-key.pem ocsp-responder-cert.der ocsp-responder-cert.pem ocsp-responder-key.der ocsp-responder-key.pem openssl.cnf renewcerts-for-test.sh renewcerts.sh root-ca-cert.der root-ca-cert.pem root-ca-crl.pem root-ca-key.der root-ca-key.pem server1-cert.der server1-cert.pem server1-chain-noroot.pem server1-key.der server1-key.pem server2-cert.der server2-cert.pem server2-key.der server2-key.pem server3-cert.der server3-cert.pem server3-key.der server3-key.pem server4-cert.der server4-cert.pem server4-key.der server4-key.pem server5-cert.der server5-cert.pem server5-key.der server5-key.pem test-leaf-response.der test-multi-response.der test-response-nointern.der test-response-rsapss.der test-response.der
p521 ca-p521-key.der ca-p521-key.pem ca-p521-priv.der ca-p521-priv.pem ca-p521.der ca-p521.pem client-p521-key.der client-p521-key.pem client-p521-priv.der client-p521-priv.pem client-p521.der client-p521.pem gen-p521-certs.sh gen-p521-keys.sh include.am root-p521-key.der root-p521-key.pem root-p521-priv.der root-p521-priv.pem root-p521.der root-p521.pem server-p521-cert.pem server-p521-key.der server-p521-key.pem server-p521-priv.der server-p521-priv.pem server-p521.der server-p521.pem
renewcerts wolfssl.cnf
rpk client-cert-rpk.der client-ecc-cert-rpk.der include.am server-cert-rpk.der server-ecc-cert-rpk.der
rsapss ca-3072-rsapss-key.der ca-3072-rsapss-key.pem ca-3072-rsapss-priv.der ca-3072-rsapss-priv.pem ca-3072-rsapss.der ca-3072-rsapss.pem ca-rsapss-key.der ca-rsapss-key.pem ca-rsapss-priv.der ca-rsapss-priv.pem ca-rsapss.der ca-rsapss.pem client-3072-rsapss-key.der client-3072-rsapss-key.pem client-3072-rsapss-priv.der client-3072-rsapss-priv.pem client-3072-rsapss.der client-3072-rsapss.pem client-rsapss-key.der client-rsapss-key.pem client-rsapss-priv.der client-rsapss-priv.pem client-rsapss.der client-rsapss.pem gen-rsapss-keys.sh include.am renew-rsapss-certs.sh root-3072-rsapss-key.der root-3072-rsapss-key.pem root-3072-rsapss-priv.der root-3072-rsapss-priv.pem root-3072-rsapss.der root-3072-rsapss.pem root-rsapss-key.der root-rsapss-key.pem root-rsapss-priv.der root-rsapss-priv.pem root-rsapss.der root-rsapss.pem server-3072-rsapss-cert.pem server-3072-rsapss-key.der server-3072-rsapss-key.pem server-3072-rsapss-priv.der server-3072-rsapss-priv.pem server-3072-rsapss.der server-3072-rsapss.pem server-mix-rsapss-cert.pem server-rsapss-cert.pem server-rsapss-key.der server-rsapss-key.pem server-rsapss-priv.der server-rsapss-priv.pem server-rsapss.der server-rsapss.pem
sia timestamping-sia-cert.pem
slhdsa bench_slhdsa_sha2_128f_key.der bench_slhdsa_sha2_128s_key.der bench_slhdsa_sha2_192f_key.der bench_slhdsa_sha2_192s_key.der bench_slhdsa_sha2_256f_key.der bench_slhdsa_sha2_256s_key.der bench_slhdsa_shake128f_key.der bench_slhdsa_shake128s_key.der bench_slhdsa_shake192f_key.der bench_slhdsa_shake192s_key.der bench_slhdsa_shake256f_key.der bench_slhdsa_shake256s_key.der client-mldsa44-priv.pem client-mldsa44-sha2.der client-mldsa44-sha2.pem client-mldsa44-shake.der client-mldsa44-shake.pem gen-slhdsa-mldsa-certs.sh include.am root-slhdsa-sha2-128s-priv.der root-slhdsa-sha2-128s-priv.pem root-slhdsa-sha2-128s.der root-slhdsa-sha2-128s.pem root-slhdsa-shake-128s-priv.der root-slhdsa-shake-128s-priv.pem root-slhdsa-shake-128s.der root-slhdsa-shake-128s.pem server-mldsa44-priv.pem server-mldsa44-sha2.der server-mldsa44-sha2.pem server-mldsa44-shake.der server-mldsa44-shake.pem
sm2 ca-sm2-key.der ca-sm2-key.pem ca-sm2-priv.der ca-sm2-priv.pem ca-sm2.der ca-sm2.pem client-sm2-key.der client-sm2-key.pem client-sm2-priv.der client-sm2-priv.pem client-sm2.der client-sm2.pem fix_sm2_spki.py gen-sm2-certs.sh gen-sm2-keys.sh include.am root-sm2-key.der root-sm2-key.pem root-sm2-priv.der root-sm2-priv.pem root-sm2.der root-sm2.pem self-sm2-cert.pem self-sm2-key.pem self-sm2-priv.pem server-sm2-cert.der server-sm2-cert.pem server-sm2-key.der server-sm2-key.pem server-sm2-priv.der server-sm2-priv.pem server-sm2.der server-sm2.pem
statickeys dh-ffdhe2048-params.pem dh-ffdhe2048-pub.der dh-ffdhe2048-pub.pem dh-ffdhe2048.der dh-ffdhe2048.pem ecc-secp256r1.der ecc-secp256r1.pem gen-static.sh include.am x25519-pub.der x25519-pub.pem x25519.der x25519.pem
test
expired expired-ca.der expired-ca.pem expired-cert.der expired-cert.pem
catalog.txt cert-bad-neg-int.der cert-bad-oid.der cert-bad-utf8.der cert-ext-ia.cfg cert-ext-ia.der cert-ext-ia.pem cert-ext-joi.cfg cert-ext-joi.der cert-ext-joi.pem cert-ext-mnc.der cert-ext-multiple.cfg cert-ext-multiple.der cert-ext-multiple.pem cert-ext-nc-combined.der cert-ext-nc-combined.pem cert-ext-nc.cfg cert-ext-nc.der cert-ext-nc.pem cert-ext-ncdns.der cert-ext-ncdns.pem cert-ext-ncip.der cert-ext-ncip.pem cert-ext-ncmixed.der cert-ext-ncmulti.der cert-ext-ncmulti.pem cert-ext-ncrid.der cert-ext-ncrid.pem cert-ext-nct.cfg cert-ext-nct.der cert-ext-nct.pem cert-ext-ndir-exc.cfg cert-ext-ndir-exc.der cert-ext-ndir-exc.pem cert-ext-ndir.cfg cert-ext-ndir.der cert-ext-ndir.pem cert-ext-ns.der cert-over-max-altnames.cfg cert-over-max-altnames.der cert-over-max-altnames.pem cert-over-max-nc.cfg cert-over-max-nc.der cert-over-max-nc.pem client-ecc-cert-ski.hex cn-ip-literal.der cn-ip-wildcard.der crit-cert.pem crit-key.pem dh1024.der dh1024.pem dh512.der dh512.pem digsigku.pem encrypteddata.msg gen-badsig.sh gen-ext-certs.sh gen-testcerts.sh include.am kari-keyid-cms.msg ktri-keyid-cms.msg ossl-trusted-cert.pem server-badaltname.der server-badaltname.pem server-badaltnull.der server-badaltnull.pem server-badcn.der server-badcn.pem server-badcnnull.der server-badcnnull.pem server-cert-ecc-badsig.der server-cert-ecc-badsig.pem server-cert-rsa-badsig.der server-cert-rsa-badsig.pem server-duplicate-policy.pem server-garbage.der server-garbage.pem server-goodalt.der server-goodalt.pem server-goodaltwild.der server-goodaltwild.pem server-goodcn.der server-goodcn.pem server-goodcnwild.der server-goodcnwild.pem server-localhost.der server-localhost.pem smime-test-canon.p7s smime-test-multipart-badsig.p7s smime-test-multipart.p7s smime-test.p7s
test-pathlen assemble-chains.sh chainA-ICA1-key.pem chainA-ICA1-pathlen0.pem chainA-assembled.pem chainA-entity-key.pem chainA-entity.pem chainB-ICA1-key.pem chainB-ICA1-pathlen0.pem chainB-ICA2-key.pem chainB-ICA2-pathlen1.pem chainB-assembled.pem chainB-entity-key.pem chainB-entity.pem chainC-ICA1-key.pem chainC-ICA1-pathlen1.pem chainC-assembled.pem chainC-entity-key.pem chainC-entity.pem chainD-ICA1-key.pem chainD-ICA1-pathlen127.pem chainD-assembled.pem chainD-entity-key.pem chainD-entity.pem chainE-ICA1-key.pem chainE-ICA1-pathlen128.pem chainE-assembled.pem chainE-entity-key.pem chainE-entity.pem chainF-ICA1-key.pem chainF-ICA1-pathlen1.pem chainF-ICA2-key.pem chainF-ICA2-pathlen0.pem chainF-assembled.pem chainF-entity-key.pem chainF-entity.pem chainG-ICA1-key.pem chainG-ICA1-pathlen0.pem chainG-ICA2-key.pem chainG-ICA2-pathlen1.pem chainG-ICA3-key.pem chainG-ICA3-pathlen99.pem chainG-ICA4-key.pem chainG-ICA4-pathlen5.pem chainG-ICA5-key.pem chainG-ICA5-pathlen20.pem chainG-ICA6-key.pem chainG-ICA6-pathlen10.pem chainG-ICA7-key.pem chainG-ICA7-pathlen100.pem chainG-assembled.pem chainG-entity-key.pem chainG-entity.pem chainH-ICA1-key.pem chainH-ICA1-pathlen0.pem chainH-ICA2-key.pem chainH-ICA2-pathlen2.pem chainH-ICA3-key.pem chainH-ICA3-pathlen2.pem chainH-ICA4-key.pem chainH-ICA4-pathlen2.pem chainH-assembled.pem chainH-entity-key.pem chainH-entity.pem chainI-ICA1-key.pem chainI-ICA1-no_pathlen.pem chainI-ICA2-key.pem chainI-ICA2-no_pathlen.pem chainI-ICA3-key.pem chainI-ICA3-pathlen2.pem chainI-assembled.pem chainI-entity-key.pem chainI-entity.pem chainJ-ICA1-key.pem chainJ-ICA1-no_pathlen.pem chainJ-ICA2-key.pem chainJ-ICA2-no_pathlen.pem chainJ-ICA3-key.pem chainJ-ICA3-no_pathlen.pem chainJ-ICA4-key.pem chainJ-ICA4-pathlen2.pem chainJ-assembled.pem chainJ-entity-key.pem chainJ-entity.pem include.am refreshkeys.sh
test-serial0 ee_normal.pem ee_serial0.pem generate_certs.sh include.am intermediate_serial0.pem root_serial0.pem root_serial0_key.pem selfsigned_nonca_serial0.pem
xmss bc_xmss_chain_ca.der bc_xmss_chain_leaf.der bc_xmss_sha2_10_256_root.der bc_xmss_sha2_16_256_root.der bc_xmssmt_sha2_20_2_256_root.der bc_xmssmt_sha2_20_4_256_root.der bc_xmssmt_sha2_40_8_256_root.der include.am
ca-cert-chain.der ca-cert.der ca-cert.pem ca-ecc-cert.der ca-ecc-cert.pem ca-ecc-key.der ca-ecc-key.pem ca-ecc384-cert.der ca-ecc384-cert.pem ca-ecc384-key.der ca-ecc384-key.pem ca-key-pkcs8-attribute.der ca-key.der ca-key.pem check_dates.sh client-absolute-urn.pem client-ca-cert.der client-ca-cert.pem client-ca.pem client-cert-ext.der client-cert-ext.pem client-cert.der client-cert.pem client-crl-dist.der client-crl-dist.pem client-ecc-ca-cert.der client-ecc-ca-cert.pem client-ecc-cert.der client-ecc-cert.pem client-ecc384-cert.der client-ecc384-cert.pem client-ecc384-key.der client-ecc384-key.pem client-key.der client-key.pem client-keyEnc.pem client-keyPub.der client-keyPub.pem client-relative-uri.pem client-uri-cert.pem csr.attr.der csr.dsa.der csr.dsa.pem csr.ext.der csr.signed.der dh-priv-2048.der dh-priv-2048.pem dh-pub-2048.der dh-pub-2048.pem dh-pubkey-2048.der dh2048.der dh2048.pem dh3072.der dh3072.pem dh4096.der dh4096.pem dsa-pubkey-2048.der dsa2048.der dsa2048.pem dsa3072.der dsaparams.der dsaparams.pem ecc-client-key.der ecc-client-key.pem ecc-client-keyPub.der ecc-client-keyPub.pem ecc-key-comp.pem ecc-keyPkcs8.der ecc-keyPkcs8.pem ecc-keyPkcs8Enc.der ecc-keyPkcs8Enc.pem ecc-keyPub.der ecc-keyPub.pem ecc-params.der ecc-params.pem ecc-privOnlyCert.pem ecc-privOnlyKey.pem ecc-privkey.der ecc-privkey.pem ecc-privkeyPkcs8.der ecc-privkeyPkcs8.pem ecc-rsa-server.p12 empty-issuer-cert.pem entity-no-ca-bool-cert.pem entity-no-ca-bool-key.pem fpki-cert.der fpki-certpol-cert.der gen_revoked.sh include.am renewcerts.sh rid-cert.der rsa-pub-2048.pem rsa2048.der rsa3072.der server-cert-chain.der server-cert.der server-cert.pem server-ecc-comp.der server-ecc-comp.pem server-ecc-rsa.der server-ecc-rsa.pem server-ecc-self.der server-ecc-self.pem server-ecc.der server-ecc.pem server-ecc384-cert.der server-ecc384-cert.pem server-ecc384-key.der server-ecc384-key.pem server-key.der server-key.pem server-keyEnc.pem server-keyPkcs8.der server-keyPkcs8.pem server-keyPkcs8Enc.der server-keyPkcs8Enc.pem server-keyPkcs8Enc12.pem server-keyPkcs8Enc2.pem server-keyPub.der server-keyPub.pem server-revoked-cert.pem server-revoked-key.pem taoCert.txt test-ber-exp02-05-2022.p7b test-degenerate.p7b test-multiple-recipients.p7b test-servercert-rc2.p12 test-servercert.p12 test-stream-dec.p7b test-stream-sign.p7b wolfssl-website-ca.pem x942dh2048.der x942dh2048.pem
cmake
consumer CMakeLists.txt README.md main.c
modules FindARIA.cmake FindOQS.cmake
Config.cmake.in README.md config.in functions.cmake include.am options.h.in wolfssl-config-version.cmake.in wolfssl-targets.cmake.in
debian
source format
changelog.in control.in copyright include.am libwolfssl-dev.install libwolfssl.install rules.in
doc
dox_comments
header_files aes.h arc4.h ascon.h asn.h asn_public.h blake2.h bn.h camellia.h chacha.h chacha20_poly1305.h cmac.h coding.h compress.h cryptocb.h curve25519.h curve448.h des3.h dh.h doxygen_groups.h doxygen_pages.h dsa.h ecc.h eccsi.h ed25519.h ed448.h error-crypt.h evp.h hash.h hmac.h iotsafe.h kdf.h logging.h md2.h md4.h md5.h memory.h ocsp.h pem.h pkcs11.h pkcs7.h poly1305.h psa.h puf.h pwdbased.h quic.h random.h ripemd.h rsa.h sakke.h sha.h sha256.h sha3.h sha512.h signature.h siphash.h srp.h ssl.h tfm.h types.h wc_encrypt.h wc_port.h wc_she.h wc_slhdsa.h wolfio.h
header_files-ja aes.h arc4.h ascon.h asn.h asn_public.h blake2.h bn.h camellia.h chacha.h chacha20_poly1305.h cmac.h coding.h compress.h cryptocb.h curve25519.h curve448.h des3.h dh.h doxygen_groups.h doxygen_pages.h dsa.h ecc.h eccsi.h ed25519.h ed448.h error-crypt.h evp.h hash.h hmac.h iotsafe.h kdf.h logging.h md2.h md4.h md5.h memory.h ocsp.h pem.h pkcs11.h pkcs7.h poly1305.h psa.h pwdbased.h quic.h random.h ripemd.h rsa.h sakke.h sha.h sha256.h sha3.h sha512.h signature.h siphash.h srp.h ssl.h tfm.h types.h wc_encrypt.h wc_port.h wolfio.h
formats
html
html_changes
search search.css search.js
customdoxygen.css doxygen.css menu.js menudata.js tabs.css
Doxyfile footer.html header.html mainpage.dox
pdf Doxyfile header.tex
images wolfssl_logo.png
QUIC.md README.txt README_DOXYGEN check_api.sh generate_documentation.sh include.am
examples
asn1 asn1.c dumpasn1.cfg gen_oid_names.rb include.am oid_names.h
async Makefile README.md async_client.c async_server.c async_tls.c async_tls.h include.am user_settings.h
benchmark include.am tls_bench.c tls_bench.h
client client.c client.h client.sln client.vcproj client.vcxproj include.am
configs README.md include.am user_settings_EBSnet.h user_settings_all.h user_settings_arduino.h user_settings_baremetal.h user_settings_ca.h user_settings_curve25519nonblock.h user_settings_dtls13.h user_settings_eccnonblock.h user_settings_espressif.h user_settings_fipsv2.h user_settings_fipsv5.h user_settings_min_ecc.h user_settings_openssl_compat.h user_settings_pkcs7.h user_settings_platformio.h user_settings_pq.h user_settings_rsa_only.h user_settings_stm32.h user_settings_template.h user_settings_tls12.h user_settings_tls13.h user_settings_wolfboot_keytools.h user_settings_wolfssh.h user_settings_wolftpm.h
crypto_policies
default wolfssl.txt
future wolfssl.txt
legacy wolfssl.txt
echoclient echoclient.c echoclient.h echoclient.sln echoclient.vcproj echoclient.vcxproj include.am quit
echoserver echoserver.c echoserver.h echoserver.sln echoserver.vcproj echoserver.vcxproj include.am
ocsp_responder include.am ocsp_responder.c ocsp_responder.h
pem include.am pem.c
sctp include.am sctp-client-dtls.c sctp-client.c sctp-server-dtls.c sctp-server.c
server include.am server.c server.h server.sln server.vcproj server.vcxproj
README.md include.am
linuxkm
patches
5.10.17 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v10v17.patch
5.10.236 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v10v236.patch
5.14.0-570.58.1.el9_6 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v14-570v58v1-el9_6.patch
5.15 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v15.patch
5.17 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v17.patch
5.17-ubuntu-jammy-tegra WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-5v17-ubuntu-jammy-tegra.patch
6.1.73 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v1v73.patch
6.12 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v12.patch
6.15 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-6v15.patch
7.0 WOLFSSL_LINUXKM_HAVE_GET_RANDOM_CALLBACKS-7v0.patch
regen-patches.sh
Kbuild Makefile README.md get_thread_size.c include.am linuxkm-fips-hash-wrapper.sh linuxkm-fips-hash.c linuxkm_memory.c linuxkm_memory.h linuxkm_wc_port.h lkcapi_aes_glue.c lkcapi_dh_glue.c lkcapi_ecdh_glue.c lkcapi_ecdsa_glue.c lkcapi_glue.c lkcapi_rsa_glue.c lkcapi_sha_glue.c module_exports.c.template module_hooks.c pie_redirect_table.c wolfcrypt.lds x86_vector_register_glue.c
m4 ax_add_am_macro.m4 ax_am_jobserver.m4 ax_am_macros.m4 ax_append_compile_flags.m4 ax_append_flag.m4 ax_append_link_flags.m4 ax_append_to_file.m4 ax_atomic.m4 ax_bsdkm.m4 ax_check_compile_flag.m4 ax_check_link_flag.m4 ax_compiler_version.m4 ax_count_cpus.m4 ax_create_generic_config.m4 ax_debug.m4 ax_file_escapes.m4 ax_harden_compiler_flags.m4 ax_linuxkm.m4 ax_print_to_file.m4 ax_pthread.m4 ax_require_defined.m4 ax_tls.m4 ax_vcs_checkout.m4 hexversion.m4 lib_socket_nsl.m4 visibility.m4
mcapi
wolfcrypt_mcapi.X
nbproject configurations.xml include.am project.xml
Makefile
wolfcrypt_test.X
nbproject configurations.xml include.am project.xml
Makefile
wolfssl.X
nbproject configurations.xml include.am project.xml
Makefile
zlib.X
nbproject configurations.xml include.am project.xml
Makefile
PIC32MZ-serial.h README crypto.c crypto.h include.am mcapi_test.c user_settings.h
mplabx
wolfcrypt_benchmark.X
nbproject configurations.xml include.am project.xml
Makefile
wolfcrypt_test.X
nbproject configurations.xml include.am project.xml
Makefile
wolfssl.X
nbproject configurations.xml include.am project.xml
Makefile
PIC32MZ-serial.h README benchmark_main.c include.am test_main.c user_settings.h
mqx
util_lib
Sources include.am util.c util.h
wolfcrypt_benchmark
Debugger K70FN1M0.mem init_kinetis.tcl mass_erase_kinetis.tcl
Sources include.am main.c main.h
ReferencedRSESystems.xml wolfcrypt_benchmark_twrk70f120m_Int_Flash_DDRData_Debug_PnE_U-MultiLink.launch wolfcrypt_benchmark_twrk70f120m_Int_Flash_DDRData_Release_PnE_U-MultiLink.launch wolfcrypt_benchmark_twrk70f120m_Int_Flash_SramData_Debug_JTrace.jlink wolfcrypt_benchmark_twrk70f120m_Int_Flash_SramData_Debug_JTrace.launch wolfcrypt_benchmark_twrk70f120m_Int_Flash_SramData_Debug_PnE_U-MultiLink.launch wolfcrypt_benchmark_twrk70f120m_Int_Flash_SramData_Release_PnE_U-MultiLink.launch
wolfcrypt_test
Debugger K70FN1M0.mem init_kinetis.tcl mass_erase_kinetis.tcl
Sources include.am main.c main.h
ReferencedRSESystems.xml wolfcrypt_test_twrk70f120m_Int_Flash_DDRData_Debug_PnE_U-MultiLink.launch wolfcrypt_test_twrk70f120m_Int_Flash_DDRData_Release_PnE_U-MultiLink.launch wolfcrypt_test_twrk70f120m_Int_Flash_SramData_Debug_JTrace.jlink wolfcrypt_test_twrk70f120m_Int_Flash_SramData_Debug_JTrace.launch wolfcrypt_test_twrk70f120m_Int_Flash_SramData_Debug_PnE_U-MultiLink.launch wolfcrypt_test_twrk70f120m_Int_Flash_SramData_Release_PnE_U-MultiLink.launch
wolfssl include.am
wolfssl_client
Debugger K70FN1M0.mem init_kinetis.tcl mass_erase_kinetis.tcl
Sources include.am main.c main.h
ReferencedRSESystems.xml wolfssl_client_twrk70f120m_Int_Flash_DDRData_Debug_PnE_U-MultiLink.launch wolfssl_client_twrk70f120m_Int_Flash_DDRData_Release_PnE_U-MultiLink.launch wolfssl_client_twrk70f120m_Int_Flash_SramData_Debug_JTrace.jlink wolfssl_client_twrk70f120m_Int_Flash_SramData_Debug_JTrace.launch wolfssl_client_twrk70f120m_Int_Flash_SramData_Debug_PnE_U-MultiLink.launch wolfssl_client_twrk70f120m_Int_Flash_SramData_Release_PnE_U-MultiLink.launch
README
rpm include.am spec.in
scripts
bench bench_functions.sh
aria-cmake-build-test.sh asn1_oid_sum.pl benchmark.test benchmark_compare.sh cleanup_testfiles.sh crl-gen-openssl.test crl-revoked.test dertoc.pl dtls.test dtlscid.test external.test google.test include.am makedistsmall.sh memtest.sh ocsp-responder-openssl-interop.test ocsp-stapling-with-ca-as-responder.test ocsp-stapling-with-wolfssl-responder.test ocsp-stapling.test ocsp-stapling2.test ocsp-stapling_tls13multi.test ocsp.test openssl.test openssl_srtp.test pem.test ping.test pkcallbacks.test psk.test resume.test rsapss.test sniffer-gen.sh sniffer-ipv6.pcap sniffer-static-rsa.pcap sniffer-testsuite.test sniffer-tls12-keylog.out sniffer-tls12-keylog.pcap sniffer-tls12-keylog.sslkeylog sniffer-tls13-dh-resume.pcap sniffer-tls13-dh.pcap sniffer-tls13-ecc-resume.pcap sniffer-tls13-ecc.pcap sniffer-tls13-hrr.pcap sniffer-tls13-keylog.out sniffer-tls13-keylog.pcap sniffer-tls13-keylog.sslkeylog sniffer-tls13-x25519-resume.pcap sniffer-tls13-x25519.pcap stm32l4-v4_0_1_build.sh tls13.test trusted_peer.test unit.test.in user_settings_asm.sh
src bio.c conf.c crl.c dtls.c dtls13.c include.am internal.c keys.c ocsp.c pk.c pk_ec.c pk_rsa.c quic.c sniffer.c ssl.c ssl_api_cert.c ssl_api_crl_ocsp.c ssl_api_pk.c ssl_asn1.c ssl_bn.c ssl_certman.c ssl_crypto.c ssl_ech.c ssl_load.c ssl_misc.c ssl_p7p12.c ssl_sess.c ssl_sk.c tls.c tls13.c wolfio.c x509.c x509_str.c
sslSniffer
sslSnifferTest README_WIN.md include.am snifftest.c sslSniffTest.vcproj sslSniffTest.vcxproj
README.md sslSniffer.vcproj sslSniffer.vcxproj
support gen-debug-trace-error-codes.sh include.am wolfssl.pc.in
tests
api api.h api_decl.h create_ocsp_test_blobs.py include.am test_aes.c test_aes.h test_arc4.c test_arc4.h test_ascon.c test_ascon.h test_ascon_kats.h test_asn.c test_asn.h test_blake2.c test_blake2.h test_camellia.c test_camellia.h test_certman.c test_certman.h test_chacha.c test_chacha.h test_chacha20_poly1305.c test_chacha20_poly1305.h test_cmac.c test_cmac.h test_curve25519.c test_curve25519.h test_curve448.c test_curve448.h test_des3.c test_des3.h test_dh.c test_dh.h test_digest.h test_dsa.c test_dsa.h test_dtls.c test_dtls.h test_ecc.c test_ecc.h test_ed25519.c test_ed25519.h test_ed448.c test_ed448.h test_evp.c test_evp.h test_evp_cipher.c test_evp_cipher.h test_evp_digest.c test_evp_digest.h test_evp_pkey.c test_evp_pkey.h test_hash.c test_hash.h test_hmac.c test_hmac.h test_md2.c test_md2.h test_md4.c test_md4.h test_md5.c test_md5.h test_mldsa.c test_mldsa.h test_mlkem.c test_mlkem.h test_ocsp.c test_ocsp.h test_ocsp_test_blobs.h test_ossl_asn1.c test_ossl_asn1.h test_ossl_bio.c test_ossl_bio.h test_ossl_bn.c test_ossl_bn.h test_ossl_cipher.c test_ossl_cipher.h test_ossl_dgst.c test_ossl_dgst.h test_ossl_dh.c test_ossl_dh.h test_ossl_dsa.c test_ossl_dsa.h test_ossl_ec.c test_ossl_ec.h test_ossl_ecx.c test_ossl_ecx.h test_ossl_mac.c test_ossl_mac.h test_ossl_obj.c test_ossl_obj.h test_ossl_p7p12.c test_ossl_p7p12.h test_ossl_pem.c test_ossl_pem.h test_ossl_rand.c test_ossl_rand.h test_ossl_rsa.c test_ossl_rsa.h test_ossl_sk.c test_ossl_sk.h test_ossl_x509.c test_ossl_x509.h test_ossl_x509_acert.c test_ossl_x509_acert.h test_ossl_x509_crypto.c test_ossl_x509_crypto.h test_ossl_x509_ext.c test_ossl_x509_ext.h test_ossl_x509_info.c test_ossl_x509_info.h test_ossl_x509_io.c test_ossl_x509_io.h test_ossl_x509_lu.c test_ossl_x509_lu.h test_ossl_x509_name.c test_ossl_x509_name.h test_ossl_x509_pk.c test_ossl_x509_pk.h test_ossl_x509_str.c test_ossl_x509_str.h test_ossl_x509_vp.c test_ossl_x509_vp.h test_pkcs12.c test_pkcs12.h test_pkcs7.c test_pkcs7.h test_poly1305.c test_poly1305.h test_random.c test_random.h test_rc2.c test_rc2.h test_ripemd.c test_ripemd.h test_rsa.c test_rsa.h test_sha.c test_sha.h test_sha256.c test_sha256.h test_sha3.c test_sha3.h test_sha512.c test_sha512.h test_she.c test_she.h test_signature.c test_signature.h test_slhdsa.c test_slhdsa.h test_sm2.c test_sm2.h test_sm3.c test_sm3.h test_sm4.c test_sm4.h test_tls.c test_tls.h test_tls13.c test_tls13.h test_tls_ext.c test_tls_ext.h test_wc_encrypt.c test_wc_encrypt.h test_wolfmath.c test_wolfmath.h test_x509.c test_x509.h
emnet
IP IP.h
Makefile emnet_nonblock_test.c emnet_shim.c
freertos-mem-track-repro FreeRTOS.h repro.c run.sh semphr.h task.h user_settings.h
swdev .gitignore Makefile README.md swdev.c swdev.h swdev_loader.c swdev_loader.h user_settings.h
CONF_FILES_README.md NCONF_test.cnf README TXT_DB.txt api.c include.am quic.c srp.c suites.c test-altchains.conf test-chains.conf test-dhprime.conf test-dtls-downgrade.conf test-dtls-fails-cipher.conf test-dtls-fails.conf test-dtls-group.conf test-dtls-mtu.conf test-dtls-reneg-client.conf test-dtls-reneg-server.conf test-dtls-resume.conf test-dtls-sha2.conf test-dtls-srtp-fails.conf test-dtls-srtp.conf test-dtls.conf test-dtls13-cid.conf test-dtls13-downgrade-fails.conf test-dtls13-downgrade.conf test-dtls13-pq-hybrid-extra-frag.conf test-dtls13-pq-hybrid-extra.conf test-dtls13-pq-hybrid-frag.conf test-dtls13-pq-standalone-frag.conf test-dtls13-pq-standalone.conf test-dtls13-psk.conf test-dtls13.conf test-ecc-cust-curves.conf test-ed25519.conf test-ed448.conf test-enckeys.conf test-fails.conf test-maxfrag-dtls.conf test-maxfrag.conf test-p521.conf test-psk-no-id-sha2.conf test-psk-no-id.conf test-psk.conf test-rsapss.conf test-sctp-sha2.conf test-sctp.conf test-sha2.conf test-sig.conf test-sm2.conf test-tls-downgrade.conf test-tls13-down.conf test-tls13-ecc.conf test-tls13-pq-hybrid-extra.conf test-tls13-pq-hybrid.conf test-tls13-pq-standalone.conf test-tls13-psk-certs.conf test-tls13-psk.conf test-tls13-slhdsa-fail.conf test-tls13-slhdsa-sha2.conf test-tls13-slhdsa-shake.conf test-tls13.conf test-trustpeer.conf test.conf unit.c unit.h utils.c utils.h w64wrapper.c
testsuite include.am testsuite.c testsuite.sln testsuite.vcproj testsuite.vcxproj utils.c utils.h
tirtos
packages
ti
net
wolfssl
tests
EK_TM4C1294XL
wolfcrypt
benchmark TM4C1294NC.icf benchmark.cfg main.c package.bld.hide package.xdc
test TM4C1294NC.icf main.c package.bld.hide package.xdc test.cfg
package.bld package.xdc package.xs
.gitignore README include.am products.mak wolfssl.bld wolfssl.mak
wolfcrypt
benchmark README.md benchmark-VS2022.sln benchmark-VS2022.vcxproj benchmark-VS2022.vcxproj.user benchmark.c benchmark.h benchmark.sln benchmark.vcproj benchmark.vcxproj include.am
src
port
Espressif
esp_crt_bundle README.md cacrt_all.pem cacrt_deprecated.pem cacrt_local.pem esp_crt_bundle.c gen_crt_bundle.py pio_install_cryptography.py
README.md esp32_aes.c esp32_mp.c esp32_sha.c esp32_util.c esp_sdk_mem_lib.c esp_sdk_time_lib.c esp_sdk_wifi_lib.c
Renesas README.md renesas_common.c renesas_fspsm_aes.c renesas_fspsm_rsa.c renesas_fspsm_sha.c renesas_fspsm_util.c renesas_rx64_hw_sha.c renesas_rx64_hw_util.c renesas_tsip_aes.c renesas_tsip_rsa.c renesas_tsip_sha.c renesas_tsip_util.c
af_alg afalg_aes.c afalg_hash.c wc_afalg.c
aria aria-crypt.c aria-cryptocb.c
arm armv8-32-aes-asm.S armv8-32-aes-asm_c.c armv8-32-chacha-asm.S armv8-32-chacha-asm_c.c armv8-32-curve25519.S armv8-32-curve25519_c.c armv8-32-mlkem-asm.S armv8-32-mlkem-asm_c.c armv8-32-poly1305-asm.S armv8-32-poly1305-asm_c.c armv8-32-sha256-asm.S armv8-32-sha256-asm_c.c armv8-32-sha3-asm.S armv8-32-sha3-asm_c.c armv8-32-sha512-asm.S armv8-32-sha512-asm_c.c armv8-aes-asm.S armv8-aes-asm_c.c armv8-aes.c armv8-chacha-asm.S armv8-chacha-asm_c.c armv8-curve25519.S armv8-curve25519_c.c armv8-mlkem-asm.S armv8-mlkem-asm_c.c armv8-poly1305-asm.S armv8-poly1305-asm_c.c armv8-sha256-asm.S armv8-sha256-asm_c.c armv8-sha256.c armv8-sha3-asm.S armv8-sha3-asm_c.c armv8-sha512-asm.S armv8-sha512-asm_c.c armv8-sha512.c cryptoCell.c cryptoCellHash.c thumb2-aes-asm.S thumb2-aes-asm_c.c thumb2-chacha-asm.S thumb2-chacha-asm_c.c thumb2-curve25519.S thumb2-curve25519_c.c thumb2-mlkem-asm.S thumb2-mlkem-asm_c.c thumb2-poly1305-asm.S thumb2-poly1305-asm_c.c thumb2-sha256-asm.S thumb2-sha256-asm_c.c thumb2-sha3-asm.S thumb2-sha3-asm_c.c thumb2-sha512-asm.S thumb2-sha512-asm_c.c
atmel README.md atmel.c
autosar README.md cryif.c crypto.c csm.c include.am test.c
caam README.md caam_aes.c caam_doc.pdf caam_driver.c caam_error.c caam_integrity.c caam_qnx.c caam_sha.c wolfcaam_aes.c wolfcaam_cmac.c wolfcaam_ecdsa.c wolfcaam_fsl_nxp.c wolfcaam_hash.c wolfcaam_hmac.c wolfcaam_init.c wolfcaam_qnx.c wolfcaam_rsa.c wolfcaam_seco.c wolfcaam_x25519.c
cavium README.md README_Octeon.md cavium_nitrox.c cavium_octeon_sync.c
cuda README.md aes-cuda.cu
cypress README.md psoc6_crypto.c
devcrypto README.md devcrypto_aes.c devcrypto_ecdsa.c devcrypto_hash.c devcrypto_hmac.c devcrypto_rsa.c devcrypto_x25519.c wc_devcrypto.c
intel README.md quickassist.c quickassist_mem.c quickassist_sync.c
iotsafe iotsafe.c
kcapi README.md kcapi_aes.c kcapi_dh.c kcapi_ecc.c kcapi_hash.c kcapi_hmac.c kcapi_rsa.c
liboqs liboqs.c
maxim README.md max3266x.c maxq10xx.c
mynewt mynewt_port.c
nxp README.md README_SE050.md casper_port.c dcp_port.c hashcrypt_port.c ksdk_port.c se050_port.c
pic32 pic32mz-crypt.c
ppc32 ppc32-sha256-asm.S ppc32-sha256-asm_c.c ppc32-sha256-asm_cr.c
psa README.md psa.c psa_aes.c psa_hash.c psa_pkcbs.c
riscv riscv-64-aes.c riscv-64-chacha.c riscv-64-poly1305.c riscv-64-sha256.c riscv-64-sha3.c riscv-64-sha512.c
rpi_pico README.md pico.c
silabs README.md silabs_aes.c silabs_ecc.c silabs_hash.c silabs_random.c
st README.md STM32MP13.md STM32MP25.md stm32.c stsafe.c
ti ti-aes.c ti-ccm.c ti-des3.c ti-hash.c
tropicsquare README.md tropic01.c
xilinx xil-aesgcm.c xil-sha3.c xil-versal-glue.c xil-versal-trng.c
nrf51.c
ASN_TEMPLATE.md aes.c aes_asm.S aes_asm.asm aes_gcm_asm.S aes_gcm_asm.asm aes_gcm_x86_asm.S aes_xts_asm.S aes_xts_asm.asm arc4.c ascon.c asm.c asn.c asn_orig.c async.c blake2b.c blake2s.c camellia.c chacha.c chacha20_poly1305.c chacha_asm.S chacha_asm.asm cmac.c coding.c compress.c cpuid.c cryptocb.c curve25519.c curve448.c des3.c dh.c dilithium.c dsa.c ecc.c ecc_fp.c eccsi.c ed25519.c ed448.c error.c evp.c evp_pk.c falcon.c fe_448.c fe_low_mem.c fe_operations.c fe_x25519_128.h fe_x25519_asm.S fp_mont_small.i fp_mul_comba_12.i fp_mul_comba_17.i fp_mul_comba_20.i fp_mul_comba_24.i fp_mul_comba_28.i fp_mul_comba_3.i fp_mul_comba_32.i fp_mul_comba_4.i fp_mul_comba_48.i fp_mul_comba_6.i fp_mul_comba_64.i fp_mul_comba_7.i fp_mul_comba_8.i fp_mul_comba_9.i fp_mul_comba_small_set.i fp_sqr_comba_12.i fp_sqr_comba_17.i fp_sqr_comba_20.i fp_sqr_comba_24.i fp_sqr_comba_28.i fp_sqr_comba_3.i fp_sqr_comba_32.i fp_sqr_comba_4.i fp_sqr_comba_48.i fp_sqr_comba_6.i fp_sqr_comba_64.i fp_sqr_comba_7.i fp_sqr_comba_8.i fp_sqr_comba_9.i fp_sqr_comba_small_set.i ge_448.c ge_low_mem.c ge_operations.c hash.c hmac.c hpke.c include.am integer.c kdf.c logging.c md2.c md4.c md5.c memory.c misc.c pkcs12.c pkcs7.c poly1305.c poly1305_asm.S poly1305_asm.asm puf.c pwdbased.c random.c rc2.c ripemd.c rng_bank.c rsa.c sakke.c sha.c sha256.c sha256_asm.S sha3.c sha3_asm.S sha512.c sha512_asm.S signature.c siphash.c sm2.c sm3.c sm3_asm.S sm4.c sp_arm32.c sp_arm64.c sp_armthumb.c sp_c32.c sp_c64.c sp_cortexm.c sp_dsp32.c sp_int.c sp_sm2_arm32.c sp_sm2_arm64.c sp_sm2_armthumb.c sp_sm2_c32.c sp_sm2_c64.c sp_sm2_cortexm.c sp_sm2_x86_64.c sp_sm2_x86_64_asm.S sp_x86_64.c sp_x86_64_asm.S sp_x86_64_asm.asm srp.c tfm.c wc_dsp.c wc_encrypt.c wc_lms.c wc_lms_impl.c wc_mldsa_asm.S wc_mlkem.c wc_mlkem_asm.S wc_mlkem_poly.c wc_pkcs11.c wc_port.c wc_she.c wc_slhdsa.c wc_xmss.c wc_xmss_impl.c wolfentropy.c wolfevent.c wolfmath.c
test README.md include.am test-VS2022.sln test-VS2022.vcxproj test-VS2022.vcxproj.user test.c test.h test.sln test.vcproj test_paths.h.in
wolfssl
openssl aes.h asn1.h asn1t.h bio.h bn.h buffer.h camellia.h cmac.h cms.h compat_types.h conf.h crypto.h des.h dh.h dsa.h ec.h ec25519.h ec448.h ecdh.h ecdsa.h ed25519.h ed448.h engine.h err.h evp.h fips_rand.h hmac.h include.am kdf.h lhash.h md4.h md5.h modes.h obj_mac.h objects.h ocsp.h opensslconf.h opensslv.h ossl_typ.h pem.h pkcs12.h pkcs7.h rand.h rc4.h ripemd.h rsa.h safestack.h sha.h sha3.h srp.h ssl.h ssl23.h stack.h tls1.h txt_db.h ui.h x509.h x509_vfy.h x509v3.h
wolfcrypt
port
Espressif esp-sdk-lib.h esp32-crypt.h esp_crt_bundle.h
Renesas renesas-fspsm-crypt.h renesas-fspsm-types.h renesas-rx64-hw-crypt.h renesas-tsip-crypt.h renesas_cmn.h renesas_fspsm_internal.h renesas_sync.h renesas_tsip_internal.h renesas_tsip_types.h
af_alg afalg_hash.h wc_afalg.h
aria aria-crypt.h aria-cryptocb.h
arm cryptoCell.h
atmel atmel.h
autosar CryIf.h Crypto.h Csm.h StandardTypes.h
caam caam_driver.h caam_error.h caam_qnx.h wolfcaam.h wolfcaam_aes.h wolfcaam_cmac.h wolfcaam_ecdsa.h wolfcaam_fsl_nxp.h wolfcaam_hash.h wolfcaam_qnx.h wolfcaam_rsa.h wolfcaam_seco.h wolfcaam_sha.h wolfcaam_x25519.h
cavium cavium_nitrox.h cavium_octeon_sync.h
cypress psoc6_crypto.h
devcrypto wc_devcrypto.h
intel quickassist.h quickassist_mem.h quickassist_sync.h
iotsafe iotsafe.h
kcapi kcapi_dh.h kcapi_ecc.h kcapi_hash.h kcapi_hmac.h kcapi_rsa.h wc_kcapi.h
liboqs liboqs.h
maxim max3266x-cryptocb.h max3266x.h maxq10xx.h
nxp casper_port.h dcp_port.h hashcrypt_port.h ksdk_port.h se050_port.h
pic32 pic32mz-crypt.h
psa psa.h
riscv riscv-64-asm.h
rpi_pico pico.h
silabs silabs_aes.h silabs_ecc.h silabs_hash.h silabs_random.h
st stm32.h stsafe.h
ti ti-ccm.h ti-hash.h
tropicsquare tropic01.h
xilinx xil-sha3.h xil-versal-glue.h xil-versal-trng.h
nrf51.h
aes.h arc4.h ascon.h asn.h asn_public.h async.h blake2-impl.h blake2-int.h blake2.h camellia.h chacha.h chacha20_poly1305.h cmac.h coding.h compress.h cpuid.h cryptocb.h curve25519.h curve448.h des3.h dh.h dilithium.h dsa.h ecc.h eccsi.h ed25519.h ed448.h error-crypt.h falcon.h fe_448.h fe_operations.h fips_test.h ge_448.h ge_operations.h hash.h hmac.h hpke.h include.am integer.h kdf.h libwolfssl_sources.h libwolfssl_sources_asm.h logging.h md2.h md4.h md5.h mem_track.h memory.h misc.h mpi_class.h mpi_superclass.h oid_sum.h pkcs11.h pkcs12.h pkcs7.h poly1305.h puf.h pwdbased.h random.h rc2.h ripemd.h rng_bank.h rsa.h sakke.h selftest.h settings.h sha.h sha256.h sha3.h sha512.h signature.h siphash.h sm2.h sm3.h sm4.h sp.h sp_int.h srp.h tfm.h types.h visibility.h wc_encrypt.h wc_lms.h wc_mlkem.h wc_pkcs11.h wc_port.h wc_she.h wc_slhdsa.h wc_xmss.h wolfentropy.h wolfevent.h wolfmath.h
callbacks.h certs_test.h certs_test_sm.h crl.h error-ssl.h include.am internal.h ocsp.h options.h.in quic.h sniffer.h sniffer_error.h sniffer_error.rc ssl.h test.h version.h version.h.in wolfio.h
wrapper
Ada
examples
src aes_verify_main.adb rsa_verify_main.adb sha256_main.adb spark_sockets.adb spark_sockets.ads spark_terminal.adb spark_terminal.ads tls_client.adb tls_client.ads tls_client_main.adb tls_server.adb tls_server.ads tls_server_main.adb
.gitignore alire.toml examples.gpr
tests
src
support test_support.adb test_support.ads tests_root_suite.adb tests_root_suite.ads
aes_bindings_tests.adb aes_bindings_tests.ads rsa_verify_bindings_tests.adb rsa_verify_bindings_tests.ads sha256_bindings_tests.adb sha256_bindings_tests.ads tests.adb
.gitignore README.md alire.toml tests.gpr valgrind.supp
.gitignore README.md ada_binding.c alire.toml default.gpr include.am restricted.adc user_settings.h wolfssl-full_runtime.adb wolfssl-full_runtime.ads wolfssl.adb wolfssl.ads wolfssl.gpr
CSharp
wolfCrypt-Test
Properties AssemblyInfo.cs
App.config wolfCrypt-Test.cs wolfCrypt-Test.csproj
wolfSSL-DTLS-PSK-Server
Properties AssemblyInfo.cs
App.config wolfSSL-DTLS-PSK-Server.cs wolfSSL-DTLS-PSK-Server.csproj
wolfSSL-DTLS-Server
Properties AssemblyInfo.cs
App.config wolfSSL-DTLS-Server.cs wolfSSL-DTLS-Server.csproj
wolfSSL-Example-IOCallbacks
Properties AssemblyInfo.cs
App.config wolfSSL-Example-IOCallbacks.cs wolfSSL-Example-IOCallbacks.csproj
wolfSSL-TLS-Client
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-Client.cs wolfSSL-TLS-Client.csproj
wolfSSL-TLS-PSK-Client
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-PSK-Client.cs wolfSSL-TLS-PSK-Client.csproj
wolfSSL-TLS-PSK-Server
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-PSK-Server.cs wolfSSL-TLS-PSK-Server.csproj
wolfSSL-TLS-Server
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-Server.cs wolfSSL-TLS-Server.csproj
wolfSSL-TLS-ServerThreaded
Properties AssemblyInfo.cs
App.config wolfSSL-TLS-ServerThreaded.cs wolfSSL-TLS-ServerThreaded.csproj
wolfSSL_CSharp
Properties AssemblyInfo.cs Resources.Designer.cs Resources.resx
X509.cs wolfCrypt.cs wolfSSL.cs wolfSSL_CSharp.csproj
README.md include.am user_settings.h wolfSSL_CSharp.sln wolfssl.vcxproj
python README.md
rust
wolfssl-wolfcrypt
src aes.rs blake2.rs chacha20_poly1305.rs cmac.rs cmac_mac.rs curve25519.rs dh.rs dilithium.rs ecc.rs ecdsa.rs ed25519.rs ed448.rs fips.rs hkdf.rs hmac.rs hmac_mac.rs kdf.rs lib.rs lms.rs mlkem.rs mlkem_kem.rs pbkdf2_password_hash.rs prf.rs random.rs rsa.rs rsa_pkcs1v15.rs sha.rs sha_digest.rs sys.rs
tests
common mod.rs
test_aes.rs test_blake2.rs test_chacha20_poly1305.rs test_cmac.rs test_cmac_mac.rs test_curve25519.rs test_dh.rs test_dilithium.rs test_ecc.rs test_ecdsa.rs test_ed25519.rs test_ed448.rs test_hkdf.rs test_hmac.rs test_hmac_mac.rs test_kdf.rs test_lms.rs test_mlkem.rs test_mlkem_kem.rs test_pbkdf2_password_hash.rs test_prf.rs test_random.rs test_rsa.rs test_rsa_pkcs1v15.rs test_sha.rs test_sha_digest.rs test_wolfcrypt.rs
CHANGELOG.md Cargo.lock Cargo.toml Makefile README.md build.rs headers.h
Makefile README.md include.am
include.am
zephyr
samples
wolfssl_benchmark
boards native_sim.conf nrf5340dk_nrf5340_cpuapp.conf nrf5340dk_nrf5340_cpuapp_ns.conf
CMakeLists.txt README install_test.sh prj.conf sample.yaml zephyr_legacy.conf zephyr_v4.1.conf
wolfssl_test
boards native_sim.conf nrf5340dk_nrf5340_cpuapp.conf nrf5340dk_nrf5340_cpuapp_ns.conf
CMakeLists.txt README install_test.sh prj-no-malloc.conf prj.conf sample.yaml zephyr_legacy.conf zephyr_v4.1.conf
wolfssl_tls_sock
boards native_sim.conf
src tls_sock.c
CMakeLists.txt README install_sample.sh prj-no-malloc.conf prj.conf sample.yaml zephyr_legacy.conf zephyr_v4.1.conf
wolfssl_tls_thread
boards native_sim.conf nrf5340dk_nrf5340_cpuapp.conf nrf5340dk_nrf5340_cpuapp_ns.conf
src tls_threaded.c
CMakeLists.txt README install_sample.sh prj.conf sample.yaml zephyr_legacy.conf zephyr_v4.1.conf
wolfssl options.h
CMakeLists.txt Kconfig Kconfig.tls-generic README.md include.am module.yml user_settings-no-malloc.h user_settings.h zephyr_init.c
.codespellexcludelines .cyignore .editorconfig .gitignore .wolfssl_known_macro_extras AUTHORS CMakeLists.txt CMakePresets.json CMakeSettings.json COPYING ChangeLog.md INSTALL LICENSING LPCExpresso.cproject LPCExpresso.project Makefile.am README README-async.md README.md SCRIPTS-LIST SECURITY-POLICY.md SECURITY-REPORT-TEMPLATE.md Vagrantfile autogen.sh commit-tests.sh configure.ac fips-check.sh fips-hash.sh gencertbuf.pl input pull_to_vagrant.sh quit resource.h stamp-h.in valgrind-bash.supp valgrind-error.sh wnr-example.conf wolfssl-VS2022.vcxproj wolfssl.rc wolfssl.vcproj wolfssl.vcxproj wolfssl64.sln
.clangd .gitignore DOCS.md Makefile README.md assert.c core.c crypto.c env.c fs.c http.c ini.c json.c log.c luna.h main.c makext.mk path.c process.c request.c sqlite.c stash.c template.c util.c
wolfssl/wolfcrypt/src/rsa.c raw
   1/* rsa.c
   2 *
   3 * Copyright (C) 2006-2026 wolfSSL Inc.
   4 *
   5 * This file is part of wolfSSL.
   6 *
   7 * wolfSSL is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 3 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * wolfSSL is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20 */
  21
  22/*
  23
  24DESCRIPTION
  25This library provides the interface to the RSA.
  26RSA keys can be used to encrypt, decrypt, sign and verify data.
  27
  28*/
  29
  30#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
  31
  32#ifndef NO_RSA
  33
  34#if FIPS_VERSION3_GE(2,0,0)
  35    /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  36    #define FIPS_NO_WRAPPERS
  37
  38       #ifdef USE_WINDOWS_API
  39               #pragma code_seg(".fipsA$j")
  40               #pragma const_seg(".fipsB$j")
  41       #endif
  42#endif
  43
  44#include <wolfssl/wolfcrypt/rsa.h>
  45#include <wolfssl/wolfcrypt/logging.h>
  46
  47#ifdef WOLFSSL_AFALG_XILINX_RSA
  48#include <wolfssl/wolfcrypt/port/af_alg/wc_afalg.h>
  49#endif
  50#if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  51#include <xsecure_rsaclient.h>
  52#endif
  53#if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
  54#include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  55#endif
  56#ifdef WOLFSSL_HAVE_SP_RSA
  57#include <wolfssl/wolfcrypt/sp.h>
  58#endif
  59#if defined(WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD)
  60#include <wolfssl/wolfcrypt/port/nxp/casper_port.h>
  61#endif
  62
  63#if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && !defined(WOLFSSL_SP_ASM)
  64    /* force off unneeded vector register save/restore. */
  65    #undef SAVE_VECTOR_REGISTERS
  66    #define SAVE_VECTOR_REGISTERS(fail_clause) SAVE_NO_VECTOR_REGISTERS(fail_clause)
  67    #undef RESTORE_VECTOR_REGISTERS
  68    #define RESTORE_VECTOR_REGISTERS() RESTORE_NO_VECTOR_REGISTERS()
  69#endif
  70
  71/*
  72 * RSA Build Options:
  73 *
  74 * Core:
  75 * NO_RSA:                  Disable RSA support entirely            default: off
  76 * WOLFSSL_RSA_PUBLIC_ONLY: Only include RSA public key operations  default: off
  77 * WOLFSSL_RSA_VERIFY_ONLY: Only include RSA verify operation       default: off
  78 * WOLFSSL_RSA_VERIFY_INLINE: RSA verify inline (no output copy)   default: off
  79 * WC_RSA_DIRECT:           Enable direct RSA encrypt/decrypt API   default: off
  80 * WC_RSA_NO_PADDING:       Enable no-padding RSA mode              default: off
  81 * WOLFSSL_RSA_KEY_CHECK:   Enable RSA key pair consistency check   default: off
  82 * WOLFSSL_RSA_CHECK_D_ON_DECRYPT: Validate private exponent d     default: off
  83 *                           before each decrypt operation
  84 * WOLFSSL_RSA_DECRYPT_TO_0_LEN: Allow RSA decrypt result of 0     default: off
  85 *                           length (empty plaintext)
  86 * NO_RSA_BOUNDS_CHECK:     Disable RSA bounds checking on input    default: off
  87 * SHOW_GEN:                Show key generation progress dots        default: off
  88 *
  89 * Padding:
  90 * WC_RSA_PSS:              Enable RSA-PSS signature support        default: off
  91 * WC_NO_RSA_OAEP:          Disable RSA OAEP padding                default: off
  92 * WOLFSSL_PSS_LONG_SALT:   Allow PSS salt longer than hash length  default: off
  93 * WOLFSSL_PSS_SALT_LEN_DISCOVER: Auto-discover PSS salt length    default: off
  94 *                           during verification
  95 *
  96 * Performance:
  97 * WC_RSA_BLINDING:         Use blinding with private key ops       default: on
  98 *                           Note: ~20% slower, protects against
  99 *                           timing side-channels
 100 * RSA_LOW_MEM:             Non-CRT private ops, less memory        default: off
 101 * WC_RSA_NONBLOCK:         Non-blocking RSA operations             default: off
 102 * WC_RSA_NONBLOCK_TIME:    Time-based non-blocking RSA             default: off
 103 * WOLFSSL_MP_INVMOD_CONSTANT_TIME: Constant-time modular inverse  default: off
 104 * WC_RSA_NO_FERMAT_CHECK:  Skip Fermat factorization check on     default: off
 105 *                           key generation (p and q closeness)
 106 *
 107 * Key Generation:
 108 * WOLFSSL_KEY_GEN:         Enable RSA private key generation       default: off
 109 * FP_MAX_BITS:             Max key bits with USE_FAST_MATH         default: 4096
 110 *                           Value is key size * 2 (e.g. RSA 3072 = 6144)
 111 *
 112 * SP Math:
 113 * WOLFSSL_HAVE_SP_RSA:     Use SP math for RSA operations          default: off
 114 * WOLFSSL_SP_MATH:         Use SP math only (no multi-precision)   default: off
 115 * WOLFSSL_SP_MATH_ALL:     SP math for all key sizes               default: off
 116 * WOLFSSL_SP_NO_2048:      Disable SP RSA 2048-bit support         default: off
 117 * WOLFSSL_SP_NO_3072:      Disable SP RSA 3072-bit support         default: off
 118 * WOLFSSL_SP_4096:         Enable SP RSA 4096-bit support          default: off
 119 * WOLFSSL_SP_ASM:          Use SP assembly optimizations           default: off
 120 *
 121 * Hardware Acceleration (RSA-specific):
 122 * WC_ASYNC_ENABLE_RSA:     Enable async RSA operations             default: off
 123 * WOLFSSL_KCAPI_RSA:       Linux kernel crypto API for RSA         default: off
 124 * WOLFSSL_AFALG_XILINX_RSA: AF_ALG Xilinx RSA acceleration        default: off
 125 * WOLFSSL_SE050_NO_RSA:    Disable SE050 RSA                       default: off
 126 * WOLFSSL_XILINX_CRYPT:    Xilinx crypto RSA acceleration          default: off
 127 */
 128
 129
 130#include <wolfssl/wolfcrypt/random.h>
 131#ifdef WOLF_CRYPTO_CB
 132    #include <wolfssl/wolfcrypt/cryptocb.h>
 133#endif
 134#ifdef NO_INLINE
 135    #include <wolfssl/wolfcrypt/misc.h>
 136#else
 137    #define WOLFSSL_MISC_INCLUDED
 138    #include <wolfcrypt/src/misc.c>
 139#endif
 140
 141#if FIPS_VERSION3_GE(6,0,0)
 142    const unsigned int wolfCrypt_FIPS_rsa_ro_sanity[2] =
 143                                                     { 0x1a2b3c4d, 0x00000012 };
 144    int wolfCrypt_FIPS_RSA_sanity(void)
 145    {
 146        return 0;
 147    }
 148#endif
 149
 150enum {
 151    RSA_STATE_NONE = 0,
 152
 153    RSA_STATE_ENCRYPT_PAD,
 154    RSA_STATE_ENCRYPT_EXPTMOD,
 155    RSA_STATE_ENCRYPT_RES,
 156
 157    RSA_STATE_DECRYPT_EXPTMOD,
 158    RSA_STATE_DECRYPT_UNPAD,
 159    RSA_STATE_DECRYPT_RES
 160};
 161
 162static void wc_RsaCleanup(RsaKey* key)
 163{
 164#if !defined(WOLFSSL_NO_MALLOC) && (defined(WOLFSSL_ASYNC_CRYPT) || \
 165    (!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE)))
 166    if (key != NULL) {
 167    #ifndef WOLFSSL_RSA_PUBLIC_ONLY
 168        /* if private operation zero temp buffer */
 169        if ((key->data != NULL && key->dataLen > 0) &&
 170            (key->type == RSA_PRIVATE_DECRYPT ||
 171             key->type == RSA_PRIVATE_ENCRYPT)) {
 172            ForceZero(key->data, key->dataLen);
 173        }
 174    #endif
 175        /* make sure any allocated memory is free'd */
 176        if (key->dataIsAlloc) {
 177            XFREE(key->data, key->heap, DYNAMIC_TYPE_WOLF_BIGINT);
 178            key->dataIsAlloc = 0;
 179        }
 180
 181        key->data = NULL;
 182        key->dataLen = 0;
 183    }
 184#else
 185    (void)key;
 186#endif
 187}
 188
 189#ifndef WC_NO_CONSTRUCTORS
 190
 191#define RSA_NEW_INIT_PLAIN  0
 192#ifdef WOLF_PRIVATE_KEY_ID
 193#define RSA_NEW_INIT_ID     1
 194#define RSA_NEW_INIT_LABEL  2
 195#endif
 196
 197static RsaKey* _NewRsaKey_common(void* heap, int devId, int *result_code,
 198                                  int rsaInitType, unsigned char* id,
 199                                  int idLen, const char* label)
 200{
 201    int ret;
 202    RsaKey* key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA);
 203    if (key == NULL) {
 204        ret = MEMORY_E;
 205    }
 206    else {
 207        switch (rsaInitType) {
 208#ifdef WOLF_PRIVATE_KEY_ID
 209        case RSA_NEW_INIT_ID:
 210            if (id == NULL || idLen == 0 || label != NULL) {
 211                ret = BAD_FUNC_ARG;
 212            }
 213            else {
 214                ret = wc_InitRsaKey_Id(key, id, idLen, heap, devId);
 215            }
 216            break;
 217        case RSA_NEW_INIT_LABEL:
 218            if (label == NULL || id != NULL || idLen != 0) {
 219                ret = BAD_FUNC_ARG;
 220            }
 221            else {
 222                ret = wc_InitRsaKey_Label(key, label, heap, devId);
 223            }
 224            break;
 225#endif
 226        default:
 227            if (id != NULL || idLen != 0 || label != NULL) {
 228                ret = BAD_FUNC_ARG;
 229            }
 230            else {
 231                ret = wc_InitRsaKey_ex(key, heap, devId);
 232            }
 233            break;
 234        }
 235        if (ret != 0) {
 236            XFREE(key, heap, DYNAMIC_TYPE_RSA);
 237            key = NULL;
 238        }
 239    }
 240    (void)rsaInitType;
 241    (void)id;
 242    (void)idLen;
 243    (void)label;
 244
 245    if (result_code != NULL) {
 246        *result_code = ret;
 247    }
 248
 249    return key;
 250}
 251
 252RsaKey* wc_NewRsaKey(void* heap, int devId, int *result_code)
 253{
 254    return _NewRsaKey_common(heap, devId, result_code,
 255                             RSA_NEW_INIT_PLAIN, NULL, 0, NULL);
 256}
 257
 258#ifdef WOLF_PRIVATE_KEY_ID
 259RsaKey* wc_NewRsaKey_Id(unsigned char* id, int len, void* heap, int devId,
 260                         int *result_code)
 261{
 262    return _NewRsaKey_common(heap, devId, result_code,
 263                             RSA_NEW_INIT_ID, id, len, NULL);
 264}
 265
 266RsaKey* wc_NewRsaKey_Label(const char* label, void* heap, int devId,
 267                            int *result_code)
 268{
 269    return _NewRsaKey_common(heap, devId, result_code,
 270                             RSA_NEW_INIT_LABEL, NULL, 0, label);
 271}
 272#endif /* WOLF_PRIVATE_KEY_ID */
 273
 274int wc_DeleteRsaKey(RsaKey* key, RsaKey** key_p)
 275{
 276    void* heap;
 277    if (key == NULL) {
 278        return BAD_FUNC_ARG;
 279    }
 280    heap = key->heap;
 281    wc_FreeRsaKey(key);
 282    XFREE(key, heap, DYNAMIC_TYPE_RSA);
 283    if (key_p != NULL) {
 284        *key_p = NULL;
 285    }
 286    return 0;
 287}
 288#endif /* !WC_NO_CONSTRUCTORS */
 289
 290int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
 291{
 292    int ret = 0;
 293
 294    if (key == NULL) {
 295        return BAD_FUNC_ARG;
 296    }
 297
 298    XMEMSET(key, 0, sizeof(RsaKey));
 299
 300    key->type = RSA_TYPE_UNKNOWN;
 301    key->state = RSA_STATE_NONE;
 302    key->heap = heap;
 303#if !defined(WOLFSSL_NO_MALLOC) && (defined(WOLFSSL_ASYNC_CRYPT) || \
 304    (!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE)))
 305    key->dataIsAlloc = 0;
 306#endif
 307
 308#ifdef WOLF_CRYPTO_CB
 309    key->devId = devId;
 310#else
 311    (void)devId;
 312#endif
 313
 314#ifdef WOLFSSL_ASYNC_CRYPT
 315    #ifdef WOLFSSL_CERT_GEN
 316        XMEMSET(&key->certSignCtx, 0, sizeof(CertSignCtx));
 317    #endif
 318
 319    #ifdef WC_ASYNC_ENABLE_RSA
 320        #ifdef WOLF_CRYPTO_CB
 321        /* prefer crypto callback */
 322        if (key->devId != INVALID_DEVID)
 323        #endif
 324        {
 325            /* handle as async */
 326            ret = wolfAsync_DevCtxInit(&key->asyncDev,
 327                    WOLFSSL_ASYNC_MARKER_RSA, key->heap, devId);
 328            if (ret != 0)
 329                return ret;
 330        }
 331    #endif /* WC_ASYNC_ENABLE_RSA */
 332#endif /* WOLFSSL_ASYNC_CRYPT */
 333
 334#ifndef WOLFSSL_RSA_PUBLIC_ONLY
 335    ret = mp_init_multi(&key->n, &key->e, NULL, NULL, NULL, NULL);
 336    if (ret != MP_OKAY)
 337        return ret;
 338
 339#if !defined(WOLFSSL_KEY_GEN) && !defined(OPENSSL_EXTRA) && defined(RSA_LOW_MEM)
 340    ret = mp_init_multi(&key->d, &key->p, &key->q, NULL, NULL, NULL);
 341#else
 342    ret = mp_init_multi(&key->d, &key->p, &key->q, &key->dP, &key->dQ, &key->u);
 343#endif
 344    if (ret != MP_OKAY) {
 345        mp_clear(&key->n);
 346        mp_clear(&key->e);
 347        return ret;
 348    }
 349#else
 350    ret = mp_init(&key->n);
 351    if (ret != MP_OKAY)
 352        return ret;
 353    ret = mp_init(&key->e);
 354    if (ret != MP_OKAY) {
 355        mp_clear(&key->n);
 356        return ret;
 357    }
 358#endif
 359
 360#ifdef WOLFSSL_XILINX_CRYPT
 361    key->pubExp = 0;
 362    key->mod    = NULL;
 363#endif
 364
 365#ifdef WOLFSSL_AFALG_XILINX_RSA
 366    key->alFd = WC_SOCK_NOTSET;
 367    key->rdFd = WC_SOCK_NOTSET;
 368#endif
 369
 370#ifdef WOLFSSL_KCAPI_RSA
 371    key->handle = NULL;
 372#endif
 373
 374#if defined(WOLFSSL_RENESAS_FSPSM)
 375    key->ctx.wrapped_pri1024_key = NULL;
 376    key->ctx.wrapped_pub1024_key = NULL;
 377    key->ctx.wrapped_pri2048_key = NULL;
 378    key->ctx.wrapped_pub2048_key = NULL;
 379    key->ctx.keySz = 0;
 380#endif
 381
 382    return ret;
 383}
 384
 385int wc_InitRsaKey(RsaKey* key, void* heap)
 386{
 387    return wc_InitRsaKey_ex(key, heap, INVALID_DEVID);
 388}
 389
 390#ifdef WOLF_PRIVATE_KEY_ID
 391int wc_InitRsaKey_Id(RsaKey* key, unsigned char* id, int len, void* heap,
 392                     int devId)
 393{
 394    int ret = 0;
 395#if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
 396    /* SE050 TLS users store a word32 at id, need to cast back */
 397    word32* keyPtr = NULL;
 398#endif
 399
 400    if (key == NULL)
 401        ret = BAD_FUNC_ARG;
 402    if (ret == 0 && (len < 0 || len > RSA_MAX_ID_LEN))
 403        ret = BUFFER_E;
 404    if (ret == 0)
 405        ret = wc_InitRsaKey_ex(key, heap, devId);
 406    if (ret == 0 && id != NULL && len != 0) {
 407        XMEMCPY(key->id, id, (size_t)len);
 408        key->idLen = len;
 409    #if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
 410        /* Set SE050 ID from word32, populate RsaKey with public from SE050 */
 411        if (len == (int)sizeof(word32)) {
 412            keyPtr = (word32*)key->id;
 413            ret = wc_RsaUseKeyId(key, *keyPtr, 0);
 414        }
 415    #endif
 416    }
 417
 418    return ret;
 419}
 420
 421int wc_InitRsaKey_Label(RsaKey* key, const char* label, void* heap, int devId)
 422{
 423    int ret = 0;
 424    int labelLen = 0;
 425
 426    if (key == NULL || label == NULL)
 427        ret = BAD_FUNC_ARG;
 428    if (ret == 0) {
 429        labelLen = (int)XSTRLEN(label);
 430        if (labelLen == 0 || labelLen > RSA_MAX_LABEL_LEN)
 431            ret = BUFFER_E;
 432    }
 433    if (ret == 0)
 434        ret = wc_InitRsaKey_ex(key, heap, devId);
 435    if (ret == 0) {
 436        XMEMCPY(key->label, label, (size_t)labelLen);
 437        key->labelLen = labelLen;
 438    }
 439
 440    return ret;
 441}
 442#endif /* WOLF_PRIVATE_KEY_ID */
 443
 444
 445#ifdef WOLFSSL_XILINX_CRYPT
 446#define MAX_E_SIZE 4
 447/* Used to setup hardware state
 448 *
 449 * key  the RSA key to setup
 450 *
 451 * returns 0 on success
 452 */
 453int wc_InitRsaHw(RsaKey* key)
 454{
 455    unsigned char* m; /* RSA modulus */
 456    word32 e = 0;     /* RSA public exponent */
 457    int mSz;
 458    int eSz;
 459    int ret;
 460
 461    if (key == NULL) {
 462        return BAD_FUNC_ARG;
 463    }
 464
 465    mSz = mp_unsigned_bin_size(&(key->n));
 466#if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
 467    if (mSz > WOLFSSL_XSECURE_RSA_KEY_SIZE) {
 468        return BAD_FUNC_ARG;
 469    }
 470    /* Allocate 4 bytes more for the public exponent. */
 471    m = (unsigned char*) XMALLOC(WOLFSSL_XSECURE_RSA_KEY_SIZE + 4, key->heap,
 472                                 DYNAMIC_TYPE_KEY);
 473#else
 474    m = (unsigned char*)XMALLOC(mSz, key->heap, DYNAMIC_TYPE_KEY);
 475#endif
 476    if (m == NULL) {
 477        return MEMORY_E;
 478    }
 479
 480    if (mp_to_unsigned_bin(&(key->n), m) != MP_OKAY) {
 481        WOLFSSL_MSG("Unable to get RSA key modulus");
 482        XFREE(m, key->heap, DYNAMIC_TYPE_KEY);
 483        return MP_READ_E;
 484    }
 485#if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
 486    XMEMSET(m + mSz, 0, WOLFSSL_XSECURE_RSA_KEY_SIZE + 4 - mSz);
 487#endif
 488
 489    eSz = mp_unsigned_bin_size(&(key->e));
 490    if (eSz > MAX_E_SIZE) {
 491        WOLFSSL_MSG("Exponent of size 4 bytes expected");
 492        XFREE(m, key->heap, DYNAMIC_TYPE_KEY);
 493        return BAD_FUNC_ARG;
 494    }
 495
 496    if (mp_to_unsigned_bin(&(key->e), (byte*)&e + (MAX_E_SIZE - eSz))
 497                != MP_OKAY) {
 498        XFREE(m, key->heap, DYNAMIC_TYPE_KEY);
 499        WOLFSSL_MSG("Unable to get RSA key exponent");
 500        return MP_READ_E;
 501    }
 502
 503    /* check for existing mod buffer to avoid memory leak */
 504    XFREE(key->mod, key->heap, DYNAMIC_TYPE_KEY);
 505
 506    key->pubExp = e;
 507    key->mod    = m;
 508
 509#if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
 510    ret = wc_InitXsecure(&(key->xSec));
 511    if (ret != 0) {
 512        WOLFSSL_MSG("Unable to initialize xSecure for RSA");
 513        XFREE(m, key->heap, DYNAMIC_TYPE_KEY);
 514        return ret;
 515    }
 516    XMEMCPY(&m[WOLFSSL_XSECURE_RSA_KEY_SIZE], &e, sizeof(e));
 517    key->mSz = mSz;
 518#else
 519    if (XSecure_RsaInitialize(&(key->xRsa), key->mod, NULL,
 520                (byte*)&(key->pubExp)) != XST_SUCCESS) {
 521        WOLFSSL_MSG("Unable to initialize RSA on hardware");
 522        XFREE(m, key->heap, DYNAMIC_TYPE_KEY);
 523        return BAD_STATE_E;
 524    }
 525
 526#ifdef WOLFSSL_XILINX_PATCH
 527   /* currently a patch of xsecure_rsa.c for 2048 bit keys */
 528   if (wc_RsaEncryptSize(key) == 256) {
 529       if (XSecure_RsaSetSize(&(key->xRsa), 2048) != XST_SUCCESS) {
 530           WOLFSSL_MSG("Unable to set RSA key size on hardware");
 531           XFREE(m, key->heap, DYNAMIC_TYPE_KEY);
 532           return BAD_STATE_E;
 533       }
 534   }
 535#endif
 536#endif
 537    return 0;
 538} /* WOLFSSL_XILINX_CRYPT*/
 539
 540#elif defined(WOLFSSL_CRYPTOCELL)
 541
 542int wc_InitRsaHw(RsaKey* key)
 543{
 544    CRYSError_t ret = 0;
 545    byte e[3];
 546    word32 eSz = sizeof(e);
 547    byte n[256];
 548    word32 nSz = sizeof(n);
 549    byte d[256];
 550    word32 dSz = sizeof(d);
 551    byte p[128];
 552    word32 pSz = sizeof(p);
 553    byte q[128];
 554    word32 qSz = sizeof(q);
 555
 556    if (key == NULL) {
 557        return BAD_FUNC_ARG;
 558    }
 559
 560    ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
 561    if (ret != 0)
 562        return MP_READ_E;
 563
 564    ret = CRYS_RSA_Build_PubKey(&key->ctx.pubKey, e, eSz, n, nSz);
 565    if (ret != SA_SILIB_RET_OK){
 566        WOLFSSL_MSG("CRYS_RSA_Build_PubKey failed");
 567        return ret;
 568    }
 569
 570    ret =  CRYS_RSA_Build_PrivKey(&key->ctx.privKey, d, dSz, e, eSz, n, nSz);
 571
 572    if (ret != SA_SILIB_RET_OK){
 573        WOLFSSL_MSG("CRYS_RSA_Build_PrivKey failed");
 574        return ret;
 575    }
 576    key->type = RSA_PRIVATE;
 577    return 0;
 578}
 579
 580static int cc310_RSA_GenerateKeyPair(RsaKey* key, int size, long e)
 581{
 582    CRYSError_t             ret = 0;
 583    CRYS_RSAKGData_t        KeyGenData;
 584    CRYS_RSAKGFipsContext_t FipsCtx;
 585    byte ex[3];
 586    word16 eSz = sizeof(ex);
 587    byte n[256];
 588    word16 nSz = sizeof(n);
 589
 590    ret = CRYS_RSA_KG_GenerateKeyPair(&wc_rndState,
 591                        wc_rndGenVectFunc,
 592                        (byte*)&e,
 593                        3*sizeof(byte),
 594                        size,
 595                        &key->ctx.privKey,
 596                        &key->ctx.pubKey,
 597                        &KeyGenData,
 598                        &FipsCtx);
 599
 600    if (ret != SA_SILIB_RET_OK){
 601        WOLFSSL_MSG("CRYS_RSA_KG_GenerateKeyPair failed");
 602        return ret;
 603    }
 604
 605    ret = CRYS_RSA_Get_PubKey(&key->ctx.pubKey, ex, &eSz, n, &nSz);
 606    if (ret != SA_SILIB_RET_OK){
 607        WOLFSSL_MSG("CRYS_RSA_Get_PubKey failed");
 608        return ret;
 609    }
 610    ret = wc_RsaPublicKeyDecodeRaw(n, nSz, ex, eSz, key);
 611
 612    key->type = RSA_PRIVATE;
 613
 614    return ret;
 615}
 616#endif /* WOLFSSL_CRYPTOCELL */
 617
 618#if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
 619/* Use specified hardware key ID with RsaKey operations. Unlike devId,
 620 * keyId is a word32 so can handle key IDs larger than an int.
 621 *
 622 * key    initialized RsaKey struct
 623 * keyId  hardware key ID which stores RSA key
 624 * flags  optional flags, currently unused
 625 *
 626 * Return 0 on success, negative on error */
 627int wc_RsaUseKeyId(RsaKey* key, word32 keyId, word32 flags)
 628{
 629    (void)flags;
 630
 631    if (key == NULL) {
 632        return BAD_FUNC_ARG;
 633    }
 634
 635    return se050_rsa_use_key_id(key, keyId);
 636}
 637
 638/* Get hardware key ID associated with this RsaKey structure.
 639 *
 640 * key    initialized RsaKey struct
 641 * keyId  [OUT] output for key ID associated with this structure
 642 *
 643 * Returns 0 on success, negative on error.
 644 */
 645int wc_RsaGetKeyId(RsaKey* key, word32* keyId)
 646{
 647    if (key == NULL || keyId == NULL) {
 648        return BAD_FUNC_ARG;
 649    }
 650
 651    return se050_rsa_get_key_id(key, keyId);
 652}
 653#endif /* WOLFSSL_SE050 */
 654
 655int wc_FreeRsaKey(RsaKey* key)
 656{
 657    int ret = 0;
 658
 659    if (key == NULL) {
 660        return BAD_FUNC_ARG;
 661    }
 662
 663#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_FREE)
 664    #ifndef WOLF_CRYPTO_CB_FIND
 665    if (key->devId != INVALID_DEVID)
 666    #endif
 667    {
 668        ret = wc_CryptoCb_Free(key->devId, WC_ALGO_TYPE_PK,
 669                               WC_PK_TYPE_RSA, 0, key);
 670        /* If callback wants standard free, it returns CRYPTOCB_UNAVAILABLE.
 671         * Otherwise assume the callback handled cleanup. */
 672        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
 673            return ret;
 674        /* fall-through to software cleanup */
 675        ret = 0;
 676    }
 677#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_FREE */
 678
 679#if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
 680    se050_rsa_free_key(key);
 681#endif
 682
 683    wc_RsaCleanup(key);
 684
 685#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
 686    wolfAsync_DevCtxFree(&key->asyncDev, WOLFSSL_ASYNC_MARKER_RSA);
 687#endif
 688
 689#ifndef WOLFSSL_RSA_PUBLIC_ONLY
 690    /* Forcezero all private key fields that are present in this build
 691     * configuration, since they may contain residual sensitive data even when
 692     * key->type is not RSA_PRIVATE (e.g., after a partial key decode failure). */
 693#if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
 694    mp_forcezero(&key->u);
 695    mp_forcezero(&key->dQ);
 696    mp_forcezero(&key->dP);
 697#endif
 698    mp_forcezero(&key->q);
 699    mp_forcezero(&key->p);
 700    mp_forcezero(&key->d);
 701#endif /* WOLFSSL_RSA_PUBLIC_ONLY */
 702
 703    /* public part */
 704    mp_clear(&key->e);
 705    mp_clear(&key->n);
 706
 707#ifdef WOLFSSL_XILINX_CRYPT
 708    XFREE(key->mod, key->heap, DYNAMIC_TYPE_KEY);
 709    key->mod = NULL;
 710#endif
 711
 712#ifdef WOLFSSL_AFALG_XILINX_RSA
 713    /* make sure that sockets are closed on cleanup */
 714    if (key->alFd > 0) {
 715        close(key->alFd);
 716        key->alFd = WC_SOCK_NOTSET;
 717    }
 718    if (key->rdFd > 0) {
 719        close(key->rdFd);
 720        key->rdFd = WC_SOCK_NOTSET;
 721    }
 722#endif
 723
 724#ifdef WOLFSSL_KCAPI_RSA
 725    KcapiRsa_Free(key);
 726#endif
 727
 728#ifdef WOLFSSL_CHECK_MEM_ZERO
 729    wc_MemZero_Check(key, sizeof(RsaKey));
 730#endif
 731
 732#if defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY)
 733    wc_fspsm_RsaKeyFree(key);
 734#endif
 735#ifdef WOLFSSL_MICROCHIP_TA100
 736    wc_Microchip_rsa_free(key);
 737#endif
 738    return ret;
 739}
 740
 741#ifdef WOLFSSL_RSA_KEY_CHECK
 742/* Check the pair-wise consistency of the RSA key. */
 743static int _ifc_pairwise_consistency_test(RsaKey* key, WC_RNG* rng)
 744{
 745    static const char* msg = "Everyone gets Friday off.";
 746#ifndef WOLFSSL_NO_MALLOC
 747    byte* sig = NULL;
 748#else
 749    byte sig[RSA_MAX_SIZE/8];
 750#endif
 751    byte* plain;
 752    int ret = 0;
 753    word32 msgLen, plainLen, sigLen;
 754
 755    msgLen = (word32)XSTRLEN(msg);
 756    ret = wc_RsaEncryptSize(key);
 757    if (ret < 0)
 758        return ret;
 759    else if (ret == 0)
 760        return BAD_FUNC_ARG;
 761    sigLen = (word32)ret;
 762
 763    WOLFSSL_MSG("Doing RSA consistency test");
 764
 765#ifndef WOLFSSL_NO_MALLOC
 766    /* Sign and verify. */
 767    sig = (byte*)XMALLOC(sigLen, key->heap, DYNAMIC_TYPE_RSA);
 768    if (sig == NULL) {
 769        return MEMORY_E;
 770    }
 771#endif
 772    XMEMSET(sig, 0, sigLen);
 773#ifdef WOLFSSL_CHECK_MEM_ZERO
 774    wc_MemZero_Add("Pairwise CT sig", sig, sigLen);
 775#endif
 776    plain = sig;
 777
 778#ifdef WOLFSSL_ASYNC_CRYPT
 779    /* Do blocking async calls here, caller does not support WC_PENDING_E */
 780    do {
 781        if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
 782            ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
 783        if (ret >= 0)
 784#endif
 785            ret = wc_RsaSSL_Sign((const byte*)msg, msgLen, sig, sigLen, key, rng);
 786#ifdef WOLFSSL_ASYNC_CRYPT
 787    } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
 788#endif
 789
 790    if (ret > 0) {
 791        sigLen = (word32)ret;
 792#ifdef WOLFSSL_ASYNC_CRYPT
 793        /* Do blocking async calls here, caller does not support WC_PENDING_E */
 794        do {
 795            if (ret == WC_NO_ERR_TRACE(WC_PENDING_E))
 796                ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
 797            if (ret >= 0)
 798#endif
 799                ret = wc_RsaSSL_VerifyInline(sig, sigLen, &plain, key);
 800#ifdef WOLFSSL_ASYNC_CRYPT
 801        } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E));
 802#endif
 803    }
 804
 805    if (ret > 0) {
 806        plainLen = (word32)ret;
 807        ret = (msgLen != plainLen) || (XMEMCMP(plain, msg, msgLen) != 0);
 808    }
 809
 810    if (ret != 0)
 811        ret = RSA_KEY_PAIR_E;
 812
 813    ForceZero(sig, sigLen);
 814#ifndef WOLFSSL_NO_MALLOC
 815    XFREE(sig, key->heap, DYNAMIC_TYPE_RSA);
 816#endif
 817
 818    return ret;
 819}
 820
 821
 822int wc_CheckRsaKey(RsaKey* key)
 823{
 824    WC_RNG *rng = NULL;
 825#if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
 826    WC_RNG rng_buf;
 827#endif
 828    int ret = 0;
 829    DECL_MP_INT_SIZE_DYN(tmp, (key)? mp_bitsused(&key->n) : 0, RSA_MAX_SIZE);
 830
 831    if (key == NULL) {
 832        return BAD_FUNC_ARG;
 833    }
 834
 835#ifdef WOLFSSL_CAAM
 836    /* can not perform these checks on an encrypted key */
 837    if (key->blackKey != 0) {
 838        return 0;
 839    }
 840#endif
 841
 842    NEW_MP_INT_SIZE(tmp, mp_bitsused(&key->n), NULL, DYNAMIC_TYPE_RSA);
 843#ifdef MP_INT_SIZE_CHECK_NULL
 844    if (tmp == NULL) {
 845        return MEMORY_E;
 846    }
 847#endif
 848
 849    if (key->rng)
 850        rng = key->rng;
 851    else {
 852#if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
 853        rng = &rng_buf;
 854#else
 855        rng = (WC_RNG *)XMALLOC(sizeof(*rng), NULL, DYNAMIC_TYPE_RNG);
 856        if (rng == NULL) {
 857            FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA);
 858            return MEMORY_E;
 859        }
 860#endif
 861        ret = wc_InitRng(rng);
 862        if (ret != 0) {
 863#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
 864            XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
 865            FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA);
 866#endif
 867            return ret;
 868        }
 869    }
 870
 871    SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
 872
 873    if (ret == 0) {
 874        if (INIT_MP_INT_SIZE(tmp, mp_bitsused(&key->n)) != MP_OKAY)
 875            ret = MP_INIT_E;
 876    }
 877
 878    if (ret == 0)
 879        ret = _ifc_pairwise_consistency_test(key, rng);
 880
 881    /* Check d is less than n. */
 882    if (ret == 0 ) {
 883        if (mp_cmp(&key->d, &key->n) != MP_LT) {
 884            ret = MP_EXPTMOD_E;
 885        }
 886    }
 887    /* Check p*q = n. */
 888    if (ret == 0 ) {
 889    #ifdef WOLFSSL_CHECK_MEM_ZERO
 890        mp_memzero_add("RSA CheckKey tmp", tmp);
 891    #endif
 892        if (mp_mul(&key->p, &key->q, tmp) != MP_OKAY) {
 893            ret = MP_EXPTMOD_E;
 894        }
 895    }
 896    if (ret == 0 ) {
 897        if (mp_cmp(&key->n, tmp) != MP_EQ) {
 898            ret = MP_EXPTMOD_E;
 899        }
 900    }
 901
 902#ifndef WC_RSA_NO_FERMAT_CHECK
 903    /* Fermat's Factorization works when difference between p and q
 904     * is less than (conservatively):
 905     *     n^(1/4) + 32
 906     *  ~= 2^(bit count of n)^(1/4) + 32) = 2^((bit count of n)/4 + 32)
 907     */
 908    if (ret == 0) {
 909        ret = mp_sub(&key->p, &key->q, tmp);
 910    }
 911    if (ret == 0) {
 912        if (mp_count_bits(tmp) <= (mp_count_bits(&key->n) / 4 + 32)) {
 913            ret = MP_EXPTMOD_E;
 914        }
 915    }
 916#endif
 917
 918    /* Check dP, dQ and u if they exist */
 919    if (ret == 0 && !mp_iszero(&key->dP)) {
 920        if (mp_sub_d(&key->p, 1, tmp) != MP_OKAY) {
 921            ret = MP_EXPTMOD_E;
 922        }
 923        /* Check dP <= p-1. */
 924        if (ret == 0) {
 925            if (mp_cmp(&key->dP, tmp) != MP_LT) {
 926                ret = MP_EXPTMOD_E;
 927            }
 928        }
 929        /* Check e*dP mod p-1 = 1. (dP = 1/e mod p-1) */
 930        if (ret == 0) {
 931            if (mp_mulmod(&key->dP, &key->e, tmp, tmp) != MP_OKAY) {
 932                ret = MP_EXPTMOD_E;
 933            }
 934        }
 935        if (ret == 0 ) {
 936            if (!mp_isone(tmp)) {
 937                ret = MP_EXPTMOD_E;
 938            }
 939        }
 940
 941        if (ret == 0) {
 942            if (mp_sub_d(&key->q, 1, tmp) != MP_OKAY) {
 943                ret = MP_EXPTMOD_E;
 944            }
 945        }
 946        /* Check dQ <= q-1. */
 947        if (ret == 0) {
 948            if (mp_cmp(&key->dQ, tmp) != MP_LT) {
 949                ret = MP_EXPTMOD_E;
 950            }
 951        }
 952        /* Check e*dP mod p-1 = 1. (dQ = 1/e mod q-1) */
 953        if (ret == 0) {
 954            if (mp_mulmod(&key->dQ, &key->e, tmp, tmp) != MP_OKAY) {
 955                ret = MP_EXPTMOD_E;
 956            }
 957        }
 958        if (ret == 0 ) {
 959            if (!mp_isone(tmp)) {
 960                ret = MP_EXPTMOD_E;
 961            }
 962        }
 963
 964        /* Check u <= p. */
 965        if (ret == 0) {
 966            if (mp_cmp(&key->u, &key->p) != MP_LT) {
 967                ret = MP_EXPTMOD_E;
 968            }
 969        }
 970        /* Check u*q mod p = 1. (u = 1/q mod p) */
 971        if (ret == 0) {
 972            if (mp_mulmod(&key->u, &key->q, &key->p, tmp) != MP_OKAY) {
 973                ret = MP_EXPTMOD_E;
 974            }
 975        }
 976        if (ret == 0 ) {
 977            if (!mp_isone(tmp)) {
 978                ret = MP_EXPTMOD_E;
 979            }
 980        }
 981    }
 982
 983    mp_forcezero(tmp);
 984
 985    RESTORE_VECTOR_REGISTERS();
 986
 987    if ((rng != NULL) && (rng != key->rng)) {
 988        wc_FreeRng(rng);
 989#ifdef WOLFSSL_SMALL_STACK
 990        XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
 991#endif
 992    }
 993    FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA);
 994#ifdef WOLFSSL_CHECK_MEM_ZERO
 995    mp_memzero_check(tmp);
 996#endif
 997
 998    return ret;
 999}
1000#endif /* WOLFSSL_RSA_KEY_CHECK */
1001
1002
1003#if !defined(WC_NO_RSA_OAEP) || defined(WC_RSA_PSS)
1004/* Uses MGF1 standard as a mask generation function
1005   hType: hash type used
1006   seed:  seed to use for generating mask
1007   seedSz: size of seed buffer
1008   out:   mask output after generation
1009   outSz: size of output buffer
1010 */
1011#if !defined(NO_SHA) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
1012static int RsaMGF1(enum wc_HashType hType, byte* seed, word32 seedSz,
1013                                        byte* out, word32 outSz, void* heap)
1014{
1015#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1016    byte* tmp = NULL;
1017    byte   tmpF = 0;     /* 1 if dynamic memory needs freed */
1018#else
1019    byte tmp[RSA_MAX_SIZE/8];
1020#endif
1021    /* needs to be large enough for seed size plus counter(4) */
1022    byte  tmpA[WC_MAX_DIGEST_SIZE + 4];
1023    word32 tmpSz = 0;
1024    int hLen;
1025    int ret;
1026    word32 counter;
1027    word32 idx;
1028#ifdef WOLFSSL_SMALL_STACK_CACHE
1029    wc_HashAlg *hash;
1030#endif
1031    hLen    = wc_HashGetDigestSize(hType);
1032    counter = 0;
1033    idx     = 0;
1034
1035    (void)heap;
1036
1037    XMEMSET(tmpA, 0, sizeof(tmpA));
1038    /* check error return of wc_HashGetDigestSize */
1039    if (hLen < 0) {
1040        return hLen;
1041    }
1042
1043    /* if tmp is not large enough than use some dynamic memory */
1044    if ((seedSz + 4) > sizeof(tmpA) || (word32)hLen > sizeof(tmpA)) {
1045        /* find largest amount of memory needed which will be the max of
1046         * hLen and (seedSz + 4) since tmp is used to store the hash digest */
1047        tmpSz = ((seedSz + 4) > (word32)hLen)? seedSz + 4: (word32)hLen;
1048#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1049        tmp = (byte*)XMALLOC(tmpSz, heap, DYNAMIC_TYPE_RSA_BUFFER);
1050        if (tmp == NULL) {
1051            return MEMORY_E;
1052        }
1053        tmpF = 1; /* make sure to free memory when done */
1054#else
1055        if (tmpSz > RSA_MAX_SIZE/8)
1056            return BAD_FUNC_ARG;
1057#endif
1058    }
1059    else {
1060        /* use array on the stack */
1061    #ifndef WOLFSSL_SMALL_STACK_CACHE
1062        tmpSz = sizeof(tmpA);
1063    #endif
1064#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1065        tmp  = tmpA;
1066        tmpF = 0; /* no need to free memory at end */
1067#endif
1068    }
1069
1070#ifdef WOLFSSL_SMALL_STACK_CACHE
1071    hash = (wc_HashAlg*)XMALLOC(sizeof(*hash), heap, DYNAMIC_TYPE_DIGEST);
1072    if (hash == NULL) {
1073    #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1074        if (tmpF) {
1075            XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
1076        }
1077    #endif
1078        return MEMORY_E;
1079    }
1080    ret = wc_HashInit_ex(hash, hType, heap, INVALID_DEVID);
1081    if (ret != 0) {
1082        XFREE(hash, heap, DYNAMIC_TYPE_DIGEST);
1083    #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1084        if (tmpF) {
1085            XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
1086        }
1087    #endif
1088        return ret;
1089    }
1090#endif
1091
1092    do {
1093        int i = 0;
1094        XMEMCPY(tmp, seed, seedSz);
1095
1096        /* counter to byte array appended to tmp */
1097        tmp[seedSz]     = (byte)((counter >> 24) & 0xFF);
1098        tmp[seedSz + 1] = (byte)((counter >> 16) & 0xFF);
1099        tmp[seedSz + 2] = (byte)((counter >>  8) & 0xFF);
1100        tmp[seedSz + 3] = (byte)((counter)       & 0xFF);
1101
1102        /* hash and append to existing output */
1103#ifdef WOLFSSL_SMALL_STACK_CACHE
1104        ret = wc_HashUpdate(hash, hType, tmp, (seedSz + 4));
1105        if (ret == 0) {
1106            ret = wc_HashFinal(hash, hType, tmp);
1107        }
1108#else
1109        ret = wc_Hash(hType, tmp, (seedSz + 4), tmp, tmpSz);
1110#endif
1111        if (ret != 0) {
1112            /* check for if dynamic memory was needed, then free */
1113#ifdef WOLFSSL_SMALL_STACK_CACHE
1114            wc_HashFree(hash, hType);
1115            XFREE(hash, heap, DYNAMIC_TYPE_DIGEST);
1116#endif
1117#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1118            if (tmpF) {
1119                XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
1120            }
1121#endif
1122            return ret;
1123        }
1124
1125        for (i = 0; i < hLen && idx < outSz; i++) {
1126            out[idx++] = tmp[i];
1127        }
1128        counter++;
1129    } while (idx < outSz);
1130#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1131    /* check for if dynamic memory was needed, then free */
1132    if (tmpF) {
1133        XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
1134    }
1135#endif
1136#ifdef WOLFSSL_SMALL_STACK_CACHE
1137    wc_HashFree(hash, hType);
1138    XFREE(hash, heap, DYNAMIC_TYPE_DIGEST);
1139#endif
1140
1141    return 0;
1142}
1143#endif /* SHA2 Hashes */
1144
1145#if defined(WOLFSSL_SHA3) && \
1146    (defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256))
1147/* SHAKE XOF used directly as mask generation function (not MGF1).
1148 * Per FIPS 186-5, SHAKE can be used as the MGF for RSA-PSS. */
1149static int RsaMGF_SHAKE(enum wc_HashType shakeType, byte* seed, word32 seedSz,
1150                         byte* out, word32 outSz, void* heap)
1151{
1152    WC_DECLARE_VAR(shake, wc_Shake, 1, heap);
1153    int ret;
1154
1155    (void)heap;
1156    (void)shakeType;
1157
1158    WC_ALLOC_VAR_EX(shake, wc_Shake, 1, heap, DYNAMIC_TYPE_TMP_BUFFER,
1159        return MEMORY_E);
1160
1161#ifdef WOLFSSL_SHAKE128
1162    if (shakeType == WC_HASH_TYPE_SHAKE128) {
1163        ret = wc_InitShake128(shake, heap, INVALID_DEVID);
1164        if (ret == 0) {
1165            ret = wc_Shake128_Update(shake, seed, seedSz);
1166            if (ret == 0)
1167                ret = wc_Shake128_Final(shake, out, outSz);
1168            wc_Shake128_Free(shake);
1169        }
1170    }
1171    else
1172#endif
1173#ifdef WOLFSSL_SHAKE256
1174    if (shakeType == WC_HASH_TYPE_SHAKE256) {
1175        ret = wc_InitShake256(shake, heap, INVALID_DEVID);
1176        if (ret == 0) {
1177            ret = wc_Shake256_Update(shake, seed, seedSz);
1178            if (ret == 0)
1179                ret = wc_Shake256_Final(shake, out, outSz);
1180            wc_Shake256_Free(shake);
1181        }
1182    }
1183    else
1184#endif
1185    {
1186        ret = BAD_FUNC_ARG;
1187    }
1188    WC_FREE_VAR_EX(shake, heap, DYNAMIC_TYPE_TMP_BUFFER);
1189    return ret;
1190}
1191#endif /* WOLFSSL_SHA3 && (WOLFSSL_SHAKE128 || WOLFSSL_SHAKE256) */
1192
1193/* helper function to direct which mask generation function is used
1194   switched on type input
1195 */
1196static int RsaMGF(int type, byte* seed, word32 seedSz, byte* out,
1197                                                    word32 outSz, void* heap)
1198{
1199    int ret;
1200
1201    switch(type) {
1202    #ifndef NO_SHA
1203        case WC_MGF1SHA1:
1204            ret = RsaMGF1(WC_HASH_TYPE_SHA, seed, seedSz, out, outSz, heap);
1205            break;
1206    #endif
1207    #ifndef NO_SHA256
1208    #ifdef WOLFSSL_SHA224
1209        case WC_MGF1SHA224:
1210            ret = RsaMGF1(WC_HASH_TYPE_SHA224, seed, seedSz, out, outSz, heap);
1211            break;
1212    #endif
1213        case WC_MGF1SHA256:
1214            ret = RsaMGF1(WC_HASH_TYPE_SHA256, seed, seedSz, out, outSz, heap);
1215            break;
1216    #endif
1217    #ifdef WOLFSSL_SHA384
1218        case WC_MGF1SHA384:
1219            ret = RsaMGF1(WC_HASH_TYPE_SHA384, seed, seedSz, out, outSz, heap);
1220            break;
1221    #endif
1222    #ifdef WOLFSSL_SHA512
1223        case WC_MGF1SHA512:
1224            ret = RsaMGF1(WC_HASH_TYPE_SHA512, seed, seedSz, out, outSz, heap);
1225            break;
1226        #ifndef WOLFSSL_NOSHA512_224
1227        case WC_MGF1SHA512_224:
1228            ret = RsaMGF1(WC_HASH_TYPE_SHA512_224, seed, seedSz, out, outSz,
1229                heap);
1230            break;
1231        #endif
1232        #ifndef WOLFSSL_NOSHA512_256
1233        case WC_MGF1SHA512_256:
1234            ret = RsaMGF1(WC_HASH_TYPE_SHA512_256, seed, seedSz, out, outSz,
1235                heap);
1236            break;
1237        #endif
1238    #endif
1239    #ifdef WOLFSSL_SHA3
1240    #ifndef WOLFSSL_NOSHA3_224
1241        case WC_MGF1SHA3_224:
1242            ret = RsaMGF1(WC_HASH_TYPE_SHA3_224, seed, seedSz, out, outSz,
1243                heap);
1244            break;
1245    #endif
1246    #ifndef WOLFSSL_NOSHA3_256
1247        case WC_MGF1SHA3_256:
1248            ret = RsaMGF1(WC_HASH_TYPE_SHA3_256, seed, seedSz, out, outSz,
1249                heap);
1250            break;
1251    #endif
1252    #ifndef WOLFSSL_NOSHA3_384
1253        case WC_MGF1SHA3_384:
1254            ret = RsaMGF1(WC_HASH_TYPE_SHA3_384, seed, seedSz, out, outSz,
1255                heap);
1256            break;
1257    #endif
1258    #ifndef WOLFSSL_NOSHA3_512
1259        case WC_MGF1SHA3_512:
1260            ret = RsaMGF1(WC_HASH_TYPE_SHA3_512, seed, seedSz, out, outSz,
1261                heap);
1262            break;
1263    #endif
1264    #endif /* WOLFSSL_SHA3 */
1265    #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE128)
1266        case WC_MGF1SHAKE128:
1267            ret = RsaMGF1(WC_HASH_TYPE_SHAKE128, seed, seedSz, out, outSz,
1268                heap);
1269            break;
1270        case WC_MGFSHAKE128:
1271            ret = RsaMGF_SHAKE(WC_HASH_TYPE_SHAKE128, seed, seedSz, out, outSz,
1272                heap);
1273            break;
1274    #endif
1275    #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_SHAKE256)
1276        case WC_MGF1SHAKE256:
1277            ret = RsaMGF1(WC_HASH_TYPE_SHAKE256, seed, seedSz, out, outSz,
1278                heap);
1279            break;
1280        case WC_MGFSHAKE256:
1281            ret = RsaMGF_SHAKE(WC_HASH_TYPE_SHAKE256, seed, seedSz, out, outSz,
1282                heap);
1283            break;
1284    #endif
1285        default:
1286            WOLFSSL_MSG("Unknown MGF type: check build options");
1287            ret = BAD_FUNC_ARG;
1288    }
1289
1290    /* in case of default avoid unused warning */
1291    (void)seed;
1292    (void)seedSz;
1293    (void)out;
1294    (void)outSz;
1295    (void)heap;
1296
1297    return ret;
1298}
1299#endif /* !WC_NO_RSA_OAEP || WC_RSA_PSS */
1300
1301
1302/* Padding */
1303#ifndef WOLFSSL_RSA_VERIFY_ONLY
1304#ifndef WC_NO_RNG
1305#ifndef WC_NO_RSA_OAEP
1306static int RsaPad_OAEP(const byte* input, word32 inputLen, byte* pkcsBlock,
1307        word32 pkcsBlockLen, byte padValue, WC_RNG* rng,
1308        enum wc_HashType hType, int mgf, byte* optLabel, word32 labelLen,
1309        void* heap)
1310{
1311    int ret;
1312    word32 hLen;
1313    int psLen;
1314    word32 idx;
1315
1316    #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1317        byte* dbMask = NULL;
1318        byte* lHash = NULL;
1319        byte* seed  = NULL;
1320    #else
1321        byte dbMask[RSA_MAX_SIZE/8 + RSA_PSS_PAD_SZ];
1322        /* must be large enough to contain largest hash */
1323        byte lHash[WC_MAX_DIGEST_SIZE];
1324        byte seed[WC_MAX_DIGEST_SIZE];
1325    #endif
1326
1327    /* no label is allowed, but catch if no label provided and length > 0 */
1328    if (optLabel == NULL && labelLen > 0) {
1329        return BUFFER_E;
1330    }
1331
1332    /* limit of label is the same as limit of hash function which is massive */
1333    ret = wc_HashGetDigestSize(hType);
1334    if (ret < 0) {
1335        return ret;
1336    }
1337    hLen = (word32)ret;
1338
1339    #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1340        lHash = (byte*)XMALLOC(hLen, heap, DYNAMIC_TYPE_RSA_BUFFER);
1341        if (lHash == NULL) {
1342            return MEMORY_E;
1343        }
1344        seed = (byte*)XMALLOC(hLen, heap, DYNAMIC_TYPE_RSA_BUFFER);
1345        if (seed == NULL) {
1346            XFREE(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1347            return MEMORY_E;
1348        }
1349    #else
1350        /* hLen should never be larger than lHash since size is max digest size,
1351           but check before blindly calling wc_Hash */
1352        if (hLen > sizeof(lHash)) {
1353            WOLFSSL_MSG("OAEP lHash to small for digest!!");
1354            return MEMORY_E;
1355        }
1356    #endif
1357
1358    if ((ret = wc_Hash(hType, optLabel, labelLen, lHash, hLen)) != 0) {
1359        WOLFSSL_MSG("OAEP hash type possibly not supported or lHash to small");
1360            WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1361            WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER);
1362        return ret;
1363    }
1364
1365    /* handles check of location for idx as well as psLen, cast to int to check
1366       for pkcsBlockLen(k) - 2 * hLen - 2 being negative
1367       This check is similar to decryption where k > 2 * hLen + 2 as msg
1368       size approaches 0. In decryption if k is less than or equal -- then there
1369       is no possible room for msg.
1370       k = RSA key size
1371       hLen = hash digest size -- will always be >= 0 at this point
1372     */
1373    if ((2 * hLen + 2) > pkcsBlockLen) {
1374        WOLFSSL_MSG("OAEP pad error hash to big for RSA key size");
1375            WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1376            WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER);
1377        return BAD_FUNC_ARG;
1378    }
1379
1380    if (inputLen > (pkcsBlockLen - 2 * hLen - 2)) {
1381        WOLFSSL_MSG("OAEP pad error message too long");
1382            WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1383            WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER);
1384        return BAD_FUNC_ARG;
1385    }
1386
1387    /* concatenate lHash || PS || 0x01 || msg */
1388    idx = pkcsBlockLen - 1 - inputLen;
1389    psLen = (int)pkcsBlockLen - (int)inputLen - 2 * (int)hLen - 2;
1390    if (pkcsBlockLen < inputLen) { /*make sure not writing over end of buffer */
1391            WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1392            WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER);
1393        return BUFFER_E;
1394    }
1395    XMEMCPY(pkcsBlock + (pkcsBlockLen - inputLen), input, inputLen);
1396    pkcsBlock[idx--] = 0x01; /* PS and M separator */
1397    XMEMSET(pkcsBlock + idx - psLen + 1, 0, (size_t)psLen);
1398    idx -= (word32)psLen;
1399
1400    idx = idx - hLen + 1;
1401    XMEMCPY(pkcsBlock + idx, lHash, hLen);
1402
1403    /* generate random seed */
1404    if ((ret = wc_RNG_GenerateBlock(rng, seed, hLen)) != 0) {
1405            WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1406            ForceZero(seed, hLen);
1407            WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER);
1408        return ret;
1409    }
1410
1411#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1412    /* create maskedDB from dbMask */
1413    dbMask = (byte*)XMALLOC(pkcsBlockLen - hLen - 1, heap, DYNAMIC_TYPE_RSA);
1414    if (dbMask == NULL) {
1415
1416            XFREE(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1417            ForceZero(seed, hLen);
1418            XFREE(seed,  heap, DYNAMIC_TYPE_RSA_BUFFER);
1419        return MEMORY_E;
1420    }
1421#else
1422    if (pkcsBlockLen - hLen - 1 > sizeof(dbMask)) {
1423        return MEMORY_E;
1424    }
1425#endif
1426    XMEMSET(dbMask, 0, pkcsBlockLen - hLen - 1); /* help static analyzer */
1427    ret = RsaMGF(mgf, seed, hLen, dbMask, pkcsBlockLen - hLen - 1, heap);
1428    if (ret != 0) {
1429            WC_FREE_VAR_EX(dbMask, heap, DYNAMIC_TYPE_RSA);
1430            WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1431            ForceZero(seed, hLen);
1432            WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER);
1433        return ret;
1434    }
1435
1436    xorbuf(pkcsBlock + hLen + 1, dbMask,pkcsBlockLen - hLen - 1);
1437
1438    WC_FREE_VAR_EX(dbMask, heap, DYNAMIC_TYPE_RSA);
1439
1440    /* create maskedSeed from seedMask */
1441    pkcsBlock[0] = 0x00;
1442    /* create seedMask inline */
1443    if ((ret = RsaMGF(mgf, pkcsBlock + hLen + 1, pkcsBlockLen - hLen - 1,
1444                                           pkcsBlock + 1, hLen, heap)) != 0) {
1445            WC_FREE_VAR_EX(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1446            ForceZero(seed, hLen);
1447            WC_FREE_VAR_EX(seed, heap, DYNAMIC_TYPE_RSA_BUFFER);
1448        return ret;
1449    }
1450
1451    /* xor created seedMask with seed to make maskedSeed */
1452    xorbuf(pkcsBlock + 1, seed, hLen);
1453#ifdef WOLFSSL_CHECK_MEM_ZERO
1454    /* Seed must be zeroized now that it has been used. */
1455    wc_MemZero_Add("Pad OAEP seed", seed, hLen);
1456#endif
1457
1458    /* Zeroize masking bytes so that padding can't be unmasked. */
1459    ForceZero(seed, hLen);
1460    #ifdef WOLFSSL_SMALL_STACK
1461        XFREE(lHash, heap, DYNAMIC_TYPE_RSA_BUFFER);
1462        XFREE(seed,  heap, DYNAMIC_TYPE_RSA_BUFFER);
1463    #elif defined(WOLFSSL_CHECK_MEM_ZERO)
1464        wc_MemZero_Check(seed, hLen);
1465    #endif
1466    (void)padValue;
1467
1468    return 0;
1469}
1470#endif /* !WC_NO_RSA_OAEP */
1471
1472#ifdef WC_RSA_PSS
1473
1474/* 0x00 .. 0x00 0x01 | Salt | Gen Hash | 0xbc
1475 * XOR MGF over all bytes down to end of Salt
1476 * Gen Hash = HASH(8 * 0x00 | Message Hash | Salt)
1477 *
1478 * input         Digest of the message.
1479 * inputLen      Length of digest.
1480 * pkcsBlock     Buffer to write to.
1481 * pkcsBlockLen  Length of buffer to write to.
1482 * rng           Random number generator (for salt).
1483 * htype         Hash function to use.
1484 * mgf           Mask generation function.
1485 * saltLen       Length of salt to put in padding.
1486 * bits          Length of key in bits.
1487 * heap          Used for dynamic memory allocation.
1488 * returns 0 on success, PSS_SALTLEN_E when the salt length is invalid
1489 * and other negative values on error.
1490 */
1491static int RsaPad_PSS(const byte* input, word32 inputLen, byte* pkcsBlock,
1492        word32 pkcsBlockLen, WC_RNG* rng, enum wc_HashType hType, int mgf,
1493        int saltLen, int bits, void* heap)
1494{
1495    int   ret = 0;
1496    int   hLen, o, maskLen, hiBits;
1497    byte* m;
1498    byte* s;
1499#if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY)
1500    byte msg[RSA_MAX_SIZE/8 + RSA_PSS_PAD_SZ];
1501#else
1502    byte* msg = NULL;
1503#endif
1504#if defined(WOLFSSL_PSS_LONG_SALT) || defined(WOLFSSL_PSS_SALT_LEN_DISCOVER)
1505    byte* salt;
1506#else
1507    byte salt[WC_MAX_DIGEST_SIZE];
1508#endif
1509
1510#if defined(WOLFSSL_PSS_LONG_SALT) || defined(WOLFSSL_PSS_SALT_LEN_DISCOVER)
1511    if (pkcsBlockLen > RSA_MAX_SIZE/8) {
1512        return MEMORY_E;
1513    }
1514#endif
1515
1516    hLen = wc_HashGetDigestSize(hType);
1517    if (hLen < 0)
1518        return hLen;
1519    if ((int)inputLen != hLen) {
1520        return BAD_FUNC_ARG;
1521    }
1522
1523    hiBits = (bits - 1) & 0x7;
1524    if (hiBits == 0) {
1525        /* Per RFC8017, set the leftmost 8emLen - emBits bits of the
1526           leftmost octet in DB to zero.
1527        */
1528        *(pkcsBlock++) = 0;
1529        pkcsBlockLen--;
1530    }
1531
1532    if (saltLen == RSA_PSS_SALT_LEN_DEFAULT) {
1533        saltLen = hLen;
1534        #ifdef WOLFSSL_SHA512
1535            /* See FIPS 186-4 section 5.5 item (e). */
1536            if (bits == 1024 && hLen == WC_SHA512_DIGEST_SIZE) {
1537                saltLen = RSA_PSS_SALT_MAX_SZ;
1538            }
1539        #endif
1540    }
1541#ifndef WOLFSSL_PSS_LONG_SALT
1542    else if (saltLen > hLen) {
1543        return PSS_SALTLEN_E;
1544    }
1545#endif
1546#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
1547    else if (saltLen < RSA_PSS_SALT_LEN_DEFAULT) {
1548        return PSS_SALTLEN_E;
1549    }
1550#else
1551    else if (saltLen == RSA_PSS_SALT_LEN_DISCOVER) {
1552        saltLen = (int)pkcsBlockLen - hLen - 2;
1553        if (saltLen < 0) {
1554            return PSS_SALTLEN_E;
1555        }
1556    }
1557    else if (saltLen < RSA_PSS_SALT_LEN_DISCOVER) {
1558        return PSS_SALTLEN_E;
1559    }
1560#endif
1561    if ((int)pkcsBlockLen - hLen < saltLen + 2) {
1562        return PSS_SALTLEN_E;
1563    }
1564    maskLen = (int)pkcsBlockLen - 1 - hLen;
1565
1566#if defined(WOLFSSL_PSS_LONG_SALT) || defined(WOLFSSL_PSS_SALT_LEN_DISCOVER)
1567    #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
1568        msg = (byte*)XMALLOC(
1569                          (size_t)(RSA_PSS_PAD_SZ + inputLen + (word32)saltLen),
1570                          heap, DYNAMIC_TYPE_RSA_BUFFER);
1571        if (msg == NULL) {
1572            return MEMORY_E;
1573        }
1574    #endif
1575    salt = s = m = msg;
1576    XMEMSET(m, 0, RSA_PSS_PAD_SZ);
1577    m += RSA_PSS_PAD_SZ;
1578    XMEMCPY(m, input, inputLen);
1579    m += inputLen;
1580    o = (int)(m - s);
1581    if (saltLen > 0) {
1582        ret = wc_RNG_GenerateBlock(rng, m, (word32)saltLen);
1583        if (ret == 0) {
1584            m += saltLen;
1585        }
1586    }
1587#else
1588    if ((int)pkcsBlockLen < RSA_PSS_PAD_SZ + (int)inputLen + saltLen) {
1589    #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
1590        msg = (byte*)XMALLOC(
1591                          (size_t)(RSA_PSS_PAD_SZ + inputLen + (word32)saltLen),
1592                          heap, DYNAMIC_TYPE_RSA_BUFFER);
1593        if (msg == NULL) {
1594            return MEMORY_E;
1595        }
1596    #endif
1597        m = msg;
1598    }
1599    else {
1600        m = pkcsBlock;
1601    }
1602    s = m;
1603    XMEMSET(m, 0, RSA_PSS_PAD_SZ);
1604    m += RSA_PSS_PAD_SZ;
1605    XMEMCPY(m, input, inputLen);
1606    m += inputLen;
1607    o = 0;
1608    if (saltLen > 0) {
1609        ret = wc_RNG_GenerateBlock(rng, salt, (word32)saltLen);
1610        if (ret == 0) {
1611            XMEMCPY(m, salt, (size_t)saltLen);
1612            m += saltLen;
1613        }
1614    }
1615#endif
1616    if (ret == 0) {
1617        /* Put Hash at end of pkcsBlock - 1 */
1618        ret = wc_Hash(hType, s, (word32)(m - s), pkcsBlock + maskLen, (word32)hLen);
1619    }
1620    if (ret == 0) {
1621       /* Set the last eight bits or trailer field to the octet 0xbc */
1622        pkcsBlock[pkcsBlockLen - 1] = RSA_PSS_PAD_TERM;
1623
1624        ret = RsaMGF(mgf, pkcsBlock + maskLen, (word32)hLen, pkcsBlock, (word32)maskLen, heap);
1625    }
1626    if (ret == 0) {
1627        /* Clear the first high bit when "8emLen - emBits" is non-zero.
1628           where emBits = n modBits - 1 */
1629        if (hiBits)
1630            pkcsBlock[0] &= (byte)((1 << hiBits) - 1);
1631
1632        m = pkcsBlock + maskLen - saltLen - 1;
1633        *(m++) ^= 0x01;
1634        xorbuf(m, salt + o, (word32)saltLen);
1635    }
1636
1637#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
1638    /* msg is always not NULL as we bail on allocation failure */
1639    XFREE(msg, heap, DYNAMIC_TYPE_RSA_BUFFER);
1640#endif
1641    return ret;
1642}
1643#endif /* WC_RSA_PSS */
1644#endif /* !WC_NO_RNG */
1645
1646static int RsaPad(const byte* input, word32 inputLen, byte* pkcsBlock,
1647                           word32 pkcsBlockLen, byte padValue, WC_RNG* rng)
1648{
1649    if (input == NULL || inputLen == 0 || pkcsBlock == NULL ||
1650                                                        pkcsBlockLen == 0) {
1651        return BAD_FUNC_ARG;
1652    }
1653
1654    if (pkcsBlockLen - RSA_MIN_PAD_SZ < inputLen) {
1655        WOLFSSL_MSG("RsaPad error, invalid length");
1656        return RSA_PAD_E;
1657    }
1658    pkcsBlock[0] = 0x0;       /* set first byte to zero and advance */
1659    pkcsBlock++; pkcsBlockLen--;
1660    pkcsBlock[0] = padValue;  /* insert padValue */
1661
1662    if (padValue == RSA_BLOCK_TYPE_1) {
1663
1664        /* pad with 0xff bytes */
1665        XMEMSET(&pkcsBlock[1], 0xFF, pkcsBlockLen - inputLen - 2);
1666    }
1667    else {
1668#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WC_NO_RNG)
1669        /* pad with non-zero random bytes */
1670        word32 padLen, i;
1671        int    ret;
1672        padLen = pkcsBlockLen - inputLen - 1;
1673        ret    = wc_RNG_GenerateBlock(rng, &pkcsBlock[1], padLen);
1674        if (ret != 0) {
1675            return ret;
1676        }
1677
1678        /* remove zeros */
1679        for (i = 1; i < padLen; i++) {
1680            if (pkcsBlock[i] == 0) pkcsBlock[i] = 0x01;
1681        }
1682#else
1683        (void)rng;
1684        return RSA_WRONG_TYPE_E;
1685#endif
1686    }
1687
1688    pkcsBlock[pkcsBlockLen-inputLen-1] = 0;     /* separator */
1689    XMEMCPY(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen);
1690
1691    return 0;
1692}
1693
1694/* helper function to direct which padding is used */
1695int wc_RsaPad_ex(const byte* input, word32 inputLen, byte* pkcsBlock,
1696    word32 pkcsBlockLen, byte padValue, WC_RNG* rng, int padType,
1697    enum wc_HashType hType, int mgf, byte* optLabel, word32 labelLen,
1698    int saltLen, int bits, void* heap)
1699{
1700    int ret;
1701
1702    switch (padType)
1703    {
1704        case WC_RSA_PKCSV15_PAD:
1705            /*WOLFSSL_MSG("wolfSSL Using RSA PKCSV15 padding");*/
1706            ret = RsaPad(input, inputLen, pkcsBlock, pkcsBlockLen,
1707                                                                 padValue, rng);
1708            break;
1709
1710#ifndef WC_NO_RNG
1711    #ifndef WC_NO_RSA_OAEP
1712        case WC_RSA_OAEP_PAD:
1713            WOLFSSL_MSG("wolfSSL Using RSA OAEP padding");
1714            ret = RsaPad_OAEP(input, inputLen, pkcsBlock, pkcsBlockLen,
1715                           padValue, rng, hType, mgf, optLabel, labelLen, heap);
1716            break;
1717    #endif
1718
1719    #ifdef WC_RSA_PSS
1720        case WC_RSA_PSS_PAD:
1721            WOLFSSL_MSG("wolfSSL Using RSA PSS padding");
1722            ret = RsaPad_PSS(input, inputLen, pkcsBlock, pkcsBlockLen, rng,
1723                                               hType, mgf, saltLen, bits, heap);
1724            break;
1725    #endif
1726#endif /* !WC_NO_RNG */
1727
1728    #ifdef WC_RSA_NO_PADDING
1729        case WC_RSA_NO_PAD:
1730        {
1731            int bytes = (bits + WOLFSSL_BIT_SIZE - 1) / WOLFSSL_BIT_SIZE;
1732
1733            WOLFSSL_MSG("wolfSSL Using NO padding");
1734
1735            /* In the case of no padding being used check that input is exactly
1736             * the RSA key length */
1737            if ((bits <= 0) || (inputLen != (word32)bytes)) {
1738                WOLFSSL_MSG("Bad input size");
1739                ret = RSA_PAD_E;
1740            }
1741            else {
1742                XMEMCPY(pkcsBlock, input, inputLen);
1743                ret = 0;
1744            }
1745            break;
1746        }
1747    #endif
1748
1749        default:
1750            WOLFSSL_MSG("Unknown RSA Pad Type");
1751            ret = RSA_PAD_E;
1752    }
1753
1754    /* silence warning if not used with padding scheme */
1755    (void)input;
1756    (void)inputLen;
1757    (void)pkcsBlock;
1758    (void)pkcsBlockLen;
1759    (void)padValue;
1760    (void)rng;
1761    (void)padType;
1762    (void)hType;
1763    (void)mgf;
1764    (void)optLabel;
1765    (void)labelLen;
1766    (void)saltLen;
1767    (void)bits;
1768    (void)heap;
1769
1770    return ret;
1771}
1772#endif /* WOLFSSL_RSA_VERIFY_ONLY */
1773
1774
1775/* UnPadding */
1776#if !defined(WC_NO_RSA_OAEP) && !defined(NO_HASH_WRAPPER)
1777/* UnPad plaintext, set start to *output, return length of plaintext,
1778 * < 0 on error */
1779static int RsaUnPad_OAEP(byte *pkcsBlock, unsigned int pkcsBlockLen,
1780                            byte **output, enum wc_HashType hType, int mgf,
1781                            byte* optLabel, word32 labelLen, void* heap)
1782{
1783    word32 hLen;
1784    int ret;
1785    byte h[WC_MAX_DIGEST_SIZE]; /* max digest size */
1786    word32 idx;
1787    word32 i;
1788    volatile word32 inc;
1789
1790#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1791    byte* tmp  = NULL;
1792#else
1793    byte tmp[RSA_MAX_SIZE/8 + RSA_PSS_PAD_SZ];
1794#endif
1795
1796    /* no label is allowed, but catch if no label provided and length > 0 */
1797    if (optLabel == NULL && labelLen > 0) {
1798        return BUFFER_E;
1799    }
1800
1801    ret = wc_HashGetDigestSize(hType);
1802    if ((ret < 0) || (pkcsBlockLen < (2 * (word32)ret + 2))) {
1803        return BAD_FUNC_ARG;
1804    }
1805    hLen = (word32)ret;
1806
1807#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
1808    tmp = (byte*)XMALLOC(pkcsBlockLen, heap, DYNAMIC_TYPE_RSA_BUFFER);
1809    if (tmp == NULL) {
1810        return MEMORY_E;
1811    }
1812#endif
1813    XMEMSET(tmp, 0, pkcsBlockLen);
1814#ifdef WOLFSSL_CHECK_MEM_ZERO
1815    wc_MemZero_Add("OAEP UnPad temp", tmp, pkcsBlockLen);
1816#endif
1817
1818    /* find seedMask value */
1819    ret = RsaMGF(mgf, (byte*)(pkcsBlock + (hLen + 1)),
1820                 pkcsBlockLen - hLen - 1, tmp, hLen, heap);
1821    if (ret != 0) {
1822        WC_FREE_VAR_EX(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
1823        return ret;
1824    }
1825
1826    /* xor seedMask value with maskedSeed to get seed value */
1827    xorbuf(tmp, pkcsBlock + 1, hLen);
1828
1829    /* get dbMask value */
1830    ret = RsaMGF(mgf, tmp, hLen, tmp + hLen, pkcsBlockLen - hLen - 1, heap);
1831    if (ret != 0) {
1832        ForceZero(tmp, hLen);
1833#ifdef WOLFSSL_SMALL_STACK
1834        XFREE(tmp, NULL, DYNAMIC_TYPE_RSA_BUFFER);
1835#elif defined(WOLFSSL_CHECK_MEM_ZERO)
1836        wc_MemZero_Check(tmp, hLen);
1837#endif
1838        return ret;
1839    }
1840
1841    /* get DB value by doing maskedDB xor dbMask */
1842    xorbuf(pkcsBlock + hLen + 1, tmp + hLen, pkcsBlockLen - hLen - 1);
1843
1844    ForceZero(tmp, pkcsBlockLen);
1845#ifdef WOLFSSL_SMALL_STACK
1846    /* done with use of tmp buffer */
1847    XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
1848#elif defined(WOLFSSL_CHECK_MEM_ZERO)
1849    wc_MemZero_Check(tmp, pkcsBlockLen);
1850#endif
1851
1852    /* advance idx to index of PS and msg separator, account for PS size of 0*/
1853    idx = hLen + 1 + hLen;
1854    /* Don't reveal length of message: look at every byte. */
1855    inc = 1;
1856    for (i = hLen + 1 + hLen; i < pkcsBlockLen - 1; i++) {
1857        /* Looking for non-zero byte. */
1858        inc &= 1 - (((word32)0 - pkcsBlock[i]) >> 31);
1859        idx += inc;
1860    }
1861
1862    /* create hash of label for comparison with hash sent */
1863    ret = wc_Hash(hType, optLabel, labelLen, h, hLen);
1864    if (ret != 0) {
1865        return ret;
1866    }
1867
1868    /* say no to chosen ciphertext attack.
1869       Comparison of lHash, Y, and separator value needs to all happen in
1870       constant time.
1871       Attackers should not be able to get error condition from the timing of
1872       these checks.
1873     */
1874    {
1875        volatile int c = ConstantCompare(pkcsBlock + hLen + 1, h, (int)hLen);
1876        c = c + (pkcsBlock[idx++] ^ 0x01); /* separator value is 0x01 */
1877        c = c + (pkcsBlock[0]     ^ 0x00); /* Y, the first value, should be 0 */
1878
1879        /* Return 0 data length on error. */
1880        idx = ctMaskSelWord32(ctMaskEq(c, 0), idx, pkcsBlockLen);
1881    }
1882
1883    /* adjust pointer to correct location in array and return size of M */
1884    *output = (byte*)(pkcsBlock + idx);
1885    return (int)(pkcsBlockLen - idx);
1886}
1887#endif /* !WC_NO_RSA_OAEP */
1888
1889#ifdef WC_RSA_PSS
1890/* 0x00 .. 0x00 0x01 | Salt | Gen Hash | 0xbc
1891 * MGF over all bytes down to end of Salt
1892 *
1893 * pkcsBlock     Buffer holding decrypted data.
1894 * pkcsBlockLen  Length of buffer.
1895 * htype         Hash function to use.
1896 * mgf           Mask generation function.
1897 * saltLen       Length of salt to put in padding.
1898 * bits          Length of key in bits.
1899 * heap          Used for dynamic memory allocation.
1900 * returns       the sum of salt length and SHA-256 digest size on success.
1901 *               Otherwise, PSS_SALTLEN_E for an incorrect salt length,
1902 *               WC_KEY_SIZE_E for an incorrect encoded message (EM) size
1903                 and other negative values on error.
1904 */
1905static int RsaUnPad_PSS(byte *pkcsBlock, unsigned int pkcsBlockLen,
1906                        byte **output, enum wc_HashType hType, int mgf,
1907                        int saltLen, int bits, void* heap)
1908{
1909    int   ret;
1910    byte* tmp;
1911    int   hLen, i, maskLen;
1912#ifdef WOLFSSL_SHA512
1913    int orig_bits = bits;
1914#endif
1915#if defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_STATIC_MEMORY)
1916    byte tmp_buf[RSA_MAX_SIZE/8];
1917    tmp = tmp_buf;
1918
1919    if (pkcsBlockLen > RSA_MAX_SIZE/8) {
1920        return MEMORY_E;
1921    }
1922#endif
1923
1924    hLen = wc_HashGetDigestSize(hType);
1925    if (hLen < 0)
1926        return hLen;
1927    bits = (bits - 1) & 0x7;
1928    if ((pkcsBlock[0] & (0xff << bits)) != 0) {
1929        return BAD_PADDING_E;
1930    }
1931    if (bits == 0) {
1932        pkcsBlock++;
1933        pkcsBlockLen--;
1934    }
1935    maskLen = (int)pkcsBlockLen - 1 - hLen;
1936    if (maskLen < 0) {
1937        WOLFSSL_MSG("RsaUnPad_PSS: Hash too large");
1938        return WC_KEY_SIZE_E;
1939    }
1940
1941    if (saltLen == RSA_PSS_SALT_LEN_DEFAULT) {
1942        saltLen = hLen;
1943        #ifdef WOLFSSL_SHA512
1944            /* See FIPS 186-4 section 5.5 item (e). */
1945            if (orig_bits == 1024 && hLen == WC_SHA512_DIGEST_SIZE)
1946                saltLen = RSA_PSS_SALT_MAX_SZ;
1947        #endif
1948    }
1949#ifndef WOLFSSL_PSS_LONG_SALT
1950    else if (saltLen > hLen)
1951        return PSS_SALTLEN_E;
1952#endif
1953#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
1954    else if (saltLen < RSA_PSS_SALT_LEN_DEFAULT)
1955        return PSS_SALTLEN_E;
1956    if (maskLen < saltLen + 1) {
1957        return PSS_SALTLEN_E;
1958    }
1959#else
1960    else if (saltLen < RSA_PSS_SALT_LEN_DISCOVER)
1961        return PSS_SALTLEN_E;
1962    if (saltLen != RSA_PSS_SALT_LEN_DISCOVER && maskLen < saltLen + 1) {
1963        return WC_KEY_SIZE_E;
1964    }
1965#endif
1966
1967    if (pkcsBlock[pkcsBlockLen - 1] != RSA_PSS_PAD_TERM) {
1968        WOLFSSL_MSG("RsaUnPad_PSS: Padding Term Error");
1969        return BAD_PADDING_E;
1970    }
1971
1972#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
1973    tmp = (byte*)XMALLOC((size_t)maskLen, heap, DYNAMIC_TYPE_RSA_BUFFER);
1974    if (tmp == NULL) {
1975        return MEMORY_E;
1976    }
1977    XMEMSET(tmp, 0, (size_t)maskLen);
1978#endif
1979
1980    if ((ret = RsaMGF(mgf, pkcsBlock + maskLen, (word32)hLen, tmp, (word32)maskLen,
1981                                                                  heap)) != 0) {
1982        #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
1983        XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
1984        #endif
1985        return ret;
1986    }
1987
1988    tmp[0] &= (byte)((1 << bits) - 1);
1989    pkcsBlock[0] &= (byte)((1 << bits) - 1);
1990#ifdef WOLFSSL_PSS_SALT_LEN_DISCOVER
1991    if (saltLen == RSA_PSS_SALT_LEN_DISCOVER) {
1992        for (i = 0; i < maskLen - 1; i++) {
1993            if (tmp[i] != pkcsBlock[i]) {
1994                break;
1995            }
1996        }
1997        if (tmp[i] != (pkcsBlock[i] ^ 0x01)) {
1998            #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
1999            XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
2000            #endif
2001            WOLFSSL_MSG("RsaUnPad_PSS: Padding Error Match");
2002            return PSS_SALTLEN_RECOVER_E;
2003        }
2004        saltLen = maskLen - (i + 1);
2005    }
2006    else
2007#endif
2008    {
2009        for (i = 0; i < maskLen - 1 - saltLen; i++) {
2010            if (tmp[i] != pkcsBlock[i]) {
2011                #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
2012                XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
2013                #endif
2014                WOLFSSL_MSG("RsaUnPad_PSS: Padding Error Match");
2015                return PSS_SALTLEN_E;
2016            }
2017        }
2018        if (tmp[i] != (pkcsBlock[i] ^ 0x01)) {
2019            #if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
2020            XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
2021            #endif
2022            WOLFSSL_MSG("RsaUnPad_PSS: Padding Error End");
2023            return PSS_SALTLEN_E;
2024        }
2025    }
2026    xorbuf(pkcsBlock + i, tmp + i, (word32)(maskLen - i));
2027
2028#if !defined(WOLFSSL_NO_MALLOC) || defined(WOLFSSL_STATIC_MEMORY)
2029    XFREE(tmp, heap, DYNAMIC_TYPE_RSA_BUFFER);
2030#endif
2031
2032    *output = pkcsBlock + maskLen - saltLen;
2033    return saltLen + hLen;
2034}
2035#endif
2036
2037/* UnPad plaintext, set start to *output, return length of plaintext,
2038 * < 0 on error */
2039static int RsaUnPad(const byte *pkcsBlock, unsigned int pkcsBlockLen,
2040                    const byte **output, byte padValue)
2041{
2042    int    ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG);
2043    word16 i;
2044
2045    if (output == NULL || pkcsBlockLen < 2 || pkcsBlockLen > 0xFFFF) {
2046        return BAD_FUNC_ARG;
2047    }
2048
2049    if (padValue == RSA_BLOCK_TYPE_1) {
2050        /* First byte must be 0x00 and Second byte, block type, 0x01 */
2051        if (pkcsBlock[0] != 0 || pkcsBlock[1] != RSA_BLOCK_TYPE_1) {
2052            WOLFSSL_MSG("RsaUnPad error, invalid formatting");
2053            return RSA_PAD_E;
2054        }
2055
2056        /* check the padding until we find the separator */
2057        for (i = 2; i < pkcsBlockLen; ) {
2058            if (pkcsBlock[i++] != 0xFF) {
2059                break;
2060            }
2061        }
2062
2063        /* Minimum of 11 bytes of pre-message data and must have separator. */
2064        if (i < RSA_MIN_PAD_SZ || pkcsBlock[i-1] != 0) {
2065            WOLFSSL_MSG("RsaUnPad error, bad formatting");
2066            return RSA_PAD_E;
2067        }
2068
2069        *output = (const byte *)(pkcsBlock + i);
2070        ret = (int)pkcsBlockLen - i;
2071    }
2072#ifndef WOLFSSL_RSA_VERIFY_ONLY
2073    else {
2074        unsigned int    j;
2075        volatile word16 pastSep = 0;
2076        volatile byte   invalid = 0;
2077        volatile byte   minPad;
2078        volatile int    invalidMask;
2079        byte inv;
2080        word16 sep;
2081
2082        i = 0;
2083        /* Decrypted with private key - unpad must be constant time. */
2084        for (j = 2; j < pkcsBlockLen; j++) {
2085           /* Update i if not passed the separator and at separator. */
2086            i |= (word16)(~pastSep) & ctMask16Eq(pkcsBlock[j], 0x00) &
2087                (word16)(j + 1);
2088            pastSep |= ctMask16Eq(pkcsBlock[j], 0x00);
2089        }
2090
2091        /* Snapshot volatiles to avoid multiple volatile accesses per
2092         * expression. */
2093        inv = invalid;
2094        sep = pastSep;
2095
2096        /* Minimum of 11 bytes of pre-message data - including leading 0x00. */
2097        minPad = ctMaskLT(i, RSA_MIN_PAD_SZ);
2098        inv |= minPad;
2099        /* Must have seen separator. */
2100        inv |= (byte)~sep;
2101        /* First byte must be 0x00. */
2102        inv |= ctMaskNotEq(pkcsBlock[0], 0x00);
2103        /* Check against expected block type: padValue */
2104        inv |= ctMaskNotEq(pkcsBlock[1], padValue);
2105
2106        invalid = inv;
2107        *output = (const byte *)(pkcsBlock + i);
2108        invalidMask = (int)-1 + (int)(inv >> 7);
2109        ret = invalidMask & ((int)pkcsBlockLen - i);
2110    }
2111#endif
2112
2113    return ret;
2114}
2115
2116/* helper function to direct unpadding
2117 *
2118 * bits is the key modulus size in bits
2119 */
2120int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, byte** out,
2121                   byte padValue, int padType, enum wc_HashType hType,
2122                   int mgf, byte* optLabel, word32 labelLen, int saltLen,
2123                   int bits, void* heap)
2124{
2125    int ret;
2126
2127    switch (padType) {
2128        case WC_RSA_PKCSV15_PAD:
2129            /*WOLFSSL_MSG("wolfSSL Using RSA PKCSV15 un-padding");*/
2130            ret = RsaUnPad(pkcsBlock, pkcsBlockLen, (const byte **)(void *)out,
2131                           padValue);
2132            break;
2133
2134    #ifndef WC_NO_RSA_OAEP
2135        case WC_RSA_OAEP_PAD:
2136            WOLFSSL_MSG("wolfSSL Using RSA OAEP un-padding");
2137            ret = RsaUnPad_OAEP((byte*)pkcsBlock, pkcsBlockLen, out,
2138                                        hType, mgf, optLabel, labelLen, heap);
2139            break;
2140    #endif
2141
2142    #ifdef WC_RSA_PSS
2143        case WC_RSA_PSS_PAD:
2144            WOLFSSL_MSG("wolfSSL Using RSA PSS un-padding");
2145            ret = RsaUnPad_PSS((byte*)pkcsBlock, pkcsBlockLen, out, hType, mgf,
2146                                                           saltLen, bits, heap);
2147            break;
2148    #endif
2149
2150    #ifdef WC_RSA_NO_PADDING
2151        case WC_RSA_NO_PAD:
2152            WOLFSSL_MSG("wolfSSL Using NO un-padding");
2153
2154            /* In the case of no padding being used check that input is exactly
2155             * the RSA key length */
2156            if (bits <= 0 || pkcsBlockLen !=
2157                         ((word32)(bits+WOLFSSL_BIT_SIZE-1)/WOLFSSL_BIT_SIZE)) {
2158                WOLFSSL_MSG("Bad input size");
2159                ret = RSA_PAD_E;
2160            }
2161            else {
2162                if (out != NULL) {
2163                    *out = pkcsBlock;
2164                }
2165                ret = (int)pkcsBlockLen;
2166            }
2167            break;
2168    #endif /* WC_RSA_NO_PADDING */
2169
2170        default:
2171            WOLFSSL_MSG("Unknown RSA UnPad Type");
2172            ret = RSA_PAD_E;
2173    }
2174
2175    /* silence warning if not used with padding scheme */
2176    (void)hType;
2177    (void)mgf;
2178    (void)optLabel;
2179    (void)labelLen;
2180    (void)saltLen;
2181    (void)bits;
2182    (void)heap;
2183
2184    return ret;
2185}
2186
2187int wc_hash2mgf(enum wc_HashType hType)
2188{
2189    switch (hType) {
2190    case WC_HASH_TYPE_NONE:
2191        return WC_MGF1NONE;
2192    case WC_HASH_TYPE_SHA:
2193#ifndef NO_SHA
2194        return WC_MGF1SHA1;
2195#else
2196        break;
2197#endif
2198    case WC_HASH_TYPE_SHA224:
2199#ifdef WOLFSSL_SHA224
2200        return WC_MGF1SHA224;
2201#else
2202        break;
2203#endif
2204    case WC_HASH_TYPE_SHA256:
2205#ifndef NO_SHA256
2206        return WC_MGF1SHA256;
2207#else
2208        break;
2209#endif
2210    case WC_HASH_TYPE_SHA384:
2211#ifdef WOLFSSL_SHA384
2212        return WC_MGF1SHA384;
2213#else
2214        break;
2215#endif
2216    case WC_HASH_TYPE_SHA512:
2217#ifdef WOLFSSL_SHA512
2218        return WC_MGF1SHA512;
2219#else
2220        break;
2221#endif
2222    case WC_HASH_TYPE_SHA512_224:
2223#if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
2224        return WC_MGF1SHA512_224;
2225#else
2226        break;
2227#endif
2228    case WC_HASH_TYPE_SHA512_256:
2229#if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
2230        return WC_MGF1SHA512_256;
2231#else
2232        break;
2233#endif
2234    case WC_HASH_TYPE_MD2:
2235    case WC_HASH_TYPE_MD4:
2236    case WC_HASH_TYPE_MD5:
2237    case WC_HASH_TYPE_MD5_SHA:
2238    case WC_HASH_TYPE_SHA3_224:
2239#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
2240        return WC_MGF1SHA3_224;
2241#else
2242        break;
2243#endif
2244    case WC_HASH_TYPE_SHA3_256:
2245#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
2246        return WC_MGF1SHA3_256;
2247#else
2248        break;
2249#endif
2250    case WC_HASH_TYPE_SHA3_384:
2251#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
2252        return WC_MGF1SHA3_384;
2253#else
2254        break;
2255#endif
2256    case WC_HASH_TYPE_SHA3_512:
2257#if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
2258        return WC_MGF1SHA3_512;
2259#else
2260        break;
2261#endif
2262    case WC_HASH_TYPE_BLAKE2B:
2263    case WC_HASH_TYPE_BLAKE2S:
2264    case WC_HASH_TYPE_SM3:
2265        break;
2266#ifdef WOLFSSL_SHAKE128
2267    case WC_HASH_TYPE_SHAKE128:
2268        return WC_MGF1SHAKE128;
2269#else
2270    case WC_HASH_TYPE_SHAKE128:
2271        break;
2272#endif
2273#ifdef WOLFSSL_SHAKE256
2274    case WC_HASH_TYPE_SHAKE256:
2275        return WC_MGF1SHAKE256;
2276#else
2277    case WC_HASH_TYPE_SHAKE256:
2278        break;
2279#endif
2280    default:
2281        break;
2282    }
2283    WOLFSSL_MSG("Unrecognized or unsupported hash function");
2284    return WC_MGF1NONE;
2285}
2286
2287#ifdef WC_RSA_NONBLOCK
2288static int wc_RsaFunctionNonBlock(const byte* in, word32 inLen, byte* out,
2289                          word32* outLen, int type, RsaKey* key)
2290{
2291    int    ret = 0;
2292#ifdef USE_FAST_MATH
2293    word32 keyLen, len;
2294#endif
2295    /* SP non-blocking RSA wrappers depend on sp_<N>_mod_exp_<W>_nb,
2296     * which the SP generator only emits when (!RSA_PUBLIC_ONLY ||
2297     * HAVE_SP_DH). Match that gate here so the dispatch is omitted when
2298     * those symbols are not available. */
2299#if defined(WOLFSSL_HAVE_SP_RSA) && defined(WOLFSSL_SP_NONBLOCK) && \
2300    defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) && \
2301    (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_HAVE_SP_DH))
2302    int bits;
2303#endif
2304
2305    if (key == NULL || key->nb == NULL) {
2306        return BAD_FUNC_ARG;
2307    }
2308
2309#if defined(WOLFSSL_HAVE_SP_RSA) && defined(WOLFSSL_SP_NONBLOCK) && \
2310    defined(WOLFSSL_SP_SMALL) && !defined(WOLFSSL_SP_FAST_MODEXP) && \
2311    (!defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_HAVE_SP_DH))
2312    bits = mp_count_bits(&key->n);
2313#ifndef WOLFSSL_SP_NO_2048
2314    if (bits == 2048) {
2315        if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) {
2316            return sp_RsaPublic_2048_nb(&key->nb->sp_ctx, in, inLen,
2317                       &key->e, &key->n, out, outLen);
2318        }
2319    #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
2320        (defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM))
2321        return sp_RsaPrivate_2048_nb(&key->nb->sp_ctx, in, inLen,
2322                   &key->d, &key->n, out, outLen);
2323    #endif
2324    }
2325#endif
2326#ifndef WOLFSSL_SP_NO_3072
2327    if (bits == 3072) {
2328        if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) {
2329            return sp_RsaPublic_3072_nb(&key->nb->sp_ctx, in, inLen,
2330                       &key->e, &key->n, out, outLen);
2331        }
2332    #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
2333        (defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM))
2334        return sp_RsaPrivate_3072_nb(&key->nb->sp_ctx, in, inLen,
2335                   &key->d, &key->n, out, outLen);
2336    #endif
2337    }
2338#endif
2339#ifdef WOLFSSL_SP_4096
2340    if (bits == 4096) {
2341        if (type == RSA_PUBLIC_ENCRYPT || type == RSA_PUBLIC_DECRYPT) {
2342            return sp_RsaPublic_4096_nb(&key->nb->sp_ctx, in, inLen,
2343                       &key->e, &key->n, out, outLen);
2344        }
2345    #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
2346        (defined(SP_RSA_PRIVATE_EXP_D) || defined(RSA_LOW_MEM))
2347        return sp_RsaPrivate_4096_nb(&key->nb->sp_ctx, in, inLen,
2348                   &key->d, &key->n, out, outLen);
2349    #endif
2350    }
2351#endif
2352#endif /* SP nonblock RSA */
2353
2354#ifdef USE_FAST_MATH
2355    if (key->nb->exptmod.state == TFM_EXPTMOD_NB_INIT) {
2356        if (mp_init(&key->nb->tmp) != MP_OKAY) {
2357            ret = MP_INIT_E;
2358        }
2359
2360        if (ret == 0) {
2361            if (mp_read_unsigned_bin(&key->nb->tmp, (byte*)in, inLen) != MP_OKAY) {
2362                ret = MP_READ_E;
2363            }
2364        }
2365    }
2366
2367    if (ret == 0) {
2368        switch(type) {
2369#if !defined(WOLFSSL_RSA_PUBLIC_ONLY)
2370        case RSA_PRIVATE_DECRYPT:
2371        case RSA_PRIVATE_ENCRYPT:
2372            ret = fp_exptmod_nb(&key->nb->exptmod, &key->nb->tmp, &key->d,
2373                &key->n, &key->nb->tmp);
2374            if (ret == FP_WOULDBLOCK)
2375                return ret;
2376            if (ret != MP_OKAY)
2377                ret = MP_EXPTMOD_E;
2378            break;
2379#endif
2380        case RSA_PUBLIC_ENCRYPT:
2381        case RSA_PUBLIC_DECRYPT:
2382            ret = fp_exptmod_nb(&key->nb->exptmod, &key->nb->tmp, &key->e,
2383                &key->n, &key->nb->tmp);
2384            if (ret == FP_WOULDBLOCK)
2385                return ret;
2386            if (ret != MP_OKAY)
2387                ret = MP_EXPTMOD_E;
2388            break;
2389        default:
2390            ret = RSA_WRONG_TYPE_E;
2391            break;
2392        }
2393    }
2394
2395    if (ret == 0) {
2396        keyLen = wc_RsaEncryptSize(key);
2397        if (keyLen > *outLen)
2398            ret = RSA_BUFFER_E;
2399    }
2400    if (ret == 0) {
2401        len = mp_unsigned_bin_size(&key->nb->tmp);
2402
2403        /* pad front w/ zeros to match key length */
2404        while (len < keyLen) {
2405            *out++ = 0x00;
2406            len++;
2407        }
2408
2409        *outLen = keyLen;
2410
2411        /* convert */
2412        if (mp_to_unsigned_bin(&key->nb->tmp, out) != MP_OKAY) {
2413             ret = MP_TO_E;
2414        }
2415    }
2416
2417    mp_clear(&key->nb->tmp);
2418#else
2419    /* No non-blocking backend available for this build. The SP non-block
2420     * dispatch above only matches enabled key sizes; if we reach this
2421     * point the key is not 2048/3072/4096 (or SP RSA itself isn't built)
2422     * and TFM fastmath isn't compiled in either. */
2423    (void)in;
2424    (void)inLen;
2425    (void)out;
2426    (void)outLen;
2427    (void)type;
2428    ret = NOT_COMPILED_IN;
2429#endif /* USE_FAST_MATH */
2430
2431    return ret;
2432}
2433#endif /* WC_RSA_NONBLOCK */
2434
2435#ifdef WOLFSSL_XILINX_CRYPT
2436/*
2437 * Xilinx hardened crypto acceleration.
2438 *
2439 * Returns 0 on success and negative values on error.
2440 */
2441static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
2442                          word32* outLen, int type, RsaKey* key, WC_RNG* rng)
2443{
2444    int    ret = 0;
2445    word32 keyLen;
2446    (void)rng;
2447
2448    keyLen = wc_RsaEncryptSize(key);
2449    if (keyLen > *outLen) {
2450        WOLFSSL_MSG("Output buffer is not big enough");
2451        return BAD_FUNC_ARG;
2452    }
2453
2454    if (inLen != keyLen) {
2455        WOLFSSL_MSG("Expected that inLen equals RSA key length");
2456        return BAD_FUNC_ARG;
2457    }
2458
2459    switch(type) {
2460    case RSA_PRIVATE_DECRYPT:
2461    case RSA_PRIVATE_ENCRYPT:
2462    #ifdef WOLFSSL_XILINX_CRYPTO_OLD
2463        /* Currently public exponent is loaded by default.
2464         * In SDK 2017.1 RSA exponent values are expected to be of 4 bytes
2465         * leading to private key operations with Xsecure_RsaDecrypt not being
2466         * supported */
2467        ret = RSA_WRONG_TYPE_E;
2468    #else
2469        {
2470            byte *d;
2471            int dSz;
2472#if !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
2473            XSecure_Rsa rsa;
2474#endif
2475
2476#if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
2477            dSz = WOLFSSL_XSECURE_RSA_KEY_SIZE * 2;
2478#else
2479            dSz = mp_unsigned_bin_size(&key->d);
2480#endif
2481            d = (byte*)XMALLOC(dSz, key->heap, DYNAMIC_TYPE_PRIVATE_KEY);
2482            if (d == NULL) {
2483                ret = MEMORY_E;
2484            } else {
2485#if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
2486                XMEMSET(d, 0, dSz);
2487                XMEMCPY(d, key->mod, key->mSz);
2488                ret = mp_to_unsigned_bin(&key->d, &d[WOLFSSL_XSECURE_RSA_KEY_SIZE]);
2489#else
2490                ret = mp_to_unsigned_bin(&key->d, d);
2491                XSecure_RsaInitialize(&rsa, key->mod, NULL, d);
2492#endif
2493            }
2494
2495            if (ret == 0) {
2496#if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
2497                WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)d, dSz);
2498                WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)in, inLen);
2499                if (XSecure_RsaPrivateDecrypt(&(key->xSec.cinst), XIL_CAST_U64(d),
2500                                              XIL_CAST_U64(in), inLen,
2501                                              XIL_CAST_U64(out)) != XST_SUCCESS) {
2502                    ret = BAD_STATE_E;
2503                }
2504                WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)out, inLen);
2505#else
2506                if (XSecure_RsaPrivateDecrypt(&rsa, (u8*)in, inLen, out) !=
2507                        XST_SUCCESS) {
2508                    ret = BAD_STATE_E;
2509                }
2510#endif
2511            }
2512
2513            if (d != NULL)
2514                ForceZero(d, dSz);
2515            XFREE(d, key->heap, DYNAMIC_TYPE_PRIVATE_KEY);
2516        }
2517    #endif
2518        break;
2519    case RSA_PUBLIC_ENCRYPT:
2520    case RSA_PUBLIC_DECRYPT:
2521#if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
2522        WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)key->mod,
2523                                       WOLFSSL_XSECURE_RSA_KEY_SIZE + 4);
2524        WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)in, inLen);
2525        if (XSecure_RsaPublicEncrypt(&(key->xSec.cinst),
2526                                     XIL_CAST_U64(key->mod),
2527                                     XIL_CAST_U64(in), inLen,
2528                                     XIL_CAST_U64(out))) {
2529            WOLFSSL_MSG("RSA public operation failed");
2530            ret = BAD_STATE_E;
2531        }
2532        WOLFSSL_XIL_DCACHE_FLUSH_RANGE((UINTPTR)out, inLen);
2533#elif defined(WOLFSSL_XILINX_CRYPTO_OLD)
2534        if (XSecure_RsaDecrypt(&(key->xRsa), in, out) != XST_SUCCESS) {
2535            ret = BAD_STATE_E;
2536        }
2537#else
2538        /* starting at Xilinx release 2019 the function XSecure_RsaDecrypt was removed */
2539        if (XSecure_RsaPublicEncrypt(&(key->xRsa), (u8*)in, inLen, out) != XST_SUCCESS) {
2540            WOLFSSL_MSG("Error happened when calling hardware RSA public operation");
2541            ret = BAD_STATE_E;
2542        }
2543#endif
2544        break;
2545    default:
2546        ret = RSA_WRONG_TYPE_E;
2547    }
2548
2549    *outLen = keyLen;
2550
2551    return ret;
2552}
2553
2554#elif defined(WOLFSSL_AFALG_XILINX_RSA)
2555#ifndef ERROR_OUT
2556#define ERROR_OUT(x) ret = (x); goto done
2557#endif
2558
2559static const char WC_TYPE_ASYMKEY[] = "skcipher";
2560static const char WC_NAME_RSA[] = "xilinx-zynqmp-rsa";
2561#ifndef MAX_XILINX_RSA_KEY
2562    /* max key size of 4096 bits / 512 bytes */
2563    #define MAX_XILINX_RSA_KEY 512
2564#endif
2565static const byte XILINX_RSA_FLAG[] = {0x1};
2566
2567
2568/* AF_ALG implementation of RSA */
2569static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
2570                          word32* outLen, int type, RsaKey* key, WC_RNG* rng)
2571{
2572    struct msghdr   msg;
2573    struct cmsghdr* cmsg;
2574    struct iovec      iov;
2575    byte*  keyBuf   = NULL;
2576    word32 keyBufSz = 0;
2577    char cbuf[CMSG_SPACE(4) + CMSG_SPACE(sizeof(struct af_alg_iv) + 1)] = {0};
2578    int    ret = 0;
2579    int    op  = 0;    /* decryption vs encryption flag */
2580    word32 keyLen;
2581
2582    /* input and output buffer need to be aligned */
2583    ALIGN64 byte outBuf[MAX_XILINX_RSA_KEY];
2584    ALIGN64 byte inBuf[MAX_XILINX_RSA_KEY];
2585
2586    XMEMSET(&msg, 0, sizeof(struct msghdr));
2587    (void)rng;
2588
2589    keyLen = wc_RsaEncryptSize(key);
2590    if (keyLen > *outLen) {
2591        ERROR_OUT(RSA_BUFFER_E);
2592    }
2593
2594    if (keyLen > MAX_XILINX_RSA_KEY) {
2595        WOLFSSL_MSG("RSA key size larger than supported");
2596        ERROR_OUT(BAD_FUNC_ARG);
2597    }
2598
2599    if (inLen != keyLen) {
2600        WOLFSSL_MSG("Expected that inLen equals RSA key length");
2601        ERROR_OUT(BAD_FUNC_ARG);
2602    }
2603
2604    if ((keyBuf = (byte*)XMALLOC(keyLen * 2, key->heap, DYNAMIC_TYPE_KEY))
2605            == NULL) {
2606        ERROR_OUT(MEMORY_E);
2607    }
2608
2609    if ((ret = mp_to_unsigned_bin(&(key->n), keyBuf)) != MP_OKAY) {
2610        ERROR_OUT(MP_TO_E);
2611    }
2612
2613    switch(type) {
2614        case RSA_PRIVATE_DECRYPT:
2615        case RSA_PRIVATE_ENCRYPT:
2616            op = 1; /* set as decrypt */
2617            {
2618                keyBufSz = mp_unsigned_bin_size(&(key->d));
2619                if ((mp_to_unsigned_bin(&(key->d), keyBuf + keyLen))
2620                        != MP_OKAY) {
2621                    ERROR_OUT(MP_TO_E);
2622                }
2623            #ifdef WOLFSSL_CHECK_MEM_ZERO
2624                /* Seed must be zeroized now that it has been used. */
2625                wc_MemZero_Add("RSA Sync Priv Enc/Dec keyBuf", keyBuf + keyLen,
2626                    keyBufSz);
2627            #endif
2628            }
2629            break;
2630
2631        case RSA_PUBLIC_DECRYPT:
2632        case RSA_PUBLIC_ENCRYPT: {
2633            word32 exp = 0;
2634            word32 eSz = mp_unsigned_bin_size(&(key->e));
2635            if ((mp_to_unsigned_bin(&(key->e), (byte*)&exp +
2636                            (sizeof(word32) - eSz))) != MP_OKAY) {
2637                ERROR_OUT(MP_TO_E);
2638            }
2639            keyBufSz = sizeof(word32);
2640            XMEMCPY(keyBuf + keyLen, (byte*)&exp, keyBufSz);
2641            break;
2642        }
2643
2644        default:
2645            ERROR_OUT(RSA_WRONG_TYPE_E);
2646    }
2647    keyBufSz += keyLen; /* add size of modulus */
2648
2649    /* check for existing sockets before creating new ones */
2650    if (key->alFd > 0) {
2651        close(key->alFd);
2652        key->alFd = WC_SOCK_NOTSET;
2653    }
2654    if (key->rdFd > 0) {
2655        close(key->rdFd);
2656        key->rdFd = WC_SOCK_NOTSET;
2657    }
2658
2659    /* create new sockets and set the key to use */
2660    if ((key->alFd = wc_Afalg_Socket()) < 0) {
2661        WOLFSSL_MSG("Unable to create socket");
2662        ERROR_OUT(key->alFd);
2663    }
2664    if ((key->rdFd = wc_Afalg_CreateRead(key->alFd, WC_TYPE_ASYMKEY,
2665                    WC_NAME_RSA)) < 0) {
2666        WOLFSSL_MSG("Unable to bind and create read/send socket");
2667        ERROR_OUT(key->rdFd);
2668    }
2669    if ((ret = setsockopt(key->alFd, SOL_ALG, ALG_SET_KEY, keyBuf,
2670                    keyBufSz)) < 0) {
2671        WOLFSSL_MSG("Error setting RSA key");
2672        ERROR_OUT(ret);
2673    }
2674
2675    msg.msg_control    = cbuf;
2676    msg.msg_controllen = sizeof(cbuf);
2677    cmsg = CMSG_FIRSTHDR(&msg);
2678    if ((ret = wc_Afalg_SetOp(cmsg, op)) < 0) {
2679        ERROR_OUT(ret);
2680    }
2681
2682    /* set flag in IV spot, needed for Xilinx hardware acceleration use */
2683    cmsg = CMSG_NXTHDR(&msg, cmsg);
2684    if ((ret = wc_Afalg_SetIv(cmsg, (byte*)XILINX_RSA_FLAG,
2685                    sizeof(XILINX_RSA_FLAG))) != 0) {
2686        ERROR_OUT(ret);
2687    }
2688
2689    /* compose and send msg */
2690    XMEMCPY(inBuf, (byte*)in, inLen); /* for alignment */
2691    iov.iov_base = inBuf;
2692    iov.iov_len  = inLen;
2693    msg.msg_iov  = &iov;
2694    msg.msg_iovlen = 1;
2695    if ((ret = sendmsg(key->rdFd, &msg, 0)) <= 0) {
2696        ERROR_OUT(WC_AFALG_SOCK_E);
2697    }
2698
2699    if ((ret = read(key->rdFd, outBuf, inLen)) <= 0) {
2700        ERROR_OUT(WC_AFALG_SOCK_E);
2701    }
2702    XMEMCPY(out, outBuf, ret);
2703    *outLen = keyLen;
2704
2705done:
2706    /* clear key data and free buffer */
2707    if (keyBuf != NULL) {
2708        ForceZero(keyBuf, keyBufSz);
2709    }
2710    XFREE(keyBuf, key->heap, DYNAMIC_TYPE_KEY);
2711
2712    if (key->alFd > 0) {
2713        close(key->alFd);
2714        key->alFd = WC_SOCK_NOTSET;
2715    }
2716    if (key->rdFd > 0) {
2717        close(key->rdFd);
2718        key->rdFd = WC_SOCK_NOTSET;
2719    }
2720
2721    return ret;
2722}
2723
2724#elif defined(WOLFSSL_KCAPI_RSA)
2725static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
2726                              word32* outLen, int type, RsaKey* key,
2727                              WC_RNG* rng)
2728{
2729    int ret;
2730
2731    (void)rng;
2732
2733    switch(type) {
2734        case RSA_PRIVATE_DECRYPT:
2735        case RSA_PRIVATE_ENCRYPT:
2736            ret = KcapiRsa_Decrypt(key, in, inLen, out, outLen);
2737            break;
2738
2739        case RSA_PUBLIC_DECRYPT:
2740        case RSA_PUBLIC_ENCRYPT:
2741            ret = KcapiRsa_Encrypt(key, in, inLen, out, outLen);
2742            break;
2743
2744        default:
2745            ret = RSA_WRONG_TYPE_E;
2746    }
2747
2748    return ret;
2749}
2750
2751#else
2752#ifndef WOLF_CRYPTO_CB_ONLY_RSA
2753#ifdef WOLFSSL_HAVE_SP_RSA
2754static int RsaFunction_SP(const byte* in, word32 inLen, byte* out,
2755    word32* outLen, int type, RsaKey* key, WC_RNG* rng)
2756{
2757    (void)rng;
2758
2759#ifndef WOLFSSL_SP_NO_2048
2760    if (mp_count_bits(&key->n) == 2048) {
2761        switch(type) {
2762#ifndef WOLFSSL_RSA_PUBLIC_ONLY
2763        case RSA_PRIVATE_DECRYPT:
2764        case RSA_PRIVATE_ENCRYPT:
2765    #ifdef WC_RSA_BLINDING
2766            if (rng == NULL)
2767                return MISSING_RNG_E;
2768    #endif
2769    #ifndef RSA_LOW_MEM
2770            if ((mp_count_bits(&key->p) == 1024) &&
2771                    (mp_count_bits(&key->q) == 1024) &&
2772                    (mp_count_bits(&key->dP) > 0) &&
2773                    (mp_count_bits(&key->dQ) > 0) &&
2774                    (mp_count_bits(&key->u) > 0)) {
2775                return sp_RsaPrivate_2048(in, inLen, &key->d, &key->p, &key->q,
2776                                          &key->dP, &key->dQ, &key->u, &key->n,
2777                                          out, outLen);
2778            }
2779            break;
2780    #else
2781            return sp_RsaPrivate_2048(in, inLen, &key->d, NULL, NULL, NULL,
2782                                      NULL, NULL, &key->n, out, outLen);
2783    #endif
2784#endif
2785        case RSA_PUBLIC_ENCRYPT:
2786        case RSA_PUBLIC_DECRYPT:
2787            return sp_RsaPublic_2048(in, inLen, &key->e, &key->n, out, outLen);
2788        default:
2789            break;
2790        }
2791    }
2792#endif
2793#ifndef WOLFSSL_SP_NO_3072
2794    if (mp_count_bits(&key->n) == 3072) {
2795        switch(type) {
2796#ifndef WOLFSSL_RSA_PUBLIC_ONLY
2797        case RSA_PRIVATE_DECRYPT:
2798        case RSA_PRIVATE_ENCRYPT:
2799    #ifdef WC_RSA_BLINDING
2800            if (rng == NULL)
2801                return MISSING_RNG_E;
2802    #endif
2803    #ifndef RSA_LOW_MEM
2804            if ((mp_count_bits(&key->p) == 1536) &&
2805                    (mp_count_bits(&key->q) == 1536) &&
2806                    (mp_count_bits(&key->dP) > 0) &&
2807                    (mp_count_bits(&key->dQ) > 0) &&
2808                    (mp_count_bits(&key->u) > 0)) {
2809                return sp_RsaPrivate_3072(in, inLen, &key->d, &key->p, &key->q,
2810                                          &key->dP, &key->dQ, &key->u, &key->n,
2811                                          out, outLen);
2812            }
2813            break;
2814    #else
2815            return sp_RsaPrivate_3072(in, inLen, &key->d, NULL, NULL, NULL,
2816                                      NULL, NULL, &key->n, out, outLen);
2817    #endif
2818#endif
2819        case RSA_PUBLIC_ENCRYPT:
2820        case RSA_PUBLIC_DECRYPT:
2821            return sp_RsaPublic_3072(in, inLen, &key->e, &key->n, out, outLen);
2822        default:
2823            break;
2824        }
2825    }
2826#endif
2827#ifdef WOLFSSL_SP_4096
2828    if (mp_count_bits(&key->n) == 4096) {
2829        switch(type) {
2830#ifndef WOLFSSL_RSA_PUBLIC_ONLY
2831        case RSA_PRIVATE_DECRYPT:
2832        case RSA_PRIVATE_ENCRYPT:
2833    #ifdef WC_RSA_BLINDING
2834            if (rng == NULL)
2835                return MISSING_RNG_E;
2836    #endif
2837    #ifndef RSA_LOW_MEM
2838            if ((mp_count_bits(&key->p) == 2048) &&
2839                    (mp_count_bits(&key->q) == 2048) &&
2840                    (mp_count_bits(&key->dP) > 0) &&
2841                    (mp_count_bits(&key->dQ) > 0) &&
2842                    (mp_count_bits(&key->u) > 0)) {
2843                return sp_RsaPrivate_4096(in, inLen, &key->d, &key->p, &key->q,
2844                                          &key->dP, &key->dQ, &key->u, &key->n,
2845                                          out, outLen);
2846            }
2847            break;
2848    #else
2849            return sp_RsaPrivate_4096(in, inLen, &key->d, NULL, NULL, NULL,
2850                                      NULL, NULL, &key->n, out, outLen);
2851    #endif
2852#endif
2853        case RSA_PUBLIC_ENCRYPT:
2854        case RSA_PUBLIC_DECRYPT:
2855            return sp_RsaPublic_4096(in, inLen, &key->e, &key->n, out, outLen);
2856        default:
2857            break;
2858        }
2859    }
2860#endif
2861
2862    /* SP not able to do operation. */
2863    return WC_KEY_SIZE_E;
2864}
2865#endif
2866
2867#if !defined(WOLFSSL_SP_MATH)
2868#if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
2869static int RsaFunctionPrivate(mp_int* tmp, RsaKey* key, WC_RNG* rng)
2870{
2871    int    ret = 0;
2872#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
2873    mp_digit mp = 0;
2874    DECL_MP_INT_SIZE_DYN(rnd, mp_bitsused(&key->n), RSA_MAX_SIZE);
2875    DECL_MP_INT_SIZE_DYN(rndi, mp_bitsused(&key->n), RSA_MAX_SIZE);
2876#endif /* WC_RSA_BLINDING && !WC_NO_RNG */
2877
2878    (void)rng;
2879
2880#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
2881    NEW_MP_INT_SIZE(rnd, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA);
2882    NEW_MP_INT_SIZE(rndi, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA);
2883#ifdef MP_INT_SIZE_CHECK_NULL
2884    if ((rnd == NULL) || (rndi == NULL)) {
2885        FREE_MP_INT_SIZE(rnd, key->heap, DYNAMIC_TYPE_RSA);
2886        FREE_MP_INT_SIZE(rndi, key->heap, DYNAMIC_TYPE_RSA);
2887        return MEMORY_E;
2888    }
2889#endif
2890
2891    if ((INIT_MP_INT_SIZE(rnd, mp_bitsused(&key->n)) != MP_OKAY) ||
2892            (INIT_MP_INT_SIZE(rndi, mp_bitsused(&key->n)) != MP_OKAY)) {
2893        ret = MP_INIT_E;
2894    }
2895
2896    if (ret == 0) {
2897        /* blind */
2898        ret = mp_rand(rnd, mp_get_digit_count(&key->n), rng);
2899    }
2900    if (ret == 0) {
2901        /* rndi = 1/rnd mod n */
2902        if (mp_invmod(rnd, &key->n, rndi) != MP_OKAY) {
2903            ret = MP_INVMOD_E;
2904        }
2905    }
2906    if (ret == 0) {
2907    #ifdef WOLFSSL_CHECK_MEM_ZERO
2908        mp_memzero_add("RSA Private rnd", rnd);
2909        mp_memzero_add("RSA Private rndi", rndi);
2910    #endif
2911
2912        /* rnd = rnd^e */
2913    #ifndef WOLFSSL_SP_MATH_ALL
2914        if (mp_exptmod(rnd, &key->e, &key->n, rnd) != MP_OKAY) {
2915            ret = MP_EXPTMOD_E;
2916        }
2917    #else
2918        if (mp_exptmod_nct(rnd, &key->e, &key->n, rnd) != MP_OKAY) {
2919            ret = MP_EXPTMOD_E;
2920        }
2921    #endif
2922    }
2923
2924    if (ret == 0) {
2925        /* tmp = tmp*rnd mod n */
2926        if (mp_mulmod(tmp, rnd, &key->n, tmp) != MP_OKAY) {
2927            ret = MP_MULMOD_E;
2928        }
2929    }
2930#endif /* WC_RSA_BLINDING && !WC_NO_RNG */
2931
2932#ifdef RSA_LOW_MEM      /* half as much memory but twice as slow */
2933    if (ret == 0) {
2934        if (mp_exptmod(tmp, &key->d, &key->n, tmp) != MP_OKAY) {
2935            ret = MP_EXPTMOD_E;
2936        }
2937    }
2938#else
2939    if (ret == 0 && (mp_iszero(&key->p) || mp_iszero(&key->q) ||
2940            mp_iszero(&key->dP) || mp_iszero(&key->dQ) || mp_iszero(&key->u))) {
2941        if (mp_exptmod(tmp, &key->d, &key->n, tmp) != MP_OKAY) {
2942            ret = MP_EXPTMOD_E;
2943        }
2944    }
2945    else if (ret == 0) {
2946        mp_int* tmpa = tmp;
2947#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
2948        mp_int* tmpb = rnd;
2949#else
2950        DECL_MP_INT_SIZE_DYN(tmpb, mp_bitsused(&key->n), RSA_MAX_SIZE);
2951#endif
2952
2953#if !defined(WC_RSA_BLINDING) || defined(WC_NO_RNG)
2954        NEW_MP_INT_SIZE(tmpb, mp_bitsused(&key->n), key->heap,
2955            DYNAMIC_TYPE_RSA);
2956    #ifdef MP_INT_SIZE_CHECK_NULL
2957        if (tmpb == NULL) {
2958            ret = MEMORY_E;
2959        }
2960    #endif
2961        if ((ret == 0) && INIT_MP_INT_SIZE(tmpb, mp_bitsused(&key->n)) !=
2962                MP_OKAY) {
2963            ret = MP_INIT_E;
2964        }
2965#endif
2966
2967    #ifdef WOLFSSL_CHECK_MEM_ZERO
2968        if (ret == 0) {
2969            mp_memzero_add("RSA Sync tmpb", tmpb);
2970        }
2971    #endif
2972
2973        /* tmpb = tmp^dQ mod q */
2974        if (ret == 0 && mp_exptmod(tmp, &key->dQ, &key->q, tmpb) != MP_OKAY)
2975            ret = MP_EXPTMOD_E;
2976
2977        /* tmpa = tmp^dP mod p */
2978        if (ret == 0 && mp_exptmod(tmp, &key->dP, &key->p, tmpa) != MP_OKAY)
2979            ret = MP_EXPTMOD_E;
2980
2981        /* tmp = (tmp - tmpb) * qInv (mod p) */
2982    #if (defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL)) && \
2983                                              !defined(WOLFSSL_SP_INT_NEGATIVE))
2984        if (ret == 0 && mp_submod(tmpa, tmpb, &key->p, tmp) != MP_OKAY)
2985            ret = MP_SUB_E;
2986    #else
2987        if (ret == 0 && mp_sub(tmpa, tmpb, tmp) != MP_OKAY)
2988            ret = MP_SUB_E;
2989    #endif
2990
2991        if (ret == 0 && mp_mulmod(tmp, &key->u, &key->p, tmp) != MP_OKAY)
2992            ret = MP_MULMOD_E;
2993
2994        /* tmp = tmpb + q * tmp */
2995        if (ret == 0 && mp_mul(tmp, &key->q, tmp) != MP_OKAY)
2996            ret = MP_MUL_E;
2997
2998        if (ret == 0 && mp_add(tmp, tmpb, tmp) != MP_OKAY)
2999            ret = MP_ADD_E;
3000
3001#if !defined(WC_RSA_BLINDING) || defined(WC_NO_RNG)
3002        mp_forcezero(tmpb);
3003        FREE_MP_INT_SIZE(tmpb, key->heap, DYNAMIC_TYPE_RSA);
3004    #if !defined(MP_INT_SIZE_CHECK_NULL) && defined(WOLFSSL_CHECK_MEM_ZERO)
3005        mp_memzero_check(tmpb);
3006    #endif
3007#endif
3008    }
3009#endif   /* RSA_LOW_MEM */
3010
3011#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
3012    /* Multiply result (tmp) by blinding invertor (rndi).
3013     * Use Montgomery form to make operation more constant time.
3014     */
3015    if ((ret == 0) && (mp_montgomery_setup(&key->n, &mp) != MP_OKAY)) {
3016        ret = MP_MULMOD_E;
3017    }
3018    if ((ret == 0) && (mp_montgomery_calc_normalization(rnd, &key->n) !=
3019            MP_OKAY)) {
3020        ret = MP_MULMOD_E;
3021    }
3022    /* Convert blinding invert to Montgomery form. */
3023    if ((ret == 0) && (mp_mul(rndi, rnd, rndi) != MP_OKAY)) {
3024        ret = MP_MULMOD_E;
3025    }
3026    if ((ret == 0) && (mp_mod(rndi, &key->n, rndi) != MP_OKAY)) {
3027        ret = MP_MULMOD_E;
3028    }
3029    /* Multiply result by blinding invert. */
3030    if ((ret == 0) && (mp_mul(tmp, rndi, tmp) != MP_OKAY)) {
3031        ret = MP_MULMOD_E;
3032    }
3033    /* Reduce result. */
3034    if ((ret == 0) && (mp_montgomery_reduce_ct(tmp, &key->n, mp) != MP_OKAY)) {
3035        ret = MP_MULMOD_E;
3036    }
3037
3038    mp_forcezero(rndi);
3039    mp_forcezero(rnd);
3040    FREE_MP_INT_SIZE(rndi, key->heap, DYNAMIC_TYPE_RSA);
3041    FREE_MP_INT_SIZE(rnd, key->heap, DYNAMIC_TYPE_RSA);
3042#if !defined(MP_INT_SIZE_CHECK_NULL) && defined(WOLFSSL_CHECK_MEM_ZERO)
3043    mp_memzero_check(rnd);
3044    mp_memzero_check(rndi);
3045#endif
3046#endif /* WC_RSA_BLINDING && !WC_NO_RNG */
3047    return ret;
3048}
3049#endif
3050
3051static int RsaFunctionSync(const byte* in, word32 inLen, byte* out,
3052    word32* outLen, int type, RsaKey* key, WC_RNG* rng)
3053{
3054    DECL_MP_INT_SIZE_DYN(tmp, mp_bitsused(&key->n), RSA_MAX_SIZE);
3055    int    ret = 0;
3056
3057    (void)rng;
3058
3059    NEW_MP_INT_SIZE(tmp, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA);
3060#ifdef MP_INT_SIZE_CHECK_NULL
3061    if (tmp == NULL) {
3062        WOLFSSL_MSG("NEW_MP_INT_SIZE tmp is NULL, return MEMORY_E");
3063        return MEMORY_E;
3064    }
3065#endif
3066
3067    if (INIT_MP_INT_SIZE(tmp, mp_bitsused(&key->n)) != MP_OKAY) {
3068        WOLFSSL_MSG("INIT_MP_INT_SIZE failed.");
3069        ret = MP_INIT_E;
3070    }
3071
3072#ifndef TEST_UNPAD_CONSTANT_TIME
3073    if (ret == 0 && mp_read_unsigned_bin(tmp, in, inLen) != MP_OKAY)
3074        ret = MP_READ_E;
3075
3076#ifdef WOLFSSL_CHECK_MEM_ZERO
3077    if (ret == 0) {
3078        mp_memzero_add("RSA sync tmp", tmp);
3079    }
3080#endif
3081
3082    if (ret == 0) {
3083        switch(type) {
3084    #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
3085        case RSA_PRIVATE_DECRYPT:
3086        case RSA_PRIVATE_ENCRYPT:
3087        {
3088            ret = RsaFunctionPrivate(tmp, key, rng);
3089            break;
3090        }
3091    #endif
3092        case RSA_PUBLIC_ENCRYPT:
3093        case RSA_PUBLIC_DECRYPT:
3094            if (mp_exptmod_nct(tmp, &key->e, &key->n, tmp) != MP_OKAY) {
3095                WOLFSSL_MSG_CERT_LOG("mp_exptmod_nct failed");
3096                ret = MP_EXPTMOD_E;
3097            }
3098            break;
3099        default:
3100            ret = RSA_WRONG_TYPE_E;
3101            break;
3102        }
3103    }
3104
3105    if (ret == 0) {
3106        WOLFSSL_MSG("mp_to_unsigned_bin_len_ct...");
3107        if (mp_to_unsigned_bin_len_ct(tmp, out, (int)*outLen) != MP_OKAY) {
3108            WOLFSSL_MSG("mp_to_unsigned_bin_len_ct failed");
3109            ret = MP_TO_E;
3110        }
3111    }
3112#ifdef WOLFSSL_RSA_CHECK_D_ON_DECRYPT
3113    if ((ret == 0) && (type == RSA_PRIVATE_DECRYPT)) {
3114        mp_sub(&key->n, &key->p, tmp);
3115        mp_sub(tmp, &key->q, tmp);
3116        mp_add_d(tmp, 1, tmp);
3117        mp_mulmod(&key->d, &key->e, tmp, tmp);
3118        if (!mp_isone(tmp)) {
3119            ret = MP_EXPTMOD_E;
3120        }
3121    }
3122#endif
3123#else
3124    (void)type;
3125    (void)key;
3126    XMEMCPY(out, in, inLen);
3127#endif
3128
3129    mp_forcezero(tmp);
3130    FREE_MP_INT_SIZE(tmp, key->heap, DYNAMIC_TYPE_RSA);
3131#if !defined(MP_INT_SIZE_CHECK_NULL) && defined(WOLFSSL_CHECK_MEM_ZERO)
3132    mp_memzero_check(tmp);
3133#endif
3134    return ret;
3135}
3136#endif /* !WOLFSSL_SP_MATH */
3137
3138static int wc_RsaFunctionSync(const byte* in, word32 inLen, byte* out,
3139                          word32* outLen, int type, RsaKey* key, WC_RNG* rng)
3140{
3141    int ret;
3142    word32 keyLen;
3143
3144    ret = wc_RsaEncryptSize(key);
3145    if (ret < 0) {
3146#ifdef DEBUG_WOLFSSL
3147        WOLFSSL_MSG_EX("wc_RsaEncryptSize failed err = %d", ret);
3148#endif
3149        return ret;
3150    }
3151    keyLen = (word32)ret;
3152
3153    if (inLen > keyLen) {
3154        WOLFSSL_MSG("Expected that inLen be no longer RSA key length");
3155        return BAD_FUNC_ARG;
3156    }
3157    if (keyLen > *outLen) {
3158        WOLFSSL_MSG("Expected that outLen be no shorter RSA key length");
3159        return RSA_BUFFER_E;
3160    }
3161
3162    if (mp_iseven(&key->n)) {
3163        WOLFSSL_MSG("MP_VAL is even");
3164        return MP_VAL;
3165    }
3166
3167#if defined(WOLFSSL_NXP_CASPER_RSA_PUB_EXPTMOD)
3168    if (type == RSA_PUBLIC_DECRYPT || type == RSA_PUBLIC_ENCRYPT) {
3169        ret = casper_rsa_public_exptmod(in, inLen, out, outLen, key);
3170        if (ret == 0)
3171            return MP_OKAY;
3172        /* else fall through for software fallback */
3173    }
3174#endif
3175
3176#ifdef WOLFSSL_HAVE_SP_RSA
3177    ret = RsaFunction_SP(in, inLen, out, outLen, type, key, rng);
3178    if (ret != WC_NO_ERR_TRACE(WC_KEY_SIZE_E))
3179        return ret;
3180#endif /* WOLFSSL_HAVE_SP_RSA */
3181
3182#if defined(WOLFSSL_SP_MATH)
3183    (void)rng;
3184#ifndef WOLFSSL_HAVE_SP_RSA
3185    (void)in;
3186    (void)inLen;
3187    (void)out;
3188    (void)outLen;
3189    (void)type;
3190    (void)key;
3191    #error RSA SP option invalid (enable WOLFSSL_HAVE_SP_RSA or disable WOLFSSL_SP_MATH)
3192    return NOT_COMPILED_IN;
3193#else
3194    WOLFSSL_MSG("SP Key Size Error");
3195    return WC_KEY_SIZE_E;
3196#endif
3197#else
3198    *outLen = keyLen;
3199    return RsaFunctionSync(in, inLen, out, outLen, type, key, rng);
3200#endif /* WOLFSSL_SP_MATH */
3201} /* wc_RsaFunctionSync */
3202#endif /* WOLF_CRYPTO_CB_ONLY_RSA */
3203#endif
3204
3205#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
3206static int wc_RsaFunctionAsync(const byte* in, word32 inLen, byte* out,
3207                          word32* outLen, int type, RsaKey* key, WC_RNG* rng)
3208{
3209    int ret = 0;
3210
3211    (void)rng;
3212
3213#ifdef WOLFSSL_ASYNC_CRYPT_SW
3214    if (wc_AsyncSwInit(&key->asyncDev, ASYNC_SW_RSA_FUNC)) {
3215        WC_ASYNC_SW* sw = &key->asyncDev.sw;
3216        sw->rsaFunc.in = in;
3217        sw->rsaFunc.inSz = inLen;
3218        sw->rsaFunc.out = out;
3219        sw->rsaFunc.outSz = outLen;
3220        sw->rsaFunc.type = type;
3221        sw->rsaFunc.key = key;
3222        sw->rsaFunc.rng = rng;
3223        return WC_PENDING_E;
3224    }
3225#endif /* WOLFSSL_ASYNC_CRYPT_SW */
3226
3227#ifdef WC_RSA_NONBLOCK
3228    /* When a non-blocking context is attached and the SP nonblock backend
3229     * is available, drive the chunked state machine here. wolfAsync_DoSw
3230     * (line "if (ret == FP_WOULDBLOCK) ret = WC_PENDING_E;" at the bottom
3231     * of the SW switch in wolfcrypt/src/async.c, FP_WOULDBLOCK aliases
3232     * MP_WOULDBLOCK) translates per-yield MP_WOULDBLOCK into WC_PENDING_E
3233     * so the TLS / async event loop can drive the operation to completion. */
3234    if (key->nb != NULL) {
3235        return wc_RsaFunctionNonBlock(in, inLen, out, outLen, type, key);
3236    }
3237#endif
3238
3239    switch (type) {
3240#ifndef WOLFSSL_RSA_PUBLIC_ONLY
3241    case RSA_PRIVATE_DECRYPT:
3242    case RSA_PRIVATE_ENCRYPT:
3243    #ifdef HAVE_CAVIUM
3244        key->dataLen = key->n.raw.len;
3245        ret = NitroxRsaExptMod(in, inLen,
3246                               key->d.raw.buf, key->d.raw.len,
3247                               key->n.raw.buf, key->n.raw.len,
3248                               out, outLen, key);
3249    #elif defined(HAVE_INTEL_QA)
3250        #ifdef RSA_LOW_MEM
3251            ret = IntelQaRsaPrivate(&key->asyncDev, in, inLen,
3252                                    &key->d.raw, &key->n.raw,
3253                                    out, outLen);
3254        #else
3255            ret = IntelQaRsaCrtPrivate(&key->asyncDev, in, inLen,
3256                                &key->p.raw, &key->q.raw,
3257                                &key->dP.raw, &key->dQ.raw,
3258                                &key->u.raw,
3259                                out, outLen);
3260        #endif
3261    #else
3262        ret = wc_RsaFunctionSync(in, inLen, out, outLen, type, key, rng);
3263    #endif
3264        break;
3265#endif
3266
3267    case RSA_PUBLIC_ENCRYPT:
3268    case RSA_PUBLIC_DECRYPT:
3269    #ifdef HAVE_CAVIUM
3270        key->dataLen = key->n.raw.len;
3271        ret = NitroxRsaExptMod(in, inLen,
3272                               key->e.raw.buf, key->e.raw.len,
3273                               key->n.raw.buf, key->n.raw.len,
3274                               out, outLen, key);
3275    #elif defined(HAVE_INTEL_QA)
3276        ret = IntelQaRsaPublic(&key->asyncDev, in, inLen,
3277                               &key->e.raw, &key->n.raw,
3278                               out, outLen);
3279    #else
3280        ret = wc_RsaFunctionSync(in, inLen, out, outLen, type, key, rng);
3281    #endif
3282        break;
3283
3284    default:
3285        ret = RSA_WRONG_TYPE_E;
3286    }
3287
3288    return ret;
3289}
3290#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_RSA */
3291
3292#if defined(WC_RSA_DIRECT) || defined(WC_RSA_NO_PADDING) || \
3293    defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
3294/* Performs direct RSA computation without padding. The input and output must
3295 * match the key size (ex: 2048-bits = 256 bytes). Returns the size of the
3296 * output on success or negative value on failure. */
3297int wc_RsaDirect(const byte* in, word32 inLen, byte* out, word32* outSz,
3298        RsaKey* key, int type, WC_RNG* rng)
3299{
3300    int ret;
3301
3302    if (in == NULL || outSz == NULL || key == NULL) {
3303        return BAD_FUNC_ARG;
3304    }
3305
3306    /* sanity check on type of RSA operation */
3307    switch (type) {
3308        case RSA_PUBLIC_ENCRYPT:
3309        case RSA_PUBLIC_DECRYPT:
3310        case RSA_PRIVATE_ENCRYPT:
3311        case RSA_PRIVATE_DECRYPT:
3312            break;
3313        default:
3314            WOLFSSL_MSG("Bad RSA type");
3315            return BAD_FUNC_ARG;
3316    }
3317
3318    if ((ret = wc_RsaEncryptSize(key)) < 0) {
3319        return BAD_FUNC_ARG;
3320    }
3321
3322    if (inLen != (word32)ret) {
3323        WOLFSSL_MSG("Bad input length. Should be RSA key size");
3324        return BAD_FUNC_ARG;
3325    }
3326
3327    if (out == NULL) {
3328        *outSz = inLen;
3329        return WC_NO_ERR_TRACE(LENGTH_ONLY_E);
3330    }
3331
3332    switch (key->state) {
3333        case RSA_STATE_NONE:
3334        case RSA_STATE_ENCRYPT_PAD:
3335        case RSA_STATE_ENCRYPT_EXPTMOD:
3336        case RSA_STATE_DECRYPT_EXPTMOD:
3337        case RSA_STATE_DECRYPT_UNPAD:
3338            key->state = (type == RSA_PRIVATE_ENCRYPT ||
3339                    type == RSA_PUBLIC_ENCRYPT) ? RSA_STATE_ENCRYPT_EXPTMOD:
3340                                                  RSA_STATE_DECRYPT_EXPTMOD;
3341
3342            key->dataLen = *outSz;
3343
3344            ret = wc_RsaFunction(in, inLen, out, &key->dataLen, type, key, rng);
3345            if (ret >= 0 || ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
3346                key->state = (type == RSA_PRIVATE_ENCRYPT ||
3347                    type == RSA_PUBLIC_ENCRYPT) ? RSA_STATE_ENCRYPT_RES:
3348                                                  RSA_STATE_DECRYPT_RES;
3349            }
3350            if (ret < 0) {
3351                break;
3352            }
3353
3354            FALL_THROUGH;
3355
3356        case RSA_STATE_ENCRYPT_RES:
3357        case RSA_STATE_DECRYPT_RES:
3358            ret = (int)key->dataLen;
3359            break;
3360
3361        default:
3362            ret = BAD_STATE_E;
3363    }
3364
3365    /* if async pending then skip cleanup*/
3366    if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)
3367    #ifdef WC_RSA_NONBLOCK
3368        || ret == FP_WOULDBLOCK
3369    #endif
3370    ) {
3371        return ret;
3372    }
3373
3374    key->state = RSA_STATE_NONE;
3375    wc_RsaCleanup(key);
3376
3377    return ret;
3378}
3379#endif /* WC_RSA_DIRECT || WC_RSA_NO_PADDING || OPENSSL_EXTRA || \
3380        * OPENSSL_EXTRA_X509_SMALL */
3381
3382#if defined(WOLFSSL_CRYPTOCELL)
3383static int cc310_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
3384                            word32 outLen, RsaKey* key)
3385{
3386    CRYSError_t ret = 0;
3387    CRYS_RSAPrimeData_t primeData;
3388    int modulusSize = wc_RsaEncryptSize(key);
3389
3390    /* The out buffer must be at least modulus size bytes long. */
3391    if (outLen < modulusSize)
3392        return BAD_FUNC_ARG;
3393
3394    ret = CRYS_RSA_PKCS1v15_Encrypt(&wc_rndState,
3395                                    wc_rndGenVectFunc,
3396                                    &key->ctx.pubKey,
3397                                    &primeData,
3398                                    (byte*)in,
3399                                    inLen,
3400                                    out);
3401
3402    if (ret != SA_SILIB_RET_OK){
3403        WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Encrypt failed");
3404        return -1;
3405    }
3406
3407    return modulusSize;
3408}
3409static int cc310_RsaPublicDecrypt(const byte* in, word32 inLen, byte* out,
3410                            word32 outLen, RsaKey* key)
3411{
3412    CRYSError_t ret = 0;
3413    CRYS_RSAPrimeData_t primeData;
3414    word16 actualOutLen = outLen;
3415
3416    ret = CRYS_RSA_PKCS1v15_Decrypt(&key->ctx.privKey,
3417                                    &primeData,
3418                                    (byte*)in,
3419                                    inLen,
3420                                    out,
3421                                    &actualOutLen);
3422
3423    if (ret != SA_SILIB_RET_OK){
3424        WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Decrypt failed");
3425        return -1;
3426    }
3427    return actualOutLen;
3428}
3429
3430int cc310_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
3431                  word32 outLen, RsaKey* key, CRYS_RSA_HASH_OpMode_t mode)
3432{
3433    CRYSError_t ret = 0;
3434    word16 actualOutLen = outLen*sizeof(byte);
3435    CRYS_RSAPrivUserContext_t  contextPrivate;
3436
3437    ret =  CRYS_RSA_PKCS1v15_Sign(&wc_rndState,
3438                wc_rndGenVectFunc,
3439                &contextPrivate,
3440                &key->ctx.privKey,
3441                mode,
3442                (byte*)in,
3443                inLen,
3444                out,
3445                &actualOutLen);
3446
3447    if (ret != SA_SILIB_RET_OK){
3448        WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Sign failed");
3449        return -1;
3450    }
3451    return actualOutLen;
3452}
3453
3454int cc310_RsaSSL_Verify(const byte* in, word32 inLen, byte* sig,
3455                               RsaKey* key, CRYS_RSA_HASH_OpMode_t mode)
3456{
3457    CRYSError_t ret = 0;
3458    CRYS_RSAPubUserContext_t contextPub;
3459
3460    /* verify the signature in the sig pointer */
3461    ret =  CRYS_RSA_PKCS1v15_Verify(&contextPub,
3462                &key->ctx.pubKey,
3463                mode,
3464                (byte*)in,
3465                inLen,
3466                sig);
3467
3468    if (ret != SA_SILIB_RET_OK){
3469        WOLFSSL_MSG("CRYS_RSA_PKCS1v15_Verify failed");
3470        return -1;
3471    }
3472
3473    return ret;
3474}
3475#endif /* WOLFSSL_CRYPTOCELL */
3476
3477#ifndef WOLF_CRYPTO_CB_ONLY_RSA
3478#if !defined(NO_RSA_BOUNDS_CHECK)
3479/* Check that 1 < in < n-1. (Requirement of 800-56B.) */
3480int RsaFunctionCheckIn(const byte* in, word32 inLen, RsaKey* key,
3481    int checkSmallCt)
3482{
3483    int ret = 0;
3484    DECL_MP_INT_SIZE_DYN(c, mp_bitsused(&key->n), RSA_MAX_SIZE);
3485
3486    NEW_MP_INT_SIZE(c, mp_bitsused(&key->n), key->heap, DYNAMIC_TYPE_RSA);
3487#ifdef MP_INT_SIZE_CHECK_NULL
3488    if (c == NULL)
3489        ret = MEMORY_E;
3490#endif
3491
3492    if (ret == 0 && INIT_MP_INT_SIZE(c, mp_bitsused(&key->n)) != MP_OKAY) {
3493        ret = MP_INIT_E;
3494    }
3495    if (ret == 0) {
3496        if (mp_read_unsigned_bin(c, in, inLen) != 0)
3497            ret = MP_READ_E;
3498    }
3499    if (ret == 0) {
3500        /* check c > 1 */
3501        if (checkSmallCt && (mp_cmp_d(c, 1) != MP_GT))
3502            ret = RSA_OUT_OF_RANGE_E;
3503    }
3504    if (ret == 0) {
3505        /* add c+1 */
3506        if (mp_add_d(c, 1, c) != MP_OKAY)
3507            ret = MP_ADD_E;
3508    }
3509    if (ret == 0) {
3510        /* check c+1 < n */
3511        if (mp_cmp(c, &key->n) != MP_LT)
3512            ret = RSA_OUT_OF_RANGE_E;
3513    }
3514    mp_clear(c);
3515
3516    FREE_MP_INT_SIZE(c, key->heap, DYNAMIC_TYPE_RSA);
3517
3518    return ret;
3519}
3520#endif /* !NO_RSA_BOUNDS_CHECK */
3521#endif /* WOLF_CRYPTO_CB_ONLY_RSA */
3522
3523static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
3524                             word32* outLen, int type, RsaKey* key, WC_RNG* rng,
3525                             int checkSmallCt)
3526{
3527    int ret = 0;
3528#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3529    RsaPadding padding;
3530#endif
3531
3532    (void)rng;
3533    (void)checkSmallCt;
3534
3535    if (key == NULL || in == NULL || inLen == 0 || out == NULL ||
3536            outLen == NULL || *outLen == 0 || type == RSA_TYPE_UNKNOWN) {
3537        return BAD_FUNC_ARG;
3538    }
3539
3540#ifdef WOLF_CRYPTO_CB
3541    #ifndef WOLF_CRYPTO_CB_FIND
3542    if (key->devId != INVALID_DEVID)
3543    #endif
3544    {
3545    #if defined(WOLF_CRYPTO_CB_RSA_PAD)
3546        /* If we are here, either the RSA PAD callback was already called
3547         * and returned that it could not implement for that padding scheme,
3548         * or this is a public verify operation. Either way indicate to the
3549         * callback that this should be a raw RSA operation with no padding.*/
3550        XMEMSET(&padding, 0, sizeof(RsaPadding));
3551        padding.pad_type = WC_RSA_NO_PAD;
3552        ret = wc_CryptoCb_RsaPad(in, inLen, out,
3553                            outLen, type, key, rng, &padding);
3554    #else
3555        ret = wc_CryptoCb_Rsa(in, inLen, out, outLen, type, key, rng);
3556    #endif
3557        #ifndef WOLF_CRYPTO_CB_ONLY_RSA
3558        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
3559            return ret;
3560        /* fall-through when unavailable and try using software */
3561        #endif
3562        #ifdef WOLF_CRYPTO_CB_ONLY_RSA
3563        if (ret == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
3564            return NO_VALID_DEVID;
3565        }
3566        return ret;
3567        #endif
3568    }
3569#endif
3570
3571#ifdef WOLF_CRYPTO_CB_ONLY_RSA
3572    return NO_VALID_DEVID;
3573#else /* !WOLF_CRYPTO_CB_ONLY_RSA */
3574    SAVE_VECTOR_REGISTERS(return _svr_ret;);
3575
3576#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(TEST_UNPAD_CONSTANT_TIME) && \
3577    !defined(NO_RSA_BOUNDS_CHECK)
3578    if (type == RSA_PRIVATE_DECRYPT &&
3579        key->state == RSA_STATE_DECRYPT_EXPTMOD) {
3580
3581        ret = RsaFunctionCheckIn(in, inLen, key, checkSmallCt);
3582        if (ret != 0) {
3583            RESTORE_VECTOR_REGISTERS();
3584            return ret;
3585        }
3586    }
3587#endif /* !WOLFSSL_RSA_VERIFY_ONLY && !TEST_UNPAD_CONSTANT_TIME && \
3588        * !NO_RSA_BOUNDS_CHECK */
3589#if !defined(NO_RSA_BOUNDS_CHECK)
3590    if (type == RSA_PUBLIC_DECRYPT &&
3591        key->state == RSA_STATE_DECRYPT_EXPTMOD) {
3592
3593        ret = RsaFunctionCheckIn(in, inLen, key, checkSmallCt);
3594        if (ret != 0) {
3595            RESTORE_VECTOR_REGISTERS();
3596            return ret;
3597        }
3598    }
3599#endif
3600
3601#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
3602    if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA &&
3603                                                        key->n.raw.len > 0) {
3604        /* wc_RsaFunctionAsync dispatches to the SP nonblock state machine
3605         * in its compute path when key->nb is attached - wolfAsync_DoSw
3606         * (in wolfcrypt/src/async.c) translates per-yield FP_WOULDBLOCK
3607         * (alias of MP_WOULDBLOCK) into WC_PENDING_E so the TLS / async
3608         * event loop can drive completion. */
3609        ret = wc_RsaFunctionAsync(in, inLen, out, outLen, type, key, rng);
3610    }
3611    else
3612#endif
3613#ifdef WC_RSA_NONBLOCK
3614    if (key->nb) {
3615        /* Direct (non-async) nonblock dispatch - the caller (e.g. wolfcrypt
3616         * test) drives the loop on MP_WOULDBLOCK directly. Reached when no
3617         * async marker is set on the key. */
3618        ret = wc_RsaFunctionNonBlock(in, inLen, out, outLen, type, key);
3619    }
3620    else
3621#endif
3622    {
3623        ret = wc_RsaFunctionSync(in, inLen, out, outLen, type, key, rng);
3624    }
3625
3626    RESTORE_VECTOR_REGISTERS();
3627
3628    /* handle error */
3629    if (ret < 0 && ret != WC_NO_ERR_TRACE(WC_PENDING_E)
3630    #ifdef WC_RSA_NONBLOCK
3631        && ret != FP_WOULDBLOCK
3632    #endif
3633    ) {
3634        if (ret == WC_NO_ERR_TRACE(MP_EXPTMOD_E)) {
3635            /* This can happen due to incorrectly set FP_MAX_BITS or missing XREALLOC */
3636            WOLFSSL_MSG("RSA_FUNCTION MP_EXPTMOD_E: memory/config problem");
3637        }
3638
3639        key->state = RSA_STATE_NONE;
3640        wc_RsaCleanup(key);
3641    }
3642    return ret;
3643#endif /* !WOLF_CRYPTO_CB_ONLY_RSA */
3644}
3645
3646int wc_RsaFunction(const byte* in, word32 inLen, byte* out,
3647                          word32* outLen, int type, RsaKey* key, WC_RNG* rng)
3648{
3649    /* Always check for ciphertext of 0 or 1. (Shouldn't for OAEP decrypt.) */
3650    return wc_RsaFunction_ex(in, inLen, out, outLen, type, key, rng, 1);
3651}
3652
3653#ifndef WOLFSSL_RSA_VERIFY_ONLY
3654/* Internal Wrappers */
3655/* Gives the option of choosing padding type
3656   in : input to be encrypted
3657   inLen: length of input buffer
3658   out: encrypted output
3659   outLen: length of encrypted output buffer
3660   key   : wolfSSL initialized RSA key struct
3661   rng   : wolfSSL initialized random number struct
3662   rsa_type  : type of RSA: RSA_PUBLIC_ENCRYPT, RSA_PUBLIC_DECRYPT,
3663        RSA_PRIVATE_ENCRYPT or RSA_PRIVATE_DECRYPT
3664   pad_value: RSA_BLOCK_TYPE_1 or RSA_BLOCK_TYPE_2
3665   pad_type  : type of padding: WC_RSA_PKCSV15_PAD, WC_RSA_OAEP_PAD,
3666        WC_RSA_NO_PAD or WC_RSA_PSS_PAD
3667   hash  : type of hash algorithm to use found in wolfssl/wolfcrypt/hash.h
3668   mgf   : type of mask generation function to use
3669   label : optional label
3670   labelSz : size of optional label buffer
3671   saltLen : Length of salt used in PSS
3672   rng : random number generator */
3673static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
3674                            word32 outLen, RsaKey* key, int rsa_type,
3675                            byte pad_value, int pad_type,
3676                            enum wc_HashType hash, int mgf,
3677                            byte* label, word32 labelSz, int saltLen,
3678                            WC_RNG* rng)
3679{
3680    int ret = 0;
3681    int sz;
3682    int state;
3683#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3684    RsaPadding padding;
3685#endif
3686
3687    if (in == NULL || inLen == 0 || out == NULL || key == NULL) {
3688        return BAD_FUNC_ARG;
3689    }
3690
3691    sz = wc_RsaEncryptSize(key);
3692    if (sz > (int)outLen) {
3693        return RSA_BUFFER_E;
3694    }
3695
3696    if (sz < RSA_MIN_PAD_SZ || sz > (int)RSA_MAX_SIZE/8) {
3697        return WC_KEY_SIZE_E;
3698    }
3699
3700    if (inLen > (word32)(sz - RSA_MIN_PAD_SZ)) {
3701#ifdef WC_RSA_NO_PADDING
3702        /* In the case that no padding is used the input length can and should
3703         * be the same size as the RSA key. */
3704        if (pad_type != WC_RSA_NO_PAD)
3705#endif
3706        return RSA_BUFFER_E;
3707    }
3708
3709#ifndef WOLFSSL_BIND
3710    state = key->state;
3711#else
3712    /* Bind9 shares the EVP_PKEY struct across multiple threads so let's just
3713     * force a restart on each RsaPublicEncryptEx call for it. */
3714    state = RSA_STATE_NONE;
3715#ifdef WOLFSSL_ASYNC_CRYPT
3716#error wolfSSL does not handle building bind support with async crypto
3717#endif
3718#endif
3719    switch (state) {
3720    case RSA_STATE_NONE:
3721    case RSA_STATE_ENCRYPT_PAD:
3722    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \
3723            defined(HAVE_CAVIUM)
3724        if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA &&
3725                                 pad_type != WC_RSA_PSS_PAD && key->n.raw.buf) {
3726            /* Async operations that include padding */
3727            if (rsa_type == RSA_PUBLIC_ENCRYPT &&
3728                                                pad_value == RSA_BLOCK_TYPE_2) {
3729                key->state = RSA_STATE_ENCRYPT_RES;
3730                key->dataLen = key->n.raw.len;
3731                return NitroxRsaPublicEncrypt(in, inLen, out, outLen, key);
3732            }
3733            else if (rsa_type == RSA_PRIVATE_ENCRYPT &&
3734                                                pad_value == RSA_BLOCK_TYPE_1) {
3735                key->state = RSA_STATE_ENCRYPT_RES;
3736                key->dataLen = key->n.raw.len;
3737                return NitroxRsaSSL_Sign(in, inLen, out, outLen, key);
3738            }
3739        }
3740    #elif defined(WOLFSSL_CRYPTOCELL)
3741        if (rsa_type == RSA_PUBLIC_ENCRYPT &&
3742                                            pad_value == RSA_BLOCK_TYPE_2) {
3743
3744            return cc310_RsaPublicEncrypt(in, inLen, out, outLen, key);
3745        }
3746        else if (rsa_type == RSA_PRIVATE_ENCRYPT &&
3747                                         pad_value == RSA_BLOCK_TYPE_1) {
3748            return cc310_RsaSSL_Sign(in, inLen, out, outLen, key,
3749                                  cc310_hashModeRSA(hash, 0));
3750        }
3751    #elif defined(WOLFSSL_MICROCHIP_TA100)
3752        if (rsa_type == RSA_PUBLIC_ENCRYPT &&
3753                                            pad_value == RSA_BLOCK_TYPE_2) {
3754            if (key->uKeyH != 0) {
3755                return wc_Microchip_rsa_encrypt(in, inLen, out, outLen, key);
3756            }
3757            return WC_HW_E;
3758        }
3759        else if (rsa_type == RSA_PRIVATE_ENCRYPT &&
3760                                         pad_value == RSA_BLOCK_TYPE_1) {
3761            if (key->rKeyH != 0) {
3762                if (pad_type != WC_RSA_PSS_PAD) {
3763                    return WC_HW_E;
3764                }
3765                return wc_Microchip_rsa_sign(in, inLen, out, outLen, key);
3766            }
3767            return WC_HW_E;
3768        }
3769    #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
3770        if (rsa_type == RSA_PUBLIC_ENCRYPT && pad_value == RSA_BLOCK_TYPE_2) {
3771            return se050_rsa_public_encrypt(in, inLen, out, outLen, key,
3772                                            rsa_type, pad_value, pad_type, hash,
3773                                            mgf, label, labelSz, sz);
3774        }
3775        else if (rsa_type == RSA_PRIVATE_ENCRYPT &&
3776                 pad_value == RSA_BLOCK_TYPE_1 &&
3777                 pad_type != WC_RSA_PSS_PAD) {
3778            /* SE050 handles PKCS#1 v1.5 signing directly. PSS signing falls
3779             * through to software path because the SE050 PSS sign API
3780             * (Se05x_API_RSASign) is hash-then-sign and does not support
3781             * signing a pre-computed digest without double-hashing. */
3782            return se050_rsa_sign(in, inLen, out, outLen, key, rsa_type,
3783                                  pad_value, pad_type, hash, mgf, label,
3784                                  labelSz, sz);
3785        }
3786    #endif /* RSA CRYPTO HW */
3787
3788    #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3789        if (key->devId != INVALID_DEVID) {
3790            XMEMSET(&padding, 0, sizeof(RsaPadding));
3791            padding.pad_value = pad_value;
3792            padding.pad_type = pad_type;
3793            padding.hash = hash;
3794            padding.mgf = mgf;
3795            padding.label = label;
3796            padding.labelSz = labelSz;
3797            padding.saltLen = saltLen;
3798            ret = wc_CryptoCb_RsaPad(in, inLen, out, &outLen, rsa_type, key, rng,
3799                                     &padding);
3800
3801            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
3802                if (ret < 0) {
3803                    break;
3804                }
3805
3806                ret = outLen;
3807                break;
3808            }
3809        }
3810    #endif
3811        key->state = RSA_STATE_ENCRYPT_PAD;
3812        ret = wc_RsaPad_ex(in, inLen, out, (word32)sz, pad_value, rng, pad_type,
3813                           hash, mgf, label, labelSz, saltLen,
3814                           mp_count_bits(&key->n), key->heap);
3815        if (ret < 0) {
3816            break;
3817        }
3818
3819        key->state = RSA_STATE_ENCRYPT_EXPTMOD;
3820        FALL_THROUGH;
3821
3822    case RSA_STATE_ENCRYPT_EXPTMOD:
3823
3824        key->dataLen = outLen;
3825        ret = wc_RsaFunction(out, (word32)sz, out, &key->dataLen, rsa_type, key,
3826                             rng);
3827
3828        if (ret >= 0 || ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
3829            key->state = RSA_STATE_ENCRYPT_RES;
3830        }
3831        if (ret < 0) {
3832            break;
3833        }
3834
3835        FALL_THROUGH;
3836
3837    case RSA_STATE_ENCRYPT_RES:
3838        ret = (int)key->dataLen;
3839        break;
3840
3841    default:
3842        ret = BAD_STATE_E;
3843        break;
3844    }
3845
3846    /* if async pending then return and skip done cleanup below */
3847    if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)
3848    #ifdef WC_RSA_NONBLOCK
3849        || ret == FP_WOULDBLOCK
3850    #endif
3851    ) {
3852        return ret;
3853    }
3854
3855    key->state = RSA_STATE_NONE;
3856    wc_RsaCleanup(key);
3857
3858    return ret;
3859}
3860
3861#endif
3862
3863/* Gives the option of choosing padding type
3864   in : input to be decrypted
3865   inLen: length of input buffer
3866   out:  decrypted message
3867   outLen: length of decrypted message in bytes
3868   outPtr: optional inline output pointer (if provided doing inline)
3869   key   : wolfSSL initialized RSA key struct
3870   rsa_type  : type of RSA: RSA_PUBLIC_ENCRYPT, RSA_PUBLIC_DECRYPT,
3871        RSA_PRIVATE_ENCRYPT or RSA_PRIVATE_DECRYPT
3872   pad_value: RSA_BLOCK_TYPE_1 or RSA_BLOCK_TYPE_2
3873   pad_type  : type of padding: WC_RSA_PKCSV15_PAD, WC_RSA_OAEP_PAD,
3874        WC_RSA_NO_PAD, WC_RSA_PSS_PAD
3875   hash  : type of hash algorithm to use found in wolfssl/wolfcrypt/hash.h
3876   mgf   : type of mask generation function to use
3877   label : optional label
3878   labelSz : size of optional label buffer
3879   saltLen : Length of salt used in PSS
3880   rng : random number generator */
3881static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
3882                            word32 outLen, byte** outPtr, RsaKey* key,
3883                            int rsa_type, byte pad_value, int pad_type,
3884                            enum wc_HashType hash, int mgf,
3885                            byte* label, word32 labelSz, int saltLen,
3886                            WC_RNG* rng)
3887{
3888    int ret = WC_NO_ERR_TRACE(RSA_WRONG_TYPE_E);
3889    byte* pad = NULL;
3890#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
3891    RsaPadding padding;
3892#endif
3893
3894    if (in == NULL || inLen == 0 || out == NULL || key == NULL) {
3895        return BAD_FUNC_ARG;
3896    }
3897
3898    switch (key->state) {
3899    case RSA_STATE_NONE:
3900        key->dataLen = inLen;
3901
3902    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \
3903            defined(HAVE_CAVIUM)
3904        /* Async operations that include padding */
3905        if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA &&
3906                                                   pad_type != WC_RSA_PSS_PAD) {
3907#ifndef WOLFSSL_RSA_PUBLIC_ONLY
3908            if (rsa_type == RSA_PRIVATE_DECRYPT &&
3909                                                pad_value == RSA_BLOCK_TYPE_2) {
3910                key->state = RSA_STATE_DECRYPT_RES;
3911                key->data = NULL;
3912                return NitroxRsaPrivateDecrypt(in, inLen, out, &key->dataLen,
3913                                               key);
3914#endif
3915            }
3916            else if (rsa_type == RSA_PUBLIC_DECRYPT &&
3917                                                pad_value == RSA_BLOCK_TYPE_1) {
3918                key->state = RSA_STATE_DECRYPT_RES;
3919                key->data = NULL;
3920                return NitroxRsaSSL_Verify(in, inLen, out, &key->dataLen, key);
3921            }
3922        }
3923    #elif defined(WOLFSSL_CRYPTOCELL)
3924        if (rsa_type == RSA_PRIVATE_DECRYPT &&
3925                                            pad_value == RSA_BLOCK_TYPE_2) {
3926            ret = cc310_RsaPublicDecrypt(in, inLen, out, outLen, key);
3927            if (outPtr != NULL)
3928                *outPtr = out; /* for inline */
3929            return ret;
3930        }
3931        else if (rsa_type == RSA_PUBLIC_DECRYPT &&
3932                                            pad_value == RSA_BLOCK_TYPE_1) {
3933            return cc310_RsaSSL_Verify(in, inLen, out, key,
3934                                       cc310_hashModeRSA(hash, 0));
3935        }
3936    #elif defined(WOLFSSL_MICROCHIP_TA100)
3937        if (rsa_type == RSA_PRIVATE_DECRYPT &&
3938                                            pad_value == RSA_BLOCK_TYPE_2) {
3939            if (key->rKeyH != 0) {
3940                return wc_Microchip_rsa_decrypt(in, inLen, out, outLen, key);
3941            }
3942            return WC_HW_E;
3943        }
3944        /* Note: RSA_PUBLIC_DECRYPT (verify) is intentionally not intercepted
3945         * here. wc_Microchip_rsa_verify takes a digest as input, not a raw
3946         * signature blob; the proper TA100 short-circuit lives in the
3947         * wc_RsaPSS_CheckPadding / wc_RsaPSS_VerifyCheck path which has the
3948         * digest available. */
3949    #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
3950        if (rsa_type == RSA_PRIVATE_DECRYPT && pad_value == RSA_BLOCK_TYPE_2) {
3951            ret = se050_rsa_private_decrypt(in, inLen, out, outLen, key,
3952                                            rsa_type, pad_value, pad_type, hash,
3953                                            mgf, label, labelSz);
3954            if (outPtr != NULL) {
3955                *outPtr = out;
3956            }
3957            return ret;
3958        }
3959    #if !defined(WOLFSSL_SE050_NO_RSA_VERIFY)
3960        else if (rsa_type == RSA_PUBLIC_DECRYPT &&
3961                 pad_value == RSA_BLOCK_TYPE_1 &&
3962                 pad_type != WC_RSA_PSS_PAD) {
3963            /* SE050 handles PKCS#1 v1.5 verification directly. PSS
3964             * verification falls through to software path to match the
3965             * software PSS signing path (SE050 PSS sign uses hash-then-sign
3966             * which double-hashes a pre-computed digest). */
3967            ret = se050_rsa_verify(in, inLen, out, outLen, key, rsa_type,
3968                                   pad_value, pad_type, hash, mgf, label,
3969                                   labelSz);
3970            if (outPtr != NULL) {
3971                *outPtr = out;
3972            }
3973            return ret;
3974        }
3975    #endif /* !WOLFSSL_SE050_NO_RSA_VERIFY */
3976    #endif /* RSA CRYPTO HW */
3977
3978
3979#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
3980    !defined(WOLFSSL_NO_MALLOC)
3981        /* verify the tmp ptr is NULL, otherwise indicates bad state */
3982        if (key->data != NULL) {
3983            ret = BAD_STATE_E;
3984            break;
3985        }
3986
3987        /* if not doing this inline then allocate a buffer for it */
3988        if (outPtr == NULL) {
3989            key->data = (byte*)XMALLOC(inLen, key->heap,
3990                                                      DYNAMIC_TYPE_WOLF_BIGINT);
3991            key->dataIsAlloc = 1;
3992            if (key->data == NULL) {
3993                ret = MEMORY_E;
3994                break;
3995            }
3996            XMEMCPY(key->data, in, inLen);
3997            key->dataLen = inLen;
3998        }
3999        else {
4000            key->dataIsAlloc = 0;
4001            key->data = out;
4002        }
4003#endif
4004
4005        key->state = RSA_STATE_DECRYPT_EXPTMOD;
4006        FALL_THROUGH;
4007
4008    case RSA_STATE_DECRYPT_EXPTMOD:
4009#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD)
4010    if ((key->devId != INVALID_DEVID)
4011    #if !defined(WOLFSSL_RENESAS_FSPSM_CRYPTONLY) && \
4012        !defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
4013    && (rsa_type != RSA_PUBLIC_DECRYPT)
4014    #endif
4015    ) {
4016        /* Everything except verify goes to crypto cb if
4017         * WOLF_CRYPTO_CB_RSA_PAD defined */
4018        XMEMSET(&padding, 0, sizeof(RsaPadding));
4019        padding.pad_value = pad_value;
4020        padding.pad_type = pad_type;
4021        padding.hash = hash;
4022        padding.mgf = mgf;
4023        padding.label = label;
4024        padding.labelSz = labelSz;
4025        padding.saltLen = saltLen;
4026        ret = wc_CryptoCb_RsaPad(in, inLen, out,
4027                            &outLen, rsa_type, key, rng, &padding);
4028        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
4029            if (outPtr != NULL) {
4030                *outPtr = out;
4031            }
4032            if (ret == 0) {
4033                ret = (int)outLen;
4034            }
4035            break;
4036        }
4037    }
4038#endif
4039#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
4040    !defined(WOLFSSL_NO_MALLOC)
4041        ret = wc_RsaFunction_ex(key->data, inLen, key->data, &key->dataLen,
4042                                                   rsa_type, key, rng,
4043                                                   pad_type != WC_RSA_OAEP_PAD);
4044#else
4045        ret = wc_RsaFunction_ex(in, inLen, out, &key->dataLen, rsa_type, key,
4046                                              rng, pad_type != WC_RSA_OAEP_PAD);
4047#endif
4048
4049        if (ret >= 0 || ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
4050            key->state = RSA_STATE_DECRYPT_UNPAD;
4051        }
4052        if (ret < 0) {
4053            break;
4054        }
4055
4056        FALL_THROUGH;
4057
4058    case RSA_STATE_DECRYPT_UNPAD:
4059#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
4060    !defined(WOLFSSL_NO_MALLOC)
4061        ret = wc_RsaUnPad_ex(key->data,
4062            key->dataLen, &pad, pad_value, pad_type, hash, mgf,
4063            label, labelSz, saltLen, mp_count_bits(&key->n), key->heap);
4064#else
4065        ret = wc_RsaUnPad_ex(out,
4066            key->dataLen, &pad, pad_value, pad_type, hash, mgf, label,
4067            labelSz, saltLen, mp_count_bits(&key->n), key->heap);
4068#endif
4069        if (rsa_type == RSA_PUBLIC_DECRYPT && ret > (int)outLen) {
4070            ret = RSA_BUFFER_E;
4071        }
4072        else if (ret >= 0 && pad != NULL) {
4073            /* only copy output if not inline */
4074            if (outPtr == NULL) {
4075#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
4076    !defined(WOLFSSL_NO_MALLOC)
4077                if (rsa_type == RSA_PRIVATE_DECRYPT) {
4078                    word32 i = 0;
4079                    word32 j;
4080                    byte last = 0;
4081                    int start = (int)((size_t)pad - (size_t)key->data);
4082
4083                    for (j = 0; j < key->dataLen; j++) {
4084                        signed char incMask;
4085                        signed char maskData;
4086
4087                        /* When j < start + outLen then out[i] = key->data[j]
4088                         *                         else out[i] = last
4089                         */
4090                        maskData = (signed char)ctMaskLT((int)j,
4091                            start + (int)outLen);
4092                        out[i] = (byte)(key->data[j] &   maskData ) |
4093                                 (byte)(last         & (~maskData));
4094                        last = out[i];
4095
4096                        /* Increment i when j is in range:
4097                         *   [start..(start + outLen - 1)]. */
4098                        incMask  = (signed char)ctMaskGTE((int)j, start);
4099                        incMask &= (signed char)ctMaskLT((int)j,
4100                            start + (int)outLen - 1);
4101                        i += (word32)((byte)(-incMask));
4102                    }
4103                }
4104                else
4105#endif
4106                {
4107                    XMEMCPY(out, pad, (size_t)ret);
4108                }
4109            }
4110            else {
4111                *outPtr = pad;
4112            }
4113
4114#if !defined(WOLFSSL_RSA_VERIFY_ONLY)
4115            ret = ctMaskSelInt(ctMaskLTE(ret, (int)outLen), ret,
4116                               WC_NO_ERR_TRACE(RSA_BUFFER_E));
4117    #ifndef WOLFSSL_RSA_DECRYPT_TO_0_LEN
4118            ret = ctMaskSelInt(ctMaskNotEq(ret, 0), ret,
4119                               WC_NO_ERR_TRACE(RSA_BUFFER_E));
4120    #endif
4121#else
4122            if (outLen < (word32)ret)
4123                ret = RSA_BUFFER_E;
4124#endif
4125        }
4126
4127        key->state = RSA_STATE_DECRYPT_RES;
4128        FALL_THROUGH;
4129
4130    case RSA_STATE_DECRYPT_RES:
4131    #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \
4132            defined(HAVE_CAVIUM)
4133        if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA &&
4134                                                   pad_type != WC_RSA_PSS_PAD) {
4135            ret = key->asyncDev.event.ret;
4136            if (ret >= 0) {
4137                /* convert result */
4138                byte* dataLen = (byte*)&key->dataLen;
4139                ret = (dataLen[0] << 8) | (dataLen[1]);
4140
4141                if (outPtr)
4142                    *outPtr = in;
4143            }
4144        }
4145    #endif
4146        break;
4147
4148    default:
4149        ret = BAD_STATE_E;
4150        break;
4151    }
4152
4153    /* if async pending then return and skip done cleanup below */
4154    if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)
4155    #ifdef WC_RSA_NONBLOCK
4156        || ret == FP_WOULDBLOCK
4157    #endif
4158    ) {
4159        return ret;
4160    }
4161
4162    key->state = RSA_STATE_NONE;
4163    wc_RsaCleanup(key);
4164
4165    return ret;
4166}
4167
4168
4169#ifndef WOLFSSL_RSA_VERIFY_ONLY
4170/* Public RSA Functions */
4171int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, word32 outLen,
4172                                                     RsaKey* key, WC_RNG* rng)
4173{
4174    int ret;
4175    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4176    ret = RsaPublicEncryptEx(in, inLen, out, outLen, key,
4177        RSA_PUBLIC_ENCRYPT, RSA_BLOCK_TYPE_2, WC_RSA_PKCSV15_PAD,
4178        WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng);
4179    RESTORE_VECTOR_REGISTERS();
4180    return ret;
4181}
4182
4183
4184#if !defined(WC_NO_RSA_OAEP) || defined(WC_RSA_NO_PADDING)
4185int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out,
4186                    word32 outLen, RsaKey* key, WC_RNG* rng, int type,
4187                    enum wc_HashType hash, int mgf, byte* label,
4188                    word32 labelSz)
4189{
4190    int ret;
4191    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4192    ret = RsaPublicEncryptEx(in, inLen, out, outLen, key, RSA_PUBLIC_ENCRYPT,
4193        RSA_BLOCK_TYPE_2, type, hash, mgf, label, labelSz, 0, rng);
4194    RESTORE_VECTOR_REGISTERS();
4195    return ret;
4196}
4197#endif /* WC_NO_RSA_OAEP */
4198#endif
4199
4200
4201#ifndef WOLFSSL_RSA_PUBLIC_ONLY
4202int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out, RsaKey* key)
4203{
4204    WC_RNG* rng;
4205    int ret;
4206#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
4207    if (key == NULL) {
4208        return BAD_FUNC_ARG;
4209    }
4210    rng = key->rng;
4211#else
4212    rng = NULL;
4213#endif
4214    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4215    ret = RsaPrivateDecryptEx(in, inLen, in, inLen, out, key,
4216        RSA_PRIVATE_DECRYPT, RSA_BLOCK_TYPE_2, WC_RSA_PKCSV15_PAD,
4217        WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng);
4218    RESTORE_VECTOR_REGISTERS();
4219    return ret;
4220}
4221
4222
4223#ifndef WC_NO_RSA_OAEP
4224int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen, byte** out,
4225                                  RsaKey* key, int type, enum wc_HashType hash,
4226                                  int mgf, byte* label, word32 labelSz)
4227{
4228    WC_RNG* rng;
4229    int ret;
4230#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
4231    if (key == NULL) {
4232        return BAD_FUNC_ARG;
4233    }
4234    rng = key->rng;
4235#else
4236    rng = NULL;
4237#endif
4238    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4239    ret = RsaPrivateDecryptEx(in, inLen, in, inLen, out, key,
4240        RSA_PRIVATE_DECRYPT, RSA_BLOCK_TYPE_2, type, hash,
4241        mgf, label, labelSz, 0, rng);
4242    RESTORE_VECTOR_REGISTERS();
4243    return ret;
4244}
4245#endif /* WC_NO_RSA_OAEP */
4246
4247
4248int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
4249                                                 word32 outLen, RsaKey* key)
4250{
4251    WC_RNG* rng;
4252    int ret;
4253#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
4254    if (key == NULL) {
4255        return BAD_FUNC_ARG;
4256    }
4257    rng = key->rng;
4258#else
4259    rng = NULL;
4260#endif
4261    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4262    ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key,
4263        RSA_PRIVATE_DECRYPT, RSA_BLOCK_TYPE_2, WC_RSA_PKCSV15_PAD,
4264        WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng);
4265    RESTORE_VECTOR_REGISTERS();
4266    return ret;
4267}
4268
4269#if !defined(WC_NO_RSA_OAEP) || defined(WC_RSA_NO_PADDING)
4270int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen, byte* out,
4271                            word32 outLen, RsaKey* key, int type,
4272                            enum wc_HashType hash, int mgf, byte* label,
4273                            word32 labelSz)
4274{
4275    WC_RNG* rng;
4276    int ret;
4277#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
4278    if (key == NULL) {
4279        return BAD_FUNC_ARG;
4280    }
4281    rng = key->rng;
4282#else
4283    rng = NULL;
4284#endif
4285    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4286    ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key,
4287        RSA_PRIVATE_DECRYPT, RSA_BLOCK_TYPE_2, type, hash, mgf, label,
4288        labelSz, 0, rng);
4289    RESTORE_VECTOR_REGISTERS();
4290    return ret;
4291}
4292#endif /* WC_NO_RSA_OAEP || WC_RSA_NO_PADDING */
4293#endif /* WOLFSSL_RSA_PUBLIC_ONLY */
4294
4295#if !defined(WOLFSSL_CRYPTOCELL)
4296int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out, RsaKey* key)
4297{
4298    WC_RNG* rng;
4299    int ret;
4300#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
4301    if (key == NULL) {
4302        return BAD_FUNC_ARG;
4303    }
4304    rng = key->rng;
4305#else
4306    rng = NULL;
4307#endif
4308    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4309    ret = RsaPrivateDecryptEx(in, inLen, in, inLen, out, key,
4310        RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PKCSV15_PAD,
4311        WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng);
4312    RESTORE_VECTOR_REGISTERS();
4313    return ret;
4314}
4315#endif
4316
4317#ifndef WOLFSSL_RSA_VERIFY_INLINE
4318int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out, word32 outLen,
4319                                                                 RsaKey* key)
4320{
4321    return wc_RsaSSL_Verify_ex(in, inLen, out, outLen, key, WC_RSA_PKCSV15_PAD);
4322}
4323
4324int  wc_RsaSSL_Verify_ex(const byte* in, word32 inLen, byte* out, word32 outLen,
4325                         RsaKey* key, int pad_type)
4326{
4327    int ret;
4328    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4329    ret = wc_RsaSSL_Verify_ex2(in, inLen, out, outLen, key, pad_type,
4330            WC_HASH_TYPE_NONE);
4331    RESTORE_VECTOR_REGISTERS();
4332    return ret;
4333}
4334
4335int  wc_RsaSSL_Verify_ex2(const byte* in, word32 inLen, byte* out, word32 outLen,
4336                         RsaKey* key, int pad_type, enum wc_HashType hash)
4337{
4338    WC_RNG* rng;
4339    int ret;
4340
4341    if (key == NULL) {
4342        return BAD_FUNC_ARG;
4343    }
4344
4345#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
4346    rng = key->rng;
4347#else
4348    rng = NULL;
4349#endif
4350
4351    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4352#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
4353    ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key,
4354        RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, pad_type,
4355        hash, wc_hash2mgf(hash), NULL, 0, RSA_PSS_SALT_LEN_DEFAULT, rng);
4356#else
4357    ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key,
4358        RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, pad_type,
4359        hash, wc_hash2mgf(hash), NULL, 0, RSA_PSS_SALT_LEN_DISCOVER, rng);
4360#endif
4361    RESTORE_VECTOR_REGISTERS();
4362    return ret;
4363}
4364#endif
4365
4366#ifdef WC_RSA_PSS
4367/* Verify the message signed with RSA-PSS.
4368 * The input buffer is reused for the output buffer.
4369 * Salt length is equal to hash length.
4370 *
4371 * in     Buffer holding encrypted data.
4372 * inLen  Length of data in buffer.
4373 * out    Pointer to address containing the PSS data.
4374 * hash   Hash algorithm.
4375 * mgf    Mask generation function.
4376 * key    Public RSA key.
4377 * returns the length of the PSS data on success and negative indicates failure.
4378 */
4379int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out,
4380                           enum wc_HashType hash, int mgf, RsaKey* key)
4381{
4382#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
4383    return wc_RsaPSS_VerifyInline_ex(in, inLen, out, hash, mgf,
4384                                                 RSA_PSS_SALT_LEN_DEFAULT, key);
4385#else
4386    return wc_RsaPSS_VerifyInline_ex(in, inLen, out, hash, mgf,
4387                                                RSA_PSS_SALT_LEN_DISCOVER, key);
4388#endif
4389}
4390
4391/* Verify the message signed with RSA-PSS.
4392 * The input buffer is reused for the output buffer.
4393 *
4394 * in       Buffer holding encrypted data.
4395 * inLen    Length of data in buffer.
4396 * out      Pointer to address containing the PSS data.
4397 * hash     Hash algorithm.
4398 * mgf      Mask generation function.
4399 * key      Public RSA key.
4400 * saltLen  Length of salt used. RSA_PSS_SALT_LEN_DEFAULT (-1) indicates salt
4401 *          length is the same as the hash length. RSA_PSS_SALT_LEN_DISCOVER
4402 *          indicates salt length is determined from the data.
4403 * returns the length of the PSS data on success and negative indicates failure.
4404 */
4405int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out,
4406                              enum wc_HashType hash, int mgf, int saltLen,
4407                              RsaKey* key)
4408{
4409    WC_RNG* rng;
4410    int ret;
4411#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
4412    if (key == NULL) {
4413        return BAD_FUNC_ARG;
4414    }
4415    rng = key->rng;
4416#else
4417    rng = NULL;
4418#endif
4419    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4420    ret = RsaPrivateDecryptEx(in, inLen, in, inLen, out, key,
4421        RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PSS_PAD,
4422        hash, mgf, NULL, 0, saltLen, rng);
4423    RESTORE_VECTOR_REGISTERS();
4424    return ret;
4425}
4426
4427/* Verify the message signed with RSA-PSS.
4428 * Salt length is equal to hash length.
4429 *
4430 * in     Buffer holding encrypted data.
4431 * inLen  Length of data in buffer.
4432 * out    Pointer to address containing the PSS data.
4433 * hash   Hash algorithm.
4434 * mgf    Mask generation function.
4435 * key    Public RSA key.
4436 * returns the length of the PSS data on success and negative indicates failure.
4437 */
4438int wc_RsaPSS_Verify(const byte* in, word32 inLen, byte* out, word32 outLen,
4439                     enum wc_HashType hash, int mgf, RsaKey* key)
4440{
4441#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
4442    return wc_RsaPSS_Verify_ex(in, inLen, out, outLen, hash, mgf,
4443                                                 RSA_PSS_SALT_LEN_DEFAULT, key);
4444#else
4445    return wc_RsaPSS_Verify_ex(in, inLen, out, outLen, hash, mgf,
4446                                                RSA_PSS_SALT_LEN_DISCOVER, key);
4447#endif
4448}
4449
4450/* Verify the message signed with RSA-PSS.
4451 *
4452 * in       Buffer holding encrypted data.
4453 * inLen    Length of data in buffer.
4454 * out      Pointer to address containing the PSS data.
4455 * hash     Hash algorithm.
4456 * mgf      Mask generation function.
4457 * key      Public RSA key.
4458 * saltLen  Length of salt used. RSA_PSS_SALT_LEN_DEFAULT (-1) indicates salt
4459 *          length is the same as the hash length. RSA_PSS_SALT_LEN_DISCOVER
4460 *          indicates salt length is determined from the data.
4461 * returns the length of the PSS data on success and negative indicates failure.
4462 */
4463int wc_RsaPSS_Verify_ex(const byte* in, word32 inLen, byte* out, word32 outLen,
4464                        enum wc_HashType hash, int mgf, int saltLen,
4465                        RsaKey* key)
4466{
4467    WC_RNG* rng;
4468    int ret;
4469#if defined(WC_RSA_BLINDING) && !defined(WC_NO_RNG)
4470    if (key == NULL) {
4471        return BAD_FUNC_ARG;
4472    }
4473    rng = key->rng;
4474#else
4475    rng = NULL;
4476#endif
4477    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4478    ret = RsaPrivateDecryptEx(in, inLen, out, outLen, NULL, key,
4479        RSA_PUBLIC_DECRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PSS_PAD,
4480        hash, mgf, NULL, 0, saltLen, rng);
4481    RESTORE_VECTOR_REGISTERS();
4482    return ret;
4483}
4484
4485
4486/* Checks the PSS data to ensure that the signature matches.
4487 * Salt length is equal to hash length.
4488 *
4489 * in        Hash of the data that is being verified.
4490 * inSz      Length of hash.
4491 * sig       Buffer holding PSS data.
4492 * sigSz     Size of PSS data.
4493 * hashType  Hash algorithm.
4494 * returns BAD_PADDING_E when the PSS data is invalid, BAD_FUNC_ARG when
4495 * NULL is passed in to in or sig or inSz is not the same as the hash
4496 * algorithm length and 0 on success.
4497 */
4498int wc_RsaPSS_CheckPadding(const byte* in, word32 inSz, const byte* sig,
4499                           word32 sigSz, enum wc_HashType hashType)
4500{
4501#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
4502    return wc_RsaPSS_CheckPadding_ex(in, inSz, sig, sigSz, hashType, RSA_PSS_SALT_LEN_DEFAULT, 0);
4503#else
4504    return wc_RsaPSS_CheckPadding_ex(in, inSz, sig, sigSz, hashType, RSA_PSS_SALT_LEN_DISCOVER, 0);
4505#endif
4506}
4507
4508/* Checks the PSS data to ensure that the signature matches.
4509 *
4510 * in        Hash of the data that is being verified.
4511 * inSz      Length of hash.
4512 * sig       Buffer holding PSS data.
4513 * sigSz     Size of PSS data.
4514 * hashType  Hash algorithm.
4515 * saltLen   Length of salt used. RSA_PSS_SALT_LEN_DEFAULT (-1) indicates salt
4516 *           length is the same as the hash length. RSA_PSS_SALT_LEN_DISCOVER
4517 *           indicates salt length is determined from the data.
4518 * bits      Can be used to calculate salt size in FIPS case
4519 * returns BAD_PADDING_E when the PSS data is invalid, BAD_FUNC_ARG when
4520 * NULL is passed in to in or sig or inSz is not the same as the hash
4521 * algorithm length and 0 on success.
4522 */
4523int wc_RsaPSS_CheckPadding_ex2(const byte* in, word32 inSz, const byte* sig,
4524                               word32 sigSz, enum wc_HashType hashType,
4525                               int saltLen, int bits, void* heap)
4526{
4527    int ret = 0;
4528    byte sigCheckBuf[WC_MAX_DIGEST_SIZE*2 + RSA_PSS_PAD_SZ];
4529    byte *sigCheck = sigCheckBuf;
4530    int digSz;
4531    (void)bits;
4532
4533    digSz = wc_HashGetDigestSize(hashType);
4534
4535    if (in == NULL || sig == NULL || digSz < 0 || inSz != (word32)digSz) {
4536        ret = BAD_FUNC_ARG;
4537    }
4538
4539    if (ret == 0) {
4540        if (saltLen == RSA_PSS_SALT_LEN_DEFAULT) {
4541            saltLen = (int)inSz;
4542            #ifdef WOLFSSL_SHA512
4543                /* See FIPS 186-4 section 5.5 item (e). */
4544                if (bits == 1024 && inSz == WC_SHA512_DIGEST_SIZE) {
4545                    saltLen = RSA_PSS_SALT_MAX_SZ;
4546                }
4547            #endif
4548        }
4549#ifndef WOLFSSL_PSS_LONG_SALT
4550        else if (saltLen > (int)inSz) {
4551            ret = PSS_SALTLEN_E;
4552        }
4553#endif
4554#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
4555        else if (saltLen < RSA_PSS_SALT_LEN_DEFAULT) {
4556            ret = PSS_SALTLEN_E;
4557        }
4558#else
4559        else if (saltLen == RSA_PSS_SALT_LEN_DISCOVER) {
4560            saltLen = sigSz - inSz;
4561            if (saltLen < 0) {
4562                ret = PSS_SALTLEN_E;
4563            }
4564        }
4565        else if (saltLen < RSA_PSS_SALT_LEN_DISCOVER) {
4566            ret = PSS_SALTLEN_E;
4567        }
4568#endif
4569    }
4570
4571    /* Sig = Salt | Exp Hash */
4572    if (ret == 0) {
4573        word32 totalSz = 0;
4574        if ((WC_SAFE_SUM_WORD32(inSz, (word32)saltLen, totalSz) == 0) ||
4575            (sigSz != totalSz))
4576        {
4577            ret = PSS_SALTLEN_E;
4578        }
4579    }
4580
4581#ifdef WOLFSSL_PSS_LONG_SALT
4582    /* if long salt is larger then default maximum buffer then allocate a buffer */
4583    if ((ret == 0) &&
4584            (sizeof(sigCheckBuf) < (RSA_PSS_PAD_SZ + inSz + (word32)saltLen))) {
4585        sigCheck = (byte*)XMALLOC(
4586                              (size_t)(RSA_PSS_PAD_SZ + inSz + (word32)saltLen),
4587                              heap, DYNAMIC_TYPE_RSA_BUFFER);
4588        if (sigCheck == NULL) {
4589            ret = MEMORY_E;
4590        }
4591    }
4592#else
4593    if (ret == 0 && sizeof(sigCheckBuf) < (RSA_PSS_PAD_SZ + inSz + (word32)saltLen)) {
4594        ret = BUFFER_E;
4595    }
4596#endif
4597
4598    /* Exp Hash = HASH(8 * 0x00 | Message Hash | Salt) */
4599    if (ret == 0) {
4600        XMEMSET(sigCheck, 0, RSA_PSS_PAD_SZ);
4601        XMEMCPY(sigCheck + RSA_PSS_PAD_SZ, in, inSz);
4602        XMEMCPY(sigCheck + RSA_PSS_PAD_SZ + inSz, sig, (size_t)saltLen);
4603        ret = wc_Hash(hashType, sigCheck, RSA_PSS_PAD_SZ + inSz + (word32)saltLen,
4604                      sigCheck, inSz);
4605    }
4606    if (ret == 0) {
4607        if (XMEMCMP(sigCheck, sig + saltLen, inSz) != 0) {
4608            WOLFSSL_MSG("RsaPSS_CheckPadding: Padding Error");
4609            ret = BAD_PADDING_E;
4610        }
4611    }
4612
4613#ifdef WOLFSSL_PSS_LONG_SALT
4614    if (sigCheck != NULL && sigCheck != sigCheckBuf) {
4615        XFREE(sigCheck, heap, DYNAMIC_TYPE_RSA_BUFFER);
4616    }
4617#endif
4618
4619    (void)heap; /* unused if memory is disabled */
4620    return ret;
4621}
4622int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inSz, const byte* sig,
4623                               word32 sigSz, enum wc_HashType hashType,
4624                               int saltLen, int bits)
4625{
4626    return wc_RsaPSS_CheckPadding_ex2(in, inSz, sig, sigSz, hashType, saltLen,
4627        bits, NULL);
4628}
4629
4630
4631/* Verify the message signed with RSA-PSS.
4632 * The input buffer is reused for the output buffer.
4633 * Salt length is equal to hash length.
4634 *
4635 * in     Buffer holding encrypted data.
4636 * inLen  Length of data in buffer.
4637 * out    Pointer to address containing the PSS data.
4638 * digest Hash of the data that is being verified.
4639 * digestLen Length of hash.
4640 * hash   Hash algorithm.
4641 * mgf    Mask generation function.
4642 * key    Public RSA key.
4643 * returns the length of the PSS data on success and negative indicates failure.
4644 */
4645int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out,
4646                           const byte* digest, word32 digestLen,
4647                           enum wc_HashType hash, int mgf, RsaKey* key)
4648{
4649    int ret = 0, verify, saltLen, hLen, bits = 0;
4650#ifdef WOLFSSL_MICROCHIP_TA100
4651    if (key != NULL && key->uKeyH != 0) {
4652        int verified = 0;
4653        ret = wc_Microchip_rsa_verify(digest, digestLen, in, inLen, key,
4654                                      &verified);
4655        if (ret != 0) {
4656            return ret;
4657        }
4658        return verified ? (int)inLen : SIG_VERIFY_E;
4659    }
4660#endif
4661
4662    hLen = wc_HashGetDigestSize(hash);
4663    if (hLen < 0)
4664        return BAD_FUNC_ARG;
4665    if ((word32)hLen != digestLen)
4666        return BAD_FUNC_ARG;
4667
4668    saltLen = hLen;
4669    #ifdef WOLFSSL_SHA512
4670        if (key == NULL) {
4671            return BAD_FUNC_ARG;
4672        }
4673        /* See FIPS 186-4 section 5.5 item (e). */
4674        bits = mp_count_bits(&key->n);
4675        if (bits == 1024 && hLen == WC_SHA512_DIGEST_SIZE)
4676            saltLen = RSA_PSS_SALT_MAX_SZ;
4677    #endif
4678
4679    verify = wc_RsaPSS_VerifyInline_ex(in, inLen, out, hash, mgf, saltLen, key);
4680    if (verify > 0)
4681        ret = wc_RsaPSS_CheckPadding_ex(digest, digestLen, *out, (word32)verify,
4682                                        hash, saltLen, bits);
4683    if (ret == 0)
4684        ret = verify;
4685
4686    return ret;
4687}
4688
4689
4690/* Verify the message signed with RSA-PSS.
4691 * Salt length is equal to hash length.
4692 *
4693 * in     Buffer holding encrypted data.
4694 * inLen  Length of data in buffer.
4695 * out    Pointer to address containing the PSS data.
4696 * outLen Length of the output.
4697 * digest Hash of the data that is being verified.
4698 * digestLen Length of hash.
4699 * hash   Hash algorithm.
4700 * mgf    Mask generation function.
4701 * key    Public RSA key.
4702 * returns the length of the PSS data on success and negative indicates failure.
4703 */
4704int wc_RsaPSS_VerifyCheck(const byte* in, word32 inLen, byte* out, word32 outLen,
4705                          const byte* digest, word32 digestLen,
4706                          enum wc_HashType hash, int mgf,
4707                          RsaKey* key)
4708{
4709    int ret = 0, verify, saltLen, hLen, bits = 0;
4710#ifdef WOLFSSL_MICROCHIP_TA100
4711    if (key != NULL && key->uKeyH != 0) {
4712        int verified = 0;
4713        ret = wc_Microchip_rsa_verify(digest, digestLen, (byte*)in, inLen,
4714                                      key, &verified);
4715        if (ret != 0) {
4716            return ret;
4717        }
4718        return verified ? (int)inLen : SIG_VERIFY_E;
4719    }
4720#endif
4721
4722    hLen = wc_HashGetDigestSize(hash);
4723    if (hLen < 0)
4724        return hLen;
4725    if ((word32)hLen != digestLen)
4726        return BAD_FUNC_ARG;
4727
4728    saltLen = hLen;
4729    #ifdef WOLFSSL_SHA512
4730        if (key == NULL) {
4731            return BAD_FUNC_ARG;
4732        }
4733        /* See FIPS 186-4 section 5.5 item (e). */
4734        bits = mp_count_bits(&key->n);
4735        if (bits == 1024 && hLen == WC_SHA512_DIGEST_SIZE)
4736            saltLen = RSA_PSS_SALT_MAX_SZ;
4737    #endif
4738
4739    verify = wc_RsaPSS_Verify_ex(in, inLen, out, outLen, hash,
4740                                 mgf, saltLen, key);
4741    if (verify > 0)
4742        ret = wc_RsaPSS_CheckPadding_ex(digest, digestLen, out, (word32)verify,
4743                                        hash, saltLen, bits);
4744    if (ret == 0)
4745        ret = verify;
4746
4747    return ret;
4748}
4749
4750#endif
4751
4752#if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
4753int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out, word32 outLen,
4754                                                   RsaKey* key, WC_RNG* rng)
4755{
4756    int ret;
4757    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4758    ret = RsaPublicEncryptEx(in, inLen, out, outLen, key,
4759        RSA_PRIVATE_ENCRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PKCSV15_PAD,
4760        WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0, 0, rng);
4761    RESTORE_VECTOR_REGISTERS();
4762    return ret;
4763}
4764
4765#ifdef WC_RSA_PSS
4766/* Sign the hash of a message using RSA-PSS.
4767 * Salt length is equal to hash length.
4768 *
4769 * in      Buffer holding hash of message.
4770 * inLen   Length of data in buffer (hash length).
4771 * out     Buffer to write encrypted signature into.
4772 * outLen  Size of buffer to write to.
4773 * hash    Hash algorithm.
4774 * mgf     Mask generation function.
4775 * key     Public RSA key.
4776 * rng     Random number generator.
4777 * returns the length of the encrypted signature on success, a negative value
4778 * indicates failure.
4779 */
4780int wc_RsaPSS_Sign(const byte* in, word32 inLen, byte* out, word32 outLen,
4781                       enum wc_HashType hash, int mgf, RsaKey* key, WC_RNG* rng)
4782{
4783    return wc_RsaPSS_Sign_ex(in, inLen, out, outLen, hash, mgf,
4784                                            RSA_PSS_SALT_LEN_DEFAULT, key, rng);
4785}
4786
4787/* Sign the hash of a message using RSA-PSS.
4788 *
4789 * in       Buffer holding hash of message.
4790 * inLen    Length of data in buffer (hash length).
4791 * out      Buffer to write encrypted signature into.
4792 * outLen   Size of buffer to write to.
4793 * hash     Hash algorithm.
4794 * mgf      Mask generation function.
4795 * saltLen  Length of salt used. RSA_PSS_SALT_LEN_DEFAULT (-1) indicates salt
4796 *          length is the same as the hash length. RSA_PSS_SALT_LEN_DISCOVER
4797 *          indicates salt length is determined from the data.
4798 * key      Public RSA key.
4799 * rng      Random number generator.
4800 * returns the length of the encrypted signature on success, a negative value
4801 * indicates failure.
4802 */
4803int wc_RsaPSS_Sign_ex(const byte* in, word32 inLen, byte* out, word32 outLen,
4804                      enum wc_HashType hash, int mgf, int saltLen, RsaKey* key,
4805                      WC_RNG* rng)
4806{
4807    int ret;
4808    SAVE_VECTOR_REGISTERS(return _svr_ret;);
4809    ret = RsaPublicEncryptEx(in, inLen, out, outLen, key,
4810        RSA_PRIVATE_ENCRYPT, RSA_BLOCK_TYPE_1, WC_RSA_PSS_PAD,
4811        hash, mgf, NULL, 0, saltLen, rng);
4812    RESTORE_VECTOR_REGISTERS();
4813    return ret;
4814}
4815#endif
4816#endif
4817
4818int wc_RsaEncryptSize(const RsaKey* key)
4819{
4820    int ret;
4821
4822    if (key == NULL) {
4823        return BAD_FUNC_ARG;
4824    }
4825
4826    ret = mp_unsigned_bin_size(&key->n);
4827
4828#if defined(WOLFSSL_MICROCHIP_TA100)
4829    if (ret == 0 && (key->rKeyH != 0 || key->uKeyH != 0)) {
4830        ret = 2048 / 8;
4831    }
4832#endif
4833
4834#ifdef WOLF_CRYPTO_CB
4835    if (ret == 0 && key->devId != INVALID_DEVID) {
4836        if (wc_CryptoCb_RsaGetSize(key, &ret) == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
4837            ret = 2048/8; /* hardware handles, use 2048-bit as default */
4838        }
4839    }
4840#endif
4841
4842    return ret;
4843}
4844
4845#ifndef WOLFSSL_RSA_VERIFY_ONLY
4846/* Software-only export of RSA public key elements from RsaKey.
4847 * This internal helper avoids recursion when called from the EXPORT_KEY path. */
4848static int _RsaFlattenPublicKey(const RsaKey* key, byte* e, word32* eSz,
4849                                byte* n, word32* nSz)
4850{
4851    int sz, ret;
4852
4853    if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL) {
4854        return BAD_FUNC_ARG;
4855    }
4856
4857    sz = mp_unsigned_bin_size(&key->e);
4858    if ((word32)sz > *eSz) {
4859        return RSA_BUFFER_E;
4860    }
4861    ret = mp_to_unsigned_bin(&key->e, e);
4862    if (ret != MP_OKAY) {
4863        return ret;
4864    }
4865    *eSz = (word32)sz;
4866
4867    sz = wc_RsaEncryptSize(key);
4868    if ((word32)sz > *nSz) {
4869        return RSA_BUFFER_E;
4870    }
4871    ret = mp_to_unsigned_bin(&key->n, n);
4872    if (ret != MP_OKAY) {
4873        return ret;
4874    }
4875    *nSz = (word32)sz;
4876
4877    return 0;
4878}
4879
4880/* flatten RsaKey structure into individual elements (e, n) */
4881int wc_RsaFlattenPublicKey(const RsaKey* key, byte* e, word32* eSz, byte* n,
4882                                                                   word32* nSz)
4883{
4884    if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL) {
4885        return BAD_FUNC_ARG;
4886    }
4887
4888#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
4889#ifndef WOLF_CRYPTO_CB_FIND
4890    if (key->devId != INVALID_DEVID)
4891#endif
4892    {
4893        int ret;
4894        WC_DECLARE_VAR(tmpKey, RsaKey, 1, NULL);
4895
4896        WC_ALLOC_VAR(tmpKey, RsaKey, 1, key->heap);
4897        if (!WC_VAR_OK(tmpKey)) {
4898            return MEMORY_E;
4899        }
4900        XMEMSET(tmpKey, 0, sizeof(RsaKey));
4901
4902        ret = wc_InitRsaKey_ex(tmpKey, key->heap, INVALID_DEVID);
4903        if (ret != 0) {
4904            WC_FREE_VAR(tmpKey, key->heap);
4905            return ret;
4906        }
4907
4908        ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_RSA,
4909                                     key, tmpKey);
4910        if (ret == 0) {
4911            /* Call software helper (no callback recursion) */
4912            ret = _RsaFlattenPublicKey(tmpKey, e, eSz, n, nSz);
4913        }
4914        /* wc_FreeRsaKey calls mp_forcezero on all private key components,
4915         * so no separate ForceZero of the struct is needed here. Calling
4916         * ForceZero before wc_FreeRsaKey would zero the mp_int metadata
4917         * and cause a crash. */
4918        wc_FreeRsaKey(tmpKey);
4919        WC_FREE_VAR(tmpKey, key->heap);
4920        if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
4921            return ret;
4922        }
4923        /* fall through to software */
4924    }
4925#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
4926
4927    return _RsaFlattenPublicKey(key, e, eSz, n, nSz);
4928}
4929#endif
4930
4931#ifndef WOLFSSL_RSA_VERIFY_ONLY
4932static int RsaGetValue(const mp_int* in, byte* out, word32* outSz)
4933{
4934    word32 sz;
4935    int ret = 0;
4936
4937    /* Parameters ensured by calling function. */
4938
4939    sz = (word32)mp_unsigned_bin_size(in);
4940    if (sz > *outSz)
4941        ret = RSA_BUFFER_E;
4942
4943    if (ret == 0)
4944        ret = mp_to_unsigned_bin(in, out);
4945
4946    if (ret == MP_OKAY)
4947        *outSz = sz;
4948
4949    return ret;
4950}
4951
4952
4953/* Software-only export of RSA key elements from RsaKey.
4954 * This internal helper avoids recursion when called from the EXPORT_KEY path. */
4955static int _RsaExportKey(const RsaKey* key,
4956                         byte* e, word32* eSz, byte* n, word32* nSz,
4957                         byte* d, word32* dSz, byte* p, word32* pSz,
4958                         byte* q, word32* qSz)
4959{
4960    int ret = 0;
4961
4962    if (key == NULL || e == NULL || eSz == NULL || n == NULL || nSz == NULL
4963            || d == NULL || dSz == NULL || p == NULL || pSz == NULL
4964            || q == NULL || qSz == NULL) {
4965        return BAD_FUNC_ARG;
4966    }
4967
4968    if (ret == 0) {
4969        ret = RsaGetValue(&key->e, e, eSz);
4970    }
4971    if (ret == 0) {
4972        ret = RsaGetValue(&key->n, n, nSz);
4973    }
4974#ifndef WOLFSSL_RSA_PUBLIC_ONLY
4975    if (ret == 0) {
4976        ret = RsaGetValue(&key->d, d, dSz);
4977    }
4978    if (ret == 0) {
4979        ret = RsaGetValue(&key->p, p, pSz);
4980    }
4981    if (ret == 0) {
4982        ret = RsaGetValue(&key->q, q, qSz);
4983    }
4984#else
4985    /* no private parts to key */
4986    if (d == NULL || p == NULL || q == NULL || dSz == NULL || pSz == NULL
4987            || qSz == NULL) {
4988        ret = BAD_FUNC_ARG;
4989    }
4990    else {
4991        *dSz = 0;
4992        *pSz = 0;
4993        *qSz = 0;
4994    }
4995#endif /* WOLFSSL_RSA_PUBLIC_ONLY */
4996
4997    return ret;
4998}
4999
5000int wc_RsaExportKey(const RsaKey* key,
5001                    byte* e, word32* eSz, byte* n, word32* nSz,
5002                    byte* d, word32* dSz, byte* p, word32* pSz,
5003                    byte* q, word32* qSz)
5004{
5005    int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG);
5006
5007    if (key && e && eSz && n && nSz && d && dSz && p && pSz && q && qSz) {
5008        ret = 0;
5009    }
5010
5011#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
5012    if (ret == 0) {
5013    #ifndef WOLF_CRYPTO_CB_FIND
5014        if (key->devId != INVALID_DEVID)
5015    #endif
5016        {
5017            WC_DECLARE_VAR(tmpKey, RsaKey, 1, NULL);
5018
5019            WC_ALLOC_VAR(tmpKey, RsaKey, 1, key->heap);
5020            if (!WC_VAR_OK(tmpKey)) {
5021                return MEMORY_E;
5022            }
5023            XMEMSET(tmpKey, 0, sizeof(RsaKey));
5024
5025            ret = wc_InitRsaKey_ex(tmpKey, key->heap, INVALID_DEVID);
5026            if (ret != 0) {
5027                WC_FREE_VAR(tmpKey, key->heap);
5028                return ret;
5029            }
5030
5031            ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_RSA,
5032                                         key, tmpKey);
5033            if (ret == 0) {
5034                /* Call software helper (no callback recursion) */
5035                ret = _RsaExportKey(tmpKey, e, eSz, n, nSz,
5036                                    d, dSz, p, pSz, q, qSz);
5037            }
5038            /* wc_FreeRsaKey calls mp_forcezero on all private key components,
5039             * so no separate ForceZero of the struct is needed here. */
5040            wc_FreeRsaKey(tmpKey);
5041            WC_FREE_VAR(tmpKey, key->heap);
5042            if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
5043                return ret;
5044            }
5045            ret = 0; /* fall through to software */
5046        }
5047    }
5048#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
5049
5050    if (ret == 0) {
5051        ret = _RsaExportKey(key, e, eSz, n, nSz, d, dSz, p, pSz, q, qSz);
5052    }
5053
5054    return ret;
5055}
5056#endif
5057
5058
5059#if defined(WOLFSSL_KEY_GEN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
5060
5061/* Check that |p-q| > 2^((size/2)-100) */
5062static int wc_CompareDiffPQ(mp_int* p, mp_int* q, int size, int* valid)
5063{
5064#ifdef WOLFSSL_SMALL_STACK
5065    mp_int *c = NULL, *d = NULL;
5066#else
5067    mp_int c[1], d[1];
5068#endif
5069    int ret;
5070
5071    if (p == NULL || q == NULL)
5072        return BAD_FUNC_ARG;
5073
5074#ifdef WOLFSSL_SMALL_STACK
5075    if (((c = (mp_int *)XMALLOC(sizeof(*c), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL) ||
5076        ((d = (mp_int *)XMALLOC(sizeof(*d), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL))
5077        ret = MEMORY_E;
5078    else
5079        ret = 0;
5080
5081    if (ret == 0)
5082#endif
5083        ret = mp_init_multi(c, d, NULL, NULL, NULL, NULL);
5084
5085    /* c = 2^((size/2)-100) */
5086    if (ret == 0)
5087        ret = mp_2expt(c, (size/2)-100);
5088
5089    /* d = |p-q| */
5090    if (ret == 0)
5091        ret = mp_sub(p, q, d);
5092
5093#ifdef WOLFSSL_CHECK_MEM_ZERO
5094    if (ret == 0)
5095        mp_memzero_add("Compare PQ d", d);
5096#endif
5097
5098#if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
5099                                               defined(WOLFSSL_SP_INT_NEGATIVE))
5100    if (ret == 0)
5101        ret = mp_abs(d, d);
5102#endif
5103
5104    /* compare */
5105    if (ret == 0)
5106        *valid = (mp_cmp(d, c) == MP_GT);
5107
5108#ifdef WOLFSSL_SMALL_STACK
5109    if (d != NULL) {
5110        mp_forcezero(d);
5111        XFREE(d, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
5112    }
5113    if (c != NULL) {
5114        mp_clear(c);
5115        XFREE(c, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
5116    }
5117#else
5118    mp_forcezero(d);
5119    mp_clear(c);
5120#ifdef WOLFSSL_CHECK_MEM_ZERO
5121    mp_memzero_check(d);
5122#endif
5123#endif
5124
5125    return ret;
5126}
5127
5128
5129/* The lower_bound value is floor(2^(0.5) * 2^((nlen/2)-1)) where nlen is 4096.
5130 * This number was calculated using a small test tool written with a common
5131 * large number math library. Other values of nlen may be checked with a subset
5132 * of lower_bound. */
5133static const byte lower_bound[] = {
5134    0xB5, 0x04, 0xF3, 0x33, 0xF9, 0xDE, 0x64, 0x84,
5135    0x59, 0x7D, 0x89, 0xB3, 0x75, 0x4A, 0xBE, 0x9F,
5136    0x1D, 0x6F, 0x60, 0xBA, 0x89, 0x3B, 0xA8, 0x4C,
5137    0xED, 0x17, 0xAC, 0x85, 0x83, 0x33, 0x99, 0x15,
5138/* 512 */
5139    0x4A, 0xFC, 0x83, 0x04, 0x3A, 0xB8, 0xA2, 0xC3,
5140    0xA8, 0xB1, 0xFE, 0x6F, 0xDC, 0x83, 0xDB, 0x39,
5141    0x0F, 0x74, 0xA8, 0x5E, 0x43, 0x9C, 0x7B, 0x4A,
5142    0x78, 0x04, 0x87, 0x36, 0x3D, 0xFA, 0x27, 0x68,
5143/* 1024 */
5144    0xD2, 0x20, 0x2E, 0x87, 0x42, 0xAF, 0x1F, 0x4E,
5145    0x53, 0x05, 0x9C, 0x60, 0x11, 0xBC, 0x33, 0x7B,
5146    0xCA, 0xB1, 0xBC, 0x91, 0x16, 0x88, 0x45, 0x8A,
5147    0x46, 0x0A, 0xBC, 0x72, 0x2F, 0x7C, 0x4E, 0x33,
5148    0xC6, 0xD5, 0xA8, 0xA3, 0x8B, 0xB7, 0xE9, 0xDC,
5149    0xCB, 0x2A, 0x63, 0x43, 0x31, 0xF3, 0xC8, 0x4D,
5150    0xF5, 0x2F, 0x12, 0x0F, 0x83, 0x6E, 0x58, 0x2E,
5151    0xEA, 0xA4, 0xA0, 0x89, 0x90, 0x40, 0xCA, 0x4A,
5152/* 2048 */
5153    0x81, 0x39, 0x4A, 0xB6, 0xD8, 0xFD, 0x0E, 0xFD,
5154    0xF4, 0xD3, 0xA0, 0x2C, 0xEB, 0xC9, 0x3E, 0x0C,
5155    0x42, 0x64, 0xDA, 0xBC, 0xD5, 0x28, 0xB6, 0x51,
5156    0xB8, 0xCF, 0x34, 0x1B, 0x6F, 0x82, 0x36, 0xC7,
5157    0x01, 0x04, 0xDC, 0x01, 0xFE, 0x32, 0x35, 0x2F,
5158    0x33, 0x2A, 0x5E, 0x9F, 0x7B, 0xDA, 0x1E, 0xBF,
5159    0xF6, 0xA1, 0xBE, 0x3F, 0xCA, 0x22, 0x13, 0x07,
5160    0xDE, 0xA0, 0x62, 0x41, 0xF7, 0xAA, 0x81, 0xC2,
5161/* 3072 */
5162    0xC1, 0xFC, 0xBD, 0xDE, 0xA2, 0xF7, 0xDC, 0x33,
5163    0x18, 0x83, 0x8A, 0x2E, 0xAF, 0xF5, 0xF3, 0xB2,
5164    0xD2, 0x4F, 0x4A, 0x76, 0x3F, 0xAC, 0xB8, 0x82,
5165    0xFD, 0xFE, 0x17, 0x0F, 0xD3, 0xB1, 0xF7, 0x80,
5166    0xF9, 0xAC, 0xCE, 0x41, 0x79, 0x7F, 0x28, 0x05,
5167    0xC2, 0x46, 0x78, 0x5E, 0x92, 0x95, 0x70, 0x23,
5168    0x5F, 0xCF, 0x8F, 0x7B, 0xCA, 0x3E, 0xA3, 0x3B,
5169    0x4D, 0x7C, 0x60, 0xA5, 0xE6, 0x33, 0xE3, 0xE1
5170/* 4096 */
5171};
5172
5173
5174/* returns 1 on key size ok and 0 if not ok */
5175static WC_INLINE int RsaSizeCheck(int size)
5176{
5177    if (size < RSA_MIN_SIZE || size > RSA_MAX_SIZE) {
5178        return 0;
5179    }
5180
5181#ifdef HAVE_FIPS
5182    /* Key size requirements for CAVP */
5183    switch (size) {
5184        case 1024:
5185        case 2048:
5186        case 3072:
5187        case 4096:
5188            return 1;
5189    }
5190
5191    return 0;
5192#else
5193    return 1; /* allow unusual key sizes in non FIPS mode */
5194#endif /* HAVE_FIPS */
5195}
5196
5197
5198static int _CheckProbablePrime(mp_int* p, mp_int* q, mp_int* e, int nlen,
5199                                    int* isPrime, WC_RNG* rng)
5200{
5201    int ret;
5202#ifdef WOLFSSL_SMALL_STACK
5203    mp_int *tmp1 = NULL, *tmp2 = NULL;
5204#else
5205    mp_int tmp1[1], tmp2[2];
5206#endif
5207    mp_int* prime;
5208
5209    if (p == NULL || e == NULL || isPrime == NULL)
5210        return BAD_FUNC_ARG;
5211
5212    if (!RsaSizeCheck(nlen))
5213        return BAD_FUNC_ARG;
5214
5215    *isPrime = MP_NO;
5216
5217#ifdef WOLFSSL_SMALL_STACK
5218    if (((tmp1 = (mp_int *)XMALLOC(sizeof(*tmp1), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL) ||
5219        ((tmp2 = (mp_int *)XMALLOC(sizeof(*tmp2), NULL, DYNAMIC_TYPE_WOLF_BIGINT)) == NULL)) {
5220        ret = MEMORY_E;
5221        goto notOkay;
5222    }
5223#endif
5224
5225    ret = mp_init_multi(tmp1, tmp2, NULL, NULL, NULL, NULL);
5226    if (ret != MP_OKAY) goto notOkay;
5227
5228    if (q != NULL) {
5229        int valid = 0;
5230        /* 5.4 (186-4) 5.5 (186-5) -
5231         * check that |p-q| <= (2^(1/2))(2^((nlen/2)-1)) */
5232        ret = wc_CompareDiffPQ(p, q, nlen, &valid);
5233        if ((ret != MP_OKAY) || (!valid)) goto notOkay;
5234        prime = q;
5235    }
5236    else
5237        prime = p;
5238
5239    /* 4.4,5.5 (186-4) 4.4,5.4 (186-5) -
5240     * Check that prime >= (2^(1/2))(2^((nlen/2)-1))
5241     *           This is a comparison against lowerBound */
5242    ret = mp_read_unsigned_bin(tmp1, lower_bound, (word32)nlen/16);
5243    if (ret != MP_OKAY) goto notOkay;
5244    ret = mp_cmp(prime, tmp1);
5245    if (ret == MP_LT) goto exit;
5246
5247    /* 4.5,5.6 (186-4 & 186-5) - Check that GCD(p-1, e) == 1 */
5248    ret = mp_sub_d(prime, 1, tmp1);  /* tmp1 = prime-1 */
5249    if (ret != MP_OKAY) goto notOkay;
5250#ifdef WOLFSSL_CHECK_MEM_ZERO
5251    mp_memzero_add("Check Probable Prime tmp1", tmp1);
5252#endif
5253    ret = mp_gcd(tmp1, e, tmp2);  /* tmp2 = gcd(prime-1, e) */
5254    if (ret != MP_OKAY) goto notOkay;
5255    ret = mp_cmp_d(tmp2, 1);
5256    if (ret != MP_EQ) goto exit; /* e divides p-1 */
5257
5258    /* 4.5.1,5.6.1 - Check primality of p with 8 rounds of M-R.
5259     * mp_prime_is_prime_ex() performs test divisions against the first 256
5260     * prime numbers. After that it performs 8 rounds of M-R using random
5261     * bases between 2 and n-2.
5262     * mp_prime_is_prime() performs the same test divisions and then does
5263     * M-R with the first 8 primes. Both functions set isPrime as a
5264     * side-effect. */
5265    if (rng != NULL)
5266        ret = mp_prime_is_prime_ex(prime, 8, isPrime, rng);
5267    else
5268        ret = mp_prime_is_prime(prime, 8, isPrime);
5269    if (ret != MP_OKAY) goto notOkay;
5270
5271exit:
5272    ret = MP_OKAY;
5273
5274notOkay:
5275
5276#ifdef WOLFSSL_SMALL_STACK
5277    if (tmp1 != NULL) {
5278        mp_forcezero(tmp1);
5279        XFREE(tmp1, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
5280    }
5281    if (tmp2 != NULL) {
5282        mp_clear(tmp2);
5283        XFREE(tmp2, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
5284    }
5285#else
5286    mp_forcezero(tmp1);
5287    mp_clear(tmp2);
5288#ifdef WOLFSSL_CHECK_MEM_ZERO
5289    mp_memzero_check(tmp1);
5290#endif
5291#endif
5292
5293    return ret;
5294}
5295
5296
5297int wc_CheckProbablePrime_ex(const byte* pRaw, word32 pRawSz,
5298                          const byte* qRaw, word32 qRawSz,
5299                          const byte* eRaw, word32 eRawSz,
5300                          int nlen, int* isPrime, WC_RNG* rng)
5301{
5302#ifdef WOLFSSL_SMALL_STACK
5303    mp_int *p = NULL, *q = NULL, *e = NULL;
5304#else
5305    mp_int p[1], q[1], e[1];
5306#endif
5307    mp_int* Q = NULL;
5308    int ret;
5309
5310    if (pRaw == NULL || pRawSz == 0 ||
5311        eRaw == NULL || eRawSz == 0 ||
5312        isPrime == NULL) {
5313
5314        return BAD_FUNC_ARG;
5315    }
5316
5317    if ((qRaw != NULL && qRawSz == 0) || (qRaw == NULL && qRawSz != 0))
5318        return BAD_FUNC_ARG;
5319
5320#ifdef WOLFSSL_SMALL_STACK
5321
5322    if (((p = (mp_int *)XMALLOC(sizeof(*p), NULL, DYNAMIC_TYPE_RSA_BUFFER)) == NULL) ||
5323        ((q = (mp_int *)XMALLOC(sizeof(*q), NULL, DYNAMIC_TYPE_RSA_BUFFER)) == NULL) ||
5324        ((e = (mp_int *)XMALLOC(sizeof(*e), NULL, DYNAMIC_TYPE_RSA_BUFFER)) == NULL))
5325        ret = MEMORY_E;
5326    else
5327        ret = 0;
5328    if (ret == 0)
5329#endif
5330        ret = mp_init_multi(p, q, e, NULL, NULL, NULL);
5331
5332    if (ret == MP_OKAY)
5333        ret = mp_read_unsigned_bin(p, pRaw, pRawSz);
5334
5335    if (ret == MP_OKAY) {
5336    #ifdef WOLFSSL_CHECK_MEM_ZERO
5337        mp_memzero_add("wc_CheckProbablePrime_ex p", p);
5338    #endif
5339        if (qRaw != NULL) {
5340            ret = mp_read_unsigned_bin(q, qRaw, qRawSz);
5341            if (ret == MP_OKAY) {
5342            #ifdef WOLFSSL_CHECK_MEM_ZERO
5343                mp_memzero_add("wc_CheckProbablePrime_ex q", q);
5344            #endif
5345                Q = q;
5346            }
5347        }
5348    }
5349
5350    if (ret == MP_OKAY)
5351        ret = mp_read_unsigned_bin(e, eRaw, eRawSz);
5352
5353    if (ret == MP_OKAY)
5354        SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
5355
5356    if (ret == 0) {
5357        ret = _CheckProbablePrime(p, Q, e, nlen, isPrime, rng);
5358        RESTORE_VECTOR_REGISTERS();
5359    }
5360
5361    ret = (ret == MP_OKAY) ? 0 : PRIME_GEN_E;
5362
5363#ifdef WOLFSSL_SMALL_STACK
5364    if (p != NULL) {
5365        mp_forcezero(p);
5366        XFREE(p, NULL, DYNAMIC_TYPE_RSA_BUFFER);
5367    }
5368    if (q != NULL) {
5369        mp_forcezero(q);
5370        XFREE(q, NULL, DYNAMIC_TYPE_RSA_BUFFER);
5371    }
5372    if (e != NULL) {
5373        mp_clear(e);
5374        XFREE(e, NULL, DYNAMIC_TYPE_RSA_BUFFER);
5375    }
5376#else
5377    mp_forcezero(p);
5378    mp_forcezero(q);
5379    mp_clear(e);
5380#ifdef WOLFSSL_CHECK_MEM_ZERO
5381    mp_memzero_check(p);
5382    mp_memzero_check(q);
5383#endif
5384#endif
5385
5386    return ret;
5387}
5388
5389
5390int wc_CheckProbablePrime(const byte* pRaw, word32 pRawSz,
5391                          const byte* qRaw, word32 qRawSz,
5392                          const byte* eRaw, word32 eRawSz,
5393                          int nlen, int* isPrime)
5394{
5395    return wc_CheckProbablePrime_ex(pRaw, pRawSz, qRaw, qRawSz,
5396                          eRaw, eRawSz, nlen, isPrime, NULL);
5397}
5398
5399#if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && \
5400        defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
5401/* Make an RSA key for size bits, with e specified, 65537 is a good e */
5402int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
5403{
5404#ifndef WC_NO_RNG
5405#if !defined(WOLFSSL_CRYPTOCELL) && \
5406    (!defined(WOLFSSL_SE050) || defined(WOLFSSL_SE050_NO_RSA)) && \
5407    !defined(WOLF_CRYPTO_CB_ONLY_RSA) && \
5408    !defined(WOLFSSL_MICROCHIP_TA100)
5409#ifdef WOLFSSL_SMALL_STACK
5410    mp_int *p = NULL;
5411    mp_int *q = NULL;
5412    mp_int *tmp1 = NULL;
5413    mp_int *tmp2 = NULL;
5414    mp_int *tmp3 = NULL;
5415#else
5416    mp_int p_buf, *p = &p_buf;
5417    mp_int q_buf, *q = &q_buf;
5418    mp_int tmp1_buf, *tmp1 = &tmp1_buf;
5419    mp_int tmp2_buf, *tmp2 = &tmp2_buf;
5420    mp_int tmp3_buf, *tmp3 = &tmp3_buf;
5421#endif /* WOLFSSL_SMALL_STACK */
5422    int i, failCount, isPrime = 0;
5423    word32 primeSz;
5424#ifndef WOLFSSL_NO_MALLOC
5425    byte* buf = NULL;
5426#else
5427    /* RSA_MAX_SIZE is the size of n in bits. */
5428    byte buf[RSA_MAX_SIZE/16];
5429#endif
5430#endif /* !WOLFSSL_CRYPTOCELL && !WOLFSSL_SE050 */
5431    int err;
5432
5433    if (key == NULL || rng == NULL) {
5434        err = BAD_FUNC_ARG;
5435        goto out;
5436    }
5437
5438    if (!RsaSizeCheck(size)) {
5439        err = BAD_FUNC_ARG;
5440        goto out;
5441    }
5442
5443    if (e < 3 || (e & 1) == 0) {
5444        err = BAD_FUNC_ARG;
5445        goto out;
5446    }
5447
5448#if defined(WOLFSSL_CRYPTOCELL)
5449    err = cc310_RSA_GenerateKeyPair(key, size, e);
5450    goto out;
5451#elif defined(WOLFSSL_MICROCHIP_TA100)
5452    err = wc_Microchip_rsa_create_key(key, size, e);
5453    goto out;
5454#elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
5455    err = se050_rsa_create_key(key, size, e);
5456    goto out;
5457#else
5458    /* software crypto */
5459
5460#ifdef WOLFSSL_SMALL_STACK
5461    p = (mp_int *)XMALLOC(sizeof *p, key->heap, DYNAMIC_TYPE_RSA);
5462    q = (mp_int *)XMALLOC(sizeof *q, key->heap, DYNAMIC_TYPE_RSA);
5463    tmp1 = (mp_int *)XMALLOC(sizeof *tmp1, key->heap, DYNAMIC_TYPE_RSA);
5464    tmp2 = (mp_int *)XMALLOC(sizeof *tmp2, key->heap, DYNAMIC_TYPE_RSA);
5465    tmp3 = (mp_int *)XMALLOC(sizeof *tmp3, key->heap, DYNAMIC_TYPE_RSA);
5466
5467    if ((p == NULL) ||
5468        (q == NULL) ||
5469        (tmp1 == NULL) ||
5470        (tmp2 == NULL) ||
5471        (tmp3 == NULL)) {
5472      err = MEMORY_E;
5473      goto out;
5474    }
5475#endif
5476#ifdef WOLFSSL_CHECK_MEM_ZERO
5477    XMEMSET(p, 0, sizeof(*p));
5478    XMEMSET(q, 0, sizeof(*q));
5479    XMEMSET(tmp1, 0, sizeof(*tmp1));
5480    XMEMSET(tmp2, 0, sizeof(*tmp2));
5481    XMEMSET(tmp3, 0, sizeof(*tmp3));
5482#endif
5483
5484#ifdef WOLF_CRYPTO_CB
5485    #ifndef WOLF_CRYPTO_CB_FIND
5486    if (key->devId != INVALID_DEVID)
5487    #endif
5488    {
5489        err = wc_CryptoCb_MakeRsaKey(key, size, e, rng);
5490    #ifdef WOLF_CRYPTO_CB_ONLY_RSA
5491        if (err == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
5492            err = NO_VALID_DEVID;
5493            goto out;
5494        }
5495    #else
5496        if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
5497            goto out;
5498        }
5499        /* fall-through when unavailable */
5500    #endif
5501    }
5502    #if !defined(WOLF_CRYPTO_CB_FIND) && defined(WOLF_CRYPTO_CB_ONLY_RSA)
5503    else {
5504        err = NO_VALID_DEVID;
5505    }
5506    #endif
5507#endif
5508
5509#ifndef WOLF_CRYPTO_CB_ONLY_RSA
5510#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \
5511    defined(WC_ASYNC_ENABLE_RSA_KEYGEN)
5512    if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA) {
5513    #ifdef HAVE_CAVIUM
5514        /* TODO: Not implemented */
5515    #elif defined(HAVE_INTEL_QA)
5516        err = IntelQaRsaKeyGen(&key->asyncDev, key, size, e, rng);
5517        goto out;
5518    #elif defined(WOLFSSL_ASYNC_CRYPT_SW)
5519        if (wc_AsyncSwInit(&key->asyncDev, ASYNC_SW_RSA_MAKE)) {
5520            WC_ASYNC_SW* sw = &key->asyncDev.sw;
5521            sw->rsaMake.rng = rng;
5522            sw->rsaMake.key = key;
5523            sw->rsaMake.size = size;
5524            sw->rsaMake.e = e;
5525            err = WC_PENDING_E;
5526            goto out;
5527        }
5528    #endif
5529    }
5530#endif
5531
5532    err = mp_init_multi(p, q, tmp1, tmp2, tmp3, NULL);
5533
5534    if (err == MP_OKAY)
5535        err = mp_set_int(tmp3, (unsigned long)e);
5536
5537    /* The failCount value comes from NIST FIPS 186-4, section B.3.3,
5538     * process steps 4.7 and 5.8. */
5539    failCount = 5 * (size / 2);
5540    primeSz = (word32)size / 16; /* size is the size of n in bits.
5541                            primeSz is in bytes. */
5542
5543#ifndef WOLFSSL_NO_MALLOC
5544    /* allocate buffer to work with */
5545    if (err == MP_OKAY) {
5546        buf = (byte*)XMALLOC(primeSz, key->heap, DYNAMIC_TYPE_RSA);
5547        if (buf == NULL)
5548            err = MEMORY_E;
5549    }
5550#endif
5551
5552    SAVE_VECTOR_REGISTERS(err = _svr_ret;);
5553
5554    /* make p */
5555    if (err == MP_OKAY) {
5556    #ifdef WOLFSSL_CHECK_MEM_ZERO
5557        wc_MemZero_Add("RSA gen buf", buf, primeSz);
5558        mp_memzero_add("RSA gen p", p);
5559        mp_memzero_add("RSA gen q", q);
5560        mp_memzero_add("RSA gen tmp1", tmp1);
5561        mp_memzero_add("RSA gen tmp2", tmp2);
5562        mp_memzero_add("RSA gen tmp3", tmp3);
5563    #endif
5564        isPrime = 0;
5565        i = 0;
5566        for (;;) {
5567#ifdef SHOW_GEN
5568            printf(".");
5569            fflush(stdout);
5570#endif
5571            /* generate value */
5572            err = wc_RNG_GenerateBlock(rng, buf, primeSz);
5573            if (err == 0) {
5574                /* prime lower bound has the MSB set, set it in candidate */
5575                buf[0] |= 0x80;
5576                /* make candidate odd */
5577                buf[primeSz-1] |= 0x01;
5578                /* load value */
5579                err = mp_read_unsigned_bin(p, buf, primeSz);
5580            }
5581
5582            if (err == MP_OKAY)
5583                err = _CheckProbablePrime(p, NULL, tmp3, size, &isPrime, rng);
5584
5585#ifdef HAVE_FIPS
5586            i++;
5587#else
5588            /* Keep the old retry behavior in non-FIPS build. */
5589#endif
5590
5591            if (err != MP_OKAY || isPrime || i >= failCount)
5592                break;
5593
5594            /* linuxkm: release the kernel for a moment before iterating. */
5595            RESTORE_VECTOR_REGISTERS();
5596            SAVE_VECTOR_REGISTERS(err = _svr_ret; break;);
5597        };
5598    }
5599
5600    if (err == MP_OKAY && !isPrime)
5601        err = PRIME_GEN_E;
5602
5603    /* make q */
5604    if (err == MP_OKAY) {
5605        isPrime = 0;
5606        i = 0;
5607        do {
5608#ifdef SHOW_GEN
5609            printf(".");
5610            fflush(stdout);
5611#endif
5612            /* generate value */
5613            err = wc_RNG_GenerateBlock(rng, buf, primeSz);
5614            if (err == 0) {
5615                /* prime lower bound has the MSB set, set it in candidate */
5616                buf[0] |= 0x80;
5617                /* make candidate odd */
5618                buf[primeSz-1] |= 0x01;
5619                /* load value */
5620                err = mp_read_unsigned_bin(q, buf, primeSz);
5621            }
5622
5623            if (err == MP_OKAY)
5624                err = _CheckProbablePrime(p, q, tmp3, size, &isPrime, rng);
5625
5626#ifndef WC_RSA_NO_FERMAT_CHECK
5627            if (err == MP_OKAY && isPrime) {
5628                /* Fermat's Factorization works when difference between p and q
5629                 * is less than (conservatively):
5630                 *     n^(1/4) + 32
5631                 *  ~= 2^(bit count of n)^(1/4) + 32)
5632                 *   = 2^((bit count of n)/4 + 32)
5633                 */
5634                err = mp_sub(p, q, tmp1);
5635                if (err == MP_OKAY && mp_count_bits(tmp1) <= (size / 4) + 32) {
5636                    isPrime = 0;
5637                }
5638            }
5639#endif
5640
5641#ifdef HAVE_FIPS
5642            i++;
5643#else
5644            /* Keep the old retry behavior in non-FIPS build. */
5645            (void)i;
5646#endif
5647        } while (err == MP_OKAY && !isPrime && i < failCount);
5648    }
5649
5650    if (err == MP_OKAY && !isPrime)
5651        err = PRIME_GEN_E;
5652
5653#ifndef WOLFSSL_NO_MALLOC
5654    if (buf) {
5655        ForceZero(buf, primeSz);
5656        XFREE(buf, key->heap, DYNAMIC_TYPE_RSA);
5657    }
5658#else
5659    ForceZero(buf, primeSz);
5660#endif
5661
5662    if (err == MP_OKAY && mp_cmp(p, q) < 0) {
5663        err = mp_copy(p, tmp1);
5664        if (err == MP_OKAY)
5665            err = mp_copy(q, p);
5666        if (err == MP_OKAY)
5667            mp_copy(tmp1, q);
5668    }
5669
5670    /* Setup RsaKey buffers */
5671    if (err == MP_OKAY)
5672        err = mp_init_multi(&key->n, &key->e, &key->d, &key->p, &key->q, NULL);
5673    if (err == MP_OKAY)
5674        err = mp_init_multi(&key->dP, &key->dQ, &key->u, NULL, NULL, NULL);
5675
5676    /* Software Key Calculation */
5677    if (err == MP_OKAY)                /* tmp1 = p-1 */
5678        err = mp_sub_d(p, 1, tmp1);
5679    if (err == MP_OKAY)                /* tmp2 = q-1 */
5680        err = mp_sub_d(q, 1, tmp2);
5681#ifdef WC_RSA_BLINDING
5682    if (err == MP_OKAY)                /* tmp3 = order of n */
5683        err = mp_mul(tmp1, tmp2, tmp3);
5684#else
5685    if (err == MP_OKAY)                /* tmp3 = lcm(p-1, q-1), last loop */
5686        err = mp_lcm(tmp1, tmp2, tmp3);
5687#endif
5688    /* make key */
5689    if (err == MP_OKAY)                /* key->e = e */
5690        err = mp_set_int(&key->e, (unsigned long)e);
5691#ifdef WC_RSA_BLINDING
5692    /* Blind the inverse operation with a value that is invertable */
5693    if (err == MP_OKAY) {
5694        do {
5695            err = mp_rand(&key->p, mp_get_digit_count(tmp3), rng);
5696            if (err == MP_OKAY)
5697                err = mp_set_bit(&key->p, 0);
5698            if (err == MP_OKAY)
5699                err = mp_set_bit(&key->p, size - 1);
5700            if (err == MP_OKAY)
5701                err = mp_gcd(&key->p, tmp3, &key->q);
5702        }
5703        while ((err == MP_OKAY) && !mp_isone(&key->q));
5704    }
5705    /* 8/16-bit word size requires a full multiply when e=0x10001 */
5706    if (err == MP_OKAY)
5707        err = mp_mul(&key->p, &key->e, &key->e);
5708#endif
5709    if (err == MP_OKAY)                /* key->d = 1/e mod lcm(p-1, q-1) */
5710        err = mp_invmod(&key->e, tmp3, &key->d);
5711#ifdef WC_RSA_BLINDING
5712    /* Take off blinding from d and reset e */
5713    if (err == MP_OKAY)
5714        err = mp_mulmod(&key->d, &key->p, tmp3, &key->d);
5715    if (err == MP_OKAY)
5716        err = mp_set_int(&key->e, (unsigned long)e);
5717#endif
5718    if (err == MP_OKAY)                /* key->n = pq */
5719        err = mp_mul(p, q, &key->n);
5720    if (err == MP_OKAY)                /* key->dP = d mod(p-1) */
5721        err = mp_mod(&key->d, tmp1, &key->dP);
5722    if (err == MP_OKAY)                /* key->dQ = d mod(q-1) */
5723        err = mp_mod(&key->d, tmp2, &key->dQ);
5724#ifdef WOLFSSL_MP_INVMOD_CONSTANT_TIME
5725    if (err == MP_OKAY)                /* key->u = 1/q mod p */
5726        err = mp_invmod(q, p, &key->u);
5727#else
5728    if (err == MP_OKAY)
5729        err = mp_sub_d(p, 2, tmp3);
5730    if (err == MP_OKAY)                /* key->u = 1/q mod p = q^p-2 mod p */
5731        err = mp_exptmod(q, tmp3, p, &key->u);
5732#endif
5733    if (err == MP_OKAY)
5734        err = mp_copy(p, &key->p);
5735    if (err == MP_OKAY)
5736        err = mp_copy(q, &key->q);
5737
5738#ifdef HAVE_WOLF_BIGINT
5739    /* make sure raw unsigned bin version is available */
5740    if (err == MP_OKAY)
5741         err = wc_mp_to_bigint(&key->n, &key->n.raw);
5742    if (err == MP_OKAY)
5743         err = wc_mp_to_bigint(&key->e, &key->e.raw);
5744    if (err == MP_OKAY)
5745         err = wc_mp_to_bigint(&key->d, &key->d.raw);
5746    if (err == MP_OKAY)
5747         err = wc_mp_to_bigint(&key->p, &key->p.raw);
5748    if (err == MP_OKAY)
5749         err = wc_mp_to_bigint(&key->q, &key->q.raw);
5750    if (err == MP_OKAY)
5751         err = wc_mp_to_bigint(&key->dP, &key->dP.raw);
5752    if (err == MP_OKAY)
5753         err = wc_mp_to_bigint(&key->dQ, &key->dQ.raw);
5754    if (err == MP_OKAY)
5755         err = wc_mp_to_bigint(&key->u, &key->u.raw);
5756#endif
5757
5758    if (err == MP_OKAY)
5759        key->type = RSA_PRIVATE;
5760
5761#ifdef WOLFSSL_CHECK_MEM_ZERO
5762    if (err == MP_OKAY) {
5763        mp_memzero_add("Make RSA key d", &key->d);
5764        mp_memzero_add("Make RSA key p", &key->p);
5765        mp_memzero_add("Make RSA key q", &key->q);
5766        mp_memzero_add("Make RSA key dP", &key->dP);
5767        mp_memzero_add("Make RSA key dQ", &key->dQ);
5768        mp_memzero_add("Make RSA key u", &key->u);
5769    }
5770#endif
5771
5772    if (err != WC_NO_ERR_TRACE(WC_ACCEL_INHIBIT_E))
5773        RESTORE_VECTOR_REGISTERS();
5774
5775    /* Last value p - 1. */
5776    mp_forcezero(tmp1);
5777    /* Last value q - 1. */
5778    mp_forcezero(tmp2);
5779    /* Last value p - 2. */
5780    mp_forcezero(tmp3);
5781    mp_forcezero(p);
5782    mp_forcezero(q);
5783
5784#ifdef WOLFSSL_RSA_KEY_CHECK
5785    /* Perform the pair-wise consistency test on the new key. */
5786    if (err == 0)
5787        err = _ifc_pairwise_consistency_test(key, rng);
5788#endif
5789
5790    if (err != 0) {
5791        wc_FreeRsaKey(key);
5792        goto out;
5793    }
5794
5795#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_CRYPTOCELL)
5796    if (wc_InitRsaHw(key) != 0) {
5797        return BAD_STATE_E;
5798    }
5799#endif
5800
5801    err = 0;
5802#endif /* WOLF_CRYPTO_CB_ONLY_RSA */
5803#endif /* WOLFSSL_CRYPTOCELL / SW only */
5804  out:
5805
5806#if !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
5807#ifdef WOLFSSL_SMALL_STACK
5808    if (key != NULL) {
5809        XFREE(p, key->heap, DYNAMIC_TYPE_RSA);
5810        XFREE(q, key->heap, DYNAMIC_TYPE_RSA);
5811        XFREE(tmp1, key->heap, DYNAMIC_TYPE_RSA);
5812        XFREE(tmp2, key->heap, DYNAMIC_TYPE_RSA);
5813        XFREE(tmp3, key->heap, DYNAMIC_TYPE_RSA);
5814    }
5815#elif defined(WOLFSSL_CHECK_MEM_ZERO)
5816    mp_memzero_check(p);
5817    mp_memzero_check(q);
5818    mp_memzero_check(tmp1);
5819    mp_memzero_check(tmp2);
5820    mp_memzero_check(tmp3);
5821#endif /* WOLFSSL_SMALL_STACK */
5822#endif /* !WOLFSSL_CRYPTOCELL && !WOLFSSL_SE050 */
5823
5824    return err;
5825
5826#else
5827    return NOT_COMPILED_IN;
5828#endif
5829}
5830#endif /* !FIPS || FIPS_VER >= 2 */
5831#endif /* WOLFSSL_KEY_GEN */
5832
5833#ifndef WC_NO_RNG
5834int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng)
5835{
5836    if (key == NULL || rng == NULL)
5837        return BAD_FUNC_ARG;
5838
5839    key->rng = rng;
5840
5841    return 0;
5842}
5843#endif /* !WC_NO_RNG */
5844
5845#ifdef WC_RSA_NONBLOCK
5846int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb)
5847{
5848    if (key == NULL)
5849        return BAD_FUNC_ARG;
5850
5851    if (nb) {
5852        XMEMSET(nb, 0, sizeof(RsaNb));
5853    }
5854
5855    /* Allow nb == NULL to clear non-block mode */
5856    key->nb = nb;
5857
5858    return 0;
5859}
5860#if defined(WC_RSA_NONBLOCK_TIME) && defined(USE_FAST_MATH)
5861int wc_RsaSetNonBlockTime(RsaKey* key, word32 maxBlockUs, word32 cpuMHz)
5862{
5863    if (key == NULL || key->nb == NULL) {
5864        return BAD_FUNC_ARG;
5865    }
5866
5867    /* calculate maximum number of instructions to block */
5868    key->nb->exptmod.maxBlockInst = cpuMHz * maxBlockUs;
5869
5870    return 0;
5871}
5872#endif /* WC_RSA_NONBLOCK_TIME && USE_FAST_MATH */
5873#endif /* WC_RSA_NONBLOCK */
5874
5875#ifndef WOLFSSL_RSA_PUBLIC_ONLY
5876
5877#if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
5878/*
5879 * Calculate  y = d mod(x-1)
5880 */
5881static int CalcDX(mp_int* y, mp_int* x, mp_int* d)
5882{
5883    int err;
5884#ifndef WOLFSSL_SMALL_STACK
5885    mp_int  m[1];
5886#else
5887    mp_int* m = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_WOLF_BIGINT);
5888    if (m == NULL)
5889        return MEMORY_E;
5890#endif
5891
5892    err = mp_init(m);
5893    if (err == MP_OKAY) {
5894        err = mp_sub_d(x, 1, m);
5895        if (err == MP_OKAY)
5896            err = mp_mod(d, m, y);
5897        mp_forcezero(m);
5898    }
5899
5900    WC_FREE_VAR_EX(m, NULL, DYNAMIC_TYPE_WOLF_BIGINT);
5901
5902    return err;
5903}
5904#endif
5905
5906/* Software-only import of RSA private key elements into RsaKey.
5907 * This internal helper avoids recursion when called from the SETKEY path. */
5908static int _RsaPrivateKeyDecodeRaw(const byte* n, word32 nSz,
5909        const byte* e, word32 eSz, const byte* d, word32 dSz,
5910        const byte* u, word32 uSz, const byte* p, word32 pSz,
5911        const byte* q, word32 qSz, const byte* dP, word32 dPSz,
5912        const byte* dQ, word32 dQSz, RsaKey* key)
5913{
5914    int err = MP_OKAY;
5915
5916    if (n == NULL || nSz == 0 || e == NULL || eSz == 0
5917            || d == NULL || dSz == 0 || p == NULL || pSz == 0
5918            || q == NULL || qSz == 0 || key == NULL) {
5919        return BAD_FUNC_ARG;
5920    }
5921
5922#if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
5923    if ((u == NULL || uSz == 0)
5924            || (dP != NULL && dPSz == 0)
5925            || (dQ != NULL && dQSz == 0)) {
5926        return BAD_FUNC_ARG;
5927    }
5928#else
5929    (void)u;
5930    (void)uSz;
5931    (void)dP;
5932    (void)dPSz;
5933    (void)dQ;
5934    (void)dQSz;
5935#endif
5936
5937    if (err == MP_OKAY) {
5938        err = mp_read_unsigned_bin(&key->n, n, nSz);
5939    }
5940    if (err == MP_OKAY) {
5941        err = mp_read_unsigned_bin(&key->e, e, eSz);
5942    }
5943    if (err == MP_OKAY) {
5944        err = mp_read_unsigned_bin(&key->d, d, dSz);
5945    }
5946    if (err == MP_OKAY) {
5947        err = mp_read_unsigned_bin(&key->p, p, pSz);
5948    }
5949    if (err == MP_OKAY) {
5950        err = mp_read_unsigned_bin(&key->q, q, qSz);
5951    }
5952#if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
5953    if (err == MP_OKAY) {
5954        err = mp_read_unsigned_bin(&key->u, u, uSz);
5955    }
5956    if (err == MP_OKAY) {
5957        if (dP != NULL) {
5958            err = mp_read_unsigned_bin(&key->dP, dP, dPSz);
5959        }
5960        else {
5961            err = CalcDX(&key->dP, &key->p, &key->d);
5962        }
5963    }
5964    if (err == MP_OKAY) {
5965        if (dQ != NULL) {
5966            err = mp_read_unsigned_bin(&key->dQ, dQ, dQSz);
5967        }
5968        else {
5969            err = CalcDX(&key->dQ, &key->q, &key->d);
5970        }
5971    }
5972#endif
5973
5974    if (err == MP_OKAY) {
5975        key->type = RSA_PRIVATE;
5976    }
5977    else {
5978        mp_clear(&key->n);
5979        mp_clear(&key->e);
5980        mp_forcezero(&key->d);
5981        mp_forcezero(&key->p);
5982        mp_forcezero(&key->q);
5983#if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
5984        mp_forcezero(&key->u);
5985        mp_forcezero(&key->dP);
5986        mp_forcezero(&key->dQ);
5987#endif
5988    }
5989
5990    return err;
5991}
5992
5993int wc_RsaPrivateKeyDecodeRaw(const byte* n, word32 nSz,
5994        const byte* e, word32 eSz, const byte* d, word32 dSz,
5995        const byte* u, word32 uSz, const byte* p, word32 pSz,
5996        const byte* q, word32 qSz, const byte* dP, word32 dPSz,
5997        const byte* dQ, word32 dQSz, RsaKey* key)
5998{
5999    int err = MP_OKAY;
6000#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_SETKEY)
6001    int cbRet = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
6002    WC_DECLARE_VAR(tmpKey, RsaKey, 1, NULL);
6003#endif
6004
6005    if (n == NULL || nSz == 0 || e == NULL || eSz == 0
6006            || d == NULL || dSz == 0 || p == NULL || pSz == 0
6007            || q == NULL || qSz == 0 || key == NULL) {
6008        err = BAD_FUNC_ARG;
6009    }
6010
6011#if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
6012    if (err == MP_OKAY) {
6013        if ((u == NULL || uSz == 0)
6014                || (dP != NULL && dPSz == 0)
6015                || (dQ != NULL && dQSz == 0)) {
6016            err = BAD_FUNC_ARG;
6017        }
6018    }
6019#else
6020    (void)u;
6021    (void)uSz;
6022    (void)dP;
6023    (void)dPSz;
6024    (void)dQ;
6025    (void)dQSz;
6026#endif
6027
6028#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_SETKEY)
6029    #ifndef WOLF_CRYPTO_CB_FIND
6030    if (err == MP_OKAY && key->devId != INVALID_DEVID)
6031    #else
6032    if (err == MP_OKAY)
6033    #endif
6034    {
6035        /* Allocate temp key for callback to export from */
6036        WC_ALLOC_VAR(tmpKey, RsaKey, 1, key->heap);
6037        if (!WC_VAR_OK(tmpKey)) {
6038            return MEMORY_E;
6039        }
6040        XMEMSET(tmpKey, 0, sizeof(RsaKey));
6041
6042        /* Init temp with INVALID_DEVID to prevent callback recursion */
6043        err = wc_InitRsaKey_ex(tmpKey, key->heap, INVALID_DEVID);
6044        if (err != MP_OKAY) {
6045            WC_FREE_VAR(tmpKey, key->heap);
6046            return err;
6047        }
6048
6049        /* Import into temp via software helper (no callback recursion) */
6050        err = _RsaPrivateKeyDecodeRaw(n, nSz, e, eSz, d, dSz,
6051            u, uSz, p, pSz, q, qSz, dP, dPSz, dQ, dQSz, tmpKey);
6052        if (err == MP_OKAY) {
6053            cbRet = wc_CryptoCb_SetKey(key->devId,
6054                WC_SETKEY_RSA_PRIV, key, tmpKey,
6055                wc_RsaEncryptSize(tmpKey), NULL, 0, 0);
6056        }
6057
6058        /* wc_FreeRsaKey calls mp_forcezero on all private key components,
6059         * so no separate ForceZero of the struct is needed here. */
6060        wc_FreeRsaKey(tmpKey);
6061        WC_FREE_VAR(tmpKey, key->heap);
6062
6063        if (err != MP_OKAY) {
6064            return err;
6065        }
6066        if (cbRet != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
6067            return cbRet;
6068        }
6069        /* CRYPTOCB_UNAVAILABLE: fall through to software import */
6070        err = MP_OKAY;
6071    }
6072#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_SETKEY */
6073
6074    if (err == MP_OKAY) {
6075        err = _RsaPrivateKeyDecodeRaw(n, nSz, e, eSz, d, dSz,
6076            u, uSz, p, pSz, q, qSz, dP, dPSz, dQ, dQSz, key);
6077    }
6078
6079    return err;
6080}
6081#endif /* WOLFSSL_RSA_PUBLIC_ONLY */
6082
6083#endif /* NO_RSA */