Cleaned main.c for testing with an arduino, sha1.c: moved arrays to global to save program memory

master
Stefan Krulj 14 years ago
parent be49d342e2
commit b11f2152bd
  1. 6
      Makefile
  2. 4
      debug.py
  3. 85
      main.c
  4. 1
      main.h
  5. 11
      sha1.c
  6. 12
      test.c

@ -16,11 +16,11 @@
#-------------------------------------------------------------------------
PROJNAME = main
OBJS = main.o sha1.o hmac.o trunc.o usbdrv/usbdrvasm.o usbdrv/usbdrv.o
OBJS = main.o sha1.o hmac.o trunc.o #usbdrv/usbdrvasm.o usbdrv/usbdrv.o
# target architecture
MCU = attiny45
MCU = atmega168
#MCU = attiny45
#-------------------------------------------------------------------------
# macros for the tools
#-------------------------------------------------------------------------

@ -71,8 +71,8 @@ class SHA1 (object):
for i in range(len(hexdigest) // 2))
#H(K XOR opad, H(K XOR ipad, text))
k = b"\x0b"*20 + b"\x00"*(64-20)
text = b"Hi There"
k = b"\x4a\x65\x66\x65" + b"\x00"*(64-4)
text = b"what do ya want for nothing?"
ipad=b""
opad=b""
for i in range(64):

@ -1,58 +1,53 @@
#include "main.h"
static uint8_t reportBuffer[64];
const PROGMEM char usbHidReportDescriptor[35] = { /* USB report descriptor */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
usbMsgLen_t usbFunctionSetup(uchar data[8]) {
usbRequest_t *rq = (void *)data;
usbMsgPtr = reportBuffer;
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) { /* HID class request */
if(rq->bRequest == USBRQ_HID_GET_REPORT) { /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* since we have only one report type, we can ignore the report-ID */
//buildReport();
return sizeof(reportBuffer);
}/*else if(rq->bRequest == USBRQ_HID_GET_IDLE){
usbMsgPtr = &idleRate;
return 1;
}else if(rq->bRequest == USBRQ_HID_SET_IDLE){
idleRate = rq->wValue.bytes[1];
}*/
} else {
/* ignore vendor type requests, we don't use any */
#define DEBUG
#ifdef DEBUG
void tx( char data ){
while ( !( UCSR0A & (1<<UDRE0)) );
UDR0 = data;
}
void sendstr( char* str){
while(str[0]!=0){
tx(str[0]);
str++;
}
return 0;
}
void sendunum(uint32_t num){
char text[11] = {0,0,0,0,0,0,0,0,0,0,0};
uint32_t tmp=1;
char leadingzero=0;
char digit=0;
char i;
if(num==0){
tx('0');
return;
}
for(i=1;i<=10;i++){
digit = '0'+((num/tmp)%10);
tmp*=10;
if(digit=='0'){
leadingzero++;
}else{
leadingzero=0;
}
text[10-i]=digit;
}
sendstr( text+leadingzero );
}
char rx( void ){
while ( !(UCSR0A & (1<<RXC0)) );
return UDR0;
}
#endif //DEBUG
int main(void) __attribute__((noreturn));
int __attribute__((OS_main)) main(void) {
usbDeviceDisconnect();
_delay_ms(200);
usbDeviceConnect();
usbInit();
//printf("OTP: %d\n", get_otp_from_cnt(7,6) );
sendstr("Hello");
for(;;){
}

@ -6,6 +6,7 @@
#include "usbconfig.h"
#include <util/delay.h>
#include <avr/io.h>
#define _PORT_BTN B
#define _NUM_BTN 3

@ -19,14 +19,15 @@
#define K4 0xCA62C1D6
#define _LROT32(x,n) (( (uint32_t)(x) << n ) | ( (uint32_t)(x) >>(32-n) ))
#define _W(n) (n>=16 ? chunk_e[n-16] : chunk[n])
#define _W(n) (n>=16 ? _sha1_chunk_e[n-16] : _sha1_chunk[n])
static uint32_t chunk[16];
uint32_t _sha1_chunk[16];
static uint8_t chunk_cnt;
uint32_t _sha1_chunk_e[64]; /*putting this here saves a lot of code space*/
void sha1_addbyte(uint8_t c){
uint8_t* p=(uint8_t*)chunk;
uint8_t* p=(uint8_t*)_sha1_chunk;
uint8_t i=(chunk_cnt & 0x03);
/* chunk_cnt-i)+3-i deals with the endianess */
@ -34,9 +35,9 @@ void sha1_addbyte(uint8_t c){
chunk_cnt++;
if(chunk_cnt==64){
/*chunk extension*/
uint32_t a,b,c,d,e,chunk_e[64];
uint32_t a,b,c,d,e;
for(i=0; i<64; ++i){
chunk_e[i]= _LROT32( _W(i+16-3) ^ _W(i+16-8) ^ _W(i+16-14) ^ _W(i+16-16), 1 );
_sha1_chunk_e[i]= _LROT32( _W(i+16-3) ^ _W(i+16-8) ^ _W(i+16-14) ^ _W(i+16-16), 1 );
}
a=sha1_digest[4];
b=sha1_digest[3];

@ -21,7 +21,7 @@ char sha1sum[41];
#define MSG1 "Hi There"
#define SECRET2 {'J', 'e', 'f', 'e'};
#define MSG2 ""
#define MSG2 "what do ya want for nothing?"
int main(){
/*SHA1STR makro needs those two*/
@ -53,14 +53,16 @@ int main(){
assert(strcmp("4d71043a68fa28b9592d1aa5748e5cd4291c0512",sha1sum)==0);
char sec1[20]=SECRET1;
hmac(MSG1,8,sec,20);
hmac(MSG1,8,sec1,20);
SHA1STR();
printf("HMAC: %s\n",sha1sum);
assert(strcmp("b617318655057264e28bc0b6fb378c8ef146be00",sha1sum)==0);
char sec1[20]=SECRET2;
hmac(MSG2,8,sec,20);
char sec2[4]=SECRET2;
hmac(MSG2,28,sec2,4);
SHA1STR();
printf("HMAC: %s\n",sha1sum);
//assert(strcmp("b617318655057264e28bc0b6fb378c8ef146be00",sha1sum)==0);
assert(strcmp("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",sha1sum)==0);
}

Loading…
Cancel
Save