![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/UNITTESTS/features/lorawan/loramaccrypto/Test_LoRaMacCrypto.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2018, Arm Limited and affiliates |
kadonotakashi | 0:8fdf9a60065b | 3 | * SPDX-License-Identifier: Apache-2.0 |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 6 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 7 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 8 | * |
kadonotakashi | 0:8fdf9a60065b | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 14 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 15 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 16 | */ |
kadonotakashi | 0:8fdf9a60065b | 17 | |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "gtest/gtest.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "LoRaMacCrypto.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | |
kadonotakashi | 0:8fdf9a60065b | 21 | #include "cipher_stub.h" |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "cmac_stub.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | #include "aes_stub.h" |
kadonotakashi | 0:8fdf9a60065b | 24 | |
kadonotakashi | 0:8fdf9a60065b | 25 | class Test_LoRaMacCrypto : public testing::Test { |
kadonotakashi | 0:8fdf9a60065b | 26 | protected: |
kadonotakashi | 0:8fdf9a60065b | 27 | LoRaMacCrypto *object; |
kadonotakashi | 0:8fdf9a60065b | 28 | |
kadonotakashi | 0:8fdf9a60065b | 29 | virtual void SetUp() |
kadonotakashi | 0:8fdf9a60065b | 30 | { |
kadonotakashi | 0:8fdf9a60065b | 31 | cipher_stub.info_value = NULL; |
kadonotakashi | 0:8fdf9a60065b | 32 | cipher_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 33 | cipher_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 34 | cmac_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 35 | cmac_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 36 | aes_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 37 | aes_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 38 | object = new LoRaMacCrypto(); |
kadonotakashi | 0:8fdf9a60065b | 39 | } |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | virtual void TearDown() |
kadonotakashi | 0:8fdf9a60065b | 42 | { |
kadonotakashi | 0:8fdf9a60065b | 43 | delete object; |
kadonotakashi | 0:8fdf9a60065b | 44 | } |
kadonotakashi | 0:8fdf9a60065b | 45 | }; |
kadonotakashi | 0:8fdf9a60065b | 46 | |
kadonotakashi | 0:8fdf9a60065b | 47 | TEST_F(Test_LoRaMacCrypto, constructor) |
kadonotakashi | 0:8fdf9a60065b | 48 | { |
kadonotakashi | 0:8fdf9a60065b | 49 | EXPECT_TRUE(object); |
kadonotakashi | 0:8fdf9a60065b | 50 | LoRaMacCrypto obj; |
kadonotakashi | 0:8fdf9a60065b | 51 | } |
kadonotakashi | 0:8fdf9a60065b | 52 | |
kadonotakashi | 0:8fdf9a60065b | 53 | TEST_F(Test_LoRaMacCrypto, compute_mic) |
kadonotakashi | 0:8fdf9a60065b | 54 | { |
kadonotakashi | 0:8fdf9a60065b | 55 | EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 56 | |
kadonotakashi | 0:8fdf9a60065b | 57 | mbedtls_cipher_info_t info; |
kadonotakashi | 0:8fdf9a60065b | 58 | cipher_stub.info_value = &info; |
kadonotakashi | 0:8fdf9a60065b | 59 | cipher_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 60 | cipher_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 61 | EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 62 | |
kadonotakashi | 0:8fdf9a60065b | 63 | cipher_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 64 | cmac_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 65 | cmac_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 66 | EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 67 | |
kadonotakashi | 0:8fdf9a60065b | 68 | cmac_stub.int_zero_counter = 1; |
kadonotakashi | 0:8fdf9a60065b | 69 | cmac_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 70 | EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 71 | |
kadonotakashi | 0:8fdf9a60065b | 72 | cmac_stub.int_zero_counter = 2; |
kadonotakashi | 0:8fdf9a60065b | 73 | cmac_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 74 | EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 75 | |
kadonotakashi | 0:8fdf9a60065b | 76 | cmac_stub.int_zero_counter = 3; |
kadonotakashi | 0:8fdf9a60065b | 77 | cmac_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 78 | EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 79 | |
kadonotakashi | 0:8fdf9a60065b | 80 | uint32_t mic[16]; |
kadonotakashi | 0:8fdf9a60065b | 81 | cmac_stub.int_zero_counter = 3; |
kadonotakashi | 0:8fdf9a60065b | 82 | cmac_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 83 | EXPECT_TRUE(0 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, mic)); |
kadonotakashi | 0:8fdf9a60065b | 84 | |
kadonotakashi | 0:8fdf9a60065b | 85 | } |
kadonotakashi | 0:8fdf9a60065b | 86 | |
kadonotakashi | 0:8fdf9a60065b | 87 | TEST_F(Test_LoRaMacCrypto, encrypt_payload) |
kadonotakashi | 0:8fdf9a60065b | 88 | { |
kadonotakashi | 0:8fdf9a60065b | 89 | aes_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 90 | aes_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 91 | EXPECT_TRUE(-1 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 92 | |
kadonotakashi | 0:8fdf9a60065b | 93 | aes_stub.int_zero_counter = 1; |
kadonotakashi | 0:8fdf9a60065b | 94 | aes_stub.int_value = -2; |
kadonotakashi | 0:8fdf9a60065b | 95 | uint8_t buf[60]; |
kadonotakashi | 0:8fdf9a60065b | 96 | uint8_t enc[60]; |
kadonotakashi | 0:8fdf9a60065b | 97 | EXPECT_TRUE(-2 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc)); |
kadonotakashi | 0:8fdf9a60065b | 98 | |
kadonotakashi | 0:8fdf9a60065b | 99 | aes_stub.int_zero_counter = 2; |
kadonotakashi | 0:8fdf9a60065b | 100 | aes_stub.int_value = -3; |
kadonotakashi | 0:8fdf9a60065b | 101 | EXPECT_TRUE(-3 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc)); |
kadonotakashi | 0:8fdf9a60065b | 102 | |
kadonotakashi | 0:8fdf9a60065b | 103 | aes_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 104 | EXPECT_TRUE(0 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc)); |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | EXPECT_TRUE(0 == object->encrypt_payload(buf, 60, NULL, 0, 0, 0, 0, enc)); |
kadonotakashi | 0:8fdf9a60065b | 107 | |
kadonotakashi | 0:8fdf9a60065b | 108 | aes_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 109 | EXPECT_TRUE(0 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 110 | } |
kadonotakashi | 0:8fdf9a60065b | 111 | |
kadonotakashi | 0:8fdf9a60065b | 112 | TEST_F(Test_LoRaMacCrypto, decrypt_payload) |
kadonotakashi | 0:8fdf9a60065b | 113 | { |
kadonotakashi | 0:8fdf9a60065b | 114 | EXPECT_TRUE(0 == object->decrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 115 | } |
kadonotakashi | 0:8fdf9a60065b | 116 | |
kadonotakashi | 0:8fdf9a60065b | 117 | TEST_F(Test_LoRaMacCrypto, compute_join_frame_mic) |
kadonotakashi | 0:8fdf9a60065b | 118 | { |
kadonotakashi | 0:8fdf9a60065b | 119 | uint32_t mic[16]; |
kadonotakashi | 0:8fdf9a60065b | 120 | EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 121 | mbedtls_cipher_info_t info; |
kadonotakashi | 0:8fdf9a60065b | 122 | cipher_stub.info_value = &info; |
kadonotakashi | 0:8fdf9a60065b | 123 | cipher_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 124 | cipher_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 125 | EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | cipher_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 128 | cmac_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 129 | cmac_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 130 | EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 131 | |
kadonotakashi | 0:8fdf9a60065b | 132 | cmac_stub.int_zero_counter = 1; |
kadonotakashi | 0:8fdf9a60065b | 133 | cmac_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 134 | EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 135 | |
kadonotakashi | 0:8fdf9a60065b | 136 | cmac_stub.int_zero_counter = 2; |
kadonotakashi | 0:8fdf9a60065b | 137 | cmac_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 138 | EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 139 | |
kadonotakashi | 0:8fdf9a60065b | 140 | cmac_stub.int_zero_counter = 3; |
kadonotakashi | 0:8fdf9a60065b | 141 | cmac_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 142 | EXPECT_TRUE(0 == object->compute_join_frame_mic(NULL, 0, NULL, 0, mic)); |
kadonotakashi | 0:8fdf9a60065b | 143 | } |
kadonotakashi | 0:8fdf9a60065b | 144 | |
kadonotakashi | 0:8fdf9a60065b | 145 | TEST_F(Test_LoRaMacCrypto, decrypt_join_frame) |
kadonotakashi | 0:8fdf9a60065b | 146 | { |
kadonotakashi | 0:8fdf9a60065b | 147 | aes_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 148 | aes_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 149 | EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 150 | |
kadonotakashi | 0:8fdf9a60065b | 151 | aes_stub.int_zero_counter = 1; |
kadonotakashi | 0:8fdf9a60065b | 152 | aes_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 153 | EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL)); |
kadonotakashi | 0:8fdf9a60065b | 154 | |
kadonotakashi | 0:8fdf9a60065b | 155 | aes_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 156 | uint8_t buf[60]; |
kadonotakashi | 0:8fdf9a60065b | 157 | uint8_t enc[60]; |
kadonotakashi | 0:8fdf9a60065b | 158 | EXPECT_TRUE(0 == object->decrypt_join_frame(buf, 60, NULL, 0, enc)); |
kadonotakashi | 0:8fdf9a60065b | 159 | } |
kadonotakashi | 0:8fdf9a60065b | 160 | |
kadonotakashi | 0:8fdf9a60065b | 161 | TEST_F(Test_LoRaMacCrypto, compute_skeys_for_join_frame) |
kadonotakashi | 0:8fdf9a60065b | 162 | { |
kadonotakashi | 0:8fdf9a60065b | 163 | uint8_t nwk_key[16]; |
kadonotakashi | 0:8fdf9a60065b | 164 | uint8_t app_key[16]; |
kadonotakashi | 0:8fdf9a60065b | 165 | uint8_t nonce[16]; |
kadonotakashi | 0:8fdf9a60065b | 166 | |
kadonotakashi | 0:8fdf9a60065b | 167 | aes_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 168 | aes_stub.int_value = -1; |
kadonotakashi | 0:8fdf9a60065b | 169 | EXPECT_TRUE(-1 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key)); |
kadonotakashi | 0:8fdf9a60065b | 170 | |
kadonotakashi | 0:8fdf9a60065b | 171 | aes_stub.int_zero_counter = 1; |
kadonotakashi | 0:8fdf9a60065b | 172 | aes_stub.int_value = -2; |
kadonotakashi | 0:8fdf9a60065b | 173 | EXPECT_TRUE(-2 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key)); |
kadonotakashi | 0:8fdf9a60065b | 174 | |
kadonotakashi | 0:8fdf9a60065b | 175 | aes_stub.int_zero_counter = 0; |
kadonotakashi | 0:8fdf9a60065b | 176 | aes_stub.int_value = 0; |
kadonotakashi | 0:8fdf9a60065b | 177 | EXPECT_TRUE(0 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key)); |
kadonotakashi | 0:8fdf9a60065b | 178 | } |