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,
41 if (!alg.getParam(paramName, result)) {
42 ThrowErr(Exc::Crypto::InputParam, "Wrong input param");
48 ////////// Validators //////////////
50 // Always validates as true. Useful for checking parameter existence only
52 struct DefaultValidator {
53 static bool Check(const T&) { return true; }
54 static void Why(std::ostringstream& os) { os << "is ok"; }
57 // Validates as true if parameter value is equal to one of Args
64 struct Equals<First> {
66 static bool Check(const T& value) {
67 return First == value;
69 static void Why(std::ostringstream& os) {
70 os << "doesn't match " << static_cast<int>(First);
74 template <T First, T ...Args>
75 struct Equals<First, Args...> : public Equals<First>, public Equals<Args...> {
77 static bool Check(const T& value) {
78 return Equals<First>::Check(value) || Equals<Args...>::Check(value);
80 static void Why(std::ostringstream& os) {
81 Equals<First>::Why(os);
83 Equals<Args...>::Why(os);
89 ////////// Getters //////////////
91 // simply returns parameter value
93 struct DefaultGetter {
94 static T Get(const T& value) { return value; }
95 static void What(std::ostringstream& os) { os << "value"; }
98 // returns buffer param size
99 struct BufferSizeGetter {
100 static size_t Get(const RawBuffer& buffer) { return buffer.size(); }
101 static void What(std::ostringstream& os) { os << "buffer size"; }
105 ////////// ErrorHandlers //////////////
107 struct ThrowingHandler {
108 static void Handle(std::string message) {
109 ThrowErr(Exc::Crypto::InputParam, message);
114 // base class for parameter check
115 struct ParamCheckBase {
116 virtual ~ParamCheckBase() {}
117 virtual void Check(const CryptoAlgorithm& ca) const = 0;
120 typedef std::unique_ptr<const ParamCheckBase> ParamCheckBasePtr;
122 typedef std::vector<ParamCheckBasePtr> ValidatorVector;
125 // ValidatorVector builder. Creates a vector of ParamCheckBasePtr's specified as Args
126 template <typename ...Args>
129 template <typename First>
130 struct VBuilder<First> {
131 static ValidatorVector Build() {
132 ValidatorVector validators;
137 static void Add(ValidatorVector& validators) {
138 validators.emplace_back(new First);
142 template <typename First, typename ...Args>
143 struct VBuilder<First, Args...> : public VBuilder<First>, public VBuilder<Args...> {
144 static ValidatorVector Build() {
145 ValidatorVector validators;
150 static void Add(ValidatorVector& validators) {
151 VBuilder<First>::Add(validators);
152 VBuilder<Args...>::Add(validators);
157 * Generic struct responsible for checking a single constraint on given algorithm parameter
159 * Name - name of param to check
160 * Type - type of param value
161 * Mandatory - true if param is mandatory
162 * Validator - class providing validation function bool Check(const CryptoAlgorithm&)
163 * Getter - gets the value used for validation (param value itself or a buffer size for example)
164 * ErrorHandler - class providing method for error handling void Handle(std::string)
167 template <ParamName Name,
170 typename Validator = DefaultValidator<Type>,
171 typename Getter = DefaultGetter<Type>,
172 typename ErrorHandler = ThrowingHandler>
173 struct ParamCheck : public ParamCheckBase {
174 void Check(const CryptoAlgorithm& ca) const {
176 std::ostringstream os;
179 if(!ca.getParam(Name,value)) {
181 os << "Mandatory parameter " << static_cast<int>(Name) << " doesn't exist";
182 ErrorHandler::Handle(os.str());
187 if(!Validator::Check(Getter::Get(value))) {
190 os << " of param '" << static_cast<int>(Name) << "'=" <<
191 static_cast<int>(Getter::Get(value)) << " ";
193 ErrorHandler::Handle(os.str());
198 } // namespace Crypto