3 #endif /* ifdef HAVE_CONFIG_H */
8 # define alloca __builtin_alloca
10 # define alloca __alloca
11 #elif defined _MSC_VER
13 # define alloca _alloca
14 #else /* ifdef HAVE_ALLOCA_H */
18 # endif /* ifdef __cplusplus */
19 void * alloca (size_t);
20 #endif /* ifdef HAVE_ALLOCA_H */
24 #include <sys/types.h>
30 #endif /* ifndef _MSC_VER */
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
34 #endif /* ifdef HAVE_NETINET_IN_H */
38 # include <gnutls/gnutls.h>
39 # include <gnutls/x509.h>
40 # else /* ifdef HAVE_GNUTLS */
41 # include <openssl/rsa.h>
42 # include <openssl/objects.h>
43 # include <openssl/err.h>
44 # include <openssl/ssl.h>
45 # include <openssl/dh.h>
46 # include <openssl/dsa.h>
47 # include <openssl/evp.h>
48 # include <openssl/x509.h>
49 # include <openssl/pem.h>
50 # endif /* ifdef HAVE_GNUTLS */
51 #endif /* ifdef HAVE_SIGNATURE */
54 # include <openssl/sha.h>
55 #endif /* ifdef HAVE_OPENSSL */
59 # include <gnutls/x509.h>
61 # else /* ifdef HAVE_GNUTLS */
62 # include <openssl/evp.h>
63 # include <openssl/hmac.h>
64 # include <openssl/rand.h>
65 # endif /* ifdef HAVE_GNUTLS */
66 #endif /* ifdef HAVE_CIPHER */
69 #include "Eet_private.h"
71 #define EET_MAGIC_SIGN 0x1ee74271
74 # define MAX_KEY_LEN 32
75 # define MAX_IV_LEN 16
76 #else /* ifdef HAVE_GNUTLS */
77 # define MAX_KEY_LEN EVP_MAX_KEY_LENGTH
78 # define MAX_IV_LEN EVP_MAX_IV_LENGTH
79 #endif /* ifdef HAVE_GNUTLS */
83 static Eet_Error eet_hmac_sha1(const void *key,
88 # endif /* ifdef HAVE_GNUTLS */
89 static Eet_Error eet_pbkdf2_sha1(const char *key,
91 const unsigned char *salt,
92 unsigned int salt_len,
96 #endif /* ifdef HAVE_CIPHER */
101 #ifdef HAVE_SIGNATURE
103 gnutls_x509_crt_t certificate;
104 gnutls_x509_privkey_t private_key;
105 # else /* ifdef HAVE_GNUTLS */
107 EVP_PKEY *private_key;
108 # endif /* ifdef HAVE_GNUTLS */
109 #endif /* ifdef HAVE_SIGNATURE */
113 eet_identity_open(const char *certificate_file,
114 const char *private_key_file,
115 Eet_Key_Password_Callback cb)
117 #ifdef HAVE_SIGNATURE
118 /* Signature declarations */
122 /* Gnutls private declarations */
126 gnutls_datum_t load_file = { NULL, 0 };
130 if (!(key = malloc(sizeof(Eet_Key))))
135 if (gnutls_x509_crt_init(&(key->certificate)))
138 if (gnutls_x509_privkey_init(&(key->private_key)))
141 /* Mmap certificate_file */
142 if (!(fp = fopen(certificate_file, "r")))
145 if ((fd = fileno(fp)) == -1)
152 mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
155 /* Import the certificate in Eet_Key structure */
156 load_file.data = data;
157 load_file.size = st.st_size;
158 if (gnutls_x509_crt_import(key->certificate, &load_file,
159 GNUTLS_X509_FMT_PEM) < 0)
162 if (munmap(data, st.st_size))
169 load_file.data = NULL;
172 /* Mmap private_key_file */
173 if (!(fp = fopen(private_key_file, "r")))
176 if ((fd = fileno(fp)) == -1)
183 mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
186 /* Import the private key in Eet_Key structure */
187 load_file.data = data;
188 load_file.size = st.st_size;
189 /* Try to directly import the PEM encoded private key */
190 if (gnutls_x509_privkey_import(key->private_key, &load_file,
191 GNUTLS_X509_FMT_PEM) < 0)
193 /* Else ask for the private key pass */
194 if (cb && cb(pass, 1024, 0, NULL))
196 /* If pass then try to decode the pkcs 8 private key */
197 if (gnutls_x509_privkey_import_pkcs8(key->private_key, &load_file,
198 GNUTLS_X509_FMT_PEM, pass, 0))
202 /* Else try to import the pkcs 8 private key without pass */
203 if (gnutls_x509_privkey_import_pkcs8(key->private_key, &load_file,
204 GNUTLS_X509_FMT_PEM, NULL, 1))
208 if (munmap(data, st.st_size))
221 if (key->certificate)
222 gnutls_x509_crt_deinit(key->certificate);
224 if (key->private_key)
225 gnutls_x509_privkey_deinit(key->private_key);
231 munmap(data, st.st_size);
233 # else /* ifdef HAVE_GNUTLS */
234 /* Openssl private declarations */
235 EVP_PKEY *pkey = NULL;
238 /* Load the X509 certificate in memory. */
239 fp = fopen(certificate_file, "r");
243 cert = PEM_read_X509(fp, NULL, NULL, NULL);
248 /* Check the presence of the public key. Just in case. */
249 pkey = X509_get_pubkey(cert);
253 /* Load the private key in memory. */
254 fp = fopen(private_key_file, "r");
258 pkey = PEM_read_PrivateKey(fp, NULL, cb, NULL);
263 /* Load the certificate and the private key in Eet_Key structure */
264 key = malloc(sizeof(Eet_Key));
269 key->certificate = cert;
270 key->private_key = pkey;
281 # endif /* ifdef HAVE_GNUTLS */
282 #endif /* ifdef HAVE_SIGNATURE */
284 } /* eet_identity_open */
287 eet_identity_close(Eet_Key *key)
289 #ifdef HAVE_SIGNATURE
290 if (!key || (key->references > 0))
294 gnutls_x509_crt_deinit(key->certificate);
295 gnutls_x509_privkey_deinit(key->private_key);
296 # else /* ifdef HAVE_GNUTLS */
297 X509_free(key->certificate);
298 EVP_PKEY_free(key->private_key);
299 # endif /* ifdef HAVE_GNUTLS */
301 #endif /* ifdef HAVE_SIGNATURE */
302 } /* eet_identity_close */
305 eet_identity_print(Eet_Key *key,
308 #ifdef HAVE_SIGNATURE
310 const char *names[6] = {
319 gnutls_datum_t data = { NULL, 0 };
320 gnutls_datum_t rsa_raw[6];
329 if (key->private_key)
331 if (gnutls_x509_privkey_export_rsa_raw(key->private_key,
332 rsa_raw + 0, /* Modulus */
333 rsa_raw + 1, /* Public exponent */
334 rsa_raw + 2, /* Private exponent */
335 rsa_raw + 3, /* First prime */
336 rsa_raw + 4, /* Second prime */
337 rsa_raw + 5)) /* Coefficient */
340 if (!(res = malloc(size)))
343 fprintf(out, "Private Key:\n");
346 for (i = 0; i < 6; i++)
348 while ((err = gnutls_hex_encode(rsa_raw + i, res, &size)) ==
349 GNUTLS_E_SHORT_MEMORY_BUFFER)
352 if (!(res = realloc(res, size)))
358 fprintf(out, "\t%s:\n", names[i]);
359 for (j = 0; strlen(res) > j; j += 32)
361 snprintf(buf, 32, "%s", res + j);
362 fprintf(out, "\t\t%s\n", buf);
369 if (key->certificate)
371 fprintf(out, "Public certificate:\n");
372 if (gnutls_x509_crt_print(key->certificate, GNUTLS_X509_CRT_FULL,
376 fprintf(out, "%s\n", data.data);
377 gnutls_free(data.data);
386 gnutls_free(data.data);
389 # else /* ifdef HAVE_GNUTLS */
397 rsa = EVP_PKEY_get1_RSA(key->private_key);
400 fprintf(out, "Private key (RSA):\n");
401 RSA_print_fp(out, rsa, 0);
404 dsa = EVP_PKEY_get1_DSA(key->private_key);
407 fprintf(out, "Private key (DSA):\n");
408 DSA_print_fp(out, dsa, 0);
411 dh = EVP_PKEY_get1_DH(key->private_key);
414 fprintf(out, "Private key (DH):\n");
415 DHparams_print_fp(out, dh);
418 fprintf(out, "Public certificate:\n");
419 X509_print_fp(out, key->certificate);
420 # endif /* ifdef HAVE_GNUTLS */
421 #else /* ifdef HAVE_SIGNATURE */
422 ERR("You need to compile signature support in EET.");
423 #endif /* ifdef HAVE_SIGNATURE */
424 } /* eet_identity_print */
427 eet_identity_ref(Eet_Key *key)
433 } /* eet_identity_ref */
436 eet_identity_unref(Eet_Key *key)
442 eet_identity_close(key);
443 } /* eet_identity_unref */
446 eet_identity_compute_sha1(const void *data_base,
447 unsigned int data_length,
452 #ifdef HAVE_SIGNATURE
454 result = malloc(gcry_md_get_algo_dlen(GCRY_MD_SHA1));
458 gcry_md_hash_buffer(GCRY_MD_SHA1, result, data_base, data_length);
460 *sha1_length = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
462 # else /* ifdef HAVE_GNUTLS */
464 result = malloc(SHA_DIGEST_LENGTH);
468 SHA1(data_base, data_length, result);
470 *sha1_length = SHA_DIGEST_LENGTH;
472 # else /* ifdef HAVE_OPENSSL */
474 # endif /* ifdef HAVE_OPENSSL */
475 # endif /* ifdef HAVE_GNUTLS */
476 #else /* ifdef HAVE_SIGNATURE */
478 #endif /* ifdef HAVE_SIGNATURE */
481 } /* eet_identity_compute_sha1 */
484 eet_identity_sign(FILE *fp,
487 #ifdef HAVE_SIGNATURE
488 Eet_Error err = EET_ERROR_NONE;
493 unsigned char *sign = NULL;
494 unsigned char *cert = NULL;
496 gnutls_datum_t datum = { NULL, 0 };
499 # else /* ifdef HAVE_GNUTLS */
501 unsigned int sign_len = 0;
503 # endif /* ifdef HAVE_GNUTLS */
505 /* A few check and flush pending write. */
506 if (!fp || !key || !key->certificate || !key->private_key)
507 return EET_ERROR_BAD_OBJECT;
509 /* Get the file size. */
512 return EET_ERROR_BAD_OBJECT;
514 if (fstat(fd, &st_buf) < 0)
515 return EET_ERROR_MMAP_FAILED;
517 /* Map the file in memory. */
518 data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
519 if (data == MAP_FAILED)
520 return EET_ERROR_MMAP_FAILED;
524 datum.size = st_buf.st_size;
526 /* Get the signature length */
527 if (gnutls_x509_privkey_sign_data(key->private_key, GNUTLS_DIG_SHA1, 0,
528 &datum, sign, &sign_len) &&
531 err = EET_ERROR_SIGNATURE_FAILED;
535 /* Get the signature */
536 sign = malloc(sign_len);
538 gnutls_x509_privkey_sign_data(key->private_key, GNUTLS_DIG_SHA1, 0,
543 err = EET_ERROR_OUT_OF_MEMORY;
545 err = EET_ERROR_SIGNATURE_FAILED;
550 /* Get the certificate length */
551 if (gnutls_x509_crt_export(key->certificate, GNUTLS_X509_FMT_DER, cert,
555 err = EET_ERROR_SIGNATURE_FAILED;
559 /* Get the certificate */
560 cert = malloc(cert_len);
562 gnutls_x509_crt_export(key->certificate, GNUTLS_X509_FMT_DER, cert,
566 err = EET_ERROR_OUT_OF_MEMORY;
568 err = EET_ERROR_SIGNATURE_FAILED;
573 # else /* ifdef HAVE_GNUTLS */
574 sign_len = EVP_PKEY_size(key->private_key);
575 sign = malloc(sign_len);
578 err = EET_ERROR_OUT_OF_MEMORY;
582 /* Do the signature. */
583 EVP_SignInit(&md_ctx, EVP_sha1());
584 EVP_SignUpdate(&md_ctx, data, st_buf.st_size);
585 err = EVP_SignFinal(&md_ctx,
587 (unsigned int *)&sign_len,
591 ERR_print_errors_fp(stdout);
592 err = EET_ERROR_SIGNATURE_FAILED;
596 /* Give me the der (binary form for X509). */
597 cert_len = i2d_X509(key->certificate, &cert);
600 ERR_print_errors_fp(stdout);
601 err = EET_ERROR_X509_ENCODING_FAILED;
605 # endif /* ifdef HAVE_GNUTLS */
606 /* Append the signature at the end of the file. */
607 head[0] = (int)htonl ((unsigned int)EET_MAGIC_SIGN);
608 head[1] = (int)htonl ((unsigned int)sign_len);
609 head[2] = (int)htonl ((unsigned int)cert_len);
611 if (fwrite(head, sizeof(head), 1, fp) != 1)
613 err = EET_ERROR_WRITE_ERROR;
617 if (fwrite(sign, sign_len, 1, fp) != 1)
619 err = EET_ERROR_WRITE_ERROR;
623 if (fwrite(cert, cert_len, 1, fp) != 1)
625 err = EET_ERROR_WRITE_ERROR;
634 # else /* ifdef HAVE_GNUTLS */
638 # endif /* ifdef HAVE_GNUTLS */
642 munmap(data, st_buf.st_size);
644 #else /* ifdef HAVE_SIGNATURE */
645 return EET_ERROR_NOT_IMPLEMENTED;
646 #endif /* ifdef HAVE_SIGNATURE */
647 } /* eet_identity_sign */
650 eet_identity_check(const void *data_base,
651 unsigned int data_length,
654 const void *signature_base,
655 unsigned int signature_length,
656 const void **raw_signature_base,
657 unsigned int *raw_signature_length,
660 #ifdef HAVE_SIGNATURE
661 const int *header = signature_base;
662 const unsigned char *sign;
663 const unsigned char *cert_der;
668 /* At least the header size */
669 if (signature_length < sizeof(int) * 3)
673 magic = ntohl(header[0]);
674 sign_len = ntohl(header[1]);
675 cert_len = ntohl(header[2]);
677 /* Verify the header */
678 if (magic != EET_MAGIC_SIGN)
681 if (sign_len + cert_len + sizeof(int) * 3 > signature_length)
684 /* Update the signature and certificate pointer */
685 sign = (unsigned char *)signature_base + sizeof(int) * 3;
686 cert_der = sign + sign_len;
689 gnutls_x509_crt_t cert;
690 gnutls_datum_t datum;
691 gnutls_datum_t signature;
692 # if EET_USE_NEW_GNUTLS_API
696 # endif /* if EET_USE_NEW_GNUTLS_API */
698 /* Create an understanding certificate structure for gnutls */
699 datum.data = (void *)cert_der;
700 datum.size = cert_len;
701 gnutls_x509_crt_init(&cert);
702 gnutls_x509_crt_import(cert, &datum, GNUTLS_X509_FMT_DER);
704 signature.data = (void *)sign;
705 signature.size = sign_len;
707 /* Verify the signature */
708 # if EET_USE_NEW_GNUTLS_API
710 I am waiting for my patch being accepted in GnuTLS release.
711 But we now have a way to prevent double computation of SHA1.
713 err = gcry_md_open (&md, GCRY_MD_SHA1, 0);
717 gcry_md_write(md, data_base, data_length);
719 hash = gcry_md_read(md, GCRY_MD_SHA1);
726 datum.size = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
729 if (!gnutls_x509_crt_verify_hash(cert, 0, &datum, &signature))
737 *sha1 = malloc(datum.size);
744 memcpy(*sha1, hash, datum.size);
745 *sha1_length = datum.size;
749 # else /* if EET_USE_NEW_GNUTLS_API */
750 datum.data = (void *)data_base;
751 datum.size = data_length;
753 if (!gnutls_x509_crt_verify_data(cert, 0, &datum, &signature))
762 # endif /* if EET_USE_NEW_GNUTLS_API */
763 gnutls_x509_crt_deinit(cert);
765 # else /* ifdef HAVE_GNUTLS */
766 const unsigned char *tmp;
772 /* Strange but d2i_X509 seems to put 0 all over the place. */
773 tmp = alloca(cert_len);
774 memcpy((char *)tmp, cert_der, cert_len);
775 x509 = d2i_X509(NULL, &tmp, cert_len);
779 /* Get public key - eay */
780 pkey = X509_get_pubkey(x509);
787 /* Verify the signature */
788 EVP_VerifyInit(&md_ctx, EVP_sha1());
789 EVP_VerifyUpdate(&md_ctx, data_base, data_length);
790 err = EVP_VerifyFinal(&md_ctx, sign, sign_len, pkey);
804 # endif /* ifdef HAVE_GNUTLS */
806 *x509_length = cert_len;
808 if (raw_signature_base)
809 *raw_signature_base = sign;
811 if (raw_signature_length)
812 *raw_signature_length = sign_len;
815 #else /* ifdef HAVE_SIGNATURE */
817 #endif /* ifdef HAVE_SIGNATURE */
818 } /* eet_identity_check */
821 eet_identity_certificate_print(const unsigned char *certificate,
825 #ifdef HAVE_SIGNATURE
826 if (!certificate || !out || der_length <= 0)
828 ERR("No certificate provided.");
833 gnutls_datum_t datum;
834 gnutls_x509_crt_t cert;
836 /* Create an understanding certificate structure for gnutls */
837 datum.data = (void *)certificate;
838 datum.size = der_length;
839 if (gnutls_x509_crt_init(&cert))
842 if (gnutls_x509_crt_import(cert, &datum, GNUTLS_X509_FMT_DER))
845 /* Pretty print the certificate */
848 if (gnutls_x509_crt_print(cert, GNUTLS_X509_CRT_FULL, &datum))
851 INF("Public certificate :");
852 INF("%s", datum.data);
856 gnutls_free(datum.data);
858 gnutls_x509_crt_deinit(cert);
859 # else /* ifdef HAVE_GNUTLS */
860 const unsigned char *tmp;
863 /* Strange but d2i_X509 seems to put 0 all over the place. */
864 tmp = alloca(der_length);
865 memcpy((char *)tmp, certificate, der_length);
866 x509 = d2i_X509(NULL, &tmp, der_length);
869 INF("Not a valid certificate.");
873 INF("Public certificate :");
874 X509_print_fp(out, x509);
877 # endif /* ifdef HAVE_GNUTLS */
878 #else /* ifdef HAVE_SIGNATURE */
879 ERR("You need to compile signature support in EET.");
880 #endif /* ifdef HAVE_SIGNATURE */
881 } /* eet_identity_certificate_print */
884 eet_cipher(const void *data,
889 unsigned int *result_length)
892 /* Cipher declarations */
893 unsigned int *ret = NULL;
894 unsigned char iv[MAX_IV_LEN];
895 unsigned char ik[MAX_KEY_LEN];
896 unsigned char key_material[MAX_IV_LEN + MAX_KEY_LEN];
898 unsigned int tmp = 0;
902 /* Gcrypt declarations */
903 gcry_error_t err = 0;
904 gcry_cipher_hd_t cipher;
905 # else /* ifdef HAVE_GNUTLS */
906 /* Openssl declarations*/
908 unsigned int *buffer;
910 # endif /* ifdef HAVE_GNUTLS */
913 /* Gcrypt salt generation */
914 gcry_create_nonce((unsigned char *)&salt, sizeof(salt));
915 # else /* ifdef HAVE_GNUTLS */
916 /* Openssl salt generation */
917 if (!RAND_bytes((unsigned char *)&salt, sizeof (unsigned int)))
918 return EET_ERROR_PRNG_NOT_SEEDED;
920 # endif /* ifdef HAVE_GNUTLS */
924 (unsigned char *)&salt,
925 sizeof(unsigned int),
928 MAX_KEY_LEN + MAX_IV_LEN);
930 memcpy(iv, key_material, MAX_IV_LEN);
931 memcpy(ik, key_material + MAX_IV_LEN, MAX_KEY_LEN);
933 memset(key_material, 0, sizeof (key_material));
935 crypted_length = ((((size + sizeof (unsigned int)) >> 5) + 1) << 5);
936 ret = malloc(crypted_length + sizeof(unsigned int));
939 memset(iv, 0, sizeof (iv));
940 memset(ik, 0, sizeof (ik));
941 memset(&salt, 0, sizeof (salt));
942 return EET_ERROR_OUT_OF_MEMORY;
946 memset(&salt, 0, sizeof (salt));
951 memcpy(ret + 2, data, size);
953 /* Gcrypt create the corresponding cipher
954 AES with a 256 bit key, Cipher Block Chaining mode */
955 err = gcry_cipher_open(&cipher, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 0);
960 err = gcry_cipher_setiv(cipher, iv, MAX_IV_LEN);
964 err = gcry_cipher_setkey(cipher, ik, MAX_KEY_LEN);
968 memset(iv, 0, sizeof (iv));
969 memset(ik, 0, sizeof (ik));
972 err = gcry_cipher_encrypt(cipher,
973 (unsigned char *)(ret + 1),
980 /* Gcrypt close the cipher */
981 gcry_cipher_close(cipher);
982 # else /* ifdef HAVE_GNUTLS */
983 buffer = alloca(crypted_length);
986 memcpy(buffer + 1, data, size);
988 /* Openssl create the corresponding cipher
989 AES with a 256 bit key, Cipher Block Chaining mode */
990 EVP_CIPHER_CTX_init(&ctx);
991 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, ik, iv))
996 memset(iv, 0, sizeof (iv));
997 memset(ik, 0, sizeof (ik));
999 /* Openssl encrypt */
1000 if (!EVP_EncryptUpdate(&ctx, (unsigned char *)(ret + 1), &tmp_len,
1001 (unsigned char *)buffer,
1002 size + sizeof(unsigned int)))
1005 /* Openssl close the cipher */
1006 if (!EVP_EncryptFinal_ex(&ctx, ((unsigned char *)(ret + 1)) + tmp_len,
1010 EVP_CIPHER_CTX_cleanup(&ctx);
1011 # endif /* ifdef HAVE_GNUTLS */
1013 /* Set return values */
1015 *result_length = crypted_length + sizeof(unsigned int);
1022 return EET_ERROR_NONE;
1025 memset(iv, 0, sizeof (iv));
1026 memset(ik, 0, sizeof (ik));
1031 gcry_cipher_close(cipher);
1033 # else /* ifdef HAVE_GNUTLS */
1036 EVP_CIPHER_CTX_cleanup(&ctx);
1038 # endif /* ifdef HAVE_GNUTLS */
1047 return EET_ERROR_ENCRYPT_FAILED;
1048 #else /* ifdef HAVE_CIPHER */
1049 /* Cipher not supported */
1055 (void)result_length;
1056 return EET_ERROR_NOT_IMPLEMENTED;
1057 #endif /* ifdef HAVE_CIPHER */
1061 eet_decipher(const void *data,
1064 unsigned int length,
1066 unsigned int *result_length)
1069 const unsigned int *over = data;
1070 unsigned int *ret = NULL;
1071 unsigned char ik[MAX_KEY_LEN];
1072 unsigned char iv[MAX_IV_LEN];
1073 unsigned char key_material[MAX_KEY_LEN + MAX_IV_LEN];
1078 /* At least the salt and an AES block */
1079 if (size < sizeof(unsigned int) + 16)
1080 return EET_ERROR_BAD_OBJECT;
1085 /* Generate the iv and the key with the salt */
1086 eet_pbkdf2_sha1(key, length, (unsigned char *)&salt,
1087 sizeof(unsigned int), 2048, key_material,
1088 MAX_KEY_LEN + MAX_IV_LEN);
1090 memcpy(iv, key_material, MAX_IV_LEN);
1091 memcpy(ik, key_material + MAX_IV_LEN, MAX_KEY_LEN);
1093 memset(key_material, 0, sizeof (key_material));
1094 memset(&salt, 0, sizeof (salt));
1096 /* Align to AES block size if size is not align */
1097 tmp_len = size - sizeof (unsigned int);
1098 if ((tmp_len & 0x1F) != 0)
1101 ret = malloc(tmp_len);
1106 gcry_error_t err = 0;
1107 gcry_cipher_hd_t cipher;
1109 /* Gcrypt create the corresponding cipher */
1110 err = gcry_cipher_open(&cipher, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 0);
1112 return EET_ERROR_DECRYPT_FAILED;
1114 err = gcry_cipher_setiv(cipher, iv, MAX_IV_LEN);
1118 err = gcry_cipher_setkey(cipher, ik, MAX_KEY_LEN);
1122 memset(iv, 0, sizeof (iv));
1123 memset(ik, 0, sizeof (ik));
1125 /* Gcrypt decrypt */
1126 err = gcry_cipher_decrypt(cipher, ret, tmp_len,
1127 ((unsigned int *)data) + 1, tmp_len);
1131 /* Gcrypt close the cipher */
1132 gcry_cipher_close(cipher);
1134 # else /* ifdef HAVE_GNUTLS */
1138 /* Openssl create the corresponding cipher */
1139 EVP_CIPHER_CTX_init(&ctx);
1142 if (!EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, ik, iv))
1145 memset(iv, 0, sizeof (iv));
1146 memset(ik, 0, sizeof (ik));
1148 /* Openssl decrypt */
1149 if (!EVP_DecryptUpdate(&ctx, (unsigned char *)ret, &tmp,
1150 (unsigned char *)(over + 1), tmp_len))
1153 /* Openssl close the cipher*/
1154 EVP_CIPHER_CTX_cleanup(&ctx);
1155 # endif /* ifdef HAVE_GNUTLS */
1156 /* Get the decrypted data size */
1162 /* Update the return values */
1164 *result_length = tmp;
1169 *result = malloc(tmp);
1173 memcpy(*result, ret + 1, tmp);
1178 return EET_ERROR_NONE;
1181 memset(iv, 0, sizeof (iv));
1182 memset(ik, 0, sizeof (ik));
1187 EVP_CIPHER_CTX_cleanup(&ctx);
1189 # endif /* ifdef HAVE_GNUTLS */
1199 return EET_ERROR_DECRYPT_FAILED;
1200 #else /* ifdef HAVE_CIPHER */
1206 (void)result_length;
1207 return EET_ERROR_NOT_IMPLEMENTED;
1208 #endif /* ifdef HAVE_CIPHER */
1209 } /* eet_decipher */
1214 eet_hmac_sha1(const void *key,
1220 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
1222 unsigned char *hash;
1225 err = gcry_md_open(&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
1226 if (err != GPG_ERR_NO_ERROR)
1229 err = gcry_md_setkey(mdh, key, key_len);
1230 if (err != GPG_ERR_NO_ERROR)
1236 gcry_md_write(mdh, data, data_len);
1238 hash = gcry_md_read(mdh, GCRY_MD_SHA1);
1245 memcpy(res, hash, hlen);
1250 } /* eet_hmac_sha1 */
1252 # endif /* ifdef HAVE_GNUTLS */
1255 eet_pbkdf2_sha1(const char *key,
1257 const unsigned char *salt,
1258 unsigned int salt_len,
1263 unsigned char digest[20];
1264 unsigned char tab[4];
1265 unsigned char *p = res;
1268 int digest_len = 20;
1275 # endif /* ifdef HAVE_GNUTLS */
1277 buf = alloca(salt_len + 4);
1281 for (i = 1; len; len -= tmp_len, p += tmp_len, i++)
1283 if (len > digest_len)
1284 tmp_len = digest_len;
1288 tab[0] = (unsigned char)(i & 0xff000000) >> 24;
1289 tab[1] = (unsigned char)(i & 0x00ff0000) >> 16;
1290 tab[2] = (unsigned char)(i & 0x0000ff00) >> 8;
1291 tab[3] = (unsigned char)(i & 0x000000ff) >> 0;
1294 memcpy(buf, salt, salt_len);
1295 memcpy(buf + salt_len, tab, 4);
1296 eet_hmac_sha1(key, key_len, buf, salt_len + 4, digest);
1297 # else /* ifdef HAVE_GNUTLS */
1298 HMAC_Init(&hctx, key, key_len, EVP_sha1());
1299 HMAC_Update(&hctx, salt, salt_len);
1300 HMAC_Update(&hctx, tab, 4);
1301 HMAC_Final(&hctx, digest, NULL);
1302 # endif /* ifdef HAVE_GNUTLS */
1303 memcpy(p, digest, tmp_len);
1305 for (j = 1; j < iter; j++)
1308 eet_hmac_sha1(key, key_len, digest, 20, digest);
1309 # else /* ifdef HAVE_GNUTLS */
1310 HMAC(EVP_sha1(), key, key_len, digest, 20, digest, NULL);
1311 # endif /* ifdef HAVE_GNUTLS */
1312 for (k = 0; k < tmp_len; k++)
1319 HMAC_cleanup(&hctx);
1320 # endif /* ifdef HAVE_GNUTLS */
1322 } /* eet_pbkdf2_sha1 */
1324 #endif /* ifdef HAVE_CIPHER */