IBM Crypto Education Community - Group home

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

  
/* Rexx */                                                                                                                                      /*------------------------------------------------------------------*/  /* Description:                                                     */  /*  Sample rexx program for multi-part encryption and multi-part    */  /*  decryption (chaining) using CSNBSYE/CSNBSYD.                    */  /*                                                                  */  /*   - call CSNBSYE with rules : AES CFB INITIAL                    */  /*   - call CSNBSYE with rules : AES CFB CONTINUE                   */  /*        (SYE can be called multiple times with rule CONTINUE)     */  /*   - call CSNBSYE with rules : AES CFB FINAL                      */  /*   - call CSNBSYD with rules : AES CFB INITIAL                    */  /*   - call CSNBSYD with rules : AES CFB CONTINUE                   */  /*        (SYD can be called multiple times with rule CONTINUE)     */  /*   - call CSNBSYD with rules : AES CFB FINAL                      */  /*                                                                  */  /* See ICSF Application Programmer's Guide for api and parameter    */  /*   documentation.                                                 */  /*                                                                  */  /*------------------------------------------------------------------*/                                                                          my_AES_clearkey        = '2b7e151628aed2a6abf7158809cf4f3c'X;           my_AES_clearkey_length = '00000010'x ;                                                                                                          /* ----------------------------------- */                               /* setup parms for first call to SYE   */                               /* ----------------------------------- */                               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_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. ';          call Symmetric_Key_Encipher;                                                                                                                    If (SYE_rc <> '00000000'x) & (SYE_rs <> '00000000'x) then                  signal getout ;                                                                                                                              /* Save cipher text and length for call to SYD */                       Init_Cipher_Len  = SYE_Cipher_text_Length                               Init_Cipher_text = substr(SYE_Cipher_text,1,c2d(SYE_Cipher_text_Length))/* -------------------------------------------- */                      /* setup parms for intermediate call to SYE     */                      /*  (there can be multiple intermediate calls ) */                      /* -------------------------------------------- */                      SYE_Rule_Count            = '00000004'x ;                               SYE_Rule_Array            = 'Aes     CFB     KEY-CLR CONTINUE';         SYE_clear_text_Length     = '00000020'x                                 SYE_clear_text            = 'Middle clear text to CSNBSYE.  ';          call Symmetric_Key_Encipher;                                                                                                                    If (SYE_rc <> '00000000'x) & (SYE_rs <> '00000000'x) then                  signal getout ;                                                                                                                              /* Save cipher text and length for call to SYD */                       Middle_Cipher_Len  = SYE_Cipher_text_Length                             Middle_Cipher_text = ,                                                      substr(SYE_Cipher_text,1,c2d(SYE_Cipher_text_Length))                                                                                       /* -------------------------------------------- */                      /* setup parms for last call to SYE             */                      /* -------------------------------------------- */                      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 );               call Symmetric_Key_Encipher;                                                                                                                    If (SYE_rc <> '00000000'x) & (SYE_rs <> '00000000'x) then                  signal getout ;                                                                                                                              /* Save cipher text and length for call to SYD */                       Final_Cipher_len =SYE_Cipher_text_Length                                Final_Cipher_text=substr(SYE_Cipher_text,1,c2d(SYE_Cipher_text_Length))                                                                        /* ----------------------------------- */                               /* setup parms for first call to SYD   */                               /* ----------------------------------- */                               SYD_Rule_Count            = '00000004'x ;                               SYD_Rule_Array            = 'Aes     CFB     KEY-CLR INITIAL ';         SYD_Chain_Data_Length     = '00000020'x ;                               SYD_Chain_Data            = copies('00'x,32)                            SYD_cipher_text           = Init_cipher_text                            SYD_cipher_text_Length    = d2c( length(SYD_cipher_text),4 )            call Symmetric_Key_Decipher;                                                                                                                    If (SYD_rc <> '00000000'x) & (SYD_rs <> '00000000'x) then                     signal getout ;                                                   say 'SYD_Clear_Text : ' syd_clear_text                                 /* -------------------------------------------- */              /* setup parms for intermediate call to SYD     */              /*  (there can be multiple intermediate calls ) */              /* -------------------------------------------- */              SYD_Rule_Count           = '00000004'x ;                        SYD_Rule_Array           = 'Aes     CFB     KEY-CLR CONTINUE';  SYD_cipher_text          = Middle_cipher_text                   SYD_cipher_text_Length   = d2c( length(SYD_cipher_text),4 )     call Symmetric_Key_Decipher;                                    If (SYD_rc <> '00000000'x) & (SYD_rs <> '00000000'x) then             signal getout ;                                           say 'SYD_Clear_Text : ' syd_clear_text                                                                                          /* -------------------------------------------- */              /* setup parms for last call to SYD             */              /* -------------------------------------------- */              SYD_Rule_Count           = '00000004'x ;                        SYD_Rule_Array           = 'Aes     CFB     KEY-CLR FINAL  ';   SYD_cipher_text          = Final_cipher_text                    SYD_cipher_text_Length   = d2c( length(SYD_cipher_text),4 )     call Symmetric_Key_Decipher;                                    If (SYD_rc <> '00000000'x) & (SYD_rs <> '00000000'x) then             signal getout ;                                           say 'SYD_Clear_Text : ' syd_clear_text                                                                                          getout:                                                         exit;                                                          /*------------------------------------------------------------------*//* Symmetric_Key_Encipher                                           *//*   Setup parms and invoke CSNBSYE                                 *//*------------------------------------------------------------------*/Symmetric_Key_Encipher:                                                 SYE_rc                    = 'FFFFFFFF'x ;                              SYE_rs                    = 'FFFFFFFF'x ;                              SYE_Exit_Length           = '00000000'x ;                              SYE_Exit_Data             = '' ;                                       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_cipher_text_Length    = '00000080'x ;                              SYE_cipher_text           = copies('00'x,128) ;                        SYE_Optional_Data_Length  = '00000000'x ; /* ignored */                SYE_Optional_Data         = '' ;          /* ignored */                                                                                       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) ;                                                                                             return;                                                                                                                                      
/*------------------------------------------------------------------*//* Symmetric_Key_Decipher                                           *//*   Setup parms and invoke CSNBSYD                                 *//*------------------------------------------------------------------*/Symmetric_Key_Decipher:                                                 SYD_rc                    = 'FFFFFFFF'x ;                              SYD_rs                    = 'FFFFFFFF'x ;                              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_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) ;                                                                                              SYD_clear_text =substr(SYD_Clear_text,1,c2d(SYD_Clear_text_Length))                                                                          return;