Skip to content

Commit

Permalink
Use C99 for size_t loops.
Browse files Browse the repository at this point in the history
This was done just by grepping for 'size_t i;' and 'size_t j;'. I left
everything in crypto/x509 and friends alone.

There's some instances in gcm.c that are non-trivial and pulled into a
separate CL for ease of review.

Change-Id: I6515804e3097f7e90855f1e7610868ee87117223
Reviewed-on: https://boringssl-review.googlesource.com/10801
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
  • Loading branch information
davidben authored and CQ bot account: commit-bot@chromium.org committed Sep 12, 2016
1 parent c763a40 commit 5409123
Show file tree
Hide file tree
Showing 37 changed files with 136 additions and 235 deletions.
3 changes: 1 addition & 2 deletions crypto/base64/base64_test.cc
Expand Up @@ -107,8 +107,7 @@ static std::string RemoveNewlines(const char *in) {
std::string ret;
const size_t in_len = strlen(in);

size_t i;
for (i = 0; i < in_len; i++) {
for (size_t i = 0; i < in_len; i++) {
if (in[i] != '\n') {
ret.push_back(in[i]);
}
Expand Down
3 changes: 1 addition & 2 deletions crypto/bio/hexdump.c
Expand Up @@ -86,7 +86,6 @@ static char to_char(uint8_t b) {
* |ctx|. */
static int hexdump_write(struct hexdump_ctx *ctx, const uint8_t *data,
size_t len) {
size_t i;
char buf[10];
unsigned l;

Expand All @@ -95,7 +94,7 @@ static int hexdump_write(struct hexdump_ctx *ctx, const uint8_t *data,
* ^ offset ^ extra space ^ ASCII of line
*/

for (i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) {
if (ctx->used == 0) {
/* The beginning of a line. */
BIO_indent(ctx->bio, ctx->indent, UINT_MAX);
Expand Down
9 changes: 3 additions & 6 deletions crypto/bn/convert.c
Expand Up @@ -160,9 +160,6 @@ static BN_ULONG read_word_padded(const BIGNUM *in, size_t i) {
}

int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
size_t i;
BN_ULONG l;

/* Special case for |in| = 0. Just branch as the probability is negligible. */
if (BN_is_zero(in)) {
memset(out, 0, len);
Expand All @@ -175,7 +172,7 @@ int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
return 0;
}
if ((len % BN_BYTES) != 0) {
l = read_word_padded(in, len / BN_BYTES);
BN_ULONG l = read_word_padded(in, len / BN_BYTES);
if (l >> (8 * (len % BN_BYTES)) != 0) {
return 0;
}
Expand All @@ -188,9 +185,9 @@ int BN_bn2bin_padded(uint8_t *out, size_t len, const BIGNUM *in) {
* leading zero octets is low.
*
* See Falko Stenzke, "Manger's Attack revisited", ICICS 2010. */
i = len;
size_t i = len;
while (i--) {
l = read_word_padded(in, i / BN_BYTES);
BN_ULONG l = read_word_padded(in, i / BN_BYTES);
*(out++) = (uint8_t)(l >> (8 * (i % BN_BYTES))) & 0xff;
}
return 1;
Expand Down
10 changes: 4 additions & 6 deletions crypto/bytestring/cbb.c
Expand Up @@ -142,17 +142,16 @@ static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,

static int cbb_buffer_add_u(struct cbb_buffer_st *base, uint32_t v,
size_t len_len) {
uint8_t *buf;
size_t i;

if (len_len == 0) {
return 1;
}

uint8_t *buf;
if (!cbb_buffer_add(base, &buf, len_len)) {
return 0;
}

for (i = len_len - 1; i < len_len; i--) {
for (size_t i = len_len - 1; i < len_len; i--) {
buf[i] = v;
v >>= 8;
}
Expand Down Expand Up @@ -440,14 +439,13 @@ void CBB_discard_child(CBB *cbb) {

int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) {
CBB child;
size_t i;
int started = 0;

if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER)) {
return 0;
}

for (i = 0; i < 8; i++) {
for (size_t i = 0; i < 8; i++) {
uint8_t byte = (value >> 8*(7-i)) & 0xff;
if (!started) {
if (byte == 0) {
Expand Down
3 changes: 1 addition & 2 deletions crypto/bytestring/cbs.c
Expand Up @@ -88,13 +88,12 @@ int CBS_mem_equal(const CBS *cbs, const uint8_t *data, size_t len) {

static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
uint32_t result = 0;
size_t i;
const uint8_t *data;

if (!cbs_get(cbs, &data, len)) {
return 0;
}
for (i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) {
result <<= 8;
result |= data[i];
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/cipher/e_aes.c
Expand Up @@ -353,14 +353,14 @@ static int aes_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
static int aes_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
size_t len) {
size_t bl = ctx->cipher->block_size;
size_t i;
EVP_AES_KEY *dat = (EVP_AES_KEY *)ctx->cipher_data;

if (len < bl) {
return 1;
}

for (i = 0, len -= bl; i <= len; i += bl) {
len -= bl;
for (size_t i = 0; i <= len; i += bl) {
(*dat->block)(in + i, out + i, &dat->ks);
}

Expand Down
6 changes: 2 additions & 4 deletions crypto/cipher/e_des.c
Expand Up @@ -104,8 +104,7 @@ static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
in_len -= ctx->cipher->block_size;

EVP_DES_KEY *dat = (EVP_DES_KEY *) ctx->cipher_data;
size_t i;
for (i = 0; i <= in_len; i += ctx->cipher->block_size) {
for (size_t i = 0; i <= in_len; i += ctx->cipher->block_size) {
DES_ecb_encrypt((DES_cblock *) (in + i), (DES_cblock *) (out + i),
&dat->ks.ks, ctx->encrypt);
}
Expand Down Expand Up @@ -189,8 +188,7 @@ static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
in_len -= ctx->cipher->block_size;

DES_EDE_KEY *dat = (DES_EDE_KEY *) ctx->cipher_data;
size_t i;
for (i = 0; i <= in_len; i += ctx->cipher->block_size) {
for (size_t i = 0; i <= in_len; i += ctx->cipher->block_size) {
DES_ecb3_encrypt((DES_cblock *) (in + i), (DES_cblock *) (out + i),
&dat->ks.ks[0], &dat->ks.ks[1], &dat->ks.ks[2],
ctx->encrypt);
Expand Down
3 changes: 1 addition & 2 deletions crypto/digest/digest_test.cc
Expand Up @@ -142,10 +142,9 @@ static bool CompareDigest(const TestVector *test,
const uint8_t *digest,
size_t digest_len) {
static const char kHexTable[] = "0123456789abcdef";
size_t i;
char digest_hex[2*EVP_MAX_MD_SIZE + 1];

for (i = 0; i < digest_len; i++) {
for (size_t i = 0; i < digest_len; i++) {
digest_hex[2*i] = kHexTable[digest[i] >> 4];
digest_hex[2*i + 1] = kHexTable[digest[i] & 0xf];
}
Expand Down
4 changes: 1 addition & 3 deletions crypto/ec/ec.c
Expand Up @@ -709,9 +709,7 @@ int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) {

int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[],
BN_CTX *ctx) {
size_t i;

for (i = 0; i < num; i++) {
for (size_t i = 0; i < num; i++) {
if (group->meth != points[i]->meth) {
OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
return 0;
Expand Down
36 changes: 14 additions & 22 deletions crypto/ec/p224-64.c
Expand Up @@ -192,8 +192,7 @@ static void bin28_to_felem(felem out, const u8 in[28]) {
}

static void felem_to_bin28(u8 out[28], const felem in) {
size_t i;
for (i = 0; i < 7; ++i) {
for (size_t i = 0; i < 7; ++i) {
out[i] = in[0] >> (8 * i);
out[i + 7] = in[1] >> (8 * i);
out[i + 14] = in[2] >> (8 * i);
Expand All @@ -203,8 +202,7 @@ static void felem_to_bin28(u8 out[28], const felem in) {

/* To preserve endianness when using BN_bn2bin and BN_bin2bn */
static void flip_endian(u8 *out, const u8 *in, size_t len) {
size_t i;
for (i = 0; i < len; ++i) {
for (size_t i = 0; i < len; ++i) {
out[i] = in[len - 1 - i];
}
}
Expand Down Expand Up @@ -524,7 +522,6 @@ static limb felem_is_zero(const felem in) {
static void felem_inv(felem out, const felem in) {
felem ftmp, ftmp2, ftmp3, ftmp4;
widefelem tmp;
size_t i;

felem_square(tmp, in);
felem_reduce(ftmp, tmp); /* 2 */
Expand All @@ -544,45 +541,45 @@ static void felem_inv(felem out, const felem in) {
felem_reduce(ftmp, tmp); /* 2^6 - 1 */
felem_square(tmp, ftmp);
felem_reduce(ftmp2, tmp); /* 2^7 - 2 */
for (i = 0; i < 5; ++i) { /* 2^12 - 2^6 */
for (size_t i = 0; i < 5; ++i) { /* 2^12 - 2^6 */
felem_square(tmp, ftmp2);
felem_reduce(ftmp2, tmp);
}
felem_mul(tmp, ftmp2, ftmp);
felem_reduce(ftmp2, tmp); /* 2^12 - 1 */
felem_square(tmp, ftmp2);
felem_reduce(ftmp3, tmp); /* 2^13 - 2 */
for (i = 0; i < 11; ++i) {/* 2^24 - 2^12 */
for (size_t i = 0; i < 11; ++i) {/* 2^24 - 2^12 */
felem_square(tmp, ftmp3);
felem_reduce(ftmp3, tmp);
}
felem_mul(tmp, ftmp3, ftmp2);
felem_reduce(ftmp2, tmp); /* 2^24 - 1 */
felem_square(tmp, ftmp2);
felem_reduce(ftmp3, tmp); /* 2^25 - 2 */
for (i = 0; i < 23; ++i) {/* 2^48 - 2^24 */
for (size_t i = 0; i < 23; ++i) {/* 2^48 - 2^24 */
felem_square(tmp, ftmp3);
felem_reduce(ftmp3, tmp);
}
felem_mul(tmp, ftmp3, ftmp2);
felem_reduce(ftmp3, tmp); /* 2^48 - 1 */
felem_square(tmp, ftmp3);
felem_reduce(ftmp4, tmp); /* 2^49 - 2 */
for (i = 0; i < 47; ++i) {/* 2^96 - 2^48 */
for (size_t i = 0; i < 47; ++i) {/* 2^96 - 2^48 */
felem_square(tmp, ftmp4);
felem_reduce(ftmp4, tmp);
}
felem_mul(tmp, ftmp3, ftmp4);
felem_reduce(ftmp3, tmp); /* 2^96 - 1 */
felem_square(tmp, ftmp3);
felem_reduce(ftmp4, tmp); /* 2^97 - 2 */
for (i = 0; i < 23; ++i) {/* 2^120 - 2^24 */
for (size_t i = 0; i < 23; ++i) {/* 2^120 - 2^24 */
felem_square(tmp, ftmp4);
felem_reduce(ftmp4, tmp);
}
felem_mul(tmp, ftmp2, ftmp4);
felem_reduce(ftmp2, tmp); /* 2^120 - 1 */
for (i = 0; i < 6; ++i) { /* 2^126 - 2^6 */
for (size_t i = 0; i < 6; ++i) { /* 2^126 - 2^6 */
felem_square(tmp, ftmp2);
felem_reduce(ftmp2, tmp);
}
Expand All @@ -592,7 +589,7 @@ static void felem_inv(felem out, const felem in) {
felem_reduce(ftmp, tmp); /* 2^127 - 2 */
felem_mul(tmp, ftmp, in);
felem_reduce(ftmp, tmp); /* 2^127 - 1 */
for (i = 0; i < 97; ++i) {/* 2^224 - 2^97 */
for (size_t i = 0; i < 97; ++i) {/* 2^224 - 2^97 */
felem_square(tmp, ftmp);
felem_reduce(ftmp, tmp);
}
Expand All @@ -604,10 +601,9 @@ static void felem_inv(felem out, const felem in) {
* if icopy == 1, copy in to out,
* if icopy == 0, copy out to itself. */
static void copy_conditional(felem out, const felem in, limb icopy) {
size_t i;
/* icopy is a (64-bit) 0 or 1, so copy is either all-zero or all-one */
const limb copy = -icopy;
for (i = 0; i < 4; ++i) {
for (size_t i = 0; i < 4; ++i) {
const limb tmp = copy & (in[i] ^ out[i]);
out[i] ^= tmp;
}
Expand Down Expand Up @@ -866,17 +862,15 @@ static void select_point(const u64 idx, size_t size,
limb *outlimbs = &out[0][0];
memset(outlimbs, 0, 3 * sizeof(felem));

size_t i;
for (i = 0; i < size; i++) {
for (size_t i = 0; i < size; i++) {
const limb *inlimbs = &pre_comp[i][0][0];
u64 mask = i ^ idx;
mask |= mask >> 4;
mask |= mask >> 2;
mask |= mask >> 1;
mask &= 1;
mask--;
size_t j;
for (j = 0; j < 4 * 3; j++) {
for (size_t j = 0; j < 4 * 3; j++) {
outlimbs[j] |= inlimbs[j] & mask;
}
}
Expand Down Expand Up @@ -1082,8 +1076,7 @@ static int ec_GFp_nistp224_points_mul(const EC_GROUP *group,
* i.e., they contribute nothing to the linear combination */
memset(secrets, 0, num_points * sizeof(felem_bytearray));
memset(pre_comp, 0, num_points * 17 * 3 * sizeof(felem));
size_t i;
for (i = 0; i < num_points; ++i) {
for (size_t i = 0; i < num_points; ++i) {
if (i == num) {
/* the generator */
p = EC_GROUP_get0_generator(group);
Expand Down Expand Up @@ -1121,8 +1114,7 @@ static int ec_GFp_nistp224_points_mul(const EC_GROUP *group,
felem_assign(pre_comp[i][1][1], y_out);
felem_assign(pre_comp[i][1][2], z_out);

size_t j;
for (j = 2; j <= 16; ++j) {
for (size_t j = 2; j <= 16; ++j) {
if (j & 1) {
point_add(pre_comp[i][j][0], pre_comp[i][j][1], pre_comp[i][j][2],
pre_comp[i][1][0], pre_comp[i][1][1], pre_comp[i][1][2],
Expand Down

0 comments on commit 5409123

Please sign in to comment.