4 Mayıs 2020 Pazartesi

AES - Advanced Encryption Standard

Giriş
AES kısaltması Advanced Encryption Standard kelimelerinin baş harflerinden oluşur. İlk öğrenirken hep Asymmetric Encryption Standard olarak hatırlıyordum. Ama aslında simetrik bir şifreleme algoritmasıdır. Yani aynı anahtar ile düz metin şifrelenir ve açılır. Açıklaması şöyle.
The Advanced Encryption Standard (AES) is the latest encryption standard adopted by NIST in 2001 for symmetric encryption of messages. The AES algorithm was selected as part of a contest to find a replacement for the Data Encryption Standard (DES). This algorithm was based on the Rijndael cipher developed by two Belgian mathematicians, Joan Daemen and Vincent Rijmen.

As part of the NIST standard,  AES uses a block size of 128 bits and supports three key lengths: 128, 192 and 256 bits.
Askeri ve Sivil Kripto
Askeri ve sivil kripto diye bir ayrım aslında yok. Açıklaması şöyle.
There’s no difference except for marketing. Neither of the terms has a meaningful technical definition on which a substantial share of experts in the field can agree.

Historically there was a difference in permissible key length due to U. S. export restrictions but those stopped a long time ago except for a few countries that are under broader embargo (e. g. North Korea, Iran, Syria).
Data Protection API
Açıklaması şöyle.
Data Protection API (DPAPI), which is in Windows from NT 5.0 onwards, which nowadays uses AES-256 to encrypt the password data.
Password Key Derivation Function
Password Key Derivation Function (PKDF) AES'e dayalıdır.

Initialization Vector
Initialization Vector (IV) açık olarak gönderilebilir. Açıklaması şöyle.
There is no security hole by sending the IV in cleartext - this is similar to storing the salt for a hash in plaintext: As long as the attacker has no control over the IV/salt, and as long as it is random, there is no problem.
Key - Anahtar
Aynı makinede çalışan iki uygulama anahtar üretmek için şu yöntemi kullanabilir.
With traditional servers, the only real solution that I'm aware of is to derive an AES key from some sort of "hardware fingerprint" built by combining serial numbers of all the hardware devices. Then use that AES key to encrypt the credentials. A process running on the same server can reconstruct the AES key and decrypt the credentials,
statik key ile ilgili bir açıklama burada.

Anahtar ve Blok Büyüklüğü
Anahtar 128 veya 192 veya 256 bit uzunluğunda olabilir. Blok büyüklüğü ise her zaman 128 bit uzunluğundadır. 128 bit binary çıktıdır. Bu çıktı ASCII veya Base64 ile büyümesi pahasına okunabilir hale getirilebilir.

Bu kriptonun yayınlandıktan sonra standart hale gelmesi 2 sene sürdü. AES ssl/tls bağlantısı kurulurken istemci tarafından gönderilen kullanılabilecek kriptolar listesi arasında genellikle mevcuttur.

AES-256 2017 yılında ben bu yazıyı yazarken ABD hükümeti tarafından en gizli belgeleri şifrelemek için kullanılıyordu

Number Of Rounds
Key Size Number Of Rounds
128         10
192         12
256         14
Açıklaması şöyle.
The standard specifies these specific number of rounds to ensure that different implementations are interoperable.
Her 32 bit için round sayısı 1 artırılır. Açıklaması şöyle.
For Rijndael versions with a longer key, the number of rounds was raised by one for every additional 32 bits in the cipher key. This was done for the following reasons:
  1. One of the main objectives is the absence of shortcut attacks, i.e. attacks that are more efficient than an exhaustive key search. Since the workload of an exhaustive key search grows with the key length, shortcut attacks can afford to be less efficient for longer keys.
  2. (Partially) known-key and related-key attacks exploit the knowledge of cipher key bits or the ability to apply different cipher keys. If the cipher key grows, the range of possibilities available to the cryptanalyst increases.
Çalışması
AES şöyle çalışır.
E: AES şifrelemesi
D: AES şifre açması
x: düz metin
y: şifrelenmiş metin
k: anahtar

ise

şifrelenmiş metin: y = E(x, k)
düz metin: x = D(y, k)

Modların Özellikleri
AES modları iki grupta düşünülebilir. 

1.Veriyi pad'leyen modlar
Padleme için bir yöntem seçilir. Örneğin PKCS#7. Pad byte'ları kaçlık pad olduğunu gösterir. Örneğin 4 byte'lık pad kullanılıyorsa byte'lar şöyledir.
04 04 04 04
Örneğin 3 byte'lık pad kullanılıyorsa byte'lar şöyledir.
03 03 03
2. Stream Cipher modlar.
Veriyi padlemezler.

Modlar
1. CFB1
Açıklaması şöyle.
CFB1 is an older format that is mainly useful for its error propagation properties. That's not often considered an important factor anymore. 
2. ECB - Electronic Codebook  - Kullanmayın
Açıklaması şöyle. Veriyi gerekiyorsa padler. Kullanılmaması öneriliyor.
The electronic codebook (ECB) mode encrypts each block individually. Any blocks that are identical and in the same message,  or that are in a different message encrypted with the same key, will be transformed into identical ciphertext blocks. The disadvantage of ECB is that identical plaintext blocks are encrypted to identical ciphertext blocks (thus, it does not hide data patterns well). In some senses, it doesn't provide message confidentiality at all and so it is not recommended for cryptographic protocols
Blok büyüklüğü 16 byte. Ancak eskiden 8 byte imiş. Açıklaması şöyle.
The ECB (Electronic Code Book) block cipher mode of operation mentioned above encrypts every block (typically 16 bytes for modern ciphers; historically often only 8 bytes) using the same algorithm...
Aynı blokların açıklaması şöyle.
ECB is a block cipher algorithm which will convert the repeated plain text to same repeated cipher text. Means if "a" means 0011 then every time when "a" strikes, it will encode it as 0011. So it's not good for small block sizes...
Pek tercih edilmiyor. Hatta TLS 1.3 artık AES-CBC desteklemiyor. Açıklaması şöyle.
The problem here is not so much with CBC, but with alternatives that are easier to implement safely, without losing mathematical security.In fact, AES-CBC turned out to be notoriously difficult to implement correctly.
Bu modda IV'nin verilmesi gerekmiyor

3. CBC - Cypher Block Chaining - Kullanmayın
Açıklaması şöyle. Veriyi gerekiyorsa padler. Bu mod sıkça kullanılıyor ancak artık en iyisi değil
The cypher block chaining (CBC) mode introduces feedback. Before each plaintext block is encrypted, it is combined with the ciphertext of the previous block with a bitwise exclusive OR operation. This ensures that, even if the plaintext contains many identical blocks, they will each encrypt to a different ciphertext block. The initialization vector  (IV) is combined with the first plaintext block by a bitwise exclusive OR operation before the block is encrypted. 
Bu modda aynı anahtar ile yapılan her şifrelemede IV'nin random olması gerekir. IV 128 bit (16 byte) uzunlunğunda olmalıdır

CBC Authentication sağlamaz. Açıklaması şöyle.
AES-CBC does not provide authentication, which are words that should scare the ever-living daylights out of you. AES-CBC is only an encryption scheme, not an authenticated-encryption scheme.
CBC Nedir?
Bir bloğun çıktısının diğer bloğa eklenmesine Cypher Block Chaining (CBC) denilir. Çok basit bir örnek şöyle. Elimizde A = 1, B = 2, C = 3 diye bir tablo olsun ve AAA kelimesini şifrelemek isteyelim. Şöyle yaparız.
1. A = A + previous + key = 1 + 0 + 5 = 6
*6 corresponds to the letter F
2. A = A + previous + key = 1 + 6 + 5 = 12
*12 corresponds to L
3. A = 1 + 12 + 5 = 18
*18 corresponds to R
4. A = 1 + 18 + 5 = 24
*24 corresponds to X
Çıktı olarak şunu alırız.
FLRX
Bu modun kodlaması karışık olduğu için TLS 1.3'ten çıkarıldı. Açıklaması şöyle.
AES-CBC mode combined with decent HMAC can be as secure as AES-GCM. However, combining the cipher and MAC securely has been in practice found to be much harder said than done. Also, padding that is required by AES-CBC mode complicates things.

4. CFB -Cipher Feedback
Şeklen şöyle. Veriyi padlemez, veri ile aynı büyüklükte çıktı verir. Bu yüzden Stream Cipher olarak sınıflandırılır.

Açıklaması şöyle.
CFB just takes the previous block as the IV for the next block making it a stream cipher.
Açıklaması şöyle.
CBC, CFB and CFB1 are all non-authenticated modes of operation. CFB is a streaming mode of operation which doesn't require a randomized IV (just a unique IV). Because it is a streaming mode it also doesn't require a padding mode.
Açıklaması şöyle.
In CFB-mode you effectively have a data-dependent stream cipher and you'll need ceil (l/n) block cipher calls to process the full message, where l is the mesage length (in bits), n the block size (in bits).

Açıklaması şöyle.
So, what actually happens in CFB mode encryption is that the plaintext message is divided into blocks of some fixed length (which typically matches the block size of the block cipher being used). Each of these plaintext blocks is then XORed with the output of the block cipher, and the result of this XOR operation is output as the corresponding ciphertext block, and also used as the input to the block cipher for encrypting the next plaintext block.

Most of the other classical block cipher modes of operation work in a similar way, only the details differ. For example, in OFB mode the output of the block cipher is saved for use as input to the next block cipher call before it is XORed with the current plaintext block to produce the corresponding ciphertext block.

5. OFB - Output Feedback
Açıklaması şöyle. Veriyi padlemez, veri ile aynı büyüklükte çıktı verir. Bu yüzden Stream Cipher olarak sınıflandırılır.
The output feedback (OFB) mode processes small increments of plaintext into ciphertext instead of processing an entire block at a time. This mode is similar to CFB, but it differs in the way the shift register is filled. If a bit in the ciphertext is mangled, the corresponding bit of plaintext will be mangled. However, if there are extra or missing bits from ciphertext, the plaintext will be mangled from that point on.

6. CTR - Counter
CTR mod yazısına taşıdım.

7.GCM
TLS 1.2 ile kullanılıyorAçıklaması şöyleVeriyi padlemez, veri ile aynı büyüklükte çıktı verir. Bu yüzden Stream Cipher olarak sınıflandırılır. Bu mod allta CTR mod'u kullanıyor.
AES-GCM takes a 96-bit nonce and NIST says that you can only encrypt 232 messages under a single key if using random nonces. This is because if you throw 232 balls at 296 buckets then you have roughly a 233 chance of getting two in the same bucket and NIST drew the line there. That probability might seem either high or low to you. It's pretty small in absolute terms and, unlike a work factor, an attacker can't spend resources against it, but it's a very long way from the safety margins that we usually use in cryptography.
Açıklaması şöyle.
An AES-GCM implementation that accepts messages longer than 2^39−256 bits, i.e. 2^32−2 blocks, is broken. 
Açıklaması şöyle.
GCM does not provide secure hashing. In general, a MAC has all the properties of a hash only against an adversary who does not know the key. If you want to use the function as a MAC then the key has to be public and then A MAC is not a secure hash. With most common MAC constructions other than HMAC, if you know the key, you can easily construct, at least, a second preimage.
IV olarak verilen 12 byte yani 96 bit aslında nonce. Normalde AES 128 bit IV ile çalışıyor. Açıklaması şöyle
GCM works best with a nonce of 12 bytes though. GCM converts data - includes the nonce - to a 128 bit counter for CTR mode internally.
IV'nin CBC mod'daki gibi random olması gerekmez. Sadece aynı anahtar ile yapılan her şifreleme işleminde unique olması yeterlidir. Bu modda 128 bit IV sağlasak bile bir başka işlem ile 96 bit haline getirilir.

Bu mod yerine AES-SIV veya AES-GCM-SIV kullanılması öneriliyor.

8.SIV ve GCM-SIV
Bu modlarda sabit IV/nonce kullanılabilir.

AES Gerçekleştirimi
4 yöntem var. 
1. Lookup Table
2. With Inversion in GF(2)[X]
3. Bit Sliced
4. AES Donanım Desteği
Bu gerçekleştirim en ilginci çünkü Intel AES için instruction sağlıyor. Şöyle yaparız.
aesenc xmm1, xmm3   % xmm1 - data, xmm3 - key
Bu da şöyle bir kod çıkmasına sebep olur.
# Encrypt the block.
pxor       %xmm5,  %xmm0
aesenc     %xmm6,  %xmm0
aesenc     %xmm7,  %xmm0
aesenc     %xmm8,  %xmm0
aesenc     %xmm9,  %xmm0
aesenc     %xmm10, %xmm0
aesenc     %xmm11, %xmm0
aesenc     %xmm12, %xmm0
aesenc     %xmm13, %xmm0
aesenc     %xmm14, %xmm0
aesenclast %xmm15, %xmm0
AES Kablosuz Klavye
Kablosuz klavyelerde genellikle AES-128 şifreleme kullanılır.



Hiç yorum yok:

Yorum Gönder