Noise-C
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
signstate.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 #include <string.h>
25 
68 {
69  /* The "state" argument must be non-NULL */
70  if (!state)
72 
73  /* Create the SignState object for the "id" */
74  *state = 0;
75  switch (id) {
76  case NOISE_SIGN_ED25519:
77  *state = noise_ed25519_new();
78  break;
79 
80  default:
82  }
83 
84  /* Bail out if insufficient memory */
85  if (!(*state))
86  return NOISE_ERROR_NO_MEMORY;
87 
88  /* Ready to go */
89  return NOISE_ERROR_NONE;
90 }
91 
108 int noise_signstate_new_by_name(NoiseSignState **state, const char *name)
109 {
110  int id;
111 
112  /* The "state" and "name" arguments must be non-NULL */
113  if (!state)
115  *state = 0;
116  if (!name)
118 
119  /* Map the name and create the corresponding object */
120  id = noise_name_to_id(NOISE_SIGN_CATEGORY, name, strlen(name));
121  if (id)
122  return noise_signstate_new_by_id(state, id);
123 
124  /* We don't know what this is */
126 }
127 
139 {
140  /* Bail out if no sign state */
141  if (!state)
143 
144  /* Call the backend-specific destroy function if necessary */
145  if (state->destroy)
146  (*(state->destroy))(state);
147 
148  /* Clean and free the memory */
149  noise_free(state, state->size);
150  return NOISE_ERROR_NONE;
151 }
152 
161 {
162  return state ? state->sign_id : NOISE_SIGN_NONE;
163 }
164 
176 {
177  return state ? state->public_key_len : 0;
178 }
179 
191 {
192  return state ? state->private_key_len : 0;
193 }
194 
206 {
207  return state ? state->signature_len : 0;
208 }
209 
223 {
224  if (state)
225  return state->key_type == NOISE_KEY_TYPE_KEYPAIR;
226  else
227  return 0;
228 }
229 
243 {
244  if (state)
245  return state->key_type != NOISE_KEY_TYPE_NO_KEY;
246  else
247  return 0;
248 }
249 
265 {
266  /* Validate the parameter */
267  if (!state)
269 
270  /* Generate the new keypair */
271  (*(state->generate_keypair))(state);
273  return NOISE_ERROR_NONE;
274 }
275 
301  (NoiseSignState *state, const uint8_t *private_key, size_t private_key_len,
302  const uint8_t *public_key, size_t public_key_len)
303 {
304  int err;
305 
306  /* Validate the parameters */
307  if (!state || !private_key || !public_key)
309  if (private_key_len != state->private_key_len)
311  if (public_key_len != state->public_key_len)
313 
314  /* Validate the keypair */
315  err = (*(state->validate_keypair))(state, private_key, public_key);
316  if (err != NOISE_ERROR_NONE)
317  return err;
318 
319  /* Copy the key into place */
320  memcpy(state->private_key, private_key, state->private_key_len);
321  memcpy(state->public_key, public_key, state->public_key_len);
323  return NOISE_ERROR_NONE;
324 }
325 
352  (NoiseSignState *state, const uint8_t *private_key, size_t private_key_len)
353 {
354  int err;
355 
356  /* Validate the parameters */
357  if (!state || !private_key)
359  if (private_key_len != state->private_key_len)
361 
362  /* Derive the public key from the private key */
363  err = (*(state->derive_public_key))
364  (state, private_key, state->public_key);
365  if (err != NOISE_ERROR_NONE) {
367  return err;
368  }
369 
370  /* Copy the private key into place */
371  memcpy(state->private_key, private_key, state->private_key_len);
373  return NOISE_ERROR_NONE;
374 }
375 
395  (const NoiseSignState *state, uint8_t *private_key, size_t private_key_len,
396  uint8_t *public_key, size_t public_key_len)
397 {
398  /* Validate the parameters */
399  if (!state || !private_key || !public_key)
401  if (private_key_len != state->private_key_len)
403  if (public_key_len != state->public_key_len)
405 
406  /* Is this actually a keypair? */
407  if (state->key_type != NOISE_KEY_TYPE_KEYPAIR) {
408  memset(private_key, 0, private_key_len);
409  memset(public_key, 0, public_key_len);
411  }
412 
413  /* Copy the keypair out */
414  memcpy(private_key, state->private_key, private_key_len);
415  memcpy(public_key, state->public_key, public_key_len);
416  return NOISE_ERROR_NONE;
417 }
418 
443  (NoiseSignState *state, const uint8_t *public_key, size_t public_key_len)
444 {
445  int err;
446 
447  /* Validate the parameters */
448  if (!state || !public_key)
450  if (public_key_len != state->public_key_len)
452 
453  /* Validate the public key with the back end */
454  err = (*(state->validate_public_key))(state, public_key);
455  if (err != NOISE_ERROR_NONE)
456  return err;
457 
458  /* Copy the public key into place and clear the private key */
459  memcpy(state->public_key, public_key, public_key_len);
460  memset(state->private_key, 0, state->private_key_len);
462  return NOISE_ERROR_NONE;
463 }
464 
482  (const NoiseSignState *state, uint8_t *public_key, size_t public_key_len)
483 {
484  /* Validate the parameters */
485  if (!state || !public_key)
487  if (public_key_len != state->public_key_len)
489  if (state->key_type == NOISE_KEY_TYPE_NO_KEY)
491 
492  /* Copy the public key out */
493  memcpy(public_key, state->public_key, public_key_len);
494  return NOISE_ERROR_NONE;
495 }
496 
508 {
509  /* Validate the parameter */
510  if (!state)
512 
513  /* Clear the key to all-zeroes */
514  memset(state->public_key, 0, state->public_key_len);
515  memset(state->private_key, 0, state->private_key_len);
516 
517  /* There is no key in the object now */
519  return NOISE_ERROR_NONE;
520 }
521 
545  (const NoiseSignState *state, const uint8_t *message, size_t message_len,
546  uint8_t *signature, size_t signature_len)
547 {
548  /* Validate the parameters */
549  if (!state || !message || !signature)
551  if (signature_len != state->signature_len)
553  if (state->key_type != NOISE_KEY_TYPE_KEYPAIR)
555 
556  /* Create the digial signature */
557  return (*(state->sign))(state, message, message_len, signature);
558 }
559 
583  (const NoiseSignState *state, const uint8_t *message, size_t message_len,
584  const uint8_t *signature, size_t signature_len)
585 {
586  /* Validate the parameters */
587  if (!state || !message || !signature)
589  if (signature_len != state->signature_len)
591  if (state->key_type == NOISE_KEY_TYPE_NO_KEY)
593 
594  /* Verify the digial signature */
595  return (*(state->verify))(state, message, message_len, signature);
596 }
597 
610 {
611  /* Validate the parameters */
612  if (!state || !from)
614  if (state->sign_id != from->sign_id)
616 
617  /* Copy the key information across */
618  if (state != from) {
619  state->key_type = from->key_type;
620  memcpy(state->private_key, from->private_key, from->private_key_len);
621  memcpy(state->public_key, from->public_key, from->public_key_len);
622  }
623  return NOISE_ERROR_NONE;
624 }
625 
652  (const NoiseSignState *state, int fingerprint_type,
653  char *buffer, size_t len)
654 {
655  /* Validate the parameters */
656  if (!buffer)
658  if (!len)
660  *buffer = '\0'; /* In case we bail out with an error later */
661  if (!state)
663  if (state->key_type == NOISE_KEY_TYPE_NO_KEY)
665 
666  /* Format the fingerprint */
668  (fingerprint_type, buffer, len,
669  state->public_key, state->public_key_len);
670 }
671 
678 {
679  return 32;
680 }
681 
688 {
689  return 64;
690 }
691 
int noise_signstate_get_public_key(const NoiseSignState *state, uint8_t *public_key, size_t public_key_len)
Gets the public key value from a SignState object.
Definition: signstate.c:482
#define NOISE_ERROR_UNKNOWN_ID
Algorithm identifier is unknown.
int(* validate_keypair)(const NoiseSignState *state, const uint8_t *private_key, const uint8_t *public_key)
Validates a keypair.
Definition: internal.h:409
int noise_signstate_set_keypair_private(NoiseSignState *state, const uint8_t *private_key, size_t private_key_len)
Sets the keypair within a SignState object based on a private key only.
Definition: signstate.c:352
#define NOISE_ERROR_INVALID_STATE
Operation cannot be performed in the current state.
Internal structure of the NoiseSignState type.
Definition: internal.h:362
#define NOISE_ERROR_INVALID_PARAM
Invalid parameter to function; e.g. a NULL value.
uint8_t * public_key
Points to the public key in the subclass state.
Definition: internal.h:386
int noise_signstate_new_by_name(NoiseSignState **state, const char *name)
Creates a new SignState object by its algorithm name.
Definition: signstate.c:108
#define NOISE_SIGN_CATEGORY
Category for signature algorithms.
#define NOISE_ERROR_NONE
Success, no error.
#define NOISE_SIGN_ED25519
Signature algorithm identifier for "Ed25519".
#define NOISE_KEY_TYPE_NO_KEY
Definition: internal.h:206
int(* validate_public_key)(const NoiseSignState *state, const uint8_t *public_key)
Validates a public key.
Definition: internal.h:440
size_t noise_signstate_get_private_key_length(const NoiseSignState *state)
Gets the length of the private key for a SignState object.
Definition: signstate.c:190
int noise_signstate_get_max_key_length(void)
Gets the maximum length of signing keys for the supported algorithms.
Definition: signstate.c:677
size_t noise_signstate_get_public_key_length(const NoiseSignState *state)
Gets the length of the public key for a SignState object.
Definition: signstate.c:175
int noise_signstate_free(NoiseSignState *state)
Frees a SignState object after destroying all sensitive material.
Definition: signstate.c:138
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
#define NOISE_SIGN_NONE
Signature algorithm identifier that indicates "no algorithm".
int noise_format_fingerprint(int fingerprint_type, char *buffer, size_t len, const uint8_t *public_key, size_t public_key_len)
Formats the fingerprint for a raw public key value.
Definition: util.c:246
int noise_signstate_get_sign_id(const NoiseSignState *state)
Gets the algorithm identifier for a SignState object.
Definition: signstate.c:160
#define NOISE_KEY_TYPE_KEYPAIR
Definition: internal.h:207
#define NOISE_ERROR_INVALID_PUBLIC_KEY
Invalid public key value.
#define NOISE_ERROR_NO_MEMORY
Insufficient memory to complete the operation.
int noise_signstate_has_public_key(const NoiseSignState *state)
Determine if a SignState object contains a public key.
Definition: signstate.c:242
#define NOISE_ERROR_UNKNOWN_NAME
Algorithm name is unknown.
int(* sign)(const NoiseSignState *state, const uint8_t *message, size_t message_len, uint8_t *signature)
Creates a signature.
Definition: internal.h:460
#define NOISE_ERROR_INVALID_LENGTH
Invalid length specified for a key, packet, etc.
int noise_signstate_get_max_signature_length(void)
Gets the maximum length of signatures for the supported algorithms.
Definition: signstate.c:687
int noise_signstate_format_fingerprint(const NoiseSignState *state, int fingerprint_type, char *buffer, size_t len)
Formats the public key fingerprint for the key within a SignState.
Definition: signstate.c:652
uint16_t key_type
The type of key stored within this SignState object.
Definition: internal.h:371
#define NOISE_ERROR_INVALID_PRIVATE_KEY
Invalid private key value.
void(* destroy)(NoiseSignState *state)
Destroys this SignState prior to the memory being freed.
Definition: internal.h:494
int noise_name_to_id(int category, const char *name, size_t name_len)
Maps an algorithm name to the corresponding identifier.
Definition: names.c:146
size_t noise_signstate_get_signature_length(const NoiseSignState *state)
Gets the length of the signature for a SignState object.
Definition: signstate.c:205
#define NOISE_ERROR_NOT_APPLICABLE
An option was supplied that was not applicable to the selected protocol.
Internal definitions for the library.
int noise_signstate_new_by_id(NoiseSignState **state, int id)
Creates a new SignState object by its algorithm identifier.
Definition: signstate.c:67
void noise_free(void *ptr, size_t size)
Destroys the contents of a block of memory and free it.
Definition: util.c:152
uint16_t private_key_len
Length of the private key for this algorithm in bytes.
Definition: internal.h:374
uint8_t * private_key
Points to the private key in the subclass state.
Definition: internal.h:383
#define NOISE_KEY_TYPE_PUBLIC
Definition: internal.h:208
uint16_t public_key_len
Length of the public key for this algorithm in bytes.
Definition: internal.h:377
int noise_signstate_verify(const NoiseSignState *state, const uint8_t *message, size_t message_len, const uint8_t *signature, size_t signature_len)
Verifies a digital signature on a message.
Definition: signstate.c:583
int noise_signstate_set_keypair(NoiseSignState *state, const uint8_t *private_key, size_t private_key_len, const uint8_t *public_key, size_t public_key_len)
Sets the keypair within a SignState object.
Definition: signstate.c:301
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 noise_signstate_generate_keypair(NoiseSignState *state)
Generates a new key pair within a SignState object.
Definition: signstate.c:264
uint16_t signature_len
Length of the signature for this algorithm in bytes.
Definition: internal.h:380
int noise_signstate_clear_key(NoiseSignState *state)
Clears the key in a SignState object.
Definition: signstate.c:507
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
int noise_signstate_sign(const NoiseSignState *state, const uint8_t *message, size_t message_len, uint8_t *signature, size_t signature_len)
Signs a message to create a digital signature.
Definition: signstate.c:545
void(* generate_keypair)(NoiseSignState *state)
Generates a new key pair for this digital signature algorithm.
Definition: internal.h:393
int noise_signstate_has_keypair(const NoiseSignState *state)
Determine if a SignState object contains a keypair.
Definition: signstate.c:222
int noise_signstate_copy(NoiseSignState *state, const NoiseSignState *from)
Copies the keys from one SignState object to another.
Definition: signstate.c:609
int noise_signstate_get_keypair(const NoiseSignState *state, uint8_t *private_key, size_t private_key_len, uint8_t *public_key, size_t public_key_len)
Gets the keypair from within a SignState object.
Definition: signstate.c:395
int noise_signstate_set_public_key(NoiseSignState *state, const uint8_t *public_key, size_t public_key_len)
Sets the public key in a SignState object.
Definition: signstate.c:443