1d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 2d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Ultra Wide Band 3d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * AES-128 CCM Encryption 4d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 5d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Copyright (C) 2007 Intel Corporation 6d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> 7d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 8d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * This program is free software; you can redistribute it and/or 9d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * modify it under the terms of the GNU General Public License version 10d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 2 as published by the Free Software Foundation. 11d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 12d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * This program is distributed in the hope that it will be useful, 13d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * but WITHOUT ANY WARRANTY; without even the implied warranty of 14d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * GNU General Public License for more details. 16d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 17d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * You should have received a copy of the GNU General Public License 18d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * along with this program; if not, write to the Free Software 19d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 20d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 02110-1301, USA. 21d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 22d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 23d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * We don't do any encryption here; we use the Linux Kernel's AES-128 24d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * crypto modules to construct keys and payload blocks in a way 25d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * defined by WUSB1.0[6]. Check the erratas, as typos are are patched 26d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * there. 27d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 28d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Thanks a zillion to John Keys for his help and clarifications over 29d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the designed-by-a-committee text. 30d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 31d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * So the idea is that there is this basic Pseudo-Random-Function 32d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * defined in WUSB1.0[6.5] which is the core of everything. It works 33d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * by tweaking some blocks, AES crypting them and then xoring 34d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * something else with them (this seems to be called CBC(AES) -- can 35d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * you tell I know jack about crypto?). So we just funnel it into the 36d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Linux Crypto API. 37d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 38d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * We leave a crypto test module so we can verify that vectors match, 39d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * every now and then. 40d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 41d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Block size: 16 bytes -- AES seems to do things in 'block sizes'. I 42d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * am learning a lot... 43d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 44d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Conveniently, some data structures that need to be 45d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * funneled through AES are...16 bytes in size! 46d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 47d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 48d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/crypto.h> 49d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/module.h> 50d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/err.h> 51d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/uwb.h> 525a0e3ad6af8660be21ca98a971cd00f331318c05Tejun Heo#include <linux/slab.h> 53d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/usb/wusb.h> 54d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez#include <linux/scatterlist.h> 55d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 56d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabelstatic int debug_crypto_verify = 0; 57d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel 58d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabelmodule_param(debug_crypto_verify, int, 0); 59d409f3bf47c5e5ae10601d079204e263bc176bcfDavid VrabelMODULE_PARM_DESC(debug_crypto_verify, "verify the key generation algorithms"); 60d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 61e43ace891229607c43d35597cbba77c2e40f48d4David Vrabelstatic void wusb_key_dump(const void *buf, size_t len) 62e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel{ 63e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel print_hex_dump(KERN_ERR, " ", DUMP_PREFIX_OFFSET, 16, 1, 64e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel buf, len, 0); 65e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel} 66e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel 67d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 68d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Block of data, as understood by AES-CCM 69d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 70d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * The code assumes this structure is nothing but a 16 byte array 71d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * (packed in a struct to avoid common mess ups that I usually do with 72d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * arrays and enforcing type checking). 73d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 74d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstruct aes_ccm_block { 75d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u8 data[16]; 76d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} __attribute__((packed)); 77d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 78d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 79d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Counter-mode Blocks (WUSB1.0[6.4]) 80d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 81d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * According to CCM (or so it seems), for the purpose of calculating 82d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the MIC, the message is broken in N counter-mode blocks, B0, B1, 83d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * ... BN. 84d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 85d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * B0 contains flags, the CCM nonce and l(m). 86d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 87d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * B1 contains l(a), the MAC header, the encryption offset and padding. 88d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 89d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * If EO is nonzero, additional blocks are built from payload bytes 901076e7a4d91230eb277735ce297fe544c0202d30Rahul Bedarkar * until EO is exhausted (FIXME: padding to 16 bytes, I guess). The 91d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * padding is not xmitted. 92d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 93d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 94d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* WUSB1.0[T6.4] */ 95d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstruct aes_ccm_b0 { 96d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u8 flags; /* 0x59, per CCM spec */ 97d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct aes_ccm_nonce ccm_nonce; 98d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez __be16 lm; 99d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} __attribute__((packed)); 100d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 101d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* WUSB1.0[T6.5] */ 102d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstruct aes_ccm_b1 { 103d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez __be16 la; 104d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u8 mac_header[10]; 105d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez __le16 eo; 106d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u8 security_reserved; /* This is always zero */ 107d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u8 padding; /* 0 */ 108d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} __attribute__((packed)); 109d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 110d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 111d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Encryption Blocks (WUSB1.0[6.4.4]) 112d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 113d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * CCM uses Ax blocks to generate a keystream with which the MIC and 114d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the message's payload are encoded. A0 always encrypts/decrypts the 115af901ca181d92aac3a7dc265144a9081a86d8f39André Goddard Rosa * MIC. Ax (x>0) are used for the successive payload blocks. 116d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 117d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * The x is the counter, and is increased for each block. 118d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 119d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstruct aes_ccm_a { 120d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u8 flags; /* 0x01, per CCM spec */ 121d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct aes_ccm_nonce ccm_nonce; 122d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez __be16 counter; /* Value of x */ 123d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} __attribute__((packed)); 124d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 125d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic void bytewise_xor(void *_bo, const void *_bi1, const void *_bi2, 126d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez size_t size) 127d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{ 128d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u8 *bo = _bo; 129d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez const u8 *bi1 = _bi1, *bi2 = _bi2; 130d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez size_t itr; 131d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez for (itr = 0; itr < size; itr++) 132d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez bo[itr] = bi1[itr] ^ bi2[itr]; 133d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} 134d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 135d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 136d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * CC-MAC function WUSB1.0[6.5] 137d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 138d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Take a data string and produce the encrypted CBC Counter-mode MIC 139d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 140d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Note the names for most function arguments are made to (more or 141d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * less) match those used in the pseudo-function definition given in 142d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * WUSB1.0[6.5]. 143d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 144d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @tfm_cbc: CBC(AES) blkcipher handle (initialized) 145d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 146d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @tfm_aes: AES cipher handle (initialized) 147d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 148d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @mic: buffer for placing the computed MIC (Message Integrity 149d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Code). This is exactly 8 bytes, and we expect the buffer to 150d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * be at least eight bytes in length. 151d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 152d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @key: 128 bit symmetric key 153d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 154d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @n: CCM nonce 155d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 156d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @a: ASCII string, 14 bytes long (I guess zero padded if needed; 157d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * we use exactly 14 bytes). 158d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 159d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @b: data stream to be processed; cannot be a global or const local 160d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * (will confuse the scatterlists) 161d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 162d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @blen: size of b... 163d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 164d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Still not very clear how this is done, but looks like this: we 165d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * create block B0 (as WUSB1.0[6.5] says), then we AES-crypt it with 166d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @key. We bytewise xor B0 with B1 (1) and AES-crypt that. Then we 167d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * take the payload and divide it in blocks (16 bytes), xor them with 168d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the previous crypto result (16 bytes) and crypt it, repeat the next 169d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * block with the output of the previous one, rinse wash (I guess this 170d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * is what AES CBC mode means...but I truly have no idea). So we use 171d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the CBC(AES) blkcipher, that does precisely that. The IV (Initial 172d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Vector) is 16 bytes and is set to zero, so 173d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 174d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * See rfc3610. Linux crypto has a CBC implementation, but the 175d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * documentation is scarce, to say the least, and the example code is 176d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * so intricated that is difficult to understand how things work. Most 177d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * of this is guess work -- bite me. 178d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 179d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * (1) Created as 6.5 says, again, using as l(a) 'Blen + 14', and 180d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * using the 14 bytes of @a to fill up 181d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * b1.{mac_header,e0,security_reserved,padding}. 182d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 18325985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * NOTE: The definition of l(a) in WUSB1.0[6.5] vs the definition of 184d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * l(m) is orthogonal, they bear no relationship, so it is not 185d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * in conflict with the parameter's relation that 186d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * WUSB1.0[6.4.2]) defines. 187d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 188d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * NOTE: WUSB1.0[A.1]: Host Nonce is missing a nibble? (1e); fixed in 189d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * first errata released on 2005/07. 190d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 191d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * NOTE: we need to clean IV to zero at each invocation to make sure 192d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * we start with a fresh empty Initial Vector, so that the CBC 193d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * works ok. 194d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 195d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * NOTE: blen is not aligned to a block size, we'll pad zeros, that's 196d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * what sg[4] is for. Maybe there is a smarter way to do this. 197d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 198d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic int wusb_ccm_mac(struct crypto_blkcipher *tfm_cbc, 199d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct crypto_cipher *tfm_aes, void *mic, 200d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez const struct aes_ccm_nonce *n, 201d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez const struct aes_ccm_label *a, const void *b, 202d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez size_t blen) 203d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{ 204d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez int result = 0; 205d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct blkcipher_desc desc; 206d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct aes_ccm_b0 b0; 207d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct aes_ccm_b1 b1; 208d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct aes_ccm_a ax; 209d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct scatterlist sg[4], sg_dst; 210d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez void *iv, *dst_buf; 211d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez size_t ivsize, dst_size; 212d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez const u8 bzero[16] = { 0 }; 213d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez size_t zero_padding; 214d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 215d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez /* 216d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * These checks should be compile time optimized out 217d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * ensure @a fills b1's mac_header and following fields 218d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 219d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez WARN_ON(sizeof(*a) != sizeof(b1) - sizeof(b1.la)); 220d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez WARN_ON(sizeof(b0) != sizeof(struct aes_ccm_block)); 221d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez WARN_ON(sizeof(b1) != sizeof(struct aes_ccm_block)); 222d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez WARN_ON(sizeof(ax) != sizeof(struct aes_ccm_block)); 223d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 224d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = -ENOMEM; 225d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez zero_padding = blen % sizeof(struct aes_ccm_block); 226d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (zero_padding) 227d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez zero_padding = sizeof(struct aes_ccm_block) - zero_padding; 228d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez dst_size = blen + sizeof(b0) + sizeof(b1) + zero_padding; 229d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez dst_buf = kzalloc(dst_size, GFP_KERNEL); 230d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (dst_buf == NULL) { 231d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: can't alloc destination buffer\n"); 232d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez goto error_dst_buf; 233d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 234d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 235d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez iv = crypto_blkcipher_crt(tfm_cbc)->iv; 236d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez ivsize = crypto_blkcipher_ivsize(tfm_cbc); 237d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez memset(iv, 0, ivsize); 238d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 239d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez /* Setup B0 */ 240d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez b0.flags = 0x59; /* Format B0 */ 241d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez b0.ccm_nonce = *n; 242d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez b0.lm = cpu_to_be16(0); /* WUSB1.0[6.5] sez l(m) is 0 */ 243d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 244d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez /* Setup B1 245d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 246d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * The WUSB spec is anything but clear! WUSB1.0[6.5] 247d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * says that to initialize B1 from A with 'l(a) = blen + 248d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 14'--after clarification, it means to use A's contents 249d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * for MAC Header, EO, sec reserved and padding. 250d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 251d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez b1.la = cpu_to_be16(blen + 14); 252d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez memcpy(&b1.mac_header, a, sizeof(*a)); 253d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 254d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez sg_init_table(sg, ARRAY_SIZE(sg)); 255d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez sg_set_buf(&sg[0], &b0, sizeof(b0)); 256d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez sg_set_buf(&sg[1], &b1, sizeof(b1)); 257d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez sg_set_buf(&sg[2], b, blen); 258d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez /* 0 if well behaved :) */ 259d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez sg_set_buf(&sg[3], bzero, zero_padding); 260d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez sg_init_one(&sg_dst, dst_buf, dst_size); 261d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 262d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez desc.tfm = tfm_cbc; 263d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez desc.flags = 0; 264d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = crypto_blkcipher_encrypt(&desc, &sg_dst, sg, dst_size); 265d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (result < 0) { 266d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: can't compute CBC-MAC tag (MIC): %d\n", 267d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result); 268d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez goto error_cbc_crypt; 269d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 270d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 271d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez /* Now we crypt the MIC Tag (*iv) with Ax -- values per WUSB1.0[6.5] 272d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * The procedure is to AES crypt the A0 block and XOR the MIC 27325985edcedea6396277003854657b5f3cb31a628Lucas De Marchi * Tag against it; we only do the first 8 bytes and place it 274d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * directly in the destination buffer. 275d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 276d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * POS Crypto API: size is assumed to be AES's block size. 277d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Thanks for documenting it -- tip taken from airo.c 278d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 279d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez ax.flags = 0x01; /* as per WUSB 1.0 spec */ 280d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez ax.ccm_nonce = *n; 281d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez ax.counter = 0; 282d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez crypto_cipher_encrypt_one(tfm_aes, (void *)&ax, (void *)&ax); 283d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez bytewise_xor(mic, &ax, iv, 8); 284d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = 8; 285d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_cbc_crypt: 286d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez kfree(dst_buf); 287d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_dst_buf: 288d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez return result; 289d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} 290d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 291d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 292d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * WUSB Pseudo Random Function (WUSB1.0[6.5]) 293d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 294d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * @b: buffer to the source data; cannot be a global or const local 295d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * (will confuse the scatterlists) 296d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 297d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezssize_t wusb_prf(void *out, size_t out_size, 298d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez const u8 key[16], const struct aes_ccm_nonce *_n, 299d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez const struct aes_ccm_label *a, 300d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez const void *b, size_t blen, size_t len) 301d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{ 302d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez ssize_t result, bytes = 0, bitr; 303d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct aes_ccm_nonce n = *_n; 304d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct crypto_blkcipher *tfm_cbc; 305d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct crypto_cipher *tfm_aes; 306d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u64 sfn = 0; 307d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez __le64 sfn_le; 308d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 309d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez tfm_cbc = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC); 310d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (IS_ERR(tfm_cbc)) { 311d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = PTR_ERR(tfm_cbc); 312d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: can't load CBC(AES): %d\n", (int)result); 313d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez goto error_alloc_cbc; 314d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 315d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = crypto_blkcipher_setkey(tfm_cbc, key, 16); 316d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (result < 0) { 317d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: can't set CBC key: %d\n", (int)result); 318d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez goto error_setkey_cbc; 319d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 320d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 321d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC); 322d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (IS_ERR(tfm_aes)) { 323d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = PTR_ERR(tfm_aes); 324d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: can't load AES: %d\n", (int)result); 325d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez goto error_alloc_aes; 326d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 327d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = crypto_cipher_setkey(tfm_aes, key, 16); 328d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (result < 0) { 329d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: can't set AES key: %d\n", (int)result); 330d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez goto error_setkey_aes; 331d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 332d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 333d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez for (bitr = 0; bitr < (len + 63) / 64; bitr++) { 334d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez sfn_le = cpu_to_le64(sfn++); 335d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez memcpy(&n.sfn, &sfn_le, sizeof(n.sfn)); /* n.sfn++... */ 336d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = wusb_ccm_mac(tfm_cbc, tfm_aes, out + bytes, 337d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez &n, a, b, blen); 338d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (result < 0) 339d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez goto error_ccm_mac; 340d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez bytes += result; 341d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 342d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = bytes; 343d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_ccm_mac: 344d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_setkey_aes: 345d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez crypto_free_cipher(tfm_aes); 346d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_alloc_aes: 347d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_setkey_cbc: 348d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez crypto_free_blkcipher(tfm_cbc); 349d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezerror_alloc_cbc: 350d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez return result; 351d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} 352d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 353d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* WUSB1.0[A.2] test vectors */ 354d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const u8 stv_hsmic_key[16] = { 355d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x4b, 0x79, 0xa3, 0xcf, 0xe5, 0x53, 0x23, 0x9d, 356d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0xd7, 0xc1, 0x6d, 0x1c, 0x2d, 0xab, 0x6d, 0x3f 357d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}; 358d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 359d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const struct aes_ccm_nonce stv_hsmic_n = { 360d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .sfn = { 0 }, 361d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .tkid = { 0x76, 0x98, 0x01, }, 362d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .dest_addr = { .data = { 0xbe, 0x00 } }, 363d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .src_addr = { .data = { 0x76, 0x98 } }, 364d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}; 365d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 366d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 367d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Out-of-band MIC Generation verification code 368d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 369d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 370d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic int wusb_oob_mic_verify(void) 371d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{ 372d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez int result; 373d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez u8 mic[8]; 374d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez /* WUSB1.0[A.2] test vectors 375d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 376d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Need to keep it in the local stack as GCC 4.1.3something 377d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * messes up and generates noise. 378d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 379d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct usb_handshake stv_hsmic_hs = { 380d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .bMessageNumber = 2, 381d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .bStatus = 00, 382d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .tTKID = { 0x76, 0x98, 0x01 }, 383d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .bReserved = 00, 384d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .CDID = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 385d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 386d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x3c, 0x3d, 0x3e, 0x3f }, 387d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .nonce = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 388d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 389d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x2c, 0x2d, 0x2e, 0x2f }, 390d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .MIC = { 0x75, 0x6a, 0x97, 0x51, 0x0c, 0x8c, 391d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x14, 0x7b } , 392d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez }; 393d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez size_t hs_size; 394d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 395d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = wusb_oob_mic(mic, stv_hsmic_key, &stv_hsmic_n, &stv_hsmic_hs); 396d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (result < 0) 397d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: WUSB OOB MIC test: failed: %d\n", result); 398d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez else if (memcmp(stv_hsmic_hs.MIC, mic, sizeof(mic))) { 399d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: OOB MIC test: " 400d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez "mismatch between MIC result and WUSB1.0[A2]\n"); 401d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez hs_size = sizeof(stv_hsmic_hs) - sizeof(stv_hsmic_hs.MIC); 402d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: Handshake2 in: (%zu bytes)\n", hs_size); 403e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump(&stv_hsmic_hs, hs_size); 404d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: CCM Nonce in: (%zu bytes)\n", 405d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez sizeof(stv_hsmic_n)); 406e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump(&stv_hsmic_n, sizeof(stv_hsmic_n)); 407d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: MIC out:\n"); 408e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump(mic, sizeof(mic)); 409d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: MIC out (from WUSB1.0[A.2]):\n"); 410e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump(stv_hsmic_hs.MIC, sizeof(stv_hsmic_hs.MIC)); 411d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = -EINVAL; 412d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } else 413d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = 0; 414d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez return result; 415d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} 416d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 417d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 418d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Test vectors for Key derivation 419d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 420d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * These come from WUSB1.0[6.5.1], the vectors in WUSB1.0[A.1] 421d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * (errata corrected in 2005/07). 422d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 423d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const u8 stv_key_a1[16] __attribute__ ((__aligned__(4))) = { 424d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87, 425d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x78, 0x69, 0x5a, 0x4b, 0x3c, 0x2d, 0x1e, 0x0f 426d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}; 427d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 428d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const struct aes_ccm_nonce stv_keydvt_n_a1 = { 429d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .sfn = { 0 }, 430d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .tkid = { 0x76, 0x98, 0x01, }, 431d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .dest_addr = { .data = { 0xbe, 0x00 } }, 432d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .src_addr = { .data = { 0x76, 0x98 } }, 433d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}; 434d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 435d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic const struct wusb_keydvt_out stv_keydvt_out_a1 = { 436d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .kck = { 437d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x4b, 0x79, 0xa3, 0xcf, 0xe5, 0x53, 0x23, 0x9d, 438d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0xd7, 0xc1, 0x6d, 0x1c, 0x2d, 0xab, 0x6d, 0x3f 439d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez }, 440d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .ptk = { 441d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0xc8, 0x70, 0x62, 0x82, 0xb6, 0x7c, 0xe9, 0x06, 442d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x7b, 0xc5, 0x25, 0x69, 0xf2, 0x36, 0x61, 0x2d 443d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 444d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez}; 445d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 446d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 447d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Performa a test to make sure we match the vectors defined in 448d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * WUSB1.0[A.1](Errata2006/12) 449d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 450d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezstatic int wusb_key_derive_verify(void) 451d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{ 452d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez int result = 0; 453d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct wusb_keydvt_out keydvt_out; 454d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez /* These come from WUSB1.0[A.1] + 2006/12 errata 455d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * NOTE: can't make this const or global -- somehow it seems 456d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * the scatterlists for crypto get confused and we get 457d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * bad data. There is no doc on this... */ 458d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez struct wusb_keydvt_in stv_keydvt_in_a1 = { 459d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .hnonce = { 460d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 461d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f 462d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez }, 463d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez .dnonce = { 464d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 465d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f 466d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } 467d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez }; 468d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 469d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = wusb_key_derive(&keydvt_out, stv_key_a1, &stv_keydvt_n_a1, 470d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez &stv_keydvt_in_a1); 471d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (result < 0) 472d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: WUSB key derivation test: " 473d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez "derivation failed: %d\n", result); 474d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez if (memcmp(&stv_keydvt_out_a1, &keydvt_out, sizeof(keydvt_out))) { 475d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: WUSB key derivation test: " 476d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez "mismatch between key derivation result " 477d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez "and WUSB1.0[A1] Errata 2006/12\n"); 478e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel printk(KERN_ERR "E: keydvt in: key\n"); 479e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump(stv_key_a1, sizeof(stv_key_a1)); 480e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel printk(KERN_ERR "E: keydvt in: nonce\n"); 481e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump( &stv_keydvt_n_a1, sizeof(stv_keydvt_n_a1)); 482e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel printk(KERN_ERR "E: keydvt in: hnonce & dnonce\n"); 483e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump(&stv_keydvt_in_a1, sizeof(stv_keydvt_in_a1)); 484d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: keydvt out: KCK\n"); 485e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump(&keydvt_out.kck, sizeof(keydvt_out.kck)); 486d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez printk(KERN_ERR "E: keydvt out: PTK\n"); 487e43ace891229607c43d35597cbba77c2e40f48d4David Vrabel wusb_key_dump(&keydvt_out.ptk, sizeof(keydvt_out.ptk)); 488d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = -EINVAL; 489d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez } else 490d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez result = 0; 491d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez return result; 492d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} 493d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 494d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez/* 495d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * Initialize crypto system 496d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * 497d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * FIXME: we do nothing now, other than verifying. Later on we'll 498d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez * cache the encryption stuff, so that's why we have a separate init. 499d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez */ 500d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezint wusb_crypto_init(void) 501d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{ 502d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez int result; 503d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 504d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel if (debug_crypto_verify) { 505d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel result = wusb_key_derive_verify(); 506d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel if (result < 0) 507d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel return result; 508d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel return wusb_oob_mic_verify(); 509d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel } 510d409f3bf47c5e5ae10601d079204e263bc176bcfDavid Vrabel return 0; 511d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} 512d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez 513d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalezvoid wusb_crypto_exit(void) 514d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez{ 515d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez /* FIXME: free cached crypto transforms */ 516d59db761b8559f07a7161ca3387d6c6949667edeInaky Perez-Gonzalez} 517