aes.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // aes.h
  3. // nuSDKService
  4. //
  5. // Created by W.YudoAji on 29/06/20.
  6. // Copyright © 2020 newuniverse. All rights reserved.
  7. //
  8. #ifndef _AES_H_
  9. #define _AES_H_
  10. //#include <stdint.h>
  11. //#include <stdio.h>
  12. // #define the macros below to 1/0 to enable/disable the mode of operation.
  13. //
  14. // CBC enables AES encryption in CBC-mode of operation.
  15. // CTR enables encryption in counter-mode.
  16. // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
  17. // The #ifndef-guard allows it to be configured before #include'ing or at compile time.
  18. #ifndef CBC
  19. #define CBC 1
  20. #endif
  21. //#ifndef ECB
  22. //#define ECB 1
  23. //#endif
  24. //
  25. //#ifndef CTR
  26. //#define CTR 1
  27. //#endif
  28. #define AES256 1
  29. //#define AES128 1
  30. //#define AES192 1
  31. #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only
  32. #if defined(AES256) && (AES256 == 1)
  33. #define AES_KEYLEN 32
  34. #define AES_keyExpSize 240
  35. //#elif defined(AES192) && (AES192 == 1)
  36. //#define AES_KEYLEN 24
  37. //#define AES_keyExpSize 208
  38. //#else
  39. //#define AES_KEYLEN 16 // Key length in bytes
  40. //#define AES_keyExpSize 176
  41. #endif
  42. //struct AES_ctx {
  43. // uint8_t RoundKey[AES_keyExpSize];
  44. //#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
  45. // uint8_t Iv[AES_BLOCKLEN];
  46. //#endif
  47. //};
  48. //void AES_init_ctx(struct AES_ctx *ctx, const uint8_t *key);
  49. //
  50. //#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
  51. //
  52. //void AES_init_ctx_iv(struct AES_ctx *ctx, const uint8_t *key, const uint8_t *iv);
  53. //
  54. //void AES_ctx_set_iv(struct AES_ctx *ctx, const uint8_t *iv);
  55. //
  56. //#endif
  57. //
  58. //#if defined(ECB) && (ECB == 1)
  59. //
  60. //// buffer size is exactly AES_BLOCKLEN bytes;
  61. //// you need only AES_init_ctx as IV is not used in ECB
  62. //// NB: ECB is considered insecure for most uses
  63. //void AES_ECB_encrypt(const struct AES_ctx *ctx, uint8_t *buf);
  64. //
  65. //void AES_ECB_decrypt(const struct AES_ctx *ctx, uint8_t *buf);
  66. //
  67. //#endif // #if defined(ECB) && (ECB == !)
  68. //
  69. //
  70. //#if defined(CBC) && (CBC == 1)
  71. //
  72. //// buffer size MUST be mutile of AES_BLOCKLEN;
  73. //// Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
  74. //// NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
  75. //// no IV should ever be reused with the same key
  76. //void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t *buf, uint32_t length);
  77. //
  78. //void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t *buf, uint32_t length);
  79. //
  80. //#endif // #if defined(CBC) && (CBC == 1)
  81. //
  82. //
  83. //#if defined(CTR) && (CTR == 1)
  84. //
  85. //// Same function for encrypting as for decrypting.
  86. //// IV is incremented for every block, and used after encryption as XOR-compliment for output
  87. //// Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
  88. //// NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
  89. //// no IV should ever be reused with the same key
  90. //void AES_CTR_xcrypt_buffer(struct AES_ctx *ctx, uint8_t *buf, uint32_t length);
  91. //
  92. //#endif // #if defined(CTR) && (CTR == 1)
  93. // uint8_t iAx(uint8_t *abKey, const uint8_t len);
  94. void initCND(void);
  95. uint16_t abN(uint8_t *inpBytes, const uint16_t nInpLen, uint8_t *outBytes, uint8_t *aUint8Key);
  96. uint16_t abD(uint8_t *inpBytes, const uint16_t nInpLen, uint8_t *outBytes, uint8_t *aUint8Key, const uint8_t nRemoveTI);
  97. #endif //_AES_H_