Appendix B: Calling the Cryptography Functions from Fortran-90

The Intel® Integrated Performance Primitives (Intel® IPP) for cryptography basically provide C interface. However, you can invoke Intel IPP cryptography functions directly from other languages if you are familiar with the inter-language calling conventions of your platforms. To promote portability, relieve you of having to deal with the calling convention specifics, and provide an interface to the functions that looks natural in Fortran-90, the Fortran-specific header file has been implemented.

The header file is available with the Intel IPP Samples, an extensive library of code samples and codecs implemented using the Intel IPP functions to help demonstrate the use of Intel IPP and accelerate the development of your application, components, and codecs. The samples can be downloaded from http://www.intel.com/cd/software/products/asmo-na/eng/220046.htm. The header file is a part of the Fortran-90 Interface to Intel IPP for Cryptography sample, which can be found in the \ipp_samples\language-interface\f90-crypto directory after downloading the code samples.

The interface header file .\include\ippcp.f90 contains interfaces for direct calling Intel IPP cryptography functions from Fortran-90 applications.

Along with the header file, the sample, in particular, provides Fortran-90 examples for all Intel IPP cryptography functions.

The Fortran-90 Interface to Intel IPP for Cryptography sample demonstrates how to call Intel IPP cryptography functions using Fortran-90 API. The structure of the sample is designed so that each specific cryptographic algorithm, for example, an encryption in the OFB mode according to DES algorithm, is represented by a module. Each module contains the sequence of operations that an application code must perform to complete the algorithm. So, from the module you can get to know how to carry out the algorithmic chain, define parameters, set data, and call the cryptography functions in a Fortran-90 application. Modules related to a certain cryptographic algorithm, for example, TDES block cipher, make up a file with the corresponding name, crypto_tdes_samples.f90, in this case. Names of modules inside the files are also self-explanatory. For example, in the above file, the module TDES_CBC_SAMPLE implements a typical TDES encryption in the CBC mode.

The file \ipp_samples\language-interface\f90-crypto\doc\crypto-f90.pdf contains the detailed description of the Fortran-90 cryptography API and the sample usage.

In Example “Fortran-90 Implementation of DES Encryption and Decryption”, modules are fragments of the definition module IPPCP_DEFS and interface module IPPCP_F90 from the header file ippcp.f90 and the DES_ECB_SAMPLE program demonstrates a Fortran-90 implementation of Example “DES/TDES Encryption and Decryption”.

Fortran-90 Implementation of DES Encryption and Decryption

MODULE IPPCP_DEFS

    ! Codes for parameter PADDING
    INTEGER(4), PARAMETER ::  IppsCPPaddingNONE  = 0
  
    ! DES/TDES DEFINITIONS
    INTEGER(4), PARAMETER ::  DES_BLOCKSIZE = 8 ! cipher blocksize (bytes)
    INTEGER(4), PARAMETER ::  DES_KEYSIZE   = 8 ! cipher keysize (bytes)

END MODULE IPPCP_DEFS
 
MODULE IPPCP_F90
 
  USE IPPCP_DEFS
  INTERFACE 
    !IppStatus ippsDESGetSize(int* pSize)
    INTEGER(4) FUNCTION ippsDESGetSize(size)

        INTEGER(4), INTENT(OUT) :: size
        !DEC$ATTRIBUTES STDCALL, DECORATE, ALIAS : 'ippsDESGetSize':: ippsDESGetSize
        !MS$ATTRIBUTES REFERENCE :: size
    END FUNCTION ippsDESGetSize
  END INTERFACE 
  
  INTERFACE 
    !IppStatus ippsDESInit(const Ipp8u* pKey, IppsDESSpec* pCtx);
     INTEGER(4) FUNCTION ippsDESInit(Key, SPEC_CONTEXT)
       USE IPPCP_DEFS
       CHARACTER(LEN=DES_KEYSIZE), INTENT(IN) :: Key
       CHARACTER(LEN=1), INTENT(INOUT) :: SPEC_CONTEXT(*)
        
       !DEC$ATTRIBUTES STDCALL, DECORATE, ALIAS : 'ippsDESInit'::ippsDESInit
       !MS$ATTRIBUTES REFERENCE  :: Key
       !MS$ATTRIBUTES REFERENCE  :: SPEC_CONTEXT
    END FUNCTION ippsDESInit 
  END INTERFACE
  INTERFACE 
    !IppStatus ippsDESEncryptECB(const Ipp8u* pSrc, Ipp8u* pDst, int len,
    !                    const IppsDESSpec* pCtx, IppsCPPadding padding);
    INTEGER(4) FUNCTION ippsDESEncryptECB(Src, Dst, len, SPEC_CONTEXT, PADDING)
       
       CHARACTER(LEN=*), INTENT(IN)  :: Src
       CHARACTER(LEN=*), INTENT(OUT) :: Dst
       INTEGER(4), INTENT(IN) :: len
       CHARACTER(LEN=1), INTENT(IN) :: SPEC_CONTEXT(*)
       INTEGER(4), INTENT(IN) :: PADDING
       
       !DEC$ATTRIBUTES STDCALL, DECORATE, ALIAS : 'ippsDESEncryptECB'::ippsDESEncryptECB
       !MS$ATTRIBUTES REFERENCE  :: Src
       !MS$ATTRIBUTES REFERENCE  :: Dst
       !MS$ATTRIBUTES REFERENCE  :: SPEC_CONTEXT
    END FUNCTION ippsDESEncryptECB
  END INTERFACE 
  
  INTERFACE 
    !IppStatus ippsDESDecryptECB(const Ipp8u* pSrc, Ipp8u* pDst, int len,
    !                    const IppsDESSpec* pCtx, IppsCPPadding padding);
    INTEGER(4) FUNCTION ippsDESDecryptECB(Src, Dst, len, SPEC_CONTEXT, PADDING)

       CHARACTER(LEN=*), INTENT(IN)  :: Src
       CHARACTER(LEN=*), INTENT(OUT) :: Dst
       INTEGER(4), INTENT(IN) :: len
       CHARACTER(LEN=1), INTENT(IN) :: SPEC_CONTEXT(*)
       INTEGER(4), INTENT(IN) :: PADDING
       
       !DEC$ATTRIBUTES STDCALL, DECORATE, ALIAS : 'ippsDESDecryptECB'::ippsDESDecryptECB
       !MS$ATTRIBUTES REFERENCE  :: Src
       !MS$ATTRIBUTES REFERENCE  :: Dst
       !MS$ATTRIBUTES REFERENCE  :: SPEC_CONTEXT
    END FUNCTION ippsDESDecryptECB
  END INTERFACE 

END MODULE IPPCP_F90
PROGRAM DES_ECB_SAMPLE
  USE IPPCP_F90
  
  INTEGER(4) :: size
  INTEGER(4) status
    
  CHARACTER(LEN=DES_KEYSIZE) Key
  INTEGER(1), DIMENSION(DES_KEYSIZE) :: Key_int
  INTEGER(4) textSize
  
! context description 
  CHARACTER(LEN=1), ALLOCATABLE :: SPEC_CONTEXT(:)

  CHARACTER(LEN=40) ptext
  CHARACTER(LEN=40) rtext
  CHARACTER(LEN=40) ctext
  
! the size of text is always multiple of cipher block size
! (DES_DES_BLOCKSIZE =8 bytes)
  textSize = 40
  
! define the message to be encrypted
  ptext = 'quick brown fox jupm over lazy dog     '

! define the Key
  Key_int = (/Z'01',Z'02',Z'03',Z'04',Z'05',Z'06',Z'07',Z'08'/)
  Key = TRANSFER(Key_int, Key)

! get size of the context needed for the encryption/decryption operation
  status = ippsDESGetSize(size)

! allocate context 
  ALLOCATE (SPEC_CONTEXT(size))

! prepare the context for the DES usage
  status = ippsDESInit(Key, SPEC_CONTEXT)
  
! encrypt (ECB mode) ptext message
  status = ippsDESEncryptECB(ptext, ctext, textSize, SPEC_CONTEXT, IppsCPPaddingNONE)

! decrypt (ECB mode) ctext message
  status = ippsDESDecryptECB(ctext, rtext, textSize, SPEC_CONTEXT, IppsCPPaddingNONE)
    
! deallocate context 
  DEALLOCATE (SPEC_CONTEXT)

END PROGRAM

See Also

Submit feedback on this help topic

Copyright © 2000 - 2010, Intel Corporation. All rights reserved.