├── .gitignore ├── Makefile ├── README.md ├── TODO.md ├── examples └── random_dot_org │ └── random_dot_org.cpp ├── library.properties ├── src ├── mbedtls │ ├── aes.c │ ├── aes.h │ ├── aesni.c │ ├── aesni.h │ ├── arc4.c │ ├── arc4.h │ ├── asn1.h │ ├── asn1parse.c │ ├── asn1write.c │ ├── asn1write.h │ ├── base64.c │ ├── base64.h │ ├── bignum.c │ ├── bignum.h │ ├── blowfish.c │ ├── blowfish.h │ ├── bn_mul.h │ ├── camellia.c │ ├── camellia.h │ ├── ccm.c │ ├── ccm.h │ ├── certs.c │ ├── certs.h │ ├── check_config.h │ ├── cipher.c │ ├── cipher.h │ ├── cipher_internal.h │ ├── cipher_wrap.c │ ├── cmac.c │ ├── cmac.h │ ├── compat-1.3.h │ ├── config.h │ ├── ctr_drbg.c │ ├── ctr_drbg.h │ ├── debug.c │ ├── debug.h │ ├── des.c │ ├── des.h │ ├── dhm.c │ ├── dhm.h │ ├── ecdh.c │ ├── ecdh.h │ ├── ecdsa.c │ ├── ecdsa.h │ ├── ecjpake.c │ ├── ecjpake.h │ ├── ecp.c │ ├── ecp.h │ ├── ecp_curves.c │ ├── entropy.c │ ├── entropy.h │ ├── entropy_poll.c │ ├── entropy_poll.h │ ├── error.c │ ├── error.h │ ├── gcm.c │ ├── gcm.h │ ├── havege.c │ ├── havege.h │ ├── hmac_drbg.c │ ├── hmac_drbg.h │ ├── md.c │ ├── md.h │ ├── md2.c │ ├── md2.h │ ├── md4.c │ ├── md4.h │ ├── md5.c │ ├── md5.h │ ├── md_internal.h │ ├── md_wrap.c │ ├── memory_buffer_alloc.c │ ├── memory_buffer_alloc.h │ ├── net.h │ ├── net_sockets.c │ ├── net_sockets.h │ ├── oid.c │ ├── oid.h │ ├── padlock.c │ ├── padlock.h │ ├── pem.c │ ├── pem.h │ ├── pk.c │ ├── pk.h │ ├── pk_internal.h │ ├── pk_wrap.c │ ├── pkcs11.c │ ├── pkcs11.h │ ├── pkcs12.c │ ├── pkcs12.h │ ├── pkcs5.c │ ├── pkcs5.h │ ├── pkparse.c │ ├── pkwrite.c │ ├── platform.c │ ├── platform.h │ ├── platform_time.h │ ├── ripemd160.c │ ├── ripemd160.h │ ├── rsa.c │ ├── rsa.h │ ├── sha1.c │ ├── sha1.h │ ├── sha256.c │ ├── sha256.h │ ├── sha512.c │ ├── sha512.h │ ├── ssl.h │ ├── ssl_cache.c │ ├── ssl_cache.h │ ├── ssl_ciphersuites.c │ ├── ssl_ciphersuites.h │ ├── ssl_cli.c │ ├── ssl_cookie.c │ ├── ssl_cookie.h │ ├── ssl_internal.h │ ├── ssl_srv.c │ ├── ssl_ticket.c │ ├── ssl_ticket.h │ ├── ssl_tls.c │ ├── threading.c │ ├── threading.h │ ├── timing.c │ ├── timing.h │ ├── version.c │ ├── version.h │ ├── version_features.c │ ├── x509.c │ ├── x509.h │ ├── x509_create.c │ ├── x509_crl.c │ ├── x509_crl.h │ ├── x509_crt.c │ ├── x509_crt.h │ ├── x509_csr.c │ ├── x509_csr.h │ ├── x509write_crt.c │ ├── x509write_csr.c │ ├── xtea.c │ └── xtea.h ├── tls.cpp └── tls.h └── test ├── catch.hpp ├── runner.cpp └── test_tls.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | *.swp 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TARGET_LIB ?= particletls.a 2 | TEST_RUNNER ?= runner 3 | 4 | BUILD_DIR ?= ./build 5 | SRC_DIR ?= ./src 6 | TEST_DIR ?= ./test 7 | 8 | SRCS := $(shell find $(SRC_DIR) -name *.cpp -or -name *.c) 9 | OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) 10 | DEPS := $(OBJS:.o=.d) 11 | 12 | TEST_SRCS := $(shell find $(TEST_DIR) -name *.cpp) 13 | TEST_OBJS := $(TEST_SRCS:%=$(BUILD_DIR)/%.o) 14 | TEST_DEPS := $(TEST_OBJS:.o=.d) 15 | 16 | INC_DIRS := $(shell find $(SRC_DIR) -type d) 17 | INC_FLAGS := $(addprefix -I,$(INC_DIRS)) 18 | 19 | CPPFLAGS ?= $(INC_FLAGS) -MMD -MP 20 | ARFLAGS ?= rvs 21 | 22 | 23 | # The first rule is the default target. Just build the library. 24 | $(BUILD_DIR)/$(TARGET_LIB): $(OBJS) 25 | $(AR) $(ARFLAGS) $@ $(OBJS) 26 | 27 | $(BUILD_DIR)/%.cpp.o: %.cpp 28 | $(MKDIR_P) $(dir $@) 29 | $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ 30 | 31 | $(BUILD_DIR)/%.c.o: %.c 32 | $(MKDIR_P) $(dir $@) 33 | $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ 34 | 35 | $(BUILD_DIR)/$(TEST_DIR)/$(TEST_RUNNER): $(TEST_OBJS) $(OBJS) 36 | $(CXX) $(TEST_OBJS) $(OBJS) -o $@ $(LDFLAGS) $(LDLIBS) 37 | 38 | $(TEST_DIR)/%.cpp.o: %.cpp 39 | $(MKDIR_P) $(dir $@) 40 | $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ 41 | 42 | .PHONY: clean test 43 | 44 | clean: 45 | $(RM) -r $(BUILD_DIR) 46 | 47 | test: $(BUILD_DIR)/$(TEST_DIR)/$(TEST_RUNNER) 48 | $(BUILD_DIR)/$(TEST_DIR)/$(TEST_RUNNER) 49 | 50 | -include $(DEPS) 51 | 52 | MKDIR_P ?= mkdir -p 53 | 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TLS Library for [Particle](https://www.particle.io/) 2 | 3 | This library wraps [mbed TLS](https://tls.mbed.org) into a format that can be 4 | easily used in the Particle ecosystem of IoT devices. It is appropriate for 5 | contrained, embedded devices and should also work well on other systems. 6 | 7 | This library's code is test-driven and comes with examples that may be run on a 8 | [Photon (Wi-Fi)](https://store.particle.io/collections/photon), 9 | [Electron (Cellular)](https://store.particle.io/collections/electron), 10 | or other Particle dev kit. 11 | 12 | You can get started using the [web IDE](https://build.particle.io/), click the 13 | libraries icon in the sidebar and search for TLS. 14 | 15 | You can also simply clone this repo and run `make`. 16 | 17 | Run the tests with `make test`. 18 | 19 | Build the examples with `make examples`. 20 | 21 | 22 | ## Usage 23 | 24 | Declare the instance statically at the top of your program. 25 | 26 | ``` 27 | TLS tls; 28 | ``` 29 | 30 | In `setup()` call `tls.init()` and check the return value. 31 | 32 | ``` 33 | void setup() { 34 | int error = tls.init(); 35 | if (error < 0) { 36 | // Uh oh! Failure! The library won't work! 37 | } else if (error > 0) { 38 | // Partial success parsing root CA certificates. 39 | // Things may or may not work. 40 | } else { 41 | // error == 0 42 | // Woot! Success! 43 | } 44 | } 45 | ``` 46 | 47 | Define some function that calls connect, write, read, and close. 48 | You'll probably call this function from `loop()` or from a `Particle.function()`. 49 | 50 | ``` 51 | char currentRandomHexString[1024]; 52 | 53 | int updateRandomHexString() { 54 | tls.connect("www.random.org", "443"); 55 | tls.write("GET /cgi-bin/randbyte HTTP/1.1\r\n" 56 | "Host: www.random.org\r\n\r\n"); 57 | int charsReadOrError = tls.read(currentRandomHexString, 1024); 58 | tls.close(); 59 | return charsReadOrError; 60 | } 61 | 62 | ``` 63 | 64 | 65 | ## Root Certificate 66 | 67 | You will need to initialize the library with a root certificate. You can mimic 68 | the format of the examples here, including the PEM file in the source code as a 69 | long string. Alternatively you can save a little flash and RAM by including the 70 | DER formatted binary certificate in your code. Some handy terminal commands: 71 | 72 | ``` 73 | # converts PEM to DER 74 | openssl x509 -outform der -in certificate.pem -out certificate.der 75 | 76 | # outputs a copy-pastable C array 77 | xxd -i certificate.der 78 | ``` 79 | 80 | 81 | ## Troubleshooting 82 | 83 | The return value from `init()`, when non-zero, is an mbed TLS 84 | error code returned from either `mbedtls_ctr_drbg_seed()` 85 | (where the only documented error value is `-0x0034` for 86 | [MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED](https://tls.mbed.org/api/ctr__drbg_8h.html#a15d1931ea5d133062cd93a3374a5bcf0)) 87 | or, more likely, 88 | [`mbedtls_x509_crt_parse()`](https://tls.mbed.org/api/group__x509__module.html#ga033567483649030f7f859db4f4cb7e14). 89 | 90 | There are 4 possible return values from `connect()`: 91 | - 0: Success 92 | - -82: Unknown host 93 | - -68: Connection failed 94 | - -66: Socket failed 95 | 96 | 97 | ## License 98 | 99 | This repo is almost entirely released under the 100 | [Apache License, Version 2.0.](https://www.apache.org/licenses/LICENSE-2.0). 101 | 102 | The mbed TLS library (Apache 2.0 licensed) is copyright ARM Limited. 103 | 104 | The Catch test framework (one file: test/catch.hpp) is released under the 105 | Boost Software License, Version 1.0 and is copyright Two Blue Cubes Ltd. 106 | 107 | All other files are copyright Zachary Crockett. 108 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | # To Do 2 | 3 | - Add README documentation that if you read the entire buffer you passed, then you should deal with it and call read again 4 | -------------------------------------------------------------------------------- /examples/random_dot_org/random_dot_org.cpp: -------------------------------------------------------------------------------- 1 | #include "tls.h" 2 | 3 | static volatile char currentRandomHexString[1024]; 4 | 5 | TLS tls; 6 | 7 | void logFailure(const char *whichCall, int error) { 8 | if (error != 0) { 9 | String msg = String("TLS ") + String(whichCall); 10 | msg += String(" failed with error ") + String(error); 11 | Serial.println(msg); 12 | Particle.publish("tls-fail", msg, 60, PRIVATE); 13 | } 14 | } 15 | 16 | int updateRandomHexString(String _) { 17 | int error = tls.connect("www.random.org", "443"); 18 | logFailure("connect", error); 19 | 20 | tls.write("GET /cgi-bin/randbyte HTTP/1.1\r\n" 21 | "Host: www.random.org\r\n\r\n"); 22 | int charsReadOrError = tls.read(currentRandomHexString, 1024); 23 | Particle.publish("tls-chars-read-or-error", charsReadOrError, 300000, PRIVATE); 24 | tls.close(); 25 | return charsReadOrError; 26 | } 27 | 28 | void setup() { 29 | Serial.begin(); 30 | Particle.variable(currentRandomHexString); 31 | Particle.function("update-rand", updateRandomHexString); 32 | 33 | int error = tls.init(); 34 | logFailure("init", error); 35 | } 36 | 37 | void loop() { 38 | // update random hex string every 5 minutes 39 | static unsigned long t = millis(); 40 | if (millis() - t > 300000) { 41 | t = millis(); 42 | updateRandomHexString(String()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=TLS 2 | version=0.1.0 3 | author=Zachary Crockett (towynlin) 4 | url=https://github.com/towynlin/particle-tls-library 5 | includes=tls.h 6 | -------------------------------------------------------------------------------- /src/mbedtls/aesni.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file aesni.h 3 | * 4 | * \brief AES-NI for hardware AES acceleration on some Intel processors 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_AESNI_H 24 | #define MBEDTLS_AESNI_H 25 | 26 | #include "aes.h" 27 | 28 | #define MBEDTLS_AESNI_AES 0x02000000u 29 | #define MBEDTLS_AESNI_CLMUL 0x00000002u 30 | 31 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && \ 32 | ( defined(__amd64__) || defined(__x86_64__) ) && \ 33 | ! defined(MBEDTLS_HAVE_X86_64) 34 | #define MBEDTLS_HAVE_X86_64 35 | #endif 36 | 37 | #if defined(MBEDTLS_HAVE_X86_64) 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * \brief AES-NI features detection routine 45 | * 46 | * \param what The feature to detect 47 | * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) 48 | * 49 | * \return 1 if CPU has support for the feature, 0 otherwise 50 | */ 51 | int mbedtls_aesni_has_support( unsigned int what ); 52 | 53 | /** 54 | * \brief AES-NI AES-ECB block en(de)cryption 55 | * 56 | * \param ctx AES context 57 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 58 | * \param input 16-byte input block 59 | * \param output 16-byte output block 60 | * 61 | * \return 0 on success (cannot fail) 62 | */ 63 | int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx, 64 | int mode, 65 | const unsigned char input[16], 66 | unsigned char output[16] ); 67 | 68 | /** 69 | * \brief GCM multiplication: c = a * b in GF(2^128) 70 | * 71 | * \param c Result 72 | * \param a First operand 73 | * \param b Second operand 74 | * 75 | * \note Both operands and result are bit strings interpreted as 76 | * elements of GF(2^128) as per the GCM spec. 77 | */ 78 | void mbedtls_aesni_gcm_mult( unsigned char c[16], 79 | const unsigned char a[16], 80 | const unsigned char b[16] ); 81 | 82 | /** 83 | * \brief Compute decryption round keys from encryption round keys 84 | * 85 | * \param invkey Round keys for the equivalent inverse cipher 86 | * \param fwdkey Original round keys (for encryption) 87 | * \param nr Number of rounds (that is, number of round keys minus one) 88 | */ 89 | void mbedtls_aesni_inverse_key( unsigned char *invkey, 90 | const unsigned char *fwdkey, int nr ); 91 | 92 | /** 93 | * \brief Perform key expansion (for encryption) 94 | * 95 | * \param rk Destination buffer where the round keys are written 96 | * \param key Encryption key 97 | * \param bits Key size in bits (must be 128, 192 or 256) 98 | * 99 | * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH 100 | */ 101 | int mbedtls_aesni_setkey_enc( unsigned char *rk, 102 | const unsigned char *key, 103 | size_t bits ); 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | 109 | #endif /* MBEDTLS_HAVE_X86_64 */ 110 | 111 | #endif /* MBEDTLS_AESNI_H */ 112 | -------------------------------------------------------------------------------- /src/mbedtls/arc4.c: -------------------------------------------------------------------------------- 1 | /* 2 | * An implementation of the ARCFOUR algorithm 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | /* 22 | * The ARCFOUR algorithm was publicly disclosed on 94/09. 23 | * 24 | * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0 25 | */ 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "mbedtls/config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #if defined(MBEDTLS_ARC4_C) 34 | 35 | #include "mbedtls/arc4.h" 36 | 37 | #include 38 | 39 | #if defined(MBEDTLS_SELF_TEST) 40 | #if defined(MBEDTLS_PLATFORM_C) 41 | #include "mbedtls/platform.h" 42 | #else 43 | #include 44 | #define mbedtls_printf printf 45 | #endif /* MBEDTLS_PLATFORM_C */ 46 | #endif /* MBEDTLS_SELF_TEST */ 47 | 48 | #if !defined(MBEDTLS_ARC4_ALT) 49 | 50 | /* Implementation that should never be optimized out by the compiler */ 51 | static void mbedtls_zeroize( void *v, size_t n ) { 52 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; 53 | } 54 | 55 | void mbedtls_arc4_init( mbedtls_arc4_context *ctx ) 56 | { 57 | memset( ctx, 0, sizeof( mbedtls_arc4_context ) ); 58 | } 59 | 60 | void mbedtls_arc4_free( mbedtls_arc4_context *ctx ) 61 | { 62 | if( ctx == NULL ) 63 | return; 64 | 65 | mbedtls_zeroize( ctx, sizeof( mbedtls_arc4_context ) ); 66 | } 67 | 68 | /* 69 | * ARC4 key schedule 70 | */ 71 | void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key, 72 | unsigned int keylen ) 73 | { 74 | int i, j, a; 75 | unsigned int k; 76 | unsigned char *m; 77 | 78 | ctx->x = 0; 79 | ctx->y = 0; 80 | m = ctx->m; 81 | 82 | for( i = 0; i < 256; i++ ) 83 | m[i] = (unsigned char) i; 84 | 85 | j = k = 0; 86 | 87 | for( i = 0; i < 256; i++, k++ ) 88 | { 89 | if( k >= keylen ) k = 0; 90 | 91 | a = m[i]; 92 | j = ( j + a + key[k] ) & 0xFF; 93 | m[i] = m[j]; 94 | m[j] = (unsigned char) a; 95 | } 96 | } 97 | 98 | /* 99 | * ARC4 cipher function 100 | */ 101 | int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input, 102 | unsigned char *output ) 103 | { 104 | int x, y, a, b; 105 | size_t i; 106 | unsigned char *m; 107 | 108 | x = ctx->x; 109 | y = ctx->y; 110 | m = ctx->m; 111 | 112 | for( i = 0; i < length; i++ ) 113 | { 114 | x = ( x + 1 ) & 0xFF; a = m[x]; 115 | y = ( y + a ) & 0xFF; b = m[y]; 116 | 117 | m[x] = (unsigned char) b; 118 | m[y] = (unsigned char) a; 119 | 120 | output[i] = (unsigned char) 121 | ( input[i] ^ m[(unsigned char)( a + b )] ); 122 | } 123 | 124 | ctx->x = x; 125 | ctx->y = y; 126 | 127 | return( 0 ); 128 | } 129 | 130 | #endif /* !MBEDTLS_ARC4_ALT */ 131 | 132 | #if defined(MBEDTLS_SELF_TEST) 133 | /* 134 | * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994: 135 | * 136 | * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0 137 | */ 138 | static const unsigned char arc4_test_key[3][8] = 139 | { 140 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, 141 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, 142 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } 143 | }; 144 | 145 | static const unsigned char arc4_test_pt[3][8] = 146 | { 147 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, 148 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 149 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } 150 | }; 151 | 152 | static const unsigned char arc4_test_ct[3][8] = 153 | { 154 | { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 }, 155 | { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 }, 156 | { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A } 157 | }; 158 | 159 | /* 160 | * Checkup routine 161 | */ 162 | int mbedtls_arc4_self_test( int verbose ) 163 | { 164 | int i, ret = 0; 165 | unsigned char ibuf[8]; 166 | unsigned char obuf[8]; 167 | mbedtls_arc4_context ctx; 168 | 169 | mbedtls_arc4_init( &ctx ); 170 | 171 | for( i = 0; i < 3; i++ ) 172 | { 173 | if( verbose != 0 ) 174 | mbedtls_printf( " ARC4 test #%d: ", i + 1 ); 175 | 176 | memcpy( ibuf, arc4_test_pt[i], 8 ); 177 | 178 | mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 ); 179 | mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf ); 180 | 181 | if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 ) 182 | { 183 | if( verbose != 0 ) 184 | mbedtls_printf( "failed\n" ); 185 | 186 | ret = 1; 187 | goto exit; 188 | } 189 | 190 | if( verbose != 0 ) 191 | mbedtls_printf( "passed\n" ); 192 | } 193 | 194 | if( verbose != 0 ) 195 | mbedtls_printf( "\n" ); 196 | 197 | exit: 198 | mbedtls_arc4_free( &ctx ); 199 | 200 | return( ret ); 201 | } 202 | 203 | #endif /* MBEDTLS_SELF_TEST */ 204 | 205 | #endif /* MBEDTLS_ARC4_C */ 206 | -------------------------------------------------------------------------------- /src/mbedtls/arc4.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file arc4.h 3 | * 4 | * \brief The ARCFOUR stream cipher 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_ARC4_H 24 | #define MBEDTLS_ARC4_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | 34 | #if !defined(MBEDTLS_ARC4_ALT) 35 | // Regular implementation 36 | // 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * \brief ARC4 context structure 44 | */ 45 | typedef struct 46 | { 47 | int x; /*!< permutation index */ 48 | int y; /*!< permutation index */ 49 | unsigned char m[256]; /*!< permutation table */ 50 | } 51 | mbedtls_arc4_context; 52 | 53 | /** 54 | * \brief Initialize ARC4 context 55 | * 56 | * \param ctx ARC4 context to be initialized 57 | */ 58 | void mbedtls_arc4_init( mbedtls_arc4_context *ctx ); 59 | 60 | /** 61 | * \brief Clear ARC4 context 62 | * 63 | * \param ctx ARC4 context to be cleared 64 | */ 65 | void mbedtls_arc4_free( mbedtls_arc4_context *ctx ); 66 | 67 | /** 68 | * \brief ARC4 key schedule 69 | * 70 | * \param ctx ARC4 context to be setup 71 | * \param key the secret key 72 | * \param keylen length of the key, in bytes 73 | */ 74 | void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key, 75 | unsigned int keylen ); 76 | 77 | /** 78 | * \brief ARC4 cipher function 79 | * 80 | * \param ctx ARC4 context 81 | * \param length length of the input data 82 | * \param input buffer holding the input data 83 | * \param output buffer for the output data 84 | * 85 | * \return 0 if successful 86 | */ 87 | int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input, 88 | unsigned char *output ); 89 | 90 | #ifdef __cplusplus 91 | } 92 | #endif 93 | 94 | #else /* MBEDTLS_ARC4_ALT */ 95 | #include "arc4_alt.h" 96 | #endif /* MBEDTLS_ARC4_ALT */ 97 | 98 | #ifdef __cplusplus 99 | extern "C" { 100 | #endif 101 | 102 | /** 103 | * \brief Checkup routine 104 | * 105 | * \return 0 if successful, or 1 if the test failed 106 | */ 107 | int mbedtls_arc4_self_test( int verbose ); 108 | 109 | #ifdef __cplusplus 110 | } 111 | #endif 112 | 113 | #endif /* arc4.h */ 114 | -------------------------------------------------------------------------------- /src/mbedtls/base64.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file base64.h 3 | * 4 | * \brief RFC 1521 base64 encoding/decoding 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_BASE64_H 24 | #define MBEDTLS_BASE64_H 25 | 26 | #include 27 | 28 | #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */ 29 | #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */ 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | /** 36 | * \brief Encode a buffer into base64 format 37 | * 38 | * \param dst destination buffer 39 | * \param dlen size of the destination buffer 40 | * \param olen number of bytes written 41 | * \param src source buffer 42 | * \param slen amount of data to be encoded 43 | * 44 | * \return 0 if successful, or MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL. 45 | * *olen is always updated to reflect the amount 46 | * of data that has (or would have) been written. 47 | * If that length cannot be represented, then no data is 48 | * written to the buffer and *olen is set to the maximum 49 | * length representable as a size_t. 50 | * 51 | * \note Call this function with dlen = 0 to obtain the 52 | * required buffer size in *olen 53 | */ 54 | int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, 55 | const unsigned char *src, size_t slen ); 56 | 57 | /** 58 | * \brief Decode a base64-formatted buffer 59 | * 60 | * \param dst destination buffer (can be NULL for checking size) 61 | * \param dlen size of the destination buffer 62 | * \param olen number of bytes written 63 | * \param src source buffer 64 | * \param slen amount of data to be decoded 65 | * 66 | * \return 0 if successful, MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL, or 67 | * MBEDTLS_ERR_BASE64_INVALID_CHARACTER if the input data is 68 | * not correct. *olen is always updated to reflect the amount 69 | * of data that has (or would have) been written. 70 | * 71 | * \note Call this function with *dst = NULL or dlen = 0 to obtain 72 | * the required buffer size in *olen 73 | */ 74 | int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, 75 | const unsigned char *src, size_t slen ); 76 | 77 | /** 78 | * \brief Checkup routine 79 | * 80 | * \return 0 if successful, or 1 if the test failed 81 | */ 82 | int mbedtls_base64_self_test( int verbose ); 83 | 84 | #ifdef __cplusplus 85 | } 86 | #endif 87 | 88 | #endif /* base64.h */ 89 | -------------------------------------------------------------------------------- /src/mbedtls/blowfish.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file blowfish.h 3 | * 4 | * \brief Blowfish block cipher 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_BLOWFISH_H 24 | #define MBEDTLS_BLOWFISH_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #define MBEDTLS_BLOWFISH_ENCRYPT 1 36 | #define MBEDTLS_BLOWFISH_DECRYPT 0 37 | #define MBEDTLS_BLOWFISH_MAX_KEY_BITS 448 38 | #define MBEDTLS_BLOWFISH_MIN_KEY_BITS 32 39 | #define MBEDTLS_BLOWFISH_ROUNDS 16 /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ 40 | #define MBEDTLS_BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ 41 | 42 | #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ 43 | #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ 44 | 45 | #if !defined(MBEDTLS_BLOWFISH_ALT) 46 | // Regular implementation 47 | // 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | /** 54 | * \brief Blowfish context structure 55 | */ 56 | typedef struct 57 | { 58 | uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ 59 | uint32_t S[4][256]; /*!< key dependent S-boxes */ 60 | } 61 | mbedtls_blowfish_context; 62 | 63 | /** 64 | * \brief Initialize Blowfish context 65 | * 66 | * \param ctx Blowfish context to be initialized 67 | */ 68 | void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ); 69 | 70 | /** 71 | * \brief Clear Blowfish context 72 | * 73 | * \param ctx Blowfish context to be cleared 74 | */ 75 | void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ); 76 | 77 | /** 78 | * \brief Blowfish key schedule 79 | * 80 | * \param ctx Blowfish context to be initialized 81 | * \param key encryption key 82 | * \param keybits must be between 32 and 448 bits 83 | * 84 | * \return 0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH 85 | */ 86 | int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key, 87 | unsigned int keybits ); 88 | 89 | /** 90 | * \brief Blowfish-ECB block encryption/decryption 91 | * 92 | * \param ctx Blowfish context 93 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT 94 | * \param input 8-byte input block 95 | * \param output 8-byte output block 96 | * 97 | * \return 0 if successful 98 | */ 99 | int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, 100 | int mode, 101 | const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE], 102 | unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] ); 103 | 104 | #if defined(MBEDTLS_CIPHER_MODE_CBC) 105 | /** 106 | * \brief Blowfish-CBC buffer encryption/decryption 107 | * Length should be a multiple of the block 108 | * size (8 bytes) 109 | * 110 | * \note Upon exit, the content of the IV is updated so that you can 111 | * call the function same function again on the following 112 | * block(s) of data and get the same result as if it was 113 | * encrypted in one call. This allows a "streaming" usage. 114 | * If on the other hand you need to retain the contents of the 115 | * IV, you should either save it manually or use the cipher 116 | * module instead. 117 | * 118 | * \param ctx Blowfish context 119 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT 120 | * \param length length of the input data 121 | * \param iv initialization vector (updated after use) 122 | * \param input buffer holding the input data 123 | * \param output buffer holding the output data 124 | * 125 | * \return 0 if successful, or 126 | * MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH 127 | */ 128 | int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, 129 | int mode, 130 | size_t length, 131 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], 132 | const unsigned char *input, 133 | unsigned char *output ); 134 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ 135 | 136 | #if defined(MBEDTLS_CIPHER_MODE_CFB) 137 | /** 138 | * \brief Blowfish CFB buffer encryption/decryption. 139 | * 140 | * \note Upon exit, the content of the IV is updated so that you can 141 | * call the function same function again on the following 142 | * block(s) of data and get the same result as if it was 143 | * encrypted in one call. This allows a "streaming" usage. 144 | * If on the other hand you need to retain the contents of the 145 | * IV, you should either save it manually or use the cipher 146 | * module instead. 147 | * 148 | * \param ctx Blowfish context 149 | * \param mode MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT 150 | * \param length length of the input data 151 | * \param iv_off offset in IV (updated after use) 152 | * \param iv initialization vector (updated after use) 153 | * \param input buffer holding the input data 154 | * \param output buffer holding the output data 155 | * 156 | * \return 0 if successful 157 | */ 158 | int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, 159 | int mode, 160 | size_t length, 161 | size_t *iv_off, 162 | unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE], 163 | const unsigned char *input, 164 | unsigned char *output ); 165 | #endif /*MBEDTLS_CIPHER_MODE_CFB */ 166 | 167 | #if defined(MBEDTLS_CIPHER_MODE_CTR) 168 | /** 169 | * \brief Blowfish-CTR buffer encryption/decryption 170 | * 171 | * Warning: You have to keep the maximum use of your counter in mind! 172 | * 173 | * \param ctx Blowfish context 174 | * \param length The length of the data 175 | * \param nc_off The offset in the current stream_block (for resuming 176 | * within current cipher stream). The offset pointer to 177 | * should be 0 at the start of a stream. 178 | * \param nonce_counter The 64-bit nonce and counter. 179 | * \param stream_block The saved stream-block for resuming. Is overwritten 180 | * by the function. 181 | * \param input The input data stream 182 | * \param output The output data stream 183 | * 184 | * \return 0 if successful 185 | */ 186 | int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, 187 | size_t length, 188 | size_t *nc_off, 189 | unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE], 190 | unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE], 191 | const unsigned char *input, 192 | unsigned char *output ); 193 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ 194 | 195 | #ifdef __cplusplus 196 | } 197 | #endif 198 | 199 | #else /* MBEDTLS_BLOWFISH_ALT */ 200 | #include "blowfish_alt.h" 201 | #endif /* MBEDTLS_BLOWFISH_ALT */ 202 | 203 | #endif /* blowfish.h */ 204 | -------------------------------------------------------------------------------- /src/mbedtls/ccm.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ccm.h 3 | * 4 | * \brief Counter with CBC-MAC (CCM) for 128-bit block ciphers 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_CCM_H 24 | #define MBEDTLS_CCM_H 25 | 26 | #include "cipher.h" 27 | 28 | #define MBEDTLS_ERR_CCM_BAD_INPUT -0x000D /**< Bad input parameters to function. */ 29 | #define MBEDTLS_ERR_CCM_AUTH_FAILED -0x000F /**< Authenticated decryption failed. */ 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | /** 36 | * \brief CCM context structure 37 | */ 38 | typedef struct { 39 | mbedtls_cipher_context_t cipher_ctx; /*!< cipher context used */ 40 | } 41 | mbedtls_ccm_context; 42 | 43 | /** 44 | * \brief Initialize CCM context (just makes references valid) 45 | * Makes the context ready for mbedtls_ccm_setkey() or 46 | * mbedtls_ccm_free(). 47 | * 48 | * \param ctx CCM context to initialize 49 | */ 50 | void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); 51 | 52 | /** 53 | * \brief CCM initialization (encryption and decryption) 54 | * 55 | * \param ctx CCM context to be initialized 56 | * \param cipher cipher to use (a 128-bit block cipher) 57 | * \param key encryption key 58 | * \param keybits key size in bits (must be acceptable by the cipher) 59 | * 60 | * \return 0 if successful, or a cipher specific error code 61 | */ 62 | int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, 63 | mbedtls_cipher_id_t cipher, 64 | const unsigned char *key, 65 | unsigned int keybits ); 66 | 67 | /** 68 | * \brief Free a CCM context and underlying cipher sub-context 69 | * 70 | * \param ctx CCM context to free 71 | */ 72 | void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); 73 | 74 | /** 75 | * \brief CCM buffer encryption 76 | * 77 | * \param ctx CCM context 78 | * \param length length of the input data in bytes 79 | * \param iv nonce (initialization vector) 80 | * \param iv_len length of IV in bytes 81 | * must be 2, 3, 4, 5, 6, 7 or 8 82 | * \param add additional data 83 | * \param add_len length of additional data in bytes 84 | * must be less than 2^16 - 2^8 85 | * \param input buffer holding the input data 86 | * \param output buffer for holding the output data 87 | * must be at least 'length' bytes wide 88 | * \param tag buffer for holding the tag 89 | * \param tag_len length of the tag to generate in bytes 90 | * must be 4, 6, 8, 10, 14 or 16 91 | * 92 | * \note The tag is written to a separate buffer. To get the tag 93 | * concatenated with the output as in the CCM spec, use 94 | * tag = output + length and make sure the output buffer is 95 | * at least length + tag_len wide. 96 | * 97 | * \return 0 if successful 98 | */ 99 | int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, 100 | const unsigned char *iv, size_t iv_len, 101 | const unsigned char *add, size_t add_len, 102 | const unsigned char *input, unsigned char *output, 103 | unsigned char *tag, size_t tag_len ); 104 | 105 | /** 106 | * \brief CCM buffer authenticated decryption 107 | * 108 | * \param ctx CCM context 109 | * \param length length of the input data 110 | * \param iv initialization vector 111 | * \param iv_len length of IV 112 | * \param add additional data 113 | * \param add_len length of additional data 114 | * \param input buffer holding the input data 115 | * \param output buffer for holding the output data 116 | * \param tag buffer holding the tag 117 | * \param tag_len length of the tag 118 | * 119 | * \return 0 if successful and authenticated, 120 | * MBEDTLS_ERR_CCM_AUTH_FAILED if tag does not match 121 | */ 122 | int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, 123 | const unsigned char *iv, size_t iv_len, 124 | const unsigned char *add, size_t add_len, 125 | const unsigned char *input, unsigned char *output, 126 | const unsigned char *tag, size_t tag_len ); 127 | 128 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) 129 | /** 130 | * \brief Checkup routine 131 | * 132 | * \return 0 if successful, or 1 if the test failed 133 | */ 134 | int mbedtls_ccm_self_test( int verbose ); 135 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ 136 | 137 | #ifdef __cplusplus 138 | } 139 | #endif 140 | 141 | #endif /* MBEDTLS_CCM_H */ 142 | -------------------------------------------------------------------------------- /src/mbedtls/certs.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file certs.h 3 | * 4 | * \brief Sample certificates and DHM parameters for testing 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_CERTS_H 24 | #define MBEDTLS_CERTS_H 25 | 26 | #include 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | #if defined(MBEDTLS_PEM_PARSE_C) 33 | /* Concatenation of all CA certificates in PEM format if available */ 34 | extern const char mbedtls_test_cas_pem[]; 35 | extern const size_t mbedtls_test_cas_pem_len; 36 | #endif 37 | 38 | /* List of all CA certificates, terminated by NULL */ 39 | extern const char * mbedtls_test_cas[]; 40 | extern const size_t mbedtls_test_cas_len[]; 41 | 42 | /* 43 | * Convenience for users who just want a certificate: 44 | * RSA by default, or ECDSA if RSA is not available 45 | */ 46 | extern const char * mbedtls_test_ca_crt; 47 | extern const size_t mbedtls_test_ca_crt_len; 48 | extern const char * mbedtls_test_ca_key; 49 | extern const size_t mbedtls_test_ca_key_len; 50 | extern const char * mbedtls_test_ca_pwd; 51 | extern const size_t mbedtls_test_ca_pwd_len; 52 | extern const char * mbedtls_test_srv_crt; 53 | extern const size_t mbedtls_test_srv_crt_len; 54 | extern const char * mbedtls_test_srv_key; 55 | extern const size_t mbedtls_test_srv_key_len; 56 | extern const char * mbedtls_test_cli_crt; 57 | extern const size_t mbedtls_test_cli_crt_len; 58 | extern const char * mbedtls_test_cli_key; 59 | extern const size_t mbedtls_test_cli_key_len; 60 | 61 | #if defined(MBEDTLS_ECDSA_C) 62 | extern const char mbedtls_test_ca_crt_ec[]; 63 | extern const size_t mbedtls_test_ca_crt_ec_len; 64 | extern const char mbedtls_test_ca_key_ec[]; 65 | extern const size_t mbedtls_test_ca_key_ec_len; 66 | extern const char mbedtls_test_ca_pwd_ec[]; 67 | extern const size_t mbedtls_test_ca_pwd_ec_len; 68 | extern const char mbedtls_test_srv_crt_ec[]; 69 | extern const size_t mbedtls_test_srv_crt_ec_len; 70 | extern const char mbedtls_test_srv_key_ec[]; 71 | extern const size_t mbedtls_test_srv_key_ec_len; 72 | extern const char mbedtls_test_cli_crt_ec[]; 73 | extern const size_t mbedtls_test_cli_crt_ec_len; 74 | extern const char mbedtls_test_cli_key_ec[]; 75 | extern const size_t mbedtls_test_cli_key_ec_len; 76 | #endif 77 | 78 | #if defined(MBEDTLS_RSA_C) 79 | extern const char mbedtls_test_ca_crt_rsa[]; 80 | extern const size_t mbedtls_test_ca_crt_rsa_len; 81 | extern const char mbedtls_test_ca_key_rsa[]; 82 | extern const size_t mbedtls_test_ca_key_rsa_len; 83 | extern const char mbedtls_test_ca_pwd_rsa[]; 84 | extern const size_t mbedtls_test_ca_pwd_rsa_len; 85 | extern const char mbedtls_test_srv_crt_rsa[]; 86 | extern const size_t mbedtls_test_srv_crt_rsa_len; 87 | extern const char mbedtls_test_srv_key_rsa[]; 88 | extern const size_t mbedtls_test_srv_key_rsa_len; 89 | extern const char mbedtls_test_cli_crt_rsa[]; 90 | extern const size_t mbedtls_test_cli_crt_rsa_len; 91 | extern const char mbedtls_test_cli_key_rsa[]; 92 | extern const size_t mbedtls_test_cli_key_rsa_len; 93 | #endif 94 | 95 | #ifdef __cplusplus 96 | } 97 | #endif 98 | 99 | #endif /* certs.h */ 100 | -------------------------------------------------------------------------------- /src/mbedtls/cipher_internal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file cipher_internal.h 3 | * 4 | * \brief Cipher wrappers. 5 | * 6 | * \author Adriaan de Jong 7 | * 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 9 | * SPDX-License-Identifier: Apache-2.0 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 12 | * not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | * 23 | * This file is part of mbed TLS (https://tls.mbed.org) 24 | */ 25 | #ifndef MBEDTLS_CIPHER_WRAP_H 26 | #define MBEDTLS_CIPHER_WRAP_H 27 | 28 | #if !defined(MBEDTLS_CONFIG_FILE) 29 | #include "config.h" 30 | #else 31 | #include MBEDTLS_CONFIG_FILE 32 | #endif 33 | 34 | #include "cipher.h" 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | /** 41 | * Base cipher information. The non-mode specific functions and values. 42 | */ 43 | struct mbedtls_cipher_base_t 44 | { 45 | /** Base Cipher type (e.g. MBEDTLS_CIPHER_ID_AES) */ 46 | mbedtls_cipher_id_t cipher; 47 | 48 | /** Encrypt using ECB */ 49 | int (*ecb_func)( void *ctx, mbedtls_operation_t mode, 50 | const unsigned char *input, unsigned char *output ); 51 | 52 | #if defined(MBEDTLS_CIPHER_MODE_CBC) 53 | /** Encrypt using CBC */ 54 | int (*cbc_func)( void *ctx, mbedtls_operation_t mode, size_t length, 55 | unsigned char *iv, const unsigned char *input, 56 | unsigned char *output ); 57 | #endif 58 | 59 | #if defined(MBEDTLS_CIPHER_MODE_CFB) 60 | /** Encrypt using CFB (Full length) */ 61 | int (*cfb_func)( void *ctx, mbedtls_operation_t mode, size_t length, size_t *iv_off, 62 | unsigned char *iv, const unsigned char *input, 63 | unsigned char *output ); 64 | #endif 65 | 66 | #if defined(MBEDTLS_CIPHER_MODE_CTR) 67 | /** Encrypt using CTR */ 68 | int (*ctr_func)( void *ctx, size_t length, size_t *nc_off, 69 | unsigned char *nonce_counter, unsigned char *stream_block, 70 | const unsigned char *input, unsigned char *output ); 71 | #endif 72 | 73 | #if defined(MBEDTLS_CIPHER_MODE_STREAM) 74 | /** Encrypt using STREAM */ 75 | int (*stream_func)( void *ctx, size_t length, 76 | const unsigned char *input, unsigned char *output ); 77 | #endif 78 | 79 | /** Set key for encryption purposes */ 80 | int (*setkey_enc_func)( void *ctx, const unsigned char *key, 81 | unsigned int key_bitlen ); 82 | 83 | /** Set key for decryption purposes */ 84 | int (*setkey_dec_func)( void *ctx, const unsigned char *key, 85 | unsigned int key_bitlen); 86 | 87 | /** Allocate a new context */ 88 | void * (*ctx_alloc_func)( void ); 89 | 90 | /** Free the given context */ 91 | void (*ctx_free_func)( void *ctx ); 92 | 93 | }; 94 | 95 | typedef struct 96 | { 97 | mbedtls_cipher_type_t type; 98 | const mbedtls_cipher_info_t *info; 99 | } mbedtls_cipher_definition_t; 100 | 101 | extern const mbedtls_cipher_definition_t mbedtls_cipher_definitions[]; 102 | 103 | extern int mbedtls_cipher_supported[]; 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | 109 | #endif /* MBEDTLS_CIPHER_WRAP_H */ 110 | -------------------------------------------------------------------------------- /src/mbedtls/cmac.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file cmac.h 3 | * 4 | * \brief Cipher-based Message Authentication Code (CMAC) Mode for 5 | * Authentication 6 | * 7 | * Copyright (C) 2015-2016, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_CMAC_H 25 | #define MBEDTLS_CMAC_H 26 | 27 | #include "mbedtls/cipher.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | #define MBEDTLS_AES_BLOCK_SIZE 16 34 | #define MBEDTLS_DES3_BLOCK_SIZE 8 35 | 36 | #if defined(MBEDTLS_AES_C) 37 | #define MBEDTLS_CIPHER_BLKSIZE_MAX 16 /* longest used by CMAC is AES */ 38 | #else 39 | #define MBEDTLS_CIPHER_BLKSIZE_MAX 8 /* longest used by CMAC is 3DES */ 40 | #endif 41 | 42 | /** 43 | * CMAC context structure - Contains internal state information only 44 | */ 45 | struct mbedtls_cmac_context_t 46 | { 47 | /** Internal state of the CMAC algorithm */ 48 | unsigned char state[MBEDTLS_CIPHER_BLKSIZE_MAX]; 49 | 50 | /** Unprocessed data - either data that was not block aligned and is still 51 | * pending to be processed, or the final block */ 52 | unsigned char unprocessed_block[MBEDTLS_CIPHER_BLKSIZE_MAX]; 53 | 54 | /** Length of data pending to be processed */ 55 | size_t unprocessed_len; 56 | }; 57 | 58 | /** 59 | * \brief Set the CMAC key and prepare to authenticate the input 60 | * data. 61 | * Should be called with an initialised cipher context. 62 | * 63 | * \param ctx Cipher context 64 | * \param key CMAC key 65 | * \param keybits length of the CMAC key in bits 66 | * (must be acceptable by the cipher) 67 | * 68 | * \return 0 if successful, or a cipher specific error code 69 | */ 70 | int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx, 71 | const unsigned char *key, size_t keybits ); 72 | 73 | /** 74 | * \brief Generic CMAC process buffer. 75 | * Called between mbedtls_cipher_cmac_starts() or 76 | * mbedtls_cipher_cmac_reset() and 77 | * mbedtls_cipher_cmac_finish(). 78 | * May be called repeatedly. 79 | * 80 | * \param ctx CMAC context 81 | * \param input buffer holding the data 82 | * \param ilen length of the input data 83 | * 84 | * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter 85 | * verification fails. 86 | */ 87 | int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx, 88 | const unsigned char *input, size_t ilen ); 89 | 90 | /** 91 | * \brief Output CMAC. 92 | * Called after mbedtls_cipher_cmac_update(). 93 | * Usually followed by mbedtls_cipher_cmac_reset(), then 94 | * mbedtls_cipher_cmac_starts(), or mbedtls_cipher_free(). 95 | * 96 | * \param ctx CMAC context 97 | * \param output Generic CMAC checksum result 98 | * 99 | * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter 100 | * verification fails. 101 | */ 102 | int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx, 103 | unsigned char *output ); 104 | 105 | /** 106 | * \brief Prepare to authenticate a new message with the same key. 107 | * Called after mbedtls_cipher_cmac_finish() and before 108 | * mbedtls_cipher_cmac_update(). 109 | * 110 | * \param ctx CMAC context to be reset 111 | * 112 | * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter 113 | * verification fails. 114 | */ 115 | int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx ); 116 | 117 | /** 118 | * \brief Output = Generic_CMAC( hmac key, input buffer ) 119 | * 120 | * \param cipher_info message digest info 121 | * \param key CMAC key 122 | * \param keylen length of the CMAC key in bits 123 | * \param input buffer holding the data 124 | * \param ilen length of the input data 125 | * \param output Generic CMAC-result 126 | * 127 | * \returns 0 on success, MBEDTLS_ERR_MD_BAD_INPUT_DATA if parameter 128 | * verification fails. 129 | */ 130 | int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info, 131 | const unsigned char *key, size_t keylen, 132 | const unsigned char *input, size_t ilen, 133 | unsigned char *output ); 134 | 135 | #if defined(MBEDTLS_AES_C) 136 | /** 137 | * \brief AES-CMAC-128-PRF 138 | * Implementation of (AES-CMAC-PRF-128), as defined in RFC 4615 139 | * 140 | * \param key PRF key 141 | * \param key_len PRF key length in bytes 142 | * \param input buffer holding the input data 143 | * \param in_len length of the input data in bytes 144 | * \param output buffer holding the generated pseudorandom output (16 bytes) 145 | * 146 | * \return 0 if successful 147 | */ 148 | int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_len, 149 | const unsigned char *input, size_t in_len, 150 | unsigned char output[16] ); 151 | #endif /* MBEDTLS_AES_C */ 152 | 153 | #if defined(MBEDTLS_SELF_TEST) && ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_DES_C) ) 154 | /** 155 | * \brief Checkup routine 156 | * 157 | * \return 0 if successful, or 1 if the test failed 158 | */ 159 | int mbedtls_cmac_self_test( int verbose ); 160 | #endif /* MBEDTLS_SELF_TEST && ( MBEDTLS_AES_C || MBEDTLS_DES_C ) */ 161 | 162 | #ifdef __cplusplus 163 | } 164 | #endif 165 | 166 | #endif /* MBEDTLS_CMAC_H */ 167 | -------------------------------------------------------------------------------- /src/mbedtls/ecdh.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Elliptic curve Diffie-Hellman 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | 22 | /* 23 | * References: 24 | * 25 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg 26 | * RFC 4492 27 | */ 28 | 29 | #if !defined(MBEDTLS_CONFIG_FILE) 30 | #include "mbedtls/config.h" 31 | #else 32 | #include MBEDTLS_CONFIG_FILE 33 | #endif 34 | 35 | #if defined(MBEDTLS_ECDH_C) 36 | 37 | #include "mbedtls/ecdh.h" 38 | 39 | #include 40 | 41 | /* 42 | * Generate public key: simple wrapper around mbedtls_ecp_gen_keypair 43 | */ 44 | int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, 45 | int (*f_rng)(void *, unsigned char *, size_t), 46 | void *p_rng ) 47 | { 48 | return mbedtls_ecp_gen_keypair( grp, d, Q, f_rng, p_rng ); 49 | } 50 | 51 | /* 52 | * Compute shared secret (SEC1 3.3.1) 53 | */ 54 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, 55 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, 56 | int (*f_rng)(void *, unsigned char *, size_t), 57 | void *p_rng ) 58 | { 59 | int ret; 60 | mbedtls_ecp_point P; 61 | 62 | mbedtls_ecp_point_init( &P ); 63 | 64 | /* 65 | * Make sure Q is a valid pubkey before using it 66 | */ 67 | MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) ); 68 | 69 | MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) ); 70 | 71 | if( mbedtls_ecp_is_zero( &P ) ) 72 | { 73 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; 74 | goto cleanup; 75 | } 76 | 77 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) ); 78 | 79 | cleanup: 80 | mbedtls_ecp_point_free( &P ); 81 | 82 | return( ret ); 83 | } 84 | 85 | /* 86 | * Initialize context 87 | */ 88 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ) 89 | { 90 | memset( ctx, 0, sizeof( mbedtls_ecdh_context ) ); 91 | } 92 | 93 | /* 94 | * Free context 95 | */ 96 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ) 97 | { 98 | if( ctx == NULL ) 99 | return; 100 | 101 | mbedtls_ecp_group_free( &ctx->grp ); 102 | mbedtls_ecp_point_free( &ctx->Q ); 103 | mbedtls_ecp_point_free( &ctx->Qp ); 104 | mbedtls_ecp_point_free( &ctx->Vi ); 105 | mbedtls_ecp_point_free( &ctx->Vf ); 106 | mbedtls_mpi_free( &ctx->d ); 107 | mbedtls_mpi_free( &ctx->z ); 108 | mbedtls_mpi_free( &ctx->_d ); 109 | } 110 | 111 | /* 112 | * Setup and write the ServerKeyExhange parameters (RFC 4492) 113 | * struct { 114 | * ECParameters curve_params; 115 | * ECPoint public; 116 | * } ServerECDHParams; 117 | */ 118 | int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, 119 | unsigned char *buf, size_t blen, 120 | int (*f_rng)(void *, unsigned char *, size_t), 121 | void *p_rng ) 122 | { 123 | int ret; 124 | size_t grp_len, pt_len; 125 | 126 | if( ctx == NULL || ctx->grp.pbits == 0 ) 127 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 128 | 129 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ) 130 | != 0 ) 131 | return( ret ); 132 | 133 | if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) ) 134 | != 0 ) 135 | return( ret ); 136 | 137 | buf += grp_len; 138 | blen -= grp_len; 139 | 140 | if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format, 141 | &pt_len, buf, blen ) ) != 0 ) 142 | return( ret ); 143 | 144 | *olen = grp_len + pt_len; 145 | return( 0 ); 146 | } 147 | 148 | /* 149 | * Read the ServerKeyExhange parameters (RFC 4492) 150 | * struct { 151 | * ECParameters curve_params; 152 | * ECPoint public; 153 | * } ServerECDHParams; 154 | */ 155 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, 156 | const unsigned char **buf, const unsigned char *end ) 157 | { 158 | int ret; 159 | 160 | if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 ) 161 | return( ret ); 162 | 163 | if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) ) 164 | != 0 ) 165 | return( ret ); 166 | 167 | return( 0 ); 168 | } 169 | 170 | /* 171 | * Get parameters from a keypair 172 | */ 173 | int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, 174 | mbedtls_ecdh_side side ) 175 | { 176 | int ret; 177 | 178 | if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ) 179 | return( ret ); 180 | 181 | /* If it's not our key, just import the public part as Qp */ 182 | if( side == MBEDTLS_ECDH_THEIRS ) 183 | return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) ); 184 | 185 | /* Our key: import public (as Q) and private parts */ 186 | if( side != MBEDTLS_ECDH_OURS ) 187 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 188 | 189 | if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 || 190 | ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ) 191 | return( ret ); 192 | 193 | return( 0 ); 194 | } 195 | 196 | /* 197 | * Setup and export the client public value 198 | */ 199 | int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, 200 | unsigned char *buf, size_t blen, 201 | int (*f_rng)(void *, unsigned char *, size_t), 202 | void *p_rng ) 203 | { 204 | int ret; 205 | 206 | if( ctx == NULL || ctx->grp.pbits == 0 ) 207 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 208 | 209 | if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ) 210 | != 0 ) 211 | return( ret ); 212 | 213 | return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format, 214 | olen, buf, blen ); 215 | } 216 | 217 | /* 218 | * Parse and import the client's public value 219 | */ 220 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, 221 | const unsigned char *buf, size_t blen ) 222 | { 223 | int ret; 224 | const unsigned char *p = buf; 225 | 226 | if( ctx == NULL ) 227 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 228 | 229 | if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 ) 230 | return( ret ); 231 | 232 | if( (size_t)( p - buf ) != blen ) 233 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 234 | 235 | return( 0 ); 236 | } 237 | 238 | /* 239 | * Derive and export the shared secret 240 | */ 241 | int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, 242 | unsigned char *buf, size_t blen, 243 | int (*f_rng)(void *, unsigned char *, size_t), 244 | void *p_rng ) 245 | { 246 | int ret; 247 | 248 | if( ctx == NULL ) 249 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 250 | 251 | if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d, 252 | f_rng, p_rng ) ) != 0 ) 253 | { 254 | return( ret ); 255 | } 256 | 257 | if( mbedtls_mpi_size( &ctx->z ) > blen ) 258 | return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); 259 | 260 | *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 ); 261 | return mbedtls_mpi_write_binary( &ctx->z, buf, *olen ); 262 | } 263 | 264 | #endif /* MBEDTLS_ECDH_C */ 265 | -------------------------------------------------------------------------------- /src/mbedtls/ecdh.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ecdh.h 3 | * 4 | * \brief Elliptic curve Diffie-Hellman 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_ECDH_H 24 | #define MBEDTLS_ECDH_H 25 | 26 | #include "ecp.h" 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | /** 33 | * When importing from an EC key, select if it is our key or the peer's key 34 | */ 35 | typedef enum 36 | { 37 | MBEDTLS_ECDH_OURS, 38 | MBEDTLS_ECDH_THEIRS, 39 | } mbedtls_ecdh_side; 40 | 41 | /** 42 | * \brief ECDH context structure 43 | */ 44 | typedef struct 45 | { 46 | mbedtls_ecp_group grp; /*!< elliptic curve used */ 47 | mbedtls_mpi d; /*!< our secret value (private key) */ 48 | mbedtls_ecp_point Q; /*!< our public value (public key) */ 49 | mbedtls_ecp_point Qp; /*!< peer's public value (public key) */ 50 | mbedtls_mpi z; /*!< shared secret */ 51 | int point_format; /*!< format for point export in TLS messages */ 52 | mbedtls_ecp_point Vi; /*!< blinding value (for later) */ 53 | mbedtls_ecp_point Vf; /*!< un-blinding value (for later) */ 54 | mbedtls_mpi _d; /*!< previous d (for later) */ 55 | } 56 | mbedtls_ecdh_context; 57 | 58 | /** 59 | * \brief Generate a public key. 60 | * Raw function that only does the core computation. 61 | * 62 | * \param grp ECP group 63 | * \param d Destination MPI (secret exponent, aka private key) 64 | * \param Q Destination point (public key) 65 | * \param f_rng RNG function 66 | * \param p_rng RNG parameter 67 | * 68 | * \return 0 if successful, 69 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code 70 | */ 71 | int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, 72 | int (*f_rng)(void *, unsigned char *, size_t), 73 | void *p_rng ); 74 | 75 | /** 76 | * \brief Compute shared secret 77 | * Raw function that only does the core computation. 78 | * 79 | * \param grp ECP group 80 | * \param z Destination MPI (shared secret) 81 | * \param Q Public key from other party 82 | * \param d Our secret exponent (private key) 83 | * \param f_rng RNG function (see notes) 84 | * \param p_rng RNG parameter 85 | * 86 | * \return 0 if successful, 87 | * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code 88 | * 89 | * \note If f_rng is not NULL, it is used to implement 90 | * countermeasures against potential elaborate timing 91 | * attacks, see \c mbedtls_ecp_mul() for details. 92 | */ 93 | int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, 94 | const mbedtls_ecp_point *Q, const mbedtls_mpi *d, 95 | int (*f_rng)(void *, unsigned char *, size_t), 96 | void *p_rng ); 97 | 98 | /** 99 | * \brief Initialize context 100 | * 101 | * \param ctx Context to initialize 102 | */ 103 | void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ); 104 | 105 | /** 106 | * \brief Free context 107 | * 108 | * \param ctx Context to free 109 | */ 110 | void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ); 111 | 112 | /** 113 | * \brief Generate a public key and a TLS ServerKeyExchange payload. 114 | * (First function used by a TLS server for ECDHE.) 115 | * 116 | * \param ctx ECDH context 117 | * \param olen number of chars written 118 | * \param buf destination buffer 119 | * \param blen length of buffer 120 | * \param f_rng RNG function 121 | * \param p_rng RNG parameter 122 | * 123 | * \note This function assumes that ctx->grp has already been 124 | * properly set (for example using mbedtls_ecp_group_load). 125 | * 126 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code 127 | */ 128 | int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, 129 | unsigned char *buf, size_t blen, 130 | int (*f_rng)(void *, unsigned char *, size_t), 131 | void *p_rng ); 132 | 133 | /** 134 | * \brief Parse and procress a TLS ServerKeyExhange payload. 135 | * (First function used by a TLS client for ECDHE.) 136 | * 137 | * \param ctx ECDH context 138 | * \param buf pointer to start of input buffer 139 | * \param end one past end of buffer 140 | * 141 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code 142 | */ 143 | int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, 144 | const unsigned char **buf, const unsigned char *end ); 145 | 146 | /** 147 | * \brief Setup an ECDH context from an EC key. 148 | * (Used by clients and servers in place of the 149 | * ServerKeyEchange for static ECDH: import ECDH parameters 150 | * from a certificate's EC key information.) 151 | * 152 | * \param ctx ECDH constext to set 153 | * \param key EC key to use 154 | * \param side Is it our key (1) or the peer's key (0) ? 155 | * 156 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code 157 | */ 158 | int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, 159 | mbedtls_ecdh_side side ); 160 | 161 | /** 162 | * \brief Generate a public key and a TLS ClientKeyExchange payload. 163 | * (Second function used by a TLS client for ECDH(E).) 164 | * 165 | * \param ctx ECDH context 166 | * \param olen number of bytes actually written 167 | * \param buf destination buffer 168 | * \param blen size of destination buffer 169 | * \param f_rng RNG function 170 | * \param p_rng RNG parameter 171 | * 172 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code 173 | */ 174 | int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, 175 | unsigned char *buf, size_t blen, 176 | int (*f_rng)(void *, unsigned char *, size_t), 177 | void *p_rng ); 178 | 179 | /** 180 | * \brief Parse and process a TLS ClientKeyExchange payload. 181 | * (Second function used by a TLS server for ECDH(E).) 182 | * 183 | * \param ctx ECDH context 184 | * \param buf start of input buffer 185 | * \param blen length of input buffer 186 | * 187 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code 188 | */ 189 | int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, 190 | const unsigned char *buf, size_t blen ); 191 | 192 | /** 193 | * \brief Derive and export the shared secret. 194 | * (Last function used by both TLS client en servers.) 195 | * 196 | * \param ctx ECDH context 197 | * \param olen number of bytes written 198 | * \param buf destination buffer 199 | * \param blen buffer length 200 | * \param f_rng RNG function, see notes for \c mbedtls_ecdh_compute_shared() 201 | * \param p_rng RNG parameter 202 | * 203 | * \return 0 if successful, or an MBEDTLS_ERR_ECP_XXX error code 204 | */ 205 | int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, 206 | unsigned char *buf, size_t blen, 207 | int (*f_rng)(void *, unsigned char *, size_t), 208 | void *p_rng ); 209 | 210 | #ifdef __cplusplus 211 | } 212 | #endif 213 | 214 | #endif /* ecdh.h */ 215 | -------------------------------------------------------------------------------- /src/mbedtls/entropy_poll.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Platform-specific and custom entropy polling functions 3 | * 4 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) 23 | #include "mbedtls/config.h" 24 | #else 25 | #include MBEDTLS_CONFIG_FILE 26 | #endif 27 | 28 | #if defined(MBEDTLS_ENTROPY_C) 29 | 30 | #include "mbedtls/entropy.h" 31 | #include "mbedtls/entropy_poll.h" 32 | 33 | #if defined(MBEDTLS_TIMING_C) 34 | #include 35 | #include "mbedtls/timing.h" 36 | #endif 37 | #if defined(MBEDTLS_HAVEGE_C) 38 | #include "mbedtls/havege.h" 39 | #endif 40 | #if defined(MBEDTLS_ENTROPY_NV_SEED) 41 | #include "mbedtls/platform.h" 42 | #endif 43 | 44 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) 45 | 46 | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ 47 | !defined(__APPLE__) && !defined(_WIN32) 48 | #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h" 49 | #endif 50 | 51 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) 52 | 53 | #if !defined(_WIN32_WINNT) 54 | #define _WIN32_WINNT 0x0400 55 | #endif 56 | #include 57 | #include 58 | 59 | int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len, 60 | size_t *olen ) 61 | { 62 | HCRYPTPROV provider; 63 | ((void) data); 64 | *olen = 0; 65 | 66 | if( CryptAcquireContext( &provider, NULL, NULL, 67 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) 68 | { 69 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 70 | } 71 | 72 | if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) 73 | { 74 | CryptReleaseContext( provider, 0 ); 75 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 76 | } 77 | 78 | CryptReleaseContext( provider, 0 ); 79 | *olen = len; 80 | 81 | return( 0 ); 82 | } 83 | #else /* _WIN32 && !EFIX64 && !EFI32 */ 84 | 85 | /* 86 | * Test for Linux getrandom() support. 87 | * Since there is no wrapper in the libc yet, use the generic syscall wrapper 88 | * available in GNU libc and compatible libc's (eg uClibc). 89 | */ 90 | #if defined(__linux__) && defined(__GLIBC__) 91 | #include 92 | #include 93 | #if defined(SYS_getrandom) 94 | #define HAVE_GETRANDOM 95 | 96 | static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) 97 | { 98 | /* MemSan cannot understand that the syscall writes to the buffer */ 99 | #if defined(__has_feature) 100 | #if __has_feature(memory_sanitizer) 101 | memset( buf, 0, buflen ); 102 | #endif 103 | #endif 104 | 105 | return( syscall( SYS_getrandom, buf, buflen, flags ) ); 106 | } 107 | 108 | #include 109 | /* Check if version is at least 3.17.0 */ 110 | static int check_version_3_17_plus( void ) 111 | { 112 | int minor; 113 | struct utsname un; 114 | const char *ver; 115 | 116 | /* Get version information */ 117 | uname(&un); 118 | ver = un.release; 119 | 120 | /* Check major version; assume a single digit */ 121 | if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' ) 122 | return( -1 ); 123 | 124 | if( ver[0] - '0' > 3 ) 125 | return( 0 ); 126 | 127 | /* Ok, so now we know major == 3, check minor. 128 | * Assume 1 or 2 digits. */ 129 | if( ver[2] < '0' || ver[2] > '9' ) 130 | return( -1 ); 131 | 132 | minor = ver[2] - '0'; 133 | 134 | if( ver[3] >= '0' && ver[3] <= '9' ) 135 | minor = 10 * minor + ver[3] - '0'; 136 | else if( ver [3] != '.' ) 137 | return( -1 ); 138 | 139 | if( minor < 17 ) 140 | return( -1 ); 141 | 142 | return( 0 ); 143 | } 144 | static int has_getrandom = -1; 145 | #endif /* SYS_getrandom */ 146 | #endif /* __linux__ */ 147 | 148 | #include 149 | 150 | int mbedtls_platform_entropy_poll( void *data, 151 | unsigned char *output, size_t len, size_t *olen ) 152 | { 153 | FILE *file; 154 | size_t read_len; 155 | ((void) data); 156 | 157 | #if defined(HAVE_GETRANDOM) 158 | if( has_getrandom == -1 ) 159 | has_getrandom = ( check_version_3_17_plus() == 0 ); 160 | 161 | if( has_getrandom ) 162 | { 163 | int ret; 164 | 165 | if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 ) 166 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 167 | 168 | *olen = ret; 169 | return( 0 ); 170 | } 171 | #endif /* HAVE_GETRANDOM */ 172 | 173 | *olen = 0; 174 | 175 | file = fopen( "/dev/urandom", "rb" ); 176 | if( file == NULL ) 177 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 178 | 179 | read_len = fread( output, 1, len, file ); 180 | if( read_len != len ) 181 | { 182 | fclose( file ); 183 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 184 | } 185 | 186 | fclose( file ); 187 | *olen = len; 188 | 189 | return( 0 ); 190 | } 191 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ 192 | #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */ 193 | 194 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) 195 | int mbedtls_null_entropy_poll( void *data, 196 | unsigned char *output, size_t len, size_t *olen ) 197 | { 198 | ((void) data); 199 | ((void) output); 200 | *olen = 0; 201 | 202 | if( len < sizeof(unsigned char) ) 203 | return( 0 ); 204 | 205 | *olen = sizeof(unsigned char); 206 | 207 | return( 0 ); 208 | } 209 | #endif 210 | 211 | #if defined(MBEDTLS_TIMING_C) 212 | int mbedtls_hardclock_poll( void *data, 213 | unsigned char *output, size_t len, size_t *olen ) 214 | { 215 | unsigned long timer = mbedtls_timing_hardclock(); 216 | ((void) data); 217 | *olen = 0; 218 | 219 | if( len < sizeof(unsigned long) ) 220 | return( 0 ); 221 | 222 | memcpy( output, &timer, sizeof(unsigned long) ); 223 | *olen = sizeof(unsigned long); 224 | 225 | return( 0 ); 226 | } 227 | #endif /* MBEDTLS_TIMING_C */ 228 | 229 | #if defined(MBEDTLS_HAVEGE_C) 230 | int mbedtls_havege_poll( void *data, 231 | unsigned char *output, size_t len, size_t *olen ) 232 | { 233 | mbedtls_havege_state *hs = (mbedtls_havege_state *) data; 234 | *olen = 0; 235 | 236 | if( mbedtls_havege_random( hs, output, len ) != 0 ) 237 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 238 | 239 | *olen = len; 240 | 241 | return( 0 ); 242 | } 243 | #endif /* MBEDTLS_HAVEGE_C */ 244 | 245 | #if defined(MBEDTLS_ENTROPY_NV_SEED) 246 | int mbedtls_nv_seed_poll( void *data, 247 | unsigned char *output, size_t len, size_t *olen ) 248 | { 249 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; 250 | size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; 251 | ((void) data); 252 | 253 | memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); 254 | 255 | if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 ) 256 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 257 | 258 | if( len < use_len ) 259 | use_len = len; 260 | 261 | memcpy( output, buf, use_len ); 262 | *olen = use_len; 263 | 264 | return( 0 ); 265 | } 266 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ 267 | 268 | #endif /* MBEDTLS_ENTROPY_C */ 269 | -------------------------------------------------------------------------------- /src/mbedtls/entropy_poll.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file entropy_poll.h 3 | * 4 | * \brief Platform-specific and custom entropy polling functions 5 | * 6 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_ENTROPY_POLL_H 24 | #define MBEDTLS_ENTROPY_POLL_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | /* 39 | * Default thresholds for built-in sources, in bytes 40 | */ 41 | #define MBEDTLS_ENTROPY_MIN_PLATFORM 32 /**< Minimum for platform source */ 42 | #define MBEDTLS_ENTROPY_MIN_HAVEGE 32 /**< Minimum for HAVEGE */ 43 | #define MBEDTLS_ENTROPY_MIN_HARDCLOCK 4 /**< Minimum for mbedtls_timing_hardclock() */ 44 | #if !defined(MBEDTLS_ENTROPY_MIN_HARDWARE) 45 | #define MBEDTLS_ENTROPY_MIN_HARDWARE 32 /**< Minimum for the hardware source */ 46 | #endif 47 | 48 | /** 49 | * \brief Entropy poll callback that provides 0 entropy. 50 | */ 51 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) 52 | int mbedtls_null_entropy_poll( void *data, 53 | unsigned char *output, size_t len, size_t *olen ); 54 | #endif 55 | 56 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) 57 | /** 58 | * \brief Platform-specific entropy poll callback 59 | */ 60 | int mbedtls_platform_entropy_poll( void *data, 61 | unsigned char *output, size_t len, size_t *olen ); 62 | #endif 63 | 64 | #if defined(MBEDTLS_HAVEGE_C) 65 | /** 66 | * \brief HAVEGE based entropy poll callback 67 | * 68 | * Requires an HAVEGE state as its data pointer. 69 | */ 70 | int mbedtls_havege_poll( void *data, 71 | unsigned char *output, size_t len, size_t *olen ); 72 | #endif 73 | 74 | #if defined(MBEDTLS_TIMING_C) 75 | /** 76 | * \brief mbedtls_timing_hardclock-based entropy poll callback 77 | */ 78 | int mbedtls_hardclock_poll( void *data, 79 | unsigned char *output, size_t len, size_t *olen ); 80 | #endif 81 | 82 | #if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) 83 | /** 84 | * \brief Entropy poll callback for a hardware source 85 | * 86 | * \warning This is not provided by mbed TLS! 87 | * See \c MBEDTLS_ENTROPY_HARDWARE_ALT in config.h. 88 | * 89 | * \note This must accept NULL as its first argument. 90 | */ 91 | int mbedtls_hardware_poll( void *data, 92 | unsigned char *output, size_t len, size_t *olen ); 93 | #endif 94 | 95 | #if defined(MBEDTLS_ENTROPY_NV_SEED) 96 | /** 97 | * \brief Entropy poll callback for a non-volatile seed file 98 | * 99 | * \note This must accept NULL as its first argument. 100 | */ 101 | int mbedtls_nv_seed_poll( void *data, 102 | unsigned char *output, size_t len, size_t *olen ); 103 | #endif 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | 109 | #endif /* entropy_poll.h */ 110 | -------------------------------------------------------------------------------- /src/mbedtls/error.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file error.h 3 | * 4 | * \brief Error to string translation 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_ERROR_H 24 | #define MBEDTLS_ERROR_H 25 | 26 | #include 27 | 28 | /** 29 | * Error code layout. 30 | * 31 | * Currently we try to keep all error codes within the negative space of 16 32 | * bits signed integers to support all platforms (-0x0001 - -0x7FFF). In 33 | * addition we'd like to give two layers of information on the error if 34 | * possible. 35 | * 36 | * For that purpose the error codes are segmented in the following manner: 37 | * 38 | * 16 bit error code bit-segmentation 39 | * 40 | * 1 bit - Unused (sign bit) 41 | * 3 bits - High level module ID 42 | * 5 bits - Module-dependent error code 43 | * 7 bits - Low level module errors 44 | * 45 | * For historical reasons, low-level error codes are divided in even and odd, 46 | * even codes were assigned first, and -1 is reserved for other errors. 47 | * 48 | * Low-level module errors (0x0002-0x007E, 0x0003-0x007F) 49 | * 50 | * Module Nr Codes assigned 51 | * MPI 7 0x0002-0x0010 52 | * GCM 2 0x0012-0x0014 53 | * BLOWFISH 2 0x0016-0x0018 54 | * THREADING 3 0x001A-0x001E 55 | * AES 2 0x0020-0x0022 56 | * CAMELLIA 2 0x0024-0x0026 57 | * XTEA 1 0x0028-0x0028 58 | * BASE64 2 0x002A-0x002C 59 | * OID 1 0x002E-0x002E 0x000B-0x000B 60 | * PADLOCK 1 0x0030-0x0030 61 | * DES 1 0x0032-0x0032 62 | * CTR_DBRG 4 0x0034-0x003A 63 | * ENTROPY 3 0x003C-0x0040 0x003D-0x003F 64 | * NET 11 0x0042-0x0052 0x0043-0x0045 65 | * ASN1 7 0x0060-0x006C 66 | * PBKDF2 1 0x007C-0x007C 67 | * HMAC_DRBG 4 0x0003-0x0009 68 | * CCM 2 0x000D-0x000F 69 | * 70 | * High-level module nr (3 bits - 0x0...-0x7...) 71 | * Name ID Nr of Errors 72 | * PEM 1 9 73 | * PKCS#12 1 4 (Started from top) 74 | * X509 2 19 75 | * PKCS5 2 4 (Started from top) 76 | * DHM 3 9 77 | * PK 3 14 (Started from top) 78 | * RSA 4 9 79 | * ECP 4 8 (Started from top) 80 | * MD 5 4 81 | * CIPHER 6 6 82 | * SSL 6 17 (Started from top) 83 | * SSL 7 31 84 | * 85 | * Module dependent error code (5 bits 0x.00.-0x.F8.) 86 | */ 87 | 88 | #ifdef __cplusplus 89 | extern "C" { 90 | #endif 91 | 92 | /** 93 | * \brief Translate a mbed TLS error code into a string representation, 94 | * Result is truncated if necessary and always includes a terminating 95 | * null byte. 96 | * 97 | * \param errnum error code 98 | * \param buffer buffer to place representation in 99 | * \param buflen length of the buffer 100 | */ 101 | void mbedtls_strerror( int errnum, char *buffer, size_t buflen ); 102 | 103 | #ifdef __cplusplus 104 | } 105 | #endif 106 | 107 | #endif /* error.h */ 108 | -------------------------------------------------------------------------------- /src/mbedtls/havege.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file havege.h 3 | * 4 | * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_HAVEGE_H 24 | #define MBEDTLS_HAVEGE_H 25 | 26 | #include 27 | 28 | #define MBEDTLS_HAVEGE_COLLECT_SIZE 1024 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /** 35 | * \brief HAVEGE state structure 36 | */ 37 | typedef struct 38 | { 39 | int PT1, PT2, offset[2]; 40 | int pool[MBEDTLS_HAVEGE_COLLECT_SIZE]; 41 | int WALK[8192]; 42 | } 43 | mbedtls_havege_state; 44 | 45 | /** 46 | * \brief HAVEGE initialization 47 | * 48 | * \param hs HAVEGE state to be initialized 49 | */ 50 | void mbedtls_havege_init( mbedtls_havege_state *hs ); 51 | 52 | /** 53 | * \brief Clear HAVEGE state 54 | * 55 | * \param hs HAVEGE state to be cleared 56 | */ 57 | void mbedtls_havege_free( mbedtls_havege_state *hs ); 58 | 59 | /** 60 | * \brief HAVEGE rand function 61 | * 62 | * \param p_rng A HAVEGE state 63 | * \param output Buffer to fill 64 | * \param len Length of buffer 65 | * 66 | * \return 0 67 | */ 68 | int mbedtls_havege_random( void *p_rng, unsigned char *output, size_t len ); 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif 73 | 74 | #endif /* havege.h */ 75 | -------------------------------------------------------------------------------- /src/mbedtls/md2.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md2.h 3 | * 4 | * \brief MD2 message digest algorithm (hash function) 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_MD2_H 24 | #define MBEDTLS_MD2_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | 34 | #if !defined(MBEDTLS_MD2_ALT) 35 | // Regular implementation 36 | // 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * \brief MD2 context structure 44 | */ 45 | typedef struct 46 | { 47 | unsigned char cksum[16]; /*!< checksum of the data block */ 48 | unsigned char state[48]; /*!< intermediate digest state */ 49 | unsigned char buffer[16]; /*!< data block being processed */ 50 | size_t left; /*!< amount of data in buffer */ 51 | } 52 | mbedtls_md2_context; 53 | 54 | /** 55 | * \brief Initialize MD2 context 56 | * 57 | * \param ctx MD2 context to be initialized 58 | */ 59 | void mbedtls_md2_init( mbedtls_md2_context *ctx ); 60 | 61 | /** 62 | * \brief Clear MD2 context 63 | * 64 | * \param ctx MD2 context to be cleared 65 | */ 66 | void mbedtls_md2_free( mbedtls_md2_context *ctx ); 67 | 68 | /** 69 | * \brief Clone (the state of) an MD2 context 70 | * 71 | * \param dst The destination context 72 | * \param src The context to be cloned 73 | */ 74 | void mbedtls_md2_clone( mbedtls_md2_context *dst, 75 | const mbedtls_md2_context *src ); 76 | 77 | /** 78 | * \brief MD2 context setup 79 | * 80 | * \param ctx context to be initialized 81 | */ 82 | void mbedtls_md2_starts( mbedtls_md2_context *ctx ); 83 | 84 | /** 85 | * \brief MD2 process buffer 86 | * 87 | * \param ctx MD2 context 88 | * \param input buffer holding the data 89 | * \param ilen length of the input data 90 | */ 91 | void mbedtls_md2_update( mbedtls_md2_context *ctx, const unsigned char *input, size_t ilen ); 92 | 93 | /** 94 | * \brief MD2 final digest 95 | * 96 | * \param ctx MD2 context 97 | * \param output MD2 checksum result 98 | */ 99 | void mbedtls_md2_finish( mbedtls_md2_context *ctx, unsigned char output[16] ); 100 | 101 | #ifdef __cplusplus 102 | } 103 | #endif 104 | 105 | #else /* MBEDTLS_MD2_ALT */ 106 | #include "md2_alt.h" 107 | #endif /* MBEDTLS_MD2_ALT */ 108 | 109 | #ifdef __cplusplus 110 | extern "C" { 111 | #endif 112 | 113 | /** 114 | * \brief Output = MD2( input buffer ) 115 | * 116 | * \param input buffer holding the data 117 | * \param ilen length of the input data 118 | * \param output MD2 checksum result 119 | */ 120 | void mbedtls_md2( const unsigned char *input, size_t ilen, unsigned char output[16] ); 121 | 122 | /** 123 | * \brief Checkup routine 124 | * 125 | * \return 0 if successful, or 1 if the test failed 126 | */ 127 | int mbedtls_md2_self_test( int verbose ); 128 | 129 | /* Internal use */ 130 | void mbedtls_md2_process( mbedtls_md2_context *ctx ); 131 | 132 | #ifdef __cplusplus 133 | } 134 | #endif 135 | 136 | #endif /* mbedtls_md2.h */ 137 | -------------------------------------------------------------------------------- /src/mbedtls/md4.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md4.h 3 | * 4 | * \brief MD4 message digest algorithm (hash function) 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_MD4_H 24 | #define MBEDTLS_MD4_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #if !defined(MBEDTLS_MD4_ALT) 36 | // Regular implementation 37 | // 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * \brief MD4 context structure 45 | */ 46 | typedef struct 47 | { 48 | uint32_t total[2]; /*!< number of bytes processed */ 49 | uint32_t state[4]; /*!< intermediate digest state */ 50 | unsigned char buffer[64]; /*!< data block being processed */ 51 | } 52 | mbedtls_md4_context; 53 | 54 | /** 55 | * \brief Initialize MD4 context 56 | * 57 | * \param ctx MD4 context to be initialized 58 | */ 59 | void mbedtls_md4_init( mbedtls_md4_context *ctx ); 60 | 61 | /** 62 | * \brief Clear MD4 context 63 | * 64 | * \param ctx MD4 context to be cleared 65 | */ 66 | void mbedtls_md4_free( mbedtls_md4_context *ctx ); 67 | 68 | /** 69 | * \brief Clone (the state of) an MD4 context 70 | * 71 | * \param dst The destination context 72 | * \param src The context to be cloned 73 | */ 74 | void mbedtls_md4_clone( mbedtls_md4_context *dst, 75 | const mbedtls_md4_context *src ); 76 | 77 | /** 78 | * \brief MD4 context setup 79 | * 80 | * \param ctx context to be initialized 81 | */ 82 | void mbedtls_md4_starts( mbedtls_md4_context *ctx ); 83 | 84 | /** 85 | * \brief MD4 process buffer 86 | * 87 | * \param ctx MD4 context 88 | * \param input buffer holding the data 89 | * \param ilen length of the input data 90 | */ 91 | void mbedtls_md4_update( mbedtls_md4_context *ctx, const unsigned char *input, size_t ilen ); 92 | 93 | /** 94 | * \brief MD4 final digest 95 | * 96 | * \param ctx MD4 context 97 | * \param output MD4 checksum result 98 | */ 99 | void mbedtls_md4_finish( mbedtls_md4_context *ctx, unsigned char output[16] ); 100 | 101 | #ifdef __cplusplus 102 | } 103 | #endif 104 | 105 | #else /* MBEDTLS_MD4_ALT */ 106 | #include "md4_alt.h" 107 | #endif /* MBEDTLS_MD4_ALT */ 108 | 109 | #ifdef __cplusplus 110 | extern "C" { 111 | #endif 112 | 113 | /** 114 | * \brief Output = MD4( input buffer ) 115 | * 116 | * \param input buffer holding the data 117 | * \param ilen length of the input data 118 | * \param output MD4 checksum result 119 | */ 120 | void mbedtls_md4( const unsigned char *input, size_t ilen, unsigned char output[16] ); 121 | 122 | /** 123 | * \brief Checkup routine 124 | * 125 | * \return 0 if successful, or 1 if the test failed 126 | */ 127 | int mbedtls_md4_self_test( int verbose ); 128 | 129 | /* Internal use */ 130 | void mbedtls_md4_process( mbedtls_md4_context *ctx, const unsigned char data[64] ); 131 | 132 | #ifdef __cplusplus 133 | } 134 | #endif 135 | 136 | #endif /* mbedtls_md4.h */ 137 | -------------------------------------------------------------------------------- /src/mbedtls/md5.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md5.h 3 | * 4 | * \brief MD5 message digest algorithm (hash function) 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_MD5_H 24 | #define MBEDTLS_MD5_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #if !defined(MBEDTLS_MD5_ALT) 36 | // Regular implementation 37 | // 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * \brief MD5 context structure 45 | */ 46 | typedef struct 47 | { 48 | uint32_t total[2]; /*!< number of bytes processed */ 49 | uint32_t state[4]; /*!< intermediate digest state */ 50 | unsigned char buffer[64]; /*!< data block being processed */ 51 | } 52 | mbedtls_md5_context; 53 | 54 | /** 55 | * \brief Initialize MD5 context 56 | * 57 | * \param ctx MD5 context to be initialized 58 | */ 59 | void mbedtls_md5_init( mbedtls_md5_context *ctx ); 60 | 61 | /** 62 | * \brief Clear MD5 context 63 | * 64 | * \param ctx MD5 context to be cleared 65 | */ 66 | void mbedtls_md5_free( mbedtls_md5_context *ctx ); 67 | 68 | /** 69 | * \brief Clone (the state of) an MD5 context 70 | * 71 | * \param dst The destination context 72 | * \param src The context to be cloned 73 | */ 74 | void mbedtls_md5_clone( mbedtls_md5_context *dst, 75 | const mbedtls_md5_context *src ); 76 | 77 | /** 78 | * \brief MD5 context setup 79 | * 80 | * \param ctx context to be initialized 81 | */ 82 | void mbedtls_md5_starts( mbedtls_md5_context *ctx ); 83 | 84 | /** 85 | * \brief MD5 process buffer 86 | * 87 | * \param ctx MD5 context 88 | * \param input buffer holding the data 89 | * \param ilen length of the input data 90 | */ 91 | void mbedtls_md5_update( mbedtls_md5_context *ctx, const unsigned char *input, size_t ilen ); 92 | 93 | /** 94 | * \brief MD5 final digest 95 | * 96 | * \param ctx MD5 context 97 | * \param output MD5 checksum result 98 | */ 99 | void mbedtls_md5_finish( mbedtls_md5_context *ctx, unsigned char output[16] ); 100 | 101 | /* Internal use */ 102 | void mbedtls_md5_process( mbedtls_md5_context *ctx, const unsigned char data[64] ); 103 | 104 | #ifdef __cplusplus 105 | } 106 | #endif 107 | 108 | #else /* MBEDTLS_MD5_ALT */ 109 | #include "md5_alt.h" 110 | #endif /* MBEDTLS_MD5_ALT */ 111 | 112 | #ifdef __cplusplus 113 | extern "C" { 114 | #endif 115 | 116 | /** 117 | * \brief Output = MD5( input buffer ) 118 | * 119 | * \param input buffer holding the data 120 | * \param ilen length of the input data 121 | * \param output MD5 checksum result 122 | */ 123 | void mbedtls_md5( const unsigned char *input, size_t ilen, unsigned char output[16] ); 124 | 125 | /** 126 | * \brief Checkup routine 127 | * 128 | * \return 0 if successful, or 1 if the test failed 129 | */ 130 | int mbedtls_md5_self_test( int verbose ); 131 | 132 | #ifdef __cplusplus 133 | } 134 | #endif 135 | 136 | #endif /* mbedtls_md5.h */ 137 | -------------------------------------------------------------------------------- /src/mbedtls/md_internal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file md_internal.h 3 | * 4 | * \brief Message digest wrappers. 5 | * 6 | * \warning This in an internal header. Do not include directly. 7 | * 8 | * \author Adriaan de Jong 9 | * 10 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 11 | * SPDX-License-Identifier: Apache-2.0 12 | * 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 14 | * not use this file except in compliance with the License. 15 | * You may obtain a copy of the License at 16 | * 17 | * http://www.apache.org/licenses/LICENSE-2.0 18 | * 19 | * Unless required by applicable law or agreed to in writing, software 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 | * See the License for the specific language governing permissions and 23 | * limitations under the License. 24 | * 25 | * This file is part of mbed TLS (https://tls.mbed.org) 26 | */ 27 | #ifndef MBEDTLS_MD_WRAP_H 28 | #define MBEDTLS_MD_WRAP_H 29 | 30 | #if !defined(MBEDTLS_CONFIG_FILE) 31 | #include "config.h" 32 | #else 33 | #include MBEDTLS_CONFIG_FILE 34 | #endif 35 | 36 | #include "md.h" 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * Message digest information. 44 | * Allows message digest functions to be called in a generic way. 45 | */ 46 | struct mbedtls_md_info_t 47 | { 48 | /** Digest identifier */ 49 | mbedtls_md_type_t type; 50 | 51 | /** Name of the message digest */ 52 | const char * name; 53 | 54 | /** Output length of the digest function in bytes */ 55 | int size; 56 | 57 | /** Block length of the digest function in bytes */ 58 | int block_size; 59 | 60 | /** Digest initialisation function */ 61 | void (*starts_func)( void *ctx ); 62 | 63 | /** Digest update function */ 64 | void (*update_func)( void *ctx, const unsigned char *input, size_t ilen ); 65 | 66 | /** Digest finalisation function */ 67 | void (*finish_func)( void *ctx, unsigned char *output ); 68 | 69 | /** Generic digest function */ 70 | void (*digest_func)( const unsigned char *input, size_t ilen, 71 | unsigned char *output ); 72 | 73 | /** Allocate a new context */ 74 | void * (*ctx_alloc_func)( void ); 75 | 76 | /** Free the given context */ 77 | void (*ctx_free_func)( void *ctx ); 78 | 79 | /** Clone state from a context */ 80 | void (*clone_func)( void *dst, const void *src ); 81 | 82 | /** Internal use only */ 83 | void (*process_func)( void *ctx, const unsigned char *input ); 84 | }; 85 | 86 | #if defined(MBEDTLS_MD2_C) 87 | extern const mbedtls_md_info_t mbedtls_md2_info; 88 | #endif 89 | #if defined(MBEDTLS_MD4_C) 90 | extern const mbedtls_md_info_t mbedtls_md4_info; 91 | #endif 92 | #if defined(MBEDTLS_MD5_C) 93 | extern const mbedtls_md_info_t mbedtls_md5_info; 94 | #endif 95 | #if defined(MBEDTLS_RIPEMD160_C) 96 | extern const mbedtls_md_info_t mbedtls_ripemd160_info; 97 | #endif 98 | #if defined(MBEDTLS_SHA1_C) 99 | extern const mbedtls_md_info_t mbedtls_sha1_info; 100 | #endif 101 | #if defined(MBEDTLS_SHA256_C) 102 | extern const mbedtls_md_info_t mbedtls_sha224_info; 103 | extern const mbedtls_md_info_t mbedtls_sha256_info; 104 | #endif 105 | #if defined(MBEDTLS_SHA512_C) 106 | extern const mbedtls_md_info_t mbedtls_sha384_info; 107 | extern const mbedtls_md_info_t mbedtls_sha512_info; 108 | #endif 109 | 110 | #ifdef __cplusplus 111 | } 112 | #endif 113 | 114 | #endif /* MBEDTLS_MD_WRAP_H */ 115 | -------------------------------------------------------------------------------- /src/mbedtls/memory_buffer_alloc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file memory_buffer_alloc.h 3 | * 4 | * \brief Buffer-based memory allocator 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_MEMORY_BUFFER_ALLOC_H 24 | #define MBEDTLS_MEMORY_BUFFER_ALLOC_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | 34 | /** 35 | * \name SECTION: Module settings 36 | * 37 | * The configuration options you can set for this module are in this section. 38 | * Either change them in config.h or define them on the compiler command line. 39 | * \{ 40 | */ 41 | 42 | #if !defined(MBEDTLS_MEMORY_ALIGN_MULTIPLE) 43 | #define MBEDTLS_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ 44 | #endif 45 | 46 | /* \} name SECTION: Module settings */ 47 | 48 | #define MBEDTLS_MEMORY_VERIFY_NONE 0 49 | #define MBEDTLS_MEMORY_VERIFY_ALLOC (1 << 0) 50 | #define MBEDTLS_MEMORY_VERIFY_FREE (1 << 1) 51 | #define MBEDTLS_MEMORY_VERIFY_ALWAYS (MBEDTLS_MEMORY_VERIFY_ALLOC | MBEDTLS_MEMORY_VERIFY_FREE) 52 | 53 | #ifdef __cplusplus 54 | extern "C" { 55 | #endif 56 | 57 | /** 58 | * \brief Initialize use of stack-based memory allocator. 59 | * The stack-based allocator does memory management inside the 60 | * presented buffer and does not call calloc() and free(). 61 | * It sets the global mbedtls_calloc() and mbedtls_free() pointers 62 | * to its own functions. 63 | * (Provided mbedtls_calloc() and mbedtls_free() are thread-safe if 64 | * MBEDTLS_THREADING_C is defined) 65 | * 66 | * \note This code is not optimized and provides a straight-forward 67 | * implementation of a stack-based memory allocator. 68 | * 69 | * \param buf buffer to use as heap 70 | * \param len size of the buffer 71 | */ 72 | void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len ); 73 | 74 | /** 75 | * \brief Free the mutex for thread-safety and clear remaining memory 76 | */ 77 | void mbedtls_memory_buffer_alloc_free( void ); 78 | 79 | /** 80 | * \brief Determine when the allocator should automatically verify the state 81 | * of the entire chain of headers / meta-data. 82 | * (Default: MBEDTLS_MEMORY_VERIFY_NONE) 83 | * 84 | * \param verify One of MBEDTLS_MEMORY_VERIFY_NONE, MBEDTLS_MEMORY_VERIFY_ALLOC, 85 | * MBEDTLS_MEMORY_VERIFY_FREE or MBEDTLS_MEMORY_VERIFY_ALWAYS 86 | */ 87 | void mbedtls_memory_buffer_set_verify( int verify ); 88 | 89 | #if defined(MBEDTLS_MEMORY_DEBUG) 90 | /** 91 | * \brief Print out the status of the allocated memory (primarily for use 92 | * after a program should have de-allocated all memory) 93 | * Prints out a list of 'still allocated' blocks and their stack 94 | * trace if MBEDTLS_MEMORY_BACKTRACE is defined. 95 | */ 96 | void mbedtls_memory_buffer_alloc_status( void ); 97 | 98 | /** 99 | * \brief Get the peak heap usage so far 100 | * 101 | * \param max_used Peak number of bytes in use or committed. This 102 | * includes bytes in allocated blocks too small to split 103 | * into smaller blocks but larger than the requested size. 104 | * \param max_blocks Peak number of blocks in use, including free and used 105 | */ 106 | void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks ); 107 | 108 | /** 109 | * \brief Reset peak statistics 110 | */ 111 | void mbedtls_memory_buffer_alloc_max_reset( void ); 112 | 113 | /** 114 | * \brief Get the current heap usage 115 | * 116 | * \param cur_used Current number of bytes in use or committed. This 117 | * includes bytes in allocated blocks too small to split 118 | * into smaller blocks but larger than the requested size. 119 | * \param cur_blocks Current number of blocks in use, including free and used 120 | */ 121 | void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks ); 122 | #endif /* MBEDTLS_MEMORY_DEBUG */ 123 | 124 | /** 125 | * \brief Verifies that all headers in the memory buffer are correct 126 | * and contain sane values. Helps debug buffer-overflow errors. 127 | * 128 | * Prints out first failure if MBEDTLS_MEMORY_DEBUG is defined. 129 | * Prints out full header information if MBEDTLS_MEMORY_DEBUG 130 | * is defined. (Includes stack trace information for each block if 131 | * MBEDTLS_MEMORY_BACKTRACE is defined as well). 132 | * 133 | * \return 0 if verified, 1 otherwise 134 | */ 135 | int mbedtls_memory_buffer_alloc_verify( void ); 136 | 137 | #if defined(MBEDTLS_SELF_TEST) 138 | /** 139 | * \brief Checkup routine 140 | * 141 | * \return 0 if successful, or 1 if a test failed 142 | */ 143 | int mbedtls_memory_buffer_alloc_self_test( int verbose ); 144 | #endif 145 | 146 | #ifdef __cplusplus 147 | } 148 | #endif 149 | 150 | #endif /* memory_buffer_alloc.h */ 151 | -------------------------------------------------------------------------------- /src/mbedtls/net.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file net.h 3 | * 4 | * \brief Deprecated header file that includes mbedtls/net_sockets.h 5 | * 6 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | * 23 | * \deprecated Superseded by mbedtls/net_sockets.h 24 | */ 25 | 26 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) 27 | #include "mbedtls/net_sockets.h" 28 | #if defined(MBEDTLS_DEPRECATED_WARNING) 29 | #warning "Deprecated header file: Superseded by mbedtls/net_sockets.h" 30 | #endif /* MBEDTLS_DEPRECATED_WARNING */ 31 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ 32 | -------------------------------------------------------------------------------- /src/mbedtls/padlock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * VIA PadLock support functions 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | /* 22 | * This implementation is based on the VIA PadLock Programming Guide: 23 | * 24 | * http://www.via.com.tw/en/downloads/whitepapers/initiatives/padlock/ 25 | * programming_guide.pdf 26 | */ 27 | 28 | #if !defined(MBEDTLS_CONFIG_FILE) 29 | #include "mbedtls/config.h" 30 | #else 31 | #include MBEDTLS_CONFIG_FILE 32 | #endif 33 | 34 | #if defined(MBEDTLS_PADLOCK_C) 35 | 36 | #include "mbedtls/padlock.h" 37 | 38 | #include 39 | 40 | #ifndef asm 41 | #define asm __asm 42 | #endif 43 | 44 | #if defined(MBEDTLS_HAVE_X86) 45 | 46 | /* 47 | * PadLock detection routine 48 | */ 49 | int mbedtls_padlock_has_support( int feature ) 50 | { 51 | static int flags = -1; 52 | int ebx = 0, edx = 0; 53 | 54 | if( flags == -1 ) 55 | { 56 | asm( "movl %%ebx, %0 \n\t" 57 | "movl $0xC0000000, %%eax \n\t" 58 | "cpuid \n\t" 59 | "cmpl $0xC0000001, %%eax \n\t" 60 | "movl $0, %%edx \n\t" 61 | "jb unsupported \n\t" 62 | "movl $0xC0000001, %%eax \n\t" 63 | "cpuid \n\t" 64 | "unsupported: \n\t" 65 | "movl %%edx, %1 \n\t" 66 | "movl %2, %%ebx \n\t" 67 | : "=m" (ebx), "=m" (edx) 68 | : "m" (ebx) 69 | : "eax", "ecx", "edx" ); 70 | 71 | flags = edx; 72 | } 73 | 74 | return( flags & feature ); 75 | } 76 | 77 | /* 78 | * PadLock AES-ECB block en(de)cryption 79 | */ 80 | int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, 81 | int mode, 82 | const unsigned char input[16], 83 | unsigned char output[16] ) 84 | { 85 | int ebx = 0; 86 | uint32_t *rk; 87 | uint32_t *blk; 88 | uint32_t *ctrl; 89 | unsigned char buf[256]; 90 | 91 | rk = ctx->rk; 92 | blk = MBEDTLS_PADLOCK_ALIGN16( buf ); 93 | memcpy( blk, input, 16 ); 94 | 95 | ctrl = blk + 4; 96 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode^1 ) - 10 ) << 9 ); 97 | 98 | asm( "pushfl \n\t" 99 | "popfl \n\t" 100 | "movl %%ebx, %0 \n\t" 101 | "movl $1, %%ecx \n\t" 102 | "movl %2, %%edx \n\t" 103 | "movl %3, %%ebx \n\t" 104 | "movl %4, %%esi \n\t" 105 | "movl %4, %%edi \n\t" 106 | ".byte 0xf3,0x0f,0xa7,0xc8 \n\t" 107 | "movl %1, %%ebx \n\t" 108 | : "=m" (ebx) 109 | : "m" (ebx), "m" (ctrl), "m" (rk), "m" (blk) 110 | : "memory", "ecx", "edx", "esi", "edi" ); 111 | 112 | memcpy( output, blk, 16 ); 113 | 114 | return( 0 ); 115 | } 116 | 117 | /* 118 | * PadLock AES-CBC buffer en(de)cryption 119 | */ 120 | int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx, 121 | int mode, 122 | size_t length, 123 | unsigned char iv[16], 124 | const unsigned char *input, 125 | unsigned char *output ) 126 | { 127 | int ebx = 0; 128 | size_t count; 129 | uint32_t *rk; 130 | uint32_t *iw; 131 | uint32_t *ctrl; 132 | unsigned char buf[256]; 133 | 134 | if( ( (long) input & 15 ) != 0 || 135 | ( (long) output & 15 ) != 0 ) 136 | return( MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED ); 137 | 138 | rk = ctx->rk; 139 | iw = MBEDTLS_PADLOCK_ALIGN16( buf ); 140 | memcpy( iw, iv, 16 ); 141 | 142 | ctrl = iw + 4; 143 | *ctrl = 0x80 | ctx->nr | ( ( ctx->nr + ( mode ^ 1 ) - 10 ) << 9 ); 144 | 145 | count = ( length + 15 ) >> 4; 146 | 147 | asm( "pushfl \n\t" 148 | "popfl \n\t" 149 | "movl %%ebx, %0 \n\t" 150 | "movl %2, %%ecx \n\t" 151 | "movl %3, %%edx \n\t" 152 | "movl %4, %%ebx \n\t" 153 | "movl %5, %%esi \n\t" 154 | "movl %6, %%edi \n\t" 155 | "movl %7, %%eax \n\t" 156 | ".byte 0xf3,0x0f,0xa7,0xd0 \n\t" 157 | "movl %1, %%ebx \n\t" 158 | : "=m" (ebx) 159 | : "m" (ebx), "m" (count), "m" (ctrl), 160 | "m" (rk), "m" (input), "m" (output), "m" (iw) 161 | : "memory", "eax", "ecx", "edx", "esi", "edi" ); 162 | 163 | memcpy( iv, iw, 16 ); 164 | 165 | return( 0 ); 166 | } 167 | 168 | #endif /* MBEDTLS_HAVE_X86 */ 169 | 170 | #endif /* MBEDTLS_PADLOCK_C */ 171 | -------------------------------------------------------------------------------- /src/mbedtls/padlock.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file padlock.h 3 | * 4 | * \brief VIA PadLock ACE for HW encryption/decryption supported by some 5 | * processors 6 | * 7 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 8 | * SPDX-License-Identifier: Apache-2.0 9 | * 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 | * not use this file except in compliance with the License. 12 | * You may obtain a copy of the License at 13 | * 14 | * http://www.apache.org/licenses/LICENSE-2.0 15 | * 16 | * Unless required by applicable law or agreed to in writing, software 17 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 | * See the License for the specific language governing permissions and 20 | * limitations under the License. 21 | * 22 | * This file is part of mbed TLS (https://tls.mbed.org) 23 | */ 24 | #ifndef MBEDTLS_PADLOCK_H 25 | #define MBEDTLS_PADLOCK_H 26 | 27 | #include "aes.h" 28 | 29 | #define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */ 30 | 31 | #if defined(__has_feature) 32 | #if __has_feature(address_sanitizer) 33 | #define MBEDTLS_HAVE_ASAN 34 | #endif 35 | #endif 36 | 37 | /* Some versions of ASan result in errors about not enough registers */ 38 | #if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) && \ 39 | !defined(MBEDTLS_HAVE_ASAN) 40 | 41 | #ifndef MBEDTLS_HAVE_X86 42 | #define MBEDTLS_HAVE_X86 43 | #endif 44 | 45 | #include 46 | 47 | #define MBEDTLS_PADLOCK_RNG 0x000C 48 | #define MBEDTLS_PADLOCK_ACE 0x00C0 49 | #define MBEDTLS_PADLOCK_PHE 0x0C00 50 | #define MBEDTLS_PADLOCK_PMM 0x3000 51 | 52 | #define MBEDTLS_PADLOCK_ALIGN16(x) (uint32_t *) (16 + ((int32_t) x & ~15)) 53 | 54 | #ifdef __cplusplus 55 | extern "C" { 56 | #endif 57 | 58 | /** 59 | * \brief PadLock detection routine 60 | * 61 | * \param feature The feature to detect 62 | * 63 | * \return 1 if CPU has support for the feature, 0 otherwise 64 | */ 65 | int mbedtls_padlock_has_support( int feature ); 66 | 67 | /** 68 | * \brief PadLock AES-ECB block en(de)cryption 69 | * 70 | * \param ctx AES context 71 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 72 | * \param input 16-byte input block 73 | * \param output 16-byte output block 74 | * 75 | * \return 0 if success, 1 if operation failed 76 | */ 77 | int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, 78 | int mode, 79 | const unsigned char input[16], 80 | unsigned char output[16] ); 81 | 82 | /** 83 | * \brief PadLock AES-CBC buffer en(de)cryption 84 | * 85 | * \param ctx AES context 86 | * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT 87 | * \param length length of the input data 88 | * \param iv initialization vector (updated after use) 89 | * \param input buffer holding the input data 90 | * \param output buffer holding the output data 91 | * 92 | * \return 0 if success, 1 if operation failed 93 | */ 94 | int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx, 95 | int mode, 96 | size_t length, 97 | unsigned char iv[16], 98 | const unsigned char *input, 99 | unsigned char *output ); 100 | 101 | #ifdef __cplusplus 102 | } 103 | #endif 104 | 105 | #endif /* HAVE_X86 */ 106 | 107 | #endif /* padlock.h */ 108 | -------------------------------------------------------------------------------- /src/mbedtls/pem.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pem.h 3 | * 4 | * \brief Privacy Enhanced Mail (PEM) decoding 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_PEM_H 24 | #define MBEDTLS_PEM_H 25 | 26 | #include 27 | 28 | /** 29 | * \name PEM Error codes 30 | * These error codes are returned in case of errors reading the 31 | * PEM data. 32 | * \{ 33 | */ 34 | #define MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT -0x1080 /**< No PEM header or footer found. */ 35 | #define MBEDTLS_ERR_PEM_INVALID_DATA -0x1100 /**< PEM string is not as expected. */ 36 | #define MBEDTLS_ERR_PEM_ALLOC_FAILED -0x1180 /**< Failed to allocate memory. */ 37 | #define MBEDTLS_ERR_PEM_INVALID_ENC_IV -0x1200 /**< RSA IV is not in hex-format. */ 38 | #define MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG -0x1280 /**< Unsupported key encryption algorithm. */ 39 | #define MBEDTLS_ERR_PEM_PASSWORD_REQUIRED -0x1300 /**< Private key password can't be empty. */ 40 | #define MBEDTLS_ERR_PEM_PASSWORD_MISMATCH -0x1380 /**< Given private key password does not allow for correct decryption. */ 41 | #define MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE -0x1400 /**< Unavailable feature, e.g. hashing/encryption combination. */ 42 | #define MBEDTLS_ERR_PEM_BAD_INPUT_DATA -0x1480 /**< Bad input parameters to function. */ 43 | /* \} name */ 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | #if defined(MBEDTLS_PEM_PARSE_C) 50 | /** 51 | * \brief PEM context structure 52 | */ 53 | typedef struct 54 | { 55 | unsigned char *buf; /*!< buffer for decoded data */ 56 | size_t buflen; /*!< length of the buffer */ 57 | unsigned char *info; /*!< buffer for extra header information */ 58 | } 59 | mbedtls_pem_context; 60 | 61 | /** 62 | * \brief PEM context setup 63 | * 64 | * \param ctx context to be initialized 65 | */ 66 | void mbedtls_pem_init( mbedtls_pem_context *ctx ); 67 | 68 | /** 69 | * \brief Read a buffer for PEM information and store the resulting 70 | * data into the specified context buffers. 71 | * 72 | * \param ctx context to use 73 | * \param header header string to seek and expect 74 | * \param footer footer string to seek and expect 75 | * \param data source data to look in (must be nul-terminated) 76 | * \param pwd password for decryption (can be NULL) 77 | * \param pwdlen length of password 78 | * \param use_len destination for total length used (set after header is 79 | * correctly read, so unless you get 80 | * MBEDTLS_ERR_PEM_BAD_INPUT_DATA or 81 | * MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT, use_len is 82 | * the length to skip) 83 | * 84 | * \note Attempts to check password correctness by verifying if 85 | * the decrypted text starts with an ASN.1 sequence of 86 | * appropriate length 87 | * 88 | * \return 0 on success, or a specific PEM error code 89 | */ 90 | int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const char *footer, 91 | const unsigned char *data, 92 | const unsigned char *pwd, 93 | size_t pwdlen, size_t *use_len ); 94 | 95 | /** 96 | * \brief PEM context memory freeing 97 | * 98 | * \param ctx context to be freed 99 | */ 100 | void mbedtls_pem_free( mbedtls_pem_context *ctx ); 101 | #endif /* MBEDTLS_PEM_PARSE_C */ 102 | 103 | #if defined(MBEDTLS_PEM_WRITE_C) 104 | /** 105 | * \brief Write a buffer of PEM information from a DER encoded 106 | * buffer. 107 | * 108 | * \param header header string to write 109 | * \param footer footer string to write 110 | * \param der_data DER data to write 111 | * \param der_len length of the DER data 112 | * \param buf buffer to write to 113 | * \param buf_len length of output buffer 114 | * \param olen total length written / required (if buf_len is not enough) 115 | * 116 | * \return 0 on success, or a specific PEM or BASE64 error code. On 117 | * MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL olen is the required 118 | * size. 119 | */ 120 | int mbedtls_pem_write_buffer( const char *header, const char *footer, 121 | const unsigned char *der_data, size_t der_len, 122 | unsigned char *buf, size_t buf_len, size_t *olen ); 123 | #endif /* MBEDTLS_PEM_WRITE_C */ 124 | 125 | #ifdef __cplusplus 126 | } 127 | #endif 128 | 129 | #endif /* pem.h */ 130 | -------------------------------------------------------------------------------- /src/mbedtls/pk_internal.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pk.h 3 | * 4 | * \brief Public Key abstraction layer: wrapper functions 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | 24 | #ifndef MBEDTLS_PK_WRAP_H 25 | #define MBEDTLS_PK_WRAP_H 26 | 27 | #if !defined(MBEDTLS_CONFIG_FILE) 28 | #include "config.h" 29 | #else 30 | #include MBEDTLS_CONFIG_FILE 31 | #endif 32 | 33 | #include "pk.h" 34 | 35 | struct mbedtls_pk_info_t 36 | { 37 | /** Public key type */ 38 | mbedtls_pk_type_t type; 39 | 40 | /** Type name */ 41 | const char *name; 42 | 43 | /** Get key size in bits */ 44 | size_t (*get_bitlen)( const void * ); 45 | 46 | /** Tell if the context implements this type (e.g. ECKEY can do ECDSA) */ 47 | int (*can_do)( mbedtls_pk_type_t type ); 48 | 49 | /** Verify signature */ 50 | int (*verify_func)( void *ctx, mbedtls_md_type_t md_alg, 51 | const unsigned char *hash, size_t hash_len, 52 | const unsigned char *sig, size_t sig_len ); 53 | 54 | /** Make signature */ 55 | int (*sign_func)( void *ctx, mbedtls_md_type_t md_alg, 56 | const unsigned char *hash, size_t hash_len, 57 | unsigned char *sig, size_t *sig_len, 58 | int (*f_rng)(void *, unsigned char *, size_t), 59 | void *p_rng ); 60 | 61 | /** Decrypt message */ 62 | int (*decrypt_func)( void *ctx, const unsigned char *input, size_t ilen, 63 | unsigned char *output, size_t *olen, size_t osize, 64 | int (*f_rng)(void *, unsigned char *, size_t), 65 | void *p_rng ); 66 | 67 | /** Encrypt message */ 68 | int (*encrypt_func)( void *ctx, const unsigned char *input, size_t ilen, 69 | unsigned char *output, size_t *olen, size_t osize, 70 | int (*f_rng)(void *, unsigned char *, size_t), 71 | void *p_rng ); 72 | 73 | /** Check public-private key pair */ 74 | int (*check_pair_func)( const void *pub, const void *prv ); 75 | 76 | /** Allocate a new context */ 77 | void * (*ctx_alloc_func)( void ); 78 | 79 | /** Free the given context */ 80 | void (*ctx_free_func)( void *ctx ); 81 | 82 | /** Interface with the debug module */ 83 | void (*debug_func)( const void *ctx, mbedtls_pk_debug_item *items ); 84 | 85 | }; 86 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) 87 | /* Container for RSA-alt */ 88 | typedef struct 89 | { 90 | void *key; 91 | mbedtls_pk_rsa_alt_decrypt_func decrypt_func; 92 | mbedtls_pk_rsa_alt_sign_func sign_func; 93 | mbedtls_pk_rsa_alt_key_len_func key_len_func; 94 | } mbedtls_rsa_alt_context; 95 | #endif 96 | 97 | #if defined(MBEDTLS_RSA_C) 98 | extern const mbedtls_pk_info_t mbedtls_rsa_info; 99 | #endif 100 | 101 | #if defined(MBEDTLS_ECP_C) 102 | extern const mbedtls_pk_info_t mbedtls_eckey_info; 103 | extern const mbedtls_pk_info_t mbedtls_eckeydh_info; 104 | #endif 105 | 106 | #if defined(MBEDTLS_ECDSA_C) 107 | extern const mbedtls_pk_info_t mbedtls_ecdsa_info; 108 | #endif 109 | 110 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) 111 | extern const mbedtls_pk_info_t mbedtls_rsa_alt_info; 112 | #endif 113 | 114 | #endif /* MBEDTLS_PK_WRAP_H */ 115 | -------------------------------------------------------------------------------- /src/mbedtls/pkcs11.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pkcs11.c 3 | * 4 | * \brief Wrapper for PKCS#11 library libpkcs11-helper 5 | * 6 | * \author Adriaan de Jong 7 | * 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 9 | * SPDX-License-Identifier: Apache-2.0 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 12 | * not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | * 23 | * This file is part of mbed TLS (https://tls.mbed.org) 24 | */ 25 | 26 | #include "mbedtls/pkcs11.h" 27 | 28 | #if defined(MBEDTLS_PKCS11_C) 29 | 30 | #include "mbedtls/md.h" 31 | #include "mbedtls/oid.h" 32 | #include "mbedtls/x509_crt.h" 33 | 34 | #if defined(MBEDTLS_PLATFORM_C) 35 | #include "mbedtls/platform.h" 36 | #else 37 | #include 38 | #define mbedtls_calloc calloc 39 | #define mbedtls_free free 40 | #endif 41 | 42 | #include 43 | 44 | void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx ) 45 | { 46 | memset( ctx, 0, sizeof( mbedtls_pkcs11_context ) ); 47 | } 48 | 49 | int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11_cert ) 50 | { 51 | int ret = 1; 52 | unsigned char *cert_blob = NULL; 53 | size_t cert_blob_size = 0; 54 | 55 | if( cert == NULL ) 56 | { 57 | ret = 2; 58 | goto cleanup; 59 | } 60 | 61 | if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, NULL, 62 | &cert_blob_size ) != CKR_OK ) 63 | { 64 | ret = 3; 65 | goto cleanup; 66 | } 67 | 68 | cert_blob = mbedtls_calloc( 1, cert_blob_size ); 69 | if( NULL == cert_blob ) 70 | { 71 | ret = 4; 72 | goto cleanup; 73 | } 74 | 75 | if( pkcs11h_certificate_getCertificateBlob( pkcs11_cert, cert_blob, 76 | &cert_blob_size ) != CKR_OK ) 77 | { 78 | ret = 5; 79 | goto cleanup; 80 | } 81 | 82 | if( 0 != mbedtls_x509_crt_parse( cert, cert_blob, cert_blob_size ) ) 83 | { 84 | ret = 6; 85 | goto cleanup; 86 | } 87 | 88 | ret = 0; 89 | 90 | cleanup: 91 | if( NULL != cert_blob ) 92 | mbedtls_free( cert_blob ); 93 | 94 | return( ret ); 95 | } 96 | 97 | 98 | int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key, 99 | pkcs11h_certificate_t pkcs11_cert ) 100 | { 101 | int ret = 1; 102 | mbedtls_x509_crt cert; 103 | 104 | mbedtls_x509_crt_init( &cert ); 105 | 106 | if( priv_key == NULL ) 107 | goto cleanup; 108 | 109 | if( 0 != mbedtls_pkcs11_x509_cert_bind( &cert, pkcs11_cert ) ) 110 | goto cleanup; 111 | 112 | priv_key->len = mbedtls_pk_get_len( &cert.pk ); 113 | priv_key->pkcs11h_cert = pkcs11_cert; 114 | 115 | ret = 0; 116 | 117 | cleanup: 118 | mbedtls_x509_crt_free( &cert ); 119 | 120 | return( ret ); 121 | } 122 | 123 | void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key ) 124 | { 125 | if( NULL != priv_key ) 126 | pkcs11h_certificate_freeCertificate( priv_key->pkcs11h_cert ); 127 | } 128 | 129 | int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx, 130 | int mode, size_t *olen, 131 | const unsigned char *input, 132 | unsigned char *output, 133 | size_t output_max_len ) 134 | { 135 | size_t input_len, output_len; 136 | 137 | if( NULL == ctx ) 138 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 139 | 140 | if( MBEDTLS_RSA_PRIVATE != mode ) 141 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 142 | 143 | output_len = input_len = ctx->len; 144 | 145 | if( input_len < 16 || input_len > output_max_len ) 146 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 147 | 148 | /* Determine size of output buffer */ 149 | if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input, 150 | input_len, NULL, &output_len ) != CKR_OK ) 151 | { 152 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 153 | } 154 | 155 | if( output_len > output_max_len ) 156 | return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE ); 157 | 158 | if( pkcs11h_certificate_decryptAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, input, 159 | input_len, output, &output_len ) != CKR_OK ) 160 | { 161 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 162 | } 163 | *olen = output_len; 164 | return( 0 ); 165 | } 166 | 167 | int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx, 168 | int mode, 169 | mbedtls_md_type_t md_alg, 170 | unsigned int hashlen, 171 | const unsigned char *hash, 172 | unsigned char *sig ) 173 | { 174 | size_t sig_len = 0, asn_len = 0, oid_size = 0; 175 | unsigned char *p = sig; 176 | const char *oid; 177 | 178 | if( NULL == ctx ) 179 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 180 | 181 | if( MBEDTLS_RSA_PRIVATE != mode ) 182 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 183 | 184 | if( md_alg != MBEDTLS_MD_NONE ) 185 | { 186 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); 187 | if( md_info == NULL ) 188 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 189 | 190 | if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 ) 191 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 192 | 193 | hashlen = mbedtls_md_get_size( md_info ); 194 | asn_len = 10 + oid_size; 195 | } 196 | 197 | sig_len = ctx->len; 198 | if( hashlen > sig_len || asn_len > sig_len || 199 | hashlen + asn_len > sig_len ) 200 | { 201 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 202 | } 203 | 204 | if( md_alg != MBEDTLS_MD_NONE ) 205 | { 206 | /* 207 | * DigestInfo ::= SEQUENCE { 208 | * digestAlgorithm DigestAlgorithmIdentifier, 209 | * digest Digest } 210 | * 211 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier 212 | * 213 | * Digest ::= OCTET STRING 214 | */ 215 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; 216 | *p++ = (unsigned char) ( 0x08 + oid_size + hashlen ); 217 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; 218 | *p++ = (unsigned char) ( 0x04 + oid_size ); 219 | *p++ = MBEDTLS_ASN1_OID; 220 | *p++ = oid_size & 0xFF; 221 | memcpy( p, oid, oid_size ); 222 | p += oid_size; 223 | *p++ = MBEDTLS_ASN1_NULL; 224 | *p++ = 0x00; 225 | *p++ = MBEDTLS_ASN1_OCTET_STRING; 226 | *p++ = hashlen; 227 | } 228 | 229 | memcpy( p, hash, hashlen ); 230 | 231 | if( pkcs11h_certificate_signAny( ctx->pkcs11h_cert, CKM_RSA_PKCS, sig, 232 | asn_len + hashlen, sig, &sig_len ) != CKR_OK ) 233 | { 234 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); 235 | } 236 | 237 | return( 0 ); 238 | } 239 | 240 | #endif /* defined(MBEDTLS_PKCS11_C) */ 241 | -------------------------------------------------------------------------------- /src/mbedtls/pkcs11.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pkcs11.h 3 | * 4 | * \brief Wrapper for PKCS#11 library libpkcs11-helper 5 | * 6 | * \author Adriaan de Jong 7 | * 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 9 | * SPDX-License-Identifier: Apache-2.0 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 12 | * not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | * 23 | * This file is part of mbed TLS (https://tls.mbed.org) 24 | */ 25 | #ifndef MBEDTLS_PKCS11_H 26 | #define MBEDTLS_PKCS11_H 27 | 28 | #if !defined(MBEDTLS_CONFIG_FILE) 29 | #include "config.h" 30 | #else 31 | #include MBEDTLS_CONFIG_FILE 32 | #endif 33 | 34 | #if defined(MBEDTLS_PKCS11_C) 35 | 36 | #include "x509_crt.h" 37 | 38 | #include 39 | 40 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ 41 | !defined(inline) && !defined(__cplusplus) 42 | #define inline __inline 43 | #endif 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | /** 50 | * Context for PKCS #11 private keys. 51 | */ 52 | typedef struct { 53 | pkcs11h_certificate_t pkcs11h_cert; 54 | int len; 55 | } mbedtls_pkcs11_context; 56 | 57 | /** 58 | * Initialize a mbedtls_pkcs11_context. 59 | * (Just making memory references valid.) 60 | */ 61 | void mbedtls_pkcs11_init( mbedtls_pkcs11_context *ctx ); 62 | 63 | /** 64 | * Fill in a mbed TLS certificate, based on the given PKCS11 helper certificate. 65 | * 66 | * \param cert X.509 certificate to fill 67 | * \param pkcs11h_cert PKCS #11 helper certificate 68 | * 69 | * \return 0 on success. 70 | */ 71 | int mbedtls_pkcs11_x509_cert_bind( mbedtls_x509_crt *cert, pkcs11h_certificate_t pkcs11h_cert ); 72 | 73 | /** 74 | * Set up a mbedtls_pkcs11_context storing the given certificate. Note that the 75 | * mbedtls_pkcs11_context will take over control of the certificate, freeing it when 76 | * done. 77 | * 78 | * \param priv_key Private key structure to fill. 79 | * \param pkcs11_cert PKCS #11 helper certificate 80 | * 81 | * \return 0 on success 82 | */ 83 | int mbedtls_pkcs11_priv_key_bind( mbedtls_pkcs11_context *priv_key, 84 | pkcs11h_certificate_t pkcs11_cert ); 85 | 86 | /** 87 | * Free the contents of the given private key context. Note that the structure 88 | * itself is not freed. 89 | * 90 | * \param priv_key Private key structure to cleanup 91 | */ 92 | void mbedtls_pkcs11_priv_key_free( mbedtls_pkcs11_context *priv_key ); 93 | 94 | /** 95 | * \brief Do an RSA private key decrypt, then remove the message 96 | * padding 97 | * 98 | * \param ctx PKCS #11 context 99 | * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature 100 | * \param input buffer holding the encrypted data 101 | * \param output buffer that will hold the plaintext 102 | * \param olen will contain the plaintext length 103 | * \param output_max_len maximum length of the output buffer 104 | * 105 | * \return 0 if successful, or an MBEDTLS_ERR_RSA_XXX error code 106 | * 107 | * \note The output buffer must be as large as the size 108 | * of ctx->N (eg. 128 bytes if RSA-1024 is used) otherwise 109 | * an error is thrown. 110 | */ 111 | int mbedtls_pkcs11_decrypt( mbedtls_pkcs11_context *ctx, 112 | int mode, size_t *olen, 113 | const unsigned char *input, 114 | unsigned char *output, 115 | size_t output_max_len ); 116 | 117 | /** 118 | * \brief Do a private RSA to sign a message digest 119 | * 120 | * \param ctx PKCS #11 context 121 | * \param mode must be MBEDTLS_RSA_PRIVATE, for compatibility with rsa.c's signature 122 | * \param md_alg a MBEDTLS_MD_XXX (use MBEDTLS_MD_NONE for signing raw data) 123 | * \param hashlen message digest length (for MBEDTLS_MD_NONE only) 124 | * \param hash buffer holding the message digest 125 | * \param sig buffer that will hold the ciphertext 126 | * 127 | * \return 0 if the signing operation was successful, 128 | * or an MBEDTLS_ERR_RSA_XXX error code 129 | * 130 | * \note The "sig" buffer must be as large as the size 131 | * of ctx->N (eg. 128 bytes if RSA-1024 is used). 132 | */ 133 | int mbedtls_pkcs11_sign( mbedtls_pkcs11_context *ctx, 134 | int mode, 135 | mbedtls_md_type_t md_alg, 136 | unsigned int hashlen, 137 | const unsigned char *hash, 138 | unsigned char *sig ); 139 | 140 | /** 141 | * SSL/TLS wrappers for PKCS#11 functions 142 | */ 143 | static inline int mbedtls_ssl_pkcs11_decrypt( void *ctx, int mode, size_t *olen, 144 | const unsigned char *input, unsigned char *output, 145 | size_t output_max_len ) 146 | { 147 | return mbedtls_pkcs11_decrypt( (mbedtls_pkcs11_context *) ctx, mode, olen, input, output, 148 | output_max_len ); 149 | } 150 | 151 | static inline int mbedtls_ssl_pkcs11_sign( void *ctx, 152 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, 153 | int mode, mbedtls_md_type_t md_alg, unsigned int hashlen, 154 | const unsigned char *hash, unsigned char *sig ) 155 | { 156 | ((void) f_rng); 157 | ((void) p_rng); 158 | return mbedtls_pkcs11_sign( (mbedtls_pkcs11_context *) ctx, mode, md_alg, 159 | hashlen, hash, sig ); 160 | } 161 | 162 | static inline size_t mbedtls_ssl_pkcs11_key_len( void *ctx ) 163 | { 164 | return ( (mbedtls_pkcs11_context *) ctx )->len; 165 | } 166 | 167 | #ifdef __cplusplus 168 | } 169 | #endif 170 | 171 | #endif /* MBEDTLS_PKCS11_C */ 172 | 173 | #endif /* MBEDTLS_PKCS11_H */ 174 | -------------------------------------------------------------------------------- /src/mbedtls/pkcs12.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pkcs12.h 3 | * 4 | * \brief PKCS#12 Personal Information Exchange Syntax 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_PKCS12_H 24 | #define MBEDTLS_PKCS12_H 25 | 26 | #include "md.h" 27 | #include "cipher.h" 28 | #include "asn1.h" 29 | 30 | #include 31 | 32 | #define MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA -0x1F80 /**< Bad input parameters to function. */ 33 | #define MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE -0x1F00 /**< Feature not available, e.g. unsupported encryption scheme. */ 34 | #define MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT -0x1E80 /**< PBE ASN.1 data not as expected. */ 35 | #define MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH -0x1E00 /**< Given private key password does not allow for correct decryption. */ 36 | 37 | #define MBEDTLS_PKCS12_DERIVE_KEY 1 /**< encryption/decryption key */ 38 | #define MBEDTLS_PKCS12_DERIVE_IV 2 /**< initialization vector */ 39 | #define MBEDTLS_PKCS12_DERIVE_MAC_KEY 3 /**< integrity / MAC key */ 40 | 41 | #define MBEDTLS_PKCS12_PBE_DECRYPT 0 42 | #define MBEDTLS_PKCS12_PBE_ENCRYPT 1 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | /** 49 | * \brief PKCS12 Password Based function (encryption / decryption) 50 | * for pbeWithSHAAnd128BitRC4 51 | * 52 | * \param pbe_params an ASN1 buffer containing the pkcs-12PbeParams structure 53 | * \param mode either MBEDTLS_PKCS12_PBE_ENCRYPT or MBEDTLS_PKCS12_PBE_DECRYPT 54 | * \param pwd the password used (may be NULL if no password is used) 55 | * \param pwdlen length of the password (may be 0) 56 | * \param input the input data 57 | * \param len data length 58 | * \param output the output buffer 59 | * 60 | * \return 0 if successful, or a MBEDTLS_ERR_XXX code 61 | */ 62 | int mbedtls_pkcs12_pbe_sha1_rc4_128( mbedtls_asn1_buf *pbe_params, int mode, 63 | const unsigned char *pwd, size_t pwdlen, 64 | const unsigned char *input, size_t len, 65 | unsigned char *output ); 66 | 67 | /** 68 | * \brief PKCS12 Password Based function (encryption / decryption) 69 | * for cipher-based and mbedtls_md-based PBE's 70 | * 71 | * \param pbe_params an ASN1 buffer containing the pkcs-12PbeParams structure 72 | * \param mode either MBEDTLS_PKCS12_PBE_ENCRYPT or MBEDTLS_PKCS12_PBE_DECRYPT 73 | * \param cipher_type the cipher used 74 | * \param md_type the mbedtls_md used 75 | * \param pwd the password used (may be NULL if no password is used) 76 | * \param pwdlen length of the password (may be 0) 77 | * \param input the input data 78 | * \param len data length 79 | * \param output the output buffer 80 | * 81 | * \return 0 if successful, or a MBEDTLS_ERR_XXX code 82 | */ 83 | int mbedtls_pkcs12_pbe( mbedtls_asn1_buf *pbe_params, int mode, 84 | mbedtls_cipher_type_t cipher_type, mbedtls_md_type_t md_type, 85 | const unsigned char *pwd, size_t pwdlen, 86 | const unsigned char *input, size_t len, 87 | unsigned char *output ); 88 | 89 | /** 90 | * \brief The PKCS#12 derivation function uses a password and a salt 91 | * to produce pseudo-random bits for a particular "purpose". 92 | * 93 | * Depending on the given id, this function can produce an 94 | * encryption/decryption key, an nitialization vector or an 95 | * integrity key. 96 | * 97 | * \param data buffer to store the derived data in 98 | * \param datalen length to fill 99 | * \param pwd password to use (may be NULL if no password is used) 100 | * \param pwdlen length of the password (may be 0) 101 | * \param salt salt buffer to use 102 | * \param saltlen length of the salt 103 | * \param mbedtls_md mbedtls_md type to use during the derivation 104 | * \param id id that describes the purpose (can be MBEDTLS_PKCS12_DERIVE_KEY, 105 | * MBEDTLS_PKCS12_DERIVE_IV or MBEDTLS_PKCS12_DERIVE_MAC_KEY) 106 | * \param iterations number of iterations 107 | * 108 | * \return 0 if successful, or a MD, BIGNUM type error. 109 | */ 110 | int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen, 111 | const unsigned char *pwd, size_t pwdlen, 112 | const unsigned char *salt, size_t saltlen, 113 | mbedtls_md_type_t mbedtls_md, int id, int iterations ); 114 | 115 | #ifdef __cplusplus 116 | } 117 | #endif 118 | 119 | #endif /* pkcs12.h */ 120 | -------------------------------------------------------------------------------- /src/mbedtls/pkcs5.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file pkcs5.h 3 | * 4 | * \brief PKCS#5 functions 5 | * 6 | * \author Mathias Olsson 7 | * 8 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 9 | * SPDX-License-Identifier: Apache-2.0 10 | * 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 12 | * not use this file except in compliance with the License. 13 | * You may obtain a copy of the License at 14 | * 15 | * http://www.apache.org/licenses/LICENSE-2.0 16 | * 17 | * Unless required by applicable law or agreed to in writing, software 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 | * See the License for the specific language governing permissions and 21 | * limitations under the License. 22 | * 23 | * This file is part of mbed TLS (https://tls.mbed.org) 24 | */ 25 | #ifndef MBEDTLS_PKCS5_H 26 | #define MBEDTLS_PKCS5_H 27 | 28 | #include "asn1.h" 29 | #include "md.h" 30 | 31 | #include 32 | #include 33 | 34 | #define MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA -0x2f80 /**< Bad input parameters to function. */ 35 | #define MBEDTLS_ERR_PKCS5_INVALID_FORMAT -0x2f00 /**< Unexpected ASN.1 data. */ 36 | #define MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE -0x2e80 /**< Requested encryption or digest alg not available. */ 37 | #define MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH -0x2e00 /**< Given private key password does not allow for correct decryption. */ 38 | 39 | #define MBEDTLS_PKCS5_DECRYPT 0 40 | #define MBEDTLS_PKCS5_ENCRYPT 1 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | /** 47 | * \brief PKCS#5 PBES2 function 48 | * 49 | * \param pbe_params the ASN.1 algorithm parameters 50 | * \param mode either MBEDTLS_PKCS5_DECRYPT or MBEDTLS_PKCS5_ENCRYPT 51 | * \param pwd password to use when generating key 52 | * \param pwdlen length of password 53 | * \param data data to process 54 | * \param datalen length of data 55 | * \param output output buffer 56 | * 57 | * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails. 58 | */ 59 | int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode, 60 | const unsigned char *pwd, size_t pwdlen, 61 | const unsigned char *data, size_t datalen, 62 | unsigned char *output ); 63 | 64 | /** 65 | * \brief PKCS#5 PBKDF2 using HMAC 66 | * 67 | * \param ctx Generic HMAC context 68 | * \param password Password to use when generating key 69 | * \param plen Length of password 70 | * \param salt Salt to use when generating key 71 | * \param slen Length of salt 72 | * \param iteration_count Iteration count 73 | * \param key_length Length of generated key in bytes 74 | * \param output Generated key. Must be at least as big as key_length 75 | * 76 | * \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails. 77 | */ 78 | int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password, 79 | size_t plen, const unsigned char *salt, size_t slen, 80 | unsigned int iteration_count, 81 | uint32_t key_length, unsigned char *output ); 82 | 83 | /** 84 | * \brief Checkup routine 85 | * 86 | * \return 0 if successful, or 1 if the test failed 87 | */ 88 | int mbedtls_pkcs5_self_test( int verbose ); 89 | 90 | #ifdef __cplusplus 91 | } 92 | #endif 93 | 94 | #endif /* pkcs5.h */ 95 | -------------------------------------------------------------------------------- /src/mbedtls/platform_time.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file platform_time.h 3 | * 4 | * \brief mbed TLS Platform time abstraction 5 | * 6 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_PLATFORM_TIME_H 24 | #define MBEDTLS_PLATFORM_TIME_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | /** 37 | * \name SECTION: Module settings 38 | * 39 | * The configuration options you can set for this module are in this section. 40 | * Either change them in config.h or define them on the compiler command line. 41 | * \{ 42 | */ 43 | 44 | /* 45 | * The time_t datatype 46 | */ 47 | #if defined(MBEDTLS_PLATFORM_TIME_TYPE_MACRO) 48 | typedef MBEDTLS_PLATFORM_TIME_TYPE_MACRO mbedtls_time_t; 49 | #else 50 | /* For time_t */ 51 | #include 52 | typedef time_t mbedtls_time_t; 53 | #endif /* MBEDTLS_PLATFORM_TIME_TYPE_MACRO */ 54 | 55 | /* 56 | * The function pointers for time 57 | */ 58 | #if defined(MBEDTLS_PLATFORM_TIME_ALT) 59 | extern mbedtls_time_t (*mbedtls_time)( mbedtls_time_t* time ); 60 | 61 | /** 62 | * \brief Set your own time function pointer 63 | * 64 | * \param time_func the time function implementation 65 | * 66 | * \return 0 67 | */ 68 | int mbedtls_platform_set_time( mbedtls_time_t (*time_func)( mbedtls_time_t* time ) ); 69 | #else 70 | #if defined(MBEDTLS_PLATFORM_TIME_MACRO) 71 | #define mbedtls_time MBEDTLS_PLATFORM_TIME_MACRO 72 | #else 73 | #define mbedtls_time time 74 | #endif /* MBEDTLS_PLATFORM_TIME_MACRO */ 75 | #endif /* MBEDTLS_PLATFORM_TIME_ALT */ 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | #endif /* platform_time.h */ 82 | -------------------------------------------------------------------------------- /src/mbedtls/ripemd160.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ripemd160.h 3 | * 4 | * \brief RIPE MD-160 message digest 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_RIPEMD160_H 24 | #define MBEDTLS_RIPEMD160_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #if !defined(MBEDTLS_RIPEMD160_ALT) 36 | // Regular implementation 37 | // 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * \brief RIPEMD-160 context structure 45 | */ 46 | typedef struct 47 | { 48 | uint32_t total[2]; /*!< number of bytes processed */ 49 | uint32_t state[5]; /*!< intermediate digest state */ 50 | unsigned char buffer[64]; /*!< data block being processed */ 51 | } 52 | mbedtls_ripemd160_context; 53 | 54 | /** 55 | * \brief Initialize RIPEMD-160 context 56 | * 57 | * \param ctx RIPEMD-160 context to be initialized 58 | */ 59 | void mbedtls_ripemd160_init( mbedtls_ripemd160_context *ctx ); 60 | 61 | /** 62 | * \brief Clear RIPEMD-160 context 63 | * 64 | * \param ctx RIPEMD-160 context to be cleared 65 | */ 66 | void mbedtls_ripemd160_free( mbedtls_ripemd160_context *ctx ); 67 | 68 | /** 69 | * \brief Clone (the state of) an RIPEMD-160 context 70 | * 71 | * \param dst The destination context 72 | * \param src The context to be cloned 73 | */ 74 | void mbedtls_ripemd160_clone( mbedtls_ripemd160_context *dst, 75 | const mbedtls_ripemd160_context *src ); 76 | 77 | /** 78 | * \brief RIPEMD-160 context setup 79 | * 80 | * \param ctx context to be initialized 81 | */ 82 | void mbedtls_ripemd160_starts( mbedtls_ripemd160_context *ctx ); 83 | 84 | /** 85 | * \brief RIPEMD-160 process buffer 86 | * 87 | * \param ctx RIPEMD-160 context 88 | * \param input buffer holding the data 89 | * \param ilen length of the input data 90 | */ 91 | void mbedtls_ripemd160_update( mbedtls_ripemd160_context *ctx, 92 | const unsigned char *input, size_t ilen ); 93 | 94 | /** 95 | * \brief RIPEMD-160 final digest 96 | * 97 | * \param ctx RIPEMD-160 context 98 | * \param output RIPEMD-160 checksum result 99 | */ 100 | void mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx, unsigned char output[20] ); 101 | 102 | /* Internal use */ 103 | void mbedtls_ripemd160_process( mbedtls_ripemd160_context *ctx, const unsigned char data[64] ); 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | 109 | #else /* MBEDTLS_RIPEMD160_ALT */ 110 | #include "ripemd160.h" 111 | #endif /* MBEDTLS_RIPEMD160_ALT */ 112 | 113 | #ifdef __cplusplus 114 | extern "C" { 115 | #endif 116 | 117 | /** 118 | * \brief Output = RIPEMD-160( input buffer ) 119 | * 120 | * \param input buffer holding the data 121 | * \param ilen length of the input data 122 | * \param output RIPEMD-160 checksum result 123 | */ 124 | void mbedtls_ripemd160( const unsigned char *input, size_t ilen, 125 | unsigned char output[20] ); 126 | 127 | /** 128 | * \brief Checkup routine 129 | * 130 | * \return 0 if successful, or 1 if the test failed 131 | */ 132 | int mbedtls_ripemd160_self_test( int verbose ); 133 | 134 | #ifdef __cplusplus 135 | } 136 | #endif 137 | 138 | #endif /* mbedtls_ripemd160.h */ 139 | -------------------------------------------------------------------------------- /src/mbedtls/sha1.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file sha1.h 3 | * 4 | * \brief SHA-1 cryptographic hash function 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_SHA1_H 24 | #define MBEDTLS_SHA1_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #if !defined(MBEDTLS_SHA1_ALT) 36 | // Regular implementation 37 | // 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * \brief SHA-1 context structure 45 | */ 46 | typedef struct 47 | { 48 | uint32_t total[2]; /*!< number of bytes processed */ 49 | uint32_t state[5]; /*!< intermediate digest state */ 50 | unsigned char buffer[64]; /*!< data block being processed */ 51 | } 52 | mbedtls_sha1_context; 53 | 54 | /** 55 | * \brief Initialize SHA-1 context 56 | * 57 | * \param ctx SHA-1 context to be initialized 58 | */ 59 | void mbedtls_sha1_init( mbedtls_sha1_context *ctx ); 60 | 61 | /** 62 | * \brief Clear SHA-1 context 63 | * 64 | * \param ctx SHA-1 context to be cleared 65 | */ 66 | void mbedtls_sha1_free( mbedtls_sha1_context *ctx ); 67 | 68 | /** 69 | * \brief Clone (the state of) a SHA-1 context 70 | * 71 | * \param dst The destination context 72 | * \param src The context to be cloned 73 | */ 74 | void mbedtls_sha1_clone( mbedtls_sha1_context *dst, 75 | const mbedtls_sha1_context *src ); 76 | 77 | /** 78 | * \brief SHA-1 context setup 79 | * 80 | * \param ctx context to be initialized 81 | */ 82 | void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); 83 | 84 | /** 85 | * \brief SHA-1 process buffer 86 | * 87 | * \param ctx SHA-1 context 88 | * \param input buffer holding the data 89 | * \param ilen length of the input data 90 | */ 91 | void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); 92 | 93 | /** 94 | * \brief SHA-1 final digest 95 | * 96 | * \param ctx SHA-1 context 97 | * \param output SHA-1 checksum result 98 | */ 99 | void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ); 100 | 101 | /* Internal use */ 102 | void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ); 103 | 104 | #ifdef __cplusplus 105 | } 106 | #endif 107 | 108 | #else /* MBEDTLS_SHA1_ALT */ 109 | #include "sha1_alt.h" 110 | #endif /* MBEDTLS_SHA1_ALT */ 111 | 112 | #ifdef __cplusplus 113 | extern "C" { 114 | #endif 115 | 116 | /** 117 | * \brief Output = SHA-1( input buffer ) 118 | * 119 | * \param input buffer holding the data 120 | * \param ilen length of the input data 121 | * \param output SHA-1 checksum result 122 | */ 123 | void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ); 124 | 125 | /** 126 | * \brief Checkup routine 127 | * 128 | * \return 0 if successful, or 1 if the test failed 129 | */ 130 | int mbedtls_sha1_self_test( int verbose ); 131 | 132 | #ifdef __cplusplus 133 | } 134 | #endif 135 | 136 | #endif /* mbedtls_sha1.h */ 137 | -------------------------------------------------------------------------------- /src/mbedtls/sha256.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file sha256.h 3 | * 4 | * \brief SHA-224 and SHA-256 cryptographic hash function 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_SHA256_H 24 | #define MBEDTLS_SHA256_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #if !defined(MBEDTLS_SHA256_ALT) 36 | // Regular implementation 37 | // 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * \brief SHA-256 context structure 45 | */ 46 | typedef struct 47 | { 48 | uint32_t total[2]; /*!< number of bytes processed */ 49 | uint32_t state[8]; /*!< intermediate digest state */ 50 | unsigned char buffer[64]; /*!< data block being processed */ 51 | int is224; /*!< 0 => SHA-256, else SHA-224 */ 52 | } 53 | mbedtls_sha256_context; 54 | 55 | /** 56 | * \brief Initialize SHA-256 context 57 | * 58 | * \param ctx SHA-256 context to be initialized 59 | */ 60 | void mbedtls_sha256_init( mbedtls_sha256_context *ctx ); 61 | 62 | /** 63 | * \brief Clear SHA-256 context 64 | * 65 | * \param ctx SHA-256 context to be cleared 66 | */ 67 | void mbedtls_sha256_free( mbedtls_sha256_context *ctx ); 68 | 69 | /** 70 | * \brief Clone (the state of) a SHA-256 context 71 | * 72 | * \param dst The destination context 73 | * \param src The context to be cloned 74 | */ 75 | void mbedtls_sha256_clone( mbedtls_sha256_context *dst, 76 | const mbedtls_sha256_context *src ); 77 | 78 | /** 79 | * \brief SHA-256 context setup 80 | * 81 | * \param ctx context to be initialized 82 | * \param is224 0 = use SHA256, 1 = use SHA224 83 | */ 84 | void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); 85 | 86 | /** 87 | * \brief SHA-256 process buffer 88 | * 89 | * \param ctx SHA-256 context 90 | * \param input buffer holding the data 91 | * \param ilen length of the input data 92 | */ 93 | void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, 94 | size_t ilen ); 95 | 96 | /** 97 | * \brief SHA-256 final digest 98 | * 99 | * \param ctx SHA-256 context 100 | * \param output SHA-224/256 checksum result 101 | */ 102 | void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ); 103 | 104 | /* Internal use */ 105 | void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] ); 106 | 107 | #ifdef __cplusplus 108 | } 109 | #endif 110 | 111 | #else /* MBEDTLS_SHA256_ALT */ 112 | #include "sha256_alt.h" 113 | #endif /* MBEDTLS_SHA256_ALT */ 114 | 115 | #ifdef __cplusplus 116 | extern "C" { 117 | #endif 118 | 119 | /** 120 | * \brief Output = SHA-256( input buffer ) 121 | * 122 | * \param input buffer holding the data 123 | * \param ilen length of the input data 124 | * \param output SHA-224/256 checksum result 125 | * \param is224 0 = use SHA256, 1 = use SHA224 126 | */ 127 | void mbedtls_sha256( const unsigned char *input, size_t ilen, 128 | unsigned char output[32], int is224 ); 129 | 130 | /** 131 | * \brief Checkup routine 132 | * 133 | * \return 0 if successful, or 1 if the test failed 134 | */ 135 | int mbedtls_sha256_self_test( int verbose ); 136 | 137 | #ifdef __cplusplus 138 | } 139 | #endif 140 | 141 | #endif /* mbedtls_sha256.h */ 142 | -------------------------------------------------------------------------------- /src/mbedtls/sha512.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file sha512.h 3 | * 4 | * \brief SHA-384 and SHA-512 cryptographic hash function 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_SHA512_H 24 | #define MBEDTLS_SHA512_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #if !defined(MBEDTLS_SHA512_ALT) 36 | // Regular implementation 37 | // 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * \brief SHA-512 context structure 45 | */ 46 | typedef struct 47 | { 48 | uint64_t total[2]; /*!< number of bytes processed */ 49 | uint64_t state[8]; /*!< intermediate digest state */ 50 | unsigned char buffer[128]; /*!< data block being processed */ 51 | int is384; /*!< 0 => SHA-512, else SHA-384 */ 52 | } 53 | mbedtls_sha512_context; 54 | 55 | /** 56 | * \brief Initialize SHA-512 context 57 | * 58 | * \param ctx SHA-512 context to be initialized 59 | */ 60 | void mbedtls_sha512_init( mbedtls_sha512_context *ctx ); 61 | 62 | /** 63 | * \brief Clear SHA-512 context 64 | * 65 | * \param ctx SHA-512 context to be cleared 66 | */ 67 | void mbedtls_sha512_free( mbedtls_sha512_context *ctx ); 68 | 69 | /** 70 | * \brief Clone (the state of) a SHA-512 context 71 | * 72 | * \param dst The destination context 73 | * \param src The context to be cloned 74 | */ 75 | void mbedtls_sha512_clone( mbedtls_sha512_context *dst, 76 | const mbedtls_sha512_context *src ); 77 | 78 | /** 79 | * \brief SHA-512 context setup 80 | * 81 | * \param ctx context to be initialized 82 | * \param is384 0 = use SHA512, 1 = use SHA384 83 | */ 84 | void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, int is384 ); 85 | 86 | /** 87 | * \brief SHA-512 process buffer 88 | * 89 | * \param ctx SHA-512 context 90 | * \param input buffer holding the data 91 | * \param ilen length of the input data 92 | */ 93 | void mbedtls_sha512_update( mbedtls_sha512_context *ctx, const unsigned char *input, 94 | size_t ilen ); 95 | 96 | /** 97 | * \brief SHA-512 final digest 98 | * 99 | * \param ctx SHA-512 context 100 | * \param output SHA-384/512 checksum result 101 | */ 102 | void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, unsigned char output[64] ); 103 | 104 | #ifdef __cplusplus 105 | } 106 | #endif 107 | 108 | #else /* MBEDTLS_SHA512_ALT */ 109 | #include "sha512_alt.h" 110 | #endif /* MBEDTLS_SHA512_ALT */ 111 | 112 | #ifdef __cplusplus 113 | extern "C" { 114 | #endif 115 | 116 | /** 117 | * \brief Output = SHA-512( input buffer ) 118 | * 119 | * \param input buffer holding the data 120 | * \param ilen length of the input data 121 | * \param output SHA-384/512 checksum result 122 | * \param is384 0 = use SHA512, 1 = use SHA384 123 | */ 124 | void mbedtls_sha512( const unsigned char *input, size_t ilen, 125 | unsigned char output[64], int is384 ); 126 | 127 | /** 128 | * \brief Checkup routine 129 | * 130 | * \return 0 if successful, or 1 if the test failed 131 | */ 132 | int mbedtls_sha512_self_test( int verbose ); 133 | 134 | /* Internal use */ 135 | void mbedtls_sha512_process( mbedtls_sha512_context *ctx, const unsigned char data[128] ); 136 | 137 | #ifdef __cplusplus 138 | } 139 | #endif 140 | 141 | #endif /* mbedtls_sha512.h */ 142 | -------------------------------------------------------------------------------- /src/mbedtls/ssl_cache.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ssl_cache.h 3 | * 4 | * \brief SSL session cache implementation 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_SSL_CACHE_H 24 | #define MBEDTLS_SSL_CACHE_H 25 | 26 | #include "ssl.h" 27 | 28 | #if defined(MBEDTLS_THREADING_C) 29 | #include "threading.h" 30 | #endif 31 | 32 | /** 33 | * \name SECTION: Module settings 34 | * 35 | * The configuration options you can set for this module are in this section. 36 | * Either change them in config.h or define them on the compiler command line. 37 | * \{ 38 | */ 39 | 40 | #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT) 41 | #define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */ 42 | #endif 43 | 44 | #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES) 45 | #define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */ 46 | #endif 47 | 48 | /* \} name SECTION: Module settings */ 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context; 55 | typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry; 56 | 57 | /** 58 | * \brief This structure is used for storing cache entries 59 | */ 60 | struct mbedtls_ssl_cache_entry 61 | { 62 | #if defined(MBEDTLS_HAVE_TIME) 63 | mbedtls_time_t timestamp; /*!< entry timestamp */ 64 | #endif 65 | mbedtls_ssl_session session; /*!< entry session */ 66 | #if defined(MBEDTLS_X509_CRT_PARSE_C) 67 | mbedtls_x509_buf peer_cert; /*!< entry peer_cert */ 68 | #endif 69 | mbedtls_ssl_cache_entry *next; /*!< chain pointer */ 70 | }; 71 | 72 | /** 73 | * \brief Cache context 74 | */ 75 | struct mbedtls_ssl_cache_context 76 | { 77 | mbedtls_ssl_cache_entry *chain; /*!< start of the chain */ 78 | int timeout; /*!< cache entry timeout */ 79 | int max_entries; /*!< maximum entries */ 80 | #if defined(MBEDTLS_THREADING_C) 81 | mbedtls_threading_mutex_t mutex; /*!< mutex */ 82 | #endif 83 | }; 84 | 85 | /** 86 | * \brief Initialize an SSL cache context 87 | * 88 | * \param cache SSL cache context 89 | */ 90 | void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache ); 91 | 92 | /** 93 | * \brief Cache get callback implementation 94 | * (Thread-safe if MBEDTLS_THREADING_C is enabled) 95 | * 96 | * \param data SSL cache context 97 | * \param session session to retrieve entry for 98 | */ 99 | int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session ); 100 | 101 | /** 102 | * \brief Cache set callback implementation 103 | * (Thread-safe if MBEDTLS_THREADING_C is enabled) 104 | * 105 | * \param data SSL cache context 106 | * \param session session to store entry for 107 | */ 108 | int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session ); 109 | 110 | #if defined(MBEDTLS_HAVE_TIME) 111 | /** 112 | * \brief Set the cache timeout 113 | * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day)) 114 | * 115 | * A timeout of 0 indicates no timeout. 116 | * 117 | * \param cache SSL cache context 118 | * \param timeout cache entry timeout in seconds 119 | */ 120 | void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout ); 121 | #endif /* MBEDTLS_HAVE_TIME */ 122 | 123 | /** 124 | * \brief Set the maximum number of cache entries 125 | * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50)) 126 | * 127 | * \param cache SSL cache context 128 | * \param max cache entry maximum 129 | */ 130 | void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max ); 131 | 132 | /** 133 | * \brief Free referenced items in a cache context and clear memory 134 | * 135 | * \param cache SSL cache context 136 | */ 137 | void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache ); 138 | 139 | #ifdef __cplusplus 140 | } 141 | #endif 142 | 143 | #endif /* ssl_cache.h */ 144 | -------------------------------------------------------------------------------- /src/mbedtls/ssl_cookie.c: -------------------------------------------------------------------------------- 1 | /* 2 | * DTLS cookie callbacks implementation 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | /* 22 | * These session callbacks use a simple chained list 23 | * to store and retrieve the session information. 24 | */ 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "mbedtls/config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #if defined(MBEDTLS_SSL_COOKIE_C) 33 | 34 | #if defined(MBEDTLS_PLATFORM_C) 35 | #include "mbedtls/platform.h" 36 | #else 37 | #define mbedtls_calloc calloc 38 | #define mbedtls_free free 39 | #endif 40 | 41 | #include "mbedtls/ssl_cookie.h" 42 | #include "mbedtls/ssl_internal.h" 43 | 44 | #include 45 | 46 | /* Implementation that should never be optimized out by the compiler */ 47 | static void mbedtls_zeroize( void *v, size_t n ) { 48 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; 49 | } 50 | 51 | /* 52 | * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is 53 | * available. Try SHA-256 first, 512 wastes resources since we need to stay 54 | * with max 32 bytes of cookie for DTLS 1.0 55 | */ 56 | #if defined(MBEDTLS_SHA256_C) 57 | #define COOKIE_MD MBEDTLS_MD_SHA224 58 | #define COOKIE_MD_OUTLEN 32 59 | #define COOKIE_HMAC_LEN 28 60 | #elif defined(MBEDTLS_SHA512_C) 61 | #define COOKIE_MD MBEDTLS_MD_SHA384 62 | #define COOKIE_MD_OUTLEN 48 63 | #define COOKIE_HMAC_LEN 28 64 | #elif defined(MBEDTLS_SHA1_C) 65 | #define COOKIE_MD MBEDTLS_MD_SHA1 66 | #define COOKIE_MD_OUTLEN 20 67 | #define COOKIE_HMAC_LEN 20 68 | #else 69 | #error "DTLS hello verify needs SHA-1 or SHA-2" 70 | #endif 71 | 72 | /* 73 | * Cookies are formed of a 4-bytes timestamp (or serial number) and 74 | * an HMAC of timestemp and client ID. 75 | */ 76 | #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN ) 77 | 78 | void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx ) 79 | { 80 | mbedtls_md_init( &ctx->hmac_ctx ); 81 | #if !defined(MBEDTLS_HAVE_TIME) 82 | ctx->serial = 0; 83 | #endif 84 | ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT; 85 | 86 | #if defined(MBEDTLS_THREADING_C) 87 | mbedtls_mutex_init( &ctx->mutex ); 88 | #endif 89 | } 90 | 91 | void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay ) 92 | { 93 | ctx->timeout = delay; 94 | } 95 | 96 | void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx ) 97 | { 98 | mbedtls_md_free( &ctx->hmac_ctx ); 99 | 100 | #if defined(MBEDTLS_THREADING_C) 101 | mbedtls_mutex_init( &ctx->mutex ); 102 | #endif 103 | 104 | mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) ); 105 | } 106 | 107 | int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx, 108 | int (*f_rng)(void *, unsigned char *, size_t), 109 | void *p_rng ) 110 | { 111 | int ret; 112 | unsigned char key[COOKIE_MD_OUTLEN]; 113 | 114 | if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 ) 115 | return( ret ); 116 | 117 | ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 ); 118 | if( ret != 0 ) 119 | return( ret ); 120 | 121 | ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) ); 122 | if( ret != 0 ) 123 | return( ret ); 124 | 125 | mbedtls_zeroize( key, sizeof( key ) ); 126 | 127 | return( 0 ); 128 | } 129 | 130 | /* 131 | * Generate the HMAC part of a cookie 132 | */ 133 | static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx, 134 | const unsigned char time[4], 135 | unsigned char **p, unsigned char *end, 136 | const unsigned char *cli_id, size_t cli_id_len ) 137 | { 138 | unsigned char hmac_out[COOKIE_MD_OUTLEN]; 139 | 140 | if( (size_t)( end - *p ) < COOKIE_HMAC_LEN ) 141 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 142 | 143 | if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 || 144 | mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 || 145 | mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 || 146 | mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 ) 147 | { 148 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); 149 | } 150 | 151 | memcpy( *p, hmac_out, COOKIE_HMAC_LEN ); 152 | *p += COOKIE_HMAC_LEN; 153 | 154 | return( 0 ); 155 | } 156 | 157 | /* 158 | * Generate cookie for DTLS ClientHello verification 159 | */ 160 | int mbedtls_ssl_cookie_write( void *p_ctx, 161 | unsigned char **p, unsigned char *end, 162 | const unsigned char *cli_id, size_t cli_id_len ) 163 | { 164 | int ret; 165 | mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx; 166 | unsigned long t; 167 | 168 | if( ctx == NULL || cli_id == NULL ) 169 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 170 | 171 | if( (size_t)( end - *p ) < COOKIE_LEN ) 172 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); 173 | 174 | #if defined(MBEDTLS_HAVE_TIME) 175 | t = (unsigned long) mbedtls_time( NULL ); 176 | #else 177 | t = ctx->serial++; 178 | #endif 179 | 180 | (*p)[0] = (unsigned char)( t >> 24 ); 181 | (*p)[1] = (unsigned char)( t >> 16 ); 182 | (*p)[2] = (unsigned char)( t >> 8 ); 183 | (*p)[3] = (unsigned char)( t ); 184 | *p += 4; 185 | 186 | #if defined(MBEDTLS_THREADING_C) 187 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) 188 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret ); 189 | #endif 190 | 191 | ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4, 192 | p, end, cli_id, cli_id_len ); 193 | 194 | #if defined(MBEDTLS_THREADING_C) 195 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) 196 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + 197 | MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 198 | #endif 199 | 200 | return( ret ); 201 | } 202 | 203 | /* 204 | * Check a cookie 205 | */ 206 | int mbedtls_ssl_cookie_check( void *p_ctx, 207 | const unsigned char *cookie, size_t cookie_len, 208 | const unsigned char *cli_id, size_t cli_id_len ) 209 | { 210 | unsigned char ref_hmac[COOKIE_HMAC_LEN]; 211 | int ret = 0; 212 | unsigned char *p = ref_hmac; 213 | mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx; 214 | unsigned long cur_time, cookie_time; 215 | 216 | if( ctx == NULL || cli_id == NULL ) 217 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); 218 | 219 | if( cookie_len != COOKIE_LEN ) 220 | return( -1 ); 221 | 222 | #if defined(MBEDTLS_THREADING_C) 223 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) 224 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret ); 225 | #endif 226 | 227 | if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie, 228 | &p, p + sizeof( ref_hmac ), 229 | cli_id, cli_id_len ) != 0 ) 230 | ret = -1; 231 | 232 | #if defined(MBEDTLS_THREADING_C) 233 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) 234 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + 235 | MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 236 | #endif 237 | 238 | if( ret != 0 ) 239 | return( ret ); 240 | 241 | if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 ) 242 | return( -1 ); 243 | 244 | #if defined(MBEDTLS_HAVE_TIME) 245 | cur_time = (unsigned long) mbedtls_time( NULL ); 246 | #else 247 | cur_time = ctx->serial; 248 | #endif 249 | 250 | cookie_time = ( (unsigned long) cookie[0] << 24 ) | 251 | ( (unsigned long) cookie[1] << 16 ) | 252 | ( (unsigned long) cookie[2] << 8 ) | 253 | ( (unsigned long) cookie[3] ); 254 | 255 | if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout ) 256 | return( -1 ); 257 | 258 | return( 0 ); 259 | } 260 | #endif /* MBEDTLS_SSL_COOKIE_C */ 261 | -------------------------------------------------------------------------------- /src/mbedtls/ssl_cookie.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ssl_cookie.h 3 | * 4 | * \brief DTLS cookie callbacks implementation 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_SSL_COOKIE_H 24 | #define MBEDTLS_SSL_COOKIE_H 25 | 26 | #include "ssl.h" 27 | 28 | #if defined(MBEDTLS_THREADING_C) 29 | #include "threading.h" 30 | #endif 31 | 32 | /** 33 | * \name SECTION: Module settings 34 | * 35 | * The configuration options you can set for this module are in this section. 36 | * Either change them in config.h or define them on the compiler command line. 37 | * \{ 38 | */ 39 | #ifndef MBEDTLS_SSL_COOKIE_TIMEOUT 40 | #define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */ 41 | #endif 42 | 43 | /* \} name SECTION: Module settings */ 44 | 45 | #ifdef __cplusplus 46 | extern "C" { 47 | #endif 48 | 49 | /** 50 | * \brief Context for the default cookie functions. 51 | */ 52 | typedef struct 53 | { 54 | mbedtls_md_context_t hmac_ctx; /*!< context for the HMAC portion */ 55 | #if !defined(MBEDTLS_HAVE_TIME) 56 | unsigned long serial; /*!< serial number for expiration */ 57 | #endif 58 | unsigned long timeout; /*!< timeout delay, in seconds if HAVE_TIME, 59 | or in number of tickets issued */ 60 | 61 | #if defined(MBEDTLS_THREADING_C) 62 | mbedtls_threading_mutex_t mutex; 63 | #endif 64 | } mbedtls_ssl_cookie_ctx; 65 | 66 | /** 67 | * \brief Initialize cookie context 68 | */ 69 | void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx ); 70 | 71 | /** 72 | * \brief Setup cookie context (generate keys) 73 | */ 74 | int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx, 75 | int (*f_rng)(void *, unsigned char *, size_t), 76 | void *p_rng ); 77 | 78 | /** 79 | * \brief Set expiration delay for cookies 80 | * (Default MBEDTLS_SSL_COOKIE_TIMEOUT) 81 | * 82 | * \param ctx Cookie contex 83 | * \param delay Delay, in seconds if HAVE_TIME, or in number of cookies 84 | * issued in the meantime. 85 | * 0 to disable expiration (NOT recommended) 86 | */ 87 | void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay ); 88 | 89 | /** 90 | * \brief Free cookie context 91 | */ 92 | void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx ); 93 | 94 | /** 95 | * \brief Generate cookie, see \c mbedtls_ssl_cookie_write_t 96 | */ 97 | mbedtls_ssl_cookie_write_t mbedtls_ssl_cookie_write; 98 | 99 | /** 100 | * \brief Verify cookie, see \c mbedtls_ssl_cookie_write_t 101 | */ 102 | mbedtls_ssl_cookie_check_t mbedtls_ssl_cookie_check; 103 | 104 | #ifdef __cplusplus 105 | } 106 | #endif 107 | 108 | #endif /* ssl_cookie.h */ 109 | -------------------------------------------------------------------------------- /src/mbedtls/ssl_ticket.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file ssl_ticket.h 3 | * 4 | * \brief TLS server ticket callbacks implementation 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_SSL_TICKET_H 24 | #define MBEDTLS_SSL_TICKET_H 25 | 26 | /* 27 | * This implementation of the session ticket callbacks includes key 28 | * management, rotating the keys periodically in order to preserve forward 29 | * secrecy, when MBEDTLS_HAVE_TIME is defined. 30 | */ 31 | 32 | #include "ssl.h" 33 | #include "cipher.h" 34 | 35 | #if defined(MBEDTLS_THREADING_C) 36 | #include "threading.h" 37 | #endif 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /** 44 | * \brief Information for session ticket protection 45 | */ 46 | typedef struct 47 | { 48 | unsigned char name[4]; /*!< random key identifier */ 49 | uint32_t generation_time; /*!< key generation timestamp (seconds) */ 50 | mbedtls_cipher_context_t ctx; /*!< context for auth enc/decryption */ 51 | } 52 | mbedtls_ssl_ticket_key; 53 | 54 | /** 55 | * \brief Context for session ticket handling functions 56 | */ 57 | typedef struct 58 | { 59 | mbedtls_ssl_ticket_key keys[2]; /*!< ticket protection keys */ 60 | unsigned char active; /*!< index of the currently active key */ 61 | 62 | uint32_t ticket_lifetime; /*!< lifetime of tickets in seconds */ 63 | 64 | /** Callback for getting (pseudo-)random numbers */ 65 | int (*f_rng)(void *, unsigned char *, size_t); 66 | void *p_rng; /*!< context for the RNG function */ 67 | 68 | #if defined(MBEDTLS_THREADING_C) 69 | mbedtls_threading_mutex_t mutex; 70 | #endif 71 | } 72 | mbedtls_ssl_ticket_context; 73 | 74 | /** 75 | * \brief Initialize a ticket context. 76 | * (Just make it ready for mbedtls_ssl_ticket_setup() 77 | * or mbedtls_ssl_ticket_free().) 78 | * 79 | * \param ctx Context to be initialized 80 | */ 81 | void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx ); 82 | 83 | /** 84 | * \brief Prepare context to be actually used 85 | * 86 | * \param ctx Context to be set up 87 | * \param f_rng RNG callback function 88 | * \param p_rng RNG callback context 89 | * \param cipher AEAD cipher to use for ticket protection. 90 | * Recommended value: MBEDTLS_CIPHER_AES_256_GCM. 91 | * \param lifetime Tickets lifetime in seconds 92 | * Recommended value: 86400 (one day). 93 | * 94 | * \note It is highly recommended to select a cipher that is at 95 | * least as strong as the the strongest ciphersuite 96 | * supported. Usually that means a 256-bit key. 97 | * 98 | * \note The lifetime of the keys is twice the lifetime of tickets. 99 | * It is recommended to pick a reasonnable lifetime so as not 100 | * to negate the benefits of forward secrecy. 101 | * 102 | * \return 0 if successful, 103 | * or a specific MBEDTLS_ERR_XXX error code 104 | */ 105 | int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, 106 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, 107 | mbedtls_cipher_type_t cipher, 108 | uint32_t lifetime ); 109 | 110 | /** 111 | * \brief Implementation of the ticket write callback 112 | * 113 | * \note See \c mbedlts_ssl_ticket_write_t for description 114 | */ 115 | mbedtls_ssl_ticket_write_t mbedtls_ssl_ticket_write; 116 | 117 | /** 118 | * \brief Implementation of the ticket parse callback 119 | * 120 | * \note See \c mbedlts_ssl_ticket_parse_t for description 121 | */ 122 | mbedtls_ssl_ticket_parse_t mbedtls_ssl_ticket_parse; 123 | 124 | /** 125 | * \brief Free a context's content and zeroize it. 126 | * 127 | * \param ctx Context to be cleaned up 128 | */ 129 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ); 130 | 131 | #ifdef __cplusplus 132 | } 133 | #endif 134 | 135 | #endif /* ssl_ticket.h */ 136 | -------------------------------------------------------------------------------- /src/mbedtls/threading.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Threading abstraction layer 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) 23 | #include "mbedtls/config.h" 24 | #else 25 | #include MBEDTLS_CONFIG_FILE 26 | #endif 27 | 28 | #if defined(MBEDTLS_THREADING_C) 29 | 30 | #include "mbedtls/threading.h" 31 | 32 | #if defined(MBEDTLS_THREADING_PTHREAD) 33 | static void threading_mutex_init_pthread( mbedtls_threading_mutex_t *mutex ) 34 | { 35 | if( mutex == NULL || mutex->is_valid ) 36 | return; 37 | 38 | mutex->is_valid = pthread_mutex_init( &mutex->mutex, NULL ) == 0; 39 | } 40 | 41 | static void threading_mutex_free_pthread( mbedtls_threading_mutex_t *mutex ) 42 | { 43 | if( mutex == NULL || !mutex->is_valid ) 44 | return; 45 | 46 | (void) pthread_mutex_destroy( &mutex->mutex ); 47 | mutex->is_valid = 0; 48 | } 49 | 50 | static int threading_mutex_lock_pthread( mbedtls_threading_mutex_t *mutex ) 51 | { 52 | if( mutex == NULL || ! mutex->is_valid ) 53 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 54 | 55 | if( pthread_mutex_lock( &mutex->mutex ) != 0 ) 56 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 57 | 58 | return( 0 ); 59 | } 60 | 61 | static int threading_mutex_unlock_pthread( mbedtls_threading_mutex_t *mutex ) 62 | { 63 | if( mutex == NULL || ! mutex->is_valid ) 64 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 65 | 66 | if( pthread_mutex_unlock( &mutex->mutex ) != 0 ) 67 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); 68 | 69 | return( 0 ); 70 | } 71 | 72 | void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_init_pthread; 73 | void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_free_pthread; 74 | int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_lock_pthread; 75 | int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_unlock_pthread; 76 | 77 | /* 78 | * With phtreads we can statically initialize mutexes 79 | */ 80 | #define MUTEX_INIT = { PTHREAD_MUTEX_INITIALIZER, 1 } 81 | 82 | #endif /* MBEDTLS_THREADING_PTHREAD */ 83 | 84 | #if defined(MBEDTLS_THREADING_ALT) 85 | static int threading_mutex_fail( mbedtls_threading_mutex_t *mutex ) 86 | { 87 | ((void) mutex ); 88 | return( MBEDTLS_ERR_THREADING_BAD_INPUT_DATA ); 89 | } 90 | static void threading_mutex_dummy( mbedtls_threading_mutex_t *mutex ) 91 | { 92 | ((void) mutex ); 93 | return; 94 | } 95 | 96 | void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; 97 | void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t * ) = threading_mutex_dummy; 98 | int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; 99 | int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t * ) = threading_mutex_fail; 100 | 101 | /* 102 | * Set functions pointers and initialize global mutexes 103 | */ 104 | void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), 105 | void (*mutex_free)( mbedtls_threading_mutex_t * ), 106 | int (*mutex_lock)( mbedtls_threading_mutex_t * ), 107 | int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ) 108 | { 109 | mbedtls_mutex_init = mutex_init; 110 | mbedtls_mutex_free = mutex_free; 111 | mbedtls_mutex_lock = mutex_lock; 112 | mbedtls_mutex_unlock = mutex_unlock; 113 | 114 | mbedtls_mutex_init( &mbedtls_threading_readdir_mutex ); 115 | mbedtls_mutex_init( &mbedtls_threading_gmtime_mutex ); 116 | } 117 | 118 | /* 119 | * Free global mutexes 120 | */ 121 | void mbedtls_threading_free_alt( void ) 122 | { 123 | mbedtls_mutex_free( &mbedtls_threading_readdir_mutex ); 124 | mbedtls_mutex_free( &mbedtls_threading_gmtime_mutex ); 125 | } 126 | #endif /* MBEDTLS_THREADING_ALT */ 127 | 128 | /* 129 | * Define global mutexes 130 | */ 131 | #ifndef MUTEX_INIT 132 | #define MUTEX_INIT 133 | #endif 134 | mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex MUTEX_INIT; 135 | mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex MUTEX_INIT; 136 | 137 | #endif /* MBEDTLS_THREADING_C */ 138 | -------------------------------------------------------------------------------- /src/mbedtls/threading.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file threading.h 3 | * 4 | * \brief Threading abstraction layer 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_THREADING_H 24 | #define MBEDTLS_THREADING_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #define MBEDTLS_ERR_THREADING_FEATURE_UNAVAILABLE -0x001A /**< The selected feature is not available. */ 39 | #define MBEDTLS_ERR_THREADING_BAD_INPUT_DATA -0x001C /**< Bad input parameters to function. */ 40 | #define MBEDTLS_ERR_THREADING_MUTEX_ERROR -0x001E /**< Locking / unlocking / free failed with error code. */ 41 | 42 | #if defined(MBEDTLS_THREADING_PTHREAD) 43 | #include 44 | typedef struct 45 | { 46 | pthread_mutex_t mutex; 47 | char is_valid; 48 | } mbedtls_threading_mutex_t; 49 | #endif 50 | 51 | #if defined(MBEDTLS_THREADING_ALT) 52 | /* You should define the mbedtls_threading_mutex_t type in your header */ 53 | #include "threading_alt.h" 54 | 55 | /** 56 | * \brief Set your alternate threading implementation function 57 | * pointers and initialize global mutexes. If used, this 58 | * function must be called once in the main thread before any 59 | * other mbed TLS function is called, and 60 | * mbedtls_threading_free_alt() must be called once in the main 61 | * thread after all other mbed TLS functions. 62 | * 63 | * \note mutex_init() and mutex_free() don't return a status code. 64 | * If mutex_init() fails, it should leave its argument (the 65 | * mutex) in a state such that mutex_lock() will fail when 66 | * called with this argument. 67 | * 68 | * \param mutex_init the init function implementation 69 | * \param mutex_free the free function implementation 70 | * \param mutex_lock the lock function implementation 71 | * \param mutex_unlock the unlock function implementation 72 | */ 73 | void mbedtls_threading_set_alt( void (*mutex_init)( mbedtls_threading_mutex_t * ), 74 | void (*mutex_free)( mbedtls_threading_mutex_t * ), 75 | int (*mutex_lock)( mbedtls_threading_mutex_t * ), 76 | int (*mutex_unlock)( mbedtls_threading_mutex_t * ) ); 77 | 78 | /** 79 | * \brief Free global mutexes. 80 | */ 81 | void mbedtls_threading_free_alt( void ); 82 | #endif /* MBEDTLS_THREADING_ALT */ 83 | 84 | #if defined(MBEDTLS_THREADING_C) 85 | /* 86 | * The function pointers for mutex_init, mutex_free, mutex_ and mutex_unlock 87 | * 88 | * All these functions are expected to work or the result will be undefined. 89 | */ 90 | extern void (*mbedtls_mutex_init)( mbedtls_threading_mutex_t *mutex ); 91 | extern void (*mbedtls_mutex_free)( mbedtls_threading_mutex_t *mutex ); 92 | extern int (*mbedtls_mutex_lock)( mbedtls_threading_mutex_t *mutex ); 93 | extern int (*mbedtls_mutex_unlock)( mbedtls_threading_mutex_t *mutex ); 94 | 95 | /* 96 | * Global mutexes 97 | */ 98 | extern mbedtls_threading_mutex_t mbedtls_threading_readdir_mutex; 99 | extern mbedtls_threading_mutex_t mbedtls_threading_gmtime_mutex; 100 | #endif /* MBEDTLS_THREADING_C */ 101 | 102 | #ifdef __cplusplus 103 | } 104 | #endif 105 | 106 | #endif /* threading.h */ 107 | -------------------------------------------------------------------------------- /src/mbedtls/timing.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file timing.h 3 | * 4 | * \brief Portable interface to the CPU cycle counter 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_TIMING_H 24 | #define MBEDTLS_TIMING_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #if !defined(MBEDTLS_TIMING_ALT) 33 | // Regular implementation 34 | // 35 | 36 | #include 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | /** 43 | * \brief timer structure 44 | */ 45 | struct mbedtls_timing_hr_time 46 | { 47 | unsigned char opaque[32]; 48 | }; 49 | 50 | /** 51 | * \brief Context for mbedtls_timing_set/get_delay() 52 | */ 53 | typedef struct 54 | { 55 | struct mbedtls_timing_hr_time timer; 56 | uint32_t int_ms; 57 | uint32_t fin_ms; 58 | } mbedtls_timing_delay_context; 59 | 60 | extern volatile int mbedtls_timing_alarmed; 61 | 62 | /** 63 | * \brief Return the CPU cycle counter value 64 | * 65 | * \warning This is only a best effort! Do not rely on this! 66 | * In particular, it is known to be unreliable on virtual 67 | * machines. 68 | */ 69 | unsigned long mbedtls_timing_hardclock( void ); 70 | 71 | /** 72 | * \brief Return the elapsed time in milliseconds 73 | * 74 | * \param val points to a timer structure 75 | * \param reset if set to 1, the timer is restarted 76 | */ 77 | unsigned long mbedtls_timing_get_timer( struct mbedtls_timing_hr_time *val, int reset ); 78 | 79 | /** 80 | * \brief Setup an alarm clock 81 | * 82 | * \param seconds delay before the "mbedtls_timing_alarmed" flag is set 83 | * 84 | * \warning Only one alarm at a time is supported. In a threaded 85 | * context, this means one for the whole process, not one per 86 | * thread. 87 | */ 88 | void mbedtls_set_alarm( int seconds ); 89 | 90 | /** 91 | * \brief Set a pair of delays to watch 92 | * (See \c mbedtls_timing_get_delay().) 93 | * 94 | * \param data Pointer to timing data 95 | * Must point to a valid \c mbedtls_timing_delay_context struct. 96 | * \param int_ms First (intermediate) delay in milliseconds. 97 | * \param fin_ms Second (final) delay in milliseconds. 98 | * Pass 0 to cancel the current delay. 99 | */ 100 | void mbedtls_timing_set_delay( void *data, uint32_t int_ms, uint32_t fin_ms ); 101 | 102 | /** 103 | * \brief Get the status of delays 104 | * (Memory helper: number of delays passed.) 105 | * 106 | * \param data Pointer to timing data 107 | * Must point to a valid \c mbedtls_timing_delay_context struct. 108 | * 109 | * \return -1 if cancelled (fin_ms = 0) 110 | * 0 if none of the delays are passed, 111 | * 1 if only the intermediate delay is passed, 112 | * 2 if the final delay is passed. 113 | */ 114 | int mbedtls_timing_get_delay( void *data ); 115 | 116 | #ifdef __cplusplus 117 | } 118 | #endif 119 | 120 | #else /* MBEDTLS_TIMING_ALT */ 121 | #include "timing_alt.h" 122 | #endif /* MBEDTLS_TIMING_ALT */ 123 | 124 | #ifdef __cplusplus 125 | extern "C" { 126 | #endif 127 | 128 | #if defined(MBEDTLS_SELF_TEST) 129 | /** 130 | * \brief Checkup routine 131 | * 132 | * \return 0 if successful, or 1 if a test failed 133 | */ 134 | int mbedtls_timing_self_test( int verbose ); 135 | #endif 136 | 137 | #ifdef __cplusplus 138 | } 139 | #endif 140 | 141 | #endif /* timing.h */ 142 | -------------------------------------------------------------------------------- /src/mbedtls/version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Version information 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | 22 | #if !defined(MBEDTLS_CONFIG_FILE) 23 | #include "mbedtls/config.h" 24 | #else 25 | #include MBEDTLS_CONFIG_FILE 26 | #endif 27 | 28 | #if defined(MBEDTLS_VERSION_C) 29 | 30 | #include "mbedtls/version.h" 31 | #include 32 | 33 | unsigned int mbedtls_version_get_number() 34 | { 35 | return( MBEDTLS_VERSION_NUMBER ); 36 | } 37 | 38 | void mbedtls_version_get_string( char *string ) 39 | { 40 | memcpy( string, MBEDTLS_VERSION_STRING, 41 | sizeof( MBEDTLS_VERSION_STRING ) ); 42 | } 43 | 44 | void mbedtls_version_get_string_full( char *string ) 45 | { 46 | memcpy( string, MBEDTLS_VERSION_STRING_FULL, 47 | sizeof( MBEDTLS_VERSION_STRING_FULL ) ); 48 | } 49 | 50 | #endif /* MBEDTLS_VERSION_C */ 51 | -------------------------------------------------------------------------------- /src/mbedtls/version.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file version.h 3 | * 4 | * \brief Run-time version information 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | /* 24 | * This set of compile-time defines and run-time variables can be used to 25 | * determine the version number of the mbed TLS library used. 26 | */ 27 | #ifndef MBEDTLS_VERSION_H 28 | #define MBEDTLS_VERSION_H 29 | 30 | #if !defined(MBEDTLS_CONFIG_FILE) 31 | #include "config.h" 32 | #else 33 | #include MBEDTLS_CONFIG_FILE 34 | #endif 35 | 36 | /** 37 | * The version number x.y.z is split into three parts. 38 | * Major, Minor, Patchlevel 39 | */ 40 | #define MBEDTLS_VERSION_MAJOR 2 41 | #define MBEDTLS_VERSION_MINOR 4 42 | #define MBEDTLS_VERSION_PATCH 0 43 | 44 | /** 45 | * The single version number has the following structure: 46 | * MMNNPP00 47 | * Major version | Minor version | Patch version 48 | */ 49 | #define MBEDTLS_VERSION_NUMBER 0x02040000 50 | #define MBEDTLS_VERSION_STRING "2.4.0" 51 | #define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.4.0" 52 | 53 | #if defined(MBEDTLS_VERSION_C) 54 | 55 | #ifdef __cplusplus 56 | extern "C" { 57 | #endif 58 | 59 | /** 60 | * Get the version number. 61 | * 62 | * \return The constructed version number in the format 63 | * MMNNPP00 (Major, Minor, Patch). 64 | */ 65 | unsigned int mbedtls_version_get_number( void ); 66 | 67 | /** 68 | * Get the version string ("x.y.z"). 69 | * 70 | * \param string The string that will receive the value. 71 | * (Should be at least 9 bytes in size) 72 | */ 73 | void mbedtls_version_get_string( char *string ); 74 | 75 | /** 76 | * Get the full version string ("mbed TLS x.y.z"). 77 | * 78 | * \param string The string that will receive the value. The mbed TLS version 79 | * string will use 18 bytes AT MOST including a terminating 80 | * null byte. 81 | * (So the buffer should be at least 18 bytes to receive this 82 | * version string). 83 | */ 84 | void mbedtls_version_get_string_full( char *string ); 85 | 86 | /** 87 | * \brief Check if support for a feature was compiled into this 88 | * mbed TLS binary. This allows you to see at runtime if the 89 | * library was for instance compiled with or without 90 | * Multi-threading support. 91 | * 92 | * \note only checks against defines in the sections "System 93 | * support", "mbed TLS modules" and "mbed TLS feature 94 | * support" in config.h 95 | * 96 | * \param feature The string for the define to check (e.g. "MBEDTLS_AES_C") 97 | * 98 | * \return 0 if the feature is present, 99 | * -1 if the feature is not present and 100 | * -2 if support for feature checking as a whole was not 101 | * compiled in. 102 | */ 103 | int mbedtls_version_check_feature( const char *feature ); 104 | 105 | #ifdef __cplusplus 106 | } 107 | #endif 108 | 109 | #endif /* MBEDTLS_VERSION_C */ 110 | 111 | #endif /* version.h */ 112 | -------------------------------------------------------------------------------- /src/mbedtls/x509_crl.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file x509_crl.h 3 | * 4 | * \brief X.509 certificate revocation list parsing 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_X509_CRL_H 24 | #define MBEDTLS_X509_CRL_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include "x509.h" 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | /** 39 | * \addtogroup x509_module 40 | * \{ */ 41 | 42 | /** 43 | * \name Structures and functions for parsing CRLs 44 | * \{ 45 | */ 46 | 47 | /** 48 | * Certificate revocation list entry. 49 | * Contains the CA-specific serial numbers and revocation dates. 50 | */ 51 | typedef struct mbedtls_x509_crl_entry 52 | { 53 | mbedtls_x509_buf raw; 54 | 55 | mbedtls_x509_buf serial; 56 | 57 | mbedtls_x509_time revocation_date; 58 | 59 | mbedtls_x509_buf entry_ext; 60 | 61 | struct mbedtls_x509_crl_entry *next; 62 | } 63 | mbedtls_x509_crl_entry; 64 | 65 | /** 66 | * Certificate revocation list structure. 67 | * Every CRL may have multiple entries. 68 | */ 69 | typedef struct mbedtls_x509_crl 70 | { 71 | mbedtls_x509_buf raw; /**< The raw certificate data (DER). */ 72 | mbedtls_x509_buf tbs; /**< The raw certificate body (DER). The part that is To Be Signed. */ 73 | 74 | int version; /**< CRL version (1=v1, 2=v2) */ 75 | mbedtls_x509_buf sig_oid; /**< CRL signature type identifier */ 76 | 77 | mbedtls_x509_buf issuer_raw; /**< The raw issuer data (DER). */ 78 | 79 | mbedtls_x509_name issuer; /**< The parsed issuer data (named information object). */ 80 | 81 | mbedtls_x509_time this_update; 82 | mbedtls_x509_time next_update; 83 | 84 | mbedtls_x509_crl_entry entry; /**< The CRL entries containing the certificate revocation times for this CA. */ 85 | 86 | mbedtls_x509_buf crl_ext; 87 | 88 | mbedtls_x509_buf sig_oid2; 89 | mbedtls_x509_buf sig; 90 | mbedtls_md_type_t sig_md; /**< Internal representation of the MD algorithm of the signature algorithm, e.g. MBEDTLS_MD_SHA256 */ 91 | mbedtls_pk_type_t sig_pk; /**< Internal representation of the Public Key algorithm of the signature algorithm, e.g. MBEDTLS_PK_RSA */ 92 | void *sig_opts; /**< Signature options to be passed to mbedtls_pk_verify_ext(), e.g. for RSASSA-PSS */ 93 | 94 | struct mbedtls_x509_crl *next; 95 | } 96 | mbedtls_x509_crl; 97 | 98 | /** 99 | * \brief Parse a DER-encoded CRL and append it to the chained list 100 | * 101 | * \param chain points to the start of the chain 102 | * \param buf buffer holding the CRL data in DER format 103 | * \param buflen size of the buffer 104 | * (including the terminating null byte for PEM data) 105 | * 106 | * \return 0 if successful, or a specific X509 or PEM error code 107 | */ 108 | int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain, 109 | const unsigned char *buf, size_t buflen ); 110 | /** 111 | * \brief Parse one or more CRLs and append them to the chained list 112 | * 113 | * \note Mutliple CRLs are accepted only if using PEM format 114 | * 115 | * \param chain points to the start of the chain 116 | * \param buf buffer holding the CRL data in PEM or DER format 117 | * \param buflen size of the buffer 118 | * (including the terminating null byte for PEM data) 119 | * 120 | * \return 0 if successful, or a specific X509 or PEM error code 121 | */ 122 | int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, size_t buflen ); 123 | 124 | #if defined(MBEDTLS_FS_IO) 125 | /** 126 | * \brief Load one or more CRLs and append them to the chained list 127 | * 128 | * \note Mutliple CRLs are accepted only if using PEM format 129 | * 130 | * \param chain points to the start of the chain 131 | * \param path filename to read the CRLs from (in PEM or DER encoding) 132 | * 133 | * \return 0 if successful, or a specific X509 or PEM error code 134 | */ 135 | int mbedtls_x509_crl_parse_file( mbedtls_x509_crl *chain, const char *path ); 136 | #endif /* MBEDTLS_FS_IO */ 137 | 138 | /** 139 | * \brief Returns an informational string about the CRL. 140 | * 141 | * \param buf Buffer to write to 142 | * \param size Maximum size of buffer 143 | * \param prefix A line prefix 144 | * \param crl The X509 CRL to represent 145 | * 146 | * \return The length of the string written (not including the 147 | * terminated nul byte), or a negative error code. 148 | */ 149 | int mbedtls_x509_crl_info( char *buf, size_t size, const char *prefix, 150 | const mbedtls_x509_crl *crl ); 151 | 152 | /** 153 | * \brief Initialize a CRL (chain) 154 | * 155 | * \param crl CRL chain to initialize 156 | */ 157 | void mbedtls_x509_crl_init( mbedtls_x509_crl *crl ); 158 | 159 | /** 160 | * \brief Unallocate all CRL data 161 | * 162 | * \param crl CRL chain to free 163 | */ 164 | void mbedtls_x509_crl_free( mbedtls_x509_crl *crl ); 165 | 166 | /* \} name */ 167 | /* \} addtogroup x509_module */ 168 | 169 | #ifdef __cplusplus 170 | } 171 | #endif 172 | 173 | #endif /* mbedtls_x509_crl.h */ 174 | -------------------------------------------------------------------------------- /src/mbedtls/xtea.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file xtea.h 3 | * 4 | * \brief XTEA block cipher (32-bit) 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_XTEA_H 24 | #define MBEDTLS_XTEA_H 25 | 26 | #if !defined(MBEDTLS_CONFIG_FILE) 27 | #include "config.h" 28 | #else 29 | #include MBEDTLS_CONFIG_FILE 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #define MBEDTLS_XTEA_ENCRYPT 1 36 | #define MBEDTLS_XTEA_DECRYPT 0 37 | 38 | #define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */ 39 | 40 | #if !defined(MBEDTLS_XTEA_ALT) 41 | // Regular implementation 42 | // 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | /** 49 | * \brief XTEA context structure 50 | */ 51 | typedef struct 52 | { 53 | uint32_t k[4]; /*!< key */ 54 | } 55 | mbedtls_xtea_context; 56 | 57 | /** 58 | * \brief Initialize XTEA context 59 | * 60 | * \param ctx XTEA context to be initialized 61 | */ 62 | void mbedtls_xtea_init( mbedtls_xtea_context *ctx ); 63 | 64 | /** 65 | * \brief Clear XTEA context 66 | * 67 | * \param ctx XTEA context to be cleared 68 | */ 69 | void mbedtls_xtea_free( mbedtls_xtea_context *ctx ); 70 | 71 | /** 72 | * \brief XTEA key schedule 73 | * 74 | * \param ctx XTEA context to be initialized 75 | * \param key the secret key 76 | */ 77 | void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] ); 78 | 79 | /** 80 | * \brief XTEA cipher function 81 | * 82 | * \param ctx XTEA context 83 | * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT 84 | * \param input 8-byte input block 85 | * \param output 8-byte output block 86 | * 87 | * \return 0 if successful 88 | */ 89 | int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, 90 | int mode, 91 | const unsigned char input[8], 92 | unsigned char output[8] ); 93 | 94 | #if defined(MBEDTLS_CIPHER_MODE_CBC) 95 | /** 96 | * \brief XTEA CBC cipher function 97 | * 98 | * \param ctx XTEA context 99 | * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT 100 | * \param length the length of input, multiple of 8 101 | * \param iv initialization vector for CBC mode 102 | * \param input input block 103 | * \param output output block 104 | * 105 | * \return 0 if successful, 106 | * MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0 107 | */ 108 | int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, 109 | int mode, 110 | size_t length, 111 | unsigned char iv[8], 112 | const unsigned char *input, 113 | unsigned char *output); 114 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ 115 | 116 | #ifdef __cplusplus 117 | } 118 | #endif 119 | 120 | #else /* MBEDTLS_XTEA_ALT */ 121 | #include "xtea_alt.h" 122 | #endif /* MBEDTLS_XTEA_ALT */ 123 | 124 | #ifdef __cplusplus 125 | extern "C" { 126 | #endif 127 | 128 | /** 129 | * \brief Checkup routine 130 | * 131 | * \return 0 if successful, or 1 if the test failed 132 | */ 133 | int mbedtls_xtea_self_test( int verbose ); 134 | 135 | #ifdef __cplusplus 136 | } 137 | #endif 138 | 139 | #endif /* xtea.h */ 140 | -------------------------------------------------------------------------------- /src/tls.cpp: -------------------------------------------------------------------------------- 1 | #include "tls.h" 2 | #include 3 | 4 | int TLS::init( const char *root_crt, const size_t root_crt_len ) 5 | { 6 | int error; 7 | mbedtls_net_init( &server_fd ); 8 | mbedtls_ssl_init( &ssl ); 9 | mbedtls_ssl_config_init( &conf ); 10 | mbedtls_x509_crt_init( &cacert ); 11 | mbedtls_ctr_drbg_init( &ctr_drbg ); 12 | mbedtls_entropy_init( &entropy ); 13 | const char *personalization = "particle-tls-library"; 14 | error = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, 15 | (const unsigned char *)personalization, 16 | strlen( personalization )); 17 | if( error < 0 ) 18 | { 19 | return error; 20 | } 21 | 22 | return mbedtls_x509_crt_parse( &cacert, 23 | (const unsigned char *) root_crt, 24 | root_crt_len ); 25 | } 26 | 27 | int TLS::connect( const char *host, const char *port ) 28 | { 29 | uint32_t flags; 30 | int error = mbedtls_net_connect( &server_fd, host, port, MBEDTLS_NET_PROTO_TCP ); 31 | 32 | if( error != 0 ) 33 | { 34 | return error; 35 | } 36 | 37 | error = mbedtls_ssl_config_defaults( &conf, 38 | MBEDTLS_SSL_IS_CLIENT, 39 | MBEDTLS_SSL_TRANSPORT_STREAM, 40 | MBEDTLS_SSL_PRESET_DEFAULT ); 41 | 42 | if( error != 0 ) 43 | { 44 | return error; 45 | } 46 | 47 | mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL ); 48 | mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL ); 49 | mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg ); 50 | 51 | error = mbedtls_ssl_setup( &ssl, &conf ); 52 | 53 | if( error != 0 ) 54 | { 55 | return error; 56 | } 57 | 58 | error = mbedtls_ssl_set_hostname( &ssl, host ); 59 | 60 | if( error != 0 ) 61 | { 62 | return error; 63 | } 64 | 65 | mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL ); 66 | 67 | do 68 | { 69 | error = mbedtls_ssl_handshake( &ssl ); 70 | } 71 | while( error == MBEDTLS_ERR_SSL_WANT_READ || error == MBEDTLS_ERR_SSL_WANT_WRITE ); 72 | 73 | if( error != 0 ) 74 | { 75 | return error; 76 | } 77 | 78 | flags = mbedtls_ssl_get_verify_result( &ssl ); 79 | if( flags != 0 ) 80 | { 81 | return flags; 82 | } 83 | 84 | return 0; 85 | } 86 | 87 | int TLS::write( const unsigned char *buf, size_t len ) 88 | { 89 | const size_t original_len = len; 90 | int ret = mbedtls_ssl_write( &ssl, buf, len ); 91 | while( (ret >= 0 && ret < len) || ret == MBEDTLS_ERR_SSL_WANT_WRITE ) 92 | { 93 | buf += ret; 94 | len -= ret; 95 | while( ret == 0 || ret == MBEDTLS_ERR_SSL_WANT_WRITE ) 96 | { 97 | ret = mbedtls_ssl_write( &ssl, buf, len ); 98 | } 99 | } 100 | 101 | if( ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_READ ) 102 | { 103 | int ret2 = mbedtls_ssl_session_reset( &ssl ); 104 | if( ret2 < 0 ) 105 | { 106 | // conundrum, we have 2 errors now, could return either 107 | return ret2; 108 | } 109 | return ret; 110 | } 111 | else 112 | { 113 | return original_len; 114 | } 115 | } 116 | 117 | int TLS::read( unsigned char *buf, size_t len ) 118 | { 119 | memset( buf, 0, len ); 120 | const size_t original_len = len; 121 | int ret = mbedtls_ssl_read( &ssl, buf, len ); 122 | while( (ret > 0 && ret < len) || ret == MBEDTLS_ERR_SSL_WANT_READ ) 123 | { 124 | buf += ret; 125 | len -= ret; 126 | while( ret == MBEDTLS_ERR_SSL_WANT_READ ) 127 | { 128 | ret = mbedtls_ssl_read( &ssl, buf, len ); 129 | } 130 | } 131 | 132 | if( ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_WRITE ) 133 | { 134 | int ret2 = mbedtls_ssl_session_reset( &ssl ); 135 | if( ret2 < 0 ) 136 | { 137 | // conundrum, we have 2 errors now, could return either 138 | return ret2; 139 | } 140 | return ret; 141 | } 142 | else 143 | { 144 | return original_len; 145 | } 146 | } 147 | 148 | int TLS::close() 149 | { 150 | return mbedtls_ssl_close_notify( &ssl ); 151 | } 152 | -------------------------------------------------------------------------------- /src/tls.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | class TLS 9 | { 10 | public: 11 | int init( const char *root_crt, const size_t root_crt_len ); 12 | int connect( const char *host, const char *port ); 13 | int write( const unsigned char *buf, size_t len ); 14 | int read( unsigned char *buf, size_t len ); 15 | int close(); 16 | 17 | private: 18 | mbedtls_net_context server_fd; 19 | mbedtls_entropy_context entropy; 20 | mbedtls_ctr_drbg_context ctr_drbg; 21 | mbedtls_ssl_context ssl; 22 | mbedtls_ssl_config conf; 23 | mbedtls_x509_crt cacert; 24 | }; 25 | -------------------------------------------------------------------------------- /test/runner.cpp: -------------------------------------------------------------------------------- 1 | #define CATCH_CONFIG_MAIN 2 | #include "catch.hpp" 3 | -------------------------------------------------------------------------------- /test/test_tls.cpp: -------------------------------------------------------------------------------- 1 | #include "catch.hpp" 2 | #include "tls.h" 3 | 4 | #define DIGICERT_HIGH_ASSURANCE_EV_ROOT_CA \ 5 | "-----BEGIN CERTIFICATE-----\r\n" \ 6 | "MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs\r\n" \ 7 | "MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\r\n" \ 8 | "d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j\r\n" \ 9 | "ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL\r\n" \ 10 | "MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3\r\n" \ 11 | "LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug\r\n" \ 12 | "RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm\r\n" \ 13 | "+9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW\r\n" \ 14 | "PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM\r\n" \ 15 | "xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB\r\n" \ 16 | "Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3\r\n" \ 17 | "hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg\r\n" \ 18 | "EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF\r\n" \ 19 | "MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA\r\n" \ 20 | "FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec\r\n" \ 21 | "nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z\r\n" \ 22 | "eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF\r\n" \ 23 | "hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2\r\n" \ 24 | "Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe\r\n" \ 25 | "vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep\r\n" \ 26 | "+OkuE6N36B9K\r\n" \ 27 | "-----END CERTIFICATE-----\r\n" 28 | const char root_pem[] = DIGICERT_HIGH_ASSURANCE_EV_ROOT_CA; 29 | 30 | SCENARIO( "Writing and reading over TLS" ) { 31 | GIVEN( "I have initialized TLS" ) { 32 | TLS tls; 33 | int error = tls.init(root_pem, sizeof(root_pem)); 34 | 35 | CHECK( error == 0 ); 36 | 37 | WHEN( "I call connect with a valid host and port" ) { 38 | error = tls.connect("www.random.org", "443"); 39 | 40 | THEN( "the return value is zero" ) { 41 | if( error == MBEDTLS_ERR_NET_UNKNOWN_HOST ) { 42 | printf(" !! These tests require internet access.\n"); 43 | printf(" !! It looks like you may not have it.\n"); 44 | printf(" !! The -82 error below is \"unknown host\".\n"); 45 | } 46 | REQUIRE( error == 0 ); 47 | 48 | AND_WHEN( "I call write with a valid HTTP request" ) { 49 | const char *buf = 50 | "GET /cgi-bin/randbyte HTTP/1.1\r\n" 51 | "Host: www.random.org\r\n\r\n"; 52 | size_t len = strlen( buf ); 53 | const unsigned char *ubuf = 54 | reinterpret_cast( buf ); 55 | error = tls.write( ubuf, len ); 56 | 57 | THEN( "56 bytes are written" ) { 58 | REQUIRE( error == 56 ); 59 | 60 | AND_WHEN( "I call read" ) { 61 | unsigned char recv_buf[20]; 62 | len = sizeof( recv_buf ); 63 | error = tls.read( recv_buf, len); 64 | 65 | THEN( "the response looks like HTTP" ) { 66 | // There was more to read, so we filled the buffer 67 | REQUIRE( error == len ); 68 | 69 | // First line of response was HTTP 70 | int cmp = strncmp( (const char *)recv_buf, 71 | "HTTP/1.1 200 OK\r\n", 17 ); 72 | REQUIRE( cmp == 0 ); 73 | 74 | AND_WHEN( "I call close" ) { 75 | error = tls.close(); 76 | 77 | THEN( "the connection closes successfully" ) { 78 | REQUIRE( error == 0 ); 79 | } 80 | } 81 | } 82 | } 83 | } 84 | } 85 | } 86 | } 87 | 88 | WHEN( "I call connect with a bad host" ) { 89 | error = tls.connect("gazorpa.zorp", "443"); 90 | 91 | THEN( "the return value is MBEDTLS_ERR_NET_UNKNOWN_HOST" ) { 92 | REQUIRE( error == MBEDTLS_ERR_NET_UNKNOWN_HOST ); 93 | } 94 | } 95 | } 96 | } 97 | --------------------------------------------------------------------------------