aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2012-02-04 23:37:25 +0100
committerAndreas Schneider <asn@cryptomilk.org>2012-02-04 23:44:55 +0100
commit216cb8b1aa86d6fd5cf8db38938e70d5986403cd (patch)
tree96a4bb4b9fab1a34a719e42d3f5ea1c68a52461a
parentfb6855a8213fb8b0434fae3a08943d3bd4c827a9 (diff)
downloadlibssh-216cb8b1aa86d6fd5cf8db38938e70d5986403cd.tar.gz
libssh-216cb8b1aa86d6fd5cf8db38938e70d5986403cd.tar.xz
libssh-216cb8b1aa86d6fd5cf8db38938e70d5986403cd.zip
crypto: Add evp hashing function.
-rw-r--r--include/libssh/libcrypto.h8
-rw-r--r--include/libssh/libgcrypt.h2
-rw-r--r--include/libssh/wrapper.h2
-rw-r--r--src/libcrypto.c28
4 files changed, 40 insertions, 0 deletions
diff --git a/include/libssh/libcrypto.h b/include/libssh/libcrypto.h
index 8ddeac9..acfa109 100644
--- a/include/libssh/libcrypto.h
+++ b/include/libssh/libcrypto.h
@@ -31,6 +31,10 @@
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <openssl/hmac.h>
+#ifdef HAVE_OPENSSL_ECC
+#include <openssl/evp.h>
+#endif
+
typedef SHA_CTX* SHACTX;
typedef SHA256_CTX* SHA256CTX;
typedef MD5_CTX* MD5CTX;
@@ -42,6 +46,10 @@ typedef HMAC_CTX* HMACCTX;
#endif
#define MD5_DIGEST_LEN MD5_DIGEST_LENGTH
+#ifdef HAVE_OPENSSL_ECC
+#define EVP_DIGEST_LEN EVP_MAX_MD_SIZE
+#endif
+
#include <openssl/bn.h>
#include <openssl/opensslv.h>
#define OPENSSL_0_9_7b 0x0090702fL
diff --git a/include/libssh/libgcrypt.h b/include/libssh/libgcrypt.h
index b8704d5..104fdee 100644
--- a/include/libssh/libgcrypt.h
+++ b/include/libssh/libgcrypt.h
@@ -41,6 +41,8 @@ typedef gcry_md_hd_t HMACCTX;
#define EVP_MAX_MD_SIZE 36
#endif
+#define EVP_DIGEST_LEN EVP_MAX_MD_SIZE
+
typedef gcry_mpi_t bignum;
/* missing gcrypt functions */
diff --git a/include/libssh/wrapper.h b/include/libssh/wrapper.h
index c08ab22..79d8745 100644
--- a/include/libssh/wrapper.h
+++ b/include/libssh/wrapper.h
@@ -48,6 +48,8 @@ void sha1_final(unsigned char *md,SHACTX c);
void sha1(unsigned char *digest,int len,unsigned char *hash);
void sha256(unsigned char *digest, int len, unsigned char *hash);
+void evp(int nid, unsigned char *digest, int len, unsigned char *hash, unsigned int *hlen);
+
ssh_mac_ctx ssh_mac_ctx_init(enum ssh_mac_e type);
void ssh_mac_update(ssh_mac_ctx ctx, const void *data, unsigned long len);
void ssh_mac_final(unsigned char *md, ssh_mac_ctx ctx);
diff --git a/src/libcrypto.c b/src/libcrypto.c
index 747994e..7977541 100644
--- a/src/libcrypto.c
+++ b/src/libcrypto.c
@@ -97,6 +97,34 @@ void sha1(unsigned char *digest, int len, unsigned char *hash) {
SHA1(digest, len, hash);
}
+#ifdef HAVE_OPENSSL_ECC
+static const EVP_MD *nid_to_evpmd(int nid)
+{
+ switch (nid) {
+ case NID_X9_62_prime256v1:
+ return EVP_sha256();
+ case NID_secp384r1:
+ return EVP_sha384();
+ case NID_secp521r1:
+ return EVP_sha512();
+ default:
+ return NULL;
+ }
+
+ return NULL;
+}
+
+void evp(int nid, unsigned char *digest, int len, unsigned char *hash, unsigned int *hlen)
+{
+ const EVP_MD *evp_md = nid_to_evpmd(nid);
+ EVP_MD_CTX md;
+
+ EVP_DigestInit(&md, evp_md);
+ EVP_DigestUpdate(&md, digest, len);
+ EVP_DigestFinal(&md, hash, hlen);
+}
+#endif
+
SHA256CTX sha256_init(void){
SHA256CTX c = malloc(sizeof(*c));
if (c == NULL) {