Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 03, 2016, 10:31:45 10:31


Login with username, password and session length


Pages: [1]
Print
Author Topic: Stream cipher for CortexM4  (Read 881 times)
0 Members and 1 Guest are viewing this topic.
dotm
Active Member
***
Offline Offline

Posts: 149

Thank You
-Given: 61
-Receive: 47


C15H15NO2S


« on: September 07, 2014, 02:58:29 14:58 »

Hi.
I'm recently looking for a fast stream cipher implementation in C. It should:
- use very little RAM
- be quite fast
- its implementation should be real single value in&out. Not a pointer to a buffer, but char in - char out.
- any needed prng should be delivered as well and also use very little ram.
- strength must not be very high, but nothing like rot13 Smiley
any recommendations?
Logged
pablo2048
Active Member
***
Offline Offline

Posts: 103

Thank You
-Given: 96
-Receive: 82


« Reply #1 on: September 07, 2014, 03:37:14 15:37 »

I think there is no algorithm for "real single value in&out"  - so i suggest xtea - it's very fast, no known attack...
Logged
dotm
Active Member
***
Offline Offline

Posts: 149

Thank You
-Given: 61
-Receive: 47


C15H15NO2S


« Reply #2 on: September 07, 2014, 04:11:16 16:11 »

I think there is no algorithm for "real single value in&out"  - so i suggest xtea - it's very fast, no known attack...

Xtea is a block cipher according to wikipedia.
Logged
pablo2048
Active Member
***
Offline Offline

Posts: 103

Thank You
-Given: 96
-Receive: 82


« Reply #3 on: September 07, 2014, 04:46:48 16:46 »

Xtea is a block cipher according to wikipedia.
Yes it is - I think that You can implement stream cipher over block cipher (if You don't want to end with RC4, or ordinary XOR function)...
Logged
an007_rld
Newbie
*
Offline Offline

Posts: 21

Thank You
-Given: 18
-Receive: 14


« Reply #4 on: September 07, 2014, 05:01:50 17:01 »

Hi dotm,
Any char based encryption is more or less a joke; what can you do? ... XOR maybe?

If you need encryption please consider block based encryption even the block size it's small...

This is my 5cents advice...

-an
Logged
dotm
Active Member
***
Offline Offline

Posts: 149

Thank You
-Given: 61
-Receive: 47


C15H15NO2S


« Reply #5 on: September 07, 2014, 05:46:17 17:46 »

Hi dotm,
Any char based encryption is more or less a joke; what can you do? ... XOR maybe?

If you need encryption please consider block based encryption even the block size it's small...

This is my 5cents advice...

-an

Are you serious? A stream cipher works as well as block cipher. There are tons of stream cipher out there, i just wanted to avoid rewriting any of them from buffer in/out to char in/out.
Logged
wild
V.I.P
Active Member
*****
Offline Offline

Posts: 113

Thank You
-Given: 274
-Receive: 327



« Reply #6 on: September 07, 2014, 07:11:21 19:11 »

Even if really simple, can this be interesting?
http://partners.adobe.com/public/developer/en/font/T1_SPEC.PDF (pages 67-71 of PDF, 61-65 of the document)
it uses very low resources!
« Last Edit: September 07, 2014, 07:13:41 19:13 by wild » Logged
str67
Newbie
*
 Warned
Offline Offline

Posts: 25

Thank You
-Given: 29
-Receive: 23


« Reply #7 on: September 08, 2014, 08:27:30 08:27 »

Hi dotm,

RC4 is likely the most widely used stream cipher. I found an implementation in C on the web which seems to me quite usable for your purposes.

Code:
#include <stdio.h>
#include <stdint.h>
#include <assert.h>


typedef uint8_t byte;
typedef struct
{
    byte i, j;
    byte S[256];
} Rc4State;

void swap(byte *a, byte *b)
{
    byte temp = *a;
    *a = *b;
    *b = temp;
}

/*Initialization & initial permutation
also initialize i&j counters in state for stream generation*/
void initState(const byte K[256], int keylen, Rc4State *state)
{
    byte T[256];
    assert(keylen>=1 && keylen<=256);
    int i;
    for(i = 0; i < 256; i++) {
        state-> S[i] = i;
        T[i] = K[i % keylen];
    }

    //Initial permutation of S
    byte *S = state->S;
    int j = 0;
    for(i = 0; i < 256; i++) {
        j = (j + S[i] + T[i]) % 256;
        swap(&S[i], &S[j]);
    }

    //Initialize counters in state
    state->i = state->j = 0;
}

/*Encrypt/Decrypt text by XORing with next byte of keystream*/
byte crypt(byte text, Rc4State *state)
{
    byte t, k;
    byte *i = &(state->i), *j = &(state->j);
    byte *S = state->S;
    *i = (*i+1) % 256;
    *j = (*j+S[*i]) % 256;
    swap(&S[*i], &S[*j]);
    t = (S[*i] + S[*j]) % 256;
    k = S[t];

    return text ^ k;
}

Hope this helps...
str
Logged
dotm
Active Member
***
Offline Offline

Posts: 149

Thank You
-Given: 61
-Receive: 47


C15H15NO2S


« Reply #8 on: September 08, 2014, 09:41:06 09:41 »

I found an implementation in C on the web which seems to me quite usable for your purposes.

i can't believe how simple that looks Smiley
This one uses 512 bytes memory at least. That is acceptable but not good.
I ask myself which one of these huge arrays can be made const...
« Last Edit: September 08, 2014, 09:43:56 09:43 by dotm » Logged
str67
Newbie
*
 Warned
Offline Offline

Posts: 25

Thank You
-Given: 29
-Receive: 23


« Reply #9 on: September 08, 2014, 01:56:19 13:56 »

I think the minimum memory you will need for RC4 will be around 258 byte (state memory) plus k byte for the key (i.e. 8 byte for 256-bit).
In case you fix the key length you can fill up the initial state from your key data directly, without the need for the temporary array T.
That would result more or less in the minimum data memory footprint for the code.

In case that still is too much for your application, a modified cipher with less security might be an option (smaller state variable?).

str
Logged
pablo2048
Active Member
***
Offline Offline

Posts: 103

Thank You
-Given: 96
-Receive: 82


« Reply #10 on: September 08, 2014, 02:58:23 14:58 »

I think the minimum memory you will need for RC4 will be around 258 byte (state memory) plus k byte for the key (i.e. 8 byte for 256-bit).
In case you fix the key length you can fill up the initial state from your key data directly, without the need for the temporary array T.
That would result more or less in the minimum data memory footprint for the code.

In case that still is too much for your application, a modified cipher with less security might be an option (smaller state variable?).

str

Key can be stored in Flash memory. The only static RAM used is RC4 context (RC4state), work buffer (T) is allocated dynamically on the stack and AFAIK this
Code:
j = (j + S[i] + T[i]) % 256;
can be written as
Code:
j = (j + S[i] + K[i % keylen]) % 256;
thus eliminating T buffer completely...
« Last Edit: September 08, 2014, 03:39:30 15:39 by pablo2048 » Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC