keyple-card-calypso-crypto-legacysam-cpp-lib - 1.0.2
Component of the Keyple C++ middleware
LegacySamConstants.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/LegacySamConstants.hpp"
15
16#include <map>
17#include <string>
18#include <vector>
19
20#include "keyple/core/util/HexUtil.hpp"
21#include "keyple/core/util/cpp/exception/IllegalArgumentException.hpp"
22
23namespace keyple {
24namespace card {
25namespace calypso {
26namespace crypto {
27namespace legacysam {
28
29using keyple::core::util::HexUtil;
30using keyple::core::util::cpp::exception::IllegalArgumentException;
31
33
34const int LegacySamConstants::MIN_KEY_RECORD_NUMBER = 1;
35const int LegacySamConstants::MAX_KEY_RECORD_NUMBER = 126;
36const int LegacySamConstants::MIN_COUNTER_NUMBER = 0;
37const int LegacySamConstants::MAX_COUNTER_NUMBER = 26;
38const int LegacySamConstants::MIN_COUNTER_CEILING_NUMBER = 0;
39const int LegacySamConstants::MAX_COUNTER_CEILING_NUMBER = 26;
40const int LegacySamConstants::MIN_COUNTER_CEILING_VALUE = 0;
41const int LegacySamConstants::MAX_COUNTER_CEILING_VALUE = 0xFFFFFA;
42const std::vector<int> LegacySamConstants::COUNTER_TO_RECORD_LOOKUP
43 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
44 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2};
45const uint8_t LegacySamConstants::AID_SIZE_MAX = 16;
46const int LegacySamConstants::CA_CERTIFICATE_SIZE = 384;
47const int LegacySamConstants::ECC_PUBLIC_KEY_SIZE = 64;
48const int LegacySamConstants::PLAIN_CEILING_DATA_BLOCK_SIZE = 30;
49const int LegacySamConstants::KEY_DATA_BLOCK_SIZE = 48;
50const int LegacySamConstants::LOCK_FILE_SIZE = 29;
51const int LegacySamConstants::SAM_PARAMETERS_LENGTH = 29;
52const int LegacySamConstants::KEY_PARAMETERS_LENGTH = 10;
53const int LegacySamConstants::LOCK_VALUE_LENGTH = 16;
54
55const uint8_t LegacySamConstants::TARGET_IS_SYSTEM_KEY_FILE = 0xC0;
56const uint8_t LegacySamConstants::TARGET_IS_LOCK_FILE = 0xE0;
57const uint8_t LegacySamConstants::RECORD_CHOSEN_BY_THE_SAM = 0xF0;
58
59const uint8_t LegacySamConstants::FIRST_EVENT_CEILING_RECORD_REFERENCE = 0xB1;
60const uint8_t LegacySamConstants::SINGLE_CEILING_REFERENCE = 0xB8;
61
62const uint8_t LegacySamConstants::LOCK_KIF = 0xEF;
63
64const uint8_t LegacySamConstants::DYNAMIC_MODE_CIPHERING = 0x00;
65const uint8_t LegacySamConstants::STATIC_MODE_CIPHERING = 0x08;
66
67const std::map<SystemKeyType, uint8_t>
68 LegacySamConstants::SYSTEM_KEY_TYPE_KIF_MAP;
69
70const TagInfo LegacySamConstants::TagInfo::CA_CERTIFICATE(
71 0xDF43, CA_CERTIFICATE_SIZE, HexUtil::toByteArray("DF43820180"));
72const TagInfo LegacySamConstants::TagInfo::CARD_PUBLIC_KEY_DATA(
73 0xDF25, 60, HexUtil::toByteArray("DF253F"));
74const TagInfo LegacySamConstants::TagInfo::GENERATED_CARD_ECC_KEY_PAIR(
75 0xDF3C, 96, HexUtil::toByteArray("DF3C60"));
76const TagInfo LegacySamConstants::TagInfo::GENERATED_CARD_CERTIFICATE(
77 0xDF45, 316, HexUtil::toByteArray("DF4582013C"));
78
80 int value, int length, const std::vector<uint8_t>& header)
81: mValue(value)
82, mLength(length)
83, mTotalLength(length + static_cast<int>(header.size()))
84, mHeader(header)
85, mLsb((uint8_t)(value & 0xFF))
86, mMsb((uint8_t)((value & 0xFF00) >> 8))
87{
88}
89
90int
91LegacySamConstants::TagInfo::getValue() const
92{
93 return mValue;
94}
95
96int
97LegacySamConstants::TagInfo::getLength() const
98{
99 return mLength;
100}
101
102int
103LegacySamConstants::TagInfo::getTotalLength() const
104{
105 return mTotalLength;
106}
107
108const std::vector<uint8_t>&
109LegacySamConstants::TagInfo::getHeader() const
110{
111 return mHeader;
112}
113
114uint8_t
115LegacySamConstants::TagInfo::getLsb() const
116{
117 return mLsb;
118}
119
120uint8_t
121LegacySamConstants::TagInfo::getMsb() const
122{
123 return mMsb;
124}
125
127LegacySamConstants::TagInfo::valueOf(const std::string& name)
128{
129 if (name == "CA_CERTIFICATE") {
130 return CA_CERTIFICATE;
131 } else if (name == "CARD_PUBLIC_KEY_DATA") {
132 return CARD_PUBLIC_KEY_DATA;
133 } else if (name == "GENERATED_CARD_ECC_KEY_PAIR") {
134 return GENERATED_CARD_ECC_KEY_PAIR;
135 } else {
136 throw IllegalArgumentException("Unknown tag name");
137 }
138}
139
140} /* namespace legacysam */
141} /* namespace crypto */
142} /* namespace calypso */
143} /* namespace card */
144} /* namespace keyple */