| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2006-2009, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bsettop_crypto.c $ |
|---|
| 11 | * $brcm_Revision: 6 $ |
|---|
| 12 | * $brcm_Date: 12/9/09 12:03p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/api/src/nexus/bsettop_crypto.c $ |
|---|
| 19 | * |
|---|
| 20 | * 6 12/9/09 12:03p gmohile |
|---|
| 21 | * SW7408-1 : Add 7408 support |
|---|
| 22 | * |
|---|
| 23 | * 5 6/15/09 5:08p jtna |
|---|
| 24 | * PR43001: add support for TTS+encryption record/playback |
|---|
| 25 | * |
|---|
| 26 | * 4 12/24/08 11:56a jgarrett |
|---|
| 27 | * PR 50703: Allowing for security module to be absent |
|---|
| 28 | * |
|---|
| 29 | * 3 8/13/08 12:56p mphillip |
|---|
| 30 | * PR40027: Fix return code from bcrypto_process |
|---|
| 31 | * |
|---|
| 32 | * 2 8/6/08 1:47p mphillip |
|---|
| 33 | * PR40027: Add basic block modes |
|---|
| 34 | * |
|---|
| 35 | * 1 10/15/07 2:35p erickson |
|---|
| 36 | * PR36068: initial |
|---|
| 37 | * |
|---|
| 38 | * |
|---|
| 39 | * |
|---|
| 40 | ***************************************************************************/ |
|---|
| 41 | |
|---|
| 42 | #include "bsettop_impl.h" |
|---|
| 43 | |
|---|
| 44 | #if NEXUS_HAS_DMA |
|---|
| 45 | #include "nexus_memory.h" |
|---|
| 46 | #include "nexus_dma.h" |
|---|
| 47 | |
|---|
| 48 | BDBG_MODULE(crypto); |
|---|
| 49 | |
|---|
| 50 | typedef struct bcrypto { |
|---|
| 51 | bcrypto_settings settings; |
|---|
| 52 | NEXUS_KeySlotHandle keyHandle; |
|---|
| 53 | |
|---|
| 54 | bool key_loaded; |
|---|
| 55 | unsigned int key_slot; |
|---|
| 56 | |
|---|
| 57 | } bcrypto; |
|---|
| 58 | |
|---|
| 59 | /* B_CRYPTO_N_CRYPTS controls the number of bcrypto structures available to |
|---|
| 60 | bcrypto_open (and thus the number of possible concurrent crypto sessions). |
|---|
| 61 | |
|---|
| 62 | This should be enough for reasonable cryptographic use. Even for 3-way |
|---|
| 63 | PVR, you have 1 for encrypting and 2 for decrypting per channel (9 total |
|---|
| 64 | used). This can be safely increased, it just results in an array of |
|---|
| 65 | struct bcrypto of this size. |
|---|
| 66 | */ |
|---|
| 67 | #define B_CRYPTO_N_CRYPTS (32) |
|---|
| 68 | static struct bcrypto g_crypto[B_CRYPTO_N_CRYPTS]; |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | void bcrypto_p_init(void) |
|---|
| 72 | { |
|---|
| 73 | BKNI_Memset(g_crypto, 0, sizeof(g_crypto)); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void bcrypto_p_shutdown(void) |
|---|
| 77 | { |
|---|
| 78 | unsigned i; |
|---|
| 79 | for(i=0;i<B_CRYPTO_N_CRYPTS;i++) { |
|---|
| 80 | if (g_crypto[i].keyHandle) { |
|---|
| 81 | bcrypto_close(&g_crypto[i]); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void bcrypto_settings_init(bcrypto_settings *settings) |
|---|
| 87 | { |
|---|
| 88 | BKNI_Memset(settings, 0, sizeof(bcrypto_settings)); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | bcrypto_t bcrypto_open(const bcrypto_settings *settings) |
|---|
| 92 | { |
|---|
| 93 | unsigned i; |
|---|
| 94 | |
|---|
| 95 | BDBG_ASSERT(settings); |
|---|
| 96 | |
|---|
| 97 | for(i=0;i<B_CRYPTO_N_CRYPTS;i++) { |
|---|
| 98 | if (!g_crypto[i].keyHandle) { |
|---|
| 99 | g_crypto[i].settings = *settings; |
|---|
| 100 | return &g_crypto[i]; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | BSETTOP_ERROR(berr_not_available); |
|---|
| 105 | return NULL; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | bresult bcrypto_status(bcrypto_t crypto, bcrypto_status_t *status) |
|---|
| 109 | { |
|---|
| 110 | BDBG_ASSERT(crypto); |
|---|
| 111 | if (status) { |
|---|
| 112 | switch(crypto->settings.encryption.type) { |
|---|
| 113 | case bencryption_type_des: |
|---|
| 114 | case bencryption_type_3des: |
|---|
| 115 | case bencryption_type_aes: |
|---|
| 116 | case bencryption_type_dvb: |
|---|
| 117 | case bencryption_type_rc4: |
|---|
| 118 | case bencryption_type_c2: |
|---|
| 119 | status->matching_output_size = true; |
|---|
| 120 | status->fixed_output_size = false; |
|---|
| 121 | status->fixed_output_length = 0; |
|---|
| 122 | break; |
|---|
| 123 | case bencryption_type_md5: |
|---|
| 124 | status->matching_output_size = false; |
|---|
| 125 | status->fixed_output_size = true; |
|---|
| 126 | status->fixed_output_length = 16; |
|---|
| 127 | break; |
|---|
| 128 | case bencryption_type_sha1: |
|---|
| 129 | status->matching_output_size = false; |
|---|
| 130 | status->fixed_output_size = true; |
|---|
| 131 | status->fixed_output_length = 20; |
|---|
| 132 | break; |
|---|
| 133 | /* add RSA/DSA later, output is dependent on key size */ |
|---|
| 134 | default: |
|---|
| 135 | return BSETTOP_ERROR(berr_not_available); |
|---|
| 136 | } |
|---|
| 137 | return b_ok; |
|---|
| 138 | } else { |
|---|
| 139 | return BSETTOP_ERROR(berr_invalid_parameter); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | bresult bcrypto_process(bcrypto_t crypto, uint8_t *in_buffer, unsigned long in_buffer_length, uint8_t *out_buffer, unsigned long out_buffer_length) |
|---|
| 144 | { |
|---|
| 145 | NEXUS_DmaJobBlockSettings dmaJobBlockSettings; |
|---|
| 146 | NEXUS_DmaJobSettings dmaJobSettings; |
|---|
| 147 | NEXUS_DmaJobHandle dmaJobHandle; |
|---|
| 148 | bresult rc = b_ok; |
|---|
| 149 | NEXUS_Error nrc; |
|---|
| 150 | BDBG_ASSERT(crypto); |
|---|
| 151 | switch(crypto->settings.encryption.type) { |
|---|
| 152 | case bencryption_type_des: |
|---|
| 153 | case bencryption_type_3des: |
|---|
| 154 | case bencryption_type_aes: |
|---|
| 155 | |
|---|
| 156 | if (!in_buffer || !out_buffer || (in_buffer_length != out_buffer_length) || |
|---|
| 157 | (in_buffer_length == 0) || |
|---|
| 158 | ((crypto->settings.operation != bcrypto_operation_encrypt) && (crypto->settings.operation != bcrypto_operation_decrypt))) |
|---|
| 159 | return BSETTOP_ERROR(berr_invalid_parameter); |
|---|
| 160 | |
|---|
| 161 | if (!crypto->keyHandle) { |
|---|
| 162 | crypto->keyHandle = b_keyslot_m2m_allocate(&(crypto->settings.encryption),crypto->settings.operation == bcrypto_operation_encrypt, false); |
|---|
| 163 | if (!crypto->keyHandle) { |
|---|
| 164 | BDBG_ERR(("Error allocating keyslot!")); |
|---|
| 165 | return BSETTOP_ERROR(berr_external_error); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | NEXUS_DmaJob_GetDefaultSettings(&dmaJobSettings); |
|---|
| 170 | dmaJobSettings.numBlocks = 1; |
|---|
| 171 | dmaJobSettings.keySlot = crypto->keyHandle; |
|---|
| 172 | switch(crypto->settings.data_format) { |
|---|
| 173 | case bcrypto_data_format_ts: |
|---|
| 174 | dmaJobSettings.dataFormat = NEXUS_DmaDataFormat_eMpeg; |
|---|
| 175 | break; |
|---|
| 176 | case bcrypto_data_format_dss: |
|---|
| 177 | dmaJobSettings.dataFormat = NEXUS_DmaDataFormat_eDss; |
|---|
| 178 | break; |
|---|
| 179 | default: |
|---|
| 180 | dmaJobSettings.dataFormat = NEXUS_DmaDataFormat_eBlock; |
|---|
| 181 | } |
|---|
| 182 | dmaJobHandle = NEXUS_DmaJob_Create(g_dma.hDma,&dmaJobSettings); |
|---|
| 183 | if (!dmaJobHandle) { |
|---|
| 184 | BSETTOP_ERROR(berr_external_error); |
|---|
| 185 | } |
|---|
| 186 | NEXUS_DmaJob_GetDefaultBlockSettings(&dmaJobBlockSettings); |
|---|
| 187 | dmaJobBlockSettings.pSrcAddr = in_buffer; |
|---|
| 188 | dmaJobBlockSettings.blockSize = in_buffer_length; |
|---|
| 189 | dmaJobBlockSettings.pDestAddr = out_buffer; |
|---|
| 190 | dmaJobBlockSettings.cached = true; |
|---|
| 191 | dmaJobBlockSettings.resetCrypto = true; |
|---|
| 192 | dmaJobBlockSettings.scatterGatherCryptoStart = true; |
|---|
| 193 | dmaJobBlockSettings.scatterGatherCryptoEnd = true; |
|---|
| 194 | |
|---|
| 195 | nrc = NEXUS_DmaJob_ProcessBlocks(dmaJobHandle, &dmaJobBlockSettings, 1); |
|---|
| 196 | if (nrc == NEXUS_DMA_QUEUED || nrc == NEXUS_SUCCESS) |
|---|
| 197 | rc = b_ok; |
|---|
| 198 | { |
|---|
| 199 | NEXUS_DmaJobStatus status; |
|---|
| 200 | NEXUS_DmaJob_GetStatus(dmaJobHandle, &status); |
|---|
| 201 | while (status.currentState != NEXUS_DmaJobState_eComplete) { |
|---|
| 202 | BKNI_Sleep(1); |
|---|
| 203 | NEXUS_DmaJob_GetStatus(dmaJobHandle, &status); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | NEXUS_DmaJob_Destroy(dmaJobHandle); |
|---|
| 207 | break; |
|---|
| 208 | default: |
|---|
| 209 | return BSETTOP_ERROR(berr_not_available); |
|---|
| 210 | } |
|---|
| 211 | return rc; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | void bcrypto_close(bcrypto_t crypto) |
|---|
| 215 | { |
|---|
| 216 | if (crypto) { |
|---|
| 217 | if (crypto->keyHandle) { |
|---|
| 218 | b_keyslot_m2m_free(crypto->keyHandle); |
|---|
| 219 | crypto->keyHandle = NULL; |
|---|
| 220 | } |
|---|
| 221 | BKNI_Memset(crypto, 0, sizeof(struct bcrypto)); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | uint8_t *bcrypto_alloc(unsigned buffer_length) |
|---|
| 226 | { |
|---|
| 227 | void *rp = NULL; |
|---|
| 228 | NEXUS_MemoryAllocationSettings memSettings; |
|---|
| 229 | NEXUS_Memory_GetDefaultAllocationSettings(&memSettings); |
|---|
| 230 | if (NEXUS_Memory_Allocate(buffer_length, NULL, &rp)) { |
|---|
| 231 | return NULL; |
|---|
| 232 | } |
|---|
| 233 | return (uint8_t *)rp; |
|---|
| 234 | #if 0 |
|---|
| 235 | if (b_root.dma.dma && b_root.dma.dma_mem) { |
|---|
| 236 | return BMEM_AllocAligned(b_root.transport.xpt->hMemory, buffer_length, 2, 0); |
|---|
| 237 | } |
|---|
| 238 | #endif |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | void bcrypto_free(uint8_t *buffer) |
|---|
| 242 | { |
|---|
| 243 | if (buffer) |
|---|
| 244 | NEXUS_Memory_Free(buffer); |
|---|
| 245 | #if 0 |
|---|
| 246 | BMEM_Free(b_root.transport.xpt->hMemory, buffer); |
|---|
| 247 | #endif |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | #else |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | void bcrypto_settings_init(bcrypto_settings *settings) |
|---|
| 254 | { |
|---|
| 255 | BSTD_UNUSED(settings); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | bcrypto_t bcrypto_open(const bcrypto_settings *settings) |
|---|
| 260 | { |
|---|
| 261 | BSTD_UNUSED(settings); |
|---|
| 262 | return NULL; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | bresult bcrypto_status(bcrypto_t crypto, bcrypto_status_t *status) |
|---|
| 266 | { |
|---|
| 267 | BSTD_UNUSED(crypto); |
|---|
| 268 | BSTD_UNUSED(status); |
|---|
| 269 | return 0; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | bresult bcrypto_process(bcrypto_t crypto, uint8_t *in_buffer, unsigned long in_buffer_length, uint8_t *out_buffer, unsigned long out_buffer_length) |
|---|
| 273 | { |
|---|
| 274 | BSTD_UNUSED(crypto); |
|---|
| 275 | BSTD_UNUSED(in_buffer); |
|---|
| 276 | BSTD_UNUSED(in_buffer_length); |
|---|
| 277 | BSTD_UNUSED(out_buffer); |
|---|
| 278 | BSTD_UNUSED(out_buffer_length); |
|---|
| 279 | return 0; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | void bcrypto_close(bcrypto_t crypto) |
|---|
| 283 | { |
|---|
| 284 | BSTD_UNUSED(crypto); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | uint8_t *bcrypto_alloc(unsigned buffer_length) |
|---|
| 288 | { |
|---|
| 289 | BSTD_UNUSED(buffer_length); |
|---|
| 290 | return NULL; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | void bcrypto_free(uint8_t *buffer) |
|---|
| 294 | { |
|---|
| 295 | BSTD_UNUSED(buffer); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | #endif |
|---|