| 
									
										
										
										
											2016-05-18 02:51:04 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-09-05 15:35:49 +08:00
										 |  |  |  * Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:38:06 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:51:04 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 10:46:38 +08:00
										 |  |  | #include <string.h> /* strlen */
 | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  | #include <openssl/crypto.h>
 | 
					
						
							| 
									
										
										
										
											2024-08-02 04:47:00 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:40 +08:00
										 |  |  | #include "ec_local.h"
 | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* the return value must be freed (using OPENSSL_free()) */ | 
					
						
							|  |  |  | char *EC_POINT_point2hex(const EC_GROUP *group, | 
					
						
							|  |  |  |                          const EC_POINT *point, | 
					
						
							|  |  |  |                          point_conversion_form_t form, BN_CTX *ctx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     char *ret, *p; | 
					
						
							| 
									
										
										
										
											2024-08-02 04:47:00 +08:00
										 |  |  |     size_t buf_len, i; | 
					
						
							|  |  |  |     unsigned char *buf = NULL; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-12 09:04:25 +08:00
										 |  |  |     buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-12 09:04:25 +08:00
										 |  |  |     if (buf_len == 0) | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-29 03:28:14 +08:00
										 |  |  |     ret = OPENSSL_malloc(buf_len * 2 + 2); | 
					
						
							| 
									
										
										
										
											2024-08-02 04:47:00 +08:00
										 |  |  |     if (ret == NULL) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  |     p = ret; | 
					
						
							| 
									
										
										
										
											2024-08-02 04:47:00 +08:00
										 |  |  |     for (i = 0; i < buf_len; ++i) | 
					
						
							|  |  |  |         p += ossl_to_hex(p, buf[i]); | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  |     *p = '\0'; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-02 04:47:00 +08:00
										 |  |  |  err: | 
					
						
							| 
									
										
										
										
											2002-06-06 18:33:05 +08:00
										 |  |  |     OPENSSL_free(buf); | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | EC_POINT *EC_POINT_hex2point(const EC_GROUP *group, | 
					
						
							| 
									
										
										
										
											2020-11-02 10:46:38 +08:00
										 |  |  |                              const char *hex, EC_POINT *point, BN_CTX *ctx) | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-02 10:46:38 +08:00
										 |  |  |     int ok = 0; | 
					
						
							|  |  |  |     unsigned char *oct_buf = NULL; | 
					
						
							|  |  |  |     size_t len, oct_buf_len = 0; | 
					
						
							|  |  |  |     EC_POINT *pt = NULL; | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 10:46:38 +08:00
										 |  |  |     if (group == NULL || hex == NULL) | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 10:46:38 +08:00
										 |  |  |     if (point == NULL) { | 
					
						
							|  |  |  |         pt = EC_POINT_new(group); | 
					
						
							|  |  |  |         if (pt == NULL) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         pt = point; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 10:46:38 +08:00
										 |  |  |     len = strlen(hex) / 2; | 
					
						
							|  |  |  |     oct_buf = OPENSSL_malloc(len); | 
					
						
							|  |  |  |     if (oct_buf == NULL) | 
					
						
							| 
									
										
										
										
											2021-12-15 14:53:53 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-02 10:46:38 +08:00
										 |  |  |     if (!OPENSSL_hexstr2buf_ex(oct_buf, len, &oct_buf_len, hex, '\0') | 
					
						
							|  |  |  |         || !EC_POINT_oct2point(group, pt, oct_buf, oct_buf_len, ctx)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     ok = 1; | 
					
						
							|  |  |  | err: | 
					
						
							|  |  |  |     OPENSSL_clear_free(oct_buf, oct_buf_len); | 
					
						
							|  |  |  |     if (!ok) { | 
					
						
							|  |  |  |         if (pt != point) | 
					
						
							|  |  |  |             EC_POINT_clear_free(pt); | 
					
						
							|  |  |  |         pt = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return pt; | 
					
						
							| 
									
										
										
										
											2002-05-30 21:16:03 +08:00
										 |  |  | } |