|
Qore Programming Language
0.9.16
|
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) {
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
void getString(
QoreString& str)
const {
96 for (
unsigned i = 0; i < md_len; i++)
97 str.
sprintf(
"%02x", md_value[i]);
102 for (
unsigned i = 0; i < md_len; i++)
103 str->
sprintf(
"%02x", md_value[i]);
110 b->
append(md_value, md_len);
115 class QoreEvpHelper {
117 DLLLOCAL QoreEvpHelper() {
118 mdctx = EVP_MD_CTX_create();
121 DLLLOCAL ~QoreEvpHelper() {
123 EVP_MD_CTX_destroy(mdctx);
126 DLLLOCAL EVP_MD_CTX* operator*() {
130 DLLLOCAL
const EVP_MD_CTX* operator*()
const {
138 class QoreEvpCipherCtxHelper {
140 DLLLOCAL QoreEvpCipherCtxHelper() {
141 ctx = EVP_CIPHER_CTX_new();
144 DLLLOCAL ~QoreEvpCipherCtxHelper() {
146 EVP_CIPHER_CTX_free(ctx);
149 DLLLOCAL EVP_CIPHER_CTX* operator*() {
153 DLLLOCAL
const EVP_CIPHER_CTX* operator*()
const {
161 class DigestHelper :
public BaseHelper {
163 DLLLOCAL DigestHelper(
const QoreValue v) {
171 DLLLOCAL DigestHelper(
const QoreString& str) {
179 DLLLOCAL DigestHelper(
const void* buf,
size_t len) {
180 input = (
unsigned char*)buf;
184 DLLLOCAL
int doDigest(
const char* err,
const EVP_MD* md,
ExceptionSink* xsink = 0) {
188 xsink->raiseException(err,
"error creating digest object");
192 EVP_DigestInit_ex(*mdctx, md, 0);
193 if (!EVP_DigestUpdate(*mdctx, input, input_len) || !EVP_DigestFinal_ex(*mdctx, md_value, &md_len)) {
195 xsink->raiseException(err,
"error calculating digest");
203 class QoreHmacHelper {
205 DLLLOCAL QoreHmacHelper() {
206 #ifdef HAVE_OPENSSL_INIT_CRYPTO
207 ctx = HMAC_CTX_new();
213 DLLLOCAL ~QoreHmacHelper() {
214 #ifdef HAVE_OPENSSL_INIT_CRYPTO
217 HMAC_CTX_cleanup(&ctx);
221 DLLLOCAL HMAC_CTX* operator*() {
222 #ifdef HAVE_OPENSSL_INIT_CRYPTO
229 DLLLOCAL
const HMAC_CTX* operator*()
const {
230 #ifdef HAVE_OPENSSL_INIT_CRYPTO
238 #ifdef HAVE_OPENSSL_INIT_CRYPTO
245 class HMACHelper :
public BaseHelper {
263 DLLLOCAL HMACHelper(
const void* buf,
size_t len) {
264 input = (
unsigned char*)buf;
268 DLLLOCAL
int doHMAC(
const char* err,
const EVP_MD* md,
const char* ptr,
size_t len,
ExceptionSink* xsink) {
275 #ifdef HAVE_OPENSSL_HMAC_RV
276 int rc = HMAC_Init_ex(*ctx, ptr, len, md, 0);
282 HMAC_Init_ex(*ctx, ptr, len, md, 0);
285 #ifdef HAVE_OPENSSL_HMAC_RV
286 if (!HMAC_Update(*ctx, input, input_len)
287 || !HMAC_Final(*ctx, md_value, &md_len)) {
292 HMAC_Update(*ctx, input, input_len);
293 HMAC_Final(*ctx, md_value, &md_len);
300 #endif // _QORE_QL_CRYPTO_H
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT void append(const void *nptr, qore_size_t size)
resizes the object and appends a copy of the data passed to the object
DLLEXPORT int sprintf(const char *fmt,...)
this will concatentate a formatted string to the existing string according to the format string and t...
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:81
DLLEXPORT qore_size_t size() const
returns the number of bytes in the object
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
const qore_type_t NT_STRING
type value for QoreStringNode
Definition: node_types.h:45
const DLLEXPORT char * getBuffer() const
returns the string's buffer; this data should not be changed
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
DLLEXPORT qore_size_t strlen() const
returns number of bytes in the string (not including the null pointer)
const DLLEXPORT void * getPtr() const
returns the pointer to the data
holds arbitrary binary data
Definition: BinaryNode.h:41
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
static QoreValue get_param_value(const QoreListNode *n, qore_size_t i)
returns the argument in the position given or 0 if there is none
Definition: params.h:78