ccp-ops.c (ccebcf3f224a44ec8e9c5bfca9d8e5d29298a5a8) ccp-ops.c (990672d48515ce09c76fcf1ceccee48b0dd1942b)
1/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
5 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 * Author: Gary R Hook <gary.hook@amd.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/pci.h>
17#include <linux/interrupt.h>
18#include <crypto/scatterwalk.h>
1/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
5 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 * Author: Gary R Hook <gary.hook@amd.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/pci.h>
17#include <linux/interrupt.h>
18#include <crypto/scatterwalk.h>
19#include <crypto/des.h>
19#include <linux/ccp.h>
20
21#include "ccp-dev.h"
22
23/* SHA initial context values */
24static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
25 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
26 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),

--- 907 unchanged lines hidden (view full) ---

934 ccp_dm_free(&ctx);
935
936e_key:
937 ccp_dm_free(&key);
938
939 return ret;
940}
941
20#include <linux/ccp.h>
21
22#include "ccp-dev.h"
23
24/* SHA initial context values */
25static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
26 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
27 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),

--- 907 unchanged lines hidden (view full) ---

935 ccp_dm_free(&ctx);
936
937e_key:
938 ccp_dm_free(&key);
939
940 return ret;
941}
942
943static int ccp_run_des3_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
944{
945 struct ccp_des3_engine *des3 = &cmd->u.des3;
946
947 struct ccp_dm_workarea key, ctx;
948 struct ccp_data src, dst;
949 struct ccp_op op;
950 unsigned int dm_offset;
951 unsigned int len_singlekey;
952 bool in_place = false;
953 int ret;
954
955 /* Error checks */
956 if (!cmd_q->ccp->vdata->perform->des3)
957 return -EINVAL;
958
959 if (des3->key_len != DES3_EDE_KEY_SIZE)
960 return -EINVAL;
961
962 if (((des3->mode == CCP_DES3_MODE_ECB) ||
963 (des3->mode == CCP_DES3_MODE_CBC)) &&
964 (des3->src_len & (DES3_EDE_BLOCK_SIZE - 1)))
965 return -EINVAL;
966
967 if (!des3->key || !des3->src || !des3->dst)
968 return -EINVAL;
969
970 if (des3->mode != CCP_DES3_MODE_ECB) {
971 if (des3->iv_len != DES3_EDE_BLOCK_SIZE)
972 return -EINVAL;
973
974 if (!des3->iv)
975 return -EINVAL;
976 }
977
978 ret = -EIO;
979 /* Zero out all the fields of the command desc */
980 memset(&op, 0, sizeof(op));
981
982 /* Set up the Function field */
983 op.cmd_q = cmd_q;
984 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
985 op.sb_key = cmd_q->sb_key;
986
987 op.init = (des3->mode == CCP_DES3_MODE_ECB) ? 0 : 1;
988 op.u.des3.type = des3->type;
989 op.u.des3.mode = des3->mode;
990 op.u.des3.action = des3->action;
991
992 /*
993 * All supported key sizes fit in a single (32-byte) KSB entry and
994 * (like AES) must be in little endian format. Use the 256-bit byte
995 * swap passthru option to convert from big endian to little endian.
996 */
997 ret = ccp_init_dm_workarea(&key, cmd_q,
998 CCP_DES3_KEY_SB_COUNT * CCP_SB_BYTES,
999 DMA_TO_DEVICE);
1000 if (ret)
1001 return ret;
1002
1003 /*
1004 * The contents of the key triplet are in the reverse order of what
1005 * is required by the engine. Copy the 3 pieces individually to put
1006 * them where they belong.
1007 */
1008 dm_offset = CCP_SB_BYTES - des3->key_len; /* Basic offset */
1009
1010 len_singlekey = des3->key_len / 3;
1011 ccp_set_dm_area(&key, dm_offset + 2 * len_singlekey,
1012 des3->key, 0, len_singlekey);
1013 ccp_set_dm_area(&key, dm_offset + len_singlekey,
1014 des3->key, len_singlekey, len_singlekey);
1015 ccp_set_dm_area(&key, dm_offset,
1016 des3->key, 2 * len_singlekey, len_singlekey);
1017
1018 /* Copy the key to the SB */
1019 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1020 CCP_PASSTHRU_BYTESWAP_256BIT);
1021 if (ret) {
1022 cmd->engine_error = cmd_q->cmd_error;
1023 goto e_key;
1024 }
1025
1026 /*
1027 * The DES3 context fits in a single (32-byte) KSB entry and
1028 * must be in little endian format. Use the 256-bit byte swap
1029 * passthru option to convert from big endian to little endian.
1030 */
1031 if (des3->mode != CCP_DES3_MODE_ECB) {
1032 u32 load_mode;
1033
1034 op.sb_ctx = cmd_q->sb_ctx;
1035
1036 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1037 CCP_DES3_CTX_SB_COUNT * CCP_SB_BYTES,
1038 DMA_BIDIRECTIONAL);
1039 if (ret)
1040 goto e_key;
1041
1042 /* Load the context into the LSB */
1043 dm_offset = CCP_SB_BYTES - des3->iv_len;
1044 ccp_set_dm_area(&ctx, dm_offset, des3->iv, 0, des3->iv_len);
1045
1046 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1047 load_mode = CCP_PASSTHRU_BYTESWAP_NOOP;
1048 else
1049 load_mode = CCP_PASSTHRU_BYTESWAP_256BIT;
1050 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1051 load_mode);
1052 if (ret) {
1053 cmd->engine_error = cmd_q->cmd_error;
1054 goto e_ctx;
1055 }
1056 }
1057
1058 /*
1059 * Prepare the input and output data workareas. For in-place
1060 * operations we need to set the dma direction to BIDIRECTIONAL
1061 * and copy the src workarea to the dst workarea.
1062 */
1063 if (sg_virt(des3->src) == sg_virt(des3->dst))
1064 in_place = true;
1065
1066 ret = ccp_init_data(&src, cmd_q, des3->src, des3->src_len,
1067 DES3_EDE_BLOCK_SIZE,
1068 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1069 if (ret)
1070 goto e_ctx;
1071
1072 if (in_place)
1073 dst = src;
1074 else {
1075 ret = ccp_init_data(&dst, cmd_q, des3->dst, des3->src_len,
1076 DES3_EDE_BLOCK_SIZE, DMA_FROM_DEVICE);
1077 if (ret)
1078 goto e_src;
1079 }
1080
1081 /* Send data to the CCP DES3 engine */
1082 while (src.sg_wa.bytes_left) {
1083 ccp_prepare_data(&src, &dst, &op, DES3_EDE_BLOCK_SIZE, true);
1084 if (!src.sg_wa.bytes_left) {
1085 op.eom = 1;
1086
1087 /* Since we don't retrieve the context in ECB mode
1088 * we have to wait for the operation to complete
1089 * on the last piece of data
1090 */
1091 op.soc = 0;
1092 }
1093
1094 ret = cmd_q->ccp->vdata->perform->des3(&op);
1095 if (ret) {
1096 cmd->engine_error = cmd_q->cmd_error;
1097 goto e_dst;
1098 }
1099
1100 ccp_process_data(&src, &dst, &op);
1101 }
1102
1103 if (des3->mode != CCP_DES3_MODE_ECB) {
1104 /* Retrieve the context and make BE */
1105 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1106 CCP_PASSTHRU_BYTESWAP_256BIT);
1107 if (ret) {
1108 cmd->engine_error = cmd_q->cmd_error;
1109 goto e_dst;
1110 }
1111
1112 /* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
1113 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1114 dm_offset = CCP_SB_BYTES - des3->iv_len;
1115 else
1116 dm_offset = 0;
1117 ccp_get_dm_area(&ctx, dm_offset, des3->iv, 0,
1118 DES3_EDE_BLOCK_SIZE);
1119 }
1120e_dst:
1121 if (!in_place)
1122 ccp_free_data(&dst, cmd_q);
1123
1124e_src:
1125 ccp_free_data(&src, cmd_q);
1126
1127e_ctx:
1128 if (des3->mode != CCP_DES3_MODE_ECB)
1129 ccp_dm_free(&ctx);
1130
1131e_key:
1132 ccp_dm_free(&key);
1133
1134 return ret;
1135}
1136
942static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
943{
944 struct ccp_sha_engine *sha = &cmd->u.sha;
945 struct ccp_dm_workarea ctx;
946 struct ccp_data src;
947 struct ccp_op op;
948 unsigned int ioffset, ooffset;
949 unsigned int digest_size;

--- 948 unchanged lines hidden (view full) ---

1898
1899 switch (cmd->engine) {
1900 case CCP_ENGINE_AES:
1901 ret = ccp_run_aes_cmd(cmd_q, cmd);
1902 break;
1903 case CCP_ENGINE_XTS_AES_128:
1904 ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
1905 break;
1137static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1138{
1139 struct ccp_sha_engine *sha = &cmd->u.sha;
1140 struct ccp_dm_workarea ctx;
1141 struct ccp_data src;
1142 struct ccp_op op;
1143 unsigned int ioffset, ooffset;
1144 unsigned int digest_size;

--- 948 unchanged lines hidden (view full) ---

2093
2094 switch (cmd->engine) {
2095 case CCP_ENGINE_AES:
2096 ret = ccp_run_aes_cmd(cmd_q, cmd);
2097 break;
2098 case CCP_ENGINE_XTS_AES_128:
2099 ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
2100 break;
2101 case CCP_ENGINE_DES3:
2102 ret = ccp_run_des3_cmd(cmd_q, cmd);
2103 break;
1906 case CCP_ENGINE_SHA:
1907 ret = ccp_run_sha_cmd(cmd_q, cmd);
1908 break;
1909 case CCP_ENGINE_RSA:
1910 ret = ccp_run_rsa_cmd(cmd_q, cmd);
1911 break;
1912 case CCP_ENGINE_PASSTHRU:
1913 if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)

--- 13 unchanged lines hidden ---
2104 case CCP_ENGINE_SHA:
2105 ret = ccp_run_sha_cmd(cmd_q, cmd);
2106 break;
2107 case CCP_ENGINE_RSA:
2108 ret = ccp_run_rsa_cmd(cmd_q, cmd);
2109 break;
2110 case CCP_ENGINE_PASSTHRU:
2111 if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)

--- 13 unchanged lines hidden ---