Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20241204-more-C89-expansion #8253

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions wolfcrypt/src/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -6509,7 +6509,7 @@ static WC_INLINE void RIGHTSHIFTX(byte* x)
{
int i;
int carryIn = 0;
byte borrow = (0x00 - (x[15] & 0x01)) & 0xE1;
byte borrow = (byte)((0x00U - (x[15] & 0x01U)) & 0xE1U);

for (i = 0; i < WC_AES_BLOCK_SIZE; i++) {
int carryOut = (x[i] & 0x01) << 7;
Expand Down Expand Up @@ -8037,13 +8037,13 @@ static void GHASH_UPDATE(Aes* aes, const byte* a, word32 aSz, const byte* c,
/* Check if we have unprocessed data. */
if (aes->aOver > 0) {
/* Calculate amount we can use - fill up the block. */
byte sz = WC_AES_BLOCK_SIZE - aes->aOver;
byte sz = (byte)(WC_AES_BLOCK_SIZE - aes->aOver);
if (sz > aSz) {
sz = (byte)aSz;
}
/* Copy extra into last GHASH block array and update count. */
XMEMCPY(AES_LASTGBLOCK(aes) + aes->aOver, a, sz);
aes->aOver += sz;
aes->aOver = (byte)(aes->aOver + sz);
SparkiDev marked this conversation as resolved.
Show resolved Hide resolved
if (aes->aOver == WC_AES_BLOCK_SIZE) {
/* We have filled up the block and can process. */
GHASH_ONE_BLOCK(aes, AES_LASTGBLOCK(aes));
Expand Down Expand Up @@ -8072,7 +8072,7 @@ static void GHASH_UPDATE(Aes* aes, const byte* a, word32 aSz, const byte* c,
if (aes->aOver > 0 && cSz > 0 && c != NULL) {
/* No more AAD coming and we have a partial block. */
/* Fill the rest of the block with zeros. */
byte sz = WC_AES_BLOCK_SIZE - aes->aOver;
byte sz = (byte)(WC_AES_BLOCK_SIZE - aes->aOver);
XMEMSET(AES_LASTGBLOCK(aes) + aes->aOver, 0, sz);
/* GHASH last AAD block. */
GHASH_ONE_BLOCK(aes, AES_LASTGBLOCK(aes));
Expand All @@ -8086,13 +8086,13 @@ static void GHASH_UPDATE(Aes* aes, const byte* a, word32 aSz, const byte* c,
aes->cSz += cSz;
if (aes->cOver > 0) {
/* Calculate amount we can use - fill up the block. */
byte sz = WC_AES_BLOCK_SIZE - aes->cOver;
byte sz = (byte)(WC_AES_BLOCK_SIZE - aes->cOver);
if (sz > cSz) {
sz = (byte)cSz;
}
XMEMCPY(AES_LASTGBLOCK(aes) + aes->cOver, c, sz);
/* Update count of unused encrypted counter. */
aes->cOver += sz;
aes->cOver = (byte)(aes->cOver + sz);
if (aes->cOver == WC_AES_BLOCK_SIZE) {
/* We have filled up the block and can process. */
GHASH_ONE_BLOCK(aes, AES_LASTGBLOCK(aes));
Expand Down Expand Up @@ -8139,7 +8139,7 @@ static void GHASH_FINAL(Aes* aes, byte* s, word32 sSz)
}
if (over > 0) {
/* Zeroize the unused part of the block. */
XMEMSET(AES_LASTGBLOCK(aes) + over, 0, WC_AES_BLOCK_SIZE - over);
XMEMSET(AES_LASTGBLOCK(aes) + over, 0, (size_t)WC_AES_BLOCK_SIZE - over);
/* Hash the last block of cipher text. */
GHASH_ONE_BLOCK(aes, AES_LASTGBLOCK(aes));
}
Expand Down Expand Up @@ -9352,7 +9352,7 @@ static WARN_UNUSED_RESULT int AesGcmCryptUpdate_C(

/* Check if previous encrypted block was not used up. */
if (aes->over > 0) {
byte pSz = WC_AES_BLOCK_SIZE - aes->over;
byte pSz = (byte)(WC_AES_BLOCK_SIZE - aes->over);
if (pSz > sz) pSz = (byte)sz;

/* Use some/all of last encrypted block. */
Expand Down Expand Up @@ -9579,13 +9579,13 @@ static WARN_UNUSED_RESULT int AesGcmAadUpdate_aesni(
/* Check if we have unprocessed data. */
if (aes->aOver > 0) {
/* Calculate amount we can use - fill up the block. */
byte sz = WC_AES_BLOCK_SIZE - aes->aOver;
byte sz = (byte)(WC_AES_BLOCK_SIZE - aes->aOver);
if (sz > aSz) {
sz = (byte)aSz;
}
/* Copy extra into last GHASH block array and update count. */
XMEMCPY(AES_LASTGBLOCK(aes) + aes->aOver, a, sz);
aes->aOver += sz;
aes->aOver = (byte)(aes->aOver + sz);
if (aes->aOver == WC_AES_BLOCK_SIZE) {
/* We have filled up the block and can process. */
#ifdef HAVE_INTEL_AVX2
Expand Down Expand Up @@ -9650,7 +9650,7 @@ static WARN_UNUSED_RESULT int AesGcmAadUpdate_aesni(
/* No more AAD coming and we have a partial block. */
/* Fill the rest of the block with zeros. */
XMEMSET(AES_LASTGBLOCK(aes) + aes->aOver, 0,
WC_AES_BLOCK_SIZE - aes->aOver);
(size_t)WC_AES_BLOCK_SIZE - aes->aOver);
/* GHASH last AAD block. */
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_AVX2(intel_flags)) {
Expand Down Expand Up @@ -9708,15 +9708,15 @@ static WARN_UNUSED_RESULT int AesGcmEncryptUpdate_aesni(
aes->cSz += cSz;
if (aes->cOver > 0) {
/* Calculate amount we can use - fill up the block. */
byte sz = WC_AES_BLOCK_SIZE - aes->cOver;
byte sz = (byte)(WC_AES_BLOCK_SIZE - aes->cOver);
if (sz > cSz) {
sz = (byte)cSz;
}
/* Encrypt some of the plaintext. */
xorbuf(AES_LASTGBLOCK(aes) + aes->cOver, p, sz);
XMEMCPY(c, AES_LASTGBLOCK(aes) + aes->cOver, sz);
/* Update count of unused encrypted counter. */
aes->cOver += sz;
aes->cOver = (byte)(aes->cOver + sz);
if (aes->cOver == WC_AES_BLOCK_SIZE) {
/* We have filled up the block and can process. */
#ifdef HAVE_INTEL_AVX2
Expand Down Expand Up @@ -9832,7 +9832,7 @@ static WARN_UNUSED_RESULT int AesGcmEncryptFinal_aesni(
}
if (over > 0) {
/* Fill the rest of the block with zeros. */
XMEMSET(AES_LASTGBLOCK(aes) + over, 0, WC_AES_BLOCK_SIZE - over);
XMEMSET(AES_LASTGBLOCK(aes) + over, 0, (size_t)WC_AES_BLOCK_SIZE - over);
/* GHASH last cipher block. */
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_AVX2(intel_flags)) {
Expand Down Expand Up @@ -9939,7 +9939,7 @@ static WARN_UNUSED_RESULT int AesGcmDecryptUpdate_aesni(
aes->cSz += cSz;
if (aes->cOver > 0) {
/* Calculate amount we can use - fill up the block. */
byte sz = WC_AES_BLOCK_SIZE - aes->cOver;
byte sz = (byte)(WC_AES_BLOCK_SIZE - aes->cOver);
if (sz > cSz) {
sz = (byte)cSz;
}
Expand All @@ -9949,7 +9949,7 @@ static WARN_UNUSED_RESULT int AesGcmDecryptUpdate_aesni(
xorbuf(AES_LASTGBLOCK(aes) + aes->cOver, c, sz);
XMEMCPY(p, AES_LASTGBLOCK(aes) + aes->cOver, sz);
/* Update count of unused encrypted counter. */
aes->cOver += sz;
aes->cOver = (byte)(aes->cOver + sz);
if (aes->cOver == WC_AES_BLOCK_SIZE) {
/* We have filled up the block and can process. */
#ifdef HAVE_INTEL_AVX2
Expand Down Expand Up @@ -10072,7 +10072,7 @@ static WARN_UNUSED_RESULT int AesGcmDecryptFinal_aesni(
}
if (over > 0) {
/* Zeroize the unused part of the block. */
XMEMSET(lastBlock + over, 0, WC_AES_BLOCK_SIZE - over);
XMEMSET(lastBlock + over, 0, (size_t)WC_AES_BLOCK_SIZE - over);
/* Hash the last block of cipher text. */
#ifdef HAVE_INTEL_AVX2
if (IS_INTEL_AVX2(intel_flags)) {
Expand Down Expand Up @@ -11044,14 +11044,14 @@ static WC_INLINE void AesCcmCtrIncSet4(byte* B, word32 lenSz)
for (i = 0; i < lenSz; i++) {
if (++B[WC_AES_BLOCK_SIZE * 2 - 1 - i] != 0) break;
}
B[WC_AES_BLOCK_SIZE * 3 - 1] += 2;
if (B[WC_AES_BLOCK_SIZE * 3 - 1] < 2) {
B[WC_AES_BLOCK_SIZE * 3 - 1] = (byte)(B[WC_AES_BLOCK_SIZE * 3 - 1] + 2U);
if (B[WC_AES_BLOCK_SIZE * 3 - 1] < 2U) {
for (i = 1; i < lenSz; i++) {
if (++B[WC_AES_BLOCK_SIZE * 3 - 1 - i] != 0) break;
}
}
B[WC_AES_BLOCK_SIZE * 4 - 1] += 3;
if (B[WC_AES_BLOCK_SIZE * 4 - 1] < 3) {
B[WC_AES_BLOCK_SIZE * 4 - 1] = (byte)(B[WC_AES_BLOCK_SIZE * 4 - 1] + 3U);
if (B[WC_AES_BLOCK_SIZE * 4 - 1] < 3U) {
for (i = 1; i < lenSz; i++) {
if (++B[WC_AES_BLOCK_SIZE * 4 - 1 - i] != 0) break;
}
Expand All @@ -11062,8 +11062,8 @@ static WC_INLINE void AesCcmCtrInc4(byte* B, word32 lenSz)
{
word32 i;

B[WC_AES_BLOCK_SIZE - 1] += 4;
if (B[WC_AES_BLOCK_SIZE - 1] < 4) {
B[WC_AES_BLOCK_SIZE - 1] = (byte)(B[WC_AES_BLOCK_SIZE - 1] + 4U);
if (B[WC_AES_BLOCK_SIZE - 1] < 4U) {
for (i = 1; i < lenSz; i++) {
if (++B[WC_AES_BLOCK_SIZE - 1 - i] != 0) break;
}
Expand Down Expand Up @@ -11123,7 +11123,7 @@ int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,

XMEMSET(A, 0, sizeof(A));
XMEMCPY(B+1, nonce, nonceSz);
lenSz = WC_AES_BLOCK_SIZE - 1 - (byte)nonceSz;
lenSz = (byte)(WC_AES_BLOCK_SIZE - 1U - nonceSz);
B[0] = (byte)((authInSz > 0 ? 64 : 0)
+ (8 * (((byte)authTagSz - 2) / 2))
+ (lenSz - 1));
Expand Down Expand Up @@ -11153,7 +11153,7 @@ int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
if (ret == 0) {
XMEMCPY(authTag, A, authTagSz);

B[0] = lenSz - 1;
B[0] = (byte)(lenSz - 1U);
for (i = 0; i < lenSz; i++)
B[WC_AES_BLOCK_SIZE - 1 - i] = 0;
ret = wc_AesEncrypt(aes, B, A);
Expand Down Expand Up @@ -11272,9 +11272,9 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
oSz = inSz;
XMEMSET(A, 0, sizeof A);
XMEMCPY(B+1, nonce, nonceSz);
lenSz = WC_AES_BLOCK_SIZE - 1 - (byte)nonceSz;
lenSz = (byte)(WC_AES_BLOCK_SIZE - 1U - nonceSz);

B[0] = lenSz - 1;
B[0] = (byte)(lenSz - 1U);
for (i = 0; i < lenSz; i++)
B[WC_AES_BLOCK_SIZE - 1 - i] = 0;
B[15] = 1;
Expand Down Expand Up @@ -11353,7 +11353,7 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
ret = roll_x(aes, o, oSz, A);

if (ret == 0) {
B[0] = lenSz - 1;
B[0] = (byte)(lenSz - 1U);
for (i = 0; i < lenSz; i++)
B[WC_AES_BLOCK_SIZE - 1 - i] = 0;
ret = wc_AesEncrypt(aes, B, B);
Expand Down Expand Up @@ -12175,11 +12175,11 @@ static void shiftLeftArray(byte* ary, byte shift)
else {
/* shifting over by 7 or less bits */
for (i = 0; i < WC_AES_BLOCK_SIZE - 1; i++) {
byte carry = ary[i+1] & (0XFF << (WOLFSSL_BIT_SIZE - shift));
carry >>= (WOLFSSL_BIT_SIZE - shift);
byte carry = (byte)(ary[i+1] & (0XFF << (WOLFSSL_BIT_SIZE - shift)));
carry = (byte)(carry >> (WOLFSSL_BIT_SIZE - shift));
ary[i] = (byte)((ary[i] << shift) + carry);
}
ary[i] = ary[i] << shift;
ary[i] = (byte)(ary[i] << shift);
}
}

Expand Down Expand Up @@ -12265,19 +12265,19 @@ static WARN_UNUSED_RESULT int wc_AesFeedbackCFB1(
pt = (byte*)aes->reg;

/* LSB + CAT */
tmp = (0X01 << bit) & in[0];
tmp = tmp >> bit;
tmp = (byte)((0X01U << bit) & in[0]);
dgarske marked this conversation as resolved.
Show resolved Hide resolved
tmp = (byte)(tmp >> bit);
tmp &= 0x01;
shiftLeftArray((byte*)aes->reg, 1);
pt[WC_AES_BLOCK_SIZE - 1] |= tmp;
}

/* MSB + XOR */
tmp = (0X01 << bit) & in[0];
tmp = (byte)((0X01U << bit) & in[0]);
SparkiDev marked this conversation as resolved.
Show resolved Hide resolved
pt = (byte*)aes->tmp;
tmp = (pt[0] >> 7) ^ (tmp >> bit);
tmp = (byte)((pt[0] >> 7) ^ (tmp >> bit));
SparkiDev marked this conversation as resolved.
Show resolved Hide resolved
tmp &= 0x01;
cur |= (tmp << bit);
cur = (byte)(cur | (tmp << bit));
SparkiDev marked this conversation as resolved.
Show resolved Hide resolved


if (dir == AES_ENCRYPTION) {
Expand All @@ -12294,7 +12294,7 @@ static WARN_UNUSED_RESULT int wc_AesFeedbackCFB1(
out += 1;
in += 1;
sz -= 1;
bit = 7;
bit = 7U;
cur = 0;
}
else {
Expand Down Expand Up @@ -14062,7 +14062,7 @@ static WARN_UNUSED_RESULT int S2V(
if (ret != 0)
break;
xorbuf(tmp[1-tmpi], tmp[tmpi], WC_AES_BLOCK_SIZE);
tmpi = 1 - tmpi;
tmpi = (byte)(1 - tmpi);
}

/* Add nonce as final AD. See RFC 5297 Section 3. */
Expand All @@ -14073,7 +14073,7 @@ static WARN_UNUSED_RESULT int S2V(
if (ret == 0) {
xorbuf(tmp[1-tmpi], tmp[tmpi], WC_AES_BLOCK_SIZE);
}
tmpi = 1 - tmpi;
tmpi = (byte)(1U - tmpi);
}

/* For simplicity of the remaining code, make sure the "final" result
Expand Down
21 changes: 11 additions & 10 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -1273,8 +1273,8 @@ static int GetASN_StoreData(const ASNItem* asn, ASNGetData* data,
/* Fill number with all of data. */
*data->data.u16 = 0;
for (i = 0; i < len; i++) {
*data->data.u16 <<= 8;
*data->data.u16 |= input[idx + (word32)i] ;
*data->data.u16 = (word16)(*data->data.u16 << 8U);
*data->data.u16 = (word16)(*data->data.u16 | input[idx + (word32)i]);
}
break;
case ASN_DATA_TYPE_WORD32:
Expand Down Expand Up @@ -8640,12 +8640,12 @@ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
pbeOidBuf = pbes2;
pbeOidBufSz = sizeof(pbes2);
/* kdf = OBJ pbkdf2 [ SEQ innerLen ] */
kdfLen = 2 + sizeof(pbkdf2Oid) + 2 + innerLen;
kdfLen = 2U + (word32)sizeof(pbkdf2Oid) + 2U + innerLen;
/* enc = OBJ enc_alg OCT iv */
encLen = 2 + (word32)encOidSz + 2 + (word32)blockSz;
encLen = 2U + (word32)encOidSz + 2U + (word32)blockSz;
/* pbe = OBJ pbse2 SEQ [ SEQ [ kdf ] SEQ [ enc ] ] */
pbeLen = (word32)(2 + sizeof(pbes2) + 2 + 2 + (size_t)kdfLen + 2 +
(size_t)encLen);
pbeLen = 2U + (word32)sizeof(pbes2) + 2U + 2U + kdfLen + 2U +
encLen;

ret = wc_RNG_GenerateBlock(rng, cbcIv, (word32)blockSz);
}
Expand Down Expand Up @@ -8715,7 +8715,7 @@ int wc_EncryptPKCS8Key(byte* key, word32 keySz, byte* out, word32* outSz,
idx += SetSequence(kdfLen, out + idx);
idx += (word32)SetObjectId((int)sizeof(pbkdf2Oid), out + idx);
XMEMCPY(out + idx, pbkdf2Oid, sizeof(pbkdf2Oid));
idx += sizeof(pbkdf2Oid);
idx += (word32)sizeof(pbkdf2Oid);
}
idx += SetSequence(innerLen, out + idx);
idx += SetOctetString(saltSz, out + idx);
Expand Down Expand Up @@ -24085,7 +24085,7 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm, Signer
}
}
else {
cert->maxPathLen = (byte)min(cert->ca->maxPathLen - 1,
cert->maxPathLen = (byte)min(cert->ca->maxPathLen - 1U,
cert->maxPathLen);
}
}
Expand Down Expand Up @@ -27020,7 +27020,7 @@ static int wc_SetCert_LoadDer(Cert* cert, const byte* der, word32 derSz,
#ifndef NO_ASN_TIME
static WC_INLINE byte itob(int number)
{
return (byte)number + 0x30;
return (byte)(number + 0x30);
}


Expand Down Expand Up @@ -33432,7 +33432,8 @@ int EncodePolicyOID(byte *out, word32 *outSz, const char *in, void* heap)
return BUFFER_E;
}

out[idx++] += (byte)val;
out[idx] = (byte)(out[idx] + val);
++idx;
}
else {
word32 tb = 0;
Expand Down
16 changes: 8 additions & 8 deletions wolfcrypt/src/coding.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static WC_INLINE byte Base64_Char2Val(byte c)
byte v;
byte mask;

c -= BASE64_MIN;
c = (byte)(c - BASE64_MIN);
mask = (byte)((((byte)(0x3f - c)) >> 7) - 1);
/* Load a value from the first cache line and use when mask set. */
v = (byte)(base64Decode[ c & 0x3f ] & mask);
Expand Down Expand Up @@ -507,7 +507,7 @@ int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
return BAD_FUNC_ARG;

if (inLen == 1 && *outLen && in) {
byte b = in[inIdx++] - BASE16_MIN; /* 0 starts at 0x30 */
byte b = (byte)(in[inIdx++] - BASE16_MIN); /* 0 starts at 0x30 */
SparkiDev marked this conversation as resolved.
Show resolved Hide resolved

/* sanity check */
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
Expand All @@ -531,8 +531,8 @@ int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
return BAD_FUNC_ARG;

while (inLen) {
byte b = in[inIdx++] - BASE16_MIN; /* 0 starts at 0x30 */
byte b2 = in[inIdx++] - BASE16_MIN;
byte b = (byte)(in[inIdx++] - BASE16_MIN); /* 0 starts at 0x30 */
byte b2 = (byte)(in[inIdx++] - BASE16_MIN);

/* sanity checks */
if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
Expand Down Expand Up @@ -570,14 +570,14 @@ int Base16_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
byte lb = in[i] & 0x0f;

/* ASCII value */
hb += '0';
hb = (byte)(hb + '0');
if (hb > '9')
hb += 7;
hb = (byte)(hb + 7U);

/* ASCII value */
lb += '0';
lb = (byte)(lb + '0');
if (lb>'9')
lb += 7;
lb = (byte)(lb + 7U);

out[outIdx++] = hb;
out[outIdx++] = lb;
Expand Down
Loading
Loading