Cipher AES, How IV and CBC mode works

Share your advanced PureBasic knowledge/code with the community.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Cipher AES, How IV and CBC mode works

Post by walbus »

How IV and CBC mode works - alterable code - full working

I see again and again that it is not understood how an IV or the CBC mode works.

This almost inevitably results in a wrong application, which can have fatal consequences.

Unfortunately, the PB manual is not helpful here

I have therefore broken down the way in which it works here :wink:

This codes works primary with any block chiffres
Also you can looking how this works, for creating other, not from PB supported crypt modes
The handling of these little tricky codes is extremely simple 8)

Make a try, it is as sample available for creating CFB and OFB mode with this base codes, be creative !
Its only a little brain jogging :o

https://en.wikipedia.org/wiki/Block_cip ... _.28CFB.29

Code: Select all

; Author  W. Albus - www.nachtoptik.de
; No warranty whatsoever
; Use at your own risk

;----------------- CBC Crypting - base code ------------------

Procedure AES_Encoder(*buffer_in.quad, *buffer_out.quad, bytes, *key, *iv )
  
  Protected i, rounds=bytes>>4 -1
  Static.q Dim iv(1)
  CopyMemory(*iv, @iv(0), 16)
  
  For i=0 To rounds
    *buffer_in\q ! iv(0) : *buffer_in+8
    *buffer_in\q ! iv(1) : *buffer_in-8
    result=AESEncoder(*buffer_in ,*buffer_out ,16 ,*key ,256 ,0 , #PB_Cipher_ECB)
    CopyMemory(*buffer_out, @iv(0), 16)
    *buffer_in+16 : *buffer_out+16
  Next i
  
  ProcedureReturn result 
EndProcedure
;---------------------------------------------

Procedure AES_Decoder(*buffer_in.quad, *buffer_out.quad, bytes, *key, *iv )
  
  Protected i, rounds=bytes>>4 -1
  Static.q Dim iv(1)
  MoveMemory(*iv, @iv(0), 16)
  
  For i=0 To rounds
    result=AESDecoder(*buffer_in ,*buffer_out ,16 ,*key ,256 ,0 , #PB_Cipher_ECB)
    *buffer_out\q ! iv(0) : *buffer_out+8
    *buffer_out\q ! iv(1) : *buffer_out+8
    CopyMemory(*buffer_in, @iv(0), 16)
    *buffer_in+16
  Next i
  
  ProcedureReturn result 
EndProcedure

;---------------------------------------------
string$="aaaaaaaaaaaaaaaa"+"bbbbbbbbbbbbbbbb"+"cccccccccccccccc"+"dddddddddddddddd"
len_string=StringByteLength(string$)
*buffer_0=AllocateMemory(len_string)
string_result$=Space(len_string)

; --------------------------------------------
AES_Encoder(@string$, *buffer_0, len_string, ?KEY, ?IV )
ShowMemoryViewer(*buffer_0,len_string)

; --------------------------------------------
AES_decoder(*buffer_0, @string_result$, len_string, ?KEY, ?IV )
; ShowMemoryViewer(@string_result$,len_string)
Debug string_result$

; --------------------------------------------
DataSection ; Sample key and initialization vector for coder test demo
  KEY:
  Data.b $09 , $a9 , $20 , $40 , $35 , $b8 , $a1 , $5b , $52 , $2e , $03 ,$d5 , $34 , $11 , $00 , $08
  Data.b $11 , $b8 , $31 , $61 , $26 , $c3 , $32 , $64 , $d9 , $f3 , $01 ,$a4 , $27 , $61 , $56 , $29
  IV:
  Data.b $3d , $ae , $ba , $43 , $9d , $9e , $b5 , $30 , $b4 , $23 , $da ,$80 , $2d , $9f , $ac , $45
EndDataSection
User avatar
the.weavster
Addict
Addict
Posts: 1537
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Cipher AES, How IV and CBC mode works

Post by the.weavster »

walbus wrote:How IV and CBC mode works - alterable code - full working

I see again and again that it is not understood how an IV or the CBC mode works.

This almost inevitably results in a wrong application, which can have fatal consequences.
Hi Walbus,

It's my understanding that the IV should be different every time so that if you encrypt the same data twice you get different outcomes so I'd suggest if you are going to make a post to demonstrate the right way to do encryption that really should be part of the demo.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: Cipher AES, How IV and CBC mode works

Post by walbus »

Hi the.weavster

So far :
The IV should always be different
The IV must not depend on the password (key)
IV does not provide protection against brute forcing
The encryption is not stronger with a used IV - regardless of whether one or a thousands different IV uses
For many things the CBC mode is not the best solution, or it is even unsuitable
It is available, add the IV directly to the data
There's a lot of crap being written about it
Last edited by walbus on Thu Nov 30, 2017 10:09 am, edited 1 time in total.
Post Reply