src/lib/crypto.c
CIPHER_CONTEXT *crypto_cipher_new(CRYPTO_SESSION *cs, bool encrypt, uint32_t *blocksize)
{
   CIPHER_CONTEXT *cipher_ctx;
   const EVP_CIPHER *ec;

   cipher_ctx = (CIPHER_CONTEXT *)malloc(sizeof(CIPHER_CONTEXT));
   if (!cipher_ctx)
           return NULL;

   cipher_ctx->ctx = EVP_CIPHER_CTX_new();
   if (!cipher_ctx->ctx)
           goto err;

   /*
    * Acquire a cipher instance for the given ASN.1 cipher NID
    */
   if ((ec = EVP_get_cipherbyobj(cs->cryptoData->contentEncryptionAlgorithm)) == NULL) {
      Jmsg1(NULL, M_ERROR, 0,
         _("Unsupported contentEncryptionAlgorithm: %d\n"), OBJ_obj2nid(cs->cryptoData->contentEncryptionAlgorithm));
      free(cipher_ctx);
      return NULL;
   }

   /* Initialize the OpenSSL cipher context */
   EVP_CIPHER_CTX_reset(cipher_ctx->ctx);
   if (encrypt) {
      /* Initialize for encryption */
      if (!EVP_CipherInit_ex(cipher_ctx->ctx, ec, NULL, NULL, NULL, 1)) {
         openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
         goto err;
      }
   } else {
      /* Initialize for decryption */
      if (!EVP_CipherInit_ex(cipher_ctx->ctx, ec, NULL, NULL, NULL, 0)) {
         openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
         goto err;
      }
   }

   /* Set the key size */
   if (!EVP_CIPHER_CTX_set_key_length(cipher_ctx->ctx, cs->session_key_len)) {
      openssl_post_errors(M_ERROR, _("Encryption session provided an invalid symmetric key"));
      goto err;
   }

   /* Validate the IV length */
   if (EVP_CIPHER_iv_length(ec) != ASN1_STRING_length(cs->cryptoData->iv)) {
      openssl_post_errors(M_ERROR, _("Encryption session provided an invalid IV"));
      goto err;
   }

   /* Add the key and IV to the cipher context */
   if (!EVP_CipherInit_ex(cipher_ctx->ctx, NULL, NULL, cs->session_key, ASN1_STRING_get0_data(cs->cryptoData->iv), -1)) {
      openssl_post_errors(M_ERROR, _("OpenSSL cipher context key/IV initialization failed"));
      goto err;
   }

   *blocksize = EVP_CIPHER_CTX_block_size(cipher_ctx->ctx);
   return cipher_ctx;

err:
   crypto_cipher_free(cipher_ctx);
   return NULL;
}
/*
 * Create a new crypto cipher context with the specified session object
 *  Returns: A pointer to a CIPHER_CONTEXT object on success. The cipher block size is returned in blocksize.
 *           NULL on failure.
 */
Create a new crypto cipher context with the specified session object
Returns: A pointer to a CIPHER_CONTEXT object on success. The cipher block size is returned in blocksize.
NULL on failure.
Create a new crypto cipher context with the specified session object Returns : A pointer to a CIPHER_CONTEXT object on success . The cipher block size is returned in blocksize . NULL on failure .
CIPHER_CONTEXT *crypto_cipher_new(CRYPTO_SESSION *cs, bool encrypt, uint32_t *blocksize)
{
   CIPHER_CONTEXT *cipher_ctx;
   const EVP_CIPHER *ec;
   cipher_ctx = (CIPHER_CONTEXT *)malloc(sizeof(CIPHER_CONTEXT));
   if (!cipher_ctx)
           return NULL;
   cipher_ctx->ctx = EVP_CIPHER_CTX_new();
   if (!cipher_ctx->ctx)
           goto err;
   if ((ec = EVP_get_cipherbyobj(cs->cryptoData->contentEncryptionAlgorithm)) == NULL) {
      Jmsg1(NULL, M_ERROR, 0,
         _("Unsupported contentEncryptionAlgorithm: %d\n"), OBJ_obj2nid(cs->cryptoData->contentEncryptionAlgorithm));
      free(cipher_ctx);
      return NULL;
   }
   EVP_CIPHER_CTX_reset(cipher_ctx->ctx);
   if (encrypt) {
      if (!EVP_CipherInit_ex(cipher_ctx->ctx, ec, NULL, NULL, NULL, 1)) {
         openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
         goto err;
      }
   } else {
      if (!EVP_CipherInit_ex(cipher_ctx->ctx, ec, NULL, NULL, NULL, 0)) {
         openssl_post_errors(M_ERROR, _("OpenSSL cipher context initialization failed"));
         goto err;
      }
   }
   if (!EVP_CIPHER_CTX_set_key_length(cipher_ctx->ctx, cs->session_key_len)) {
      openssl_post_errors(M_ERROR, _("Encryption session provided an invalid symmetric key"));
      goto err;
   }
   if (EVP_CIPHER_iv_length(ec) != ASN1_STRING_length(cs->cryptoData->iv)) {
      openssl_post_errors(M_ERROR, _("Encryption session provided an invalid IV"));
      goto err;
   }
   if (!EVP_CipherInit_ex(cipher_ctx->ctx, NULL, NULL, cs->session_key, ASN1_STRING_get0_data(cs->cryptoData->iv), -1)) {
      openssl_post_errors(M_ERROR, _("OpenSSL cipher context key/IV initialization failed"));
      goto err;
   }
   *blocksize = EVP_CIPHER_CTX_block_size(cipher_ctx->ctx);
   return cipher_ctx;
err:
   crypto_cipher_free(cipher_ctx);
   return NULL;
}
CIPHER_CONTEXT * crypto_cipher_new ( CRYPTO_SESSION * cs , bool encrypt , uint32_t * blocksize ) { CIPHER_CONTEXT * cipher_ctx ; const EVP_CIPHER * ec ; cipher_ctx = ( CIPHER_CONTEXT * ) malloc ( sizeof ( CIPHER_CONTEXT ) ) ; if ( ! cipher_ctx ) return NULL ; cipher_ctx -> ctx = EVP_CIPHER_CTX_new ( ) ; if ( ! cipher_ctx -> ctx ) goto err ; if ( ( ec = EVP_get_cipherbyobj ( cs -> cryptoData -> contentEncryptionAlgorithm ) ) == NULL ) { Jmsg1 ( NULL , M_ERROR , 0 , _ ( " \n " ) , OBJ_obj2nid ( cs -> cryptoData -> contentEncryptionAlgorithm ) ) ; free ( cipher_ctx ) ; return NULL ; } EVP_CIPHER_CTX_reset ( cipher_ctx -> ctx ) ; if ( encrypt ) { if ( ! EVP_CipherInit_ex ( cipher_ctx -> ctx , ec , NULL , NULL , NULL , 1 ) ) { openssl_post_errors ( M_ERROR , _ ( " " ) ) ; goto err ; } } else { if ( ! EVP_CipherInit_ex ( cipher_ctx -> ctx , ec , NULL , NULL , NULL , 0 ) ) { openssl_post_errors ( M_ERROR , _ ( " " ) ) ; goto err ; } } if ( ! EVP_CIPHER_CTX_set_key_length ( cipher_ctx -> ctx , cs -> session_key_len ) ) { openssl_post_errors ( M_ERROR , _ ( " " ) ) ; goto err ; } if ( EVP_CIPHER_iv_length ( ec ) != ASN1_STRING_length ( cs -> cryptoData -> iv ) ) { openssl_post_errors ( M_ERROR , _ ( " " ) ) ; goto err ; } if ( ! EVP_CipherInit_ex ( cipher_ctx -> ctx , NULL , NULL , cs -> session_key , ASN1_STRING_get0_data ( cs -> cryptoData -> iv ) , -1 ) ) { openssl_post_errors ( M_ERROR , _ ( " " ) ) ; goto err ; } * blocksize = EVP_CIPHER_CTX_block_size ( cipher_ctx -> ctx ) ; return cipher_ctx ; err : crypto_cipher_free ( cipher_ctx ) ; return NULL ; }
Create a new crypto cipher context with the specified session object
  Returns: A pointer to a CIPHER_CONTEXT object on success.
Create a new crypto cipher context with the specified session object Returns : A pointer to a CIPHER_CONTEXT object on success .
/*
    * Acquire a cipher instance for the given ASN.1 cipher NID
    */ /* Initialize the OpenSSL cipher context */ /* Initialize for encryption */ /* Initialize for decryption */ /* Set the key size */ /* Validate the IV length */ /* Add the key and IV to the cipher context */