2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file algo-validation.h
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
28 #include <ckm/ckm-type.h>
30 #include <exception.h>
37 const CryptoAlgorithm &alg,
42 if (!alg.getParam(paramName, result))
43 ThrowErr(Exc::Crypto::InputParam, "Wrong input param");
49 ////////// Validators //////////////
51 // Always validates as true. Useful for checking parameter existence only
53 struct DefaultValidator {
54 static bool Check(const T &)
58 static void Why(std::ostringstream &os)
64 // Validates as true if parameter value is equal to one of Args
71 struct Equals<First> {
73 static bool Check(const T &value)
75 return First == value;
78 static void Why(std::ostringstream &os)
80 os << "doesn't match " << static_cast<int>(First);
84 template <T First, T ...Args>
85 struct Equals<First, Args...> : public Equals<First>, public Equals<Args...> {
87 static bool Check(const T &value)
89 return Equals<First>::Check(value) || Equals<Args...>::Check(value);
92 static void Why(std::ostringstream &os)
94 Equals<First>::Why(os);
96 Equals<Args...>::Why(os);
101 template <typename T>
103 static bool Check(const T &)
107 static void Why(std::ostringstream &os)
109 os << "is not supported";
114 ////////// Getters //////////////
116 // simply returns parameter value
117 template <typename T>
118 struct DefaultGetter {
119 static T Get(const T &value)
123 static void What(std::ostringstream &os)
127 static void Print(std::ostringstream &os, const T &value)
129 os << static_cast<int>(value);
134 inline void DefaultGetter<RawBuffer>::Print(std::ostringstream &os,
135 const RawBuffer &buffer)
137 os << "[" << buffer.size() << "B buffer]";
140 // returns buffer param size
141 struct BufferSizeGetter {
142 static size_t Get(const RawBuffer &buffer)
144 return buffer.size();
146 static void What(std::ostringstream &os)
150 static void Print(std::ostringstream &os, const RawBuffer &buffer)
157 ////////// ErrorHandlers //////////////
159 struct ThrowingHandler {
160 static void Handle(std::string message)
162 ThrowErr(Exc::Crypto::InputParam, message);
167 // base class for parameter check
168 struct ParamCheckBase {
169 virtual ~ParamCheckBase() {}
170 virtual void Check(const CryptoAlgorithm &ca) const = 0;
173 typedef std::unique_ptr<const ParamCheckBase> ParamCheckBasePtr;
175 typedef std::vector<ParamCheckBasePtr> ValidatorVector;
178 // ValidatorVector builder. Creates a vector of ParamCheckBasePtr's specified as Args
179 template <typename ...Args>
184 static ValidatorVector Build()
186 return ValidatorVector();
190 template <typename First>
191 struct VBuilder<First> {
192 static ValidatorVector Build()
194 ValidatorVector validators;
199 static void Add(ValidatorVector &validators)
201 validators.emplace_back(new First);
205 template <typename First, typename ...Args>
206 struct VBuilder<First, Args...> : public VBuilder<First>,
207 public VBuilder<Args...> {
208 static ValidatorVector Build()
210 ValidatorVector validators;
216 static void Add(ValidatorVector &validators)
218 VBuilder<First>::Add(validators);
219 VBuilder<Args...>::Add(validators);
224 * Generic struct responsible for checking a single constraint on given algorithm parameter
226 * Name - name of param to check
227 * Type - type of param value
228 * Mandatory - true if param is mandatory
229 * Validator - class providing validation function bool Check(const CryptoAlgorithm&)
230 * Getter - gets the value used for validation (param value itself or a buffer size for example)
231 * ErrorHandler - class providing method for error handling void Handle(std::string)
234 template <ParamName Name,
237 typename Validator = DefaultValidator<Type>,
238 typename Getter = DefaultGetter<Type>,
239 typename ErrorHandler = ThrowingHandler>
240 struct ParamCheck : public ParamCheckBase {
241 void Check(const CryptoAlgorithm &ca) const
244 std::ostringstream os;
247 if (!ca.getParam(Name, value)) {
249 os << "Mandatory parameter " << static_cast<int>(Name) << " doesn't exist";
250 ErrorHandler::Handle(os.str());
257 if (!Validator::Check(Getter::Get(value))) {
260 os << " of param '" << static_cast<int>(Name) << "'=";
261 Getter::Print(os, value);
264 ErrorHandler::Handle(os.str());
269 } // namespace Crypto