-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added pin_policy library implementing both Ledger and SGX pin policies - Updated SGX's trusted HAL access module to use the pin policy library for password validation - Updated the Ledger UI to use the pin policy library for pin validation - Added pin_policy unit tests - Updated failing unit tests
- Loading branch information
1 parent
6d89209
commit 049083c
Showing
9 changed files
with
243 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 RSK Labs Ltd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to | ||
* deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
|
||
#include "pin_policy.h" | ||
|
||
// Helper macros for pin validation | ||
#define IS_IN_RANGE(c, begin, end) (((c) >= (begin)) && ((c) <= (end))) | ||
#define IS_ALPHA(c) (IS_IN_RANGE(c, 'a', 'z') || IS_IN_RANGE(c, 'A', 'Z')) | ||
#define IS_NUM(c) IS_IN_RANGE(c, '0', '9') | ||
#define IS_ALPHANUM(c) (IS_ALPHA(c) || IS_NUM(c)) | ||
|
||
bool pin_policy_is_valid_pin(const char* pin, size_t pin_length) { | ||
// Pin should be exactly MAX_PIN_LENGTH characters long | ||
if (pin_length != MAX_PIN_LENGTH) { | ||
return false; | ||
} | ||
|
||
// Pin should: | ||
// - Be entirely alphanumeric and; | ||
// - Contain at least one alphabetic character | ||
bool hasAlpha = false; | ||
for (size_t i = 0; i < pin_length; i++) { | ||
if (!IS_ALPHANUM(pin[i])) { | ||
return false; | ||
} | ||
hasAlpha |= IS_ALPHA(pin[i]); | ||
} | ||
|
||
return hasAlpha; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 RSK Labs Ltd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to | ||
* deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
|
||
#ifndef __PIN_POLICY_H | ||
#define __PIN_POLICY_H | ||
|
||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
#define MAX_PIN_LENGTH 8 | ||
|
||
/* | ||
* Tells whether the given pin is valid under the pin policy | ||
* | ||
* @param pin the buffer where the pin to validate is stored | ||
* @param pin_length the pin length | ||
* @returns true if pin is valid, false otherwise | ||
*/ | ||
bool pin_policy_is_valid_pin(const char* pin, size_t pin_length); | ||
|
||
#endif // __PIN_POLICY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# The MIT License (MIT) | ||
# | ||
# Copyright (c) 2021 RSK Labs Ltd | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
# of the Software, and to permit persons to whom the Software is furnished to do | ||
# so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
include ../common.mk | ||
|
||
PROG = test.out | ||
OBJS = pin_policy.o test_pin_policy.o | ||
|
||
all: $(PROG) | ||
|
||
$(PROG): $(OBJS) | ||
$(CC) $(COVFLAGS) -o $@ $^ | ||
|
||
.PHONY: clean test | ||
|
||
clean: | ||
rm -f $(PROG) *.o $(COVFILES) | ||
|
||
test: all | ||
./$(PROG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 RSK Labs Ltd | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to | ||
* deal in the Software without restriction, including without limitation the | ||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
* sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
* IN THE SOFTWARE. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <assert.h> | ||
#include <string.h> | ||
|
||
#include "pin_policy.h" | ||
|
||
#define IS_VALID true | ||
#define IS_NOT_VALID false | ||
|
||
// Other helpers | ||
void assert_pin(char *pin, bool expected) { | ||
size_t pin_length = strlen(pin); | ||
assert(pin_policy_is_valid_pin(pin, pin_length) == expected); | ||
} | ||
|
||
void test_validate_ok() { | ||
printf("Test validate pin OK...\n"); | ||
|
||
assert_pin("abcdefgh", IS_VALID); | ||
assert_pin("8b23ef1s", IS_VALID); | ||
assert_pin("MN22P3S9", IS_VALID); | ||
assert_pin("MN22p3s9", IS_VALID); | ||
} | ||
|
||
void test_validate_numeric_pin() { | ||
printf("Test validate pin with only numbers...\n"); | ||
|
||
assert_pin("1234", IS_NOT_VALID); | ||
assert_pin("123456", IS_NOT_VALID); | ||
assert_pin("12345678", IS_NOT_VALID); | ||
assert_pin("1234567890", IS_NOT_VALID); | ||
} | ||
|
||
void test_validate_pin_too_long() { | ||
printf("Test validate pin too long...\n"); | ||
|
||
// Long pins are accepted, but internally capped to PIN_LENGTH | ||
assert_pin("abcdefghi", IS_NOT_VALID); | ||
assert_pin("8b23ef1s85", IS_NOT_VALID); | ||
assert_pin("MN22P3S9P20", IS_NOT_VALID); | ||
assert_pin("MNOPQRSTQDAS", IS_NOT_VALID); | ||
} | ||
|
||
void test_validate_pin_too_short() { | ||
printf("Test validate pin too short...\n"); | ||
|
||
assert_pin("abcdefg", IS_NOT_VALID); | ||
assert_pin("8b23ef", IS_NOT_VALID); | ||
assert_pin("MN22P", IS_NOT_VALID); | ||
assert_pin("MNOP", IS_NOT_VALID); | ||
} | ||
|
||
void test_validate_pin_non_alpha() { | ||
printf("Test validate pin non alpha chars...\n"); | ||
|
||
assert_pin("a1-@.;", IS_NOT_VALID); | ||
assert_pin("!@#$^&*", IS_NOT_VALID); | ||
assert_pin("(),./;']", IS_NOT_VALID); | ||
assert_pin("abcdefg", IS_NOT_VALID); | ||
} | ||
|
||
int main() { | ||
test_validate_ok(); | ||
test_validate_numeric_pin(); | ||
test_validate_pin_too_long(); | ||
test_validate_pin_too_short(); | ||
test_validate_pin_non_alpha(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters