test_params.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. */
  7. #include "soc/soc_caps.h"
  8. #include "hal/sha_types.h"
  9. #if SOC_SHA_SUPPORTED
  10. #define PUT_UINT32_BE(n,b,i) \
  11. { \
  12. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  13. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  14. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  15. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  16. }
  17. #define PUT_UINT64_BE(n,b,i) \
  18. { \
  19. (b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
  20. (b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
  21. (b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
  22. (b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
  23. (b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
  24. (b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
  25. (b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
  26. (b)[(i) + 7] = (unsigned char) ( (n) ); \
  27. }
  28. #define BUFFER_SZ 1030 // NB: not an exact multiple of SHA block size
  29. static inline size_t block_length(esp_sha_type type)
  30. {
  31. switch (type) {
  32. case SHA1:
  33. case SHA2_224:
  34. case SHA2_256:
  35. return 64;
  36. #if SOC_SHA_SUPPORT_SHA384
  37. case SHA2_384:
  38. #endif
  39. #if SOC_SHA_SUPPORT_SHA512
  40. case SHA2_512:
  41. #endif
  42. #if SOC_SHA_SUPPORT_SHA512_T
  43. case SHA2_512224:
  44. case SHA2_512256:
  45. case SHA2_512T:
  46. #endif
  47. return 128;
  48. default:
  49. return 0;
  50. }
  51. }
  52. #if defined(SOC_SHA_SUPPORT_SHA1)
  53. static const unsigned char sha1_padding[64] = {
  54. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  58. };
  59. typedef struct {
  60. uint32_t total[2]; /*!< number of bytes processed */
  61. uint32_t state[5]; /*!< intermediate digest state */
  62. unsigned char buffer[64]; /*!< data block being processed */
  63. int first_block; /*!< if first then true else false */
  64. } sha1_ctx;
  65. #endif /* defined(SOC_SHA_SUPPORT_SHA1) */
  66. #if defined(SOC_SHA_SUPPORT_SHA224) || defined(SOC_SHA_SUPPORT_SHA256)
  67. static const unsigned char sha256_padding[64] = {
  68. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  69. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  70. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  71. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  72. };
  73. typedef struct {
  74. uint32_t total[2]; /*!< number of bytes processed */
  75. uint32_t state[8]; /*!< intermediate digest state */
  76. unsigned char buffer[64]; /*!< data block being processed */
  77. int first_block; /*!< if first then true, else false */
  78. } sha256_ctx;
  79. #endif /* defined(SOC_SHA_SUPPORT_SHA224) || defined(SOC_SHA_SUPPORT_SHA256) */
  80. #if defined(SOC_SHA_SUPPORT_SHA384) || defined(SOC_SHA_SUPPORT_SHA512)
  81. static const unsigned char sha512_padding[128] = {
  82. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  90. };
  91. typedef struct {
  92. uint64_t total[2]; /*!< number of bytes processed */
  93. uint64_t state[8]; /*!< intermediate digest state */
  94. unsigned char buffer[128]; /*!< data block being processed */
  95. int first_block;
  96. uint32_t t_val; /*!< t_val for 512/t mode */
  97. } sha512_ctx;
  98. #if SOC_SHA_SUPPORT_SHA512_T
  99. int sha_512_t_init_hash_block(uint16_t t);
  100. /*
  101. * FIPS-180-2 test vectors
  102. */
  103. static const unsigned char sha512T_test_buf[2][113] = {
  104. { "abc" },
  105. {
  106. "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
  107. "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
  108. }
  109. };
  110. static const size_t sha512T_test_buflen[2] = {
  111. 3, 112
  112. };
  113. static const esp_sha_type sha512T_algo[4] = {
  114. SHA2_512224, SHA2_512256, SHA2_512T, SHA2_512T
  115. };
  116. static const size_t sha512T_t_len[4] = { 224, 256, 224, 256 };
  117. static const unsigned char sha512_test_sum[4][32] = {
  118. /* SHA512-224 */
  119. {
  120. 0x46, 0x34, 0x27, 0x0f, 0x70, 0x7b, 0x6a, 0x54,
  121. 0xda, 0xae, 0x75, 0x30, 0x46, 0x08, 0x42, 0xe2,
  122. 0x0e, 0x37, 0xed, 0x26, 0x5c, 0xee, 0xe9, 0xa4,
  123. 0x3e, 0x89, 0x24, 0xaa
  124. },
  125. {
  126. 0x23, 0xfe, 0xc5, 0xbb, 0x94, 0xd6, 0x0b, 0x23,
  127. 0x30, 0x81, 0x92, 0x64, 0x0b, 0x0c, 0x45, 0x33,
  128. 0x35, 0xd6, 0x64, 0x73, 0x4f, 0xe4, 0x0e, 0x72,
  129. 0x68, 0x67, 0x4a, 0xf9
  130. },
  131. /* SHA512-256 */
  132. {
  133. 0x53, 0x04, 0x8e, 0x26, 0x81, 0x94, 0x1e, 0xf9,
  134. 0x9b, 0x2e, 0x29, 0xb7, 0x6b, 0x4c, 0x7d, 0xab,
  135. 0xe4, 0xc2, 0xd0, 0xc6, 0x34, 0xfc, 0x6d, 0x46,
  136. 0xe0, 0xe2, 0xf1, 0x31, 0x07, 0xe7, 0xaf, 0x23
  137. },
  138. {
  139. 0x39, 0x28, 0xe1, 0x84, 0xfb, 0x86, 0x90, 0xf8,
  140. 0x40, 0xda, 0x39, 0x88, 0x12, 0x1d, 0x31, 0xbe,
  141. 0x65, 0xcb, 0x9d, 0x3e, 0xf8, 0x3e, 0xe6, 0x14,
  142. 0x6f, 0xea, 0xc8, 0x61, 0xe1, 0x9b, 0x56, 0x3a
  143. }
  144. /* For SHA512_T testing we use t=224 & t=256
  145. * so the hash digest should be same as above
  146. */
  147. };
  148. #endif //SOC_SHA_SUPPORT_SHA512_T
  149. #endif /* defined(SOC_SHA_SUPPORT_SHA384) || defined(SOC_SHA_SUPPORT_SHA512) */
  150. #endif /* SOC_SHA_SUPPORTED */