{"id":447,"date":"2018-07-07T16:57:23","date_gmt":"2018-07-07T07:57:23","guid":{"rendered":"http:\/\/tamatoyaku.com\/b\/?p=447"},"modified":"2018-07-07T16:57:23","modified_gmt":"2018-07-07T07:57:23","slug":"447","status":"publish","type":"post","link":"https:\/\/p-0.me\/b\/p\/447\/","title":{"rendered":"PIC\u306eMPLAB Harmony\u3068C#(.NET Framework)\u306eSystem.Security.Cryptography\u306e\u9593\u3067ECDH\u9375\u5171\u6709\u3092\u884c\u3046"},"content":{"rendered":"<p>\u4eca\u56de\u306e\u5352\u7814\u3067\u306fPIC\u3068PC\u306e\u9593\u3067\u6697\u53f7\u5316\u3057\u305f\u30c7\u30fc\u30bf\u306e\u3084\u308a\u53d6\u308a\u3092\u884c\u3044\u305f\u3044\uff0e\u6697\u53f7\u5316\u306b\u306fAES\u3092\u4f7f\u3044\u305f\u3044\u304c\uff0c\u6bce\u56de\u540c\u3058\u9375\u3092\u4f7f\u7528\u3059\u308b\u306e\u306f\u9375\u304c\u6f0f\u6d29\u3057\u305f\u3068\u304d\u306e\u3053\u3068\u3092\u8003\u3048\u308b\u3068\u597d\u307e\u3057\u304f\u306a\u3044\uff0e\u305d\u306e\u305f\u3081\u9375\u5171\u6709\u3092\u884c\u3046\uff0e\u306a\u304a\uff0c\u9375\u5171\u6709\u306b\u306fECDH\u9375\u5171\u6709\u3092\u4f7f\u7528\u3059\u308b\uff0e<br \/>\n<!--more--><br \/>\n&nbsp;<br \/>\nPIC\u3067ECDH\u9375\u5171\u6709\u3092\u4f7f\u3046\u305f\u3081\u306b\u306fMPLAB Harmony\u306e\u4e2d\u306eCrypto Library\u306b\u5165\u3063\u3066\u3044\u308bCRYPT_ECC_DHE_SharedSecretMake\u95a2\u6570\u3092\u4f7f\u7528\u3059\u308b\uff0e\u4e00\u65b9\uff0cPC\u5074\u3067\u306f\u4eca\u56de\u306fWindows\u4e0a\u306e\u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3068\u3057\u3066\u4f5c\u6210\u3059\u308b\u306e\u3067\uff0c.NET Framework 4.7\u4ee5\u964d\u306b\u8ffd\u52a0\u3055\u308c\u305f<span class=\"selflink\">System.Security.Cryptography.ECDiffieHellmanCng\u30af\u30e9\u30b9\u3092\u4f7f\u7528\u3059\u308b\uff0e<\/span><br \/>\n&nbsp;<br \/>\n\u3057\u304b\u3057\uff0c\u4e0a\u8a18\u306e2\u3064\u306e\u9593\u3067\u306f\u516c\u958b\u9375\u306e\u5171\u6709\u306b\u4e92\u63db\u6027\u304c\u306a\u3044\uff0e<br \/>\nHarmony\u306e\u65b9\u3067\u306fImport\/Export\u304c\u7528\u610f\u3055\u308c\u3066\u304a\u308a\uff0c\u3053\u308c\u306fX9.63(RFC5480\u3068\u3082\u540c\u3058)\u3067\u3042\u308b\u00a0 04||Hex(X)||Hex(Y)\u00a0 \u3068\u3044\u3046\u5f62\u5f0f\u3092\u4f7f\u3046\uff0e\u306a\u304a\uff0c04\u306f\u975e\u5727\u7e2e\uff0cHex(X)\u306f\u70b9\u306eX\u5ea7\u6a19\uff0cHex(Y)\u306f\u70b9\u306eY\u5ea7\u6a19\u3092\u8868\u3059\uff0e\u307e\u305f\uff0c.NET\u306e\u65b9\u3067\u306fSHA256\u3067\u30cf\u30c3\u30b7\u30e5\u3092\u304b\u3051\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u306e\u3067\uff0c\u3053\u3061\u3089\u306f\u624b\u52d5\u3067SHA256\u3092\u304b\u3051\u308b\uff0e<br \/>\n\u4e00\u65b9\u3067.NET\u306e\u65b9\u3067\u306fECDiffieHellmanCng\u30af\u30e9\u30b9\u306bExportParameters\u30e1\u30bd\u30c3\u30c9\u304c\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\uff0e\u51fa\u529b\u3055\u308c\u308bECParameters\u69cb\u9020\u4f53\u306b\u306fQ\u3068\u3044\u3046\u516c\u958b\u9375\u3092\u8868\u3059ECPoint\u69cb\u9020\u4f53\u304c\u5165\u3063\u3066\u304a\u308a\uff0cQ\u306e\u4e2d\u306b\u306fX,Y\u3068\u3044\u3046\u5ea7\u6a19\u3092\u8868\u3059byte[]\u578b\u306e\u5909\u6570\u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\uff0e\u3053\u306eX,Y\u3092\u5148\u7a0b\u306eX9.63\u5f62\u5f0f\u306b\u6574\u5f62\u3057\u3066PIC\u5074\u306b\u6e21\u305b\u3070\uff0cPIC\u5074\u306f\u305d\u308c\u3092Import\u3059\u308b\u3060\u3051\u3067\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u308b\uff0e\u306a\u304a\uff0cImport\u306b\u3064\u3044\u3066\u306f\u4ee5\u4e0b\u306e\u624b\u9806\u3067\u884c\u3046\uff0e<br \/>\n1.ECDiffieHellmanCng\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9(A\u3068\u3059\u308b)\u3092\u751f\u6210\u3057\uff0cGenerateKey\u30e1\u30bd\u30c3\u30c9\u3067\u9069\u5f53\u306a\u9375\u3092\u751f\u6210\u3059\u308b\uff0e<br \/>\n2.ECParameters\u69cb\u9020\u4f53\u306e\u5909\u6570(B\u3068\u3059\u308b)\u3092\u751f\u6210\u3057\uff0cQ\u306e\u4e2d\u306b\u3042\u308bX,Y\u3092PIC\u5074\u304b\u3089\u53d7\u3051\u53d6\u3063\u305f\u3082\u306e\u306b\u5909\u66f4\u3059\u308b\uff0e<br \/>\n3. A\u3067ImportParameters\u30e1\u30bd\u30c3\u30c9\u3092\u7528\u3044\u3066B\u3092Import\u3059\u308b\uff0e<br \/>\n&nbsp;<br \/>\n&nbsp;<br \/>\n\u5b9f\u969b\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\uff0e\u306a\u304a\uff0c\u629c\u304d\u51fa\u3057\u3066\u66f8\u304f\u305f\u3081\u52d5\u4f5c\u78ba\u8a8d\u306f\u3057\u3066\u3044\u306a\u3044\uff0e<br \/>\n&nbsp;<br \/>\nPIC<\/p>\n<pre class=\"lang:c decode:true \">case APP_STATE_KEY_EXCHANGE:\n    switch(appData.keState){\n        case KE_STATE_INIT:\n            CRYPT_RNG_Initialize(&amp;appData.kePicRngCtx);\n            CRYPT_RNG_Initialize(&amp;appData.kePcRngCtx);\n            CRYPT_ECC_Initialize(&amp;appData.kePicEccCtx);\n            CRYPT_ECC_Initialize(&amp;appData.kePcEccCtx);\n            CRYPT_ECC_DHE_KeyMakeEx(&amp;appData.kePicEccCtx,&amp;appData.kePicRngCtx,32,ECC_SECP256R1);\n            CRYPT_ECC_DHE_KeyMakeEx(&amp;appData.kePcEccCtx,&amp;appData.kePcRngCtx,32,ECC_SECP256R1);\n            CRYPT_ECC_PublicExport(&amp;appData.kePicEccCtx,appData.kePicPubkey,sizeof(appData.kePicPubkey),&amp;appData.kePubkeySize);\n            appData.keState=KE_STATE_PUBKEY_SEND;\n            break;\n        case KE_STATE_PUBKEY_SEND:\n            appData.isWriteComplete = false;\n            USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0,\n                    &amp;appData.writeTransferHandle, appData.kePicPubkey,appData.kePubkeySize,\n                    USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);\n            appData.keState=KE_STATE_WAIT_SEND_COMPLETE;\n            break;\n        case KE_STATE_WAIT_SEND_COMPLETE:\n            if(appData.isWriteComplete == true)\n            {\n                appData.keState = KE_STATE_PUBKEY_RECEIVE;\n            }\n            break;\n        case KE_STATE_PUBKEY_RECEIVE:\n            if(appData.isReadComplete == true)\n            {\n                appData.isReadComplete = false;\n                appData.readTransferHandle =  USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;\n                USB_DEVICE_CDC_Read (USB_DEVICE_CDC_INDEX_0,\n                    &amp;appData.readTransferHandle, appData.kePcPubkey+appData.keReceivedPcPubkeySize,\n                    128);\n                appData.keReceivedPcPubkeySize+=appData.numBytesRead;\n                if(appData.readTransferHandle == USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID)\n                {\n                    appData.state = APP_STATE_ERROR;\n                    break;\n                }\n                if(appData.keReceivedPcPubkeySize&gt;=65){\n                    CRYPT_ECC_PublicImport(&amp;appData.kePcEccCtx,appData.kePcPubkey,appData.kePubkeySize);\n                    CRYPT_ECC_DHE_SharedSecretMake(&amp;appData.kePicEccCtx,&amp;appData.kePcEccCtx,appData.keSharedSecret,sizeof(appData.keSharedSecret),&amp;appData.keSharedSecretSize);\n                    appData.keSharedSecretSize;\n                    sizeof(appData.keSharedSecret);\n                    appData.keState=KE_STATE_CALC_SHA256;\n                }\n            }\n            break;\n        case KE_STATE_CALC_SHA256:\n            CRYPT_SHA256_Initialize(&amp;appData.sha256Ctx);\n            CRYPT_SHA256_DataAdd(&amp;appData.sha256Ctx,appData.keSharedSecret,appData.keSharedSecretSize);\n            CRYPT_SHA256_Finalize(&amp;appData.sha256Ctx,appData.shaSharedSecret);\n            appData.keState=KE_STATE_COMPLETE;\n            break;\n        case KE_STATE_COMPLETE:\n            memcpy(appData.aesKey,appData.shaSharedSecret,AES_BLOCK_SIZE);\n            memcpy(appData.aesIv ,\"1234567890abcdef\",AES_BLOCK_SIZE);\n            CRYPT_AES_KeySet(&amp;appData.aesCtx,appData.aesKey,AES_BLOCK_SIZE,appData.aesIv,CRYPT_AES_ENCRYPTION);\n            appData.state = APP_STATE_SCHEDULE_READ;\n            break;\n        default:\n            appData.state = APP_STATE_SCHEDULE_READ;\n            break;\n    }\n}\n<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<br \/>\nPC<\/p>\n<pre class=\"lang:c# decode:true \">ECCurve curve = ECCurve.CreateFromFriendlyName(\"secp256r1\");\nECDiffieHellmanCng pc = new ECDiffieHellmanCng();\nECDiffieHellmanCng pic = new ECDiffieHellmanCng();\npc.GenerateKey(curve);\npic.GenerateKey(curve);\nECParameters pcParam = pc.ExportParameters(true);\nECParameters picParam = pc.ExportParameters(false);\n\/\/\u3053\u3053\u304b\u3089PC\u306e\u516c\u958b\u9375\u3092\u9001\u308b\u51e6\u7406\nbyte[] X = pcParam.Q.X;\nbyte[] Y = pcParam.Q.Y;\nbyte[] X963 = { 0x04 };\nX963 = Enumerable.Concat(X963, X).ToArray();\nX963 = Enumerable.Concat(X963, Y).ToArray();\nConsole.WriteLine(\"\\npcPubkey  : \" + ToHexString(X963));\nConsole.WriteLine(\"pcX  : \" + ToHexString(pcParam.Q.X));\nConsole.WriteLine(\"pcY  : \" + ToHexString(pcParam.Q.Y));\n\/\/\u2193SerialPort\u3067\u9001\u308b\nspp.WriteData(X963);\n\/\/\u3053\u3053\u307e\u3067PC\u306e\u516c\u958b\u9375\u3092\u9001\u308b\u51e6\u7406\n\/\/\u3053\u3053\u304b\u3089PIC\u306e\u516c\u958b\u9375\u3092\u53d7\u3051\u53d6\u308b\u51e6\u7406\npicParam.Q.X = picX;\npicParam.Q.Y = picY;\npic.ImportParameters(picParam);\n\/\/\u3053\u3053\u307e\u3067PIC\u306e\u516c\u958b\u9375\u3092\u53d7\u3051\u53d6\u308b\u51e6\u7406\npc.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;\npc.HashAlgorithm = CngAlgorithm.Sha256;\nbyte[] sharedSecret = pc.DeriveKeyMaterial(pic.PublicKey);\nConsole.WriteLine(\"\\npcSharedSecret  : \" + ToHexString(sharedSecret));\n<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306e\u5352\u7814\u3067\u306fPIC\u3068PC\u306e\u9593\u3067\u6697\u53f7\u5316\u3057\u305f\u30c7\u30fc\u30bf\u306e\u3084\u308a\u53d6\u308a\u3092\u884c\u3044\u305f\u3044\uff0e\u6697\u53f7\u5316\u306b\u306fAES\u3092\u4f7f\u3044\u305f\u3044\u304c\uff0c\u6bce\u56de\u540c\u3058\u9375\u3092\u4f7f\u7528\u3059\u308b\u306e\u306f\u9375\u304c\u6f0f\u6d29\u3057\u305f\u3068\u304d\u306e\u3053\u3068\u3092\u8003\u3048\u308b\u3068\u597d\u307e\u3057\u304f\u306a\u3044\uff0e\u305d\u306e\u305f\u3081\u9375\u5171\u6709\u3092\u884c\u3046\uff0e\u306a\u304a\uff0c\u9375\u5171\u6709\u306b\u306fECDH\u9375\u5171 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-447","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/posts\/447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/comments?post=447"}],"version-history":[{"count":0,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/posts\/447\/revisions"}],"wp:attachment":[{"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/media?parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/categories?post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/tags?post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}