jobdesc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * SEC Descriptor Construction Library
  3. * Basic job descriptor construction
  4. *
  5. * Copyright 2014 Freescale Semiconductor, Inc.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. *
  9. */
  10. #include <common.h>
  11. #include "desc_constr.h"
  12. #include "jobdesc.h"
  13. #define KEY_BLOB_SIZE 32
  14. #define MAC_SIZE 16
  15. void inline_cnstr_jobdesc_hash(uint32_t *desc,
  16. const uint8_t *msg, uint32_t msgsz, uint8_t *digest,
  17. u32 alg_type, uint32_t alg_size, int sg_tbl)
  18. {
  19. /* SHA 256 , output is of length 32 words */
  20. uint32_t storelen = alg_size;
  21. u32 options;
  22. dma_addr_t dma_addr_in, dma_addr_out;
  23. dma_addr_in = virt_to_phys((void *)msg);
  24. dma_addr_out = virt_to_phys((void *)digest);
  25. init_job_desc(desc, 0);
  26. append_operation(desc, OP_TYPE_CLASS2_ALG |
  27. OP_ALG_AAI_HASH | OP_ALG_AS_INITFINAL |
  28. OP_ALG_ENCRYPT | OP_ALG_ICV_OFF | alg_type);
  29. options = LDST_CLASS_2_CCB | FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2;
  30. if (sg_tbl)
  31. options |= FIFOLDST_SGF;
  32. if (msgsz > 0xffff) {
  33. options |= FIFOLDST_EXT;
  34. append_fifo_load(desc, dma_addr_in, 0, options);
  35. append_cmd(desc, msgsz);
  36. } else {
  37. append_fifo_load(desc, dma_addr_in, msgsz, options);
  38. }
  39. append_store(desc, dma_addr_out, storelen,
  40. LDST_CLASS_2_CCB | LDST_SRCDST_BYTE_CONTEXT);
  41. }
  42. void inline_cnstr_jobdesc_blob_encap(uint32_t *desc, uint8_t *key_idnfr,
  43. uint8_t *plain_txt, uint8_t *enc_blob,
  44. uint32_t in_sz)
  45. {
  46. dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out;
  47. uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
  48. /* output blob will have 32 bytes key blob in beginning and
  49. * 16 byte HMAC identifier at end of data blob */
  50. uint32_t out_sz = in_sz + KEY_BLOB_SIZE + MAC_SIZE;
  51. dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr);
  52. dma_addr_in = virt_to_phys((void *)plain_txt);
  53. dma_addr_out = virt_to_phys((void *)enc_blob);
  54. init_job_desc(desc, 0);
  55. append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2);
  56. append_seq_in_ptr(desc, dma_addr_in, in_sz, 0);
  57. append_seq_out_ptr(desc, dma_addr_out, out_sz, 0);
  58. append_operation(desc, OP_TYPE_ENCAP_PROTOCOL | OP_PCLID_BLOB);
  59. }
  60. void inline_cnstr_jobdesc_blob_decap(uint32_t *desc, uint8_t *key_idnfr,
  61. uint8_t *enc_blob, uint8_t *plain_txt,
  62. uint32_t out_sz)
  63. {
  64. dma_addr_t dma_addr_key_idnfr, dma_addr_in, dma_addr_out;
  65. uint32_t key_sz = KEY_IDNFR_SZ_BYTES;
  66. uint32_t in_sz = out_sz + KEY_BLOB_SIZE + MAC_SIZE;
  67. dma_addr_key_idnfr = virt_to_phys((void *)key_idnfr);
  68. dma_addr_in = virt_to_phys((void *)enc_blob);
  69. dma_addr_out = virt_to_phys((void *)plain_txt);
  70. init_job_desc(desc, 0);
  71. append_key(desc, dma_addr_key_idnfr, key_sz, CLASS_2);
  72. append_seq_in_ptr(desc, dma_addr_in, in_sz, 0);
  73. append_seq_out_ptr(desc, dma_addr_out, out_sz, 0);
  74. append_operation(desc, OP_TYPE_DECAP_PROTOCOL | OP_PCLID_BLOB);
  75. }
  76. /*
  77. * Descriptor to instantiate RNG State Handle 0 in normal mode and
  78. * load the JDKEK, TDKEK and TDSK registers
  79. */
  80. void inline_cnstr_jobdesc_rng_instantiation(uint32_t *desc)
  81. {
  82. u32 *jump_cmd;
  83. init_job_desc(desc, 0);
  84. /* INIT RNG in non-test mode */
  85. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  86. OP_ALG_AS_INIT);
  87. /* wait for done */
  88. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
  89. set_jump_tgt_here(desc, jump_cmd);
  90. /*
  91. * load 1 to clear written reg:
  92. * resets the done interrrupt and returns the RNG to idle.
  93. */
  94. append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
  95. /* generate secure keys (non-test) */
  96. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  97. OP_ALG_RNG4_SK);
  98. }