Unit Crypto
Return to Unit Reference
Description
Ultibo Crypto interface unit
To be documented
Constants
CRYPTO_CIPHER_ALG_*
CRYPTO_CIPHER_ALG_NONE = 0;
|
|
CRYPTO_CIPHER_ALG_AES = 1;
|
|
CRYPTO_CIPHER_ALG_DES = 2;
|
|
CRYPTO_CIPHER_ALG_3DES = 3;
|
|
CRYPTO_CIPHER_ALG_RC4 = 4;
|
CRYPTO_CIPHER_MODE_*
CRYPTO_CIPHER_MODE_ECB = 0;
|
Electronic Codebook |
CRYPTO_CIPHER_MODE_CBC = 1;
|
Cipher Block Chaining |
CRYPTO_CIPHER_MODE_CFB = 2;
|
Cipher Feedback |
CRYPTO_CIPHER_MODE_OFB = 3;
|
Output Feedback |
CRYPTO_CIPHER_MODE_CTR = 4;
|
Counter |
CRYPTO_CIPHER_MODE_CCM = 5;
|
Counter with CBC-MAC |
CRYPTO_CIPHER_MODE_GCM = 5;
|
Galois/Counter Mode |
CRYPTO_HASH_ALG_*
CRYPTO_HASH_ALG_NONE = 0;
|
|
CRYPTO_HASH_ALG_MD5 = 1;
|
|
CRYPTO_HASH_ALG_SHA1 = 2;
|
|
CRYPTO_HASH_ALG_SHA256 = 3;
|
|
CRYPTO_HASH_ALG_HMAC_MD5 = 4;
|
|
CRYPTO_HASH_ALG_HMAC_SHA1 = 5;
|
|
CRYPTO_HASH_ALG_HMAC_SHA256 = 6;
|
|
CRYPTO_HASH_ALG_SHA384 = 7;
|
|
CRYPTO_HASH_ALG_SHA512 = 8;
|
|
CRYPTO_HASH_ALG_HMAC_SHA384 = 9;
|
|
CRYPTO_HASH_ALG_HMAC_SHA512 = 10;
|
AES_BLOCK_*
AES_BLOCK_SIZE = 16;
|
128 bit blocks |
AES_KEY_*
AES_KEY_SIZE128 = 16;
|
128 bit keys |
AES_KEY_SIZE192 = 24;
|
192 bit keys |
AES_KEY_SIZE256 = 32;
|
256 bit keys |
DES_BLOCK_*
DES_BLOCK_SIZE = 8;
|
64 bit blocks |
DES_KEY_*
DES_KEY_SIZE = 8;
|
64 bit keys |
DES_KEYTYPE_*
DES_KEYTYPE_ENCRYPT = 0;
|
|
DES_KEYTYPE_DECRYPT = 1;
|
|
DES_BYTEBIT:array[0..7] of LongWord = (
| |
$80, $40, $20, $10, $08, $04, $02, $01);
|
DES3_BLOCK_*
DES3_BLOCK_SIZE = 8;
|
64 bit blocks |
DES3_KEY_*
DES3_KEY_SIZE = 24;
|
192 bit (3 x 64 bit) keys |
SHA1_*
SHA1_K20 = $5A827999;
|
|
SHA1_K40 = $6ED9EBA1;
|
|
SHA1_K60 = $8F1BBCDC;
|
|
SHA1_K80 = $CA62C1D6;
|
RSA_*
RSA_MODULUS_BYTES_MAX = 512;
|
4096 bit maximum |
BASE64_*
Base64EncodeTable:String = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
Type definitions
MD5 digest
PMD5Digest = ^TMD5Digest;
TMD5Digest = array[0..15] of Byte;
MD5 block
PMD5Block = ^TMD5Block;
TMD5Block = record
Data:Pointer;
|
|
Size:LongWord;
|
|
Next:PMD5Block;
|
MD5 context
PMD5Context = ^TMD5Context;
TMD5Context = record
Data:array[0..63] of Byte;
|
|
Buffer:array[0..3] of LongWord;
|
|
Count:QWord;
|
MD5 byte buffer
PMD5ByteBuffer = ^TMD5ByteBuffer;
TMD5ByteBuffer = array[0..63] of Byte;
MD5 long buffer
PMD5LongBuffer = ^TMD5LongBuffer;
TMD5LongBuffer = array[0..15] of LongWord;
AES key
PAESKey = ^TAESKey;
TAESKey = record
Rounds:LongWord;
|
|
EncryptKey:array[0..59] of LongWord;
|
|
DecryptKey:array[0..59] of LongWord;
|
AES context
PAESContext = ^TAESContext;
TAESContext = record
AESKey:TAESKey;
|
|
CBC:array[0..AES_BLOCK_SIZE - 1] of Byte;
|
|
CTR:array[0..AES_BLOCK_SIZE - 1] of Byte;
|
DES key
PDESKey = ^TDESKey;
TDESKey = array[0..31] of LongWord;
DES context
PDESContext = ^TDESContext;
TDESContext = record
EncryptKey:TDESKey;
|
|
DecryptKey:TDESKey;
|
|
CBC:array[0..DES_BLOCK_SIZE - 1] of Byte;
|
3DES key
PDES3Key = ^TDES3Key;
TDES3Key = record
EncryptKey:array[0..2] of TDESKey;
|
|
DecryptKey:array[0..2] of TDESKey;
|
3DES context
PDES3Context = ^TDES3Context;
TDES3Context = record
DES3Key:TDES3Key;
|
|
CBC:array[0..DES3_BLOCK_SIZE - 1] of Byte;
|
RC4 state
PRC4State = ^TRC4State;
TRC4State = array[0..255] of Byte;
RC4 context
PRC4Context = ^TRC4Context;
TRC4Context = record
Key:array[0..31] of Byte;
|
|
KeySize:LongWord;
|
|
Start:LongWord;
|
SHA1 digest
PSHA1Digest = ^TSHA1Digest;
TSHA1Digest = array[0..19] of Byte;
SHA1 block
PSHA1Block = ^TSHA1Block;
TSHA1Block = record
Data:Pointer;
|
|
Size:LongWord;
|
|
Next:PSHA1Block;
|
SHA1 context
PSHA1Context = ^TSHA1Context;
TSHA1Context = record
Data:array[0..63] of Byte;
|
|
State:array[0..4] of LongWord;
|
|
Count:QWord;
|
SHA1 byte buffer
PSHA1ByteBuffer = ^TSHA1ByteBuffer;
TSHA1ByteBuffer = array[0..63] of Byte;
SHA1 long buffer
PSHA1LongBuffer = ^TSHA1LongBuffer;
TSHA1LongBuffer = array[0..15] of LongWord;
SHA256 digest
PSHA256Digest = ^TSHA256Digest;
TSHA256Digest = array[0..31] of Byte;
SHA256 block
PSHA256Block = ^TSHA256Block;
TSHA256Block = record
Data:Pointer;
|
|
Size:LongWord;
|
|
Next:PSHA256Block;
|
SHA256 context
PSHA256Context = ^TSHA256Context;
TSHA256Context = record
Data:array[0..63] of Byte;
|
|
State:array[0..7] of LongWord;
|
|
Count:QWord;
|
SHA256 byte buffer
PSHA256ByteBuffer = ^TSHA256ByteBuffer;
TSHA256ByteBuffer = array[0..63] of Byte;
SHA256 long buffer
PSHA256LongBuffer = ^TSHA256LongBuffer;
TSHA256LongBuffer = array[0..15] of LongWord;
SHA256 algorithm
PSHA256_W = ^TSHA256_W;
TSHA256_W = array[0..63] of LongWord;
Note: This is the W array for the SHA256 algorithm | |
SHA512 digest
PSHA512Digest = ^TSHA512Digest;
TSHA512Digest = array[0..63] of Byte;
SHA512 block
PSHA512Block = ^TSHA512Block;
TSHA512Block = record
Data:Pointer;
|
|
Size:LongWord;
|
|
Next:PSHA512Block;
|
SHA512 context
PSHA512Context = ^TSHA512Context;
TSHA512Context = record
Data:array[0..127] of Byte;
|
|
State:array[0..7] of QWord;
|
|
Count:QWord;
|
SHA512 byte buffer
PSHA512ByteBuffer = ^TSHA512ByteBuffer;
TSHA512ByteBuffer = array[0..127] of Byte;
SHA512 algorithm
PSHA512_W = ^TSHA512_W;
TSHA512_W = array[0..79] of QWord;
Note: This is the W array for the SHA512 algorithm | |
SHA384 digest
PSHA384Digest = ^TSHA384Digest;
TSHA384Digest = array[0..47] of Byte;
SHA384 block
PSHA384Block = PSHA512Block;
TSHA384Block = TSHA512Block;
SHA384 context
PSHA384Context = PSHA512Context;
TSHA384Context = TSHA512Context;
SHA384 byte buffer
PSHA384ByteBuffer = PSHA512ByteBuffer;
TSHA384ByteBuffer = TSHA512ByteBuffer;
RSA context
PRSAContext = ^TRSAContext;
TRSAContext = record
M:PBigInt;
|
Modulus |
E:PBigInt;
|
Public exponent |
D:PBigInt;
|
Private exponent |
P:PBigInt;
|
p in m = pq |
Q:PBigInt;
|
q in m = pq |
DP:PBigInt;
|
d mod (p-1) |
DQ:PBigInt;
|
d mod (q-1) |
QInv:PBigInt;
|
q^-1 mod p |
ModulusLen:Integer;
|
|
Context:PBigIntContext;
|
Get random bytes
TGetRandomBytes = function(Buffer:PByte; Count:Integer):Boolean;
|
Base64 decode table
TBase64DecodeTable = array[0..255] of Char;
|
Hash context
PHashContext = ^THashContext;
THashContext = record
Algorithm:LongWord;
|
|
Key:array[0..127] of Byte;
|
|
KeySize:LongWord;
|
|
case Integer of
|
|
0:(MD5:TMD5Context);
|
|
1:(SHA1:TSHA1Context);
|
|
2:(SHA256:TSHA256Context);
|
|
3:(SHA384:TSHA384Context);
|
|
4:(SHA512:TSHA512Context);
|
Cipher context
PCipherContext = ^TCipherContext;
TCipherContext = record
Algorithm:LongWord;
|
|
Mode:LongWord;
|
|
case Integer of
|
|
0:(RC4:TRC4Context);
|
|
1:(DES:TDESContext);
|
|
2:(DES3:TDES3Context);
|
|
3:(AES:TAESContext);
|
Public variables
Crypto specific variables
CryptoInitialized:Boolean;
|
MIME64 variables
MIME64DecodingTable:String;
|
Function declarations
Initialization functions
procedure CryptoInit;
Note | None documented |
---|
Crypto functions
function HashCreate(Algorithm:LongWord; Key:Pointer; KeySize:LongWord):PHashContext;
Note | None documented |
---|
function HashDestroy(Context:PHashContext):Boolean;
Note | None documented |
---|
function HashUpdate(Context:PHashContext; Data:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
function HashFinish(Context:PHashContext; Digest:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
function CipherCreate(Algorithm:LongWord; Vector,Key:Pointer; KeySize:LongWord):PCipherContext;
Note | For block ciphers also include an initialization vector for CBC mode |
---|
function CipherDestroy(Context:PCipherContext):Boolean;
Note | None documented |
---|
function CipherEncrypt(Context:PCipherContext; Plain,Crypt:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
function CipherDecrypt(Context:PCipherContext; Crypt,Plain:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
MD5 functions
function MD5DigestData(Data:PMD5Block; Digest:PMD5Digest):Boolean;
Data | A linked list which can contain multiple independent blocks to be included in the hash. The data block itself does not form part of the hash. |
---|
function MD5DigestString(const Value:String; Digest:PMD5Digest):Boolean;
Note | None documented |
---|
function HMACMD5DigestData(const Key:String; Data:PMD5Block; Digest:PMD5Digest):Boolean;
Key | If Key is more than 64 bytes it will be hashed to Key = MD5(Key) instead. If Key is less than 64 bytes it will be padded with zeros. |
---|---|
Note | The MD5 HMAC algorithm is: MD5(Key xor oPad, MD5(Key xor iPad, Data)). Where iPad is the byte $36 repeated 64 times, oPad is the byte $5c repeated 64 times. |
function HMACMD5DigestString(const Key,Value:String; Digest:PMD5Digest):Boolean;
Key | If Key is more than 64 bytes it will be hashed to Key = MD5(Key) instead. If Key is less than 64 bytes it will be padded with zeros. |
---|---|
Note | The MD5 HMAC algorithm is: MD5(Key xor oPad, MD5(Key xor iPad, Value)). Where iPad is the byte $36 repeated 64 times, oPad is the byte $5c repeated 64 times. |
AES functions
function AESEncryptData(Key:Pointer; KeySize:LongWord; Vector,Plain,Crypt:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
function AESDecryptData(Key:Pointer; KeySize:LongWord; Vector,Crypt,Plain:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
DES functions
function DESEncryptData(Key:Pointer; KeySize:LongWord; Vector,Plain,Crypt:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
function DESDecryptData(Key:Pointer; KeySize:LongWord; Vector,Crypt,Plain:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
3DES functions
function DES3EncryptData(Key:Pointer; KeySize:LongWord; Vector,Plain,Crypt:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
function DES3DecryptData(Key:Pointer; KeySize:LongWord; Vector,Crypt,Plain:Pointer; Size:LongWord):Boolean;
Note | None documented |
---|
RC4 functions
function RC4EncryptData(Key:Pointer; KeySize:LongWord; Plain,Crypt:Pointer; Size,Start:LongWord):Boolean;
Start | Specify how many bytes of the RC4 cipher stream to skip to allow for previous blocks of data or to comply with RFC4345 requirements to discard the first 1536 bytes of the RC4 cipher stream |
---|
function RC4DecryptData(Key:Pointer; KeySize:LongWord; Crypt,Plain:Pointer; Size,Start:LongWord):Boolean; inline;
Note | None documented |
---|
SHA1 functions
function SHA1DigestData(Data:PSHA1Block; Digest:PSHA1Digest):Boolean;
Data | A linked list which can contain multiple independent blocks to be included in the hash. The data block itself does not form part of the hash. |
---|
function SHA1DigestString(const Value:String; Digest:PSHA1Digest):Boolean;
Note | None documented |
---|
function HMACSHA1DigestData(const Key:String; Data:PSHA1Block; Digest:PSHA1Digest):Boolean;
Key | If Key is more than 64 bytes it will be hashed to Key = SHA1(Key) instead. If Key is less than 64 bytes it will be padded with zeros. |
---|---|
Note | The SHA1 HMAC algorithm is: SHA1(Key xor oPad, SHA1(Key xor iPad, Data)). Where iPad is the byte $36 repeated 64 times, oPad is the byte $5c repeated 64 times. |
function HMACSHA1DigestString(const Key,Value:String; Digest:PSHA1Digest):Boolean;
Key | If Key is more than 64 bytes it will be hashed to Key = SHA1(Key) instead. If Key is less than 64 bytes it will be padded with zeros. |
---|---|
Note | The SHA1 HMAC algorithm is: SHA1(Key xor oPad, SHA1(Key xor iPad, Value)). Where iPad is the byte $36 repeated 64 times, oPad is the byte $5c repeated 64 times. |
SHA256 functions
function SHA256DigestData(Data:PSHA256Block; Digest:PSHA256Digest):Boolean;
Data | A linked list which can contain multiple independent blocks to be included in the hash. The data block itself does not form part of the hash. |
---|
function SHA256DigestString(const Value:String; Digest:PSHA256Digest):Boolean;
Note | None documented |
---|
function HMACSHA256DigestData(const Key:String; Data:PSHA256Block; Digest:PSHA256Digest):Boolean;
Key | If Key is more than 64 bytes it will be hashed to Key = SHA256(Key) instead. If Key is less than 64 bytes it will be padded with zeros. |
---|---|
Note | The SHA256 HMAC algorithm is: SHA256(Key xor oPad, SHA256(Key xor iPad, Data)). Where iPad is the byte $36 repeated 64 times, oPad is the byte $5c repeated 64 times. |
function HMACSHA256DigestString(const Key,Value:String; Digest:PSHA256Digest):Boolean;
Key | If Key is more than 64 bytes it will be hashed to Key = SHA256(Key) instead. If Key is less than 64 bytes it will be padded with zeros. |
---|---|
Note | The SHA256 HMAC algorithm is: SHA256(Key xor oPad, SHA256(Key xor iPad, Value)). Where iPad is the byte $36 repeated 64 times, oPad is the byte $5c repeated 64 times. |
CRC functions
function CRC16CCITT(CRC:Word; Data:PByte; Size:LongWord):Word;
Note | None documented |
---|
MIME64 functions
function MIME64EncodeString(const AValue:String):String;
Note | None documented |
---|
function MIME64DecodeString(const AValue:String):String;
Note | None documented |
---|
Crypto helper functions
procedure BytesToLE(Buffer:PByte; Count:LongWord);
Note | None documented |
---|
procedure BytesToBE(Buffer:PByte; Count:LongWord);
Note | None documented |
---|
function LongWordToBE(Buffer:PByte):LongWord; inline;
Note | None documented |
---|
procedure BEToLongWord(Value:LongWord; Buffer:PByte); inline;
Note | None documented |
---|
MD5 helper functions
procedure MD5Init(var Context:TMD5Context);
Note | None documented |
---|
procedure MD5Update(var Context:TMD5Context; Data:Pointer; Size:LongWord);
Note | None documented |
---|
procedure MD5Final(var Context:TMD5Context; var Digest:TMD5Digest);
Note | None documented |
---|
procedure MD5Transform(var Context:TMD5Context; Buffer:Pointer);
Note | None documented |
---|
function MD5DigestToString(Digest:PMD5Digest):String;
Note | None documented |
---|
AES helper functions
function AESTE0(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTE1(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTE2(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTE3(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTD0(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTD1(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTD2(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTD3(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTE4_0(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTE4_1(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTE4_2(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESTE4_3(I:Byte):LongWord; inline;
Note | None documented |
---|
function AESByte(X,N:LongWord):Byte; inline;
Note | None documented |
---|
function AESKeySetup(Key:Pointer; KeySize:LongWord; AESKey:PAESKey):Boolean;
Note | None documented |
---|
procedure AESEncryptBlock(Plain,Crypt:Pointer; AESKey:PAESKey);
Note | None documented |
---|
procedure AESDecryptBlock(Crypt,Plain:Pointer; AESKey:PAESKey);
Note | None documented |
---|
DES helper functions
procedure DESKey(Key:PByte; KeyType:LongWord; CryptKey:PDESKey);
Note | None documented |
---|
procedure DESCook(Raw1:PLongWord; Key:PDESKey);
Note | None documented |
---|
procedure DESProcess(Block,Key:PLongWord);
Note | None documented |
---|
function DESKeySetup(Key:Pointer; KeySize:LongWord; EncryptKey,DecryptKey:PDESKey):Boolean;
Note | None documented |
---|
procedure DESEncryptBlock(Plain,Crypt:Pointer; EncryptKey:PDESKey);
Note | None documented |
---|
procedure DESDecryptBlock(Crypt,Plain:Pointer; DecryptKey:PDESKey);
Note | None documented |
---|
3DES helper functions
function DES3KeySetup(Key:Pointer; KeySize:LongWord; DES3Key:PDES3Key):Boolean;
Note | None documented |
---|
procedure DES3EncryptBlock(Plain,Crypt:Pointer; DES3Key:PDES3Key);
Note | None documented |
---|
procedure DES3DecryptBlock(Crypt,Plain:Pointer; DES3Key:PDES3Key);
Note | None documented |
---|
SHA1 helper functions
procedure SHA1Init(var Context:TSHA1Context);
Note | None documented |
---|
procedure SHA1Update(var Context:TSHA1Context; Data:Pointer; Size:LongWord);
Note | None documented |
---|
procedure SHA1Final(var Context:TSHA1Context; var Digest:TSHA1Digest);
Note | None documented |
---|
procedure SHA1Transform(var Context:TSHA1Context; Buffer:Pointer);
Note | None documented |
---|
function SHA1DigestToString(Digest:PSHA1Digest):String;
Note | None documented |
---|
SHA256 helper functions
procedure SHA256Init(var Context:TSHA256Context);
Note | None documented |
---|
procedure SHA256Process(var Context:TSHA256Context; Data:Pointer; Size:LongWord);
Note | None documented |
---|
procedure SHA256Complete(var Context:TSHA256Context; var Digest:TSHA256Digest);
Note | None documented |
---|
procedure SHA256Compress(var Context:TSHA256Context; Buffer:Pointer);
Note | None documented |
---|
function SHA256DigestToString(Digest:PSHA256Digest):String;
Note | None documented |
---|
MIME64 helper functions
procedure MIME64InitTables;
Note | None documented |
---|
Return to Unit Reference