/src/rnp/src/lib/crypto/mpi.cpp
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c) 2018-2025 Ribose Inc. |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
15 | | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
16 | | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS |
18 | | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
19 | | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
20 | | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
21 | | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
22 | | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
23 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
24 | | * POSSIBILITY OF SUCH DAMAGE. |
25 | | */ |
26 | | |
27 | | #include <string.h> |
28 | | #include <stdlib.h> |
29 | | #include "mpi.hpp" |
30 | | #include "mem.h" |
31 | | #include "utils.h" |
32 | | |
33 | | namespace pgp { |
34 | | |
35 | | size_t |
36 | | mpi::bits() const noexcept |
37 | 3.70M | { |
38 | 3.70M | size_t bits = 0; |
39 | 3.70M | size_t idx = 0; |
40 | 3.70M | uint8_t bt; |
41 | | |
42 | 5.12M | for (idx = 0; (idx < size()) && !data_[idx]; idx++) |
43 | 1.42M | ; |
44 | | |
45 | 3.70M | if (idx < size()) { |
46 | 21.0M | for (bits = (size() - idx - 1) << 3, bt = data_[idx]; bt; bits++, bt = bt >> 1) |
47 | 18.2M | ; |
48 | 2.79M | } |
49 | | |
50 | 3.70M | return bits; |
51 | 3.70M | } |
52 | | |
53 | | size_t |
54 | | mpi::size() const noexcept |
55 | 14.2M | { |
56 | 14.2M | return data_.size(); |
57 | 14.2M | } |
58 | | |
59 | | uint8_t * |
60 | | mpi::data() noexcept |
61 | 4.00M | { |
62 | 4.00M | return data_.data(); |
63 | 4.00M | } |
64 | | |
65 | | const uint8_t * |
66 | | mpi::data() const noexcept |
67 | 1.70M | { |
68 | 1.70M | return data_.data(); |
69 | 1.70M | } |
70 | | |
71 | | bool |
72 | | mpi::operator==(const mpi &src) const |
73 | 0 | { |
74 | 0 | size_t idx1 = 0; |
75 | 0 | size_t idx2 = 0; |
76 | |
|
77 | 0 | for (idx1 = 0; (idx1 < size()) && !data_[idx1]; idx1++) |
78 | 0 | ; |
79 | |
|
80 | 0 | for (idx2 = 0; (idx2 < src.size()) && !src[idx2]; idx2++) |
81 | 0 | ; |
82 | |
|
83 | 0 | return ((size() - idx1) == (src.size() - idx2) && |
84 | 0 | !memcmp(data() + idx1, src.data() + idx2, size() - idx1)); |
85 | 0 | } |
86 | | |
87 | | bool |
88 | | mpi::operator!=(const mpi &src) const |
89 | 0 | { |
90 | 0 | return !(*this == src); |
91 | 0 | } |
92 | | |
93 | | uint8_t & |
94 | | mpi::operator[](size_t idx) |
95 | 171k | { |
96 | 171k | return data_.at(idx); |
97 | 171k | } |
98 | | |
99 | | const uint8_t & |
100 | | mpi::operator[](size_t idx) const |
101 | 4.64M | { |
102 | 4.64M | return data_.at(idx); |
103 | 4.64M | } |
104 | | |
105 | | void |
106 | | mpi::assign(const uint8_t *val, size_t size) |
107 | 688k | { |
108 | 688k | data_.assign(val, val + size); |
109 | 688k | } |
110 | | |
111 | | void |
112 | | mpi::copy(uint8_t *dst) const noexcept |
113 | 318k | { |
114 | 318k | memcpy(dst, data_.data(), data_.size()); |
115 | 318k | } |
116 | | |
117 | | void |
118 | | mpi::resize(size_t size, uint8_t fill) |
119 | 3.82M | { |
120 | 3.82M | data_.resize(size, fill); |
121 | 3.82M | } |
122 | | |
123 | | void |
124 | | mpi::forget() noexcept |
125 | 9.65M | { |
126 | 9.65M | secure_clear(data_.data(), data_.size()); |
127 | 9.65M | data_.resize(0); |
128 | 9.65M | } |
129 | | |
130 | | } // namespace pgp |