CmpCrypto CryptoGenerateHash Not Outputting

otbeka
2024-09-06
2024-09-06
  • otbeka - 2024-09-06

    Hi, I have been trying to use CryptoGenerateHash from the CmpCrypto Implementation library. My code is taken almost directly from the CryptoDemo.project example provided on Codesys Forge, yet the CryptoGenerateHash function does not write to the address listed in pHash. RTS_IEC_RESULT is OK, but I am getting nothing out of the function. No errors either, all my libraries are up to date.

    Any help would be appreicated!

    PROGRAM PLC_PRG
    VAR
    
        sMessage : MESSAGE := 'The red fox runs across the ice';
        abyHashCode : HASH_CODE := [
        16#52, 16#ED, 16#87, 16#9E, 16#70,
        16#F7, 16#1D, 16#92, 16#6E, 16#B6,
        16#95, 16#70, 16#08, 16#E0, 16#3C,
        16#E4, 16#CA, 16#69, 16#45, 16#D3
        ];
        xMessageOK : BOOL;
    END_VAR
    
    xMessageOK := CheckMessage(sMessage, abyHashCode);
    
    FUNCTION CheckMessage : BOOL
    VAR_INPUT
        sMessage : REFERENCE TO MESSAGE;
        abyHashCode : REFERENCE TO HASH_CODE;
    END_VAR
    VAR
        _hHASH : RTS_IEC_HANDLE := CryptoGetAlgorithmById(ui32CryptoID:=RtsCryptoID.HASH_SHA1, pResult:=0);
        Result : RTS_IEC_RESULT;
        bsMessage : RtsByteString := (ui32MaxLen:=SIZEOF(sMessage), pByData:=ADR(sMessage), ui32Len:=TO_UDINT(LEN(sMessage)));
        abyNewHashCode : HASH_CODE;
        bsNewHashCode : RtsByteString := (ui32MaxLen:=SIZEOF(abyNewHashCode), pByData:=ADR(abyNewHashCode));
        diCmpResult : DINT;
    END_VAR
    
    Result := CryptoGenerateHash(hAlgo:=_hHASH, pData:=ADR(bsMessage), pHash:=ADR(bsNewHashCode));
    diCmpResult := SysMemCmp(pBuffer1:=ADR(abyHashCode), pBuffer2:=ADR(abyNewHashCode), udiCount:=SIZEOF(HASH_CODE));
    CheckMessage := diCmpResult = 0;
    
     
  • dkugler - 2024-09-06

    looks like your _hHash handle is "empty" (00000) at the breakpoint! I don't know if CryptoGetAlgorithmById(...) call in the VAR section is possible? would be good to know.

     
    • otbeka - 2024-09-06

      Unfortunately I noticed that, and tried:
      * using CryptoGeteAlgorithmByID within the function call
      * inputting the raw byte pointer as a testByte
      * instantiating the _hHash handle within the function body
      * using a different cryptoID or the raw DINT values from the RtsCryptoID DUT

      ... to no avail.

      The pReturn value is also set to 0, which would indicate that it is OK, right? This is odd given that the function is the same within the CryptoDemo example project here, just with a newer version.

      Is it possible that there is something wrong with the way my bytestring is being set up? I use the following DUTs here:

      TYPE MESSAGE : STRING(255); END_TYPE
      TYPE HASH_CODE : ARRAY[0..19] OF BYTE; END_TYPE
      
       

Log in to post a comment.