• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 13219090513

08 Feb 2025 08:36PM UTC coverage: 91.652% (-0.002%) from 91.654%
13219090513

push

github

web-flow
Merge pull request #4650 from randombit/jack/header-minimization

Reorganize code and reduce header dependencies

94833 of 103471 relevant lines covered (91.65%)

11224601.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.37
/src/cli/cc_enc.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8
#include <botan/hex.h>
9

10
#if defined(BOTAN_HAS_FPE_FE1) && defined(BOTAN_HAS_PBKDF)
11

12
   #include <botan/fpe_fe1.h>
13
   #include <botan/pbkdf.h>
14
   #include <botan/symkey.h>
15

16
namespace Botan_CLI {
17

18
namespace {
19

20
uint8_t luhn_checksum(uint64_t cc_number) {
17✔
21
   uint8_t sum = 0;
17✔
22

23
   bool alt = false;
17✔
24
   while(cc_number) {
289✔
25
      uint8_t digit = cc_number % 10;
272✔
26
      if(alt) {
272✔
27
         digit *= 2;
136✔
28
         if(digit > 9) {
136✔
29
            digit -= 9;
60✔
30
         }
31
      }
32

33
      sum += digit;
272✔
34

35
      cc_number /= 10;
272✔
36
      alt = !alt;
272✔
37
   }
38

39
   return (sum % 10);
17✔
40
}
41

42
bool luhn_check(uint64_t cc_number) {
17✔
43
   return (luhn_checksum(cc_number) == 0);
17✔
44
}
45

46
uint64_t cc_rank(uint64_t cc_number) {
2✔
47
   // Remove Luhn checksum
48
   return cc_number / 10;
2✔
49
}
50

51
uint64_t cc_derank(uint64_t cc_number) {
2✔
52
   for(size_t i = 0; i != 10; ++i) {
17✔
53
      if(luhn_check(cc_number * 10 + i)) {
34✔
54
         return (cc_number * 10 + i);
2✔
55
      }
56
   }
57

58
   return 0;
59
}
60

61
uint64_t encrypt_cc_number(uint64_t cc_number, const Botan::SymmetricKey& key, const std::vector<uint8_t>& tweak) {
1✔
62
   const Botan::BigInt n = 1000000000000000;
1✔
63

64
   const uint64_t cc_ranked = cc_rank(cc_number);
1✔
65

66
   const Botan::BigInt c = Botan::FPE::fe1_encrypt(n, cc_ranked, key, tweak);
1✔
67

68
   if(c.bits() > 50) {
1✔
69
      throw Botan::Internal_Error("FPE produced a number too large");
×
70
   }
71

72
   uint64_t enc_cc = 0;
73
   for(size_t i = 0; i != 7; ++i) {
8✔
74
      enc_cc = (enc_cc << 8) | c.byte_at(6 - i);
7✔
75
   }
76
   return cc_derank(enc_cc);
1✔
77
}
1✔
78

79
uint64_t decrypt_cc_number(uint64_t enc_cc, const Botan::SymmetricKey& key, const std::vector<uint8_t>& tweak) {
1✔
80
   const Botan::BigInt n = 1000000000000000;
1✔
81

82
   const uint64_t cc_ranked = cc_rank(enc_cc);
1✔
83

84
   const Botan::BigInt c = Botan::FPE::fe1_decrypt(n, cc_ranked, key, tweak);
1✔
85

86
   if(c.bits() > 50) {
1✔
87
      throw CLI_Error("FPE produced a number too large");
×
88
   }
89

90
   uint64_t dec_cc = 0;
91
   for(size_t i = 0; i != 7; ++i) {
8✔
92
      dec_cc = (dec_cc << 8) | c.byte_at(6 - i);
7✔
93
   }
94
   return cc_derank(dec_cc);
1✔
95
}
1✔
96

97
}  // namespace
98

99
class CC_Encrypt final : public Command {
100
   public:
101
      CC_Encrypt() : Command("cc_encrypt CC passphrase --tweak=") {}
4✔
102

103
      std::string group() const override { return "misc"; }
1✔
104

105
      std::string description() const override {
1✔
106
         return "Encrypt the passed valid credit card number using FPE encryption";
1✔
107
      }
108

109
      void go() override {
1✔
110
         const uint64_t cc_number = std::stoull(get_arg("CC"));
2✔
111
         const std::vector<uint8_t> tweak = Botan::hex_decode(get_arg("tweak"));
2✔
112
         const std::string pass = get_arg("passphrase");
1✔
113

114
         auto pbkdf = Botan::PBKDF::create("PBKDF2(SHA-256)");
1✔
115
         if(!pbkdf) {
1✔
116
            throw CLI_Error_Unsupported("PBKDF", "PBKDF2(SHA-256)");
×
117
         }
118

119
         auto key = Botan::SymmetricKey(pbkdf->pbkdf_iterations(32, pass, tweak.data(), tweak.size(), 100000));
1✔
120

121
         output() << encrypt_cc_number(cc_number, key, tweak) << "\n";
1✔
122
      }
3✔
123
};
124

125
BOTAN_REGISTER_COMMAND("cc_encrypt", CC_Encrypt);
2✔
126

127
class CC_Decrypt final : public Command {
128
   public:
129
      CC_Decrypt() : Command("cc_decrypt CC passphrase --tweak=") {}
4✔
130

131
      std::string group() const override { return "misc"; }
1✔
132

133
      std::string description() const override {
1✔
134
         return "Decrypt the passed valid ciphertext credit card number using FPE decryption";
1✔
135
      }
136

137
      void go() override {
1✔
138
         const uint64_t cc_number = std::stoull(get_arg("CC"));
2✔
139
         const std::vector<uint8_t> tweak = Botan::hex_decode(get_arg("tweak"));
2✔
140
         const std::string pass = get_arg("passphrase");
1✔
141

142
         auto pbkdf = Botan::PBKDF::create("PBKDF2(SHA-256)");
1✔
143
         if(!pbkdf) {
1✔
144
            throw CLI_Error_Unsupported("PBKDF", "PBKDF2(SHA-256)");
×
145
         }
146

147
         auto key = Botan::SymmetricKey(pbkdf->pbkdf_iterations(32, pass, tweak.data(), tweak.size(), 100000));
1✔
148

149
         output() << decrypt_cc_number(cc_number, key, tweak) << "\n";
1✔
150
      }
3✔
151
};
152

153
BOTAN_REGISTER_COMMAND("cc_decrypt", CC_Decrypt);
2✔
154

155
}  // namespace Botan_CLI
156

157
#endif  // FPE && PBKDF
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc