X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=md5-asm.S;h=de3b170092330281674357475131a58019413d22;hb=f215d8ed649fd5bf5f85eb34f851a1539302013d;hp=2928fe370b6463ab3ad244f3b296feb6cf641ee3;hpb=a397db40b94b01a3ed72f9367961cf68d82593d1;p=avr-crypto-lib.git diff --git a/md5-asm.S b/md5-asm.S index 2928fe3..de3b170 100644 --- a/md5-asm.S +++ b/md5-asm.S @@ -1,6 +1,6 @@ /* md5-asm.S */ /* - This file is part of the Crypto-avr-lib/microcrypt-lib. + This file is part of the AVR-Crypto-Lib. Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de) This program is free software: you can redistribute it and/or modify @@ -785,74 +785,75 @@ md5_lastBlock: movw r16, r20 /* length_b */ movw r14, r22 /* block_ptr */ movw r12, r24 /* state_ptr */ - + ldi r18, 64 +2: cpi r17, 2 /* hi8(512) */ brlo 2f 1: movw r24, r12 movw r22, r14 rcall md5_nextBlock - ldi r18, 64 add r14, r18 adc r15, r1 subi r17, 2 - brge 1b + rjmp 2b 2: pop r31 pop r30 - adiw r30, 1 + adiw r30, 1 /* adjust Z to point to buffer */ movw r26, r14 movw r24, r16 adiw r24, 7 lsr r25 ror r24 - lsr r24 + lsr r25 + ror r24 lsr r24 /* r24 now holds how many bytes are to copy */ - ldi r18, 64 - sub r18, r24 + ldi r18, 64 + sub r18, r24 /* r18 will hold the amount of used bytes in buffer */ tst r24 4: breq 5f ld r0, X+ - st Z+, r0 + st Z+, r0 dec r24 - rjmp 4b + rjmp 4b /* Z points to the byte after msg in buffer */ 5: /* append 1-bit */ mov r20, r16 + ldi r19, 0x80 andi r20, 0x07 brne bit_fucking - ldi r19, 0x80 st Z+, r19 - dec r18 + dec r18 /* 'allocate' another byte in buffer */ rjmp after_bit_fucking bit_fucking: - ldi r19, 0x80 1: lsr r19 dec r20 brne 1b or r0, r19 st -Z, r0 - adiw r30, 1 + adiw r30, 1 after_bit_fucking: clt cpi r18, 8 brmi 2f - set /* store in t if the counter will also fit in this block */ + set /* store in t if the counter will also fit in this block (1 if fit)*/ 2: tst r18 breq 2f -1: +1: /* fill remaning buffer with zeros */ st Z+, r1 dec r18 brne 1b 2: sbiw r30, 63 sbiw r30, 1 - movw r14, r30 + movw r14, r30 /* r14:r15 now points to buffer */ brts load_counter + /* counter does not fit, finalize this block */ movw r24, r12 movw r22, r14 rcall md5_nextBlock @@ -864,13 +865,14 @@ after_bit_fucking: brne 3b load_counter: - movw r26, r12 + movw r26, r12 /* X points to state */ adiw r26, 16 ld r19, X+ ld r20, X+ ld r21, X+ ld r22, X+ - brts post_counter_decrement + brts post_counter_decrement /* do not decremen because counter fits */ +counter_decrement: subi r19, 1 sbci r20, 0 sbci r21, 0 @@ -883,8 +885,8 @@ post_counter_decrement: rol r21 rol r22 rol r23 - add r18, r16 - adc r19, r17 + mov r18, r16 /* r16:r17 length_b */ + add r19, r17 adc r20, r1 adc r21, r1 adc r22, r1 @@ -911,5 +913,65 @@ md5_lastBlock_exit: ret +;############################################################################### + + +.global md5_ctx2hash +md5_ctx2hash: + movw r26, r24 + movw r30, r22 + ldi r22, 16 +1: + ld r0, Z+ + st X+, r0 + dec r22 + brne 1b + ret + + +;############################################################################### + + +.global md5 +md5: + stack_alloc 20 + push_range 8, 17 + adiw r30, 1 + movw r8, r30 /* ctx */ + movw r10, r24 /* dest */ + movw r12, r22 /* msg */ + movw r14, r18 /* length (low) */ + movw r16, r20 /* length (high) */ + movw r24, r30 + rcall md5_init +1: + tst r16 + brne next_round + tst r17 + breq last_round +next_round: + movw r24, r8 + movw r22, r12 + rcall md5_nextBlock + ldi r22, 64 + add r12, r22 + adc r13, r1 + ldi r22, 2 + sub r15, r22 + sbci r16, 0 + sbci r17, 0 + rjmp 1b +last_round: + movw r24, r8 + movw r22, r12 + movw r20, r14 + rcall md5_lastBlock + movw r24, r10 + movw r22, r8 + rcall md5_ctx2hash + pop_range 8, 17 + stack_free 20 + ret +