34 #ifndef _QORE_QL_CRYPTO_H
36 #define _QORE_QL_CRYPTO_H
38 #include <openssl/err.h>
39 #include <openssl/evp.h>
40 #include <openssl/des.h>
41 #include <openssl/hmac.h>
43 #define MD2_ERR "MD2-DIGEST-ERROR"
44 #define MD4_ERR "MD4-DIGEST-ERROR"
45 #define MD5_ERR "MD5-DIGEST-ERROR"
46 #define SHA_ERR "SHA-DIGEST-ERROR"
47 #define SHA1_ERR "SHA1-DIGEST-ERROR"
48 static const char SHA224_ERR[] =
"SHA224-DIGEST-ERROR";
49 static const char SHA256_ERR[] =
"SHA256-DIGEST-ERROR";
50 static const char SHA384_ERR[] =
"SHA384-DIGEST-ERROR";
51 static const char SHA512_ERR[] =
"SHA512-DIGEST-ERROR";
52 #define DSS_ERR "DSS-DIGEST-ERROR"
53 #define DSS1_ERR "DSS1-DIGEST-ERROR"
54 static const char MDC2_ERR[] =
"MDC2-DIGEST-ERROR";
55 #define RIPEMD160_ERR "RIPEMD160-DIGEST-ERROR"
64 unsigned char md_value[EVP_MAX_MD_SIZE > HMAC_MAX_MD_CBLOCK ? EVP_MAX_MD_SIZE : HMAC_MAX_MD_CBLOCK];
67 DLLLOCAL
void setInput(
const QoreString& str) {
68 input = (
unsigned char*)str.
c_str();
73 input = (
unsigned char*)b.
getPtr();
77 DLLLOCAL
void setInput(
const QoreValue pt) {
87 DLLLOCAL
unsigned int size()
const {
91 DLLLOCAL
const void* getBuffer()
const {
92 return (
const void*)md_value;
95 DLLLOCAL
const void* c_str()
const {
96 return (
const void*)md_value;
99 DLLLOCAL
void getString(
QoreString& str)
const {
100 for (
unsigned i = 0; i < md_len; i++)
101 str.
sprintf(
"%02x", md_value[i]);
106 for (
unsigned i = 0; i < md_len; i++)
107 str->
sprintf(
"%02x", md_value[i]);
114 b->
append(md_value, md_len);
119 class QoreEvpHelper {
121 DLLLOCAL QoreEvpHelper() {
122 mdctx = EVP_MD_CTX_create();
125 DLLLOCAL ~QoreEvpHelper() {
127 EVP_MD_CTX_destroy(mdctx);
130 DLLLOCAL EVP_MD_CTX* operator*() {
134 DLLLOCAL
const EVP_MD_CTX* operator*()
const {
142 class QoreEvpCipherCtxHelper {
144 DLLLOCAL QoreEvpCipherCtxHelper() : ctx(EVP_CIPHER_CTX_new()) {
146 EVP_CIPHER_CTX_init(ctx);
150 DLLLOCAL ~QoreEvpCipherCtxHelper() {
152 EVP_CIPHER_CTX_free(ctx);
156 DLLLOCAL EVP_CIPHER_CTX* operator*() {
160 DLLLOCAL
const EVP_CIPHER_CTX* operator*()
const {
164 DLLLOCAL
operator bool()
const {
165 return ctx ? true :
false;
172 class DigestHelper :
public BaseHelper {
174 DLLLOCAL DigestHelper(
const QoreValue v) {
182 DLLLOCAL DigestHelper(
const QoreString& str) {
190 DLLLOCAL DigestHelper(
const void* buf,
size_t len) {
191 input = (
unsigned char*)buf;
195 DLLLOCAL
int doDigest(
const char* err,
const EVP_MD* md,
ExceptionSink* xsink = 0) {
199 xsink->raiseException(err,
"error creating digest object");
203 EVP_DigestInit_ex(*mdctx, md, 0);
204 if (!EVP_DigestUpdate(*mdctx, input, input_len) || !EVP_DigestFinal_ex(*mdctx, md_value, &md_len)) {
206 xsink->raiseException(err,
"error calculating digest");
215 #ifndef OPENSSL_3_PLUS
216 class QoreHmacHelper {
218 DLLLOCAL QoreHmacHelper() {
219 #if defined(HAVE_OPENSSL_INIT_CRYPTO)
220 ctx = HMAC_CTX_new();
226 DLLLOCAL ~QoreHmacHelper() {
227 #ifdef HAVE_OPENSSL_INIT_CRYPTO
230 HMAC_CTX_cleanup(&ctx);
234 DLLLOCAL HMAC_CTX* operator*() {
235 #ifdef HAVE_OPENSSL_INIT_CRYPTO
242 DLLLOCAL
const HMAC_CTX* operator*()
const {
243 #ifdef HAVE_OPENSSL_INIT_CRYPTO
251 #ifdef HAVE_OPENSSL_INIT_CRYPTO
252 typedef HMAC_CTX* q_hmac_t;
254 typedef HMAC_CTX q_hmac_t;
261 class HMACHelper :
public BaseHelper {
279 DLLLOCAL HMACHelper(
const void* buf,
size_t len) {
280 input = (
unsigned char*)buf;
284 DLLLOCAL
int doHMAC(
const char* err,
const char* digest,
const char* ptr,
size_t len,
ExceptionSink* xsink);
holds arbitrary binary data
Definition: BinaryNode.h:41
DLLEXPORT void append(const void *nptr, size_t size)
resizes the object and appends a copy of the data passed to the object
DLLEXPORT size_t size() const
returns the number of bytes in the object
DLLEXPORT const void * getPtr() const
returns the pointer to the data
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
DLLEXPORT size_t strlen() const
returns number of bytes in the string (not including the null pointer)
DLLEXPORT int sprintf(const char *fmt,...)
this will concatentate a formatted string to the existing string according to the format string and t...
DLLEXPORT const char * c_str() const
returns the string's buffer; this data should not be changed
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
const qore_type_t NT_BINARY
type value for BinaryNode
Definition: node_types.h:49
const qore_type_t NT_STRING
type value for QoreStringNode
Definition: node_types.h:45
static QoreValue get_param_value(const QoreListNode *n, size_t i)
returns the argument in the position given or 0 if there is none
Definition: params.h:78
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275