_aes_AES.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "_aes_AES.h"
  2. #include "mbedtls/aes.h"
  3. enum {
  4. MODE_ECB,
  5. MODE_CBC,
  6. } pika_aes_t;
  7. void _aes_AES___init__(PikaObj* self) {
  8. obj_setInt(self, "MODE_ECB", MODE_ECB);
  9. obj_setInt(self, "MODE_CBC", MODE_CBC);
  10. }
  11. void _aes_AES_new(PikaObj* self, Arg* password, int mode, Arg* iv) {
  12. ArgType t;
  13. mbedtls_aes_context context;
  14. t = arg_getType(password);
  15. if (ARG_TYPE_BYTES != t) {
  16. obj_setErrorCode(self, -2); // io error
  17. }
  18. t = arg_getType(iv);
  19. if (ARG_TYPE_NONE != t) {
  20. if (ARG_TYPE_BYTES != t) {
  21. obj_setErrorCode(self, -2); // io error
  22. } else {
  23. size_t len = arg_getBytesSize(iv);
  24. if (len != 16) {
  25. obj_setErrorCode(self, -2); // io error
  26. } else {
  27. obj_setBytes(self, "_iv", arg_getBytes(iv), len);
  28. }
  29. }
  30. }
  31. obj_setBytes(self, "_password", arg_getBytes(password),
  32. arg_getBytesSize(password));
  33. obj_setInt(self, "_mode", mode);
  34. mbedtls_aes_init(&context);
  35. obj_setStruct(self, "context", context);
  36. }
  37. Arg* _aes_AES_encrypt(PikaObj* self, Arg* msg) {
  38. ArgType t;
  39. t = arg_getType(msg);
  40. if (ARG_TYPE_BYTES != t) {
  41. obj_setErrorCode(self, -2); // io error
  42. }
  43. uint8_t* data = arg_getBytes(msg);
  44. uint8_t data_len = arg_getBytesSize(msg);
  45. uint8_t mode = obj_getInt(self, "_mode");
  46. void* context = obj_getStruct(self, "context");
  47. uint8_t* password = obj_getBytes(self, "_password");
  48. uint8_t password_len = obj_getBytesSize(self, "_password");
  49. uint8_t* iv = obj_getBytes(self, "_iv");
  50. uint8_t iv_in[16];
  51. if (obj_getBytes(self, "_buff") != NULL) {
  52. obj_removeArg(self, "_buff");
  53. }
  54. obj_setBytes(self, "_buff", NULL, data_len * sizeof(uint8_t));
  55. uint8_t* buff = obj_getBytes(self, "_buff");
  56. if (mode == MODE_CBC) {
  57. if (iv != NULL) {
  58. __platform_memcpy(iv_in, iv, 16);
  59. } else {
  60. obj_setErrorCode(self, -2); // io error
  61. }
  62. }
  63. mbedtls_aes_setkey_enc((mbedtls_aes_context*)context, password,
  64. password_len * 8);
  65. switch (mode) {
  66. case MODE_ECB:
  67. mbedtls_aes_crypt_ecb((mbedtls_aes_context*)context,
  68. MBEDTLS_AES_ENCRYPT, data, buff);
  69. break;
  70. case MODE_CBC:
  71. mbedtls_aes_crypt_cbc((mbedtls_aes_context*)context,
  72. MBEDTLS_AES_ENCRYPT, data_len, iv_in, data,
  73. buff);
  74. break;
  75. default:
  76. obj_setSysOut(self, "[%s]not support mode", __FUNCTION__);
  77. obj_setErrorCode(self, -3); // not support now
  78. break;
  79. }
  80. return arg_newBytes(buff, data_len);
  81. }
  82. Arg* _aes_AES_decrypt(PikaObj* self, Arg* msg) {
  83. ArgType t;
  84. t = arg_getType(msg);
  85. if (ARG_TYPE_BYTES != t) {
  86. obj_setErrorCode(self, -2); // io error
  87. }
  88. uint8_t* data = arg_getBytes(msg);
  89. uint8_t data_len = arg_getBytesSize(msg);
  90. void* context = obj_getStruct(self, "context");
  91. uint8_t mode = obj_getInt(self, "_mode");
  92. uint8_t* password = obj_getBytes(self, "_password");
  93. uint8_t password_len = obj_getBytesSize(self, "_password");
  94. uint8_t* iv = obj_getBytes(self, "_iv");
  95. uint8_t iv_in[16];
  96. if (obj_getBytes(self, "_buff") != NULL) {
  97. obj_removeArg(self, "_buff");
  98. }
  99. obj_setBytes(self, "_buff", NULL, data_len * sizeof(uint8_t));
  100. uint8_t* buff = obj_getBytes(self, "_buff");
  101. if (mode == MODE_CBC) {
  102. if (iv != NULL) {
  103. __platform_memcpy(iv_in, iv, 16);
  104. } else {
  105. obj_setErrorCode(self, -2); // io error
  106. }
  107. }
  108. mbedtls_aes_setkey_dec((mbedtls_aes_context*)context, password,
  109. password_len * 8);
  110. switch (mode) {
  111. case MODE_ECB:
  112. mbedtls_aes_crypt_ecb((mbedtls_aes_context*)context,
  113. MBEDTLS_AES_DECRYPT, data, buff);
  114. break;
  115. case MODE_CBC:
  116. mbedtls_aes_crypt_cbc((mbedtls_aes_context*)context,
  117. MBEDTLS_AES_DECRYPT, data_len, iv_in, data,
  118. buff);
  119. break;
  120. default:
  121. obj_setSysOut(self, "[%s]not support mode", __FUNCTION__);
  122. obj_setErrorCode(self, -3); // not support now
  123. break;
  124. }
  125. return arg_newBytes(buff, data_len);
  126. }