aboutsummaryrefslogtreecommitdiffstats
path: root/librecrypt_get_encoding.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--librecrypt_get_encoding.c112
1 files changed, 105 insertions, 7 deletions
diff --git a/librecrypt_get_encoding.c b/librecrypt_get_encoding.c
index 7d0e62c..6d02d07 100644
--- a/librecrypt_get_encoding.c
+++ b/librecrypt_get_encoding.c
@@ -38,6 +38,88 @@ librecrypt_get_encoding(const char *settings, size_t len, char *pad_out, int *st
# ifndef FUZZ
+#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
+#define ALT_ALPHABET "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
+
+NONSTRING static const char encoding_lut[256u] = MAKE_ENCODING_LUT(ALPHABET);
+NONSTRING static const char alt_encoding_lut[256u] = MAKE_ENCODING_LUT(ALT_ALPHABET);
+
+static const unsigned char decoding_lut[256u] = {
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63,
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX, XX,
+ XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, XX, XX, XX, XX, XX,
+ XX, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX
+};
+
+static const unsigned char alt_decoding_lut[256u] = {
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63,
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, XX, XX, XX, XX, XX, XX,
+ XX, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
+ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX,
+ XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
+ XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX
+};
+
+static unsigned
+rot4_is_algorithm(const char *settings, size_t len)
+{
+ if (len >= sizeof("$rot4$") - 1u)
+ if (!strncmp(settings, "$rot4$", sizeof("$rot4$") - 1u))
+ return 1u;
+ return 0u;
+}
+
+static unsigned
+trunc_is_algorithm(const char *settings, size_t len)
+{
+ if (len >= sizeof("$trunc$") - 1u)
+ if (!strncmp(settings, "$trunc$", sizeof("$trunc$") - 1u))
+ return 1u;
+ return 0u;
+}
+
+static const struct librecrypt_algorithm rot4_algo = {
+ .is_algorithm = &rot4_is_algorithm,
+ .encoding_lut = encoding_lut,
+ .decoding_lut = decoding_lut,
+ .hash_size = 8u,
+ .flexible_hash_size = 0,
+ .strict_pad = 1,
+ .pad = '#'
+};
+
+static const struct librecrypt_algorithm trunc_algo = {
+ .is_algorithm = &trunc_is_algorithm,
+ .encoding_lut = alt_encoding_lut,
+ .decoding_lut = alt_decoding_lut,
+ .hash_size = 4u,
+ .flexible_hash_size = 1,
+ .strict_pad = 1,
+ .pad = '@'
+};
+
#define NSA "$~no~such~algorithm~$"
@@ -73,7 +155,7 @@ check_decoding_lut(const unsigned char *lut, const char *alpha)
do {\
pad = (char)~(PAD);\
strict_pad = -1;\
- elut = librecrypt_get_encoding(PREFIX, sizeof(PREFIX) - 1u, &pad, &strict_pad, 0, NULL);\
+ elut = librecrypt_get_encoding(PREFIX, sizeof(PREFIX) - 1u, &pad, &strict_pad, 0, ctx);\
EXPECT(elut != NULL);\
EXPECT(pad == (PAD));\
EXPECT(strict_pad == (STRICT_PAD));\
@@ -81,7 +163,7 @@ check_decoding_lut(const unsigned char *lut, const char *alpha)
\
pad = (char)~(PAD);\
strict_pad = -1;\
- dlut = librecrypt_get_encoding(PREFIX, sizeof(PREFIX) - 1u, &pad, &strict_pad, 1, NULL);\
+ dlut = librecrypt_get_encoding(PREFIX, sizeof(PREFIX) - 1u, &pad, &strict_pad, 1, ctx);\
EXPECT(dlut != NULL);\
EXPECT(pad == (PAD));\
EXPECT(strict_pad == (STRICT_PAD));\
@@ -89,7 +171,7 @@ check_decoding_lut(const unsigned char *lut, const char *alpha)
\
pad = (char)~(PAD);\
strict_pad = -1;\
- elut = librecrypt_get_encoding(NSA">"PREFIX, sizeof(NSA">"PREFIX) - 1u, &pad, &strict_pad, 0, NULL);\
+ elut = librecrypt_get_encoding(NSA">"PREFIX, sizeof(NSA">"PREFIX) - 1u, &pad, &strict_pad, 0, ctx);\
EXPECT(elut != NULL);\
EXPECT(pad == (PAD));\
EXPECT(strict_pad == (STRICT_PAD));\
@@ -105,6 +187,8 @@ check_decoding_lut(const unsigned char *lut, const char *alpha)
int
main(void)
{
+ const struct librecrypt_algorithm custom[] = {rot4_algo, trunc_algo};
+ LIBRECRYPT_CONTEXT *ctx = NULL;
const char *elut;
const unsigned char *dlut;
char pad;
@@ -114,19 +198,19 @@ main(void)
INIT_RESOURCE_TEST();
errno = 0;
- EXPECT(librecrypt_get_encoding(NSA, sizeof(NSA) - 1u, &pad, &strict_pad, 0, NULL) == NULL);
+ EXPECT(librecrypt_get_encoding(NSA, sizeof(NSA) - 1u, &pad, &strict_pad, 0, ctx) == NULL);
EXPECT(errno == ENOSYS);
errno = 0;
- EXPECT(librecrypt_get_encoding(NSA, sizeof(NSA) - 1u, &pad, &strict_pad, 1, NULL) == NULL);
+ EXPECT(librecrypt_get_encoding(NSA, sizeof(NSA) - 1u, &pad, &strict_pad, 1, ctx) == NULL);
EXPECT(errno == ENOSYS);
errno = 0;
- EXPECT(librecrypt_get_encoding(">"NSA, sizeof(">"NSA) - 1u, &pad, &strict_pad, 0, NULL) == NULL);
+ EXPECT(librecrypt_get_encoding(">"NSA, sizeof(">"NSA) - 1u, &pad, &strict_pad, 0, ctx) == NULL);
EXPECT(errno == ENOSYS);
errno = 0;
- EXPECT(librecrypt_get_encoding(">"NSA, sizeof(">"NSA) - 1u, &pad, &strict_pad, 1, NULL) == NULL);
+ EXPECT(librecrypt_get_encoding(">"NSA, sizeof(">"NSA) - 1u, &pad, &strict_pad, 1, ctx) == NULL);
EXPECT(errno == ENOSYS);
IF__argon2i_v1_0__SUPPORTED(CHECK("$argon2i$", UPPER LOWER DIGIT "+/", '=', 0);)
@@ -139,6 +223,20 @@ main(void)
IF__argon2id_v1_3__SUPPORTED(CHECK("$argon2id$v=19$", UPPER LOWER DIGIT "+/", '=', 0);)
IF__argon2ds_v1_3__SUPPORTED(CHECK("$argon2ds$v=19$", UPPER LOWER DIGIT "+/", '=', 0);)
+ ctx = librecrypt_create_context();
+ assert(ctx != NULL);
+
+ librecrypt_set_custom_algorithms(ctx, custom, ELEMSOF(custom));
+ CHECK("$rot4$", ALPHABET, '#', 1);
+ CHECK("$trunc$", ALT_ALPHABET, '@', 1);
+ CHECK("$trunc$*4", ALT_ALPHABET, '@', 1);
+ CHECK("$rot4$>$trunc$", ALT_ALPHABET, '@', 1);
+ CHECK("$rot4$>$trunc$*4", ALT_ALPHABET, '@', 1);
+ CHECK("$trunc$>$rot4$", ALPHABET, '#', 1);
+ CHECK("$trunc$*4>$rot4$", ALPHABET, '#', 1);
+
+ librecrypt_free_context(ctx);
+
STOP_RESOURCE_TEST();
return 0;
}