CryptoSys API Library Manual

CIPHER_StreamInit

Initializes the CIPHERSTREAM context ready for repeated operations of CIPHER_StreamUpdate()

VBA/VB6 Syntax

Public Declare Function CIPHER_StreamInit Lib "diCryptoSys.dll" (ByRef lpKey As Byte, ByVal nKeyLen As Long, ByRef lpIV As Byte, ByVal nIvLen As Long, ByVal nCounter As Long, ByVal nOptions As Long) As Long

nRet = CIPHER_StreamInit(abKey(0), nKeyLen, abIV(0), nIvLen, nCounter, nOptions)

C/C++ Syntax

long __stdcall CIPHER_StreamInit(const unsigned char *lpKey, long nKeyLen, unsigned char *lpIV, long nIvLen, long nCounter, long nOptions);

Parameters

lpKey
[in] array containing the key.
nKeyLen
[in] specifying the length of the key in bytes.
lpIV
[in] array containing the initialization vector (IV, nonce), if required.
nIvLen
[in] specifying the length of the IV in bytes.
nCounter
[in] the value of the counter (ChaCha20 only).
nOptions
[in] option flags:
Select one of the following:
API_SC_ARCFOUR to use ARCFOUR (RC4)
API_SC_SALSA20 to use Salsa20
API_SC_CHACHA20 to use ChaCha20

Returns (VBA/C)

If successful, the return value is a non-zero handle to the new CIPHERSTREAM context. Returns zero if an error occurs.

.NET Equivalent

CipherStream.Init Method

Remarks

It is important to check for a zero context handle and stop if one occurs. Use API_ErrorCode to find the error code of the error that occured.

Example

' Ref: `draft-strombergson-chacha-test-vectors-02.txt` TC7: Sequence patterns in key and IV. Rounds: 20
Dim nRet As Long
Dim strFileIn As String
Dim strFileOut As String
Dim abKey() As Byte
Dim abIV() As Byte
Dim abInput() As Byte
Dim lpOutput() As Byte
Dim nKeyLen As Long
Dim nIvLen As Long
Dim strCorrect As String
Dim nDataLen As Long
Dim hContext As Long

' Use incremental functions to encrypt a 65-byte input of zeros in chunks of 1, 62 and 2 bytes
Debug.Print "CHACHA20 (INCREMENTAL):"
strCorrect = "9fadf409c00811d00431d67efbd88fba59218d5d6708b1d685863fabbb0e961eea480fd6fb532bfd494b2151015057423ab60a63fe4f55f7a212e2167ccab931fb"
' Obtain parameters in byte array format
abKey = cnvBytesFromHexStr("00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100")
abIV = cnvBytesFromHexStr("0f1e2d3c4b5a6978")
nKeyLen = UBound(abKey) + 1
nIvLen = UBound(abIV) + 1
Debug.Print "Key: " & cnvHexStrFromBytes(abKey)
Debug.Print "IV:  " & cnvHexStrFromBytes(abIV)

' Initialize CIPHERSTREAM context
hContext = CIPHER_StreamInit(abKey(0), nKeyLen, abIV(0), nIvLen, 0, API_SC_CHACHA20)
If (hContext = 0) Then
    Debug.Print "ERROR: " + API_ErrorCode()
    Exit Function
End If

' Part 1: block of 1 byte
nDataLen = 1
ReDim abInput(nDataLen - 1)   ' Set to zero by default
ReDim lpOutput(UBound(abInput))
nRet = CIPHER_StreamUpdate(hContext, lpOutput(0), abInput(0), nDataLen)
Debug.Print cnvHexStrFromBytes(lpOutput)

' Part 2: block of 62 bytes
nDataLen = 62
ReDim abInput(nDataLen - 1)
ReDim lpOutput(UBound(abInput))
nRet = CIPHER_StreamUpdate(hContext, lpOutput(0), abInput(0), nDataLen)
Debug.Print cnvHexStrFromBytes(lpOutput)

' Part 3: block of 2 bytes
nDataLen = 2
ReDim abInput(nDataLen - 1)
ReDim lpOutput(UBound(abInput))
nRet = CIPHER_StreamUpdate(hContext, lpOutput(0), abInput(0), nDataLen)
Debug.Print cnvHexStrFromBytes(lpOutput)

Debug.Print "CORRECT="
Debug.Print strCorrect

' We are done with context
nRet = CIPHER_StreamFinal(hContext)
Debug.Print "CIPHER_StreamFinal retrurns " & nRet & " (expecting 0)"

This should result in output as follows:

CHACHA20 (INCREMENTAL):
Key: 00112233445566778899AABBCCDDEEFFFFEEDDCCBBAA99887766554433221100
IV:  0F1E2D3C4B5A6978
9F
ADF409C00811D00431D67EFBD88FBA59218D5D6708B1D685863FABBB0E961EEA480FD6FB532BFD494B2151015057423AB60A63FE4F55F7A212E2167CCAB9
31FB
CORRECT=
9fadf409c00811d00431d67efbd88fba59218d5d6708b1d685863fabbb0e961eea480fd6fb532bfd494b2151015057423ab60a63fe4f55f7a212e2167ccab931fb
CIPHER_StreamFinal retrurns 0 (expecting 0)

See Also

CIPHER_StreamUpdate CIPHER_StreamFinal

[Contents] [Index]

[PREV: CIPHER_StreamHex...]   [Contents]   [Index]   
   [NEXT: CIPHER_StreamUpdate...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.