lua-jmutil.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #define JM_UTIL_LIB
  2. #include <lua.h>
  3. #include <lauxlib.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "sw_aes.h"
  8. #define FREE_PTR(ptr) \
  9. if (NULL != (ptr)) {\
  10. free (ptr);\
  11. (ptr) = NULL;\
  12. }
  13. static const unsigned int kAesKeySize = 16;
  14. static const unsigned int kAesIVSize = 16;
  15. static const char *
  16. buffer2lstring(lua_State *L, size_t *sz, int index) {
  17. const char * ptr;
  18. if (lua_isuserdata(L,index)) {
  19. ptr = (const char *)lua_touserdata(L,index);
  20. *sz = (size_t)luaL_checkinteger(L, index+1);
  21. } else {
  22. ptr = luaL_checklstring(L, index, sz);
  23. }
  24. return ptr;
  25. }
  26. char *AES_CBCDecrypt( const char * sSource, const uint32_t iSize,
  27. const char * sKey, uint32_t iKeySize,
  28. const char * sIv, uint32_t iIvSize)
  29. {
  30. // printf("\r\n ====================%d, %d, %d, %d, %d\r\n",!sSource,!sKey,iSize < kAesKeySize,iSize % kAesKeySize != 0,!poResult);
  31. if ( !sSource || !sKey || iSize < kAesKeySize || iSize % kAesKeySize != 0) return NULL;
  32. uint8_t * out = (uint8_t*)malloc(iSize);
  33. uint8_t *key = (uint8_t*)malloc(kAesKeySize);
  34. uint8_t *iv = (uint8_t*)malloc(kAesIVSize);
  35. memcpy( key, sKey, iKeySize > kAesKeySize ? kAesKeySize : iKeySize );
  36. memcpy( iv, sIv, iIvSize > kAesIVSize ? kAesIVSize : iIvSize );
  37. AES_CTX context;
  38. AES_set_key(&context, (const uint8_t *)key, (const uint8_t *)iv, AES_MODE_128);
  39. AES_convert_key(&context);
  40. AES_cbc_decrypt(&context, (const uint8_t *)sSource, out, iSize);
  41. // printf("\r\n -----------out[%s]\r\n", out);
  42. char * poResult = NULL;
  43. if( out[iSize-1] > 0 && out[iSize-1] <= kAesKeySize && (iSize - out[iSize-1]) > 0 )
  44. {
  45. int len = iSize - out[iSize-1];
  46. poResult = (char*)malloc(len);
  47. memcpy( poResult, out, len);
  48. }
  49. FREE_PTR(out);
  50. FREE_PTR(key);
  51. FREE_PTR(iv);
  52. return poResult;
  53. }
  54. static int
  55. lstr2lightuserdata(lua_State *L) {
  56. size_t len;
  57. const char * ptr = buffer2lstring(L, &len, 1);
  58. if (len >= 0x10000) {
  59. return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
  60. }
  61. uint8_t * buffer = malloc(len);
  62. buffer[0] = (len >> 8) & 0xff;
  63. buffer[1] = len & 0xff;
  64. memcpy(buffer, ptr, len);
  65. lua_pushlightuserdata(L, buffer);
  66. lua_pushinteger(L, len);
  67. return 2;
  68. }
  69. static int
  70. lwxdecryptdata(lua_State *L) {
  71. size_t sEncryptedDataSize, sIvSize, sSessionKeySize;
  72. const char * sEncryptedData = buffer2lstring(L, &sEncryptedDataSize, 1);
  73. // printf("\r\n sEncryptedData[%zu]\r\n",sEncryptedDataSize);
  74. const char * sIv = buffer2lstring(L, &sIvSize, 2);
  75. // printf("\r\n sIv[%zu]\r\n",sIvSize);
  76. const char * sSessionkey = buffer2lstring(L, &sSessionKeySize, 3);
  77. // printf("\r\n sSessionkey[%zu]\r\n",sSessionKeySize);
  78. char *res = AES_CBCDecrypt(sEncryptedData, sEncryptedDataSize, sSessionkey, sSessionKeySize, sIv, sIvSize);
  79. // printf("\r\n out[%s]\r\n", res);
  80. lua_pushstring(L, res);
  81. return 1;
  82. }
  83. LUAMOD_API int
  84. luaopen_jmutil(lua_State *L) {
  85. luaL_checkversion(L);
  86. luaL_Reg l[] = {
  87. { "str2lightuserdata", lstr2lightuserdata},
  88. { "wxdecryptdata", lwxdecryptdata},
  89. { NULL, NULL },
  90. };
  91. luaL_newlib(L,l);
  92. return 1;
  93. }