Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Yen Lin | c3813db | 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 | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 9 | #include <linux/errno.h> |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 10 | #include <asm/arch-tegra/crypto.h> |
Stefano Babic | 4a24308 | 2017-04-05 18:08:00 +0200 | [diff] [blame] | 11 | #include "uboot_aes.h" |
Yen Lin | c3813db | 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 */ |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 20 | SECURITY_DECRYPT = 1 << 2, /* Dectypt the data */ |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 23 | /** |
| 24 | * Shift a vector left by one bit |
| 25 | * |
| 26 | * \param in Input vector |
| 27 | * \param out Output vector |
| 28 | * \param size Length of vector in bytes |
| 29 | */ |
| 30 | static void left_shift_vector(u8 *in, u8 *out, int size) |
| 31 | { |
| 32 | int carry = 0; |
| 33 | int i; |
| 34 | |
| 35 | for (i = size - 1; i >= 0; i--) { |
| 36 | out[i] = (in[i] << 1) | carry; |
| 37 | carry = in[i] >> 7; /* get most significant bit */ |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Sign a block of data, putting the result into dst. |
| 43 | * |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 44 | * \param key Input AES key, length AES128_KEY_LENGTH |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 45 | * \param key_schedule Expanded key to use |
| 46 | * \param src Source data of length 'num_aes_blocks' blocks |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 47 | * \param dst Destination buffer, length AES128_KEY_LENGTH |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 48 | * \param num_aes_blocks Number of AES blocks to encrypt |
| 49 | */ |
| 50 | static void sign_object(u8 *key, u8 *key_schedule, u8 *src, u8 *dst, |
| 51 | u32 num_aes_blocks) |
| 52 | { |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 53 | u8 tmp_data[AES128_KEY_LENGTH]; |
| 54 | u8 iv[AES128_KEY_LENGTH] = {0}; |
| 55 | u8 left[AES128_KEY_LENGTH]; |
| 56 | u8 k1[AES128_KEY_LENGTH]; |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 57 | u8 *cbc_chain_data; |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 58 | unsigned int i; |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 59 | |
| 60 | cbc_chain_data = zero_key; /* Convenient array of 0's for IV */ |
| 61 | |
| 62 | /* compute K1 constant needed by AES-CMAC calculation */ |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 63 | for (i = 0; i < AES128_KEY_LENGTH; i++) |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 64 | tmp_data[i] = 0; |
| 65 | |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 66 | aes_cbc_encrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv, |
| 67 | tmp_data, left, 1); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 68 | |
| 69 | left_shift_vector(left, k1, sizeof(left)); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 70 | |
| 71 | if ((left[0] >> 7) != 0) /* get MSB of L */ |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 72 | k1[AES128_KEY_LENGTH - 1] ^= AES_CMAC_CONST_RB; |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 73 | |
| 74 | /* compute the AES-CMAC value */ |
| 75 | for (i = 0; i < num_aes_blocks; i++) { |
| 76 | /* Apply the chain data */ |
Stephen Warren | 398c70c | 2014-04-18 10:28:58 -0600 | [diff] [blame] | 77 | aes_apply_cbc_chain_data(cbc_chain_data, src, tmp_data); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 78 | |
| 79 | /* for the final block, XOR K1 into the IV */ |
| 80 | if (i == num_aes_blocks - 1) |
Stephen Warren | 398c70c | 2014-04-18 10:28:58 -0600 | [diff] [blame] | 81 | aes_apply_cbc_chain_data(tmp_data, k1, tmp_data); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 82 | |
| 83 | /* encrypt the AES block */ |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 84 | aes_encrypt(AES128_KEY_LENGTH, tmp_data, |
| 85 | key_schedule, dst); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 86 | |
| 87 | debug("sign_obj: block %d of %d\n", i, num_aes_blocks); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 88 | |
| 89 | /* Update pointers for next loop. */ |
| 90 | cbc_chain_data = dst; |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 91 | src += AES128_KEY_LENGTH; |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 92 | } |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 96 | * Decrypt, encrypt or sign a block of data (depending on security mode). |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 97 | * |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 98 | * \param key Input AES key, length AES128_KEY_LENGTH |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 99 | * \param oper Security operations mask to perform (enum security_op) |
| 100 | * \param src Source data |
| 101 | * \param length Size of source data |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 102 | * \param sig_dst Destination address for signature, AES128_KEY_LENGTH bytes |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 103 | */ |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 104 | static int tegra_crypto_core(u8 *key, enum security_op oper, u8 *src, |
| 105 | u32 length, u8 *sig_dst) |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 106 | { |
| 107 | u32 num_aes_blocks; |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 108 | u8 key_schedule[AES128_EXPAND_KEY_LENGTH]; |
| 109 | u8 iv[AES128_KEY_LENGTH] = {0}; |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 110 | |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 111 | debug("%s: length = %d\n", __func__, length); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 112 | |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 113 | aes_expand_key(key, AES128_KEY_LENGTH, key_schedule); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 114 | |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 115 | num_aes_blocks = (length + AES128_KEY_LENGTH - 1) / AES128_KEY_LENGTH; |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 116 | |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 117 | if (oper & SECURITY_DECRYPT) { |
| 118 | /* Perform this in place, resulting in src being decrypted. */ |
| 119 | debug("%s: begin decryption\n", __func__); |
| 120 | aes_cbc_decrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv, src, |
| 121 | src, num_aes_blocks); |
| 122 | debug("%s: end decryption\n", __func__); |
| 123 | } |
| 124 | |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 125 | if (oper & SECURITY_ENCRYPT) { |
| 126 | /* Perform this in place, resulting in src being encrypted. */ |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 127 | debug("%s: begin encryption\n", __func__); |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 128 | aes_cbc_encrypt_blocks(AES128_KEY_LENGTH, key_schedule, iv, src, |
| 129 | src, num_aes_blocks); |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 130 | debug("%s: end encryption\n", __func__); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (oper & SECURITY_SIGN) { |
| 134 | /* encrypt the data, overwriting the result in signature. */ |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 135 | debug("%s: begin signing\n", __func__); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 136 | sign_object(key, key_schedule, src, sig_dst, num_aes_blocks); |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 137 | debug("%s: end signing\n", __func__); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 143 | /** |
| 144 | * Tegra crypto group |
| 145 | */ |
| 146 | int sign_data_block(u8 *source, unsigned int length, u8 *signature) |
| 147 | { |
| 148 | return tegra_crypto_core(zero_key, SECURITY_SIGN, source, |
| 149 | length, signature); |
| 150 | } |
| 151 | |
| 152 | int sign_enc_data_block(u8 *source, unsigned int length, u8 *signature, u8 *key) |
| 153 | { |
| 154 | return tegra_crypto_core(key, SECURITY_SIGN, source, |
| 155 | length, signature); |
| 156 | } |
| 157 | |
| 158 | int encrypt_data_block(u8 *source, unsigned int length, u8 *key) |
| 159 | { |
| 160 | return tegra_crypto_core(key, SECURITY_ENCRYPT, source, |
| 161 | length, NULL); |
| 162 | } |
| 163 | |
| 164 | int decrypt_data_block(u8 *source, unsigned int length, u8 *key) |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 165 | { |
Svyatoslav Ryhel | c14b207 | 2023-02-14 19:35:36 +0200 | [diff] [blame^] | 166 | return tegra_crypto_core(key, SECURITY_DECRYPT, source, |
| 167 | length, NULL); |
Yen Lin | c3813db | 2012-04-02 13:18:48 +0000 | [diff] [blame] | 168 | } |