Author | Jamozed <[email protected]> |
Date | 2022-02-05 06:40:12 |
Commit | 15c1e58fadea99893e704732ec0695ea43bb7730 |
Parent | c01a723a048b012ff1903cad9bbbac032f37ef5a |
rc2: Add rc2 utility
Diffstat
M | CMakeLists.txt | | | 1 | + |
A | src/rc2.c | | | 153 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 154 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 759df8a..42e693f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,5 +17,6 @@ ADD_EXECUTABLE(alder32 ${PROJECT_SOURCE_DIR}/src/alder32.c) ADD_EXECUTABLE(crc32 ${PROJECT_SOURCE_DIR}/src/crc32.c) ADD_EXECUTABLE(fnv1a64 ${PROJECT_SOURCE_DIR}/src/fnv1a64.c) ADD_EXECUTABLE(otp ${PROJECT_SOURCE_DIR}/src/otp.c) +ADD_EXECUTABLE(rc2 ${PROJECT_SOURCE_DIR}/src/rc2.c) TARGET_LINK_LIBRARIES(otp OpenSSL::Crypto) diff --git a/src/rc2.c b/src/rc2.c new file mode 100644 index 0000000..aa8b1d1 --- /dev/null +++ b/src/rc2.c @@ -0,0 +1,153 @@ +// rc2.c, version 0.2.0 +// OMKOV cryptutils rc2 +// Copyright (C) 2021, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "util/error.c" +#include "util/optget.h" +#include "util/rc2.h" +#include "util/util.h" + +#include <limits.h> +#include <stdio.h> +#include <string.h> + +#define VERSION "0.2.0" + +static struct lop lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } +}; + +static int mode = 0; +static char *karg; +static int larg; + +static inline void rc2(const char *ifile, const char *ofile); + +static void hlp(void); +static void ver(void); + +int main(int ac, char *av[]) { A0 = av[0]; + struct opt opt = OPTGET_INIT; opt.str = "dek:l:m"; opt.lops = lops; + for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { + case 'e': { mode = 0; } break; + case 'd': { mode = 1; } break; + case 'k': { karg = opt.arg; } break; + case 'l': { + register int d; register char *p = opt.arg; + for (larg = 0; *p >= '0' && *p < '9'; ++p) { + d = (int)*p - '0'; + if (larg > (INT_MAX - d) / 10) { break; } + larg = larg * 10 + d; + } + + if (*p) { error(1, "invalid length"); } + if (larg > 1024) { error(1, "length must be at most 1024"); } + if (larg == 0) { error(1, "length must be greater than 0"); } + } break; + case 'm': { /* TODO */ error(1, "-m not implemented"); } break; + case 256: { hlp(); } return 0; + case 257: { ver(); } return 0; + default: {} return 1; + } + + if (!larg) { larg = 1024; } + + if (opt.ind == ac) { error(1, "missing operand"); } + else if (opt.ind == ac - 1) { rc2(av[opt.ind], NULL); } + else if (opt.ind == ac - 2) { rc2(av[opt.ind], av[opt.ind + 1]); } + else { error(1, "excess operand"); } + + return 0; +} + +static inline void rc2(const char *ifile, const char *ofile) { + struct rc2 ctx; u8 b1[128], b2[16]; FILE *fk, *fi, *fo; size_t kl, fl; + + if (!karg) { error(1, "-k is required"); } + else if (!(fk = fopen(karg, "r"))) { error(1, "%s: %s", karg, serr()); } + + // TODO Check key size is within bounds + kl = fread(b1, 1, sizeof (b1), fk); fclose(fk); fk = NULL; + rc2expand(&ctx, b1, kl, larg); + + if (!ifile) { fi = stdin; } + else if (!(fi = fopen(ifile, "r"))) { error(1, "%s: %s", ifile, serr()); } + + if (!ofile) { fo = stdout; } + else if (!(fo = fopen(ofile, "w"))) { error(1, "%s: %s", ofile, serr()); } + + for (size_t c; (c = fread(b1, 1, 8, fi));) { + while (c >= 8) { + memset(b2, 0, 8); + if (mode == 0) { rc2encrypt(&ctx, b1, b2); } + if (mode == 1) { rc2decrypt(&ctx, b1, b2); } + fwrite(b2, 1, 8, fo); c -= 8; + } + + if (c != 0) { // Should only be true during encryption + memset(b2, 0, 8); + if (mode == 0) { rc2encrypt(&ctx, b1, b2); } + fwrite(b2, 1, 8, fo); + } + + memset(b1, 0, 8); + } + + if (fi != stdin) { fclose(fi); } + if (fo != stdin) { fclose(fo); } + + return; +} + +/* Print help information. */ +static void hlp(void) { + puts("rc2 - encrypt and decrypt files using rc2\n"); + puts("usage: rc2 [-d|-e] [-k keyfile] [-l length] [-m mode] [infile] " + "[outfile]\n"); + puts("options:"); + puts(" -d Decrypt the input data"); + puts(" -e Encrypt the input data (default)"); + puts(" -k file Specify keyfile"); + puts(" -l length Override effective key length"); + puts(" -m mode Specify block cipher mode of operation"); + puts(" --help Display help information"); + puts(" --version Display version information"); +} + +/* Print version information. */ +static void ver(void) { + puts("OMKOV cryptutils rc2, version " VERSION); + puts("Copyright (C) 2021, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); +}