📄 bf-i386.s
字号:
/* Bruce Schneier's Blowfish in i386 assembler (for linux/gcc) Author: Olaf Titz <olaf@bigred.inka.de> This code is in the public domain. $Id: bf-i386.S,v 1.2 1997/03/31 22:00:06 olaf Exp $*/#define ASM_BF_Crypt#ifdef ASM_BF_Crypt#ifndef __ASSEMBLY__#define __ASSEMBLY__#endif#include <linux/linkage.h>/* this header just defines ENTRY to make an appropriate global symbol */#define PosP0 0#define PosP17 68#define PosS0 72#define PosS1 1096#define PosS2 2120#define PosS3 3144#define KeyLenL 1042#define KeyLenB 521/* This code is optimized for speed rather than size - loops unrolled. etc. *//* Endian-ness is taken care of by (a) the order of shifts in the Round macro and (b) the order of shifts below under the ukx label. The key tables and user data are stored and processed in the CPU byte order.*//* Do one round */#define Round(lw,rw) \ movl rw, %edx; \ shrl $24, %edx; \ movl PosS0(%edi,%edx,4), %eax; \ movl rw, %edx; \ shrl $16, %edx; \ andl $0xFF, %edx; \ addl PosS1(%edi,%edx,4), %eax; \ movl rw, %edx; \ shrl $8, %edx; \ andl $0xFF, %edx; \ xorl PosS2(%edi,%edx,4), %eax; \ movl rw, %edx; \ andl $0xFF, %edx; \ addl PosS3(%edi,%edx,4), %eax; \ xorl %eax, lw; \ lodsl; \ xorl %eax, lw/* Words in %ebx, %ecx - Key in %edi - P-index in %esi - result swapped */blowfish: lodsl xorl %eax, %ebx Round(%ecx,%ebx); Round(%ebx,%ecx) Round(%ecx,%ebx); Round(%ebx,%ecx) Round(%ecx,%ebx); Round(%ebx,%ecx) Round(%ecx,%ebx); Round(%ebx,%ecx) Round(%ecx,%ebx); Round(%ebx,%ecx) Round(%ecx,%ebx); Round(%ebx,%ecx) Round(%ecx,%ebx); Round(%ebx,%ecx) Round(%ecx,%ebx); Round(%ebx,%ecx) lodsl xorl %eax, %ecx ret/* void Blowfish_Encrypt(Blowfish_Data dataIn, Blowfish_Data dataOut, Blowfish_Key key);*/ENTRY(Blowfish_Encrypt) pushl %ebx pushl %ebp pushl %esi pushl %edi#define SAVE 16 /* no. of bytes the saved registers occupy *//* arguments relative to %esp */#define dataIn SAVE+4#define dataOut SAVE+8#define key SAVE+12 movl dataIn(%esp), %esi movl (%esi), %ebx movl 4(%esi), %ecx movl key(%esp), %edi movl %edi, %esi cld call blowfish movl dataOut(%esp), %edi movl %ebx, 4(%edi) movl %ecx, (%edi) popl %edi popl %esi popl %ebp popl %ebx ret#undef dataIn#undef dataOut#undef key/* void Blowfish_Decrypt(Blowfish_Data dataIn, Blowfish_Data dataOut, Blowfish_Key key);*/ENTRY(Blowfish_Decrypt) pushl %ebx pushl %ebp pushl %esi pushl %edi#define SAVE 16 /* no. of bytes the saved registers occupy *//* arguments relative to %esp */#define dataIn SAVE+4#define dataOut SAVE+8#define key SAVE+12 movl dataIn(%esp), %esi movl (%esi), %ebx movl 4(%esi), %ecx movl key(%esp), %edi movl %edi, %esi leal PosP17(%edi), %esi std call blowfish movl dataOut(%esp), %edi movl %ebx, 4(%edi) movl %ecx, (%edi) popl %edi popl %esi popl %ebp popl %ebx ret#undef dataIn#undef dataOut#undef key/* load byte from key, start over if exhausted */#define lodsbw(base,len) \ lodsb; \ decl %ecx; \ cmpl $0, %ecx; \ jg 1f; \ movl base, %esi; \ movl len, %ecx; \1:/* void Blowfish_ExpandUserKey(Blowfish_UserKey userKey, int userKeyLen, Blowfish_Key key);*/ENTRY(Blowfish_ExpandUserKey) pushl %ebx pushl %ebp pushl %esi pushl %edi#define SAVE 16 /* no. of bytes the saved registers occupy *//* arguments relative to %esp */#define userKey SAVE+4#define userKeyLen SAVE+8#define key SAVE+12 /* Copy the init vector into key */ leal bftab0, %esi movl key(%esp), %edi movl $KeyLenL, %ecx cld rep; movsl /* XOR the user key into the P table */ movl key(%esp), %edi movl $18, %ebp movl userKey(%esp), %esi movl userKeyLen(%esp), %ecxukx: /* process one 32-bit word swapped */ lodsbw(userKey(%esp), userKeyLen(%esp)) shll $8, %eax lodsbw(userKey(%esp), userKeyLen(%esp)) shll $8, %eax lodsbw(userKey(%esp), userKeyLen(%esp)) shll $8, %eax lodsbw(userKey(%esp), userKeyLen(%esp)) xorl %eax, (%edi) addl $4, %edi decl %ebp cmpl $0, %ebp jg ukx /* Now do the repeated encryption process */ xorl %ebx, %ebx xorl %ecx, %ecx movl $KeyLenB, %ebp movl key(%esp), %ediukb: pushl %edi movl key+4(%esp), %edi movl %edi, %esi call blowfish popl %edi xchgl %ebx, %ecx movl %ebx, (%edi) movl %ecx, 4(%edi) addl $8, %edi decl %ebp cmpl $0, %ebp jg ukb popl %edi popl %esi popl %ebp popl %ebx ret#undef dataIn#undef dataOut#undef key/* The initialization key. According to Schneier, this is not a magic pattern but simply the first 33344 (after point) bits of "pi". */.align 4bftab0:/* The eighteen P boxes @ 1 word */.long 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344.long 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89.long 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c.long 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917.long 0x9216d5d9, 0x8979fb1b/* The four S boxes @ 256 words */.long 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7.long 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99.long 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16.long 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e.long 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee.long 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013.long 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef.long 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e.long 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60.long 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440.long 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce.long 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -