Skip to content

Commit 7db0c35

Browse files
authored
Merge pull request #180 from haydenroche5/mac
Improve the HMAC code.
2 parents ddf3e9e + 7449beb commit 7db0c35

File tree

1 file changed

+84
-7
lines changed

1 file changed

+84
-7
lines changed

src/we_mac.c

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,13 +1296,13 @@ static int we_hmac_pkey_asn1_size(const EVP_PKEY *pkey)
12961296
* @param len [in] Length of data in buffer.
12971297
* @returns 1 on success and 0 on failure.
12981298
*/
1299-
static int we_hmac_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
1300-
size_t len)
1299+
static int we_hmac_pkey_asn1_set_priv_key(EVP_PKEY *pkey,
1300+
const unsigned char *priv, size_t len)
13011301
{
13021302
int ret = 1;
13031303
ASN1_OCTET_STRING *asn1 = NULL;
13041304

1305-
WOLFENGINE_ENTER(WE_LOG_MAC, "we_hmac_set_priv_key");
1305+
WOLFENGINE_ENTER(WE_LOG_MAC, "we_hmac_pkey_asn1_set_priv_key");
13061306
WOLFENGINE_MSG_VERBOSE(WE_LOG_MAC, "ARGS [pkey = %p, priv = %p, len = %zu]",
13071307
pkey, priv, len);
13081308

@@ -1331,12 +1331,87 @@ static int we_hmac_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
13311331
ASN1_OCTET_STRING_free(asn1);
13321332
}
13331333

1334-
WOLFENGINE_LEAVE(WE_LOG_MAC, "we_hmac_set_priv_key", ret);
1334+
WOLFENGINE_LEAVE(WE_LOG_MAC, "we_hmac_pkey_asn1_set_priv_key", ret);
1335+
1336+
return ret;
1337+
}
1338+
1339+
/**
1340+
* Get the private key as a byte buffer from the passed in EVP_PKEY.
1341+
*
1342+
* @param pkey [in] EVP_PKEY to get private key buffer from.
1343+
* @param priv [out] Output byte buffer to hold private key.
1344+
* @param len [out] Holds the length of priv.
1345+
* @returns 1 on success, 0 on failure.
1346+
*/
1347+
static int we_hmac_pkey_asn1_get_priv_key(const EVP_PKEY *pkey,
1348+
unsigned char *priv, size_t *len)
1349+
{
1350+
int ret = 1;
1351+
const unsigned char* privTmp;
1352+
1353+
WOLFENGINE_ENTER(WE_LOG_MAC, "we_hmac_pkey_asn1_get_priv_key");
1354+
1355+
if (pkey == NULL || len == NULL) {
1356+
WOLFENGINE_ERROR_MSG(WE_LOG_MAC, "Bad argument.");
1357+
ret = 0;
1358+
}
1359+
1360+
if (ret == 1) {
1361+
privTmp = EVP_PKEY_get0_hmac(pkey, len);
1362+
if (privTmp == NULL) {
1363+
ret = 0;
1364+
}
1365+
else {
1366+
if (priv != NULL) {
1367+
XMEMCPY(priv, privTmp, *len);
1368+
}
1369+
}
1370+
}
1371+
1372+
WOLFENGINE_LEAVE(WE_LOG_MAC, "we_hmac_pkey_asn1_get_priv_key", ret);
13351373

13361374
return ret;
13371375
}
13381376
#endif
13391377

1378+
/**
1379+
* Compare the public keys (ASN1_OCTET_STRINGs) held in a and b.
1380+
*
1381+
* @param a [in] First key for comparison.
1382+
* @param b [in] Second key for comparison.
1383+
* @returns -2 on error, 1 if the keys match, and 0 if they don't.
1384+
*/
1385+
static int we_hmac_pkey_pub_cmp(const EVP_PKEY* a, const EVP_PKEY* b)
1386+
{
1387+
int ret = 1;
1388+
ASN1_OCTET_STRING* aString;
1389+
ASN1_OCTET_STRING* bString;
1390+
1391+
WOLFENGINE_ENTER(WE_LOG_MAC, "we_hmac_pkey_pub_cmp");
1392+
1393+
if (a == NULL || b == NULL) {
1394+
WOLFENGINE_ERROR_MSG(WE_LOG_MAC, "Bad argument.");
1395+
ret = -2;
1396+
}
1397+
1398+
if (ret == 1) {
1399+
aString = (ASN1_OCTET_STRING*)EVP_PKEY_get0((EVP_PKEY*)a);
1400+
bString = (ASN1_OCTET_STRING*)EVP_PKEY_get0((EVP_PKEY*)b);
1401+
1402+
if (aString != NULL && bString != NULL) {
1403+
ret = ASN1_OCTET_STRING_cmp(aString, bString) == 0;
1404+
}
1405+
else {
1406+
ret = 0;
1407+
}
1408+
}
1409+
1410+
WOLFENGINE_LEAVE(WE_LOG_MAC, "we_hmac_pkey_pub_cmp", ret);
1411+
1412+
return ret;
1413+
}
1414+
13401415
/**
13411416
* Create a new method and assign the functions to use for ASN.1 HMAC
13421417
* operations.
@@ -1360,11 +1435,13 @@ int we_init_hmac_pkey_asn1_meth(void)
13601435
if (ret == 1) {
13611436
/* Set with HMAC methods. */
13621437
EVP_PKEY_asn1_set_free(we_hmac_pkey_asn1_method, we_hmac_pkey_free);
1363-
EVP_PKEY_asn1_set_public(we_hmac_pkey_asn1_method, 0, 0, 0, 0,
1364-
we_hmac_pkey_asn1_size, 0);
1438+
EVP_PKEY_asn1_set_public(we_hmac_pkey_asn1_method, 0,
1439+
0, we_hmac_pkey_pub_cmp, 0, we_hmac_pkey_asn1_size, 0);
13651440
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
13661441
EVP_PKEY_asn1_set_set_priv_key(we_hmac_pkey_asn1_method,
1367-
we_hmac_set_priv_key);
1442+
we_hmac_pkey_asn1_set_priv_key);
1443+
EVP_PKEY_asn1_set_get_priv_key(we_hmac_pkey_asn1_method,
1444+
we_hmac_pkey_asn1_get_priv_key);
13681445
#endif
13691446
/* Add our created asn1 method to the internal list of available
13701447
* methods. */

0 commit comments

Comments
 (0)