Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Freescale Semiconductor, Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <malloc.h> |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 10 | #include <fsl_sec.h> |
| 11 | #include <asm-generic/errno.h> |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 12 | #include "jobdesc.h" |
| 13 | #include "desc.h" |
| 14 | #include "jr.h" |
| 15 | |
gaurav rana | b64308e | 2015-02-25 09:37:09 +0530 | [diff] [blame] | 16 | int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 17 | { |
| 18 | int ret, i = 0; |
| 19 | u32 *desc; |
| 20 | |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 21 | printf("\nDecapsulating blob to get data\n"); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 22 | desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE); |
| 23 | if (!desc) { |
| 24 | debug("Not enough memory for descriptor allocation\n"); |
| 25 | return -1; |
| 26 | } |
| 27 | |
| 28 | inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len); |
| 29 | |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 30 | debug("Descriptor dump:\n"); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 31 | for (i = 0; i < 14; i++) |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 32 | debug("Word[%d]: %08x\n", i, *(desc + i)); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 33 | ret = run_descriptor_jr(desc); |
| 34 | |
| 35 | if (ret) |
| 36 | printf("Error in Decapsulation %d\n", ret); |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 37 | else |
| 38 | printf("Decapsulation Success\n"); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 39 | |
| 40 | free(desc); |
| 41 | return ret; |
| 42 | } |
| 43 | |
gaurav rana | b64308e | 2015-02-25 09:37:09 +0530 | [diff] [blame] | 44 | int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 45 | { |
| 46 | int ret, i = 0; |
| 47 | u32 *desc; |
| 48 | |
| 49 | printf("\nEncapsulating data to form blob\n"); |
| 50 | desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE); |
| 51 | if (!desc) { |
| 52 | debug("Not enough memory for descriptor allocation\n"); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len); |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 57 | |
| 58 | debug("Descriptor dump:\n"); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 59 | for (i = 0; i < 14; i++) |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 60 | debug("Word[%d]: %08x\n", i, *(desc + i)); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 61 | ret = run_descriptor_jr(desc); |
| 62 | |
| 63 | if (ret) |
| 64 | printf("Error in Encapsulation %d\n", ret); |
Sumit Garg | ff9eb05 | 2016-07-14 13:27:50 -0400 | [diff] [blame] | 65 | else |
| 66 | printf("Encapsulation Success\n"); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 67 | |
| 68 | free(desc); |
| 69 | return ret; |
| 70 | } |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 71 | |
| 72 | #ifdef CONFIG_CMD_DEKBLOB |
| 73 | int blob_dek(const u8 *src, u8 *dst, u8 len) |
| 74 | { |
| 75 | int ret, size, i = 0; |
| 76 | u32 *desc; |
| 77 | |
| 78 | int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE; |
| 79 | |
| 80 | puts("\nEncapsulating provided DEK to form blob\n"); |
| 81 | desc = memalign(ARCH_DMA_MINALIGN, |
| 82 | sizeof(uint32_t) * DEK_BLOB_DESCSIZE); |
| 83 | if (!desc) { |
| 84 | debug("Not enough memory for descriptor allocation\n"); |
| 85 | return -ENOMEM; |
| 86 | } |
| 87 | |
| 88 | ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len); |
| 89 | if (ret) { |
| 90 | debug("Error in Job Descriptor Construction: %d\n", ret); |
| 91 | } else { |
| 92 | size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE, |
| 93 | ARCH_DMA_MINALIGN); |
| 94 | flush_dcache_range((unsigned long)desc, |
| 95 | (unsigned long)desc + size); |
| 96 | size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN); |
| 97 | flush_dcache_range((unsigned long)dst, |
| 98 | (unsigned long)dst + size); |
| 99 | |
| 100 | ret = run_descriptor_jr(desc); |
| 101 | } |
| 102 | |
| 103 | if (ret) { |
| 104 | debug("Error in Encapsulation %d\n", ret); |
| 105 | goto err; |
| 106 | } |
| 107 | |
| 108 | size = roundup(out_sz, ARCH_DMA_MINALIGN); |
| 109 | invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size); |
| 110 | |
| 111 | puts("DEK Blob\n"); |
| 112 | for (i = 0; i < out_sz; i++) |
| 113 | printf("%02X", ((uint8_t *)dst)[i]); |
| 114 | printf("\n"); |
| 115 | |
| 116 | err: |
| 117 | free(desc); |
| 118 | return ret; |
| 119 | } |
| 120 | #endif |