Line data Source code
1 : /** 2 : * The MIT License (MIT) 3 : * 4 : * Copyright (c) 2021 RSK Labs Ltd 5 : * 6 : * Permission is hereby granted, free of charge, to any person obtaining a copy 7 : * of this software and associated documentation files (the "Software"), to 8 : * deal in the Software without restriction, including without limitation the 9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 : * sell copies of the Software, and to permit persons to whom the Software is 11 : * furnished to do so, subject to the following conditions: 12 : * 13 : * The above copyright notice and this permission notice shall be included in 14 : * all copies or substantial portions of the Software. 15 : * 16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 : * IN THE SOFTWARE. 23 : */ 24 : 25 : #include "pin_policy.h" 26 : 27 : // Helper macros for pin validation 28 : #define IS_IN_RANGE(c, begin, end) (((c) >= (begin)) && ((c) <= (end))) 29 : #define IS_ALPHA(c) (IS_IN_RANGE(c, 'a', 'z') || IS_IN_RANGE(c, 'A', 'Z')) 30 : #define IS_NUM(c) IS_IN_RANGE(c, '0', '9') 31 : #define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c)) 32 : 33 45 : bool pin_policy_is_valid_pin(const char* pin, size_t pin_length) { 34 : // Pin should be exactly MAX_PIN_LENGTH characters long 35 45 : if (pin_length != MAX_PIN_LENGTH) { 36 23 : return false; 37 : } 38 : 39 : // Pin should: 40 : // - Be entirely alphanumeric and; 41 : // - Contain at least one alphabetic character 42 22 : bool hasAlpha = false; 43 182 : for (size_t i = 0; i < pin_length; i++) { 44 162 : if (!IS_ALPHANUM(pin[i])) { 45 2 : return false; 46 : } 47 160 : hasAlpha |= IS_ALPHA(pin[i]); 48 : } 49 : 50 20 : return hasAlpha; 51 : }