Noise-C
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
patterns.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "internal.h"
24 
31 #define FLAGS(x) ((uint8_t)((x) & 0xFF)), ((uint8_t)(((x) >> 8) & 0xFF))
32 
44 static uint8_t const noise_pattern_N[] = {
45  FLAGS(
49  ),
50 
54 };
55 
67 static uint8_t const noise_pattern_K[] = {
68  FLAGS(
74  ),
75 
80 };
81 
92 static uint8_t const noise_pattern_X[] = {
93  FLAGS(
98  ),
99 
105 };
106 
116 static uint8_t const noise_pattern_NN[] = {
117  FLAGS(
120  ),
121 
127 };
128 
140 static uint8_t const noise_pattern_NK[] = {
141  FLAGS(
146  ),
147 
154 };
155 
165 static uint8_t const noise_pattern_NX[] = {
166  FLAGS(
170  ),
171 
179 };
180 
191 static uint8_t const noise_pattern_XN[] = {
192  FLAGS(
196  ),
197 
206 };
207 
220 static uint8_t const noise_pattern_XK[] = {
221  FLAGS(
227  ),
228 
238 };
239 
250 static uint8_t const noise_pattern_XX[] = {
251  FLAGS(
256  ),
257 
268 };
269 
281 static uint8_t const noise_pattern_KN[] = {
282  FLAGS(
287  ),
288 
295 };
296 
309 static uint8_t const noise_pattern_KK[] = {
310  FLAGS(
317  ),
318 
327 };
328 
340 static uint8_t const noise_pattern_KX[] = {
341  FLAGS(
347  ),
348 
357 };
358 
368 static uint8_t const noise_pattern_IN[] = {
369  FLAGS(
373  ),
374 
382 };
383 
395 static uint8_t const noise_pattern_IK[] = {
396  FLAGS(
402  ),
403 
413 };
414 
424 static uint8_t const noise_pattern_IX[] = {
425  FLAGS(
430  ),
431 
441 };
442 
454 static uint8_t const noise_pattern_XXfallback[] = {
455  FLAGS(
461  ),
462 
471 };
472 
483 static uint8_t const noise_pattern_Xnoidh[] = {
484  FLAGS(
489  ),
490 
496 };
497 
507 static uint8_t const noise_pattern_NXnoidh[] = {
508  FLAGS(
512  ),
513 
521 };
522 
533 static uint8_t const noise_pattern_XXnoidh[] = {
534  FLAGS(
539  ),
540 
551 };
552 
564 static uint8_t const noise_pattern_KXnoidh[] = {
565  FLAGS(
571  ),
572 
581 };
582 
594 static uint8_t const noise_pattern_IKnoidh[] = {
595  FLAGS(
601  ),
602 
612 };
613 
623 static uint8_t const noise_pattern_IXnoidh[] = {
624  FLAGS(
629  ),
630 
640 };
641 
651 static uint8_t const noise_pattern_NNhfs[] = {
652  FLAGS(
657  ),
658 
667 };
668 
680 static uint8_t const noise_pattern_NKhfs[] = {
681  FLAGS(
688  ),
689 
699 };
700 
710 static uint8_t const noise_pattern_NXhfs[] = {
711  FLAGS(
717  ),
718 
729 };
730 
741 static uint8_t const noise_pattern_XNhfs[] = {
742  FLAGS(
748  ),
749 
761 };
762 
775 static uint8_t const noise_pattern_XKhfs[] = {
776  FLAGS(
784  ),
785 
798 };
799 
810 static uint8_t const noise_pattern_XXhfs[] = {
811  FLAGS(
818  ),
819 
833 };
834 
846 static uint8_t const noise_pattern_KNhfs[] = {
847  FLAGS(
854  ),
855 
865 };
866 
879 static uint8_t const noise_pattern_KKhfs[] = {
880  FLAGS(
889  ),
890 
902 };
903 
915 static uint8_t const noise_pattern_KXhfs[] = {
916  FLAGS(
924  ),
925 
937 };
938 
948 static uint8_t const noise_pattern_INhfs[] = {
949  FLAGS(
955  ),
956 
967 };
968 
980 static uint8_t const noise_pattern_IKhfs[] = {
981  FLAGS(
989  ),
990 
1003 };
1004 
1014 static uint8_t const noise_pattern_IXhfs[] = {
1015  FLAGS(
1022  ),
1023 
1024  NOISE_TOKEN_E,
1025  NOISE_TOKEN_F,
1026  NOISE_TOKEN_S,
1028  NOISE_TOKEN_E,
1029  NOISE_TOKEN_F,
1033  NOISE_TOKEN_S,
1036 };
1037 
1049 static uint8_t const noise_pattern_XXfallback_hfs[] = {
1050  FLAGS(
1059  ),
1060 
1061  NOISE_TOKEN_E,
1062  NOISE_TOKEN_F,
1065  NOISE_TOKEN_S,
1068  NOISE_TOKEN_S,
1071 };
1072 
1082 static uint8_t const noise_pattern_NXnoidh_hfs[] = {
1083  FLAGS(
1089  ),
1090 
1091  NOISE_TOKEN_E,
1092  NOISE_TOKEN_F,
1094  NOISE_TOKEN_E,
1095  NOISE_TOKEN_F,
1096  NOISE_TOKEN_S,
1101 };
1102 
1113 static uint8_t const noise_pattern_XXnoidh_hfs[] = {
1114  FLAGS(
1121  ),
1122 
1123  NOISE_TOKEN_E,
1124  NOISE_TOKEN_F,
1126  NOISE_TOKEN_E,
1127  NOISE_TOKEN_F,
1128  NOISE_TOKEN_S,
1133  NOISE_TOKEN_S,
1136 };
1137 
1149 static uint8_t const noise_pattern_KXnoidh_hfs[] = {
1150  FLAGS(
1158  ),
1159 
1160  NOISE_TOKEN_E,
1161  NOISE_TOKEN_F,
1163  NOISE_TOKEN_E,
1164  NOISE_TOKEN_F,
1165  NOISE_TOKEN_S,
1171 };
1172 
1184 static uint8_t const noise_pattern_IKnoidh_hfs[] = {
1185  FLAGS(
1193  ),
1194 
1195  NOISE_TOKEN_E,
1196  NOISE_TOKEN_F,
1197  NOISE_TOKEN_S,
1201  NOISE_TOKEN_E,
1202  NOISE_TOKEN_F,
1207 };
1208 
1218 static uint8_t const noise_pattern_IXnoidh_hfs[] = {
1219  FLAGS(
1226  ),
1227 
1228  NOISE_TOKEN_E,
1229  NOISE_TOKEN_F,
1230  NOISE_TOKEN_S,
1232  NOISE_TOKEN_E,
1233  NOISE_TOKEN_F,
1234  NOISE_TOKEN_S,
1240 };
1241 
1253 const uint8_t *noise_pattern_lookup(int id)
1254 {
1255  switch (id) {
1256  case NOISE_PATTERN_N: return noise_pattern_N;
1257  case NOISE_PATTERN_K: return noise_pattern_K;
1258  case NOISE_PATTERN_X: return noise_pattern_X;
1259  case NOISE_PATTERN_NN: return noise_pattern_NN;
1260  case NOISE_PATTERN_NK: return noise_pattern_NK;
1261  case NOISE_PATTERN_NX: return noise_pattern_NX;
1262  case NOISE_PATTERN_XN: return noise_pattern_XN;
1263  case NOISE_PATTERN_XK: return noise_pattern_XK;
1264  case NOISE_PATTERN_XX: return noise_pattern_XX;
1265  case NOISE_PATTERN_KN: return noise_pattern_KN;
1266  case NOISE_PATTERN_KK: return noise_pattern_KK;
1267  case NOISE_PATTERN_KX: return noise_pattern_KX;
1268  case NOISE_PATTERN_IN: return noise_pattern_IN;
1269  case NOISE_PATTERN_IK: return noise_pattern_IK;
1270  case NOISE_PATTERN_IX: return noise_pattern_IX;
1271  case NOISE_PATTERN_XX_FALLBACK: return noise_pattern_XXfallback;
1272  case NOISE_PATTERN_X_NOIDH: return noise_pattern_Xnoidh;
1273  case NOISE_PATTERN_NX_NOIDH: return noise_pattern_NXnoidh;
1274  case NOISE_PATTERN_XX_NOIDH: return noise_pattern_XXnoidh;
1275  case NOISE_PATTERN_KX_NOIDH: return noise_pattern_KXnoidh;
1276  case NOISE_PATTERN_IK_NOIDH: return noise_pattern_IKnoidh;
1277  case NOISE_PATTERN_IX_NOIDH: return noise_pattern_IXnoidh;
1278  case NOISE_PATTERN_NN_HFS: return noise_pattern_NNhfs;
1279  case NOISE_PATTERN_NK_HFS: return noise_pattern_NKhfs;
1280  case NOISE_PATTERN_NX_HFS: return noise_pattern_NXhfs;
1281  case NOISE_PATTERN_XN_HFS: return noise_pattern_XNhfs;
1282  case NOISE_PATTERN_XK_HFS: return noise_pattern_XKhfs;
1283  case NOISE_PATTERN_XX_HFS: return noise_pattern_XXhfs;
1284  case NOISE_PATTERN_KN_HFS: return noise_pattern_KNhfs;
1285  case NOISE_PATTERN_KK_HFS: return noise_pattern_KKhfs;
1286  case NOISE_PATTERN_KX_HFS: return noise_pattern_KXhfs;
1287  case NOISE_PATTERN_IN_HFS: return noise_pattern_INhfs;
1288  case NOISE_PATTERN_IK_HFS: return noise_pattern_IKhfs;
1289  case NOISE_PATTERN_IX_HFS: return noise_pattern_IXhfs;
1290  case NOISE_PATTERN_XX_FALLBACK_HFS: return noise_pattern_XXfallback_hfs;
1291  case NOISE_PATTERN_NX_NOIDH_HFS: return noise_pattern_NXnoidh_hfs;
1292  case NOISE_PATTERN_XX_NOIDH_HFS: return noise_pattern_XXnoidh_hfs;
1293  case NOISE_PATTERN_KX_NOIDH_HFS: return noise_pattern_KXnoidh_hfs;
1294  case NOISE_PATTERN_IK_NOIDH_HFS: return noise_pattern_IKnoidh_hfs;
1295  case NOISE_PATTERN_IX_NOIDH_HFS: return noise_pattern_IXnoidh_hfs;
1296  default: return 0;
1297  }
1298 }
1299 
1306 NoisePatternFlags_t noise_pattern_reverse_flags(NoisePatternFlags_t flags)
1307 {
1308  return ((flags >> 8) & 0x00FF) | ((flags << 8) & 0xFF00);
1309 }
#define NOISE_PATTERN_XX
Handshake pattern identifier for "XX".
#define NOISE_PAT_FLAG_LOCAL_EPHEMERAL
Definition: internal.h:603
#define NOISE_PATTERN_NX_NOIDH_HFS
Definition: constants.h:93
#define NOISE_PATTERN_NX_HFS
Definition: constants.h:82
#define NOISE_PATTERN_KX
Handshake pattern identifier for "KX".
#define NOISE_PATTERN_KX_NOIDH
Handshake pattern identifier for "KXnoidh".
#define NOISE_PATTERN_KX_HFS
Definition: constants.h:88
#define NOISE_TOKEN_FF
Definition: internal.h:597
#define NOISE_PAT_FLAG_REMOTE_HYBRID_REQ
Definition: internal.h:634
#define NOISE_PATTERN_IK_HFS
Definition: constants.h:90
#define NOISE_PATTERN_IK
Handshake pattern identifier for "IK".
#define NOISE_PAT_FLAG_LOCAL_REQUIRED
Definition: internal.h:608
#define NOISE_TOKEN_EE
Definition: internal.h:592
#define NOISE_PATTERN_IX
Handshake pattern identifier for "IX".
#define NOISE_PATTERN_NN_HFS
Definition: constants.h:80
#define NOISE_PATTERN_XN
Handshake pattern identifier for "XN".
#define NOISE_PATTERN_KK_HFS
Definition: constants.h:87
#define NOISE_PATTERN_XK
Handshake pattern identifier for "XK".
#define NOISE_PATTERN_NK
Handshake pattern identifier for "NK".
NoisePatternFlags_t noise_pattern_reverse_flags(NoisePatternFlags_t flags)
Reverses the local and remote flags for a pattern.
Definition: patterns.c:1306
#define NOISE_PATTERN_XN_HFS
Definition: constants.h:83
#define NOISE_PAT_FLAG_LOCAL_STATIC
Definition: internal.h:601
#define NOISE_PATTERN_XK_HFS
Definition: constants.h:84
#define NOISE_PAT_FLAG_REMOTE_STATIC
Definition: internal.h:619
#define NOISE_PATTERN_KK
Handshake pattern identifier for "KK".
#define NOISE_PATTERN_NK_HFS
Definition: constants.h:81
#define NOISE_TOKEN_F
Definition: internal.h:596
#define NOISE_PAT_FLAG_REMOTE_HYBRID
Definition: internal.h:631
#define NOISE_TOKEN_ES
Definition: internal.h:593
#define NOISE_PAT_FLAG_REMOTE_EPHEMERAL
Definition: internal.h:621
#define NOISE_PAT_FLAG_REMOTE_EPHEM_REQ
Definition: internal.h:629
#define NOISE_PAT_FLAG_REMOTE_REQUIRED
Definition: internal.h:626
#define NOISE_PATTERN_NX_NOIDH
Handshake pattern identifier for "NXnoidh".
const uint8_t * noise_pattern_lookup(int id)
Looks up a specific handshake pattern.
Definition: patterns.c:1253
#define NOISE_TOKEN_FLIP_DIR
Definition: internal.h:598
#define NOISE_PATTERN_K
Handshake pattern identifier for "K".
#define NOISE_PATTERN_IX_NOIDH_HFS
Definition: constants.h:97
#define NOISE_TOKEN_END
Definition: internal.h:589
#define NOISE_PATTERN_IN
Handshake pattern identifier for "IN".
Internal definitions for the library.
#define NOISE_PATTERN_IN_HFS
Definition: constants.h:89
#define NOISE_PATTERN_IK_NOIDH
Handshake pattern identifier for "IKnoidh".
#define NOISE_PATTERN_NX
Handshake pattern identifier for "NX".
#define NOISE_PATTERN_KN_HFS
Definition: constants.h:86
#define NOISE_PATTERN_XX_HFS
Definition: constants.h:85
#define NOISE_TOKEN_S
Definition: internal.h:590
#define NOISE_TOKEN_SS
Definition: internal.h:595
#define NOISE_PATTERN_NN
Handshake pattern identifier for "NN".
#define NOISE_PATTERN_IX_HFS
Definition: constants.h:91
#define NOISE_PATTERN_XX_FALLBACK
Handshake pattern identifier for "XXfallback".
#define NOISE_PATTERN_XX_NOIDH
Handshake pattern identifier for "XXnoidh".
#define NOISE_PATTERN_N
Handshake pattern identifier for "N".
#define NOISE_PATTERN_KN
Handshake pattern identifier for "KN".
#define NOISE_PATTERN_X_NOIDH
Handshake pattern identifier for "Xnoidh".
#define NOISE_PATTERN_IK_NOIDH_HFS
Definition: constants.h:96
#define NOISE_TOKEN_SE
Definition: internal.h:594
#define NOISE_PATTERN_XX_FALLBACK_HFS
Definition: constants.h:92
#define NOISE_PATTERN_KX_NOIDH_HFS
Definition: constants.h:95
#define NOISE_PAT_FLAG_LOCAL_HYBRID
Definition: internal.h:613
#define NOISE_TOKEN_E
Definition: internal.h:591
#define NOISE_PATTERN_XX_NOIDH_HFS
Definition: constants.h:94
#define NOISE_PATTERN_IX_NOIDH
Handshake pattern identifier for "IXnoidh".
#define NOISE_PATTERN_X
Handshake pattern identifier for "X".