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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*************************************************************************
 *
 *  Project
 *                         _____ _____  __  __ _____
 *                        / ____|  __ \|  \/  |  __ \
 *  ___  _ __   ___ _ __ | |  __| |__) | \  / | |__) |
 * / _ \| '_ \ / _ \ '_ \| | |_ |  ___/| |\/| |  ___/
 *| (_) | |_) |  __/ | | | |__| | |    | |  | | |
 * \___/| .__/ \___|_| |_|\_____|_|    |_|  |_|_|
 *      | |
 *      |_|
 *
 * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
 *
 * This software is licensed as described in the file LICENSE, which
 * you should have received as part of this distribution. The terms
 * among other details are referenced in the official documentation
 * seen here : https://akielaries.github.io/openGPMP/ along with
 * important files seen in this project.
 *
 * You may opt to use, copy, modify, merge, publish, distribute
 * and/or sell copies of the Software, and permit persons to whom
 * the Software is furnished to do so, under the terms of the
 * LICENSE file. As this is an Open Source effort, all implementations
 * must be of the same methodology.
 *
 *
 *
 * This software is distributed on an AS IS basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied.
 *
 ************************************************************************/

/*
 * This file shows the implementation of the Rivest Cipher 4 (RC4)
 * encryption algorithm created by Ron Rivest using either a
 * traditional swap method or with the XOR operator
 */
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <openGPMP/nt/rc4.hpp>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

void gpmp::RC4::byte_swap(uint8_t *a, uint8_t *b) {
    uint8_t swapped = *a;<--- swapped is initialized
    swapped = *a;<--- swapped is overwritten
    *a = *b;
    *b = swapped;
}

void gpmp::RC4::trad_swap(unsigned char *a, unsigned char *b) {
    int swapped = *a;
    *a = *b;
    *b = swapped;
}

void gpmp::RC4::XOR_swap(unsigned char *a, unsigned char *b) {
    *a ^= *b;
    *b ^= *a;
    *a ^= *b;
}

void gpmp::RC4::KSA(char *key, unsigned char *S, int swap_type) {
    uint32_t len = strlen(key);
    int j = 0;

    for (int i = 0; i < BYTE_LIMIT; i++) {
        S[i] = i;
    }

    for (int i = 0; i < BYTE_LIMIT; i++) {
        j = (j + S[i] + key[i % len]) & BITS;

        // choose swap algorithm based off swap_type
        if (swap_type == 0) {
            XOR_swap(&S[i], &S[j]);
        } else if (swap_type == 1) {
            trad_swap(&S[i], &S[j]);
        } else if (swap_type == 2) {
            byte_swap(&S[i], &S[j]);
        }
    }
}

void gpmp::RC4::PRGA(unsigned char *S,
                     char *plaintext,
                     unsigned char *ciphertext,
                     int swap_type) {
    int i = 0;
    int j = 0;

    for (size_t n = 0, len = strlen(plaintext); n < len; n++) {
        i = (i + 1) & BITS;
        j = (j + S[i]) & BITS;

        // choose swap algorithm based off swap_type
        if (swap_type == 0) {
            XOR_swap(&S[i], &S[j]);
        } else if (swap_type == 1) {
            trad_swap(&S[i], &S[j]);
        } else if (swap_type == 2) {
            byte_swap(&S[i], &S[j]);
        }

        uint32_t rnd = S[(S[i] + S[j]) & BITS];

        ciphertext[n] = rnd ^ plaintext[n];
    }
}

std::string
gpmp::RC4::store_hash(char *plaintext, unsigned char *hashtext, int swap_type) {
    // length of our plaintext
    size_t len = strlen((char *)plaintext);
    // for snprintf declare a buffer
    char buffer[len + 1];
    size_t size = sizeof(buffer);

    // initialize empty string
    std::string stored_text = "";

    // traverse the hashtext appending each block to a string
    for (size_t index = 0; index < len; index++) {
        if (swap_type == 0) {
            snprintf(buffer, size, "|x%02hhx|", hashtext[index]);
        }
        // to format our hash to a string for future manipulation
        else {
            snprintf(buffer, size, "%02hhX", hashtext[index]);
        }
        // append the declared string
        stored_text += std::string(buffer);
    }
    return stored_text;
}

unsigned char *gpmp::RC4::compute(char *key,
                                  char *plaintext,
                                  unsigned char *ciphertext,
                                  int swap_type) {
    if (ciphertext == NULL) {
        throw std::runtime_error("[-] Error Allocating Memory");
    }

    // check for swap types 1-3
    if (0 <= swap_type && swap_type <= 2) {
        unsigned char S[BYTE_LIMIT];
        KSA(key, S, swap_type);
        PRGA(S, plaintext, ciphertext, swap_type);
    }

    else if (swap_type > 2) {
        throw std::runtime_error("[-] Invalid swap_type");
    }

    return ciphertext;
}