aboutsummarylogtreecommitdiffstats
path: root/rc4.h
blob: 7f6a9eec428f5cfddb9f85732de7769e845df281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef RC4_H
#define RC4_H

#define PASS_MAX 32
#define BACKSPACE 127
#define KEY_SCHEDULE_LENGTH 256
#define DECRYPT 0
#define ENCRYPT 1

#include <ncurses.h>
#include <json-c/json_tokener.h>
#include <unistd.h>
#include "string-tick.h"

/**
 * Returns a password string taken from user input with a maximum of 32 characters. The user's input
 * should not be echoed in the terminal, so ncurses noecho() function should be used.
 * @return the password taken from input
 */
char* rc4_getPassword(void);

/**
 * Precondition: keySchedule is an int array of size 256 and key is a valid string
 * Postcondition: keySchedule values have been initialized by the RC4 key-scheduling algorithm.
 * Performs the RC4 key exchange with keylength 256. Pseudocode example:
 * for i from 0 to 255
 *   S[i] := i
 * endfor
 * j := 0
 * for i from 0 to 255
 *   j := (j + S[i] + key[i mod keylength]) mod 256
    swap values of S[i] and S[j]
 * endfor
 *
 * @param keySchedule int array of size keylength
 * @param key user given password
 */
void rc4_key_exchange(int keySchedule[KEY_SCHEDULE_LENGTH], char* key);

/**
 * Precondition: keySchedule is an int array of size 256 already initialized by the RC4 key-scheduling algorithm
 * Performs the RC4 pseudo-random generation algorithm with keylength 256. Returns a string of length len with output
 * frm this algorithm. Pseudocode example:
 * i := 0
 * j := 0
 * while GeneratingOutput:
 *   i := (i + 1) mod 256
 *   j := (j + S[i]) mod 256
 *   swap values of S[i] and S[j]
 *   K := S[(S[i] + S[j]) mod 256]
 *   output K
 * endwhile
 *
 * @param keySchedule int array of size keylength (already initialized)
 * @param len size of portfolio in bytes
 * @return string of size len that can be XOR'ed with message to be en/decoded
 */
char* rc4_prga(int keySchedule[KEY_SCHEDULE_LENGTH], size_t len);

/**
 * Precondition: output is a string containing the output of the RC4 pseudo-random generation algorithm. pString
 * is a String object containing the portfolio.
 * XOR's the each index of output by each index of the char* in pString and stores that in the index of output
 * @param output string
 * @param message portoflio String
 */
void rc4_execute(char* output, String* pString);

/**
 * Returns an encoded string given a String object and password used to encode it.
 * @param pString The String to encode.
 * @param password the key used to encode
 * @return encoded string
 */
char* rc4_encode_string(String* pString, char* password);

/**
 * Given input input_pString, returns an encrypted or decrypted version of it. If trying to encrypt a non-JSON
 * formatted input_pString, abort. Similarly, if trying to decrypt a JSON-formatted input_pString, abort. password may
 * be a string or NULL. If NULL, get a password from the user if encrypting, ask for the password again to confirm
 * inputs are the same. If decrypting and the decrypted String is not JSON formatted, abort (wrong password).
 * @param input_pString the String to encrypt/decrypt
 * @param password the key or NULL
 * @param option ENCRYPT or DECRYPT
 * @return encrypted or decrypted String
 */
String* rc4_get_crypted_string(String* input_pString, char* password, int option);

#endif