libavb: Avoid conflict with system-provided crc32 symbol.
Some environments define crc32() themselves which leads to a conflict
in avb_crc32.c where we define the crc32() function. Fix this by
prefixing with iavb_.
Also spotted that avb_rsa.c had two internal functions that weren't
marked as static functions. Fix this and future problems like this by
using -Wmissing-prototypes (except don't use it together with gtest
because it doesn't work there). Additionally prefix the identifiers
used in avb_rsa.c since they are very common (Key struct for example).
We could change all internal identifiers (function names, struct
names, macros) to be prefixed (with iavb_, IAvb, IAVB_ respectively)
but this CL does not do that.
Test: New unit tests and all unit tests pass.
Test: Manually tested on UEFI-based boot loader.
Bug: 63072777
Change-Id: Ie894679a64cd9c305b32d610dae2aae1d5fd486e
diff --git a/Android.bp b/Android.bp
index 48480d1..79914bd 100644
--- a/Android.bp
+++ b/Android.bp
@@ -24,6 +24,7 @@
"-Wall",
"-Wextra",
"-Wformat=2",
+ "-Wmissing-prototypes",
"-Wno-psabi",
"-Wno-unused-parameter",
"-ffunction-sections",
@@ -175,6 +176,9 @@
"libchrome",
"libcrypto",
],
+ cflags: [
+ "-Wno-missing-prototypes",
+ ],
srcs: [
"test/avb_ab_flow_unittest.cc",
"test/avb_atx_validate_unittest.cc",
diff --git a/libavb/avb_crc32.c b/libavb/avb_crc32.c
index 9abed54..7d4cb09 100644
--- a/libavb/avb_crc32.c
+++ b/libavb/avb_crc32.c
@@ -43,10 +43,11 @@
*/
#include "avb_sysdeps.h"
+#include "avb_util.h"
/* Code taken from FreeBSD 8 */
-static uint32_t crc32_tab[] = {
+static uint32_t iavb_crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
@@ -98,16 +99,16 @@
* in sys/libkern.h, where it can be inlined.
*/
-static uint32_t crc32(uint32_t crc_in, const uint8_t* buf, int size) {
+static uint32_t iavb_crc32(uint32_t crc_in, const uint8_t* buf, int size) {
const uint8_t* p = buf;
uint32_t crc;
crc = crc_in ^ ~0U;
while (size--)
- crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+ crc = iavb_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
return crc ^ ~0U;
}
uint32_t avb_crc32(const uint8_t* buf, size_t size) {
- return crc32(0, buf, size);
+ return iavb_crc32(0, buf, size);
}
diff --git a/libavb/avb_rsa.c b/libavb/avb_rsa.c
index dcecc16..f4cb322 100644
--- a/libavb/avb_rsa.c
+++ b/libavb/avb_rsa.c
@@ -37,16 +37,16 @@
#include "avb_util.h"
#include "avb_vbmeta_image.h"
-typedef struct Key {
+typedef struct IAvbKey {
unsigned int len; /* Length of n[] in number of uint32_t */
uint32_t n0inv; /* -1 / n[0] mod 2^32 */
uint32_t* n; /* modulus as array (host-byte order) */
uint32_t* rr; /* R^2 as array (host-byte order) */
-} Key;
+} IAvbKey;
-Key* parse_key_data(const uint8_t* data, size_t length) {
+static IAvbKey* iavb_parse_key_data(const uint8_t* data, size_t length) {
AvbRSAPublicKeyHeader h;
- Key* key = NULL;
+ IAvbKey* key = NULL;
size_t expected_length;
unsigned int i;
const uint8_t* n;
@@ -76,14 +76,14 @@
/* Store n and rr following the key header so we only have to do one
* allocation.
*/
- key = (Key*)(avb_malloc(sizeof(Key) + 2 * h.key_num_bits / 8));
+ key = (IAvbKey*)(avb_malloc(sizeof(IAvbKey) + 2 * h.key_num_bits / 8));
if (key == NULL) {
goto fail;
}
key->len = h.key_num_bits / 32;
key->n0inv = h.n0inv;
- key->n = (uint32_t*)(key + 1); /* Skip ahead sizeof(Key) bytes. */
+ key->n = (uint32_t*)(key + 1); /* Skip ahead sizeof(IAvbKey) bytes. */
key->rr = key->n + key->len;
/* Crypto-code below (modpowF4() and friends) expects the key in
@@ -103,12 +103,12 @@
return NULL;
}
-void free_parsed_key(Key* key) {
+static void iavb_free_parsed_key(IAvbKey* key) {
avb_free(key);
}
/* a[] -= mod */
-static void subM(const Key* key, uint32_t* a) {
+static void subM(const IAvbKey* key, uint32_t* a) {
int64_t A = 0;
uint32_t i;
for (i = 0; i < key->len; ++i) {
@@ -119,7 +119,7 @@
}
/* return a[] >= mod */
-static int geM(const Key* key, uint32_t* a) {
+static int geM(const IAvbKey* key, uint32_t* a) {
uint32_t i;
for (i = key->len; i;) {
--i;
@@ -134,7 +134,7 @@
}
/* montgomery c[] += a * b[] / R % mod */
-static void montMulAdd(const Key* key,
+static void montMulAdd(const IAvbKey* key,
uint32_t* c,
const uint32_t a,
const uint32_t* b) {
@@ -159,7 +159,7 @@
}
/* montgomery c[] = a[] * b[] / R % mod */
-static void montMul(const Key* key, uint32_t* c, uint32_t* a, uint32_t* b) {
+static void montMul(const IAvbKey* key, uint32_t* c, uint32_t* a, uint32_t* b) {
uint32_t i;
for (i = 0; i < key->len; ++i) {
c[i] = 0;
@@ -172,7 +172,7 @@
/* In-place public exponentiation. (65537}
* Input and output big-endian byte array in inout.
*/
-static void modpowF4(const Key* key, uint8_t* inout) {
+static void modpowF4(const IAvbKey* key, uint8_t* inout) {
uint32_t* a = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
uint32_t* aR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
uint32_t* aaR = (uint32_t*)avb_malloc(key->len * sizeof(uint32_t));
@@ -237,7 +237,7 @@
const uint8_t* padding,
size_t padding_num_bytes) {
uint8_t* buf = NULL;
- Key* parsed_key = NULL;
+ IAvbKey* parsed_key = NULL;
bool success = false;
if (key == NULL || sig == NULL || hash == NULL || padding == NULL) {
@@ -245,7 +245,7 @@
goto out;
}
- parsed_key = parse_key_data(key, key_num_bytes);
+ parsed_key = iavb_parse_key_data(key, key_num_bytes);
if (parsed_key == NULL) {
avb_error("Error parsing key.\n");
goto out;
@@ -290,7 +290,7 @@
out:
if (parsed_key != NULL) {
- free_parsed_key(parsed_key);
+ iavb_free_parsed_key(parsed_key);
}
if (buf != NULL) {
avb_free(buf);