Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
| 3 | * (C) Copyright 2010 - 2011 NVIDIA Corporation <www.nvidia.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/errno.h> |
| 10 | #include "crypto.h" |
| 11 | #include "aes.h" |
| 12 | |
| 13 | static u8 zero_key[16]; |
| 14 | |
| 15 | #define AES_CMAC_CONST_RB 0x87 /* from RFC 4493, Figure 2.2 */ |
| 16 | |
| 17 | enum security_op { |
| 18 | SECURITY_SIGN = 1 << 0, /* Sign the data */ |
| 19 | SECURITY_ENCRYPT = 1 << 1, /* Encrypt the data */ |
| 20 | }; |
| 21 | |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 22 | /** |
| 23 | * Shift a vector left by one bit |
| 24 | * |
| 25 | * \param in Input vector |
| 26 | * \param out Output vector |
| 27 | * \param size Length of vector in bytes |
| 28 | */ |
| 29 | static void left_shift_vector(u8 *in, u8 *out, int size) |
| 30 | { |
| 31 | int carry = 0; |
| 32 | int i; |
| 33 | |
| 34 | for (i = size - 1; i >= 0; i--) { |
| 35 | out[i] = (in[i] << 1) | carry; |
| 36 | carry = in[i] >> 7; /* get most significant bit */ |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Sign a block of data, putting the result into dst. |
| 42 | * |
| 43 | * \param key Input AES key, length AES_KEY_LENGTH |
| 44 | * \param key_schedule Expanded key to use |
| 45 | * \param src Source data of length 'num_aes_blocks' blocks |
| 46 | * \param dst Destination buffer, length AES_KEY_LENGTH |
| 47 | * \param num_aes_blocks Number of AES blocks to encrypt |
| 48 | */ |
| 49 | static void sign_object(u8 *key, u8 *key_schedule, u8 *src, u8 *dst, |
| 50 | u32 num_aes_blocks) |
| 51 | { |
| 52 | u8 tmp_data[AES_KEY_LENGTH]; |
| 53 | u8 left[AES_KEY_LENGTH]; |
| 54 | u8 k1[AES_KEY_LENGTH]; |
| 55 | u8 *cbc_chain_data; |
| 56 | unsigned i; |
| 57 | |
| 58 | cbc_chain_data = zero_key; /* Convenient array of 0's for IV */ |
| 59 | |
| 60 | /* compute K1 constant needed by AES-CMAC calculation */ |
| 61 | for (i = 0; i < AES_KEY_LENGTH; i++) |
| 62 | tmp_data[i] = 0; |
| 63 | |
Marek Vasut | 6e7b9f4 | 2014-03-05 19:58:37 +0100 | [diff] [blame^] | 64 | aes_cbc_encrypt_blocks(key_schedule, tmp_data, left, 1); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 65 | debug_print_vector("AES(key, nonce)", AES_KEY_LENGTH, left); |
| 66 | |
| 67 | left_shift_vector(left, k1, sizeof(left)); |
| 68 | debug_print_vector("L", AES_KEY_LENGTH, left); |
| 69 | |
| 70 | if ((left[0] >> 7) != 0) /* get MSB of L */ |
| 71 | k1[AES_KEY_LENGTH-1] ^= AES_CMAC_CONST_RB; |
| 72 | debug_print_vector("K1", AES_KEY_LENGTH, k1); |
| 73 | |
| 74 | /* compute the AES-CMAC value */ |
| 75 | for (i = 0; i < num_aes_blocks; i++) { |
| 76 | /* Apply the chain data */ |
| 77 | apply_cbc_chain_data(cbc_chain_data, src, tmp_data); |
| 78 | |
| 79 | /* for the final block, XOR K1 into the IV */ |
| 80 | if (i == num_aes_blocks - 1) |
| 81 | apply_cbc_chain_data(tmp_data, k1, tmp_data); |
| 82 | |
| 83 | /* encrypt the AES block */ |
| 84 | aes_encrypt(tmp_data, key_schedule, dst); |
| 85 | |
| 86 | debug("sign_obj: block %d of %d\n", i, num_aes_blocks); |
| 87 | debug_print_vector("AES-CMAC Src", AES_KEY_LENGTH, src); |
| 88 | debug_print_vector("AES-CMAC Xor", AES_KEY_LENGTH, tmp_data); |
| 89 | debug_print_vector("AES-CMAC Dst", AES_KEY_LENGTH, dst); |
| 90 | |
| 91 | /* Update pointers for next loop. */ |
| 92 | cbc_chain_data = dst; |
| 93 | src += AES_KEY_LENGTH; |
| 94 | } |
| 95 | |
| 96 | debug_print_vector("AES-CMAC Hash", AES_KEY_LENGTH, dst); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Encrypt and sign a block of data (depending on security mode). |
| 101 | * |
| 102 | * \param key Input AES key, length AES_KEY_LENGTH |
| 103 | * \param oper Security operations mask to perform (enum security_op) |
| 104 | * \param src Source data |
| 105 | * \param length Size of source data |
| 106 | * \param sig_dst Destination address for signature, AES_KEY_LENGTH bytes |
| 107 | */ |
| 108 | static int encrypt_and_sign(u8 *key, enum security_op oper, u8 *src, |
| 109 | u32 length, u8 *sig_dst) |
| 110 | { |
| 111 | u32 num_aes_blocks; |
| 112 | u8 key_schedule[AES_EXPAND_KEY_LENGTH]; |
| 113 | |
| 114 | debug("encrypt_and_sign: length = %d\n", length); |
| 115 | debug_print_vector("AES key", AES_KEY_LENGTH, key); |
| 116 | |
| 117 | /* |
| 118 | * The only need for a key is for signing/checksum purposes, so |
| 119 | * if not encrypting, expand a key of 0s. |
| 120 | */ |
| 121 | aes_expand_key(oper & SECURITY_ENCRYPT ? key : zero_key, key_schedule); |
| 122 | |
| 123 | num_aes_blocks = (length + AES_KEY_LENGTH - 1) / AES_KEY_LENGTH; |
| 124 | |
| 125 | if (oper & SECURITY_ENCRYPT) { |
| 126 | /* Perform this in place, resulting in src being encrypted. */ |
| 127 | debug("encrypt_and_sign: begin encryption\n"); |
Marek Vasut | 6e7b9f4 | 2014-03-05 19:58:37 +0100 | [diff] [blame^] | 128 | aes_cbc_encrypt_blocks(key_schedule, src, src, num_aes_blocks); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 129 | debug("encrypt_and_sign: end encryption\n"); |
| 130 | } |
| 131 | |
| 132 | if (oper & SECURITY_SIGN) { |
| 133 | /* encrypt the data, overwriting the result in signature. */ |
| 134 | debug("encrypt_and_sign: begin signing\n"); |
| 135 | sign_object(key, key_schedule, src, sig_dst, num_aes_blocks); |
| 136 | debug("encrypt_and_sign: end signing\n"); |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | int sign_data_block(u8 *source, unsigned length, u8 *signature) |
| 143 | { |
| 144 | return encrypt_and_sign(zero_key, SECURITY_SIGN, source, |
| 145 | length, signature); |
| 146 | } |