crypto_openssl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #ifndef CONFIG
  2. #define CONFIG "config.h"
  3. #endif // CONFIG
  4. #include CONFIG
  5. #if defined(_CRYPTO_OPENSSL)
  6. #include "crypto.h"
  7. #include "crypto_openssl.h" // Required for Eclipse only
  8. #include <stdint.h>
  9. #include "endian.h"
  10. #ifndef _OPENSSL_NO_HMAC
  11. int Sha256HmacInit_OpenSSL(HMAC_CTX *c, const void *k, int l)
  12. {
  13. HMAC_CTX_init(c);
  14. #if OPENSSL_VERSION_NUMBER >= 0x10000000L
  15. int result =
  16. #else
  17. int result = TRUE;
  18. #endif
  19. HMAC_Init_ex(c, k, l, EVP_sha256(), NULL);
  20. return result;
  21. }
  22. int Sha256HmacFinish_OpenSSL(HMAC_CTX *c, unsigned char *h, unsigned int *l)
  23. {
  24. #if OPENSSL_VERSION_NUMBER >= 0x10000000L
  25. int result =
  26. #else
  27. int result = !0;
  28. #endif
  29. HMAC_Final(c, h, l);
  30. HMAC_CTX_cleanup(c);
  31. return result;
  32. }
  33. int_fast8_t Sha256Hmac(BYTE* key, BYTE* restrict data, DWORD len, BYTE* restrict hmac)
  34. {
  35. HMAC_CTX Ctx;
  36. # if OPENSSL_VERSION_NUMBER >= 0x10000000L
  37. return
  38. Sha256HmacInit_OpenSSL(&Ctx, key, 16) &&
  39. HMAC_Update(&Ctx, data, len) &&
  40. Sha256HmacFinish_OpenSSL(&Ctx, hmac, NULL);
  41. # else // OpenSSL 0.9.x
  42. Sha256HmacInit_OpenSSL(&Ctx, key, 16);
  43. HMAC_Update(&Ctx, data, len);
  44. Sha256HmacFinish_OpenSSL(&Ctx, hmac, NULL);
  45. return TRUE;
  46. # endif
  47. }
  48. #else // _OPENSSL_NO_HMAC (some routers have OpenSSL without support for HMAC)
  49. int _Sha256HmacInit(Sha256HmacCtx *Ctx, BYTE *key, size_t klen)
  50. {
  51. BYTE IPad[64];
  52. unsigned int i;
  53. memset(IPad, 0x36, sizeof(IPad));
  54. memset(Ctx->OPad, 0x5C, sizeof(Ctx->OPad));
  55. if ( klen > 64 )
  56. {
  57. BYTE *temp = (BYTE*)alloca(32);
  58. SHA256(key, klen, temp);
  59. klen = 32;
  60. key = temp;
  61. }
  62. for (i = 0; i < klen; i++)
  63. {
  64. IPad[ i ] ^= key[ i ];
  65. Ctx->OPad[ i ] ^= key[ i ];
  66. }
  67. SHA256_Init(&Ctx->ShaCtx);
  68. return SHA256_Update(&Ctx->ShaCtx, IPad, sizeof(IPad));
  69. }
  70. int _Sha256HmacUpdate(Sha256HmacCtx *Ctx, BYTE *data, size_t len)
  71. {
  72. int rc = SHA256_Update(&Ctx->ShaCtx, data, len);
  73. return rc;
  74. }
  75. int _Sha256HmacFinish(Sha256HmacCtx *Ctx, BYTE *hmac, void* dummy)
  76. {
  77. BYTE temp[32];
  78. SHA256_Final(temp, &Ctx->ShaCtx);
  79. SHA256_Init(&Ctx->ShaCtx);
  80. SHA256_Update(&Ctx->ShaCtx, Ctx->OPad, sizeof(Ctx->OPad));
  81. SHA256_Update(&Ctx->ShaCtx, temp, sizeof(temp));
  82. return SHA256_Final(hmac, &Ctx->ShaCtx);
  83. }
  84. int_fast8_t Sha256Hmac(BYTE* key, BYTE* restrict data, DWORD len, BYTE* restrict hmac)
  85. {
  86. Sha256HmacCtx Ctx;
  87. _Sha256HmacInit(&Ctx, key, 16);
  88. _Sha256HmacUpdate(&Ctx, data, len);
  89. _Sha256HmacFinish(&Ctx, hmac, NULL);
  90. return TRUE;
  91. }
  92. #endif
  93. #if defined(_USE_AES_FROM_OPENSSL)
  94. void TransformOpenSslEncryptKey(AES_KEY *k, const AesCtx *const Ctx)
  95. {
  96. uint32_t *rk_OpenSSL = k->rd_key, *rk_vlmcsd = (uint32_t*)Ctx->Key;
  97. k->rounds = Ctx->rounds;
  98. for (; rk_OpenSSL < k->rd_key + ((k->rounds + 1) << 2); rk_OpenSSL++, rk_vlmcsd++)
  99. {
  100. #ifdef _OPENSSL_SOFTWARE
  101. *rk_OpenSSL = BE32(*rk_vlmcsd);
  102. #else
  103. *rk_OpenSSL = LE32(*rk_vlmcsd);
  104. #endif
  105. }
  106. }
  107. void TransformOpenSslDecryptKey(AES_KEY *k, const AesCtx *const Ctx)
  108. {
  109. uint_fast8_t i;
  110. #ifdef _DEBUG_OPENSSL
  111. AES_set_decrypt_key((BYTE*)Ctx->Key, 128, k);
  112. errorout("Correct V5 round key:");
  113. for (i = 0; i < (Ctx->rounds + 1) << 4; i++)
  114. {
  115. if (!(i % 16)) errorout("\n");
  116. if (!(i % 4)) errorout(" ");
  117. errorout("%02X", ((BYTE*)(k->rd_key))[i]);
  118. }
  119. errorout("\n");
  120. #endif
  121. k->rounds = Ctx->rounds;
  122. /* invert the order of the round keys blockwise (1 Block = AES_BLOCK_SIZE = 16): */
  123. for (i = 0; i < (Ctx->rounds + 1) << 2; i++)
  124. {
  125. #ifdef _OPENSSL_SOFTWARE
  126. k->rd_key[((Ctx->rounds-(i >> 2)) << 2) + (i & 3)] = BE32(Ctx->Key[i]);
  127. #else
  128. k->rd_key[((Ctx->rounds-(i >> 2)) << 2) + (i & 3)] = LE32(Ctx->Key[i]);
  129. #endif
  130. }
  131. /* apply the inverse MixColumn transform to all round keys but the first and the last: */
  132. uint32_t *rk = k->rd_key + 4;
  133. for (i = 0; i < (Ctx->rounds - 1); i++)
  134. {
  135. MixColumnsR((BYTE*)(rk + (i << 2)));
  136. }
  137. #ifdef _DEBUG_OPENSSL
  138. errorout("Real round key:");
  139. for (i = 0; i < (Ctx->rounds + 1) << 4; i++)
  140. {
  141. if (!(i % 16)) errorout("\n");
  142. if (!(i % 4)) errorout(" ");
  143. errorout("%02X", ((BYTE*)(k->rd_key))[i]);
  144. }
  145. errorout("\n");
  146. #endif
  147. }
  148. static BYTE NullIV[AES_BLOCK_SIZE + 8]; // OpenSSL may overwrite bytes behind IV
  149. void AesEncryptCbc(const AesCtx *const Ctx, BYTE *iv, BYTE *data, size_t *len)
  150. {
  151. AES_KEY k;
  152. // OpenSSL overwrites IV plus 4 bytes
  153. BYTE localIV[24]; // 4 spare bytes for safety
  154. if (iv) memcpy(localIV, iv, AES_BLOCK_SIZE);
  155. // OpenSSL Low-Level APIs do not pad. Could use EVP API instead but needs more code to access the expanded key
  156. uint_fast8_t pad = (~*len & (AES_BLOCK_SIZE - 1)) + 1;
  157. #if defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8) // gcc 4.8 memset bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56977
  158. size_t i;
  159. for (i = 0; i < pad; i++) data[*len + i] = pad;
  160. #else
  161. memset(data + *len, pad, pad);
  162. #endif
  163. *len += pad;
  164. memset(NullIV, 0, sizeof(NullIV));
  165. TransformOpenSslEncryptKey(&k, Ctx);
  166. AES_cbc_encrypt(data, data, *len, &k, iv ? localIV : NullIV, AES_ENCRYPT);
  167. }
  168. void AesDecryptBlock(const AesCtx *const Ctx, BYTE *block)
  169. {
  170. AES_KEY k;
  171. TransformOpenSslDecryptKey(&k, Ctx);
  172. AES_decrypt(block, block, &k);
  173. }
  174. #if defined(_CRYPTO_OPENSSL) && defined(_USE_AES_FROM_OPENSSL) && !defined(_OPENSSL_SOFTWARE)
  175. void AesEncryptBlock(const AesCtx *const Ctx, BYTE *block)
  176. {
  177. AES_KEY k;
  178. TransformOpenSslEncryptKey(&k, Ctx);
  179. AES_encrypt(block, block, &k);
  180. }
  181. #endif
  182. void AesDecryptCbc(const AesCtx *const Ctx, BYTE *iv, BYTE *data, size_t len)
  183. {
  184. AES_KEY k;
  185. memset(NullIV, 0, sizeof(NullIV));
  186. TransformOpenSslDecryptKey(&k, Ctx);
  187. AES_cbc_encrypt(data, data, len, &k, iv ? iv : NullIV, AES_DECRYPT);
  188. }
  189. #ifndef _OPENSSL_SOFTWARE
  190. void AesCmacV4(BYTE *Message, size_t MessageSize, BYTE *HashOut)
  191. {
  192. size_t i;
  193. BYTE hash[AES_BLOCK_BYTES];
  194. AesCtx Ctx;
  195. AES_KEY k;
  196. AesInitKey(&Ctx, AesKeyV4, FALSE, V4_KEY_BYTES);
  197. TransformOpenSslEncryptKey(&k, &Ctx);
  198. memset(hash, 0, sizeof(hash));
  199. memset(Message + MessageSize, 0, AES_BLOCK_BYTES);
  200. Message[MessageSize] = 0x80;
  201. for (i = 0; i <= MessageSize; i += AES_BLOCK_BYTES)
  202. {
  203. XorBlock(Message + i, hash);
  204. AES_encrypt(hash, hash, &k);
  205. }
  206. memcpy(HashOut, hash, AES_BLOCK_BYTES);
  207. }
  208. #endif // !_OPENSSL_SOFTWARE
  209. #endif // defined(_USE_AES_FROM_OPENSSL)
  210. #endif // _CRYPTO_OPENSSL