Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
| 4 | * (C) Copyright 2010 - 2011 NVIDIA Corporation <www.nvidia.com> |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 8 | #include <log.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 9 | #include <linux/errno.h> |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 10 | #include "crypto.h" |
Stefano Babic | b80c0b9 | 2017-04-05 18:08:00 +0200 | [diff] [blame] | 11 | #include "uboot_aes.h" |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 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 | * |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 43 | * \param key Input AES key, length AES128_KEY_LENGTH |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 44 | * \param key_schedule Expanded key to use |
| 45 | * \param src Source data of length 'num_aes_blocks' blocks |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 46 | * \param dst Destination buffer, length AES128_KEY_LENGTH |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 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 | { |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 52 | u8 tmp_data[AES128_KEY_LENGTH]; |
| 53 | u8 iv[AES128_KEY_LENGTH] = {0}; |
| 54 | u8 left[AES128_KEY_LENGTH]; |
| 55 | u8 k1[AES128_KEY_LENGTH]; |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 56 | u8 *cbc_chain_data; |
| 57 | unsigned i; |
| 58 | |
| 59 | cbc_chain_data = zero_key; /* Convenient array of 0's for IV */ |
| 60 | |
| 61 | /* compute K1 constant needed by AES-CMAC calculation */ |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 62 | for (i = 0; i < AES128_KEY_LENGTH; i++) |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 63 | tmp_data[i] = 0; |
| 64 | |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 65 | aes_cbc_encrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv, |
| 66 | tmp_data, left, 1); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 67 | |
| 68 | left_shift_vector(left, k1, sizeof(left)); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 69 | |
| 70 | if ((left[0] >> 7) != 0) /* get MSB of L */ |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 71 | k1[AES128_KEY_LENGTH - 1] ^= AES_CMAC_CONST_RB; |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 72 | |
| 73 | /* compute the AES-CMAC value */ |
| 74 | for (i = 0; i < num_aes_blocks; i++) { |
| 75 | /* Apply the chain data */ |
Stephen Warren | 53eb768 | 2014-04-18 10:28:58 -0600 | [diff] [blame] | 76 | aes_apply_cbc_chain_data(cbc_chain_data, src, tmp_data); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 77 | |
| 78 | /* for the final block, XOR K1 into the IV */ |
| 79 | if (i == num_aes_blocks - 1) |
Stephen Warren | 53eb768 | 2014-04-18 10:28:58 -0600 | [diff] [blame] | 80 | aes_apply_cbc_chain_data(tmp_data, k1, tmp_data); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 81 | |
| 82 | /* encrypt the AES block */ |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 83 | aes_encrypt(AES128_KEY_LENGTH, tmp_data, |
| 84 | key_schedule, dst); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 85 | |
| 86 | debug("sign_obj: block %d of %d\n", i, num_aes_blocks); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 87 | |
| 88 | /* Update pointers for next loop. */ |
| 89 | cbc_chain_data = dst; |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 90 | src += AES128_KEY_LENGTH; |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 91 | } |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Encrypt and sign a block of data (depending on security mode). |
| 96 | * |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 97 | * \param key Input AES key, length AES128_KEY_LENGTH |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 98 | * \param oper Security operations mask to perform (enum security_op) |
| 99 | * \param src Source data |
| 100 | * \param length Size of source data |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 101 | * \param sig_dst Destination address for signature, AES128_KEY_LENGTH bytes |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 102 | */ |
| 103 | static int encrypt_and_sign(u8 *key, enum security_op oper, u8 *src, |
| 104 | u32 length, u8 *sig_dst) |
| 105 | { |
| 106 | u32 num_aes_blocks; |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 107 | u8 key_schedule[AES128_EXPAND_KEY_LENGTH]; |
| 108 | u8 iv[AES128_KEY_LENGTH] = {0}; |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 109 | |
| 110 | debug("encrypt_and_sign: length = %d\n", length); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * The only need for a key is for signing/checksum purposes, so |
| 114 | * if not encrypting, expand a key of 0s. |
| 115 | */ |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 116 | aes_expand_key(oper & SECURITY_ENCRYPT ? key : zero_key, |
| 117 | AES128_KEY_LENGTH, key_schedule); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 118 | |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 119 | num_aes_blocks = (length + AES128_KEY_LENGTH - 1) / AES128_KEY_LENGTH; |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 120 | |
| 121 | if (oper & SECURITY_ENCRYPT) { |
| 122 | /* Perform this in place, resulting in src being encrypted. */ |
| 123 | debug("encrypt_and_sign: begin encryption\n"); |
Philippe Reynes | 8302d17 | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 124 | aes_cbc_encrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv, src, |
| 125 | src, num_aes_blocks); |
Yen Lin | 2a6f036 | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 126 | debug("encrypt_and_sign: end encryption\n"); |
| 127 | } |
| 128 | |
| 129 | if (oper & SECURITY_SIGN) { |
| 130 | /* encrypt the data, overwriting the result in signature. */ |
| 131 | debug("encrypt_and_sign: begin signing\n"); |
| 132 | sign_object(key, key_schedule, src, sig_dst, num_aes_blocks); |
| 133 | debug("encrypt_and_sign: end signing\n"); |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | int sign_data_block(u8 *source, unsigned length, u8 *signature) |
| 140 | { |
| 141 | return encrypt_and_sign(zero_key, SECURITY_SIGN, source, |
| 142 | length, signature); |
| 143 | } |