Skip to content

Commit

Permalink
Merge pull request #610 from JohnSanpe/feat-md5
Browse files Browse the repository at this point in the history
Feat md5
  • Loading branch information
JohnSanpe committed Jun 28, 2024
2 parents 98d281d + 271e85f commit 7e3d7c0
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 65 deletions.
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_subdirectory(btree)
add_subdirectory(cache)
add_subdirectory(circle)
add_subdirectory(crc)
add_subdirectory(crypto)
add_subdirectory(fifo)
add_subdirectory(fsm)
add_subdirectory(guards)
Expand All @@ -38,7 +39,6 @@ add_subdirectory(rbtree)
add_subdirectory(respool)
add_subdirectory(ringbuf)
add_subdirectory(segtree)
add_subdirectory(sha)
add_subdirectory(skiplist)
add_subdirectory(slist)
add_subdirectory(sort)
Expand Down
4 changes: 2 additions & 2 deletions examples/sha/.gitignore → examples/crypto/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-or-later
/sha-bandwidth
/sha
/crypto-bandwidth
/crypto
32 changes: 32 additions & 0 deletions examples/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright(c) 2023 John Sanpe <sanpeqf@gmail.com>
#

add_executable(crypto-bandwidth bandwidth.c)
target_link_libraries(crypto-bandwidth bfdev)
add_test(crypto-bandwidth crypto-bandwidth)

add_executable(crypto utils.c)
target_link_libraries(crypto bfdev)

add_test(sha1 crypto "helloworld")
add_test(sha224 crypto -l "helloworld")
add_test(sha256 crypto -e "helloworld")
add_test(md5 crypto -m "helloworld")

if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev")
install(FILES
bandwidth.c
utils.c
DESTINATION
${CMAKE_INSTALL_DOCDIR}/examples/crypto
)

install(TARGETS
crypto
crypto-bandwidth
DESTINATION
${CMAKE_INSTALL_DOCDIR}/bin
)
endif()
22 changes: 21 additions & 1 deletion examples/sha/bandwidth.c → examples/crypto/bandwidth.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* Copyright(c) 2024 John Sanpe <sanpeqf@gmail.com>
*/

#define MODULE_NAME "sha-bandwidth"
#define MODULE_NAME "crypto-bandwidth"
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt

#include <stdio.h>
#include <stdlib.h>
#include <bfdev/sha1.h>
#include <bfdev/sha2.h>
#include <bfdev/md5.h>
#include <bfdev/log.h>
#include <bfdev/size.h>
#include "../time.h"
Expand Down Expand Up @@ -71,6 +72,24 @@ sha256_test(void *buff)
}
}

static void
md5_test(void *buff)
{
uint8_t digest[BFDEV_MD5_DIGEST_SIZE];
bfdev_md5_ctx_t md5;
unsigned int count, loop;

bfdev_md5_init(&md5);
for (count = 0; count < TEST_LOOP; ++count) {
EXAMPLE_TIME_LOOP(&loop, 1000,
bfdev_md5_update(&md5, buff, TEST_SIZE);
bfdev_md5_finish(&md5, digest);
0;
);
bfdev_log_info("md5 bandwidth %u: %uMiB/s\n", count, loop);
}
}

int
main(int argc, char const *argv[])
{
Expand All @@ -88,6 +107,7 @@ main(int argc, char const *argv[])
sha1_test(buff);
sha224_test(buff);
sha256_test(buff);
md5_test(buff);

free(buff);
return 0;
Expand Down
24 changes: 23 additions & 1 deletion examples/sha/utils.c → examples/crypto/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright(c) 2024 John Sanpe <sanpeqf@gmail.com>
*/

#define MODULE_NAME "bfdev-sha"
#define MODULE_NAME "bfdev-crypto"
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt

#include <stdio.h>
Expand All @@ -12,11 +12,13 @@
#include <bfdev/errname.h>
#include <bfdev/sha1.h>
#include <bfdev/sha2.h>
#include <bfdev/md5.h>

enum {
ALGO_SHA1 = 0,
ALGO_SHA224,
ALGO_SHA256,
ALGO_MD5,
};

int
Expand All @@ -41,6 +43,10 @@ main(int argc, const char *argv[])
algo = ALGO_SHA256;
break;

case 'm':
algo = ALGO_MD5;
break;

case 'h': default:
goto usage;
}
Expand Down Expand Up @@ -101,6 +107,21 @@ main(int argc, const char *argv[])
fputc('\n', stdout);
break;
}

case ALGO_MD5: {
bfdev_md5_ctx_t md5;
uint8_t digest[BFDEV_MD5_DIGEST_SIZE];
unsigned int count;

bfdev_md5_init(&md5);
bfdev_md5_update(&md5, data, length);
bfdev_md5_finish(&md5, digest);

for (count = 0; count < BFDEV_MD5_DIGEST_SIZE; ++count)
printf("%02x", digest[count]);
fputc('\n', stdout);
break;
}
}
} while (++index < argc);

Expand All @@ -110,6 +131,7 @@ main(int argc, const char *argv[])
bfdev_log_err("Usage: %s [-h -l -e] data ...\n", argv[0]);
bfdev_log_err("\t-l using SHA224 algorithm\n");
bfdev_log_err("\t-e using SHA256 algorithm\n");
bfdev_log_err("\t-u using MD5 algorithm\n");
bfdev_log_err("The default algorithm is SHA1\n");
return 1;
}
31 changes: 0 additions & 31 deletions examples/sha/CMakeLists.txt

This file was deleted.

40 changes: 39 additions & 1 deletion include/bfdev/asm-generic/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,45 @@

BFDEV_BEGIN_DECLS

/* Nothing */
#define BFDEV_GENERIC_BYTEORDER_ARRAY(name, type1, type2, func) \
static inline void \
name(type1 *dest, const type2 *src, size_t size) \
{ \
size_t index; \
\
for (index = 0; index < size; ++index) \
dest[index] = func(src[index]); \
}

BFDEV_GENERIC_BYTEORDER_ARRAY(
bfdev_cpu_to_le16_array,
bfdev_le16, uint16_t, bfdev_cpu_to_le16
)

BFDEV_GENERIC_BYTEORDER_ARRAY(
bfdev_cpu_to_le32_array,
bfdev_le32, uint32_t, bfdev_cpu_to_le32
)

BFDEV_GENERIC_BYTEORDER_ARRAY(
bfdev_cpu_to_le64_array,
bfdev_le64, uint64_t, bfdev_cpu_to_le64
)

BFDEV_GENERIC_BYTEORDER_ARRAY(
bfdev_le16_to_cpu_array,
uint16_t, bfdev_le16, bfdev_le16_to_cpu
)

BFDEV_GENERIC_BYTEORDER_ARRAY(
bfdev_le32_to_cpu_array,
uint32_t, bfdev_le32, bfdev_le32_to_cpu
)

BFDEV_GENERIC_BYTEORDER_ARRAY(
bfdev_le64_to_cpu_array,
uint64_t, bfdev_le64, bfdev_le64_to_cpu
)

BFDEV_END_DECLS

Expand Down
65 changes: 37 additions & 28 deletions include/bfdev/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,43 @@

BFDEV_BEGIN_DECLS

#define _bfdev_cpu_to_le_type(type) bfdev_cpu_to_le##type
#define bfdev_cpu_to_le_type(type) _bfdev_cpu_to_le_type(type)
#define _bfdev_cpu_to_be_type(type) bfdev_cpu_to_be##type
#define bfdev_cpu_to_be_type(type) _bfdev_cpu_to_be_type(type)
#define _bfdev_le_type_to_cpu(type) bfdev_le##type##_to_cpu
#define bfdev_le_type_to_cpu(type) _bfdev_le_type_to_cpu(type)
#define _bfdev_be_type_to_cpu(type) bfdev_be##type##_to_cpu
#define bfdev_be_type_to_cpu(type) _bfdev_be_type_to_cpu(type)

#define bfdev_cpu_to_le_short(val) bfdev_cpu_to_le_type(BFDEV_BITS_PER_SHORT)(val)
#define bfdev_cpu_to_le_int(val) bfdev_cpu_to_le_type(BFDEV_BITS_PER_INT)(val)
#define bfdev_cpu_to_le_long(val) bfdev_cpu_to_le_type(BFDEV_BITS_PER_LONG)(val)
#define bfdev_cpu_to_le_long_long(val) bfdev_cpu_to_le_type(BFDEV_BITS_PER_LONG_LONG)(val)

#define bfdev_cpu_to_be_short(val) bfdev_cpu_to_be_type(BFDEV_BITS_PER_SHORT)(val)
#define bfdev_cpu_to_be_int(val) bfdev_cpu_to_be_type(BFDEV_BITS_PER_INT)(val)
#define bfdev_cpu_to_be_long(val) bfdev_cpu_to_be_type(BFDEV_BITS_PER_LONG)(val)
#define bfdev_cpu_to_be_long_long(val) bfdev_cpu_to_be_type(BFDEV_BITS_PER_LONG_LONG)(val)

#define bfdev_le_short_to_cpu(val) bfdev_le_type_to_cpu(BFDEV_BITS_PER_SHORT)(val)
#define bfdev_le_int_to_cpu(val) bfdev_le_type_to_cpu(BFDEV_BITS_PER_INT)(val)
#define bfdev_le_long_to_cpu(val) bfdev_le_type_to_cpu(BFDEV_BITS_PER_LONG)(val)
#define bfdev_le_long_long_to_cpu(val) bfdev_le_type_to_cpu(BFDEV_BITS_PER_LONG_LONG)(val)

#define bfdev_be_short_to_cpu(val) bfdev_be_type_to_cpu(BFDEV_BITS_PER_SHORT)(val)
#define bfdev_be_int_to_cpu(val) bfdev_be_type_to_cpu(BFDEV_BITS_PER_INT)(val)
#define bfdev_be_long_to_cpu(val) bfdev_be_type_to_cpu(BFDEV_BITS_PER_LONG)(val)
#define bfdev_be_long_long_to_cpu(val) bfdev_be_type_to_cpu(BFDEV_BITS_PER_LONG_LONG)(val)
#define _bfdev_cpu_to_le_type(type) bfdev_cpu_to_le##type
#define bfdev_cpu_to_le_type(type) _bfdev_cpu_to_le_type(type)
#define _bfdev_cpu_to_be_type(type) bfdev_cpu_to_be##type
#define bfdev_cpu_to_be_type(type) _bfdev_cpu_to_be_type(type)
#define _bfdev_le_type_to_cpu(type) bfdev_le##type##_to_cpu
#define bfdev_le_type_to_cpu(type) _bfdev_le_type_to_cpu(type)
#define _bfdev_be_type_to_cpu(type) bfdev_be##type##_to_cpu
#define bfdev_be_type_to_cpu(type) _bfdev_be_type_to_cpu(type)

#define _bfdev_cpu_to_le_type_array(type) bfdev_cpu_to_le##type##_array
#define bfdev_cpu_to_le_type_array(type) _bfdev_cpu_to_le_type_array(type)
#define _bfdev_cpu_to_be_type_array(type) bfdev_cpu_to_be##type##_array
#define bfdev_cpu_to_be_type_array(type) _bfdev_cpu_to_be_type_array(type)
#define _bfdev_le_type_to_cpu_array(type) bfdev_le##type##_to_cpu_array
#define bfdev_le_type_to_cpu_array(type) _bfdev_le_type_to_cpu_array(type)
#define _bfdev_be_type_to_cpu_array(type) bfdev_be##type##_to_cpu_array
#define bfdev_be_type_to_cpu_array(type) _bfdev_be_type_to_cpu_array(type)

#define bfdev_cpu_to_le_long(val) \
bfdev_cpu_to_le_type(BFDEV_BITS_PER_LONG)(val)
#define bfdev_cpu_to_be_long(val) \
bfdev_cpu_to_be_type(BFDEV_BITS_PER_LONG)(val)

#define bfdev_le_long_to_cpu(val) \
bfdev_le_type_to_cpu(BFDEV_BITS_PER_LONG)(val)
#define bfdev_be_long_to_cpu(val) \
bfdev_be_type_to_cpu(BFDEV_BITS_PER_LONG)(val)

#define bfdev_cpu_to_le_long_array(dest, src, size) \
bfdev_cpu_to_le_type_array(BFDEV_BITS_PER_LONG)(dest, src, size)
#define bfdev_cpu_to_be_long_array(dest, src, size) \
bfdev_cpu_to_be_type_array(BFDEV_BITS_PER_LONG)(dest, src, size)

#define bfdev_le_long_to_cpu_array(dest, src, size) \
bfdev_le_type_to_cpu_array(BFDEV_BITS_PER_LONG)(dest, src, size)
#define bfdev_be_long_to_cpu_array(dest, src, size) \
bfdev_be_type_to_cpu_array(BFDEV_BITS_PER_LONG)(dest, src, size)

BFDEV_END_DECLS

Expand Down
62 changes: 62 additions & 0 deletions include/bfdev/crypto/chacha.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2024 John Sanpe <sanpeqf@gmail.com>
*/

#ifndef _BFDEV_CRYPTO_SHA1_H_
#define _BFDEV_CRYPTO_SHA1_H_

#include <bfdev/config.h>
#include <bfdev/types.h>
#include <bfdev/bitops.h>
#include <bfdev/unaligned.h>

#define BFDEV_CHACHA_BLEND(b, A, B, C, D, E) \
b[A] += b[B]; b[C] = bfdev_rol32(b[C] ^ b[D], E)
static inline void
bfdev_chacha_permute(uint32_t *block, unsigned int rounds)
{
unsigned int count;

for (count = 0; count < rounds; count += 2) {
BFDEV_CHACHA_BLEND(block, 0, 4, 12, 0, 16);
BFDEV_CHACHA_BLEND(block, 1, 5, 13, 1, 16);
BFDEV_CHACHA_BLEND(block, 2, 6, 14, 2, 16);
BFDEV_CHACHA_BLEND(block, 3, 7, 15, 3, 16);

BFDEV_CHACHA_BLEND(block, 8, 12, 4, 8, 12);
BFDEV_CHACHA_BLEND(block, 9, 13, 5, 9, 12);
BFDEV_CHACHA_BLEND(block, 10, 14, 6, 10, 12);
BFDEV_CHACHA_BLEND(block, 11, 15, 7, 11, 12);

BFDEV_CHACHA_BLEND(block, 0, 4, 12, 0, 8);
BFDEV_CHACHA_BLEND(block, 1, 5, 13, 1, 8);
BFDEV_CHACHA_BLEND(block, 2, 6, 14, 2, 8);
BFDEV_CHACHA_BLEND(block, 3, 7, 15, 3, 8);

BFDEV_CHACHA_BLEND(block, 8, 12, 4, 8, 7);
BFDEV_CHACHA_BLEND(block, 9, 13, 5, 9, 7);
BFDEV_CHACHA_BLEND(block, 10, 14, 6, 10, 7);
BFDEV_CHACHA_BLEND(block, 11, 15, 7, 11, 7);

BFDEV_CHACHA_BLEND(block, 0, 5, 15, 0, 16);
BFDEV_CHACHA_BLEND(block, 1, 6, 12, 1, 16);
BFDEV_CHACHA_BLEND(block, 2, 7, 13, 2, 16);
BFDEV_CHACHA_BLEND(block, 3, 4, 14, 3, 16);

BFDEV_CHACHA_BLEND(block, 10, 15, 5, 10, 12);
BFDEV_CHACHA_BLEND(block, 11, 12, 6, 11, 12);
BFDEV_CHACHA_BLEND(block, 8, 13, 7, 8, 12);
BFDEV_CHACHA_BLEND(block, 9, 14, 4, 9, 12);

BFDEV_CHACHA_BLEND(block, 0, 5, 15, 0, 8);
BFDEV_CHACHA_BLEND(block, 1, 6, 12, 1, 8);
BFDEV_CHACHA_BLEND(block, 2, 7, 13, 2, 8);
BFDEV_CHACHA_BLEND(block, 3, 4, 14, 3, 8);

BFDEV_CHACHA_BLEND(block, 10, 15, 5, 10, 7);
BFDEV_CHACHA_BLEND(block, 11, 12, 6, 11, 7);
BFDEV_CHACHA_BLEND(block, 8, 13, 7, 8, 7);
BFDEV_CHACHA_BLEND(block, 9, 14, 4, 9, 7);
}
}
Loading

0 comments on commit 7e3d7c0

Please sign in to comment.