|
libenigma
Enigma machine utility library
|
#include "crack.h"#include "common.h"#include "enigma.h"#include "io.h"#include "rotor.h"#include <ctype.h>#include <stdio.h>#include <stdlib.h>#include <string.h>Macros | |
| #define | _POSIX_C_SOURCE 200809L |
Functions | |
| EnigmaCrackParams * | enigma_crack_params_new (void) |
| Create a new EnigmaCrackParams structure. | |
| int | enigma_crack_params_validate (const EnigmaCrackParams *cfg) |
| Check what capabilities the EnigmaCrackParams structure has. | |
| int | enigma_crack_plugboard (EnigmaCrackParams *cfg, float(*scoreFunc)(const EnigmaCrackParams *, const char *)) |
| Crack the plugboard using a scoring function. | |
| int | enigma_crack_reflector (EnigmaCrackParams *cfg, float(*scoreFunc)(const EnigmaCrackParams *, const char *)) |
| Crack the reflector using a scoring function. | |
| int | enigma_crack_rotor (EnigmaCrackParams *cfg, int targetRotor, float(*scoreFunc)(const EnigmaCrackParams *, const char *)) |
| Crack the rotor using a scoring function. | |
| int | enigma_crack_rotors (EnigmaCrackParams *cfg, float(*scoreFunc)(const EnigmaCrackParams *, const char *)) |
| Crack the rotor order using a scoring function. | |
| int | enigma_crack_rotor_position (EnigmaCrackParams *cfg, int rotor, float(*scoreFunc)(const EnigmaCrackParams *, const char *)) |
| Crack a rotor's position using a scoring function. | |
| int | enigma_crack_rotor_positions (EnigmaCrackParams *cfg, float(*scoreFunc)(const EnigmaCrackParams *, const char *)) |
| Crack the rotor positions using a scoring function. | |
| int | enigma_dict_match (const EnigmaCrackParams *cfg, const char *plaintext) |
| Checks if multiple words from the dictionary are present in the plaintext. | |
| int | enigma_find_potential_indices (const char *ciphertext, const char *plaintext, int *indices) |
| Finds potential indices in the ciphertext where the known plaintext may exist. | |
| float | enigma_freq (const char *plaintext, int len) |
| Calculate the frequency of characters in the plaintext. | |
| int | enigma_letter_freq (const EnigmaCrackParams *cfg, const char *plaintext) |
| Check if the letter frequencies in the plaintext match target frequencies within an offset. | |
| int | enigma_score_append (EnigmaCrackParams *cfg, Enigma *enigma, const char *plaintext, float score) |
| Append a score to an EnigmaScoreList. | |
| int | enigma_score_flags (const EnigmaCrackParams *cfg, const char *plaintext) |
| Get the flags for a given plaintext based on the crack configuration. | |
| const Enigma * | enigma_crack_get_enigma (const EnigmaCrackParams *cfg) |
| Get the enigma field in the given EnigmaCrackParams struct. | |
| const EnigmaScoreList * | enigma_crack_get_score_list (const EnigmaCrackParams *cfg) |
| Get the score_list field in the given EnigmaCrackParams struct. | |
| const char ** | enigma_crack_get_dictionary (const EnigmaCrackParams *cfg) |
| Get the dictionary field in the given EnigmaCrackParams struct. | |
| size_t | enigma_crack_get_dictionary_length (const EnigmaCrackParams *cfg) |
Get the dictionary_length field in the given EnigmaCrackParams struct. | |
| const float * | enigma_crack_get_ngrams (const EnigmaCrackParams *cfg) |
| Get the ngrams field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_get_n (const EnigmaCrackParams *cfg) |
| Get the n field in the given EnigmaCrackParams struct. | |
| size_t | enigma_crack_get_ngrams_length (const EnigmaCrackParams *cfg) |
| Get the ngrams_length field in the given EnigmaCrackParams struct. | |
| const char * | enigma_crack_get_ciphertext (const EnigmaCrackParams *cfg) |
| Get the ciphertext field in the given EnigmaCrackParams struct. | |
| size_t | enigma_crack_get_ciphertext_length (const EnigmaCrackParams *cfg) |
| Get the ciphertext_length field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_get_flags (const EnigmaCrackParams *cfg) |
| Get the flags field in the given EnigmaCrackParams struct. | |
| const float * | enigma_crack_get_frequency_targets (const EnigmaCrackParams *cfg) |
| Get the frequency_targets field in the given EnigmaCrackParams struct. | |
| float | enigma_crack_get_min_score (const EnigmaCrackParams *cfg) |
| Get the min_score field in the given EnigmaCrackParams struct. | |
| float | enigma_crack_get_max_score (const EnigmaCrackParams *cfg) |
| Get the max_score field in the given EnigmaCrackParams struct. | |
| float | enigma_crack_get_target_score (const EnigmaCrackParams *cfg) |
| Get the target_score field in the given EnigmaCrackParams struct. | |
| const char * | enigma_crack_get_known_plaintext (const EnigmaCrackParams *cfg) |
| Get the known_plaintext field in the given EnigmaCrackParams struct. | |
| size_t | enigma_crack_get_known_plaintext_length (const EnigmaCrackParams *cfg) |
| Get the known_plaintext_length field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_enigma (EnigmaCrackParams *cfg, Enigma *enigma) |
| Set the enigma field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_score_list (EnigmaCrackParams *cfg, EnigmaScoreList *scoreList) |
| Set the score_list field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_dictionary (EnigmaCrackParams *cfg, const char **dictionary, size_t length) |
| Set the dictionary field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_ngrams (EnigmaCrackParams *cfg, float *ngrams, int n, size_t length) |
| Set the ngrams field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_n (EnigmaCrackParams *cfg, int n) |
| Set the n field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_ciphertext (EnigmaCrackParams *cfg, const char *ciphertext, size_t length) |
| Set the ciphertext field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_flags (EnigmaCrackParams *cfg, int flags) |
| Set the flags field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_frequency_targets (EnigmaCrackParams *cfg, float *frequencyTargets) |
| Set the frequency targets field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_min_score (EnigmaCrackParams *cfg, float minScore) |
| Set the minimum score field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_max_score (EnigmaCrackParams *cfg, float maxScore) |
| Set the maximum score field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_target_score (EnigmaCrackParams *cfg, float targetScore) |
| Set the target score field in the given EnigmaCrackParams struct. | |
| int | enigma_crack_set_known_plaintext (EnigmaCrackParams *cfg, const char *knownPlaintext, size_t length) |
| Set the known plaintext field in the given EnigmaCrackParams struct. | |
This file implements functions for cracking Enigma machine configurations using various methods such as brute force and n-gram analysis. It includes functions for loading dictionaries, scoring decrypted text, and finding potential indices in the ciphertext where known plaintext may exist.
| #define _POSIX_C_SOURCE 200809L |
| const char * enigma_crack_get_ciphertext | ( | const EnigmaCrackParams * | cfg | ) |
Get the ciphertext field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| size_t enigma_crack_get_ciphertext_length | ( | const EnigmaCrackParams * | cfg | ) |
Get the ciphertext_length field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| const char ** enigma_crack_get_dictionary | ( | const EnigmaCrackParams * | cfg | ) |
Get the dictionary field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| size_t enigma_crack_get_dictionary_length | ( | const EnigmaCrackParams * | cfg | ) |
Get the dictionary_length field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
dictionary_length field, or ENIGMA_FAILURE if cfg is NULL. | const Enigma * enigma_crack_get_enigma | ( | const EnigmaCrackParams * | cfg | ) |
Get the enigma field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| int enigma_crack_get_flags | ( | const EnigmaCrackParams * | cfg | ) |
Get the flags field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| const float * enigma_crack_get_frequency_targets | ( | const EnigmaCrackParams * | cfg | ) |
Get the frequency_targets field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| const char * enigma_crack_get_known_plaintext | ( | const EnigmaCrackParams * | cfg | ) |
Get the known_plaintext field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| size_t enigma_crack_get_known_plaintext_length | ( | const EnigmaCrackParams * | cfg | ) |
Get the known_plaintext_length field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| float enigma_crack_get_max_score | ( | const EnigmaCrackParams * | cfg | ) |
Get the max_score field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| float enigma_crack_get_min_score | ( | const EnigmaCrackParams * | cfg | ) |
Get the min_score field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| int enigma_crack_get_n | ( | const EnigmaCrackParams * | cfg | ) |
Get the n field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| const float * enigma_crack_get_ngrams | ( | const EnigmaCrackParams * | cfg | ) |
Get the ngrams field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
ngrams field, or NULL if cfg is NULL. | size_t enigma_crack_get_ngrams_length | ( | const EnigmaCrackParams * | cfg | ) |
Get the ngrams_length field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| const EnigmaScoreList * enigma_crack_get_score_list | ( | const EnigmaCrackParams * | cfg | ) |
Get the score_list field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| float enigma_crack_get_target_score | ( | const EnigmaCrackParams * | cfg | ) |
Get the target_score field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| EnigmaCrackParams * enigma_crack_params_new | ( | void | ) |
Create a new EnigmaCrackParams structure.
This function allocates memory for a new EnigmaCrackParams structure.
| int enigma_crack_params_validate | ( | const EnigmaCrackParams * | cfg | ) |
Check what capabilities the EnigmaCrackParams structure has.
This function checks if the EnigmaCrackParams structure is able to be used to perform cracking operations. The following flags are checked:
| cfg | Pointer to the EnigmaCrackParams structure. |
| int enigma_crack_plugboard | ( | EnigmaCrackParams * | cfg, |
| float(*)(const EnigmaCrackParams *, const char *) | scoreFunc | ||
| ) |
Crack the plugboard using a scoring function.
This function attempts to determine the plugboard settings used in the Enigma machine by evaluating all possible plugboard configurations and scoring the resulting plaintext using the provided scoring function.
| cfg | Pointer to the cracking configuration structure. |
| scoreFunc | Function pointer to the scoring function to use. |
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure. | int enigma_crack_reflector | ( | EnigmaCrackParams * | cfg, |
| float(*)(const EnigmaCrackParams *, const char *) | scoreFunc | ||
| ) |
Crack the reflector using a scoring function.
This function attempts to determine the reflector used in the Enigma machine by evaluating all possible reflectors and scoring the resulting plaintext using the provided scoring function.
| cfg | Pointer to the cracking configuration structure. |
| scoreFunc | Function pointer to the scoring function to use. |
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure. | int enigma_crack_rotor | ( | EnigmaCrackParams * | cfg, |
| int | targetRotor, | ||
| float(*)(const EnigmaCrackParams *, const char *) | scoreFunc | ||
| ) |
Crack the rotor using a scoring function.
This function attempts to determine the rotor used in the Enigma machine by evaluating all possible rotors and scoring the resulting plaintext using the provided scoring function.
| cfg | Pointer to the cracking configuration structure. |
| targetRotor | The index of the rotor to crack (0-based). |
| scoreFunc | Function pointer to the scoring function to use. |
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure. | int enigma_crack_rotor_position | ( | EnigmaCrackParams * | cfg, |
| int | rotor, | ||
| float(*)(const EnigmaCrackParams *, const char *) | scoreFunc | ||
| ) |
Crack a rotor's position using a scoring function.
This function attempts to determine the starting position of the given rotor by evaluating all possible rotor positions and scoring the resulting plaintext using the provided scoring function.
| cfg | Pointer to the cracking configuration structure. |
| scoreFunc | Function pointer to the scoring function to use. |
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure. | int enigma_crack_rotor_positions | ( | EnigmaCrackParams * | cfg, |
| float(*)(const EnigmaCrackParams *, const char *) | scoreFunc | ||
| ) |
Crack the rotor positions using a scoring function.
This function attempts to determine the rotor starting positions of the Enigma machine by evaluating all possible rotor positions and scoring the resulting plaintext using the provided scoring function.
| cfg | Pointer to the cracking configuration structure. |
| scoreFunc | Function pointer to the scoring function to use. |
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure. | int enigma_crack_rotors | ( | EnigmaCrackParams * | cfg, |
| float(*)(const EnigmaCrackParams *, const char *) | scoreFunc | ||
| ) |
Crack the rotor order using a scoring function.
This function attempts to determine the order of rotors used in the Enigma machine by evaluating all possible rotor orders and scoring the resulting plaintext using the provided scoring function.
| cfg | Pointer to the cracking configuration structure. |
| scoreFunc | Function pointer to the scoring function to use. |
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure. | int enigma_crack_set_ciphertext | ( | EnigmaCrackParams * | cfg, |
| const char * | ciphertext, | ||
| size_t | length | ||
| ) |
Set the ciphertext field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| ciphertext | The ciphertext string |
| length | The length of the ciphertext string |
| int enigma_crack_set_dictionary | ( | EnigmaCrackParams * | cfg, |
| const char ** | dictionary, | ||
| size_t | length | ||
| ) |
Set the dictionary field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| dictionary | The dictionary array |
| length | The length of the dictionary array |
| int enigma_crack_set_enigma | ( | EnigmaCrackParams * | cfg, |
| Enigma * | enigma | ||
| ) |
Set the enigma field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| enigma | The Enigmainstance |
| int enigma_crack_set_flags | ( | EnigmaCrackParams * | cfg, |
| int | flags | ||
| ) |
Set the flags field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| flags | The flags value |
| int enigma_crack_set_frequency_targets | ( | EnigmaCrackParams * | cfg, |
| float * | frequencyTargets | ||
| ) |
Set the frequency targets field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| frequencyTargets | The frequency targets array |
| int enigma_crack_set_known_plaintext | ( | EnigmaCrackParams * | cfg, |
| const char * | knownPlaintext, | ||
| size_t | length | ||
| ) |
Set the known plaintext field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| knownPlaintext | The known plaintext string |
| length | The length of the known plaintext string |
| int enigma_crack_set_max_score | ( | EnigmaCrackParams * | cfg, |
| float | maxScore | ||
| ) |
Set the maximum score field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| maxScore | The maximum score value |
| int enigma_crack_set_min_score | ( | EnigmaCrackParams * | cfg, |
| float | minScore | ||
| ) |
Set the minimum score field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| minScore | The minimum score value |
| int enigma_crack_set_n | ( | EnigmaCrackParams * | cfg, |
| int | n | ||
| ) |
Set the n field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| n | The n value |
| int enigma_crack_set_ngrams | ( | EnigmaCrackParams * | cfg, |
| float * | ngrams, | ||
| int | n, | ||
| size_t | length | ||
| ) |
Set the ngrams field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| ngrams | The ngrams array |
| n | The n value |
| length | The length of the ngrams array |
| int enigma_crack_set_score_list | ( | EnigmaCrackParams * | cfg, |
| EnigmaScoreList * | scoreList | ||
| ) |
Set the score_list field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| scoreList | The EnigmaScoreList instance |
| int enigma_crack_set_target_score | ( | EnigmaCrackParams * | cfg, |
| float | targetScore | ||
| ) |
Set the target score field in the given EnigmaCrackParams struct.
| cfg | The EnigmaCrackParams struct instance |
| targetScore | The target score value |
| int enigma_dict_match | ( | const EnigmaCrackParams * | cfg, |
| const char * | plaintext | ||
| ) |
Checks if multiple words from the dictionary are present in the plaintext.
This function checks the plaintext against a dictionary of words and returns 1 if multiple words are found, otherwise returns 0.
The dictionary must be uppercase and sorted.
| cfg | The EnigmaCrackParams struct containing the dictionary and its size |
| plaintext | The plaintext to check |
ENIGMA_FAILURE on error | int enigma_find_potential_indices | ( | const char * | ciphertext, |
| const char * | plaintext, | ||
| int * | indices | ||
| ) |
Finds potential indices in the ciphertext where the known plaintext may exist.
Due to the nature of the Enigma machine, a letter cannot be encoded to itself. This function checks the ciphertext for potential indices where the given string could potentially be in the plaintext.
| ciphertext | The ciphertext to analyze |
| plaintext | The known plaintext string to test against the ciphertext |
| indices | Pointer to an array (length >= length of ciphertext) to store the indices (-1-terminated) |
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure. | float enigma_freq | ( | const char * | plaintext, |
| int | len | ||
| ) |
Calculate the frequency of characters in the plaintext.
This function calculates the frequency of each character in the plaintext and returns a score based on the frequencies.
| plaintext | The plaintext to analyze |
| len | The length of the plaintext |
ENIGMA_FAILURE if the input is invalid. | int enigma_letter_freq | ( | const EnigmaCrackParams * | cfg, |
| const char * | plaintext | ||
| ) |
Check if the letter frequencies in the plaintext match target frequencies within an offset.
This function checks if the frequency of each letter in the plaintext is within the specified offset of the target frequencies. If more than ten letters deviate from the target frequencies, the function returns 0 (false). Otherwise, it returns 1 (true).
| cfg | The EnigmaCrackParams struct containing the target frequencies and offset |
| plaintext | The plaintext to analyze |
ENIGMA_FAILURE if error. | int enigma_score_append | ( | EnigmaCrackParams * | cfg, |
| Enigma * | enigma, | ||
| const char * | plaintext, | ||
| float | score | ||
| ) |
Append a score to an EnigmaScoreList.
If the scores array is full, it will be resized to double its current size.
| cfg | Pointer to the cracking configuration structure. |
| enigma | Pointer to the Enigma structure representing the scored configuration. |
| plaintext | The plaintext corresponding to the score. |
| score | The score to append. |
ENIGMA_SUCCESS on success, ENIGMA_FAILURE on failure. | int enigma_score_flags | ( | const EnigmaCrackParams * | cfg, |
| const char * | plaintext | ||
| ) |
Get the flags for a given plaintext based on the crack configuration.
This function checks the plaintext against the criteria specified in the EnigmaCrackParams and returns a bitmask of flags indicating which criteria were met.
| cfg | The EnigmaCrackParams containing the criteria and flags. |
| plaintext | The plaintext to evaluate. |
ENIGMA_FAILURE if error.