Replace malloc/memcpy pairs with memdup.

Change-Id: Icc56ceb3f27be3c02aeb6a169b044c7846f1ce97
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/55268
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
diff --git a/crypto/asn1/tasn_utl.c b/crypto/asn1/tasn_utl.c
index b7604e2..9316c22 100644
--- a/crypto/asn1/tasn_utl.c
+++ b/crypto/asn1/tasn_utl.c
@@ -165,11 +165,10 @@
   if (enc->alias_only) {
     enc->enc = (uint8_t *)in;
   } else {
-    enc->enc = OPENSSL_malloc(inlen);
+    enc->enc = OPENSSL_memdup(in, inlen);
     if (!enc->enc) {
       return 0;
     }
-    OPENSSL_memcpy(enc->enc, in, inlen);
   }
 
   enc->len = inlen;
diff --git a/crypto/fipsmodule/cipher/e_aes.c b/crypto/fipsmodule/cipher/e_aes.c
index 5f23793..e8e03fe 100644
--- a/crypto/fipsmodule/cipher/e_aes.c
+++ b/crypto/fipsmodule/cipher/e_aes.c
@@ -529,11 +529,10 @@
       if (gctx->iv == c->iv) {
         gctx_out->iv = out->iv;
       } else {
-        gctx_out->iv = OPENSSL_malloc(gctx->ivlen);
+        gctx_out->iv = OPENSSL_memdup(gctx->iv, gctx->ivlen);
         if (!gctx_out->iv) {
           return 0;
         }
-        OPENSSL_memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
       }
       return 1;
     }
diff --git a/tool/speed.cc b/tool/speed.cc
index c12ebb2..abd387a 100644
--- a/tool/speed.cc
+++ b/tool/speed.cc
@@ -1061,12 +1061,8 @@
 
 static TRUST_TOKEN_PRETOKEN *trust_token_pretoken_dup(
     TRUST_TOKEN_PRETOKEN *in) {
-  TRUST_TOKEN_PRETOKEN *out =
-      (TRUST_TOKEN_PRETOKEN *)OPENSSL_malloc(sizeof(TRUST_TOKEN_PRETOKEN));
-  if (out) {
-    OPENSSL_memcpy(out, in, sizeof(TRUST_TOKEN_PRETOKEN));
-  }
-  return out;
+  return (TRUST_TOKEN_PRETOKEN *)OPENSSL_memdup(in,
+                                                sizeof(TRUST_TOKEN_PRETOKEN));
 }
 
 static bool SpeedTrustToken(std::string name, const TRUST_TOKEN_METHOD *method,