bl_sec.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __BL_SEC_H__
  31. #define __BL_SEC_H__
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #include <stdlib.h>
  35. /* copied SEC_Eng_SHA256_Ctx from stddrv */
  36. typedef struct {
  37. uint32_t total[2];
  38. uint32_t *shaBuf;
  39. uint32_t *shaPadding;
  40. uint8_t shaFeed;
  41. } _bl_sha_SEC_Eng_SHA256_Ctx_t;
  42. /* copied SEC_ENG_SHA_Type from stddrv, SHA1_RSVD removed */
  43. typedef enum {
  44. BL_SHA256,
  45. BL_SHA224,
  46. BL_SHA1,
  47. } bl_sha_type_t;
  48. typedef struct bl_sha_ctx {
  49. _bl_sha_SEC_Eng_SHA256_Ctx_t sha_ctx;
  50. uint32_t tmp[16];
  51. uint32_t pad[16];
  52. } bl_sha_ctx_t;
  53. // extern BL_Sem_t g_bl_sec_sha_mutex;
  54. int bl_sec_init(void);
  55. int bl_sec_test(void);
  56. int bl_pka_test(void);
  57. int bl_sec_aes_init(void);
  58. int bl_sec_aes_enc(uint8_t *key, int keysize, uint8_t *input, uint8_t *output);
  59. int bl_sec_aes_test(void);
  60. uint32_t bl_sec_get_random_word(void);
  61. void bl_rand_stream(uint8_t *buf, int len);
  62. int bl_rand(void);
  63. /*SHA Engine API*/
  64. int bl_sec_sha_test(void);
  65. int bl_sha_mutex_take();
  66. int bl_sha_mutex_give();
  67. void bl_sha_init(bl_sha_ctx_t *ctx, const bl_sha_type_t type);
  68. int bl_sha_update(bl_sha_ctx_t *ctx, const uint8_t *input, uint32_t len);
  69. int bl_sha_finish(bl_sha_ctx_t *ctx, uint8_t *hash);
  70. int bl_sec_ccm_encrypt_and_tag(const uint8_t *key, unsigned int key_bytelen, size_t length, const unsigned char *iv, size_t iv_len, const unsigned char *add, size_t add_len,
  71. const unsigned char *input, unsigned char *output, unsigned char *tag, size_t tag_len);
  72. int bl_sec_ccm_auth_decrypt(const uint8_t *key, unsigned int key_bytelen, size_t length,const unsigned char *iv, size_t iv_len, const unsigned char *add,
  73. size_t add_len, const unsigned char *input, unsigned char *output, const unsigned char *tag, size_t tag_len);
  74. int bl_sec_aes_ecb_encrypt(const uint8_t *key, unsigned int key_bytelen, size_t length, const unsigned char *input, unsigned char *output);
  75. int bl_sec_aes_ecb_decrypt(const uint8_t *key, unsigned int key_bytelen, size_t length, const unsigned char *input, unsigned char *output);
  76. #endif