0

RFC 4880 の 50 ページの下部には、平文データを暗号化して Symmetrically Encrypted Integrity Protected Data Packet (Tag 18) に格納する方法が説明されています。CFBモードを使用しているとのこと。セクション 13.9 (対称的に暗号化されたデータ パケット (タグ 9) の場合) で説明されているように機能するように、CFB のコードを作成しました。ただし、50ページには次のようにも書かれています。

対称的に暗号化されたデータ パケットとは異なり、このプレフィックス データを暗号化した後、特別な CFB 再同期は行われません。

したがって、タグ 18 の場合、手順は次のようになります。

ステップごとに、手順は次のとおりです。

  1. フィードバック レジスタ (FR) は、すべてゼロの IV に設定されます。

  2. FR を暗号化して FRE (FR Encrypted) を生成します。これは、すべてゼロの値の暗号化です。

  3. FRE は、暗号文の最初の BS オクテットである C1 ~ C[BS] を生成するために、平文の前に付けられたランダム データの最初の BS オクテットと xor されます

  4. FR には C[ 1 ] から C[BS] がロードされます。

  5. FR は、暗号文の最初の BS オクテットの暗号化である FRE を生成するために暗号化されます。

  6. FRE の左側の 2 オクテットは、平文の前に付けられた次の 2 オクテットのデータと xor されます。これにより、暗号文の次の 2 オクテットである C[BS+1] と C[BS+2] が生成されます。

  7. (̶T̶h̶e̶ ̶r̶e̶s̶y̶n̶c̶h̶r̶o̶n̶i̶z̶a̶t̶i̶o̶n̶s̶t̶e̶p̶)̶ ̶F̶R̶ ̶i̶s̶ ̶l̶o̶a̶d̶e̶d̶ ̶w̶i̶t̶h̶ C̶[̶3̶]̶ ̶t̶h̶r̶o̶u̶g̶h̶ C̶[2̶̶̶̶+

  8. FR は暗号化されて生成されます— FRE

  9. FRE は、プレフィックス付きデータの BS+2 オクテットの暗号化が完了したので、指定された平文の最初の BS オクテットで xor されます。これにより、暗号文の次の BS オクテットである C[BS+3] から C[BS+(BS+2)] が生成されます。

  10. FR には、C[BS+3] から C[BS + (BS+2)] (8 オクテット ブロックの場合は C11-C18) がロードされます。

    1. FR を暗号化して FRE を生成します。

    2. FRE は、暗号文の次の BS オクテットを生成するために、平文の次の BS オクテットと xor されます。これらは FR にロードされ、平文が使い果たされるまでプロセスが繰り返されます。

再同期ステップが実行されていないため、ステップ 8 はステップ 5 の FRE を使用するため、無視できますよね?

上記の手順から、次のコードで何を誤解しましたか? タグ 9 では再同期ステップで機能することはわかっていますが、タグ 18 では何かがうまくいきません。

std::string CFB_encrypt(SymAlg * crypt, uint8_t packet, std::string data, std::string prefix){
    uint8_t BS = crypt -> blocksize() >> 3;
    // 1
    std::string IV(BS, 0);
    // 2
    std::string FR = IV;
    std::string FRE = crypt -> encrypt(FR);
    // 3
    FRE = xor_strings(FRE, prefix);
    std::string C = FRE;
    // 4
    FR = C;
    // 5
    FRE = crypt -> encrypt(FR);
    // 6
    C += xor_strings(FRE.substr(0, 2), prefix.substr(BS - 2, 2));
    // 7
    if (packet == 9){
        FR = C.substr(2, BS);
    }
    // 8
    FRE = crypt -> encrypt(FR);
    // 9
    C += xor_strings(FRE, data.substr(0, BS));
    unsigned int x = BS;
    while (x < data.size()){
        // 10
        FR = C.substr(x + 2, BS);
        // 11
        FRE = crypt -> encrypt(FR);
        // 12
        C += xor_strings(FRE, data.substr(x, BS));
        x += BS;
    }
    return C;
}

私は何を取りこぼしたか?

4

2 に答える 2

1

ここであなたのステップ3:

3. FRE is xored with the first BS octets of random data prefixed to the plaintext

あなたのコードと一致しません:

// 3
std::string C = FRE;

ここでは xoring は行われません。次のように変更してみてください。

std::string C = xor_strings(FRE, prefix.substr(0,8));
于 2013-08-14T21:09:12.587 に答える
0

このコードのチャンクは何とか機能します。理解できません:

    else {
        plaintext = "  "+plaintext;
        // 9.  FRE is xored with the first 8 octets of the given plaintext, now
        //     that we have finished encrypting the 10 octets of prefixed data.
        //     This produces C11-C18, the next 8 octets of ciphertext.
        for (var i = 2; i &lt; block_size; i++) ciphertext += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(i));
        var tempCiphertext = ciphertext.substring(0,2*block_size).split('');
        var tempCiphertextString = ciphertext.substring(block_size);
        for(n=block_size; n&lt;plaintext.length; n+=block_size) {
            // 10. FR is loaded with C11-C18
            for (var i = 0; i &lt; block_size; i++) FR[i] = tempCiphertextString.charCodeAt(i);
            tempCiphertextString='';

            // 11. FR is encrypted to produce FRE.
            FRE = blockcipherencryptfn(FR, key);

            // 12. FRE is xored with the next 8 octets of plaintext, to produce the
            //     next 8 octets of ciphertext.  These are loaded into FR and the
            //     process is repeated until the plaintext is used up.
            for (var i = 0; i &lt; block_size; i++){ tempCiphertext.push(String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(n+i)));
            tempCiphertextString += String.fromCharCode(FRE[i] ^ plaintext.charCodeAt(n+i));
            }
        }
        ciphertext = tempCiphertext.join('');

    }
    return ciphertext;
}
于 2013-08-15T21:05:15.547 に答える