Wolfram MathematicaでのAES実装

Wolfram Mathematicaの記事 Habermanの紹介で8bitjoeyはコミュニティに素晴らしいWolfram Mathematica数学パッケージを紹介しました。

今日、私はこの製品への遠足を続けます。 ビジネスと喜びを組み合わせるために、この製品を使用してAESアルゴリズムを実装します。







導入する代わりに



この記事はすでにMathematicaにある程度慣れているユーザーを対象としています。 問題のパッケージには、非常に詳細なドキュメントが含まれています。 関数に関するヘルプを呼び出すには、その関数を選択してF1を押すだけです。 AESの仕組み (JavaScriptの例を含む)については、記事「AESの仕組み」で説明されています。



AESの実装



まず、グローバル変数を宣言しましょう:置換(Sボックス)、ラウンド数(Nr)、ブロックサイズ(Nb)、キーサイズ(Nk)。

 Needs["FiniteFields`"]; fld = FiniteFields`GF[2, {1, 1, 0, 1, 1, 0, 0, 0, 1}]; Sbox = {99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22}; Nr = 10; Nb = 4; Nk = 4;
      
      









Needs["FiniteFields`"]; fld = FiniteFields`GF[2, {1, 1, 0, 1, 1, 0, 0, 0, 1}]; Sbox = {99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22}; Nr = 10; Nb = 4; Nk = 4;













最初の行はパッケージを有限フィールドで動作するように接続し、2行目はそれを宣言します。 {1、1、0、1、1、0、0、0、1} = 1 + x + x ^ 3 + x ^ 4 + x ^ 8は体GF(256)の既約多項式です。



Mathematicaのすべての変数はグローバルであることを思い出してください。 変数をローカルにするために、Module関数が使用されます。 以下は、密接に関連する関数を含むMain関数のコードです。



 Rijndael[State_, CipherKey_] := Module[{ExpandedKey, i, tState}, ( tState = State; (*   state    *) ExpandedKey = KeyExpansion[CipherKey]; (*          *) tState = AddRoundKey[tState, ExpandedKey[[1]]]; (*   *) For[i = 1, i < Nr, i++, ( tState = RRound[tState, ExpandedKey[[i + 1]]]; (* *) ) ]; tState = FinalRRound[tState, ExpandedKey[[Nr + 1]]]; (* *) Return[tState]; ) ]; Main[] := Module[{}, ( Plaintext = FromDigits["00112233445566778899aabbccddeeff", 16]; Key = FromDigits["000102030405060708090a0b0c0d0e0f", 16]; CipherText = Rijndael[Plaintext, Key]; Print[BaseForm[CipherText, 16]]; Print[CipherText == 16^^69c4e0d86a7b0430d8cdb78070b4c55a]; ) ]; Main[];
      
      









Rijndael[State_, CipherKey_] := Module[{ExpandedKey, i, tState}, ( tState = State; (* state *) ExpandedKey = KeyExpansion[CipherKey]; (* *) tState = AddRoundKey[tState, ExpandedKey[[1]]]; (* *) For[i = 1, i < Nr, i++, ( tState = RRound[tState, ExpandedKey[[i + 1]]]; (* *) ) ]; tState = FinalRRound[tState, ExpandedKey[[Nr + 1]]]; (* *) Return[tState]; ) ]; Main[] := Module[{}, ( Plaintext = FromDigits["00112233445566778899aabbccddeeff", 16]; Key = FromDigits["000102030405060708090a0b0c0d0e0f", 16]; CipherText = Rijndael[Plaintext, Key]; Print[BaseForm[CipherText, 16]]; Print[CipherText == 16^^69c4e0d86a7b0430d8cdb78070b4c55a]; ) ]; Main[];











習慣ではないが、空のパラメーターであっても、モジュールを介して関数を記述します。 Main関数で指定されたプレーンテキスト(Plaintext)とキー(Key)は、コードをテストするためにfips 197から取得されます。 16番目のシステムから10番目のシステムへの転送は、マクロ「16 ^^」またはFromDigits関数を使用する2つの方法で実行できます。

Mathematicaで暗号化アルゴリズムを作成する場合、IntegerDigitsおよびFromDigits関数がよく使用されます。 1つ目は数値を任意の基底に変換し、2つ目は反対のアクションを実行します。

状態は常に整数を表します。 これは、アルゴリズムを理解するためです。



以下は、主要な展開機能のコードです。

 KeyExpansion[CipherKey_] := Module[{i, j, k, ExpandedKey}, ( ExpandedKey = Array[#*0 &, Nr + 1]; (*   Nr + 1   *) ExpandedKey[[1]] = CipherKey; For[i = 1, i <= Nr + 1, i++, ( ExpandedKey[[i]] = IntegerDigits[ExpandedKey[[i]], 2^8, 16]; (*   *) ExpandedKey[[i]] = Partition[ExpandedKey[[i]], 4]; (*   *) ) ]; (* rcon*) Rcon = Array[{#*0, #*0, #*0, #*0} &, Nr]; Rcon[[1, 1]] = 1; For[i = 2, i <= Nr, i++, ( Rcon[[i, 1]] = Rcon[[i - 1, 1]]*2; If[Rcon[[i, 1]] >= 256, Rcon[[i, 1]] = BitXor[Rcon[[i, 1]], 283]]; ) ]; For[i = 2, i <= Nr + 1, i++, ( ExpandedKey[[i, 1]] = RotateLeft[ExpandedKey[[i - 1, 4]], 1]; (*    1 *) (* *) For[j = 1, j <= 4, j++, ( ExpandedKey[[i, 1, j]] = Sbox[[ExpandedKey[[i, 1, j]] + 1]]; ) ]; (*Xor  *) ExpandedKey[[i, 1]] = BitXor[ExpandedKey[[i, 1]], Rcon[[i - 1]], ExpandedKey[[i - 1, 1]]]; (*   *) For[k = 2, k <= 4, k++, ( ExpandedKey[[i, k]] = BitXor[ExpandedKey[[i, k - 1]], ExpandedKey[[i - 1, k]]]; ) ]; ) ]; (*   Integer*) For[i = 1, i <= Nr + 1, i++, ( ExpandedKey[[i]] = Flatten[ExpandedKey[[i]]]; ExpandedKey[[i]] = FromDigits[ExpandedKey[[i]], 2^8]; ) ]; Return[ExpandedKey]; (*   *) ) ];
      
      









KeyExpansion[CipherKey_] := Module[{i, j, k, ExpandedKey}, ( ExpandedKey = Array[#*0 &, Nr + 1]; (* Nr + 1 *) ExpandedKey[[1]] = CipherKey; For[i = 1, i <= Nr + 1, i++, ( ExpandedKey[[i]] = IntegerDigits[ExpandedKey[[i]], 2^8, 16]; (* *) ExpandedKey[[i]] = Partition[ExpandedKey[[i]], 4]; (* *) ) ]; (* rcon*) Rcon = Array[{#*0, #*0, #*0, #*0} &, Nr]; Rcon[[1, 1]] = 1; For[i = 2, i <= Nr, i++, ( Rcon[[i, 1]] = Rcon[[i - 1, 1]]*2; If[Rcon[[i, 1]] >= 256, Rcon[[i, 1]] = BitXor[Rcon[[i, 1]], 283]]; ) ]; For[i = 2, i <= Nr + 1, i++, ( ExpandedKey[[i, 1]] = RotateLeft[ExpandedKey[[i - 1, 4]], 1]; (* 1 *) (* *) For[j = 1, j <= 4, j++, ( ExpandedKey[[i, 1, j]] = Sbox[[ExpandedKey[[i, 1, j]] + 1]]; ) ]; (*Xor *) ExpandedKey[[i, 1]] = BitXor[ExpandedKey[[i, 1]], Rcon[[i - 1]], ExpandedKey[[i - 1, 1]]]; (* *) For[k = 2, k <= 4, k++, ( ExpandedKey[[i, k]] = BitXor[ExpandedKey[[i, k - 1]], ExpandedKey[[i - 1, k]]]; ) ]; ) ]; (* Integer*) For[i = 1, i <= Nr + 1, i++, ( ExpandedKey[[i]] = Flatten[ExpandedKey[[i]]]; ExpandedKey[[i]] = FromDigits[ExpandedKey[[i]], 2^8]; ) ]; Return[ExpandedKey]; (* *) ) ];











質問がある場合は、コードにコメントを付けてから質問してください。



 RRound[State_, Key_] := Module[{i, tState}, ( tState = State; tState = ByteSub[tState]; tState = ShiftRow[tState]; tState = MixColumn[tState]; tState = AddRoundKey[tState, Key]; Return[tState]; ) ]; FinalRRound[State_, Key_] := Module[{i, tState}, ( tState = State; tState = ByteSub[tState]; tState = ShiftRow[tState]; tState = AddRoundKey[tState, Key]; Return[tState]; ) ];
      
      









RRound[State_, Key_] := Module[{i, tState}, ( tState = State; tState = ByteSub[tState]; tState = ShiftRow[tState]; tState = MixColumn[tState]; tState = AddRoundKey[tState, Key]; Return[tState]; ) ]; FinalRRound[State_, Key_] := Module[{i, tState}, ( tState = State; tState = ByteSub[tState]; tState = ShiftRow[tState]; tState = AddRoundKey[tState, Key]; Return[tState]; ) ];











このコードに示されている巡回(ラウンド)関数と最終変換の関数は、説明を必要としないと思います-それらはfips 197とまったく同じ方法で示されます。



 AddRoundKey[State_, RoundKey_] := Module[{tState}, ( tState = BitXor[State, RoundKey]; Return[tState]; ) ];
      
      









AddRoundKey[State_, RoundKey_] := Module[{tState}, ( tState = BitXor[State, RoundKey]; Return[tState]; ) ];











キー追加機能は非常に原始的です。モジュール2で入力値とキーを追加する必要があります。 これを行うには、BitXor関数を使用します。この関数は整数を入力に渡します。



 ByteSub[State_] := Module[{i, tState}, ( tState = State; tState = IntegerDigits[tState, 2^8, 16]; For[i = 1, i <= 16, i++, ( tState[[i]] = Sbox[[tState[[i]] + 1]]; ) ]; tState = FromDigits[tState, 2^8]; Return[tState]; ) ];
      
      









ByteSub[State_] := Module[{i, tState}, ( tState = State; tState = IntegerDigits[tState, 2^8, 16]; For[i = 1, i <= 16, i++, ( tState[[i]] = Sbox[[tState[[i]] + 1]]; ) ]; tState = FromDigits[tState, 2^8]; Return[tState]; ) ];











SubByte関数は3つの部分で構成されています。





 ShiftRow[State_] := Module[{tState}, ( tState = State; tState = IntegerDigits[tState, 2^8, 16]; tState = Partition[tState, 4]; tState = Transpose[tState]; tState[[2]] = RotateLeft[tState[[2]], 1]; tState[[3]] = RotateLeft[tState[[3]], 2]; tState[[4]] = RotateLeft[tState[[4]], 3]; tState = Transpose[tState]; tState = Flatten[tState]; tState = FromDigits[tState, 2^8]; Return[tState]; ) ];
      
      









ShiftRow[State_] := Module[{tState}, ( tState = State; tState = IntegerDigits[tState, 2^8, 16]; tState = Partition[tState, 4]; tState = Transpose[tState]; tState[[2]] = RotateLeft[tState[[2]], 1]; tState[[3]] = RotateLeft[tState[[3]], 2]; tState[[4]] = RotateLeft[tState[[4]], 3]; tState = Transpose[tState]; tState = Flatten[tState]; tState = FromDigits[tState, 2^8]; Return[tState]; ) ];











ShiftRow関数のコードは非常に簡単です。 1行目から3行目-整数からマトリックスへの変換、次の3行はそれぞれ1、2、3バイトをシフトし、マトリックスから整数に戻します。



そして、最も興味深いのは、数学の観点から、MixColumn関数です。これはコードで直接コメントします。 質問がある場合-質問してください。

 MixColumn[State_] := Module[{i, j, tState}, ( tState = State; (* Integer  *) tState = IntegerDigits[tState, 2^8, 16]; tState = Partition[tState, 4]; tState = Transpose[tState]; (*   fips 197*) M = ({ {2, 3, 1, 1}, {1, 2, 3, 1}, {1, 1, 2, 3}, {3, 1, 1, 2} }); (*    *) For[i = 1, i <= 4, i++, ( For[j = 1, j <= 4, j++, ( M[[i, j]] = fld[Reverse[IntegerDigits[M[[i, j]], 2, 8]]]; (* Integer    *) tState[[i, j]] = fld[Reverse[IntegerDigits[tState[[i, j]], 2, 8]]]; ) ]; ) ]; (*   *) tState = M.tState; (* State  *) For[i = 1, i <= 4, i++, ( For[j = 1, j <= 4, j++, ( If[tState[[i, j]] == 0, Continue[]]; tState[[i, j]] = FromDigits[Reverse[tState[[i, j]][[1]]], 2]; ) ]; ) ]; (*    Integer*) tState = Transpose[tState]; tState = Flatten[tState]; tState = FromDigits[tState, 2^8]; Return[tState]; ) ];
      
      









MixColumn[State_] := Module[{i, j, tState}, ( tState = State; (* Integer *) tState = IntegerDigits[tState, 2^8, 16]; tState = Partition[tState, 4]; tState = Transpose[tState]; (* fips 197*) M = ({ {2, 3, 1, 1}, {1, 2, 3, 1}, {1, 1, 2, 3}, {3, 1, 1, 2} }); (* *) For[i = 1, i <= 4, i++, ( For[j = 1, j <= 4, j++, ( M[[i, j]] = fld[Reverse[IntegerDigits[M[[i, j]], 2, 8]]]; (* Integer *) tState[[i, j]] = fld[Reverse[IntegerDigits[tState[[i, j]], 2, 8]]]; ) ]; ) ]; (* *) tState = M.tState; (* State *) For[i = 1, i <= 4, i++, ( For[j = 1, j <= 4, j++, ( If[tState[[i, j]] == 0, Continue[]]; tState[[i, j]] = FromDigits[Reverse[tState[[i, j]][[1]]], 2]; ) ]; ) ]; (* Integer*) tState = Transpose[tState]; tState = Flatten[tState]; tState = FromDigits[tState, 2^8]; Return[tState]; ) ];













結論



したがって、Mathematicaを使用すると、数学(ほとんどの部品番号理論 )を知っていれば、暗号化アルゴリズムを簡単に実装できます。



あとがき



先月、私はセージ製品を使用してプログラミングを行ってきました。 移行の理由は、暗号化のニーズに対するMathematicaの不十分なパフォーマンスと製品の高コストでした。 Mathematicaをセージに変更するのは、組み込みのヘルプが未発達であるため少し複雑でした。 しかし、数日後にあなたはそれに慣れ、すべてが簡単になります。 簡単に習得できるセージは、Pythonを知っている人向けです。



Mathematicaについて質問がある場合は、お問い合わせください! 可能であれば、私は答えようとします。



ここからアルゴリズム全体のソースコードを取得できます



All Articles