ssh_sign_misc.c
Go to the documentation of this file.
1 /**
2  * @file ssh_sign_misc.c
3  * @brief Helper functions for signature generation and verification
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSH Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SSH_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "ssh/ssh_sign_misc.h"
37 #include "ssh/ssh_misc.h"
38 #include "debug.h"
39 
40 //Check SSH stack configuration
41 #if (SSH_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Format an ECDSA signature
46  * @param[in] signature ECDSA signature
47  * @param[out] p Output stream where to write the ECDSA signature
48  * @param[out] written Total number of bytes that have been written
49  * @return Error code
50  **/
51 
53  uint8_t *p, size_t *written)
54 {
55 #if (SSH_ECDSA_SIGN_SUPPORT == ENABLED)
56  error_t error;
57  size_t rLen;
58  size_t sLen;
59 
60  //Encode integer R
61  error = sshConvertArrayToMpint(signature->r.value, signature->r.length,
62  p + 4, &rLen);
63 
64  //Check status code
65  if(!error)
66  {
67  //Encode integer S
68  error = sshConvertArrayToMpint(signature->s.value, signature->s.length,
69  p + rLen + 4, &sLen);
70  }
71 
72  //Check status code
73  if(!error)
74  {
75  //The resulting ECDSA signature blob is encoded as a string
76  STORE32BE(rLen + sLen, p);
77  //Total number of bytes that have been written
78  *written = sizeof(uint32_t) + rLen + sLen;
79  }
80 
81  //Return status code
82  return error;
83 #else
84  //Not implemented
85  return ERROR_NOT_IMPLEMENTED;
86 #endif
87 }
88 
89 
90 /**
91  * @brief Parse an ECDSA signature
92  * @param[in] data Pointer to the ECDSA signature structure
93  * @param[in] length Length of the ECDSA signature structure, in bytes
94  * @param[out] signature Information resulting from the parsing process
95  * @return Error code
96  **/
97 
98 error_t sshParseEcdsaSignature(const uint8_t *data, size_t length,
99  SshEcdsaSignature *signature)
100 {
101 #if (SSH_ECDSA_SIGN_SUPPORT == ENABLED)
102  error_t error;
103 
104  //Decode integer R
105  error = sshParseBinaryString(data, length, &signature->r);
106  //Any error to report?
107  if(error)
108  return error;
109 
110  //Point to the next field
111  data += sizeof(uint32_t) + signature->r.length;
112  length -= sizeof(uint32_t) + signature->r.length;
113 
114  //Decode integer S
115  error = sshParseBinaryString(data, length, &signature->s);
116  //Any error to report?
117  if(error)
118  return error;
119 
120  //Point to the next field
121  data += sizeof(uint32_t) + signature->s.length;
122  length -= sizeof(uint32_t) + signature->s.length;
123 
124  //Malformed signature?
125  if(length != 0)
126  return ERROR_INVALID_MESSAGE;
127 
128  //Successful processing
129  return NO_ERROR;
130 #else
131  //Not implemented
132  return ERROR_NOT_IMPLEMENTED;
133 #endif
134 }
135 
136 #endif
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
Debugging facilities.
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_MESSAGE
Definition: error.h:105
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
uint8_t p
Definition: ndp.h:300
Secure Shell (SSH)
error_t sshConvertArrayToMpint(const uint8_t *value, size_t length, uint8_t *p, size_t *written)
Convert a binary string to mpint representation.
Definition: ssh_misc.c:1531
error_t sshParseBinaryString(const uint8_t *p, size_t length, SshBinaryString *string)
Parse a binary string.
Definition: ssh_misc.c:1189
SSH helper functions.
error_t sshFormatEcdsaSignature(const SshEcdsaSignature *signature, uint8_t *p, size_t *written)
Format an ECDSA signature.
Definition: ssh_sign_misc.c:52
error_t sshParseEcdsaSignature(const uint8_t *data, size_t length, SshEcdsaSignature *signature)
Parse an ECDSA signature.
Definition: ssh_sign_misc.c:98
Helper functions for signature generation and verification.
const uint8_t * value
Definition: ssh_types.h:68
size_t length
Definition: ssh_types.h:69
ECDSA signature.
Definition: ssh_sign_misc.h:48
SshBinaryString s
Definition: ssh_sign_misc.h:50
SshBinaryString r
Definition: ssh_sign_misc.h:49
uint8_t length
Definition: tcp.h:368