Skip to content

Commit

Permalink
Use new API to fetch algorithms on OpenSSL3 (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed May 16, 2024
1 parent 947cfdb commit e21504c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: openssl
Type: Package
Title: Toolkit for Encryption, Signatures and Certificates Based on OpenSSL
Version: 2.1.2
Version: 2.2.0
Authors@R: c(person("Jeroen", "Ooms", role = c("aut", "cre"), email = "jeroen@berkeley.edu",
comment = c(ORCID = "0000-0002-4035-0289")),
person("Oliver", "Keyes", role = "ctb"))
Expand Down
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2.2.0
- Use new EVP_MD_fetch() api on libssl 3 to find non-default algorithms.

2.1.2
- MacOS: avoid linking against legacy versions of openssl

Expand Down
6 changes: 6 additions & 0 deletions R/hash.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ sha512 <- function(x, key = NULL){
rawstringhash(x, "sha512", key)
}

#' @rdname hash
#' @export
keccak256 <- function(x, key = NULL){
rawstringhash(x, "keccak-256", key)
}

#' @rdname hash
#' @export
sha2 <- function(x, size = 256, key = NULL){
Expand Down
3 changes: 3 additions & 0 deletions man/hash.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#define HAS_OPENSSL11_API 1
#endif

#if defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 3
#define HAS_OPENSSL3_API 1
#endif

#ifdef HAS_OPENSSL11_API
#define MY_EVP_PKEY_get0_RSA EVP_PKEY_get0_RSA
#define MY_EVP_PKEY_get0_DSA EVP_PKEY_get0_DSA
Expand Down
7 changes: 7 additions & 0 deletions src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <string.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/pem.h>
#include "utils.h"
#include "compatibility.h"

/*
* Adapted from example at: https://www.openssl.org/docs/crypto/EVP_DigestInit.html
Expand All @@ -13,7 +15,12 @@ unsigned int digest_string(unsigned char *x, int len, SEXP key, const char *algo

/* init openssl stuff */
unsigned int md_len;
#ifdef HAS_OPENSSL3_API
EVP_MD *md = EVP_MD_fetch(NULL, algo, NULL);
#else
const EVP_MD *md = EVP_get_digestbyname(algo);
#endif

if(!md)
error("Unknown cryptographic algorithm %s\n", algo);

Expand Down
5 changes: 1 addition & 4 deletions src/onload.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
#include <openssl/ssl.h>
#include <openssl/engine.h>
#include <openssl/hmac.h>

#if defined(OPENSSL_VERSION_MAJOR) && OPENSSL_VERSION_MAJOR >= 3
#define HAS_OPENSSL3_API 1
#endif
#include "compatibility.h"

#ifdef HAS_OPENSSL3_API
#include <openssl/provider.h>
Expand Down
8 changes: 8 additions & 0 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ void fin_md(SEXP ptr){
}

SEXP R_md_init(SEXP algo){
#ifdef HAS_OPENSSL3_API
EVP_MD *md = EVP_MD_fetch(NULL, CHAR(asChar(algo)), NULL);
#else
const EVP_MD *md = EVP_get_digestbyname(CHAR(asChar(algo)));
#endif
if(!md)
error("Unknown cryptographic algorithm %s\n", CHAR(asChar(algo)));
EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
Expand Down Expand Up @@ -61,7 +65,11 @@ void fin_hmac(SEXP ptr){
}

SEXP R_hmac_init(SEXP algo, SEXP key){
#ifdef HAS_OPENSSL3_API
EVP_MD *md = EVP_MD_fetch(NULL, CHAR(asChar(algo)), NULL);
#else
const EVP_MD *md = EVP_get_digestbyname(CHAR(asChar(algo)));
#endif
if(!md)
error("Unknown cryptographic algorithm %s\n", CHAR(asChar(algo)));
#ifdef HAS_OPENSSL11_API
Expand Down

0 comments on commit e21504c

Please sign in to comment.