2009-07-20 21 views
16

का उपयोग कर टेक्स्ट स्ट्रिंग को एन्क्रिप्ट/डिक्रिप्ट करना मैं टेक्स्ट स्ट्रिंग को एन्क्रिप्ट या डिक्रिप्ट करने के लिए ओपनएसएसएल के ईसीसी समर्थन का उपयोग कैसे कर सकता हूं? मैं ओपनएसएसएल एपीआई का उपयोग कर ईसीसी निजी/सार्वजनिक कुंजी उत्पन्न करने में सक्षम हूं, लेकिन मुझे नहीं पता कि उन चाबियों का उपयोग करके सादे पाठ को कैसे एन्क्रिप्ट करना है।ओपनएसएसएल ईसीसी

उत्तर

9

ईसीसी स्वयं एन्क्रिप्शन/डिक्रिप्शन ऑपरेशंस को वास्तव में परिभाषित नहीं करता है - अंडाकार घटता पर निर्मित एल्गोरिदम।

एक उदाहरण एल्लिप्टिक-वक्र डिफी-हेलमैन है। आप ECDH का उपयोग करके एक संदेश एन्क्रिप्ट कर सकते हैं:

  1. एक अल्पकालिक ईसी कुंजी उत्पन्न करना।
  2. उस कुंजी और प्राप्तकर्ता की सार्वजनिक कुंजी का उपयोग करके, ईसीडीएच का उपयोग करके एक रहस्य उत्पन्न करें।
  3. एईएस की तरह एक सममित सिफर के साथ संदेश एन्क्रिप्ट करने के लिए उस कुंजी का उपयोग करें।

    1. लोड संदेश से अल्पकालिक सार्वजनिक कुंजी:
    2. एन्क्रिप्टेड संदेश और अल्पकालिक सार्वजनिक महत्वपूर्ण कदम 1.

    में उत्पन्न डिक्रिप्ट करने के लिए संचारित।

  4. ईसीडीएच का उपयोग करके एक गुप्त उत्पन्न करने के लिए अपनी प्राप्तकर्ता कुंजी के साथ उस सार्वजनिक कुंजी का उपयोग करें।
  5. सममित सिफर के साथ संदेश को डिक्रिप्ट करने के लिए उस कुंजी का उपयोग कुंजी के रूप में करें।

संपादित करें: ईसीडीएच का उपयोग करके एक रहस्य उत्पन्न करने का मूल विचार निम्नलिखित है। सबसे पहले हमें एक प्रमुख व्युत्पन्न समारोह को परिभाषित करने की आवश्यकता है - यह SHA1 हैश का उपयोग करता है।

void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen) 
{ 
    if (*outlen < SHA_DIGEST_LENGTH) 
     return NULL; 
    else 
     *outlen = SHA_DIGEST_LENGTH; 
    return SHA1(in, inlen, out); 
} 

यह प्रेषक पक्ष के लिए ईसीडीएच कोड है। यह मानता है कि प्राप्तकर्ता की सार्वजनिक कुंजी पहले से ही "recip_key" में है, और आपने इसे EC_KEY_check_key() से सत्यापित कर लिया है। यह ब्रेवटी के लिए बहुत महत्वपूर्ण त्रुटि जांच को भी छोड़ देता है, जिसे आप निश्चित रूप से उत्पादन कोड में शामिल करना चाहते हैं।

EC_KEY *ephemeral_key = NULL; 
const EC_GROUP *group = NULL; 
unsigned char buf[SHA_DIGEST_LENGTH] = { 0 }; 

group = EC_KEY_get0_group(recip_key); 
ephemeral_key = EC_KEY_new(); 
EC_KEY_set_group(ephemeral_key, group); 

EC_KEY_generate_key(ephemeral_key); 
ECDH_compute_key(buf, sizeof buf, EC_KEY_get0_public_key(recip_key), ephemeral_key, KDF1_SHA1); 

इसके बाद बफर 'buf' में 20 बाइट सामग्री शामिल है जिसका उपयोग आप कुंजी के लिए कर सकते हैं। यह संक्षिप्त उदाहरण openssl स्रोत वितरण में "ecdhtest.c" में कोड पर आधारित है - मेरा सुझाव है कि इसे देखें।

आप एन्क्रिप्टेड संदेश के साथ ephemeral_key का सार्वजनिक कुंजी हिस्सा भेजना चाहते हैं, और सुरक्षित रूप से निजी कुंजी भाग को त्यागें। डेटा पर एक मैक भी एक अच्छा विचार है, और यदि आपको कुंजीिंग सामग्री के 20 से अधिक बाइट्स की आवश्यकता है तो शायद लंबे हैश शायद क्रम में है।

प्राप्तकर्ता कुछ ऐसा ही करता है, सिवाय इसके कि इसकी निजी कुंजी पहले से मौजूद है (क्योंकि प्रेषक को पहले से संबंधित सार्वजनिक कुंजी जाननी थी), और सार्वजनिक कुंजी प्रेषक से प्राप्त की जाती है।

+0

कैफ, उत्तर के लिए धन्यवाद। मैं ऊपर बताए गए कदम के साथ ठीक हूं। मैं ईसीईएच का उपयोग कर उत्पन्न एक रहस्य का उपयोग कर संदेश एन्क्रिप्ट करने के लिए एईएस का उपयोग कर सकता हूं। क्या कोई नमूना प्रोग्राम है जो उपरोक्त चरणों को करता है? यदि ऐसा है तो कृपया मुझे उस पर इंगित करें। मैंने इस तरह के नमूना कार्यक्रम की खोज करने की कोशिश की, लेकिन बिना किसी किस्मत के। धन्यवाद –

+0

क्या आप प्रेषक/रिसीवर (ओपनएसएसएल संभवतः उपयोग करके) के बीच रहस्य उत्पन्न करने के बारे में कुछ और संदर्भों की ओर इशारा कर सकते हैं। धन्यवाद। – ALOToverflow

16

उदाहरणों को खोजने में इतनी मुश्किल है कि डेटा को एन्क्रिप्ट करने के लिए ईसीसी का उपयोग कैसे किया जाए, मैंने सोचा कि मैं दूसरों के उपयोग के लिए कुछ कोड पोस्ट करूंगा।पूरी सूची के लिए, मेरे openssl-देव पोस्टिंग की जाँच:

http://www.mail-archive.com/[email protected]/msg28042.html

असल में इसकी एक बाहर प्लावित कैसे ECDH उपयोग करने के लिए की प्रयोग करने योग्य संस्करण डेटा का एक ब्लॉक सुरक्षित करने के लिए। एक साझा रहस्य उत्पन्न करने के लिए ईसीडीएच का उपयोग किया जाता है। साझा रहस्य को एसएचए 512 का उपयोग करके धोया जाता है। परिणामी 512 बिट्स विभाजित हो जाते हैं, जिसमें 256 सममित सिफर (मेरे उदाहरण में एईएस 256) की कुंजी और अन्य 256 बिट्स एचएमएसी के लिए कुंजी के रूप में उपयोग की जाती हैं। मेरा कार्यान्वयन एसईसीजी कार्यकारी समूह द्वारा उल्लिखित ईसीआईएस मानक पर आधारित है।

प्रमुख कार्य ecies_encrypt() जो हेक्स रूप में सार्वजनिक कुंजी स्वीकार करता है और एन्क्रिप्टेड डेटा देता हैं:

secure_t * ecies_encrypt(char *key, unsigned char *data, size_t length) { 

void *body; 
HMAC_CTX hmac; 
int body_length; 
secure_t *cryptex; 
EVP_CIPHER_CTX cipher; 
unsigned int mac_length; 
EC_KEY *user, *ephemeral; 
size_t envelope_length, block_length, key_length; 
unsigned char envelope_key[SHA512_DIGEST_LENGTH], iv[EVP_MAX_IV_LENGTH], block[EVP_MAX_BLOCK_LENGTH]; 

// Simple sanity check. 
if (!key || !data || !length) { 
    printf("Invalid parameters passed in.\n"); 
    return NULL; 
} 

// Make sure we are generating enough key material for the symmetric ciphers. 
if ((key_length = EVP_CIPHER_key_length(ECIES_CIPHER)) * 2 > SHA512_DIGEST_LENGTH) { 
    printf("The key derivation method will not produce enough envelope key material for the chosen ciphers. {envelope = %i/required = %zu}", SHA512_DIGEST_LENGTH/8, 
      (key_length * 2)/8); 
    return NULL; 
} 

// Convert the user's public key from hex into a full EC_KEY structure. 
if (!(user = ecies_key_create_public_hex(key))) { 
    printf("Invalid public key provided.\n"); 
    return NULL; 
} 

// Create the ephemeral key used specifically for this block of data. 
else if (!(ephemeral = ecies_key_create())) { 
    printf("An error occurred while trying to generate the ephemeral key.\n"); 
    EC_KEY_free(user); 
    return NULL; 
} 

// Use the intersection of the provided keys to generate the envelope data used by the ciphers below. The ecies_key_derivation() function uses 
// SHA 512 to ensure we have a sufficient amount of envelope key material and that the material created is sufficiently secure. 
else if (ECDH_compute_key(envelope_key, SHA512_DIGEST_LENGTH, EC_KEY_get0_public_key(user), ephemeral, ecies_key_derivation) != SHA512_DIGEST_LENGTH) { 
    printf("An error occurred while trying to compute the envelope key. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    EC_KEY_free(ephemeral); 
    EC_KEY_free(user); 
    return NULL; 
} 

// Determine the envelope and block lengths so we can allocate a buffer for the result. 
else if ((block_length = EVP_CIPHER_block_size(ECIES_CIPHER)) == 0 || block_length > EVP_MAX_BLOCK_LENGTH || (envelope_length = EC_POINT_point2oct(EC_KEY_get0_group(
     ephemeral), EC_KEY_get0_public_key(ephemeral), POINT_CONVERSION_COMPRESSED, NULL, 0, NULL)) == 0) { 
    printf("Invalid block or envelope length. {block = %zu/envelope = %zu}\n", block_length, envelope_length); 
    EC_KEY_free(ephemeral); 
    EC_KEY_free(user); 
    return NULL; 
} 

// We use a conditional to pad the length if the input buffer is not evenly divisible by the block size. 
else if (!(cryptex = secure_alloc(envelope_length, EVP_MD_size(ECIES_HASHER), length, length + (length % block_length ? (block_length - (length % block_length)) : 0)))) { 
    printf("Unable to allocate a secure_t buffer to hold the encrypted result.\n"); 
    EC_KEY_free(ephemeral); 
    EC_KEY_free(user); 
    return NULL; 
} 

// Store the public key portion of the ephemeral key. 
else if (EC_POINT_point2oct(EC_KEY_get0_group(ephemeral), EC_KEY_get0_public_key(ephemeral), POINT_CONVERSION_COMPRESSED, secure_key_data(cryptex), envelope_length, 
     NULL) != envelope_length) { 
    printf("An error occurred while trying to record the public portion of the envelope key. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    EC_KEY_free(ephemeral); 
    EC_KEY_free(user); 
    secure_free(cryptex); 
    return NULL; 
} 

// The envelope key has been stored so we no longer need to keep the keys around. 
EC_KEY_free(ephemeral); 
EC_KEY_free(user); 

// For now we use an empty initialization vector. 
memset(iv, 0, EVP_MAX_IV_LENGTH); 

// Setup the cipher context, the body length, and store a pointer to the body buffer location. 
EVP_CIPHER_CTX_init(&cipher); 
body = secure_body_data(cryptex); 
body_length = secure_body_length(cryptex); 

// Initialize the cipher with the envelope key. 
if (EVP_EncryptInit_ex(&cipher, ECIES_CIPHER, NULL, envelope_key, iv) != 1 || EVP_CIPHER_CTX_set_padding(&cipher, 0) != 1 || EVP_EncryptUpdate(&cipher, body, 
     &body_length, data, length - (length % block_length)) != 1) { 
    printf("An error occurred while trying to secure the data using the chosen symmetric cipher. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    EVP_CIPHER_CTX_cleanup(&cipher); 
    secure_free(cryptex); 
    return NULL; 
} 

// Check whether all of the data was encrypted. If they don't match up, we either have a partial block remaining, or an error occurred. 
else if (body_length != length) { 

    // Make sure all that remains is a partial block, and their wasn't an error. 
    if (length - body_length >= block_length) { 
     printf("Unable to secure the data using the chosen symmetric cipher. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
     EVP_CIPHER_CTX_cleanup(&cipher); 
     secure_free(cryptex); 
     return NULL; 
    } 

    // Copy the remaining data into our partial block buffer. The memset() call ensures any extra bytes will be zero'ed out. 
    memset(block, 0, EVP_MAX_BLOCK_LENGTH); 
    memcpy(block, data + body_length, length - body_length); 

    // Advance the body pointer to the location of the remaining space, and calculate just how much room is still available. 
    body += body_length; 
    if ((body_length = secure_body_length(cryptex) - body_length) < 0) { 
     printf("The symmetric cipher overflowed!\n"); 
     EVP_CIPHER_CTX_cleanup(&cipher); 
     secure_free(cryptex); 
     return NULL; 
    } 

    // Pass the final partially filled data block into the cipher as a complete block. The padding will be removed during the decryption process. 
    else if (EVP_EncryptUpdate(&cipher, body, &body_length, block, block_length) != 1) { 
     printf("Unable to secure the data using the chosen symmetric cipher. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
     EVP_CIPHER_CTX_cleanup(&cipher); 
     secure_free(cryptex); 
     return NULL; 
    } 
} 

// Advance the pointer, then use pointer arithmetic to calculate how much of the body buffer has been used. The complex logic is needed so that we get 
// the correct status regardless of whether there was a partial data block. 
body += body_length; 
if ((body_length = secure_body_length(cryptex) - (body - secure_body_data(cryptex))) < 0) { 
    printf("The symmetric cipher overflowed!\n"); 
    EVP_CIPHER_CTX_cleanup(&cipher); 
    secure_free(cryptex); 
    return NULL; 
} 

else if (EVP_EncryptFinal_ex(&cipher, body, &body_length) != 1) { 
    printf("Unable to secure the data using the chosen symmetric cipher. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    EVP_CIPHER_CTX_cleanup(&cipher); 
    secure_free(cryptex); 
    return NULL; 
} 

EVP_CIPHER_CTX_cleanup(&cipher); 

// Generate an authenticated hash which can be used to validate the data during decryption. 
HMAC_CTX_init(&hmac); 
mac_length = secure_mac_length(cryptex); 

// At the moment we are generating the hash using encrypted data. At some point we may want to validate the original text instead. 
if (HMAC_Init_ex(&hmac, envelope_key + key_length, key_length, ECIES_HASHER, NULL) != 1 || HMAC_Update(&hmac, secure_body_data(cryptex), secure_body_length(cryptex)) 
     != 1 || HMAC_Final(&hmac, secure_mac_data(cryptex), &mac_length) != 1) { 
    printf("Unable to generate a data authentication code. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    HMAC_CTX_cleanup(&hmac); 
    secure_free(cryptex); 
    return NULL; 
} 

HMAC_CTX_cleanup(&hmac); 

return cryptex; 
} 

और ecies_decrypt() जो हेक्स रूप में फिर से निजी कुंजी लेता है, और decrypts पहले से सुरक्षित बफर:

unsigned char * ecies_decrypt(char *key, secure_t *cryptex, size_t *length) { 

HMAC_CTX hmac; 
size_t key_length; 
int output_length; 
EVP_CIPHER_CTX cipher; 
EC_KEY *user, *ephemeral; 
unsigned int mac_length = EVP_MAX_MD_SIZE; 
unsigned char envelope_key[SHA512_DIGEST_LENGTH], iv[EVP_MAX_IV_LENGTH], md[EVP_MAX_MD_SIZE], *block, *output; 

// Simple sanity check. 
if (!key || !cryptex || !length) { 
    printf("Invalid parameters passed in.\n"); 
    return NULL; 
} 

// Make sure we are generating enough key material for the symmetric ciphers. 
else if ((key_length = EVP_CIPHER_key_length(ECIES_CIPHER)) * 2 > SHA512_DIGEST_LENGTH) { 
    printf("The key derivation method will not produce enough envelope key material for the chosen ciphers. {envelope = %i/required = %zu}", SHA512_DIGEST_LENGTH/8, 
      (key_length * 2)/8); 
    return NULL; 
} 

// Convert the user's public key from hex into a full EC_KEY structure. 
else if (!(user = ecies_key_create_private_hex(key))) { 
    printf("Invalid private key provided.\n"); 
    return NULL; 
} 

// Create the ephemeral key used specifically for this block of data. 
else if (!(ephemeral = ecies_key_create_public_octets(secure_key_data(cryptex), secure_key_length(cryptex)))) { 
    printf("An error occurred while trying to recreate the ephemeral key.\n"); 
    EC_KEY_free(user); 
    return NULL; 
} 

// Use the intersection of the provided keys to generate the envelope data used by the ciphers below. The ecies_key_derivation() function uses 
// SHA 512 to ensure we have a sufficient amount of envelope key material and that the material created is sufficiently secure. 
else if (ECDH_compute_key(envelope_key, SHA512_DIGEST_LENGTH, EC_KEY_get0_public_key(ephemeral), user, ecies_key_derivation) != SHA512_DIGEST_LENGTH) { 
    printf("An error occurred while trying to compute the envelope key. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    EC_KEY_free(ephemeral); 
    EC_KEY_free(user); 
    return NULL; 
} 

// The envelope key material has been extracted, so we no longer need the user and ephemeral keys. 
EC_KEY_free(ephemeral); 
EC_KEY_free(user); 

// Use the authenticated hash of the ciphered data to ensure it was not modified after being encrypted. 
HMAC_CTX_init(&hmac); 

// At the moment we are generating the hash using encrypted data. At some point we may want to validate the original text instead. 
if (HMAC_Init_ex(&hmac, envelope_key + key_length, key_length, ECIES_HASHER, NULL) != 1 || HMAC_Update(&hmac, secure_body_data(cryptex), secure_body_length(cryptex)) 
     != 1 || HMAC_Final(&hmac, md, &mac_length) != 1) { 
    printf("Unable to generate the authentication code needed for validation. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    HMAC_CTX_cleanup(&hmac); 
    return NULL; 
} 

HMAC_CTX_cleanup(&hmac); 

// We can use the generated hash to ensure the encrypted data was not altered after being encrypted. 
if (mac_length != secure_mac_length(cryptex) || memcmp(md, secure_mac_data(cryptex), mac_length)) { 
    printf("The authentication code was invalid! The ciphered data has been corrupted!\n"); 
    return NULL; 
} 

// Create a buffer to hold the result. 
output_length = secure_body_length(cryptex); 
if (!(block = output = malloc(output_length + 1))) { 
    printf("An error occurred while trying to allocate memory for the decrypted data.\n"); 
    return NULL; 
} 

// For now we use an empty initialization vector. We also clear out the result buffer just to be on the safe side. 
memset(iv, 0, EVP_MAX_IV_LENGTH); 
memset(output, 0, output_length + 1); 

EVP_CIPHER_CTX_init(&cipher); 

// Decrypt the data using the chosen symmetric cipher. 
if (EVP_DecryptInit_ex(&cipher, ECIES_CIPHER, NULL, envelope_key, iv) != 1 || EVP_CIPHER_CTX_set_padding(&cipher, 0) != 1 || EVP_DecryptUpdate(&cipher, block, 
     &output_length, secure_body_data(cryptex), secure_body_length(cryptex)) != 1) { 
    printf("Unable to decrypt the data using the chosen symmetric cipher. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    EVP_CIPHER_CTX_cleanup(&cipher); 
    free(output); 
    return NULL; 
} 

block += output_length; 
if ((output_length = secure_body_length(cryptex) - output_length) != 0) { 
    printf("The symmetric cipher failed to properly decrypt the correct amount of data!\n"); 
    EVP_CIPHER_CTX_cleanup(&cipher); 
    free(output); 
    return NULL; 
} 

if (EVP_DecryptFinal_ex(&cipher, block, &output_length) != 1) { 
    printf("Unable to decrypt the data using the chosen symmetric cipher. {error = %s}\n", ERR_error_string(ERR_get_error(), NULL)); 
    EVP_CIPHER_CTX_cleanup(&cipher); 
    free(output); 
    return NULL; 
} 

EVP_CIPHER_CTX_cleanup(&cipher); 

*length = secure_orig_length(cryptex); 
return output; 
} 

मैं इस पोस्ट कर रहा हूँ क्योंकि मैं व्यक्तिगत रूप से कैसे ईसीसी और OpenSSL पुस्तकालय का उपयोग करके फ़ाइलें सुरक्षित करने के लिए किसी भी अन्य उदाहरण नहीं मिल सका। उन्होंने कहा कि ओपनएसएसएल का उपयोग न करने वाले विकल्पों का उल्लेख करने लायक है। एक है जो मेरे उदाहरण के समान पैटर्न का पालन करता है, केवल यह libgcrypt पर निर्भर करता है। चूंकि libgcrypt आवश्यक सभी अंतर्निहित ईसीसी कार्यों को प्रदान नहीं करता है, इसलिए सुरक्षित कार्यक्रम अंतराल में भर जाता है और libgcrypt से अनुपलब्ध ईसीसी तर्क लागू करता है।

एसकेएस देखने के लायक एक और कार्यक्रम है जो उपरोक्त उदाहरण के रूप में एक समान ईसीसी आधारित एन्क्रिप्शन प्रक्रिया का उपयोग करता है, लेकिन इसमें कोई बाहरी निर्भरता नहीं है (इसलिए सभी ईसीसी कोड आपके लिए देखने के लिए सही हैं)।

+0

यह किस भाषा में लिखा गया है? – starbeamrainbowlabs

संबंधित मुद्दे