Path: blob/master/libs/tomcrypt/src/math/radix_to_bin.c
5971 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file radix_to_bin.c12Convert data from a specific radix to binary.13Steffen Jaeckel14*/1516/**17Convert data from a specific radix to binary1819The default MPI descriptors #ltm_desc, #tfm_desc and #gmp_desc20have the following restrictions on parameters:2122\p in - NUL-terminated char buffer2324\p radix - 2..642526@param in The input27@param radix The radix of the input28@param out The output buffer29@param len [in/out] The length of the output buffer3031@return CRYPT_OK on success.32*/33int radix_to_bin(const void *in, int radix, void *out, unsigned long *len)34{35unsigned long l;36void* mpi;37int err;3839LTC_ARGCHK(in != NULL);40LTC_ARGCHK(len != NULL);4142if ((err = mp_init(&mpi)) != CRYPT_OK) return err;43if ((err = mp_read_radix(mpi, in, radix)) != CRYPT_OK) goto LBL_ERR;4445if ((l = mp_unsigned_bin_size(mpi)) > *len) {46*len = l;47err = CRYPT_BUFFER_OVERFLOW;48goto LBL_ERR;49}50*len = l;5152if ((err = mp_to_unsigned_bin(mpi, out)) != CRYPT_OK) goto LBL_ERR;5354LBL_ERR:55mp_clear(mpi);56return err;57}585960