Skip to content

Commit

Permalink
minor function to build with Ruby's cipher module
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel40791765 committed Apr 30, 2024
1 parent 4d280eb commit 6103e75
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 4 deletions.
1 change: 1 addition & 0 deletions crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ add_library(
decrepit/cfb/cfb.c
decrepit/dh/dh_decrepit.c
decrepit/evp/evp_do_all.c
decrepit/obj/obj_decrepit.c
decrepit/ripemd/ripemd.c
decrepit/rsa/rsa_decrepit.c
decrepit/x509/x509_decrepit.c
Expand Down
3 changes: 1 addition & 2 deletions crypto/cipher_extra/derive_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@

#include <openssl/digest.h>
#include <openssl/mem.h>
#include <openssl/evp.h>


#define PKCS5_SALT_LEN 8

int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
const uint8_t *salt, const uint8_t *data, size_t data_len,
unsigned count, uint8_t *key, uint8_t *iv) {
Expand Down
70 changes: 70 additions & 0 deletions crypto/decrepit/obj/obj_decrepit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Copyright (c) 2016, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#include <openssl/obj.h>

#include <assert.h>
#include <string.h>

#include <openssl/evp.h>

#include "../../internal.h"


struct wrapped_callback {
void (*callback)(const OBJ_NAME *, void *arg);
void *arg;
};

static void cipher_callback(const EVP_CIPHER *cipher, const char *name,
const char *unused, void *arg) {
const struct wrapped_callback *wrapped = (struct wrapped_callback *)arg;
OBJ_NAME obj_name;

OPENSSL_memset(&obj_name, 0, sizeof(obj_name));
obj_name.type = OBJ_NAME_TYPE_CIPHER_METH;
obj_name.name = name;
obj_name.data = (const char *)cipher;

wrapped->callback(&obj_name, wrapped->arg);
}

static void md_callback(const EVP_MD *md, const char *name, const char *unused,
void *arg) {
const struct wrapped_callback *wrapped = (struct wrapped_callback*) arg;
OBJ_NAME obj_name;

OPENSSL_memset(&obj_name, 0, sizeof(obj_name));
obj_name.type = OBJ_NAME_TYPE_MD_METH;
obj_name.name = name;
obj_name.data = (const char *)md;

wrapped->callback(&obj_name, wrapped->arg);
}

void OBJ_NAME_do_all_sorted(int type,
void (*callback)(const OBJ_NAME *, void *arg),
void *arg) {
struct wrapped_callback wrapped;
wrapped.callback = callback;
wrapped.arg = arg;

if (type == OBJ_NAME_TYPE_CIPHER_METH) {
EVP_CIPHER_do_all_sorted(cipher_callback, &wrapped);
} else if (type == OBJ_NAME_TYPE_MD_METH) {
EVP_MD_do_all_sorted(md_callback, &wrapped);
} else {
assert(0);
}
}
4 changes: 4 additions & 0 deletions crypto/fipsmodule/bn/bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,7 @@ void bn_set_minimal_width(BIGNUM *bn) {
bn->neg = 0;
}
}

int BN_get_flags(const BIGNUM *bn, int flags) {
return bn->flags & flags;
}
8 changes: 8 additions & 0 deletions crypto/fipsmodule/cipher/cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include <openssl/err.h>
#include <openssl/mem.h>
#include <openssl/nid.h>
#include <openssl/obj.h>

#include "internal.h"
#include "../../internal.h"
Expand Down Expand Up @@ -703,6 +704,13 @@ uint32_t EVP_CIPHER_mode(const EVP_CIPHER *cipher) {
return cipher->flags & EVP_CIPH_MODE_MASK;
}

const char *EVP_CIPHER_name(const EVP_CIPHER *cipher) {
if (cipher != NULL) {
return OBJ_nid2sn(cipher->nid);
}
return NULL;
}

int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
const uint8_t *key, const uint8_t *iv, int enc) {
if (cipher) {
Expand Down
2 changes: 0 additions & 2 deletions crypto/pkcs8/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ struct pbe_suite {
const char *pass, size_t pass_len, CBS *param);
};

#define PKCS5_SALT_LEN 8

int PKCS5_pbe2_decrypt_init(const struct pbe_suite *suite, EVP_CIPHER_CTX *ctx,
const char *pass, size_t pass_len, CBS *param);

Expand Down
5 changes: 5 additions & 0 deletions include/openssl/bn.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ OPENSSL_EXPORT BN_ULONG BN_get_word(const BIGNUM *bn);
// returns zero.
OPENSSL_EXPORT int BN_get_u64(const BIGNUM *bn, uint64_t *out);

// BN_get_flags interprets |flags| as a bitmask and returns the flags for |bn|.
// The returned value is a set of bitmask of |BN_FLG_*| values, ORed together,
// or 0 if none of the given flags are set.
OPENSSL_EXPORT int BN_get_flags(const BIGNUM *bn, int flags);


// ASN.1 functions.

Expand Down
3 changes: 3 additions & 0 deletions include/openssl/cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ OPENSSL_EXPORT int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *ctx,
// |NID_aes_128_gcm|.)
OPENSSL_EXPORT int EVP_CIPHER_nid(const EVP_CIPHER *cipher);

// EVP_CIPHER_name returns the short name of |cipher|.
OPENSSL_EXPORT const char *EVP_CIPHER_name(const EVP_CIPHER *cipher);

// EVP_CIPHER_block_size returns the block size, in bytes, for |cipher|, or one
// if |cipher| is a stream cipher.
OPENSSL_EXPORT unsigned EVP_CIPHER_block_size(const EVP_CIPHER *cipher);
Expand Down
2 changes: 2 additions & 0 deletions include/openssl/evp.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ OPENSSL_EXPORT int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
// function that results in a key suitable for use in symmetric
// cryptography.

#define PKCS5_SALT_LEN 8

// PKCS5_PBKDF2_HMAC computes |iterations| iterations of PBKDF2 of |password|
// and |salt|, using |digest|, and outputs |key_len| bytes to |out_key|. It
// returns one on success and zero on allocation failure or if |iterations| is
Expand Down
11 changes: 11 additions & 0 deletions include/openssl/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ typedef struct obj_name_st {
#define OBJ_NAME_TYPE_MD_METH 1
#define OBJ_NAME_TYPE_CIPHER_METH 2

// OBJ_NAME_do_all_sorted calls |callback| zero or more times, each time with
// the name of a different primitive. If |type| is |OBJ_NAME_TYPE_MD_METH| then
// the primitives will be hash functions, alternatively if |type| is
// |OBJ_NAME_TYPE_CIPHER_METH| then the primitives will be ciphers or cipher
// modes.
//
// This function is ill-specified and should never be used.
OPENSSL_EXPORT void OBJ_NAME_do_all_sorted(
int type, void (*callback)(const OBJ_NAME *, void *arg), void *arg);


// OBJ_cleanup does nothing.
OPENSSL_EXPORT void OBJ_cleanup(void);

Expand Down

0 comments on commit 6103e75

Please sign in to comment.