AES encryption algorithm PureBasic LIB available !

Share your advanced PureBasic knowledge/code with the community.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

I think we will let you optimize your lib and test yourself ;)
For now I am using the v2 of Sec's lib, and I'm happy with it ^^

next point : 1 000 000 000 000 000 loops in 5ms :lol:

edit : still looking at the SHA functions family... I think it's far easier to do than AES ^^
Last edited by newbie on Fri Aug 13, 2004 9:42 pm, edited 2 times in total.
- Registered PB user -

Using PB 4.00
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

newbie wrote:I think we will let you optimize your lib and test yourself ;)
Done that. :lol:

AMD XP 2400+

65764 (Orig.)
52896 (Sec V2)
25587 (MMX ASM)
22852 (ASM)

Intel Centrino 1400

44093 (Orig.)
41560 (Sec V2)
28321 (ASM)
27199 (MMX ASM)

50 000 000 loops; interesting to see how much the architecture influences it.

http://host4scripts.de/pub/AESLIB.zip
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

Edit³: Fixed another bug with key handling. Encryption worked previously, just the key got transformed incorrectly. Tested with ECB test vectors and is ok now.
/* Description of what tables are tested:
The provided implementations each use a different set of tables
- Java implementation: uses no tables
- reference C implementation: uses Logtable, Alogtable, S, Si, rcon
- fast C implementation: uses Logtable, Alogtable, rcon
and additionally, T1, T2, T3, T4, T5, T6, T7, T8
and (for the inverse key schedule only) U1, U2, U3, U4.
All these tables are tested.

=========================

FILENAME: "ecb_tbl.txt"

Electronic Codebook (ECB) Mode
Tables Known Answer Tests

Algorithm Name: Rijndael
Principal Submitter: Joan Daemen

==========

KEYSIZE=128


I=1
KEY=00010203050607080A0B0C0D0F101112
PT=506812A45F08C889B97F5980038B8359
CT=D8F532538289EF7D06B506A4FD5BE9C9

Edit²: Bugfixed; stripped too much previously sometimes. Speed is not bad. A 480MB file took 80 seconds to encrypt and to decrypt. A comparison between original and decrypted via commandline "fc" showed no differences.

Edit: Updated it with 00 byte padding. Need to test with a few files now.

I tried myself on a AES file en-/decryption. So far, it seems to work. Cannot make my mind up on what the best way to pad is... :roll:
Any ideas?

The code so far

(see code in follow up posting)
Last edited by Max.² on Sat Aug 14, 2004 12:27 pm, edited 6 times in total.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

Hey Max,

file encryption was the next part I wanted to try myself, but I see you are already far, I'll let you do it so ^^
What is your problem with padding ?
Just encrypt data until the last block, and only pad the last block with $0 bytes to do a 128 bits block ? is there a problem ?

Apart of that, I announce that I done a small page to merge all of the known purebasic encryption/hash algorithm :

PB crypto ressource page :
http://perso.wanadoo.fr/jugesoftware/pu ... crypto.htm


P.S : I'm tired now, but I would like to play with files tomorrow, don't give up Max ;)

EDIT : would be fine to use the same Lib to be "unified" and that for everyone could use your code. If it works, I will need it, and it would be annoying to convert Sec code to Max lib and Max code to Sec lib :?

EDIT 2 : if you do a nice file encryption/decryption code, I could add it to the page ;) and if you look carefully at it you will see that SHA is waiting for someone lol
- Registered PB user -

Using PB 4.00
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

newbie wrote:Hey Max,

file encryption was the next part I wanted to try myself, but I see you are already far, I'll let you do it so ^^
What is your problem with padding ?
Just encrypt data until the last block, and only pad the last block with $0 bytes to do a 128 bits block ? is there a problem ?

Apart of that, I announce that I done a small page to merge all of the known purebasic encryption/hash algorithm :

PB crypto ressource page :
http://perso.wanadoo.fr/jugesoftware/pu ... crypto.htm


P.S : I'm tired now, but I would like to play with files tomorrow, don't give up Max ;)

EDIT : would be fine to use the same Lib to be "unified" and that for everyone could use your code. If it works, I will need it, and it would be annoying to convert Sec code to Max lib and Max code to Sec lib :?

EDIT 2 : if you do a nice file encryption/decryption code, I could add it to the page ;) and if you look carefully at it you will see that SHA is waiting for someone lol
Padding is no problem (from the idea), but I learnt that there are several methods used with AES and each of it has it pros and cons. Maybe best is to add all (most) padding methods so users can choose.

I think there is no need to unify the libs themselves; what can be provided easily is a wrapper - some PB procedures who do the calling conversions and get each the same code running with each other lib.

Will think about it (in the queue with padding now) :wink:

Edit:

Just watched your HP; to add:

There are CRC32 and DESFingerprints available in PureBasic, both Hashes, too. And if I am not mistaken, Rings has Hash generation too in his FastFile Library.
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

newbie, Nice site.
I am on HAVAL, SHA* .
Max^2: How about other? (RSA;RC5,6;tea;solitaire;twofish / ripemd ... :wink: )
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

Thanks Max.² for the correction about the page, it is done ;)

Sec, great news ! :)
The "site" is just an inventory, an enumeration, and I hope it can be usefull, since I found that encryption was lacking in PB, even if the built-in MD5 funtion is very usefull.

You guys rocks :wink:
- Registered PB user -

Using PB 4.00
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

sec wrote:newbie, Nice site.
I am on HAVAL, SHA* .
Max^2: How about other? (RSA;RC5,6;tea;solitaire;twofish / ripemd ... :wink: )
Tea doesn't sound bad; maybe I give it a try... but first a coffee ;-)
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

newbie wrote:Thanks Max.² for the correction about the page, it is done ;)
Dunno if you saw it, but I updated the file crypt procedure some posts ago.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

Max.² wrote:
newbie wrote:Thanks Max.² for the correction about the page, it is done ;)
Dunno if you saw it, but I updated the file crypt procedure some posts ago.
Let me try it first my the Lib I use ;)

EDIT : I tested your code Max with your last Lib (from the link within the code) and it is not correct unfortunaly :-/
When encrypting a 3Ko executable, the final decrypted is 2.98Ko, and is not runable (Windows gives an error).
I just changed the target file, and input/output files, do I need to change anything else ?
- Registered PB user -

Using PB 4.00
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

Baeh, now troubles with multiples of FileReadBufferSize fixed.

---
- Fixed trouble when the file read buffer was as big as the file itself.
- Fixed another padding hick-up (a file containing only 20 20 20 20 20 20)

These padding conditions can give a head-ache!


-----
- Fixed unsigned/signed hick-up

- Freeing memory

- Updated padding method. Now the value of the padded byte is equal to the numbers of bytes to pad. If 12 bytes are needed, the pad (hex) is "0c".

---------------
Another thought:

Trouble is with files that are multiples of 16 already _and_ contain 00 bytes at the end. The decryption unpadding tries to unpad then.

That was what I meant when saying "not sure which padding method to use" :lol: Will switch to RFC2630 padding.

Edit: Ah, seems to be really a filesize issue; strangely, small files I tried worked, but a 3KB exe I made didn't. Will look into it. Yep, it indeed is a padding issue.
newbie wrote:
Max.² wrote:
newbie wrote:Thanks Max.² for the correction about the page, it is done ;)
Dunno if you saw it, but I updated the file crypt procedure some posts ago.
Let me try it first my the Lib I use ;)

EDIT : I tested your code Max with your last Lib (from the link within the code) and it is not correct unfortunaly :-/
When encrypting a 3Ko executable, the final decrypted is 2.98Ko, and is not runable (Windows gives an error).
I just changed the target file, and input/output files, do I need to change anything else ?
This is the latest version I am using; since yesterday, no matter which file type and file size, it worked correctly. Only thing I changed was key handling, but that shouldn't have an effect on the result.

Code: Select all

;http://www.host4scripts.de/pub/AESLIB.zip ; use just _1_ of the libs 
;to make life easier, FileReadBufferSize needs to be a multiplier of 16 
;Key is meant hexademical. A common way to generate a 16 byte key out of a password is to use MD5Fingerprint 

;Procedure by PB / english forum
Procedure.l hex2dec(h$)
  h$=UCase(h$)
  For r=1 To Len(h$)
  d<<4 : a$=Mid(h$,r,1)
  If Asc(a$)>60
    d+Asc(a$)-55
  Else
    d+Asc(a$)-48
  EndIf
  Next
  ProcedureReturn d

EndProcedure

Procedure AES_Crypt_File(Mode,InFilename.s,OutFileName.s,Key.s,FileReadBufferSize) 

;Mode 0 = Encrypt 
;Mode 1 = Decrypt 
 
  If FileReadBufferSize % 16 = 0 
    #OutFile  = 0 
    #InFile   = 1 
    
    FileIn=OpenFile(#InFile,InFilename.s) 
    If FileIn<>0 
      ;Key transformation
      *KeyBuffer = AllocateMemory(16)
      j=-1
      For i=1 To Len(Key.s) Step 2
        j=j+1
        PokeB(*KeyBuffer+j,Hex2Dec(Mid(Key,i,2)))     
      Next i

      KeyLengthinBits = Len(Key)*4

      AES_Gen_Tabs() 
      If Mode = 0 
        Ecx.s = Space(255) 
        AES_Encrypt_Key(*KeyBuffer, KeyLengthinBits, @Ecx) 
      ElseIf Mode = 1 
        Dcx.s = Space(255) 
        AES_Decrypt_Key(*KeyBuffer, KeyLengthinBits, @Dcx) 
      EndIf 

      *InputBuffer = AllocateMemory(FileReadBufferSize) 
      *OutputBuffer = AllocateMemory(FileReadBufferSize) 
      *PlainBlock = AllocateMemory(16) 
      *EncryptionBlock = AllocateMemory(16) 

      FileLength = FileSize(InFileName) 
      BlocksToRead = Int(FileLength/FileReadBufferSize) 

      Result=CreateFile(#OutFile,OutFileName) 
      If Result 
        For Offset = 0 To BlocksToRead 
          If OffSet<BlocksToRead 
            BlockSize = FileReadBufferSize 
          Else 
            BlockSize = FileLength-Offset*FileReadBufferSize 
          EndIf          
          ; get data from input file 
          UseFile(1) 
          FileSeek(OffSet*FileReadBufferSize) 
          Result=ReadData(*InputBuffer,BlockSize) 
          
          ;Pad (make multiple of 16bytes) if needed 
          If BlockSize<>FileReadBufferSize And Mode = 0          
            Debug "Pad!"
            PadBytes = (FileReadBufferSize - BlockSize) % 16
            Debug "Need to pad with "+Str(PadBytes)+" bytes"
            For i=0 To PadBytes-1
              ;Using pad method RFC2630 
              PokeB(*inputBuffer+Blocksize+i,PadBytes) 
            Next i 
            BlockSize = BlockSize + PadBytes 
          EndIf 
          
          ; write data to outputfile 
          UseFile(0) 
          For  EncryptionBlockOffset = 0 To FileReadBufferSize / 16 
            CopyMemory(*InputBuffer+EncryptionBlockOffset*16, *PlainBlock , 16) 
            If Mode = 0          
              AES_Encrypt(*PlainBlock,*EncryptionBlock,@Ecx) 
            ElseIf Mode = 1 
              AES_Decrypt(*PlainBlock,*EncryptionBlock,@Dcx) 
            EndIf 
            CopyMemory(*EncryptionBlock,*OutputBuffer+EncryptionBlockOffset*16,16) 
          Next EncryptionBlockOffset 
          FileSeek(OffSet*FileReadBufferSize) 
          ;Strip padded bytes 
          If mode=1 And ((offset = BlocksToRead) Or (FileLength % FileReadBufferSize = 0))


            Debug "Unpad!" 
            PaddedBytes=PeekB(*OutputBuffer+BlockSize-1)  & $FF
            Debug "Supposed # of padded bytes: "+Str(PaddedBytes)
            For i=1 To PaddedBytes
              If PeekB(*OutputBuffer+BlockSize-i)<>PaddedBytes
                Debug "False alarm. No unpadding needed."
                Break                
              EndIf
            Next i
            If i>PaddedBytes And PaddedBytes<16
              BlockSize=BlockSize-PaddedBytes
              Debug "Decreasing Block by "+Str(PaddedBytes)+" to get rid of padded bytes"
            EndIf
          EndIf 
          WriteData(*OutputBuffer,BlockSize) 
        Next Offset 
        CloseFile(#InFile) 
        CloseFile(#OutFile) 
      EndIf 
      FreeMemory(-1)
    EndIf 
  Else 
    Error = 1 
  EndIf 

EndProcedure 

start = GetTickCount_()

Key.s = "00010203050607080A0B0C0D0F101112"
InFile.s = "c:\_2.mpg"
EncryptFile.s = "c:\_Encrypt.enc"
DecryptFile.s = "c:\_Decrypt.exe"

AES_Crypt_File(0,InFile.s,EncryptFile.s,Key.s,8192*2) 
AES_Crypt_File(1,EncryptFile.s,DecryptFile.s,Key.s,8192*2)
Debug "Milliseconds: "+Str(GetTickCount_()-start)
If MD5FileFingerprint(InFile) = MD5FileFingerprint(DecryptFile)
  Debug "ok"
Else
  Debug "error"
EndIf
Last edited by Max.² on Sat Aug 14, 2004 3:58 pm, edited 2 times in total.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

Sorry but it still give me an error :-/

Code: Select all

InFile.s = "c:\toto.exe"
EncryptFile.s = "c:\toto.enc"
DecryptFile.s = "c:\toto_dec.exe"
toto.exe = 3Ko (3072 bytes)
toto.enc = 3Ko (3072 bytes)
toto_dec.exe = 2.98Ko (3057 bytes) and not a valid Win32 executable

I have tried with C:\Windows\system32\alg.exe (that I moved to C:\ first) and same, the final executable file is smaller than the original file, so there is a problem in th decryption or unpadding procedure.

EDIT : i am using the Lib at this link :
http://www.host4scripts.de/pub/AESLIB.zip
- Registered PB user -

Using PB 4.00
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post by blueb »

:(

I seem to have a problem with AES_Gen_Tabs() since upgrading to version 2.

Any ideas?

--blueb
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

newbie wrote:Sorry but it still give me an error :-/

Code: Select all

InFile.s = "c:\toto.exe"
EncryptFile.s = "c:\toto.enc"
DecryptFile.s = "c:\toto_dec.exe"
toto.exe = 3Ko (3072 bytes)
toto.enc = 3Ko (3072 bytes)
toto_dec.exe = 2.98Ko (3057 bytes) and not a valid Win32 executable

I have tried with C:\Windows\system32\alg.exe (that I moved to C:\ first) and same, the final executable file is smaller than the original file, so there is a problem in th decryption or unpadding procedure.

EDIT : i am using the Lib at this link :
http://www.host4scripts.de/pub/AESLIB.zip
Probably still a padding problem; just updated the posting again. The lib is fine. I am currently encrypting/decrypting any file on my harddisk to find file situations which may cause troubles and fixing situation one by one. :lol:

Can you upload toto.exe? Then I can try it.

BTW, files with write protection don't work, but that is a PB limitation (No matter if readfile or openfile is used).
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Post by Max.² »

blueb wrote::(

I seem to have a problem with AES_Gen_Tabs() since upgrading to version 2.

Any ideas?

--blueb
Which Library do you use? If you use sec's lib, then there is no AES_Gen_Tabs() (not needed). If you use my lib, then there is no V2. :lol:
Post Reply