Commit Graph

90 Commits

Author SHA1 Message Date
Mike Frysinger b5775df441 prng: support getrandom & getentropy
If the C library supports these random functions, use them directly
instead of reading the /dev/urandom file.  This makes life easier on
platforms that don't have /dev/urandom (like WASM).
2026-02-27 19:55:52 -05:00
Benjamin Barenblat 3acaa1c4d3 clang-format Mosh
Run clang-format over the Mosh source tree. This is a large change and
has been factored into its own commit for auditability. Reproduce it
with

    find . -name \*.cc -or -name \*.h | while read f; do clang-format -i --style=file $f; done
2023-08-07 22:03:00 -04:00
Benjamin Barenblat 0b15dc94fa Add a clang-format file and prepare for clang-formatting
Create .clang-format to describe the current C++ style used in Mosh.

Mark one carefully-formatted array with `// clang-format off`. Also turn
off clang-format in src/crypto/ocb_internal.cc, since it was imported
almost wholesale from another project and is written in a style
different from the rest of Mosh.
2023-08-07 22:03:00 -04:00
Alex Chernyakhovsky cf542739cc Switch to C++ versions of standard C headers 2023-07-30 19:02:51 -04:00
Alex Chernyakhovsky 19ad493dcb Remove using-declarations for std:: types 2023-07-30 19:02:51 -04:00
Alex Chernyakhovsky 325098ac07 Switch to fully-qualified #include
Previously, mosh used extensive -I flags and all of the mosh-local
makes it really hard to tell what the proper dependency graph is, so
instead remove the -I arguments in favvor of $(top_srcdir) and qualify
the paths wherever they are used.
2023-07-30 17:03:40 -04:00
Benjamin Barenblat 1416e9a3c1 OCB: Use OpenSSL EVP instead of deprecated AES
Replace calls to AES_* APIs, which were deprecated in OpenSSL 3, with
calls to EVP_* APIs.

Closes: https://github.com/mobile-shell/mosh/issues/1174
2022-06-27 15:10:49 -10:00
Alex Chernyakhovsky 5ad20dbc50 Stop using deprecated Nettle functions
Previously, ocb_internal.cc supported different key sizes, by way of
the deprecated aes_* function family. However, in practice, mosh
always uses AES-128. In Nettle, the explicit key-size APIs are not
deprecated, so switch to AES-128 directly.

Fixes: 1202
2022-06-27 14:34:26 -10:00
Benjamin Barenblat db49808ac3 OCB: Heap-allocate keys
The OpenSSL EVP API requires that keys be heap-allocated, so switch
_ae_ctx to use pointers to keys and opaque allocation functions.

Bug: https://github.com/mobile-shell/mosh/issues/1174
2022-06-27 14:11:09 -10:00
Benjamin Barenblat ad85b90505 OCB: Make primitive AES API explicit
Explicitly define the primitive AES API used by the internal OCB
implementation, and move it into its own namespace (ocb_aes). This will
ease future implementation changes.

Also make some style fixes to affected lines: Replace C-style casts
with C++-style casts, add some missing spaces in argument lists, and
remove some `inline` that the compiler will ignore.

Bug: https://github.com/mobile-shell/mosh/issues/1174
2022-06-27 13:56:07 -10:00
Benjamin Barenblat 0a30c5acd5 Delete unused ROUNDS macro
This macro was used in the reference and AES-NI AES implementations,
both of which were deleted in a563093f16.
2022-06-27 13:56:07 -10:00
Alex Chernyakhovsky e5e62b4c76 Add nettle to the CI matrix 2022-06-27 13:46:18 -10:00
Benjamin Barenblat bacc024083 Go back to internal OCB implementation
After further discussion, the Mosh maintainers have decided to stick
with the internal OCB implementation for this release. Restore support
for using OpenSSL’s AES but internal OCB. To make this commit easy to
audit, restore the code exactly, including calls to AES functions that
are deprecated in OpenSSL 3; a future commit will update ocb_internal.cc
to use EVP instead of directly calling the AES primitives.

In anticipation of future changes, preserve support for OpenSSL’s
AES-OCB, but don’t compile it in. Add
--with-crypto-library=openssl-with-openssl-ocb and
--with-crypto-library=openssl-with-internal-ocb options to configure so
that developers can easily test Mosh using OpenSSL’s AES-OCB. These
options are intended only for testing, are undocumented, and are not
subject to any API stability guarantees.

Rework configure to look for all possible cryptography libraries first
and then dispatch on --with-crypto-library as appropriate.
2022-06-22 15:02:19 -10:00
Alex Chernyakhovsky 135a11a2bb Use OpenSSL native OCB-AES implementation
OpenSSL 3.0 deprecated many of the functions that ocb.cc used to
implement OCB-AES, causing a build failure when -Wdeprecated collided
with -Werror. Debian temporarily fixed this by suppressing the error
in #1191.

Since mosh 1.4 will be the next stable release of mosh, it should not
depend on deprecated functions in OpenSSL. Since version 1.1.0,
OpenSSL natively supports OCB-AES through the EVP_CIPHER API. @cgull
started early support for this in #924.

This change extends upon the previous work by @cgull in a few ways

 * EVP_CipherInit_ex is called in ae_init to set up the
   EVP_CIPHER_CTX. It is later called in ae_encrypt and ae_decrypt
   just to load nonce (IV in OpenSSL EVP parlance), which reduces the
   amount of initialization done per-packet. However, due to OpenSSL
   API limitations, two copies of the EVP_CIPHER_CTX are kept: one for
   encryption, and one for decryption.

 * Adds missing support for an external tag, rather than just one
   appended to the ciphertext

 * Support for non-default-sized tags

as well as some improved error handling.

Note that this change raises the minimum OpenSSL version for Mosh to
1.1.0. OpenSSL does not provide security support for versions prior to
1.1 at this time, so this is in principle reasonable dependency. If we
want to continue to support distributions (such as RHEL7) which
continue to be supported by their vendor but use an unsupported
OpenSSL, then some future work will have to restore the ocb.cc
implementation that uses the deprecated functions.

Bugs: #1174
2022-06-13 18:37:03 -10:00
Benjamin Barenblat a563093f16 Separate OpenSSL-based OCB implementation from others
Split src/crypto/ocb.cc into two files – one containing the AES-OCB
implementation backed by OpenSSL, and the other containing
implementations backed by Apple Common Crypto and Nettle. This paves the
way for a new OpenSSL implementation that uses OpenSSL 1.1’s OCB support
directly, rather than one that merely uses OpenSSL to provide the
underlying block cipher.

Remove support for rijndael-alg-fst.c and compiler-provided AES
intrinsics, since they’re not in use anymore. (Mosh can still use
hardware-accelerated AES if it’s available; it just now relies
exclusively on the underlying cryptography library to accelerate AES if
possible.)

Update the build system to conditionally compile in either
ocb_openssl.cc or ocb_internal.cc, depending on which cryptography
library you pass to ./configure.

To make this commit easy to audit, ocb_openssl.cc and ocb_internal.cc
are trivially diffable against ocb.cc (now deleted). Expected diffs
consist of a copyright notice update, a preprocessor check to ensure the
appropriate cryptography implementation has been selected, and deletions
to remove code that’s no longer in use. This does mean a substantial
amount of code is duplicated between ocb_openssl.cc and ocb_internal.cc;
however, ocb_openssl.cc should be completely replaced soon, so it won’t
be an issue in the long term.

Bug: https://github.com/mobile-shell/mosh/issues/1174
2022-06-06 16:30:41 -10:00
Alex Chernyakhovsky 2f90addb7c Revert "Remove redundant malloc/free"
This reverts commit 6321b1d9c5.

The original commit 6321b1d9c5 switched
from a malloc call of a 22400 byte buffer to a stack-allocated 22400
byte buffer, in addition to the fairly large buffers already allocated
in the functions. Some systems have fairly small stack frames, making
this 22K allocation potentially dangerous. On my stock Debian bullseye
system, I have 200809 bytes (from `getconf
_POSIX_THREAD_ATTR_STACKSIZE`); a 22400 byte buffer already represents
about 10% of the available stacksize.

Other systems, such as those with musl libc, may have either 80KiB or
128KiB [1], making this allocation represent between 18% to 28% of the
available stack space.

[1] https://wiki.musl-libc.org/functional-differences-from-glibc.html#Thread-stack-size
2022-05-30 20:55:21 -04:00
Alex Chernyakhovsky 70a02d1e83 Add support for generating coverage reports
This change adds autoconf/automake support for building all of mosh
with gcov, and generates an lcov html report. This allows seeing which
parts ofthe source tree have good test coverage, and which can be
shored up. Eventually, it would be good to hook this up to Github
Actions to be generated automatically.
2022-05-30 19:38:10 -04:00
John Hood fecd4ee29b Some more namespace hygiene for "using decl;". 2018-08-15 19:28:38 -04:00
John Hood 756f4f8e98 Remove "using namespace std;". 2018-08-15 19:27:23 -04:00
John Hood 6321b1d9c5 Remove redundant malloc/free 2017-08-02 22:07:02 -04:00
John Hood a993c83632 Handle GCC 7's new -Wimplicit-fallthrough. 2017-01-31 22:12:27 -05:00
Carlos Cabanero 0ceb4f26f4 Memory Alignment issues on ARM processors
Unaligned data on ARM architectures do not perform efficiently unaligned
memory access, and in the case of ARMv7 and iOS it completely breaks. The OCB
algorithm dereferences a uint64x2_t pointer, and is replaced by a
memcpy to avoid penalties when trying to align it.

More info https://brewx.qualcomm.com/bws/content/gi/common/appseng/en/knowledgebase/docs/kb95.html
2017-01-05 00:14:36 -05:00
John Hood 9005ea6cf3 Use a table lookup for base64 decode 2016-10-29 20:50:33 -04:00
John Hood f429fd38b0 Use old AppleCommonCrypto name for AES 2016-09-25 22:24:03 -04:00
John Hood 25a65fc071 Coverity fixes: tainted/unbounded array index 2016-05-10 23:54:41 -04:00
john hood 9073983df3 crypto.cc: prefer bswap64() and ffs() if available.
Fixes #745.
2016-05-08 20:45:01 -04:00
John Hood 2ba5d2bbb2 Handle zero-length allocations in AlignedBuffer. 2016-03-31 10:41:00 -04:00
John Hood 7af87d85cc Remove redundant stringification for decrypt(). 2016-03-30 23:52:19 -04:00
John Hood c964d40dd8 Do not throw from destructors, fatal_assert() instead. 2016-03-30 23:52:19 -04:00
John Hood 255dc39c9f Factor encrypt/decrypt out of Network::Message. 2016-03-30 23:52:19 -04:00
John Hood 6abd4739de Const correctness and const-ref in Crypto and elsewhere. 2016-03-30 23:52:19 -04:00
John Hood 13928e9c10 Use a secure counter for OCB's nonce. Protect nonce in Network::Packet. 2016-03-30 23:52:19 -04:00
John Hood 6fd570f81f Various fixes for Illumos Hipster 20151003. 2016-03-30 23:52:19 -04:00
John Hood 3fa42cb8bb Support different IPv4 and IPv6 MTUs.
Closes #688.
2015-11-29 10:31:17 -05:00
John Hood 0de4082e86 base64: remove a timing variation 2015-10-20 02:39:36 -04:00
John Hood 0eb614809a Add configurable support for Apple Common Crypto and Nettle libraries. 2015-10-16 19:00:42 -04:00
John Hood db311f92f7 Replace OpenSSL base64 impl with a simple direct impl.
Unit tests, too.
2015-10-16 19:00:42 -04:00
John Hood d0db0968b1 Enable altivec on ppc64el
From http://launchpadlibrarian.net/171225681/mosh_1.2.4a-1build1_1.2.4a-1ubuntu1.diff.gz

Fixes #615.
2015-06-11 00:55:12 -04:00
John Hood dd58a398de ocb.cc: Mark local functions as static
This fixes the broken i386 build (aka generic code).
Untested on __ALTIVEC__ and __ARM_NEON__.

Signed-off-by: John Hood <cgull@glup.org>
2015-06-07 17:06:40 -04:00
Anders Kaseorg 0824e6549a base64: Make base64_encode declaration consistent with its definition
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2015-06-04 23:50:40 -04:00
Anders Kaseorg 8f6b226d8b Base64Key: Deduplicate PRNG code
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2015-06-04 23:50:40 -04:00
Geoffrey Thomas aeffb71cfc Style cleanup: "foo &x", not "foo& x" 2015-06-04 23:47:32 -04:00
Geoffrey Thomas 5721b392ab Make all exception classes inherit from std::exception
This refactors out a very common pattern of formatting "%s: %s" with
e.function.c_str() and strerror( e.the_errno ) into just the what()
method of NetworkException. It's also a prerequisite for making cleaner
public API for any exceptions we throw, and allows us to more easily
get exceptions passed back to us to handle.
2015-06-04 23:47:31 -04:00
John Hood 814a2ae55d clang/arm fixup for FreeBSD-CURRENT 2015-05-25 02:17:25 -04:00
Pasi Sjöholm ff84425885 ocb: fix gcc4.8 compiling problem with __ARM_NEON__
Fixes following problem when compiling mosh with gcc4.8 and
__ARM_NEON__ defined by using -121 instead of 135.

ocb.cc: In function 'block double_block(block)':
ocb.cc:263:56: error: narrowing conversion of '135' from
'int' to '__builtin_neon_qi' inside { } is ill-formed in
C++11 [-Werror=narrowing]

Signed-off-by: Pasi Sjöholm <pasi.sjoholm@jollamobile.com>
2015-02-03 11:21:46 +02:00
Jérémie Courrèges-Anglas 9314ea18fa use betoh64 if be64toh not found
Instead of looking for htobe64 which is be available both when
be64toh or betoh64 are, check for the latter functions.  If we
find betoh64 but not be64toh, use compat #defines.  If both
can't be found, search for OSX' OSSwapHostToBigInt64.
Also include sys/types.h in byteorder.h (which is necessary for
byteorder functions on OpenBSD), and incidentally fixes build
of networkfragment.cc.

Fixes build on OpenBSD

Signed-off-by: Jérémie Courrèges-Anglas <jca@wxcvbn.org>
2013-04-16 11:11:45 +02:00
Keith Winstein 9669178f07 Eliminate ocb.cc test program (closes #408)
This test doesn't return an error on failure and also was triggering a
stack protector warning on some platforms. We have an end-to-end test of
OCB in src/tests/ocb-aes.cc that seems to work well.
2013-03-27 18:36:03 -04:00
Anders Kaseorg 8c5ded4e6f Nonce::{cc_str,data}: Make const
Found by cppcheck.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2013-03-10 15:48:08 -04:00
Anders Kaseorg ecdd2dd648 PRNG: Read input using C++
In the old code, cppcheck complained about throwing in the destructor,
but like, seriously?

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2013-03-10 15:48:07 -04:00
Anders Kaseorg 14ef590220 Base64::Base64: Fix exception safety
Found by cppcheck.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
2013-03-10 15:48:07 -04:00