Skip to content

Commit

Permalink
add ChaCha20 large tester
Browse files Browse the repository at this point in the history
Note, ChaCha20 performance  is about en par with cSHAKE encryption (i.e.
cSHAKE AEAD without the authentication).

Signed-off-by: Stephan Mueller <[email protected]>
  • Loading branch information
smuellerDD committed Sep 9, 2023
1 parent c0f86b6 commit 0e8220b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
70 changes: 70 additions & 0 deletions sym/tests/chacha20_stream_large_tester.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2022 - 2023, Stephan Mueller <[email protected]>
*
* License, 0x see LICENSE file in root directory
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
* WHICH ARE HEREBY 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 NOT ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#include "conv_be_le.h"
#include "lc_chacha20.h"
#include "ret_checkers.h"
#include "visibility.h"

#define LC_CHACHA_SIZE (1UL << 30)
static int chacha20_large_tester(void)
{
/* Test vector according to RFC 7539 section 2.4.2 */
static const uint8_t key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f };
static const uint8_t iv[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x4a, 0x00, 0x00, 0x00, 0x00 };
uint8_t *pt = NULL;
int ret;
LC_SYM_CTX_ON_STACK(chacha20, lc_chacha20);

pt = calloc(1, LC_CHACHA_SIZE);
if (!pt)
return 1;

/* Encrypt */
lc_sym_init(chacha20);
CKINT(lc_sym_setkey(chacha20, (uint8_t *)key, sizeof(key)));
CKINT(lc_sym_setiv(chacha20, (uint8_t *)iv, sizeof(iv)));
lc_sym_encrypt(chacha20, pt, pt, LC_CHACHA_SIZE);
lc_sym_zero(chacha20);

/* Decrypt */
lc_sym_init(chacha20);
CKINT(lc_sym_setkey(chacha20, (uint8_t *)key, sizeof(key)));
CKINT(lc_sym_setiv(chacha20, (uint8_t *)iv, sizeof(iv)));
lc_sym_decrypt(chacha20, pt, pt, LC_CHACHA_SIZE);
lc_sym_zero(chacha20);

out:
if (pt)
free(pt);
return ret;
}

LC_TEST_FUNC(int, main, int argc, char *argv[])
{
(void)argc;
(void)argv;

return chacha20_large_tester();
}
8 changes: 8 additions & 0 deletions sym/tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ if get_option('chacha20').enabled()
'../src' ],
dependencies: leancrypto
)
chacha20_stream_large_tester = executable('chacha20_stream_large_tester',
[ 'chacha20_stream_large_tester.c',
internal_src ],
include_directories: [ include_internal_dirs,
'../src' ],
dependencies: leancrypto
)

test('Symmetric ChaCha20 block', chacha20_block_tester)
test('Symmetric ChaCha20 stream', chacha20_stream_tester)
test('Symmetric ChaCha20 1GB', chacha20_stream_large_tester)
endif

if get_option('aes_block').enabled()
Expand Down

0 comments on commit 0e8220b

Please sign in to comment.