├── .gitignore ├── .gitmodules ├── Makefile ├── README.md ├── card_reader.py ├── gtest ├── bstrcheck.cpp ├── dolcheck.cpp ├── makefile ├── ptest.cpp ├── stm32fsheck.cpp └── tlvcheck.cpp ├── libs ├── bearssl │ ├── LICENSE.txt │ ├── aes_big_cbcdec.c │ ├── aes_big_cbcenc.c │ ├── aes_big_ctr.c │ ├── aes_big_ctrcbc.c │ ├── aes_big_dec.c │ ├── aes_big_enc.c │ ├── aes_common.c │ ├── aes_ct.c │ ├── aes_ct64.c │ ├── aes_ct64_cbcdec.c │ ├── aes_ct64_cbcenc.c │ ├── aes_ct64_ctr.c │ ├── aes_ct64_ctrcbc.c │ ├── aes_ct64_dec.c │ ├── aes_ct64_enc.c │ ├── aes_ct_cbcdec.c │ ├── aes_ct_cbcenc.c │ ├── aes_ct_ctr.c │ ├── aes_ct_ctrcbc.c │ ├── aes_ct_dec.c │ ├── aes_ct_enc.c │ ├── aes_pwr8.c │ ├── aes_pwr8_cbcdec.c │ ├── aes_pwr8_cbcenc.c │ ├── aes_pwr8_ctr.c │ ├── aes_pwr8_ctrcbc.c │ ├── aes_small_cbcdec.c │ ├── aes_small_cbcenc.c │ ├── aes_small_ctr.c │ ├── aes_small_ctrcbc.c │ ├── aes_small_dec.c │ ├── aes_small_enc.c │ ├── aes_x86ni.c │ ├── aes_x86ni_cbcdec.c │ ├── aes_x86ni_cbcenc.c │ ├── aes_x86ni_ctr.c │ ├── aesctr_drbg.c │ ├── asn1enc.c │ ├── bearssl.h │ ├── bearssl_aead.h │ ├── bearssl_block.h │ ├── bearssl_ec.h │ ├── bearssl_hash.h │ ├── bearssl_hmac.h │ ├── bearssl_kdf.h │ ├── bearssl_pem.h │ ├── bearssl_prf.h │ ├── bearssl_rand.h │ ├── bearssl_rsa.h │ ├── bearssl_ssl.h │ ├── bearssl_x509.h │ ├── ccm.c │ ├── ccopy.c │ ├── chacha20_ct.c │ ├── chacha20_sse2.c │ ├── config.h │ ├── dec16be.c │ ├── dec16le.c │ ├── dec32be.c │ ├── dec32le.c │ ├── dec64be.c │ ├── dec64le.c │ ├── des_ct.c │ ├── des_ct_cbcdec.c │ ├── des_ct_cbcenc.c │ ├── des_support.c │ ├── des_tab.c │ ├── des_tab_cbcdec.c │ ├── des_tab_cbcenc.c │ ├── dig_oid.c │ ├── dig_size.c │ ├── eax.c │ ├── ec_all_m15.c │ ├── ec_all_m31.c │ ├── ec_c25519_i15.c │ ├── ec_c25519_i31.c │ ├── ec_c25519_m15.c │ ├── ec_c25519_m31.c │ ├── ec_curve25519.c │ ├── ec_default.c │ ├── ec_keygen.c │ ├── ec_p256_m15.c │ ├── ec_p256_m31.c │ ├── ec_prime_i15.c │ ├── ec_prime_i31.c │ ├── ec_pubkey.c │ ├── ec_secp256r1.c │ ├── ec_secp384r1.c │ ├── ec_secp521r1.c │ ├── ecdsa_atr.c │ ├── ecdsa_default_sign_asn1.c │ ├── ecdsa_default_sign_raw.c │ ├── ecdsa_default_vrfy_asn1.c │ ├── ecdsa_default_vrfy_raw.c │ ├── ecdsa_i15_bits.c │ ├── ecdsa_i15_sign_asn1.c │ ├── ecdsa_i15_sign_raw.c │ ├── ecdsa_i15_vrfy_asn1.c │ ├── ecdsa_i15_vrfy_raw.c │ ├── ecdsa_i31_bits.c │ ├── ecdsa_i31_sign_asn1.c │ ├── ecdsa_i31_sign_raw.c │ ├── ecdsa_i31_vrfy_asn1.c │ ├── ecdsa_i31_vrfy_raw.c │ ├── ecdsa_rta.c │ ├── enc16be.c │ ├── enc16le.c │ ├── enc32be.c │ ├── enc32le.c │ ├── enc64be.c │ ├── enc64le.c │ ├── encode_ec_pk8der.c │ ├── encode_ec_rawder.c │ ├── encode_rsa_pk8der.c │ ├── encode_rsa_rawder.c │ ├── gcm.c │ ├── ghash_ctmul.c │ ├── ghash_ctmul32.c │ ├── ghash_ctmul64.c │ ├── ghash_pclmul.c │ ├── ghash_pwr8.c │ ├── hkdf.c │ ├── hmac.c │ ├── hmac_ct.c │ ├── hmac_drbg.c │ ├── i15_add.c │ ├── i15_addon.c │ ├── i15_addon.h │ ├── i15_bitlen.c │ ├── i15_decmod.c │ ├── i15_decode.c │ ├── i15_decred.c │ ├── i15_encode.c │ ├── i15_fmont.c │ ├── i15_iszero.c │ ├── i15_moddiv.c │ ├── i15_modpow.c │ ├── i15_modpow2.c │ ├── i15_montmul.c │ ├── i15_mulacc.c │ ├── i15_muladd.c │ ├── i15_ninv15.c │ ├── i15_reduce.c │ ├── i15_rshift.c │ ├── i15_sub.c │ ├── i15_tmont.c │ ├── i31_add.c │ ├── i31_bitlen.c │ ├── i31_decmod.c │ ├── i31_decode.c │ ├── i31_decred.c │ ├── i31_encode.c │ ├── i31_fmont.c │ ├── i31_iszero.c │ ├── i31_moddiv.c │ ├── i31_modpow.c │ ├── i31_modpow2.c │ ├── i31_montmul.c │ ├── i31_mulacc.c │ ├── i31_muladd.c │ ├── i31_ninv31.c │ ├── i31_reduce.c │ ├── i31_rshift.c │ ├── i31_sub.c │ ├── i31_tmont.c │ ├── i32_add.c │ ├── i32_bitlen.c │ ├── i32_decmod.c │ ├── i32_decode.c │ ├── i32_decred.c │ ├── i32_div32.c │ ├── i32_encode.c │ ├── i32_fmont.c │ ├── i32_iszero.c │ ├── i32_modpow.c │ ├── i32_montmul.c │ ├── i32_mulacc.c │ ├── i32_muladd.c │ ├── i32_ninv32.c │ ├── i32_reduce.c │ ├── i32_sub.c │ ├── i32_tmont.c │ ├── i62_modpow2.c │ ├── inner.h │ ├── md5.c │ ├── md5sha1.c │ ├── mgf1.c │ ├── multihash.c │ ├── pemdec.c │ ├── pemenc.c │ ├── poly1305_ctmul.c │ ├── poly1305_ctmul32.c │ ├── poly1305_ctmulq.c │ ├── poly1305_i15.c │ ├── prf.c │ ├── prf_md5sha1.c │ ├── prf_sha256.c │ ├── prf_sha384.c │ ├── rsa_default_keygen.c │ ├── rsa_default_modulus.c │ ├── rsa_default_oaep_decrypt.c │ ├── rsa_default_oaep_encrypt.c │ ├── rsa_default_pkcs1_sign.c │ ├── rsa_default_pkcs1_vrfy.c │ ├── rsa_default_priv.c │ ├── rsa_default_privexp.c │ ├── rsa_default_pub.c │ ├── rsa_default_pubexp.c │ ├── rsa_i15_keygen.c │ ├── rsa_i15_modulus.c │ ├── rsa_i15_oaep_decrypt.c │ ├── rsa_i15_oaep_encrypt.c │ ├── rsa_i15_pkcs1_sign.c │ ├── rsa_i15_pkcs1_vrfy.c │ ├── rsa_i15_priv.c │ ├── rsa_i15_privexp.c │ ├── rsa_i15_pub.c │ ├── rsa_i15_pubexp.c │ ├── rsa_i31_keygen.c │ ├── rsa_i31_keygen_inner.c │ ├── rsa_i31_modulus.c │ ├── rsa_i31_oaep_decrypt.c │ ├── rsa_i31_oaep_encrypt.c │ ├── rsa_i31_pkcs1_sign.c │ ├── rsa_i31_pkcs1_vrfy.c │ ├── rsa_i31_priv.c │ ├── rsa_i31_privexp.c │ ├── rsa_i31_pub.c │ ├── rsa_i31_pubexp.c │ ├── rsa_i32_oaep_decrypt.c │ ├── rsa_i32_oaep_encrypt.c │ ├── rsa_i32_pkcs1_sign.c │ ├── rsa_i32_pkcs1_vrfy.c │ ├── rsa_i32_priv.c │ ├── rsa_i32_pub.c │ ├── rsa_i62_keygen.c │ ├── rsa_i62_oaep_decrypt.c │ ├── rsa_i62_oaep_encrypt.c │ ├── rsa_i62_pkcs1_sign.c │ ├── rsa_i62_pkcs1_vrfy.c │ ├── rsa_i62_priv.c │ ├── rsa_i62_pub.c │ ├── rsa_oaep_pad.c │ ├── rsa_oaep_unpad.c │ ├── rsa_pkcs1_sig_pad.c │ ├── rsa_pkcs1_sig_unpad.c │ ├── rsa_ssl_decrypt.c │ ├── settings.c │ ├── sha1.c │ ├── sha2big.c │ ├── sha2small.c │ ├── skey_decoder.c │ ├── ssl_ccert_single_ec.c │ ├── ssl_ccert_single_rsa.c │ ├── ssl_client.c │ ├── ssl_client_default_rsapub.c │ ├── ssl_client_full.c │ ├── ssl_engine.c │ ├── ssl_engine_default_aescbc.c │ ├── ssl_engine_default_aesccm.c │ ├── ssl_engine_default_aesgcm.c │ ├── ssl_engine_default_chapol.c │ ├── ssl_engine_default_descbc.c │ ├── ssl_engine_default_ec.c │ ├── ssl_engine_default_ecdsa.c │ ├── ssl_engine_default_rsavrfy.c │ ├── ssl_hashes.c │ ├── ssl_hs_client.c │ ├── ssl_hs_server.c │ ├── ssl_io.c │ ├── ssl_keyexport.c │ ├── ssl_lru.c │ ├── ssl_rec_cbc.c │ ├── ssl_rec_ccm.c │ ├── ssl_rec_chapol.c │ ├── ssl_rec_gcm.c │ ├── ssl_scert_single_ec.c │ ├── ssl_scert_single_rsa.c │ ├── ssl_server.c │ ├── ssl_server_full_ec.c │ ├── ssl_server_full_rsa.c │ ├── ssl_server_mine2c.c │ ├── ssl_server_mine2g.c │ ├── ssl_server_minf2c.c │ ├── ssl_server_minf2g.c │ ├── ssl_server_minr2g.c │ ├── ssl_server_minu2g.c │ ├── ssl_server_minv2g.c │ ├── sysrng.c │ ├── x509_decoder.c │ ├── x509_knownkey.c │ ├── x509_minimal.c │ └── x509_minimal_full.c └── stm32fs │ ├── stm32fs.cpp │ └── stm32fs.h ├── obj └── .dummy ├── pc ├── Makefile ├── ccid.cpp ├── ccid.h ├── main.cpp ├── opgpdevice.cpp ├── usbip.cpp └── usbip.h ├── pytest ├── card_const.py ├── card_pcsc_reader.py ├── card_reader.py ├── card_test_empty_card.py ├── card_test_kdf_full.py ├── card_test_kdf_single.py ├── card_test_keygen.py ├── card_test_personalize_admin_less.py ├── card_test_personalize_card.py ├── card_test_personalize_reset.py ├── card_test_remove_keys.py ├── card_test_reset_pw3.py ├── conftest.py ├── constants_for_test.py ├── ecdsa_keys.py ├── gcrypt.dll ├── kdf_calc.py ├── openpgp_card.py ├── readme.md ├── rsa-aut.key ├── rsa-dec.key ├── rsa-sig.key ├── rsa_keys.py ├── skip_gnuk_only_tests.py ├── test_000_empty_card.py ├── test_001_personalize_card.py ├── test_002_personalize_reset.py ├── test_003_remove_keys.py ├── test_004_reset_pw3.py ├── test_005_personalize_admin_less.py ├── test_009_keygen.py ├── test_011_kdf_full.py ├── test_016_kdf_single.py ├── test_021_personalize_admin_less.py ├── test_025_kdf_none.py ├── test_030_empty_card.py ├── test_031_user_do.py ├── test_032_rsa4096.py ├── test_033_rsa4096_keygen.py ├── test_035_ecdsa.py ├── test_036_aes.py ├── test_037_challenge.py ├── test_038_ed25519.py ├── tlv.py └── util.py ├── src ├── apduexecutor.cpp ├── apduexecutor.h ├── applications │ ├── apducommand.cpp │ ├── apducommand.h │ ├── apduconst.h │ ├── application.cpp │ ├── application.h │ ├── applicationstorage.cpp │ ├── applicationstorage.h │ ├── openpgp │ │ ├── cryptoapdu.cpp │ │ ├── cryptoapdu.h │ │ ├── openpgpconst.cpp │ │ ├── openpgpconst.h │ │ ├── openpgpfactory.cpp │ │ ├── openpgpfactory.h │ │ ├── openpgpstruct.cpp │ │ ├── openpgpstruct.h │ │ ├── resetprovider.cpp │ │ ├── resetprovider.h │ │ ├── secureapdu.cpp │ │ ├── secureapdu.h │ │ ├── security.cpp │ │ ├── security.h │ │ ├── userapdu.cpp │ │ └── userapdu.h │ ├── openpgpapplication.cpp │ ├── openpgpapplication.h │ ├── testapplication.cpp │ └── testapplication.h ├── cryptolib.cpp ├── cryptolib.h ├── errors.h ├── filesystem.cpp ├── filesystem.h ├── opgpdevice.h ├── opgputil.cpp ├── opgputil.h ├── solofactory.cpp ├── solofactory.h ├── tlv.cpp └── tlv.h ├── stm32l432 ├── Makefile.lib ├── openpgplib.cpp ├── openpgplib.h └── opgpdevice.cpp └── tmp ├── cryptolib-mbedtls.cpp └── cryptolib-mbedtls.h /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Byte-compiled / optimized / DLL files 3 | __pycache__/ 4 | *.py[cod] 5 | *$py.class 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | .pytest_cache/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | db.sqlite3 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # pyenv 77 | .python-version 78 | 79 | # celery beat schedule file 80 | celerybeat-schedule 81 | 82 | # SageMath parsed files 83 | *.sage.py 84 | 85 | # Environments 86 | .env 87 | .venv 88 | env/ 89 | venv/ 90 | ENV/ 91 | env.bak/ 92 | venv.bak/ 93 | 94 | # Spyder project settings 95 | .spyderproject 96 | .spyproject 97 | 98 | # Rope project settings 99 | .ropeproject 100 | 101 | # mkdocs documentation 102 | /site 103 | 104 | # mypy 105 | .mypy_cache/ 106 | 107 | 108 | main 109 | openpgp_test 110 | ptest 111 | *.o 112 | *.a 113 | .cproject 114 | .project 115 | .settings 116 | /Debug/ 117 | data/ 118 | pytest/.idea/ 119 | .directory 120 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solokeys/openpgp/5e5080f2b258b11921573dab87167d69cebef00d/.gitmodules -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = g++ 2 | RM = rm -rf 3 | 4 | 5 | rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)) 6 | 7 | OBJ_DIR := ./obj 8 | SRC_DIRS := ./pc \ 9 | ./src \ 10 | ./src/applications \ 11 | ./src/applications/openpgp \ 12 | ./libs/stm32fs 13 | SRC_FILES := $(sort $(foreach var, $(SRC_DIRS), $(wildcard $(var)/*.cpp))) 14 | OBJ_FILES := $(patsubst %.cpp, $(OBJ_DIR)/%.o, $(notdir $(SRC_FILES))) 15 | DEP_FILES = $(OBJ_FILES:.o=.d) 16 | 17 | 18 | INC = -I. -Ipc/ -Isrc/ -Ilibs/mbedtls/ -Ilibs/mbedtls/mbedtls/crypto/include/\ 19 | -Ilibs/stm32fs/\ 20 | -Ilibs/bearssl/ 21 | 22 | CPPFLAGS = -std=c++17 -Os -Wall -g3 $(INC) 23 | LDFLAGS = -Wl,-Bdynamic -lpthread 24 | 25 | TARGET=openpgp_test 26 | 27 | $(OBJ_DIR)/%.o: 28 | $(CC) $(CPPFLAGS) -c -o $@ $(filter %/$(strip $(patsubst %.o, %.cpp, $(notdir $@))), $(SRC_FILES)) 29 | 30 | all: $(OBJ_FILES) 31 | $(CC) -o $(TARGET) $^ $(LDFLAGS) 32 | 33 | clean: 34 | $(RM) $(OBJ_FILES) $(DEP_FILES) $(TARGET) $(MBEDTLS_OBJ) $(MBEDTLS_A) 35 | 36 | testpy: 37 | #cd ./pytest 38 | cd ~/solo/gnuk/tests; py.test-3 -x 39 | 40 | testc: 41 | cd ./gtest; make clean; make all; ./ptest 42 | 43 | testall: testc testpy 44 | -------------------------------------------------------------------------------- /gtest/makefile: -------------------------------------------------------------------------------- 1 | GOOGLE_TEST_LIB = gtest 2 | GOOGLE_TEST_INCLUDE = /usr/local/include 3 | 4 | G++ = g++ 5 | G++_FLAGS = -c -Wall -std=c++17 -I $(GOOGLE_TEST_INCLUDE) -I../src/ -DGTEST_EX 6 | LD_FLAGS = -L /usr/local/lib -l $(GOOGLE_TEST_LIB) -l pthread 7 | 8 | OBJECTS = ptest.o bstrcheck.o tlvcheck.o dolcheck.o stm32fs.o stm32fsheck.o 9 | TARGET = ptest 10 | 11 | all: $(TARGET) 12 | 13 | $(TARGET): $(OBJECTS) 14 | $(G++) -o $(TARGET) $(OBJECTS) $(LD_FLAGS) 15 | 16 | stm32fs.o : 17 | $(G++) $(G++_FLAGS) ../libs/stm32fs/stm32fs.cpp 18 | 19 | %.o : %.cpp 20 | $(G++) $(G++_FLAGS) $< 21 | 22 | clean: 23 | rm -f $(TARGET) $(OBJECTS) 24 | 25 | test: all 26 | ./ptest 27 | 28 | .PHONY: all clean test 29 | 30 | -------------------------------------------------------------------------------- /gtest/ptest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char **argv) { 4 | ::testing::InitGoogleTest(&argc, argv); 5 | auto ret = RUN_ALL_TESTS(); 6 | if (ret == 0) 7 | printf("[TestsOk]\n"); 8 | else 9 | printf("[TestsError]\n"); 10 | return ret; 11 | } 12 | -------------------------------------------------------------------------------- /libs/bearssl/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Thomas Pornin 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 18 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 19 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /libs/bearssl/ccopy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_ccopy(uint32_t ctl, void *dst, const void *src, size_t len) 30 | { 31 | unsigned char *d; 32 | const unsigned char *s; 33 | 34 | d = dst; 35 | s = src; 36 | while (len -- > 0) { 37 | uint32_t x, y; 38 | 39 | x = *s ++; 40 | y = *d; 41 | *d = MUX(ctl, x, y); 42 | d ++; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /libs/bearssl/dec16be.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_dec16be(uint16_t *v, size_t num, const void *src) 30 | { 31 | const unsigned char *buf; 32 | 33 | buf = src; 34 | while (num -- > 0) { 35 | *v ++ = br_dec16be(buf); 36 | buf += 2; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/dec16le.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_dec16le(uint16_t *v, size_t num, const void *src) 30 | { 31 | const unsigned char *buf; 32 | 33 | buf = src; 34 | while (num -- > 0) { 35 | *v ++ = br_dec16le(buf); 36 | buf += 2; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/dec32be.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_dec32be(uint32_t *v, size_t num, const void *src) 30 | { 31 | const unsigned char *buf; 32 | 33 | buf = src; 34 | while (num -- > 0) { 35 | *v ++ = br_dec32be(buf); 36 | buf += 4; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/dec32le.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_dec32le(uint32_t *v, size_t num, const void *src) 30 | { 31 | const unsigned char *buf; 32 | 33 | buf = src; 34 | while (num -- > 0) { 35 | *v ++ = br_dec32le(buf); 36 | buf += 4; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/dec64be.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_dec64be(uint64_t *v, size_t num, const void *src) 30 | { 31 | const unsigned char *buf; 32 | 33 | buf = src; 34 | while (num -- > 0) { 35 | *v ++ = br_dec64be(buf); 36 | buf += 8; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/dec64le.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_dec64le(uint64_t *v, size_t num, const void *src) 30 | { 31 | const unsigned char *buf; 32 | 33 | buf = src; 34 | while (num -- > 0) { 35 | *v ++ = br_dec64le(buf); 36 | buf += 8; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/dig_size.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | size_t 29 | br_digest_size_by_ID(int digest_id) 30 | { 31 | switch (digest_id) { 32 | case br_md5sha1_ID: 33 | return br_md5_SIZE + br_sha1_SIZE; 34 | case br_md5_ID: 35 | return br_md5_SIZE; 36 | case br_sha1_ID: 37 | return br_sha1_SIZE; 38 | case br_sha224_ID: 39 | return br_sha224_SIZE; 40 | case br_sha256_ID: 41 | return br_sha256_SIZE; 42 | case br_sha384_ID: 43 | return br_sha384_SIZE; 44 | case br_sha512_ID: 45 | return br_sha512_SIZE; 46 | default: 47 | /* abort(); */ 48 | return 0; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /libs/bearssl/ec_curve25519.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | static const unsigned char GEN[] = { 28 | 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 29 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 30 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 31 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 32 | }; 33 | 34 | static const unsigned char ORDER[] = { 35 | 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 36 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 37 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 38 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF 39 | }; 40 | 41 | /* see inner.h */ 42 | const br_ec_curve_def br_curve25519 = { 43 | BR_EC_curve25519, 44 | ORDER, sizeof ORDER, 45 | GEN, sizeof GEN 46 | }; 47 | -------------------------------------------------------------------------------- /libs/bearssl/ec_default.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ec.h */ 28 | const br_ec_impl * 29 | br_ec_get_default(void) 30 | { 31 | #if BR_LOMUL 32 | return &br_ec_all_m15; 33 | #else 34 | return &br_ec_all_m31; 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_default_sign_asn1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ec.h */ 28 | br_ecdsa_sign 29 | br_ecdsa_sign_asn1_get_default(void) 30 | { 31 | #if BR_LOMUL 32 | return &br_ecdsa_i15_sign_asn1; 33 | #else 34 | return &br_ecdsa_i31_sign_asn1; 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_default_sign_raw.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ec.h */ 28 | br_ecdsa_sign 29 | br_ecdsa_sign_raw_get_default(void) 30 | { 31 | #if BR_LOMUL 32 | return &br_ecdsa_i15_sign_raw; 33 | #else 34 | return &br_ecdsa_i31_sign_raw; 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_default_vrfy_asn1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ec.h */ 28 | br_ecdsa_vrfy 29 | br_ecdsa_vrfy_asn1_get_default(void) 30 | { 31 | #if BR_LOMUL 32 | return &br_ecdsa_i15_vrfy_asn1; 33 | #else 34 | return &br_ecdsa_i31_vrfy_asn1; 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_default_vrfy_raw.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ec.h */ 28 | br_ecdsa_vrfy 29 | br_ecdsa_vrfy_raw_get_default(void) 30 | { 31 | #if BR_LOMUL 32 | return &br_ecdsa_i15_vrfy_raw; 33 | #else 34 | return &br_ecdsa_i31_vrfy_raw; 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_i15_bits.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_ecdsa_i15_bits2int(uint16_t *x, 30 | const void *src, size_t len, uint32_t ebitlen) 31 | { 32 | uint32_t bitlen, hbitlen; 33 | int sc; 34 | 35 | bitlen = ebitlen - (ebitlen >> 4); 36 | hbitlen = (uint32_t)len << 3; 37 | if (hbitlen > bitlen) { 38 | len = (bitlen + 7) >> 3; 39 | sc = (int)((hbitlen - bitlen) & 7); 40 | } else { 41 | sc = 0; 42 | } 43 | br_i15_zero(x, ebitlen); 44 | br_i15_decode(x, src, len); 45 | br_i15_rshift(x, sc); 46 | x[0] = ebitlen; 47 | } 48 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_i15_sign_asn1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | #define ORDER_LEN ((BR_MAX_EC_SIZE + 7) >> 3) 28 | 29 | /* see bearssl_ec.h */ 30 | size_t 31 | br_ecdsa_i15_sign_asn1(const br_ec_impl *impl, 32 | const br_hash_class *hf, const void *hash_value, 33 | const br_ec_private_key *sk, void *sig) 34 | { 35 | unsigned char rsig[(ORDER_LEN << 1) + 12]; 36 | size_t sig_len; 37 | 38 | sig_len = br_ecdsa_i15_sign_raw(impl, hf, hash_value, sk, rsig); 39 | if (sig_len == 0) { 40 | return 0; 41 | } 42 | sig_len = br_ecdsa_raw_to_asn1(rsig, sig_len); 43 | memcpy(sig, rsig, sig_len); 44 | return sig_len; 45 | } 46 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_i15_vrfy_asn1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | #define FIELD_LEN ((BR_MAX_EC_SIZE + 7) >> 3) 28 | 29 | /* see bearssl_ec.h */ 30 | uint32_t 31 | br_ecdsa_i15_vrfy_asn1(const br_ec_impl *impl, 32 | const void *hash, size_t hash_len, 33 | const br_ec_public_key *pk, 34 | const void *sig, size_t sig_len) 35 | { 36 | /* 37 | * We use a double-sized buffer because a malformed ASN.1 signature 38 | * may trigger a size expansion when converting to "raw" format. 39 | */ 40 | unsigned char rsig[(FIELD_LEN << 2) + 24]; 41 | 42 | if (sig_len > ((sizeof rsig) >> 1)) { 43 | return 0; 44 | } 45 | memcpy(rsig, sig, sig_len); 46 | sig_len = br_ecdsa_asn1_to_raw(rsig, sig_len); 47 | return br_ecdsa_i15_vrfy_raw(impl, hash, hash_len, pk, rsig, sig_len); 48 | } 49 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_i31_bits.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_ecdsa_i31_bits2int(uint32_t *x, 30 | const void *src, size_t len, uint32_t ebitlen) 31 | { 32 | uint32_t bitlen, hbitlen; 33 | int sc; 34 | 35 | bitlen = ebitlen - (ebitlen >> 5); 36 | hbitlen = (uint32_t)len << 3; 37 | if (hbitlen > bitlen) { 38 | len = (bitlen + 7) >> 3; 39 | sc = (int)((hbitlen - bitlen) & 7); 40 | } else { 41 | sc = 0; 42 | } 43 | br_i31_zero(x, ebitlen); 44 | br_i31_decode(x, src, len); 45 | br_i31_rshift(x, sc); 46 | x[0] = ebitlen; 47 | } 48 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_i31_sign_asn1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | #define ORDER_LEN ((BR_MAX_EC_SIZE + 7) >> 3) 28 | 29 | /* see bearssl_ec.h */ 30 | size_t 31 | br_ecdsa_i31_sign_asn1(const br_ec_impl *impl, 32 | const br_hash_class *hf, const void *hash_value, 33 | const br_ec_private_key *sk, void *sig) 34 | { 35 | unsigned char rsig[(ORDER_LEN << 1) + 12]; 36 | size_t sig_len; 37 | 38 | sig_len = br_ecdsa_i31_sign_raw(impl, hf, hash_value, sk, rsig); 39 | if (sig_len == 0) { 40 | return 0; 41 | } 42 | sig_len = br_ecdsa_raw_to_asn1(rsig, sig_len); 43 | memcpy(sig, rsig, sig_len); 44 | return sig_len; 45 | } 46 | -------------------------------------------------------------------------------- /libs/bearssl/ecdsa_i31_vrfy_asn1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | #define FIELD_LEN ((BR_MAX_EC_SIZE + 7) >> 3) 28 | 29 | /* see bearssl_ec.h */ 30 | uint32_t 31 | br_ecdsa_i31_vrfy_asn1(const br_ec_impl *impl, 32 | const void *hash, size_t hash_len, 33 | const br_ec_public_key *pk, 34 | const void *sig, size_t sig_len) 35 | { 36 | /* 37 | * We use a double-sized buffer because a malformed ASN.1 signature 38 | * may trigger a size expansion when converting to "raw" format. 39 | */ 40 | unsigned char rsig[(FIELD_LEN << 2) + 24]; 41 | 42 | if (sig_len > ((sizeof rsig) >> 1)) { 43 | return 0; 44 | } 45 | memcpy(rsig, sig, sig_len); 46 | sig_len = br_ecdsa_asn1_to_raw(rsig, sig_len); 47 | return br_ecdsa_i31_vrfy_raw(impl, hash, hash_len, pk, rsig, sig_len); 48 | } 49 | -------------------------------------------------------------------------------- /libs/bearssl/enc16be.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_enc16be(void *dst, const uint16_t *v, size_t num) 30 | { 31 | unsigned char *buf; 32 | 33 | buf = dst; 34 | while (num -- > 0) { 35 | br_enc16be(buf, *v ++); 36 | buf += 2; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/enc16le.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_enc16le(void *dst, const uint16_t *v, size_t num) 30 | { 31 | unsigned char *buf; 32 | 33 | buf = dst; 34 | while (num -- > 0) { 35 | br_enc16le(buf, *v ++); 36 | buf += 2; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/enc32be.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_enc32be(void *dst, const uint32_t *v, size_t num) 30 | { 31 | unsigned char *buf; 32 | 33 | buf = dst; 34 | while (num -- > 0) { 35 | br_enc32be(buf, *v ++); 36 | buf += 4; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/enc32le.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_enc32le(void *dst, const uint32_t *v, size_t num) 30 | { 31 | unsigned char *buf; 32 | 33 | buf = dst; 34 | while (num -- > 0) { 35 | br_enc32le(buf, *v ++); 36 | buf += 4; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/enc64be.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_enc64be(void *dst, const uint64_t *v, size_t num) 30 | { 31 | unsigned char *buf; 32 | 33 | buf = dst; 34 | while (num -- > 0) { 35 | br_enc64be(buf, *v ++); 36 | buf += 8; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/enc64le.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_range_enc64le(void *dst, const uint64_t *v, size_t num) 30 | { 31 | unsigned char *buf; 32 | 33 | buf = dst; 34 | while (num -- > 0) { 35 | br_enc64le(buf, *v ++); 36 | buf += 8; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/i15_add.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i15_add(uint16_t *a, const uint16_t *b, uint32_t ctl) 30 | { 31 | uint32_t cc; 32 | size_t u, m; 33 | 34 | cc = 0; 35 | m = (a[0] + 31) >> 4; 36 | for (u = 1; u < m; u ++) { 37 | uint32_t aw, bw, naw; 38 | 39 | aw = a[u]; 40 | bw = b[u]; 41 | naw = aw + bw + cc; 42 | cc = naw >> 15; 43 | a[u] = MUX(ctl, naw & 0x7FFF, aw); 44 | } 45 | return cc; 46 | } 47 | -------------------------------------------------------------------------------- /libs/bearssl/i15_addon.h: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * (c) 2020 Merlok 4 | * 5 | * 6 | */ 7 | 8 | #ifndef I15_ADDON_C 9 | #define I15_ADDON_C 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #include 16 | #include 17 | 18 | #include "bearssl.h" 19 | 20 | void br_i15_print_int(const char *name, const uint16_t *x); 21 | uint32_t br_i15_sub_uint(uint16_t *a, const uint32_t b, uint32_t ctl); 22 | uint32_t br_i15_add_uint(uint16_t *a, const uint32_t b, uint32_t ctl); 23 | 24 | bool br_rsa_deduce_crt(uint8_t *buffer, br_rsa_private_key *sk, uint8_t *exp); 25 | 26 | size_t ecdh_shared_secret(const br_ec_impl *impl, br_ec_private_key *sk, 27 | br_ec_public_key *pk, uint8_t *secret); 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | #endif // I15_ADDON_C 34 | -------------------------------------------------------------------------------- /libs/bearssl/i15_bitlen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i15_bit_length(uint16_t *x, size_t xlen) 30 | { 31 | uint32_t tw, twk; 32 | 33 | tw = 0; 34 | twk = 0; 35 | while (xlen -- > 0) { 36 | uint32_t w, c; 37 | 38 | c = EQ(tw, 0); 39 | w = x[xlen]; 40 | tw = MUX(c, w, tw); 41 | twk = MUX(c, (uint32_t)xlen, twk); 42 | } 43 | return (twk << 4) + BIT_LENGTH(tw); 44 | } 45 | -------------------------------------------------------------------------------- /libs/bearssl/i15_decode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i15_decode(uint16_t *x, const void *src, size_t len) 30 | { 31 | const unsigned char *buf; 32 | size_t v; 33 | uint32_t acc; 34 | int acc_len; 35 | 36 | buf = src; 37 | v = 1; 38 | acc = 0; 39 | acc_len = 0; 40 | while (len -- > 0) { 41 | uint32_t b; 42 | 43 | b = buf[len]; 44 | acc |= (b << acc_len); 45 | acc_len += 8; 46 | if (acc_len >= 15) { 47 | x[v ++] = acc & 0x7FFF; 48 | acc_len -= 15; 49 | acc >>= 15; 50 | } 51 | } 52 | if (acc_len != 0) { 53 | x[v ++] = acc; 54 | } 55 | x[0] = br_i15_bit_length(x + 1, v - 1); 56 | } 57 | -------------------------------------------------------------------------------- /libs/bearssl/i15_encode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i15_encode(void *dst, size_t len, const uint16_t *x) 30 | { 31 | unsigned char *buf; 32 | size_t u, xlen; 33 | uint32_t acc; 34 | int acc_len; 35 | 36 | xlen = (x[0] + 15) >> 4; 37 | if (xlen == 0) { 38 | memset(dst, 0, len); 39 | return; 40 | } 41 | u = 1; 42 | acc = 0; 43 | acc_len = 0; 44 | buf = dst; 45 | while (len -- > 0) { 46 | if (acc_len < 8) { 47 | if (u <= xlen) { 48 | acc += (uint32_t)x[u ++] << acc_len; 49 | } 50 | acc_len += 15; 51 | } 52 | buf[len] = (unsigned char)acc; 53 | acc >>= 8; 54 | acc_len -= 8; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /libs/bearssl/i15_fmont.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i15_from_monty(uint16_t *x, const uint16_t *m, uint16_t m0i) 30 | { 31 | size_t len, u, v; 32 | 33 | len = (m[0] + 15) >> 4; 34 | for (u = 0; u < len; u ++) { 35 | uint32_t f, cc; 36 | 37 | f = MUL15(x[1], m0i) & 0x7FFF; 38 | cc = 0; 39 | for (v = 0; v < len; v ++) { 40 | uint32_t z; 41 | 42 | z = (uint32_t)x[v + 1] + MUL15(f, m[v + 1]) + cc; 43 | cc = z >> 15; 44 | if (v != 0) { 45 | x[v] = z & 0x7FFF; 46 | } 47 | } 48 | x[len] = cc; 49 | } 50 | 51 | /* 52 | * We may have to do an extra subtraction, but only if the 53 | * value in x[] is indeed greater than or equal to that of m[], 54 | * which is why we must do two calls (first call computes the 55 | * carry, second call performs the subtraction only if the carry 56 | * is 0). 57 | */ 58 | br_i15_sub(x, m, NOT(br_i15_sub(x, m, 0))); 59 | } 60 | -------------------------------------------------------------------------------- /libs/bearssl/i15_iszero.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i15_iszero(const uint16_t *x) 30 | { 31 | uint32_t z; 32 | size_t u; 33 | 34 | z = 0; 35 | for (u = (x[0] + 15) >> 4; u > 0; u --) { 36 | z |= x[u]; 37 | } 38 | return ~(z | -z) >> 31; 39 | } 40 | -------------------------------------------------------------------------------- /libs/bearssl/i15_modpow.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i15_modpow(uint16_t *x, 30 | const unsigned char *e, size_t elen, 31 | const uint16_t *m, uint16_t m0i, uint16_t *t1, uint16_t *t2) 32 | { 33 | size_t mlen; 34 | unsigned k; 35 | 36 | mlen = ((m[0] + 31) >> 4) * sizeof m[0]; 37 | memcpy(t1, x, mlen); 38 | br_i15_to_monty(t1, m); 39 | br_i15_zero(x, m[0]); 40 | x[1] = 1; 41 | for (k = 0; k < ((unsigned)elen << 3); k ++) { 42 | uint32_t ctl; 43 | 44 | ctl = (e[elen - 1 - (k >> 3)] >> (k & 7)) & 1; 45 | br_i15_montymul(t2, x, t1, m, m0i); 46 | CCOPY(ctl, x, t2, mlen); 47 | br_i15_montymul(t2, t1, t1, m, m0i); 48 | memcpy(t1, t2, mlen); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /libs/bearssl/i15_ninv15.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint16_t 29 | br_i15_ninv15(uint16_t x) 30 | { 31 | uint32_t y; 32 | 33 | y = 2 - x; 34 | y = MUL15(y, 2 - MUL15(x, y)); 35 | y = MUL15(y, 2 - MUL15(x, y)); 36 | y = MUL15(y, 2 - MUL15(x, y)); 37 | return MUX(x & 1, -y, 0) & 0x7FFF; 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/i15_rshift.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i15_rshift(uint16_t *x, int count) 30 | { 31 | size_t u, len; 32 | unsigned r; 33 | 34 | len = (x[0] + 15) >> 4; 35 | if (len == 0) { 36 | return; 37 | } 38 | r = x[1] >> count; 39 | for (u = 2; u <= len; u ++) { 40 | unsigned w; 41 | 42 | w = x[u]; 43 | x[u - 1] = ((w << (15 - count)) | r) & 0x7FFF; 44 | r = w >> count; 45 | } 46 | x[len] = r; 47 | } 48 | -------------------------------------------------------------------------------- /libs/bearssl/i15_sub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i15_sub(uint16_t *a, const uint16_t *b, uint32_t ctl) 30 | { 31 | uint32_t cc; 32 | size_t u, m; 33 | 34 | cc = 0; 35 | m = (a[0] + 31) >> 4; 36 | for (u = 1; u < m; u ++) { 37 | uint32_t aw, bw, naw; 38 | 39 | aw = a[u]; 40 | bw = b[u]; 41 | naw = aw - bw - cc; 42 | cc = naw >> 31; 43 | a[u] = MUX(ctl, naw & 0x7FFF, aw); 44 | } 45 | return cc; 46 | } 47 | -------------------------------------------------------------------------------- /libs/bearssl/i15_tmont.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i15_to_monty(uint16_t *x, const uint16_t *m) 30 | { 31 | unsigned k; 32 | 33 | for (k = (m[0] + 15) >> 4; k > 0; k --) { 34 | br_i15_muladd_small(x, 0, m); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/i31_add.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i31_add(uint32_t *a, const uint32_t *b, uint32_t ctl) 30 | { 31 | uint32_t cc; 32 | size_t u, m; 33 | 34 | cc = 0; 35 | m = (a[0] + 63) >> 5; 36 | for (u = 1; u < m; u ++) { 37 | uint32_t aw, bw, naw; 38 | 39 | aw = a[u]; 40 | bw = b[u]; 41 | naw = aw + bw + cc; 42 | cc = naw >> 31; 43 | a[u] = MUX(ctl, naw & (uint32_t)0x7FFFFFFF, aw); 44 | } 45 | return cc; 46 | } 47 | -------------------------------------------------------------------------------- /libs/bearssl/i31_bitlen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i31_bit_length(uint32_t *x, size_t xlen) 30 | { 31 | uint32_t tw, twk; 32 | 33 | tw = 0; 34 | twk = 0; 35 | while (xlen -- > 0) { 36 | uint32_t w, c; 37 | 38 | c = EQ(tw, 0); 39 | w = x[xlen]; 40 | tw = MUX(c, w, tw); 41 | twk = MUX(c, (uint32_t)xlen, twk); 42 | } 43 | return (twk << 5) + BIT_LENGTH(tw); 44 | } 45 | -------------------------------------------------------------------------------- /libs/bearssl/i31_decode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i31_decode(uint32_t *x, const void *src, size_t len) 30 | { 31 | const unsigned char *buf; 32 | size_t u, v; 33 | uint32_t acc; 34 | int acc_len; 35 | 36 | buf = src; 37 | u = len; 38 | v = 1; 39 | acc = 0; 40 | acc_len = 0; 41 | while (u -- > 0) { 42 | uint32_t b; 43 | 44 | b = buf[u]; 45 | acc |= (b << acc_len); 46 | acc_len += 8; 47 | if (acc_len >= 31) { 48 | x[v ++] = acc & (uint32_t)0x7FFFFFFF; 49 | acc_len -= 31; 50 | acc = b >> (8 - acc_len); 51 | } 52 | } 53 | if (acc_len != 0) { 54 | x[v ++] = acc; 55 | } 56 | x[0] = br_i31_bit_length(x + 1, v - 1); 57 | } 58 | -------------------------------------------------------------------------------- /libs/bearssl/i31_iszero.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i31_iszero(const uint32_t *x) 30 | { 31 | uint32_t z; 32 | size_t u; 33 | 34 | z = 0; 35 | for (u = (x[0] + 31) >> 5; u > 0; u --) { 36 | z |= x[u]; 37 | } 38 | return ~(z | -z) >> 31; 39 | } 40 | -------------------------------------------------------------------------------- /libs/bearssl/i31_ninv31.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i31_ninv31(uint32_t x) 30 | { 31 | uint32_t y; 32 | 33 | y = 2 - x; 34 | y *= 2 - y * x; 35 | y *= 2 - y * x; 36 | y *= 2 - y * x; 37 | y *= 2 - y * x; 38 | return MUX(x & 1, -y, 0) & 0x7FFFFFFF; 39 | } 40 | -------------------------------------------------------------------------------- /libs/bearssl/i31_rshift.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i31_rshift(uint32_t *x, int count) 30 | { 31 | size_t u, len; 32 | uint32_t r; 33 | 34 | len = (x[0] + 31) >> 5; 35 | if (len == 0) { 36 | return; 37 | } 38 | r = x[1] >> count; 39 | for (u = 2; u <= len; u ++) { 40 | uint32_t w; 41 | 42 | w = x[u]; 43 | x[u - 1] = ((w << (31 - count)) | r) & 0x7FFFFFFF; 44 | r = w >> count; 45 | } 46 | x[len] = r; 47 | } 48 | -------------------------------------------------------------------------------- /libs/bearssl/i31_sub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i31_sub(uint32_t *a, const uint32_t *b, uint32_t ctl) 30 | { 31 | uint32_t cc; 32 | size_t u, m; 33 | 34 | cc = 0; 35 | m = (a[0] + 63) >> 5; 36 | for (u = 1; u < m; u ++) { 37 | uint32_t aw, bw, naw; 38 | 39 | aw = a[u]; 40 | bw = b[u]; 41 | naw = aw - bw - cc; 42 | cc = naw >> 31; 43 | a[u] = MUX(ctl, naw & 0x7FFFFFFF, aw); 44 | } 45 | return cc; 46 | } 47 | -------------------------------------------------------------------------------- /libs/bearssl/i31_tmont.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i31_to_monty(uint32_t *x, const uint32_t *m) 30 | { 31 | uint32_t k; 32 | 33 | for (k = (m[0] + 31) >> 5; k > 0; k --) { 34 | br_i31_muladd_small(x, 0, m); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/i32_add.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i32_add(uint32_t *a, const uint32_t *b, uint32_t ctl) 30 | { 31 | uint32_t cc; 32 | size_t u, m; 33 | 34 | cc = 0; 35 | m = (a[0] + 63) >> 5; 36 | for (u = 1; u < m; u ++) { 37 | uint32_t aw, bw, naw; 38 | 39 | aw = a[u]; 40 | bw = b[u]; 41 | naw = aw + bw + cc; 42 | 43 | /* 44 | * Carry is 1 if naw < aw. Carry is also 1 if naw == aw 45 | * AND the carry was already 1. 46 | */ 47 | cc = (cc & EQ(naw, aw)) | LT(naw, aw); 48 | a[u] = MUX(ctl, naw, aw); 49 | } 50 | return cc; 51 | } 52 | -------------------------------------------------------------------------------- /libs/bearssl/i32_bitlen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i32_bit_length(uint32_t *x, size_t xlen) 30 | { 31 | uint32_t tw, twk; 32 | 33 | tw = 0; 34 | twk = 0; 35 | while (xlen -- > 0) { 36 | uint32_t w, c; 37 | 38 | c = EQ(tw, 0); 39 | w = x[xlen]; 40 | tw = MUX(c, w, tw); 41 | twk = MUX(c, (uint32_t)xlen, twk); 42 | } 43 | return (twk << 5) + BIT_LENGTH(tw); 44 | } 45 | -------------------------------------------------------------------------------- /libs/bearssl/i32_decode.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i32_decode(uint32_t *x, const void *src, size_t len) 30 | { 31 | const unsigned char *buf; 32 | size_t u, v; 33 | 34 | buf = src; 35 | u = len; 36 | v = 1; 37 | for (;;) { 38 | if (u < 4) { 39 | uint32_t w; 40 | 41 | if (u < 2) { 42 | if (u == 0) { 43 | break; 44 | } else { 45 | w = buf[0]; 46 | } 47 | } else { 48 | if (u == 2) { 49 | w = br_dec16be(buf); 50 | } else { 51 | w = ((uint32_t)buf[0] << 16) 52 | | br_dec16be(buf + 1); 53 | } 54 | } 55 | x[v ++] = w; 56 | break; 57 | } else { 58 | u -= 4; 59 | x[v ++] = br_dec32be(buf + u); 60 | } 61 | } 62 | x[0] = br_i32_bit_length(x + 1, v - 1); 63 | } 64 | -------------------------------------------------------------------------------- /libs/bearssl/i32_div32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_divrem(uint32_t hi, uint32_t lo, uint32_t d, uint32_t *r) 30 | { 31 | /* TODO: optimize this */ 32 | uint32_t q; 33 | uint32_t ch, cf; 34 | int k; 35 | 36 | q = 0; 37 | ch = EQ(hi, d); 38 | hi = MUX(ch, 0, hi); 39 | for (k = 31; k > 0; k --) { 40 | int j; 41 | uint32_t w, ctl, hi2, lo2; 42 | 43 | j = 32 - k; 44 | w = (hi << j) | (lo >> k); 45 | ctl = GE(w, d) | (hi >> k); 46 | hi2 = (w - d) >> j; 47 | lo2 = lo - (d << k); 48 | hi = MUX(ctl, hi2, hi); 49 | lo = MUX(ctl, lo2, lo); 50 | q |= ctl << k; 51 | } 52 | cf = GE(lo, d) | hi; 53 | q |= cf; 54 | *r = MUX(cf, lo - d, lo); 55 | return q; 56 | } 57 | -------------------------------------------------------------------------------- /libs/bearssl/i32_iszero.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i32_iszero(const uint32_t *x) 30 | { 31 | uint32_t z; 32 | size_t u; 33 | 34 | z = 0; 35 | for (u = (x[0] + 31) >> 5; u > 0; u --) { 36 | z |= x[u]; 37 | } 38 | return ~(z | -z) >> 31; 39 | } 40 | -------------------------------------------------------------------------------- /libs/bearssl/i32_mulacc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i32_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b) 30 | { 31 | size_t alen, blen, u; 32 | 33 | alen = (a[0] + 31) >> 5; 34 | blen = (b[0] + 31) >> 5; 35 | d[0] = a[0] + b[0]; 36 | for (u = 0; u < blen; u ++) { 37 | uint32_t f; 38 | size_t v; 39 | uint64_t cc; 40 | 41 | f = b[1 + u]; 42 | cc = 0; 43 | for (v = 0; v < alen; v ++) { 44 | uint64_t z; 45 | 46 | z = (uint64_t)d[1 + u + v] + MUL(f, a[1 + v]) + cc; 47 | cc = z >> 32; 48 | d[1 + u + v] = (uint32_t)z; 49 | } 50 | d[1 + u + alen] = (uint32_t)cc; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /libs/bearssl/i32_ninv32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i32_ninv32(uint32_t x) 30 | { 31 | uint32_t y; 32 | 33 | y = 2 - x; 34 | y *= 2 - y * x; 35 | y *= 2 - y * x; 36 | y *= 2 - y * x; 37 | y *= 2 - y * x; 38 | return MUX(x & 1, -y, 0); 39 | } 40 | -------------------------------------------------------------------------------- /libs/bearssl/i32_sub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | uint32_t 29 | br_i32_sub(uint32_t *a, const uint32_t *b, uint32_t ctl) 30 | { 31 | uint32_t cc; 32 | size_t u, m; 33 | 34 | cc = 0; 35 | m = (a[0] + 63) >> 5; 36 | for (u = 1; u < m; u ++) { 37 | uint32_t aw, bw, naw; 38 | 39 | aw = a[u]; 40 | bw = b[u]; 41 | naw = aw - bw - cc; 42 | 43 | /* 44 | * Carry is 1 if naw > aw. Carry is 1 also if naw == aw 45 | * AND the carry was already 1. 46 | */ 47 | cc = (cc & EQ(naw, aw)) | GT(naw, aw); 48 | a[u] = MUX(ctl, naw, aw); 49 | } 50 | return cc; 51 | } 52 | -------------------------------------------------------------------------------- /libs/bearssl/i32_tmont.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_i32_to_monty(uint32_t *x, const uint32_t *m) 30 | { 31 | uint32_t k; 32 | 33 | for (k = (m[0] + 31) >> 5; k > 0; k --) { 34 | br_i32_muladd_small(x, 0, m); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/mgf1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | void 29 | br_mgf1_xor(void *data, size_t len, 30 | const br_hash_class *dig, const void *seed, size_t seed_len) 31 | { 32 | unsigned char *buf; 33 | size_t u, hlen; 34 | uint32_t c; 35 | 36 | buf = data; 37 | hlen = br_digest_size(dig); 38 | for (u = 0, c = 0; u < len; u += hlen, c ++) { 39 | br_hash_compat_context hc; 40 | unsigned char tmp[64]; 41 | size_t v; 42 | 43 | hc.vtable = dig; 44 | dig->init(&hc.vtable); 45 | dig->update(&hc.vtable, seed, seed_len); 46 | br_enc32be(tmp, c); 47 | dig->update(&hc.vtable, tmp, 4); 48 | dig->out(&hc.vtable, tmp); 49 | for (v = 0; v < hlen; v ++) { 50 | if ((u + v) >= len) { 51 | break; 52 | } 53 | buf[u + v] ^= tmp[v]; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /libs/bearssl/prf_md5sha1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl.h */ 28 | void 29 | br_tls10_prf(void *dst, size_t len, 30 | const void *secret, size_t secret_len, const char *label, 31 | size_t seed_num, const br_tls_prf_seed_chunk *seed) 32 | { 33 | const unsigned char *s1; 34 | size_t slen; 35 | 36 | s1 = secret; 37 | slen = (secret_len + 1) >> 1; 38 | memset(dst, 0, len); 39 | br_tls_phash(dst, len, &br_md5_vtable, 40 | s1, slen, label, seed_num, seed); 41 | br_tls_phash(dst, len, &br_sha1_vtable, 42 | s1 + secret_len - slen, slen, label, seed_num, seed); 43 | } 44 | -------------------------------------------------------------------------------- /libs/bearssl/prf_sha256.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl.h */ 28 | void 29 | br_tls12_sha256_prf(void *dst, size_t len, 30 | const void *secret, size_t secret_len, const char *label, 31 | size_t seed_num, const br_tls_prf_seed_chunk *seed) 32 | { 33 | memset(dst, 0, len); 34 | br_tls_phash(dst, len, &br_sha256_vtable, 35 | secret, secret_len, label, seed_num, seed); 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/prf_sha384.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl.h */ 28 | void 29 | br_tls12_sha384_prf(void *dst, size_t len, 30 | const void *secret, size_t secret_len, const char *label, 31 | size_t seed_num, const br_tls_prf_seed_chunk *seed) 32 | { 33 | memset(dst, 0, len); 34 | br_tls_phash(dst, len, &br_sha384_vtable, 35 | secret, secret_len, label, seed_num, seed); 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_keygen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_keygen 29 | br_rsa_keygen_get_default(void) 30 | { 31 | #if BR_INT128 || BR_UMUL128 32 | return &br_rsa_i62_keygen; 33 | #elif BR_LOMUL 34 | return &br_rsa_i15_keygen; 35 | #else 36 | return &br_rsa_i31_keygen; 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_modulus.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_compute_modulus 29 | br_rsa_compute_modulus_get_default(void) 30 | { 31 | #if BR_LOMUL 32 | return &br_rsa_i15_compute_modulus; 33 | #else 34 | return &br_rsa_i31_compute_modulus; 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_oaep_decrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_oaep_decrypt 29 | br_rsa_oaep_decrypt_get_default(void) 30 | { 31 | #if BR_INT128 || BR_UMUL128 32 | return &br_rsa_i62_oaep_decrypt; 33 | #elif BR_LOMUL 34 | return &br_rsa_i15_oaep_decrypt; 35 | #else 36 | return &br_rsa_i31_oaep_decrypt; 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_oaep_encrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_oaep_encrypt 29 | br_rsa_oaep_encrypt_get_default(void) 30 | { 31 | #if BR_INT128 || BR_UMUL128 32 | return &br_rsa_i62_oaep_encrypt; 33 | #elif BR_LOMUL 34 | return &br_rsa_i15_oaep_encrypt; 35 | #else 36 | return &br_rsa_i31_oaep_encrypt; 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_pkcs1_sign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_pkcs1_sign 29 | br_rsa_pkcs1_sign_get_default(void) 30 | { 31 | #if BR_INT128 || BR_UMUL128 32 | return &br_rsa_i62_pkcs1_sign; 33 | #elif BR_LOMUL 34 | return &br_rsa_i15_pkcs1_sign; 35 | #else 36 | return &br_rsa_i31_pkcs1_sign; 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_pkcs1_vrfy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_pkcs1_vrfy 29 | br_rsa_pkcs1_vrfy_get_default(void) 30 | { 31 | #if BR_INT128 || BR_UMUL128 32 | return &br_rsa_i62_pkcs1_vrfy; 33 | #elif BR_LOMUL 34 | return &br_rsa_i15_pkcs1_vrfy; 35 | #else 36 | return &br_rsa_i31_pkcs1_vrfy; 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_priv.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_private 29 | br_rsa_private_get_default(void) 30 | { 31 | #if BR_INT128 || BR_UMUL128 32 | return &br_rsa_i62_private; 33 | #elif BR_LOMUL 34 | return &br_rsa_i15_private; 35 | #else 36 | return &br_rsa_i31_private; 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_privexp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_compute_privexp 29 | br_rsa_compute_privexp_get_default(void) 30 | { 31 | #if BR_LOMUL 32 | return &br_rsa_i15_compute_privexp; 33 | #else 34 | return &br_rsa_i31_compute_privexp; 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_pub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_public 29 | br_rsa_public_get_default(void) 30 | { 31 | #if BR_INT128 || BR_UMUL128 32 | return &br_rsa_i62_public; 33 | #elif BR_LOMUL 34 | return &br_rsa_i15_public; 35 | #else 36 | return &br_rsa_i31_public; 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_default_pubexp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | br_rsa_compute_pubexp 29 | br_rsa_compute_pubexp_get_default(void) 30 | { 31 | #if BR_LOMUL 32 | return &br_rsa_i15_compute_pubexp; 33 | #else 34 | return &br_rsa_i31_compute_pubexp; 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i15_oaep_decrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i15_oaep_decrypt(const br_hash_class *dig, 30 | const void *label, size_t label_len, 31 | const br_rsa_private_key *sk, void *data, size_t *len) 32 | { 33 | uint32_t r; 34 | 35 | if (*len != ((sk->n_bitlen + 7) >> 3)) { 36 | return 0; 37 | } 38 | r = br_rsa_i15_private(data, sk); 39 | r &= br_rsa_oaep_unpad(dig, label, label_len, data, len); 40 | return r; 41 | } 42 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i15_oaep_encrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | size_t 29 | br_rsa_i15_oaep_encrypt( 30 | const br_prng_class **rnd, const br_hash_class *dig, 31 | const void *label, size_t label_len, 32 | const br_rsa_public_key *pk, 33 | void *dst, size_t dst_max_len, 34 | const void *src, size_t src_len) 35 | { 36 | size_t dlen; 37 | 38 | dlen = br_rsa_oaep_pad(rnd, dig, label, label_len, 39 | pk, dst, dst_max_len, src, src_len); 40 | if (dlen == 0) { 41 | return 0; 42 | } 43 | return dlen & -(size_t)br_rsa_i15_public(dst, dlen, pk); 44 | } 45 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i15_pkcs1_sign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i15_pkcs1_sign(const unsigned char *hash_oid, 30 | const unsigned char *hash, size_t hash_len, 31 | const br_rsa_private_key *sk, unsigned char *x) 32 | { 33 | if (!br_rsa_pkcs1_sig_pad(hash_oid, hash, hash_len, sk->n_bitlen, x)) { 34 | return 0; 35 | } 36 | return br_rsa_i15_private(x, sk); 37 | } 38 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i15_pkcs1_vrfy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i15_pkcs1_vrfy(const unsigned char *x, size_t xlen, 30 | const unsigned char *hash_oid, size_t hash_len, 31 | const br_rsa_public_key *pk, unsigned char *hash_out) 32 | { 33 | unsigned char sig[BR_MAX_RSA_SIZE >> 3]; 34 | 35 | if (xlen > (sizeof sig)) { 36 | return 0; 37 | } 38 | memcpy(sig, x, xlen); 39 | if (!br_rsa_i15_public(sig, xlen, pk)) { 40 | return 0; 41 | } 42 | return br_rsa_pkcs1_sig_unpad(sig, xlen, hash_oid, hash_len, hash_out); 43 | } 44 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i31_keygen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i31_keygen(const br_prng_class **rng, 30 | br_rsa_private_key *sk, void *kbuf_priv, 31 | br_rsa_public_key *pk, void *kbuf_pub, 32 | unsigned size, uint32_t pubexp) 33 | { 34 | return br_rsa_i31_keygen_inner(rng, 35 | sk, kbuf_priv, pk, kbuf_pub, size, pubexp, 36 | &br_i31_modpow_opt); 37 | } 38 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i31_oaep_decrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i31_oaep_decrypt(const br_hash_class *dig, 30 | const void *label, size_t label_len, 31 | const br_rsa_private_key *sk, void *data, size_t *len) 32 | { 33 | uint32_t r; 34 | 35 | if (*len != ((sk->n_bitlen + 7) >> 3)) { 36 | return 0; 37 | } 38 | r = br_rsa_i31_private(data, sk); 39 | r &= br_rsa_oaep_unpad(dig, label, label_len, data, len); 40 | return r; 41 | } 42 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i31_oaep_encrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | size_t 29 | br_rsa_i31_oaep_encrypt( 30 | const br_prng_class **rnd, const br_hash_class *dig, 31 | const void *label, size_t label_len, 32 | const br_rsa_public_key *pk, 33 | void *dst, size_t dst_max_len, 34 | const void *src, size_t src_len) 35 | { 36 | size_t dlen; 37 | 38 | dlen = br_rsa_oaep_pad(rnd, dig, label, label_len, 39 | pk, dst, dst_max_len, src, src_len); 40 | if (dlen == 0) { 41 | return 0; 42 | } 43 | return dlen & -(size_t)br_rsa_i31_public(dst, dlen, pk); 44 | } 45 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i31_pkcs1_sign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i31_pkcs1_sign(const unsigned char *hash_oid, 30 | const unsigned char *hash, size_t hash_len, 31 | const br_rsa_private_key *sk, unsigned char *x) 32 | { 33 | if (!br_rsa_pkcs1_sig_pad(hash_oid, hash, hash_len, sk->n_bitlen, x)) { 34 | return 0; 35 | } 36 | return br_rsa_i31_private(x, sk); 37 | } 38 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i31_pkcs1_vrfy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i31_pkcs1_vrfy(const unsigned char *x, size_t xlen, 30 | const unsigned char *hash_oid, size_t hash_len, 31 | const br_rsa_public_key *pk, unsigned char *hash_out) 32 | { 33 | unsigned char sig[BR_MAX_RSA_SIZE >> 3]; 34 | 35 | if (xlen > (sizeof sig)) { 36 | return 0; 37 | } 38 | memcpy(sig, x, xlen); 39 | if (!br_rsa_i31_public(sig, xlen, pk)) { 40 | return 0; 41 | } 42 | return br_rsa_pkcs1_sig_unpad(sig, xlen, hash_oid, hash_len, hash_out); 43 | } 44 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i32_oaep_decrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i32_oaep_decrypt(const br_hash_class *dig, 30 | const void *label, size_t label_len, 31 | const br_rsa_private_key *sk, void *data, size_t *len) 32 | { 33 | uint32_t r; 34 | 35 | if (*len != ((sk->n_bitlen + 7) >> 3)) { 36 | return 0; 37 | } 38 | r = br_rsa_i32_private(data, sk); 39 | r &= br_rsa_oaep_unpad(dig, label, label_len, data, len); 40 | return r; 41 | } 42 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i32_oaep_encrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | size_t 29 | br_rsa_i32_oaep_encrypt( 30 | const br_prng_class **rnd, const br_hash_class *dig, 31 | const void *label, size_t label_len, 32 | const br_rsa_public_key *pk, 33 | void *dst, size_t dst_max_len, 34 | const void *src, size_t src_len) 35 | { 36 | size_t dlen; 37 | 38 | dlen = br_rsa_oaep_pad(rnd, dig, label, label_len, 39 | pk, dst, dst_max_len, src, src_len); 40 | if (dlen == 0) { 41 | return 0; 42 | } 43 | return dlen & -(size_t)br_rsa_i32_public(dst, dlen, pk); 44 | } 45 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i32_pkcs1_sign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i32_pkcs1_sign(const unsigned char *hash_oid, 30 | const unsigned char *hash, size_t hash_len, 31 | const br_rsa_private_key *sk, unsigned char *x) 32 | { 33 | if (!br_rsa_pkcs1_sig_pad(hash_oid, hash, hash_len, sk->n_bitlen, x)) { 34 | return 0; 35 | } 36 | return br_rsa_i32_private(x, sk); 37 | } 38 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i32_pkcs1_vrfy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_i32_pkcs1_vrfy(const unsigned char *x, size_t xlen, 30 | const unsigned char *hash_oid, size_t hash_len, 31 | const br_rsa_public_key *pk, unsigned char *hash_out) 32 | { 33 | unsigned char sig[BR_MAX_RSA_SIZE >> 3]; 34 | 35 | if (xlen > (sizeof sig)) { 36 | return 0; 37 | } 38 | memcpy(sig, x, xlen); 39 | if (!br_rsa_i32_public(sig, xlen, pk)) { 40 | return 0; 41 | } 42 | return br_rsa_pkcs1_sig_unpad(sig, xlen, hash_oid, hash_len, hash_out); 43 | } 44 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i62_keygen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | #if BR_INT128 || BR_UMUL128 28 | 29 | /* see bearssl_rsa.h */ 30 | uint32_t 31 | br_rsa_i62_keygen(const br_prng_class **rng, 32 | br_rsa_private_key *sk, void *kbuf_priv, 33 | br_rsa_public_key *pk, void *kbuf_pub, 34 | unsigned size, uint32_t pubexp) 35 | { 36 | return br_rsa_i31_keygen_inner(rng, 37 | sk, kbuf_priv, pk, kbuf_pub, size, pubexp, 38 | &br_i62_modpow_opt_as_i31); 39 | } 40 | 41 | /* see bearssl_rsa.h */ 42 | br_rsa_keygen 43 | br_rsa_i62_keygen_get() 44 | { 45 | return &br_rsa_i62_keygen; 46 | } 47 | 48 | #else 49 | 50 | /* see bearssl_rsa.h */ 51 | br_rsa_keygen 52 | br_rsa_i62_keygen_get() 53 | { 54 | return 0; 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i62_oaep_decrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | #if BR_INT128 || BR_UMUL128 28 | 29 | /* see bearssl_rsa.h */ 30 | uint32_t 31 | br_rsa_i62_oaep_decrypt(const br_hash_class *dig, 32 | const void *label, size_t label_len, 33 | const br_rsa_private_key *sk, void *data, size_t *len) 34 | { 35 | uint32_t r; 36 | 37 | if (*len != ((sk->n_bitlen + 7) >> 3)) { 38 | return 0; 39 | } 40 | r = br_rsa_i62_private(data, sk); 41 | r &= br_rsa_oaep_unpad(dig, label, label_len, data, len); 42 | return r; 43 | } 44 | 45 | /* see bearssl_rsa.h */ 46 | br_rsa_oaep_decrypt 47 | br_rsa_i62_oaep_decrypt_get(void) 48 | { 49 | return &br_rsa_i62_oaep_decrypt; 50 | } 51 | 52 | #else 53 | 54 | /* see bearssl_rsa.h */ 55 | br_rsa_oaep_decrypt 56 | br_rsa_i62_oaep_decrypt_get(void) 57 | { 58 | return 0; 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i62_pkcs1_sign.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | #if BR_INT128 || BR_UMUL128 28 | 29 | /* see bearssl_rsa.h */ 30 | uint32_t 31 | br_rsa_i62_pkcs1_sign(const unsigned char *hash_oid, 32 | const unsigned char *hash, size_t hash_len, 33 | const br_rsa_private_key *sk, unsigned char *x) 34 | { 35 | if (!br_rsa_pkcs1_sig_pad(hash_oid, hash, hash_len, sk->n_bitlen, x)) { 36 | return 0; 37 | } 38 | return br_rsa_i62_private(x, sk); 39 | } 40 | 41 | /* see bearssl_rsa.h */ 42 | br_rsa_pkcs1_sign 43 | br_rsa_i62_pkcs1_sign_get(void) 44 | { 45 | return &br_rsa_i62_pkcs1_sign; 46 | } 47 | 48 | #else 49 | 50 | /* see bearssl_rsa.h */ 51 | br_rsa_pkcs1_sign 52 | br_rsa_i62_pkcs1_sign_get(void) 53 | { 54 | return 0; 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_i62_pkcs1_vrfy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | #if BR_INT128 || BR_UMUL128 28 | 29 | /* see bearssl_rsa.h */ 30 | uint32_t 31 | br_rsa_i62_pkcs1_vrfy(const unsigned char *x, size_t xlen, 32 | const unsigned char *hash_oid, size_t hash_len, 33 | const br_rsa_public_key *pk, unsigned char *hash_out) 34 | { 35 | unsigned char sig[BR_MAX_RSA_SIZE >> 3]; 36 | 37 | if (xlen > (sizeof sig)) { 38 | return 0; 39 | } 40 | memcpy(sig, x, xlen); 41 | if (!br_rsa_i62_public(sig, xlen, pk)) { 42 | return 0; 43 | } 44 | return br_rsa_pkcs1_sig_unpad(sig, xlen, hash_oid, hash_len, hash_out); 45 | } 46 | 47 | /* see bearssl_rsa.h */ 48 | br_rsa_pkcs1_vrfy 49 | br_rsa_i62_pkcs1_vrfy_get(void) 50 | { 51 | return &br_rsa_i62_pkcs1_vrfy; 52 | } 53 | 54 | #else 55 | 56 | /* see bearssl_rsa.h */ 57 | br_rsa_pkcs1_vrfy 58 | br_rsa_i62_pkcs1_vrfy_get(void) 59 | { 60 | return 0; 61 | } 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /libs/bearssl/rsa_ssl_decrypt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_rsa.h */ 28 | uint32_t 29 | br_rsa_ssl_decrypt(br_rsa_private core, const br_rsa_private_key *sk, 30 | unsigned char *data, size_t len) 31 | { 32 | uint32_t x; 33 | size_t u; 34 | 35 | /* 36 | * A first check on length. Since this test works only on the 37 | * buffer length, it needs not (and cannot) be constant-time. 38 | */ 39 | if (len < 59 || len != (sk->n_bitlen + 7) >> 3) { 40 | return 0; 41 | } 42 | x = core(data, sk); 43 | 44 | x &= EQ(data[0], 0x00); 45 | x &= EQ(data[1], 0x02); 46 | for (u = 2; u < (len - 49); u ++) { 47 | x &= NEQ(data[u], 0); 48 | } 49 | x &= EQ(data[len - 49], 0x00); 50 | memmove(data, data + len - 48, 48); 51 | return x; 52 | } 53 | -------------------------------------------------------------------------------- /libs/bearssl/ssl_client_default_rsapub.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ssl.h */ 28 | void 29 | br_ssl_client_set_default_rsapub(br_ssl_client_context *cc) 30 | { 31 | br_ssl_client_set_rsapub(cc, br_rsa_public_get_default()); 32 | } 33 | -------------------------------------------------------------------------------- /libs/bearssl/ssl_engine_default_descbc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ssl.h */ 28 | void 29 | br_ssl_engine_set_default_des_cbc(br_ssl_engine_context *cc) 30 | { 31 | br_ssl_engine_set_cbc(cc, 32 | &br_sslrec_in_cbc_vtable, 33 | &br_sslrec_out_cbc_vtable); 34 | br_ssl_engine_set_des_cbc(cc, 35 | &br_des_ct_cbcenc_vtable, 36 | &br_des_ct_cbcdec_vtable); 37 | } 38 | -------------------------------------------------------------------------------- /libs/bearssl/ssl_engine_default_ec.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ssl.h */ 28 | void 29 | br_ssl_engine_set_default_ec(br_ssl_engine_context *cc) 30 | { 31 | #if BR_LOMUL 32 | br_ssl_engine_set_ec(cc, &br_ec_all_m15); 33 | #else 34 | br_ssl_engine_set_ec(cc, &br_ec_all_m31); 35 | #endif 36 | } 37 | -------------------------------------------------------------------------------- /libs/bearssl/ssl_engine_default_ecdsa.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ssl.h */ 28 | void 29 | br_ssl_engine_set_default_ecdsa(br_ssl_engine_context *cc) 30 | { 31 | #if BR_LOMUL 32 | br_ssl_engine_set_ec(cc, &br_ec_all_m15); 33 | br_ssl_engine_set_ecdsa(cc, &br_ecdsa_i15_vrfy_asn1); 34 | #else 35 | br_ssl_engine_set_ec(cc, &br_ec_all_m31); 36 | br_ssl_engine_set_ecdsa(cc, &br_ecdsa_i31_vrfy_asn1); 37 | #endif 38 | } 39 | -------------------------------------------------------------------------------- /libs/bearssl/ssl_engine_default_rsavrfy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ssl.h */ 28 | void 29 | br_ssl_engine_set_default_rsavrfy(br_ssl_engine_context *cc) 30 | { 31 | br_ssl_engine_set_rsavrfy(cc, br_rsa_pkcs1_vrfy_get_default()); 32 | } 33 | -------------------------------------------------------------------------------- /libs/bearssl/ssl_hashes.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see inner.h */ 28 | int 29 | br_ssl_choose_hash(unsigned bf) 30 | { 31 | static const unsigned char pref[] = { 32 | br_sha256_ID, br_sha384_ID, br_sha512_ID, 33 | br_sha224_ID, br_sha1_ID 34 | }; 35 | size_t u; 36 | 37 | for (u = 0; u < sizeof pref; u ++) { 38 | int x; 39 | 40 | x = pref[u]; 41 | if ((bf >> x) & 1) { 42 | return x; 43 | } 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /libs/bearssl/ssl_server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Thomas Pornin 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining 5 | * a copy of this software and associated documentation files (the 6 | * "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, 8 | * distribute, sublicense, and/or sell copies of the Software, and to 9 | * permit persons to whom the Software is furnished to do so, subject to 10 | * the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be 13 | * included in all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #include "inner.h" 26 | 27 | /* see bearssl_ssl.h */ 28 | void 29 | br_ssl_server_zero(br_ssl_server_context *cc) 30 | { 31 | /* 32 | * For really standard C, we should explicitly set to NULL all 33 | * pointers, and 0 all other fields. However, on all our target 34 | * architectures, a direct memset() will work, be faster, and 35 | * use a lot less code. 36 | */ 37 | memset(cc, 0, sizeof *cc); 38 | } 39 | 40 | /* see bearssl_ssl.h */ 41 | int 42 | br_ssl_server_reset(br_ssl_server_context *cc) 43 | { 44 | br_ssl_engine_set_buffer(&cc->eng, NULL, 0, 0); 45 | if (!br_ssl_engine_init_rand(&cc->eng)) { 46 | return 0; 47 | } 48 | cc->eng.reneg = 0; 49 | br_ssl_engine_hs_reset(&cc->eng, 50 | br_ssl_hs_server_init_main, br_ssl_hs_server_run); 51 | return br_ssl_engine_last_error(&cc->eng) == BR_ERR_OK; 52 | } 53 | -------------------------------------------------------------------------------- /obj/.dummy: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /pc/Makefile: -------------------------------------------------------------------------------- 1 | CC=g++ 2 | CFLAGS= -Wall -DLINUX 3 | PROGS= ccid 4 | 5 | all: ${PROGS} 6 | 7 | ccid: usbip.cpp ccid.cpp 8 | ${CC} ${CFLAGS} usbip.cpp -c 9 | ${CC} ${CFLAGS} usbip.o ccid.cpp -o ccid 10 | 11 | clean: 12 | rm -f ${PROGS} *.o *.d 13 | -------------------------------------------------------------------------------- /pc/ccid.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | 11 | #ifndef CCID_H_ 12 | #define CCID_H_ 13 | 14 | #include 15 | 16 | /* reg_callback.h */ 17 | typedef void (*ex_cb)(uint8_t*, size_t, uint8_t*, size_t*); 18 | 19 | extern int usbip_ccid_start(ex_cb cb); 20 | 21 | 22 | 23 | #endif /* CCID_H_ */ 24 | -------------------------------------------------------------------------------- /pytest/card_const.py: -------------------------------------------------------------------------------- 1 | FACTORY_PASSPHRASE_PW1=b"123456" 2 | FACTORY_PASSPHRASE_PW3=b"12345678" 3 | KEY_ATTRIBUTES_RSA4K=b"\x01\x10\x00\x00\x20\x00" 4 | KEY_ATTRIBUTES_RSA2K=b"\x01\x08\x00\x00\x20\x00" 5 | -------------------------------------------------------------------------------- /pytest/card_test_kdf_full.py: -------------------------------------------------------------------------------- 1 | """ 2 | card_test_kdf_full.py - test KDF data object 3 | 4 | Copyright (C) 2018, 2019 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | from card_const import * 24 | from constants_for_test import * 25 | 26 | class Test_Card_KDF_full(object): 27 | 28 | def test_verify_pw3(self, card): 29 | v = card.verify(3, FACTORY_PASSPHRASE_PW3) 30 | assert v 31 | 32 | def test_kdf_put_full(self, card): 33 | r = card.cmd_put_data(0x00, 0xf9, KDF_FULL) 34 | if r: 35 | card.configure_with_kdf() 36 | assert r 37 | -------------------------------------------------------------------------------- /pytest/card_test_kdf_single.py: -------------------------------------------------------------------------------- 1 | """ 2 | card_test_kdf_single.py - test KDF data object 3 | 4 | Copyright (C) 2018, 2019 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | from card_const import * 24 | from constants_for_test import * 25 | 26 | class Test_Card_KDF_Single(object): 27 | def test_verify_pw3(self, card): 28 | v = card.verify(3, FACTORY_PASSPHRASE_PW3) 29 | assert v 30 | 31 | def test_kdf_put_single(self, card): 32 | r = card.cmd_put_data(0x00, 0xf9, KDF_SINGLE) 33 | if r: 34 | card.configure_with_kdf() 35 | assert r 36 | -------------------------------------------------------------------------------- /pytest/card_test_remove_keys.py: -------------------------------------------------------------------------------- 1 | """ 2 | card_test_remove_keys.py - test removing keys on card 3 | 4 | Copyright (C) 2016, 2018, 2019 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | # Remove a key material on card by changing algorithm attributes of the key 24 | 25 | from card_const import * 26 | 27 | class Test_Remove_Keys(object): 28 | 29 | def test_rsa_keyattr_change_1(self, card): 30 | r = card.cmd_put_data(0x00, 0xc1, KEY_ATTRIBUTES_RSA4K) 31 | if r: 32 | r = card.cmd_put_data(0x00, 0xc1, KEY_ATTRIBUTES_RSA2K) 33 | assert r 34 | 35 | def test_rsa_keyattr_change_2(self, card): 36 | r = card.cmd_put_data(0x00, 0xc2, KEY_ATTRIBUTES_RSA4K) 37 | if r: 38 | r = card.cmd_put_data(0x00, 0xc2, KEY_ATTRIBUTES_RSA2K) 39 | assert r 40 | 41 | def test_rsa_keyattr_change_3(self, card): 42 | r = card.cmd_put_data(0x00, 0xc3, KEY_ATTRIBUTES_RSA4K) 43 | if r: 44 | r = card.cmd_put_data(0x00, 0xc3, KEY_ATTRIBUTES_RSA2K) 45 | assert r 46 | -------------------------------------------------------------------------------- /pytest/card_test_reset_pw3.py: -------------------------------------------------------------------------------- 1 | """ 2 | card_test_reset_pw3.py - test resetting pw3 3 | 4 | Copyright (C) 2018, 2019 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | from card_const import * 24 | import pytest 25 | 26 | class Test_Reset_PW3(object): 27 | # Gnuk specific feature of clear PW3 28 | def test_setup_pw3_null(self, card): 29 | if card.is_gnuk: 30 | r = card.change_passwd(3, FACTORY_PASSPHRASE_PW3, None) 31 | assert r 32 | else: 33 | pytest.skip("Gnuk only feature of clearing PW3") 34 | 35 | def test_verify_pw3(self, card): 36 | v = card.verify(3, FACTORY_PASSPHRASE_PW3) 37 | assert v 38 | 39 | # Check PW1 again to see the possiblity of admin-less mode 40 | def test_verify_pw1(self, card): 41 | v = card.verify(1, FACTORY_PASSPHRASE_PW1) 42 | assert v 43 | 44 | def test_verify_pw1_2(self, card): 45 | v = card.verify(2, FACTORY_PASSPHRASE_PW1) 46 | assert v 47 | -------------------------------------------------------------------------------- /pytest/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from card_reader import get_ccid_device 3 | from openpgp_card import OpenPGP_Card 4 | 5 | def pytest_addoption(parser): 6 | parser.addoption("--reader", dest="reader", type=str, action="store", 7 | default="gnuk", help="specify reader: gnuk or gemalto") 8 | 9 | @pytest.fixture(scope="session") 10 | def card(): 11 | print() 12 | print("Test start!") 13 | reader = get_ccid_device() 14 | print("Reader:", reader.get_string(1), reader.get_string(2)) 15 | card = OpenPGP_Card(reader) 16 | card.cmd_select_openpgp() 17 | card.is_gnuk = True 18 | yield card 19 | del card 20 | reader.ccid_power_off() 21 | -------------------------------------------------------------------------------- /pytest/gcrypt.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/solokeys/openpgp/5e5080f2b258b11921573dab87167d69cebef00d/pytest/gcrypt.dll -------------------------------------------------------------------------------- /pytest/readme.md: -------------------------------------------------------------------------------- 1 | # Tests 2 | 3 | Initially obtain from GNUK repository. 4 | 5 | [wiki](https://wiki.debian.org/GNUK) 6 | 7 | [repository](https://salsa.debian.org/gnuk-team/gnuk/gnuk) 8 | 9 | [repository with last commits](http://git.gniibe.org/gitweb/?p=gnuk/gnuk.git) 10 | 11 | 12 | tests lay [here](https://salsa.debian.org/gnuk-team/gnuk/gnuk/tree/master/tests) 13 | 14 | 15 | ## original readme 16 | 17 | Here is a test suite for OpenPGP card. 18 | 19 | For now, only TPDU card reader is supported for OpenPGP card. 20 | Gnuk Token is supported as well. 21 | 22 | 23 | You need to install: 24 | 25 | `$ sudo apt install python3-pytest python3-usb python3-cffi` 26 | 27 | and install crypto library 28 | 29 | `python -m pip install pycryptodome` 30 | 31 | Please run test by typing: 32 | 33 | `$ py.test-3 -x` 34 | 35 | Or with verbose 36 | 37 | `$ py.test-3 -x -vv` 38 | 39 | or 40 | 41 | `$ py.test-3 -x v` 42 | -------------------------------------------------------------------------------- /pytest/rsa-aut.key: -------------------------------------------------------------------------------- 1 | 9cf7192b51a574d1ad3ccb08ba09b87f228573893eee355529ff243e90fd4b86f79a82097cc7922c0485bed1616b1656a9b0b19ef78ea8ec34c384019adc5d5bf4db2d2a0a2d9cf14277bdcb7056f48b81214e3f7f7742231e29673966f9b1106862112cc798dba8d4a138bb5abfc6d4c12d53a5d39b2f783da916da20852ee139bbafda61d429caf2a4f30847ce7e7ae32ab4061e27dd9e4d00d60910249db8d8559dd85f7ca59659ef400c8f6318700f4e97f0c6f4165de80641490433c88da8682befe68eb311f54af2b07d97ac74edb5399cf054764211694fbb8d1d333f3269f235abe025067f811ff83a2224826219b309ea3e6c968f42b3e52f245dc9 2 | 010001 3 | b5ab7b159220b18e363258f61ebde08bae83d6ce2dbfe4adc143628c527887acde9de09bf9b49f438019004d71855f30c2d69b6c29bb9882ab641b3387409fe9199464a7faa4b5230c56d9e17cd9ed074bc00180ebed62bae3af28e6ff2ac2654ad968834c5d5c88f8d9d3cc5e167b10453b049d4e454a5761fb0ac717185907 4 | dd2fffa9814296156a6926cd17b65564187e424dcadce9b032246ad7e46448bb0f9e0ff3c64f987424b1a40bc694e2e9ac4fb1930d163582d7acf20653a1c44b97846c1c5fd8a7b19bb225fb39c30e25410483deaf8c2538d222b748c4d8103b11cec04f666a5c0dbcbf5d5f625f158f65746c3fafe6418145f7cffa5fadeeaf 5 | -------------------------------------------------------------------------------- /pytest/rsa-dec.key: -------------------------------------------------------------------------------- 1 | d392714c29738aac6372f2c8654a08c25a1299fed7004bd512cd2452b503ebad6301130816ac525ba528dc155be6347a5c70407fb4fbdaed751dfc0a7cd5e3910272ff236c4ed1ce5de6620b191a172e5b247347b8cab73a43d79221708755c959a2f83f486439da30917384554331532aabc8326db48866f8c91198834a86ab94679f6175db737bdf399e3f0b737dcb1f4208279d3e1cc694e78686785e4f363a377dec912b7c2f757b1422d866fb9fa85c96b83adfd6a223989a9a02988bdee81ad17eff6385e7b38cec8611fdf367ba4ac8e90d5f48ac7715c5f47aea06a4a37cdaa3029ce59d29bc66853bf6758ef4a7da5a5953f5e557a5a22f67c368c3 2 | 010001 3 | dae085952c5beee38f25f09bc37a4ca2434c31f78055469d0d5f0bf3337e3a70ba6c91734f195b742e211a5fe283befdf66820008e6ef2c8ca54a91922838fce07d9e33a331ce20dac36803e777d5ee2195ed28d6a4045e28623a6a60b0661e45f7c4f84ae2b1dfad0cf1ec30605158323382a819e730c09a33fad704dd67501 4 | f774be43ea198aa2f089274e4fffd7d0092ee7b35a1d2f854cdb166f698caab72fdeb099e690e78438b2e043e452d4d2f19d7f44ba6b286642f0ce5204966ff98ecd9e3b448877324631365dc860797429b9414a21a7e166d504cace156588b9a145657eeb1afb43b8ff65d8d6d93cea2ba4ef8aab047885c4de64ffef0b49c3 5 | -------------------------------------------------------------------------------- /pytest/rsa-sig.key: -------------------------------------------------------------------------------- 1 | c6c877dfd3b441f8fb1b8dc504093a51c2efe4883fe0a6379205acc6e673709905e4d767ddf46143c535cc6d7f10b616f520d8346320ef69ff4a2c4f4a148edc65f7ad24ed7d4fe23bb862a0ae71f4f7904abac0397abf3213df91326b1a25554b3b18cf54584d8bf220169fc92b2aa511e8313983e72b4c9110b3a1aea087aebef95873865608e8faea9ef10e7f7f3a66ca8def2d499c3149c127491e0e4339fd6abe10bfc6c13e43d522004f1485767328eabe35d6ffa8df4c15f0fbcd4eb1c07cc6d85e275139ac69e2962273ae987236926dd6c1144fce3e7ae567fa58ea60620dfafc52f95299fea601739fce27ee71eea978d0074f21e7086f60ba8331 2 | 010001 3 | cc365b5702714bf203e8c49b0b8afa8dad586e929cf5edca38ad07fa45efd5c2d89022d29f40283a57e50ca24c5f28c8e911a74faaf796f112e7e48195956f9a4df7668a5342523b27179cec958f363211ee11d0ec0e0e1b92ca007a61e8c9ac14e00229b9a7624850199e6667afa1a44db8f3c5de0a8eef0e6de050ac0ac633 4 | f931a3c12f0e3a5276f712b7706590ba02e14a97ff9b8ce3152af0fc4d9cdc690ea9bc4c82cb16c7d23136cbdab58fbec69880a88bca85c4214df01045082cbe9f4192e3e39c79896533c37dad9eb9e73c2643b9c0a704a4f93d81573537963d6b6e5140a24c702d9f26e06a2095de906daa8824172a6b39f563b7153907050b 5 | -------------------------------------------------------------------------------- /pytest/skip_gnuk_only_tests.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | @pytest.fixture(scope="module",autouse=True) 4 | def check_gnuk(card): 5 | if not card.is_gnuk: 6 | pytest.skip("Gnuk only feature", allow_module_level=True) 7 | -------------------------------------------------------------------------------- /pytest/test_000_empty_card.py: -------------------------------------------------------------------------------- 1 | from card_test_empty_card import * -------------------------------------------------------------------------------- /pytest/test_001_personalize_card.py: -------------------------------------------------------------------------------- 1 | from card_test_personalize_card import * 2 | -------------------------------------------------------------------------------- /pytest/test_002_personalize_reset.py: -------------------------------------------------------------------------------- 1 | from card_test_personalize_reset import * 2 | -------------------------------------------------------------------------------- /pytest/test_003_remove_keys.py: -------------------------------------------------------------------------------- 1 | from card_test_remove_keys import * 2 | -------------------------------------------------------------------------------- /pytest/test_004_reset_pw3.py: -------------------------------------------------------------------------------- 1 | from card_test_reset_pw3 import * 2 | -------------------------------------------------------------------------------- /pytest/test_005_personalize_admin_less.py: -------------------------------------------------------------------------------- 1 | """ 2 | test_005_personalize_admin_less.py - test admin-less mode 3 | 4 | Copyright (C) 2016, 2018, 2019 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | from skip_gnuk_only_tests import * 24 | 25 | from card_test_personalize_admin_less import * 26 | from card_test_personalize_reset import * 27 | from card_test_remove_keys import * 28 | from card_test_reset_pw3 import * 29 | -------------------------------------------------------------------------------- /pytest/test_009_keygen.py: -------------------------------------------------------------------------------- 1 | """ 2 | test_005_keygen.py - test key generation 3 | 4 | Copyright (C) 2018, 2019 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | from card_test_keygen import * 24 | from card_test_remove_keys import * 25 | -------------------------------------------------------------------------------- /pytest/test_011_kdf_full.py: -------------------------------------------------------------------------------- 1 | """ 2 | test_007_kdf_full.py - test KDF data object 3 | 4 | Copyright (C) 2018, 2019 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | from skip_gnuk_only_tests import * 24 | 25 | from card_test_kdf_full import * 26 | from card_test_personalize_card import * 27 | from card_test_personalize_reset import * 28 | from card_test_remove_keys import * 29 | from card_test_reset_pw3 import * 30 | -------------------------------------------------------------------------------- /pytest/test_016_kdf_single.py: -------------------------------------------------------------------------------- 1 | """ 2 | test_012_kdf_single.py - test KDF data object 3 | 4 | Copyright (C) 2018, 2019 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | from skip_gnuk_only_tests import * 24 | 25 | from card_test_kdf_single import * 26 | from card_test_personalize_card import * 27 | from card_test_personalize_reset import * 28 | from card_test_remove_keys import * 29 | from card_test_reset_pw3 import * 30 | -------------------------------------------------------------------------------- /pytest/test_021_personalize_admin_less.py: -------------------------------------------------------------------------------- 1 | from test_005_personalize_admin_less import * 2 | -------------------------------------------------------------------------------- /pytest/test_025_kdf_none.py: -------------------------------------------------------------------------------- 1 | """ 2 | test_017_kdf_none.py - test KDF data object 3 | 4 | Copyright (C) 2018 g10 Code GmbH 5 | Author: NIIBE Yutaka 6 | 7 | This file is a part of Gnuk, a GnuPG USB Token implementation. 8 | 9 | Gnuk is free software: you can redistribute it and/or modify it 10 | under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | Gnuk is distributed in the hope that it will be useful, but WITHOUT 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 17 | License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see . 21 | """ 22 | 23 | from skip_gnuk_only_tests import * 24 | 25 | from card_const import * 26 | from constants_for_test import * 27 | 28 | def test_verify_pw3(card): 29 | v = card.verify(3, FACTORY_PASSPHRASE_PW3) 30 | assert v 31 | 32 | def test_kdf_put_none(card): 33 | r = card.cmd_put_data(0x00, 0xf9, b"") 34 | if r: 35 | card.configure_with_kdf() 36 | assert r 37 | 38 | def test_verify_pw3_1(card): 39 | v = card.verify(3, FACTORY_PASSPHRASE_PW3) 40 | assert v 41 | -------------------------------------------------------------------------------- /pytest/test_030_empty_card.py: -------------------------------------------------------------------------------- 1 | from card_test_empty_card import * -------------------------------------------------------------------------------- /pytest/test_032_rsa4096.py: -------------------------------------------------------------------------------- 1 | """ 2 | test_032_rsa4096.py - test setting rsa 4096 3 | 4 | Copyright (C) 2019 SoloKeys 5 | Author: Oleg Moiseenko (merlokk) 6 | 7 | """ 8 | 9 | from skip_gnuk_only_tests import * 10 | 11 | from card_const import * 12 | from constants_for_test import * 13 | from openpgp_card import * 14 | 15 | 16 | def test_setup_rsa4096(card): 17 | assert card.verify(3, FACTORY_PASSPHRASE_PW3) 18 | 19 | assert card.set_rsa_algorithm_attributes( 20 | CryptoAlg.Signature.value, CryptoAlgType.RSA.value, 4096, 32, CryptoAlgImportFormat.RSAStandard.value) 21 | assert card.set_rsa_algorithm_attributes( 22 | CryptoAlg.Decryption.value, CryptoAlgType.RSA.value, 4096, 32, CryptoAlgImportFormat.RSAStandard.value) 23 | assert card.set_rsa_algorithm_attributes( 24 | CryptoAlg.Authentication.value, CryptoAlgType.RSA.value, 4096, 32, CryptoAlgImportFormat.RSAStandard.value) 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /pytest/test_033_rsa4096_keygen.py: -------------------------------------------------------------------------------- 1 | from card_test_keygen import * 2 | from card_test_remove_keys import * 3 | -------------------------------------------------------------------------------- /pytest/test_037_challenge.py: -------------------------------------------------------------------------------- 1 | """ 2 | test_037_challenge.py - test setting rsa 4096 3 | 4 | Copyright (C) 2020 SoloKeys 5 | Author: Oleg Moiseenko (merlokk) 6 | 7 | """ 8 | 9 | from skip_gnuk_only_tests import * 10 | 11 | from card_const import * 12 | from constants_for_test import * 13 | from openpgp_card import * 14 | 15 | 16 | def test_challenge(card): 17 | rndval = card.cmd_get_challenge(10) 18 | assert len(rndval) == 10 19 | assert rndval[0] != rndval[1] and rndval[1] != rndval[2] and \ 20 | rndval[3] != rndval[4] and rndval[4] != rndval[5] 21 | 22 | def test_challenge_long(card): 23 | rndval = card.cmd_get_challenge(255) 24 | assert len(rndval) == 255 25 | 26 | -------------------------------------------------------------------------------- /pytest/util.py: -------------------------------------------------------------------------------- 1 | from tlv import * 2 | from re import match, DOTALL 3 | 4 | 5 | def get_data_object(card, tag): 6 | tagh = tag >> 8 7 | tagl = tag & 0xff 8 | return card.cmd_get_data(tagh, tagl) 9 | 10 | 11 | def check_null(data_object): 12 | return data_object == None or len(data_object) == 0 13 | 14 | 15 | def check_zeroes(data_object): 16 | for c in data_object: 17 | if c != 0x00: 18 | return False 19 | return True 20 | 21 | 22 | def get_pk_info(pk): 23 | pktlv = TLV(pk) 24 | #pktlv.show() 25 | tag81 = pktlv.search(0x81) 26 | tag82 = pktlv.search(0x82) 27 | tag86 = pktlv.search(0x86) # format `04 || x || y` 28 | if tag86 is None: 29 | assert not (tag81 is None) 30 | assert not (tag82 is None) 31 | return tag81.data, tag82.data 32 | else: 33 | return tag86.data, None 34 | 35 | 36 | def create_ecdsa_signature(r, s): 37 | ktlv = TLV(b"\x30\x00") 38 | elm30 = ktlv.search(0x30) 39 | 40 | elm30.append(0x02, r) 41 | elm30.append(0x02, s) 42 | 43 | #ktlv.show() 44 | return ktlv.encode() 45 | 46 | 47 | def create_ecdsa_4D_key(KeyType, PrivateKey, PublicKey): 48 | ktlv = TLV(b"\x4d\x00") 49 | elm4d = ktlv.search(0x4d) 50 | 51 | elm4d.append(KeyType, b"") 52 | elm4d.append(0x7f48, encode_taglen(0x92, len(PrivateKey)) + encode_taglen(0x99, len(PublicKey))) 53 | elm4d.append(0x5f48, PrivateKey + PublicKey) 54 | #ktlv.show() 55 | return ktlv.encode() 56 | 57 | 58 | def ecdh_public_key_encode(PublicKey): 59 | ktlv = TLV(b"\xa6\x00") 60 | elm = ktlv.search(0xa6) 61 | 62 | elm.append(0x7f49, b"") 63 | elm = ktlv.search(0x7f49) 64 | elm.append(0x86, PublicKey) 65 | #ktlv.show() 66 | return ktlv.encode() 67 | 68 | 69 | def check_extended_capabilities(data): 70 | return match(b'[\x70\x74\x75\x7f]\x00\x00[\x20\x40\x80][\x00\x04\x08\x10]\x00[\x00\x01]\xff\x01\x00', data) 71 | 72 | 73 | def check_pw_status(data): 74 | return match(b'\x00...\x03[\x00\x03]\x03', data, DOTALL) 75 | -------------------------------------------------------------------------------- /src/apduexecutor.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #ifndef SRC_APDUEXECUTOR_H_ 11 | #define SRC_APDUEXECUTOR_H_ 12 | 13 | #include 14 | #include 15 | #include "opgputil.h" 16 | #include "errors.h" 17 | #include "applications/applicationstorage.h" 18 | #include "applications/apduconst.h" 19 | 20 | namespace Application { 21 | 22 | class APDUExecutor { 23 | private: 24 | void SetResultError(bstr &result, Util::Error error); 25 | public: 26 | APDUExecutor(); 27 | 28 | Util::Error Execute(bstr apdu, bstr &result); 29 | }; 30 | 31 | } /* namespace OpenPGP */ 32 | 33 | #endif /* SRC_APDUEXECUTOR_H_ */ 34 | -------------------------------------------------------------------------------- /src/applications/apducommand.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include "apducommand.h" 11 | #include "errors.h" 12 | 13 | namespace Application { 14 | 15 | APDUCommand::~APDUCommand() { 16 | } 17 | 18 | Util::Error APDUCommand::Check(uint8_t cla, uint8_t ins, uint8_t p1, 19 | uint8_t p2) { 20 | 21 | return Util::Error::WrongCommand; 22 | } 23 | 24 | Util::Error APDUCommand::Process(uint8_t cla, uint8_t ins, uint8_t p1, 25 | uint8_t p2, bstr data, uint8_t le, bstr &dataOut) { 26 | 27 | return Util::Error::WrongCommand; 28 | } 29 | 30 | std::string_view APDUCommand::GetName() { 31 | using namespace std::literals; 32 | return "base class"sv; 33 | } 34 | 35 | } // namespace Application 36 | -------------------------------------------------------------------------------- /src/applications/apducommand.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | 11 | #ifndef SRC_APDUCOMMAND_H_ 12 | #define SRC_APDUCOMMAND_H_ 13 | 14 | #include 15 | #include 16 | 17 | #include "opgputil.h" 18 | #include "errors.h" 19 | 20 | namespace Application { 21 | 22 | class APDUCommand { 23 | private: 24 | public: 25 | virtual ~APDUCommand(); 26 | 27 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 28 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 29 | virtual std::string_view GetName(); 30 | }; 31 | 32 | } 33 | 34 | #endif /* SRC_APDUCOMMAND_H_ */ 35 | -------------------------------------------------------------------------------- /src/applications/application.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include "application.h" 11 | 12 | namespace Application { 13 | 14 | Application::~Application() { 15 | } 16 | 17 | Util::Error Application::Init() { 18 | selected = false; 19 | 20 | return Util::Error::NoError; 21 | } 22 | 23 | Util::Error Application::Select(bstr &result) { 24 | result.clear(); 25 | selected = true; 26 | 27 | return Util::Error::NoError; 28 | } 29 | 30 | Util::Error Application::DeSelect() { 31 | selected = false; 32 | 33 | return Util::Error::NoError; 34 | } 35 | 36 | bool Application::Selected() { 37 | return selected; 38 | } 39 | 40 | const bstr* Application::GetAID() { 41 | return &aid; 42 | } 43 | 44 | Util::Error Application::APDUExchange(APDUStruct &apdu, bstr &result) { 45 | result.clear(); 46 | 47 | if (!selected) 48 | return Util::Error::ApplicationNotSelected; 49 | 50 | return Util::Error::NoError; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/applications/application.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #ifndef SRC_APPLICATION_H_ 11 | #define SRC_APPLICATION_H_ 12 | 13 | #include 14 | #include 15 | 16 | #include "opgputil.h" 17 | #include "errors.h" 18 | #include "filesystem.h" 19 | #include "apduconst.h" 20 | 21 | namespace Application { 22 | 23 | class Application { 24 | protected: 25 | bool selected = false; 26 | const bstr aid = "\x00"_bstr; 27 | 28 | // TODO: application config load/save 29 | 30 | public: 31 | virtual ~Application(); 32 | 33 | virtual Util::Error Init(); 34 | 35 | virtual Util::Error Select(bstr &result); 36 | virtual Util::Error DeSelect(); 37 | virtual bool Selected(); 38 | 39 | virtual const bstr *GetAID(); 40 | 41 | virtual Util::Error APDUExchange(APDUStruct &apdu, bstr &result); 42 | }; 43 | 44 | } // namespace Application 45 | 46 | #endif /* SRC_APPLICATION_H_ */ 47 | -------------------------------------------------------------------------------- /src/applications/applicationstorage.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include "applicationstorage.h" 11 | 12 | namespace Application { 13 | 14 | Util::Error ApplicationStorage::SelectApplication(bstr aid, bstr &result) { 15 | Application *sapp = nullptr; 16 | for(const auto& app: applications) { 17 | if (*app->GetAID() == aid) { 18 | sapp = app; 19 | break; 20 | } 21 | } 22 | 23 | if (sapp == nullptr) 24 | return Util::Error::ApplicationNotFound; 25 | 26 | for(const auto& app: applications) 27 | app->DeSelect(); 28 | 29 | auto err = sapp->Select(result); 30 | if (err != Util::Error::NoError && 31 | err != Util::Error::ApplicationTerminated) 32 | return err; 33 | 34 | selectedApplication = sapp; 35 | return err; 36 | } 37 | 38 | Application* ApplicationStorage::GetSelectedApplication() { 39 | for(const auto& app: applications) { 40 | if (app->Selected()) 41 | return app; 42 | } 43 | 44 | return nullptr; 45 | } 46 | 47 | OpenPGPApplication& ApplicationStorage::GetOpenPGPApplication() { 48 | return openPGPApplication; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/applications/applicationstorage.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #ifndef SRC_APPLICATIONSTORAGE_H_ 11 | #define SRC_APPLICATIONSTORAGE_H_ 12 | 13 | #include 14 | #include 15 | 16 | #include "application.h" 17 | #include "errors.h" 18 | #include "application.h" 19 | #include "openpgpapplication.h" 20 | #include "testapplication.h" 21 | 22 | namespace Application { 23 | 24 | class ApplicationStorage { 25 | private: 26 | OpenPGPApplication openPGPApplication; 27 | TestApplication testApplication; 28 | 29 | std::array applications = {&openPGPApplication, &testApplication}; 30 | 31 | Application *selectedApplication = nullptr; 32 | 33 | public: 34 | Util::Error SelectApplication(bstr aid, bstr &result); 35 | Application *GetSelectedApplication(); 36 | 37 | OpenPGPApplication &GetOpenPGPApplication(); 38 | }; 39 | 40 | } 41 | 42 | 43 | #endif /* SRC_APPLICATIONSTORAGE_H_ */ 44 | -------------------------------------------------------------------------------- /src/applications/openpgp/cryptoapdu.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | 11 | #ifndef SRC_CRYPTOAPDU_H_ 12 | #define SRC_CRYPTOAPDU_H_ 13 | 14 | #include 15 | #include "errors.h" 16 | #include "applications/apducommand.h" 17 | 18 | namespace OpenPGP { 19 | 20 | class APDUGetChallenge : public Application::APDUCommand { 21 | public: 22 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 23 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 24 | virtual std::string_view GetName(); 25 | }; 26 | 27 | class APDUInternalAuthenticate : public Application::APDUCommand { 28 | public: 29 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 30 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 31 | virtual std::string_view GetName(); 32 | }; 33 | 34 | class APDUGenerateAsymmetricKeyPair : public Application::APDUCommand { 35 | public: 36 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 37 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 38 | virtual std::string_view GetName(); 39 | }; 40 | 41 | // decipher, encipher, compute digital signature 42 | class APDUPSO : public Application::APDUCommand { 43 | public: 44 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 45 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 46 | virtual std::string_view GetName(); 47 | }; 48 | 49 | } 50 | 51 | #endif /* SRC_CRYPTOAPDU_H_ */ 52 | -------------------------------------------------------------------------------- /src/applications/openpgp/openpgpconst.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include "openpgpconst.h" 11 | 12 | namespace OpenPGP { 13 | 14 | const bstr PGPConst::DefaultPW1 = "123456"_bstr; 15 | const bstr PGPConst::DefaultPW3 = "12345678"_bstr; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/applications/openpgp/openpgpfactory.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include 11 | #include "solofactory.h" 12 | 13 | namespace OpenPGP { 14 | 15 | Application::APDUCommand* OpenPGPFactory::GetAPDUCommand(uint8_t cla, 16 | uint8_t ins, uint8_t p1, uint8_t p2) { 17 | 18 | for(const auto& cmd: commands) { 19 | if (cmd->Check(cla, ins, p1, p2) == Util::Error::NoError) { 20 | return cmd; 21 | } 22 | } 23 | 24 | return nullptr; 25 | } 26 | 27 | ResetProvider& OpenPGPFactory::GetResetProvider() { 28 | return resetProvider; 29 | } 30 | 31 | Security& OpenPGPFactory::GetSecurity() { 32 | return security; 33 | } 34 | 35 | } /* namespace OpenPGP */ 36 | -------------------------------------------------------------------------------- /src/applications/openpgp/resetprovider.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include "resetprovider.h" 11 | 12 | #include "opgpdevice.h" 13 | #include "solofactory.h" 14 | #include "opgputil.h" 15 | 16 | namespace OpenPGP { 17 | 18 | Util::Error ResetProvider::ResetCard() { 19 | Factory::SoloFactory &factory = Factory::SoloFactory::GetSoloFactory(); 20 | File::FileSystem &filesystem = factory.GetFileSystem(); 21 | 22 | return filesystem.DeleteFiles(File::AppID::OpenPGP); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/applications/openpgp/resetprovider.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | 11 | #ifndef SRC_RESETPROVIDER_H_ 12 | #define SRC_RESETPROVIDER_H_ 13 | 14 | #include "errors.h" 15 | 16 | namespace OpenPGP { 17 | 18 | class ResetProvider { 19 | public: 20 | Util::Error ResetCard(); 21 | }; 22 | 23 | } 24 | 25 | 26 | #endif /* SRC_RESETPROVIDER_H_ */ 27 | -------------------------------------------------------------------------------- /src/applications/openpgp/secureapdu.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | 11 | #ifndef SRC_SECUREAPDU_H_ 12 | #define SRC_SECUREAPDU_H_ 13 | 14 | #include 15 | #include "errors.h" 16 | #include "applications/apducommand.h" 17 | 18 | namespace OpenPGP { 19 | 20 | class APDUActivateFile : public Application::APDUCommand { 21 | public: 22 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 23 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 24 | virtual std::string_view GetName(); 25 | }; 26 | 27 | class APDUTerminateDF : public Application::APDUCommand { 28 | public: 29 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 30 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 31 | virtual std::string_view GetName(); 32 | }; 33 | 34 | class APDUManageSecurityEnvironment : public Application::APDUCommand { 35 | public: 36 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 37 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 38 | virtual std::string_view GetName(); 39 | }; 40 | 41 | 42 | class APDUSoloReboot : public Application::APDUCommand { 43 | public: 44 | virtual Util::Error Check(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2); 45 | virtual Util::Error Process(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2, bstr data, uint8_t le, bstr &dataOut); 46 | virtual std::string_view GetName(); 47 | }; 48 | } 49 | 50 | #endif /* SRC_SECUREAPDU_H_ */ 51 | -------------------------------------------------------------------------------- /src/applications/openpgpapplication.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #ifndef SRC_OPENPGPAPPLICATION_H_ 11 | #define SRC_OPENPGPAPPLICATION_H_ 12 | 13 | #include "application.h" 14 | #include "openpgp/openpgpfactory.h" 15 | #include "openpgp/openpgpconst.h" 16 | #include "openpgp/openpgpstruct.h" 17 | 18 | namespace Application { 19 | 20 | class OpenPGPApplication: public Application { 21 | // TODO: application state. INIT/WORK. save/load to file 22 | OpenPGP::ApplicationState state; 23 | OpenPGP::ApplicationConfig config; 24 | OpenPGP::PWStatusBytes pwstatus; 25 | 26 | private: 27 | // OpenPGP AID 28 | const bstr aid = "\xd2\x76\x00\x01\x24\x01"_bstr; 29 | public: 30 | OpenPGPApplication(); 31 | 32 | virtual const bstr *GetAID(); 33 | 34 | virtual Util::Error APDUExchange(APDUStruct &apdu, bstr &result); 35 | virtual Util::Error Select(bstr &result); 36 | }; 37 | 38 | } // namespace Application 39 | 40 | 41 | #endif /* SRC_OPENPGPAPPLICATION_H_ */ 42 | -------------------------------------------------------------------------------- /src/applications/testapplication.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include "testapplication.h" 11 | 12 | const bstr* Application::TestApplication::GetAID() { 13 | return &aid; 14 | } 15 | 16 | Util::Error Application::TestApplication::APDUExchange(APDUStruct &apdu, bstr &result) { 17 | result.set(apdu.data); 18 | return Util::Error::NoError; 19 | } 20 | -------------------------------------------------------------------------------- /src/applications/testapplication.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #ifndef SRC_TESTAPPLICATION_H_ 11 | #define SRC_TESTAPPLICATION_H_ 12 | 13 | #include "application.h" 14 | 15 | namespace Application { 16 | 17 | class TestApplication: public Application { 18 | private: 19 | const bstr aid = "\xfa\xfa\xfa\xfa"_bstr; 20 | public: 21 | virtual const bstr *GetAID(); 22 | 23 | virtual Util::Error APDUExchange(APDUStruct &apdu, bstr &result); 24 | }; 25 | 26 | } 27 | 28 | #endif /* SRC_TESTAPPLICATION_H_ */ 29 | -------------------------------------------------------------------------------- /src/opgpdevice.h: -------------------------------------------------------------------------------- 1 | #ifndef _OPGPDEVICE_H_ 2 | #define _OPGPDEVICE_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define OPGP_DEBUG 9 | 10 | template 11 | constexpr void printf_device(const char *fmt, Args ... args) { 12 | #ifdef OPGP_DEBUG 13 | printf(fmt, args...); 14 | #endif 15 | } 16 | constexpr void printf_device(const char *fmt) { 17 | #ifdef OPGP_DEBUG 18 | printf("%s", fmt); 19 | #endif 20 | } 21 | 22 | #ifndef PUT_TO_SRAM2 23 | #define PUT_TO_SRAM2 __attribute__((section(".sram2"))) 24 | #endif 25 | 26 | void ccid_init(); 27 | 28 | uint32_t ccid_recv(uint8_t * buf); 29 | 30 | void ccid_send(uint8_t * buf, uint32_t sz); 31 | 32 | int hwinit(); 33 | int hwreboot(); 34 | int hw_reset_fs_and_reboot(bool reboot); 35 | 36 | bool fileexist(char* name); 37 | int readfile(char* name, uint8_t * buf, size_t max_size, size_t *size); 38 | int writefile(char* name, uint8_t * buf, size_t size); 39 | int deletefile(char* name); 40 | int deletefiles(char* name); 41 | 42 | int gen_random_device_callback(void *parameters, uint8_t *data, size_t size); 43 | int gen_random_device(uint8_t * data, size_t size); 44 | 45 | void ecdsa_init(); 46 | bool ecdsa_keygen(uint8_t *sk, size_t *sklen, uint8_t *pk, size_t *pklen, int curve); 47 | size_t ecdsa_sign(uint8_t *sk, uint8_t *data, int len, uint8_t *sig, int curve); 48 | size_t ecdsa_calc_public_key(uint8_t *sk, uint8_t *pk, int curve); 49 | size_t ecdsa_ecdh_shared_secret(uint8_t *sk, uint8_t *pk, uint8_t *secret, int curve); 50 | 51 | bool aes_encode_cbc(uint8_t *key, size_t keylen, uint8_t *data, uint8_t *encdata, size_t datalen); 52 | bool aes_decode_cbc(uint8_t *key, size_t keylen, uint8_t *encdata, uint8_t *data, size_t datalen); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/opgputil.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2019 SoloKeys Developers 2 | // 3 | // Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 6 | // copied, modified, or distributed except according to those terms. 7 | 8 | #include "opgputil.h" 9 | #include "led.h" 10 | 11 | void stop_and_show_error() { 12 | led_rgb(0xFF0000U); 13 | while (true); 14 | } 15 | 16 | //void std::__throw_out_of_range_fmt(char const*, ...) {stop_and_show_error();}; 17 | -------------------------------------------------------------------------------- /src/solofactory.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include "solofactory.h" 11 | 12 | namespace Factory { 13 | 14 | using namespace Crypto; 15 | using namespace Application; 16 | using namespace OpenPGP; 17 | using namespace File; 18 | 19 | SoloFactory &SoloFactory::GetSoloFactory() { 20 | PUT_TO_SRAM2 static SoloFactory soloFactory; 21 | return soloFactory; 22 | } 23 | 24 | SoloFactory::SoloFactory() { 25 | } 26 | 27 | Util::Error SoloFactory::Init() { 28 | static PUT_TO_SRAM2 OpenPGPFactory sopenPGPFactory; 29 | static PUT_TO_SRAM2 ApplicationStorage sapplicationStorage; 30 | static PUT_TO_SRAM2 APDUExecutor sapduExecutor; 31 | static PUT_TO_SRAM2 CryptoEngine scryptoEngine; 32 | static PUT_TO_SRAM2 FileSystem sfileSystem; 33 | 34 | openPGPFactory = &sopenPGPFactory; 35 | applicationStorage = &sapplicationStorage; 36 | apduExecutor = &sapduExecutor; 37 | cryptoEngine = &scryptoEngine; 38 | fileSystem = &sfileSystem; 39 | 40 | return Util::NoError; 41 | } 42 | 43 | APDUExecutor& Factory::SoloFactory::GetAPDUExecutor() { 44 | return *apduExecutor; 45 | } 46 | 47 | ApplicationStorage& SoloFactory::GetApplicationStorage() { 48 | return *applicationStorage; 49 | } 50 | 51 | CryptoEngine& SoloFactory::GetCryptoEngine() { 52 | return *cryptoEngine; 53 | } 54 | 55 | CryptoLib& SoloFactory::GetCryptoLib() { 56 | return cryptoEngine->getCryptoLib(); 57 | } 58 | 59 | KeyStorage& Factory::SoloFactory::GetKeyStorage() { 60 | return cryptoEngine->getKeyStorage(); 61 | } 62 | 63 | OpenPGPFactory& SoloFactory::GetOpenPGPFactory() { 64 | return *openPGPFactory; 65 | } 66 | 67 | FileSystem& Factory::SoloFactory::GetFileSystem() { 68 | return *fileSystem; 69 | } 70 | 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/solofactory.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | 11 | #ifndef SRC_SOLOFACTORY_H_ 12 | #define SRC_SOLOFACTORY_H_ 13 | 14 | #include "cryptolib.h" 15 | #include "apduexecutor.h" 16 | #include "applications/applicationstorage.h" 17 | #include "applications/openpgp/openpgpfactory.h" 18 | #include "filesystem.h" 19 | 20 | namespace Factory { 21 | 22 | using namespace Crypto; 23 | using namespace Application; 24 | using namespace OpenPGP; 25 | using namespace File; 26 | 27 | class SoloFactory { 28 | private: 29 | OpenPGPFactory *openPGPFactory; 30 | ApplicationStorage *applicationStorage; 31 | APDUExecutor *apduExecutor; 32 | CryptoEngine *cryptoEngine; 33 | FileSystem *fileSystem; 34 | public: 35 | SoloFactory(); 36 | 37 | Util::Error Init(); 38 | 39 | APDUExecutor &GetAPDUExecutor(); 40 | 41 | ApplicationStorage &GetApplicationStorage(); 42 | 43 | CryptoEngine &GetCryptoEngine(); 44 | CryptoLib &GetCryptoLib(); 45 | KeyStorage &GetKeyStorage(); 46 | 47 | OpenPGPFactory &GetOpenPGPFactory(); 48 | FileSystem &GetFileSystem(); 49 | 50 | static SoloFactory &GetSoloFactory(); 51 | }; 52 | 53 | } 54 | 55 | #endif /* SRC_SOLOFACTORY_H_ */ 56 | -------------------------------------------------------------------------------- /src/tlv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include 11 | 12 | namespace Util { 13 | 14 | 15 | } /* namespace Util */ 16 | -------------------------------------------------------------------------------- /stm32l432/Makefile.lib: -------------------------------------------------------------------------------- 1 | CC = g++ 2 | RM = rm -rf 3 | 4 | 5 | rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2)) 6 | 7 | OBJ_DIR := ./obj 8 | SRC_DIRS := ./pc \ 9 | ./src \ 10 | ./src/applications \ 11 | ./src/applications/openpgp \ 12 | ./libs/stm32fs 13 | SRC_FILES := $(sort $(foreach var, $(SRC_DIRS), $(wildcard $(var)/*.cpp))) 14 | OBJ_FILES := $(patsubst %.cpp, $(OBJ_DIR)/%.o, $(notdir $(SRC_FILES))) 15 | DEP_FILES = $(OBJ_FILES:.o=.d) 16 | 17 | 18 | INC = -I. -Ipc/ -Isrc/ -Ilibs/mbedtls/ -Ilibs/mbedtls/mbedtls/crypto/include/\ 19 | -Ilibs/stm32fs/ 20 | 21 | CPPFLAGS = -std=c++17 -O0 -Wall -g3 $(INC) 22 | LDFLAGS = -Wl,-Bdynamic -lpthread 23 | 24 | LIBS=libs/mbedtls/mbedtls.a 25 | 26 | TARGET=main 27 | 28 | $(OBJ_DIR)/%.o: 29 | $(CC) $(CPPFLAGS) -c -o $@ $(filter %/$(strip $(patsubst %.o, %.cpp, $(notdir $@))), $(SRC_FILES)) 30 | 31 | all: $(OBJ_FILES) $(LIBS) 32 | $(CC) -o $(TARGET) $^ $(LDFLAGS) 33 | 34 | include libs/mbedtls/mbedtls.mk 35 | 36 | clean: 37 | $(RM) $(OBJ_FILES) $(DEP_FILES) $(TARGET) $(MBEDTLS_OBJ) $(MBEDTLS_A) 38 | 39 | -------------------------------------------------------------------------------- /stm32l432/openpgplib.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #include "openpgplib.h" 11 | #include "opgpdevice.h" 12 | #include "solofactory.h" 13 | #include "applications/apduconst.h" 14 | 15 | #include "device.h" 16 | 17 | // result buffer 18 | PUT_TO_SRAM2 static uint8_t apdu_result[4096] = {0}; 19 | 20 | bool DoReset = false; 21 | 22 | Application::APDUExecutor *fexecutor = nullptr; 23 | OpenPGP::Security *fsecurity = nullptr; 24 | void OpenpgpExchange(uint8_t *datain, size_t datainlen, uint8_t *dataout, uint32_t *outlen) { 25 | *outlen = 0; 26 | 27 | if (fexecutor == nullptr) 28 | return; 29 | 30 | auto resstr = bstr(apdu_result, 0, sizeof(apdu_result) - 10); 31 | auto apdu = bstr(datain, datainlen); 32 | 33 | printf_device("================\na>> "); dump_hex(apdu, 16); 34 | fexecutor->Execute(apdu, resstr); 35 | printf_device("a<< "); dump_hex(resstr, 16); 36 | 37 | *outlen = resstr.length(); 38 | memcpy(dataout, apdu_result, *outlen); 39 | 40 | // finish operation and then reset 41 | DoReset = fsecurity->DoReset; 42 | 43 | return; 44 | } 45 | 46 | void OpenpgpInit() { 47 | printf_device("-------- INIT --------\n"); 48 | 49 | hwinit(); 50 | printf_device("Init hardware: ok\n"); 51 | 52 | Factory::SoloFactory &factory = Factory::SoloFactory::GetSoloFactory(); 53 | factory.Init(); 54 | 55 | static Application::APDUExecutor &executor = factory.GetAPDUExecutor(); 56 | fexecutor = &executor; 57 | 58 | OpenPGP::OpenPGPFactory &opgp_factory = factory.GetOpenPGPFactory(); 59 | static OpenPGP::Security &security = opgp_factory.GetSecurity(); 60 | fsecurity = &security; 61 | printf_device("OpenPGP init: ok.\n"); 62 | 63 | return; 64 | } 65 | -------------------------------------------------------------------------------- /stm32l432/openpgplib.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2019 SoloKeys Developers 3 | 4 | Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be 7 | copied, modified, or distributed except according to those terms. 8 | */ 9 | 10 | #ifndef _OPENPGPLIB_H_ 11 | #define _OPENPGPLIB_H_ 12 | 13 | #include "stdint.h" 14 | #include "stddef.h" 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | extern bool DoReset; 20 | 21 | void OpenpgpInit(); 22 | void OpenpgpExchange(uint8_t *datain, size_t datainlen, uint8_t *dataout, uint32_t *outlen); 23 | 24 | #ifdef __cplusplus 25 | } 26 | #endif 27 | 28 | #endif 29 | --------------------------------------------------------------------------------