keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
CommandSamGenerateKey.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/crypto/legacysam/CommandSamGenerateKey.hpp"
15
16#include <map>
17#include <memory>
18#include <vector>
19
20#include "keyple/card/calypso/crypto/legacysam/AccessForbiddenException.hpp"
21#include "keyple/card/calypso/crypto/legacysam/CounterOverflowException.hpp"
22#include "keyple/card/calypso/crypto/legacysam/DataAccessException.hpp"
23#include "keyple/card/calypso/crypto/legacysam/IllegalParameterException.hpp"
24#include "keyple/card/calypso/crypto/legacysam/IncorrectInputDataException.hpp"
25#include "keyple/card/calypso/crypto/legacysam/LegacySamConstants.hpp"
26#include "keyple/core/util/ApduUtil.hpp"
27#include "keyple/core/util/ByteArrayUtil.hpp"
28#include "keyple/core/util/cpp/Arrays.hpp"
29#include "keyple/core/util/cpp/System.hpp"
30
31namespace keyple {
32namespace card {
33namespace calypso {
34namespace crypto {
35namespace legacysam {
36
37using keyple::core::util::ApduUtil;
38using keyple::core::util::ByteArrayUtil;
39using keyple::core::util::cpp::Arrays;
40using keyple::core::util::cpp::System;
41
42const std::map<int, const std::shared_ptr<StatusProperties>>
43 CommandSamGenerateKey::STATUS_TABLE = [] {
44 std::map<int, const std::shared_ptr<StatusProperties>> m(
45 Command::STATUS_TABLE);
46 return m;
47 }();
48
49CommandSamGenerateKey::CommandSamGenerateKey(
50 std::shared_ptr<CommandContextDto> context,
51 std::uint8_t targetKeyReference,
52 std::uint8_t cipheringKvc,
53 std::uint8_t sourceKif,
54 std::uint8_t sourceKvc,
55 const std::vector<std::uint8_t>& keyParameters,
56 bool isTransferredObjectDiversified)
57: Command(CommandRef::SAM_GENERATE_KEY, 48, context)
58{
59 const std::uint8_t cla = context->getTargetSam()->getClassByte();
60 const std::uint8_t inst = getCommandRef().getInstructionByte();
61
62 const std::uint8_t p1 = isTransferredObjectDiversified ? 0x01 : 0x00;
63 std::vector<std::uint8_t> dataIn(13);
64 dataIn[0] = cipheringKvc;
65 dataIn[1] = sourceKif;
66 dataIn[2] = sourceKvc;
67 System::arraycopy(keyParameters, 0, dataIn, 3, 10);
68
69 setApduRequest(
70 std::make_shared<ApduRequestAdapter>(
71 ApduUtil::build(cla, inst, p1, targetKeyReference, dataIn)));
72}
73
74CommandSamGenerateKey::CommandSamGenerateKey(
75 std::shared_ptr<CommandContextDto> context,
76 std::uint8_t targetKeyReference,
77 std::uint8_t cipheringKvc,
78 std::uint8_t sourceKif,
79 std::uint8_t sourceKvc,
80 const std::vector<std::uint8_t>& keyParameters,
81 bool isTransferredObjectDiversified,
82 const std::vector<std::uint8_t>& arbitraryDiversifier)
83: Command(CommandRef::SAM_GENERATE_KEY, 48, context)
84{
85 const std::uint8_t cla = context->getTargetSam()->getClassByte();
86 const std::uint8_t inst = getCommandRef().getInstructionByte();
87
88 const std::uint8_t p1 = isTransferredObjectDiversified ? 0x01 : 0x00;
89 std::vector<std::uint8_t> dataIn(13 + 8);
90 dataIn[0] = cipheringKvc;
91 dataIn[1] = sourceKif;
92 dataIn[2] = sourceKvc;
93 System::arraycopy(keyParameters, 0, dataIn, 3, 10);
94 System::arraycopy(arbitraryDiversifier, 0, dataIn, 13, 8);
95
96 setApduRequest(
97 std::make_shared<ApduRequestAdapter>(
98 ApduUtil::build(cla, inst, p1, targetKeyReference, dataIn)));
99}
100
101const std::vector<std::uint8_t>&
102CommandSamGenerateKey::getCipheredData() const
103{
104 return mCipheredData;
105}
106
107void
108CommandSamGenerateKey::finalizeRequest()
109{
110 /* Nothing to do */
111}
112
113bool
114CommandSamGenerateKey::isControlSamRequiredToFinalizeRequest() const
115{
116 return false;
117}
118
119void
120CommandSamGenerateKey::parseResponse(
121 std::shared_ptr<ApduResponseApi> apduResponse)
122{
123 setResponseAndCheckStatus(apduResponse);
124 mCipheredData = apduResponse->getDataOut();
125}
126
127const std::map<int, const std::shared_ptr<StatusProperties>>&
128CommandSamGenerateKey::getStatusTable() const
129{
130 return STATUS_TABLE;
131}
132
133} /* namespace legacysam */
134} /* namespace crypto */
135} /* namespace calypso */
136} /* namespace card */
137} /* namespace keyple */