Skip to content

Commit f7701d7

Browse files
authored
Implement crypto digest context in terms of software sha2 implementation (#61)
This pull request introduces the `nat20` cryptographic implementation to the codebase. This is a free standing implementation of the n20_crypto_digest_context subset of the crypto interface. It provides an implementation of the digest function in terms of the software Implementation added in #56. The function hmac is implemented in terms of this digest function and the hkdf family of function is implemented in terms of the hmac function. - [x] The digest subset of the crypto test suite is run against the new implementation.
1 parent f898487 commit f7701d7

6 files changed

Lines changed: 670 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ set(LIBNAT20_PUB_HEADERS
7272

7373
set(LIBNAT20_CRYPTO_NAT20_SOURCES
7474
# Add the core library source files here.
75+
src/crypto/nat20/crypto.c
7576
src/crypto/nat20/sha256.c
7677
src/crypto/nat20/sha512.c
7778
)
7879

7980
set(LIBNAT20_CRYPTO_NAT20_PUB_HEADERS
8081
# Add the public headers here.
8182
# These files will be included in the generation of the API documentation.
83+
include/nat20/crypto/nat20/crypto.h
8284
include/nat20/crypto/nat20/sha.h
8385
)
8486

@@ -270,6 +272,9 @@ if (NAT20_WITH_TESTS)
270272
target_link_libraries(nat20_crypto_test_bin nat20_crypto_boringssl)
271273
add_definitions(-DN20_CONFIG_ENABLE_CRYPTO_TEST_IMPL=1)
272274

275+
target_sources(nat20_crypto_test_bin
276+
PRIVATE src/crypto/test/crypto_nat20.cpp
277+
)
273278
target_link_libraries(nat20_crypto_test_bin nat20_crypto_nat20)
274279

275280

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2025 Aurora Operations, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <nat20/crypto.h>
20+
#include <nat20/error.h>
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
/**
27+
* @brief Open a new NAT20 cryptographic (digest) context.
28+
*
29+
* This is the factory function to create a crypto digest context
30+
* @ref n20_crypto_digest_context_t implementing SHA2
31+
* (SHA-224, SHA-256, SHA-384, SHA-512), HMAC, and HKDF without
32+
* external library dependencies.
33+
*
34+
* Each call to this function must be matched with a call to
35+
* @ref n20_crypto_nat20_close.
36+
*
37+
* In the current implementation the context returned is a singleton,
38+
* and @ref n20_crypto_nat20_close is a no-op. But this may change
39+
* in the future, and cannot be relied on.
40+
*
41+
* @param ctx_out Pointer to the context to be initialized.
42+
* @return n20_error_t Error code indicating success or failure.
43+
*/
44+
n20_error_t n20_crypto_nat20_open(n20_crypto_digest_context_t** ctx_out);
45+
46+
/**
47+
* @brief Close the NAT20 cryptographic context.
48+
*
49+
* This function closes and frees the resources associated with the
50+
* context @ref ctx_out.
51+
*
52+
* In the current implementation this is a no-op, as the context
53+
* is a singleton. But this may change in the future, and must
54+
* not be relied on.
55+
*
56+
* @param ctx_out Pointer to the context to be closed.
57+
* @return n20_error_t Error code indicating success or failure.
58+
*/
59+
n20_error_t n20_crypto_nat20_close(n20_crypto_digest_context_t* ctx_out);
60+
61+
#ifdef __cplusplus
62+
}
63+
#endif

0 commit comments

Comments
 (0)