RetroBSD

2.11BSD operating system for microcontrollers
It is currently Tue Apr 07, 2020 7:00 am

All times are UTC




Post new topic Reply to topic  [ 26 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: RC4 encr/decr
PostPosted: Sat Oct 10, 2015 3:35 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
This is an RC4 DropN encr/decr.
Code:
// rc4dn.c
// RC4 encryption/decryption
// RC4 stream ciphre, dropN modification
// Use: rc4 file encrfile key
//
// For example:
// rc4 myfile encrmyfile QWERTY2387
// rc4 encrmyfile myfile QWERTY2387
//
// Compiled under SmallerC
// Pito 11/2013 and 10/2015 based on available www sources

#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>

#define ARRAY_LENGTH   256

unsigned char s[ARRAY_LENGTH];
int rc4_i;
int rc4_j;

void swap(unsigned char *s1, unsigned char *s2)
{
   char temp = *s1;
   *s1 = *s2;
   *s2 = temp;
}

int InitRC4(void)
{
   int i;
   for(i=0 ; i< ARRAY_LENGTH; i++)
      s[i] = i;
   rc4_i = rc4_j = 0;
   return 1;
}

int DoKSA(unsigned char *key, int key_len)
{
   int n;
   for(rc4_i = 0; rc4_i < ARRAY_LENGTH; rc4_i++)
   {
      rc4_j = (rc4_j + s[rc4_i] + key[rc4_i % key_len])% ARRAY_LENGTH;
      swap(&s[rc4_i], &s[rc4_j]);
   }
   
   // Reset counters for Prga
   rc4_i = rc4_j = 0;

   // Drop first N=1024 bytes of the key stream
   for(n = 0; n < 1024; n++)
   {
      rc4_i = (rc4_i + 1 ) % ARRAY_LENGTH;
      rc4_j = (rc4_j + s[rc4_i]) % ARRAY_LENGTH;
      swap(&s[rc4_i], &s[rc4_j]);
   }
}

char GetPRGAOutput(void)
{
   rc4_i = (rc4_i +1 ) % ARRAY_LENGTH;
   rc4_j = (rc4_j + s[rc4_i]) % ARRAY_LENGTH;
   swap(&s[rc4_i], &s[rc4_j]);
   return s[(s[rc4_i] + s[rc4_j]) % ARRAY_LENGTH];
}

main(int argc, char **argv)
{
   FILE * file_to_encrypt;
   FILE * encrypted_output_file;
   unsigned char *encryption_key;
   int encryption_key_length;
   unsigned char data_byte;
   unsigned char keystream_byte;
   unsigned char encrypted_byte;
   int bytes_read = 1;

        if ( argc < 4) {
              //printf("Too few arguments\n");
               exit(-1);
        }

   file_to_encrypt = fopen(argv[1], "rb");
   if(file_to_encrypt == NULL)
   {
      //printf("Cannot open file: %s\n", argv[1]);
      exit(-1);
   }
   
   encrypted_output_file = fopen(argv[2], "wb");
   if(encrypted_output_file == NULL)
   {
      //printf("Cannot open output file : %s\n", argv[2]);
      exit(-1);
   }

   encryption_key = (unsigned char *)argv[3];
   encryption_key_length = strlen((char *)encryption_key);

   if(encryption_key_length > ARRAY_LENGTH)
   {
      //printf("Key too large. should be <= 256 charachters\n");
      exit(-1);
   }

   //printf("Initializing the RC4 array\n");
   InitRC4();
   //printf("Starting KSA \n");
   DoKSA(encryption_key, encryption_key_length);

   //printf("Opening Files for encryption phase\n");
   while((bytes_read = fread(&data_byte, sizeof(char), sizeof(data_byte), file_to_encrypt)) > 0)
   {
      // Get A KeyStream byte from PRGA
      keystream_byte = GetPRGAOutput();
      encrypted_byte = data_byte ^ keystream_byte;  // XOR
      fwrite(&encrypted_byte, sizeof(char), sizeof(encrypted_byte), encrypted_output_file);      
   }
   
   //printf("Done ....exiting\n\n");
   fclose(file_to_encrypt);
   fclose(encrypted_output_file);
   return 1;
}

Code:
# cc -o rc4 rc4dn.c
# ./rc4 rc4dn.c enrc4dn.c QWERTY
# ./rc4 enrc4dn.c myfile.c QWERTY

myfile.c and rc4dn.c shall be identical, if lucky :)
Code:
# dd bs=1k count=1024 if=/dev/zero of=myfile
1024+0 records in
1024+0 records out
# time ./rc4 myfile encfile QWERTY1234
        8.2 real         4.8 user         3.1 sys
# time ./rc4 encfile myfile1 QWERTY1234
        8.5 real         4.8 user         3.1 sys
# wc encfile
    4115   11830 1048576 encfile
# wc myfile
       0       0 1048576 myfile
# wc myfile1
       0       0 1048576 myfile1
#

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Sat Oct 10, 2015 4:49 pm 
Contributor

Joined: Mon Nov 12, 2012 1:34 pm
Posts: 1092
Hi Pito,

VERY NICE.

I was just wondering about encryption/decryption.

I suppose with EF chip some things or additional encryption could be done in hardware.

Lots of fun :).

Wiz


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Sat Oct 10, 2015 5:14 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
If somebody thinks RC4 is easy to crack - attached a text file encrypted with above code.
So let us try to decrypt :)


Attachments:
myfile.zip [1.77 KiB]
Downloaded 522 times

_________________
Pukao Hats Cleaning Services Ltd.
Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Sat Oct 10, 2015 7:36 pm 
Contributor

Joined: Mon Nov 12, 2012 1:34 pm
Posts: 1092
Hi Pito,

Nice.

LOL - Looks like Windoze code to me :)

Wiz


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Sun Oct 11, 2015 8:51 pm 
Committer

Joined: Thu Nov 08, 2012 3:55 am
Posts: 177
Pito, it is your to your credit that I've spent the last day or so playing with RC4. Thanks for the puzzle.

Assuming my assumptions about the features of the file you encrypted are correct (no embedded nulls and no chars with values > 127), and that I have not not made some coding errors along the way, I can say the following about your chosen password:

  • It does not appear in several large lists of passwords that I've found online (or simple variations such as all upper/lower or initial cap).
  • Assuming an alphabet of mixed-case alpha, digits, and most printable 7 bit ascii chars, it is at least 5 chars long.
  • Assuming an alphabet of uppercase alpha and digits, it is at least 7 chars long.

RC4 does have at least one serious flaw. If you know the cypertext and plaintext of a document of length L that was encrypted with password X, you can trivially decrypt the first L bytes of any document encrypted with password X (even though you don't know what the value of password X is). So one really does not want to reuse the same password to encrypt different documents.

Some references:


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Sun Oct 11, 2015 9:18 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
Hi madscifi, I will provide you with some hints:
1. the password is 14 chars long
2. the password consist of 2 standard words (capital letters, 4chars and 5chars) and one number 5 digits long, no special chars.
3. the plain text is in english, it is a technical text related to our hobby.
;)

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Mon Oct 12, 2015 5:21 am 
Committer

Joined: Thu Nov 08, 2012 3:55 am
Posts: 177
Even with all of those restrictions, it would take a bit over 500 days to exhaustively test (assuming the 6071 5-char words and 3164 4-char words out of the English dictionary list on Debian) on my desktop using brute force.

Using a list of the top 10000 US English words instead of the much longer dictionary list cuts the number of words to 1125 4-char words and 1385 5-char words to try. Hopefully that does not remove the actual words that you have used. Moving to a somewhat newer laptop and splitting the work across the 4 effective cores and the total time to try the entire key space is now just a bit less than 6 days. That seems a bit more reasonable...

What this really means, as far as I am concerned, is that is probably time for me to retire my current desktop as it is getting a bit long in the tooth.


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Mon Oct 12, 2015 5:28 am 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
The two words are absolutely top english and international as well words :)
The speed of decryption is important as the message may loose its importance (not in our case probably) :P

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Mon Oct 12, 2015 8:28 am 
Contributor

Joined: Mon Nov 12, 2012 1:34 pm
Posts: 1092
Hi Madscifi,

RC4 has this flaw....

What would you use/recommend/suggest?

Simple to code, but reasonably secure?

I guess maybe add some sort of password variation scheme?

Wiz


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Mon Oct 12, 2015 5:48 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
Hi Wiz, RC4 is reasonable secure and fastest and simplest. When not sending over hundreds millions identical plain texts encrypted with different keys (ie your cookies or passwords over some by RC4 encr protocols..) you are on the safe side :). The flaw does exist, but cracking RC4 is rather an academic exercise when talking a moderate retrobsd user and his apps.. Unless your password is a crap or known, nobody can decrypt your data within reasonable time and effort.

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Tue Oct 13, 2015 9:47 am 
Contributor

Joined: Mon Nov 12, 2012 1:34 pm
Posts: 1092
Hi Pito,

Good points :).

Now can we get your PIC32 talking to my PIC32 over the web encrypted?

I bet we can !!

It would seem that appending the date to whatever password would solve most problems? So the real key would change from time to time?

But first we gotta get connected.

I notice that the MZ...EF does not seem to have the MX clock problem [funny bypass cap]. So I think it might be a better chip to 'standardize' on.

I wonder if Microchip will fix the MX clock problem? Maybe they already have??

I still like the idea of a cheap phone as the display. I bet we could send serial data into the phone via the 5th usb wire. Who knows maybe the phone can also output data there. I guess only time will tell. We just need some real schematics. I bet they are already on the web somewhere!

Lots of fun :).

Wiz


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Tue Oct 13, 2015 10:05 am 
Committer
User avatar

Joined: Thu Oct 11, 2012 8:45 am
Posts: 1801
Location: Room 217, Floor 8, Arm 8, Wheel S7, Mars Base Alpha 3
Quote:
I bet we could send serial data into the phone via the 5th usb wire. Who knows maybe the phone can also output data there

I have already told you, you can't do that. The 5th wire is a passive pullup / pulldown only to place the MCU in one of two states.

_________________
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Wed Oct 14, 2015 5:40 am 
Contributor

Joined: Mon Nov 12, 2012 1:34 pm
Posts: 1092
Hi Matt,

I wasn't trying to contradict what you said was the current usage.

I seems to me that the phone must be able to read that wire.

So if the outside device wiggles that wire, the phone cpu must be able to see it.

So serial input to the phone 'must' be possible.

And if that wire is like many other CPU pins, it can [perhaps] be turned into an output? I suppose this depends on whether 'protection' or hardware on the CPU chip prevents this from happening?

I would be surprised if such two way operation is not part of the bigger USB plan?

I was hoping some soul out there might know the innards of phones enough to be able to answer the question.

Lots of fun :).

Wiz


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Wed Oct 14, 2015 10:48 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
Wiz, the easiest way to interconnect your pic32 w/ pic32 or your phone is via ESP8266 wifi module. It has got uart interface. It works with arduino or standalone as well, so it will work with pic32 or retrobsd too..
It costs $2.66 incl. shipping http://www.ebay.com/sch/i.html?_from=R4 ... le&_sop=12

Quote:
It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.

ESP8266 has powerful on-board processing and storage capabilities that allow it to be integrated with the sensors and other application specific devices through its GPIOs with minimal development up-front and minimal loading during runtime. Its high degree of on-chip integration allows for minimal external circuitry, and the entire solution, including front-end module, is designed to occupy minimal PCB area.

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Wed Oct 14, 2015 11:13 pm 
Committer
User avatar

Joined: Thu Oct 11, 2012 8:45 am
Posts: 1801
Location: Room 217, Floor 8, Arm 8, Wheel S7, Mars Base Alpha 3
Why not port RetroBSD to the XTensa core?

_________________
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Wed Oct 14, 2015 11:26 pm 
Contributor
User avatar

Joined: Thu Nov 08, 2012 7:04 am
Posts: 2401
Location: Rapa Nui
When Cadence acquires ImaginationTech :) They share the same parking places at Scott Boulevard, S.C. :P

_________________
Pukao Hats Cleaning Services Ltd.


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Thu Oct 15, 2015 1:29 am 
Committer
User avatar

Joined: Wed Oct 10, 2012 11:01 pm
Posts: 1081
Location: Sunnyvale, CA
Pito wrote:
When Cadence acquires ImaginationTech :) They share the same parking places at Scott Boulevard, S.C. :P

Well, not exactly... :)
Cadence is about 4 miles, 10min driving time from Imagination.


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Thu Oct 15, 2015 1:31 am 
Committer
User avatar

Joined: Wed Oct 10, 2012 11:01 pm
Posts: 1081
Location: Sunnyvale, CA
majenko wrote:
Why not port RetroBSD to the XTensa core?

Probably nobody is interested enough.
Is there any suitable chip with XTensa core avalable?


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Thu Oct 15, 2015 9:41 am 
Contributor

Joined: Mon Nov 12, 2012 1:34 pm
Posts: 1092
Hi Matt,

Yup. That chip looks interesting. The only possible problem I have with it is that I would like to find a low level WIFI chip.

One that allows me to process incoming RF and put out whatever RF I would like. Clearly the chip, except for bandwidth problems, can/does do that. One day a fast FPGA will probably be able to do that also?

That said, It would be much easier to just plug the PIC32 into the 'phone' and get both power and low speed comms that way.

I have this nasty feeling that [many/most?] WIFI chips have 'extra' features hidden inside? Given the 'wake on lan' business of ethernet fame, I assume many more such features have been added to the RF path where they are VERY difficult to discover and disable.

Just my own paranoia :).

Lots of fun :).

Wiz


Top
 Profile  
 
 Post subject: Re: RC4 encr/decr
PostPosted: Thu Oct 15, 2015 12:08 pm 
Committer
User avatar

Joined: Thu Oct 11, 2012 8:45 am
Posts: 1801
Location: Room 217, Floor 8, Arm 8, Wheel S7, Mars Base Alpha 3
"One day"? They already do.

I have already pointed you towards the term you need to google: SDR - Software Defined Radio. A chip that you program to send and receive RF signals in the way that you want. That is how many of the cheap RF devices operate.

CSR (Cambridge Silicon Radio) are one of the world leader's in that field.

_________________
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page 1, 2  Next

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron




Powered by phpBB® Forum Software © phpBB Group

BSD Daemon used with permission