micom 에서 SHA-1 으로 otp 생성하기
micom우선 Hash, Mac, Signature 에 대해 알아 둘 필요가 있다.
https://rookiecj.tistory.com/368
hash, MAC vs Signature 차이점
hash나 MAC, Signature모두 hash를 기본으로 사용하지만 어떤부분이 다른지 살펴본다. 먼저 기본이 되는 hash는 메세지를 요약하는데 사용된다. byte[] data = "plain text".getBytes("UTF-8"); MessageDigest mes..
rookiecj.tistory.com
otp 생성을 위해 시간값과 특정 기기 아이디로 Mac (Message Authentication Code) 를 생성해 보자.
java code example..
com.j256.twofactorauth.TimeBasedOneTimePasswordUtil Maven / Gradle / Ivy
com.j256.two-factor-auth two-factor-auth 0.2 compile group: 'com.j256.two-factor-auth', name: 'two-factor-auth', version: '0.2' //Thanks for using https://jar-download.com libraryDependencies += "com.j256.two-factor-auth" % "two-factor-auth" % "0.2" //Than
jar-download.com
c code example..
https://github.com/cantora/avr-crypto-lib
cantora/avr-crypto-lib
mirror of avr-crypto-lib (http://code.nerilex.org/bg/avr-crypto-lib.git) - cantora/avr-crypto-lib
github.com
mkj/pihelp
A hardware watchdog for a Raspberry Pi using an AVR - mkj/pihelp
github.com
8비트 마이컴에서는 uint64_t 자료형을 사용할 수 없어 문제가 발생한다. 참고할 만한 문서.
https://stackoverflow.com/questions/33498371/wrong-sha-1-hash
Wrong SHA-1 hash
I'm planning to use AVR-Crypto's SHA-1 implementation for HMAC. However, I can't seem to generate the correct SHA-1 sum. For instance, if I call the function with the following unsigned char sh...
stackoverflow.com
c code example 에서 micom 을 위해 고쳐져야 하는 부분은
sha1_ctx_t 구조체 선언 부분을 아래와 같이 uint64_t -> uint32_t 로 수정해준다.
/*
typedef struct
{
uint32_t h[5];
uint64_t length;
} sha1_ctx_t;
*/
typedef struct
{
uint32_t h[5];
uint32_t length;
} sha1_ctx_t;
sha1_lastBlock 함수에서 주석 부분을 아래와 같이 수정해 준다.
uint8_t i;
/*
for (i=0; i<8; ++i)
{
lb[56+i] = ((uint8_t*)&(state->length))[7-i];
}
*/
for (i = SHA1_BLOCK_BYTES; state->length != 0; state->length >>= 8)
lb[--i] = (uint8_t)(state->length);
리눅스에서 컴파일 한 것과 다르게 뒤집어진(?) 결과가 나온다.
예를들어 리눅스에서 b696eeac 라면 8비트 마이컴(8051)에서는 acee96b6
함수를 아래와 같이 값을 그냥 리턴하도록 수정해준다.
uint32_t change_endian32(uint32_t x)
{
//return (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8));
return x;
}