Skip to content

Commit 66cc301

Browse files
Merge pull request #6 from dreamer-coding/main
Extend Fossil Cryptic API
2 parents 511161f + 72ecc40 commit 66cc301

28 files changed

+3104
-2679
lines changed

code/logic/auth.c

Lines changed: 94 additions & 530 deletions
Large diffs are not rendered by default.

code/logic/cipher.c

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
/**
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop
6+
* high-performance, cross-platform applications and libraries. The code
7+
* contained herein is licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License. You may obtain
9+
* a copy of the License at:
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* Author: Michael Gene Brockus (Dreamer)
20+
* Date: 04/05/2014
21+
*
22+
* Copyright (C) 2014-2025 Fossil Logic. All rights reserved.
23+
* -----------------------------------------------------------------------------
24+
*/
25+
#include "fossil/cryptic/cipher.h"
26+
#include <stdint.h>
27+
#include <string.h>
28+
29+
// ===========================================================
30+
// Advanced XOR Cipher (with key schedule)
31+
// ===========================================================
32+
static void cipher_xor(const uint8_t* key, size_t key_len,
33+
const uint8_t* input, uint8_t* output, size_t len) {
34+
// Simple key schedule: derive a pseudo-random stream from key
35+
uint32_t seed = 0xA5A5A5A5;
36+
for (size_t k = 0; k < key_len; ++k)
37+
seed ^= key[k] * 0x45D9F3B;
38+
for (size_t i = 0; i < len; i++) {
39+
seed = (seed ^ (seed << 13) ^ (seed >> 17)) + 0x9E3779B9;
40+
output[i] = input[i] ^ key[i % key_len] ^ ((seed >> (i % 24)) & 0xFF);
41+
}
42+
}
43+
44+
// ===========================================================
45+
// Morse Code Cipher (encode/decode)
46+
// ===========================================================
47+
static const char* morse_table[128] = {
48+
['A'] = ".-", ['B'] = "-...", ['C'] = "-.-.", ['D'] = "-..",
49+
['E'] = ".", ['F'] = "..-.", ['G'] = "--.", ['H'] = "....",
50+
['I'] = "..", ['J'] = ".---", ['K'] = "-.-", ['L'] = ".-..",
51+
['M'] = "--", ['N'] = "-.", ['O'] = "---", ['P'] = ".--.",
52+
['Q'] = "--.-", ['R'] = ".-.", ['S'] = "...", ['T'] = "-",
53+
['U'] = "..-", ['V'] = "...-", ['W'] = ".--", ['X'] = "-..-",
54+
['Y'] = "-.--", ['Z'] = "--..",
55+
['0'] = "-----", ['1'] = ".----", ['2'] = "..---", ['3'] = "...--",
56+
['4'] = "....-", ['5'] = ".....", ['6'] = "-....", ['7'] = "--...",
57+
['8'] = "---..", ['9'] = "----.",
58+
[' '] = "/"
59+
};
60+
61+
static void cipher_morse_encode(const uint8_t* input, uint8_t* output, size_t len) {
62+
size_t out_idx = 0;
63+
for (size_t i = 0; i < len; ++i) {
64+
char ch = input[i];
65+
if (ch >= 'a' && ch <= 'z') ch -= 32; // Convert to uppercase
66+
const char* code = morse_table[(unsigned char)ch];
67+
if (code) {
68+
size_t code_len = strlen(code);
69+
memcpy(output + out_idx, code, code_len);
70+
out_idx += code_len;
71+
output[out_idx++] = ' ';
72+
}
73+
}
74+
if (out_idx > 0) output[out_idx - 1] = '\0'; // Null-terminate
75+
else output[0] = '\0';
76+
}
77+
78+
static void cipher_morse_decode(const uint8_t* input, uint8_t* output, size_t len) {
79+
size_t out_idx = 0;
80+
size_t i = 0;
81+
while (i < len) {
82+
// Find next space or end
83+
size_t start = i;
84+
while (i < len && input[i] != ' ') i++;
85+
size_t code_len = i - start;
86+
if (code_len > 0) {
87+
char code[8] = {0};
88+
memcpy(code, input + start, code_len);
89+
code[code_len] = '\0';
90+
char decoded = '?';
91+
for (int c = 0; c < 128; ++c) {
92+
if (morse_table[c] && strcmp(morse_table[c], code) == 0) {
93+
decoded = (char)c;
94+
break;
95+
}
96+
}
97+
output[out_idx++] = decoded;
98+
}
99+
i++; // Skip space
100+
}
101+
output[out_idx] = '\0';
102+
}
103+
104+
//
105+
// ===========================================================
106+
// Advanced Caesar Cipher (with variable shift)
107+
// ===========================================================
108+
static void cipher_caesar(const uint8_t* key, size_t key_len,
109+
const uint8_t* input, uint8_t* output, size_t len) {
110+
// Use key-derived shift value
111+
uint8_t shift = 0;
112+
for (size_t k = 0; k < key_len; ++k)
113+
shift ^= key[k];
114+
shift = (shift % 25) + 1; // Ensure shift is between 1 and 25
115+
116+
for (size_t i = 0; i < len; ++i) {
117+
output[i] = input[i] + shift;
118+
}
119+
}
120+
121+
static void cipher_caesar_decrypt(const uint8_t* key, size_t key_len,
122+
const uint8_t* input, uint8_t* output, size_t len) {
123+
uint8_t shift = 0;
124+
for (size_t k = 0; k < key_len; ++k)
125+
shift ^= key[k];
126+
shift = (shift % 25) + 1;
127+
128+
for (size_t i = 0; i < len; ++i) {
129+
output[i] = input[i] - shift;
130+
}
131+
}
132+
133+
// ===========================================================
134+
// Advanced Vigenère Cipher (with key schedule)
135+
// ===========================================================
136+
static void cipher_vigenere(const uint8_t* key, size_t key_len,
137+
const uint8_t* input, uint8_t* output, size_t len) {
138+
for (size_t i = 0; i < len; ++i) {
139+
output[i] = input[i] + key[i % key_len];
140+
}
141+
}
142+
143+
static void cipher_vigenere_decrypt(const uint8_t* key, size_t key_len,
144+
const uint8_t* input, uint8_t* output, size_t len) {
145+
for (size_t i = 0; i < len; ++i) {
146+
output[i] = input[i] - key[i % key_len];
147+
}
148+
}
149+
150+
// ===========================================================
151+
// Advanced Feistel Cipher (multiple rounds, S-box)
152+
// ===========================================================
153+
static uint32_t feistel_sbox(uint32_t x) {
154+
// Simple non-linear S-box
155+
x = ((x >> 16) ^ x) * 0x45d9f3b;
156+
x = ((x >> 16) ^ x) * 0x45d9f3b;
157+
x = (x >> 16) ^ x;
158+
return x;
159+
}
160+
161+
static uint32_t feistel_round(uint32_t half, uint32_t key, int round) {
162+
// Mix with S-box and round number
163+
return feistel_sbox(half ^ key ^ (0x9E3779B9 * round));
164+
}
165+
166+
#define FEISTEL_ROUNDS 8
167+
168+
static void cipher_feistel_encrypt(uint8_t* data, size_t len, uint32_t key) {
169+
for (size_t i = 0; i + 8 <= len; i += 8) {
170+
uint32_t left, right;
171+
memcpy(&left, data + i, 4);
172+
memcpy(&right, data + i + 4, 4);
173+
for (int r = 0; r < FEISTEL_ROUNDS; r++) {
174+
uint32_t temp = right;
175+
right = left ^ feistel_round(right, key, r);
176+
left = temp;
177+
}
178+
memcpy(data + i, &left, 4);
179+
memcpy(data + i + 4, &right, 4);
180+
}
181+
}
182+
183+
static void cipher_feistel_decrypt(uint8_t* data, size_t len, uint32_t key) {
184+
for (size_t i = 0; i + 8 <= len; i += 8) {
185+
uint32_t left, right;
186+
memcpy(&left, data + i, 4);
187+
memcpy(&right, data + i + 4, 4);
188+
for (int r = FEISTEL_ROUNDS - 1; r >= 0; r--) {
189+
uint32_t temp = left;
190+
left = right ^ feistel_round(left, key, r);
191+
right = temp;
192+
}
193+
memcpy(data + i, &left, 4);
194+
memcpy(data + i + 4, &right, 4);
195+
}
196+
}
197+
198+
// ===========================================================
199+
// Advanced Key Conversion Utility (FNV-1a + extra mixing)
200+
// ===========================================================
201+
static uint32_t key_to_u32(const char* key) {
202+
uint32_t hash = 0x811C9DC5;
203+
for (const char* p = key; *p; p++)
204+
hash = (hash ^ (uint8_t)*p) * 16777619u;
205+
// Extra mixing for advanced key schedule
206+
hash ^= (hash << 13);
207+
hash ^= (hash >> 7);
208+
hash ^= (hash << 17);
209+
return hash;
210+
}
211+
212+
static uint64_t key_to_u64(const char* key) {
213+
uint64_t hash = 0xCBF29CE484222325ull;
214+
for (const char* p = key; *p; p++)
215+
hash = (hash ^ (uint8_t)*p) * 1099511628211ull;
216+
// Extra mixing for advanced key schedule
217+
hash ^= (hash << 21);
218+
hash ^= (hash >> 35);
219+
hash ^= (hash << 4);
220+
return hash;
221+
}
222+
223+
// ===========================================================
224+
// Main Entry Point (advanced selection and error handling)
225+
// ===========================================================
226+
int fossil_cryptic_cipher_compute(
227+
const char* algorithm,
228+
const char* mode,
229+
const char* bits,
230+
const char* key,
231+
const void* input, size_t input_len,
232+
void* output, size_t* output_len
233+
) {
234+
if (!algorithm || !mode || !bits || !key || !input || !output || !output_len)
235+
return -1;
236+
237+
int is_encrypt = 1;
238+
if (strcmp(mode, "decrypt") == 0) is_encrypt = 0;
239+
else if (strcmp(mode, "encrypt") == 0) is_encrypt = 1;
240+
else if (strcmp(mode, "auto") != 0) return -2;
241+
242+
// Use advanced key conversion utilities
243+
uint32_t k32 = key_to_u32(key);
244+
uint64_t k64 = key_to_u64(key);
245+
246+
// Algorithm selection (advanced: fallback and auto)
247+
if (strcmp(algorithm, "xor") == 0 || strcmp(algorithm, "auto") == 0) {
248+
cipher_xor((const uint8_t*)&k64, sizeof(k64),
249+
(const uint8_t*)input, (uint8_t*)output, input_len);
250+
*output_len = input_len;
251+
}
252+
else if (strcmp(algorithm, "feistel") == 0) {
253+
if (is_encrypt)
254+
cipher_feistel_encrypt((uint8_t*)output, input_len, k32);
255+
else
256+
cipher_feistel_decrypt((uint8_t*)output, input_len, k32);
257+
*output_len = input_len;
258+
}
259+
else if (strcmp(algorithm, "caesar") == 0) {
260+
if (is_encrypt)
261+
cipher_caesar((const uint8_t*)&k32, sizeof(k32),
262+
(const uint8_t*)input, (uint8_t*)output, input_len);
263+
else
264+
cipher_caesar_decrypt((const uint8_t*)&k32, sizeof(k32),
265+
(const uint8_t*)input, (uint8_t*)output, input_len);
266+
*output_len = input_len;
267+
}
268+
else if (strcmp(algorithm, "vigenere") == 0) {
269+
if (is_encrypt)
270+
cipher_vigenere((const uint8_t*)&k64, sizeof(k64),
271+
(const uint8_t*)input, (uint8_t*)output, input_len);
272+
else
273+
cipher_vigenere_decrypt((const uint8_t*)&k64, sizeof(k64),
274+
(const uint8_t*)input, (uint8_t*)output, input_len);
275+
*output_len = input_len;
276+
}
277+
else if (strcmp(algorithm, "morse") == 0) {
278+
if (is_encrypt) {
279+
cipher_morse_encode((const uint8_t*)input, (uint8_t*)output, input_len);
280+
*output_len = strlen((const char*)output);
281+
} else {
282+
cipher_morse_decode((const uint8_t*)input, (uint8_t*)output, input_len);
283+
*output_len = strlen((const char*)output);
284+
}
285+
}
286+
else {
287+
// Advanced: fallback to XOR if unsupported algorithm
288+
cipher_xor((const uint8_t*)&k64, sizeof(k64),
289+
(const uint8_t*)input, (uint8_t*)output, input_len);
290+
*output_len = input_len;
291+
}
292+
293+
return 0;
294+
}

0 commit comments

Comments
 (0)