1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd 4 */ 5 6 #include <crypto.h> 7 8 static const u8 null_hash_sha1_value[] = { 9 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 10 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 11 0xaf, 0xd8, 0x07, 0x09 12 }; 13 14 static const u8 null_hash_md5_value[] = { 15 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 16 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e 17 }; 18 19 static const u8 null_hash_sha256_value[] = { 20 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 21 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 22 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 23 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 24 }; 25 26 static const u8 null_hash_sha512_value[] = { 27 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 28 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07, 29 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 30 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 31 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 32 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 33 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 34 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e 35 }; 36 37 u32 crypto_algo_nbits(u32 algo) 38 { 39 switch (algo) { 40 case CRYPTO_MD5: 41 return 128; 42 case CRYPTO_SHA1: 43 return 160; 44 case CRYPTO_SHA256: 45 return 256; 46 case CRYPTO_SHA512: 47 return 512; 48 case CRYPTO_RSA512: 49 return 512; 50 case CRYPTO_RSA1024: 51 return 1024; 52 case CRYPTO_RSA2048: 53 return 2048; 54 case CRYPTO_RSA3072: 55 return 3072; 56 case CRYPTO_RSA4096: 57 return 4096; 58 } 59 60 printf("Unknown crypto algorithm: 0x%x\n", algo); 61 62 return 0; 63 } 64 65 struct udevice *crypto_get_device(u32 capability) 66 { 67 const struct dm_crypto_ops *ops; 68 struct udevice *dev; 69 struct uclass *uc; 70 int ret; 71 u32 cap; 72 73 ret = uclass_get(UCLASS_CRYPTO, &uc); 74 if (ret) 75 return NULL; 76 77 for (uclass_first_device(UCLASS_CRYPTO, &dev); 78 dev; 79 uclass_next_device(&dev)) { 80 ops = device_get_ops(dev); 81 if (!ops || !ops->capability) 82 continue; 83 84 cap = ops->capability(dev); 85 if ((cap & capability) == capability) 86 return dev; 87 } 88 89 return NULL; 90 } 91 92 int crypto_sha_init(struct udevice *dev, sha_context *ctx) 93 { 94 const struct dm_crypto_ops *ops = device_get_ops(dev); 95 96 if (ctx && !ctx->length) 97 return 0; 98 99 if (!ops || !ops->sha_init) 100 return -ENOSYS; 101 102 return ops->sha_init(dev, ctx); 103 } 104 105 int crypto_sha_update(struct udevice *dev, u32 *input, u32 len) 106 { 107 const struct dm_crypto_ops *ops = device_get_ops(dev); 108 109 if (!len) 110 return 0; 111 112 if (!ops || !ops->sha_update) 113 return -ENOSYS; 114 115 return ops->sha_update(dev, input, len); 116 } 117 118 int crypto_sha_final(struct udevice *dev, sha_context *ctx, u8 *output) 119 { 120 const struct dm_crypto_ops *ops = device_get_ops(dev); 121 const u8 *null_hash = NULL; 122 u32 hash_size = 0; 123 124 if (ctx && !ctx->length && output) { 125 switch (ctx->algo) { 126 case CRYPTO_MD5: 127 null_hash = null_hash_md5_value; 128 hash_size = sizeof(null_hash_md5_value); 129 break; 130 case CRYPTO_SHA1: 131 null_hash = null_hash_sha1_value; 132 hash_size = sizeof(null_hash_sha1_value); 133 break; 134 case CRYPTO_SHA256: 135 null_hash = null_hash_sha256_value; 136 hash_size = sizeof(null_hash_sha256_value); 137 break; 138 case CRYPTO_SHA512: 139 null_hash = null_hash_sha512_value; 140 hash_size = sizeof(null_hash_sha512_value); 141 break; 142 default: 143 return -EINVAL; 144 } 145 146 memcpy(output, null_hash, hash_size); 147 148 return 0; 149 } 150 151 if (!ops || !ops->sha_final) 152 return -ENOSYS; 153 154 return ops->sha_final(dev, ctx, output); 155 } 156 157 int crypto_sha_csum(struct udevice *dev, sha_context *ctx, 158 char *input, u32 input_len, u8 *output) 159 { 160 int ret; 161 162 ret = crypto_sha_init(dev, ctx); 163 if (ret) 164 return ret; 165 166 ret = crypto_sha_update(dev, (u32 *)input, input_len); 167 if (ret) 168 return ret; 169 170 ret = crypto_sha_final(dev, ctx, output); 171 172 return ret; 173 } 174 175 int crypto_sha_regions_csum(struct udevice *dev, sha_context *ctx, 176 const struct image_region region[], 177 int region_count, u8 *output) 178 { 179 int i, ret; 180 181 ctx->length = 0; 182 for (i = 0; i < region_count; i++) 183 ctx->length += region[i].size; 184 185 ret = crypto_sha_init(dev, ctx); 186 if (ret) 187 return ret; 188 189 for (i = 0; i < region_count; i++) { 190 ret = crypto_sha_update(dev, (void *)region[i].data, 191 region[i].size); 192 if (ret) 193 return ret; 194 } 195 196 return crypto_sha_final(dev, ctx, output); 197 } 198 199 int crypto_rsa_verify(struct udevice *dev, rsa_key *ctx, u8 *sign, u8 *output) 200 { 201 const struct dm_crypto_ops *ops = device_get_ops(dev); 202 203 if (!ops || !ops->rsa_verify) 204 return -ENOSYS; 205 206 return ops->rsa_verify(dev, ctx, sign, output); 207 } 208 209 int crypto_get_trng(struct udevice *dev, u8 *output, u32 len) 210 { 211 const struct dm_crypto_ops *ops = device_get_ops(dev); 212 213 if (!ops || !ops->get_trng) 214 return -ENOSYS; 215 216 return ops->get_trng(dev, output, len); 217 } 218 219 UCLASS_DRIVER(crypto) = { 220 .id = UCLASS_CRYPTO, 221 .name = "crypto", 222 }; 223