1#! /usr/bin/env perl 2# Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. 3# 4# Licensed under the OpenSSL license (the "License"). You may not use 5# this file except in compliance with the License. You can obtain a copy 6# in the file LICENSE in the source distribution or at 7# https://www.openssl.org/source/license.html 8 9 10$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; 11push(@INC,"${dir}","${dir}../../perlasm"); 12require "x86asm.pl"; 13require "cbc.pl"; 14 15$output = pop; 16open STDOUT,">$output"; 17 18&asm_init($ARGV[0],$ARGV[$#ARGV] eq "386"); 19 20$BF_ROUNDS=16; 21$BF_OFF=($BF_ROUNDS+2)*4; 22$L="edi"; 23$R="esi"; 24$P="ebp"; 25$tmp1="eax"; 26$tmp2="ebx"; 27$tmp3="ecx"; 28$tmp4="edx"; 29 30&BF_encrypt("BF_encrypt",1); 31&BF_encrypt("BF_decrypt",0); 32&cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1); 33&asm_finish(); 34 35close STDOUT or die "error closing STDOUT: $!"; 36 37sub BF_encrypt 38 { 39 local($name,$enc)=@_; 40 41 &function_begin_B($name,""); 42 43 &comment(""); 44 45 &push("ebp"); 46 &push("ebx"); 47 &mov($tmp2,&wparam(0)); 48 &mov($P,&wparam(1)); 49 &push("esi"); 50 &push("edi"); 51 52 &comment("Load the 2 words"); 53 &mov($L,&DWP(0,$tmp2,"",0)); 54 &mov($R,&DWP(4,$tmp2,"",0)); 55 56 &xor( $tmp1, $tmp1); 57 58 # encrypting part 59 60 if ($enc) 61 { 62 &mov($tmp2,&DWP(0,$P,"",0)); 63 &xor( $tmp3, $tmp3); 64 65 &xor($L,$tmp2); 66 for ($i=0; $i<$BF_ROUNDS; $i+=2) 67 { 68 &comment(""); 69 &comment("Round $i"); 70 &BF_ENCRYPT($i+1,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,1); 71 72 &comment(""); 73 &comment("Round ".sprintf("%d",$i+1)); 74 &BF_ENCRYPT($i+2,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,1); 75 } 76 # &mov($tmp1,&wparam(0)); In last loop 77 &mov($tmp4,&DWP(($BF_ROUNDS+1)*4,$P,"",0)); 78 } 79 else 80 { 81 &mov($tmp2,&DWP(($BF_ROUNDS+1)*4,$P,"",0)); 82 &xor( $tmp3, $tmp3); 83 84 &xor($L,$tmp2); 85 for ($i=$BF_ROUNDS; $i>0; $i-=2) 86 { 87 &comment(""); 88 &comment("Round $i"); 89 &BF_ENCRYPT($i,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,0); 90 &comment(""); 91 &comment("Round ".sprintf("%d",$i-1)); 92 &BF_ENCRYPT($i-1,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,0); 93 } 94 # &mov($tmp1,&wparam(0)); In last loop 95 &mov($tmp4,&DWP(0,$P,"",0)); 96 } 97 98 &xor($R,$tmp4); 99 &mov(&DWP(4,$tmp1,"",0),$L); 100 101 &mov(&DWP(0,$tmp1,"",0),$R); 102 &function_end($name); 103 } 104 105sub BF_ENCRYPT 106 { 107 local($i,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,$enc)=@_; 108 109 &mov( $tmp4, &DWP(&n2a($i*4),$P,"",0)); # for next round 110 111 &mov( $tmp2, $R); 112 &xor( $L, $tmp4); 113 114 &shr( $tmp2, 16); 115 &mov( $tmp4, $R); 116 117 &movb( &LB($tmp1), &HB($tmp2)); # A 118 &and( $tmp2, 0xff); # B 119 120 &movb( &LB($tmp3), &HB($tmp4)); # C 121 &and( $tmp4, 0xff); # D 122 123 &mov( $tmp1, &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4)); 124 &mov( $tmp2, &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4)); 125 126 &add( $tmp2, $tmp1); 127 &mov( $tmp1, &DWP(&n2a($BF_OFF+0x0800),$P,$tmp3,4)); 128 129 &xor( $tmp2, $tmp1); 130 &mov( $tmp4, &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp4,4)); 131 132 &add( $tmp2, $tmp4); 133 if (($enc && ($i != 16)) || ((!$enc) && ($i != 1))) 134 { &xor( $tmp1, $tmp1); } 135 else 136 { 137 &comment("Load parameter 0 ($i) enc=$enc"); 138 &mov($tmp1,&wparam(0)); 139 } # In last loop 140 141 &xor( $L, $tmp2); 142 # delay 143 } 144 145sub n2a 146 { 147 sprintf("%d",$_[0]); 148 } 149 150