IBM Crypto Education Community - Group home

REXX sample - multi-part (chain) encryption with CSNBSYE and CSNBSYD

  
/* Rexx */                                                               /*------------------------------------------------------------------*/  /* Description:                                                     */  /*  Sample rexx program for multi-part encryption (chaining) and    */  /*  single-part decryption using CSNBSYE/CSNBSYD.                   */  /*                                                                  */  /*   - call CSNBSYE with rules : AES CFB INITIAL                    */  /*   - call CSNBSYE with rules : AES CFB FINAL                      */  /*   - call CSNBSYD with rules : AES CFB ONLY                       */  /*                                                                  */  /* See ICSF Application Programmer's Guide for api and parameter    */  /*   documentation.                                                 */  /*                                                                  */  /*------------------------------------------------------------------*/   my_AES_clearkey        = '2b7e151628aed2a6abf7158809cf4f3c'X;           my_AES_clearkey_length = '00000010'x ;                                   /*------------------------------------------------------------------*/  /* initialize parameters for Initial Symmetric Key Encipher call    */  /*------------------------------------------------------------------*/  SYE_rc               = 'FFFFFFFF'x ;                                    SYE_rs               = 'FFFFFFFF'x ;                                    SYE_Exit_Length      = '00000000'x ;                                    SYE_Exit_Data        = '' ;                                             SYE_Rule_Count       = '00000004'x ;                                    SYE_Rule_Array       = 'Aes     CFB     KEY-CLR INITIAL ';              SYE_Key_identifier_length = my_AES_clearkey_length                      SYE_Key_Identifier        = my_AES_clearkey                             SYE_Key_Parms_Length      = '00000000'x ;   /* ignored */               SYE_Key_Parms             = '' ;            /* ignored */               SYE_Block_Size            = '00000010'x ;                               SYE_Initialization_Vector_Length = '00000010'x ;                        SYE_Initialization_Vector        = '000102030405060708090a0b0c0d0e0f'x ;SYE_Chain_Data_length     = '00000020'x ;                               SYE_Chain_Data            = copies('00'x,32)                            SYE_clear_text_Length     = '00000020'x                                 SYE_clear_text            = 'Initial clear text to CSNBSYE. ';          SYE_cipher_text_Length    = '00000080'x ;                               SYE_cipher_text           = copies('00'x,128) ;                         SYE_Optional_Data_Length  = '00000000'x ; /* ignored */                 SYE_Optional_Data         = '' ;          /* ignored */                  say 'SYE_Rule_Array: ' SYE_Rule_Array                                   say 'SYE_Clear_Text: ' SYE_Clear_Text                                   /* call Symmetric Key Encipher */                                       address linkpgm 'CSNBSYE'                                        ,                      'SYE_rc'                    'SYE_rs'             ,                      'SYE_Exit_Length'           'SYE_Exit_Data'      ,                      'SYE_Rule_Count'            'SYE_Rule_Array'     ,                      'SYE_Key_identifier_length' 'SYE_Key_Identifier' ,                      'SYE_Key_Parms_Length'      'SYE_Key_Parms'      ,                      'SYE_Block_Size'                                 ,                      'SYE_Initialization_Vector_Length'               ,                      'SYE_Initialization_Vector'                      ,                      'SYE_Chain_Data_Length'     'SYE_Chain_Data'     ,                      'SYE_clear_text_Length'     'SYE_clear_text'     ,                      'SYE_cipher_text_Length'    'SYE_cipher_text'    ,                      'SYE_Optional_Data_Length'  'SYE_Optional_Data'  ;                                                                              say 'SYE: rc =' c2x(SYE_rc) 'rs =' c2x(SYE_rs) ;                                                                                                If (SYE_rc <> '00000000'x) & (SYE_rs <> '00000000'x) then                  signal getout ;                                                                                                                              /* save cipher_text to pass to SYD */                                   Init_Cipher_Len  = SYE_Cipher_text_Length                               Init_Cipher_text = substr(SYE_Cipher_text,1,c2d(SYE_Cipher_text_Length))                                                                       /*------------------------------------------------------------------*/   /* initialize parameters for FINAL Symmetric Key Encipher call      */   /*------------------------------------------------------------------*/   SYE_rc                 = 'FFFFFFFF'x ;                                   SYE_rs                 = 'FFFFFFFF'x ;                                   SYE_Exit_Length        = '00000000'x ;                                   SYE_Exit_Data          = '' ;                                            SYE_Rule_Count         = '00000004'x ;                                   SYE_Rule_Array         = 'Aes     CFB     KEY-CLR FINAL   ';             SYE_clear_text         = 'FINAL clear text to SYE.';                     SYE_clear_text_Length  = d2c( length(SYE_clear_text),4 );                SYE_cipher_text_Length = '00000080'x ;                                   SYE_cipher_text        = copies('00'x,128) ;                                                                                                      say                                                                      say 'SYE_Rule_Array: ' SYE_Rule_Array                                    say 'SYE_Clear_Text: ' SYE_Clear_Text                                                                                                             /* call Symmetric Key Encipher */                                        address linkpgm 'CSNBSYE'                                        ,                       'SYE_rc'                    'SYE_rs'             ,                       'SYE_Exit_Length'           'SYE_Exit_Data'      ,                       'SYE_Rule_Count'            'SYE_Rule_Array'     ,                       'SYE_Key_identifier_length' 'SYE_Key_Identifier' ,                       'SYE_Key_Parms_Length'      'SYE_Key_Parms'      ,                       'SYE_Block_Size'                                 ,                       'SYE_Initialization_Vector_Length'               ,                       'SYE_Initialization_Vector'                      ,                       'SYE_Chain_Data_Length'     'SYE_Chain_Data'     ,                       'SYE_clear_text_Length'     'SYE_clear_text'     ,                       'SYE_cipher_text_Length'    'SYE_cipher_text'    ,                       'SYE_Optional_Data_Length'  'SYE_Optional_Data'  ;                                                                                say 'SYE: rc =' c2x(SYE_rc) 'rs =' c2x(SYE_rs) ;                                                                                                  If (SYE_rc <> '00000000'x) & (SYE_rs <> '00000000'x) then                   signal getout ;                                                                                                                                /* save cipher_text to pass to SYD */                                    Final_Cipher_len =SYE_Cipher_text_Length                                 Final_Cipher_text=substr(SYE_Cipher_text,1,c2d(SYE_Cipher_text_Length))                                                                          
/*------------------------------------------------------------------*//* initialize parameters for ONLY  Symmetric Key Decipher call      *//*------------------------------------------------------------------*/SYD_rc                    = 'FFFFFFFF'x ;                             SYD_rs                    = 'FFFFFFFF'x ;                             SYD_Rule_Count            = '00000004'x ;                             SYD_Rule_Array            = 'Aes     CFB     KEY-CLR ONLY   ';        SYD_Key_identifier_length = SYE_Key_identifier_length                 SYD_Key_Identifier        = SYE_Key_Identifier                        SYD_Key_Parms_Length      = SYE_Key_Parms_Length                      SYD_Key_Parms             = SYE_Key_Parms                             SYD_Block_size            = SYE_Block_Size                            SYD_Initialization_Vector_Length = SYE_Initialization_Vector_Length   SYD_Initialization_Vector        = SYE_Initialization_Vector          SYD_Chain_Data_Length     = '00000020'x ;                             SYD_Chain_Data            = copies('00'x,32)                          SYD_cipher_text           = Init_cipher_text||Final_cipher_text       SYD_cipher_text_Length    = d2c( length(SYD_cipher_text),4 )          SYD_Clear_text_Length     = '00000080'x ;                             SYD_Clear_text            = copies('00'x,128) ;                       SYD_Optional_Data_Length  = '00000000'x ; /* ignored */               SYD_Optional_Data         = '' ;          /* ignored */                                                                                     say                                                                   say 'SYD_Rule_Array: ' SYD_Rule_Array                                                                                                       address linkpgm 'CSNBSYD'                                        ,                    'SYD_rc'                    'SYD_rs'             ,                    'SYD_Exit_Length'           'SYD_Exit_Data'      ,                    'SYD_Rule_Count'            'SYD_Rule_Array'     ,                    'SYD_Key_Identifier_Length' 'SYD_Key_Identifier' ,                    'SYD_Key_Parms_Length'      'SYD_Key_Parms'      ,                    'SYD_Block_Size'                                 ,                    'SYD_Initialization_Vector_Length'               ,                    'SYD_Initialization_Vector'                      ,                    'SYD_Chain_Data_Length'     'SYD_Chain_Data'     ,                    'SYD_cipher_text_Length'    'SYD_cipher_text'    ,                    'SYD_Clear_text_Length'     'SYD_Clear_text'     ,                    'SYD_Optional_Data_Length'  'SYD_Optional_Data' ;                                                                           say 'SYD: rc =' c2x(SYD_rc) 'rs =' c2x(SYD_rs) ;                                                                                            If (SYD_rc <> '00000000'x) & (SYD_rs <> '00000000'x) then                   signal getout ;                                                                                                                       SYD_clear_text = substr(SYD_Clear_text,1,c2d(SYD_Clear_text_Length))  say 'SYD_Clear_Text : ' syd_clear_text                                                                                                      getout:                                                               exit ;