Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/scripts/test-otp22.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh
# Run inside the official erlang:22.3 image, in a fresh checkout.
set -eu

erl -noshell -eval '"22" = erlang:system_info(otp_release), halt().'

# Current publishing/test plugins may require newer OTP. Keep the project's
# dependencies and test settings, and invoke PropEr directly without a plugin.
config_backup=$(mktemp)
cp rebar.config "$config_backup"
trap 'cp "$config_backup" rebar.config; rm -f "$config_backup"' EXIT

erl -noshell -eval '
{ok, Config} = file:consult("rebar.config"),
WithoutPlugins = fun(Options) ->
proplists:delete(project_plugins, proplists:delete(plugins, Options))
end,
Profiles = [{Name, WithoutPlugins(Options)} ||
{Name, Options} <- proplists:get_value(profiles, Config, [])],
Updated = lists:keystore(profiles, 1, WithoutPlugins(Config),
{profiles, Profiles}),
ok = file:write_file("rebar.config",
[io_lib:format("~p.~n", [Term]) || Term <- Updated]),
halt().'

rebar3 version
make
rebar3 as test eunit
make security-test
ERL_FLAGS="+S 1" rebar3 as test eunit --module=bcrypt_security_tests

erl -noshell -pa _build/test/lib/*/ebin _build/test/lib/bcrypt/test -eval '
case proper:module(prop_bcrypt, [{numtests, 100}]) of
[] -> halt(0);
_ -> halt(1)
end.'

make xref
make dialyzer
23 changes: 21 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,38 @@ jobs:

strategy:
matrix:
otp_version: [21.3, 22.3, 23, 24]
otp_version: [27,28,29]
os: [ubuntu-latest]

container:
image: erlang:${{ matrix.otp_version }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v7
- name: Compile
run: make
- name: Test
run: make test
- name: Native security tests
run: make security-test
- name: Single-scheduler security tests
run: ERL_FLAGS="+S 1" rebar3 as test eunit --module=bcrypt_security_tests
- name: XRef
run: make xref
- name: Dialyzer
run: make dialyzer

otp22:
name: Test on OTP 22.3
runs-on: ubuntu-24.04
timeout-minutes: 30
steps:
# Keep Node-based actions on the host, outside the old Debian image.
- uses: actions/checkout@v7
- name: Test in the OTP 22.3 toolchain
run: |
docker run --rm \
--volume "$GITHUB_WORKSPACE:/work" \
--workdir /work \
erlang:22.3 \
sh .github/scripts/test-otp22.sh
71 changes: 13 additions & 58 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -51,66 +51,21 @@ subject to the following license:
*/

The underlying bcrypt (hashing) code is derived from OpenBSD libc and is
subject to the following license:
subject to the following license:

/*
* Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
* All rights reserved.
* Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
* Copyright (c) 1997 Niels Provos <provos@umich.edu>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Niels Provos.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

The asynchronous queue code (c_src/async_queue.c and
c_src/async_queue.h) is from the esnappy project, copyright 2011
Konstantin V. Sorokin. It is subject to the following license:

Copyright (c) 2011 Konstantin V. Sorokin
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ edoc_private: $(REBAR)

exdoc: $(REBAR)
$(REBAR) ex_doc --logo doc/img/logo.png --output edoc

# Isolated native boundary checks with address/undefined-behavior sanitizers.
.PHONY: security-test
security-test:
$(MAKE) -C c_src security-test
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ as the basis of the Hex package.
OTP Compatibility
-----------------

erlang-bcrypt is compatible with OTP 21.3 to 23.
erlang-bcrypt is compatible with OTP 21.3 to 29.

Use version 1.0.3 on OTP versions before 21.3

Expand Down Expand Up @@ -75,9 +75,9 @@ Hash a password using a salt with the default number of rounds:

```erlang
1> {ok, Salt} = bcrypt:gen_salt().
{ok,"$2a$12$sSS8Eg.ovVzaHzi1nUHYK."}
{ok,"$2b$12$sSS8Eg.ovVzaHzi1nUHYK."}
2> {ok, Hash} = bcrypt:hashpw("foo", Salt).
{ok,"$2a$12$sSS8Eg.ovVzaHzi1nUHYK.HbUIOdlQI0iS22Q5rd5z.JVVYH6sfm6"}
{ok,"$2b$12$sSS8Eg.ovVzaHzi1nUHYK.HbUIOdlQI0iS22Q5rd5z.JVVYH6sfm6"}
```

Verify the password:
Expand All @@ -103,7 +103,7 @@ application's environment:
Specifies whether to use the NIF implementation (`'nif'`) or a
pool of port programs (`'port'`). Defaults to `'nif'`.

`Note: the NIF implementation no longer blocks the Erlang VM scheduler threads`
`Note: hashing runs on OTP dirty CPU schedulers, not normal scheduler threads.`

`pool_size`
Specifies the size of the port program pool. Defaults to `4`.
Expand Down Expand Up @@ -184,3 +184,35 @@ Original authors
----------------

Hunter Morris & [Mrinal Wadhwa](https://github.com/mrinalwadhwa).

## Password compatibility and native safety

New salts use `$2b$`. Hashing uses at most the first 72 password bytes,
following OpenBSD bcrypt. Passwords up to 1,024 bytes are accepted; longer
inputs return `{error, invalid_password_length}`. Malformed salts return
`{error, invalid_salt}`. These rules apply to both the NIF and port backends.

`$2b$` rejects embedded NUL bytes with `{error, invalid_password}`. Pass text
passwords as encoded bytes; do not pass raw binary prehashes containing NULs.
Existing `$2a$` hashes retain historical password-length wraparound and NUL
termination semantics for verification. After a successful legacy verification,
generate a fresh salt and rehash with `$2b$`; do not just change the prefix.
Legacy hashes affected by those behaviors may warrant a password reset.

The NIF uses OTP dirty CPU schedulers. Its asynchronous `hashpw/5` interface
uses an Erlang helper process and an opaque reference from `create_ctx/0`;
there are no custom native threads, task queues, or blocking destructors.
The public pool still limits concurrent requests. Killing a caller does not
cancel an already running hash; applications should restrict accepted costs
and rate-limit authentication requests. Contexts created before this change
must be recreated when upgrading the application.

The C core incorporates OpenBSD `bcrypt.c` revision 1.58, including `$2b$`,
strict salt parsing, decoder error checking, output bounds, and secure clearing,
with a bounded input adapter shared by both backends. Upstream source:
[OpenBSD bcrypt.c](https://github.com/openbsd/src/blob/master/lib/libc/crypt/bcrypt.c).

Run `make security-test` for native boundary checks under ASan/UBSan. Run
`rebar3 as test eunit` for compatibility, malformed-input, and lifecycle tests.
The lifecycle regression can also be run with `ERL_FLAGS="+S 1"` to verify
that owner exit does not block the sole normal scheduler.
17 changes: 13 additions & 4 deletions c_src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c

all: $(C_SRC_NIF) $(C_SRC_PORT)

$(C_SRC_PORT): $(OBJECTS)
$(C_SRC_PORT): bcrypt_port.o bcrypt.o blowfish.o
@mkdir -p $(BASEDIR)/priv/
$(CC) $(CFLAGS) bcrypt_port.o bcrypt.o blowfish.o $(DRV_LDFLAGS) $(LDLIBS) -o ../priv/bcrypt

$(C_SRC_NIF): $(OBJECTS)
$(C_SRC_NIF): bcrypt_nif.o bcrypt.o blowfish.o
@mkdir -p $(BASEDIR)/priv/
$(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) -shared $(LDLIBS) -o $(C_SRC_NIF)
$(link_verbose) $(CC) bcrypt_nif.o bcrypt.o blowfish.o $(LDFLAGS) -shared $(LDLIBS) -o $(C_SRC_NIF)

$(OBJECTS): bcrypt.h erl_blf.h

%.o: %.c
$(COMPILE_C) $(OUTPUT_OPTION) $<
Expand All @@ -86,4 +88,11 @@ $(C_SRC_NIF): $(OBJECTS)
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

clean:
@rm -f $(C_SRC_OUTPUT) $(OBJECTS)
@rm -f $(C_SRC_NIF) $(C_SRC_PORT) $(OBJECTS)

.PHONY: security-test
security-test:
@tmpdir=$$(mktemp -d) || exit 1; trap 'rm -rf "$$tmpdir"' EXIT; \
$(CC) -std=c99 -D_DEFAULT_SOURCE -g -O1 -fsanitize=address,undefined \
-fno-omit-frame-pointer -fno-sanitize-recover=all -I. ../test/bcrypt_native_tests.c bcrypt.c blowfish.c \
-o "$$tmpdir/bcrypt_native_tests" && "$$tmpdir/bcrypt_native_tests"
Loading
Loading