Feature/mbedtls (#84)
* try to import mbedtls and build it * add stubs socket class * some boilterplate, read and write function implemented * more boilterplate / current error in handshake because no CA cert is setup * add something so skip ca verification, can ws curl https://google.com ! * cleanup / close implemented * tweak CMakefiles * typo in include * update readme * disable unittests
This commit is contained in:
committed by
GitHub
parent
977e8794ec
commit
57976cf613
47
third_party/mbedtls/crypto/programs/.gitignore
vendored
Normal file
47
third_party/mbedtls/crypto/programs/.gitignore
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
*/Makefile
|
||||
*.sln
|
||||
*.vcxproj
|
||||
|
||||
aes/aescrypt2
|
||||
aes/crypt_and_hash
|
||||
hash/generic_sum
|
||||
hash/hello
|
||||
hash/md5sum
|
||||
hash/sha1sum
|
||||
hash/sha2sum
|
||||
pkey/dh_genprime
|
||||
pkey/ecdsa
|
||||
pkey/ecdh_curve25519
|
||||
pkey/gen_key
|
||||
pkey/key_app
|
||||
pkey/key_app_writer
|
||||
pkey/mpi_demo
|
||||
pkey/pk_decrypt
|
||||
pkey/pk_encrypt
|
||||
pkey/pk_sign
|
||||
pkey/pk_verify
|
||||
pkey/rsa_decrypt
|
||||
pkey/rsa_encrypt
|
||||
pkey/rsa_genkey
|
||||
pkey/rsa_sign
|
||||
pkey/rsa_sign_pss
|
||||
pkey/rsa_verify
|
||||
pkey/rsa_verify_pss
|
||||
psa/crypto_examples
|
||||
psa/psa_constant_names
|
||||
psa/psa_constant_names_generated.c
|
||||
psa/key_ladder_demo
|
||||
random/gen_entropy
|
||||
random/gen_random_ctr_drbg
|
||||
random/gen_random_havege
|
||||
test/benchmark
|
||||
test/ecp-bench
|
||||
test/selftest
|
||||
test/cpp_dummy_build
|
||||
test/zeroize
|
||||
test/query_compile_time_config
|
||||
util/pem2der
|
||||
util/strerror
|
||||
|
||||
# generated files
|
||||
pkey/keyfile.key
|
7
third_party/mbedtls/crypto/programs/CMakeLists.txt
vendored
Normal file
7
third_party/mbedtls/crypto/programs/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
add_subdirectory(aes)
|
||||
add_subdirectory(hash)
|
||||
add_subdirectory(pkey)
|
||||
add_subdirectory(psa)
|
||||
add_subdirectory(random)
|
||||
add_subdirectory(test)
|
||||
add_subdirectory(util)
|
254
third_party/mbedtls/crypto/programs/Makefile
vendored
Normal file
254
third_party/mbedtls/crypto/programs/Makefile
vendored
Normal file
@ -0,0 +1,254 @@
|
||||
|
||||
# To compile on SunOS: add "-lsocket -lnsl" to LDFLAGS
|
||||
|
||||
CFLAGS ?= -O2
|
||||
WARNING_CFLAGS ?= -Wall -W -Wdeclaration-after-statement
|
||||
WARNING_CXXFLAGS ?= -Wall -W
|
||||
LDFLAGS ?=
|
||||
|
||||
LOCAL_CFLAGS = $(WARNING_CFLAGS) -I../include -D_FILE_OFFSET_BITS=64
|
||||
LOCAL_CXXFLAGS = $(WARNING_CXXFLAGS) -I../include -D_FILE_OFFSET_BITS=64
|
||||
LOCAL_LDFLAGS = -L../library \
|
||||
-lmbedcrypto$(SHARED_SUFFIX)
|
||||
|
||||
ifndef SHARED
|
||||
DEP=../library/libmbedcrypto.a
|
||||
else
|
||||
DEP=../library/libmbedcrypto.$(DLEXT)
|
||||
endif
|
||||
|
||||
ifdef DEBUG
|
||||
LOCAL_CFLAGS += -g3
|
||||
endif
|
||||
|
||||
# if we're running on Windows, build for Windows
|
||||
ifdef WINDOWS
|
||||
WINDOWS_BUILD=1
|
||||
endif
|
||||
|
||||
ifdef WINDOWS_BUILD
|
||||
DLEXT=dll
|
||||
EXEXT=.exe
|
||||
LOCAL_LDFLAGS += -lws2_32
|
||||
ifdef SHARED
|
||||
SHARED_SUFFIX=.$(DLEXT)
|
||||
endif
|
||||
else
|
||||
DLEXT ?= so
|
||||
EXEXT=
|
||||
SHARED_SUFFIX=
|
||||
endif
|
||||
|
||||
APPS = \
|
||||
aes/aescrypt2$(EXEXT) \
|
||||
aes/crypt_and_hash$(EXEXT) \
|
||||
hash/hello$(EXEXT) \
|
||||
hash/generic_sum$(EXEXT) \
|
||||
pkey/dh_genprime$(EXEXT) \
|
||||
pkey/ecdh_curve25519$(EXEXT) \
|
||||
pkey/ecdsa$(EXEXT) \
|
||||
pkey/gen_key$(EXEXT) \
|
||||
pkey/key_app$(EXEXT) \
|
||||
pkey/key_app_writer$(EXEXT) \
|
||||
pkey/mpi_demo$(EXEXT) \
|
||||
pkey/pk_decrypt$(EXEXT) \
|
||||
pkey/pk_encrypt$(EXEXT) \
|
||||
pkey/pk_sign$(EXEXT) \
|
||||
pkey/pk_verify$(EXEXT) \
|
||||
pkey/rsa_genkey$(EXEXT) \
|
||||
pkey/rsa_decrypt$(EXEXT) \
|
||||
pkey/rsa_encrypt$(EXEXT) \
|
||||
pkey/rsa_sign$(EXEXT) \
|
||||
pkey/rsa_verify$(EXEXT) \
|
||||
pkey/rsa_sign_pss$(EXEXT) \
|
||||
pkey/rsa_verify_pss$(EXEXT) \
|
||||
psa/crypto_examples$(EXEXT) \
|
||||
psa/key_ladder_demo$(EXEXT) \
|
||||
psa/psa_constant_names$(EXEXT) \
|
||||
random/gen_entropy$(EXEXT) \
|
||||
random/gen_random_havege$(EXEXT) \
|
||||
random/gen_random_ctr_drbg$(EXEXT) \
|
||||
test/benchmark$(EXEXT) \
|
||||
test/selftest$(EXEXT) \
|
||||
test/zeroize$(EXEXT) \
|
||||
test/query_compile_time_config$(EXEXT) \
|
||||
util/pem2der$(EXEXT) \
|
||||
util/strerror$(EXEXT) \
|
||||
# End of APPS
|
||||
|
||||
ifdef TEST_CPP
|
||||
APPS += test/cpp_dummy_build$(EXEXT)
|
||||
endif
|
||||
|
||||
EXTRA_GENERATED =
|
||||
|
||||
.SILENT:
|
||||
|
||||
.PHONY: all clean list
|
||||
|
||||
all: $(APPS)
|
||||
|
||||
$(DEP):
|
||||
$(MAKE) -C ../library
|
||||
|
||||
ifdef WINDOWS
|
||||
EXTRA_GENERATED += psa\psa_constant_names_generated.c
|
||||
else
|
||||
EXTRA_GENERATED += psa/psa_constant_names_generated.c
|
||||
endif
|
||||
|
||||
psa/psa_constant_names$(EXEXT): psa/psa_constant_names_generated.c
|
||||
psa/psa_constant_names_generated.c: ../scripts/generate_psa_constants.py ../include/psa/crypto_values.h ../include/psa/crypto_extra.h
|
||||
../scripts/generate_psa_constants.py
|
||||
|
||||
aes/aescrypt2$(EXEXT): aes/aescrypt2.c $(DEP)
|
||||
echo " CC aes/aescrypt2.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) aes/aescrypt2.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
aes/crypt_and_hash$(EXEXT): aes/crypt_and_hash.c $(DEP)
|
||||
echo " CC aes/crypt_and_hash.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) aes/crypt_and_hash.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
hash/hello$(EXEXT): hash/hello.c $(DEP)
|
||||
echo " CC hash/hello.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) hash/hello.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
hash/generic_sum$(EXEXT): hash/generic_sum.c $(DEP)
|
||||
echo " CC hash/generic_sum.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) hash/generic_sum.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/dh_genprime$(EXEXT): pkey/dh_genprime.c $(DEP)
|
||||
echo " CC pkey/dh_genprime.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/dh_genprime.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/ecdh_curve25519$(EXEXT): pkey/ecdh_curve25519.c $(DEP)
|
||||
echo " CC pkey/ecdh_curve25519.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/ecdh_curve25519.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/ecdsa$(EXEXT): pkey/ecdsa.c $(DEP)
|
||||
echo " CC pkey/ecdsa.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/ecdsa.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/gen_key$(EXEXT): pkey/gen_key.c $(DEP)
|
||||
echo " CC pkey/gen_key.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/gen_key.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/key_app$(EXEXT): pkey/key_app.c $(DEP)
|
||||
echo " CC pkey/key_app.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/key_app.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/key_app_writer$(EXEXT): pkey/key_app_writer.c $(DEP)
|
||||
echo " CC pkey/key_app_writer.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/key_app_writer.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/mpi_demo$(EXEXT): pkey/mpi_demo.c $(DEP)
|
||||
echo " CC pkey/mpi_demo.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/mpi_demo.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/pk_decrypt$(EXEXT): pkey/pk_decrypt.c $(DEP)
|
||||
echo " CC pkey/pk_decrypt.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/pk_decrypt.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/pk_encrypt$(EXEXT): pkey/pk_encrypt.c $(DEP)
|
||||
echo " CC pkey/pk_encrypt.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/pk_encrypt.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/pk_sign$(EXEXT): pkey/pk_sign.c $(DEP)
|
||||
echo " CC pkey/pk_sign.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/pk_sign.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/pk_verify$(EXEXT): pkey/pk_verify.c $(DEP)
|
||||
echo " CC pkey/pk_verify.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/pk_verify.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_genkey$(EXEXT): pkey/rsa_genkey.c $(DEP)
|
||||
echo " CC pkey/rsa_genkey.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/rsa_genkey.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_sign$(EXEXT): pkey/rsa_sign.c $(DEP)
|
||||
echo " CC pkey/rsa_sign.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/rsa_sign.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_verify$(EXEXT): pkey/rsa_verify.c $(DEP)
|
||||
echo " CC pkey/rsa_verify.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/rsa_verify.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_sign_pss$(EXEXT): pkey/rsa_sign_pss.c $(DEP)
|
||||
echo " CC pkey/rsa_sign_pss.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/rsa_sign_pss.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_verify_pss$(EXEXT): pkey/rsa_verify_pss.c $(DEP)
|
||||
echo " CC pkey/rsa_verify_pss.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/rsa_verify_pss.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_decrypt$(EXEXT): pkey/rsa_decrypt.c $(DEP)
|
||||
echo " CC pkey/rsa_decrypt.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/rsa_decrypt.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
pkey/rsa_encrypt$(EXEXT): pkey/rsa_encrypt.c $(DEP)
|
||||
echo " CC pkey/rsa_encrypt.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/rsa_encrypt.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
psa/key_ladder_demo$(EXEXT): psa/key_ladder_demo.c $(DEP)
|
||||
echo " CC psa/key_ladder_demo.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/key_ladder_demo.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
psa/psa_constant_names$(EXEXT): psa/psa_constant_names.c $(DEP)
|
||||
echo " CC psa/psa_constant_names.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/psa_constant_names.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
random/gen_entropy$(EXEXT): random/gen_entropy.c $(DEP)
|
||||
echo " CC random/gen_entropy.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) random/gen_entropy.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
random/gen_random_havege$(EXEXT): random/gen_random_havege.c $(DEP)
|
||||
echo " CC random/gen_random_havege.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) random/gen_random_havege.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
random/gen_random_ctr_drbg$(EXEXT): random/gen_random_ctr_drbg.c $(DEP)
|
||||
echo " CC random/gen_random_ctr_drbg.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) random/gen_random_ctr_drbg.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
test/benchmark$(EXEXT): test/benchmark.c $(DEP)
|
||||
echo " CC test/benchmark.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/benchmark.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
test/cpp_dummy_build$(EXEXT): test/cpp_dummy_build.cpp $(DEP)
|
||||
echo " CXX test/cpp_dummy_build.cpp"
|
||||
$(CXX) $(LOCAL_CXXFLAGS) $(CXXFLAGS) test/cpp_dummy_build.cpp $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
test/selftest$(EXEXT): test/selftest.c $(DEP)
|
||||
echo " CC test/selftest.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/selftest.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
test/zeroize$(EXEXT): test/zeroize.c $(DEP)
|
||||
echo " CC test/zeroize.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/zeroize.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
test/query_compile_time_config$(EXEXT): test/query_compile_time_config.c test/query_config.c $(DEP)
|
||||
echo " CC test/query_compile_time_config.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/query_compile_time_config.c test/query_config.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
util/pem2der$(EXEXT): util/pem2der.c $(DEP)
|
||||
echo " CC util/pem2der.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) util/pem2der.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
util/strerror$(EXEXT): util/strerror.c $(DEP)
|
||||
echo " CC util/strerror.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) util/strerror.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
psa/crypto_examples$(EXEXT): psa/crypto_examples.c $(DEP)
|
||||
echo " CC psa/crypto_examples.c"
|
||||
$(CC) $(LOCAL_CFLAGS) $(CFLAGS) psa/crypto_examples.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@
|
||||
|
||||
clean:
|
||||
ifndef WINDOWS
|
||||
rm -f $(APPS) $(EXTRA_GENERATED)
|
||||
else
|
||||
if exist *.o del /S /Q /F *.o
|
||||
if exist *.exe del /S /Q /F *.exe
|
||||
if exist $(EXTRA_GENERATED) del /S /Q /F $(EXTRA_GENERATED)
|
||||
endif
|
||||
|
||||
list:
|
||||
echo $(APPS)
|
78
third_party/mbedtls/crypto/programs/README.md
vendored
Normal file
78
third_party/mbedtls/crypto/programs/README.md
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
Mbed TLS sample programs
|
||||
========================
|
||||
|
||||
This subdirectory mostly contains sample programs that illustrate specific features of the library, as well as a few test and support programs.
|
||||
|
||||
## Symmetric cryptography (AES) examples
|
||||
|
||||
* [`aes/aescrypt2.c`](aes/aescrypt2.c): file encryption and authentication with a key derived from a low-entropy secret, demonstrating the low-level AES interface, the digest interface and HMAC.
|
||||
Warning: this program illustrates how to use low-level functions in the library. It should not be taken as an example of how to build a secure encryption mechanism. To derive a key from a low-entropy secret such as a password, use a standard key stretching mechanism such as PBKDF2 (provided by the `pkcs5` module). To encrypt and authenticate data, use a standard mode such as GCM or CCM (both available as library module).
|
||||
|
||||
* [`aes/crypt_and_hash.c`](aes/crypt_and_hash.c): file encryption and authentication, demonstrating the generic cipher interface and the generic hash interface.
|
||||
|
||||
## Hash (digest) examples
|
||||
|
||||
* [`hash/generic_sum.c`](hash/generic_sum.c): file hash calculator and verifier, demonstrating the message digest (`md`) interface.
|
||||
|
||||
* [`hash/hello.c`](hash/hello.c): hello-world program for MD5.
|
||||
|
||||
## Public-key cryptography examples
|
||||
|
||||
### Generic public-key cryptography (`pk`) examples
|
||||
|
||||
* [`pkey/gen_key.c`](pkey/gen_key.c): generates a key for any of the supported public-key algorithms (RSA or ECC) and writes it to a file that can be used by the other pk sample programs.
|
||||
|
||||
* [`pkey/key_app.c`](pkey/key_app.c): loads a PEM or DER public key or private key file and dumps its content.
|
||||
|
||||
* [`pkey/key_app_writer.c`](pkey/key_app_writer.c): loads a PEM or DER public key or private key file and writes it to a new PEM or DER file.
|
||||
|
||||
* [`pkey/pk_encrypt.c`](pkey/pk_encrypt.c), [`pkey/pk_decrypt.c`](pkey/pk_decrypt.c): loads a PEM or DER public/private key file and uses the key to encrypt/decrypt a short string through the generic public-key interface.
|
||||
|
||||
* [`pkey/pk_sign.c`](pkey/pk_sign.c), [`pkey/pk_verify.c`](pkey/pk_verify.c): loads a PEM or DER private/public key file and uses the key to sign/verify a short string.
|
||||
|
||||
### ECDSA and RSA signature examples
|
||||
|
||||
* [`pkey/ecdsa.c`](pkey/ecdsa.c): generates an ECDSA key, signs a fixed message and verifies the signature.
|
||||
|
||||
* [`pkey/rsa_encrypt.c`](pkey/rsa_encrypt.c), [`pkey/rsa_decrypt.c`](pkey/rsa_decrypt.c): loads an RSA public/private key and uses it to encrypt/decrypt a short string through the low-level RSA interface.
|
||||
|
||||
* [`pkey/rsa_genkey.c`](pkey/rsa_genkey.c): generates an RSA key and writes it to a file that can be used with the other RSA sample programs.
|
||||
|
||||
* [`pkey/rsa_sign.c`](pkey/rsa_sign.c), [`pkey/rsa_verify.c`](pkey/rsa_verify.c): loads an RSA private/public key and uses it to sign/verify a short string with the RSA PKCS#1 v1.5 algorithm.
|
||||
|
||||
* [`pkey/rsa_sign_pss.c`](pkey/rsa_sign_pss.c), [`pkey/rsa_verify_pss.c`](pkey/rsa_verify_pss.c): loads an RSA private/public key and uses it to sign/verify a short string with the RSASSA-PSS algorithm.
|
||||
|
||||
### Diffie-Hellman key exchange examples
|
||||
|
||||
* [`pkey/ecdh_curve25519.c`](pkey/ecdh_curve25519.c): demonstration of a elliptic curve Diffie-Hellman (ECDH) key agreement.
|
||||
|
||||
### Bignum (`mpi`) usage examples
|
||||
|
||||
* [`pkey/dh_genprime.c`](pkey/dh_genprime.c): shows how to use the bignum (`mpi`) interface to generate Diffie-Hellman parameters.
|
||||
|
||||
* [`pkey/mpi_demo.c`](pkey/mpi_demo.c): demonstrates operations on big integers.
|
||||
|
||||
## Random number generator (RNG) examples
|
||||
|
||||
* [`random/gen_entropy.c`](random/gen_entropy.c): shows how to use the default entropy sources to generate random data.
|
||||
Note: most applications should only use the entropy generator to seed a cryptographic pseudorandom generator, as illustrated by `random/gen_random_ctr_drbg.c`.
|
||||
|
||||
* [`random/gen_random_ctr_drbg.c`](random/gen_random_ctr_drbg.c): shows how to use the default entropy sources to seed a pseudorandom generator, and how to use the resulting random generator to generate random data.
|
||||
|
||||
* [`random/gen_random_havege.c`](random/gen_random_havege.c): demonstrates the HAVEGE entropy collector.
|
||||
|
||||
## Test utilities
|
||||
|
||||
* [`test/benchmark.c`](test/benchmark.c): benchmark for cryptographic algorithms.
|
||||
|
||||
* [`test/selftest.c`](test/selftest.c): runs the self-test function in each library module.
|
||||
|
||||
* [`test/udp_proxy.c`](test/udp_proxy.c): a UDP proxy that can inject certain failures (delay, duplicate, drop). Useful for testing DTLS.
|
||||
|
||||
* [`test/zeroize.c`](test/zeroize.c): a test program for `mbedtls_platform_zeroize`, used by [`tests/scripts/test_zeroize.gdb`](tests/scripts/test_zeroize.gdb).
|
||||
|
||||
## Development utilities
|
||||
|
||||
* [`util/pem2der.c`](util/pem2der.c): a PEM to DER converter. Mbed TLS can read PEM files directly, but this utility can be useful for interacting with other tools or with minimal Mbed TLS builds that lack PEM support.
|
||||
|
||||
* [`util/strerror.c`](util/strerror.c): prints the error description corresponding to an integer status returned by an Mbed TLS function.
|
9
third_party/mbedtls/crypto/programs/aes/CMakeLists.txt
vendored
Normal file
9
third_party/mbedtls/crypto/programs/aes/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
add_executable(aescrypt2 aescrypt2.c)
|
||||
target_link_libraries(aescrypt2 mbedcrypto)
|
||||
|
||||
add_executable(crypt_and_hash crypt_and_hash.c)
|
||||
target_link_libraries(crypt_and_hash mbedcrypto)
|
||||
|
||||
install(TARGETS aescrypt2 crypt_and_hash
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
481
third_party/mbedtls/crypto/programs/aes/aescrypt2.c
vendored
Normal file
481
third_party/mbedtls/crypto/programs/aes/aescrypt2.c
vendored
Normal file
@ -0,0 +1,481 @@
|
||||
/*
|
||||
* AES-256 file encryption program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/* Enable definition of fileno() even when compiling with -std=c99. Must be
|
||||
* set before config.h, which pulls in glibc's features.h indirectly.
|
||||
* Harmless on other platforms. */
|
||||
#define _POSIX_C_SOURCE 1
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#if !defined(_WIN32_WCE)
|
||||
#include <io.h>
|
||||
#endif
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define MODE_ENCRYPT 0
|
||||
#define MODE_DECRYPT 1
|
||||
|
||||
#define USAGE \
|
||||
"\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
|
||||
"\n <mode>: 0 = encrypt, 1 = decrypt\n" \
|
||||
"\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
|
||||
"\n"
|
||||
|
||||
#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
|
||||
"and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
|
||||
"not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 0;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
|
||||
unsigned int i, n;
|
||||
int mode, lastn;
|
||||
size_t keylen;
|
||||
FILE *fkey, *fin = NULL, *fout = NULL;
|
||||
|
||||
char *p;
|
||||
|
||||
unsigned char IV[16];
|
||||
unsigned char tmp[16];
|
||||
unsigned char key[512];
|
||||
unsigned char digest[32];
|
||||
unsigned char buffer[1024];
|
||||
unsigned char diff;
|
||||
|
||||
mbedtls_aes_context aes_ctx;
|
||||
mbedtls_md_context_t sha_ctx;
|
||||
|
||||
#if defined(_WIN32_WCE)
|
||||
long filesize, offset;
|
||||
#elif defined(_WIN32)
|
||||
LARGE_INTEGER li_size;
|
||||
__int64 filesize, offset;
|
||||
#else
|
||||
off_t filesize, offset;
|
||||
#endif
|
||||
|
||||
mbedtls_aes_init( &aes_ctx );
|
||||
mbedtls_md_init( &sha_ctx );
|
||||
|
||||
ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse the command-line arguments.
|
||||
*/
|
||||
if( argc != 5 )
|
||||
{
|
||||
mbedtls_printf( USAGE );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mode = atoi( argv[1] );
|
||||
memset( IV, 0, sizeof( IV ) );
|
||||
memset( key, 0, sizeof( key ) );
|
||||
memset( digest, 0, sizeof( digest ) );
|
||||
memset( buffer, 0, sizeof( buffer ) );
|
||||
|
||||
if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "invalide operation mode\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( strcmp( argv[2], argv[3] ) == 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the secret key from file or command line
|
||||
*/
|
||||
if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
|
||||
{
|
||||
keylen = fread( key, 1, sizeof( key ), fkey );
|
||||
fclose( fkey );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( memcmp( argv[4], "hex:", 4 ) == 0 )
|
||||
{
|
||||
p = &argv[4][4];
|
||||
keylen = 0;
|
||||
|
||||
while( sscanf( p, "%02X", &n ) > 0 &&
|
||||
keylen < (int) sizeof( key ) )
|
||||
{
|
||||
key[keylen++] = (unsigned char) n;
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
keylen = strlen( argv[4] );
|
||||
|
||||
if( keylen > (int) sizeof( key ) )
|
||||
keylen = (int) sizeof( key );
|
||||
|
||||
memcpy( key, argv[4], keylen );
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(_WIN32_WCE)
|
||||
filesize = fseek( fin, 0L, SEEK_END );
|
||||
#else
|
||||
#if defined(_WIN32)
|
||||
/*
|
||||
* Support large files (> 2Gb) on Win32
|
||||
*/
|
||||
li_size.QuadPart = 0;
|
||||
li_size.LowPart =
|
||||
SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
|
||||
li_size.LowPart, &li_size.HighPart, FILE_END );
|
||||
|
||||
if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
filesize = li_size.QuadPart;
|
||||
#else
|
||||
if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
|
||||
{
|
||||
perror( "lseek" );
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if( fseek( fin, 0, SEEK_SET ) < 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mode == MODE_ENCRYPT )
|
||||
{
|
||||
/*
|
||||
* Generate the initialization vector as:
|
||||
* IV = SHA-256( filesize || filename )[0..15]
|
||||
*/
|
||||
for( i = 0; i < 8; i++ )
|
||||
buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
|
||||
|
||||
p = argv[2];
|
||||
|
||||
mbedtls_md_starts( &sha_ctx );
|
||||
mbedtls_md_update( &sha_ctx, buffer, 8 );
|
||||
mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
|
||||
mbedtls_md_finish( &sha_ctx, digest );
|
||||
|
||||
memcpy( IV, digest, 16 );
|
||||
|
||||
/*
|
||||
* The last four bits in the IV are actually used
|
||||
* to store the file size modulo the AES block size.
|
||||
*/
|
||||
lastn = (int)( filesize & 0x0F );
|
||||
|
||||
IV[15] = (unsigned char)
|
||||
( ( IV[15] & 0xF0 ) | lastn );
|
||||
|
||||
/*
|
||||
* Append the IV at the beginning of the output.
|
||||
*/
|
||||
if( fwrite( IV, 1, 16, fout ) != 16 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Hash the IV and the secret key together 8192 times
|
||||
* using the result to setup the AES context and HMAC.
|
||||
*/
|
||||
memset( digest, 0, 32 );
|
||||
memcpy( digest, IV, 16 );
|
||||
|
||||
for( i = 0; i < 8192; i++ )
|
||||
{
|
||||
mbedtls_md_starts( &sha_ctx );
|
||||
mbedtls_md_update( &sha_ctx, digest, 32 );
|
||||
mbedtls_md_update( &sha_ctx, key, keylen );
|
||||
mbedtls_md_finish( &sha_ctx, digest );
|
||||
}
|
||||
|
||||
mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
|
||||
mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
|
||||
|
||||
/*
|
||||
* Encrypt and write the ciphertext.
|
||||
*/
|
||||
for( offset = 0; offset < filesize; offset += 16 )
|
||||
{
|
||||
n = ( filesize - offset > 16 ) ? 16 : (int)
|
||||
( filesize - offset );
|
||||
|
||||
if( fread( buffer, 1, n, fin ) != (size_t) n )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < 16; i++ )
|
||||
buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
|
||||
|
||||
mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
|
||||
mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
|
||||
|
||||
if( fwrite( buffer, 1, 16, fout ) != 16 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( IV, buffer, 16 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Finally write the HMAC.
|
||||
*/
|
||||
mbedtls_md_hmac_finish( &sha_ctx, digest );
|
||||
|
||||
if( fwrite( digest, 1, 32, fout ) != 32 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( mode == MODE_DECRYPT )
|
||||
{
|
||||
/*
|
||||
* The encrypted file must be structured as follows:
|
||||
*
|
||||
* 00 .. 15 Initialization Vector
|
||||
* 16 .. 31 AES Encrypted Block #1
|
||||
* ..
|
||||
* N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
|
||||
* (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
|
||||
*/
|
||||
if( filesize < 48 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( filesize & 0x0F ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Subtract the IV + HMAC length.
|
||||
*/
|
||||
filesize -= ( 16 + 32 );
|
||||
|
||||
/*
|
||||
* Read the IV and original filesize modulo 16.
|
||||
*/
|
||||
if( fread( buffer, 1, 16, fin ) != 16 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( IV, buffer, 16 );
|
||||
lastn = IV[15] & 0x0F;
|
||||
|
||||
/*
|
||||
* Hash the IV and the secret key together 8192 times
|
||||
* using the result to setup the AES context and HMAC.
|
||||
*/
|
||||
memset( digest, 0, 32 );
|
||||
memcpy( digest, IV, 16 );
|
||||
|
||||
for( i = 0; i < 8192; i++ )
|
||||
{
|
||||
mbedtls_md_starts( &sha_ctx );
|
||||
mbedtls_md_update( &sha_ctx, digest, 32 );
|
||||
mbedtls_md_update( &sha_ctx, key, keylen );
|
||||
mbedtls_md_finish( &sha_ctx, digest );
|
||||
}
|
||||
|
||||
mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
|
||||
mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
|
||||
|
||||
/*
|
||||
* Decrypt and write the plaintext.
|
||||
*/
|
||||
for( offset = 0; offset < filesize; offset += 16 )
|
||||
{
|
||||
if( fread( buffer, 1, 16, fin ) != 16 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( tmp, buffer, 16 );
|
||||
|
||||
mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
|
||||
mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
|
||||
|
||||
for( i = 0; i < 16; i++ )
|
||||
buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
|
||||
|
||||
memcpy( IV, tmp, 16 );
|
||||
|
||||
n = ( lastn > 0 && offset == filesize - 16 )
|
||||
? lastn : 16;
|
||||
|
||||
if( fwrite( buffer, 1, n, fout ) != (size_t) n )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Verify the message authentication code.
|
||||
*/
|
||||
mbedtls_md_hmac_finish( &sha_ctx, digest );
|
||||
|
||||
if( fread( buffer, 1, 32, fin ) != 32 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Use constant-time buffer comparison */
|
||||
diff = 0;
|
||||
for( i = 0; i < 32; i++ )
|
||||
diff |= digest[i] ^ buffer[i];
|
||||
|
||||
if( diff != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
|
||||
"or file corrupted.\n" );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
if( fin )
|
||||
fclose( fin );
|
||||
if( fout )
|
||||
fclose( fout );
|
||||
|
||||
/* Zeroize all command line arguments to also cover
|
||||
the case when the user has missed or reordered some,
|
||||
in which case the key might not be in argv[4]. */
|
||||
for( i = 0; i < (unsigned int) argc; i++ )
|
||||
mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
|
||||
|
||||
mbedtls_platform_zeroize( IV, sizeof( IV ) );
|
||||
mbedtls_platform_zeroize( key, sizeof( key ) );
|
||||
mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
|
||||
mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
|
||||
mbedtls_platform_zeroize( digest, sizeof( digest ) );
|
||||
|
||||
mbedtls_aes_free( &aes_ctx );
|
||||
mbedtls_md_free( &sha_ctx );
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */
|
578
third_party/mbedtls/crypto/programs/aes/crypt_and_hash.c
vendored
Normal file
578
third_party/mbedtls/crypto/programs/aes/crypt_and_hash.c
vendored
Normal file
@ -0,0 +1,578 @@
|
||||
/*
|
||||
* \brief Generic file encryption program using generic wrappers for configured
|
||||
* security.
|
||||
*
|
||||
* Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/* Enable definition of fileno() even when compiling with -std=c99. Must be
|
||||
* set before config.h, which pulls in glibc's features.h indirectly.
|
||||
* Harmless on other platforms. */
|
||||
#define _POSIX_C_SOURCE 1
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
|
||||
defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/cipher.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#if !defined(_WIN32_WCE)
|
||||
#include <io.h>
|
||||
#endif
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define MODE_ENCRYPT 0
|
||||
#define MODE_DECRYPT 1
|
||||
|
||||
#define USAGE \
|
||||
"\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
|
||||
"\n <mode>: 0 = encrypt, 1 = decrypt\n" \
|
||||
"\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
|
||||
"\n"
|
||||
|
||||
#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1, i, n;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
int mode;
|
||||
size_t keylen, ilen, olen;
|
||||
FILE *fkey, *fin = NULL, *fout = NULL;
|
||||
|
||||
char *p;
|
||||
unsigned char IV[16];
|
||||
unsigned char key[512];
|
||||
unsigned char digest[MBEDTLS_MD_MAX_SIZE];
|
||||
unsigned char buffer[1024];
|
||||
unsigned char output[1024];
|
||||
unsigned char diff;
|
||||
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
const mbedtls_md_info_t *md_info;
|
||||
mbedtls_cipher_context_t cipher_ctx;
|
||||
mbedtls_md_context_t md_ctx;
|
||||
#if defined(_WIN32_WCE)
|
||||
long filesize, offset;
|
||||
#elif defined(_WIN32)
|
||||
LARGE_INTEGER li_size;
|
||||
__int64 filesize, offset;
|
||||
#else
|
||||
off_t filesize, offset;
|
||||
#endif
|
||||
|
||||
mbedtls_cipher_init( &cipher_ctx );
|
||||
mbedtls_md_init( &md_ctx );
|
||||
|
||||
/*
|
||||
* Parse the command-line arguments.
|
||||
*/
|
||||
if( argc != 7 )
|
||||
{
|
||||
const int *list;
|
||||
|
||||
mbedtls_printf( USAGE );
|
||||
|
||||
mbedtls_printf( "Available ciphers:\n" );
|
||||
list = mbedtls_cipher_list();
|
||||
while( *list )
|
||||
{
|
||||
cipher_info = mbedtls_cipher_info_from_type( *list );
|
||||
mbedtls_printf( " %s\n", cipher_info->name );
|
||||
list++;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\nAvailable message digests:\n" );
|
||||
list = mbedtls_md_list();
|
||||
while( *list )
|
||||
{
|
||||
md_info = mbedtls_md_info_from_type( *list );
|
||||
mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
|
||||
list++;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mode = atoi( argv[1] );
|
||||
|
||||
if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "invalid operation mode\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( strcmp( argv[2], argv[3] ) == 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the Cipher and MD from the command line
|
||||
*/
|
||||
cipher_info = mbedtls_cipher_info_from_string( argv[4] );
|
||||
if( cipher_info == NULL )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
|
||||
goto exit;
|
||||
}
|
||||
if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
md_info = mbedtls_md_info_from_string( argv[5] );
|
||||
if( md_info == NULL )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the secret key from file or command line
|
||||
*/
|
||||
if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
|
||||
{
|
||||
keylen = fread( key, 1, sizeof( key ), fkey );
|
||||
fclose( fkey );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( memcmp( argv[6], "hex:", 4 ) == 0 )
|
||||
{
|
||||
p = &argv[6][4];
|
||||
keylen = 0;
|
||||
|
||||
while( sscanf( p, "%02X", &n ) > 0 &&
|
||||
keylen < (int) sizeof( key ) )
|
||||
{
|
||||
key[keylen++] = (unsigned char) n;
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
keylen = strlen( argv[6] );
|
||||
|
||||
if( keylen > (int) sizeof( key ) )
|
||||
keylen = (int) sizeof( key );
|
||||
|
||||
memcpy( key, argv[6], keylen );
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(_WIN32_WCE)
|
||||
filesize = fseek( fin, 0L, SEEK_END );
|
||||
#else
|
||||
#if defined(_WIN32)
|
||||
/*
|
||||
* Support large files (> 2Gb) on Win32
|
||||
*/
|
||||
li_size.QuadPart = 0;
|
||||
li_size.LowPart =
|
||||
SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
|
||||
li_size.LowPart, &li_size.HighPart, FILE_END );
|
||||
|
||||
if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
filesize = li_size.QuadPart;
|
||||
#else
|
||||
if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
|
||||
{
|
||||
perror( "lseek" );
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if( fseek( fin, 0, SEEK_SET ) < 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mode == MODE_ENCRYPT )
|
||||
{
|
||||
/*
|
||||
* Generate the initialization vector as:
|
||||
* IV = MD( filesize || filename )[0..15]
|
||||
*/
|
||||
for( i = 0; i < 8; i++ )
|
||||
buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
|
||||
|
||||
p = argv[2];
|
||||
|
||||
mbedtls_md_starts( &md_ctx );
|
||||
mbedtls_md_update( &md_ctx, buffer, 8 );
|
||||
mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
|
||||
mbedtls_md_finish( &md_ctx, digest );
|
||||
|
||||
memcpy( IV, digest, 16 );
|
||||
|
||||
/*
|
||||
* Append the IV at the beginning of the output.
|
||||
*/
|
||||
if( fwrite( IV, 1, 16, fout ) != 16 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Hash the IV and the secret key together 8192 times
|
||||
* using the result to setup the AES context and HMAC.
|
||||
*/
|
||||
memset( digest, 0, 32 );
|
||||
memcpy( digest, IV, 16 );
|
||||
|
||||
for( i = 0; i < 8192; i++ )
|
||||
{
|
||||
mbedtls_md_starts( &md_ctx );
|
||||
mbedtls_md_update( &md_ctx, digest, 32 );
|
||||
mbedtls_md_update( &md_ctx, key, keylen );
|
||||
mbedtls_md_finish( &md_ctx, digest );
|
||||
|
||||
}
|
||||
|
||||
if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
|
||||
MBEDTLS_ENCRYPT ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
|
||||
goto exit;
|
||||
}
|
||||
if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
|
||||
goto exit;
|
||||
}
|
||||
if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
|
||||
|
||||
/*
|
||||
* Encrypt and write the ciphertext.
|
||||
*/
|
||||
for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
|
||||
{
|
||||
ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
|
||||
mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
|
||||
|
||||
if( fread( buffer, 1, ilen, fin ) != ilen )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_md_hmac_update( &md_ctx, output, olen );
|
||||
|
||||
if( fwrite( output, 1, olen, fout ) != olen )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
|
||||
goto exit;
|
||||
}
|
||||
mbedtls_md_hmac_update( &md_ctx, output, olen );
|
||||
|
||||
if( fwrite( output, 1, olen, fout ) != olen )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Finally write the HMAC.
|
||||
*/
|
||||
mbedtls_md_hmac_finish( &md_ctx, digest );
|
||||
|
||||
if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( mode == MODE_DECRYPT )
|
||||
{
|
||||
/*
|
||||
* The encrypted file must be structured as follows:
|
||||
*
|
||||
* 00 .. 15 Initialization Vector
|
||||
* 16 .. 31 Encrypted Block #1
|
||||
* ..
|
||||
* N*16 .. (N+1)*16 - 1 Encrypted Block #N
|
||||
* (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
|
||||
*/
|
||||
if( filesize < 16 + mbedtls_md_get_size( md_info ) )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check the file size.
|
||||
*/
|
||||
if( cipher_info->mode != MBEDTLS_MODE_GCM &&
|
||||
( ( filesize - mbedtls_md_get_size( md_info ) ) %
|
||||
mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
|
||||
mbedtls_cipher_get_block_size( &cipher_ctx ));
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Subtract the IV + HMAC length.
|
||||
*/
|
||||
filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
|
||||
|
||||
/*
|
||||
* Read the IV and original filesize modulo 16.
|
||||
*/
|
||||
if( fread( buffer, 1, 16, fin ) != 16 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( IV, buffer, 16 );
|
||||
|
||||
/*
|
||||
* Hash the IV and the secret key together 8192 times
|
||||
* using the result to setup the AES context and HMAC.
|
||||
*/
|
||||
memset( digest, 0, 32 );
|
||||
memcpy( digest, IV, 16 );
|
||||
|
||||
for( i = 0; i < 8192; i++ )
|
||||
{
|
||||
mbedtls_md_starts( &md_ctx );
|
||||
mbedtls_md_update( &md_ctx, digest, 32 );
|
||||
mbedtls_md_update( &md_ctx, key, keylen );
|
||||
mbedtls_md_finish( &md_ctx, digest );
|
||||
}
|
||||
|
||||
if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
|
||||
MBEDTLS_DECRYPT ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
|
||||
|
||||
/*
|
||||
* Decrypt and write the plaintext.
|
||||
*/
|
||||
for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
|
||||
{
|
||||
ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
|
||||
mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
|
||||
|
||||
if( fread( buffer, 1, ilen, fin ) != ilen )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
|
||||
mbedtls_cipher_get_block_size( &cipher_ctx ) );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
|
||||
if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
|
||||
&olen ) != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( fwrite( output, 1, olen, fout ) != olen )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Verify the message authentication code.
|
||||
*/
|
||||
mbedtls_md_hmac_finish( &md_ctx, digest );
|
||||
|
||||
if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Use constant-time buffer comparison */
|
||||
diff = 0;
|
||||
for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
|
||||
diff |= digest[i] ^ buffer[i];
|
||||
|
||||
if( diff != 0 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
|
||||
"or file corrupted.\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the final block of data
|
||||
*/
|
||||
mbedtls_cipher_finish( &cipher_ctx, output, &olen );
|
||||
|
||||
if( fwrite( output, 1, olen, fout ) != olen )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
if( fin )
|
||||
fclose( fin );
|
||||
if( fout )
|
||||
fclose( fout );
|
||||
|
||||
/* Zeroize all command line arguments to also cover
|
||||
the case when the user has missed or reordered some,
|
||||
in which case the key might not be in argv[6]. */
|
||||
for( i = 0; i < argc; i++ )
|
||||
mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
|
||||
|
||||
mbedtls_platform_zeroize( IV, sizeof( IV ) );
|
||||
mbedtls_platform_zeroize( key, sizeof( key ) );
|
||||
mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
|
||||
mbedtls_platform_zeroize( output, sizeof( output ) );
|
||||
mbedtls_platform_zeroize( digest, sizeof( digest ) );
|
||||
|
||||
mbedtls_cipher_free( &cipher_ctx );
|
||||
mbedtls_md_free( &md_ctx );
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */
|
9
third_party/mbedtls/crypto/programs/hash/CMakeLists.txt
vendored
Normal file
9
third_party/mbedtls/crypto/programs/hash/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
add_executable(hello hello.c)
|
||||
target_link_libraries(hello mbedcrypto)
|
||||
|
||||
add_executable(generic_sum generic_sum.c)
|
||||
target_link_libraries(generic_sum mbedcrypto)
|
||||
|
||||
install(TARGETS hello generic_sum
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
253
third_party/mbedtls/crypto/programs/hash/generic_sum.c
vendored
Normal file
253
third_party/mbedtls/crypto/programs/hash/generic_sum.c
vendored
Normal file
@ -0,0 +1,253 @@
|
||||
/*
|
||||
* generic message digest layer demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_MD_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/md.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
static int generic_wrapper( const mbedtls_md_info_t *md_info, char *filename, unsigned char *sum )
|
||||
{
|
||||
int ret = mbedtls_md_file( md_info, filename, sum );
|
||||
|
||||
if( ret == 1 )
|
||||
mbedtls_fprintf( stderr, "failed to open: %s\n", filename );
|
||||
|
||||
if( ret == 2 )
|
||||
mbedtls_fprintf( stderr, "failed to read: %s\n", filename );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
static int generic_print( const mbedtls_md_info_t *md_info, char *filename )
|
||||
{
|
||||
int i;
|
||||
unsigned char sum[MBEDTLS_MD_MAX_SIZE];
|
||||
|
||||
if( generic_wrapper( md_info, filename, sum ) != 0 )
|
||||
return( 1 );
|
||||
|
||||
for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
|
||||
mbedtls_printf( "%02x", sum[i] );
|
||||
|
||||
mbedtls_printf( " %s\n", filename );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int generic_check( const mbedtls_md_info_t *md_info, char *filename )
|
||||
{
|
||||
int i;
|
||||
size_t n;
|
||||
FILE *f;
|
||||
int nb_err1, nb_err2;
|
||||
int nb_tot1, nb_tot2;
|
||||
unsigned char sum[MBEDTLS_MD_MAX_SIZE];
|
||||
char line[1024];
|
||||
char diff;
|
||||
#if defined(__clang_analyzer__)
|
||||
char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1] = { };
|
||||
#else
|
||||
char buf[MBEDTLS_MD_MAX_SIZE * 2 + 1];
|
||||
#endif
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "failed to open: %s\n", filename );
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
nb_err1 = nb_err2 = 0;
|
||||
nb_tot1 = nb_tot2 = 0;
|
||||
|
||||
memset( line, 0, sizeof( line ) );
|
||||
|
||||
n = sizeof( line );
|
||||
|
||||
while( fgets( line, (int) n - 1, f ) != NULL )
|
||||
{
|
||||
n = strlen( line );
|
||||
|
||||
if( n < (size_t) 2 * mbedtls_md_get_size( md_info ) + 4 )
|
||||
{
|
||||
mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
|
||||
continue;
|
||||
}
|
||||
|
||||
if( line[2 * mbedtls_md_get_size( md_info )] != ' ' || line[2 * mbedtls_md_get_size( md_info ) + 1] != ' ' )
|
||||
{
|
||||
mbedtls_printf("No '%s' hash found on line.\n", mbedtls_md_get_name( md_info ));
|
||||
continue;
|
||||
}
|
||||
|
||||
if( line[n - 1] == '\n' ) { n--; line[n] = '\0'; }
|
||||
if( line[n - 1] == '\r' ) { n--; line[n] = '\0'; }
|
||||
|
||||
nb_tot1++;
|
||||
|
||||
if( generic_wrapper( md_info, line + 2 + 2 * mbedtls_md_get_size( md_info ), sum ) != 0 )
|
||||
{
|
||||
nb_err1++;
|
||||
continue;
|
||||
}
|
||||
|
||||
nb_tot2++;
|
||||
|
||||
for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
|
||||
sprintf( buf + i * 2, "%02x", sum[i] );
|
||||
|
||||
/* Use constant-time buffer comparison */
|
||||
diff = 0;
|
||||
for( i = 0; i < 2 * mbedtls_md_get_size( md_info ); i++ )
|
||||
diff |= line[i] ^ buf[i];
|
||||
|
||||
if( diff != 0 )
|
||||
{
|
||||
nb_err2++;
|
||||
mbedtls_fprintf( stderr, "wrong checksum: %s\n", line + 66 );
|
||||
}
|
||||
|
||||
n = sizeof( line );
|
||||
}
|
||||
|
||||
if( nb_err1 != 0 )
|
||||
{
|
||||
mbedtls_printf( "WARNING: %d (out of %d) input files could "
|
||||
"not be read\n", nb_err1, nb_tot1 );
|
||||
}
|
||||
|
||||
if( nb_err2 != 0 )
|
||||
{
|
||||
mbedtls_printf( "WARNING: %d (out of %d) computed checksums did "
|
||||
"not match\n", nb_err2, nb_tot2 );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
return( nb_err1 != 0 || nb_err2 != 0 );
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1, i;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
const mbedtls_md_info_t *md_info;
|
||||
mbedtls_md_context_t md_ctx;
|
||||
|
||||
mbedtls_md_init( &md_ctx );
|
||||
|
||||
if( argc == 1 )
|
||||
{
|
||||
const int *list;
|
||||
|
||||
mbedtls_printf( "print mode: generic_sum <mbedtls_md> <file> <file> ...\n" );
|
||||
mbedtls_printf( "check mode: generic_sum <mbedtls_md> -c <checksum file>\n" );
|
||||
|
||||
mbedtls_printf( "\nAvailable message digests:\n" );
|
||||
list = mbedtls_md_list();
|
||||
while( *list )
|
||||
{
|
||||
md_info = mbedtls_md_info_from_type( *list );
|
||||
mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
|
||||
list++;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the MD from the command line
|
||||
*/
|
||||
md_info = mbedtls_md_info_from_string( argv[1] );
|
||||
if( md_info == NULL )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[1] );
|
||||
return( exit_code );
|
||||
}
|
||||
if( mbedtls_md_setup( &md_ctx, md_info, 0 ) )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "Failed to initialize context.\n" );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
if( argc == 4 && strcmp( "-c", argv[2] ) == 0 )
|
||||
{
|
||||
ret |= generic_check( md_info, argv[3] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 2; i < argc; i++ )
|
||||
ret |= generic_print( md_info, argv[i] );
|
||||
|
||||
if ( ret == 0 )
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_md_free( &md_ctx );
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_MD_C && MBEDTLS_FS_IO */
|
86
third_party/mbedtls/crypto/programs/hash/hello.c
vendored
Normal file
86
third_party/mbedtls/crypto/programs/hash/hello.c
vendored
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Classic "Hello, world" demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
#include "mbedtls/md5.h"
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_MD5_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_MD5_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( void )
|
||||
{
|
||||
int i, ret;
|
||||
unsigned char digest[16];
|
||||
char str[] = "Hello, world!";
|
||||
|
||||
mbedtls_printf( "\n MD5('%s') = ", str );
|
||||
|
||||
if( ( ret = mbedtls_md5_ret( (unsigned char *) str, 13, digest ) ) != 0 )
|
||||
return( MBEDTLS_EXIT_FAILURE );
|
||||
|
||||
for( i = 0; i < 16; i++ )
|
||||
mbedtls_printf( "%02x", digest[i] );
|
||||
|
||||
mbedtls_printf( "\n\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( MBEDTLS_EXIT_SUCCESS );
|
||||
}
|
||||
#endif /* MBEDTLS_MD5_C */
|
57
third_party/mbedtls/crypto/programs/pkey/CMakeLists.txt
vendored
Normal file
57
third_party/mbedtls/crypto/programs/pkey/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
add_executable(dh_genprime dh_genprime.c)
|
||||
target_link_libraries(dh_genprime mbedcrypto)
|
||||
|
||||
add_executable(ecdh_curve25519 ecdh_curve25519.c)
|
||||
target_link_libraries(ecdh_curve25519 mbedcrypto)
|
||||
|
||||
add_executable(ecdsa ecdsa.c)
|
||||
target_link_libraries(ecdsa mbedcrypto)
|
||||
|
||||
add_executable(gen_key gen_key.c)
|
||||
target_link_libraries(gen_key mbedcrypto)
|
||||
|
||||
add_executable(key_app key_app.c)
|
||||
target_link_libraries(key_app mbedcrypto)
|
||||
|
||||
add_executable(key_app_writer key_app_writer.c)
|
||||
target_link_libraries(key_app_writer mbedcrypto)
|
||||
|
||||
add_executable(mpi_demo mpi_demo.c)
|
||||
target_link_libraries(mpi_demo mbedcrypto)
|
||||
|
||||
add_executable(rsa_genkey rsa_genkey.c)
|
||||
target_link_libraries(rsa_genkey mbedcrypto)
|
||||
|
||||
add_executable(rsa_sign rsa_sign.c)
|
||||
target_link_libraries(rsa_sign mbedcrypto)
|
||||
|
||||
add_executable(rsa_verify rsa_verify.c)
|
||||
target_link_libraries(rsa_verify mbedcrypto)
|
||||
|
||||
add_executable(rsa_sign_pss rsa_sign_pss.c)
|
||||
target_link_libraries(rsa_sign_pss mbedcrypto)
|
||||
|
||||
add_executable(rsa_verify_pss rsa_verify_pss.c)
|
||||
target_link_libraries(rsa_verify_pss mbedcrypto)
|
||||
|
||||
add_executable(rsa_encrypt rsa_encrypt.c)
|
||||
target_link_libraries(rsa_encrypt mbedcrypto)
|
||||
|
||||
add_executable(rsa_decrypt rsa_decrypt.c)
|
||||
target_link_libraries(rsa_decrypt mbedcrypto)
|
||||
|
||||
add_executable(pk_sign pk_sign.c)
|
||||
target_link_libraries(pk_sign mbedcrypto)
|
||||
|
||||
add_executable(pk_verify pk_verify.c)
|
||||
target_link_libraries(pk_verify mbedcrypto)
|
||||
|
||||
add_executable(pk_encrypt pk_encrypt.c)
|
||||
target_link_libraries(pk_encrypt mbedcrypto)
|
||||
|
||||
add_executable(pk_decrypt pk_decrypt.c)
|
||||
target_link_libraries(pk_decrypt mbedcrypto)
|
||||
|
||||
install(TARGETS dh_genprime key_app mpi_demo rsa_genkey rsa_sign rsa_verify rsa_encrypt rsa_decrypt pk_encrypt pk_decrypt pk_sign pk_verify gen_key
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
214
third_party/mbedtls/crypto/programs/pkey/dh_genprime.c
vendored
Normal file
214
third_party/mbedtls/crypto/programs/pkey/dh_genprime.c
vendored
Normal file
@ -0,0 +1,214 @@
|
||||
/*
|
||||
* Diffie-Hellman-Merkle key exchange (prime generation)
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_time_t time_t
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
|
||||
!defined(MBEDTLS_GENPRIME)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
|
||||
"MBEDTLS_GENPRIME not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define USAGE \
|
||||
"\n usage: dh_genprime param=<>...\n" \
|
||||
"\n acceprable parameters:\n" \
|
||||
" bits=%%d default: 2048\n"
|
||||
|
||||
#define DFL_BITS 2048
|
||||
|
||||
/*
|
||||
* Note: G = 4 is always a quadratic residue mod P,
|
||||
* so it is a generator of order Q (with P = 2*Q+1).
|
||||
*/
|
||||
#define GENERATOR "4"
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_mpi G, P, Q;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
const char *pers = "dh_genprime";
|
||||
FILE *fout;
|
||||
int nbits = DFL_BITS;
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
||||
mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
{
|
||||
p = argv[i];
|
||||
if( ( q = strchr( p, '=' ) ) == NULL )
|
||||
goto usage;
|
||||
*q++ = '\0';
|
||||
|
||||
if( strcmp( p, "bits" ) == 0 )
|
||||
{
|
||||
nbits = atoi( q );
|
||||
if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS )
|
||||
goto usage;
|
||||
}
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ! Generating large primes may take minutes!\n" );
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n . Generating the modulus, please wait..." );
|
||||
fflush( stdout );
|
||||
|
||||
/*
|
||||
* This can take a long time...
|
||||
*/
|
||||
if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_is_prime_ext( &Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
|
||||
( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
|
||||
fclose( fout );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n\n" );
|
||||
fclose( fout );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */
|
2
third_party/mbedtls/crypto/programs/pkey/dh_prime.txt
vendored
Normal file
2
third_party/mbedtls/crypto/programs/pkey/dh_prime.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
P = FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF
|
||||
G = 02
|
255
third_party/mbedtls/crypto/programs/pkey/ecdh_curve25519.c
vendored
Normal file
255
third_party/mbedtls/crypto/programs/pkey/ecdh_curve25519.c
vendored
Normal file
@ -0,0 +1,255 @@
|
||||
/*
|
||||
* Example ECDHE with Curve25519 program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_ECDH_C) || !defined(MBEDTLS_ECDH_LEGACY_CONTEXT) || \
|
||||
!defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf( "MBEDTLS_ECDH_C and/or MBEDTLS_ECDH_LEGACY_CONTEXT and/or "
|
||||
"MBEDTLS_ECP_DP_CURVE25519_ENABLED and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
|
||||
"not defined\n" );
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/ecdh.h"
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_ecdh_context ctx_cli, ctx_srv;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char cli_to_srv[32], srv_to_cli[32];
|
||||
const char pers[] = "ecdh";
|
||||
((void) argc);
|
||||
((void) argv);
|
||||
|
||||
mbedtls_ecdh_init( &ctx_cli );
|
||||
mbedtls_ecdh_init( &ctx_srv );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
|
||||
/*
|
||||
* Initialize random number generation
|
||||
*/
|
||||
mbedtls_printf( " . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
sizeof pers ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* Client: inialize context and generate keypair
|
||||
*/
|
||||
mbedtls_printf( " . Setting up client context..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_ecp_group_load( &ctx_cli.grp, MBEDTLS_ECP_DP_CURVE25519 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, &ctx_cli.d, &ctx_cli.Q,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, cli_to_srv, 32 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* Server: initialize context and generate keypair
|
||||
*/
|
||||
mbedtls_printf( " . Setting up server context..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_ecp_group_load( &ctx_srv.grp, MBEDTLS_ECP_DP_CURVE25519 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli, 32 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* Server: read peer's key and generate shared secret
|
||||
*/
|
||||
mbedtls_printf( " . Server reading client key and computing secret..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, 1 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, cli_to_srv, 32 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, &ctx_srv.z,
|
||||
&ctx_srv.Qp, &ctx_srv.d,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* Client: read peer's key and generate shared secret
|
||||
*/
|
||||
mbedtls_printf( " . Client reading server key and computing secret..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli, 32 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z,
|
||||
&ctx_cli.Qp, &ctx_cli.d,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* Verification: are the computed secrets equal?
|
||||
*/
|
||||
mbedtls_printf( " . Checking if both computed secrets are equal..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
mbedtls_ecdh_free( &ctx_srv );
|
||||
mbedtls_ecdh_free( &ctx_cli );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_CURVE25519_ENABLED &&
|
||||
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
265
third_party/mbedtls/crypto/programs/pkey/ecdsa.c
vendored
Normal file
265
third_party/mbedtls/crypto/programs/pkey/ecdsa.c
vendored
Normal file
@ -0,0 +1,265 @@
|
||||
/*
|
||||
* Example ECDSA program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_ECDSA_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Uncomment to show key and signature details
|
||||
*/
|
||||
#define VERBOSE
|
||||
|
||||
/*
|
||||
* Uncomment to force use of a specific curve
|
||||
*/
|
||||
#define ECPARAMS MBEDTLS_ECP_DP_SECP192R1
|
||||
|
||||
#if !defined(ECPARAMS)
|
||||
#define ECPARAMS mbedtls_ecp_curve_list()->grp_id
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_ECDSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_ECDSA_C and/or MBEDTLS_SHA256_C and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C not defined\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
#if defined(VERBOSE)
|
||||
static void dump_buf( const char *title, unsigned char *buf, size_t len )
|
||||
{
|
||||
size_t i;
|
||||
|
||||
mbedtls_printf( "%s", title );
|
||||
for( i = 0; i < len; i++ )
|
||||
mbedtls_printf("%c%c", "0123456789ABCDEF" [buf[i] / 16],
|
||||
"0123456789ABCDEF" [buf[i] % 16] );
|
||||
mbedtls_printf( "\n" );
|
||||
}
|
||||
|
||||
static void dump_pubkey( const char *title, mbedtls_ecdsa_context *key )
|
||||
{
|
||||
unsigned char buf[300];
|
||||
size_t len;
|
||||
|
||||
if( mbedtls_ecp_point_write_binary( &key->grp, &key->Q,
|
||||
MBEDTLS_ECP_PF_UNCOMPRESSED, &len, buf, sizeof buf ) != 0 )
|
||||
{
|
||||
mbedtls_printf("internal error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
dump_buf( title, buf, len );
|
||||
}
|
||||
#else
|
||||
#define dump_buf( a, b, c )
|
||||
#define dump_pubkey( a, b )
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_ecdsa_context ctx_sign, ctx_verify;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char message[100];
|
||||
unsigned char hash[32];
|
||||
unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
|
||||
size_t sig_len;
|
||||
const char *pers = "ecdsa";
|
||||
((void) argv);
|
||||
|
||||
mbedtls_ecdsa_init( &ctx_sign );
|
||||
mbedtls_ecdsa_init( &ctx_verify );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
|
||||
memset( sig, 0, sizeof( sig ) );
|
||||
memset( message, 0x25, sizeof( message ) );
|
||||
|
||||
if( argc != 1 )
|
||||
{
|
||||
mbedtls_printf( "usage: ecdsa\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate a key pair for signing
|
||||
*/
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n . Generating key pair..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ecdsa_genkey( &ctx_sign, ECPARAMS,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok (key size: %d bits)\n", (int) ctx_sign.grp.pbits );
|
||||
|
||||
dump_pubkey( " + Public key: ", &ctx_sign );
|
||||
|
||||
/*
|
||||
* Compute message hash
|
||||
*/
|
||||
mbedtls_printf( " . Computing message hash..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_sha256_ret( message, sizeof( message ), hash, 0 ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_sha256_ret returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
dump_buf( " + Hash: ", hash, sizeof( hash ) );
|
||||
|
||||
/*
|
||||
* Sign message hash
|
||||
*/
|
||||
mbedtls_printf( " . Signing message hash..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ecdsa_write_signature( &ctx_sign, MBEDTLS_MD_SHA256,
|
||||
hash, sizeof( hash ),
|
||||
sig, &sig_len,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdsa_genkey returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
mbedtls_printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
|
||||
|
||||
dump_buf( " + Signature: ", sig, sig_len );
|
||||
|
||||
/*
|
||||
* Transfer public information to verifying context
|
||||
*
|
||||
* We could use the same context for verification and signatures, but we
|
||||
* chose to use a new one in order to make it clear that the verifying
|
||||
* context only needs the public key (Q), and not the private key (d).
|
||||
*/
|
||||
mbedtls_printf( " . Preparing verification context..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ecp_group_copy( &ctx_verify.grp, &ctx_sign.grp ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecp_group_copy returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_ecp_copy( &ctx_verify.Q, &ctx_sign.Q ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecp_copy returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Verify signature
|
||||
*/
|
||||
mbedtls_printf( " ok\n . Verifying signature..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ecdsa_read_signature( &ctx_verify,
|
||||
hash, sizeof( hash ),
|
||||
sig, sig_len ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecdsa_read_signature returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
mbedtls_ecdsa_free( &ctx_verify );
|
||||
mbedtls_ecdsa_free( &ctx_sign );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
|
||||
ECPARAMS */
|
463
third_party/mbedtls/crypto/programs/pkey/gen_key.c
vendored
Normal file
463
third_party/mbedtls/crypto/programs/pkey/gen_key.c
vendored
Normal file
@ -0,0 +1,463 @@
|
||||
/*
|
||||
* Key generation application
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <unistd.h>
|
||||
|
||||
#define DEV_RANDOM_THRESHOLD 32
|
||||
|
||||
int dev_random_entropy_poll( void *data, unsigned char *output,
|
||||
size_t len, size_t *olen )
|
||||
{
|
||||
FILE *file;
|
||||
size_t ret, left = len;
|
||||
unsigned char *p = output;
|
||||
((void) data);
|
||||
|
||||
*olen = 0;
|
||||
|
||||
file = fopen( "/dev/random", "rb" );
|
||||
if( file == NULL )
|
||||
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
||||
|
||||
while( left > 0 )
|
||||
{
|
||||
/* /dev/random can return much less than requested. If so, try again */
|
||||
ret = fread( p, 1, left, file );
|
||||
if( ret == 0 && ferror( file ) )
|
||||
{
|
||||
fclose( file );
|
||||
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
||||
}
|
||||
|
||||
p += ret;
|
||||
left -= ret;
|
||||
sleep( 1 );
|
||||
}
|
||||
fclose( file );
|
||||
*olen = len;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* !_WIN32 */
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
|
||||
#else
|
||||
#define DFL_EC_CURVE 0
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
|
||||
#define USAGE_DEV_RANDOM \
|
||||
" use_dev_random=0|1 default: 0\n"
|
||||
#else
|
||||
#define USAGE_DEV_RANDOM ""
|
||||
#endif /* !_WIN32 && MBEDTLS_FS_IO */
|
||||
|
||||
#define FORMAT_PEM 0
|
||||
#define FORMAT_DER 1
|
||||
|
||||
#define DFL_TYPE MBEDTLS_PK_RSA
|
||||
#define DFL_RSA_KEYSIZE 4096
|
||||
#define DFL_FILENAME "keyfile.key"
|
||||
#define DFL_FORMAT FORMAT_PEM
|
||||
#define DFL_USE_DEV_RANDOM 0
|
||||
|
||||
#define USAGE \
|
||||
"\n usage: gen_key param=<>...\n" \
|
||||
"\n acceptable parameters:\n" \
|
||||
" type=rsa|ec default: rsa\n" \
|
||||
" rsa_keysize=%%d default: 4096\n" \
|
||||
" ec_curve=%%s see below\n" \
|
||||
" filename=%%s default: keyfile.key\n" \
|
||||
" format=pem|der default: pem\n" \
|
||||
USAGE_DEV_RANDOM \
|
||||
"\n"
|
||||
|
||||
#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
|
||||
"MBEDTLS_PEM_WRITE_C"
|
||||
"not defined.\n" );
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* global options
|
||||
*/
|
||||
struct options
|
||||
{
|
||||
int type; /* the type of key to generate */
|
||||
int rsa_keysize; /* length of key in bits */
|
||||
int ec_curve; /* curve identifier for EC keys */
|
||||
const char *filename; /* filename of the key file */
|
||||
int format; /* the output format to use */
|
||||
int use_dev_random; /* use /dev/random as entropy source */
|
||||
} opt;
|
||||
|
||||
static int write_private_key( mbedtls_pk_context *key, const char *output_file )
|
||||
{
|
||||
int ret;
|
||||
FILE *f;
|
||||
unsigned char output_buf[16000];
|
||||
unsigned char *c = output_buf;
|
||||
size_t len = 0;
|
||||
|
||||
memset(output_buf, 0, 16000);
|
||||
if( opt.format == FORMAT_PEM )
|
||||
{
|
||||
if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
len = strlen( (char *) output_buf );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
|
||||
return( ret );
|
||||
|
||||
len = ret;
|
||||
c = output_buf + sizeof(output_buf) - len;
|
||||
}
|
||||
|
||||
if( ( f = fopen( output_file, "wb" ) ) == NULL )
|
||||
return( -1 );
|
||||
|
||||
if( fwrite( c, 1, len, f ) != len )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context key;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
const char *pers = "gen_key";
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
const mbedtls_ecp_curve_info *curve_info;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
|
||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
mbedtls_pk_init( &key );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
mbedtls_printf( " available ec_curve values:\n" );
|
||||
curve_info = mbedtls_ecp_curve_list();
|
||||
mbedtls_printf( " %s (default)\n", curve_info->name );
|
||||
while( ( ++curve_info )->name != NULL )
|
||||
mbedtls_printf( " %s\n", curve_info->name );
|
||||
#endif /* MBEDTLS_ECP_C */
|
||||
goto exit;
|
||||
}
|
||||
|
||||
opt.type = DFL_TYPE;
|
||||
opt.rsa_keysize = DFL_RSA_KEYSIZE;
|
||||
opt.ec_curve = DFL_EC_CURVE;
|
||||
opt.filename = DFL_FILENAME;
|
||||
opt.format = DFL_FORMAT;
|
||||
opt.use_dev_random = DFL_USE_DEV_RANDOM;
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
{
|
||||
p = argv[i];
|
||||
if( ( q = strchr( p, '=' ) ) == NULL )
|
||||
goto usage;
|
||||
*q++ = '\0';
|
||||
|
||||
if( strcmp( p, "type" ) == 0 )
|
||||
{
|
||||
if( strcmp( q, "rsa" ) == 0 )
|
||||
opt.type = MBEDTLS_PK_RSA;
|
||||
else if( strcmp( q, "ec" ) == 0 )
|
||||
opt.type = MBEDTLS_PK_ECKEY;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "format" ) == 0 )
|
||||
{
|
||||
if( strcmp( q, "pem" ) == 0 )
|
||||
opt.format = FORMAT_PEM;
|
||||
else if( strcmp( q, "der" ) == 0 )
|
||||
opt.format = FORMAT_DER;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "rsa_keysize" ) == 0 )
|
||||
{
|
||||
opt.rsa_keysize = atoi( q );
|
||||
if( opt.rsa_keysize < 1024 ||
|
||||
opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
|
||||
goto usage;
|
||||
}
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
else if( strcmp( p, "ec_curve" ) == 0 )
|
||||
{
|
||||
if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
|
||||
goto usage;
|
||||
opt.ec_curve = curve_info->grp_id;
|
||||
}
|
||||
#endif
|
||||
else if( strcmp( p, "filename" ) == 0 )
|
||||
opt.filename = q;
|
||||
else if( strcmp( p, "use_dev_random" ) == 0 )
|
||||
{
|
||||
opt.use_dev_random = atoi( q );
|
||||
if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
|
||||
goto usage;
|
||||
}
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
|
||||
if( opt.use_dev_random )
|
||||
{
|
||||
if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
|
||||
NULL, DEV_RANDOM_THRESHOLD,
|
||||
MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf("\n Using /dev/random, so can take a long time! " );
|
||||
fflush( stdout );
|
||||
}
|
||||
#endif /* !_WIN32 && MBEDTLS_FS_IO */
|
||||
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 1.1. Generate the key
|
||||
*/
|
||||
mbedtls_printf( "\n . Generating the private key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_setup( &key,
|
||||
mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
|
||||
if( opt.type == MBEDTLS_PK_RSA )
|
||||
{
|
||||
ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
|
||||
opt.rsa_keysize, 65537 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( opt.type == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
|
||||
mbedtls_pk_ec( key ),
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", -ret );
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif /* MBEDTLS_ECP_C */
|
||||
{
|
||||
mbedtls_printf( " failed\n ! key type not supported\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
mbedtls_printf( " ok\n . Key information:\n" );
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
|
||||
|
||||
if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
|
||||
( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
|
||||
mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
|
||||
mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
|
||||
mbedtls_printf( "curve: %s\n",
|
||||
mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
|
||||
mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
mbedtls_printf(" ! key type not supported\n");
|
||||
|
||||
/*
|
||||
* 1.3 Export key
|
||||
*/
|
||||
mbedtls_printf( " . Writing key to file..." );
|
||||
|
||||
if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
mbedtls_printf( " - %s\n", buf );
|
||||
#else
|
||||
mbedtls_printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
|
||||
mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
|
||||
|
||||
mbedtls_pk_free( &key );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
|
||||
* MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
328
third_party/mbedtls/crypto/programs/pkey/key_app.c
vendored
Normal file
328
third_party/mbedtls/crypto/programs/pkey/key_app.c
vendored
Normal file
@ -0,0 +1,328 @@
|
||||
/*
|
||||
* Key reading application
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && \
|
||||
defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/pk.h"
|
||||
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define MODE_NONE 0
|
||||
#define MODE_PRIVATE 1
|
||||
#define MODE_PUBLIC 2
|
||||
|
||||
#define DFL_MODE MODE_NONE
|
||||
#define DFL_FILENAME "keyfile.key"
|
||||
#define DFL_PASSWORD ""
|
||||
#define DFL_PASSWORD_FILE ""
|
||||
#define DFL_DEBUG_LEVEL 0
|
||||
|
||||
#define USAGE \
|
||||
"\n usage: key_app param=<>...\n" \
|
||||
"\n acceptable parameters:\n" \
|
||||
" mode=private|public default: none\n" \
|
||||
" filename=%%s default: keyfile.key\n" \
|
||||
" password=%%s default: \"\"\n" \
|
||||
" password_file=%%s default: \"\"\n" \
|
||||
"\n"
|
||||
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or "
|
||||
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* global options
|
||||
*/
|
||||
struct options
|
||||
{
|
||||
int mode; /* the mode to run the application in */
|
||||
const char *filename; /* filename of the key file */
|
||||
const char *password; /* password for the private key */
|
||||
const char *password_file; /* password_file for the private key */
|
||||
} opt;
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
|
||||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
mbedtls_pk_init( &pk );
|
||||
memset( buf, 0, sizeof(buf) );
|
||||
|
||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
opt.mode = DFL_MODE;
|
||||
opt.filename = DFL_FILENAME;
|
||||
opt.password = DFL_PASSWORD;
|
||||
opt.password_file = DFL_PASSWORD_FILE;
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
{
|
||||
p = argv[i];
|
||||
if( ( q = strchr( p, '=' ) ) == NULL )
|
||||
goto usage;
|
||||
*q++ = '\0';
|
||||
|
||||
if( strcmp( p, "mode" ) == 0 )
|
||||
{
|
||||
if( strcmp( q, "private" ) == 0 )
|
||||
opt.mode = MODE_PRIVATE;
|
||||
else if( strcmp( q, "public" ) == 0 )
|
||||
opt.mode = MODE_PUBLIC;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "filename" ) == 0 )
|
||||
opt.filename = q;
|
||||
else if( strcmp( p, "password" ) == 0 )
|
||||
opt.password = q;
|
||||
else if( strcmp( p, "password_file" ) == 0 )
|
||||
opt.password_file = q;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( opt.mode == MODE_PRIVATE )
|
||||
{
|
||||
if( strlen( opt.password ) && strlen( opt.password_file ) )
|
||||
{
|
||||
mbedtls_printf( "Error: cannot have both password and password_file\n" );
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( strlen( opt.password_file ) )
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
mbedtls_printf( "\n . Loading the password file ..." );
|
||||
if( ( f = fopen( opt.password_file, "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! fopen returned NULL\n" );
|
||||
goto cleanup;
|
||||
}
|
||||
if( fgets( buf, sizeof(buf), f ) == NULL )
|
||||
{
|
||||
fclose( f );
|
||||
mbedtls_printf( "Error: fgets() failed to retrieve password\n" );
|
||||
goto cleanup;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
i = (int) strlen( buf );
|
||||
if( buf[i - 1] == '\n' ) buf[i - 1] = '\0';
|
||||
if( buf[i - 2] == '\r' ) buf[i - 2] = '\0';
|
||||
opt.password = buf;
|
||||
}
|
||||
|
||||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
mbedtls_printf( "\n . Loading the private key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
mbedtls_printf( " . Key information ...\n" );
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
|
||||
|
||||
if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
|
||||
( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
mbedtls_printf("Do not know how to print key information for this type\n" );
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
else if( opt.mode == MODE_PUBLIC )
|
||||
{
|
||||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
mbedtls_printf( "\n . Loading the public key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
mbedtls_printf( " . Key information ...\n" );
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
|
||||
|
||||
if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
|
||||
NULL, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto cleanup;
|
||||
}
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
mbedtls_printf("Do not know how to print key information for this type\n" );
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
else
|
||||
goto usage;
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
|
||||
mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
452
third_party/mbedtls/crypto/programs/pkey/key_app_writer.c
vendored
Normal file
452
third_party/mbedtls/crypto/programs/pkey/key_app_writer.c
vendored
Normal file
@ -0,0 +1,452 @@
|
||||
/*
|
||||
* Key writing application
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/error.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
#define USAGE_OUT \
|
||||
" output_file=%%s default: keyfile.pem\n" \
|
||||
" output_format=pem|der default: pem\n"
|
||||
#else
|
||||
#define USAGE_OUT \
|
||||
" output_file=%%s default: keyfile.der\n" \
|
||||
" output_format=der default: der\n"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
#define DFL_OUTPUT_FILENAME "keyfile.pem"
|
||||
#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM
|
||||
#else
|
||||
#define DFL_OUTPUT_FILENAME "keyfile.der"
|
||||
#define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER
|
||||
#endif
|
||||
|
||||
#define DFL_MODE MODE_NONE
|
||||
#define DFL_FILENAME "keyfile.key"
|
||||
#define DFL_DEBUG_LEVEL 0
|
||||
#define DFL_OUTPUT_MODE OUTPUT_MODE_NONE
|
||||
|
||||
#define MODE_NONE 0
|
||||
#define MODE_PRIVATE 1
|
||||
#define MODE_PUBLIC 2
|
||||
|
||||
#define OUTPUT_MODE_NONE 0
|
||||
#define OUTPUT_MODE_PRIVATE 1
|
||||
#define OUTPUT_MODE_PUBLIC 2
|
||||
|
||||
#define OUTPUT_FORMAT_PEM 0
|
||||
#define OUTPUT_FORMAT_DER 1
|
||||
|
||||
#define USAGE \
|
||||
"\n usage: key_app_writer param=<>...\n" \
|
||||
"\n acceptable parameters:\n" \
|
||||
" mode=private|public default: none\n" \
|
||||
" filename=%%s default: keyfile.key\n" \
|
||||
" output_mode=private|public default: none\n" \
|
||||
USAGE_OUT \
|
||||
"\n"
|
||||
|
||||
#if !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
!defined(MBEDTLS_PK_WRITE_C) || \
|
||||
!defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" );
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* global options
|
||||
*/
|
||||
struct options
|
||||
{
|
||||
int mode; /* the mode to run the application in */
|
||||
const char *filename; /* filename of the key file */
|
||||
int output_mode; /* the output mode to use */
|
||||
const char *output_file; /* where to store the constructed key file */
|
||||
int output_format; /* the output format to use */
|
||||
} opt;
|
||||
|
||||
static int write_public_key( mbedtls_pk_context *key, const char *output_file )
|
||||
{
|
||||
int ret;
|
||||
FILE *f;
|
||||
unsigned char output_buf[16000];
|
||||
unsigned char *c = output_buf;
|
||||
size_t len = 0;
|
||||
|
||||
memset(output_buf, 0, 16000);
|
||||
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
if( opt.output_format == OUTPUT_FORMAT_PEM )
|
||||
{
|
||||
if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
len = strlen( (char *) output_buf );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 )
|
||||
return( ret );
|
||||
|
||||
len = ret;
|
||||
c = output_buf + sizeof(output_buf) - len;
|
||||
}
|
||||
|
||||
if( ( f = fopen( output_file, "w" ) ) == NULL )
|
||||
return( -1 );
|
||||
|
||||
if( fwrite( c, 1, len, f ) != len )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int write_private_key( mbedtls_pk_context *key, const char *output_file )
|
||||
{
|
||||
int ret;
|
||||
FILE *f;
|
||||
unsigned char output_buf[16000];
|
||||
unsigned char *c = output_buf;
|
||||
size_t len = 0;
|
||||
|
||||
memset(output_buf, 0, 16000);
|
||||
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
if( opt.output_format == OUTPUT_FORMAT_PEM )
|
||||
{
|
||||
if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
len = strlen( (char *) output_buf );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
|
||||
return( ret );
|
||||
|
||||
len = ret;
|
||||
c = output_buf + sizeof(output_buf) - len;
|
||||
}
|
||||
|
||||
if( ( f = fopen( output_file, "w" ) ) == NULL )
|
||||
return( -1 );
|
||||
|
||||
if( fwrite( c, 1, len, f ) != len )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
char buf[1024];
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
||||
mbedtls_pk_context key;
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
|
||||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
mbedtls_pk_init( &key );
|
||||
memset( buf, 0, sizeof( buf ) );
|
||||
|
||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
opt.mode = DFL_MODE;
|
||||
opt.filename = DFL_FILENAME;
|
||||
opt.output_mode = DFL_OUTPUT_MODE;
|
||||
opt.output_file = DFL_OUTPUT_FILENAME;
|
||||
opt.output_format = DFL_OUTPUT_FORMAT;
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
{
|
||||
p = argv[i];
|
||||
if( ( q = strchr( p, '=' ) ) == NULL )
|
||||
goto usage;
|
||||
*q++ = '\0';
|
||||
|
||||
if( strcmp( p, "mode" ) == 0 )
|
||||
{
|
||||
if( strcmp( q, "private" ) == 0 )
|
||||
opt.mode = MODE_PRIVATE;
|
||||
else if( strcmp( q, "public" ) == 0 )
|
||||
opt.mode = MODE_PUBLIC;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "output_mode" ) == 0 )
|
||||
{
|
||||
if( strcmp( q, "private" ) == 0 )
|
||||
opt.output_mode = OUTPUT_MODE_PRIVATE;
|
||||
else if( strcmp( q, "public" ) == 0 )
|
||||
opt.output_mode = OUTPUT_MODE_PUBLIC;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "output_format" ) == 0 )
|
||||
{
|
||||
#if defined(MBEDTLS_PEM_WRITE_C)
|
||||
if( strcmp( q, "pem" ) == 0 )
|
||||
opt.output_format = OUTPUT_FORMAT_PEM;
|
||||
else
|
||||
#endif
|
||||
if( strcmp( q, "der" ) == 0 )
|
||||
opt.output_format = OUTPUT_FORMAT_DER;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
else if( strcmp( p, "filename" ) == 0 )
|
||||
opt.filename = q;
|
||||
else if( strcmp( p, "output_file" ) == 0 )
|
||||
opt.output_file = q;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
|
||||
if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE )
|
||||
{
|
||||
mbedtls_printf( "\nCannot output a key without reading one.\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE )
|
||||
{
|
||||
mbedtls_printf( "\nCannot output a private key from a public key.\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( opt.mode == MODE_PRIVATE )
|
||||
{
|
||||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
mbedtls_printf( "\n . Loading the private key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
mbedtls_printf( " . Key information ...\n" );
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
|
||||
|
||||
if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
|
||||
( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
|
||||
mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
|
||||
mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
|
||||
mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
|
||||
mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
mbedtls_printf("key type not supported yet\n");
|
||||
|
||||
}
|
||||
else if( opt.mode == MODE_PUBLIC )
|
||||
{
|
||||
/*
|
||||
* 1.1. Load the key
|
||||
*/
|
||||
mbedtls_printf( "\n . Loading the public key ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2 Print the key
|
||||
*/
|
||||
mbedtls_printf( " . Key information ...\n" );
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
|
||||
{
|
||||
mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
|
||||
|
||||
if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL,
|
||||
NULL, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
|
||||
mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
|
||||
{
|
||||
mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
|
||||
mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
|
||||
mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
|
||||
}
|
||||
else
|
||||
#endif
|
||||
mbedtls_printf("key type not supported yet\n");
|
||||
}
|
||||
else
|
||||
goto usage;
|
||||
|
||||
if( opt.output_mode == OUTPUT_MODE_PUBLIC )
|
||||
{
|
||||
write_public_key( &key, opt.output_file );
|
||||
}
|
||||
if( opt.output_mode == OUTPUT_MODE_PRIVATE )
|
||||
{
|
||||
write_private_key( &key, opt.output_file );
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, sizeof( buf ) );
|
||||
mbedtls_printf( " - %s\n", buf );
|
||||
#else
|
||||
mbedtls_printf("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
|
||||
mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
|
||||
|
||||
mbedtls_pk_free( &key );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */
|
128
third_party/mbedtls/crypto/programs/pkey/mpi_demo.c
vendored
Normal file
128
third_party/mbedtls/crypto/programs/pkey/mpi_demo.c
vendored
Normal file
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Simple MPI demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/bignum.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( void )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_mpi E, P, Q, N, H, D, X, Y, Z;
|
||||
|
||||
mbedtls_mpi_init( &E ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &N );
|
||||
mbedtls_mpi_init( &H ); mbedtls_mpi_init( &D ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
|
||||
mbedtls_mpi_init( &Z );
|
||||
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P, 10, "2789" ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &Q, 10, "3203" ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 10, "257" ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &N, &P, &Q ) );
|
||||
|
||||
mbedtls_printf( "\n Public key:\n\n" );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " N = ", &N, 10, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " E = ", &E, 10, NULL ) );
|
||||
|
||||
mbedtls_printf( "\n Private key:\n\n" );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " P = ", &P, 10, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Q = ", &Q, 10, NULL ) );
|
||||
|
||||
#if defined(MBEDTLS_GENPRIME)
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P, &P, 1 ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q, &Q, 1 ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P, &Q ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &D, &E, &H ) );
|
||||
|
||||
mbedtls_mpi_write_file( " D = E^-1 mod (P-1)*(Q-1) = ",
|
||||
&D, 10, NULL );
|
||||
#else
|
||||
mbedtls_printf("\nTest skipped (MBEDTLS_GENPRIME not defined).\n\n");
|
||||
#endif
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &X, 10, "55555" ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &Y, &X, &E, &N, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &Z, &Y, &D, &N, NULL ) );
|
||||
|
||||
mbedtls_printf( "\n RSA operation:\n\n" );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " X (plaintext) = ", &X, 10, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Y (ciphertext) = X^E mod N = ", &Y, 10, NULL ) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( " Z (decrypted) = Y^D mod N = ", &Z, 10, NULL ) );
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
mbedtls_mpi_free( &E ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &N );
|
||||
mbedtls_mpi_free( &H ); mbedtls_mpi_free( &D ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
|
||||
mbedtls_mpi_free( &Z );
|
||||
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_printf( "\nAn error occurred.\n" );
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_FS_IO */
|
189
third_party/mbedtls/crypto/programs/pkey/pk_decrypt.c
vendored
Normal file
189
third_party/mbedtls/crypto/programs/pkey/pk_decrypt.c
vendored
Normal file
@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Public key-based simple decryption program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
|
||||
"MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1, c;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i, olen = 0;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char result[1024];
|
||||
unsigned char buf[512];
|
||||
const char *pers = "mbedtls_pk_decrypt";
|
||||
((void) argv);
|
||||
|
||||
mbedtls_pk_init( &pk );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
|
||||
memset(result, 0, sizeof( result ) );
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "usage: mbedtls_pk_decrypt <key_file>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract the RSA encrypted value from the text file
|
||||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
while( fscanf( f, "%02X", &c ) > 0 &&
|
||||
i < (int) sizeof( buf ) )
|
||||
{
|
||||
buf[i++] = (unsigned char) c;
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* Decrypt the encrypted RSA data and print the result.
|
||||
*/
|
||||
mbedtls_printf( "\n . Decrypting the encrypted data" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . OK\n\n" );
|
||||
|
||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
|
193
third_party/mbedtls/crypto/programs/pkey/pk_encrypt.c
vendored
Normal file
193
third_party/mbedtls/crypto/programs/pkey/pk_encrypt.c
vendored
Normal file
@ -0,0 +1,193 @@
|
||||
/*
|
||||
* RSA simple data encryption program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i, olen = 0;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char input[1024];
|
||||
unsigned char buf[512];
|
||||
const char *pers = "mbedtls_pk_encrypt";
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
mbedtls_printf( "usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( strlen( argv[2] ) > 100 )
|
||||
{
|
||||
mbedtls_printf( " Input data larger than 100 characters.\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( input, argv[2], strlen( argv[2] ) );
|
||||
|
||||
/*
|
||||
* Calculate the RSA encryption of the hash.
|
||||
*/
|
||||
mbedtls_printf( "\n . Generating the encrypted value" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_encrypt( &pk, input, strlen( argv[2] ),
|
||||
buf, &olen, sizeof(buf),
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the signature into result-enc.txt
|
||||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n",
|
||||
"result-enc.txt" );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < olen; i++ )
|
||||
{
|
||||
mbedtls_fprintf( f, "%02X%s", buf[i],
|
||||
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
192
third_party/mbedtls/crypto/programs/pkey/pk_sign.c
vendored
Normal file
192
third_party/mbedtls/crypto/programs/pkey/pk_sign.c
vendored
Normal file
@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Public key-based signature creation program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
|
||||
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/pk.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char hash[32];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
const char *pers = "mbedtls_pk_sign";
|
||||
size_t olen = 0;
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
mbedtls_printf( "usage: mbedtls_pk_sign <key_file> <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not parse '%s'\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the SHA-256 hash of the input file,
|
||||
* then calculate the signature of the hash.
|
||||
*/
|
||||
mbedtls_printf( "\n . Generating the SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_md_file(
|
||||
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
|
||||
argv[2], hash ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_sign returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the signature into <filename>.sig
|
||||
*/
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( fwrite( buf, 1, olen, f ) != olen )
|
||||
{
|
||||
mbedtls_printf( "failed\n ! fwrite failed\n\n" );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_CTR_DRBG_C */
|
165
third_party/mbedtls/crypto/programs/pkey/pk_verify.c
vendored
Normal file
165
third_party/mbedtls/crypto/programs/pkey/pk_verify.c
vendored
Normal file
@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Public key-based signature verification program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_PK_PARSE_C) || \
|
||||
!defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_MD_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_PK_PARSE_C and/or "
|
||||
"MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/pk.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_pk_context pk;
|
||||
unsigned char hash[32];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
mbedtls_printf( "usage: mbedtls_pk_verify <key_file> <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract the signature from the file
|
||||
*/
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
||||
i = fread( buf, 1, sizeof(buf), f );
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* Compute the SHA-256 hash of the input file and
|
||||
* verify the signature
|
||||
*/
|
||||
mbedtls_printf( "\n . Verifying the SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_md_file(
|
||||
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
|
||||
argv[2], hash ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
|
||||
buf, i ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_verify returned -0x%04x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C)
|
||||
if( exit_code != MBEDTLS_EXIT_SUCCESS )
|
||||
{
|
||||
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
|
||||
mbedtls_printf( " ! Last error was: %s\n", buf );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
223
third_party/mbedtls/crypto/programs/pkey/rsa_decrypt.c
vendored
Normal file
223
third_party/mbedtls/crypto/programs/pkey/rsa_decrypt.c
vendored
Normal file
@ -0,0 +1,223 @@
|
||||
/*
|
||||
* RSA simple decryption program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
int c;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char result[1024];
|
||||
unsigned char buf[512];
|
||||
const char *pers = "rsa_decrypt";
|
||||
((void) argv);
|
||||
|
||||
memset(result, 0, sizeof( result ) );
|
||||
|
||||
if( argc != 1 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_decrypt\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
mbedtls_exit( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
|
||||
ret );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Extract the RSA encrypted value from the text file
|
||||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
|
||||
while( fscanf( f, "%02X", &c ) > 0 &&
|
||||
i < (int) sizeof( buf ) )
|
||||
buf[i++] = (unsigned char) c;
|
||||
|
||||
fclose( f );
|
||||
|
||||
if( i != rsa.len )
|
||||
{
|
||||
mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Decrypt the encrypted RSA data and print the result.
|
||||
*/
|
||||
mbedtls_printf( "\n . Decrypting the encrypted data" );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_rsa_pkcs1_decrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
&ctr_drbg, MBEDTLS_RSA_PRIVATE, &i,
|
||||
buf, result, 1024 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_decrypt returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . OK\n\n" );
|
||||
|
||||
mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
mbedtls_rsa_free( &rsa );
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
|
||||
mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_FS_IO */
|
201
third_party/mbedtls/crypto/programs/pkey/rsa_encrypt.c
vendored
Normal file
201
third_party/mbedtls/crypto/programs/pkey/rsa_encrypt.c
vendored
Normal file
@ -0,0 +1,201 @@
|
||||
/*
|
||||
* RSA simple data encryption program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_RSA_C) && \
|
||||
defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) && \
|
||||
defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char input[1024];
|
||||
unsigned char buf[512];
|
||||
const char *pers = "rsa_encrypt";
|
||||
mbedtls_mpi N, E;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_encrypt <string of max 100 characters>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
mbedtls_exit( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_entropy_init( &entropy );
|
||||
|
||||
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
|
||||
&entropy, (const unsigned char *) pers,
|
||||
strlen( pers ) );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_read_file( &N, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &E, 16, f ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n",
|
||||
ret );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
if( ( ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( strlen( argv[1] ) > 100 )
|
||||
{
|
||||
mbedtls_printf( " Input data larger than 100 characters.\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
memcpy( input, argv[1], strlen( argv[1] ) );
|
||||
|
||||
/*
|
||||
* Calculate the RSA encryption of the hash.
|
||||
*/
|
||||
mbedtls_printf( "\n . Generating the RSA encrypted value" );
|
||||
fflush( stdout );
|
||||
|
||||
ret = mbedtls_rsa_pkcs1_encrypt( &rsa, mbedtls_ctr_drbg_random,
|
||||
&ctr_drbg, MBEDTLS_RSA_PUBLIC,
|
||||
strlen( argv[1] ), input, buf );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the signature into result-enc.txt
|
||||
*/
|
||||
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < rsa.len; i++ )
|
||||
mbedtls_fprintf( f, "%02X%s", buf[i],
|
||||
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
|
||||
|
||||
fclose( f );
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
mbedtls_rsa_free( &rsa );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_ENTROPY_C &&
|
||||
MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
189
third_party/mbedtls/crypto/programs/pkey/rsa_genkey.c
vendored
Normal file
189
third_party/mbedtls/crypto/programs/pkey/rsa_genkey.c
vendored
Normal file
@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Example RSA key generation program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
|
||||
defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define KEY_SIZE 2048
|
||||
#define EXPONENT 65537
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
|
||||
!defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
|
||||
"MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( void )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
FILE *fpub = NULL;
|
||||
FILE *fpriv = NULL;
|
||||
const char *pers = "rsa_genkey";
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
|
||||
EXPONENT ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
|
||||
( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
mbedtls_printf( " ok\n\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
if( fpub != NULL )
|
||||
fclose( fpub );
|
||||
|
||||
if( fpriv != NULL )
|
||||
fclose( fpriv );
|
||||
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
|
||||
mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
|
||||
mbedtls_rsa_free( &rsa );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
|
||||
MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
|
8
third_party/mbedtls/crypto/programs/pkey/rsa_priv.txt
vendored
Normal file
8
third_party/mbedtls/crypto/programs/pkey/rsa_priv.txt
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
N = A1D46FBA2318F8DCEF16C280948B1CF27966B9B47225ED2989F8D74B45BD36049C0AAB5AD0FF003553BA843C8E12782FC5873BB89A3DC84B883D25666CD22BF3ACD5B675969F8BEBFBCAC93FDD927C7442B178B10D1DFF9398E52316AAE0AF74E594650BDC3C670241D418684593CDA1A7B9DC4F20D2FDC6F66344074003E211
|
||||
E = 010001
|
||||
D = 589552BB4F2F023ADDDD5586D0C8FD857512D82080436678D07F984A29D892D31F1F7000FC5A39A0F73E27D885E47249A4148C8A5653EF69F91F8F736BA9F84841C2D99CD8C24DE8B72B5C9BE0EDBE23F93D731749FEA9CFB4A48DD2B7F35A2703E74AA2D4DB7DE9CEEA7D763AF0ADA7AC176C4E9A22C4CDA65CEC0C65964401
|
||||
P = CD083568D2D46C44C40C1FA0101AF2155E59C70B08423112AF0C1202514BBA5210765E29FF13036F56C7495894D80CF8C3BAEE2839BACBB0B86F6A2965F60DB1
|
||||
Q = CA0EEEA5E710E8E9811A6B846399420E3AE4A4C16647E426DDF8BBBCB11CD3F35CE2E4B6BCAD07AE2C0EC2ECBFCC601B207CDD77B5673E16382B1130BF465261
|
||||
DP = 0D0E21C07BF434B4A83B116472C2147A11D8EB98A33CFBBCF1D275EF19D815941622435AAF3839B6C432CA53CE9E772CFBE1923A937A766FD93E96E6EDEC1DF1
|
||||
DQ = 269CEBE6305DFEE4809377F078C814E37B45AE6677114DFC4F76F5097E1F3031D592567AC55B9B98213B40ECD54A4D2361F5FAACA1B1F51F71E4690893C4F081
|
||||
QP = 97AC5BB885ABCA314375E9E4DB1BA4B2218C90619F61BD474F5785075ECA81750A735199A8C191FE2D3355E7CF601A70E5CABDE0E02C2538BB9FB4871540B3C1
|
2
third_party/mbedtls/crypto/programs/pkey/rsa_pub.txt
vendored
Normal file
2
third_party/mbedtls/crypto/programs/pkey/rsa_pub.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
N = A1D46FBA2318F8DCEF16C280948B1CF27966B9B47225ED2989F8D74B45BD36049C0AAB5AD0FF003553BA843C8E12782FC5873BB89A3DC84B883D25666CD22BF3ACD5B675969F8BEBFBCAC93FDD927C7442B178B10D1DFF9398E52316AAE0AF74E594650BDC3C670241D418684593CDA1A7B9DC4F20D2FDC6F66344074003E211
|
||||
E = 010001
|
205
third_party/mbedtls/crypto/programs/pkey/rsa_sign.c
vendored
Normal file
205
third_party/mbedtls/crypto/programs/pkey/rsa_sign.c
vendored
Normal file
@ -0,0 +1,205 @@
|
||||
/*
|
||||
* RSA/SHA-256 signature creation program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_MD_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/md.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
unsigned char hash[32];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
|
||||
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
|
||||
mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
|
||||
mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_sign <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_read_file( &N , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &E , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &D , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &P , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &Q , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &DP , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &DQ , 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &QP , 16, f ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
fclose( f );
|
||||
|
||||
if( ( ret = mbedtls_rsa_import( &rsa, &N, &P, &Q, &D, &E ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_import returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_rsa_complete( &rsa ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_complete returned %d\n\n",
|
||||
ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Checking the private key" );
|
||||
fflush( stdout );
|
||||
if( ( ret = mbedtls_rsa_check_privkey( &rsa ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_check_privkey failed with -0x%0x\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the SHA-256 hash of the input file,
|
||||
* then calculate the RSA signature of the hash.
|
||||
*/
|
||||
mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_md_file(
|
||||
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
|
||||
argv[1], hash ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
|
||||
20, hash, buf ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned -0x%0x\n\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the signature into <filename>.sig
|
||||
*/
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
|
||||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
for( i = 0; i < rsa.len; i++ )
|
||||
mbedtls_fprintf( f, "%02X%s", buf[i],
|
||||
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
|
||||
|
||||
fclose( f );
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_rsa_free( &rsa );
|
||||
mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
|
||||
mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
|
||||
mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO */
|
193
third_party/mbedtls/crypto/programs/pkey/rsa_sign_pss.c
vendored
Normal file
193
third_party/mbedtls/crypto/programs/pkey/rsa_sign_pss.c
vendored
Normal file
@ -0,0 +1,193 @@
|
||||
/*
|
||||
* RSASSA-PSS/SHA-256 signature creation program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
|
||||
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/pk.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_pk_context pk;
|
||||
mbedtls_entropy_context entropy;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
unsigned char hash[32];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
const char *pers = "rsa_sign_pss";
|
||||
size_t olen = 0;
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
mbedtls_pk_init( &pk );
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_sign_pss <key_file> <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Seeding the random number generator..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
|
||||
(const unsigned char *) pers,
|
||||
strlen( pers ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
|
||||
mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
|
||||
|
||||
/*
|
||||
* Compute the SHA-256 hash of the input file,
|
||||
* then calculate the RSA signature of the hash.
|
||||
*/
|
||||
mbedtls_printf( "\n . Generating the RSA/SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_md_file(
|
||||
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
|
||||
argv[2], hash ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, &olen,
|
||||
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_sign returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write the signature into <filename>.sig
|
||||
*/
|
||||
mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not create %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( fwrite( buf, 1, olen, f ) != olen )
|
||||
{
|
||||
mbedtls_printf( "failed\n ! fwrite failed\n\n" );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
mbedtls_printf( "\n . Done (created \"%s\")\n\n", filename );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
|
||||
MBEDTLS_SHA256_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
|
||||
MBEDTLS_CTR_DRBG_C */
|
178
third_party/mbedtls/crypto/programs/pkey/rsa_verify.c
vendored
Normal file
178
third_party/mbedtls/crypto/programs/pkey/rsa_verify.c
vendored
Normal file
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* RSA/SHA-256 signature verification program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_RSA_C) || \
|
||||
!defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_RSA_C and/or "
|
||||
"MBEDTLS_MD_C and/or "
|
||||
"MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/md.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1, c;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_rsa_context rsa;
|
||||
unsigned char hash[32];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
|
||||
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_verify <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
|
||||
" ! Please run rsa_genkey first\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
|
||||
( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
|
||||
fclose( f );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* Extract the RSA signature from the text file
|
||||
*/
|
||||
mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[1] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
while( fscanf( f, "%02X", &c ) > 0 &&
|
||||
i < (int) sizeof( buf ) )
|
||||
buf[i++] = (unsigned char) c;
|
||||
|
||||
fclose( f );
|
||||
|
||||
if( i != rsa.len )
|
||||
{
|
||||
mbedtls_printf( "\n ! Invalid RSA signature format\n\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute the SHA-256 hash of the input file and
|
||||
* verify the signature
|
||||
*/
|
||||
mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_md_file(
|
||||
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
|
||||
argv[1], hash ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[1] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
|
||||
MBEDTLS_MD_SHA256, 20, hash, buf ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned -0x%0x\n\n", -ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
|
||||
mbedtls_rsa_free( &rsa );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_FS_IO */
|
169
third_party/mbedtls/crypto/programs/pkey/rsa_verify_pss.c
vendored
Normal file
169
third_party/mbedtls/crypto/programs/pkey/rsa_verify_pss.c
vendored
Normal file
@ -0,0 +1,169 @@
|
||||
/*
|
||||
* RSASSA-PSS/SHA-256 signature verification program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if !defined(MBEDTLS_MD_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
|
||||
!defined(MBEDTLS_CTR_DRBG_C)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_MD_C and/or MBEDTLS_ENTROPY_C and/or "
|
||||
"MBEDTLS_RSA_C and/or MBEDTLS_SHA256_C and/or "
|
||||
"MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO and/or "
|
||||
"MBEDTLS_CTR_DRBG_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/pem.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/md.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
size_t i;
|
||||
mbedtls_pk_context pk;
|
||||
unsigned char hash[32];
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
|
||||
char filename[512];
|
||||
|
||||
mbedtls_pk_init( &pk );
|
||||
|
||||
if( argc != 3 )
|
||||
{
|
||||
mbedtls_printf( "usage: rsa_verify_pss <key_file> <filename>\n" );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( "\n" );
|
||||
#endif
|
||||
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . Reading public key from '%s'", argv[1] );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not read key from '%s'\n", argv[1] );
|
||||
mbedtls_printf( " ! mbedtls_pk_parse_public_keyfile returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( !mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Key is not an RSA key\n" );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_rsa_set_padding( mbedtls_pk_rsa( pk ), MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256 );
|
||||
|
||||
/*
|
||||
* Extract the RSA signature from the file
|
||||
*/
|
||||
mbedtls_snprintf( filename, 512, "%s.sig", argv[2] );
|
||||
|
||||
if( ( f = fopen( filename, "rb" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "\n ! Could not open %s\n\n", filename );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
|
||||
i = fread( buf, 1, MBEDTLS_MPI_MAX_SIZE, f );
|
||||
|
||||
fclose( f );
|
||||
|
||||
/*
|
||||
* Compute the SHA-256 hash of the input file and
|
||||
* verify the signature
|
||||
*/
|
||||
mbedtls_printf( "\n . Verifying the RSA/SHA-256 signature" );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = mbedtls_md_file(
|
||||
mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ),
|
||||
argv[2], hash ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0,
|
||||
buf, i ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_pk_verify returned %d\n\n", ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( "\n . OK (the signature is valid)\n\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free( &pk );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
|
||||
MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */
|
26
third_party/mbedtls/crypto/programs/psa/CMakeLists.txt
vendored
Normal file
26
third_party/mbedtls/crypto/programs/psa/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
add_executable(crypto_examples crypto_examples.c)
|
||||
target_link_libraries(crypto_examples mbedcrypto)
|
||||
|
||||
add_executable(key_ladder_demo key_ladder_demo.c)
|
||||
target_link_libraries(key_ladder_demo mbedcrypto)
|
||||
|
||||
add_executable(psa_constant_names psa_constant_names.c)
|
||||
target_link_libraries(psa_constant_names mbedcrypto)
|
||||
|
||||
add_custom_target(
|
||||
psa_constant_names_generated
|
||||
COMMAND ${PYTHON_EXECUTABLE} scripts/generate_psa_constants.py
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../
|
||||
)
|
||||
add_dependencies(psa_constant_names psa_constant_names_generated)
|
||||
|
||||
install(TARGETS
|
||||
crypto_examples
|
||||
key_ladder_demo
|
||||
psa_constant_names
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
||||
|
||||
install(PROGRAMS
|
||||
key_ladder_demo.sh
|
||||
DESTINATION "bin")
|
343
third_party/mbedtls/crypto/programs/psa/crypto_examples.c
vendored
Normal file
343
third_party/mbedtls/crypto/programs/psa/crypto_examples.c
vendored
Normal file
@ -0,0 +1,343 @@
|
||||
#include "psa/crypto.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ASSERT( predicate ) \
|
||||
do \
|
||||
{ \
|
||||
if( ! ( predicate ) ) \
|
||||
{ \
|
||||
printf( "\tassertion failed at %s:%d - '%s'\r\n", \
|
||||
__FILE__, __LINE__, #predicate); \
|
||||
goto exit; \
|
||||
} \
|
||||
} while ( 0 )
|
||||
|
||||
#define ASSERT_STATUS( actual, expected ) \
|
||||
do \
|
||||
{ \
|
||||
if( ( actual ) != ( expected ) ) \
|
||||
{ \
|
||||
printf( "\tassertion failed at %s:%d - " \
|
||||
"actual:%d expected:%d\r\n", __FILE__, __LINE__, \
|
||||
(psa_status_t) actual, (psa_status_t) expected ); \
|
||||
goto exit; \
|
||||
} \
|
||||
} while ( 0 )
|
||||
|
||||
#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_AES_C) || \
|
||||
!defined(MBEDTLS_CIPHER_MODE_CBC) || !defined(MBEDTLS_CIPHER_MODE_CTR) || \
|
||||
!defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
|
||||
int main( void )
|
||||
{
|
||||
printf( "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_AES_C and/or "
|
||||
"MBEDTLS_CIPHER_MODE_CBC and/or MBEDTLS_CIPHER_MODE_CTR "
|
||||
"and/or MBEDTLS_CIPHER_MODE_WITH_PADDING "
|
||||
"not defined.\r\n" );
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
static psa_status_t set_key_policy( psa_key_handle_t key_handle,
|
||||
psa_key_usage_t key_usage,
|
||||
psa_algorithm_t alg )
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
|
||||
|
||||
psa_key_policy_set_usage( &policy, key_usage, alg );
|
||||
status = psa_set_key_policy( key_handle, &policy );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
exit:
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_operation( psa_cipher_operation_t *operation,
|
||||
const uint8_t * input,
|
||||
size_t input_size,
|
||||
size_t part_size,
|
||||
uint8_t * output,
|
||||
size_t output_size,
|
||||
size_t *output_len )
|
||||
{
|
||||
psa_status_t status;
|
||||
size_t bytes_to_write = 0, bytes_written = 0, len = 0;
|
||||
|
||||
*output_len = 0;
|
||||
while( bytes_written != input_size )
|
||||
{
|
||||
bytes_to_write = ( input_size - bytes_written > part_size ?
|
||||
part_size :
|
||||
input_size - bytes_written );
|
||||
|
||||
status = psa_cipher_update( operation, input + bytes_written,
|
||||
bytes_to_write, output + *output_len,
|
||||
output_size - *output_len, &len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
bytes_written += bytes_to_write;
|
||||
*output_len += len;
|
||||
}
|
||||
|
||||
status = psa_cipher_finish( operation, output + *output_len,
|
||||
output_size - *output_len, &len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
*output_len += len;
|
||||
|
||||
exit:
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_encrypt( psa_key_handle_t key_handle,
|
||||
psa_algorithm_t alg,
|
||||
uint8_t * iv,
|
||||
size_t iv_size,
|
||||
const uint8_t * input,
|
||||
size_t input_size,
|
||||
size_t part_size,
|
||||
uint8_t * output,
|
||||
size_t output_size,
|
||||
size_t *output_len )
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
||||
size_t iv_len = 0;
|
||||
|
||||
memset( &operation, 0, sizeof( operation ) );
|
||||
status = psa_cipher_encrypt_setup( &operation, key_handle, alg );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_cipher_generate_iv( &operation, iv, iv_size, &iv_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_operation( &operation, input, input_size, part_size,
|
||||
output, output_size, output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_cipher_abort( &operation );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_decrypt( psa_key_handle_t key_handle,
|
||||
psa_algorithm_t alg,
|
||||
const uint8_t * iv,
|
||||
size_t iv_size,
|
||||
const uint8_t * input,
|
||||
size_t input_size,
|
||||
size_t part_size,
|
||||
uint8_t * output,
|
||||
size_t output_size,
|
||||
size_t *output_len )
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
|
||||
|
||||
memset( &operation, 0, sizeof( operation ) );
|
||||
status = psa_cipher_decrypt_setup( &operation, key_handle, alg );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_cipher_set_iv( &operation, iv, iv_size );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_operation( &operation, input, input_size, part_size,
|
||||
output, output_size, output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_cipher_abort( &operation );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t
|
||||
cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( void )
|
||||
{
|
||||
enum {
|
||||
block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
|
||||
key_bits = 256,
|
||||
part_size = block_size,
|
||||
};
|
||||
const psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
|
||||
|
||||
psa_status_t status;
|
||||
psa_key_handle_t key_handle = 0;
|
||||
size_t output_len = 0;
|
||||
uint8_t iv[block_size];
|
||||
uint8_t input[block_size];
|
||||
uint8_t encrypt[block_size];
|
||||
uint8_t decrypt[block_size];
|
||||
|
||||
status = psa_generate_random( input, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_allocate_key( &key_handle );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = set_key_policy( key_handle,
|
||||
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
|
||||
alg );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_generate_key( key_handle, PSA_KEY_TYPE_AES, key_bits,
|
||||
NULL, 0 );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
input, sizeof( input ), part_size,
|
||||
encrypt, sizeof( encrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
encrypt, output_len, part_size,
|
||||
decrypt, sizeof( decrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = memcmp( input, decrypt, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_destroy_key( key_handle );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( void )
|
||||
{
|
||||
enum {
|
||||
block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
|
||||
key_bits = 256,
|
||||
input_size = 100,
|
||||
part_size = 10,
|
||||
};
|
||||
|
||||
const psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
|
||||
|
||||
psa_status_t status;
|
||||
psa_key_handle_t key_handle = 0;
|
||||
size_t output_len = 0;
|
||||
uint8_t iv[block_size], input[input_size],
|
||||
encrypt[input_size + block_size], decrypt[input_size + block_size];
|
||||
|
||||
status = psa_generate_random( input, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_allocate_key( &key_handle );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = set_key_policy( key_handle,
|
||||
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
|
||||
alg );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_generate_key( key_handle, PSA_KEY_TYPE_AES, key_bits,
|
||||
NULL, 0 );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
input, sizeof( input ), part_size,
|
||||
encrypt, sizeof( encrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
encrypt, output_len, part_size,
|
||||
decrypt, sizeof( decrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = memcmp( input, decrypt, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_destroy_key( key_handle );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi( void )
|
||||
{
|
||||
enum {
|
||||
block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
|
||||
key_bits = 256,
|
||||
input_size = 100,
|
||||
part_size = 10,
|
||||
};
|
||||
const psa_algorithm_t alg = PSA_ALG_CTR;
|
||||
|
||||
psa_status_t status;
|
||||
psa_key_handle_t key_handle = 0;
|
||||
size_t output_len = 0;
|
||||
uint8_t iv[block_size], input[input_size], encrypt[input_size],
|
||||
decrypt[input_size];
|
||||
|
||||
status = psa_generate_random( input, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_allocate_key( &key_handle );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
status = set_key_policy( key_handle,
|
||||
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
|
||||
alg );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = psa_generate_key( key_handle, PSA_KEY_TYPE_AES, key_bits,
|
||||
NULL, 0 );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
input, sizeof( input ), part_size,
|
||||
encrypt, sizeof( encrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
|
||||
encrypt, output_len, part_size,
|
||||
decrypt, sizeof( decrypt ), &output_len );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
status = memcmp( input, decrypt, sizeof( input ) );
|
||||
ASSERT_STATUS( status, PSA_SUCCESS );
|
||||
|
||||
exit:
|
||||
psa_destroy_key( key_handle );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static void cipher_examples( void )
|
||||
{
|
||||
psa_status_t status;
|
||||
|
||||
printf( "cipher encrypt/decrypt AES CBC no padding:\r\n" );
|
||||
status = cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( );
|
||||
if( status == PSA_SUCCESS )
|
||||
printf( "\tsuccess!\r\n" );
|
||||
|
||||
printf( "cipher encrypt/decrypt AES CBC PKCS7 multipart:\r\n" );
|
||||
status = cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( );
|
||||
if( status == PSA_SUCCESS )
|
||||
printf( "\tsuccess!\r\n" );
|
||||
|
||||
printf( "cipher encrypt/decrypt AES CTR multipart:\r\n" );
|
||||
status = cipher_example_encrypt_decrypt_aes_ctr_multi( );
|
||||
if( status == PSA_SUCCESS )
|
||||
printf( "\tsuccess!\r\n" );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
exit( EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( void )
|
||||
{
|
||||
ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
||||
cipher_examples( );
|
||||
exit:
|
||||
mbedtls_psa_crypto_free( );
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC &&
|
||||
MBEDTLS_CIPHER_MODE_CTR && MBEDTLS_CIPHER_MODE_WITH_PADDING */
|
716
third_party/mbedtls/crypto/programs/psa/key_ladder_demo.c
vendored
Normal file
716
third_party/mbedtls/crypto/programs/psa/key_ladder_demo.c
vendored
Normal file
@ -0,0 +1,716 @@
|
||||
/**
|
||||
* PSA API key derivation demonstration
|
||||
*
|
||||
* This program calculates a key ladder: a chain of secret material, each
|
||||
* derived from the previous one in a deterministic way based on a label.
|
||||
* Two keys are identical if and only if they are derived from the same key
|
||||
* using the same label.
|
||||
*
|
||||
* The initial key is called the master key. The master key is normally
|
||||
* randomly generated, but it could itself be derived from another key.
|
||||
*
|
||||
* This program derives a series of keys called intermediate keys.
|
||||
* The first intermediate key is derived from the master key using the
|
||||
* first label passed on the command line. Each subsequent intermediate
|
||||
* key is derived from the previous one using the next label passed
|
||||
* on the command line.
|
||||
*
|
||||
* This program has four modes of operation:
|
||||
*
|
||||
* - "generate": generate a random master key.
|
||||
* - "wrap": derive a wrapping key from the last intermediate key,
|
||||
* and use that key to encrypt-and-authenticate some data.
|
||||
* - "unwrap": derive a wrapping key from the last intermediate key,
|
||||
* and use that key to decrypt-and-authenticate some
|
||||
* ciphertext created by wrap mode.
|
||||
* - "save": save the last intermediate key so that it can be reused as
|
||||
* the master key in another run of the program.
|
||||
*
|
||||
* See the usage() output for the command line usage. See the file
|
||||
* `key_ladder_demo.sh` for an example run.
|
||||
*/
|
||||
|
||||
/* Copyright (C) 2018, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
/* First include Mbed TLS headers to get the Mbed TLS configuration and
|
||||
* platform definitions that we'll use in this program. Also include
|
||||
* standard C headers for functions we'll use here. */
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mbedtls/platform_util.h" // for mbedtls_platform_zeroize
|
||||
|
||||
/* If the build options we need are not enabled, compile a placeholder. */
|
||||
#if !defined(MBEDTLS_SHA256_C) || !defined(MBEDTLS_MD_C) || \
|
||||
!defined(MBEDTLS_AES_C) || !defined(MBEDTLS_CCM_C) || \
|
||||
!defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
printf("MBEDTLS_SHA256_C and/or MBEDTLS_MD_C and/or "
|
||||
"MBEDTLS_AES_C and/or MBEDTLS_CCM_C and/or "
|
||||
"MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
/* The real program starts here. */
|
||||
|
||||
|
||||
|
||||
#include <psa/crypto.h>
|
||||
|
||||
/* Run a system function and bail out if it fails. */
|
||||
#define SYS_CHECK( expr ) \
|
||||
do \
|
||||
{ \
|
||||
if( ! ( expr ) ) \
|
||||
{ \
|
||||
perror( #expr ); \
|
||||
status = DEMO_ERROR; \
|
||||
goto exit; \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/* Run a PSA function and bail out if it fails. */
|
||||
#define PSA_CHECK( expr ) \
|
||||
do \
|
||||
{ \
|
||||
status = ( expr ); \
|
||||
if( status != PSA_SUCCESS ) \
|
||||
{ \
|
||||
printf( "Error %d at line %u: %s\n", \
|
||||
(int) status, \
|
||||
__LINE__, \
|
||||
#expr ); \
|
||||
goto exit; \
|
||||
} \
|
||||
} \
|
||||
while( 0 )
|
||||
|
||||
/* To report operational errors in this program, use an error code that is
|
||||
* different from every PSA error code. */
|
||||
#define DEMO_ERROR 120
|
||||
|
||||
/* The maximum supported key ladder depth. */
|
||||
#define MAX_LADDER_DEPTH 10
|
||||
|
||||
/* Salt to use when deriving an intermediate key. */
|
||||
#define DERIVE_KEY_SALT ( (uint8_t *) "key_ladder_demo.derive" )
|
||||
#define DERIVE_KEY_SALT_LENGTH ( strlen( (const char*) DERIVE_KEY_SALT ) )
|
||||
|
||||
/* Salt to use when deriving a wrapping key. */
|
||||
#define WRAPPING_KEY_SALT ( (uint8_t *) "key_ladder_demo.wrap" )
|
||||
#define WRAPPING_KEY_SALT_LENGTH ( strlen( (const char*) WRAPPING_KEY_SALT ) )
|
||||
|
||||
/* Size of the key derivation keys (applies both to the master key and
|
||||
* to intermediate keys). */
|
||||
#define KEY_SIZE_BYTES 40
|
||||
|
||||
/* Algorithm for key derivation. */
|
||||
#define KDF_ALG PSA_ALG_HKDF( PSA_ALG_SHA_256 )
|
||||
|
||||
/* Type and size of the key used to wrap data. */
|
||||
#define WRAPPING_KEY_TYPE PSA_KEY_TYPE_AES
|
||||
#define WRAPPING_KEY_BITS 128
|
||||
|
||||
/* Cipher mode used to wrap data. */
|
||||
#define WRAPPING_ALG PSA_ALG_CCM
|
||||
|
||||
/* Nonce size used to wrap data. */
|
||||
#define WRAPPING_IV_SIZE 13
|
||||
|
||||
/* Header used in files containing wrapped data. We'll save this header
|
||||
* directly without worrying about data representation issues such as
|
||||
* integer sizes and endianness, because the data is meant to be read
|
||||
* back by the same program on the same machine. */
|
||||
#define WRAPPED_DATA_MAGIC "key_ladder_demo" // including trailing null byte
|
||||
#define WRAPPED_DATA_MAGIC_LENGTH ( sizeof( WRAPPED_DATA_MAGIC ) )
|
||||
typedef struct
|
||||
{
|
||||
char magic[WRAPPED_DATA_MAGIC_LENGTH];
|
||||
size_t ad_size; /* Size of the additional data, which is this header. */
|
||||
size_t payload_size; /* Size of the encrypted data. */
|
||||
/* Store the IV inside the additional data. It's convenient. */
|
||||
uint8_t iv[WRAPPING_IV_SIZE];
|
||||
} wrapped_data_header_t;
|
||||
|
||||
/* The modes that this program can operate in (see usage). */
|
||||
enum program_mode
|
||||
{
|
||||
MODE_GENERATE,
|
||||
MODE_SAVE,
|
||||
MODE_UNWRAP,
|
||||
MODE_WRAP
|
||||
};
|
||||
|
||||
/* Save a key to a file. In the real world, you may want to export a derived
|
||||
* key sometimes, to share it with another party. */
|
||||
static psa_status_t save_key( psa_key_handle_t key_handle,
|
||||
const char *output_file_name )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
uint8_t key_data[KEY_SIZE_BYTES];
|
||||
size_t key_size;
|
||||
FILE *key_file = NULL;
|
||||
|
||||
PSA_CHECK( psa_export_key( key_handle,
|
||||
key_data, sizeof( key_data ),
|
||||
&key_size ) );
|
||||
SYS_CHECK( ( key_file = fopen( output_file_name, "wb" ) ) != NULL );
|
||||
SYS_CHECK( fwrite( key_data, 1, key_size, key_file ) == key_size );
|
||||
SYS_CHECK( fclose( key_file ) == 0 );
|
||||
key_file = NULL;
|
||||
|
||||
exit:
|
||||
if( key_file != NULL)
|
||||
fclose( key_file );
|
||||
return( status );
|
||||
}
|
||||
|
||||
/* Generate a master key for use in this demo.
|
||||
*
|
||||
* Normally a master key would be non-exportable. For the purpose of this
|
||||
* demo, we want to save it to a file, to avoid relying on the keystore
|
||||
* capability of the PSA crypto library. */
|
||||
static psa_status_t generate( const char *key_file_name )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_handle_t key_handle = 0;
|
||||
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
|
||||
|
||||
PSA_CHECK( psa_allocate_key( &key_handle ) );
|
||||
psa_key_policy_set_usage( &policy,
|
||||
PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT,
|
||||
KDF_ALG );
|
||||
PSA_CHECK( psa_set_key_policy( key_handle, &policy ) );
|
||||
|
||||
PSA_CHECK( psa_generate_key( key_handle,
|
||||
PSA_KEY_TYPE_DERIVE,
|
||||
PSA_BYTES_TO_BITS( KEY_SIZE_BYTES ),
|
||||
NULL, 0 ) );
|
||||
|
||||
PSA_CHECK( save_key( key_handle, key_file_name ) );
|
||||
|
||||
exit:
|
||||
(void) psa_destroy_key( key_handle );
|
||||
return( status );
|
||||
}
|
||||
|
||||
/* Load the master key from a file.
|
||||
*
|
||||
* In the real world, this master key would be stored in an internal memory
|
||||
* and the storage would be managed by the keystore capability of the PSA
|
||||
* crypto library. */
|
||||
static psa_status_t import_key_from_file( psa_key_usage_t usage,
|
||||
psa_algorithm_t alg,
|
||||
const char *key_file_name,
|
||||
psa_key_handle_t *master_key_handle )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
|
||||
uint8_t key_data[KEY_SIZE_BYTES];
|
||||
size_t key_size;
|
||||
FILE *key_file = NULL;
|
||||
unsigned char extra_byte;
|
||||
|
||||
*master_key_handle = 0;
|
||||
|
||||
SYS_CHECK( ( key_file = fopen( key_file_name, "rb" ) ) != NULL );
|
||||
SYS_CHECK( ( key_size = fread( key_data, 1, sizeof( key_data ),
|
||||
key_file ) ) != 0 );
|
||||
if( fread( &extra_byte, 1, 1, key_file ) != 0 )
|
||||
{
|
||||
printf( "Key file too large (max: %u).\n",
|
||||
(unsigned) sizeof( key_data ) );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
SYS_CHECK( fclose( key_file ) == 0 );
|
||||
key_file = NULL;
|
||||
|
||||
PSA_CHECK( psa_allocate_key( master_key_handle ) );
|
||||
psa_key_policy_set_usage( &policy, usage, alg );
|
||||
PSA_CHECK( psa_set_key_policy( *master_key_handle, &policy ) );
|
||||
PSA_CHECK( psa_import_key( *master_key_handle,
|
||||
PSA_KEY_TYPE_DERIVE,
|
||||
key_data, key_size ) );
|
||||
exit:
|
||||
if( key_file != NULL )
|
||||
fclose( key_file );
|
||||
mbedtls_platform_zeroize( key_data, sizeof( key_data ) );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
/* If psa_allocate_key hasn't been called yet or has failed,
|
||||
* *master_key_handle is 0. psa_destroy_key(0) is guaranteed to do
|
||||
* nothing and return PSA_ERROR_INVALID_HANDLE. */
|
||||
(void) psa_destroy_key( *master_key_handle );
|
||||
*master_key_handle = 0;
|
||||
}
|
||||
return( status );
|
||||
}
|
||||
|
||||
/* Derive the intermediate keys, using the list of labels provided on
|
||||
* the command line. On input, *key_handle is a handle to the master key.
|
||||
* This function closes the master key. On successful output, *key_handle
|
||||
* is a handle to the final derived key. */
|
||||
static psa_status_t derive_key_ladder( const char *ladder[],
|
||||
size_t ladder_depth,
|
||||
psa_key_handle_t *key_handle )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
|
||||
psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
|
||||
size_t i;
|
||||
psa_key_policy_set_usage( &policy,
|
||||
PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT,
|
||||
KDF_ALG );
|
||||
|
||||
/* For each label in turn, ... */
|
||||
for( i = 0; i < ladder_depth; i++ )
|
||||
{
|
||||
/* Start deriving material from the master key (if i=0) or from
|
||||
* the current intermediate key (if i>0). */
|
||||
PSA_CHECK( psa_key_derivation(
|
||||
&generator,
|
||||
*key_handle,
|
||||
KDF_ALG,
|
||||
DERIVE_KEY_SALT, DERIVE_KEY_SALT_LENGTH,
|
||||
(uint8_t*) ladder[i], strlen( ladder[i] ),
|
||||
KEY_SIZE_BYTES ) );
|
||||
/* When the parent key is not the master key, destroy it,
|
||||
* since it is no longer needed. */
|
||||
PSA_CHECK( psa_close_key( *key_handle ) );
|
||||
*key_handle = 0;
|
||||
PSA_CHECK( psa_allocate_key( key_handle ) );
|
||||
PSA_CHECK( psa_set_key_policy( *key_handle, &policy ) );
|
||||
/* Use the generator obtained from the parent key to create
|
||||
* the next intermediate key. */
|
||||
PSA_CHECK( psa_generator_import_key(
|
||||
*key_handle,
|
||||
PSA_KEY_TYPE_DERIVE,
|
||||
PSA_BYTES_TO_BITS( KEY_SIZE_BYTES ),
|
||||
&generator ) );
|
||||
PSA_CHECK( psa_generator_abort( &generator ) );
|
||||
}
|
||||
|
||||
exit:
|
||||
psa_generator_abort( &generator );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
psa_close_key( *key_handle );
|
||||
*key_handle = 0;
|
||||
}
|
||||
return( status );
|
||||
}
|
||||
|
||||
/* Derive a wrapping key from the last intermediate key. */
|
||||
static psa_status_t derive_wrapping_key( psa_key_usage_t usage,
|
||||
psa_key_handle_t derived_key_handle,
|
||||
psa_key_handle_t *wrapping_key_handle )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
|
||||
psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
|
||||
|
||||
*wrapping_key_handle = 0;
|
||||
PSA_CHECK( psa_allocate_key( wrapping_key_handle ) );
|
||||
psa_key_policy_set_usage( &policy, usage, WRAPPING_ALG );
|
||||
PSA_CHECK( psa_set_key_policy( *wrapping_key_handle, &policy ) );
|
||||
|
||||
PSA_CHECK( psa_key_derivation(
|
||||
&generator,
|
||||
derived_key_handle,
|
||||
KDF_ALG,
|
||||
WRAPPING_KEY_SALT, WRAPPING_KEY_SALT_LENGTH,
|
||||
NULL, 0,
|
||||
PSA_BITS_TO_BYTES( WRAPPING_KEY_BITS ) ) );
|
||||
PSA_CHECK( psa_generator_import_key(
|
||||
*wrapping_key_handle,
|
||||
PSA_KEY_TYPE_AES,
|
||||
WRAPPING_KEY_BITS,
|
||||
&generator ) );
|
||||
|
||||
exit:
|
||||
psa_generator_abort( &generator );
|
||||
if( status != PSA_SUCCESS )
|
||||
{
|
||||
psa_close_key( *wrapping_key_handle );
|
||||
*wrapping_key_handle = 0;
|
||||
}
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t wrap_data( const char *input_file_name,
|
||||
const char *output_file_name,
|
||||
psa_key_handle_t wrapping_key_handle )
|
||||
{
|
||||
psa_status_t status;
|
||||
FILE *input_file = NULL;
|
||||
FILE *output_file = NULL;
|
||||
long input_position;
|
||||
size_t input_size;
|
||||
size_t buffer_size = 0;
|
||||
unsigned char *buffer = NULL;
|
||||
size_t ciphertext_size;
|
||||
wrapped_data_header_t header;
|
||||
|
||||
/* Find the size of the data to wrap. */
|
||||
SYS_CHECK( ( input_file = fopen( input_file_name, "rb" ) ) != NULL );
|
||||
SYS_CHECK( fseek( input_file, 0, SEEK_END ) == 0 );
|
||||
SYS_CHECK( ( input_position = ftell( input_file ) ) != -1 );
|
||||
#if LONG_MAX > SIZE_MAX
|
||||
if( input_position > SIZE_MAX )
|
||||
{
|
||||
printf( "Input file too large.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
input_size = input_position;
|
||||
buffer_size = PSA_AEAD_ENCRYPT_OUTPUT_SIZE( WRAPPING_ALG, input_size );
|
||||
/* Check for integer overflow. */
|
||||
if( buffer_size < input_size )
|
||||
{
|
||||
printf( "Input file too large.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Load the data to wrap. */
|
||||
SYS_CHECK( fseek( input_file, 0, SEEK_SET ) == 0 );
|
||||
SYS_CHECK( ( buffer = calloc( 1, buffer_size ) ) != NULL );
|
||||
SYS_CHECK( fread( buffer, 1, input_size, input_file ) == input_size );
|
||||
SYS_CHECK( fclose( input_file ) == 0 );
|
||||
input_file = NULL;
|
||||
|
||||
/* Construct a header. */
|
||||
memcpy( &header.magic, WRAPPED_DATA_MAGIC, WRAPPED_DATA_MAGIC_LENGTH );
|
||||
header.ad_size = sizeof( header );
|
||||
header.payload_size = input_size;
|
||||
|
||||
/* Wrap the data. */
|
||||
PSA_CHECK( psa_generate_random( header.iv, WRAPPING_IV_SIZE ) );
|
||||
PSA_CHECK( psa_aead_encrypt( wrapping_key_handle, WRAPPING_ALG,
|
||||
header.iv, WRAPPING_IV_SIZE,
|
||||
(uint8_t *) &header, sizeof( header ),
|
||||
buffer, input_size,
|
||||
buffer, buffer_size,
|
||||
&ciphertext_size ) );
|
||||
|
||||
/* Write the output. */
|
||||
SYS_CHECK( ( output_file = fopen( output_file_name, "wb" ) ) != NULL );
|
||||
SYS_CHECK( fwrite( &header, 1, sizeof( header ),
|
||||
output_file ) == sizeof( header ) );
|
||||
SYS_CHECK( fwrite( buffer, 1, ciphertext_size,
|
||||
output_file ) == ciphertext_size );
|
||||
SYS_CHECK( fclose( output_file ) == 0 );
|
||||
output_file = NULL;
|
||||
|
||||
exit:
|
||||
if( input_file != NULL )
|
||||
fclose( input_file );
|
||||
if( output_file != NULL )
|
||||
fclose( output_file );
|
||||
if( buffer != NULL )
|
||||
mbedtls_platform_zeroize( buffer, buffer_size );
|
||||
free( buffer );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t unwrap_data( const char *input_file_name,
|
||||
const char *output_file_name,
|
||||
psa_key_handle_t wrapping_key_handle )
|
||||
{
|
||||
psa_status_t status;
|
||||
FILE *input_file = NULL;
|
||||
FILE *output_file = NULL;
|
||||
unsigned char *buffer = NULL;
|
||||
size_t ciphertext_size = 0;
|
||||
size_t plaintext_size;
|
||||
wrapped_data_header_t header;
|
||||
unsigned char extra_byte;
|
||||
|
||||
/* Load and validate the header. */
|
||||
SYS_CHECK( ( input_file = fopen( input_file_name, "rb" ) ) != NULL );
|
||||
SYS_CHECK( fread( &header, 1, sizeof( header ),
|
||||
input_file ) == sizeof( header ) );
|
||||
if( memcmp( &header.magic, WRAPPED_DATA_MAGIC,
|
||||
WRAPPED_DATA_MAGIC_LENGTH ) != 0 )
|
||||
{
|
||||
printf( "The input does not start with a valid magic header.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
if( header.ad_size != sizeof( header ) )
|
||||
{
|
||||
printf( "The header size is not correct.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
ciphertext_size =
|
||||
PSA_AEAD_ENCRYPT_OUTPUT_SIZE( WRAPPING_ALG, header.payload_size );
|
||||
/* Check for integer overflow. */
|
||||
if( ciphertext_size < header.payload_size )
|
||||
{
|
||||
printf( "Input file too large.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Load the payload data. */
|
||||
SYS_CHECK( ( buffer = calloc( 1, ciphertext_size ) ) != NULL );
|
||||
SYS_CHECK( fread( buffer, 1, ciphertext_size,
|
||||
input_file ) == ciphertext_size );
|
||||
if( fread( &extra_byte, 1, 1, input_file ) != 0 )
|
||||
{
|
||||
printf( "Extra garbage after ciphertext\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
SYS_CHECK( fclose( input_file ) == 0 );
|
||||
input_file = NULL;
|
||||
|
||||
/* Unwrap the data. */
|
||||
PSA_CHECK( psa_aead_decrypt( wrapping_key_handle, WRAPPING_ALG,
|
||||
header.iv, WRAPPING_IV_SIZE,
|
||||
(uint8_t *) &header, sizeof( header ),
|
||||
buffer, ciphertext_size,
|
||||
buffer, ciphertext_size,
|
||||
&plaintext_size ) );
|
||||
if( plaintext_size != header.payload_size )
|
||||
{
|
||||
printf( "Incorrect payload size in the header.\n" );
|
||||
status = DEMO_ERROR;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* Write the output. */
|
||||
SYS_CHECK( ( output_file = fopen( output_file_name, "wb" ) ) != NULL );
|
||||
SYS_CHECK( fwrite( buffer, 1, plaintext_size,
|
||||
output_file ) == plaintext_size );
|
||||
SYS_CHECK( fclose( output_file ) == 0 );
|
||||
output_file = NULL;
|
||||
|
||||
exit:
|
||||
if( input_file != NULL )
|
||||
fclose( input_file );
|
||||
if( output_file != NULL )
|
||||
fclose( output_file );
|
||||
if( buffer != NULL )
|
||||
mbedtls_platform_zeroize( buffer, ciphertext_size );
|
||||
free( buffer );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static psa_status_t run( enum program_mode mode,
|
||||
const char *key_file_name,
|
||||
const char *ladder[], size_t ladder_depth,
|
||||
const char *input_file_name,
|
||||
const char *output_file_name )
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
psa_key_handle_t derivation_key_handle = 0;
|
||||
psa_key_handle_t wrapping_key_handle = 0;
|
||||
|
||||
/* Initialize the PSA crypto library. */
|
||||
PSA_CHECK( psa_crypto_init( ) );
|
||||
|
||||
/* Generate mode is unlike the others. Generate the master key and exit. */
|
||||
if( mode == MODE_GENERATE )
|
||||
return( generate( key_file_name ) );
|
||||
|
||||
/* Read the master key. */
|
||||
PSA_CHECK( import_key_from_file( PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT,
|
||||
KDF_ALG,
|
||||
key_file_name,
|
||||
&derivation_key_handle ) );
|
||||
|
||||
/* Calculate the derived key for this session. */
|
||||
PSA_CHECK( derive_key_ladder( ladder, ladder_depth,
|
||||
&derivation_key_handle ) );
|
||||
|
||||
switch( mode )
|
||||
{
|
||||
case MODE_SAVE:
|
||||
PSA_CHECK( save_key( derivation_key_handle, output_file_name ) );
|
||||
break;
|
||||
case MODE_UNWRAP:
|
||||
PSA_CHECK( derive_wrapping_key( PSA_KEY_USAGE_DECRYPT,
|
||||
derivation_key_handle,
|
||||
&wrapping_key_handle ) );
|
||||
PSA_CHECK( unwrap_data( input_file_name, output_file_name,
|
||||
wrapping_key_handle ) );
|
||||
break;
|
||||
case MODE_WRAP:
|
||||
PSA_CHECK( derive_wrapping_key( PSA_KEY_USAGE_ENCRYPT,
|
||||
derivation_key_handle,
|
||||
&wrapping_key_handle ) );
|
||||
PSA_CHECK( wrap_data( input_file_name, output_file_name,
|
||||
wrapping_key_handle ) );
|
||||
break;
|
||||
default:
|
||||
/* Unreachable but some compilers don't realize it. */
|
||||
break;
|
||||
}
|
||||
|
||||
exit:
|
||||
/* Close any remaining key. Deinitializing the crypto library would do
|
||||
* this anyway, but explicitly closing handles makes the code easier
|
||||
* to reuse. */
|
||||
(void) psa_close_key( derivation_key_handle );
|
||||
(void) psa_close_key( wrapping_key_handle );
|
||||
/* Deinitialize the PSA crypto library. */
|
||||
mbedtls_psa_crypto_free( );
|
||||
return( status );
|
||||
}
|
||||
|
||||
static void usage( void )
|
||||
{
|
||||
printf( "Usage: key_ladder_demo MODE [OPTION=VALUE]...\n" );
|
||||
printf( "Demonstrate the usage of a key derivation ladder.\n" );
|
||||
printf( "\n" );
|
||||
printf( "Modes:\n" );
|
||||
printf( " generate Generate the master key\n" );
|
||||
printf( " save Save the derived key\n" );
|
||||
printf( " unwrap Unwrap (decrypt) input with the derived key\n" );
|
||||
printf( " wrap Wrap (encrypt) input with the derived key\n" );
|
||||
printf( "\n" );
|
||||
printf( "Options:\n" );
|
||||
printf( " input=FILENAME Input file (required for wrap/unwrap)\n" );
|
||||
printf( " master=FILENAME File containing the master key (default: master.key)\n" );
|
||||
printf( " output=FILENAME Output file (required for save/wrap/unwrap)\n" );
|
||||
printf( " label=TEXT Label for the key derivation.\n" );
|
||||
printf( " This may be repeated multiple times.\n" );
|
||||
printf( " To get the same key, you must use the same master key\n" );
|
||||
printf( " and the same sequence of labels.\n" );
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
exit( EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
const char *key_file_name = "master.key";
|
||||
const char *input_file_name = NULL;
|
||||
const char *output_file_name = NULL;
|
||||
const char *ladder[MAX_LADDER_DEPTH];
|
||||
size_t ladder_depth = 0;
|
||||
int i;
|
||||
enum program_mode mode;
|
||||
psa_status_t status;
|
||||
|
||||
if( argc <= 1 ||
|
||||
strcmp( argv[1], "help" ) == 0 ||
|
||||
strcmp( argv[1], "-help" ) == 0 ||
|
||||
strcmp( argv[1], "--help" ) == 0 )
|
||||
{
|
||||
usage( );
|
||||
return( EXIT_SUCCESS );
|
||||
}
|
||||
|
||||
for( i = 2; i < argc; i++ )
|
||||
{
|
||||
char *q = strchr( argv[i], '=' );
|
||||
if( q == NULL )
|
||||
{
|
||||
printf( "Missing argument to option %s\n", argv[i] );
|
||||
goto usage_failure;
|
||||
}
|
||||
*q = 0;
|
||||
++q;
|
||||
if( strcmp( argv[i], "input" ) == 0 )
|
||||
input_file_name = q;
|
||||
else if( strcmp( argv[i], "label" ) == 0 )
|
||||
{
|
||||
if( ladder_depth == MAX_LADDER_DEPTH )
|
||||
{
|
||||
printf( "Maximum ladder depth %u exceeded.\n",
|
||||
(unsigned) MAX_LADDER_DEPTH );
|
||||
return( EXIT_FAILURE );
|
||||
}
|
||||
ladder[ladder_depth] = q;
|
||||
++ladder_depth;
|
||||
}
|
||||
else if( strcmp( argv[i], "master" ) == 0 )
|
||||
key_file_name = q;
|
||||
else if( strcmp( argv[i], "output" ) == 0 )
|
||||
output_file_name = q;
|
||||
else
|
||||
{
|
||||
printf( "Unknown option: %s\n", argv[i] );
|
||||
goto usage_failure;
|
||||
}
|
||||
}
|
||||
|
||||
if( strcmp( argv[1], "generate" ) == 0 )
|
||||
mode = MODE_GENERATE;
|
||||
else if( strcmp( argv[1], "save" ) == 0 )
|
||||
mode = MODE_SAVE;
|
||||
else if( strcmp( argv[1], "unwrap" ) == 0 )
|
||||
mode = MODE_UNWRAP;
|
||||
else if( strcmp( argv[1], "wrap" ) == 0 )
|
||||
mode = MODE_WRAP;
|
||||
else
|
||||
{
|
||||
printf( "Unknown action: %s\n", argv[1] );
|
||||
goto usage_failure;
|
||||
}
|
||||
|
||||
if( input_file_name == NULL &&
|
||||
( mode == MODE_WRAP || mode == MODE_UNWRAP ) )
|
||||
{
|
||||
printf( "Required argument missing: input\n" );
|
||||
return( DEMO_ERROR );
|
||||
}
|
||||
if( output_file_name == NULL &&
|
||||
( mode == MODE_SAVE || mode == MODE_WRAP || mode == MODE_UNWRAP ) )
|
||||
{
|
||||
printf( "Required argument missing: output\n" );
|
||||
return( DEMO_ERROR );
|
||||
}
|
||||
|
||||
status = run( mode, key_file_name,
|
||||
ladder, ladder_depth,
|
||||
input_file_name, output_file_name );
|
||||
return( status == PSA_SUCCESS ?
|
||||
EXIT_SUCCESS :
|
||||
EXIT_FAILURE );
|
||||
|
||||
usage_failure:
|
||||
usage( );
|
||||
return( EXIT_FAILURE );
|
||||
}
|
||||
#endif /* MBEDTLS_SHA256_C && MBEDTLS_MD_C && MBEDTLS_AES_C && MBEDTLS_CCM_C && MBEDTLS_PSA_CRYPTO_C && MBEDTLS_FS_IO */
|
49
third_party/mbedtls/crypto/programs/psa/key_ladder_demo.sh
vendored
Executable file
49
third_party/mbedtls/crypto/programs/psa/key_ladder_demo.sh
vendored
Executable file
@ -0,0 +1,49 @@
|
||||
#!/bin/sh
|
||||
set -e -u
|
||||
|
||||
program="${0%/*}"/key_ladder_demo
|
||||
files_to_clean=
|
||||
|
||||
run () {
|
||||
echo
|
||||
echo "# $1"
|
||||
shift
|
||||
echo "+ $*"
|
||||
"$@"
|
||||
}
|
||||
|
||||
if [ -e master.key ]; then
|
||||
echo "# Reusing the existing master.key file."
|
||||
else
|
||||
files_to_clean="$files_to_clean master.key"
|
||||
run "Generate a master key." \
|
||||
"$program" generate master=master.key
|
||||
fi
|
||||
|
||||
files_to_clean="$files_to_clean input.txt hello_world.wrap"
|
||||
echo "Here is some input. See it wrapped." >input.txt
|
||||
run "Derive a key and wrap some data with it." \
|
||||
"$program" wrap master=master.key label=hello label=world \
|
||||
input=input.txt output=hello_world.wrap
|
||||
|
||||
files_to_clean="$files_to_clean hello_world.txt"
|
||||
run "Derive the same key again and unwrap the data." \
|
||||
"$program" unwrap master=master.key label=hello label=world \
|
||||
input=hello_world.wrap output=hello_world.txt
|
||||
run "Compare the unwrapped data with the original input." \
|
||||
cmp input.txt hello_world.txt
|
||||
|
||||
files_to_clean="$files_to_clean hellow_orld.txt"
|
||||
! run "Derive a different key and attempt to unwrap the data. This must fail." \
|
||||
"$program" unwrap master=master.key input=hello_world.wrap output=hellow_orld.txt label=hellow label=orld
|
||||
|
||||
files_to_clean="$files_to_clean hello.key"
|
||||
run "Save the first step of the key ladder, then load it as a master key and construct the rest of the ladder." \
|
||||
"$program" save master=master.key label=hello \
|
||||
input=hello_world.wrap output=hello.key
|
||||
run "Check that we get the same key by unwrapping data made by the other key." \
|
||||
"$program" unwrap master=hello.key label=world \
|
||||
input=hello_world.wrap output=hello_world.txt
|
||||
|
||||
# Cleanup
|
||||
rm -f $files_to_clean
|
266
third_party/mbedtls/crypto/programs/psa/psa_constant_names.c
vendored
Normal file
266
third_party/mbedtls/crypto/programs/psa/psa_constant_names.c
vendored
Normal file
@ -0,0 +1,266 @@
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "psa/crypto.h"
|
||||
|
||||
/* This block is present to support Visual Studio builds prior to 2015 */
|
||||
#if defined(_MSC_VER) && _MSC_VER < 1900
|
||||
#include <stdarg.h>
|
||||
int snprintf( char *s, size_t n, const char *fmt, ... )
|
||||
{
|
||||
int ret;
|
||||
va_list argp;
|
||||
|
||||
/* Avoid calling the invalid parameter handler by checking ourselves */
|
||||
if( s == NULL || n == 0 || fmt == NULL )
|
||||
return( -1 );
|
||||
|
||||
va_start( argp, fmt );
|
||||
#if defined(_TRUNCATE) && !defined(__MINGW32__)
|
||||
ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp );
|
||||
#else
|
||||
ret = _vsnprintf( s, n, fmt, argp );
|
||||
if( ret < 0 || (size_t) ret == n )
|
||||
{
|
||||
s[n-1] = '\0';
|
||||
ret = -1;
|
||||
}
|
||||
#endif
|
||||
va_end( argp );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif
|
||||
|
||||
/* There are different GET_HASH macros for different kinds of algorithms
|
||||
* built from hashes, but the values are all constructed on the
|
||||
* same model. */
|
||||
#define PSA_ALG_GET_HASH(alg) \
|
||||
(((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH)
|
||||
|
||||
static void append(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
const char *string, size_t length)
|
||||
{
|
||||
*required_size += length;
|
||||
if (*required_size < buffer_size) {
|
||||
memcpy(*buffer, string, length);
|
||||
*buffer += length;
|
||||
}
|
||||
}
|
||||
|
||||
static void append_integer(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
const char *format /*printf format for value*/,
|
||||
unsigned long value)
|
||||
{
|
||||
size_t n = snprintf(*buffer, buffer_size - *required_size, format, value);
|
||||
if (n < buffer_size - *required_size) *buffer += n;
|
||||
*required_size += n;
|
||||
}
|
||||
|
||||
/* The code of these function is automatically generated and included below. */
|
||||
static const char *psa_ecc_curve_name(psa_ecc_curve_t curve);
|
||||
static const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg);
|
||||
|
||||
static void append_with_curve(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
const char *string, size_t length,
|
||||
psa_ecc_curve_t curve)
|
||||
{
|
||||
const char *curve_name = psa_ecc_curve_name(curve);
|
||||
append(buffer, buffer_size, required_size, string, length);
|
||||
append(buffer, buffer_size, required_size, "(", 1);
|
||||
if (curve_name != NULL) {
|
||||
append(buffer, buffer_size, required_size,
|
||||
curve_name, strlen(curve_name));
|
||||
} else {
|
||||
append_integer(buffer, buffer_size, required_size,
|
||||
"0x%04x", curve);
|
||||
}
|
||||
append(buffer, buffer_size, required_size, ")", 1);
|
||||
}
|
||||
|
||||
static void append_with_hash(char **buffer, size_t buffer_size,
|
||||
size_t *required_size,
|
||||
const char *string, size_t length,
|
||||
psa_algorithm_t hash_alg)
|
||||
{
|
||||
const char *hash_name = psa_hash_algorithm_name(hash_alg);
|
||||
append(buffer, buffer_size, required_size, string, length);
|
||||
append(buffer, buffer_size, required_size, "(", 1);
|
||||
if (hash_name != NULL) {
|
||||
append(buffer, buffer_size, required_size,
|
||||
hash_name, strlen(hash_name));
|
||||
} else {
|
||||
append_integer(buffer, buffer_size, required_size,
|
||||
"0x%08lx", hash_alg);
|
||||
}
|
||||
append(buffer, buffer_size, required_size, ")", 1);
|
||||
}
|
||||
|
||||
#include "psa_constant_names_generated.c"
|
||||
|
||||
static int psa_snprint_status(char *buffer, size_t buffer_size,
|
||||
psa_status_t status)
|
||||
{
|
||||
const char *name = psa_strerror(status);
|
||||
if (name == NULL) {
|
||||
return snprintf(buffer, buffer_size, "%ld", (long) status);
|
||||
} else {
|
||||
size_t length = strlen(name);
|
||||
if (length < buffer_size) {
|
||||
memcpy(buffer, name, length + 1);
|
||||
return (int) length;
|
||||
} else {
|
||||
return (int) buffer_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size,
|
||||
psa_ecc_curve_t curve)
|
||||
{
|
||||
const char *name = psa_ecc_curve_name(curve);
|
||||
if (name == NULL) {
|
||||
return snprintf(buffer, buffer_size, "0x%04x", (unsigned) curve);
|
||||
} else {
|
||||
size_t length = strlen(name);
|
||||
if (length < buffer_size) {
|
||||
memcpy(buffer, name, length + 1);
|
||||
return (int) length;
|
||||
} else {
|
||||
return (int) buffer_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void usage(const char *program_name)
|
||||
{
|
||||
printf("Usage: %s TYPE VALUE [VALUE...]\n",
|
||||
program_name == NULL ? "psa_constant_names" : program_name);
|
||||
printf("Print the symbolic name whose numerical value is VALUE in TYPE.\n");
|
||||
printf("Supported types (with = between aliases):\n");
|
||||
printf(" alg=algorithm Algorithm (psa_algorithm_t)\n");
|
||||
printf(" curve=ecc_curve Elliptic curve identifier (psa_ecc_curve_t)\n");
|
||||
printf(" type=key_type Key type (psa_key_type_t)\n");
|
||||
printf(" usage=key_usage Key usage (psa_key_usage_t)\n");
|
||||
printf(" error=status Status code (psa_status_t)\n");
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
TYPE_STATUS,
|
||||
} signed_value_type;
|
||||
|
||||
int process_signed(signed_value_type type, long min, long max, char **argp)
|
||||
{
|
||||
for (; *argp != NULL; argp++) {
|
||||
char buffer[200];
|
||||
char *end;
|
||||
long value = strtol(*argp, &end, 0);
|
||||
if (*end) {
|
||||
printf("Non-numeric value: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (value < min || (errno == ERANGE && value < 0)) {
|
||||
printf("Value too small: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (value > max || (errno == ERANGE && value > 0)) {
|
||||
printf("Value too large: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case TYPE_STATUS:
|
||||
psa_snprint_status(buffer, sizeof(buffer),
|
||||
(psa_status_t) value);
|
||||
break;
|
||||
}
|
||||
puts(buffer);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
TYPE_ALGORITHM,
|
||||
TYPE_ECC_CURVE,
|
||||
TYPE_KEY_TYPE,
|
||||
TYPE_KEY_USAGE,
|
||||
} unsigned_value_type;
|
||||
|
||||
int process_unsigned(unsigned_value_type type, unsigned long max, char **argp)
|
||||
{
|
||||
for (; *argp != NULL; argp++) {
|
||||
char buffer[200];
|
||||
char *end;
|
||||
unsigned long value = strtoul(*argp, &end, 0);
|
||||
if (*end) {
|
||||
printf("Non-numeric value: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (value > max || errno == ERANGE) {
|
||||
printf("Value out of range: %s\n", *argp);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case TYPE_ALGORITHM:
|
||||
psa_snprint_algorithm(buffer, sizeof(buffer),
|
||||
(psa_algorithm_t) value);
|
||||
break;
|
||||
case TYPE_ECC_CURVE:
|
||||
psa_snprint_ecc_curve(buffer, sizeof(buffer),
|
||||
(psa_ecc_curve_t) value);
|
||||
break;
|
||||
case TYPE_KEY_TYPE:
|
||||
psa_snprint_key_type(buffer, sizeof(buffer),
|
||||
(psa_key_type_t) value);
|
||||
break;
|
||||
case TYPE_KEY_USAGE:
|
||||
psa_snprint_key_usage(buffer, sizeof(buffer),
|
||||
(psa_key_usage_t) value);
|
||||
break;
|
||||
}
|
||||
puts(buffer);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc <= 1 ||
|
||||
!strcmp(argv[1], "help") ||
|
||||
!strcmp(argv[1], "--help"))
|
||||
{
|
||||
usage(argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
|
||||
/* There's no way to obtain the actual range of a signed type,
|
||||
* so hard-code it here: psa_status_t is int32_t. */
|
||||
return process_signed(TYPE_STATUS, INT32_MIN, INT32_MAX,
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
|
||||
return process_unsigned(TYPE_ALGORITHM, (psa_algorithm_t) (-1),
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
|
||||
return process_unsigned(TYPE_ECC_CURVE, (psa_ecc_curve_t) (-1),
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
|
||||
return process_unsigned(TYPE_KEY_TYPE, (psa_key_type_t) (-1),
|
||||
argv + 2);
|
||||
} else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
|
||||
return process_unsigned(TYPE_KEY_USAGE, (psa_key_usage_t) (-1),
|
||||
argv + 2);
|
||||
} else {
|
||||
printf("Unknown type: %s\n", argv[1]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
12
third_party/mbedtls/crypto/programs/random/CMakeLists.txt
vendored
Normal file
12
third_party/mbedtls/crypto/programs/random/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
add_executable(gen_random_havege gen_random_havege.c)
|
||||
target_link_libraries(gen_random_havege mbedcrypto)
|
||||
|
||||
add_executable(gen_random_ctr_drbg gen_random_ctr_drbg.c)
|
||||
target_link_libraries(gen_random_ctr_drbg mbedcrypto)
|
||||
|
||||
add_executable(gen_entropy gen_entropy.c)
|
||||
target_link_libraries(gen_entropy mbedcrypto)
|
||||
|
||||
install(TARGETS gen_random_havege gen_random_ctr_drbg gen_entropy
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
115
third_party/mbedtls/crypto/programs/random/gen_entropy.c
vendored
Normal file
115
third_party/mbedtls/crypto/programs/random/gen_entropy.c
vendored
Normal file
@ -0,0 +1,115 @@
|
||||
/**
|
||||
* \brief Use and generate multiple entropies calls into a file
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/entropy.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int i, k, ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_entropy_context entropy;
|
||||
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
|
||||
if( argc < 2 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
|
||||
for( i = 0, k = 768; i < k; i++ )
|
||||
{
|
||||
ret = mbedtls_entropy_func( &entropy, buf, sizeof( buf ) );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_entropy_func returned -%04X\n",
|
||||
ret );
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
fwrite( buf, 1, sizeof( buf ), f );
|
||||
|
||||
mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
|
||||
"%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
|
||||
fflush( stdout );
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
fclose( f );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_ENTROPY_C */
|
148
third_party/mbedtls/crypto/programs/random/gen_random_ctr_drbg.c
vendored
Normal file
148
third_party/mbedtls/crypto/programs/random/gen_random_ctr_drbg.c
vendored
Normal file
@ -0,0 +1,148 @@
|
||||
/**
|
||||
* \brief Use and generate random data into a file via the CTR_DBRG based on AES
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \
|
||||
defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C) || \
|
||||
!defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
int i, k, ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_entropy_context entropy;
|
||||
unsigned char buf[1024];
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||
|
||||
if( argc < 2 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_entropy_init( &entropy );
|
||||
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( "failed in mbedtls_ctr_drbg_seed: %d\n", ret );
|
||||
goto cleanup;
|
||||
}
|
||||
mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF );
|
||||
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
ret = mbedtls_ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
|
||||
|
||||
if( ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR )
|
||||
{
|
||||
mbedtls_printf( "Failed to open seedfile. Generating one.\n" );
|
||||
ret = mbedtls_ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( "failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret );
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
else if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf( "failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret );
|
||||
goto cleanup;
|
||||
}
|
||||
#endif
|
||||
|
||||
for( i = 0, k = 768; i < k; i++ )
|
||||
{
|
||||
ret = mbedtls_ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
|
||||
if( ret != 0 )
|
||||
{
|
||||
mbedtls_printf("failed!\n");
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
fwrite( buf, 1, sizeof( buf ), f );
|
||||
|
||||
mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
|
||||
"%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
|
||||
fflush( stdout );
|
||||
}
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
mbedtls_printf("\n");
|
||||
|
||||
fclose( f );
|
||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||
mbedtls_entropy_free( &entropy );
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */
|
120
third_party/mbedtls/crypto/programs/random/gen_random_havege.c
vendored
Normal file
120
third_party/mbedtls/crypto/programs/random/gen_random_havege.c
vendored
Normal file
@ -0,0 +1,120 @@
|
||||
/**
|
||||
* \brief Generate random data into a file
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_fprintf fprintf
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_HAVEGE_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/havege.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
FILE *f;
|
||||
time_t t;
|
||||
int i, k, ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
mbedtls_havege_state hs;
|
||||
unsigned char buf[1024];
|
||||
|
||||
if( argc < 2 )
|
||||
{
|
||||
mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
|
||||
{
|
||||
mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
mbedtls_havege_init( &hs );
|
||||
|
||||
t = time( NULL );
|
||||
|
||||
for( i = 0, k = 768; i < k; i++ )
|
||||
{
|
||||
if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 )
|
||||
{
|
||||
mbedtls_printf( " failed\n ! mbedtls_havege_random returned -0x%04X",
|
||||
-ret );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fwrite( buf, sizeof( buf ), 1, f );
|
||||
|
||||
mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
|
||||
"%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
|
||||
fflush( stdout );
|
||||
}
|
||||
|
||||
if( t == time( NULL ) )
|
||||
t--;
|
||||
|
||||
mbedtls_printf(" \n ");
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
mbedtls_havege_free( &hs );
|
||||
fclose( f );
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_HAVEGE_C */
|
25
third_party/mbedtls/crypto/programs/test/CMakeLists.txt
vendored
Normal file
25
third_party/mbedtls/crypto/programs/test/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
set(libs
|
||||
mbedcrypto
|
||||
)
|
||||
|
||||
add_executable(selftest selftest.c)
|
||||
target_link_libraries(selftest ${libs})
|
||||
|
||||
add_executable(benchmark benchmark.c)
|
||||
target_link_libraries(benchmark ${libs})
|
||||
|
||||
if(TEST_CPP)
|
||||
add_executable(cpp_dummy_build cpp_dummy_build.cpp)
|
||||
target_link_libraries(cpp_dummy_build ${libs})
|
||||
endif()
|
||||
|
||||
add_executable(zeroize zeroize.c)
|
||||
target_link_libraries(zeroize ${libs})
|
||||
|
||||
add_executable(query_compile_time_config query_compile_time_config.c)
|
||||
target_sources(query_compile_time_config PUBLIC query_config.c)
|
||||
target_link_libraries(query_compile_time_config ${libs})
|
||||
|
||||
install(TARGETS selftest benchmark query_compile_time_config
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
1016
third_party/mbedtls/crypto/programs/test/benchmark.c
vendored
Normal file
1016
third_party/mbedtls/crypto/programs/test/benchmark.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
105
third_party/mbedtls/crypto/programs/test/cpp_dummy_build.cpp
vendored
Normal file
105
third_party/mbedtls/crypto/programs/test/cpp_dummy_build.cpp
vendored
Normal file
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* This program is a dummy C++ program to ensure Mbed TLS library header files
|
||||
* can be included and built with a C++ compiler.
|
||||
*
|
||||
* Copyright (C) 2018, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/aesni.h"
|
||||
#include "mbedtls/arc4.h"
|
||||
#include "mbedtls/aria.h"
|
||||
#include "mbedtls/asn1.h"
|
||||
#include "mbedtls/asn1write.h"
|
||||
#include "mbedtls/base64.h"
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/blowfish.h"
|
||||
#include "mbedtls/bn_mul.h"
|
||||
#include "mbedtls/camellia.h"
|
||||
#include "mbedtls/ccm.h"
|
||||
#include "mbedtls/certs.h"
|
||||
#include "mbedtls/chacha20.h"
|
||||
#include "mbedtls/chachapoly.h"
|
||||
#include "mbedtls/check_config.h"
|
||||
#include "mbedtls/cipher.h"
|
||||
#include "mbedtls/cipher_internal.h"
|
||||
#include "mbedtls/cmac.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/des.h"
|
||||
#include "mbedtls/dhm.h"
|
||||
#include "mbedtls/ecdh.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/ecjpake.h"
|
||||
#include "mbedtls/ecp.h"
|
||||
#include "mbedtls/ecp_internal.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/entropy_poll.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/gcm.h"
|
||||
#include "mbedtls/havege.h"
|
||||
#include "mbedtls/hkdf.h"
|
||||
#include "mbedtls/hmac_drbg.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/md2.h"
|
||||
#include "mbedtls/md4.h"
|
||||
#include "mbedtls/md5.h"
|
||||
#include "mbedtls/md_internal.h"
|
||||
#include "mbedtls/nist_kw.h"
|
||||
#include "mbedtls/oid.h"
|
||||
#include "mbedtls/padlock.h"
|
||||
#include "mbedtls/pem.h"
|
||||
#include "mbedtls/pk.h"
|
||||
#include "mbedtls/pk_internal.h"
|
||||
#include "mbedtls/pkcs12.h"
|
||||
#include "mbedtls/pkcs5.h"
|
||||
#include "mbedtls/platform_time.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "mbedtls/poly1305.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#include "mbedtls/ripemd160.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/rsa_internal.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
#include "mbedtls/threading.h"
|
||||
#include "mbedtls/timing.h"
|
||||
#include "mbedtls/version.h"
|
||||
#include "mbedtls/xtea.h"
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
#include "mbedtls/memory_buffer_alloc.h"
|
||||
#endif
|
||||
|
||||
int main()
|
||||
{
|
||||
mbedtls_platform_context *ctx = NULL;
|
||||
mbedtls_platform_setup(ctx);
|
||||
mbedtls_printf("CPP Build test\n");
|
||||
mbedtls_platform_teardown(ctx);
|
||||
}
|
56
third_party/mbedtls/crypto/programs/test/query_compile_time_config.c
vendored
Normal file
56
third_party/mbedtls/crypto/programs/test/query_compile_time_config.c
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Query the Mbed TLS compile time configuration
|
||||
*
|
||||
* Copyright (C) 2018, Arm Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of Mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
|
||||
#define USAGE \
|
||||
"usage: %s <MBEDTLS_CONFIG>\n\n" \
|
||||
"This program takes one command line argument which corresponds to\n" \
|
||||
"the string representation of a Mbed TLS compile time configuration.\n" \
|
||||
"The value 0 will be returned if this configuration is defined in the\n" \
|
||||
"Mbed TLS build and the macro expansion of that configuration will be\n" \
|
||||
"printed (if any). Otherwise, 1 will be returned.\n"
|
||||
|
||||
int query_config( const char *config );
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
if ( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( USAGE, argv[0] );
|
||||
return( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
|
||||
return( query_config( argv[1] ) );
|
||||
}
|
1966
third_party/mbedtls/crypto/programs/test/query_config.c
vendored
Normal file
1966
third_party/mbedtls/crypto/programs/test/query_config.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
430
third_party/mbedtls/crypto/programs/test/selftest.c
vendored
Normal file
430
third_party/mbedtls/crypto/programs/test/selftest.c
vendored
Normal file
@ -0,0 +1,430 @@
|
||||
/*
|
||||
* Self-test demonstration program
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/entropy_poll.h"
|
||||
#include "mbedtls/hmac_drbg.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/dhm.h"
|
||||
#include "mbedtls/gcm.h"
|
||||
#include "mbedtls/ccm.h"
|
||||
#include "mbedtls/cmac.h"
|
||||
#include "mbedtls/md2.h"
|
||||
#include "mbedtls/md4.h"
|
||||
#include "mbedtls/md5.h"
|
||||
#include "mbedtls/ripemd160.h"
|
||||
#include "mbedtls/sha1.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
#include "mbedtls/arc4.h"
|
||||
#include "mbedtls/des.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/camellia.h"
|
||||
#include "mbedtls/aria.h"
|
||||
#include "mbedtls/chacha20.h"
|
||||
#include "mbedtls/poly1305.h"
|
||||
#include "mbedtls/chachapoly.h"
|
||||
#include "mbedtls/base64.h"
|
||||
#include "mbedtls/bignum.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/xtea.h"
|
||||
#include "mbedtls/pkcs5.h"
|
||||
#include "mbedtls/ecp.h"
|
||||
#include "mbedtls/ecjpake.h"
|
||||
#include "mbedtls/timing.h"
|
||||
#include "mbedtls/nist_kw.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_snprintf snprintf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
#include "mbedtls/memory_buffer_alloc.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#include "mbedtls/platform_util.h"
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
|
||||
{
|
||||
int ret;
|
||||
char buf[10] = "xxxxxxxxx";
|
||||
const char ref[10] = "xxxxxxxxx";
|
||||
|
||||
ret = mbedtls_snprintf( buf, n, "%s", "123" );
|
||||
if( ret < 0 || (size_t) ret >= n )
|
||||
ret = -1;
|
||||
|
||||
if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
|
||||
ref_ret != ret ||
|
||||
memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
|
||||
{
|
||||
return( 1 );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static int run_test_snprintf( void )
|
||||
{
|
||||
return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
|
||||
test_snprintf( 1, "", -1 ) != 0 ||
|
||||
test_snprintf( 2, "1", -1 ) != 0 ||
|
||||
test_snprintf( 3, "12", -1 ) != 0 ||
|
||||
test_snprintf( 4, "123", 3 ) != 0 ||
|
||||
test_snprintf( 5, "123", 3 ) != 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if a seed file is present, and if not create one for the entropy
|
||||
* self-test. If this fails, we attempt the test anyway, so no error is passed
|
||||
* back.
|
||||
*/
|
||||
#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
|
||||
#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
|
||||
static void create_entropy_seed_file( void )
|
||||
{
|
||||
int result;
|
||||
size_t output_len = 0;
|
||||
unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
|
||||
|
||||
/* Attempt to read the entropy seed file. If this fails - attempt to write
|
||||
* to the file to ensure one is present. */
|
||||
result = mbedtls_platform_std_nv_seed_read( seed_value,
|
||||
MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
if( 0 == result )
|
||||
return;
|
||||
|
||||
result = mbedtls_platform_entropy_poll( NULL,
|
||||
seed_value,
|
||||
MBEDTLS_ENTROPY_BLOCK_SIZE,
|
||||
&output_len );
|
||||
if( 0 != result )
|
||||
return;
|
||||
|
||||
if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
|
||||
return;
|
||||
|
||||
mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
|
||||
}
|
||||
#endif
|
||||
|
||||
int mbedtls_entropy_self_test_wrapper( int verbose )
|
||||
{
|
||||
#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
|
||||
create_entropy_seed_file( );
|
||||
#endif
|
||||
return( mbedtls_entropy_self_test( verbose ) );
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
{
|
||||
#if defined(MBEDTLS_MEMORY_DEBUG)
|
||||
mbedtls_memory_buffer_alloc_status( );
|
||||
#endif
|
||||
}
|
||||
mbedtls_memory_buffer_alloc_free( );
|
||||
return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *name;
|
||||
int ( *function )( int );
|
||||
} selftest_t;
|
||||
|
||||
const selftest_t selftests[] =
|
||||
{
|
||||
#if defined(MBEDTLS_MD2_C)
|
||||
{"md2", mbedtls_md2_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_MD4_C)
|
||||
{"md4", mbedtls_md4_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_MD5_C)
|
||||
{"md5", mbedtls_md5_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_RIPEMD160_C)
|
||||
{"ripemd160", mbedtls_ripemd160_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA1_C)
|
||||
{"sha1", mbedtls_sha1_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA256_C)
|
||||
{"sha256", mbedtls_sha256_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_SHA512_C)
|
||||
{"sha512", mbedtls_sha512_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_ARC4_C)
|
||||
{"arc4", mbedtls_arc4_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_DES_C)
|
||||
{"des", mbedtls_des_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
{"aes", mbedtls_aes_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
|
||||
{"gcm", mbedtls_gcm_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
|
||||
{"ccm", mbedtls_ccm_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
|
||||
{"nist_kw", mbedtls_nist_kw_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CMAC_C)
|
||||
{"cmac", mbedtls_cmac_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CHACHA20_C)
|
||||
{"chacha20", mbedtls_chacha20_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_POLY1305_C)
|
||||
{"poly1305", mbedtls_poly1305_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CHACHAPOLY_C)
|
||||
{"chacha20-poly1305", mbedtls_chachapoly_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_BASE64_C)
|
||||
{"base64", mbedtls_base64_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_BIGNUM_C)
|
||||
{"mpi", mbedtls_mpi_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
{"rsa", mbedtls_rsa_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_XTEA_C)
|
||||
{"xtea", mbedtls_xtea_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CAMELLIA_C)
|
||||
{"camellia", mbedtls_camellia_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_ARIA_C)
|
||||
{"aria", mbedtls_aria_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||
{"ctr_drbg", mbedtls_ctr_drbg_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_HMAC_DRBG_C)
|
||||
{"hmac_drbg", mbedtls_hmac_drbg_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
{"ecp", mbedtls_ecp_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_ECJPAKE_C)
|
||||
{"ecjpake", mbedtls_ecjpake_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_DHM_C)
|
||||
{"dhm", mbedtls_dhm_self_test},
|
||||
#endif
|
||||
#if defined(MBEDTLS_ENTROPY_C)
|
||||
{"entropy", mbedtls_entropy_self_test_wrapper},
|
||||
#endif
|
||||
#if defined(MBEDTLS_PKCS5_C)
|
||||
{"pkcs5", mbedtls_pkcs5_self_test},
|
||||
#endif
|
||||
/* Slower test after the faster ones */
|
||||
#if defined(MBEDTLS_TIMING_C)
|
||||
{"timing", mbedtls_timing_self_test},
|
||||
#endif
|
||||
/* Heap test comes last */
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
{"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
|
||||
#endif
|
||||
{NULL, NULL}
|
||||
};
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
const selftest_t *test;
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
char **argp;
|
||||
int v = 1; /* v=1 for verbose mode */
|
||||
int exclude_mode = 0;
|
||||
int suites_tested = 0, suites_failed = 0;
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
|
||||
unsigned char buf[1000000];
|
||||
#endif
|
||||
void *pointer;
|
||||
|
||||
/*
|
||||
* The C standard doesn't guarantee that all-bits-0 is the representation
|
||||
* of a NULL pointer. We do however use that in our code for initializing
|
||||
* structures, which should work on every modern platform. Let's be sure.
|
||||
*/
|
||||
memset( &pointer, 0, sizeof( void * ) );
|
||||
if( pointer != NULL )
|
||||
{
|
||||
mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
|
||||
/*
|
||||
* Make sure we have a snprintf that correctly zero-terminates
|
||||
*/
|
||||
if( run_test_snprintf() != 0 )
|
||||
{
|
||||
mbedtls_printf( "the snprintf implementation is broken\n" );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
|
||||
for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
|
||||
{
|
||||
if( strcmp( *argp, "--quiet" ) == 0 ||
|
||||
strcmp( *argp, "-q" ) == 0 )
|
||||
{
|
||||
v = 0;
|
||||
}
|
||||
else if( strcmp( *argp, "--exclude" ) == 0 ||
|
||||
strcmp( *argp, "-x" ) == 0 )
|
||||
{
|
||||
exclude_mode = 1;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if( v != 0 )
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
|
||||
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
|
||||
mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
|
||||
#endif
|
||||
|
||||
if( *argp != NULL && exclude_mode == 0 )
|
||||
{
|
||||
/* Run the specified tests */
|
||||
for( ; *argp != NULL; argp++ )
|
||||
{
|
||||
for( test = selftests; test->name != NULL; test++ )
|
||||
{
|
||||
if( !strcmp( *argp, test->name ) )
|
||||
{
|
||||
if( test->function( v ) != 0 )
|
||||
{
|
||||
suites_failed++;
|
||||
}
|
||||
suites_tested++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( test->name == NULL )
|
||||
{
|
||||
mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
|
||||
suites_failed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Run all the tests except excluded ones */
|
||||
for( test = selftests; test->name != NULL; test++ )
|
||||
{
|
||||
if( exclude_mode )
|
||||
{
|
||||
char **excluded;
|
||||
for( excluded = argp; *excluded != NULL; ++excluded )
|
||||
{
|
||||
if( !strcmp( *excluded, test->name ) )
|
||||
break;
|
||||
}
|
||||
if( *excluded )
|
||||
{
|
||||
if( v )
|
||||
mbedtls_printf( " Skip: %s\n", test->name );
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if( test->function( v ) != 0 )
|
||||
{
|
||||
suites_failed++;
|
||||
}
|
||||
suites_tested++;
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
(void) exclude_mode;
|
||||
mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
|
||||
#endif
|
||||
|
||||
if( v != 0 )
|
||||
{
|
||||
mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
|
||||
|
||||
if( suites_failed > 0)
|
||||
{
|
||||
mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
|
||||
}
|
||||
else
|
||||
{
|
||||
mbedtls_printf( " [ All tests PASS ]\n\n" );
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
}
|
||||
|
||||
if( suites_failed > 0)
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
|
||||
/* return() is here to prevent compiler warnings */
|
||||
return( MBEDTLS_EXIT_SUCCESS );
|
||||
}
|
||||
|
101
third_party/mbedtls/crypto/programs/test/zeroize.c
vendored
Normal file
101
third_party/mbedtls/crypto/programs/test/zeroize.c
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Zeroize application for debugger-driven testing
|
||||
*
|
||||
* This is a simple test application used for debugger-driven testing to check
|
||||
* whether calls to mbedtls_platform_zeroize() are being eliminated by compiler
|
||||
* optimizations. This application is used by the GDB script at
|
||||
* tests/scripts/test_zeroize.gdb under the assumption that the code does not
|
||||
* change often (as opposed to the library code) because the script sets a
|
||||
* breakpoint at the last return statement in the main() function of this
|
||||
* program. The debugger facilities are then used to manually inspect the
|
||||
* memory and verify that the call to mbedtls_platform_zeroize() was not
|
||||
* eliminated.
|
||||
*
|
||||
* Copyright (C) 2018, Arm Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_printf printf
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif
|
||||
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#define BUFFER_LEN 1024
|
||||
|
||||
void usage( void )
|
||||
{
|
||||
mbedtls_printf( "Zeroize is a simple program to assist with testing\n" );
|
||||
mbedtls_printf( "the mbedtls_platform_zeroize() function by using the\n" );
|
||||
mbedtls_printf( "debugger. This program takes a file as input and\n" );
|
||||
mbedtls_printf( "prints the first %d characters. Usage:\n\n", BUFFER_LEN );
|
||||
mbedtls_printf( " zeroize <FILE>\n" );
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
FILE *fp;
|
||||
char buf[BUFFER_LEN];
|
||||
char *p = buf;
|
||||
char *end = p + BUFFER_LEN;
|
||||
int c;
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( "This program takes exactly 1 agument\n" );
|
||||
usage();
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
fp = fopen( argv[1], "r" );
|
||||
if( fp == NULL )
|
||||
{
|
||||
mbedtls_printf( "Could not open file '%s'\n", argv[1] );
|
||||
return( exit_code );
|
||||
}
|
||||
|
||||
while( ( c = fgetc( fp ) ) != EOF && p < end - 1 )
|
||||
*p++ = (char)c;
|
||||
*p = '\0';
|
||||
|
||||
if( p - buf != 0 )
|
||||
{
|
||||
mbedtls_printf( "%s\n", buf );
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
}
|
||||
else
|
||||
mbedtls_printf( "The file is empty!\n" );
|
||||
|
||||
fclose( fp );
|
||||
mbedtls_platform_zeroize( buf, sizeof( buf ) );
|
||||
|
||||
return( exit_code );
|
||||
}
|
13
third_party/mbedtls/crypto/programs/util/CMakeLists.txt
vendored
Normal file
13
third_party/mbedtls/crypto/programs/util/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
set(libs
|
||||
mbedcrypto
|
||||
)
|
||||
|
||||
add_executable(strerror strerror.c)
|
||||
target_link_libraries(strerror ${libs})
|
||||
|
||||
add_executable(pem2der pem2der.c)
|
||||
target_link_libraries(pem2der ${libs})
|
||||
|
||||
install(TARGETS strerror pem2der
|
||||
DESTINATION "bin"
|
||||
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
|
306
third_party/mbedtls/crypto/programs/util/pem2der.c
vendored
Normal file
306
third_party/mbedtls/crypto/programs/util/pem2der.c
vendored
Normal file
@ -0,0 +1,306 @@
|
||||
/*
|
||||
* Convert PEM to DER
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define mbedtls_free free
|
||||
#define mbedtls_calloc calloc
|
||||
#define mbedtls_printf printf
|
||||
#define mbedtls_exit exit
|
||||
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
|
||||
#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/base64.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define DFL_FILENAME "file.pem"
|
||||
#define DFL_OUTPUT_FILENAME "file.der"
|
||||
|
||||
#define USAGE \
|
||||
"\n usage: pem2der param=<>...\n" \
|
||||
"\n acceptable parameters:\n" \
|
||||
" filename=%%s default: file.pem\n" \
|
||||
" output_file=%%s default: file.der\n" \
|
||||
"\n"
|
||||
|
||||
#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
|
||||
#if defined(MBEDTLS_CHECK_PARAMS)
|
||||
#define mbedtls_exit exit
|
||||
void mbedtls_param_failed( const char *failure_condition,
|
||||
const char *file,
|
||||
int line )
|
||||
{
|
||||
mbedtls_printf( "%s:%i: Input param failed - %s\n",
|
||||
file, line, failure_condition );
|
||||
mbedtls_exit( MBEDTLS_EXIT_FAILURE );
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* global options
|
||||
*/
|
||||
struct options
|
||||
{
|
||||
const char *filename; /* filename of the input file */
|
||||
const char *output_file; /* where to store the output */
|
||||
} opt;
|
||||
|
||||
int convert_pem_to_der( const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen )
|
||||
{
|
||||
int ret;
|
||||
const unsigned char *s1, *s2, *end = input + ilen;
|
||||
size_t len = 0;
|
||||
|
||||
s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
|
||||
if( s1 == NULL )
|
||||
return( -1 );
|
||||
|
||||
s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
|
||||
if( s2 == NULL )
|
||||
return( -1 );
|
||||
|
||||
s1 += 10;
|
||||
while( s1 < end && *s1 != '-' )
|
||||
s1++;
|
||||
while( s1 < end && *s1 == '-' )
|
||||
s1++;
|
||||
if( *s1 == '\r' ) s1++;
|
||||
if( *s1 == '\n' ) s1++;
|
||||
|
||||
if( s2 <= s1 || s2 > end )
|
||||
return( -1 );
|
||||
|
||||
ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
|
||||
if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
|
||||
return( ret );
|
||||
|
||||
if( len > *olen )
|
||||
return( -1 );
|
||||
|
||||
if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
|
||||
s2 - s1 ) ) != 0 )
|
||||
{
|
||||
return( ret );
|
||||
}
|
||||
|
||||
*olen = len;
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Load all data from a file into a given buffer.
|
||||
*/
|
||||
static int load_file( const char *path, unsigned char **buf, size_t *n )
|
||||
{
|
||||
FILE *f;
|
||||
long size;
|
||||
|
||||
if( ( f = fopen( path, "rb" ) ) == NULL )
|
||||
return( -1 );
|
||||
|
||||
fseek( f, 0, SEEK_END );
|
||||
if( ( size = ftell( f ) ) == -1 )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
fseek( f, 0, SEEK_SET );
|
||||
|
||||
*n = (size_t) size;
|
||||
|
||||
if( *n + 1 == 0 ||
|
||||
( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
if( fread( *buf, 1, *n, f ) != *n )
|
||||
{
|
||||
fclose( f );
|
||||
free( *buf );
|
||||
*buf = NULL;
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
|
||||
(*buf)[*n] = '\0';
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
/*
|
||||
* Write buffer to a file
|
||||
*/
|
||||
static int write_file( const char *path, unsigned char *buf, size_t n )
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
if( ( f = fopen( path, "wb" ) ) == NULL )
|
||||
return( -1 );
|
||||
|
||||
if( fwrite( buf, 1, n, f ) != n )
|
||||
{
|
||||
fclose( f );
|
||||
return( -1 );
|
||||
}
|
||||
|
||||
fclose( f );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
int ret = 1;
|
||||
int exit_code = MBEDTLS_EXIT_FAILURE;
|
||||
unsigned char *pem_buffer = NULL;
|
||||
unsigned char der_buffer[4096];
|
||||
char buf[1024];
|
||||
size_t pem_size, der_size = sizeof(der_buffer);
|
||||
int i;
|
||||
char *p, *q;
|
||||
|
||||
/*
|
||||
* Set to sane values
|
||||
*/
|
||||
memset( buf, 0, sizeof(buf) );
|
||||
memset( der_buffer, 0, sizeof(der_buffer) );
|
||||
|
||||
if( argc == 0 )
|
||||
{
|
||||
usage:
|
||||
mbedtls_printf( USAGE );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
opt.filename = DFL_FILENAME;
|
||||
opt.output_file = DFL_OUTPUT_FILENAME;
|
||||
|
||||
for( i = 1; i < argc; i++ )
|
||||
{
|
||||
|
||||
p = argv[i];
|
||||
if( ( q = strchr( p, '=' ) ) == NULL )
|
||||
goto usage;
|
||||
*q++ = '\0';
|
||||
|
||||
if( strcmp( p, "filename" ) == 0 )
|
||||
opt.filename = q;
|
||||
else if( strcmp( p, "output_file" ) == 0 )
|
||||
opt.output_file = q;
|
||||
else
|
||||
goto usage;
|
||||
}
|
||||
|
||||
/*
|
||||
* 1.1. Load the PEM file
|
||||
*/
|
||||
mbedtls_printf( "\n . Loading the PEM file ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = load_file( opt.filename, &pem_buffer, &pem_size );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, 1024 );
|
||||
#endif
|
||||
mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.2. Convert from PEM to DER
|
||||
*/
|
||||
mbedtls_printf( " . Converting from PEM to DER ..." );
|
||||
fflush( stdout );
|
||||
|
||||
if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, 1024 );
|
||||
#endif
|
||||
mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
/*
|
||||
* 1.3. Write the DER file
|
||||
*/
|
||||
mbedtls_printf( " . Writing the DER file ..." );
|
||||
fflush( stdout );
|
||||
|
||||
ret = write_file( opt.output_file, der_buffer, der_size );
|
||||
|
||||
if( ret != 0 )
|
||||
{
|
||||
#ifdef MBEDTLS_ERROR_C
|
||||
mbedtls_strerror( ret, buf, 1024 );
|
||||
#endif
|
||||
mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
mbedtls_printf( " ok\n" );
|
||||
|
||||
exit_code = MBEDTLS_EXIT_SUCCESS;
|
||||
|
||||
exit:
|
||||
free( pem_buffer );
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( exit_code );
|
||||
}
|
||||
#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */
|
92
third_party/mbedtls/crypto/programs/util/strerror.c
vendored
Normal file
92
third_party/mbedtls/crypto/programs/util/strerror.c
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Translate error code to error string
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
|
||||
#include "mbedtls/error.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define USAGE \
|
||||
"\n usage: strerror <errorcode>\n" \
|
||||
"\n where <errorcode> can be a decimal or hexadecimal (starts with 0x or -0x)\n"
|
||||
|
||||
#if !defined(MBEDTLS_ERROR_C) && !defined(MBEDTLS_ERROR_STRERROR_DUMMY)
|
||||
int main( void )
|
||||
{
|
||||
mbedtls_printf("MBEDTLS_ERROR_C and/or MBEDTLS_ERROR_STRERROR_DUMMY not defined.\n");
|
||||
return( 0 );
|
||||
}
|
||||
#else
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
long int val;
|
||||
char *end = argv[1];
|
||||
|
||||
if( argc != 2 )
|
||||
{
|
||||
mbedtls_printf( USAGE );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
val = strtol( argv[1], &end, 10 );
|
||||
if( *end != '\0' )
|
||||
{
|
||||
val = strtol( argv[1], &end, 16 );
|
||||
if( *end != '\0' )
|
||||
{
|
||||
mbedtls_printf( USAGE );
|
||||
return( 0 );
|
||||
}
|
||||
}
|
||||
if( val > 0 )
|
||||
val = -val;
|
||||
|
||||
if( val != 0 )
|
||||
{
|
||||
char error_buf[200];
|
||||
mbedtls_strerror( val, error_buf, 200 );
|
||||
mbedtls_printf("Last error was: -0x%04x - %s\n\n", (int) -val, error_buf );
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
mbedtls_printf( " + Press Enter to exit this program.\n" );
|
||||
fflush( stdout ); getchar();
|
||||
#endif
|
||||
|
||||
return( val );
|
||||
}
|
||||
#endif /* MBEDTLS_ERROR_C */
|
45
third_party/mbedtls/crypto/programs/wince_main.c
vendored
Normal file
45
third_party/mbedtls/crypto/programs/wince_main.c
vendored
Normal file
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Windows CE console application entry point
|
||||
*
|
||||
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#if defined(_WIN32_WCE)
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
extern int main( int, const char ** );
|
||||
|
||||
int _tmain( int argc, _TCHAR* targv[] )
|
||||
{
|
||||
char **argv;
|
||||
int i;
|
||||
|
||||
argv = ( char ** ) calloc( argc, sizeof( char * ) );
|
||||
|
||||
for ( i = 0; i < argc; i++ ) {
|
||||
size_t len;
|
||||
len = _tcslen( targv[i] ) + 1;
|
||||
argv[i] = ( char * ) calloc( len, sizeof( char ) );
|
||||
wcstombs( argv[i], targv[i], len );
|
||||
}
|
||||
|
||||
return main( argc, argv );
|
||||
}
|
||||
|
||||
#endif /* defined(_WIN32_WCE) */
|
Reference in New Issue
Block a user