Skip to content

Commit

Permalink
Merge branch 'main' into ne-justsmth-bignum-big-endian
Browse files Browse the repository at this point in the history
  • Loading branch information
nebeid authored Oct 13, 2023
2 parents 78fcba9 + 496574c commit bb3b87b
Show file tree
Hide file tree
Showing 83 changed files with 1,603 additions and 393 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/abidiff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: AWS-LC ABI Diff
on:
pull_request:
branches: [ '*' ]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
DOCKER_BUILDKIT: 1
GOPROXY: https://proxy.golang.org,direct
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/aws-lc-rs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
branches: [ '*' ]
pull_request:
branches: [ '*' ]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
GOPROXY: https://proxy.golang.org,direct
jobs:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/integrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
branches: [ '*' ]
pull_request:
branches: [ '*' ]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
CC: gcc
jobs:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/osx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
branches:
- '*'

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true

env:
PACKAGE_NAME: aws-lc
# Used to enable ASAN test dimension.
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ cmake-build-debug/
symbols.txt

.DS_Store
.idea
.idea/
.fleet/
.cache/
27 changes: 27 additions & 0 deletions crypto/bio/bio_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -696,4 +696,31 @@ TEST_P(BIOPairTest, TestCallbacks) {
ASSERT_EQ(param_len_ex[0], 0u);
}

namespace {
static int callback_invoked = 0;

static long callback(BIO *b, int state, int res) {
callback_invoked = 1;
EXPECT_EQ(state, 0);
EXPECT_EQ(res, -1);
return 0;
}

TEST(BIOTest, InvokeConnectCallback) {

ASSERT_EQ(callback_invoked, 0);
BIO *bio = BIO_new(BIO_s_connect());
ASSERT_NE(bio, nullptr);

ASSERT_TRUE(BIO_set_conn_hostname(bio, "localhost"));
ASSERT_TRUE(BIO_set_conn_port(bio, "8080"));
ASSERT_TRUE(BIO_callback_ctrl(bio, BIO_CTRL_SET_CALLBACK, callback));

ASSERT_EQ(BIO_do_connect(bio), 0);
ASSERT_EQ(callback_invoked, 1);

ASSERT_TRUE(BIO_free(bio));
}
}

INSTANTIATE_TEST_SUITE_P(All, BIOPairTest, testing::Values(false, true));
14 changes: 4 additions & 10 deletions crypto/bio/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ typedef struct bio_connect_st {
// info_callback is called when the connection is initially made
// callback(BIO,state,ret); The callback should return 'ret', state is for
// compatibility with the SSL info_callback.
int (*info_callback)(const BIO *bio, int state, int ret);
bio_info_cb info_callback;
} BIO_CONNECT;

#if !defined(OPENSSL_WINDOWS)
Expand Down Expand Up @@ -168,7 +168,7 @@ static int split_host_and_port(char **out_host, char **out_port,

static int conn_state(BIO *bio, BIO_CONNECT *c) {
int ret = -1, i;
int (*cb)(const BIO *, int, int) = NULL;
bio_info_cb cb = NULL;

if (c->info_callback != NULL) {
cb = c->info_callback;
Expand Down Expand Up @@ -467,7 +467,7 @@ static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
case BIO_CTRL_FLUSH:
break;
case BIO_CTRL_GET_CALLBACK: {
int (**fptr)(const BIO *bio, int state, int xret) = ptr;
bio_info_cb *fptr = ptr;
*fptr = data->info_callback;
} break;
default:
Expand All @@ -485,13 +485,7 @@ static long conn_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {

switch (cmd) {
case BIO_CTRL_SET_CALLBACK:
// This is the actual type signature of |fp|. The caller is expected to
// cast it to |bio_info_cb| due to the |BIO_callback_ctrl| calling
// convention.
OPENSSL_MSVC_PRAGMA(warning(push))
OPENSSL_MSVC_PRAGMA(warning(disable : 4191))
data->info_callback = (int (*)(const struct bio_st *, int, int))fp;
OPENSSL_MSVC_PRAGMA(warning(pop))
data->info_callback = fp;
break;
default:
ret = 0;
Expand Down
12 changes: 11 additions & 1 deletion crypto/blake2/blake2.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ static uint64_t blake2b_load(const uint8_t block[BLAKE2B_CBLOCK], size_t i) {
return CRYPTO_load_u64_le(block + 8 * i);
}

static void copy_digest_words_to_dest(uint8_t* dest, uint64_t* src, size_t word_count) {
#ifdef OPENSSL_BIG_ENDIAN
for(size_t i = 0; i < word_count; i++) {
CRYPTO_store_u64_le(&dest[i * sizeof(uint64_t)], src[i]);
}
#else
OPENSSL_memcpy(dest, src, word_count * sizeof(uint64_t));
#endif
}

static void blake2b_transform(BLAKE2B_CTX *b2b,
const uint8_t block[BLAKE2B_CBLOCK],
size_t num_bytes, int is_final_block) {
Expand Down Expand Up @@ -158,7 +168,7 @@ void BLAKE2B256_Final(uint8_t out[BLAKE2B256_DIGEST_LENGTH], BLAKE2B_CTX *b2b) {
blake2b_transform(b2b, b2b->block, b2b->block_used,
/*is_final_block=*/1);
OPENSSL_STATIC_ASSERT(BLAKE2B256_DIGEST_LENGTH <= sizeof(b2b->h), _)
memcpy(out, b2b->h, BLAKE2B256_DIGEST_LENGTH);
copy_digest_words_to_dest(out, b2b->h, BLAKE2B256_DIGEST_LENGTH / 8);
}

void BLAKE2B256(const uint8_t *data, size_t len,
Expand Down
20 changes: 19 additions & 1 deletion crypto/chacha/chacha.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,25 @@ void CRYPTO_chacha_20(uint8_t *out, const uint8_t *in, size_t in_len,
}
#endif

ChaCha20_ctr32(out, in, in_len, key_ptr, counter_nonce);
while (in_len > 0) {
// The assembly functions do not have defined overflow behavior. While
// overflow is almost always a bug in the caller, we prefer our functions to
// behave the same across platforms, so divide into multiple calls to avoid
// this case.
uint64_t todo = 64 * ((UINT64_C(1) << 32) - counter_nonce[0]);
if (todo > in_len) {
todo = in_len;
}

ChaCha20_ctr32(out, in, (size_t)todo, key_ptr, counter_nonce);
in += todo;
out += todo;
in_len -= todo;

// We're either done and will next break out of the loop, or we stopped at
// the wraparound point and the counter should continue at zero.
counter_nonce[0] = 0;
}
}

#else
Expand Down
110 changes: 110 additions & 0 deletions crypto/chacha/chacha_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,102 @@ static const uint8_t kOutput[] = {
0x8f, 0x40, 0xcf, 0x4a,
};

static uint32_t kOverflowCounter = 0xffffffff;

static const uint8_t kOverflowOutput[] = {
0x37, 0x64, 0x38, 0xcb, 0x25, 0x69, 0x2c, 0xf5, 0x88, 0x8a, 0xfe, 0x6d,
0x3b, 0x10, 0x07, 0x3c, 0x77, 0xac, 0xcd, 0x1c, 0x0c, 0xa7, 0x17, 0x31,
0x1d, 0xc3, 0x81, 0xd1, 0xa5, 0x20, 0x55, 0xea, 0xd3, 0x00, 0xc9, 0x84,
0xde, 0xe2, 0xe5, 0x5e, 0x7b, 0x28, 0x28, 0x59, 0x73, 0x3a, 0x8e, 0x57,
0x62, 0x18, 0x50, 0x55, 0x97, 0xca, 0x50, 0x3e, 0x8a, 0x84, 0x61, 0x28,
0x4c, 0x22, 0x93, 0x50, 0x48, 0x7e, 0x65, 0x78, 0x06, 0x5a, 0xcd, 0x2b,
0x11, 0xf7, 0x10, 0xfd, 0x6f, 0x41, 0x92, 0x82, 0x7c, 0x3a, 0x71, 0x07,
0x67, 0xd0, 0x7e, 0xb7, 0xdf, 0xdc, 0xfc, 0xee, 0xe6, 0x55, 0xdd, 0x6f,
0x79, 0x23, 0xf3, 0xae, 0x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 0xb3, 0x49,
0xeb, 0xc2, 0x49, 0xdb,
};


static_assert(sizeof(kInput) == sizeof(kOutput),
"Input and output lengths don't match.");
static_assert(sizeof(kInput) == sizeof(kOverflowOutput),
"Input and output lengths don't match.");

TEST(ChaChaTest, TestVector) {
// Run the test with the test vector at all lengths.
Expand All @@ -237,6 +331,22 @@ TEST(ChaChaTest, TestVector) {
}
}

TEST(ChaChaTest, CounterOverflow) {
// Run the test with the test vector at all lengths.
for (size_t len = 0; len <= sizeof(kInput); len++) {
SCOPED_TRACE(len);

std::unique_ptr<uint8_t[]> buf(new uint8_t[len]);
CRYPTO_chacha_20(buf.get(), kInput, len, kKey, kNonce, kOverflowCounter);
EXPECT_EQ(Bytes(kOverflowOutput, len), Bytes(buf.get(), len));

// Test the in-place version.
OPENSSL_memcpy(buf.get(), kInput, len);
CRYPTO_chacha_20(buf.get(), buf.get(), len, kKey, kNonce, kOverflowCounter);
EXPECT_EQ(Bytes(kOverflowOutput, len), Bytes(buf.get(), len));
}
}

#if defined(CHACHA20_ASM) && defined(SUPPORTS_ABI_TEST)
TEST(ChaChaTest, ABI) {
uint32_t key[8];
Expand Down
9 changes: 8 additions & 1 deletion crypto/chacha/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ void CRYPTO_hchacha20(uint8_t out[32], const uint8_t key[32],
defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
#define CHACHA20_ASM

// ChaCha20_ctr32 is defined in asm/chacha-*.pl.
// ChaCha20_ctr32 encrypts |in_len| bytes from |in| and writes the result to
// |out|. If |in| and |out| alias, they must be equal.
//
// |counter[0]| is the initial 32-bit block counter, and the remainder is the
// 96-bit nonce. If the counter overflows, the output is undefined. The function
// will produce output, but the output may vary by machine and may not be
// self-consistent. (On some architectures, the assembly implements a mix of
// 64-bit and 32-bit counters.)
void ChaCha20_ctr32(uint8_t *out, const uint8_t *in, size_t in_len,
const uint32_t key[8], const uint32_t counter[4]);
#endif
Expand Down
4 changes: 2 additions & 2 deletions crypto/curve25519/curve25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,6 @@ int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
}

// The all-zero output results when the input is a point of small order.
// See https://www.rfc-editor.org/rfc/rfc7748#section-6.1.
return CRYPTO_memcmp(kZeros, out_shared_key, 32) != 0;
return constant_time_declassify_int(
CRYPTO_memcmp(kZeros, out_shared_key, 32)) != 0;
}
3 changes: 2 additions & 1 deletion crypto/curve25519/curve25519_nohw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ void x25519_scalar_mult_generic_nohw(uint8_t out[32],
}

void x25519_public_from_private_nohw(uint8_t out_public_value[32],
const uint8_t private_key[32]) {
const uint8_t private_key[32]) {

uint8_t e[32];
OPENSSL_memcpy(e, private_key, 32);
Expand All @@ -1966,4 +1966,5 @@ void x25519_public_from_private_nohw(uint8_t out_public_value[32],
fe_loose_invert(&zminusy_inv, &zminusy);
fe_mul_tlt(&zminusy_inv, &zplusy, &zminusy_inv);
fe_tobytes(out_public_value, &zminusy_inv);
CONSTTIME_DECLASSIFY(out_public_value, 32);
}
9 changes: 9 additions & 0 deletions crypto/curve25519/ed25519_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ TEST(Ed25519Test, TestVectors) {
ASSERT_TRUE(t->GetBytes(&expected_signature, "SIG"));
ASSERT_EQ(64u, expected_signature.size());

// Signing should not leak the private key or the message.
CONSTTIME_SECRET(private_key.data(), private_key.size());
CONSTTIME_SECRET(message.data(), message.size());
uint8_t signature[64];
ASSERT_TRUE(ED25519_sign(signature, message.data(), message.size(),
private_key.data()));
CONSTTIME_DECLASSIFY(signature, sizeof(signature));
CONSTTIME_DECLASSIFY(message.data(), message.size());

EXPECT_EQ(Bytes(expected_signature), Bytes(signature));
EXPECT_TRUE(ED25519_verify(message.data(), message.size(), signature,
public_key.data()));
Expand Down Expand Up @@ -114,9 +120,12 @@ TEST(Ed25519Test, KeypairFromSeed) {

uint8_t seed[32];
OPENSSL_memcpy(seed, private_key1, sizeof(seed));
CONSTTIME_SECRET(seed, sizeof(seed));

uint8_t public_key2[32], private_key2[64];
ED25519_keypair_from_seed(public_key2, private_key2, seed);
CONSTTIME_DECLASSIFY(public_key2, sizeof(public_key2));
CONSTTIME_DECLASSIFY(private_key2, sizeof(private_key2));

EXPECT_EQ(Bytes(public_key1), Bytes(public_key2));
EXPECT_EQ(Bytes(private_key1), Bytes(private_key2));
Expand Down
Loading

0 comments on commit bb3b87b

Please sign in to comment.