Browse Source

OpenSSL: Use EVP_Digest*() functions

Instead of using low level, digest-specific functions, use the generic
EVP interface for digest functions. In addition, report OpenSSL errors
in more detail.
Jouni Malinen 15 years ago
parent
commit
4b77bf2a40
1 changed files with 30 additions and 31 deletions
  1. 30 31
      src/crypto/crypto_openssl.c

+ 30 - 31
src/crypto/crypto_openssl.c

@@ -14,9 +14,7 @@
 
 #include "includes.h"
 #include <openssl/opensslv.h>
-#include <openssl/md4.h>
-#include <openssl/md5.h>
-#include <openssl/sha.h>
+#include <openssl/err.h>
 #include <openssl/des.h>
 #include <openssl/aes.h>
 #include <openssl/bn.h>
@@ -34,22 +32,43 @@
 #endif /* openssl < 0.9.7 */
 
 
-int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
+int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
+			  const u8 *addr[], const size_t *len, u8 *mac)
 {
-	MD4_CTX ctx;
+	EVP_MD_CTX ctx;
 	size_t i;
+	unsigned int mac_len;
 
-	if (!MD4_Init(&ctx))
+	EVP_MD_CTX_init(&ctx);
+	if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
+		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
+			   ERR_error_string(ERR_get_error(), NULL));
 		return -1;
-	for (i = 0; i < num_elem; i++)
-		if (!MD4_Update(&ctx, addr[i], len[i]))
+	}
+	for (i = 0; i < num_elem; i++) {
+		if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
+			wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
+				   "failed: %s",
+				   ERR_error_string(ERR_get_error(), NULL));
 			return -1;
-	if (!MD4_Final(mac, &ctx))
+		}
+	}
+	if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
+		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
+			   ERR_error_string(ERR_get_error(), NULL));
 		return -1;
+	}
+
 	return 0;
 }
 
 
+int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
+{
+	return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
+}
+
+
 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 {
 	u8 pkey[8], next, tmp;
@@ -73,33 +92,13 @@ void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 
 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 {
-	MD5_CTX ctx;
-	size_t i;
-
-	if (!MD5_Init(&ctx))
-		return -1;
-	for (i = 0; i < num_elem; i++)
-		if (!MD5_Update(&ctx, addr[i], len[i]))
-			return -1;
-	if (!MD5_Final(mac, &ctx))
-		return -1;
-	return 0;
+	return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
 }
 
 
 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 {
-	SHA_CTX ctx;
-	size_t i;
-
-	if (!SHA1_Init(&ctx))
-		return -1;
-	for (i = 0; i < num_elem; i++)
-		if (!SHA1_Update(&ctx, addr[i], len[i]))
-			return -1;
-	if (!SHA1_Final(mac, &ctx))
-		return -1;
-	return 0;
+	return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
 }