From 9f009c6f0dff2f43818bf27d0aa50d494b89d82f Mon Sep 17 00:00:00 2001 From: Jan Wichelmann Date: Mon, 29 Jul 2024 14:46:00 +0200 Subject: [PATCH] Add new cipher --- src/my_lib.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/my_lib.c b/src/my_lib.c index 8764882..2da9908 100644 --- a/src/my_lib.c +++ b/src/my_lib.c @@ -5,6 +5,35 @@ #define NOINLINE __attribute__((noinline)) +char caesarLookup[][26] = { + "abcdefghijklmnopqrstuvwxyz", + "bcdefghijklmnopqrstuvwxyza", + "cdefghijklmnopqrstuvwxyzab", + "defghijklmnopqrstuvwxyzabc", + "efghijklmnopqrstuvwxyzabcd", + "fghijklmnopqrstuvwxyzabcde", + "ghijklmnopqrstuvwxyzabcdef", + "hijklmnopqrstuvwxyzabcdefg", + "ijklmnopqrstuvwxyzabcdefgh", + "jklmnopqrstuvwxyzabcdefghi", + "klmnopqrstuvwxyzabcdefghij", + "lmnopqrstuvwxyzabcdefghijk", + "mnopqrstuvwxyzabcdefghijkl", + "nopqrstuvwxyzabcdefghijklm", + "opqrstuvwxyzabcdefghijklmn", + "pqrstuvwxyzabcdefghijklmno", + "qrstuvwxyzabcdefghijklmnop", + "rstuvwxyzabcdefghijklmnopq", + "stuvwxyzabcdefghijklmnopqr", + "tuvwxyzabcdefghijklmnopqrs", + "uvwxyzabcdefghijklmnopqrst", + "vwxyzabcdefghijklmnopqrstu", + "wxyzabcdefghijklmnopqrstuv", + "xyzabcdefghijklmnopqrstuvw", + "yzabcdefghijklmnopqrstuvwx", + "zabcdefghijklmnopqrstuvwxy" +}; + uint8_t lookup[256]; void init(void) @@ -15,9 +44,29 @@ void init(void) lookup[i] = (uint8_t)rand(); } +void caesarEncrypt(char input[], int length, char output[], int shift) +{ + for(int i = 0; i < length; ++i) + { + if(input[i] < 'a' || input[i] > 'z') + { + output[i] = input[i]; + continue; + } + + output[i] = caesarLookup[shift][input[i] - 'a']; + } +} + void lookup_leakage(uint8_t *input, int inputLength, uint8_t *output) { - // Empty and constant time + // Convert input to alpha string + char *alpha = (char *)malloc(inputLength + 1); + for(int i = 0; i < inputLength; ++i) + alpha[i] = 'a' + (input[i] % 26); + alpha[inputLength] = '\0'; + + caesarEncrypt(alpha, inputLength, (char *)output, input[0] % 26); } int branch_leakage(uint8_t *input, int inputLength)