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 /* ifdef HAVE_UNISTD_H */
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 */
283 certificate_file = NULL;
284 private_key_file = NULL;
286 #endif /* ifdef HAVE_SIGNATURE */
288 } /* eet_identity_open */
291 eet_identity_close(Eet_Key *key)
293 #ifdef HAVE_SIGNATURE
294 if (!key || (key->references > 0))
298 gnutls_x509_crt_deinit(key->certificate);
299 gnutls_x509_privkey_deinit(key->private_key);
300 # else /* ifdef HAVE_GNUTLS */
301 X509_free(key->certificate);
302 EVP_PKEY_free(key->private_key);
303 # endif /* ifdef HAVE_GNUTLS */
307 #endif /* ifdef HAVE_SIGNATURE */
308 } /* eet_identity_close */
311 eet_identity_print(Eet_Key *key,
314 #ifdef HAVE_SIGNATURE
316 const char *names[6] = {
325 gnutls_datum_t data = { NULL, 0 };
326 gnutls_datum_t rsa_raw[6];
335 if (key->private_key)
337 if (gnutls_x509_privkey_export_rsa_raw(key->private_key,
338 rsa_raw + 0, /* Modulus */
339 rsa_raw + 1, /* Public exponent */
340 rsa_raw + 2, /* Private exponent */
341 rsa_raw + 3, /* First prime */
342 rsa_raw + 4, /* Second prime */
343 rsa_raw + 5)) /* Coefficient */
346 if (!(res = malloc(size)))
349 fprintf(out, "Private Key:\n");
352 for (i = 0; i < 6; i++)
354 while ((err = gnutls_hex_encode(rsa_raw + i, res, &size)) ==
355 GNUTLS_E_SHORT_MEMORY_BUFFER)
358 if (!(res = realloc(res, size)))
364 fprintf(out, "\t%s:\n", names[i]);
365 for (j = 0; strlen(res) > j; j += 32)
367 snprintf(buf, 32, "%s", res + j);
368 fprintf(out, "\t\t%s\n", buf);
375 if (key->certificate)
377 fprintf(out, "Public certificate:\n");
378 if (gnutls_x509_crt_print(key->certificate, GNUTLS_X509_CRT_FULL,
382 fprintf(out, "%s\n", data.data);
383 gnutls_free(data.data);
392 gnutls_free(data.data);
395 # else /* ifdef HAVE_GNUTLS */
403 rsa = EVP_PKEY_get1_RSA(key->private_key);
406 fprintf(out, "Private key (RSA):\n");
407 RSA_print_fp(out, rsa, 0);
410 dsa = EVP_PKEY_get1_DSA(key->private_key);
413 fprintf(out, "Private key (DSA):\n");
414 DSA_print_fp(out, dsa, 0);
417 dh = EVP_PKEY_get1_DH(key->private_key);
420 fprintf(out, "Private key (DH):\n");
421 DHparams_print_fp(out, dh);
424 fprintf(out, "Public certificate:\n");
425 X509_print_fp(out, key->certificate);
426 # endif /* ifdef HAVE_GNUTLS */
427 #else /* ifdef HAVE_SIGNATURE */
430 ERR("You need to compile signature support in EET.");
431 #endif /* ifdef HAVE_SIGNATURE */
432 } /* eet_identity_print */
435 eet_identity_ref(Eet_Key *key)
441 } /* eet_identity_ref */
444 eet_identity_unref(Eet_Key *key)
450 eet_identity_close(key);
451 } /* eet_identity_unref */
454 eet_identity_compute_sha1(const void *data_base,
455 unsigned int data_length,
460 #ifdef HAVE_SIGNATURE
462 result = malloc(gcry_md_get_algo_dlen(GCRY_MD_SHA1));
466 gcry_md_hash_buffer(GCRY_MD_SHA1, result, data_base, data_length);
468 *sha1_length = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
470 # else /* ifdef HAVE_GNUTLS */
472 result = malloc(SHA_DIGEST_LENGTH);
476 SHA1(data_base, data_length, result);
478 *sha1_length = SHA_DIGEST_LENGTH;
480 # else /* ifdef HAVE_OPENSSL */
482 # endif /* ifdef HAVE_OPENSSL */
483 # endif /* ifdef HAVE_GNUTLS */
484 #else /* ifdef HAVE_SIGNATURE */
489 #endif /* ifdef HAVE_SIGNATURE */
492 } /* eet_identity_compute_sha1 */
495 eet_identity_sign(FILE *fp,
498 #ifdef HAVE_SIGNATURE
499 Eet_Error err = EET_ERROR_NONE;
504 unsigned char *sign = NULL;
505 unsigned char *cert = NULL;
507 gnutls_datum_t datum = { NULL, 0 };
510 # else /* ifdef HAVE_GNUTLS */
512 unsigned int sign_len = 0;
514 # endif /* ifdef HAVE_GNUTLS */
516 /* A few check and flush pending write. */
517 if (!fp || !key || !key->certificate || !key->private_key)
518 return EET_ERROR_BAD_OBJECT;
520 /* Get the file size. */
523 return EET_ERROR_BAD_OBJECT;
525 if (fstat(fd, &st_buf) < 0)
526 return EET_ERROR_MMAP_FAILED;
528 /* Map the file in memory. */
529 data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
530 if (data == MAP_FAILED)
531 return EET_ERROR_MMAP_FAILED;
535 datum.size = st_buf.st_size;
537 /* Get the signature length */
538 if (gnutls_x509_privkey_sign_data(key->private_key, GNUTLS_DIG_SHA1, 0,
539 &datum, sign, &sign_len) &&
542 err = EET_ERROR_SIGNATURE_FAILED;
546 /* Get the signature */
547 sign = malloc(sign_len);
549 gnutls_x509_privkey_sign_data(key->private_key, GNUTLS_DIG_SHA1, 0,
554 err = EET_ERROR_OUT_OF_MEMORY;
556 err = EET_ERROR_SIGNATURE_FAILED;
561 /* Get the certificate length */
562 if (gnutls_x509_crt_export(key->certificate, GNUTLS_X509_FMT_DER, cert,
566 err = EET_ERROR_SIGNATURE_FAILED;
570 /* Get the certificate */
571 cert = malloc(cert_len);
573 gnutls_x509_crt_export(key->certificate, GNUTLS_X509_FMT_DER, cert,
577 err = EET_ERROR_OUT_OF_MEMORY;
579 err = EET_ERROR_SIGNATURE_FAILED;
584 # else /* ifdef HAVE_GNUTLS */
585 sign_len = EVP_PKEY_size(key->private_key);
586 sign = malloc(sign_len);
589 err = EET_ERROR_OUT_OF_MEMORY;
593 /* Do the signature. */
594 EVP_SignInit(&md_ctx, EVP_sha1());
595 EVP_SignUpdate(&md_ctx, data, st_buf.st_size);
596 err = EVP_SignFinal(&md_ctx,
598 (unsigned int *)&sign_len,
602 ERR_print_errors_fp(stdout);
603 err = EET_ERROR_SIGNATURE_FAILED;
607 /* Give me the der (binary form for X509). */
608 cert_len = i2d_X509(key->certificate, &cert);
611 ERR_print_errors_fp(stdout);
612 err = EET_ERROR_X509_ENCODING_FAILED;
616 # endif /* ifdef HAVE_GNUTLS */
617 /* Append the signature at the end of the file. */
618 head[0] = (int)htonl ((unsigned int)EET_MAGIC_SIGN);
619 head[1] = (int)htonl ((unsigned int)sign_len);
620 head[2] = (int)htonl ((unsigned int)cert_len);
622 if (fwrite(head, sizeof(head), 1, fp) != 1)
624 err = EET_ERROR_WRITE_ERROR;
628 if (fwrite(sign, sign_len, 1, fp) != 1)
630 err = EET_ERROR_WRITE_ERROR;
634 if (fwrite(cert, cert_len, 1, fp) != 1)
636 err = EET_ERROR_WRITE_ERROR;
645 # else /* ifdef HAVE_GNUTLS */
649 # endif /* ifdef HAVE_GNUTLS */
653 munmap(data, st_buf.st_size);
655 #else /* ifdef HAVE_SIGNATURE */
658 return EET_ERROR_NOT_IMPLEMENTED;
659 #endif /* ifdef HAVE_SIGNATURE */
660 } /* eet_identity_sign */
663 eet_identity_check(const void *data_base,
664 unsigned int data_length,
667 const void *signature_base,
668 unsigned int signature_length,
669 const void **raw_signature_base,
670 unsigned int *raw_signature_length,
673 #ifdef HAVE_SIGNATURE
674 const int *header = signature_base;
675 const unsigned char *sign;
676 const unsigned char *cert_der;
681 /* At least the header size */
682 if (signature_length < sizeof(int) * 3)
686 magic = ntohl(header[0]);
687 sign_len = ntohl(header[1]);
688 cert_len = ntohl(header[2]);
690 /* Verify the header */
691 if (magic != EET_MAGIC_SIGN)
694 if (sign_len + cert_len + sizeof(int) * 3 > signature_length)
697 /* Update the signature and certificate pointer */
698 sign = (unsigned char *)signature_base + sizeof(int) * 3;
699 cert_der = sign + sign_len;
702 gnutls_x509_crt_t cert;
703 gnutls_datum_t datum;
704 gnutls_datum_t signature;
705 # if EET_USE_NEW_GNUTLS_API
709 # endif /* if EET_USE_NEW_GNUTLS_API */
711 /* Create an understanding certificate structure for gnutls */
712 datum.data = (void *)cert_der;
713 datum.size = cert_len;
714 gnutls_x509_crt_init(&cert);
715 gnutls_x509_crt_import(cert, &datum, GNUTLS_X509_FMT_DER);
717 signature.data = (void *)sign;
718 signature.size = sign_len;
720 /* Verify the signature */
721 # if EET_USE_NEW_GNUTLS_API
723 I am waiting for my patch being accepted in GnuTLS release.
724 But we now have a way to prevent double computation of SHA1.
726 err = gcry_md_open (&md, GCRY_MD_SHA1, 0);
730 gcry_md_write(md, data_base, data_length);
732 hash = gcry_md_read(md, GCRY_MD_SHA1);
739 datum.size = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
742 if (!gnutls_x509_crt_verify_hash(cert, 0, &datum, &signature))
750 *sha1 = malloc(datum.size);
757 memcpy(*sha1, hash, datum.size);
758 *sha1_length = datum.size;
762 # else /* if EET_USE_NEW_GNUTLS_API */
763 datum.data = (void *)data_base;
764 datum.size = data_length;
766 if (!gnutls_x509_crt_verify_data(cert, 0, &datum, &signature))
775 # endif /* if EET_USE_NEW_GNUTLS_API */
776 gnutls_x509_crt_deinit(cert);
778 # else /* ifdef HAVE_GNUTLS */
779 const unsigned char *tmp;
785 /* Strange but d2i_X509 seems to put 0 all over the place. */
786 tmp = alloca(cert_len);
787 memcpy((char *)tmp, cert_der, cert_len);
788 x509 = d2i_X509(NULL, &tmp, cert_len);
792 /* Get public key - eay */
793 pkey = X509_get_pubkey(x509);
800 /* Verify the signature */
801 EVP_VerifyInit(&md_ctx, EVP_sha1());
802 EVP_VerifyUpdate(&md_ctx, data_base, data_length);
803 err = EVP_VerifyFinal(&md_ctx, sign, sign_len, pkey);
817 # endif /* ifdef HAVE_GNUTLS */
819 *x509_length = cert_len;
821 if (raw_signature_base)
822 *raw_signature_base = sign;
824 if (raw_signature_length)
825 *raw_signature_length = sign_len;
828 #else /* ifdef HAVE_SIGNATURE */
833 signature_base = NULL;
834 signature_length = 0;
835 raw_signature_base = NULL;
836 raw_signature_length = NULL;
839 #endif /* ifdef HAVE_SIGNATURE */
840 } /* eet_identity_check */
843 eet_identity_certificate_print(const unsigned char *certificate,
847 #ifdef HAVE_SIGNATURE
848 if (!certificate || !out || der_length <= 0)
850 ERR("No certificate provided.");
855 gnutls_datum_t datum;
856 gnutls_x509_crt_t cert;
858 /* Create an understanding certificate structure for gnutls */
859 datum.data = (void *)certificate;
860 datum.size = der_length;
861 if (gnutls_x509_crt_init(&cert))
864 if (gnutls_x509_crt_import(cert, &datum, GNUTLS_X509_FMT_DER))
867 /* Pretty print the certificate */
870 if (gnutls_x509_crt_print(cert, GNUTLS_X509_CRT_FULL, &datum))
873 INF("Public certificate :");
874 INF("%s", datum.data);
878 gnutls_free(datum.data);
880 gnutls_x509_crt_deinit(cert);
881 # else /* ifdef HAVE_GNUTLS */
882 const unsigned char *tmp;
885 /* Strange but d2i_X509 seems to put 0 all over the place. */
886 tmp = alloca(der_length);
887 memcpy((char *)tmp, certificate, der_length);
888 x509 = d2i_X509(NULL, &tmp, der_length);
891 INF("Not a valid certificate.");
895 INF("Public certificate :");
896 X509_print_fp(out, x509);
899 # endif /* ifdef HAVE_GNUTLS */
900 #else /* ifdef HAVE_SIGNATURE */
904 ERR("You need to compile signature support in EET.");
905 #endif /* ifdef HAVE_SIGNATURE */
906 } /* eet_identity_certificate_print */
909 eet_cipher(const void *data,
914 unsigned int *result_length)
917 /* Cipher declarations */
918 unsigned int *ret = NULL;
919 unsigned char iv[MAX_IV_LEN];
920 unsigned char ik[MAX_KEY_LEN];
921 unsigned char key_material[MAX_IV_LEN + MAX_KEY_LEN];
923 unsigned int tmp = 0;
927 /* Gcrypt declarations */
928 gcry_error_t err = 0;
929 gcry_cipher_hd_t cipher;
930 # else /* ifdef HAVE_GNUTLS */
931 /* Openssl declarations*/
933 unsigned int *buffer;
935 # endif /* ifdef HAVE_GNUTLS */
938 /* Gcrypt salt generation */
939 gcry_create_nonce((unsigned char *)&salt, sizeof(salt));
940 # else /* ifdef HAVE_GNUTLS */
941 /* Openssl salt generation */
942 if (!RAND_bytes((unsigned char *)&salt, sizeof (unsigned int)))
943 return EET_ERROR_PRNG_NOT_SEEDED;
945 # endif /* ifdef HAVE_GNUTLS */
949 (unsigned char *)&salt,
950 sizeof(unsigned int),
953 MAX_KEY_LEN + MAX_IV_LEN);
955 memcpy(iv, key_material, MAX_IV_LEN);
956 memcpy(ik, key_material + MAX_IV_LEN, MAX_KEY_LEN);
958 memset(key_material, 0, sizeof (key_material));
960 crypted_length = ((((size + sizeof (unsigned int)) >> 5) + 1) << 5);
961 ret = malloc(crypted_length + sizeof(unsigned int));
964 memset(iv, 0, sizeof (iv));
965 memset(ik, 0, sizeof (ik));
966 memset(&salt, 0, sizeof (salt));
967 return EET_ERROR_OUT_OF_MEMORY;
971 memset(&salt, 0, sizeof (salt));
976 memcpy(ret + 2, data, size);
978 /* Gcrypt create the corresponding cipher
979 AES with a 256 bit key, Cipher Block Chaining mode */
980 err = gcry_cipher_open(&cipher, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 0);
985 err = gcry_cipher_setiv(cipher, iv, MAX_IV_LEN);
989 err = gcry_cipher_setkey(cipher, ik, MAX_KEY_LEN);
993 memset(iv, 0, sizeof (iv));
994 memset(ik, 0, sizeof (ik));
997 err = gcry_cipher_encrypt(cipher,
998 (unsigned char *)(ret + 1),
1005 /* Gcrypt close the cipher */
1006 gcry_cipher_close(cipher);
1007 # else /* ifdef HAVE_GNUTLS */
1008 buffer = alloca(crypted_length);
1011 memcpy(buffer + 1, data, size);
1013 /* Openssl create the corresponding cipher
1014 AES with a 256 bit key, Cipher Block Chaining mode */
1015 EVP_CIPHER_CTX_init(&ctx);
1016 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, ik, iv))
1021 memset(iv, 0, sizeof (iv));
1022 memset(ik, 0, sizeof (ik));
1024 /* Openssl encrypt */
1025 if (!EVP_EncryptUpdate(&ctx, (unsigned char *)(ret + 1), &tmp_len,
1026 (unsigned char *)buffer,
1027 size + sizeof(unsigned int)))
1030 /* Openssl close the cipher */
1031 if (!EVP_EncryptFinal_ex(&ctx, ((unsigned char *)(ret + 1)) + tmp_len,
1035 EVP_CIPHER_CTX_cleanup(&ctx);
1036 # endif /* ifdef HAVE_GNUTLS */
1038 /* Set return values */
1040 *result_length = crypted_length + sizeof(unsigned int);
1047 return EET_ERROR_NONE;
1050 memset(iv, 0, sizeof (iv));
1051 memset(ik, 0, sizeof (ik));
1056 gcry_cipher_close(cipher);
1058 # else /* ifdef HAVE_GNUTLS */
1061 EVP_CIPHER_CTX_cleanup(&ctx);
1063 # endif /* ifdef HAVE_GNUTLS */
1072 return EET_ERROR_ENCRYPT_FAILED;
1073 #else /* ifdef HAVE_CIPHER */
1074 /* Cipher not supported */
1080 (void)result_length;
1081 return EET_ERROR_NOT_IMPLEMENTED;
1082 #endif /* ifdef HAVE_CIPHER */
1086 eet_decipher(const void *data,
1089 unsigned int length,
1091 unsigned int *result_length)
1094 const unsigned int *over = data;
1095 unsigned int *ret = NULL;
1096 unsigned char ik[MAX_KEY_LEN];
1097 unsigned char iv[MAX_IV_LEN];
1098 unsigned char key_material[MAX_KEY_LEN + MAX_IV_LEN];
1103 /* At least the salt and an AES block */
1104 if (size < sizeof(unsigned int) + 16)
1105 return EET_ERROR_BAD_OBJECT;
1110 /* Generate the iv and the key with the salt */
1111 eet_pbkdf2_sha1(key, length, (unsigned char *)&salt,
1112 sizeof(unsigned int), 2048, key_material,
1113 MAX_KEY_LEN + MAX_IV_LEN);
1115 memcpy(iv, key_material, MAX_IV_LEN);
1116 memcpy(ik, key_material + MAX_IV_LEN, MAX_KEY_LEN);
1118 memset(key_material, 0, sizeof (key_material));
1119 memset(&salt, 0, sizeof (salt));
1121 /* Align to AES block size if size is not align */
1122 tmp_len = size - sizeof (unsigned int);
1123 if ((tmp_len & 0x1F) != 0)
1126 ret = malloc(tmp_len);
1131 gcry_error_t err = 0;
1132 gcry_cipher_hd_t cipher;
1134 /* Gcrypt create the corresponding cipher */
1135 err = gcry_cipher_open(&cipher, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, 0);
1137 return EET_ERROR_DECRYPT_FAILED;
1139 err = gcry_cipher_setiv(cipher, iv, MAX_IV_LEN);
1143 err = gcry_cipher_setkey(cipher, ik, MAX_KEY_LEN);
1147 memset(iv, 0, sizeof (iv));
1148 memset(ik, 0, sizeof (ik));
1150 /* Gcrypt decrypt */
1151 err = gcry_cipher_decrypt(cipher, ret, tmp_len,
1152 ((unsigned int *)data) + 1, tmp_len);
1156 /* Gcrypt close the cipher */
1157 gcry_cipher_close(cipher);
1159 # else /* ifdef HAVE_GNUTLS */
1163 /* Openssl create the corresponding cipher */
1164 EVP_CIPHER_CTX_init(&ctx);
1167 if (!EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, ik, iv))
1170 memset(iv, 0, sizeof (iv));
1171 memset(ik, 0, sizeof (ik));
1173 /* Openssl decrypt */
1174 if (!EVP_DecryptUpdate(&ctx, (unsigned char *)ret, &tmp,
1175 (unsigned char *)(over + 1), tmp_len))
1178 /* Openssl close the cipher*/
1179 EVP_CIPHER_CTX_cleanup(&ctx);
1180 # endif /* ifdef HAVE_GNUTLS */
1181 /* Get the decrypted data size */
1187 /* Update the return values */
1189 *result_length = tmp;
1194 *result = malloc(tmp);
1198 memcpy(*result, ret + 1, tmp);
1203 return EET_ERROR_NONE;
1206 memset(iv, 0, sizeof (iv));
1207 memset(ik, 0, sizeof (ik));
1212 EVP_CIPHER_CTX_cleanup(&ctx);
1214 # endif /* ifdef HAVE_GNUTLS */
1224 return EET_ERROR_DECRYPT_FAILED;
1225 #else /* ifdef HAVE_CIPHER */
1231 (void)result_length;
1232 return EET_ERROR_NOT_IMPLEMENTED;
1233 #endif /* ifdef HAVE_CIPHER */
1234 } /* eet_decipher */
1239 eet_hmac_sha1(const void *key,
1245 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
1247 unsigned char *hash;
1250 err = gcry_md_open(&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
1251 if (err != GPG_ERR_NO_ERROR)
1254 err = gcry_md_setkey(mdh, key, key_len);
1255 if (err != GPG_ERR_NO_ERROR)
1261 gcry_md_write(mdh, data, data_len);
1263 hash = gcry_md_read(mdh, GCRY_MD_SHA1);
1270 memcpy(res, hash, hlen);
1275 } /* eet_hmac_sha1 */
1277 # endif /* ifdef HAVE_GNUTLS */
1280 eet_pbkdf2_sha1(const char *key,
1282 const unsigned char *salt,
1283 unsigned int salt_len,
1288 unsigned char digest[20];
1289 unsigned char tab[4];
1290 unsigned char *p = res;
1293 int digest_len = 20;
1300 # endif /* ifdef HAVE_GNUTLS */
1302 buf = alloca(salt_len + 4);
1306 for (i = 1; len; len -= tmp_len, p += tmp_len, i++)
1308 if (len > digest_len)
1309 tmp_len = digest_len;
1313 tab[0] = (unsigned char)(i & 0xff000000) >> 24;
1314 tab[1] = (unsigned char)(i & 0x00ff0000) >> 16;
1315 tab[2] = (unsigned char)(i & 0x0000ff00) >> 8;
1316 tab[3] = (unsigned char)(i & 0x000000ff) >> 0;
1319 memcpy(buf, salt, salt_len);
1320 memcpy(buf + salt_len, tab, 4);
1321 eet_hmac_sha1(key, key_len, buf, salt_len + 4, digest);
1322 # else /* ifdef HAVE_GNUTLS */
1323 HMAC_Init(&hctx, key, key_len, EVP_sha1());
1324 HMAC_Update(&hctx, salt, salt_len);
1325 HMAC_Update(&hctx, tab, 4);
1326 HMAC_Final(&hctx, digest, NULL);
1327 # endif /* ifdef HAVE_GNUTLS */
1328 memcpy(p, digest, tmp_len);
1330 for (j = 1; j < iter; j++)
1333 eet_hmac_sha1(key, key_len, digest, 20, digest);
1334 # else /* ifdef HAVE_GNUTLS */
1335 HMAC(EVP_sha1(), key, key_len, digest, 20, digest, NULL);
1336 # endif /* ifdef HAVE_GNUTLS */
1337 for (k = 0; k < tmp_len; k++)
1344 HMAC_cleanup(&hctx);
1345 # endif /* ifdef HAVE_GNUTLS */
1347 } /* eet_pbkdf2_sha1 */
1349 #endif /* ifdef HAVE_CIPHER */