Debian Codegraph @

mlspp/lib/hpke/src/base64.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/mlspp/lib/hpke/src/base64.cpp`/include <hpke/base64.h>cxx . . $ `<file>/mlspp/lib/hpke/include/hpke/base64.h`/
2
3 #include "openssl_common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/openssl_common.h`/
4
5 #include <openssl/bio.h>
6 #include <openssl/err.h>
7 #include <openssl/evp.h>
8
9 namespace mlsppcxx . . $ mlspp/::hpkecxx . . $ mlspp/hpke/ {
10
11 stdcxx . . $ std/::stringcxx . . $ std/string#
12 to_base64cxx . . $ mlspp/hpke/to_base64(b0627b6bf491e6e2).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data)
13 {
14 if (data.emptycxx . . $ mlspp/bytes_ns/bytes#empty(50ce9a9e25b4a850).()) {
15 return ""cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).;
16 }
17
18 #if WITH_BORINGSSL
19 const auto data_size = data.size();
20 #else
21 const auto data_size = static_cast<int>(data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).());
22 #endif
23
24 // base64 encoding produces 4 characters for every 3 input bytes (rounded up)
25 const auto out_size = (data_size + 2) / 3 * 4;
26 auto out = bytescxx . . $ mlspp/bytes_ns/bytes#(out_size + 1); // NUL terminator
27
28 const auto result = EVP_EncodeBlock(out.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), data.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), data_size);
29 if (result != out_size) {
30 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
31 }
32
33 out.resizecxx . . $ mlspp/bytes_ns/bytes#resize(2c44cd906ae4e829).(out.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).() - 1); // strip NUL terminator
34 return to_asciicxx . . $ mlspp/bytes_ns/to_ascii(b0627b6bf491e6e2).(out);
35 }
36
37 stdcxx . . $ std/::stringcxx . . $ std/string#
38 to_base64urlcxx . . $ mlspp/hpke/to_base64url(b0627b6bf491e6e2).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data)
39 {
40 if (data.emptycxx . . $ mlspp/bytes_ns/bytes#empty(50ce9a9e25b4a850).()) {
41 return ""cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).;
42 }
43
44 auto encoded = to_base64cxx . . $ mlspp/hpke/to_base64(b0627b6bf491e6e2).(data);
45
46 auto pad_start = encoded.find_first_ofcxx . . $ std/__cxx11/basic_string#find_first_of(2dda2fc6b78abe68).('=');
47 if (pad_start != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) {
48 encoded =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). encoded.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(0, pad_start);
49 }
50
51 stdcxx . . $ std/::replacecxx . . $ std/replace(816cee973d7ce286).(encoded.begincxx . . $ std/__cxx11/basic_string#begin(417acd00ce8503f4).(), encoded.endcxx . . $ std/__cxx11/basic_string#end(417acd00ce8503f4).(), '+', '-');
52 stdcxx . . $ std/::replacecxx . . $ std/replace(816cee973d7ce286).(encoded.begincxx . . $ std/__cxx11/basic_string#begin(417acd00ce8503f4).(), encoded.endcxx . . $ std/__cxx11/basic_string#end(417acd00ce8503f4).(), '/', '_');
53
54 return encodedcxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
55 }
56
57 bytescxx . . $ mlspp/bytes_ns/bytes#
58 from_base64cxx . . $ mlspp/hpke/from_base64(fc01d114001b7256).(const stdcxx . . $ std/::stringcxx . . $ std/string#& enc)
59 {
60 if (enc.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() == 0) {
61 return {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).};
62 }
63
64 if (enc.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() % 4 != 0) {
65 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Base64 length is not divisible by 4");
66 }
67
68 const auto in = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).(enc);
69 const auto in_size = static_cast<int>(in.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).());
70 const auto out_size = in_size / 4 * 3;
71 auto out = bytescxx . . $ mlspp/bytes_ns/bytes#(out_size);
72
73 const auto result = EVP_DecodeBlock(out.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), in.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), in_size);
74 if (result != out_size) {
75 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
76 }
77
78 if (enc.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(enc.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() - 2, enc.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).()) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "==") {
79 out.resizecxx . . $ mlspp/bytes_ns/bytes#resize(2c44cd906ae4e829).(out.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).() - 2);
80 } else if (enc.substrcxx . . $ std/__cxx11/basic_string#substr(149c655009c9bbd2).(enc.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() - 1, enc.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).()) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "=") {
81 out.resizecxx . . $ mlspp/bytes_ns/bytes#resize(2c44cd906ae4e829).(out.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).() - 1);
82 }
83
84 return outcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).;
85 }
86
87 bytescxx . . $ mlspp/bytes_ns/bytes#
88 from_base64urlcxx . . $ mlspp/hpke/from_base64url(fc01d114001b7256).(const stdcxx . . $ std/::stringcxx . . $ std/string#& enc)
89 {
90 if (enc.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).()) {
91 return {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).};
92 }
93
94 auto enc_copy = enccxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5).;
95 stdcxx . . $ std/::replacecxx . . $ std/replace(816cee973d7ce286).(enc_copy.begincxx . . $ std/__cxx11/basic_string#begin(417acd00ce8503f4).(), enc_copy.endcxx . . $ std/__cxx11/basic_string#end(417acd00ce8503f4).(), '-', '+');
96 stdcxx . . $ std/::replacecxx . . $ std/replace(816cee973d7ce286).(enc_copy.begincxx . . $ std/__cxx11/basic_string#begin(417acd00ce8503f4).(), enc_copy.endcxx . . $ std/__cxx11/basic_string#end(417acd00ce8503f4).(), '_', '/');
97
98 while (enc_copy.lengthcxx . . $ std/__cxx11/basic_string#length(6f8bd5c0ea8fb29a).() % 4 != 0) {
99 enc_copy +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "=";
100 }
101
102 return from_base64cxx . . $ mlspp/hpke/from_base64(fc01d114001b7256).(enc_copy);
103 }
104
105 } // namespace mlspp::hpke