Jump to content
43oh

MD5 hashing in pure assembly


Recommended Posts

THIS is an early work-in-progress, it will not work (except for the first transform).

 

What works so far:

- MD5 basic functions F,G,H,I (i think is optimized enaugh)

- Selecting basic functions using a indirect call using ROM table (i think so)

- 32-Bit addition + circular shifting function

- getting shifting amount from ROM table

 

- first transform of MD5 works, i compared results ;)

a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);

 

What is missing so far:

- A Permutation (deciding that sequence)

a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);

d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);

c=FF(c,d,a,b,x[k+2], S13,0x242070DB);

....

- Optimization, refactoring architecture

- Hashing multiple blocks

 

I'm running out of registers, so should i use discrete RAM locations or should i push

it on the stack (i'm have PIC heritage and that CPU doesn't have real stack *g*).

 

What is better, as i want to make this into a C callable library?

Reason for starting this is:

Learning MSP430 ASM and since MD5 can't compile on most MSP430 due to limitations.

 

I decided to not make it like most C implementations where simply code is unrolled

as my goal is minimum code density and minimum RAM usage, but i will have to

decide, maybe refactor architecture in future

 

#include "msp430.h"                     ; #define controlled include file

       NAME    main                    ; module name

       PUBLIC  main                    ; make the main label vissible
                                       ; outside this module
       ORG     0FFFEh
       DC16    init                    ; set reset vector to 'init' label

//        RSEG    CSTACK                  ; pre-declaration of segment
//        RSEG    CODE                    ; place program in 'CODE' segment

//init:   MOV     #SFE(CSTACK), SP        ; set up stack

       ORG     1100h
init:   
       MOV     09FEh, SP

main:   NOP                             ; main program
       MOV.W   #WDTPW+WDTHOLD,&WDTCTL  ; Stop watchdog timer






; INIT MD5 STATE REGISTERS
md5_init:
       MOV.W   #md5_init_const, R4
       CLR     R5

        EVEN
md5_init_cp:
       MOV.W   @R4+, md5_context(R5)
       INCD    R5
       CMP     #0014h, R5
       JNZ      md5_init_cp   


       MOV.W  md5_a, R4
       MOV.W  md5_a+2,R5
       MOV.W  md5_b, R6
       MOV.W  md5_b+2, R7
       MOV.W  md5_c, R8
       MOV.W  md5_c+2, R9
       MOV.W  md5_d, R10
       MOV.W  md5_d+2, R11       
       CLR    R12                 ;BYTE COUTNER
       MOV    0, R15              ; F FUNCTION
       EVEN
md5_loop_1:
       CALL   #md5_transform
       INC    R12
       MOV    R12,R15
       RRA    R15
       RRA    R15
       CMP    #64,R12
       JZ     $


       ;; MAIN TRANSFORM FUNCTION
       ;; R4/R5 = A   R6/R7 = B  R8/R9 = C  R10/R11 = D
       ;; R12 = CURRENT BYTE   R15 = F_PTR
       EVEN
md5_transform:
       PUSH   R6
       PUSH    R7
       PUSH    R8
       PUSH    R9
       PUSH    R10
       PUSH    R11

       CALL   md5_ftable(R15)     
       MOV    #md5_message, R11
       RLA    R12
       ADD    md5_bytes(R12), R11

       ADD    @R11+, R8
       ADDC   @R11+, R9
       MOV    #md5_t, R11
       ADD    R12, R11
       RRA    R12
       ADD    @R11+, R8
       ADDC   @R11+, R9
       ADD    R8, R4
       ADDC   R9, R5
       MOV.B  md5_shift(R12), R11


       EVEN
md5_leftrotate:
       RLA    R4
       RLC    R5
       ADC    R4
       DEC    R11
       JNZ    md5_leftrotate
       POP    R11
       POP    R10
       POP    R9
       POP    R8
       POP    R7
       POP    R6
       ADD    R6,R4
       ADDC   R7,R5
       RET


md5_f:
;R6 = XL R7 = XH
;R8 = YL R9 = YH
;R10 = ZL R11 = ZH
;R8/R9 RETVAL
       AND   R6, R8
       AND   R7, R9    ;R8/R9 = (X AND Y)
       BIC   R6, R10    ;R8/R9 = (NOT X AND Z)
       BIC   R7, R11
       BIS    R10, R8
       BIS    R11, R9
       RET

;R6 = XL R7 = XH
;R8 = YL R9 = YH
;R10 = ZL R11 = ZH
;R8/R9 RETVAL
md5_g:
       AND   R10, R6
       AND   R11, R7    ;R6/R7 = (Z AND X)
       BIC   R10, R8    ;R6/R7 = (NOT Z AND Y)
       BIC   R11, R9
       BIS    R6, R8
       BIS    R7, R9
       RET

;R6 = XL R7 = XH
;R8 = YL R9 = YH
;R10 = ZL R11 = ZH
;R8/R9 RETVAL
md5_h:
       XOR   R6, R10
       XOR   R7, R11
       XOR   R10, R8
       XOR   R11, R9
       RET

;R6 = XL R7 = XH
;R8 = YL R9 = YH
;R10 = ZL R11 = ZH
;R8/R9 RETVAL
md5_i:
       INV   R10
       INV   R11
       BIS   R6, R10
       BIS   R7, R11
       XOR   R10, R8
       XOR   R11, R9
       RET

;R4 = AL R5 = AH
;R6 = BL R7 = BH

add_32bit:
       ADD   R6, R4
       ADDC  R7, R5
       RET

       EVEN

md5_ftable:
       DC16 md5_f, md5_g, md5_h, md5_i

       EVEN
md5_t:
       DC16 0xa478, 0xd76a, 0xb756, 0xe8c7, 0x70db, 0x2420, 0xceee, 0xc1bd, 0x0faf, 0xf57c
       DC16 0xc62a, 0x4787, 0x4613, 0xa830, 0x9501, 0xfd46, 0x98d8, 0x6980, 0xf7af, 0x8b44
       DC16 0x5bb1, 0xffff, 0xd7be, 0x895c, 0x1122, 0x6b90, 0x7193, 0xfd98, 0x438e, 0xa679
       DC16 0x0821, 0x49b4, 0x2562, 0xf61e, 0xb340, 0xc040, 0x5a51, 0x265e, 0xc7aa, 0xe9b6
       DC16 0x105d, 0xd62f, 0x1453, 0x0244, 0xe681, 0xd8a1, 0xfbc8, 0xe7d3, 0xcde6, 0x21e1
       DC16 0x07d6, 0xc337, 0x0d87, 0xf4d5, 0x14ed, 0x455a, 0xe905, 0xa9e3, 0xa3f8, 0xfcef
       DC16 0x02d9, 0x676f, 0x4c8a, 0x8d2a, 0x3942, 0xfffa, 0xf681, 0x8771, 0x6122, 0x6d9d
       DC16 0x380c, 0xfde5, 0xea44, 0xa4be, 0xcfa9, 0x4bde, 0x4b60, 0xf6bb, 0xbc70, 0xbebf
       DC16 0x7ec6, 0x289b, 0x27fa, 0xeaa1, 0x3085, 0xd4ef, 0x1d05, 0x0488, 0xd039, 0xd9d4
       DC16 0x99e5, 0xe6db, 0x7cf8, 0x1fa2, 0x5665, 0xc4ac, 0x2244, 0xf429, 0xff97, 0x432a
       DC16 0x23a7, 0xab94, 0xa039, 0xfc93, 0x59c3, 0x655b, 0xcc92, 0x8f0c, 0xf47d, 0xffef
       DC16 0x5dd1, 0x8584, 0x7e4f, 0x6fa8, 0xe6e0, 0xfe2c, 0x4314, 0xa301, 0x11a1, 0x4e08
       DC16 0x7e82, 0xf753, 0xf235, 0xbd3a, 0xd2bb, 0x2ad7, 0xd391, 0xeb86

       EVEN
md5_shift:
md5_shift_f:
       DC8   7,12,17,22
       DC8   7,12,17,22
       DC8   7,12,17,22
       DC8   7,12,17,22
md5_shift_g:
       DC8   5,9,14,20
       DC8   5,9,14,20
       DC8   5,9,14,20
       DC8   5,9,14,20
md5_shift_h:
       DC8   4,11,16,23
       DC8   4,11,16,23
       DC8   4,11,16,23
       DC8   4,11,16,23
md5_shift_i:
       DC8   6,10,15,21
       DC8   6,10,15,21
       DC8   6,10,15,21
       DC8   6,10,15,21

       EVEN
md5_bytes:
md5_bytes_f:
       DC16    0, 2, 4, 6
       DC16    8, 10,12,14
       DC16    16, 18,20,22
       DC16   24,26,28,30
md5_bytes_g:
       DC16    2, 12,22, 0
       DC16    10,20,30, 8
       DC16    18,28, 6,16
       DC16   26, 4, 14,24
md5_bytes_h:
       DC16   10,16,22,28
       DC16    2, 8,14,20
       DC16   26, 0, 6,12
       DC16   18,24,30, 4
md5_bytes_i:
       DC16    0,14,28,10
       DC16   24, 6,20, 2
       DC16   16,30,12,26
       DC16    8,22, 4,18

       EVEN
md5_message:
       DB 'TEST' ;X0
       DS 0x0000, 0x8000
       DS 0x0000, 0x0000
       DS 0x0000, 0x0000 ;X3
       DS 0x0000, 0x0000
       DS 0x0000, 0x0000
       DS 0x0000, 0x0000
       DS 0x0000, 0x0000 ;x7
       DS 0x0000, 0x0000
       DS 0x0000, 0x0000
       DS 0x0000, 0x0000
       DS 0x0000, 0x0000 ;x11
       DS 0x0000, 0x0000
       DS 0x0000, 0x0000
       DS 0x0000, 0x2000
       DS 0x0000, 0x0000 ;15



       EVEN
md5_init_const:
       DC16 0x2301, 0x6745
       DC16 0xAB89, 0xEFCD
       DC16 0xDCFE, 0x98BA
       DC16 0x5476, 0x1032
       DC16 0x0000, 0x0000


       ORG     0200h        
;MD5 STATE REGISTERS 0-3
       EVEN
md5_context:
md5_a:
       DS 4
md5_b:
       DS 4
md5_c:  
       DS 4
md5_d:
       DS 4
md5_aux:
       DS 4
       EVEN
md5_len_bytes:
       DS 2
md5_len_bits:
       DS 4



END

Link to post
Share on other sites

Nice work. Keep us posted on progress.

MD5 can't compile on most MSP430 due to limitations

You mean size, right?

 

What is better, as i want to make this into a C callable library?

http://focus.ti.com/lit/an/slaa140/slaa140.pdf

 

I'm running out of registers, so should i use discrete RAM locations or should i push

What's the harm in running from RAM locations? As long as you instruct the linker not to touch those locations, you should be fine.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...