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

#1064: small MPI cleanups and improvements #1363

Merged
merged 4 commits into from
Nov 4, 2019
Merged

#1064: small MPI cleanups and improvements #1363

merged 4 commits into from
Nov 4, 2019

Conversation

krizhanovsky
Copy link
Contributor

All changes are small, except bignum.[ch] where some small pieceis of logic were fully reworked.

  1. MPI small optimizations and improvements:
    1.1. introduce used to avoid zero skipping loops;
    1.2. remove copies in several places;
    1.3. reduce number and sizes of memset() calls
  2. Reworked MPI debugging facilities and debug.[ch]
  3. Remove secp192r1 as deprecated by RFC8422 5.1.1.
  4. Finally get rid of config.h
  5. Add MPI unit tests for basic operations to simplify debugging of new changes and adjust ECP and RSA tests.
  6. Massive coding style fixes.
  7. Proper reference to mbed TLS in licensing.

…ked.

1. MPI small optimizations and improvements:
1.1. introduce `used` to avoid zero skipping loops;
1.2. remove copies in several places;
1.3. reduce number and sizes of memset() calls;

2. Reworked MPI debugging facilities and debug.[ch]

3. Remove secp192r1 as deprecated by RFC8422 5.1.1.

4. Finally get rid of config.h

5. Add MPI unit tests for basic operations to simplify debugging of new
   changes and adjust ECP and RSA tests.

6. Massive coding style fixes.

7. Proper reference to mbed TLS in licensing.
tls/bignum.c Outdated Show resolved Hide resolved
Copy link
Contributor

@i-rinat i-rinat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is too large and takes too long to review, so here is some comments I have so far. (I'll continue to review code further.)

tls/ecp.c Outdated Show resolved Hide resolved
tls/ecp.h Outdated Show resolved Hide resolved
tls/ecp.h Outdated Show resolved Hide resolved
tls/ecp.h Outdated Show resolved Hide resolved
tls/oid.c Show resolved Hide resolved
tls/ttls.c Outdated Show resolved Hide resolved
tls/ttls.c Outdated Show resolved Hide resolved
tls/ttls.c Show resolved Hide resolved
tls/ttls.c Outdated Show resolved Hide resolved
tls/x509_crt.c Outdated Show resolved Hide resolved
@@ -249,8 +250,8 @@ int ttls_asn1_get_sequence_of(unsigned char **p,
/* Allocate and assign next pointer */
if (*p < end)
{
cur->next = (ttls_asn1_sequence*)ttls_calloc(1,
sizeof(ttls_asn1_sequence));
cur->next = kmalloc(sizeof(ttls_asn1_sequence),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike original implementation, this allocation doesn't zeroes the object. Looks suspicious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're good here: cur->next is assigned after the while loop, buf->len is assigned w/o reading in ttls_asn1_get_tag(), buf->tag and buf->p are also assigned in the loop.

tls/ttls.c Show resolved Hide resolved
const ttls_mpi *N,
const ttls_mpi *P, const ttls_mpi *Q,
const ttls_mpi *D, const ttls_mpi *E)
const TlsMpi *N,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure names was autoreplaced and the indentation became broken. The note applies to other functions in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RSA is to be reworked in #1335 and it has a lot of coding style issues, so I thinks it's better to postpone coding style cleanups until we do something useful with the file. For now we care about ecp* , so let's focus on these files only.

tls/bignum.c Outdated Show resolved Hide resolved
tls/bignum.c Outdated Show resolved Hide resolved
tls/bignum.h Outdated Show resolved Hide resolved
tls/ttls.c Show resolved Hide resolved
tls/ecp_curves.c Outdated Show resolved Hide resolved
tls/bignum.h Outdated Show resolved Hide resolved
@krizhanovsky krizhanovsky merged commit e677f19 into master Nov 4, 2019
@krizhanovsky krizhanovsky deleted the ak-mpi-adj branch November 4, 2019 17:54

if (_RR != NULL)
memcpy(_RR, &RR, sizeof(ttls_mpi));
if (!RR || !RR->p) {
Copy link
Contributor

@i-rinat i-rinat Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part makes no sense anymore.
Previously, one could pass NULL as _RR, and in that case this function did the needed calculations. If _RR wasn't NULL, it was used as RR. Now the function requires a pre-computed RR and won't work correctly if there is NULL. Moreover, in that case ttls_mpi_lset(RR, 1) will be called, which will dereference NULL.

I was wrong, this code is still needed. Although RR can't be NULL, it still can have RR->p == NULL, and in that case this code will calculate the number.


ttls_mpi_free(&T);
ttls_rnd(X->p, size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fill X->p by size bytes of random data. However, remaining bytes in the last limb will be kept as they were after memory allocation. So it's possible to have random number larger than expected.

/**
* Unsigned addition: X = |A| + |B|
*
* @A and @B must be different, but either of them can accept the result @X.
Copy link
Contributor

@i-rinat i-rinat Nov 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be a neat interface for a general purpose library, but that flexibility usually is not needed, as one knows whenever they do addition X = A + B where all numbers are in different memory blocks, or accumulation A += B. Flexibility makes code slower and more complex. I'd prefer to have two separate functions for the two cases.

}
if (!ttls_mpi_cmp_int(B, 1)) {
if (Q)
if (ttls_mpi_lset(Q, 1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a division by one, right? Shouldn't Q receive the value of A then?

M.n++; /* Make room for multiplication by 19 */
n = M.used * CIL;
memcpy(Mp, N->p + P255_WIDTH - 1, n);
memset(Mp + n, 0, sizeof(Mp) - n);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n is number of bytes, but Mp is coerced to unsigned long *, so we may write beyond the Mp here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants