Started using OpenSSL as cryptographic back-end.
[libisds.git] / src / crypto.c
blob40ba61d99f204c04b1246eb33deb6ab3203d1a5e
2 #include <assert.h>
3 #include <stdio.h>
4 #include <string.h>
6 #include "crypto.h"
7 #include "utils.h"
11 * Testing via result comparison.
12 * The only purpose of this file is to test the two cryptographic back-ends.
13 * This functions are not intended to be used in production.
14 * */
17 isds_error _isds_init_crypto_gpg(void);
18 isds_error _isds_init_crypto_openssl(void);
20 _hidden isds_error _isds_init_crypto(void)
22 if (IE_SUCCESS != _isds_init_crypto_gpg()) {
23 return IE_ERROR;
26 if (IE_SUCCESS != _isds_init_crypto_openssl()) {
27 return IE_ERROR;
30 return IE_SUCCESS;
34 isds_error _isds_compute_hash_gpg(const void *input, const size_t length,
35 struct isds_hash *hash);
36 isds_error _isds_compute_hash_openssl(const void *input, const size_t length,
37 struct isds_hash *hash);
39 _hidden isds_error _isds_compute_hash(const void *input, const size_t length,
40 struct isds_hash *hash)
42 isds_error retval_gpg = IE_SUCCESS;
43 isds_error retval_openssl = IE_SUCCESS;
44 isds_error retval = IE_SUCCESS;
45 void *orig_hash_val = NULL;
46 size_t orig_hash_len = 0;
48 retval_gpg = _isds_compute_hash_gpg(input, length, hash);
50 orig_hash_val = hash->value; hash->value = NULL;
51 orig_hash_len = hash->length; hash->length = 0;
53 retval_openssl = _isds_compute_hash_openssl(input, length, hash);
55 if (retval_gpg != retval_openssl) {
56 fprintf(stderr, "%s: Return values differ.\n", __func__);
57 assert(0);
58 retval = IE_ERROR;
59 goto fail;
62 if (IE_SUCCESS != retval_gpg) {
63 retval = retval_gpg;
64 goto fail;
67 if (orig_hash_len != hash->length) {
68 fprintf(stderr, "%s: Hash size differs %lu %lu.\n", __func__,
69 orig_hash_len, hash->length);
70 assert(0);
71 retval = IE_ERROR;
72 goto fail;
75 if (orig_hash_val == hash->value) {
76 fprintf(stderr, "%s: Hashes are in the same location.\n", __func__);
77 assert(0);
78 retval = IE_ERROR;
79 goto fail;
82 if (0 != memcmp(orig_hash_val, hash->value, hash->length)) {
83 fprintf(stderr, "%s: Hash value differ.\n", __func__);
84 assert(0);
85 retval = IE_ERROR;
86 goto fail;
89 free(orig_hash_val); orig_hash_val = NULL; orig_hash_len = 0;
91 return IE_SUCCESS;
93 fail:
94 if (NULL != orig_hash_val) {
95 free(orig_hash_val);
97 return retval;
101 void _isds_cms_data_free_gpg(void *buffer);
102 void _isds_cms_data_free_opnssl(void *buffer);
104 _hidden void _isds_cms_data_free(void *buffer)
106 _isds_cms_data_free_gpg(buffer);
110 isds_error _isds_extract_cms_data_gpg(struct isds_ctx *context,
111 const void *cms, const size_t cms_length,
112 void **data, size_t *data_length);
113 isds_error _isds_extract_cms_data_openssl(struct isds_ctx *context,
114 const void *cms, const size_t cms_length,
115 void **data, size_t *data_length);
117 _hidden isds_error _isds_extract_cms_data(struct isds_ctx *context,
118 const void *cms, const size_t cms_length,
119 void **data, size_t *data_length)
121 return _isds_extract_cms_data_gpg(context, cms, cms_length, data,
122 data_length);