From 85aee632b0799ce567b9df7a6d16f59ab1564b34 Mon Sep 17 00:00:00 2001 From: bg Date: Sun, 27 Feb 2011 18:18:34 +0100 Subject: [PATCH] added base64 encoding --- base64/base64_dec.c | 211 ++++++++++++++++++++++++++++++++++++ base64/base64_dec.h | 29 +++++ base64/base64_enc.c | 88 +++++++++++++++ base64/base64_enc.h | 28 +++++ lm3s9b90.ld | 24 +++- mkfiles/base64.mk | 14 +++ noekeon/noekeon_prng.c | 2 +- test_src/main-base64-test.c | 189 ++++++++++++++++++++++++++++++++ test_src/main-bigint-test.c | 4 - test_src/string-extras.c | 105 +++++++++++------- test_src/string-extras.h | 2 +- 11 files changed, 643 insertions(+), 53 deletions(-) create mode 100644 base64/base64_dec.c create mode 100644 base64/base64_dec.h create mode 100644 base64/base64_enc.c create mode 100644 base64/base64_enc.h create mode 100644 mkfiles/base64.mk create mode 100644 test_src/main-base64-test.c diff --git a/base64/base64_dec.c b/base64/base64_dec.c new file mode 100644 index 0000000..724caf8 --- /dev/null +++ b/base64/base64_dec.c @@ -0,0 +1,211 @@ +/* base64_dec.c */ +/* + * This file is part of the ARM-Crypto-Lib. + * Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +/** + * base64 decoder (RFC3548) + * Author: Daniel Otte + * License: GPLv3 + * + * + */ + +#include +#include "base64_dec.h" + + +/* +#define USE_GCC_EXTENSION +*/ + +#ifdef USE_GCC_EXTENSION + +static +int ascii2bit6(char a){ + switch(a){ + case 'A'...'Z': + return a-'A'; + case 'a'...'z': + return a-'a'+26; + case '0'...'9': + return a-'0'+52; + case '+': + case '-': + return 62; + case '/': + case '_': + return 63; + default: + return -1; + } +} + +#else + +static +uint8_t ascii2bit6(char a){ + int r; + switch(a>>4){ + case 0x5: + case 0x4: + r=a-'A'; + if(r<0 || r>25){ + return -1; + } else { + return r; + } + case 0x7: + case 0x6: + r=a-'a'; + if(r<0 || r>25){ + return -1; + } else { + return r+26; + } + break; + case 0x3: + if(a>'9') + return -1; + return a-'0'+52; + default: + break; + } + switch (a){ + case '+': + case '-': + return 62; + case '/': + case '_': + return 63; + default: + return 0xff; + } +} + +#endif + +int base64_binlength(char* str, uint8_t strict){ + int l=0; + uint8_t term=0; + for(;;){ + if(*str=='\0') + break; + if(*str=='\n' || *str=='\r'){ + str++; + continue; + } + if(*str=='='){ + term++; + str++; + if(term==2){ + break; + } + continue; + } + if(term) + return -1; + if(ascii2bit6(*str)==-1){ + if(strict) + return -1; + } else { + l++; + } + str++; + } + switch(term){ + case 0: + if(l%4!=0) + return -1; + return l/4*3; + case 1: + if(l%4!=3) + return -1; + return (l+1)/4*3-1; + case 2: + if(l%4!=2) + return -1; + return (l+2)/4*3-2; + default: + return -1; + } +} + +/* + |543210543210543210543210| + |765432107654321076543210| + + . . . . + |54321054|32105432|10543210| + |76543210|76543210|76543210| + +*/ + +int base64dec(void* dest, const char* b64str, uint8_t strict){ + uint8_t buffer[4]; + uint8_t idx=0; + uint8_t term=0; + for(;;){ + buffer[idx]= ascii2bit6(*b64str); + + if(buffer[idx]==0xFF){ + if(*b64str=='='){ + term++; + b64str++; + if(term==2) + goto finalize; /* definitly the end */ + }else{ + if(*b64str == '\0'){ + goto finalize; /* definitly the end */ + }else{ + if(*b64str == '\r' || *b64str == '\n' || !(strict)){ + b64str++; /* charcters that we simply ignore */ + }else{ + return -1; + } + } + } + }else{ + if(term) + return -1; /* this happens if we get a '=' in the stream */ + idx++; + b64str++; + } + if(idx==4){ + ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4; + ((uint8_t*)dest)[1] = buffer[1]<<4 | buffer[2]>>2; + ((uint8_t*)dest)[2] = buffer[2]<<6 | buffer[3]; + dest = (uint8_t*)dest +3; + idx=0; + } + } + finalize: + /* the final touch */ + if(idx==0) + return 0; + if(term==1){ + ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4; + ((uint8_t*)dest)[1] = buffer[1]<<4 | buffer[2]>>2; + return 0; + } + if(term==2){ + ((uint8_t*)dest)[0] = buffer[0]<<2 | buffer[1]>>4; + return 0; + } + return -1; +} diff --git a/base64/base64_dec.h b/base64/base64_dec.h new file mode 100644 index 0000000..5a75d00 --- /dev/null +++ b/base64/base64_dec.h @@ -0,0 +1,29 @@ +/* base64_dec.h */ +/* + * This file is part of the ARM-Crypto-Lib. + * Copyright (C) 2006, 2007, 2008 Daniel Otte (daniel.otte@rub.de) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef BASE64_DEC_H_ +#define BASE64_DEC_H_ + +#include + +int base64_binlength(char* str, uint8_t strict); +int base64dec(void* dest, const char* b64str, uint8_t strict); + +#endif /*BASE64_DEC_H_*/ diff --git a/base64/base64_enc.c b/base64/base64_enc.c new file mode 100644 index 0000000..09aed98 --- /dev/null +++ b/base64/base64_enc.c @@ -0,0 +1,88 @@ +/* base64_enc.c */ +/* + * This file is part of the ARM-Crypto-Lib. + * Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +/** + * base64 encoder (RFC3548) + * Author: Daniel Otte + * License: GPLv3 + * + * + */ + +#include +#include "base64_enc.h" + +const char base64_alphabet[64] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' }; + + +static +char bit6toAscii(uint8_t a){ + a &= (uint8_t)0x3F; + return base64_alphabet[a]; +} + +void base64enc(char* dest,const void* src, uint16_t length){ + uint16_t i,j; + uint8_t a[4]; + for(i=0; i>2; + a[1]= (((((uint8_t*)src)[i*3+0])<<4) | ((((uint8_t*)src)[i*3+1])>>4)) & 0x3F; + a[2]= (((((uint8_t*)src)[i*3+1])<<2) | ((((uint8_t*)src)[i*3+2])>>6)) & 0x3F; + a[3]= (((uint8_t*)src)[i*3+2]) & 0x3F; + for(j=0; j<4; ++j){ + *dest++=bit6toAscii(a[j]); + } + } + /* now we do the rest */ + switch(length%3){ + case 0: + break; + case 1: + a[0]=(((uint8_t*)src)[i*3+0])>>2; + a[1]=((((uint8_t*)src)[i*3+0])<<4)&0x3F; + *dest++ = bit6toAscii(a[0]); + *dest++ = bit6toAscii(a[1]); + *dest++ = '='; + *dest++ = '='; + break; + case 2: + a[0]= (((uint8_t*)src)[i*3+0])>>2; + a[1]= (((((uint8_t*)src)[i*3+0])<<4) | ((((uint8_t*)src)[i*3+1])>>4)) & 0x3F; + a[2]= ((((uint8_t*)src)[i*3+1])<<2) & 0x3F; + *dest++ = bit6toAscii(a[0]); + *dest++ = bit6toAscii(a[1]); + *dest++ = bit6toAscii(a[2]); + *dest++ = '='; + break; + default: /* this will not happen! */ + break; + } +/* finalize: */ + *dest='\0'; +} + diff --git a/base64/base64_enc.h b/base64/base64_enc.h new file mode 100644 index 0000000..4f3f98c --- /dev/null +++ b/base64/base64_enc.h @@ -0,0 +1,28 @@ +/* base64_enc.h */ +/* + * This file is part of the ARM-Crypto-Lib. + * Copyright (C) 2006, 2007, 2008 Daniel Otte (daniel.otte@rub.de) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#ifndef BASE64_ENC_H_ +#define BASE64_ENC_H_ + +#include + +void base64enc(char* dest, const void* src, uint16_t length); + +#endif /*BASE64_ENC_H_*/ diff --git a/lm3s9b90.ld b/lm3s9b90.ld index 8672a93..36ef541 100644 --- a/lm3s9b90.ld +++ b/lm3s9b90.ld @@ -26,14 +26,28 @@ SECTIONS { .text : { _text = .; KEEP(*(.isr_vectors)) - *(.init*) - *(.fini*) *(.text*) *(.rodata*) - _text_end = .; } > flash + /* .ARM.exidx is sorted, so has to go in its own output section. +*/ + .ARM.exidx : { + __exidx_start = .; + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + __exidx_end = .; + } > flash + + . = ALIGN(4); + .ARM.extab : { + __extab_start = .; + *(.ARM.extab* .gnu.linkonce.armextab.*) + __extab_end = .; + } > flash + + . = ALIGN(4); - .data : AT ( ADDR(.text) + SIZEOF(.text) ) { + _text_end = .; + .data : AT ( ADDR(.ARM.extab) + SIZEOF(.ARM.extab) ) { _data = .; *(.data*) _data_end = .; @@ -41,11 +55,9 @@ SECTIONS { .bss : AT ( ADDR(.data) + SIZEOF(.data) ) { _bss = .; - __bss_start__ = .; *(.bss*) *(COMMON) _bss_end = .; - __bss_end__ = .; } > ram end = .; } diff --git a/mkfiles/base64.mk b/mkfiles/base64.mk new file mode 100644 index 0000000..8abfbcc --- /dev/null +++ b/mkfiles/base64.mk @@ -0,0 +1,14 @@ +# Makefile for Base64-encoding +ALGO_NAME := BASE64 + +# comment out the following line for removement of base64 from the build process +ENCODINGS += $(ALGO_NAME) + +$(ALGO_NAME)_DIR := base64/ +$(ALGO_NAME)_OBJ := base64_enc.o base64_dec.o +$(ALGO_NAME)_INCDIR := noekeon/ memxor/ +$(ALGO_NAME)_TEST_BIN := main-base64-test.o $(CLI_STD) \ + performance_test.o noekeon.o noekeon_prng.o memxor.o + +$(ALGO_NAME)_PERFORMANCE_TEST := performance + diff --git a/noekeon/noekeon_prng.c b/noekeon/noekeon_prng.c index a264707..3b2eca9 100644 --- a/noekeon/noekeon_prng.c +++ b/noekeon/noekeon_prng.c @@ -31,7 +31,7 @@ static uint8_t random_state[16]; static uint8_t random_key[16]; -static uint8_t i=0; +static volatile uint8_t i=0; uint8_t random8(void){ static uint8_t sr[16]; diff --git a/test_src/main-base64-test.c b/test_src/main-base64-test.c new file mode 100644 index 0000000..67fb730 --- /dev/null +++ b/test_src/main-base64-test.c @@ -0,0 +1,189 @@ +/* main-base64-test.c */ +/* + This file is part of the AVR-Crypto-Lib. + Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ +/* + * base64 test-suit + * +*/ + +#include +#include +#include +#include "config.h" +#include "cli.h" +#include "dump.h" +#include "uart_lowlevel.h" +#include "sysclock.h" +#include "hw_gptm.h" + +#include "noekeon.h" +#include "noekeon_prng.h" +#include "base64_enc.h" +#include "base64_dec.h" +#include "string-extras.h" +#include "cli.h" +#include "performance_test.h" + +void uart0_putc(char byte){ + uart_putc(UART_0, byte); +} + +char uart0_getc(void){ + return uart_getc(UART_0); +} + +char* algo_name = "Base64"; + +/***************************************************************************** + * additional validation-functions * + *****************************************************************************/ +#define TESTLENGTH 4096 + +void testrun_stdtest_base64(void){ + uint8_t fail=0; + unsigned l,i; + int sl; + char str[32]; + uint8_t bin_buffer[TESTLENGTH]; + char b64_buffer[TESTLENGTH*4/3+10]; + uint8_t bin_buffer2[TESTLENGTH]; + random_seed(bin_buffer); + + for(l=0; l -#include -#include - char* algo_name = "BigInt"; void uart0_putc(char byte){ diff --git a/test_src/string-extras.c b/test_src/string-extras.c index 0c01a04..41e3aec 100644 --- a/test_src/string-extras.c +++ b/test_src/string-extras.c @@ -28,6 +28,7 @@ #include #include #include +#include uint32_t stridentcnt(const char* a, const char* b){ uint16_t i=0; @@ -76,63 +77,85 @@ void str_reverse(char* buffer){ --j; } } - -char* ultoa(unsigned long a, char* buffer, uint8_t radix){ - if(radix<2 || radix>36){ +char* ultoa (unsigned long value, char* buffer, uint8_t radix ){ + if((radix>36) || (radix<2) || (buffer==NULL)){ return NULL; } - char* ptr=buffer; - div_t result; - if(a==0){ - ptr[0] = '0'; - ptr[1] = '\0'; - return buffer; - } - while(a){ - result = div(a, radix); - *ptr = result.rem; - if(result.rem<10){ - *ptr += '0'; + unsigned length=(unsigned)-1; + uint8_t x; + char c; + do{ + x = value%radix; + value /= radix; + if(x<10){ + c = x+ '0'; }else{ - *ptr += 'a'-10; + c = x+ 'a'; } - ++ptr; - a = result.quot; + buffer[++length] = c; + }while(value); + buffer[length+1]='\0'; + unsigned idx=0; + while(idx+136){ +char* ulltoa(unsigned long long value, char* buffer, uint8_t radix){ + if((radix>36) || (radix<2) || (buffer==NULL)){ return NULL; } - char* ptr=buffer; - uint8_t rem; - if(a==0){ - ptr[0] = '0'; - ptr[1] = '\0'; - return buffer; - } - while(a){ - rem = a % radix; - a = a / radix; - *ptr = rem; - if(rem<10){ - *ptr += '0'; + unsigned length=(unsigned)-1; + uint8_t x; + char c; + do{ + x = value%radix; + value /= radix; + if(x<10){ + c = x+ '0'; }else{ - *ptr += 'a'-10; + c = x+ 'a'; } - ++ptr; + buffer[++length] = c; + }while(value); + buffer[length+1]='\0'; + unsigned idx=0; + while(idx+136) || (radix<2) || (buffer==NULL)){ + return NULL; + } + unsigned length=(unsigned)-1; + uint8_t x; + char c; + do{ + x = value%radix; + value /= radix; + if(x<10){ + c = x+ '0'; + }else{ + c = x+ 'a'; + } + buffer[++length] = c; + }while(value); + buffer[length+1]='\0'; + unsigned idx=0; + while(idx+1