BSL v0.0.0
AMMOS Bundle Protocol Security Library (BSL)
Loading...
Searching...
No Matches
bsl_mock_bpa_crc.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 The Johns Hopkins University Applied Physics
3 * Laboratory LLC.
4 *
5 * This file is part of the Bundle Protocol Security Library (BSL).
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * This work was performed for the Jet Propulsion Laboratory, California
18 * Institute of Technology, sponsored by the United States Government under
19 * the prime contract 80NM0018D0004 between the Caltech and NASA under
20 * subcontract 1700763.
21 */
22
28#include "bsl_mock_bpa_crc.h"
29#include <arpa/inet.h>
30#include <inttypes.h>
31#include <BPSecLib_Public.h>
32
33static uint16_t bp_crc16(UsefulBufC data)
34{
35 (void)data;
36 return 1234; // FIXME replace
37}
38
39static uint32_t bp_crc32(UsefulBufC data)
40{
41 (void)data;
42 return 1234567; // FIXME replace
43}
44
45void mock_bpa_crc_apply(UsefulBuf buf, size_t begin, size_t end, BSL_BundleCRCType_e crc_type)
46{
47 switch (crc_type)
48 {
51 // actually process
52 break;
54 default:
55 return;
56 }
57
58 UsefulBufC blk_enc = { (uint8_t *)buf.ptr + begin, end - begin };
59
60 uint8_t *endptr = (uint8_t *)blk_enc.ptr + blk_enc.len;
61
62 switch (crc_type)
63 {
65 {
66 uint16_t *crc_enc = (uint16_t *)endptr - 1; // less one crc value
67
68 const uint16_t crc_val = bp_crc16(blk_enc);
69
70 *crc_enc = htons(crc_val);
71 break;
72 }
74 {
75 uint32_t *crc_enc = (uint32_t *)endptr - 1; // less one crc value
76
77 const uint32_t crc_val = bp_crc32(blk_enc);
78
79 *crc_enc = htonl(crc_val);
80 break;
81 }
82 default:
83 break;
84 }
85}
86
87bool mock_bpa_crc_check(UsefulBufC buf, size_t begin, size_t end, BSL_BundleCRCType_e crc_type)
88{
89 switch (crc_type)
90 {
92 return true;
95 // actually process
96 break;
97 default:
98 return false;
99 }
100
101 UsefulBufC blk_enc = { (uint8_t *)buf.ptr + begin, end - begin };
102
103 const uint8_t *endptr = (uint8_t *)blk_enc.ptr + blk_enc.len;
104
105 bool same = false;
106 switch (crc_type)
107 {
109 {
110 const uint16_t *crc_enc = (uint16_t *)endptr - 1; // less one crc value
111 // copy of original for reference
112 const uint16_t orig_val = ntohs(*crc_enc);
113 const uint16_t crc_val = bp_crc16(blk_enc);
114 same = (crc_val == orig_val);
115 break;
116 }
118 {
119 const uint32_t *crc_enc = (uint32_t *)endptr - 1; // less one crc value
120 // copy of original for reference
121 const uint32_t orig_val = ntohs(*crc_enc);
122
123 const uint16_t crc_val = bp_crc32(blk_enc);
124 same = (crc_val == orig_val);
125 break;
126 }
127 default:
128 break;
129 }
130 return same;
131}
Single entry-point include file for all of the "Public" BPSec Lib (BSL) frontend API.
BSL_BundleCRCType_e
Block CRC types.
@ BSL_BUNDLECRCTYPE_NONE
No CRC value.
@ BSL_BUNDLECRCTYPE_16
CRC-16.
@ BSL_BUNDLECRCTYPE_32
CRC-32C.
void mock_bpa_crc_apply(UsefulBuf buf, size_t begin, size_t end, BSL_BundleCRCType_e crc_type)
Apply a CRC function to an encoded block.
bool mock_bpa_crc_check(UsefulBufC buf, size_t begin, size_t end, BSL_BundleCRCType_e crc_type)
Check the CRC of an encoded block.
Declarations for BPv7 block CRC handling.