psa_crypto_hash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * PSA hashing layer on top of Mbed TLS software crypto
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "common.h"
  21. #if defined(MBEDTLS_PSA_CRYPTO_C)
  22. #include <psa/crypto.h>
  23. #include "psa_crypto_core.h"
  24. #include "psa_crypto_hash.h"
  25. #include <mbedtls/error.h>
  26. #include <string.h>
  27. #if defined(MBEDTLS_PSA_BUILTIN_HASH)
  28. psa_status_t mbedtls_psa_hash_abort(
  29. mbedtls_psa_hash_operation_t *operation)
  30. {
  31. switch (operation->alg) {
  32. case 0:
  33. /* The object has (apparently) been initialized but it is not
  34. * in use. It's ok to call abort on such an object, and there's
  35. * nothing to do. */
  36. break;
  37. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  38. case PSA_ALG_MD5:
  39. mbedtls_md5_free(&operation->ctx.md5);
  40. break;
  41. #endif
  42. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  43. case PSA_ALG_RIPEMD160:
  44. mbedtls_ripemd160_free(&operation->ctx.ripemd160);
  45. break;
  46. #endif
  47. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  48. case PSA_ALG_SHA_1:
  49. mbedtls_sha1_free(&operation->ctx.sha1);
  50. break;
  51. #endif
  52. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  53. case PSA_ALG_SHA_224:
  54. mbedtls_sha256_free(&operation->ctx.sha256);
  55. break;
  56. #endif
  57. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  58. case PSA_ALG_SHA_256:
  59. mbedtls_sha256_free(&operation->ctx.sha256);
  60. break;
  61. #endif
  62. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  63. case PSA_ALG_SHA_384:
  64. mbedtls_sha512_free(&operation->ctx.sha512);
  65. break;
  66. #endif
  67. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  68. case PSA_ALG_SHA_512:
  69. mbedtls_sha512_free(&operation->ctx.sha512);
  70. break;
  71. #endif
  72. default:
  73. return PSA_ERROR_BAD_STATE;
  74. }
  75. operation->alg = 0;
  76. return PSA_SUCCESS;
  77. }
  78. psa_status_t mbedtls_psa_hash_setup(
  79. mbedtls_psa_hash_operation_t *operation,
  80. psa_algorithm_t alg)
  81. {
  82. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  83. /* A context must be freshly initialized before it can be set up. */
  84. if (operation->alg != 0) {
  85. return PSA_ERROR_BAD_STATE;
  86. }
  87. switch (alg) {
  88. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  89. case PSA_ALG_MD5:
  90. mbedtls_md5_init(&operation->ctx.md5);
  91. ret = mbedtls_md5_starts(&operation->ctx.md5);
  92. break;
  93. #endif
  94. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  95. case PSA_ALG_RIPEMD160:
  96. mbedtls_ripemd160_init(&operation->ctx.ripemd160);
  97. ret = mbedtls_ripemd160_starts(&operation->ctx.ripemd160);
  98. break;
  99. #endif
  100. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  101. case PSA_ALG_SHA_1:
  102. mbedtls_sha1_init(&operation->ctx.sha1);
  103. ret = mbedtls_sha1_starts(&operation->ctx.sha1);
  104. break;
  105. #endif
  106. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  107. case PSA_ALG_SHA_224:
  108. mbedtls_sha256_init(&operation->ctx.sha256);
  109. ret = mbedtls_sha256_starts(&operation->ctx.sha256, 1);
  110. break;
  111. #endif
  112. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  113. case PSA_ALG_SHA_256:
  114. mbedtls_sha256_init(&operation->ctx.sha256);
  115. ret = mbedtls_sha256_starts(&operation->ctx.sha256, 0);
  116. break;
  117. #endif
  118. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  119. case PSA_ALG_SHA_384:
  120. mbedtls_sha512_init(&operation->ctx.sha512);
  121. ret = mbedtls_sha512_starts(&operation->ctx.sha512, 1);
  122. break;
  123. #endif
  124. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  125. case PSA_ALG_SHA_512:
  126. mbedtls_sha512_init(&operation->ctx.sha512);
  127. ret = mbedtls_sha512_starts(&operation->ctx.sha512, 0);
  128. break;
  129. #endif
  130. default:
  131. return PSA_ALG_IS_HASH(alg) ?
  132. PSA_ERROR_NOT_SUPPORTED :
  133. PSA_ERROR_INVALID_ARGUMENT;
  134. }
  135. if (ret == 0) {
  136. operation->alg = alg;
  137. } else {
  138. mbedtls_psa_hash_abort(operation);
  139. }
  140. return mbedtls_to_psa_error(ret);
  141. }
  142. psa_status_t mbedtls_psa_hash_clone(
  143. const mbedtls_psa_hash_operation_t *source_operation,
  144. mbedtls_psa_hash_operation_t *target_operation)
  145. {
  146. switch (source_operation->alg) {
  147. case 0:
  148. return PSA_ERROR_BAD_STATE;
  149. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  150. case PSA_ALG_MD5:
  151. mbedtls_md5_clone(&target_operation->ctx.md5,
  152. &source_operation->ctx.md5);
  153. break;
  154. #endif
  155. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  156. case PSA_ALG_RIPEMD160:
  157. mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160,
  158. &source_operation->ctx.ripemd160);
  159. break;
  160. #endif
  161. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  162. case PSA_ALG_SHA_1:
  163. mbedtls_sha1_clone(&target_operation->ctx.sha1,
  164. &source_operation->ctx.sha1);
  165. break;
  166. #endif
  167. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  168. case PSA_ALG_SHA_224:
  169. mbedtls_sha256_clone(&target_operation->ctx.sha256,
  170. &source_operation->ctx.sha256);
  171. break;
  172. #endif
  173. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  174. case PSA_ALG_SHA_256:
  175. mbedtls_sha256_clone(&target_operation->ctx.sha256,
  176. &source_operation->ctx.sha256);
  177. break;
  178. #endif
  179. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  180. case PSA_ALG_SHA_384:
  181. mbedtls_sha512_clone(&target_operation->ctx.sha512,
  182. &source_operation->ctx.sha512);
  183. break;
  184. #endif
  185. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  186. case PSA_ALG_SHA_512:
  187. mbedtls_sha512_clone(&target_operation->ctx.sha512,
  188. &source_operation->ctx.sha512);
  189. break;
  190. #endif
  191. default:
  192. (void) source_operation;
  193. (void) target_operation;
  194. return PSA_ERROR_NOT_SUPPORTED;
  195. }
  196. target_operation->alg = source_operation->alg;
  197. return PSA_SUCCESS;
  198. }
  199. psa_status_t mbedtls_psa_hash_update(
  200. mbedtls_psa_hash_operation_t *operation,
  201. const uint8_t *input,
  202. size_t input_length)
  203. {
  204. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  205. switch (operation->alg) {
  206. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  207. case PSA_ALG_MD5:
  208. ret = mbedtls_md5_update(&operation->ctx.md5,
  209. input, input_length);
  210. break;
  211. #endif
  212. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  213. case PSA_ALG_RIPEMD160:
  214. ret = mbedtls_ripemd160_update(&operation->ctx.ripemd160,
  215. input, input_length);
  216. break;
  217. #endif
  218. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  219. case PSA_ALG_SHA_1:
  220. ret = mbedtls_sha1_update(&operation->ctx.sha1,
  221. input, input_length);
  222. break;
  223. #endif
  224. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  225. case PSA_ALG_SHA_224:
  226. ret = mbedtls_sha256_update(&operation->ctx.sha256,
  227. input, input_length);
  228. break;
  229. #endif
  230. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  231. case PSA_ALG_SHA_256:
  232. ret = mbedtls_sha256_update(&operation->ctx.sha256,
  233. input, input_length);
  234. break;
  235. #endif
  236. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  237. case PSA_ALG_SHA_384:
  238. ret = mbedtls_sha512_update(&operation->ctx.sha512,
  239. input, input_length);
  240. break;
  241. #endif
  242. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  243. case PSA_ALG_SHA_512:
  244. ret = mbedtls_sha512_update(&operation->ctx.sha512,
  245. input, input_length);
  246. break;
  247. #endif
  248. default:
  249. (void) input;
  250. (void) input_length;
  251. return PSA_ERROR_BAD_STATE;
  252. }
  253. return mbedtls_to_psa_error(ret);
  254. }
  255. psa_status_t mbedtls_psa_hash_finish(
  256. mbedtls_psa_hash_operation_t *operation,
  257. uint8_t *hash,
  258. size_t hash_size,
  259. size_t *hash_length)
  260. {
  261. psa_status_t status;
  262. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  263. size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg);
  264. /* Fill the output buffer with something that isn't a valid hash
  265. * (barring an attack on the hash and deliberately-crafted input),
  266. * in case the caller doesn't check the return status properly. */
  267. *hash_length = hash_size;
  268. /* If hash_size is 0 then hash may be NULL and then the
  269. * call to memset would have undefined behavior. */
  270. if (hash_size != 0) {
  271. memset(hash, '!', hash_size);
  272. }
  273. if (hash_size < actual_hash_length) {
  274. status = PSA_ERROR_BUFFER_TOO_SMALL;
  275. goto exit;
  276. }
  277. switch (operation->alg) {
  278. #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
  279. case PSA_ALG_MD5:
  280. ret = mbedtls_md5_finish(&operation->ctx.md5, hash);
  281. break;
  282. #endif
  283. #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
  284. case PSA_ALG_RIPEMD160:
  285. ret = mbedtls_ripemd160_finish(&operation->ctx.ripemd160, hash);
  286. break;
  287. #endif
  288. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
  289. case PSA_ALG_SHA_1:
  290. ret = mbedtls_sha1_finish(&operation->ctx.sha1, hash);
  291. break;
  292. #endif
  293. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
  294. case PSA_ALG_SHA_224:
  295. ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
  296. break;
  297. #endif
  298. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
  299. case PSA_ALG_SHA_256:
  300. ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
  301. break;
  302. #endif
  303. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
  304. case PSA_ALG_SHA_384:
  305. ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
  306. break;
  307. #endif
  308. #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
  309. case PSA_ALG_SHA_512:
  310. ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
  311. break;
  312. #endif
  313. default:
  314. (void) hash;
  315. return PSA_ERROR_BAD_STATE;
  316. }
  317. status = mbedtls_to_psa_error(ret);
  318. exit:
  319. if (status == PSA_SUCCESS) {
  320. *hash_length = actual_hash_length;
  321. }
  322. return status;
  323. }
  324. psa_status_t mbedtls_psa_hash_compute(
  325. psa_algorithm_t alg,
  326. const uint8_t *input,
  327. size_t input_length,
  328. uint8_t *hash,
  329. size_t hash_size,
  330. size_t *hash_length)
  331. {
  332. mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
  333. psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
  334. psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
  335. *hash_length = hash_size;
  336. status = mbedtls_psa_hash_setup(&operation, alg);
  337. if (status != PSA_SUCCESS) {
  338. goto exit;
  339. }
  340. status = mbedtls_psa_hash_update(&operation, input, input_length);
  341. if (status != PSA_SUCCESS) {
  342. goto exit;
  343. }
  344. status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length);
  345. if (status != PSA_SUCCESS) {
  346. goto exit;
  347. }
  348. exit:
  349. abort_status = mbedtls_psa_hash_abort(&operation);
  350. if (status == PSA_SUCCESS) {
  351. return abort_status;
  352. } else {
  353. return status;
  354. }
  355. }
  356. #endif /* MBEDTLS_PSA_BUILTIN_HASH */
  357. #endif /* MBEDTLS_PSA_CRYPTO_C */