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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

94.29
/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

15
namespace Botan_CLI {
16

17
namespace {
18

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

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

32
      sum += digit;
272✔
33

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

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

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

43
uint64_t cc_rank(uint64_t cc_number) {
2✔
44
   // Remove Luhn checksum
45
   return cc_number / 10;
2✔
46
}
47

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

55
   return 0;
56
}
57

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

61
   const uint64_t cc_ranked = cc_rank(cc_number);
1✔
62

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

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

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

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

79
   const uint64_t cc_ranked = cc_rank(enc_cc);
1✔
80

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

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

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

94
}
95

96
class CC_Encrypt final : public Command {
97
   public:
98
      CC_Encrypt() : Command("cc_encrypt CC passphrase --tweak=") {}
4✔
99

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

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

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

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

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

118
         output() << encrypt_cc_number(cc_number, key, tweak) << "\n";
1✔
119
      }
3✔
120
};
121

122
BOTAN_REGISTER_COMMAND("cc_encrypt", CC_Encrypt);
2✔
123

124
class CC_Decrypt final : public Command {
125
   public:
126
      CC_Decrypt() : Command("cc_decrypt CC passphrase --tweak=") {}
4✔
127

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

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

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

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

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

146
         output() << decrypt_cc_number(cc_number, key, tweak) << "\n";
1✔
147
      }
3✔
148
};
149

150
BOTAN_REGISTER_COMMAND("cc_decrypt", CC_Decrypt);
2✔
151

152
}
153

154
#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

© 2025 Coveralls, Inc