nRF52840 CryptoCell 310の使い方メモ①

はじめに

以前、CryptoCell 310を使ったサンプルアプリケーションを解析しました。

tomo-wait-for-it-yuki.hatenablog.com

もう少し真面目に使い方を知る必要があるので、nRF5 SDKAPIリファレンスを読んでいきます。 nRF5 SDK CryptoCell APIリファレンスは以下のページです。

[https://www.nordicsemi.com/DocLib/Content/SDK_Doc/nRF5_SDK/v15-2-0/groupcryptocellapi:embed:cite]

The CryptoCell API is a proprietary and confidential API owned by ARM Limited. Nordic Semiconductor is granted rights to distribute this API as part of nRF5 SDKs. See the license information at external\nrf_cc310\license.txt if you plan to use this API.

とあるので、APIライセンスを確認しておきましょう。 ちなみに、上のパスは誤植で、下記のパスが正しいです。

|> external/nrf_cc310/lib/license.txt

ライセンス

ライセンスファイルの内容です。

ARM Object Code and Header Files License 
Version 1.0

Redistribution.

Redistribution and use of object code, header files, and 
documentation, without modification, are permitted provided that the following 
conditions are met: 
1) Redistributions must reproduce the above copyright notice and the
   following disclaimer in the documentation and/or other materials
   provided with the distribution. 
2) Unless to the extent explicitly permitted by law, no reverse 
   engineering, decompilation, or disassembly of is permitted. 
3) Redistribution and use is permitted solely for the purpose of 
   developing or executing applications that are targeted for use 
   on an ARM-based product. 

ざっくりまとめます。

  • 再頒布にあたっては、バイナリおよびヘッダファイルに変更を加えない限り許可されます。
  • copyright noticeとdisclaimerを同梱する必要があります。
  • 法的な許可がない限り、リバースエンジニアリングは禁止です。
  • ARMプロダクトでしか再頒布および利用できません。

なるほど。ARMベースのプロダクトでしか利用を許可しない、というのは面白いですね。

あ、リバースエンジニアリング禁止されているので、前回記事のシンボル確認したのは、どうなんでしょうね…。 記事からは内容を削除しておきました。

nrf_crypto library

今回は、利用する予定がないのですが、nRF5 SDKでは、暗号化用のライブラリが提供されています。 暗号化ライブラリでは、大きなメモリ領域を扱うため、メモリ管理が必要になってきます。 そのあたりの話が、下記に記載されています。

www.nordicsemi.com

NRF_CRYPTO_ALLOCがメモリをアロケートするマクロです。 このマクロは、以下の5つのうち、どれかを実装します。

    • Default configuration
    • User macros
    • Allocating memory on the stack (alloca)

Default configurationでは、スタックにメモリを確保します。注意点としては、確保領域のポインタを、関数の返り値として返せないことです。

CryptoCell初期化

まずは、CryptoCellを有効化する必要があります。 これは、メモリマップドレジスタの操作で有効化できます。

www.nordicsemi.com

CryptoCellのベースアドレスは0x5002A000で、ENABLEのオフセットは0x500です。ここに、1を書き込むことで、CryptoCellが有効化されます。 これ以外は、ハードウェアを直接制御せず、Nordicから配布されているバイナリのAPIを使います。

CryptoCell library firmware初期化

SaSi_LibInit APIを利用して、library firmwareを初期化します。

This function Perform global initialization of the ARM CryptoCell 3xx runtime library; it must be called once per ARM CryptoCell for 3xx cold boot cycle.

CryptoCell Random Generator

乱数生成器の初期化関数CRYS_RndInitは、2つの引数を取ります。

CRYSError_t CRYS_RndInit(void *rnd_ctx, CRYS_RND_WorkBuff_t *rndWorkBuff_ptr )

第一引数は、おそらくコンテキストの状態を示すCRYS_RND_State_tで、第二引数ライブラリが利用するバッファCRYS_RND_WorkBuff_tです。 これら2つの引数は、他の乱数生成APIでも利用します。Initでは、CRYS_RND_State_tを初期化してくれます。

CRYS_RND_State_tは、firmwareの状態を表すデータ構造です。マイコンが動作している間中、保持しておく必要があります。 CRYS_RND_WorkBuff_tは、ユーザーが確保したint32_t[1528] (約6KB)のメモリ領域です。

この初期化が完了すれば、CRYS_RND_GenerateVectorなどのAPIが呼べるはずです。

APIが返すエラーは次のように定義されています。

エラーコード一覧