| #include "caesar_cypher.h" |
| #include <compartment-macros.h> |
| #include <debug.hh> |
| #include <errno.h> |
| |
| using Debug = ConditionalDebug<true, "Caesar">; |
| |
| int caesar_encrypt(SObj capability, |
| const char *input, |
| char *output, |
| size_t length) |
| { |
| auto *cypherState = token_unseal<CaesarCapability>( |
| STATIC_SEALING_TYPE(CaesarCapabilityType), capability); |
| if (cypherState == nullptr) |
| { |
| return -EINVAL; |
| } |
| if (!cypherState->permitEncrypt) |
| { |
| return -EPERM; |
| } |
| for (int i = 0; i < length; i++) |
| { |
| uint8_t c = input[i]; |
| // Replace control characters with 'X' |
| if ((c < 32) || (c >= 127)) |
| { |
| c = 'X'; |
| } |
| // Reset the base to 0. |
| c -= 32; |
| // Perform the encryption |
| c += cypherState->shift; |
| if (c > 94) |
| { |
| c -= 94; |
| } |
| c += 32; |
| output[i] = c; |
| } |
| return 0; |
| } |
| |
| int caesar_decrypt(SObj capability, |
| const char *input, |
| char *output, |
| size_t length) |
| { |
| auto *cypherState = token_unseal<CaesarCapability>( |
| STATIC_SEALING_TYPE(CaesarCapabilityType), capability); |
| if (cypherState == nullptr) |
| { |
| return -EINVAL; |
| } |
| if (!cypherState->permitDecrypt) |
| { |
| return -EPERM; |
| } |
| for (int i = 0; i < length; i++) |
| { |
| int c = input[i]; |
| // Perform the encryption |
| c -= cypherState->shift; |
| if (c < 32) |
| { |
| c += 94; |
| } |
| output[i] = c; |
| } |
| return 0; |
| } |