Skip to content

usage: general

amir zamani edited this page Aug 5, 2016 · 1 revision

input and output

buffer_t (synonym for std::string) and buffer_view_t are widely used in mbedcrypto api as main data container for input / output methods.

namespace mbedcrypto {
    using buffer_t = std::string;
}

current std::string implementations are known to be contiguous, so I prefer std::string over std::vector<unsigned char> because it helps users to easily feed both text and binary buffers into mbedtls api without any cast or conversion.

buffer_view_t is a minimalistic implementation of an object that can refer to a constant contiguous sequence of unsigned char like objects with the first element of the sequence at position zero.

it is analogue to a mini std::experimental::string_view<unsigned char>, and can also capture data from Qt's QByteArray.

see configs.hpp

error handling

mbedcrypto objects and functions throw mbedcrypro::exception unless they're tagged by noexcept keyword.

try {
    //
    // mbedcrypto codes ...
    //
} catch ( mbedcrypto::exception& cerr ) {
    std::cerr << "the expanded error message is: " << cerr.what() << std::endl;
    int c_error_code = cerr.code(); // the underlying error code
}

the structure of cerr.what():

[message, prefix or function name][(error code in hex): [module name] error string]

ex:
mbedtls_md_starts(-0x5100): MD - Bad input parameters to function
 - function name: mbedtls_md_starts
 - error code: -0x5100
 - module name: MD (message digest)

the low level functions returns a non-zero int as an error and are tagged by noexcept:

int ret = an_object.a_low_level_method(...);
if ( ret != 0 ) {
    std::cout << "underlying error code: "
              << mbedcrypto::mbedtls_error_string(ret) << std::endl;
}