1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32でAES暗号/ハッシュ生成する

Last updated at Posted at 2024-11-03

前回、以下の投稿で、ESP32でHMACを生成しました。

 ESP32でHMAC(SHA-256)を生成する

ついでに、SHA1/256のハッシュと、AES暗号/復号をやってみます。
前回同様に、ESP32のArduinoでは、mbedtlsを利用できるのでそれを使います。

Arduino/PlatformIOでの利用

main.cpp
long aes16_ecb_encrypt(const unsigned char *p_key, const unsigned char *p_input, unsigned char *p_output, long len)
{
  mbedtls_aes_context ctx;
  mbedtls_aes_init(&ctx);
  mbedtls_aes_setkey_enc(&ctx, p_key, 16 * 8);
  for(long i = 0 ; i < len ; i += 16){
    mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_ENCRYPT, &p_input[i], &p_output[i]);
  }
  mbedtls_aes_free(&ctx);

  return 0;
}

long aes16_cbc_encrypt(const unsigned char *p_key, const unsigned char *p_input, unsigned char *p_output, long len, unsigned char *p_iv)
{
  mbedtls_aes_context ctx;
  mbedtls_aes_init(&ctx);
  mbedtls_aes_setkey_enc(&ctx, p_key, 16 * 8);
  mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, len, p_iv, p_input, p_output);
  mbedtls_aes_free(&ctx);

  return 0;
}

long aes16_ecb_decrypt(const unsigned char *p_key, const unsigned char *p_input, unsigned char *p_output, long len)
{
  mbedtls_aes_context ctx;
  mbedtls_aes_init(&ctx);
  mbedtls_aes_setkey_enc(&ctx, p_key, 16 * 8);
  for(long i = 0 ; i < len ; i += 16){
    mbedtls_aes_crypt_ecb(&ctx, MBEDTLS_AES_DECRYPT, &p_input[i], &p_output[i]);
  }
  mbedtls_aes_free(&ctx);

  return 0;
}

long aes16_cbc_decrypt(const unsigned char *p_key, const unsigned char *p_input, unsigned char *p_output, long len, unsigned char *p_iv)
{
  mbedtls_aes_context ctx;
  mbedtls_aes_init(&ctx);
  mbedtls_aes_setkey_enc(&ctx, p_key, 16 * 8);
  mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_DECRYPT, len, p_iv, p_input, p_output);
  mbedtls_aes_free(&ctx);

  return 0;
}

long md5_create(const unsigned char *p_input, long input_len, unsigned char *p_digestResult)
{
  mbedtls_md_context_t ctx;
  mbedtls_md_init(&ctx);
  mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_MD5), 1);
  mbedtls_md_update(&ctx, p_input, input_len);
	mbedtls_md_finish(&ctx, p_digestResult); // 16 bytes

  return 0;
}

long sha256_create(const unsigned char *p_input, long input_len, unsigned char *p_digestResult)
{
  mbedtls_md_context_t ctx;
  mbedtls_md_init(&ctx);
  mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
  mbedtls_md_update(&ctx, p_input, input_len);
  mbedtls_md_finish(&ctx, p_digestResult); // 32 bytes

  return 0;
}

long hmac_sha256(const char *p_key, const char *p_payload, unsigned char *p_hmacResult)
{
  mbedtls_md_context_t ctx;
  mbedtls_md_init(&ctx);
  mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
  mbedtls_md_hmac_starts(&ctx, (const unsigned char*)p_key, strlen(p_key));
  mbedtls_md_hmac_update(&ctx, (const unsigned char*)p_payload, strlen(p_payload));
  mbedtls_md_hmac_finish(&ctx, p_hmacResult); // 32 bytes
  mbedtls_md_free(&ctx);

  return 0;
}

QuickJS for ESP32での利用

以下の、ESP32上のJavascsript環境でも実行できるようにしておきました。

実行例です。

main.js
import * as crypto from "Crypto";

console.log('start');

var result = crypto.hmacCreate(crypto.MD_SHA256, "This is Key", 'Hello World');
var array = new Uint8Array(result);
console.log(JSON.stringify(array));

var input = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
var result = crypto.mdCreate(crypto.MD_MD5, input);
var array = new Uint8Array(result);
console.log(JSON.stringify(array));

var iv = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
var key = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
var input = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]);
var result = crypto.aesCbcEncrypt(crypto.ENCRYPT, crypto.AES_KEY16, key, input, iv);
var array = new Uint8Array(result);
console.log(JSON.stringify(array));

console.log('end');

(参考) ESP32で動作するJavascript実行環境

ESP32で動作するJavascript実行環境を公開しています。

「電子書籍:M5StackとJavascriptではじめるIoTデバイス制御」

サポートサイト

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?