Skip to content

Commit

Permalink
Big and little-endian load and store support
Browse files Browse the repository at this point in the history
These are needed in ML-KEM and ML-DSA, and are likely generally useful,
so public.

Reviewed-by: Tim Hudson <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from openssl#26385)
  • Loading branch information
Viktor Dukhovni authored and mattcaswell committed Jan 14, 2025
1 parent 8e69c18 commit 92c242e
Show file tree
Hide file tree
Showing 9 changed files with 555 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ OpenSSL 3.5

### Changes between 3.4 and 3.5 [xx XXX xxxx]

* New inline functions were added to support loads and stores of unsigned
16-bit, 32-bit and 64-bit integers in either little-endian or big-endian
form, regardless of the host byte-order. See the `OPENSSL_load_u16_le(3)`
manpage for details.

*Viktor Dukhovni*

* All the BIO_meth_get_*() functions allowing reuse of the internal OpenSSL
BIO method implementations were deprecated. The reuse is unsafe due to
dependency on the code of the internal methods not changing.
Expand Down
6 changes: 6 additions & 0 deletions doc/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,10 @@ DEPEND[html/man3/OPENSSL_load_builtin_modules.html]=man3/OPENSSL_load_builtin_mo
GENERATE[html/man3/OPENSSL_load_builtin_modules.html]=man3/OPENSSL_load_builtin_modules.pod
DEPEND[man/man3/OPENSSL_load_builtin_modules.3]=man3/OPENSSL_load_builtin_modules.pod
GENERATE[man/man3/OPENSSL_load_builtin_modules.3]=man3/OPENSSL_load_builtin_modules.pod
DEPEND[html/man3/OPENSSL_load_u16_le.html]=man3/OPENSSL_load_u16_le.pod
GENERATE[html/man3/OPENSSL_load_u16_le.html]=man3/OPENSSL_load_u16_le.pod
DEPEND[man/man3/OPENSSL_load_u16_le.3]=man3/OPENSSL_load_u16_le.pod
GENERATE[man/man3/OPENSSL_load_u16_le.3]=man3/OPENSSL_load_u16_le.pod
DEPEND[html/man3/OPENSSL_malloc.html]=man3/OPENSSL_malloc.pod
GENERATE[html/man3/OPENSSL_malloc.html]=man3/OPENSSL_malloc.pod
DEPEND[man/man3/OPENSSL_malloc.3]=man3/OPENSSL_malloc.pod
Expand Down Expand Up @@ -3402,6 +3406,7 @@ html/man3/OPENSSL_init_crypto.html \
html/man3/OPENSSL_init_ssl.html \
html/man3/OPENSSL_instrument_bus.html \
html/man3/OPENSSL_load_builtin_modules.html \
html/man3/OPENSSL_load_u16_le.html \
html/man3/OPENSSL_malloc.html \
html/man3/OPENSSL_riscvcap.html \
html/man3/OPENSSL_s390xcap.html \
Expand Down Expand Up @@ -4066,6 +4071,7 @@ man/man3/OPENSSL_init_crypto.3 \
man/man3/OPENSSL_init_ssl.3 \
man/man3/OPENSSL_instrument_bus.3 \
man/man3/OPENSSL_load_builtin_modules.3 \
man/man3/OPENSSL_load_u16_le.3 \
man/man3/OPENSSL_malloc.3 \
man/man3/OPENSSL_riscvcap.3 \
man/man3/OPENSSL_s390xcap.3 \
Expand Down
84 changes: 84 additions & 0 deletions doc/man3/OPENSSL_load_u16_le.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
=pod

=head1 NAME

OPENSSL_load_u16_le, OPENSSL_load_u16_be, OPENSSL_load_u32_le,
OPENSSL_load_u32_be, OPENSSL_load_u64_le, OPENSSL_load_u64_be,
OPENSSL_store_u16_le, OPENSSL_store_u16_be,
OPENSSL_store_u32_le, OPENSSL_store_u32_be,
OPENSSL_store_u64_le, OPENSSL_store_u64_be -
Read and write unsigned 16, 32 and 64-bit integers in a specific byte order

=head1 SYNOPSIS

#include <openssl/byteorder.h>

static ossl_inline unsigned char *OPENSSL_store_u16_le(
unsigned char *out, uint16_t val);
static ossl_inline unsigned char *OPENSSL_store_u16_be(
unsigned char *out, uint16_t val);
static ossl_inline unsigned char *OPENSSL_store_u32_le(
unsigned char *out, uint32_t val);
static ossl_inline unsigned char *OPENSSL_store_u32_be(
unsigned char *out, uint32_t val);
static ossl_inline unsigned char *OPENSSL_store_u64_le(
unsigned char *out, uint64_t val);
static ossl_inline unsigned char *OPENSSL_store_u64_be(
unsigned char *out, uint64_t val);
static ossl_inline const unsigned char *OPENSSL_load_u16_le(
uint16_t *val, const unsigned char *in);
static ossl_inline const unsigned char *OPENSSL_load_u16_be(
uint16_t *val, const unsigned char *in);
static ossl_inline const unsigned char *OPENSSL_load_u32_le(
uint32_t *val, const unsigned char *in);
static ossl_inline const unsigned char *OPENSSL_load_u32_be(
uint32_t *val, const unsigned char *in);
static ossl_inline const unsigned char *OPENSSL_load_u64_le(
uint64_t *val, const unsigned char *in);
static ossl_inline const unsigned char *OPENSSL_load_u64_be(
uint64_t *val, const unsigned char *in);

=head1 DESCRIPTION

These functions read and write 16, 32 and 64 bit unsigned integers in a
specified byte order.
The C<_be> functions use big-endian byte order, while the C<_le> functions use
little-endian byte order.
They're implemented directly in the header file, and declared static. When the
compiler supports inline functions, they're also declared inline.
An optimising compiler will often convert these to just one or two machine
instructions: a load or store with a possible byte swap.

The C<load> functions write the decoded integer value at the address pointed to
by I<val>, which must be a valid (possibly suitably aligned) address of an
object of the appropriate type.
The C<store> functions write the encoding of I<val> at the address pointed to
by I<out>.

For convenience, these functions return the updated input or output pointer,
making it easy to continue reading or writing more data at the next memory
location.

No bounds checks are performed, the caller is responsible for making sure that
the input or output buffers are sufficiently large for the requested read or
write.

=head1 RETURN VALUES

All these functions return the next memory address following the last byte
written or read.

=head1 HISTORY

These functions were added in OpenSSL 3.5.

=head1 COPYRIGHT

Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use
this file except in compliance with the License. You can obtain a copy
in the file LICENSE in the source distribution or at
L<https://www.openssl.org/source/license.html>.

=cut
Loading

0 comments on commit 92c242e

Please sign in to comment.