/usr/share/doc/libtwofish-dev/examples/simple-internal-test.c is in libtwofish-dev 0.3-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* simple.c
*
* Single block encryption, and ensuing decryption.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <twofish.h>
#define INDATA "tre fula fiskar "
#ifndef FIXEDKEYSTR
# define FIXEDKEYSTR "fisker"
#endif
void to_hex(char hex[2], int ch) {
char nibble = ch & 0x0f;
hex[0] = '0' + nibble;
if ( nibble > 0x09 )
hex[0] += 'a' - '0' - 0x0a;
nibble = ch >> 4;
hex[1] = '0' + nibble;
if ( nibble > 0x09 )
hex[1] += 'a' - '0' - 0x0a;
return;
}
int main(int argc, char * argv[]) {
int keylen, j, rounds = 1, failures = 0;
int first_time = 1;
char hex[2];
Twofish_Byte key[32];
Twofish_key xkey;
Twofish_Byte inblock[16], intermediary[16], outblock[16];
#ifndef FIXEDKEY
const int multiple = sizeof(key) / sizeof(long int);
long int seed;
keylen = multiple * sizeof(long int);
#endif
j = 1;
while ( j < argc ) {
if ( strcmp(argv[j], "-h") == 0 ) {
#ifndef FIXEDKEY
printf("Usage: %s [runs]\n\t%s -h\n\n"
"Here 'runs' is the desired number of"
" encryption rounds.\n"
"Maximum is set to 50 rounds.\n",
argv[0], argv[0]);
#else
printf("Usage: %s\n\t%s -h\n\n",
argv[0], argv[0]);
#endif
return 0;
}
rounds = atoi(argv[j]);
if (rounds < 1)
rounds = 1;
else if (rounds > 50)
rounds = 50;
j++;
}
srandom(time(NULL));
Twofish_initialise();
memset(key, 0, sizeof(key));
memset(inblock, 0, sizeof(inblock));
memcpy(inblock, INDATA, sizeof(inblock));
failures = rounds;
while ( rounds ) {
#if FIXEDKEY
memcpy(key, FIXEDKEYSTR, sizeof(FIXEDKEYSTR));
/* Override values. */
keylen = sizeof(FIXEDKEYSTR);
rounds = 1;
failures = rounds;
#else
for (j=0; j < multiple; j++) {
seed = random();
memcpy(key + j * sizeof(seed), &seed, sizeof(seed));
}
#endif
Twofish_prepare_key(key, keylen, &xkey);
Twofish_encrypt(&xkey, inblock, intermediary);
Twofish_decrypt(&xkey, intermediary, outblock);
if ( first_time ) {
printf("Called for %d rounds. "
"Intermediary blocks:\n\t", rounds);
first_time = 0;
} else
printf("\t");
for (j=0; j<16; j++) {
to_hex(hex, intermediary[j]);
printf("%c%c", hex[1], hex[0]);
}
printf("\n");
/* Check if successful crypto-action. */
if ( memcmp(inblock, outblock, sizeof(inblock)) == 0 )
failures--;
rounds--;
}
if ( failures == 0 ) {
puts("\nEncryption was reversible. Luckily!");
return 0;
} else {
printf("\nFailure: %d failures between encryption and decryption!",
failures);
return 1;
}
}
|