0

code 1

uint8_t  ucAESKey_BASE64[] = "oFqTg0a0VrjiVU76M1WRVw==";
uint8_t *pucAESKey_BASE64;
pucAESKey_BASE64 = ucAESKey_BASE64;

code 2

uint8_t *pucAESKey_BASE64 = "oFqTg0a0VrjiVU76M1WRVw==";

I use mbedtls_base64_decode() to decode base64 string ,the API of mbedTLS.

int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
                   const unsigned char *src, size_t slen )

My program like this:

mbedtls_base64_decode(ucAESKey, sizeof(ucAESKey), &olen, 
                      pucAESKey_BASE64, strlen(pucAESKey_BASE64));

If the parameter *src use code 2 , the output is

00 00 00 83 46 b4 56 b8 e2 55 4e fa 33 55 91 57

if *src use code 1, the output is

a0 5a 93 83 46 b4 56 b8 e2 55 4e fa 33 55 91 57

and this is correct. Why?

RGW
  • 9
  • 2
  • 1
    Why are you using `strlen` on a buffer that is not `const char *`? – PaulMcKenzie Apr 13 '17 at 06:27
  • Probably having problems because you are declaring one as an array and the other as not: uint8_t ucAESKey_BASE64[] = "oFqTg0a0VrjiVU76M1WRVw=="; uint8_t *pucAESKey_BASE64 = "oFqTg0a0VrjiVU76M1WRVw=="; Should be: uint8_t ucAESKey_BASE64 = "oFqTg0a0VrjiVU76M1WRVw=="; uint8_t *pucAESKey_BASE64 = "oFqTg0a0VrjiVU76M1WRVw=="; – Edward B. Apr 13 '17 at 06:30
  • Show the declarations and how `ucAESKey` and `olen` are initialized. – Michael Burr Apr 13 '17 at 07:22
  • can you try with this: `char *pcaAESKey_BASE64 = "oFqTg0a0VrjiVU76M1WRVw==";` – Taha Paksu Apr 13 '17 at 07:26
  • 1
    Usually it's the issue of [using `sizeof` with pointers](http://stackoverflow.com/q/492384/69809), expecting it to return the same as `strlen`. Is `ucAESKey` initialized the same way in both cases? – vgru Apr 13 '17 at 08:00
  • 3
    It is highly unlikely that the exact thing you describe happens in reality. Please show a [mcve]. – n. m. could be an AI Apr 13 '17 at 08:22

1 Answers1

0

Since the input of mbedtls_base64_decode() is a base64 string, it is ok to use strlen, as we don't expect any null character within the input. I have tried reproducing your issue, but both inputs generate the same output, so I assume your other inputs are different, as @n.m. suggests. I have tried the following:

int main( int argc, char *argv[] )
{
    unsigned char ucAESKey[32] = { 0 };
    size_t olen = 32;
    size_t i;
#if 0
    uint8_t  ucAESKey_BASE64[] = "oFqTg0a0VrjiVU76M1WRVw==";
    uint8_t *pucAESKey_BASE64;
    pucAESKey_BASE64 = ucAESKey_BASE64;
#else
    uint8_t *pucAESKey_BASE64 = "oFqTg0a0VrjiVU76M1WRVw==";
#endif
    mbedtls_base64_decode(ucAESKey, sizeof(ucAESKey), &olen,
                   pucAESKey_BASE64, strlen(pucAESKey_BASE64));

    for (i = 0; i < olen; i++)
    {
        mbedtls_printf("0x%x ", ucAESKey[i]); 
    }
return 0;
}
Ron Eldor
  • 210
  • 1
  • 11