A simple block cipher based on the SHA-256 hash function [closed] - hash

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I've come up with this little routine for doing encryption using the SHA-2 (in this case SHA-256) hash function. As such it is a block cipher with a 256 bit (32 byte) block size and an arbitrary key length. It's easy to see that you could replace the hash function and get an entirely new type of algorithm.
I want to know if there are any problems, especially flaws, in this construction, and whether algorithms of this type have been studied.
#include <openssl/sha.h>
struct sha_crypt_state {
unsigned char digest[SHA256_DIGEST_LENGTH];
};
void sha_crypt_set_key( sha_crypt_state *state, unsigned char *key, int key_length )
{
SHA256( key, key_length, state->digest );
}
void sha_crypt( sha_crypt_state *state, unsigned char *block )
{
sha_crypt_state temp;
SHA256( state->digest, sizeof(state->digest), temp.digest );
memcpy( state->digest, temp.digest, sizeof(temp.digest) );
for ( int i = 0; i < sizeof(state->digest); ++i ) {
block[i] ^= state->digest[i];
}
}
void sha_crypt_test()
{
const char *key = "secret";
// prepare a test block
char block[SHA256_DIGEST_LENGTH];
memset( block, 0, sizeof(block) );
strcpy( block, "Hello, testing encryption!" );
// test encrypt
sha_crypt_state state;
set_key( &state, (unsigned char *)key, strlen(key) );
sha_crypt( &state, (unsigned char *)block );
// test decrypt
set_key( &state, (unsigned char *)key, strlen(key) );
sha_crypt( &state, (unsigned char *)block );
}

One obvious weakness is that if I can guess the plaintext of any block - for example if you are encrypting a Word document, which has a static header - I can decrypt all of the following blocks (just xor the plaintext with the ciphertext to yield the state).
Block ciphers based on hash functions usually uses just the compression function of the hash algorithm. One example is SHACAL-2 which is based on SHA-256 like your algorithm.

Related

Can't write Double word on STM32F429 using HAL driver

I am trying to write uint64_t(double word) variable into the flash memory, without success though. Here is the code.
#define APPLICATION_START_ADDRESS 0x8008000
void flashErase(uint8_t startSector, uint8_t numberOfSectors)
{
HAL_FLASH_Unlock();
Flash_eraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
Flash_eraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
Flash_eraseInitStruct.Sector = startSector;
Flash_eraseInitStruct.NbSectors = numberOfSectors;
if(HAL_FLASHEx_Erase(&Flash_eraseInitStruct, &Flash_halOperationSectorError) != HAL_OK)
{
Flash_raiseError(errHAL_FLASHEx_Erase);
}
HAL_FLASH_Lock();
}
int main(void)
{
HAL_Init();
main_clockSystemInit();
__IO uint64_t word = 0x1234567890;
flashErase(2, 1);
// flashProgramWord(aTxBuffer, APPLICATION_START_ADDRESS, 2 );
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, APPLICATION_START_ADDRESS, word);
}
I get error flag raised PGSERR and PGAERR. The erase operation goes without problems. But programming returns ERROR.
Some Ideas?
There is no STM32F249, did you mean STM32F429?
In order to use 64 bit programming, VPP (BOOT0) has to be powered by 8 - 9 Volts. Is it?
See the Reference Manual Section 3.6.2
By the way,
__IO uint64_t word = 0x1234567890;
would not work as (presumably) expected. It is a 32 bit architecture, integer constants will be truncated to 32 bits, unless there is an L suffix. U wouldn't hurt either, because the variable is unsigned. __IO is unnecessary.
uint64_t word = 0x1234567890UL;

PKCS#11 C_Encrypt fails with Bad Arguments for 128 bit AES key

I generate a 128-bit AES object using "C_CreateObject".
I then do the following to encrypt a piece of data and get a "Bad Argumnents" error on the call to "C_Encrypt" to get the encrypted data length.
char clear[] = "My name is Eric!";
buf_len = sizeof(clear) -1;
rv = pfunc11->C_EncryptInit(session, pMechanism, hObject);
if (rv != CKR_OK)
{
printf("ERROR: rv=0x%08X: initializing encryption:\n", (unsigned int)rv);
return false;
}
rv = pfunc11->C_Encrypt(session, (CK_BYTE_PTR)clear, (CK_ULONG)buf_len, NULL, pulEncryptedDataLen);
if (rv != CKR_OK)
{
printf("ERROR: rv=0x%08X: derror getting encryption data buffer length:\n", (unsigned int)rv);
return false;
}
What am I doing wrong here ?
Here is my mechanism definition -
CK_MECHANISM myMechanism = {CKM_AES_CBC_PAD, (CK_VOID_PTR)"01020304050607081122334455667788", (CK_ULONG)16};
CK_MECHANISM_PTR pMechanism = &myMechanism;
Your pulEncryptedDataLen is probably NULL which causes CKR_ARGUMENTS_BAD.
It is better to use e.g.:
CK_ULONG ulEncryptedDataLen;
...
rv = pfunc11->C_Encrypt(session, (CK_BYTE_PTR)clear, (CK_ULONG)buf_len, NULL, &ulEncryptedDataLen);
The number of bytes sufficient to store encryption result of a single-part encryption gets stored into ulEncryptedDataLen.
Also please note that your way of passing IV value is not correct as "01020304050607081122334455667788" results in an ASCII string (giving IV as 30313032303330343035303630373038 -- which is probably not what you want).
To get correct IV use "\x01\x02\x03\x04\x05\x06\x07\x08\x11\x22\x33\x44\x55\x66\x77\x88" instead.
Good luck!

Simblee/Rfduino Gzll communcation failing

I am attempting to send data from device to host using the Gazelle protocol, however, when reading a time varying signal in on MATLAB the values continuously change elements in the array.
Here is the Simblee/Rfduino host code:
#include <SimbleeGZLL.h>
device_t role = HOST;
char array[5];
void setup() {
Serial.begin(9600);
SimbleeGZLL.begin(role);
timer_one(1); // 1 ms timer
}
void loop() {
Serial.flush();
printf(EMG);
}
void SimbleeGZLL_onReceive(device_t device, int rssi, char *data, int len)
{
if (len > 0) {
digitalWrite(2,HIGH);
array[0] = data[0];
array[1] = data[1];
array[2] = data[2];
array[3] = data[3];
array[4] = '\0';
} else SimbleeGZLL.sendToDevice(device, 'A');
}
And the device code:
include
device_t role = DEVICE1;
volatile int state;
char array[4];
void setup() {
SimbleeGZLL.begin(role);
Serial.begin(9600);
timer_one(1);
}
void loop() {
array[0] = analogRead(2);
array[1] = analogRead(3);
array[2] = analogRead(4);
array[3] = analogRead(5);
SimbleeGZLL.sendToHost(EMG,4);
}
Could someone please provide some assistance to identify where the issue may lie?
Thank you!
Matlab is not super reliable with serial communication. I actually had a similar issue with a serial device where the input values would be out of order. Are you signaling when to start and stop printing? What does your matlab code look like?
I would set up a ring buffer on the host and the device to deal with the asycn time issues.
You are going to get timing issues with the current method. What kind of frequency are you going for? The analogRead is super slow, and double multiple in a row seems to make things even slower. Could you try to set up an ADC interrupt?
Where is your timer code?

print floats from audio input callback function

I'm working on a university project that involves a lot of programming in C, especially with Portaudio & ALSA. At the moment i'm trying to make a callback function to pass audio through, standard input/output job. I was wondering if anybody could tell me how to print the floats from my inputBuffer to display in real time in the terminal? Here is the internal structure of my callback function so far.
Thanks very much for your help in advance!
#define SAMPLE_RATE (44100)
#define PA_SAMPLE_TYPE paFloat32
#define FRAMES_PER_BUFFER (64)
typedef float SAMPLE;
static int audio_callback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
SAMPLE *out = (SAMPLE*)outputBuffer;
const SAMPLE *in = (const SAMPLE*)inputBuffer;
unsigned int i;
(void) timeInfo; /* Prevent unused variable warnings. */
(void) statusFlags;
(void) userData;
if( inputBuffer == NULL )
{
for( i=0; i<framesPerBuffer; i++ )
{
*out++ = *in++; /* left - clean */
*out++ = *in++; /* right - clean */
}
}
return paContinue;
}

IPHONE Objective-C program using OPENSSL perform DES Encryption

I have found an example about OPENSSL DES, when I applied this example into Objective-C program, the decrypted text was not equal to what I have input.
textField.text is the input textbox
Can anyone help me? Many thanks!!!
for e.g,
when I input "test", the decrypted text ="test&\264"
when I input "Enter an Text here", the decrypted text ="Ente"
when I input "1234567890", the decrypted text ="1234h&\311"
//ENCRYPTION
char* desen(char *clear, int size)
{
printf("Encrypted text\t %s \n",clear);
char *encrypted;
char key[]="password";
encrypted=(char*)malloc(sizeof(clear));
static char* Res;
int n=0;
DES_cblock Key2;
DES_key_schedule schedule;
Res = ( char * ) malloc( sizeof(clear) );
// Prepare the key for use with DES_cfb64_encrypt /
memcpy( Key2, key,8);
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );
// Encryption occurs here /
DES_cfb64_encrypt( ( unsigned char * ) clear, ( unsigned char * ) Res,
sizeof(clear), &schedule, &Key2, &n, DES_ENCRYPT );
memcpy(encrypted,Res, sizeof(clear));
printf("Key:%s\n",encrypted);
return encrypted;
}
//------------------------------------------------
//DECRYPTION-------------------------------
char* desde(char *clear, int size)
{
char *decrypted;
char key[]="password";
decrypted=(char*)malloc(sizeof(clear));
static char* Res;
int n=0;
DES_cblock Key2;
DES_key_schedule schedule;
Res = ( char * ) malloc( sizeof(clear) );
// Prepare the key for use with DES_cfb64_encrypt /
memcpy( Key2, key,8);
DES_set_odd_parity( &Key2 );
DES_set_key_checked( &Key2, &schedule );
// Encryption occurs here /
DES_cfb64_encrypt( ( unsigned char * ) clear, ( unsigned char * ) Res,
sizeof(clear), &schedule, &Key2, &n, DES_DECRYPT );
memcpy(decrypted,Res, sizeof(clear));
printf("Key:%s\n",decrypted);
return decrypted;
}
//------------------------------------------------
//----------Button------------------------------
- (IBAction)calculateDES_ENCRYPT:(id)sender
{
char key[]="password";
char *en;
char *de;
NSString *string = textField.text;
const char *temp=[string fileSystemRepresentation];
int len=strlen(temp);
char clear[len+1];
//char clear[50];
strcpy(clear,temp);
en=desen( clear,len+1);
de= desde(en, len+1);
}
------------------------------------------------
This line
encrypted=(char*)malloc(sizeof(clear));
doesn't do what you think. On a 32 bit system, sizeof(clear) will be 4 because it is the size of the pointer, not the length of the data pointed to. So you are probably only encrypting/decrypting 4 bytes and you are printing out those 4 bytes plus some garbage.