UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
shake.c
1 #include "../../firmware.h"
2 #include "../../test.h"
3 
4 uint8_t util_shake_test_buffer[16];
5 
6 uint8_t util_shake_test_pattern_1600bit[200] = { 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
7  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
8  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
9  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
10  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
11  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
12  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
13  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
14  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
15  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
16  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
17  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
18  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
19  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
20  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
21  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
22  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
23  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
24  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
25  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3 };
26 
27 /* 128-bit result from NIST Test Vectors (SHAKE-128, 0-bit Input Message Length)
28  [ https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf ] */
29 uint8_t util_shake_test_pattern_0bit_output[16] = { 0x7F, 0x9C, 0x2B, 0xA4, 0xE8, 0x8F, 0x82, 0x7D,
30  0x61, 0x60, 0x45, 0x50, 0x76, 0x05, 0x85, 0x3E };
31 
32 /* 128-bit result from NIST Test Vectors (SHAKE-128, 1600-bit Input Message Length)
33  [ https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg1600.pdf ] */
34 uint8_t util_shake_test_pattern_1600bit_output[16] = { 0x13, 0x1A, 0xB8, 0xD2, 0xB5, 0x94, 0x94, 0x6B,
35  0x9C, 0x81, 0x33, 0x3F, 0x9B, 0xB6, 0xE0, 0xCE };
36 
37 /* Sample 1024-bit Packet & 128-bit Key */
38 uint8_t util_shake_test_pattern_packet[256] = { 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
39  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
40  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA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3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
58  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
59  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
60  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
61  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
62  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3,
63  0xA3, 0xA3, 0xA3, 0xA3, 0xA3, 0xA3 };
64 uint8_t util_shake_test_pattern_packet_key[16] = { 0x45, 0xDA, 0x82, 0x92, 0xF7, 0x44, 0xD4, 0xEB,
65  0x6C, 0x81, 0x33, 0x3F, 0x9B, 0xB6, 0xE0, 0xCE };
66 uint8_t util_shake_test_pattern_packet_output[16] = { 0x87, 0x7e, 0x5a, 0xb8, 0x8f, 0xd1, 0x46, 0x6d,
67  0x9a, 0x3d, 0x0b, 0x07, 0x25, 0x85, 0xdd, 0x23 };
68 
69 bool test_shake(void)
70 {
71  util_shake_ctx_t shake_ctx;
72 
73  /* 0-bit Message Test */
74  Util_shake_init(&shake_ctx, 16);
75  Util_shake_out(&shake_ctx, util_shake_test_buffer);
76 
77  if(TEST_VERBOSE)
78  {
79  Debug_print("0-bit SHAKE-128 Output:\r\n");
80  buffer_print_hex(util_shake_test_buffer, 16);
81  }
82 
83  if(memcmp(util_shake_test_buffer, util_shake_test_pattern_0bit_output, 16) != 0)
84  {
85  return false;
86  }
87 
88  /* 1600-bit Message Test */
89  Util_shake_init(&shake_ctx, 16);
90  Util_shake_update(&shake_ctx, util_shake_test_pattern_1600bit, 200);
91 
92  Util_shake_out(&shake_ctx, util_shake_test_buffer);
93 
94  if(TEST_VERBOSE)
95  {
96  Debug_print("1600-bit SHAKE-128 Output:\r\n");
97  buffer_print_hex(util_shake_test_buffer, 16);
98  }
99 
100  if(memcmp(util_shake_test_buffer, util_shake_test_pattern_1600bit_output, 16) != 0)
101  {
102  return false;
103  }
104 
105  return true;
106 }
void Util_shake_init(util_shake_ctx_t *c, uint8_t output_length)
Definition: shake.c:116
void Util_shake_out(util_shake_ctx_t *c, uint8_t *output)
Definition: shake.c:147
void Debug_print(char *fmt,...)
Definition: debug.c:16
void buffer_print_hex(uint8_t *buffer, uint32_t length)
Definition: test.c:53
void Util_shake_update(util_shake_ctx_t *c, const uint8_t *data, uint32_t data_length)
Definition: shake.c:129