Skip to content

Commit a9e41d6

Browse files
committed
base64.c: Fix off-by-one error and cleanup.
1 parent 012d929 commit a9e41d6

File tree

5 files changed

+62
-28
lines changed

5 files changed

+62
-28
lines changed

src/base64.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@
2828
YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz
2929
3030
*/
31-
#ifndef BASE64_H
32-
#define BASE64_H
3331

3432
#include "base64.h"
35-
#include <stdio.h>
36-
#include <string.h>
3733
#include <stdlib.h>
34+
#include <string.h>
3835

3936
static const char *b64 =
4037
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
@@ -67,11 +64,8 @@ static const unsigned char unb64[] = {
6764
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //240
6865
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //250
6966
0, 0, 0, 0, 0, 0,
70-
}; // This array has 255 elements
67+
}; // This array has 256 elements
7168

72-
// Converts binary data of length=len to base64 characters.
73-
// Length of the resultant string is stored in flen
74-
// (you must pass pointer flen).
7569
char *base64( const void *binaryData, int len, int *flen )
7670
{
7771
const unsigned char *bin = (const unsigned char *) binaryData ;
@@ -124,8 +118,8 @@ unsigned char *unbase64( const char *ascii, int len, int *flen )
124118
int charNo;
125119
int pad = 0 ;
126120

127-
if ( len < 2 ) { // 2 accesses below would be OOB.
128-
// catch empty string, return NULL as result.
121+
if ((len <= 0) || (len % 4 != 0)) { // 2 accesses below would be OOB.
122+
// catch empty string or incorrect padding size, return NULL as result.
129123
*flen = 0;
130124
return 0;
131125
}
@@ -149,6 +143,14 @@ unsigned char *unbase64( const char *ascii, int len, int *flen )
149143
}
150144
}
151145

146+
/*
147+
* Length is guaranteed to be >0 and a multiple of 4 here,
148+
* so the integer division will be exact and positive.
149+
* Any '=' in the input padding removes a byte
150+
* from the output.
151+
*
152+
* Note that this guarantees a positive value for flen.
153+
*/
152154
*flen = 3 * len / 4 - pad ;
153155
bin = malloc( *flen ) ;
154156
if ( !bin ) {
@@ -182,5 +184,3 @@ unsigned char *unbase64( const char *ascii, int len, int *flen )
182184

183185
return bin ;
184186
}
185-
186-
#endif

src/base64.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,28 @@
3636
#ifndef _BASE64_H_
3737
#define _BASE64_H_
3838

39-
40-
// Converts binary data of length=len to base64 characters.
41-
// Length of the resultant string is stored in flen
42-
// (you must pass pointer flen).
43-
char *base64( const void *binaryData, int len, int *flen );
39+
/**
40+
* Converts binary data of length=len to base64 characters.
41+
*
42+
* @param[in] binaryData Binary buffer to encode. Must be len bytes long.
43+
* @paramm[in] len Length of the binaryData buffer.
44+
* @param[out] flen Resulting length of the encoded buffer.
45+
* @return Decoded buffer, or NULL if an error occurred. Must be free()'d by the
46+
* caller.
47+
*/
48+
char *base64(const void *binaryData, int len, int *flen);
49+
50+
/**
51+
* Decodes the provided base64 string.
52+
*
53+
* @param[in] ascii Base64-encoded buffer to decoded. Must contain a
54+
* zero-terminated string.
55+
* @param[in] len Length of the string to decode (excluding the terminating
56+
* character).
57+
* @param[out] flen Resulting length of the decoded buffer.
58+
* @return Decoded buffer, or NULL if an error occurred. Must be free()'d by the
59+
* caller.
60+
*/
4461
unsigned char *unbase64( const char *ascii, int len, int *flen );
4562

46-
4763
#endif

src/cipher.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ static uint8_t *cipher_aes_encrypt(const unsigned char *in, int inlen,
101101
return enc_cat;
102102
}
103103

104-
// Encrypts a given constant char array of length inlen using the AES algorithm with CBC mode
105-
// and base64 encodes the result.
106-
//
107-
// Must free() returned value (allocated inside base64() function)
104+
/**
105+
* Encrypts a given constant char array of length inlen using the AES algorithm with CBC mode
106+
* and base64 encodes the result.
107+
*
108+
* Must free() returned value (allocated inside base64() function)
109+
*
110+
* @return Encrypted, base64-encoded buffer, or NULL in case of error.
111+
*/
108112
char *cipher_aes_b64_encrypt(const unsigned char *in, int inlen, int *outb64len,
109113
const uint8_t *key)
110114
{
@@ -240,10 +244,16 @@ char *cipher_aes_hmac_decrypt(const uint8_t *in, int inlen,
240244
return ret;
241245
}
242246

243-
// Decrypts a given constant char array of length inlen using the AES algorithm with CBC mode
244-
// and base64 decodes the result.
245-
//
246-
// Must free() returned value (allocated inside base64() function)
247+
/**
248+
* Decrypts a given constant char array of length inlen using the AES algorithm with CBC mode
249+
* and base64 decodes the result.
250+
*
251+
* Must free() returned value (allocated inside base64() function)
252+
* @param[in] in Pointer to the buffer to decrpyt. Must contain a
253+
* NULL-terminated string.
254+
* @param[in] inlen Length of the buffer to decrypt, excluding the
255+
* terminating NULL character.
256+
*/
247257
char *cipher_aes_b64_hmac_decrypt(const unsigned char *in, int inlen, int *out_msg_len,
248258
const uint8_t *secret)
249259
{
@@ -273,7 +283,13 @@ char *cipher_aes_b64_hmac_decrypt(const unsigned char *in, int inlen, int *out_m
273283
return decrypted;
274284
}
275285

276-
// Must free() returned value
286+
/**
287+
* Must free() returned value.
288+
* @param[in] in Pointer to the buffer to decrpyt. Must contain a
289+
* NULL-terminated string.
290+
* @param[in] inlen Length of the buffer to decrypt, excluding the
291+
* terminating NULL character.
292+
*/
277293
char *cipher_aes_b64_decrypt(const unsigned char *in, int inlen, int *outlen,
278294
const uint8_t *key)
279295
{

tests/api.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ static void api_reset_device(void)
478478
}
479479

480480

481+
/**
482+
* @return Buffer containing the read value. Guaranteed to be nonnull.
483+
*/
481484
static const char *api_read_value(int cmd)
482485
{
483486
static char value[HID_REPORT_SIZE];

tests/tests_unit.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include "random.h"
4141
#include "base64.h"
4242
#include "base58.h"
43-
#include "base64.h"
4443
#include "pbkdf2.h"
4544
#include "flags.h"
4645
#include "flash.h"

0 commit comments

Comments
 (0)