-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathhandshakestate.c
1782 lines (1684 loc) · 69.1 KB
/
handshakestate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 Southern Storm Software, Pty 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 "internal.h"
#include <string.h>
#include <stdlib.h>
/**
* \file handshakestate.h
* \brief HandshakeState interface
*/
/**
* \file handshakestate.c
* \brief HandshakeState implementation
*/
/**
* \defgroup handshakestate HandshakeState API
*
* See the \ref example_echo "echo example" for an overview of how
* to use this API.
*/
/**@{*/
/**
* \typedef NoiseHandshakeState
* \brief Opaque object that represents a HandshakeState.
*/
/**
* \brief Gets the initial requirements for a handshake pattern.
*
* \param flags The flags from the handshake pattern.
* \param prefix_id The prefix identifier from the protocol name.
* \param role The role, either initiator or responder.
* \param is_fallback Non-zero if we are processing a fallback pattern.
*
* \return The key requirements for the handshake pattern.
*/
static int noise_handshakestate_requirements
(NoisePatternFlags_t flags, int prefix_id, int role, int is_fallback)
{
int requirements = 0;
if (flags & NOISE_PAT_FLAG_LOCAL_STATIC) {
requirements |= NOISE_REQ_LOCAL_REQUIRED;
}
if (flags & NOISE_PAT_FLAG_LOCAL_REQUIRED) {
requirements |= NOISE_REQ_LOCAL_REQUIRED;
requirements |= NOISE_REQ_LOCAL_PREMSG;
}
if (flags & NOISE_PAT_FLAG_REMOTE_REQUIRED) {
requirements |= NOISE_REQ_REMOTE_REQUIRED;
requirements |= NOISE_REQ_REMOTE_PREMSG;
}
if (flags & (NOISE_PAT_FLAG_REMOTE_EPHEM_REQ |
NOISE_PAT_FLAG_LOCAL_EPHEM_REQ)) {
if (is_fallback)
requirements |= NOISE_REQ_FALLBACK_PREMSG;
}
if (prefix_id == NOISE_PREFIX_PSK) {
requirements |= NOISE_REQ_PSK;
}
return requirements;
}
/**
* \brief Creates a new HandshakeState object.
*
* \param state Points to the variable where to store the pointer to
* the new HandshakeState object.
* \param symmetric The pre-allocated SymmetricState, which contains
* the protocol identifier.
* \param role The role for the new object, either NOISE_ROLE_INITIATOR or
* NOISE_ROLE_RESPONDER.
*
* \return NOISE_ERROR_NONE on success, or some other error code on failure.
* The \a symmetric object must be destroyed by this function if it fails.
*
* This is the internal implementation of noise_handshakestate_new_by_id()
* and noise_handshakestate_new_by_name().
*/
static int noise_handshakestate_new
(NoiseHandshakeState **state, NoiseSymmetricState *symmetric, int role)
{
const uint8_t *pattern;
int dh_id;
int hybrid_id;
NoisePatternFlags_t flags;
int extra_reqs = 0;
int err;
int local_dh_role;
int remote_dh_role;
/* Locate the information for the current handshake pattern */
pattern = noise_pattern_lookup(symmetric->id.pattern_id);
if (!pattern) {
noise_symmetricstate_free(symmetric);
return NOISE_ERROR_UNKNOWN_ID;
}
flags = ((NoisePatternFlags_t)(pattern[0])) |
(((NoisePatternFlags_t)(pattern[1])) << 8);
if ((flags & NOISE_PAT_FLAG_REMOTE_REQUIRED) != 0)
extra_reqs |= NOISE_REQ_FALLBACK_POSSIBLE;
if (role == NOISE_ROLE_RESPONDER) {
/* Reverse the pattern flags so that the responder is "local" */
flags = noise_pattern_reverse_flags(flags);
}
/* Determine the role to use for DHState objects */
if ((flags & NOISE_PAT_FLAG_REMOTE_EPHEM_REQ) != 0) {
/* Fallback pattern - reverse the DHState role */
if (role == NOISE_ROLE_INITIATOR) {
local_dh_role = NOISE_ROLE_RESPONDER;
remote_dh_role = NOISE_ROLE_INITIATOR;
} else {
local_dh_role = NOISE_ROLE_INITIATOR;
remote_dh_role = NOISE_ROLE_RESPONDER;
}
} else {
/* Regular pattern */
if (role == NOISE_ROLE_INITIATOR) {
local_dh_role = NOISE_ROLE_INITIATOR;
remote_dh_role = NOISE_ROLE_RESPONDER;
} else {
local_dh_role = NOISE_ROLE_RESPONDER;
remote_dh_role = NOISE_ROLE_INITIATOR;
}
}
/* Create the HandshakeState object */
*state = noise_new(NoiseHandshakeState);
if (!(*state)) {
noise_symmetricstate_free(symmetric);
return NOISE_ERROR_NO_MEMORY;
}
/* Initialize the HandshakeState */
(*state)->requirements = extra_reqs | noise_handshakestate_requirements
(flags, symmetric->id.prefix_id, role, 0);
(*state)->action = NOISE_ACTION_NONE;
(*state)->tokens = pattern + 2;
(*state)->role = role;
(*state)->symmetric = symmetric;
/* Create DHState objects for all of the keys we will need later */
err = NOISE_ERROR_NONE;
dh_id = symmetric->id.dh_id;
hybrid_id = symmetric->id.hybrid_id;
if ((flags & NOISE_PAT_FLAG_LOCAL_STATIC) != 0) {
err = noise_dhstate_new_by_id(&((*state)->dh_local_static), dh_id);
}
if ((flags & NOISE_PAT_FLAG_LOCAL_EPHEMERAL) != 0 && err == NOISE_ERROR_NONE) {
err = noise_dhstate_new_by_id(&((*state)->dh_local_ephemeral), dh_id);
}
if ((flags & NOISE_PAT_FLAG_LOCAL_HYBRID) != 0 && err == NOISE_ERROR_NONE) {
if (hybrid_id != NOISE_DH_NONE)
err = noise_dhstate_new_by_id(&((*state)->dh_local_hybrid), hybrid_id);
else
err = NOISE_ERROR_NOT_APPLICABLE;
} else if (hybrid_id != NOISE_DH_NONE) {
err = NOISE_ERROR_NOT_APPLICABLE;
}
if ((flags & NOISE_PAT_FLAG_REMOTE_STATIC) != 0 && err == NOISE_ERROR_NONE) {
err = noise_dhstate_new_by_id(&((*state)->dh_remote_static), dh_id);
}
if ((flags & NOISE_PAT_FLAG_REMOTE_EPHEMERAL) != 0 && err == NOISE_ERROR_NONE) {
err = noise_dhstate_new_by_id(&((*state)->dh_remote_ephemeral), dh_id);
}
if ((flags & NOISE_PAT_FLAG_REMOTE_HYBRID) != 0 && err == NOISE_ERROR_NONE) {
if (hybrid_id != NOISE_DH_NONE)
err = noise_dhstate_new_by_id(&((*state)->dh_remote_hybrid), hybrid_id);
else
err = NOISE_ERROR_NOT_APPLICABLE;
} else if (hybrid_id != NOISE_DH_NONE) {
err = NOISE_ERROR_NOT_APPLICABLE;
}
/* Set the roles for all DHState objects except those for hybrid
forward secrecy. Those roles are implicit in whether the "f"
or "g" token is used to refer to the value */
noise_dhstate_set_role((*state)->dh_local_ephemeral, local_dh_role);
noise_dhstate_set_role((*state)->dh_local_static, local_dh_role);
noise_dhstate_set_role((*state)->dh_remote_ephemeral, remote_dh_role);
noise_dhstate_set_role((*state)->dh_remote_static, remote_dh_role);
/* If the DH algorithm is ephemeral-only, then we need to apply some
* extra checks on the pattern: essentially, only "NN" is possible.
* This check isn't needed for the hybrid secrecy DH objects as they
* are by definition ephemeral-only */
if (err == NOISE_ERROR_NONE) {
if ((*state)->dh_local_static && (*state)->dh_local_static->ephemeral_only)
err = NOISE_ERROR_NOT_APPLICABLE;
if ((*state)->dh_remote_static && (*state)->dh_remote_static->ephemeral_only)
err = NOISE_ERROR_NOT_APPLICABLE;
if ((*state)->dh_local_ephemeral && (*state)->dh_local_ephemeral->ephemeral_only) {
if (!((*state)->dh_remote_ephemeral))
err = NOISE_ERROR_NOT_APPLICABLE;
} else if ((*state)->dh_remote_ephemeral && (*state)->dh_remote_ephemeral->ephemeral_only) {
err = NOISE_ERROR_NOT_APPLICABLE;
}
}
/* Bail out if we had an error trying to create the DHState objects */
if (err != NOISE_ERROR_NONE) {
noise_handshakestate_free(*state);
return err;
}
/* Ready to go */
return NOISE_ERROR_NONE;
}
/**
* \brief Creates a new HandshakeState object by protocol identifier.
*
* \param state Points to the variable where to store the pointer to
* the new HandshakeState object.
* \param protocol_id The protocol identifier as a set of algorithm identifiers.
* \param role The role for the new object, either NOISE_ROLE_INITIATOR or
* NOISE_ROLE_RESPONDER.
*
* \return NOISE_ERROR_NONE on success.
* \return NOISE_ERROR_INVALID_PARAM if either \a state or \a protocol_id
* is NULL, or \a role is not one of NOISE_ROLE_INITIATOR or
* NOISE_ROLE_RESPONDER.
* \return NOISE_ERROR_UNKNOWN_ID if the \a protocol_id is unknown.
* \return NOISE_ERROR_INVALID_LENGTH if the full name corresponding to
* \a protocol_id is too long.
* \return NOISE_ERROR_NOT_APPLICABLE if the combination of algorithm
* identifiers in \a protocol_id is not permitted.
* \return NOISE_ERROR_NO_MEMORY if there is insufficient memory to
* allocate the new HandshakeState object.
*
* \sa noise_handshakestate_free(), noise_handshakestate_new_by_name()
*/
int noise_handshakestate_new_by_id
(NoiseHandshakeState **state, const NoiseProtocolId *protocol_id, int role)
{
NoiseSymmetricState *symmetric;
int err;
/* Validate the parameters */
if (!state)
return NOISE_ERROR_INVALID_PARAM;
*state = 0;
if (!protocol_id)
return NOISE_ERROR_INVALID_PARAM;
if (role != NOISE_ROLE_INITIATOR && role != NOISE_ROLE_RESPONDER)
return NOISE_ERROR_INVALID_PARAM;
/* Create the SymmetricState object */
err = noise_symmetricstate_new_by_id(&symmetric, protocol_id);
if (err != NOISE_ERROR_NONE)
return err;
/* Create the HandshakeState object */
return noise_handshakestate_new(state, symmetric, role);
}
/**
* \brief Creates a new HandshakeState object by protocol name.
*
* \param state Points to the variable where to store the pointer to
* the new HandshakeState object.
* \param protocol_name The name of the Noise protocol to use. This string
* must be NUL-terminated.
* \param role The role for the new object, either NOISE_ROLE_INITIATOR or
* NOISE_ROLE_RESPONDER.
*
* \return NOISE_ERROR_NONE on success.
* \return NOISE_ERROR_INVALID_PARAM if either \a state or \a protocol_name
* is NULL, or \a role is not one of NOISE_ROLE_INITIATOR or
* NOISE_ROLE_RESPONDER.
* \return NOISE_ERROR_UNKNOWN_NAME if the \a protocol_name is unknown.
* \return NOISE_ERROR_NOT_APPLICABLE if the combination of algorithm
* identifiers in \a protocol_id is not permitted.
* \return NOISE_ERROR_NO_MEMORY if there is insufficient memory to
* allocate the new HandshakeState object.
*
* \sa noise_handshakestate_free(), noise_handshakestate_new_by_id()
*/
int noise_handshakestate_new_by_name
(NoiseHandshakeState **state, const char *protocol_name, int role)
{
NoiseSymmetricState *symmetric;
int err;
/* Validate the parameters */
if (!state)
return NOISE_ERROR_INVALID_PARAM;
*state = 0;
if (!protocol_name)
return NOISE_ERROR_INVALID_PARAM;
if (role != NOISE_ROLE_INITIATOR && role != NOISE_ROLE_RESPONDER)
return NOISE_ERROR_INVALID_PARAM;
/* Create the SymmetricState object */
err = noise_symmetricstate_new_by_name(&symmetric, protocol_name);
if (err != NOISE_ERROR_NONE)
return err;
/* Create the HandshakeState object */
return noise_handshakestate_new(state, symmetric, role);
}
/**
* \brief Frees a HandshakeState object after destroying all sensitive material.
*
* \param state The HandshakeState object to free.
*
* \return NOISE_ERROR_NONE on success.
* \return NOISE_ERROR_INVALID_PARAM if \a state is NULL.
*
* \sa noise_handshakestate_new_by_id(), noise_handshakestate_new_by_name()
*/
int noise_handshakestate_free(NoiseHandshakeState *state)
{
/* Bail out if no handshake state */
if (!state)
return NOISE_ERROR_INVALID_PARAM;
/* Free the sub objects that are hanging off the handshakestate */
if (state->symmetric)
noise_symmetricstate_free(state->symmetric);
if (state->dh_local_static)
noise_dhstate_free(state->dh_local_static);
if (state->dh_local_ephemeral)
noise_dhstate_free(state->dh_local_ephemeral);
if (state->dh_local_hybrid)
noise_dhstate_free(state->dh_local_hybrid);
if (state->dh_remote_static)
noise_dhstate_free(state->dh_remote_static);
if (state->dh_remote_ephemeral)
noise_dhstate_free(state->dh_remote_ephemeral);
if (state->dh_remote_hybrid)
noise_dhstate_free(state->dh_remote_hybrid);
if (state->dh_fixed_ephemeral)
noise_dhstate_free(state->dh_fixed_ephemeral);
if (state->dh_fixed_hybrid)
noise_dhstate_free(state->dh_fixed_hybrid);
noise_free(state->prologue, state->prologue_len);
/* Clean and free the memory for "state" */
noise_free(state, state->size);
return NOISE_ERROR_NONE;
}
/**
* \brief Gets the role that a HandshakeState object is playing.
*
* \param state The HandshakeState object.
*
* \return Returns one of NOISE_ROLE_INITIATOR or NOISE_ROLE_RESPONDER
* if \a state is non-NULL, or zero if \a state is NULL.
*/
int noise_handshakestate_get_role(const NoiseHandshakeState *state)
{
return state ? state->role : 0;
}
/**
* \brief Gets the protocol identifier associated with a HandshakeState object.
*
* \param state The HandshakeState object.
* \param id Return buffer for the protocol identifier, which consists of
* fields that identify the cipher algorithm, hash algorith, handshake
* pattern, etc.
*
* \return NOISE_ERROR_NONE on success.
* \return NOISE_ERROR_INVALID_PARAM if \a state or \a id is NULL.
*/
int noise_handshakestate_get_protocol_id
(const NoiseHandshakeState *state, NoiseProtocolId *id)
{
/* Validate the parameters */
if (!state || !id)
return NOISE_ERROR_INVALID_PARAM;
/* Copy the protocol identifiers to the caller's buffer */
*id = state->symmetric->id;
return NOISE_ERROR_NONE;
}
/**
* \brief Gets the DHState object that contains the local static keypair.
*
* \param state The HandshakeState object.
*
* \return Returns a pointer to the DHState object for the local static
* keypair, or NULL if the handshake does not require a local static keypair.
*
* The application uses the returned object to set the static keypair for
* the local end of the handshake if one is required.
*
* \sa noise_handshakestate_get_remote_public_key_dh()
*/
NoiseDHState *noise_handshakestate_get_local_keypair_dh
(const NoiseHandshakeState *state)
{
return state ? state->dh_local_static : 0;
}
/**
* \brief Gets the DHState object that contains the remote static public key.
*
* \param state The HandshakeState object.
*
* \return Returns a pointer to the DHState object for the remote static
* public key, or NULL if the handshake does not require a remote public key.
*
* The application uses the returned object to set the public key for
* the remote end of the handshake if the key must be provided prior to
* the handshake. The returned object can also be used to obtain the public
* key value that was transmitted by the remote party during the handshake.
*
* \sa noise_handshakestate_get_local_keypair_dh()
*/
NoiseDHState *noise_handshakestate_get_remote_public_key_dh
(const NoiseHandshakeState *state)
{
return state ? state->dh_remote_static : 0;
}
/**
* \brief Gets the DHState object that contains the local ephemeral keypair.
*
* \param state The HandshakeState object.
*
* \return Returns a pointer to the DHState object for the local ephemeral
* keypair, or NULL if the system is out of memory or \a state is NULL.
*
* \note This function is intended for testing only. It can be used to
* establish a fixed ephemeral key for test vectors. This function should
* not be used in real applications.
*
* \sa noise_handshakestate_get_local_keypair_dh()
*/
NoiseDHState *noise_handshakestate_get_fixed_ephemeral_dh
(NoiseHandshakeState *state)
{
if (!state || !state->dh_local_ephemeral)
return 0;
if (!state->dh_fixed_ephemeral) {
if (noise_dhstate_new_by_id
(&(state->dh_fixed_ephemeral), state->symmetric->id.dh_id)
!= NOISE_ERROR_NONE) {
return 0;
}
noise_dhstate_set_role
(state->dh_fixed_ephemeral,
noise_dhstate_get_role(state->dh_local_ephemeral));
}
return state->dh_fixed_ephemeral;
}
/**
* \brief Gets the DHState object that contains the local additional
* hybrid secrecy keypair.
*
* \param state The HandshakeState object.
*
* \return Returns a pointer to the DHState object for the local additional
* hybrid secrecy keypair, or NULL if the system is out of memory,
* \a state is NULL, or \a state does not support additional hybrid secrecy.
*
* \note This function is intended for testing only. It can be used to
* establish a fixed hybrid secrecy key for test vectors. This function
* should not be used in real applications.
*
* \sa noise_handshakestate_get_fixed_ephemeral_dh()
*/
NoiseDHState *noise_handshakestate_get_fixed_hybrid_dh
(NoiseHandshakeState *state)
{
if (!state || !state->dh_local_hybrid)
return 0;
if (!state->dh_fixed_hybrid) {
if (noise_dhstate_new_by_id
(&(state->dh_fixed_hybrid), state->symmetric->id.hybrid_id)
!= NOISE_ERROR_NONE) {
return 0;
}
/* Normally hybrid keys do not get a role until the "f" or "g"
tokens are encountered. However for fixed test vectors we
need to know the size of the private key ahead of time and
hence the role. Predict the future hybrid role from the
current ephemeral role. */
noise_dhstate_set_role
(state->dh_fixed_hybrid,
noise_dhstate_get_role(state->dh_local_ephemeral));
}
return state->dh_fixed_hybrid;
}
/**
* \brief Determine if a HandshakeState object requires a pre shared key.
*
* \param state The HandshakeState object.
*
* \return Returns 1 if \a state requires a pre shared key, zero if the
* pre shared key has already been supplied or it is not required.
*
* \sa noise_handshakestate_set_pre_shared_key(),
* noise_handshakestate_has_pre_shared_key()
*/
int noise_handshakestate_needs_pre_shared_key(const NoiseHandshakeState *state)
{
if (!state || state->pre_shared_key_len)
return 0;
else
return (state->requirements & NOISE_REQ_PSK) != 0;
}
/**
* \brief Determine if a HandshakeState object has already been configured
* with a pre shared key.
*
* \param state The HandshakeState object.
*
* \return Returns 1 if \a state has a pre shared key, zero if not.
*
* \sa noise_handshakestate_set_pre_shared_key(),
* noise_handshakestate_needs_pre_shared_key()
*/
int noise_handshakestate_has_pre_shared_key(const NoiseHandshakeState *state)
{
if (!state)
return 0;
else
return state->pre_shared_key_len != 0;
}
/**
* \brief Sets the pre shared key for a HandshakeState.
*
* \param state The HandshakeState object.
* \param key Points to the pre shared key.
* \param key_len The length of the \a key in bytes. This must be 32
* to comply with the requirements from the Noise protocol specification.
*
* \return NOISE_ERROR_NONE on success.
* \return NOISE_ERROR_INVALID_PARAM if \a state or \a key is NULL.
* \return NOISE_ERROR_INVALID_LENGTH if \a key_len is not 32.
* \return NOISE_ERROR_NOT_APPLICABLE if the protocol name does not
* begin with "NoisePSK".
* \return NOISE_ERROR_INVALID_STATE if this function is called after
* the protocol has already started.
*
* \sa noise_handshakestate_start(), noise_handshakestate_set_prologue(),
* noise_handshakestate_needs_pre_shared_key(),
* noise_handshakestate_has_pre_shared_key()
*/
int noise_handshakestate_set_pre_shared_key
(NoiseHandshakeState *state, const uint8_t *key, size_t key_len)
{
/* Validate the parameters and state */
if (!state || !key)
return NOISE_ERROR_INVALID_PARAM;
if (key_len != NOISE_PSK_LEN)
return NOISE_ERROR_INVALID_LENGTH;
if (state->symmetric->id.prefix_id != NOISE_PREFIX_PSK)
return NOISE_ERROR_NOT_APPLICABLE;
if (state->action != NOISE_ACTION_NONE)
return NOISE_ERROR_INVALID_STATE;
/* Record the pre-shared key for use in noise_handshakestate_start() */
memcpy(state->pre_shared_key, key, key_len);
state->pre_shared_key_len = key_len;
return NOISE_ERROR_NONE;
}
/**
* \brief Sets the prologue for a HandshakeState.
*
* \param state The HandshakeState object.
* \param prologue Points to the prologue value.
* \param prologue_len The length of the \a prologue value in bytes.
*
* \return NOISE_ERROR_NONE on success.
* \return NOISE_ERROR_INVALID_PARAM if \a state or \a prologue is NULL.
* \return NOISE_ERROR_INVALID_STATE if this function is called after
* the protocol has already started.
* \return NOISE_ERROR_NO_MEMORY if there is insufficient memory to
* save the \a prologue value.
*
* This function should be called immediately after
* noise_handshakestate_new_by_id() or noise_handshakestate_new_by_name()
* if there is a prologue for the session. If the function is not called,
* then the prologue will be assumed to be empty when the protocol starts.
*
* \sa noise_handshakestate_start(), noise_handshakestate_set_pre_shared_key()
*/
int noise_handshakestate_set_prologue
(NoiseHandshakeState *state, const void *prologue, size_t prologue_len)
{
/* Validate the parameters */
if (!state || !prologue)
return NOISE_ERROR_INVALID_PARAM;
if (state->action != NOISE_ACTION_NONE)
return NOISE_ERROR_INVALID_STATE;
/* Make a copy of the prologue for later */
if (state->prologue && state->prologue_len == prologue_len) {
memcpy(state->prologue, prologue, prologue_len);
} else {
noise_free(state->prologue, state->prologue_len);
if (prologue_len) {
state->prologue = (uint8_t *)malloc(prologue_len);
if (!(state->prologue)) {
state->prologue_len = 0;
return NOISE_ERROR_NO_MEMORY;
}
memcpy(state->prologue, prologue, prologue_len);
} else {
state->prologue = 0;
}
state->prologue_len = prologue_len;
}
return NOISE_ERROR_NONE;
}
/**
* \brief Determine if a HandshakeState still needs to be configured
* with a local keypair.
*
* \param state The HandshakeState object.
*
* \return Returns 1 if the \a state has not yet been configured with a
* local keypair, or 0 if the keypair has been provided or is not required
* at all. Also returns zero if \a state is NULL.
*
* The application configures the local keypair on the object returned by
* noise_handshakestate_get_local_keypair_dh().
*
* \sa noise_handshakestate_has_local_keypair(),
* noise_handshakestate_get_local_keypair_dh()
*/
int noise_handshakestate_needs_local_keypair(const NoiseHandshakeState *state)
{
if (!state)
return 0;
if ((state->requirements & NOISE_REQ_LOCAL_REQUIRED) == 0)
return 0;
return !noise_dhstate_has_keypair(state->dh_local_static);
}
/**
* \brief Determine if a HandshakeState has been configured with a
* local keypair.
*
* \param state The HandshakeState object.
*
* \return Returns 1 if the \a state has already been configured with a
* local keypair, or 0 if the keypair is yet to be provided. Also returns
* zero if \a state is NULL.
*
* \sa noise_handshakestate_needs_local_keypair(),
* noise_handshakestate_get_local_keypair_dh()
*/
int noise_handshakestate_has_local_keypair(const NoiseHandshakeState *state)
{
if (!state || !state->dh_local_static)
return 0;
return noise_dhstate_has_keypair(state->dh_local_static);
}
/**
* \brief Determine if a HandshakeState still needs to be configured
* with a remote public key before the protocol can start.
*
* \param state The HandshakeState object.
*
* \return Returns 1 if the \a state has not yet been configured with a
* required remote public key, or 0 if the key has been provided or is
* not required at all. Also returns zero if \a state is NULL.
*
* This function indicates that a remote public key must be supplied
* before the protocol starts. If it is possible for the remote public key
* to be provided by the remote party during the session, then the
* remote public key can be obtained at the end of the handshake using the
* noise_handshakestate_get_remote_public_key_dh() object.
*
* \sa noise_handshakestate_has_remote_public_key(),
* noise_handshakestate_get_remote_public_key_dh()
*/
int noise_handshakestate_needs_remote_public_key(const NoiseHandshakeState *state)
{
if (!state)
return 0;
if ((state->requirements & NOISE_REQ_REMOTE_REQUIRED) == 0)
return 0;
return !noise_dhstate_has_public_key(state->dh_remote_static);
}
/**
* \brief Determine if a HandshakeState has a remote public key.
*
* \param state The HandshakeState object.
*
* \return Returns 1 if the \a state has a remote public key, or 0 if the
* key is yet to be seen. Also returns zero if \a state is NULL.
*
* A remote public key may either be provided ahead of time on the
* noise_handshakestate_get_remote_public_key_dh() object, or it may be
* provided by the remote party during the handshake.
*
* \sa noise_handshakestate_needs_remote_public_key(),
* noise_handshakestate_set_remote_public_key()
*/
int noise_handshakestate_has_remote_public_key(const NoiseHandshakeState *state)
{
if (!state || !state->dh_remote_static)
return 0;
return noise_dhstate_has_public_key(state->dh_remote_static);
}
/**
* \brief Mixes a public key value into the handshake hash.
*
* \param state The HandshakeState object.
* \param dh The DHState for the key to mix in. Can be NULL.
*/
static void noise_handshakestate_mix_public_key
(NoiseHandshakeState *state, const NoiseDHState *dh)
{
if (noise_dhstate_has_public_key(dh)) {
noise_symmetricstate_mix_hash
(state->symmetric, dh->public_key, dh->public_key_len);
}
}
/**
* \brief Mixes a public key value into the chaining key.
*
* \param state The HandshakeState object.
* \param dh The DHState for the key to mix in. Can be NULL.
*/
static void noise_handshakestate_mix_chaining_key
(NoiseHandshakeState *state, const NoiseDHState *dh)
{
if (noise_dhstate_has_public_key(dh)) {
noise_symmetricstate_mix_key
(state->symmetric, dh->public_key, dh->public_key_len);
}
}
/**
* \brief Starts the handshake on a HandshakeState object.
*
* \param state The HandshakeState object.
*
* \return NOISE_ERROR_NONE on success.
* \return NOISE_ERROR_INVALID_PARAM if \a state is NULL.
* \return NOISE_ERROR_LOCAL_KEY_REQUIRED if a local keypair is required
* to start the protocol but one has not been provided yet.
* \return NOISE_ERROR_REMOTE_KEY_REQUIRED if a remote public key is required
* to start the protocol but one has not been provided yet.
* \return NOISE_ERROR_PSK_REQUIRED if a pre shared key is required
* to start the protocol but one has not been provided yet.
* \return NOISE_ERROR_INVALID_STATE if the protocol handshake
* has already started.
* \return NOISE_ERROR_NOT_APPLICABLE if an attempt was made to
* start a fallback handshake pattern without first calling
* noise_handshakestate_fallback() on a previous handshake.
*
* This function is called after all of the handshake parameters have been
* provided to the HandshakeState object. This function should be followed
* by calls to noise_handshake_write_message() or noise_handshake_read_message()
* to process the handshake messages. The noise_handshakestate_get_action()
* function indicates the action to take next.
*
* \sa noise_handshake_write_message(), noise_handshake_read_message(),
* noise_handshakestate_get_action(), noise_handshakestate_fallback()
*/
int noise_handshakestate_start(NoiseHandshakeState *state)
{
/* Validate the parameter */
if (!state)
return NOISE_ERROR_INVALID_PARAM;
if (state->action != NOISE_ACTION_NONE)
return NOISE_ERROR_INVALID_STATE;
if (state->symmetric->id.pattern_id == NOISE_PATTERN_XX_FALLBACK &&
(state->requirements & NOISE_REQ_FALLBACK_PREMSG) == 0)
return NOISE_ERROR_NOT_APPLICABLE;
/* Check that we have satisfied all of the pattern requirements */
if ((state->requirements & NOISE_REQ_LOCAL_REQUIRED) != 0 &&
!noise_dhstate_has_keypair(state->dh_local_static))
return NOISE_ERROR_LOCAL_KEY_REQUIRED;
if ((state->requirements & NOISE_REQ_REMOTE_REQUIRED) != 0 &&
!noise_dhstate_has_public_key(state->dh_remote_static))
return NOISE_ERROR_REMOTE_KEY_REQUIRED;
if ((state->requirements & NOISE_REQ_PSK) != 0 &&
state->pre_shared_key_len == 0)
return NOISE_ERROR_PSK_REQUIRED;
/* Hash the prologue value */
if (state->prologue_len) {
noise_symmetricstate_mix_hash
(state->symmetric, state->prologue, state->prologue_len);
} else {
/* No prologue, so hash an empty one */
noise_symmetricstate_mix_hash
(state->symmetric, state->pre_shared_key, 0);
}
/* Mix the pre shared key into the chaining key and handshake hash */
if (state->pre_shared_key_len) {
uint8_t temp[NOISE_MAX_HASHLEN];
NoiseHashState *hash = state->symmetric->hash;
noise_hashstate_hkdf
(hash, state->symmetric->ck, hash->hash_len,
state->pre_shared_key, state->pre_shared_key_len,
state->symmetric->ck, hash->hash_len, temp, hash->hash_len);
noise_symmetricstate_mix_hash(state->symmetric, temp, hash->hash_len);
noise_clean(temp, sizeof(temp));
}
/* Mix the pre-supplied public keys into the handshake hash */
if (state->role == NOISE_ROLE_INITIATOR) {
if (state->requirements & NOISE_REQ_LOCAL_PREMSG)
noise_handshakestate_mix_public_key(state, state->dh_local_static);
if (state->requirements & NOISE_REQ_FALLBACK_PREMSG) {
noise_handshakestate_mix_public_key(state, state->dh_remote_ephemeral);
if (state->dh_remote_hybrid) {
noise_handshakestate_mix_public_key
(state, state->dh_remote_hybrid);
}
if ((state->requirements & NOISE_REQ_PSK) != 0) {
noise_handshakestate_mix_chaining_key
(state, state->dh_remote_ephemeral);
}
}
if (state->requirements & NOISE_REQ_REMOTE_PREMSG)
noise_handshakestate_mix_public_key(state, state->dh_remote_static);
} else {
if (state->requirements & NOISE_REQ_REMOTE_PREMSG)
noise_handshakestate_mix_public_key(state, state->dh_remote_static);
if (state->requirements & NOISE_REQ_FALLBACK_PREMSG) {
noise_handshakestate_mix_public_key(state, state->dh_local_ephemeral);
if (state->dh_local_hybrid) {
noise_handshakestate_mix_public_key
(state, state->dh_local_hybrid);
}
if ((state->requirements & NOISE_REQ_PSK) != 0) {
noise_handshakestate_mix_chaining_key
(state, state->dh_local_ephemeral);
}
}
if (state->requirements & NOISE_REQ_LOCAL_PREMSG)
noise_handshakestate_mix_public_key(state, state->dh_local_static);
}
/* The handshake has now officially started */
if (state->role == NOISE_ROLE_INITIATOR)
state->action = NOISE_ACTION_WRITE_MESSAGE;
else
state->action = NOISE_ACTION_READ_MESSAGE;
return NOISE_ERROR_NONE;
}
/**
* \brief Falls back to the "XXfallback" handshake pattern.
*
* \param state The HandshakeState object.
*
* \return NOISE_ERROR_NONE on error.
* \return NOISE_ERROR_INVALID_PARAM if \a state is NULL.
* \return NOISE_ERROR_INVALID_STATE if the previous protocol has not
* been started or has not reached the fallback position yet.
* \return NOISE_ERROR_INVALID_LENGTH if the new protocol name is too long.
* \return NOISE_ERROR_NOT_APPLICABLE if the handshake pattern in the
* original protocol name is not compatible with "XXfallback".
*
* This function is intended used to help implement the "Noise Pipes" protocol.
* It resets a HandshakeState object with the original handshake pattern
* (usually "IK"), converting it into an object with the handshake pattern
* "XXfallback". Information from the previous session such as the local
* keypair, the initiator's ephemeral key, the prologue value, and the
* pre-shared key, are passed to the new session.
*
* Once the fallback has been initiated, the application can set
* new values for the handshake parameters if the values from the
* previous session do not apply. For example, the application may
* use a different prologue for the fallback than for the original
* session.
*
* After setting any new parameters, the application calls
* noise_handshakestate_start() again to restart the handshake
* from where it left off before the fallback.
*
* \note This function reverses the roles of initiator and responder.
*
* \sa noise_handshakestate_start(), noise_handshakestate_fallback_to()
*/
int noise_handshakestate_fallback(NoiseHandshakeState *state)
{
return noise_handshakestate_fallback_to(state, NOISE_PATTERN_XX_FALLBACK);
}
/**
* \brief Falls back to another handshake pattern.
*
* \param state The HandshakeState object.
* \param pattern_id The identifier for the pattern to fallback to;
* e.g. NOISE_PATTERN_XX_FALLBACK, NOISE_PATTERN_NX_FALLBACK, etc.
*
* \return NOISE_ERROR_NONE on error.
* \return NOISE_ERROR_INVALID_PARAM if \a state is NULL.
* \return NOISE_ERROR_INVALID_STATE if the previous protocol has not
* been started or has not reached the fallback position yet.
* \return NOISE_ERROR_INVALID_LENGTH if the new protocol name is too long.
* \return NOISE_ERROR_NOT_APPLICABLE if the handshake pattern in the
* original protocol name is not compatible with \a pattern_id.
* \return NOISE_ERROR_NOT_APPLICABLE if \a pattern_id does not
* identify a fallback pattern.
*
* This function is a generalization of the "Noise Pipes" protocol,
* allowing for other combinations of patterns to be used for the
* full handshake, abbreviated handshake, and fallback handshake.
* For example, "NX/NK/NXfallback", "XX/XK/XXfallback", etc.
*
* This function resets a HandshakeState object with the original
* handshake pattern, and converts it into an object with the new handshake
* \a pattern_id. Information from the previous session such as the local
* keypair, the initiator's ephemeral key, the prologue value, and the
* pre-shared key, are passed to the new session.
*
* Once the fallback has been initiated, the application can set
* new values for the handshake parameters if the values from the
* previous session do not apply. For example, the application may
* use a different prologue for the fallback than for the original
* session.
*
* After setting any new parameters, the application calls
* noise_handshakestate_start() again to restart the handshake
* from where it left off before the fallback.
*
* The new pattern may have greater key requirements than the original;
* for example changing from "NK" from "XXfallback" requires that the
* initiator's static public key be set. The application is responsible for
* setting any extra keys before calling noise_handshakestate_start().
*
* \note This function reverses the roles of initiator and responder.
*
* \sa noise_handshakestate_start(), noise_handshakestate_fallback()
*/
int noise_handshakestate_fallback_to(NoiseHandshakeState *state, int pattern_id)
{
char name[NOISE_MAX_PROTOCOL_NAME];
size_t hash_len;
size_t name_len;
NoiseProtocolId id;
const uint8_t *pattern;
NoisePatternFlags_t flags;
int err;
/* Validate the parameter */
if (!state)
return NOISE_ERROR_INVALID_PARAM;
/* The original pattern must end in "K" for fallback to be possible */
if (state->symmetric->id.pattern_id < NOISE_PATTERN_NN ||
(state->requirements & NOISE_REQ_FALLBACK_POSSIBLE) == 0)
return NOISE_ERROR_NOT_APPLICABLE;
/* Check that "pattern_id" supports fallback */
pattern = noise_pattern_lookup(pattern_id);
if (!pattern)
return NOISE_ERROR_NOT_APPLICABLE;
flags = ((NoisePatternFlags_t)(pattern[0])) |
(((NoisePatternFlags_t)(pattern[1])) << 8);
if ((flags & NOISE_PAT_FLAG_REMOTE_EPHEM_REQ) == 0)
return NOISE_ERROR_NOT_APPLICABLE;