From d5e258765801a9b13be0e0502de94d7cb8c40629 Mon Sep 17 00:00:00 2001
From: bg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Date: Fri, 5 Dec 2008 15:09:16 +0000
Subject: [PATCH] MD5 now completly in ASM

---
 md5-asm.S          | 64 ++++++++++++++++++++++++++++++++++++++++++++--
 md5-stub.c         | 43 -------------------------------
 mkfiles/md5_asm.mk |  4 +--
 3 files changed, 64 insertions(+), 47 deletions(-)
 delete mode 100644 md5-stub.c

diff --git a/md5-asm.S b/md5-asm.S
index 2928fe3..be8f50b 100644
--- a/md5-asm.S
+++ b/md5-asm.S
@@ -785,7 +785,7 @@ md5_lastBlock:
 	movw r16, r20 /* length_b  */ 
 	movw r14, r22 /* block_ptr */
 	movw r12, r24 /* state_ptr */
-
+2:
 	cpi r17, 2 /* hi8(512) */	
 	brlo 2f
 1:
@@ -796,7 +796,7 @@ md5_lastBlock:
 	add r14, r18
 	adc r15, r1
 	subi r17, 2
-	brge 1b
+	rjmp 2b
 2:
 	pop r31
 	pop r30
@@ -911,5 +911,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
+
 
 
diff --git a/md5-stub.c b/md5-stub.c
deleted file mode 100644
index 2631c37..0000000
--- a/md5-stub.c
+++ /dev/null
@@ -1,43 +0,0 @@
-/* md5-asm.c */
-/*
-    This file is part of the Crypto-avr-lib/microcrypt-lib.
-    Copyright (C) 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 <http://www.gnu.org/licenses/>.
-*/
-
- #include "md5.h"
- #include "uart.h" 
- #include <stdint.h>
- #include <string.h>
- 
- #undef DEBUG
-
-
-void md5_ctx2hash(md5_hash_t* dest, const md5_ctx_t* state){
-	memcpy(dest, state->a, MD5_HASH_BYTES);
-}
-
-void md5(md5_hash_t* dest, const void* msg, uint32_t length_b){
-	md5_ctx_t ctx;
-	md5_init(&ctx);
-	while(length_b>=MD5_BLOCK_BITS){
-		md5_nextBlock(&ctx, msg);
-		msg = (uint8_t*)msg + MD5_BLOCK_BYTES;
-		length_b -= MD5_BLOCK_BITS;
-	}
-	md5_lastBlock(&ctx, msg, length_b);
-	md5_ctx2hash(dest, &ctx);
-}
-
diff --git a/mkfiles/md5_asm.mk b/mkfiles/md5_asm.mk
index 8cba137..00a1fde 100644
--- a/mkfiles/md5_asm.mk
+++ b/mkfiles/md5_asm.mk
@@ -4,9 +4,9 @@ ALGO_NAME := MD5_ASM
 # comment out the following line for removement of MD5 from the build process
 HASHES += $(ALGO_NAME)
 
-$(ALGO_NAME)_OBJ      := md5-asm.o md5-stub.o
+$(ALGO_NAME)_OBJ      := md5-asm.o
 $(ALGO_NAME)_TEST_BIN := main-md5-test.o debug.o uart.o serial-tools.o \
                          nessie_hash_test.o nessie_common.o cli.o performance_test.o
 $(ALGO_NAME)_NESSIE_TEST      := "nessie"
-$(ALGO_NAME)_PEROFRMANCE_TEST := "performance"
+$(ALGO_NAME)_PERFORMANCE_TEST := "performance"
 
-- 
2.39.5