ডেটা এনক্রিপশনের বেশিরভাগ ব্যবহারের ক্ষেত্রে আমরা AEAD প্রিমিটিভের সাথে AES128_GCM কী টাইপ ব্যবহার করার সাজেশন দিই।
Authenticated Encryption with Associated Data (AEAD) হল সবচেয়ে সহজ এবং বেশিরভাগ ব্যবহারের ক্ষেত্রে সবচেয়ে উপযুক্ত প্রিমিটিভ। AEAD গোপনীয়তা ও সত্যতা প্রদান করে এবং নিশ্চিত করে যে মেসেজে সবসময় আলাদা সাইফারটেক্সট (এনক্রিপ্ট করা আউটপুট) থাকে, এমনকি প্লেনটেক্সট (এনক্রিপশনের জন্য ইনপুট) একই হলেও। এটি সিমেট্রিক, এনক্রিপশন ও ডিক্রিপশন, দুটির জন্যই একটি 'কী' ব্যবহার করা হয়।
নিম্নলিখিত উদাহরণগুলি আপনাকে AEAD প্রিমিটিভ ব্যবহার শুরু করতে সাহায্য করবে:
C++
// A command-line utility for testing Tink AEAD. #include <iostream> #include <memory> #include <string> #include "absl/flags/flag.h" #include "absl/flags/parse.h" #include "absl/log/absl_check.h" #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "tink/aead.h" #include "tink/aead/config_v0.h" #include "util/util.h" #include "tink/keyset_handle.h" ABSL_FLAG(std::string, keyset_filename, "", "Keyset file in JSON format"); ABSL_FLAG(std::string, mode, "", "Mode of operation {encrypt|decrypt}"); ABSL_FLAG(std::string, input_filename, "", "Filename to operate on"); ABSL_FLAG(std::string, output_filename, "", "Output file name"); ABSL_FLAG(std::string, associated_data, "", "Associated data for AEAD (default: empty"); namespace { using ::crypto::tink::Aead; using ::crypto::tink::KeysetHandle; constexpr absl::string_view kEncrypt = "encrypt"; constexpr absl::string_view kDecrypt = "decrypt"; void ValidateParams() { // ... } } // namespace namespace tink_cc_examples { // AEAD example CLI implementation. absl::Status AeadCli(absl::string_view mode, const std::string& keyset_filename, const std::string& input_filename, const std::string& output_filename, absl::string_view associated_data) { // Read the keyset from file. absl::StatusOr<std::unique_ptr<KeysetHandle>> keyset_handle = ReadJsonCleartextKeyset(keyset_filename); if (!keyset_handle.ok()) return keyset_handle.status(); // Get the primitive. absl::StatusOr<std::unique_ptr<Aead>> aead = (*keyset_handle) ->GetPrimitive<crypto::tink::Aead>(crypto::tink::ConfigAead2026()); if (!aead.ok()) return aead.status(); // Read the input. absl::StatusOr<std::string> input_file_content = ReadFile(input_filename); if (!input_file_content.ok()) return input_file_content.status(); // Compute the output. std::string output; if (mode == kEncrypt) { absl::StatusOr<std::string> encrypt_result = (*aead)->Encrypt(*input_file_content, associated_data); if (!encrypt_result.ok()) return encrypt_result.status(); output = encrypt_result.value(); } else { // operation == kDecrypt. absl::StatusOr<std::string> decrypt_result = (*aead)->Decrypt(*input_file_content, associated_data); if (!decrypt_result.ok()) return decrypt_result.status(); output = decrypt_result.value(); } // Write the output to the output file. return WriteToFile(output, output_filename); } } // namespace tink_cc_examples int main(int argc, char** argv) { absl::ParseCommandLine(argc, argv); ValidateParams(); std::string mode = absl::GetFlag(FLAGS_mode); std::string keyset_filename = absl::GetFlag(FLAGS_keyset_filename); std::string input_filename = absl::GetFlag(FLAGS_input_filename); std::string output_filename = absl::GetFlag(FLAGS_output_filename); std::string associated_data = absl::GetFlag(FLAGS_associated_data); std::clog << "Using keyset from file " << keyset_filename << " to AEAD-" << mode << " file " << input_filename << " with associated data '" << associated_data << "'." << '\n'; std::clog << "The resulting output will be written to " << output_filename << '\n'; ABSL_CHECK_OK(tink_cc_examples::AeadCli(mode, keyset_filename, input_filename, output_filename, associated_data)); return 0; }
খুলুন
import ( "bytes" "fmt" "log" "github.com/tink-crypto/tink-go/v2/aead" "github.com/tink-crypto/tink-go/v2/insecurecleartextkeyset" "github.com/tink-crypto/tink-go/v2/keyset" ) func Example() { // A keyset created with "tinkey create-keyset --key-template=AES256_GCM". Note // that this keyset has the secret key information in cleartext. jsonKeyset := `{ "key": [{ "keyData": { "keyMaterialType": "SYMMETRIC", "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey", "value": "GiBWyUfGgYk3RTRhj/LIUzSudIWlyjCftCOypTr0jCNSLg==" }, "keyId": 294406504, "outputPrefixType": "TINK", "status": "ENABLED" }], "primaryKeyId": 294406504 }` // Create a keyset handle from the cleartext keyset in the previous // step. The keyset handle provides abstract access to the underlying keyset to // limit the exposure of accessing the raw key material. // // WARNING: In practice, it is unlikely you will want to use a insecurecleartextkeyset, // as it implies that your key material is passed in cleartext, which is a security risk. // Consider encrypting it with a remote key in Cloud KMS, AWS KMS or HashiCorp Vault. // See https://developers.google.com/tink/key-management-overview. keysetHandle, err := insecurecleartextkeyset.Read( keyset.NewJSONReader(bytes.NewBufferString(jsonKeyset))) if err != nil { log.Fatal(err) } // Retrieve the AEAD primitive we want to use from the keyset handle. primitive, err := aead.New(keysetHandle) if err != nil { log.Fatal(err) } // Use the primitive to encrypt a message. In this case the primary key of the // keyset will be used (which is also the only key in this example). plaintext := []byte("message") associatedData := []byte("associated data") ciphertext, err := primitive.Encrypt(plaintext, associatedData) if err != nil { log.Fatal(err) } // Use the primitive to decrypt the message. Decrypt finds the correct key in // the keyset and decrypts the ciphertext. If no key is found or decryption // fails, it returns an error. decrypted, err := primitive.Decrypt(ciphertext, associatedData) if err != nil { log.Fatal(err) } fmt.Println(string(decrypted)) // Output: message }
Java
package aead; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.crypto.tink.Aead; import com.google.crypto.tink.InsecureSecretKeyAccess; import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.RegistryConfiguration; import com.google.crypto.tink.TinkJsonProtoKeysetFormat; import com.google.crypto.tink.aead.AeadConfig; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; /** * A command-line utility for encrypting small files with AEAD. * * <p>It loads cleartext keys from disk - this is not recommended! * * <p>It requires the following arguments: * * <ul> * <li>mode: Can be "encrypt" or "decrypt" to encrypt/decrypt the input to the output. * <li>key-file: Read the key material from this file. * <li>input-file: Read the input from this file. * <li>output-file: Write the result to this file. * <li>[optional] associated-data: Associated data used for the encryption or decryption. */ public final class AeadExample { private static final String MODE_ENCRYPT = "encrypt"; private static final String MODE_DECRYPT = "decrypt"; public static void main(String[] args) throws Exception { if (args.length != 4 && args.length != 5) { System.err.printf("Expected 4 or 5 parameters, got %d\n", args.length); System.err.println( "Usage: java AeadExample encrypt/decrypt key-file input-file output-file" + " [associated-data]"); System.exit(1); } String mode = args[0]; Path keyFile = Paths.get(args[1]); Path inputFile = Paths.get(args[2]); Path outputFile = Paths.get(args[3]); byte[] associatedData = new byte[0]; if (args.length == 5) { associatedData = args[4].getBytes(UTF_8); } // Register all AEAD key types with the Tink runtime. AeadConfig.register(); // Read the keyset into a KeysetHandle. KeysetHandle handle = TinkJsonProtoKeysetFormat.parseKeyset( new String(Files.readAllBytes(keyFile), UTF_8), InsecureSecretKeyAccess.get()); // Get the primitive. Aead aead = handle.getPrimitive(RegistryConfiguration.get(), Aead.class); // Use the primitive to encrypt/decrypt files. if (MODE_ENCRYPT.equals(mode)) { byte[] plaintext = Files.readAllBytes(inputFile); byte[] ciphertext = aead.encrypt(plaintext, associatedData); Files.write(outputFile, ciphertext); } else if (MODE_DECRYPT.equals(mode)) { byte[] ciphertext = Files.readAllBytes(inputFile); byte[] plaintext = aead.decrypt(ciphertext, associatedData); Files.write(outputFile, plaintext); } else { System.err.println("The first argument must be either encrypt or decrypt, got: " + mode); System.exit(1); } } private AeadExample() {} }
Obj-C
Python
import tink from tink import aead from tink import secret_key_access def example(): """Encrypt and decrypt using AEAD.""" # Register the AEAD key managers. This is needed to create an Aead primitive # later. aead.register() # A keyset created with "tinkey create-keyset --key-template=AES256_GCM". Note # that this keyset has the secret key information in cleartext. keyset = r"""{ "key": [{ "keyData": { "keyMaterialType": "SYMMETRIC", "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey", "value": "GiBWyUfGgYk3RTRhj/LIUzSudIWlyjCftCOypTr0jCNSLg==" }, "keyId": 294406504, "outputPrefixType": "TINK", "status": "ENABLED" }], "primaryKeyId": 294406504 }""" # Create a keyset handle from the cleartext keyset in the previous # step. The keyset handle provides abstract access to the underlying keyset to # limit access of the raw key material. WARNING: In practice, it is unlikely # you will want to use a cleartext_keyset_handle, as it implies that your key # material is passed in cleartext, which is a security risk. keyset_handle = tink.json_proto_keyset_format.parse( keyset, secret_key_access.TOKEN ) # Retrieve the Aead primitive we want to use from the keyset handle. primitive = keyset_handle.primitive(aead.Aead) # Use the primitive to encrypt a message. In this case the primary key of the # keyset will be used (which is also the only key in this example). ciphertext = primitive.encrypt(b'msg', b'associated_data') # Use the primitive to decrypt the message. Decrypt finds the correct key in # the keyset and decrypts the ciphertext. If no key is found or decryption # fails, it raises an error. output = primitive.decrypt(ciphertext, b'associated_data')
AEAD
ডেটা এনক্রিপশনের জন্য সবচেয়ে সাধারণ প্রিমিটিভ হল অ্যাসোসিয়েটেড ডেটা (AEAD) প্রিমিটিভ সহ যাচাই করা এনক্রিপশন এবং এটি বেশিরভাগ প্রয়োজনের জন্য উপযুক্ত।
AEAD-এর নিম্নলিখিত প্রপার্টি আছে:
- Secrecy: দৈর্ঘ্য ছাড়া প্লেনটেক্সট সম্পর্কে আর কিছুই জানা যায় না।
- প্রামাণ্যতা: এনক্রিপ্ট করা টেক্সট যে সাইফারটেক্সটের উপর ভিত্তি করে তৈরি হয়েছে, তা শনাক্ত না করে পরিবর্তন করা অসম্ভব।
- সিমেট্রিক: একই 'কী' ব্যবহার করে প্লেইনটেক্সট এনক্রিপ্ট এবং সাইফারটেক্সট ডিক্রিপ্ট করা হয়।
- র্যান্ডমাইজেশন: এনক্রিপশন র্যান্ডমাইজ করা হয়। একই প্লেইনটেক্সট সহ দুটি মেসেজ আলাদা আলাদা সাইফারটেক্সট তৈরি করে। আক্রমণকারীরা জানতে পারে না যে কোন সাইফারটেক্সট প্রদত্ত প্লেইনটেক্সটের সাথে সম্পর্কিত। আপনি এটি এড়াতে চাইলে, এর পরিবর্তে ডিটারমিনিস্টিক AEAD ব্যবহার করুন।
সংশ্লিষ্ট ডেটা
AEAD ব্যবহার করে নির্দিষ্ট সংশ্লিষ্ট ডেটার সাথে সাইফারটেক্সট
টাই করা যেতে পারে। ধরে নিন, আপনার কাছে user-id
এবং encrypted-medical-history ফিল্ড সহ একটি ডেটাবেস আছে। এই পরিস্থিতিতে, encrypted-medical-history এনক্রিপ্ট করার সময়
user-id সংশ্লিষ্ট ডেটা হিসেবে ব্যবহার করা যেতে পারে। এটি কোনও আক্রমণকারীকে একজন ব্যবহারকারীর থেকে অন্য ব্যবহারকারীর কাছে মেডিকেল ইতিহাস সরানো থেকে বাধা দেয়।
সংশ্লিষ্ট ডেটা ঐচ্ছিক। নির্দিষ্ট করা থাকলে, এনক্রিপ্ট ও ডিক্রিপ্ট, উভয় কলের ক্ষেত্রেই একই সংশ্লিষ্ট ডেটা পাস করা হলে তবেই ডিক্রিপশন সফল হয়।
'কী'-এর ধরন বেছে নিন
বেশিরভাগ ক্ষেত্রে আমরা AES128_GCM ব্যবহার করার সাজেশন দিলেও, বিভিন্ন প্রয়োজনের জন্য বিভিন্ন ধরনের 'কী' আছে। AES128-এ ১২৮-বিট নিরাপত্তা এবং AES256-এ ২৫৬-বিট নিরাপত্তা পাওয়া যায়।
কোনও মোড বেছে নেওয়ার সময় দুটি উল্লেখযোগ্য নিরাপত্তা সংক্রান্ত সীমাবদ্ধতা হল:
- QPS: একই 'কী' দিয়ে কতগুলি মেসেজ এনক্রিপ্ট করা হয়েছে?
- মেসেজের সাইজ: মেসেজ কতটা বড়?
যেসব ধরনের 'কী' কাজ করে:
- ১৬-বাইট
ইনিশিয়ালাইজেশন ভেক্টর (IV) সহ AES-CTR-HMAC (AES128_CTR_HMAC_SHA256, AES256_CTR_HMAC_SHA256) হল ভাল বাউন্ড সহ সবচেয়ে রক্ষণশীল মোড।
- কী-কমিটিং।
- AES-EAX (AES128_EAX, AES256_EAX) হল AES128_CTR_HMAC_SHA256-এর তুলনায় সামান্য কম রক্ষণশীল এবং সামান্য দ্রুত।
- MLGR-এ কী-কমিট করা হয়নি।
- AES-GCM (AES128_GCM, AES256_GCM) হল সাধারণত সবচেয়ে দ্রুত মোড, যেখানে মেসেজের সংখ্যার উপর সবচেয়ে কঠোর সীমা (২৩২) এবং মেসেজের সাইজ (প্রতি মেসেজে ~৬৪ জিবি) থাকে। এইসব সীমা অতিক্রম করা হলে, AES-GCM ভয়াবহভাবে ব্যর্থ হয়, কারণ এটি প্লেনটেক্সট এবং AES-GCM ইন্টার্নাল কী-এর যাচাইকরণ অংশটি ফাঁস করে দেয়।
- AES-GCM-SIV (AES128_GCM_SIV, AES256_GCM_SIV)
[RFC8452] আকস্মিক ননস অপব্যবহারের
প্রতিরোধক এবং AES-GCM-এর মতোই পারফর্ম করে। র্যান্ডম IV সহ AES-GCM-SIV-এর জন্য মেসেজের সংখ্যা ও মেসেজের সাইজের সীমা AES-GCM-এর সীমার থেকে অনেক বেশি
হয়। এইসব সীমা অতিক্রম করা হলে, AES-GCM-SIV
কম বিপর্যয়করভাবে ব্যর্থ হয়: এটি শুধুমাত্র এই তথ্য ফাঁস করে যে
দুটি মেসেজ সমান।
AES-GCM-SIV ব্যবহার করা নিরাপদ, তবে বাস্তবে এটি খুব বেশি ব্যবহার করা হয় না।
- র্যান্ডম ননসের বাউন্ড:
- 232 মেসেজ, যেখানে প্রতিটি প্লেইনটেক্সট সর্বাধিক ৮ GiB
- 248 মেসেজ, যেখানে প্রতিটি প্লেইনটেক্সট সর্বাধিক 32 MiB
- ২৬৪টি মেসেজ, যেখানে প্রতিটি প্লেনটেক্সট সর্বাধিক ১২৮ KiB
- Java-তে ব্যবহার করতে, Conscrypt ইনস্টল করুন।
- কী-কমিটিং ADGKLS নয়।
- র্যান্ডম ননসের বাউন্ড:
- XChaCha20-Poly1305 (XCHACHA20_POLY1305)-এর মেসেজের সংখ্যা ও মেসেজের সাইজের
উপর AES-GCM-এর তুলনায় অনেক বেশি সীমা আছে, কিন্তু এটি কাজ না করলে
(খুব কম সম্ভাবনা), এটি কী মেটেরিয়ালও লিক করে দেয়। এটি হার্ডওয়্যার অ্যাক্সিলারেটেড নয়,
তাই হার্ডওয়্যার অ্যাক্সিলারেশন উপলভ্য আছে এমন পরিস্থিতিতে এটি AES মোডের চেয়ে
ধীরগতির হতে পারে।
- কী-কমিটিং LGR নয়।
AEAD সাইফারটেক্সটের ওয়্যার ফর্ম্যাট সম্পর্কে আরও জানুন।
নিরাপত্তা সংক্রান্ত গ্যারান্টি
AEAD ইমপ্লিমেন্টেশন এগুলি অফার করে:
- CCA2 নিরাপত্তা।
- অন্তত ৮০-বিট যাচাইকরণ ক্ষমতা।
- মোট 250 বাইট সহ অন্তত 232 মেসেজ এনক্রিপ্ট করার ক্ষমতা। ২৩২ পর্যন্ত বেছে নেওয়া প্লেইনটেক্সট বা বেছে নেওয়া সাইফারটেক্সট সহ কোনও অ্যাটাকের সাফল্যের সম্ভাবনা ২-৩২-এর বেশি নয়।