rdDecrypt128 (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

ALL

Firmware version:

2.64 / 1.00.00


The rdDecrypt128 function will decrypt data by using Rijndael encryption algorithm with a 128 bit key.

The function decrypts the data in blocks of 16 bytes. If the data is not a multiple of 16, the result of the operation is undefined.

 

 

Input:

key  : PTR

Array containing the key used to decrypt the data.

Length of data must be 16 bytes = 128 bits.

 

buf_in  : PTR

Buffer containing the encrypted data to decrypt.

 

buf_out  : PTR

Buffer where the decrypted data is stored.

 

out_len  : INT

Size of "buf_out" in number of bytes.

 

blk  : INT

The number of block from the raw data to decrypt. Each block is 16 bytes long.

 

Returns: INT

0

Data is decrypted.

-1

Input buffer and output buffer cannot be the same.

-2

Invalid number of blocks.

-3

Encryption key is missing.

-4

Input buffer is missing.

-5

Output buffer is missing.

-6

"buf_out" buffer too small for decrypted data.

 

Declaration:

FUNCTION rdDecrypt128 : INT;
VAR_INPUT
  key     : PTR;
  buf_in : PTR;
  buf_out : PTR;
  out_len : INT;
  blk     : INT;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
VAR
  key   : ARRAY [1..16] OF SINT;
  src   : ARRAY [1..64] OF SINT;
  crypt : ARRAY [1..64] OF SINT;
  dst   : ARRAY [1..64] OF SINT;
END_VAR;
 
PROGRAM example;
VAR
  len : INT;
  str : STRING;
END_VAR;
 
  // Setup key
  key[01] := 16#F4;   key[02] := 16#98;   key[03] := 16#D8;   key[04] := 16#16;
  key[05] := 16#22;   key[06] := 16#4F;   key[07] := 16#47;   key[08] := 16#3F;
  key[09] := 16#91;   key[10] := 16#85;   key[11] := 16#96;   key[12] := 16#B8;
  key[13] := 16#DD;   key[14] := 16#57;   key[15] := 16#51;   key[16] := 16#BE;
 
  // Encrypt a string
  str := "This is a test of encryption";
  DebugMsg(message := "Original= " + str);
  strToMemory(dst := ADDR(src[2]), str := str, len := strLen(str := str));
  src[1] := SINT(strLen(str := str));
  len   := (src[1] + 1) / 16;
  IF (src[1] + 1) MOD 16 > 0 THEN len := len + 1; END_IF;
  rdEncrypt128(key := ADDR(key), buf_in := ADDR(src), buf_out := ADDR(crypt), out_len := 64, blk := len);
 
  ...
 
  // Decrypt a string
  rdDecrypt128(key := ADDR(key), buf_in := ADDR(crypt), buf_out := ADDR(dst), out_len := 64, blk := len);
  DebugMsg(message := "Decrypted= " + strFromMemory(src := ADDR(dst[2]), len := dst[1]));
 
END_PROGRAM;