| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2006-2007, 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.h $ |
|---|
| 11 | * $brcm_Revision: 13 $ |
|---|
| 12 | * $brcm_Date: 8/27/07 5:11p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/api/include/bsettop_crypto.h $ |
|---|
| 19 | * |
|---|
| 20 | * 13 8/27/07 5:11p vsilyaev |
|---|
| 21 | * PR 34353: Data structure change to allow the keyladder to be sourced |
|---|
| 22 | * correctly |
|---|
| 23 | * |
|---|
| 24 | * 12 8/22/07 5:26p jgarrett |
|---|
| 25 | * PR 31818: Adding external encryption algorithm support |
|---|
| 26 | * |
|---|
| 27 | * 11 7/19/07 12:23a vsilyaev |
|---|
| 28 | * PR 32275: Added 5-th level keys |
|---|
| 29 | * |
|---|
| 30 | * 10 6/28/07 2:19p vsilyaev |
|---|
| 31 | * PR 32275: Added hmac (keyed-hash message authentication code) operation |
|---|
| 32 | * |
|---|
| 33 | * 9 3/14/07 3:27p vsilyaev |
|---|
| 34 | * PR 26200: Changes in bcrypto configuration structures |
|---|
| 35 | * |
|---|
| 36 | * 8 11/28/06 1:23p mphillip |
|---|
| 37 | * PR26154: Enable hardware RSA for 7401 |
|---|
| 38 | * |
|---|
| 39 | * 7 11/6/06 6:00p mphillip |
|---|
| 40 | * PR23943: Add counter mode |
|---|
| 41 | * |
|---|
| 42 | * 6 11/1/06 6:27p mphillip |
|---|
| 43 | * PR23943: First pass at adding CMAC support. The current approach may |
|---|
| 44 | * change in the future if it doesn't work out correctly. |
|---|
| 45 | * |
|---|
| 46 | * 5 9/27/06 4:19p mphillip |
|---|
| 47 | * PR24607: Add extern statement |
|---|
| 48 | * |
|---|
| 49 | * 4 8/11/06 5:57p mphillip |
|---|
| 50 | * PR23573: Rename bcrypt_ to bcrypto_ |
|---|
| 51 | * |
|---|
| 52 | * 3 8/3/06 11:09a mphillip |
|---|
| 53 | * PR19544: Correct the paramater for bcrypt_open |
|---|
| 54 | * |
|---|
| 55 | * 2 8/2/06 1:58p mphillip |
|---|
| 56 | * PR19544: Update bsettop_crypt.h to handle multiple calls to hashes, and |
|---|
| 57 | * fix a typo in bsettop_crypt.c |
|---|
| 58 | * |
|---|
| 59 | * 1 7/11/06 5:47p mphillip |
|---|
| 60 | * PR19544: Initial commit of bcrypt_ stubs |
|---|
| 61 | * |
|---|
| 62 | * |
|---|
| 63 | * |
|---|
| 64 | ***************************************************************************/ |
|---|
| 65 | |
|---|
| 66 | #ifndef BSETTOP_CRYPTO_H__ |
|---|
| 67 | #define BSETTOP_CRYPTO_H__ |
|---|
| 68 | |
|---|
| 69 | #ifdef __cplusplus |
|---|
| 70 | extern "C" |
|---|
| 71 | { |
|---|
| 72 | #endif |
|---|
| 73 | |
|---|
| 74 | /*=************************* |
|---|
| 75 | The crypto module provides access to cryptographic functionality. |
|---|
| 76 | |
|---|
| 77 | This is not the interface for controlling playback or record encryption or live conditional access. |
|---|
| 78 | PVR encryption is handled through the bstream_mpeg.encryption structure passed in to |
|---|
| 79 | bplayback_start. Live conditional access (or playback of network-encrypted streams) is controlled |
|---|
| 80 | through bstream_set_encryption. |
|---|
| 81 | ****************************/ |
|---|
| 82 | |
|---|
| 83 | /* |
|---|
| 84 | Summary: |
|---|
| 85 | Cryptographic hardware selection |
|---|
| 86 | Description: |
|---|
| 87 | This controls how cryptography is performed. When bcrypto_hardware_default is used, the bcrypto layer will choose |
|---|
| 88 | whether to use a hardware or software implementation. Other values will dictate a specific implementation. |
|---|
| 89 | */ |
|---|
| 90 | typedef enum bcrypto_hardware_ctrl { |
|---|
| 91 | bcrypto_hardware_default, /* Use either a software or hardware implementation, bcrypto will choose */ |
|---|
| 92 | bcrypto_hardware_none, /* Use only a software implementation */ |
|---|
| 93 | bcrypto_hardware_m2m, /* Use only a mem-to-mem (M2M) implementation */ |
|---|
| 94 | bcrypto_hardware_keyladder /* Use the keyladder approach */ |
|---|
| 95 | } bcrypto_hardware_ctrl; |
|---|
| 96 | |
|---|
| 97 | /* |
|---|
| 98 | Summary: |
|---|
| 99 | Cryptographic operation selection |
|---|
| 100 | Description: |
|---|
| 101 | This controls what cryptographic operation should be performed. Each algorithm supports only certain operations, |
|---|
| 102 | so the bcrypto documentation should be consulted for specifics. |
|---|
| 103 | */ |
|---|
| 104 | typedef enum bcrypto_operation { |
|---|
| 105 | bcrypto_operation_encrypt, |
|---|
| 106 | bcrypto_operation_decrypt, |
|---|
| 107 | bcrypto_operation_sign, |
|---|
| 108 | bcrypto_operation_verify, |
|---|
| 109 | bcrypto_operation_hash, |
|---|
| 110 | bcrypto_operation_mac, |
|---|
| 111 | bcrypto_operation_hmac |
|---|
| 112 | } bcrypto_operation; |
|---|
| 113 | |
|---|
| 114 | /* |
|---|
| 115 | Summary: |
|---|
| 116 | Description: |
|---|
| 117 | */ |
|---|
| 118 | typedef enum bcrypto_data_format { |
|---|
| 119 | bcrypto_data_format_raw, /* Process all data */ |
|---|
| 120 | bcrypto_data_format_ts, /* Data will be processed in 188 byte packets, skipping the transport headers. */ |
|---|
| 121 | bcrypto_data_format_dss /* Data will be processed in 130 byte packets, skipping the transport headers. */ |
|---|
| 122 | } bcrypto_data_format; |
|---|
| 123 | |
|---|
| 124 | /* |
|---|
| 125 | Summary: |
|---|
| 126 | RSA 1024-bit key structure |
|---|
| 127 | Description: |
|---|
| 128 | A pointer to this structure should be used for long_key (see bencryption_params) to pass an RSA key. In this case, bencryption_params.key_length will refer to the size of the struct (in bits, so sizeof(bcrypto_rsa_1024)*8), not the length of the key. That will be in the first member variable of this struct. |
|---|
| 129 | |
|---|
| 130 | Not all values need to be filled in for all modes of operation. For signing, n, and d are required. For verifying, n and e are required. |
|---|
| 131 | |
|---|
| 132 | For software modes, dmp1, dmq1, and iqmp are also required. |
|---|
| 133 | |
|---|
| 134 | For 7401 hardware, p, q, dmp1, dmq1, and iqmp are not necessary. |
|---|
| 135 | */ |
|---|
| 136 | typedef struct bcrypto_rsa_1024 { |
|---|
| 137 | unsigned key_length; /* Should be 1024 */ |
|---|
| 138 | uint8_t n[128]; /* modulus */ |
|---|
| 139 | uint8_t e[128]; /* public exponent */ |
|---|
| 140 | uint8_t d[128]; /* private exponent */ |
|---|
| 141 | uint8_t p[128]; /* first prime number */ |
|---|
| 142 | uint8_t q[128]; /* second prime number */ |
|---|
| 143 | uint8_t dmp1[128]; /* exponent corresponding to the first prime */ |
|---|
| 144 | uint8_t dmq1[128]; /* exponent corresponding to the second prime */ |
|---|
| 145 | uint8_t iqmp[128]; /* inverse of q mod p */ |
|---|
| 146 | } bcrypto_rsa_1024; |
|---|
| 147 | |
|---|
| 148 | /* |
|---|
| 149 | Summary: |
|---|
| 150 | RSA 2048-bit key structure |
|---|
| 151 | Description: |
|---|
| 152 | A pointer to this structure should be used for long_key (see bencryption_params) to pass an RSA key. In this case, bencryption_params.key_length will refer to the size of the struct (in bits, so sizeof(bcrypto_rsa_2048)*8), not the length of the key. That will be in the first member variable of this struct. |
|---|
| 153 | |
|---|
| 154 | Not all values need to be filled in for all modes of operation. For signing, n, and d are required. For verifying, n and e are required. |
|---|
| 155 | |
|---|
| 156 | For software modes, dmp1, dmq1, and iqmp are also required. |
|---|
| 157 | |
|---|
| 158 | For 7401 hardware, p, q, dmp1, dmq1, and iqmp are not necessary. |
|---|
| 159 | */ |
|---|
| 160 | typedef struct bcrypto_rsa_2048 { |
|---|
| 161 | unsigned key_length; /* Should be 2048 */ |
|---|
| 162 | uint8_t n[256]; /* modulus */ |
|---|
| 163 | uint8_t e[256]; /* public exponent */ |
|---|
| 164 | uint8_t d[256]; /* private exponent */ |
|---|
| 165 | uint8_t p[256]; /* first prime number */ |
|---|
| 166 | uint8_t q[256]; /* second prime number */ |
|---|
| 167 | uint8_t dmp1[256]; /* exponent corresponding to the first prime */ |
|---|
| 168 | uint8_t dmq1[256]; /* exponent corresponding to the second prime */ |
|---|
| 169 | uint8_t iqmp[256]; /* inverse of q mod p */ |
|---|
| 170 | } bcrypto_rsa_2048; |
|---|
| 171 | |
|---|
| 172 | /* |
|---|
| 173 | Summary: |
|---|
| 174 | Keyladder input structure |
|---|
| 175 | Description: |
|---|
| 176 | This is part of bcrypto_keyladder_data. |
|---|
| 177 | */ |
|---|
| 178 | typedef enum bcrypto_swizzle { |
|---|
| 179 | bcrypto_swizzle_none, |
|---|
| 180 | bcrypto_swizzle_0, |
|---|
| 181 | bcrypto_swizzle_1 |
|---|
| 182 | } bcrypto_swizzle; |
|---|
| 183 | |
|---|
| 184 | /* |
|---|
| 185 | Summary: |
|---|
| 186 | Keyladder input structure |
|---|
| 187 | Description: |
|---|
| 188 | This is part of bcrypto_keyladder_data. |
|---|
| 189 | */ |
|---|
| 190 | typedef enum bcrypto_keyladder_key { |
|---|
| 191 | bcrypto_keyladder_key_0, |
|---|
| 192 | bcrypto_keyladder_key_1, |
|---|
| 193 | bcrypto_keyladder_key_2, |
|---|
| 194 | bcrypto_keyladder_key_3, |
|---|
| 195 | bcrypto_keyladder_key_4, |
|---|
| 196 | bcrypto_keyladder_key_5 |
|---|
| 197 | } bcrypto_keyladder_key; |
|---|
| 198 | |
|---|
| 199 | /* |
|---|
| 200 | Summary: |
|---|
| 201 | Keyladder input structure |
|---|
| 202 | Description: |
|---|
| 203 | This is part of bcrypto_keyladder_data. |
|---|
| 204 | */ |
|---|
| 205 | typedef enum bcrypto_keyladder_rootkey_src { |
|---|
| 206 | bcrypto_rootkey_cust_key, |
|---|
| 207 | bcrypto_rootkey_otp_a, |
|---|
| 208 | bcrypto_rootkey_otp_b, |
|---|
| 209 | bcrypto_rootkey_otp_c, |
|---|
| 210 | bcrypto_rootkey_reserved_0, |
|---|
| 211 | bcrypto_rootkey_reserved_1, |
|---|
| 212 | bcrypto_rootkey_reserved_2, |
|---|
| 213 | bcrypto_rootkey_invalid |
|---|
| 214 | } bcrypto_keyladder_rootkey_src; |
|---|
| 215 | |
|---|
| 216 | /* |
|---|
| 217 | Summary: |
|---|
| 218 | Keyladder input structure |
|---|
| 219 | Description: |
|---|
| 220 | This is passed in as long_key when using keyladder. Remember to set key_length to the size of this struct in bits (key_length = 8*sizeof(struct bcrypto_keyladder_data)). |
|---|
| 221 | */ |
|---|
| 222 | typedef struct bcrypto_keyladder_data { |
|---|
| 223 | bcrypto_swizzle swizzle; |
|---|
| 224 | bcrypto_keyladder_key keyladder_key; |
|---|
| 225 | uint8_t odd_in_3[24]; |
|---|
| 226 | uint8_t even_in_3[24]; |
|---|
| 227 | uint8_t odd_in_4[24]; |
|---|
| 228 | uint8_t even_in_4[24]; |
|---|
| 229 | uint8_t odd_in_5[24]; |
|---|
| 230 | uint8_t even_in_5[24]; |
|---|
| 231 | uint8_t key_cust_low; |
|---|
| 232 | uint8_t key_cust_high; |
|---|
| 233 | uint8_t key_var_low; |
|---|
| 234 | uint8_t key_var_high; |
|---|
| 235 | bcrypto_keyladder_rootkey_src rootkey_src; |
|---|
| 236 | bool use_cust_key_decrypt; |
|---|
| 237 | bool gen_key_decrypt; /* matches generateRouteKeyIO.bls3DESDecrypt, false unless instructed otherwise */ |
|---|
| 238 | } bcrypto_keyladder_data; |
|---|
| 239 | |
|---|
| 240 | /* |
|---|
| 241 | Summary: |
|---|
| 242 | External bcrypto algorithm input structure |
|---|
| 243 | Description: |
|---|
| 244 | This is passed in as long_key when using an external encryption algorithm. Remember to set key_length to the size of this struct in bits (key_length = 8*sizeof(struct bcrypto_external_crypto_data)). |
|---|
| 245 | |
|---|
| 246 | When using an external algorithm with encrypted PVR, this is used to control the parameters. All other encryption parameters are ignored, so it is up to the external algorithm to manage keys, state, and other data. |
|---|
| 247 | */ |
|---|
| 248 | typedef void (*bcrypto_external_callback)(void *context, void *buffer, long size); |
|---|
| 249 | typedef struct bcrypto_external_crypto_data { |
|---|
| 250 | void *callback_context; |
|---|
| 251 | bcrypto_external_callback callback; |
|---|
| 252 | } bcrypto_external_crypto_data; |
|---|
| 253 | |
|---|
| 254 | /* |
|---|
| 255 | Summary: |
|---|
| 256 | BCrypt settings structure |
|---|
| 257 | Description: |
|---|
| 258 | This structure controls the bcrypto settings. It carries bencryption_params forward to control keys and algorithm choices. |
|---|
| 259 | */ |
|---|
| 260 | typedef struct bcrypto_settings { |
|---|
| 261 | bencryption_params encryption; |
|---|
| 262 | bcrypto_operation operation; /* encrypt, decrypt, sign, verify, hash */ |
|---|
| 263 | /* (formerly "bcypher_mode", changed to avoid confusion) */ |
|---|
| 264 | bcrypto_data_format data_format; /* all, ts */ |
|---|
| 265 | bcrypto_hardware_ctrl hardware_ctrl; /* any, software only, m2m only, keyladder only, etc. */ |
|---|
| 266 | bool multiple_hashes; /* allows hashing functions to occur one block at a time; requires an extra closing process call with in_buffer == NULL to close out the hash */ |
|---|
| 267 | } bcrypto_settings; |
|---|
| 268 | |
|---|
| 269 | /* |
|---|
| 270 | Summary: |
|---|
| 271 | BCrypt status structure |
|---|
| 272 | Description: |
|---|
| 273 | This returns information about the algorithm chosen. It can be used to dynamically allocate output blocks, but it is |
|---|
| 274 | strongly recommended that the bcrypto user know the algorithms already. |
|---|
| 275 | |
|---|
| 276 | If matching_output_size is set, then the output buffer for bcrypto_process needs to be the same size as the input buffer. |
|---|
| 277 | |
|---|
| 278 | If fixed_output_size is set, then the output buffer needs to be of a specific, fixed length, regardless of the size of the |
|---|
| 279 | input buffer. |
|---|
| 280 | */ |
|---|
| 281 | typedef struct bcrypto_status_t { |
|---|
| 282 | bool matching_output_size; /* true for block ciphers, where the output block length should match the input block length */ |
|---|
| 283 | bool fixed_output_size; /* true for hash, sign, and verify operations */ |
|---|
| 284 | int fixed_output_length; /* if fixed_output_size is true, this holds the expected output size */ |
|---|
| 285 | } bcrypto_status_t; |
|---|
| 286 | |
|---|
| 287 | /* |
|---|
| 288 | Summary: |
|---|
| 289 | BCrypt structure |
|---|
| 290 | Description: |
|---|
| 291 | This is an opaque structure containing internal state for bcrypto operations. |
|---|
| 292 | */ |
|---|
| 293 | typedef struct bcrypto *bcrypto_t; |
|---|
| 294 | |
|---|
| 295 | /* |
|---|
| 296 | Summary: |
|---|
| 297 | BCrypt settings initialization |
|---|
| 298 | Description: |
|---|
| 299 | This function initializes settings to safe, default values. The bcrypto user will still need to |
|---|
| 300 | populate the structure with algorithm choices, keys, and any other necessary settings after this call. |
|---|
| 301 | */ |
|---|
| 302 | void bcrypto_settings_init( |
|---|
| 303 | bcrypto_settings *settings |
|---|
| 304 | ); |
|---|
| 305 | |
|---|
| 306 | /* |
|---|
| 307 | Summary: |
|---|
| 308 | BCrypt open call |
|---|
| 309 | Description: |
|---|
| 310 | This creates a bcrypto object and populates it from the passed in bcrypto_settings. |
|---|
| 311 | */ |
|---|
| 312 | bcrypto_t bcrypto_open ( |
|---|
| 313 | const bcrypto_settings *settings |
|---|
| 314 | ); |
|---|
| 315 | |
|---|
| 316 | /* |
|---|
| 317 | Summary: |
|---|
| 318 | BCrypt status call |
|---|
| 319 | Description: |
|---|
| 320 | This retrieves status information about a current bcrypto session. Its primary use is to find out |
|---|
| 321 | algorithmic requirements. |
|---|
| 322 | */ |
|---|
| 323 | bresult bcrypto_status ( |
|---|
| 324 | bcrypto_t crypt, |
|---|
| 325 | bcrypto_status_t *status |
|---|
| 326 | ); |
|---|
| 327 | |
|---|
| 328 | /* |
|---|
| 329 | Summary: |
|---|
| 330 | BCrypt process call |
|---|
| 331 | Description: |
|---|
| 332 | This processes a chunk of data and, where appropriate, returns the result. |
|---|
| 333 | */ |
|---|
| 334 | bresult bcrypto_process ( |
|---|
| 335 | bcrypto_t crypt, |
|---|
| 336 | uint8_t *in_buffer, |
|---|
| 337 | unsigned long in_buffer_length, |
|---|
| 338 | uint8_t *out_buffer, |
|---|
| 339 | unsigned long out_buffer_length |
|---|
| 340 | ); |
|---|
| 341 | |
|---|
| 342 | /* |
|---|
| 343 | Summary: |
|---|
| 344 | BCrypt close call |
|---|
| 345 | Description: |
|---|
| 346 | This closes out a bcrypto session. |
|---|
| 347 | */ |
|---|
| 348 | void bcrypto_close ( |
|---|
| 349 | bcrypto_t crypt |
|---|
| 350 | ); |
|---|
| 351 | |
|---|
| 352 | /* |
|---|
| 353 | Summary: |
|---|
| 354 | BCrypt m2m/dma memory allocation |
|---|
| 355 | Description: |
|---|
| 356 | This allocates a buffer suitable for use with m2m hardware encryption. |
|---|
| 357 | |
|---|
| 358 | This function is probably not suitable for here, and the m2m/dma support should probably be handled differently. |
|---|
| 359 | */ |
|---|
| 360 | uint8_t *bcrypto_alloc( |
|---|
| 361 | unsigned buffer_length |
|---|
| 362 | ); |
|---|
| 363 | |
|---|
| 364 | /* |
|---|
| 365 | Summary: |
|---|
| 366 | BCrypt m2m/dma memory de-allocation |
|---|
| 367 | Description: |
|---|
| 368 | This frees a buffer suitable for use with m2m hardware encryption. |
|---|
| 369 | |
|---|
| 370 | This function is probably not suitable for here, and the m2m/dma support should probably be handled differently. |
|---|
| 371 | */ |
|---|
| 372 | void bcrypto_free( |
|---|
| 373 | uint8_t *buffer |
|---|
| 374 | ); |
|---|
| 375 | |
|---|
| 376 | #ifdef __cplusplus |
|---|
| 377 | } |
|---|
| 378 | #endif |
|---|
| 379 | |
|---|
| 380 | #endif /* BSETTOP_CRYPTO_H__ */ |
|---|