Skip to content

Instantly share code, notes, and snippets.

@arlolra arlolra/d
Created Jul 23, 2015

Embed
What would you like to do?
diff --git a/chat/libraries/gcrypt/gcrypt-shim.c b/chat/libraries/gcrypt/gcrypt-shim.c
--- a/chat/libraries/gcrypt/gcrypt-shim.c
+++ b/chat/libraries/gcrypt/gcrypt-shim.c
@@ -17,19 +17,24 @@
/*
* The headers used to reimplement the above.
*/
#include <mozilla/mozalloc.h>
#include <gpg-error.h>
#include <assert.h>
#include "pk11pub.h"
#include "../../../mozilla/security/nss/lib/freebl/mpi/mpi.h"
+// FIXME: priv!?
#include "../../../mozilla/security/nss/lib/freebl/mpi/mpi-priv.h"
#include "../../../mozilla/security/nss/lib/freebl/blapi.h"
+// FIXME: #include "../../../mozilla/security/nss/lib/freebl/secmpi.h"
+// Modified to find mpi.h and not `goto`.
+#include "secmpi.h"
+
/* Register a custom memory allocation functions. */
void gcry_set_allocation_handler(gcry_handler_alloc_t func_alloc,
gcry_handler_alloc_t func_alloc_secure,
gcry_handler_secure_check_t func_secure_check,
gcry_handler_realloc_t func_realloc,
gcry_handler_free_t func_free) {
// This is a no-op since we just directly override the functions below.
}
@@ -249,24 +254,26 @@ struct gcry_cipher_handle {
// Always used with the same settings, AES in CTR Mode. Let's assert that
// and then just move forward. No need to generalize anything.
gcry_error_t gcry_cipher_open(gcry_cipher_hd_t *hd, int algo, int mode,
unsigned int flags) {
assert(algo == GCRY_CIPHER_AES);
assert(mode == GCRY_CIPHER_MODE_CTR);
assert(flags == GCRY_CIPHER_SECURE);
- hd = &malloc(sizeof(gcry_cipher_hd_t));
- if (!hd)
+ gcry_cipher_hd_t handle = NULL;
+ handle = malloc(sizeof(*handle));
+ if (!handle)
return gpg_error(GPG_ERR_ENOMEM);
- hd->key = NULL;
- hd->keylen = 0;
- hd->ctr = NULL;
- hd->ctrlen = 0;
- hd->cx = AES_AllocateContext();
+ handle->key = NULL;
+ handle->keylen = 0;
+ handle->ctr = NULL;
+ handle->ctrlen = 0;
+ handle->cx = AES_AllocateContext();
+ *hd = handle;
return gpg_error(GPG_ERR_NO_ERROR);
};
// This always follows a `gcry_cipher_open`.
gcry_error_t gcry_cipher_setkey(gcry_cipher_hd_t hd, const void *key,
size_t keylen) {
hd->key = key; // memcpy?
hd->keylen = keylen;
@@ -364,40 +371,42 @@ void gcry_cipher_close(gcry_cipher_hd_t
return;
if (hd->cx)
AES_DestroyContext(hd->cx, PR_TRUE); // And free it.
return free(hd);
};
// We need to comment this out from gcrypt.h and just use our definition.
-typedef struct gcry_md_handle {
+struct gcry_md_handle {
HASH_HashType algo;
HMACContext *cx;
unsigned char digest[256]; // The larger of SHA1 and SHA256.
-} *gcry_md_hd_t;
+};
// Always used with pretty much same settings, HMAC with SHA1 or SHA256. Let's
// assert that and then just move forward. No need to generalize anything.
gcry_error_t gcry_md_open(gcry_md_hd_t *hd, int algo, unsigned int flags) {
assert(flags == GCRY_MD_FLAG_HMAC);
- *hd = malloc(sizeof(gcry_md_hd_t));
- if (!*hd)
+ gcry_md_hd_t handle = NULL;
+ handle = malloc(sizeof(*handle));
+ if (!handle)
return gpg_error(GPG_ERR_ENOMEM);
switch (algo) {
case GCRY_MD_SHA1:
- hd->algo = HASH_AlgSHA1;
+ handle->algo = HASH_AlgSHA1;
break;
case GCRY_MD_SHA256:
- hd->algo = HASH_AlgSHA256;
+ handle->algo = HASH_AlgSHA256;
break;
default:
assert(false);
}
- hd->cx = NULL;
+ handle->cx = NULL;
+ *hd = handle;
return gpg_error(GPG_ERR_NO_ERROR);
};
// This always follows a `gcry_md_open`.
gcry_error_t gcry_md_setkey(gcry_md_hd_t hd, const void *key, size_t keylen) {
SECHashObject *hash_obj = NULL;
hash_obj = (SECHashObject *)HASH_GetRawHashObject(hd->algo);
hd->cx = HMAC_Create(
@@ -548,22 +557,20 @@ cleanup:
mp_clear(&y);
if (params)
PQG_DestroyParams(params);
if (verify)
PQG_DestroyVerify(verify);
- if (privKey) {
- PORT_FreeArena(privKey->arena, PR_TRUE);
- privKey = NULL;
- }
+ if (privKey)
+ PORT_FreeArena(privKey->params.arena, PR_TRUE);
- return gpg_error(err);
+ return gpg_error(rc);
};
// From libgcrypt's dsa.c
typedef struct {
gcry_mpi_t p;
gcry_mpi_t q;
gcry_mpi_t g;
gcry_mpi_t y;
@@ -586,17 +593,17 @@ gcry_error_t gcry_pk_sign(gcry_sexp_t *r
DSAPrivateKey *key = NULL;
SECItem *digest = NULL;
SECItem *signature = NULL;
PLArenaPool *arena = NULL;
mp_int r, s;
unsigned int len = 0;
// Alloc an arena
- arena = PORT_NewArena(NSS_FREEBL_DSA_DEFAULT_CHUNKSIZE);
+ arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE);
if (!arena) {
rc = GPG_ERR_ENOMEM;
goto cleanup;
}
// Convert data to a SECITEM
data_mpi = sexp_nth_mpi(data, 0, 0);
if (!data_mpi) {
@@ -627,21 +634,21 @@ gcry_error_t gcry_pk_sign(gcry_sexp_t *r
goto cleanup;
key = (DSAPrivateKey *)PORT_ArenaZAlloc(arena, sizeof(DSAPrivateKey));
if (!key) {
rc = GPG_ERR_ENOMEM;
goto cleanup;
}
- MPINT_TO_SECITEM(&sk.p, key->params.prime, arena);
- MPINT_TO_SECITEM(&sk.q, key->params.subPrime, arena);
- MPINT_TO_SECITEM(&sk.g, key->params.base, arena);
- MPINT_TO_SECITEM(&sk.y, key->publicValue, arena);
- MPINT_TO_SECITEM(&sk.x, key->privateValue, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.p, key->params.prime, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.q, key->params.subPrime, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.g, key->params.base, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.y, key->publicValue, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.x, key->privateValue, arena);
// Alloc space for the signature
signature = (SECItem *)PORT_ArenaZAlloc(arena, sizeof(SECItem));
if (!signature) {
rc = GPG_ERR_ENOMEM;
goto cleanup;
}
@@ -697,17 +704,17 @@ gcry_error_t gcry_pk_verify(gcry_sexp_t
DSAPrivateKey *key = NULL;
SECItem *digest = NULL;
SECItem *signature = NULL;
PLArenaPool *arena = NULL;
mp_err err = 0;
unsigned int dsa_subprime_len = 0;
// Alloc an arena
- arena = PORT_NewArena(NSS_FREEBL_DSA_DEFAULT_CHUNKSIZE);
+ arena = PORT_NewArena(NSS_FREEBL_DEFAULT_CHUNKSIZE);
if (!arena) {
rc = GPG_ERR_ENOMEM;
goto cleanup;
}
// Convert data to a SECITEM
data_mpi = sexp_nth_mpi(data, 0, 0);
if (!data_mpi) {
@@ -738,21 +745,21 @@ gcry_error_t gcry_pk_verify(gcry_sexp_t
goto cleanup;
key = (DSAPrivateKey *)PORT_ArenaZAlloc(arena, sizeof(DSAPrivateKey));
if (!key) {
rc = GPG_ERR_ENOMEM;
goto cleanup;
}
- MPINT_TO_SECITEM(&sk.p, key->params.prime, arena);
- MPINT_TO_SECITEM(&sk.q, key->params.subPrime, arena);
- MPINT_TO_SECITEM(&sk.g, key->params.base, arena);
- MPINT_TO_SECITEM(&sk.y, key->publicValue, arena);
- MPINT_TO_SECITEM(&sk.x, key->privateValue, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.p, key->params.prime, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.q, key->params.subPrime, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.g, key->params.base, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.y, key->publicValue, arena);
+ MPINT_TO_SECITEM((mp_int *)sk.x, key->privateValue, arena);
// Convert the signature in sexp to SECITEM.
// See the format above in `gcry_pk_sign`.
list = sexp_find_token(sigval, "sig-val", 0);
if (!list) {
rc = GPG_ERR_INV_OBJ;
goto cleanup;
}
diff --git a/chat/libraries/gcrypt/gcrypt.h b/chat/libraries/gcrypt/gcrypt.h
--- a/chat/libraries/gcrypt/gcrypt.h
+++ b/chat/libraries/gcrypt/gcrypt.h
@@ -372,26 +372,31 @@ gcry_error_t gcry_sexp_create (gcry_sexp
/* Scan BUFFER and return a new S-expression object in RETSEXP. This
function expects a printf like string in BUFFER. */
gcry_error_t gcry_sexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
const char *buffer, size_t length);
/* Same as gcry_sexp_sscan but expects a string in FORMAT and can thus
only be used for certain encodings. */
-gcry_error_t gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff,
+gcry_error_t _gcry_sexp_build (gcry_sexp_t *retsexp, size_t *erroff,
const char *format, ...);
+#define gcry_sexp_build _gcry_sexp_build
+#define sexp_build _gcry_sexp_build
+
/* Like gcry_sexp_build, but uses an array instead of variable
function arguments. */
gcry_error_t gcry_sexp_build_array (gcry_sexp_t *retsexp, size_t *erroff,
const char *format, void **arg_list);
/* Release the S-expression object SEXP */
-void gcry_sexp_release (gcry_sexp_t sexp);
+void _gcry_sexp_release (gcry_sexp_t sexp);
+#define gcry_sexp_release _gcry_sexp_release
+#define sexp_release _gcry_sexp_release
/* Calculate the length of an canonized S-expresion in BUFFER and
check for a valid encoding. */
size_t gcry_sexp_canon_len (const unsigned char *buffer, size_t length,
size_t *erroff, gcry_error_t *errcode);
/* Copies the S-expression object SEXP into BUFFER using the format
specified in MODE. */
@@ -408,18 +413,21 @@ gcry_sexp_t gcry_sexp_vlist (const gcry_
gcry_sexp_t gcry_sexp_append (const gcry_sexp_t a, const gcry_sexp_t n);
gcry_sexp_t gcry_sexp_prepend (const gcry_sexp_t a, const gcry_sexp_t n);
/* Scan the S-expression for a sublist with a type (the car of the
list) matching the string TOKEN. If TOKLEN is not 0, the token is
assumed to be raw memory of this length. The function returns a
newly allocated S-expression consisting of the found sublist or
`NULL' when not found. */
-gcry_sexp_t gcry_sexp_find_token (gcry_sexp_t list,
+gcry_sexp_t _gcry_sexp_find_token (gcry_sexp_t list,
const char *tok, size_t toklen);
+#define gcry_sexp_find_token _gcry_sexp_find_token
+#define sexp_find_token _gcry_sexp_find_token
+
/* Return the length of the LIST. For a valid S-expression this
should be at least 1. */
int gcry_sexp_length (const gcry_sexp_t list);
/* Create and return a new S-expression from the element with index
NUMBER in LIST. Note that the first element has the index 0. If
there is no such element, `NULL' is returned. */
gcry_sexp_t gcry_sexp_nth (const gcry_sexp_t list, int number);
@@ -431,17 +439,19 @@ gcry_sexp_t gcry_sexp_car (const gcry_se
/* Create and return a new list form all elements except for the first
one. Note, that this function may return an invalid S-expression
because it is not guaranteed, that the type exists and is a string.
However, for parsing a complex S-expression it might be useful for
intermediate lists. Returns `NULL' on error. */
gcry_sexp_t gcry_sexp_cdr (const gcry_sexp_t list);
-gcry_sexp_t gcry_sexp_cadr (const gcry_sexp_t list);
+gcry_sexp_t _gcry_sexp_cadr (const gcry_sexp_t list);
+#define gcry_sexp_cadr _gcry_sexp_cadr
+#define sexp_cadr _gcry_sexp_cadr
/* This function is used to get data from a LIST. A pointer to the
actual data with index NUMBER is returned and the length of this
data will be stored to DATALEN. If there is no data at the given
index or the index represents another list, `NULL' is returned.
*Note:* The returned pointer is valid as long as LIST is not
modified or released. */
@@ -463,24 +473,29 @@ void *gcry_sexp_nth_buffer (const gcry_s
char *gcry_sexp_nth_string (gcry_sexp_t list, int number);
/* This function is used to get and convert data from a LIST. This
data is assumed to be an MPI stored in the format described by
MPIFMT and returned as a standard Libgcrypt MPI. The caller must
release this returned value using `gcry_mpi_release'. If there is
no data at the given index, the index represents a list or the
value can't be converted to an MPI, `NULL' is returned. */
-gcry_mpi_t gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt);
+gcry_mpi_t _gcry_sexp_nth_mpi (gcry_sexp_t list, int number, int mpifmt);
+#define gcry_sexp_nth_mpi _gcry_sexp_nth_mpi
+#define sexp_nth_mpi _gcry_sexp_nth_mpi
/* Convenience fucntion to extract parameters from an S-expression
* using a list of single letter parameters. */
-gpg_error_t gcry_sexp_extract_param (gcry_sexp_t sexp,
+gpg_error_t _gcry_sexp_extract_param (gcry_sexp_t sexp,
const char *path,
const char *list,
...) _GCRY_GCC_ATTR_SENTINEL(0);
+#define gcry_sexp_extract_param _gcry_sexp_extract_param
+#define sexp_extract_param _gcry_sexp_extract_param
+
/*******************************************
* *
* Multi Precision Integer Functions *
* *
*******************************************/
@@ -985,17 +1000,18 @@ gcry_error_t gcry_cipher_authenticate (g
gcry_error_t gcry_cipher_gettag (gcry_cipher_hd_t hd, void *outtag,
size_t taglen);
/* Check authentication tag for AEAD modes/ciphers. */
gcry_error_t gcry_cipher_checktag (gcry_cipher_hd_t hd, const void *intag,
size_t taglen);
/* Reset the handle to the state after open. */
-#define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0)
+// #define gcry_cipher_reset(h) gcry_cipher_ctl ((h), GCRYCTL_RESET, NULL, 0)
+gcry_error_t gcry_cipher_reset(gcry_cipher_hd_t hd);
/* Perform the OpenPGP sync operation if this is enabled for the
cipher handle H. */
#define gcry_cipher_sync(h) gcry_cipher_ctl( (h), GCRYCTL_CFB_SYNC, NULL, 0)
/* Enable or disable CTS in future calls to gcry_encrypt(). CBC mode only. */
#define gcry_cipher_cts(h,on) gcry_cipher_ctl( (h), GCRYCTL_SET_CBC_CTS, \
NULL, on )
@@ -1158,26 +1174,28 @@ enum gcry_md_flags
};
/* (Forward declaration.) */
struct gcry_md_context;
/* This object is used to hold a handle to a message digest object.
This structure is private - only to be used by the public gcry_md_*
macros. */
-typedef struct gcry_md_handle
-{
- /* Actual context. */
- struct gcry_md_context *ctx;
-
- /* Buffer management. */
- int bufpos;
- int bufsize;
- unsigned char buf[1];
-} *gcry_md_hd_t;
+// typedef struct gcry_md_handle
+// {
+// /* Actual context. */
+// struct gcry_md_context *ctx;
+//
+// /* Buffer management. */
+// int bufpos;
+// int bufsize;
+// unsigned char buf[1];
+// } *gcry_md_hd_t;
+struct gcry_md_handle;
+typedef struct gcry_md_handle *gcry_md_hd_t;
/* Compatibility types, do not use them. */
#ifndef GCRYPT_NO_DEPRECATED
typedef struct gcry_md_handle *GCRY_MD_HD _GCRY_GCC_ATTR_DEPRECATED;
typedef struct gcry_md_handle *GcryMDHd _GCRY_GCC_ATTR_DEPRECATED;
#endif
/* Create a message digest object for algorithm ALGO. FLAGS may be
diff --git a/chat/libraries/gcrypt/sexp.c b/chat/libraries/gcrypt/sexp.c
--- a/chat/libraries/gcrypt/sexp.c
+++ b/chat/libraries/gcrypt/sexp.c
@@ -28,16 +28,31 @@
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#define GCRYPT_NO_MPI_MACROS 1
/*#include "g10lib.h"*/
#include "gcrypt.h"
+#define log_printf _gcry_log_printf
+void
+_gcry_log_printf (const char *fmt, ...)
+{
+ // va_list arg_ptr;
+ //
+ // if (fmt)
+ // {
+ // va_start( arg_ptr, fmt ) ;
+ // _gcry_logv (GCRY_LOG_CONT, fmt, arg_ptr);
+ // va_end(arg_ptr);
+ // }
+}
+
+
/* From gcrypt/types.h */
#include <sys/types.h>
#ifndef HAVE_BYTE_TYPEDEF
# undef byte /* In case there is a macro with that name. */
# if !(defined(_WIN32) && defined(cbNDRContext))
/* Windows typedefs byte in the rpc headers. Avoid warning about
double definition. */
typedef unsigned char byte;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.