Skip to content

Commit

Permalink
Bug 1317923 - Add hexString2SecItem to secutil, r=ttaubert
Browse files Browse the repository at this point in the history
Differential Revision: https://nss-review.dev.mozaws.net/D70

--HG--
extra : amend_source : 959eb85cc2ae0e97058d1c311d507483a7e1f6b7
  • Loading branch information
franziskuskiefer committed Nov 16, 2016
1 parent 88ded4a commit 3053d47
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 941 deletions.
65 changes: 6 additions & 59 deletions cmd/ecperf/ecperf.c
Expand Up @@ -9,6 +9,7 @@
#include "basicutil.h"
#include "pkcs11.h"
#include "nspr.h"
#include "secutil.h"
#include <stdio.h>

#define __PASTE(x, y) x##y
Expand Down Expand Up @@ -292,60 +293,6 @@ M_TimeOperation(void (*threadFunc)(void *),
printf("... okay.\n"); \
}

/*
* Initializes a SECItem from a hexadecimal string
*
* Warning: This function ignores leading 00's, so any leading 00's
* in the hexadecimal string must be optional.
*/
static SECItem *
hexString2SECItem(PLArenaPool *arena, SECItem *item, const char *str)
{
int i = 0;
int byteval = 0;
int tmp = PORT_Strlen(str);

PORT_Assert(arena);
PORT_Assert(item);

if ((tmp % 2) != 0) {
return NULL;
}

/* skip leading 00's unless the hex string is "00" */
while ((tmp > 2) && (str[0] == '0') && (str[1] == '0')) {
str += 2;
tmp -= 2;
}

item = SECITEM_AllocItem(arena, item, tmp / 2);
if (item == NULL) {
return NULL;
}

while (str[i]) {
if ((str[i] >= '0') && (str[i] <= '9')) {
tmp = str[i] - '0';
} else if ((str[i] >= 'a') && (str[i] <= 'f')) {
tmp = str[i] - 'a' + 10;
} else if ((str[i] >= 'A') && (str[i] <= 'F')) {
tmp = str[i] - 'A' + 10;
} else {
/* item is in arena and gets freed by the caller */
return NULL;
}

byteval = byteval * 16 + tmp;
if ((i % 2) != 0) {
item->data[i / 2] = byteval;
byteval = 0;
}
i++;
}

return item;
}

#define PK11_SETATTRS(x, id, v, l) \
(x)->type = (id); \
(x)->pValue = (v); \
Expand Down Expand Up @@ -622,16 +569,16 @@ ectest_curve_freebl(ECCurveName curve, int iterations, int numThreads,

ecParams.fieldID.size = ecCurve_map[curve]->size;
ecParams.fieldID.type = fieldType;
hexString2SECItem(arena, &ecParams.fieldID.u.prime, ecCurve_map[curve]->irr);
hexString2SECItem(arena, &ecParams.curve.a, ecCurve_map[curve]->curvea);
hexString2SECItem(arena, &ecParams.curve.b, ecCurve_map[curve]->curveb);
SECU_HexString2SECItem(arena, &ecParams.fieldID.u.prime, ecCurve_map[curve]->irr);
SECU_HexString2SECItem(arena, &ecParams.curve.a, ecCurve_map[curve]->curvea);
SECU_HexString2SECItem(arena, &ecParams.curve.b, ecCurve_map[curve]->curveb);
genenc[0] = '0';
genenc[1] = '4';
genenc[2] = '\0';
strcat(genenc, ecCurve_map[curve]->genx);
strcat(genenc, ecCurve_map[curve]->geny);
hexString2SECItem(arena, &ecParams.base, genenc);
hexString2SECItem(arena, &ecParams.order, ecCurve_map[curve]->order);
SECU_HexString2SECItem(arena, &ecParams.base, genenc);
SECU_HexString2SECItem(arena, &ecParams.order, ecCurve_map[curve]->order);
ecParams.cofactor = ecCurve_map[curve]->cofactor;

PORT_Memset(digestData, 0xa5, sizeof(digestData));
Expand Down
2 changes: 1 addition & 1 deletion cmd/ecperf/ecperf.gyp
Expand Up @@ -22,7 +22,7 @@
],
'target_defaults': {
'include_dirs': [
'../../nss/lib/softoken'
'<(DEPTH)/lib/softoken',
],
'defines': [
'NSS_USE_STATIC_LIBS'
Expand Down
67 changes: 11 additions & 56 deletions cmd/fbectest/fbectest.c
Expand Up @@ -9,6 +9,7 @@
#include "basicutil.h"
#include "secder.h"
#include "secitem.h"
#include "secutil.h"
#include "nspr.h"
#include <stdio.h>

Expand All @@ -32,52 +33,6 @@ typedef struct {

#include "testvecs.h"

/*
* Initializes a SECItem from a hexadecimal string
*
*/
static SECItem *
hexString2SECItem(PLArenaPool *arena, SECItem *item, const char *str)
{
int i = 0;
int byteval = 0;
int tmp = PORT_Strlen(str);

PORT_Assert(arena);
PORT_Assert(item);

if ((tmp % 2) != 0) {
return NULL;
}

item = SECITEM_AllocItem(arena, item, tmp / 2);
if (item == NULL) {
return NULL;
}

while (str[i]) {
if ((str[i] >= '0') && (str[i] <= '9')) {
tmp = str[i] - '0';
} else if ((str[i] >= 'a') && (str[i] <= 'f')) {
tmp = str[i] - 'a' + 10;
} else if ((str[i] >= 'A') && (str[i] <= 'F')) {
tmp = str[i] - 'A' + 10;
} else {
/* item is in arena and gets freed by the caller */
return NULL;
}

byteval = byteval * 16 + tmp;
if ((i % 2) != 0) {
item->data[i / 2] = byteval;
byteval = 0;
}
i++;
}

return item;
}

void
printBuf(const SECItem *item)
{
Expand Down Expand Up @@ -143,23 +98,23 @@ ectest_ecdh_kat(ECDH_KAT *kat)
return rv;
}

hexString2SECItem(arena, &ecParams.fieldID.u.prime, ecCurve_map[curve]->irr);
hexString2SECItem(arena, &ecParams.curve.a, ecCurve_map[curve]->curvea);
hexString2SECItem(arena, &ecParams.curve.b, ecCurve_map[curve]->curveb);
SECU_HexString2SECItem(arena, &ecParams.fieldID.u.prime, ecCurve_map[curve]->irr);
SECU_HexString2SECItem(arena, &ecParams.curve.a, ecCurve_map[curve]->curvea);
SECU_HexString2SECItem(arena, &ecParams.curve.b, ecCurve_map[curve]->curveb);
genenc[0] = '0';
genenc[1] = '4';
genenc[2] = '\0';
PORT_Assert(PR_ARRAY_SIZE(genenc) >= PORT_Strlen(ecCurve_map[curve]->genx));
PORT_Assert(PR_ARRAY_SIZE(genenc) >= PORT_Strlen(ecCurve_map[curve]->geny));
strcat(genenc, ecCurve_map[curve]->genx);
strcat(genenc, ecCurve_map[curve]->geny);
hexString2SECItem(arena, &ecParams.base, genenc);
hexString2SECItem(arena, &ecParams.order, ecCurve_map[curve]->order);
SECU_HexString2SECItem(arena, &ecParams.base, genenc);
SECU_HexString2SECItem(arena, &ecParams.order, ecCurve_map[curve]->order);

if (kat->our_pubhex) {
hexString2SECItem(arena, &answer, kat->our_pubhex);
SECU_HexString2SECItem(arena, &answer, kat->our_pubhex);
}
hexString2SECItem(arena, &seed, kat->privhex);
SECU_HexString2SECItem(arena, &seed, kat->privhex);
rv = EC_NewKeyFromSeed(&ecParams, &ecPriv, seed.data, seed.len);
if (rv != SECSuccess) {
rv = SECFailure;
Expand All @@ -172,8 +127,8 @@ ectest_ecdh_kat(ECDH_KAT *kat)
}
}

hexString2SECItem(arena, &theirKey, kat->their_pubhex);
hexString2SECItem(arena, &answer2, kat->common_key);
SECU_HexString2SECItem(arena, &theirKey, kat->their_pubhex);
SECU_HexString2SECItem(arena, &answer2, kat->common_key);

rv = EC_ValidatePublicKey(&ecParams, &theirKey);
if (rv != SECSuccess) {
Expand Down Expand Up @@ -231,7 +186,7 @@ ectest_validate_point(ECDH_BAD *bad)
return rv;
}

hexString2SECItem(arena, &point, bad->point);
SECU_HexString2SECItem(arena, &point, bad->point);
rv = EC_ValidatePublicKey(&ecParams, &point);

PORT_FreeArena(arena, PR_FALSE);
Expand Down
2 changes: 1 addition & 1 deletion cmd/fbectest/fbectest.gyp
Expand Up @@ -21,7 +21,7 @@
],
'target_defaults': {
'include_dirs': [
'../../nss/lib/softoken'
'<(DEPTH)/lib/softoken',
],
'defines': [
'NSS_USE_STATIC_LIBS'
Expand Down
43 changes: 43 additions & 0 deletions cmd/lib/secutil.c
Expand Up @@ -3832,3 +3832,46 @@ SECU_ParseSSLVersionRangeString(const char *input,

return SECSuccess;
}

SECItem *
SECU_HexString2SECItem(PLArenaPool *arena, SECItem *item, const char *str)
{
int i = 0;
int byteval = 0;
int tmp = PORT_Strlen(str);

PORT_Assert(arena);
PORT_Assert(item);

if ((tmp % 2) != 0) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return NULL;
}

item = SECITEM_AllocItem(arena, item, tmp / 2);
if (item == NULL) {
return NULL;
}

while (str[i]) {
if ((str[i] >= '0') && (str[i] <= '9')) {
tmp = str[i] - '0';
} else if ((str[i] >= 'a') && (str[i] <= 'f')) {
tmp = str[i] - 'a' + 10;
} else if ((str[i] >= 'A') && (str[i] <= 'F')) {
tmp = str[i] - 'A' + 10;
} else {
/* item is in arena and gets freed by the caller */
return NULL;
}

byteval = byteval * 16 + tmp;
if ((i % 2) != 0) {
item->data[i / 2] = byteval;
byteval = 0;
}
i++;
}

return item;
}
17 changes: 11 additions & 6 deletions cmd/lib/secutil.h
Expand Up @@ -322,12 +322,12 @@ extern SECStatus SECU_StoreCRL(PK11SlotInfo *slot, SECItem *derCrl,
** MD5 hashing algorithm. This routine first computes a digital signature
** using SEC_SignData, then wraps it with an CERTSignedData and then der
** encodes the result.
** "arena" is the memory arena to use to allocate data from
** "sd" returned CERTSignedData
** "result" the final der encoded data (memory is allocated)
** "buf" the input data to sign
** "len" the amount of data to sign
** "pk" the private key to encrypt with
** "arena" is the memory arena to use to allocate data from
** "sd" returned CERTSignedData
** "result" the final der encoded data (memory is allocated)
** "buf" the input data to sign
** "len" the amount of data to sign
** "pk" the private key to encrypt with
*/
extern SECStatus SECU_DerSignDataCRL(PLArenaPool *arena, CERTSignedData *sd,
unsigned char *buf, int len,
Expand Down Expand Up @@ -402,6 +402,11 @@ SECStatus
SECU_ParseSSLVersionRangeString(const char *input,
const SSLVersionRange defaultVersionRange,
SSLVersionRange *vrange);
/*
** Read a hex string into a SecItem.
*/
extern SECItem *SECU_HexString2SECItem(PLArenaPool *arena, SECItem *item,
const char *str);

/*
*
Expand Down

0 comments on commit 3053d47

Please sign in to comment.