Noise-C
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
internal.h
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 #ifndef NOISE_INTERNAL_H
24 #define NOISE_INTERNAL_H
25 
26 #include <noise/protocol.h>
27 #if defined(__WIN32__) || defined(WIN32)
28 #include <malloc.h>
29 #else
30 #include <alloca.h>
31 #endif
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
48 #define NOISE_MAX_HASHLEN 64
49 
53 #define NOISE_PSK_LEN 32
54 
59 {
61  size_t size;
62 
64  int cipher_id;
65 
67  uint8_t has_key;
68 
70  uint8_t key_len;
71 
73  uint8_t mac_len;
74 
76  uint64_t n;
77 
84  NoiseCipherState *(*create)(void);
85 
95  void (*init_key)(NoiseCipherState *state, const uint8_t *key);
96 
113  int (*encrypt)(NoiseCipherState *state, const uint8_t *ad, size_t ad_len,
114  uint8_t *data, size_t len);
115 
130  int (*decrypt)(NoiseCipherState *state, const uint8_t *ad, size_t ad_len,
131  uint8_t *data, size_t len);
132 
145  void (*destroy)(NoiseCipherState *state);
146 };
147 
152 {
154  size_t size;
155 
157  int hash_id;
158 
160  uint16_t hash_len;
161 
163  uint16_t block_len;
164 
170  void (*reset)(NoiseHashState *state);
171 
179  void (*update)(NoiseHashState *state, const uint8_t *data, size_t len);
180 
188  void (*finalize)(NoiseHashState *state, uint8_t *hash);
189 
202  void (*destroy)(NoiseHashState *state);
203 };
204 
205 /* States for public key algorithms, either DHState or SignState */
206 #define NOISE_KEY_TYPE_NO_KEY 0
207 #define NOISE_KEY_TYPE_KEYPAIR 1
208 #define NOISE_KEY_TYPE_PUBLIC 2
213 struct NoiseDHState_s
214 {
216  size_t size;
217 
219  short dh_id;
220 
222  short role;
223 
225  uint8_t key_type;
226 
228  uint8_t ephemeral_only : 1;
229 
231  uint8_t nulls_allowed : 1;
232 
234  uint16_t private_key_len;
235 
237  uint16_t public_key_len;
238 
240  uint16_t shared_key_len;
241 
243  uint8_t *private_key;
244 
246  uint8_t *public_key;
247 
257  int (*generate_keypair)(NoiseDHState *state, const NoiseDHState *other);
258 
272  int (*set_keypair)
273  (NoiseDHState *state, const uint8_t *private_key,
274  const uint8_t *public_key);
275 
286  int (*set_keypair_private)
287  (NoiseDHState *state, const uint8_t *private_key);
288 
299  int (*validate_public_key)
300  (const NoiseDHState *state, const uint8_t *public_key);
301 
310  int (*copy)(NoiseDHState *state, const NoiseDHState *from,
311  const NoiseDHState *other);
312 
329  int (*calculate)
330  (const NoiseDHState *private_key_state,
331  const NoiseDHState *public_key_state,
332  uint8_t *shared_key);
333 
342  void (*change_role)(NoiseDHState *state);
343 
356  void (*destroy)(NoiseDHState *state);
357 };
358 
363 {
365  size_t size;
366 
368  int sign_id;
369 
371  uint16_t key_type;
372 
374  uint16_t private_key_len;
375 
377  uint16_t public_key_len;
378 
380  uint16_t signature_len;
381 
383  uint8_t *private_key;
384 
386  uint8_t *public_key;
387 
394 
408  int (*validate_keypair)
409  (const NoiseSignState *state, const uint8_t *private_key,
410  const uint8_t *public_key);
411 
425  int (*derive_public_key)
426  (const NoiseSignState *state, const uint8_t *private_key,
427  uint8_t *public_key);
428 
439  int (*validate_public_key)
440  (const NoiseSignState *state, const uint8_t *public_key);
441 
459  int (*sign)
460  (const NoiseSignState *state, const uint8_t *message,
461  size_t message_len, uint8_t *signature);
462 
478  int (*verify)
479  (const NoiseSignState *state, const uint8_t *message,
480  size_t message_len, const uint8_t *signature);
481 
494  void (*destroy)(NoiseSignState *state);
495 };
496 
501 {
503  size_t size;
504 
507 
517 
520 
523 
526 };
527 
532 {
534  size_t size;
535 
537  int role;
538 
541 
543  int action;
544 
546  const uint8_t *tokens;
547 
550 
553 
556 
559 
562 
565 
568 
571 
574 
577 
580 
582  uint8_t *prologue;
583 
585  size_t prologue_len;
586 };
587 
588 /* Handshake message pattern tokens (must be single-byte values) */
589 #define NOISE_TOKEN_END 0
590 #define NOISE_TOKEN_S 1
591 #define NOISE_TOKEN_E 2
592 #define NOISE_TOKEN_EE 3
593 #define NOISE_TOKEN_ES 4
594 #define NOISE_TOKEN_SE 5
595 #define NOISE_TOKEN_SS 6
596 #define NOISE_TOKEN_F 7
597 #define NOISE_TOKEN_FF 8
598 #define NOISE_TOKEN_FLIP_DIR 255
601 #define NOISE_PAT_FLAG_LOCAL_STATIC (1 << 0)
602 
603 #define NOISE_PAT_FLAG_LOCAL_EPHEMERAL (1 << 1)
604 
608 #define NOISE_PAT_FLAG_LOCAL_REQUIRED (1 << 2)
609 
611 #define NOISE_PAT_FLAG_LOCAL_EPHEM_REQ (1 << 3)
612 
613 #define NOISE_PAT_FLAG_LOCAL_HYBRID (1 << 4)
614 
616 #define NOISE_PAT_FLAG_LOCAL_HYBRID_REQ (1 << 5)
617 
619 #define NOISE_PAT_FLAG_REMOTE_STATIC (1 << 8)
620 
621 #define NOISE_PAT_FLAG_REMOTE_EPHEMERAL (1 << 9)
622 
626 #define NOISE_PAT_FLAG_REMOTE_REQUIRED (1 << 10)
627 
629 #define NOISE_PAT_FLAG_REMOTE_EPHEM_REQ (1 << 11)
630 
631 #define NOISE_PAT_FLAG_REMOTE_HYBRID (1 << 12)
632 
634 #define NOISE_PAT_FLAG_REMOTE_HYBRID_REQ (1 << 13)
635 
637 #define NOISE_REQ_LOCAL_REQUIRED (1 << 0)
638 
639 #define NOISE_REQ_REMOTE_REQUIRED (1 << 1)
640 
641 #define NOISE_REQ_PSK (1 << 2)
642 
643 #define NOISE_REQ_FALLBACK_PREMSG (1 << 3)
644 
645 #define NOISE_REQ_LOCAL_PREMSG (1 << 4)
646 
647 #define NOISE_REQ_REMOTE_PREMSG (1 << 5)
648 
649 #define NOISE_REQ_FALLBACK_POSSIBLE (1 << 6)
650 
651 void noise_rand_bytes(void *bytes, size_t size);
652 
655 NoiseCipherState *noise_chachapoly_new(void);
657 
658 NoiseHashState *noise_blake2s_new(void);
659 NoiseHashState *noise_blake2b_new(void);
660 NoiseHashState *noise_sha256_new(void);
661 NoiseHashState *noise_sha512_new(void);
662 
663 NoiseDHState *noise_curve25519_new(void);
664 NoiseDHState *noise_curve448_new(void);
665 NoiseDHState *noise_newhope_new(void);
666 
667 NoiseSignState *noise_ed25519_new(void);
668 
669 typedef uint16_t NoisePatternFlags_t;
670 
673 const uint8_t *noise_pattern_lookup(int id);
674 NoisePatternFlags_t noise_pattern_reverse_flags(NoisePatternFlags_t flags);
675 
676 #ifdef __cplusplus
677 };
678 #endif
679 
680 #endif
#define NOISE_PSK_LEN
Standard length for pre-shared keys.
Definition: internal.h:53
Noise protocol name broken out into separate identifier fields.
Definition: names.h:33
int(* decrypt)(NoiseCipherState *state, const uint8_t *ad, size_t ad_len, uint8_t *data, size_t len)
Decrypts data with this CipherState.
Definition: internal.h:130
void(* reset)(NoiseHashState *state)
Resets the HashState for a new hashing session.
Definition: internal.h:170
uint8_t mac_len
Length of the MAC for this cipher in bytes.
Definition: internal.h:73
int(* encrypt)(NoiseCipherState *state, const uint8_t *ad, size_t ad_len, uint8_t *data, size_t len)
Encrypts data with this CipherState.
Definition: internal.h:113
int(* validate_keypair)(const NoiseSignState *state, const uint8_t *private_key, const uint8_t *public_key)
Validates a keypair.
Definition: internal.h:409
uint16_t hash_len
Length of the output from this hash algorithm.
Definition: internal.h:160
uint16_t block_len
Length of the underlying block for this hash algorithm.
Definition: internal.h:163
Internal structure of the NoiseSignState type.
Definition: internal.h:362
uint8_t ck[NOISE_MAX_HASHLEN]
Current value of the chaining key for the handshake.
Definition: internal.h:522
NoiseHashState * hash
Points to the HashState object for this SymmetricState.
Definition: internal.h:519
uint8_t * public_key
Points to the public key in the subclass state.
Definition: internal.h:386
uint8_t * public_key
Points to the public key in the subclass state.
Definition: internal.h:246
Internal structure of the NoiseHashState type.
Definition: internal.h:151
NoiseDHState * dh_fixed_ephemeral
Points to the object for the fixed ephemeral test key.
Definition: internal.h:570
NoiseCipherState * noise_aesgcm_new(void)
Creates a new AES-GCM CipherState object.
Definition: internal.c:40
NoiseProtocolId id
Algorithm identifiers for the components of the protocol.
Definition: internal.h:506
size_t size
Total size of the structure.
Definition: internal.h:503
const uint8_t * tokens
Points to the next message pattern tokens to be processed.
Definition: internal.h:546
int(* validate_public_key)(const NoiseSignState *state, const uint8_t *public_key)
Validates a public key.
Definition: internal.h:440
uint8_t has_key
Non-zero if the key has been set on this cipher.
Definition: internal.h:67
size_t prologue_len
Length of the prologue value in bytes.
Definition: internal.h:585
size_t size
Total size of the structure, including DH key storage.
Definition: internal.h:534
NoiseDHState * dh_remote_ephemeral
Points to the DHState object for remote ephemeral key.
Definition: internal.h:564
NoiseDHState * dh_local_ephemeral
Points to the DHState object for local ephemeral key.
Definition: internal.h:555
int(* verify)(const NoiseSignState *state, const uint8_t *message, size_t message_len, const uint8_t *signature)
Verifies a digital signature on a message.
Definition: internal.h:479
Main header file to include the Noise protocol library definitions.
uint8_t h[NOISE_MAX_HASHLEN]
Current value of the handshake hash.
Definition: internal.h:525
#define NOISE_MAX_HASHLEN
Maximum hash length over all supported hash algorithms.
Definition: internal.h:48
void(* destroy)(NoiseHashState *state)
Destroys this HashState prior to the memory being freed.
Definition: internal.h:202
NoiseSymmetricState * symmetric
Points to the SymmetricState object for this HandshakeState.
Definition: internal.h:549
uint16_t shared_key_len
Length of the shared key for this algorithm in bytes.
Definition: internal.h:240
uint8_t pre_shared_key[NOISE_PSK_LEN]
Pre-shared key value.
Definition: internal.h:576
Internal structure of the NoiseDHState type.
Definition: internal.h:213
NoiseDHState * dh_remote_hybrid
Points to the DHState object for remote hybrid forward secrecy key.
Definition: internal.h:567
void(* finalize)(NoiseHashState *state, uint8_t *hash)
Finalizes the HashState and returns the hash value.
Definition: internal.h:188
uint8_t key_len
Length of the key for this cipher in bytes.
Definition: internal.h:70
int action
Next action to be taken by the application.
Definition: internal.h:543
uint64_t n
The nonce value for the next packet.
Definition: internal.h:76
int(* sign)(const NoiseSignState *state, const uint8_t *message, size_t message_len, uint8_t *signature)
Creates a signature.
Definition: internal.h:460
size_t size
Total size of the structure including subclass state.
Definition: internal.h:216
uint8_t key_type
The type of key stored within this DHState object.
Definition: internal.h:225
int cipher_id
Algorithm identifier for the cipher.
Definition: internal.h:64
NoiseCipherState * cipher
Points to the CipherState object for this SymmetricState.
Definition: internal.h:516
uint16_t key_type
The type of key stored within this SignState object.
Definition: internal.h:371
void(* update)(NoiseHashState *state, const uint8_t *data, size_t len)
Updates the HashState with more input data.
Definition: internal.h:179
int role
The role of this object, initiator or responder.
Definition: internal.h:537
void(* destroy)(NoiseSignState *state)
Destroys this SignState prior to the memory being freed.
Definition: internal.h:494
Internal structure of the NoiseSymmetricState type.
Definition: internal.h:500
int requirements
Requirements that are yet to be satisfied.
Definition: internal.h:540
uint8_t * prologue
Points to the prologue value.
Definition: internal.h:582
size_t size
Total size of the structure including subclass state.
Definition: internal.h:61
size_t pre_shared_key_len
Length of the pre-shared key value: zero or NOISE_PSK_LEN only.
Definition: internal.h:579
Internal structure of the NoiseCipherState type.
Definition: internal.h:58
void(* destroy)(NoiseCipherState *state)
Destroys this CipherState prior to the memory being freed.
Definition: internal.h:145
uint16_t private_key_len
Length of the private key for this algorithm in bytes.
Definition: internal.h:374
NoisePatternFlags_t noise_pattern_reverse_flags(NoisePatternFlags_t flags)
Reverses the local and remote flags for a pattern.
Definition: patterns.c:1306
uint8_t * private_key
Points to the private key in the subclass state.
Definition: internal.h:383
const uint8_t * noise_pattern_lookup(int id)
Looks up a specific handshake pattern.
Definition: patterns.c:1253
uint16_t public_key_len
Length of the public key for this algorithm in bytes.
Definition: internal.h:377
void(* init_key)(NoiseCipherState *state, const uint8_t *key)
Sets the key for this CipherState.
Definition: internal.h:95
uint16_t private_key_len
Length of the private key for this algorithm in bytes.
Definition: internal.h:234
int(* derive_public_key)(const NoiseSignState *state, const uint8_t *private_key, uint8_t *public_key)
Derives a public key from a private key.
Definition: internal.h:426
int hash_id
Algorithm identifier for the hash.
Definition: internal.h:157
uint16_t signature_len
Length of the signature for this algorithm in bytes.
Definition: internal.h:380
NoiseDHState * dh_local_hybrid
Points to the DHState object for local hybrid forward secrecy key.
Definition: internal.h:558
short dh_id
Algorithm identifier for the Diffie-Hellman operation.
Definition: internal.h:219
int sign_id
Algorithm identifier for the digital signature operation.
Definition: internal.h:368
size_t size
Total size of the structure including subclass state.
Definition: internal.h:365
Internal structure of the NoiseHandshakeState type.
Definition: internal.h:531
void(* generate_keypair)(NoiseSignState *state)
Generates a new key pair for this digital signature algorithm.
Definition: internal.h:393
NoiseDHState * dh_remote_static
Points to the DHState object for remote static key.
Definition: internal.h:561
uint8_t * private_key
Points to the private key in the subclass state.
Definition: internal.h:243
short role
The role; either initiator or responder or zero.
Definition: internal.h:222
void noise_rand_bytes(void *bytes, size_t size)
Gets cryptographically-strong random bytes from the operating system.
Definition: rand_os.c:68
NoiseDHState * dh_local_static
Points to the DHState object for local static key.
Definition: internal.h:552
size_t size
Total size of the structure including subclass state.
Definition: internal.h:154
NoiseDHState * dh_fixed_hybrid
Points to the object for the fixed hybrid forward secrecy test key.
Definition: internal.h:573
uint16_t public_key_len
Length of the public key for this algorithm in bytes.
Definition: internal.h:237