VirtualBox

Ignore:
Timestamp:
Mar 3, 2022 7:17:34 PM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
150325
Message:

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

Location:
trunk/src/libs/openssl-3.0.1
Files:
1 deleted
57 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/openssl-3.0.1

    • Property svn:mergeinfo
      •  

        old new  
        1212/vendor/openssl/1.1.1c:131722-131725
        1313/vendor/openssl/1.1.1k:145841-145843
         14/vendor/openssl/3.0.1:150323-150324
         15/vendor/openssl/current:147554-150322
  • trunk/src/libs/openssl-3.0.1/crypto/ec/asm/ecp_nistz256-armv4.pl

    r91772 r94082  
    22# Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
    33#
    4 # Licensed under the OpenSSL license (the "License").  You may not use
     4# Licensed under the Apache License 2.0 (the "License").  You may not use
    55# this file except in compliance with the License.  You can obtain a copy
    66# in the file LICENSE in the source distribution or at
     
    3434# operation. Keep in mind that +200% means 3x improvement.
    3535
    36 $flavour = shift;
    37 if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
    38 else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
     36# $output is the last argument if it looks like a file (it has an extension)
     37# $flavour is the first argument if it doesn't look like a file
     38$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
     39$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
    3940
    4041if ($flavour && $flavour ne "void") {
     
    4445    die "can't locate arm-xlate.pl";
    4546
    46     open STDOUT,"| \"$^X\" $xlate $flavour $output";
     47    open STDOUT,"| \"$^X\" $xlate $flavour \"$output\""
     48        or die "can't call  $xlate: $!";
    4749} else {
    48     open STDOUT,">$output";
     50    $output and open STDOUT,">$output";
    4951}
    5052
     
    5254#include "arm_arch.h"
    5355
    54 .text
    5556#if defined(__thumb2__)
    5657.syntax unified
     
    8182
    8283$code.=<<___;
     84.rodata
    8385.globl  ecp_nistz256_precomputed
    8486.type   ecp_nistz256_precomputed,%object
     
    105107$code.=<<___;
    106108.size   ecp_nistz256_precomputed,.-ecp_nistz256_precomputed
     109
     110.text
    107111.align  5
    108112.LRR:   @ 2^512 mod P precomputed for NIST P256 polynomial
  • trunk/src/libs/openssl-3.0.1/crypto/ec/asm/ecp_nistz256-armv8.pl

    r91772 r94082  
    22# Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
    33#
    4 # Licensed under the OpenSSL license (the "License").  You may not use
     4# Licensed under the Apache License 2.0 (the "License").  You may not use
    55# this file except in compliance with the License.  You can obtain a copy
    66# in the file LICENSE in the source distribution or at
     
    3232# operation. Keep in mind that +400% means 5x improvement.
    3333
    34 $flavour = shift;
    35 while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
     34# $output is the last argument if it looks like a file (it has an extension)
     35# $flavour is the first argument if it doesn't look like a file
     36$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
     37$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
    3638
    3739$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
     
    4042die "can't locate arm-xlate.pl";
    4143
    42 open OUT,"| \"$^X\" $xlate $flavour $output";
     44open OUT,"| \"$^X\" $xlate $flavour \"$output\""
     45    or die "can't call $xlate: $!";
    4346*STDOUT=*OUT;
    4447
     
    14801483////////////////////////////////////////////////////////////////////////
    14811484// void ecp_nistz256_ord_sqr_mont(uint64_t res[4], uint64_t a[4],
    1482 //                                int rep);
     1485//                                uint64_t rep);
    14831486.globl  ecp_nistz256_ord_sqr_mont
    14841487.type   ecp_nistz256_ord_sqr_mont,%function
     
    16461649        ldp     x4,x5,[$inp]            // X
    16471650        ldp     x6,x7,[$inp,#16]
    1648         str     w4,[$out,#64*0-4]
     1651        stur    w4,[$out,#64*0-4]
    16491652        lsr     x4,x4,#32
    16501653        str     w5,[$out,#64*1-4]
     
    16621665        ldp     x4,x5,[$inp,#32]        // Y
    16631666        ldp     x6,x7,[$inp,#48]
    1664         str     w4,[$out,#64*0-4]
     1667        stur    w4,[$out,#64*0-4]
    16651668        lsr     x4,x4,#32
    16661669        str     w5,[$out,#64*1-4]
     
    16781681        ldp     x4,x5,[$inp,#64]        // Z
    16791682        ldp     x6,x7,[$inp,#80]
    1680         str     w4,[$out,#64*0-4]
     1683        stur    w4,[$out,#64*0-4]
    16811684        lsr     x4,x4,#32
    16821685        str     w5,[$out,#64*1-4]
  • trunk/src/libs/openssl-3.0.1/crypto/ec/asm/ecp_nistz256-ppc64.pl

    r91772 r94082  
    22# Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
    33#
    4 # Licensed under the OpenSSL license (the "License").  You may not use
     4# Licensed under the Apache License 2.0 (the "License").  You may not use
    55# this file except in compliance with the License.  You can obtain a copy
    66# in the file LICENSE in the source distribution or at
     
    2626# POWER8                +220-340%
    2727
    28 $flavour = shift;
    29 while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
     28# $output is the last argument if it looks like a file (it has an extension)
     29# $flavour is the first argument if it doesn't look like a file
     30$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
     31$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
    3032
    3133$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
     
    3436die "can't locate ppc-xlate.pl";
    3537
    36 open OUT,"| \"$^X\" $xlate $flavour $output";
     38open OUT,"| \"$^X\" $xlate $flavour \"$output\""
     39    or die "can't call $xlate: $!";
    3740*STDOUT=*OUT;
    3841
     
    19201923################################################################################
    19211924# void ecp_nistz256_ord_sqr_mont(uint64_t res[4], uint64_t a[4],
    1922 #                                int rep);
     1925#                                uint64_t rep);
    19231926.globl  ecp_nistz256_ord_sqr_mont
    19241927.align  5
  • trunk/src/libs/openssl-3.0.1/crypto/ec/asm/ecp_nistz256-sparcv9.pl

    r91772 r94082  
    11#! /usr/bin/env perl
    2 # Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
     2# Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
    33#
    4 # Licensed under the OpenSSL license (the "License").  You may not use
     4# Licensed under the Apache License 2.0 (the "License").  You may not use
    55# this file except in compliance with the License.  You can obtain a copy
    66# in the file LICENSE in the source distribution or at
     
    3232# operation. Keep in mind that +200% means 3x improvement.
    3333
    34 $output = pop;
    35 open STDOUT,">$output";
    36 
    37 $code.=<<___;
    38 #include "sparc_arch.h"
     34$output = pop and open STDOUT,">$output";
     35
     36$code.=<<___;
     37#ifndef __ASSEMBLER__
     38# define __ASSEMBLER__ 1
     39#endif
     40#include "crypto/sparc_arch.h"
    3941
    4042#define LOCALS  (STACK_BIAS+STACK_FRAME)
  • trunk/src/libs/openssl-3.0.1/crypto/ec/asm/ecp_nistz256-x86.pl

    r91772 r94082  
    22# Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
    33#
    4 # Licensed under the OpenSSL license (the "License").  You may not use
     4# Licensed under the Apache License 2.0 (the "License").  You may not use
    55# this file except in compliance with the License.  You can obtain a copy
    66# in the file LICENSE in the source distribution or at
     
    4343require "x86asm.pl";
    4444
    45 $output=pop;
    46 open STDOUT,">$output";
     45$output=pop and open STDOUT,">$output";
    4746
    4847&asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");
  • trunk/src/libs/openssl-3.0.1/crypto/ec/asm/ecp_nistz256-x86_64.pl

    r91772 r94082  
    44# Copyright (c) 2015 CloudFlare, Inc.
    55#
    6 # Licensed under the OpenSSL license (the "License").  You may not use
     6# Licensed under the Apache License 2.0 (the "License").  You may not use
    77# this file except in compliance with the License.  You can obtain a copy
    88# in the file LICENSE in the source distribution or at
     
    4141# Keep in mind that +100% means 2x improvement.
    4242
    43 $flavour = shift;
    44 $output  = shift;
    45 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
     43# $output is the last argument if it looks like a file (it has an extension)
     44# $flavour is the first argument if it doesn't look like a file
     45$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
     46$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
    4647
    4748$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
     
    5253die "can't locate x86_64-xlate.pl";
    5354
    54 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
     55open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
     56    or die "can't call $xlate: $!";
    5557*STDOUT=*OUT;
    5658
     
    827829#   uint64_t res[4],
    828830#   uint64_t a[4],
    829 #   int rep);
     831#   uint64_t rep);
    830832
    831833.globl  ecp_nistz256_ord_sqr_mont
  • trunk/src/libs/openssl-3.0.1/crypto/ec/asm/x25519-ppc64.pl

    r91772 r94082  
    22# Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
    33#
    4 # Licensed under the OpenSSL license (the "License").  You may not use
     4# Licensed under the Apache License 2.0 (the "License").  You may not use
    55# this file except in compliance with the License.  You can obtain a copy
    66# in the file LICENSE in the source distribution or at
     
    2828# from 12% to 30% improvement was measured...
    2929
    30 $flavour = shift;
    31 while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
     30# $output is the last argument if it looks like a file (it has an extension)
     31# $flavour is the first argument if it doesn't look like a file
     32$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
     33$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
    3234
    3335$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
     
    3638die "can't locate ppc-xlate.pl";
    3739
    38 open OUT,"| \"$^X\" $xlate $flavour $output";
     40open OUT,"| \"$^X\" $xlate $flavour \"$output\""
     41    or die "can't call $xlate: $!";
    3942*STDOUT=*OUT;
    4043
  • trunk/src/libs/openssl-3.0.1/crypto/ec/asm/x25519-x86_64.pl

    r91772 r94082  
    22# Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
    33#
    4 # Licensed under the OpenSSL license (the "License").  You may not use
     4# Licensed under the Apache License 2.0 (the "License").  You may not use
    55# this file except in compliance with the License.  You can obtain a copy
    66# in the file LICENSE in the source distribution or at
     
    6262#       2^51 radix;
    6363
    64 $flavour = shift;
    65 $output  = shift;
    66 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
     64# $output is the last argument if it looks like a file (it has an extension)
     65# $flavour is the first argument if it doesn't look like a file
     66$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
     67$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
    6768
    6869$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
     
    7374die "can't locate x86_64-xlate.pl";
    7475
    75 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
     76open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
     77    or die "can't call $xlate: $!";
    7678*STDOUT=*OUT;
    7779
  • trunk/src/libs/openssl-3.0.1/crypto/ec/build.info

    r91772 r94082  
    1 LIBS=../../libcrypto
    2 SOURCE[../../libcrypto]=\
    3         ec_lib.c ecp_smpl.c ecp_mont.c ecp_nist.c ec_cvt.c ec_mult.c \
    4         ec_err.c ec_curve.c ec_check.c ec_print.c ec_asn1.c ec_key.c \
    5         ec2_smpl.c ec_ameth.c ec_pmeth.c eck_prn.c \
    6         ecp_nistp224.c ecp_nistp256.c ecp_nistp521.c ecp_nistputil.c \
    7         ecp_oct.c ec2_oct.c ec_oct.c ec_kmeth.c ecdh_ossl.c ecdh_kdf.c \
    8         ecdsa_ossl.c ecdsa_sign.c ecdsa_vrf.c curve25519.c ecx_meth.c \
    9         curve448/arch_32/f_impl.c curve448/f_generic.c curve448/scalar.c \
     1$ECASM=
     2IF[{- !$disabled{asm} -}]
     3  $ECASM_x86=ecp_nistz256.c ecp_nistz256-x86.s
     4  $ECDEF_x86=ECP_NISTZ256_ASM
     5
     6  $ECASM_x86_64=ecp_nistz256.c ecp_nistz256-x86_64.s x25519-x86_64.s
     7  $ECDEF_x86_64=ECP_NISTZ256_ASM X25519_ASM
     8
     9  $ECASM_ia64=
     10
     11  $ECASM_sparcv9=ecp_nistz256.c ecp_nistz256-sparcv9.S
     12  $ECDEF_sparcv9=ECP_NISTZ256_ASM
     13
     14  $ECASM_sparcv8=
     15
     16  $ECASM_alpha=
     17
     18  $ECASM_mips32=
     19  $ECASM_mips64=
     20
     21  $ECASM_s390x=ecp_s390x_nistp.c ecx_s390x.c
     22  $ECDEF_s390x=S390X_EC_ASM
     23
     24  $ECASM_armv4=ecp_nistz256.c ecp_nistz256-armv4.S
     25  $ECDEF_armv4=ECP_NISTZ256_ASM
     26  $ECASM_aarch64=ecp_nistz256.c ecp_nistz256-armv8.S
     27  $ECDEF_aarch64=ECP_NISTZ256_ASM
     28
     29  $ECASM_parisc11=
     30  $ECASM_parisc20_64=
     31
     32  $ECASM_ppc32=
     33  $ECASM_ppc64=ecp_nistz256.c ecp_ppc.c ecp_nistz256-ppc64.s x25519-ppc64.s
     34  $ECDEF_ppc64=ECP_NISTZ256_ASM X25519_ASM
     35  IF[{- !$disabled{'ec_nistp_64_gcc_128'} -}]
     36    $ECASM_ppc64=$ECASM_ppc64 ecp_nistp521-ppc64.s
     37    $ECDEF_ppc64=$ECDEF_ppc64 ECP_NISTP521_ASM
     38    INCLUDE[ecp_nistp521.o]=..
     39  ENDIF
     40
     41  $ECASM_c64xplus=
     42
     43  # Now that we have defined all the arch specific variables, use the
     44  # appropriate one, and define the appropriate macros
     45  IF[$ECASM_{- $target{asm_arch} -}]
     46    $ECASM=$ECASM_{- $target{asm_arch} -}
     47    $ECDEF=$ECDEF_{- $target{asm_arch} -}
     48  ENDIF
     49ENDIF
     50
     51$COMMON=ec_lib.c ecp_smpl.c ecp_mont.c ecp_nist.c ec_cvt.c ec_mult.c \
     52        ec_curve.c ec_check.c ec_key.c ec_kmeth.c ecx_key.c ec_asn1.c \
     53        ec2_smpl.c \
     54        ecp_oct.c ec2_oct.c ec_oct.c ecdh_ossl.c \
     55        ecdsa_ossl.c ecdsa_sign.c ecdsa_vrf.c curve25519.c \
     56        curve448/f_generic.c curve448/scalar.c \
    1057        curve448/curve448_tables.c curve448/eddsa.c curve448/curve448.c \
    11         {- $target{ec_asm_src} -}
     58        $ECASM ec_backend.c ecx_backend.c ecdh_kdf.c curve448/arch_64/f_impl64.c \
     59        curve448/arch_32/f_impl32.c
    1260
    13 GENERATE[ecp_nistz256-x86.s]=asm/ecp_nistz256-x86.pl \
    14         $(PERLASM_SCHEME) $(LIB_CFLAGS) $(LIB_CPPFLAGS) $(PROCESSOR)
     61IF[{- !$disabled{'ec_nistp_64_gcc_128'} -}]
     62  $COMMON=$COMMON ecp_nistp224.c ecp_nistp256.c ecp_nistp521.c ecp_nistputil.c
     63ENDIF
    1564
    16 GENERATE[ecp_nistz256-x86_64.s]=asm/ecp_nistz256-x86_64.pl $(PERLASM_SCHEME)
     65SOURCE[../../libcrypto]=$COMMON ec_ameth.c ec_pmeth.c ecx_meth.c \
     66                        ec_err.c eck_prn.c \
     67                        ec_deprecated.c ec_print.c
     68SOURCE[../../providers/libfips.a]=$COMMON
    1769
    18 GENERATE[ecp_nistz256-avx2.s]=asm/ecp_nistz256-avx2.pl $(PERLASM_SCHEME)
     70# Implementations are now spread across several libraries, so the defines
     71# need to be applied to all affected libraries and modules.
     72DEFINE[../../libcrypto]=$ECDEF
     73DEFINE[../../providers/libfips.a]=$ECDEF
    1974
    20 GENERATE[ecp_nistz256-sparcv9.S]=asm/ecp_nistz256-sparcv9.pl $(PERLASM_SCHEME)
     75GENERATE[ecp_nistz256-x86.s]=asm/ecp_nistz256-x86.pl
     76
     77GENERATE[ecp_nistz256-x86_64.s]=asm/ecp_nistz256-x86_64.pl
     78
     79GENERATE[ecp_nistz256-avx2.s]=asm/ecp_nistz256-avx2.pl
     80
     81GENERATE[ecp_nistz256-sparcv9.S]=asm/ecp_nistz256-sparcv9.pl
    2182INCLUDE[ecp_nistz256-sparcv9.o]=..
    2283
    23 GENERATE[ecp_nistz256-armv4.S]=asm/ecp_nistz256-armv4.pl $(PERLASM_SCHEME)
     84INCLUDE[ecp_s390x_nistp.o]=..
     85INCLUDE[ecx_s390x.o]=..
     86INCLUDE[ecx_meth.o]=..
     87
     88GENERATE[ecp_nistz256-armv4.S]=asm/ecp_nistz256-armv4.pl
    2489INCLUDE[ecp_nistz256-armv4.o]=..
    25 GENERATE[ecp_nistz256-armv8.S]=asm/ecp_nistz256-armv8.pl $(PERLASM_SCHEME)
     90GENERATE[ecp_nistz256-armv8.S]=asm/ecp_nistz256-armv8.pl
    2691INCLUDE[ecp_nistz256-armv8.o]=..
    27 GENERATE[ecp_nistz256-ppc64.s]=asm/ecp_nistz256-ppc64.pl $(PERLASM_SCHEME)
     92GENERATE[ecp_nistz256-ppc64.s]=asm/ecp_nistz256-ppc64.pl
    2893
    29 GENERATE[x25519-x86_64.s]=asm/x25519-x86_64.pl $(PERLASM_SCHEME)
    30 GENERATE[x25519-ppc64.s]=asm/x25519-ppc64.pl $(PERLASM_SCHEME)
     94GENERATE[ecp_nistp521-ppc64.s]=asm/ecp_nistp521-ppc64.pl
    3195
    32 BEGINRAW[Makefile]
    33 {- $builddir -}/ecp_nistz256-%.S:       {- $sourcedir -}/asm/ecp_nistz256-%.pl
    34         CC="$(CC)" $(PERL) $< $(PERLASM_SCHEME) $@
    35 ENDRAW[Makefile]
    36 
    37 INCLUDE[curve448/arch_32/f_impl.o]=curve448/arch_32 curve448
    38 INCLUDE[curve448/f_generic.o]=curve448/arch_32 curve448
    39 INCLUDE[curve448/scalar.o]=curve448/arch_32 curve448
    40 INCLUDE[curve448/curve448_tables.o]=curve448/arch_32 curve448
    41 INCLUDE[curve448/eddsa.o]=curve448/arch_32 curve448
    42 INCLUDE[curve448/curve448.o]=curve448/arch_32 curve448
     96GENERATE[x25519-x86_64.s]=asm/x25519-x86_64.pl
     97GENERATE[x25519-ppc64.s]=asm/x25519-ppc64.pl
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve25519.c

    r91772 r94082  
    11/*
    2  * Copyright 2016-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    88 */
    99
     10/*
     11 * ECDSA low level APIs are deprecated for public use, but still ok for
     12 * internal use.
     13 */
     14#include "internal/deprecated.h"
     15
    1016#include <string.h>
     17#include "crypto/ecx.h"
    1118#include "ec_local.h"
     19#include <openssl/evp.h>
    1220#include <openssl/sha.h>
     21
     22#include "internal/numbers.h"
    1323
    1424#if defined(X25519_ASM) && (defined(__x86_64) || defined(__x86_64__) || \
     
    253263
    254264#if defined(X25519_ASM) \
    255     || ( (defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16) \
     265    || ( defined(INT128_MAX) \
    256266         && !defined(__sparc__) \
    257267         && (!defined(__SIZEOF_LONG__) || (__SIZEOF_LONG__ == 8)) \
     
    386396# else
    387397
    388 typedef __uint128_t u128;
     398typedef uint128_t u128;
    389399
    390400static void fe51_mul(fe51 h, const fe51 f, const fe51 g)
     
    54305440}
    54315441
    5432 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
    5433                  const uint8_t public_key[32], const uint8_t private_key[32])
     5442int
     5443ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
     5444                  const uint8_t public_key[32], const uint8_t private_key[32],
     5445                  OSSL_LIB_CTX *libctx, const char *propq)
    54345446{
    54355447    uint8_t az[SHA512_DIGEST_LENGTH];
     
    54375449    ge_p3 R;
    54385450    uint8_t hram[SHA512_DIGEST_LENGTH];
    5439     SHA512_CTX hash_ctx;
    5440 
    5441     SHA512_Init(&hash_ctx);
    5442     SHA512_Update(&hash_ctx, private_key, 32);
    5443     SHA512_Final(az, &hash_ctx);
     5451    EVP_MD *sha512 = EVP_MD_fetch(libctx, SN_sha512, propq);
     5452    EVP_MD_CTX *hash_ctx = EVP_MD_CTX_new();
     5453    unsigned int sz;
     5454    int res = 0;
     5455
     5456    if (sha512 == NULL || hash_ctx == NULL)
     5457        goto err;
     5458
     5459    if (!EVP_DigestInit_ex(hash_ctx, sha512, NULL)
     5460        || !EVP_DigestUpdate(hash_ctx, private_key, 32)
     5461        || !EVP_DigestFinal_ex(hash_ctx, az, &sz))
     5462        goto err;
    54445463
    54455464    az[0] &= 248;
     
    54475466    az[31] |= 64;
    54485467
    5449     SHA512_Init(&hash_ctx);
    5450     SHA512_Update(&hash_ctx, az + 32, 32);
    5451     SHA512_Update(&hash_ctx, message, message_len);
    5452     SHA512_Final(nonce, &hash_ctx);
     5468    if (!EVP_DigestInit_ex(hash_ctx, sha512, NULL)
     5469        || !EVP_DigestUpdate(hash_ctx, az + 32, 32)
     5470        || !EVP_DigestUpdate(hash_ctx, message, message_len)
     5471        || !EVP_DigestFinal_ex(hash_ctx, nonce, &sz))
     5472        goto err;
    54535473
    54545474    x25519_sc_reduce(nonce);
     
    54565476    ge_p3_tobytes(out_sig, &R);
    54575477
    5458     SHA512_Init(&hash_ctx);
    5459     SHA512_Update(&hash_ctx, out_sig, 32);
    5460     SHA512_Update(&hash_ctx, public_key, 32);
    5461     SHA512_Update(&hash_ctx, message, message_len);
    5462     SHA512_Final(hram, &hash_ctx);
     5478    if (!EVP_DigestInit_ex(hash_ctx, sha512, NULL)
     5479        || !EVP_DigestUpdate(hash_ctx, out_sig, 32)
     5480        || !EVP_DigestUpdate(hash_ctx, public_key, 32)
     5481        || !EVP_DigestUpdate(hash_ctx, message, message_len)
     5482        || !EVP_DigestFinal_ex(hash_ctx, hram, &sz))
     5483        goto err;
    54635484
    54645485    x25519_sc_reduce(hram);
    54655486    sc_muladd(out_sig + 32, hram, az, nonce);
    54665487
    5467     OPENSSL_cleanse(&hash_ctx, sizeof(hash_ctx));
     5488    res = 1;
     5489err:
    54685490    OPENSSL_cleanse(nonce, sizeof(nonce));
    54695491    OPENSSL_cleanse(az, sizeof(az));
    5470 
    5471     return 1;
     5492    EVP_MD_free(sha512);
     5493    EVP_MD_CTX_free(hash_ctx);
     5494    return res;
    54725495}
    54735496
    54745497static const char allzeroes[15];
    54755498
    5476 int ED25519_verify(const uint8_t *message, size_t message_len,
    5477                    const uint8_t signature[64], const uint8_t public_key[32])
     5499int
     5500ossl_ed25519_verify(const uint8_t *message, size_t message_len,
     5501                    const uint8_t signature[64], const uint8_t public_key[32],
     5502                    OSSL_LIB_CTX *libctx, const char *propq)
    54785503{
    54795504    int i;
    54805505    ge_p3 A;
    54815506    const uint8_t *r, *s;
    5482     SHA512_CTX hash_ctx;
     5507    EVP_MD *sha512;
     5508    EVP_MD_CTX *hash_ctx = NULL;
     5509    unsigned int sz;
     5510    int res = 0;
    54835511    ge_p2 R;
    54845512    uint8_t rcheck[32];
     
    55275555    fe_neg(A.T, A.T);
    55285556
    5529     SHA512_Init(&hash_ctx);
    5530     SHA512_Update(&hash_ctx, r, 32);
    5531     SHA512_Update(&hash_ctx, public_key, 32);
    5532     SHA512_Update(&hash_ctx, message, message_len);
    5533     SHA512_Final(h, &hash_ctx);
     5557    sha512 = EVP_MD_fetch(libctx, SN_sha512, propq);
     5558    if (sha512 == NULL)
     5559        return 0;
     5560    hash_ctx = EVP_MD_CTX_new();
     5561    if (hash_ctx == NULL)
     5562        goto err;
     5563
     5564    if (!EVP_DigestInit_ex(hash_ctx, sha512, NULL)
     5565        || !EVP_DigestUpdate(hash_ctx, r, 32)
     5566        || !EVP_DigestUpdate(hash_ctx, public_key, 32)
     5567        || !EVP_DigestUpdate(hash_ctx, message, message_len)
     5568        || !EVP_DigestFinal_ex(hash_ctx, h, &sz))
     5569        goto err;
    55345570
    55355571    x25519_sc_reduce(h);
     
    55395575    ge_tobytes(rcheck, &R);
    55405576
    5541     return CRYPTO_memcmp(rcheck, r, sizeof(rcheck)) == 0;
    5542 }
    5543 
    5544 void ED25519_public_from_private(uint8_t out_public_key[32],
    5545                                  const uint8_t private_key[32])
     5577    res = CRYPTO_memcmp(rcheck, r, sizeof(rcheck)) == 0;
     5578err:
     5579    EVP_MD_free(sha512);
     5580    EVP_MD_CTX_free(hash_ctx);
     5581    return res;
     5582}
     5583
     5584int
     5585ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32],
     5586                                 const uint8_t private_key[32],
     5587                                 const char *propq)
    55465588{
    55475589    uint8_t az[SHA512_DIGEST_LENGTH];
    55485590    ge_p3 A;
    5549 
    5550     SHA512(private_key, 32, az);
     5591    int r;
     5592    EVP_MD *sha512 = NULL;
     5593
     5594    sha512 = EVP_MD_fetch(ctx, SN_sha512, propq);
     5595    if (sha512 == NULL)
     5596        return 0;
     5597    r = EVP_Digest(private_key, 32, az, NULL, sha512, NULL);
     5598    EVP_MD_free(sha512);
     5599    if (!r) {
     5600        OPENSSL_cleanse(az, sizeof(az));
     5601        return 0;
     5602    }
    55515603
    55525604    az[0] &= 248;
     
    55585610
    55595611    OPENSSL_cleanse(az, sizeof(az));
    5560 }
    5561 
    5562 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
    5563            const uint8_t peer_public_value[32])
     5612    return 1;
     5613}
     5614
     5615int
     5616ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
     5617            const uint8_t peer_public_value[32])
    55645618{
    55655619    static const uint8_t kZeros[32] = {0};
     
    55695623}
    55705624
    5571 void X25519_public_from_private(uint8_t out_public_value[32],
     5625void
     5626ossl_x25519_public_from_private(uint8_t out_public_value[32],
    55725627                                const uint8_t private_key[32])
    55735628{
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/arch_32/arch_intrinsics.h

    r91772 r94082  
    33 * Copyright 2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/arch_32/f_impl.h

    r91772 r94082  
    33 * Copyright 2014-2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/curve448.c

    r91772 r94082  
    11/*
    2  * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2015-2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    1616#include "point_448.h"
    1717#include "ed448.h"
     18#include "crypto/ecx.h"
    1819#include "curve448_local.h"
    1920
     
    5556
    5657/** identity = (0,1) */
    57 const curve448_point_t curve448_point_identity =
     58const curve448_point_t ossl_curve448_point_identity =
    5859    { {{{{0}}}, {{{1}}}, {{{1}}}, {{{0}}}} };
    5960
     
    8283}
    8384
    84 void curve448_point_double(curve448_point_t p, const curve448_point_t q)
     85void ossl_curve448_point_double(curve448_point_t p, const curve448_point_t q)
    8586{
    8687    point_double_internal(p, q, 0);
     
    184185}
    185186
    186 c448_bool_t curve448_point_eq(const curve448_point_t p,
    187                               const curve448_point_t q)
     187c448_bool_t
     188ossl_curve448_point_eq(const curve448_point_t p,
     189                       const curve448_point_t q)
    188190{
    189191    mask_t succ;
     
    198200}
    199201
    200 c448_bool_t curve448_point_valid(const curve448_point_t p)
     202c448_bool_t
     203ossl_curve448_point_valid(const curve448_point_t p)
    201204{
    202205    mask_t out;
     
    225228}
    226229
    227 void curve448_precomputed_scalarmul(curve448_point_t out,
     230void
     231ossl_curve448_precomputed_scalarmul(curve448_point_t out,
    228232                                    const curve448_precomputed_s * table,
    229233                                    const curve448_scalar_t scalar)
     
    234238    curve448_scalar_t scalar1x;
    235239
    236     curve448_scalar_add(scalar1x, scalar, precomputed_scalarmul_adjustment);
    237     curve448_scalar_halve(scalar1x, scalar1x);
     240    ossl_curve448_scalar_add(scalar1x, scalar, precomputed_scalarmul_adjustment);
     241    ossl_curve448_scalar_halve(scalar1x, scalar1x);
    238242
    239243    for (i = s; i > 0; i--) {
     
    272276}
    273277
    274 void curve448_point_mul_by_ratio_and_encode_like_eddsa(
     278void
     279ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(
    275280                                    uint8_t enc[EDDSA_448_PUBLIC_BYTES],
    276281                                    const curve448_point_t p)
     
    316321    OPENSSL_cleanse(z, sizeof(z));
    317322    OPENSSL_cleanse(t, sizeof(t));
    318     curve448_point_destroy(q);
    319 }
    320 
    321 c448_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(
     323    ossl_curve448_point_destroy(q);
     324}
     325
     326c448_error_t
     327ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(
    322328                                curve448_point_t p,
    323329                                const uint8_t enc[EDDSA_448_PUBLIC_BYTES])
     
    372378
    373379    OPENSSL_cleanse(enc2, sizeof(enc2));
    374     assert(curve448_point_valid(p) || ~succ);
     380    assert(ossl_curve448_point_valid(p) || ~succ);
    375381
    376382    return c448_succeed_if(mask_to_bool(succ));
    377383}
    378384
    379 c448_error_t x448_int(uint8_t out[X_PUBLIC_BYTES],
    380                       const uint8_t base[X_PUBLIC_BYTES],
    381                       const uint8_t scalar[X_PRIVATE_BYTES])
     385c448_error_t
     386ossl_x448_int(uint8_t out[X_PUBLIC_BYTES],
     387              const uint8_t base[X_PUBLIC_BYTES],
     388              const uint8_t scalar[X_PRIVATE_BYTES])
    382389{
    383390    gf x1, x2, z2, x3, z3, t1, t2;
     
    456463}
    457464
    458 void curve448_point_mul_by_ratio_and_encode_like_x448(uint8_t
     465void
     466ossl_curve448_point_mul_by_ratio_and_encode_like_x448(uint8_t
    459467                                                      out[X_PUBLIC_BYTES],
    460468                                                      const curve448_point_t p)
     
    467475    gf_sqr(q->y, q->z);         /* (y/x)^2 */
    468476    gf_serialize(out, q->y, 1);
    469     curve448_point_destroy(q);
    470 }
    471 
    472 void x448_derive_public_key(uint8_t out[X_PUBLIC_BYTES],
    473                             const uint8_t scalar[X_PRIVATE_BYTES])
     477    ossl_curve448_point_destroy(q);
     478}
     479
     480void ossl_x448_derive_public_key(uint8_t out[X_PUBLIC_BYTES],
     481                                 const uint8_t scalar[X_PRIVATE_BYTES])
    474482{
    475483    /* Scalar conditioning */
     
    485493    scalar2[X_PRIVATE_BYTES - 1] |= 1 << ((X_PRIVATE_BITS + 7) % 8);
    486494
    487     curve448_scalar_decode_long(the_scalar, scalar2, sizeof(scalar2));
     495    ossl_curve448_scalar_decode_long(the_scalar, scalar2, sizeof(scalar2));
    488496
    489497    /* Compensate for the encoding ratio */
    490498    for (i = 1; i < X448_ENCODE_RATIO; i <<= 1)
    491         curve448_scalar_halve(the_scalar, the_scalar);
    492 
    493     curve448_precomputed_scalarmul(p, curve448_precomputed_base, the_scalar);
    494     curve448_point_mul_by_ratio_and_encode_like_x448(out, p);
    495     curve448_point_destroy(p);
     499        ossl_curve448_scalar_halve(the_scalar, the_scalar);
     500
     501    ossl_curve448_precomputed_scalarmul(p, ossl_curve448_precomputed_base,
     502                                        the_scalar);
     503    ossl_curve448_point_mul_by_ratio_and_encode_like_x448(out, p);
     504    ossl_curve448_point_destroy(p);
    496505}
    497506
     
    502511
    503512#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3))
    504 # define NUMTRAILINGZEROS       __builtin_ctz
     513# define NUMTRAILINGZEROS       __builtin_ctz
    505514#else
    506 # define NUMTRAILINGZEROS       numtrailingzeros
     515# define NUMTRAILINGZEROS       numtrailingzeros
    507516static uint32_t numtrailingzeros(uint32_t i)
    508517{
     
    610619        return;
    611620
    612     curve448_point_double(tmp, working);
     621    ossl_curve448_point_double(tmp, working);
    613622    pt_to_pniels(twop, tmp);
    614623
     
    621630    }
    622631
    623     curve448_point_destroy(tmp);
     632    ossl_curve448_point_destroy(tmp);
    624633    OPENSSL_cleanse(twop, sizeof(twop));
    625634}
    626635
    627 void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
     636void
     637ossl_curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
    628638                                               const curve448_scalar_t scalar1,
    629639                                               const curve448_point_t base2,
     
    645655
    646656    if (i < 0) {
    647         curve448_point_copy(combo, curve448_point_identity);
     657        curve448_point_copy(combo, ossl_curve448_point_identity);
    648658        return;
    649659    }
     
    653663    } else if (i == control_pre[0].power && i >= 0) {
    654664        pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
    655         add_niels_to_pt(combo, curve448_wnaf_base[control_pre[0].addend >> 1],
     665        add_niels_to_pt(combo,
     666                        ossl_curve448_wnaf_base[control_pre[0].addend >> 1],
    656667                        i);
    657668        contv++;
     
    659670    } else {
    660671        i = control_pre[0].power;
    661         niels_to_pt(combo, curve448_wnaf_base[control_pre[0].addend >> 1]);
     672        niels_to_pt(combo, ossl_curve448_wnaf_base[control_pre[0].addend >> 1]);
    662673        contp++;
    663674    }
     
    688699            if (control_pre[contp].addend > 0)
    689700                add_niels_to_pt(combo,
    690                                 curve448_wnaf_base[control_pre[contp].addend
     701                                ossl_curve448_wnaf_base[control_pre[contp].addend
    691702                                                   >> 1], i);
    692703            else
    693704                sub_niels_from_pt(combo,
    694                                   curve448_wnaf_base[(-control_pre
     705                                  ossl_curve448_wnaf_base[(-control_pre
    695706                                                      [contp].addend) >> 1], i);
    696707            contp++;
     
    709720}
    710721
    711 void curve448_point_destroy(curve448_point_t point)
     722void ossl_curve448_point_destroy(curve448_point_t point)
    712723{
    713724    OPENSSL_cleanse(point, sizeof(curve448_point_t));
    714725}
    715726
    716 int X448(uint8_t out_shared_key[56], const uint8_t private_key[56],
    717          const uint8_t peer_public_value[56])
    718 {
    719     return x448_int(out_shared_key, peer_public_value, private_key)
     727int ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
     728              const uint8_t peer_public_value[56])
     729{
     730    return ossl_x448_int(out_shared_key, peer_public_value, private_key)
    720731           == C448_SUCCESS;
    721732}
    722733
    723 void X448_public_from_private(uint8_t out_public_value[56],
    724                               const uint8_t private_key[56])
    725 {
    726     x448_derive_public_key(out_public_value, private_key);
    727 }
     734void ossl_x448_public_from_private(uint8_t out_public_value[56],
     735                                   const uint8_t private_key[56])
     736{
     737    ossl_x448_derive_public_key(out_public_value, private_key);
     738}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/curve448_local.h

    r91772 r94082  
    11/*
    2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    1111# include "curve448utils.h"
    1212
    13 int X448(uint8_t out_shared_key[56], const uint8_t private_key[56],
    14          const uint8_t peer_public_value[56]);
     13int
     14ossl_ed448ph_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t hash[64],
     15                  const uint8_t public_key[57], const uint8_t private_key[57],
     16                  const uint8_t *context, size_t context_len, const char *propq);
    1517
    16 void X448_public_from_private(uint8_t out_public_value[56],
    17                               const uint8_t private_key[56]);
    18 
    19 int ED448_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
    20                const uint8_t public_key[57], const uint8_t private_key[57],
    21                const uint8_t *context, size_t context_len);
    22 
    23 int ED448_verify(const uint8_t *message, size_t message_len,
    24                  const uint8_t signature[114], const uint8_t public_key[57],
    25                  const uint8_t *context, size_t context_len);
    26 
    27 int ED448ph_sign(uint8_t *out_sig, const uint8_t hash[64],
    28                  const uint8_t public_key[57], const uint8_t private_key[57],
    29                  const uint8_t *context, size_t context_len);
    30 
    31 int ED448ph_verify(const uint8_t hash[64], const uint8_t signature[114],
    32                    const uint8_t public_key[57], const uint8_t *context,
    33                    size_t context_len);
    34 
    35 int ED448_public_from_private(uint8_t out_public_key[57],
    36                               const uint8_t private_key[57]);
     18int
     19ossl_ed448ph_verify(OSSL_LIB_CTX *ctx, const uint8_t hash[64],
     20                    const uint8_t signature[114], const uint8_t public_key[57],
     21                    const uint8_t *context, size_t context_len,
     22                    const char *propq);
    3723
    3824#endif              /* OSSL_CRYPTO_EC_CURVE448_LOCAL_H */
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/curve448_tables.c

    r91772 r94082  
    11/*
    2  * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2015-2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    10591059    }
    10601060};
    1061 const struct curve448_precomputed_s *curve448_precomputed_base
     1061const struct curve448_precomputed_s *ossl_curve448_precomputed_base
    10621062    = &curve448_precomputed_base_table;
    10631063
     
    14811481    }}
    14821482};
    1483 const niels_t *curve448_wnaf_base = curve448_wnaf_base_table;
     1483const niels_t *ossl_curve448_wnaf_base = curve448_wnaf_base_table;
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/curve448utils.h

    r91772 r94082  
    11/*
    2  * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2015 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    1515
    1616# include <openssl/e_os2.h>
     17
     18# include "internal/numbers.h"
    1719
    1820/*
     
    4244typedef uint64_t c448_bool_t;
    4345/* Double-word size for internal computations */
    44 typedef __uint128_t c448_dword_t;
     46typedef uint128_t c448_dword_t;
    4547/* Signed double-word size for internal computations */
    46 typedef __int128_t c448_dsword_t;
     48typedef int128_t c448_dsword_t;
    4749# elif C448_WORD_BITS == 32
    4850/* Word size for internal computations */
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/ed448.h

    r91772 r94082  
    11/*
    2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2015-2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    3838 * privkey (in): The private key.
    3939 */
    40 c448_error_t c448_ed448_derive_public_key(
    41                         uint8_t pubkey [EDDSA_448_PUBLIC_BYTES],
    42                         const uint8_t privkey [EDDSA_448_PRIVATE_BYTES]);
     40c448_error_t
     41ossl_c448_ed448_derive_public_key(
     42                              OSSL_LIB_CTX *ctx,
     43                              uint8_t pubkey [EDDSA_448_PUBLIC_BYTES],
     44                              const uint8_t privkey [EDDSA_448_PRIVATE_BYTES],
     45                              const char *propq);
    4346
    4447/*
     
    5962 * disambiguation.  For Ed448 it is safe.
    6063 */
    61 c448_error_t c448_ed448_sign(
    62                         uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
    63                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
    64                         const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
    65                         const uint8_t *message, size_t message_len,
    66                         uint8_t prehashed, const uint8_t *context,
    67                         size_t context_len);
     64c448_error_t
     65ossl_c448_ed448_sign(OSSL_LIB_CTX *ctx,
     66                     uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
     67                     const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
     68                     const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
     69                     const uint8_t *message, size_t message_len,
     70                     uint8_t prehashed, const uint8_t *context,
     71                     size_t context_len,
     72                     const char *propq);
    6873
    6974/*
     
    8388 * disambiguation.  For Ed448 it is safe.
    8489 */
    85 c448_error_t c448_ed448_sign_prehash(
    86                         uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
    87                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
    88                         const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
    89                         const uint8_t hash[64],
    90                         const uint8_t *context,
    91                         size_t context_len);
     90c448_error_t
     91ossl_c448_ed448_sign_prehash(OSSL_LIB_CTX *ctx,
     92                             uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
     93                             const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
     94                             const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
     95                             const uint8_t hash[64],
     96                             const uint8_t *context,
     97                             size_t context_len,
     98                             const char *propq);
    9299
    93100/*
     
    109116 * disambiguation.  For Ed448 it is safe.
    110117 */
    111 c448_error_t c448_ed448_verify(const uint8_t
    112                                  signature[EDDSA_448_SIGNATURE_BYTES],
    113                                  const uint8_t
    114                                  pubkey[EDDSA_448_PUBLIC_BYTES],
    115                                  const uint8_t *message, size_t message_len,
    116                                  uint8_t prehashed, const uint8_t *context,
    117                                  uint8_t context_len);
     118c448_error_t
     119ossl_c448_ed448_verify(OSSL_LIB_CTX *ctx,
     120                       const uint8_t
     121                       signature[EDDSA_448_SIGNATURE_BYTES],
     122                       const uint8_t
     123                       pubkey[EDDSA_448_PUBLIC_BYTES],
     124                       const uint8_t *message, size_t message_len,
     125                       uint8_t prehashed, const uint8_t *context,
     126                       uint8_t context_len,
     127                       const char *propq);
    118128
    119129/*
     
    134144 * disambiguation.  For Ed448 it is safe.
    135145 */
    136 c448_error_t c448_ed448_verify_prehash(
    137                     const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
    138                     const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
    139                     const uint8_t hash[64],
    140                     const uint8_t *context,
    141                     uint8_t context_len);
     146c448_error_t
     147ossl_c448_ed448_verify_prehash(
     148                            OSSL_LIB_CTX *ctx,
     149                            const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
     150                            const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
     151                            const uint8_t hash[64],
     152                            const uint8_t *context,
     153                            uint8_t context_len,
     154                            const char *propq);
    142155
    143156/*
     
    164177 * p (in): The point.
    165178 */
    166 void curve448_point_mul_by_ratio_and_encode_like_eddsa(
     179void
     180ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(
    167181                                    uint8_t enc [EDDSA_448_PUBLIC_BYTES],
    168182                                    const curve448_point_t p);
     
    177191 * p (in): The point.
    178192 */
    179 c448_error_t curve448_point_decode_like_eddsa_and_mul_by_ratio(
     193c448_error_t
     194ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(
    180195                            curve448_point_t p,
    181196                            const uint8_t enc[EDDSA_448_PUBLIC_BYTES]);
     
    189204 * ed (in): The EdDSA private key
    190205 */
    191 c448_error_t c448_ed448_convert_private_key_to_x448(
    192                             uint8_t x[X448_PRIVATE_BYTES],
    193                             const uint8_t ed[EDDSA_448_PRIVATE_BYTES]);
     206c448_error_t
     207ossl_c448_ed448_convert_private_key_to_x448(
     208                                    OSSL_LIB_CTX *ctx,
     209                                    uint8_t x[X448_PRIVATE_BYTES],
     210                                    const uint8_t ed[EDDSA_448_PRIVATE_BYTES],
     211                                    const char *propq);
    194212
    195213#endif                          /* OSSL_CRYPTO_EC_CURVE448_ED448_H */
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/eddsa.c

    r91772 r94082  
    11/*
    2  * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2015-2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    1313#include <openssl/crypto.h>
    1414#include <openssl/evp.h>
     15#include "crypto/ecx.h"
    1516#include "curve448_local.h"
    1617#include "word.h"
     
    2021#define COFACTOR 4
    2122
    22 static c448_error_t oneshot_hash(uint8_t *out, size_t outlen,
    23                                  const uint8_t *in, size_t inlen)
     23static c448_error_t oneshot_hash(OSSL_LIB_CTX *ctx, uint8_t *out, size_t outlen,
     24                                 const uint8_t *in, size_t inlen,
     25                                 const char *propq)
    2426{
    2527    EVP_MD_CTX *hashctx = EVP_MD_CTX_new();
     28    EVP_MD *shake256 = NULL;
     29    c448_error_t ret = C448_FAILURE;
    2630
    2731    if (hashctx == NULL)
    2832        return C448_FAILURE;
    2933
    30     if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
     34    shake256 = EVP_MD_fetch(ctx, "SHAKE256", propq);
     35    if (shake256 == NULL)
     36        goto err;
     37
     38    if (!EVP_DigestInit_ex(hashctx, shake256, NULL)
    3139            || !EVP_DigestUpdate(hashctx, in, inlen)
    32             || !EVP_DigestFinalXOF(hashctx, out, outlen)) {
    33         EVP_MD_CTX_free(hashctx);
    34         return C448_FAILURE;
    35     }
    36 
     40            || !EVP_DigestFinalXOF(hashctx, out, outlen))
     41        goto err;
     42
     43    ret = C448_SUCCESS;
     44 err:
    3745    EVP_MD_CTX_free(hashctx);
    38     return C448_SUCCESS;
     46    EVP_MD_free(shake256);
     47    return ret;
    3948}
    4049
     
    4655}
    4756
    48 static c448_error_t hash_init_with_dom(EVP_MD_CTX *hashctx, uint8_t prehashed,
     57static c448_error_t hash_init_with_dom(OSSL_LIB_CTX *ctx, EVP_MD_CTX *hashctx,
     58                                       uint8_t prehashed,
    4959                                       uint8_t for_prehash,
    5060                                       const uint8_t *context,
    51                                        size_t context_len)
     61                                       size_t context_len,
     62                                       const char *propq)
    5263{
    5364#ifdef CHARSET_EBCDIC
     
    5869#endif
    5970    uint8_t dom[2];
     71    EVP_MD *shake256 = NULL;
    6072
    6173    if (context_len > UINT8_MAX)
     
    6678    dom[1] = (uint8_t)context_len;
    6779
    68     if (!EVP_DigestInit_ex(hashctx, EVP_shake256(), NULL)
     80    shake256 = EVP_MD_fetch(ctx, "SHAKE256", propq);
     81    if (shake256 == NULL)
     82        return C448_FAILURE;
     83
     84    if (!EVP_DigestInit_ex(hashctx, shake256, NULL)
    6985            || !EVP_DigestUpdate(hashctx, dom_s, strlen(dom_s))
    7086            || !EVP_DigestUpdate(hashctx, dom, sizeof(dom))
    71             || !EVP_DigestUpdate(hashctx, context, context_len))
    72         return C448_FAILURE;
    73 
     87            || !EVP_DigestUpdate(hashctx, context, context_len)) {
     88        EVP_MD_free(shake256);
     89        return C448_FAILURE;
     90    }
     91
     92    EVP_MD_free(shake256);
    7493    return C448_SUCCESS;
    7594}
    7695
    7796/* In this file because it uses the hash */
    78 c448_error_t c448_ed448_convert_private_key_to_x448(
     97c448_error_t
     98ossl_c448_ed448_convert_private_key_to_x448(
     99                            OSSL_LIB_CTX *ctx,
    79100                            uint8_t x[X448_PRIVATE_BYTES],
    80                             const uint8_t ed [EDDSA_448_PRIVATE_BYTES])
     101                            const uint8_t ed [EDDSA_448_PRIVATE_BYTES],
     102                            const char *propq)
    81103{
    82104    /* pass the private key through oneshot_hash function */
    83105    /* and keep the first X448_PRIVATE_BYTES bytes */
    84     return oneshot_hash(x, X448_PRIVATE_BYTES, ed,
    85                         EDDSA_448_PRIVATE_BYTES);
    86 }
    87 
    88 c448_error_t c448_ed448_derive_public_key(
     106    return oneshot_hash(ctx, x, X448_PRIVATE_BYTES, ed,
     107                        EDDSA_448_PRIVATE_BYTES, propq);
     108}
     109
     110c448_error_t
     111ossl_c448_ed448_derive_public_key(
     112                        OSSL_LIB_CTX *ctx,
    89113                        uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
    90                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES])
     114                        const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
     115                        const char *propq)
    91116{
    92117    /* only this much used for keygen */
     
    96121    curve448_point_t p;
    97122
    98     if (!oneshot_hash(secret_scalar_ser, sizeof(secret_scalar_ser), privkey,
    99                       EDDSA_448_PRIVATE_BYTES))
     123    if (!oneshot_hash(ctx, secret_scalar_ser, sizeof(secret_scalar_ser),
     124                      privkey,
     125                      EDDSA_448_PRIVATE_BYTES,
     126                      propq))
    100127        return C448_FAILURE;
    101128
    102129    clamp(secret_scalar_ser);
    103130
    104     curve448_scalar_decode_long(secret_scalar, secret_scalar_ser,
    105                                 sizeof(secret_scalar_ser));
     131    ossl_curve448_scalar_decode_long(secret_scalar, secret_scalar_ser,
     132                                     sizeof(secret_scalar_ser));
    106133
    107134    /*
     
    114141     */
    115142    for (c = 1; c < C448_EDDSA_ENCODE_RATIO; c <<= 1)
    116         curve448_scalar_halve(secret_scalar, secret_scalar);
    117 
    118     curve448_precomputed_scalarmul(p, curve448_precomputed_base, secret_scalar);
    119 
    120     curve448_point_mul_by_ratio_and_encode_like_eddsa(pubkey, p);
     143        ossl_curve448_scalar_halve(secret_scalar, secret_scalar);
     144
     145    ossl_curve448_precomputed_scalarmul(p, ossl_curve448_precomputed_base,
     146                                        secret_scalar);
     147
     148    ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(pubkey, p);
    121149
    122150    /* Cleanup */
    123     curve448_scalar_destroy(secret_scalar);
    124     curve448_point_destroy(p);
     151    ossl_curve448_scalar_destroy(secret_scalar);
     152    ossl_curve448_point_destroy(p);
    125153    OPENSSL_cleanse(secret_scalar_ser, sizeof(secret_scalar_ser));
    126154
     
    128156}
    129157
    130 c448_error_t c448_ed448_sign(
    131                         uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
    132                         const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
    133                         const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
    134                         const uint8_t *message, size_t message_len,
    135                         uint8_t prehashed, const uint8_t *context,
    136                         size_t context_len)
     158c448_error_t
     159ossl_c448_ed448_sign(OSSL_LIB_CTX *ctx,
     160                     uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
     161                     const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
     162                     const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
     163                     const uint8_t *message, size_t message_len,
     164                     uint8_t prehashed, const uint8_t *context,
     165                     size_t context_len, const char *propq)
    137166{
    138167    curve448_scalar_t secret_scalar;
     
    149178    {
    150179        /*
    151          * Schedule the secret key, First EDDSA_448_PRIVATE_BYTES is serialised
     180         * Schedule the secret key, First EDDSA_448_PRIVATE_BYTES is serialized
    152181         * secret scalar,next EDDSA_448_PRIVATE_BYTES bytes is the seed.
    153182         */
    154183        uint8_t expanded[EDDSA_448_PRIVATE_BYTES * 2];
    155184
    156         if (!oneshot_hash(expanded, sizeof(expanded), privkey,
    157                           EDDSA_448_PRIVATE_BYTES))
     185        if (!oneshot_hash(ctx, expanded, sizeof(expanded), privkey,
     186                          EDDSA_448_PRIVATE_BYTES, propq))
    158187            goto err;
    159188        clamp(expanded);
    160         curve448_scalar_decode_long(secret_scalar, expanded,
    161                                     EDDSA_448_PRIVATE_BYTES);
     189        ossl_curve448_scalar_decode_long(secret_scalar, expanded,
     190                                         EDDSA_448_PRIVATE_BYTES);
    162191
    163192        /* Hash to create the nonce */
    164         if (!hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
     193        if (!hash_init_with_dom(ctx, hashctx, prehashed, 0, context,
     194                                context_len, propq)
    165195                || !EVP_DigestUpdate(hashctx,
    166196                                     expanded + EDDSA_448_PRIVATE_BYTES,
     
    179209        if (!EVP_DigestFinalXOF(hashctx, nonce, sizeof(nonce)))
    180210            goto err;
    181         curve448_scalar_decode_long(nonce_scalar, nonce, sizeof(nonce));
     211        ossl_curve448_scalar_decode_long(nonce_scalar, nonce, sizeof(nonce));
    182212        OPENSSL_cleanse(nonce, sizeof(nonce));
    183213    }
     
    188218        curve448_point_t p;
    189219
    190         curve448_scalar_halve(nonce_scalar_2, nonce_scalar);
     220        ossl_curve448_scalar_halve(nonce_scalar_2, nonce_scalar);
    191221        for (c = 2; c < C448_EDDSA_ENCODE_RATIO; c <<= 1)
    192             curve448_scalar_halve(nonce_scalar_2, nonce_scalar_2);
    193 
    194         curve448_precomputed_scalarmul(p, curve448_precomputed_base,
    195                                        nonce_scalar_2);
    196         curve448_point_mul_by_ratio_and_encode_like_eddsa(nonce_point, p);
    197         curve448_point_destroy(p);
    198         curve448_scalar_destroy(nonce_scalar_2);
     222            ossl_curve448_scalar_halve(nonce_scalar_2, nonce_scalar_2);
     223
     224        ossl_curve448_precomputed_scalarmul(p, ossl_curve448_precomputed_base,
     225                                            nonce_scalar_2);
     226        ossl_curve448_point_mul_by_ratio_and_encode_like_eddsa(nonce_point, p);
     227        ossl_curve448_point_destroy(p);
     228        ossl_curve448_scalar_destroy(nonce_scalar_2);
    199229    }
    200230
     
    203233
    204234        /* Compute the challenge */
    205         if (!hash_init_with_dom(hashctx, prehashed, 0, context, context_len)
     235        if (!hash_init_with_dom(ctx, hashctx, prehashed, 0, context, context_len,
     236                                propq)
    206237                || !EVP_DigestUpdate(hashctx, nonce_point, sizeof(nonce_point))
    207238                || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
     
    210241            goto err;
    211242
    212         curve448_scalar_decode_long(challenge_scalar, challenge,
    213                                     sizeof(challenge));
     243        ossl_curve448_scalar_decode_long(challenge_scalar, challenge,
     244                                         sizeof(challenge));
    214245        OPENSSL_cleanse(challenge, sizeof(challenge));
    215246    }
    216247
    217     curve448_scalar_mul(challenge_scalar, challenge_scalar, secret_scalar);
    218     curve448_scalar_add(challenge_scalar, challenge_scalar, nonce_scalar);
     248    ossl_curve448_scalar_mul(challenge_scalar, challenge_scalar, secret_scalar);
     249    ossl_curve448_scalar_add(challenge_scalar, challenge_scalar, nonce_scalar);
    219250
    220251    OPENSSL_cleanse(signature, EDDSA_448_SIGNATURE_BYTES);
    221252    memcpy(signature, nonce_point, sizeof(nonce_point));
    222     curve448_scalar_encode(&signature[EDDSA_448_PUBLIC_BYTES],
    223                            challenge_scalar);
    224 
    225     curve448_scalar_destroy(secret_scalar);
    226     curve448_scalar_destroy(nonce_scalar);
    227     curve448_scalar_destroy(challenge_scalar);
     253    ossl_curve448_scalar_encode(&signature[EDDSA_448_PUBLIC_BYTES],
     254                                challenge_scalar);
     255
     256    ossl_curve448_scalar_destroy(secret_scalar);
     257    ossl_curve448_scalar_destroy(nonce_scalar);
     258    ossl_curve448_scalar_destroy(challenge_scalar);
    228259
    229260    ret = C448_SUCCESS;
     
    233264}
    234265
    235 c448_error_t c448_ed448_sign_prehash(
     266c448_error_t
     267ossl_c448_ed448_sign_prehash(
     268                        OSSL_LIB_CTX *ctx,
    236269                        uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
    237270                        const uint8_t privkey[EDDSA_448_PRIVATE_BYTES],
    238271                        const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
    239272                        const uint8_t hash[64], const uint8_t *context,
    240                         size_t context_len)
    241 {
    242     return c448_ed448_sign(signature, privkey, pubkey, hash, 64, 1, context,
    243                            context_len);
    244 }
    245 
    246 c448_error_t c448_ed448_verify(
     273                        size_t context_len, const char *propq)
     274{
     275    return ossl_c448_ed448_sign(ctx, signature, privkey, pubkey, hash, 64, 1,
     276                                context, context_len, propq);
     277}
     278
     279c448_error_t
     280ossl_c448_ed448_verify(
     281                    OSSL_LIB_CTX *ctx,
    247282                    const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
    248283                    const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
    249284                    const uint8_t *message, size_t message_len,
    250285                    uint8_t prehashed, const uint8_t *context,
    251                     uint8_t context_len)
     286                    uint8_t context_len, const char *propq)
    252287{
    253288    curve448_point_t pk_point, r_point;
     
    281316
    282317    error =
    283         curve448_point_decode_like_eddsa_and_mul_by_ratio(pk_point, pubkey);
     318        ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(pk_point, pubkey);
    284319
    285320    if (C448_SUCCESS != error)
     
    287322
    288323    error =
    289         curve448_point_decode_like_eddsa_and_mul_by_ratio(r_point, signature);
     324        ossl_curve448_point_decode_like_eddsa_and_mul_by_ratio(r_point, signature);
    290325    if (C448_SUCCESS != error)
    291326        return error;
     
    297332
    298333        if (hashctx == NULL
    299                 || !hash_init_with_dom(hashctx, prehashed, 0, context,
    300                                        context_len)
     334                || !hash_init_with_dom(ctx, hashctx, prehashed, 0, context,
     335                                       context_len, propq)
    301336                || !EVP_DigestUpdate(hashctx, signature, EDDSA_448_PUBLIC_BYTES)
    302337                || !EVP_DigestUpdate(hashctx, pubkey, EDDSA_448_PUBLIC_BYTES)
     
    308343
    309344        EVP_MD_CTX_free(hashctx);
    310         curve448_scalar_decode_long(challenge_scalar, challenge,
    311                                     sizeof(challenge));
     345        ossl_curve448_scalar_decode_long(challenge_scalar, challenge,
     346                                         sizeof(challenge));
    312347        OPENSSL_cleanse(challenge, sizeof(challenge));
    313348    }
    314     curve448_scalar_sub(challenge_scalar, curve448_scalar_zero,
    315                         challenge_scalar);
    316 
    317     curve448_scalar_decode_long(response_scalar,
    318                                 &signature[EDDSA_448_PUBLIC_BYTES],
    319                                 EDDSA_448_PRIVATE_BYTES);
     349    ossl_curve448_scalar_sub(challenge_scalar, ossl_curve448_scalar_zero,
     350                             challenge_scalar);
     351
     352    ossl_curve448_scalar_decode_long(response_scalar,
     353                                     &signature[EDDSA_448_PUBLIC_BYTES],
     354                                     EDDSA_448_PRIVATE_BYTES);
    320355
    321356    /* pk_point = -c(x(P)) + (cx + k)G = kG */
    322     curve448_base_double_scalarmul_non_secret(pk_point,
    323                                               response_scalar,
    324                                               pk_point, challenge_scalar);
    325     return c448_succeed_if(curve448_point_eq(pk_point, r_point));
    326 }
    327 
    328 c448_error_t c448_ed448_verify_prehash(
     357    ossl_curve448_base_double_scalarmul_non_secret(pk_point,
     358                                                   response_scalar,
     359                                                   pk_point, challenge_scalar);
     360    return c448_succeed_if(ossl_curve448_point_eq(pk_point, r_point));
     361}
     362
     363c448_error_t
     364ossl_c448_ed448_verify_prehash(
     365                    OSSL_LIB_CTX *ctx,
    329366                    const uint8_t signature[EDDSA_448_SIGNATURE_BYTES],
    330367                    const uint8_t pubkey[EDDSA_448_PUBLIC_BYTES],
    331368                    const uint8_t hash[64], const uint8_t *context,
    332                     uint8_t context_len)
    333 {
    334     return c448_ed448_verify(signature, pubkey, hash, 64, 1, context,
    335                              context_len);
    336 }
    337 
    338 int ED448_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
    339                const uint8_t public_key[57], const uint8_t private_key[57],
    340                const uint8_t *context, size_t context_len)
    341 {
    342     return c448_ed448_sign(out_sig, private_key, public_key, message,
    343                            message_len, 0, context, context_len)
    344         == C448_SUCCESS;
    345 }
    346 
    347 int ED448_verify(const uint8_t *message, size_t message_len,
    348                  const uint8_t signature[114], const uint8_t public_key[57],
    349                  const uint8_t *context, size_t context_len)
    350 {
    351     return c448_ed448_verify(signature, public_key, message, message_len, 0,
    352                              context, (uint8_t)context_len) == C448_SUCCESS;
    353 }
    354 
    355 int ED448ph_sign(uint8_t *out_sig, const uint8_t hash[64],
    356                  const uint8_t public_key[57], const uint8_t private_key[57],
    357                  const uint8_t *context, size_t context_len)
    358 {
    359     return c448_ed448_sign_prehash(out_sig, private_key, public_key, hash,
    360                                    context, context_len) == C448_SUCCESS;
    361 
    362 }
    363 
    364 int ED448ph_verify(const uint8_t hash[64], const uint8_t signature[114],
    365                    const uint8_t public_key[57], const uint8_t *context,
    366                    size_t context_len)
    367 {
    368     return c448_ed448_verify_prehash(signature, public_key, hash, context,
    369                                      (uint8_t)context_len) == C448_SUCCESS;
    370 }
    371 
    372 int ED448_public_from_private(uint8_t out_public_key[57],
    373                               const uint8_t private_key[57])
    374 {
    375     return c448_ed448_derive_public_key(out_public_key, private_key)
    376         == C448_SUCCESS;
    377 }
     369                    uint8_t context_len, const char *propq)
     370{
     371    return ossl_c448_ed448_verify(ctx, signature, pubkey, hash, 64, 1, context,
     372                                  context_len, propq);
     373}
     374
     375int
     376ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t *message,
     377                size_t message_len, const uint8_t public_key[57],
     378                const uint8_t private_key[57], const uint8_t *context,
     379                size_t context_len, const char *propq)
     380{
     381    return ossl_c448_ed448_sign(ctx, out_sig, private_key, public_key, message,
     382                                message_len, 0, context, context_len,
     383                                propq) == C448_SUCCESS;
     384}
     385
     386int
     387ossl_ed448_verify(OSSL_LIB_CTX *ctx, const uint8_t *message, size_t message_len,
     388                  const uint8_t signature[114], const uint8_t public_key[57],
     389                  const uint8_t *context, size_t context_len, const char *propq)
     390{
     391    return ossl_c448_ed448_verify(ctx, signature, public_key, message,
     392                                  message_len, 0, context, (uint8_t)context_len,
     393                                  propq) == C448_SUCCESS;
     394}
     395
     396int
     397ossl_ed448ph_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t hash[64],
     398                  const uint8_t public_key[57], const uint8_t private_key[57],
     399                  const uint8_t *context, size_t context_len, const char *propq)
     400{
     401    return ossl_c448_ed448_sign_prehash(ctx, out_sig, private_key, public_key,
     402                                        hash, context, context_len,
     403                                        propq) == C448_SUCCESS;
     404}
     405
     406int
     407ossl_ed448ph_verify(OSSL_LIB_CTX *ctx, const uint8_t hash[64],
     408                    const uint8_t signature[114], const uint8_t public_key[57],
     409                    const uint8_t *context, size_t context_len,
     410                    const char *propq)
     411{
     412    return ossl_c448_ed448_verify_prehash(ctx, signature, public_key, hash,
     413                                          context, (uint8_t)context_len,
     414                                          propq) == C448_SUCCESS;
     415}
     416
     417int
     418ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
     419                               const uint8_t private_key[57], const char *propq)
     420{
     421    return ossl_c448_ed448_derive_public_key(ctx, out_public_key, private_key,
     422                                             propq) == C448_SUCCESS;
     423}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/f_generic.c

    r91772 r94082  
    11/*
    2  * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2015-2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    1919
    2020/* Serialize to wire format. */
    21 void gf_serialize(uint8_t serial[SER_BYTES], const gf x, int with_hibit)
     21void gf_serialize(uint8_t *serial, const gf x, int with_hibit)
    2222{
    2323    unsigned int j = 0, fill = 0;
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/field.h

    r91772 r94082  
    11/*
    2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2014 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    6767                      uint8_t hi_nmask);
    6868
    69 # include "f_impl.h"            /* Bring in the inline implementations */
    7069
    7170# define LIMBPERM(i) (i)
    72 # define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
     71# if (ARCH_WORD_BITS == 32)
     72#  include "arch_32/f_impl.h"    /* Bring in the inline implementations */
     73#  define LIMB_MASK(i) (((1)<<LIMB_PLACE_VALUE(i))-1)
     74# elif (ARCH_WORD_BITS == 64)
     75#  include "arch_64/f_impl.h"    /* Bring in the inline implementations */
     76#  define LIMB_MASK(i) (((1ULL)<<LIMB_PLACE_VALUE(i))-1)
     77# endif
    7378
    7479static const gf ZERO = {{{0}}}, ONE = {{{1}}};
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/point_448.h

    r91772 r94082  
    11/*
    2  * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2015-2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    7070
    7171/* A scalar equal to 1. */
    72 extern const curve448_scalar_t curve448_scalar_one;
     72extern const curve448_scalar_t ossl_curve448_scalar_one;
    7373
    7474/* A scalar equal to 0. */
    75 extern const curve448_scalar_t curve448_scalar_zero;
     75extern const curve448_scalar_t ossl_curve448_scalar_zero;
    7676
    7777/* The identity point on the curve. */
    78 extern const curve448_point_t curve448_point_identity;
     78extern const curve448_point_t ossl_curve448_point_identity;
    7979
    8080/* Precomputed table for the base point on the curve. */
    81 extern const struct curve448_precomputed_s *curve448_precomputed_base;
    82 extern const niels_t *curve448_wnaf_base;
     81extern const struct curve448_precomputed_s *ossl_curve448_precomputed_base;
     82extern const niels_t *ossl_curve448_wnaf_base;
    8383
    8484/*
     
    9393 * modulo that modulus.
    9494 */
    95 c448_error_t curve448_scalar_decode(curve448_scalar_t out,
    96                                     const unsigned char ser[C448_SCALAR_BYTES]);
     95c448_error_t
     96ossl_curve448_scalar_decode(curve448_scalar_t out,
     97                            const unsigned char ser[C448_SCALAR_BYTES]);
    9798
    9899/*
     
    103104 * out (out): Deserialized form.
    104105 */
    105 void curve448_scalar_decode_long(curve448_scalar_t out,
     106void
     107ossl_curve448_scalar_decode_long(curve448_scalar_t out,
    106108                                 const unsigned char *ser, size_t ser_len);
    107109
     
    112114 * s (in): Deserialized scalar.
    113115 */
    114 void curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
     116void
     117ossl_curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
    115118                            const curve448_scalar_t s);
    116119
     
    122125 * out (out): a+b.
    123126 */
    124 void curve448_scalar_add(curve448_scalar_t out,
     127void
     128ossl_curve448_scalar_add(curve448_scalar_t out,
    125129                         const curve448_scalar_t a, const curve448_scalar_t b);
    126130
     
    131135 * out (out): a-b.
    132136 */
    133 void curve448_scalar_sub(curve448_scalar_t out,
     137void
     138ossl_curve448_scalar_sub(curve448_scalar_t out,
    134139                         const curve448_scalar_t a, const curve448_scalar_t b);
    135140
     
    141146 * out (out): a*b.
    142147 */
    143 void curve448_scalar_mul(curve448_scalar_t out,
     148void
     149ossl_curve448_scalar_mul(curve448_scalar_t out,
    144150                         const curve448_scalar_t a, const curve448_scalar_t b);
    145151
     
    150156* out (out): a/2.
    151157*/
    152 void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
     158void
     159ossl_curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a);
    153160
    154161/*
     
    189196 * C448_FALSE: The points are not equal.
    190197 */
    191 __owur c448_bool_t curve448_point_eq(const curve448_point_t a,
    192                                      const curve448_point_t b);
     198__owur c448_bool_t
     199ossl_curve448_point_eq(const curve448_point_t a,
     200                       const curve448_point_t b);
    193201
    194202/*
     
    199207 * a (in): A point.
    200208 */
    201 void curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
     209void
     210ossl_curve448_point_double(curve448_point_t two_a, const curve448_point_t a);
    202211
    203212/*
     
    214223 * small subgroup.
    215224 */
    216 __owur c448_error_t x448_int(uint8_t out[X448_PUBLIC_BYTES],
    217                              const uint8_t base[X448_PUBLIC_BYTES],
    218                              const uint8_t scalar[X448_PRIVATE_BYTES]);
     225__owur c448_error_t
     226ossl_x448_int(uint8_t out[X448_PUBLIC_BYTES],
     227              const uint8_t base[X448_PUBLIC_BYTES],
     228              const uint8_t scalar[X448_PRIVATE_BYTES]);
    219229
    220230/*
     
    237247 * p (in): The point to be scaled and encoded.
    238248 */
    239 void curve448_point_mul_by_ratio_and_encode_like_x448(
     249void
     250ossl_curve448_point_mul_by_ratio_and_encode_like_x448(
    240251                                        uint8_t out[X448_PUBLIC_BYTES],
    241252                                        const curve448_point_t p);
     
    248259 * scalar (in): The scalar to multiply by.
    249260 */
    250 void x448_derive_public_key(uint8_t out[X448_PUBLIC_BYTES],
     261void
     262ossl_x448_derive_public_key(uint8_t out[X448_PUBLIC_BYTES],
    251263                            const uint8_t scalar[X448_PRIVATE_BYTES]);
    252264
     
    258270 * scalar (in): The scalar to multiply by.
    259271 */
    260 void curve448_precomputed_scalarmul(curve448_point_t scaled,
     272void
     273ossl_curve448_precomputed_scalarmul(curve448_point_t scaled,
    261274                                    const curve448_precomputed_s * base,
    262275                                    const curve448_scalar_t scalar);
     
    277290 * It is designed for signature verification.
    278291 */
    279 void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
     292void
     293ossl_curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
    280294                                               const curve448_scalar_t scalar1,
    281295                                               const curve448_point_t base2,
     
    291305 * C448_FALSE The point is invalid.
    292306 */
    293 __owur c448_bool_t curve448_point_valid(const curve448_point_t to_test);
     307__owur c448_bool_t
     308ossl_curve448_point_valid(const curve448_point_t to_test);
    294309
    295310/* Overwrite scalar with zeros. */
    296 void curve448_scalar_destroy(curve448_scalar_t scalar);
     311void ossl_curve448_scalar_destroy(curve448_scalar_t scalar);
    297312
    298313/* Overwrite point with zeros. */
    299 void curve448_point_destroy(curve448_point_t point);
     314void ossl_curve448_point_destroy(curve448_point_t point);
    300315
    301316#endif                          /* OSSL_CRYPTO_EC_CURVE448_POINT_448_H */
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/scalar.c

    r91772 r94082  
    11/*
    2  * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2015-2016 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    3939#define WBITS C448_WORD_BITS   /* NB this may be different from ARCH_WORD_BITS */
    4040
    41 const curve448_scalar_t curve448_scalar_one = {{{1}}};
    42 const curve448_scalar_t curve448_scalar_zero = {{{0}}};
     41const curve448_scalar_t ossl_curve448_scalar_one = {{{1}}};
     42const curve448_scalar_t ossl_curve448_scalar_zero = {{{0}}};
    4343
    4444/*
     
    107107}
    108108
    109 void curve448_scalar_mul(curve448_scalar_t out, const curve448_scalar_t a,
    110                          const curve448_scalar_t b)
     109void ossl_curve448_scalar_mul(curve448_scalar_t out, const curve448_scalar_t a,
     110                              const curve448_scalar_t b)
    111111{
    112112    sc_montmul(out, a, b);
     
    114114}
    115115
    116 void curve448_scalar_sub(curve448_scalar_t out, const curve448_scalar_t a,
     116void
     117ossl_curve448_scalar_sub(curve448_scalar_t out, const curve448_scalar_t a,
    117118                         const curve448_scalar_t b)
    118119{
     
    120121}
    121122
    122 void curve448_scalar_add(curve448_scalar_t out, const curve448_scalar_t a,
     123void
     124ossl_curve448_scalar_add(curve448_scalar_t out, const curve448_scalar_t a,
    123125                         const curve448_scalar_t b)
    124126{
     
    149151}
    150152
    151 c448_error_t curve448_scalar_decode(
    152                                 curve448_scalar_t s,
    153                                 const unsigned char ser[C448_SCALAR_BYTES])
     153c448_error_t
     154ossl_curve448_scalar_decode(curve448_scalar_t s,
     155                            const unsigned char ser[C448_SCALAR_BYTES])
    154156{
    155157    unsigned int i;
     
    161163    /* Here accum == 0 or -1 */
    162164
    163     curve448_scalar_mul(s, s, curve448_scalar_one); /* ham-handed reduce */
     165    ossl_curve448_scalar_mul(s, s, ossl_curve448_scalar_one); /* ham-handed reduce */
    164166
    165167    return c448_succeed_if(~word_is_zero((uint32_t)accum));
    166168}
    167169
    168 void curve448_scalar_destroy(curve448_scalar_t scalar)
     170void ossl_curve448_scalar_destroy(curve448_scalar_t scalar)
    169171{
    170172    OPENSSL_cleanse(scalar, sizeof(curve448_scalar_t));
    171173}
    172174
    173 void curve448_scalar_decode_long(curve448_scalar_t s,
     175void
     176ossl_curve448_scalar_decode_long(curve448_scalar_t s,
    174177                                 const unsigned char *ser, size_t ser_len)
    175178{
     
    178181
    179182    if (ser_len == 0) {
    180         curve448_scalar_copy(s, curve448_scalar_zero);
     183        curve448_scalar_copy(s, ossl_curve448_scalar_zero);
    181184        return;
    182185    }
     
    191194        assert(i == 0);
    192195        /* ham-handed reduce */
    193         curve448_scalar_mul(s, t1, curve448_scalar_one);
    194         curve448_scalar_destroy(t1);
     196        ossl_curve448_scalar_mul(s, t1, ossl_curve448_scalar_one);
     197        ossl_curve448_scalar_destroy(t1);
    195198        return;
    196199    }
     
    199202        i -= C448_SCALAR_BYTES;
    200203        sc_montmul(t1, t1, sc_r2);
    201         (void)curve448_scalar_decode(t2, ser + i);
    202         curve448_scalar_add(t1, t1, t2);
     204        (void)ossl_curve448_scalar_decode(t2, ser + i);
     205        ossl_curve448_scalar_add(t1, t1, t2);
    203206    }
    204207
    205208    curve448_scalar_copy(s, t1);
    206     curve448_scalar_destroy(t1);
    207     curve448_scalar_destroy(t2);
    208 }
    209 
    210 void curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
     209    ossl_curve448_scalar_destroy(t1);
     210    ossl_curve448_scalar_destroy(t2);
     211}
     212
     213void
     214ossl_curve448_scalar_encode(unsigned char ser[C448_SCALAR_BYTES],
    211215                            const curve448_scalar_t s)
    212216{
     
    219223}
    220224
    221 void curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a)
     225void
     226ossl_curve448_scalar_halve(curve448_scalar_t out, const curve448_scalar_t a)
    222227{
    223228    c448_word_t mask = 0 - (a->limb[0] & 1);
  • trunk/src/libs/openssl-3.0.1/crypto/ec/curve448/word.h

    r91772 r94082  
    11/*
    2  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright 2014 Cryptography Research, Inc.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    1818# include <stdlib.h>
    1919# include <openssl/e_os2.h>
    20 # include "arch_intrinsics.h"
    2120# include "curve448utils.h"
     21
     22# ifdef INT128_MAX
     23#  include "arch_64/arch_intrinsics.h"
     24# else
     25#  include "arch_32/arch_intrinsics.h"
     26# endif
    2227
    2328# if (ARCH_WORD_BITS == 64)
    2429typedef uint64_t word_t, mask_t;
    25 typedef __uint128_t dword_t;
     30typedef uint128_t dword_t;
    2631typedef int32_t hsword_t;
    2732typedef int64_t sword_t;
    28 typedef __int128_t dsword_t;
     33typedef int128_t dsword_t;
    2934# elif (ARCH_WORD_BITS == 32)
    3035typedef uint32_t word_t, mask_t;
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec2_oct.c

    r91772 r94082  
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
    88 * https://www.openssl.org/source/license.html
    99 */
     10
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
    1016
    1117#include <openssl/err.h>
     
    3137 * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
    3238 */
    33 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
    34                                               EC_POINT *point,
    35                                               const BIGNUM *x_, int y_bit,
    36                                               BN_CTX *ctx)
     39int ossl_ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
     40                                                   EC_POINT *point,
     41                                                   const BIGNUM *x_, int y_bit,
     42                                                   BN_CTX *ctx)
    3743{
    38     BN_CTX *new_ctx = NULL;
    3944    BIGNUM *tmp, *x, *y, *z;
    4045    int ret = 0, z0;
    41 
    42     /* clear error queue */
    43     ERR_clear_error();
     46#ifndef FIPS_MODULE
     47    BN_CTX *new_ctx = NULL;
    4448
    4549    if (ctx == NULL) {
     
    4852            return 0;
    4953    }
     54#endif
    5055
    5156    y_bit = (y_bit != 0) ? 1 : 0;
     
    7378        if (!BN_GF2m_add(tmp, x, tmp))
    7479            goto err;
     80        ERR_set_mark();
    7581        if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
     82#ifndef FIPS_MODULE
    7683            unsigned long err = ERR_peek_last_error();
    7784
    7885            if (ERR_GET_LIB(err) == ERR_LIB_BN
    7986                && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
    80                 ERR_clear_error();
    81                 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
    82                       EC_R_INVALID_COMPRESSED_POINT);
     87                ERR_pop_to_mark();
     88                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
    8389            } else
    84                 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
    85                       ERR_R_BN_LIB);
    86             goto err;
    87         }
     90#endif
     91            {
     92                ERR_clear_last_mark();
     93                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
     94            }
     95            goto err;
     96        }
     97        ERR_clear_last_mark();
    8898        z0 = (BN_is_odd(z)) ? 1 : 0;
    8999        if (!group->meth->field_mul(group, y, x, z, ctx))
     
    102112 err:
    103113    BN_CTX_end(ctx);
     114#ifndef FIPS_MODULE
    104115    BN_CTX_free(new_ctx);
     116#endif
    105117    return ret;
    106118}
     
    111123 * an error will be returned.
    112124 */
    113 size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
    114                                 point_conversion_form_t form,
    115                                 unsigned char *buf, size_t len, BN_CTX *ctx)
     125size_t ossl_ec_GF2m_simple_point2oct(const EC_GROUP *group,
     126                                     const EC_POINT *point,
     127                                     point_conversion_form_t form,
     128                                     unsigned char *buf, size_t len, BN_CTX *ctx)
    116129{
    117130    size_t ret;
    118     BN_CTX *new_ctx = NULL;
    119131    int used_ctx = 0;
    120132    BIGNUM *x, *y, *yxi;
    121133    size_t field_len, i, skip;
     134#ifndef FIPS_MODULE
     135    BN_CTX *new_ctx = NULL;
     136#endif
    122137
    123138    if ((form != POINT_CONVERSION_COMPRESSED)
    124139        && (form != POINT_CONVERSION_UNCOMPRESSED)
    125140        && (form != POINT_CONVERSION_HYBRID)) {
    126         ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
     141        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
    127142        goto err;
    128143    }
     
    132147        if (buf != NULL) {
    133148            if (len < 1) {
    134                 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
     149                ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
    135150                return 0;
    136151            }
     
    149164    if (buf != NULL) {
    150165        if (len < ret) {
    151             ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
    152             goto err;
    153         }
    154 
     166            ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
     167            goto err;
     168        }
     169
     170#ifndef FIPS_MODULE
    155171        if (ctx == NULL) {
    156172            ctx = new_ctx = BN_CTX_new();
     
    158174                return 0;
    159175        }
     176#endif
    160177
    161178        BN_CTX_start(ctx);
     
    182199        skip = field_len - BN_num_bytes(x);
    183200        if (skip > field_len) {
    184             ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
     201            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    185202            goto err;
    186203        }
     
    192209        i += skip;
    193210        if (i != 1 + field_len) {
    194             ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
     211            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    195212            goto err;
    196213        }
     
    200217            skip = field_len - BN_num_bytes(y);
    201218            if (skip > field_len) {
    202                 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
     219                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    203220                goto err;
    204221            }
     
    212229
    213230        if (i != ret) {
    214             ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
     231            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    215232            goto err;
    216233        }
     
    219236    if (used_ctx)
    220237        BN_CTX_end(ctx);
     238#ifndef FIPS_MODULE
    221239    BN_CTX_free(new_ctx);
     240#endif
    222241    return ret;
    223242
     
    225244    if (used_ctx)
    226245        BN_CTX_end(ctx);
     246#ifndef FIPS_MODULE
    227247    BN_CTX_free(new_ctx);
     248#endif
    228249    return 0;
    229250}
     
    233254 * simple implementation only uses affine coordinates.
    234255 */
    235 int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
    236                              const unsigned char *buf, size_t len,
    237                              BN_CTX *ctx)
     256int ossl_ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
     257                                  const unsigned char *buf, size_t len,
     258                                  BN_CTX *ctx)
    238259{
    239260    point_conversion_form_t form;
    240261    int y_bit, m;
    241     BN_CTX *new_ctx = NULL;
    242262    BIGNUM *x, *y, *yxi;
    243263    size_t field_len, enc_len;
    244264    int ret = 0;
     265#ifndef FIPS_MODULE
     266    BN_CTX *new_ctx = NULL;
     267#endif
    245268
    246269    if (len == 0) {
    247         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
     270        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
    248271        return 0;
    249272    }
     
    266289        && (form != POINT_CONVERSION_UNCOMPRESSED)
    267290        && (form != POINT_CONVERSION_HYBRID)) {
    268         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     291        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    269292        return 0;
    270293    }
    271294    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
    272         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     295        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    273296        return 0;
    274297    }
     
    277300    if (form == 0) {
    278301        if (len != 1) {
    279             ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     302            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    280303            return 0;
    281304        }
     
    291314
    292315    if (len != enc_len) {
    293         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     316        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    294317        return 0;
    295318    }
    296319
     320#ifndef FIPS_MODULE
    297321    if (ctx == NULL) {
    298322        ctx = new_ctx = BN_CTX_new();
     
    300324            return 0;
    301325    }
     326#endif
    302327
    303328    BN_CTX_start(ctx);
     
    311336        goto err;
    312337    if (BN_num_bits(x) > m) {
    313         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     338        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    314339        goto err;
    315340    }
     
    322347            goto err;
    323348        if (BN_num_bits(y) > m) {
    324             ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     349            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    325350            goto err;
    326351        }
     
    333358            if (BN_is_zero(x)) {
    334359                if (y_bit != 0) {
    335                     ECerr(ERR_LIB_EC, EC_R_INVALID_ENCODING);
     360                    ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    336361                    goto err;
    337362                }
     
    340365                    goto err;
    341366                if (y_bit != BN_is_odd(yxi)) {
    342                     ECerr(ERR_LIB_EC, EC_R_INVALID_ENCODING);
     367                    ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    343368                    goto err;
    344369                }
     
    358383 err:
    359384    BN_CTX_end(ctx);
     385#ifndef FIPS_MODULE
    360386    BN_CTX_free(new_ctx);
     387#endif
    361388    return ret;
    362389}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec2_smpl.c

    r91772 r94082  
    11/*
    2  * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    99 */
    1010
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
     16
    1117#include <openssl/err.h>
    1218
     
    2026 * are handled by EC_GROUP_new.
    2127 */
    22 int ec_GF2m_simple_group_init(EC_GROUP *group)
     28int ossl_ec_GF2m_simple_group_init(EC_GROUP *group)
    2329{
    2430    group->field = BN_new();
     
    3945 * handled by EC_GROUP_free.
    4046 */
    41 void ec_GF2m_simple_group_finish(EC_GROUP *group)
     47void ossl_ec_GF2m_simple_group_finish(EC_GROUP *group)
    4248{
    4349    BN_free(group->field);
     
    5056 * members are handled by EC_GROUP_clear_free.
    5157 */
    52 void ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
     58void ossl_ec_GF2m_simple_group_clear_finish(EC_GROUP *group)
    5359{
    5460    BN_clear_free(group->field);
     
    6773 * handled by EC_GROUP_copy.
    6874 */
    69 int ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
     75int ossl_ec_GF2m_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
    7076{
    7177    if (!BN_copy(dest->field, src->field))
     
    9399
    94100/* Set the curve parameters of an EC_GROUP structure. */
    95 int ec_GF2m_simple_group_set_curve(EC_GROUP *group,
    96                                    const BIGNUM *p, const BIGNUM *a,
    97                                    const BIGNUM *b, BN_CTX *ctx)
     101int ossl_ec_GF2m_simple_group_set_curve(EC_GROUP *group,
     102                                        const BIGNUM *p, const BIGNUM *a,
     103                                        const BIGNUM *b, BN_CTX *ctx)
    98104{
    99105    int ret = 0, i;
     
    104110    i = BN_GF2m_poly2arr(group->field, group->poly, 6) - 1;
    105111    if ((i != 5) && (i != 3)) {
    106         ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, EC_R_UNSUPPORTED_FIELD);
     112        ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
    107113        goto err;
    108114    }
     
    133139 * then there values will not be set but the method will return with success.
    134140 */
    135 int ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
    136                                    BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
     141int ossl_ec_GF2m_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
     142                                        BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
    137143{
    138144    int ret = 0;
     
    163169 * m.
    164170 */
    165 int ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
     171int ossl_ec_GF2m_simple_group_get_degree(const EC_GROUP *group)
    166172{
    167173    return BN_num_bits(group->field) - 1;
     
    172178 * elliptic curve <=> b != 0 (mod p)
    173179 */
    174 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
    175                                             BN_CTX *ctx)
     180int ossl_ec_GF2m_simple_group_check_discriminant(const EC_GROUP *group,
     181                                                 BN_CTX *ctx)
    176182{
    177183    int ret = 0;
    178184    BIGNUM *b;
     185#ifndef FIPS_MODULE
    179186    BN_CTX *new_ctx = NULL;
    180187
     
    182189        ctx = new_ctx = BN_CTX_new();
    183190        if (ctx == NULL) {
    184             ECerr(EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT,
    185                   ERR_R_MALLOC_FAILURE);
     191            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    186192            goto err;
    187193        }
    188194    }
     195#endif
    189196    BN_CTX_start(ctx);
    190197    b = BN_CTX_get(ctx);
     
    206213 err:
    207214    BN_CTX_end(ctx);
     215#ifndef FIPS_MODULE
    208216    BN_CTX_free(new_ctx);
     217#endif
    209218    return ret;
    210219}
    211220
    212221/* Initializes an EC_POINT. */
    213 int ec_GF2m_simple_point_init(EC_POINT *point)
     222int ossl_ec_GF2m_simple_point_init(EC_POINT *point)
    214223{
    215224    point->X = BN_new();
     
    227236
    228237/* Frees an EC_POINT. */
    229 void ec_GF2m_simple_point_finish(EC_POINT *point)
     238void ossl_ec_GF2m_simple_point_finish(EC_POINT *point)
    230239{
    231240    BN_free(point->X);
     
    235244
    236245/* Clears and frees an EC_POINT. */
    237 void ec_GF2m_simple_point_clear_finish(EC_POINT *point)
     246void ossl_ec_GF2m_simple_point_clear_finish(EC_POINT *point)
    238247{
    239248    BN_clear_free(point->X);
     
    247256 * initialized.
    248257 */
    249 int ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
     258int ossl_ec_GF2m_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
    250259{
    251260    if (!BN_copy(dest->X, src->X))
     
    265274 * represented by having Z=0.
    266275 */
    267 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group,
    268                                          EC_POINT *point)
     276int ossl_ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *group,
     277                                              EC_POINT *point)
    269278{
    270279    point->Z_is_one = 0;
     
    277286 * the simple implementation only uses affine coordinates.
    278287 */
    279 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group,
    280                                                 EC_POINT *point,
    281                                                 const BIGNUM *x,
    282                                                 const BIGNUM *y, BN_CTX *ctx)
     288int ossl_ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *group,
     289                                                     EC_POINT *point,
     290                                                     const BIGNUM *x,
     291                                                     const BIGNUM *y,
     292                                                     BN_CTX *ctx)
    283293{
    284294    int ret = 0;
    285295    if (x == NULL || y == NULL) {
    286         ECerr(EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES,
    287               ERR_R_PASSED_NULL_PARAMETER);
     296        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    288297        return 0;
    289298    }
     
    309318 * implementation only uses affine coordinates.
    310319 */
    311 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
    312                                                 const EC_POINT *point,
    313                                                 BIGNUM *x, BIGNUM *y,
    314                                                 BN_CTX *ctx)
     320int ossl_ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *group,
     321                                                     const EC_POINT *point,
     322                                                     BIGNUM *x, BIGNUM *y,
     323                                                     BN_CTX *ctx)
    315324{
    316325    int ret = 0;
    317326
    318327    if (EC_POINT_is_at_infinity(group, point)) {
    319         ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
    320               EC_R_POINT_AT_INFINITY);
     328        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
    321329        return 0;
    322330    }
    323331
    324332    if (BN_cmp(point->Z, BN_value_one())) {
    325         ECerr(EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES,
    326               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     333        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    327334        return 0;
    328335    }
     
    347354 * b. Uses algorithm A.10.2 of IEEE P1363.
    348355 */
    349 int ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
    350                        const EC_POINT *b, BN_CTX *ctx)
    351 {
    352     BN_CTX *new_ctx = NULL;
     356int ossl_ec_GF2m_simple_add(const EC_GROUP *group, EC_POINT *r,
     357                            const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
     358{
    353359    BIGNUM *x0, *y0, *x1, *y1, *x2, *y2, *s, *t;
    354360    int ret = 0;
     361#ifndef FIPS_MODULE
     362    BN_CTX *new_ctx = NULL;
     363#endif
    355364
    356365    if (EC_POINT_is_at_infinity(group, a)) {
     
    366375    }
    367376
     377#ifndef FIPS_MODULE
    368378    if (ctx == NULL) {
    369379        ctx = new_ctx = BN_CTX_new();
     
    371381            return 0;
    372382    }
     383#endif
    373384
    374385    BN_CTX_start(ctx);
     
    454465 err:
    455466    BN_CTX_end(ctx);
     467#ifndef FIPS_MODULE
    456468    BN_CTX_free(new_ctx);
     469#endif
    457470    return ret;
    458471}
     
    462475 * A.10.2 of IEEE P1363.
    463476 */
    464 int ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
    465                        BN_CTX *ctx)
    466 {
    467     return ec_GF2m_simple_add(group, r, a, a, ctx);
    468 }
    469 
    470 int ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
     477int ossl_ec_GF2m_simple_dbl(const EC_GROUP *group, EC_POINT *r,
     478                            const EC_POINT *a, BN_CTX *ctx)
     479{
     480    return ossl_ec_GF2m_simple_add(group, r, a, a, ctx);
     481}
     482
     483int ossl_ec_GF2m_simple_invert(const EC_GROUP *group, EC_POINT *point,
     484                               BN_CTX *ctx)
    471485{
    472486    if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
     
    474488        return 1;
    475489
    476     if (!EC_POINT_make_affine(group, point, ctx))
     490    if (group->meth->make_affine == NULL
     491        || !group->meth->make_affine(group, point, ctx))
    477492        return 0;
    478493    return BN_GF2m_add(point->Y, point->X, point->Y);
     
    480495
    481496/* Indicates whether the given point is the point at infinity. */
    482 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *group,
    483                                   const EC_POINT *point)
     497int ossl_ec_GF2m_simple_is_at_infinity(const EC_GROUP *group,
     498                                       const EC_POINT *point)
    484499{
    485500    return BN_is_zero(point->Z);
     
    491506 *      y^2 + x*y = x^3 + a*x^2 + b.
    492507 */
    493 int ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
    494                                BN_CTX *ctx)
     508int ossl_ec_GF2m_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
     509                                    BN_CTX *ctx)
    495510{
    496511    int ret = -1;
    497     BN_CTX *new_ctx = NULL;
    498512    BIGNUM *lh, *y2;
    499513    int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
    500514                      const BIGNUM *, BN_CTX *);
    501515    int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
     516#ifndef FIPS_MODULE
     517    BN_CTX *new_ctx = NULL;
     518#endif
    502519
    503520    if (EC_POINT_is_at_infinity(group, point))
     
    511528        return -1;
    512529
     530#ifndef FIPS_MODULE
    513531    if (ctx == NULL) {
    514532        ctx = new_ctx = BN_CTX_new();
     
    516534            return -1;
    517535    }
     536#endif
    518537
    519538    BN_CTX_start(ctx);
     
    547566 err:
    548567    BN_CTX_end(ctx);
     568#ifndef FIPS_MODULE
    549569    BN_CTX_free(new_ctx);
     570#endif
    550571    return ret;
    551572}
     
    558579 *   1   not equal
    559580 */
    560 int ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
    561                        const EC_POINT *b, BN_CTX *ctx)
     581int ossl_ec_GF2m_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
     582                            const EC_POINT *b, BN_CTX *ctx)
    562583{
    563584    BIGNUM *aX, *aY, *bX, *bY;
     585    int ret = -1;
     586#ifndef FIPS_MODULE
    564587    BN_CTX *new_ctx = NULL;
    565     int ret = -1;
     588#endif
    566589
    567590    if (EC_POINT_is_at_infinity(group, a)) {
     
    576599    }
    577600
     601#ifndef FIPS_MODULE
    578602    if (ctx == NULL) {
    579603        ctx = new_ctx = BN_CTX_new();
     
    581605            return -1;
    582606    }
     607#endif
    583608
    584609    BN_CTX_start(ctx);
     
    598623 err:
    599624    BN_CTX_end(ctx);
     625#ifndef FIPS_MODULE
    600626    BN_CTX_free(new_ctx);
     627#endif
    601628    return ret;
    602629}
    603630
    604631/* Forces the given EC_POINT to internally use affine coordinates. */
    605 int ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
    606                                BN_CTX *ctx)
    607 {
    608     BN_CTX *new_ctx = NULL;
     632int ossl_ec_GF2m_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
     633                                    BN_CTX *ctx)
     634{
    609635    BIGNUM *x, *y;
    610636    int ret = 0;
     637#ifndef FIPS_MODULE
     638    BN_CTX *new_ctx = NULL;
     639#endif
    611640
    612641    if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
    613642        return 1;
    614643
     644#ifndef FIPS_MODULE
    615645    if (ctx == NULL) {
    616646        ctx = new_ctx = BN_CTX_new();
     
    618648            return 0;
    619649    }
     650#endif
    620651
    621652    BN_CTX_start(ctx);
     
    639670 err:
    640671    BN_CTX_end(ctx);
     672#ifndef FIPS_MODULE
    641673    BN_CTX_free(new_ctx);
     674#endif
    642675    return ret;
    643676}
     
    646679 * Forces each of the EC_POINTs in the given array to use affine coordinates.
    647680 */
    648 int ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
    649                                       EC_POINT *points[], BN_CTX *ctx)
     681int ossl_ec_GF2m_simple_points_make_affine(const EC_GROUP *group, size_t num,
     682                                           EC_POINT *points[], BN_CTX *ctx)
    650683{
    651684    size_t i;
     
    660693
    661694/* Wrapper to simple binary polynomial field multiplication implementation. */
    662 int ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r,
    663                              const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
     695int ossl_ec_GF2m_simple_field_mul(const EC_GROUP *group, BIGNUM *r,
     696                                  const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
    664697{
    665698    return BN_GF2m_mod_mul_arr(r, a, b, group->poly, ctx);
     
    667700
    668701/* Wrapper to simple binary polynomial field squaring implementation. */
    669 int ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r,
    670                              const BIGNUM *a, BN_CTX *ctx)
     702int ossl_ec_GF2m_simple_field_sqr(const EC_GROUP *group, BIGNUM *r,
     703                                  const BIGNUM *a, BN_CTX *ctx)
    671704{
    672705    return BN_GF2m_mod_sqr_arr(r, a, group->poly, ctx);
     
    674707
    675708/* Wrapper to simple binary polynomial field division implementation. */
    676 int ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r,
    677                              const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
     709int ossl_ec_GF2m_simple_field_div(const EC_GROUP *group, BIGNUM *r,
     710                                  const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
    678711{
    679712    return BN_GF2m_mod_div(r, a, b, group->field, ctx);
     
    697730    /* s blinding: make sure lambda (s->Z here) is not zero */
    698731    do {
    699         if (!BN_priv_rand(s->Z, BN_num_bits(group->field) - 1,
    700                           BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) {
    701             ECerr(EC_F_EC_GF2M_SIMPLE_LADDER_PRE, ERR_R_BN_LIB);
     732        if (!BN_priv_rand_ex(s->Z, BN_num_bits(group->field) - 1,
     733                             BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx)) {
     734            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    702735            return 0;
    703736        }
     
    712745    /* r blinding: make sure lambda (r->Y here for storage) is not zero */
    713746    do {
    714         if (!BN_priv_rand(r->Y, BN_num_bits(group->field) - 1,
    715                           BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY)) {
    716             ECerr(EC_F_EC_GF2M_SIMPLE_LADDER_PRE, ERR_R_BN_LIB);
     747        if (!BN_priv_rand_ex(r->Y, BN_num_bits(group->field) - 1,
     748                             BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx)) {
     749            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    717750            return 0;
    718751        }
     
    783816        if (!EC_POINT_copy(r, p)
    784817            || !EC_POINT_invert(group, r, ctx)) {
    785             ECerr(EC_F_EC_GF2M_SIMPLE_LADDER_POST, ERR_R_EC_LIB);
     818            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    786819            return 0;
    787820        }
     
    794827    t2 = BN_CTX_get(ctx);
    795828    if (t2 == NULL) {
    796         ECerr(EC_F_EC_GF2M_SIMPLE_LADDER_POST, ERR_R_MALLOC_FAILURE);
     829        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    797830        goto err;
    798831    }
     
    857890     */
    858891    if (num > 1 || BN_is_zero(group->order) || BN_is_zero(group->cofactor))
    859         return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
     892        return ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
    860893
    861894    if (scalar != NULL && num == 0)
    862895        /* Fixed point multiplication */
    863         return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);
     896        return ossl_ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);
    864897
    865898    if (scalar == NULL && num == 1)
    866899        /* Variable point multiplication */
    867         return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);
     900        return ossl_ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);
    868901
    869902    /*-
     
    873906
    874907    if ((t = EC_POINT_new(group)) == NULL) {
    875         ECerr(EC_F_EC_GF2M_SIMPLE_POINTS_MUL, ERR_R_MALLOC_FAILURE);
    876         return 0;
    877     }
    878 
    879     if (!ec_scalar_mul_ladder(group, t, scalar, NULL, ctx)
    880         || !ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx)
     908        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     909        return 0;
     910    }
     911
     912    if (!ossl_ec_scalar_mul_ladder(group, t, scalar, NULL, ctx)
     913        || !ossl_ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx)
    881914        || !EC_POINT_add(group, r, t, r, ctx))
    882915        goto err;
     
    900933
    901934    if (!(ret = BN_GF2m_mod_inv(r, a, group->field, ctx)))
    902         ECerr(EC_F_EC_GF2M_SIMPLE_FIELD_INV, EC_R_CANNOT_INVERT);
     935        ERR_raise(ERR_LIB_EC, EC_R_CANNOT_INVERT);
    903936    return ret;
    904937}
     
    909942        EC_FLAGS_DEFAULT_OCT,
    910943        NID_X9_62_characteristic_two_field,
    911         ec_GF2m_simple_group_init,
    912         ec_GF2m_simple_group_finish,
    913         ec_GF2m_simple_group_clear_finish,
    914         ec_GF2m_simple_group_copy,
    915         ec_GF2m_simple_group_set_curve,
    916         ec_GF2m_simple_group_get_curve,
    917         ec_GF2m_simple_group_get_degree,
    918         ec_group_simple_order_bits,
    919         ec_GF2m_simple_group_check_discriminant,
    920         ec_GF2m_simple_point_init,
    921         ec_GF2m_simple_point_finish,
    922         ec_GF2m_simple_point_clear_finish,
    923         ec_GF2m_simple_point_copy,
    924         ec_GF2m_simple_point_set_to_infinity,
    925         0, /* set_Jprojective_coordinates_GFp */
    926         0, /* get_Jprojective_coordinates_GFp */
    927         ec_GF2m_simple_point_set_affine_coordinates,
    928         ec_GF2m_simple_point_get_affine_coordinates,
     944        ossl_ec_GF2m_simple_group_init,
     945        ossl_ec_GF2m_simple_group_finish,
     946        ossl_ec_GF2m_simple_group_clear_finish,
     947        ossl_ec_GF2m_simple_group_copy,
     948        ossl_ec_GF2m_simple_group_set_curve,
     949        ossl_ec_GF2m_simple_group_get_curve,
     950        ossl_ec_GF2m_simple_group_get_degree,
     951        ossl_ec_group_simple_order_bits,
     952        ossl_ec_GF2m_simple_group_check_discriminant,
     953        ossl_ec_GF2m_simple_point_init,
     954        ossl_ec_GF2m_simple_point_finish,
     955        ossl_ec_GF2m_simple_point_clear_finish,
     956        ossl_ec_GF2m_simple_point_copy,
     957        ossl_ec_GF2m_simple_point_set_to_infinity,
     958        ossl_ec_GF2m_simple_point_set_affine_coordinates,
     959        ossl_ec_GF2m_simple_point_get_affine_coordinates,
    929960        0, /* point_set_compressed_coordinates */
    930961        0, /* point2oct */
    931962        0, /* oct2point */
    932         ec_GF2m_simple_add,
    933         ec_GF2m_simple_dbl,
    934         ec_GF2m_simple_invert,
    935         ec_GF2m_simple_is_at_infinity,
    936         ec_GF2m_simple_is_on_curve,
    937         ec_GF2m_simple_cmp,
    938         ec_GF2m_simple_make_affine,
    939         ec_GF2m_simple_points_make_affine,
     963        ossl_ec_GF2m_simple_add,
     964        ossl_ec_GF2m_simple_dbl,
     965        ossl_ec_GF2m_simple_invert,
     966        ossl_ec_GF2m_simple_is_at_infinity,
     967        ossl_ec_GF2m_simple_is_on_curve,
     968        ossl_ec_GF2m_simple_cmp,
     969        ossl_ec_GF2m_simple_make_affine,
     970        ossl_ec_GF2m_simple_points_make_affine,
    940971        ec_GF2m_simple_points_mul,
    941972        0, /* precompute_mult */
    942973        0, /* have_precompute_mult */
    943         ec_GF2m_simple_field_mul,
    944         ec_GF2m_simple_field_sqr,
    945         ec_GF2m_simple_field_div,
     974        ossl_ec_GF2m_simple_field_mul,
     975        ossl_ec_GF2m_simple_field_sqr,
     976        ossl_ec_GF2m_simple_field_div,
    946977        ec_GF2m_simple_field_inv,
    947978        0, /* field_encode */
    948979        0, /* field_decode */
    949980        0, /* field_set_to_one */
    950         ec_key_simple_priv2oct,
    951         ec_key_simple_oct2priv,
     981        ossl_ec_key_simple_priv2oct,
     982        ossl_ec_key_simple_oct2priv,
    952983        0, /* set private */
    953         ec_key_simple_generate_key,
    954         ec_key_simple_check_key,
    955         ec_key_simple_generate_public_key,
     984        ossl_ec_key_simple_generate_key,
     985        ossl_ec_key_simple_check_key,
     986        ossl_ec_key_simple_generate_public_key,
    956987        0, /* keycopy */
    957988        0, /* keyfinish */
    958         ecdh_simple_compute_key,
     989        ossl_ecdh_simple_compute_key,
     990        ossl_ecdsa_simple_sign_setup,
     991        ossl_ecdsa_simple_sign_sig,
     992        ossl_ecdsa_simple_verify_sig,
    959993        0, /* field_inverse_mod_ord */
    960994        0, /* blind_coordinates */
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_ameth.c

    r91772 r94082  
    11/*
    2  * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
    77 * https://www.openssl.org/source/license.html
    88 */
     9
     10/*
     11 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
     12 * for internal use.
     13 */
     14#include "internal/deprecated.h"
    915
    1016#include <stdio.h>
     
    1319#include <openssl/ec.h>
    1420#include <openssl/bn.h>
    15 #include <openssl/cms.h>
    1621#include <openssl/asn1t.h>
    1722#include "crypto/asn1.h"
    1823#include "crypto/evp.h"
     24#include "crypto/x509.h"
     25#include <openssl/core_names.h>
     26#include <openssl/param_build.h>
    1927#include "ec_local.h"
    20 
    21 #ifndef OPENSSL_NO_CMS
    22 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri);
    23 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri);
    24 #endif
    2528
    2629static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
     
    2831    const EC_GROUP *group;
    2932    int nid;
     33
    3034    if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
    31         ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS);
     35        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
    3236        return 0;
    3337    }
     
    4044        if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
    4145            ASN1_OBJECT_free(asn1obj);
    42             ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_OID);
     46            ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID);
    4347            return 0;
    4448        }
     
    5155        if (pstr == NULL)
    5256            return 0;
    53 
    54         /*
    55          * The cast in the following line is intentional as the
    56          * `i2d_ECParameters` signature can't be constified (see discussion at
    57          * https://github.com/openssl/openssl/pull/9347 where related and
    58          * required constification backports were rejected).
    59          *
    60          * This cast should be safe anyway, because we can expect
    61          * `i2d_ECParameters()` to treat the first argument as if it was const.
    62          */
    63         pstr->length = i2d_ECParameters((EC_KEY *)ec_key, &pstr->data);
     57        pstr->length = i2d_ECParameters(ec_key, &pstr->data);
    6458        if (pstr->length <= 0) {
    6559            ASN1_STRING_free(pstr);
    66             ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB);
     60            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    6761            return 0;
    6862        }
     
    8276
    8377    if (!eckey_param2type(&ptype, &pval, ec_key)) {
    84         ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB);
     78        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    8579        return 0;
    8680    }
     
    107101}
    108102
    109 static EC_KEY *eckey_type2param(int ptype, const void *pval)
    110 {
    111     EC_KEY *eckey = NULL;
    112     EC_GROUP *group = NULL;
    113 
    114     if (ptype == V_ASN1_SEQUENCE) {
    115         const ASN1_STRING *pstr = pval;
    116         const unsigned char *pm = pstr->data;
    117         int pmlen = pstr->length;
    118 
    119         if ((eckey = d2i_ECParameters(NULL, &pm, pmlen)) == NULL) {
    120             ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
    121             goto ecerr;
    122         }
    123     } else if (ptype == V_ASN1_OBJECT) {
    124         const ASN1_OBJECT *poid = pval;
    125 
    126         /*
    127          * type == V_ASN1_OBJECT => the parameters are given by an asn1 OID
    128          */
    129         if ((eckey = EC_KEY_new()) == NULL) {
    130             ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE);
    131             goto ecerr;
    132         }
    133         group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid));
    134         if (group == NULL)
    135             goto ecerr;
    136         EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
    137         if (EC_KEY_set_group(eckey, group) == 0)
    138             goto ecerr;
    139         EC_GROUP_free(group);
    140     } else {
    141         ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR);
    142         goto ecerr;
    143     }
    144 
    145     return eckey;
    146 
    147  ecerr:
    148     EC_KEY_free(eckey);
    149     EC_GROUP_free(group);
    150     return NULL;
    151 }
    152 
    153 static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
     103static int eckey_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
    154104{
    155105    const unsigned char *p = NULL;
    156     const void *pval;
    157     int ptype, pklen;
     106    int pklen;
    158107    EC_KEY *eckey = NULL;
    159108    X509_ALGOR *palg;
    160 
    161     if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
    162         return 0;
    163     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
    164 
    165     eckey = eckey_type2param(ptype, pval);
    166 
    167     if (!eckey) {
    168         ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB);
    169         return 0;
    170     }
     109    OSSL_LIB_CTX *libctx = NULL;
     110    const char *propq = NULL;
     111
     112    if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey)
     113        || !X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
     114        return 0;
     115    eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
     116
     117    if (!eckey)
     118        return 0;
    171119
    172120    /* We have parameters now set public key */
    173121    if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
    174         ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR);
     122        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
    175123        goto ecerr;
    176124    }
     
    190138    const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
    191139        *pb = EC_KEY_get0_public_key(b->pkey.ec);
     140
    192141    if (group == NULL || pa == NULL || pb == NULL)
    193142        return -2;
     
    200149}
    201150
    202 static int eckey_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
    203 {
    204     const unsigned char *p = NULL;
    205     const void *pval;
    206     int ptype, pklen;
    207     EC_KEY *eckey = NULL;
    208     const X509_ALGOR *palg;
    209 
    210     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
    211         return 0;
    212     X509_ALGOR_get0(NULL, &ptype, &pval, palg);
    213 
    214     eckey = eckey_type2param(ptype, pval);
    215 
    216     if (!eckey)
    217         goto ecliberr;
    218 
    219     /* We have parameters now set private key */
    220     if (!d2i_ECPrivateKey(&eckey, &p, pklen)) {
    221         ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR);
    222         goto ecerr;
    223     }
    224 
    225     EVP_PKEY_assign_EC_KEY(pkey, eckey);
    226     return 1;
    227 
    228  ecliberr:
    229     ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB);
    230  ecerr:
    231     EC_KEY_free(eckey);
    232     return 0;
     151static int eckey_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8,
     152                                OSSL_LIB_CTX *libctx, const char *propq)
     153{
     154    int ret = 0;
     155    EC_KEY *eckey = ossl_ec_key_from_pkcs8(p8, libctx, propq);
     156
     157    if (eckey != NULL) {
     158        ret = 1;
     159        EVP_PKEY_assign_EC_KEY(pkey, eckey);
     160    }
     161
     162    return ret;
    233163}
    234164
     
    242172
    243173    if (!eckey_param2type(&ptype, &pval, &ec_key)) {
    244         ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR);
     174        ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
    245175        return 0;
    246176    }
     
    257187    eplen = i2d_ECPrivateKey(&ec_key, NULL);
    258188    if (!eplen) {
    259         ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
     189        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    260190        return 0;
    261191    }
    262192    ep = OPENSSL_malloc(eplen);
    263193    if (ep == NULL) {
    264         ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
     194        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    265195        return 0;
    266196    }
     
    268198    if (!i2d_ECPrivateKey(&ec_key, &p)) {
    269199        OPENSSL_free(ep);
    270         ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB);
     200        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    271201        return 0;
    272202    }
     
    294224{
    295225    int ecbits = ec_bits(pkey);
     226
    296227    if (ecbits >= 512)
    297228        return 256;
     
    338269    const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
    339270        *group_b = EC_KEY_get0_group(b->pkey.ec);
     271
    340272    if (group_a == NULL || group_b == NULL)
    341273        return -2;
     
    366298
    367299    if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
    368         ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_PASSED_NULL_PARAMETER);
     300        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    369301        return 0;
    370302    }
     
    414346 err:
    415347    if (!ret)
    416         ECerr(EC_F_DO_EC_KEY_PRINT, ERR_R_EC_LIB);
     348        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    417349    OPENSSL_clear_free(priv, privlen);
    418350    OPENSSL_free(pub);
     
    425357    EC_KEY *eckey;
    426358
    427     if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL) {
    428         ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB);
    429         return 0;
    430     }
     359    if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL)
     360        return 0;
    431361    EVP_PKEY_assign_EC_KEY(pkey, eckey);
    432362    return 1;
     
    461391    EC_KEY *ec;
    462392
    463     if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL) {
    464         ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR);
    465         return 0;
    466     }
     393    if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL)
     394        return 0;
    467395    EVP_PKEY_assign_EC_KEY(pkey, ec);
    468396    return 1;
     
    477405{
    478406    switch (op) {
    479     case ASN1_PKEY_CTRL_PKCS7_SIGN:
    480         if (arg1 == 0) {
    481             int snid, hnid;
    482             X509_ALGOR *alg1, *alg2;
    483             PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
    484             if (alg1 == NULL || alg1->algorithm == NULL)
    485                 return -1;
    486             hnid = OBJ_obj2nid(alg1->algorithm);
    487             if (hnid == NID_undef)
    488                 return -1;
    489             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
    490                 return -1;
    491             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
    492         }
    493         return 1;
    494 #ifndef OPENSSL_NO_CMS
    495     case ASN1_PKEY_CTRL_CMS_SIGN:
    496         if (arg1 == 0) {
    497             int snid, hnid;
    498             X509_ALGOR *alg1, *alg2;
    499             CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
    500             if (alg1 == NULL || alg1->algorithm == NULL)
    501                 return -1;
    502             hnid = OBJ_obj2nid(alg1->algorithm);
    503             if (hnid == NID_undef)
    504                 return -1;
    505             if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
    506                 return -1;
    507             X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
    508         }
    509         return 1;
    510 
    511     case ASN1_PKEY_CTRL_CMS_ENVELOPE:
    512         if (arg1 == 1)
    513             return ecdh_cms_decrypt(arg2);
    514         else if (arg1 == 0)
    515             return ecdh_cms_encrypt(arg2);
    516         return -2;
    517 
    518     case ASN1_PKEY_CTRL_CMS_RI_TYPE:
    519         *(int *)arg2 = CMS_RECIPINFO_AGREE;
    520         return 1;
    521 #endif
    522 
    523407    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
    524         if (EVP_PKEY_id(pkey) == EVP_PKEY_SM2) {
     408        if (EVP_PKEY_get_id(pkey) == EVP_PKEY_SM2) {
    525409            /* For SM2, the only valid digest-alg is SM3 */
    526410            *(int *)arg2 = NID_sm3;
    527         } else {
    528             *(int *)arg2 = NID_sha256;
     411            return 2;            /* Make it mandatory */
    529412        }
     413        *(int *)arg2 = NID_sha256;
    530414        return 1;
    531415
    532416    case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
    533         return EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(pkey), arg2, arg1, NULL);
     417        /* We should only be here if we have a legacy key */
     418        if (!ossl_assert(evp_pkey_is_legacy(pkey)))
     419            return 0;
     420        return EC_KEY_oct2key(evp_pkey_get0_EC_KEY_int(pkey), arg2, arg1, NULL);
    534421
    535422    case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
     
    539426    default:
    540427        return -2;
    541 
    542     }
    543 
     428    }
    544429}
    545430
     
    550435    /* stay consistent to what EVP_PKEY_check demands */
    551436    if (eckey->priv_key == NULL) {
    552         ECerr(EC_F_EC_PKEY_CHECK, EC_R_MISSING_PRIVATE_KEY);
     437        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
    553438        return 0;
    554439    }
     
    579464    /* stay consistent to what EVP_PKEY_check demands */
    580465    if (eckey->group == NULL) {
    581         ECerr(EC_F_EC_PKEY_PARAM_CHECK, EC_R_MISSING_PARAMETERS);
     466        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
    582467        return 0;
    583468    }
     
    586471}
    587472
    588 const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = {
     473static
     474size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey)
     475{
     476    return pkey->pkey.ec->dirty_cnt;
     477}
     478
     479static
     480int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
     481                      OSSL_FUNC_keymgmt_import_fn *importer,
     482                      OSSL_LIB_CTX *libctx, const char *propq)
     483{
     484    const EC_KEY *eckey = NULL;
     485    const EC_GROUP *ecg = NULL;
     486    unsigned char *pub_key_buf = NULL, *gen_buf = NULL;
     487    size_t pub_key_buflen;
     488    OSSL_PARAM_BLD *tmpl;
     489    OSSL_PARAM *params = NULL;
     490    const BIGNUM *priv_key = NULL;
     491    const EC_POINT *pub_point = NULL;
     492    int selection = 0;
     493    int rv = 0;
     494    BN_CTX *bnctx = NULL;
     495
     496    if (from == NULL
     497            || (eckey = from->pkey.ec) == NULL
     498            || (ecg = EC_KEY_get0_group(eckey)) == NULL)
     499        return 0;
     500
     501    tmpl = OSSL_PARAM_BLD_new();
     502    if (tmpl == NULL)
     503        return 0;
     504
     505    /*
     506     * EC_POINT_point2buf() can generate random numbers in some
     507     * implementations so we need to ensure we use the correct libctx.
     508     */
     509    bnctx = BN_CTX_new_ex(libctx);
     510    if (bnctx == NULL)
     511        goto err;
     512    BN_CTX_start(bnctx);
     513
     514    /* export the domain parameters */
     515    if (!ossl_ec_group_todata(ecg, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
     516        goto err;
     517    selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
     518
     519    priv_key = EC_KEY_get0_private_key(eckey);
     520    pub_point = EC_KEY_get0_public_key(eckey);
     521
     522    if (pub_point != NULL) {
     523        /* convert pub_point to a octet string according to the SECG standard */
     524        if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
     525                                                 POINT_CONVERSION_COMPRESSED,
     526                                                 &pub_key_buf, bnctx)) == 0
     527            || !OSSL_PARAM_BLD_push_octet_string(tmpl,
     528                                                 OSSL_PKEY_PARAM_PUB_KEY,
     529                                                 pub_key_buf,
     530                                                 pub_key_buflen))
     531            goto err;
     532        selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
     533    }
     534
     535    if (priv_key != NULL) {
     536        size_t sz;
     537        int ecbits;
     538        int ecdh_cofactor_mode;
     539
     540        /*
     541         * Key import/export should never leak the bit length of the secret
     542         * scalar in the key.
     543         *
     544         * For this reason, on export we use padded BIGNUMs with fixed length.
     545         *
     546         * When importing we also should make sure that, even if short lived,
     547         * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
     548         * soon as possible, so that any processing of this BIGNUM might opt for
     549         * constant time implementations in the backend.
     550         *
     551         * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
     552         * to preallocate the BIGNUM internal buffer to a fixed public size big
     553         * enough that operations performed during the processing never trigger
     554         * a realloc which would leak the size of the scalar through memory
     555         * accesses.
     556         *
     557         * Fixed Length
     558         * ------------
     559         *
     560         * The order of the large prime subgroup of the curve is our choice for
     561         * a fixed public size, as that is generally the upper bound for
     562         * generating a private key in EC cryptosystems and should fit all valid
     563         * secret scalars.
     564         *
     565         * For padding on export we just use the bit length of the order
     566         * converted to bytes (rounding up).
     567         *
     568         * For preallocating the BIGNUM storage we look at the number of "words"
     569         * required for the internal representation of the order, and we
     570         * preallocate 2 extra "words" in case any of the subsequent processing
     571         * might temporarily overflow the order length.
     572         */
     573        ecbits = EC_GROUP_order_bits(ecg);
     574        if (ecbits <= 0)
     575            goto err;
     576
     577        sz = (ecbits + 7 ) / 8;
     578        if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
     579                                        OSSL_PKEY_PARAM_PRIV_KEY,
     580                                        priv_key, sz))
     581            goto err;
     582        selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
     583
     584        /*
     585         * The ECDH Cofactor Mode is defined only if the EC_KEY actually
     586         * contains a private key, so we check for the flag and export it only
     587         * in this case.
     588         */
     589        ecdh_cofactor_mode =
     590            (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
     591
     592        /* Export the ECDH_COFACTOR_MODE parameter */
     593        if (!OSSL_PARAM_BLD_push_int(tmpl,
     594                                     OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
     595                                     ecdh_cofactor_mode))
     596            goto err;
     597        selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
     598    }
     599
     600    params = OSSL_PARAM_BLD_to_param(tmpl);
     601
     602    /* We export, the provider imports */
     603    rv = importer(to_keydata, selection, params);
     604
     605 err:
     606    OSSL_PARAM_BLD_free(tmpl);
     607    OSSL_PARAM_free(params);
     608    OPENSSL_free(pub_key_buf);
     609    OPENSSL_free(gen_buf);
     610    BN_CTX_end(bnctx);
     611    BN_CTX_free(bnctx);
     612    return rv;
     613}
     614
     615static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
     616{
     617    EVP_PKEY_CTX *pctx = vpctx;
     618    EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
     619    EC_KEY *ec = EC_KEY_new_ex(pctx->libctx, pctx->propquery);
     620
     621    if (ec == NULL) {
     622        ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
     623        return 0;
     624    }
     625
     626    if (!ossl_ec_group_fromdata(ec, params)
     627        || !ossl_ec_key_otherparams_fromdata(ec, params)
     628        || !ossl_ec_key_fromdata(ec, params, 1)
     629        || !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
     630        EC_KEY_free(ec);
     631        return 0;
     632    }
     633    return 1;
     634}
     635
     636static int ec_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
     637{
     638    EC_KEY *eckey = from->pkey.ec;
     639    EC_KEY *dupkey = NULL;
     640    int ret;
     641
     642    if (eckey != NULL) {
     643        dupkey = EC_KEY_dup(eckey);
     644        if (dupkey == NULL)
     645            return 0;
     646    } else {
     647        /* necessary to properly copy empty SM2 keys */
     648        return EVP_PKEY_set_type(to, from->type);
     649    }
     650
     651    ret = EVP_PKEY_assign_EC_KEY(to, dupkey);
     652    if (!ret)
     653        EC_KEY_free(dupkey);
     654    return ret;
     655}
     656
     657const EVP_PKEY_ASN1_METHOD ossl_eckey_asn1_meth = {
    589658    EVP_PKEY_EC,
    590659    EVP_PKEY_EC,
     
    598667    eckey_pub_print,
    599668
    600     eckey_priv_decode,
     669    NULL,
    601670    eckey_priv_encode,
    602671    eckey_priv_print,
     
    623692    ec_pkey_check,
    624693    ec_pkey_public_check,
    625     ec_pkey_param_check
     694    ec_pkey_param_check,
     695
     696    0, /* set_priv_key */
     697    0, /* set_pub_key */
     698    0, /* get_priv_key */
     699    0, /* get_pub_key */
     700
     701    ec_pkey_dirty_cnt,
     702    ec_pkey_export_to,
     703    ec_pkey_import_from,
     704    ec_pkey_copy,
     705    eckey_priv_decode_ex
    626706};
    627707
    628708#if !defined(OPENSSL_NO_SM2)
    629 const EVP_PKEY_ASN1_METHOD sm2_asn1_meth = {
     709const EVP_PKEY_ASN1_METHOD ossl_sm2_asn1_meth = {
    630710   EVP_PKEY_SM2,
    631711   EVP_PKEY_EC,
     
    646726    return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);
    647727}
    648 
    649 #ifndef OPENSSL_NO_CMS
    650 
    651 static int ecdh_cms_set_peerkey(EVP_PKEY_CTX *pctx,
    652                                 X509_ALGOR *alg, ASN1_BIT_STRING *pubkey)
    653 {
    654     const ASN1_OBJECT *aoid;
    655     int atype;
    656     const void *aval;
    657     int rv = 0;
    658     EVP_PKEY *pkpeer = NULL;
    659     EC_KEY *ecpeer = NULL;
    660     const unsigned char *p;
    661     int plen;
    662     X509_ALGOR_get0(&aoid, &atype, &aval, alg);
    663     if (OBJ_obj2nid(aoid) != NID_X9_62_id_ecPublicKey)
    664         goto err;
    665     /* If absent parameters get group from main key */
    666     if (atype == V_ASN1_UNDEF || atype == V_ASN1_NULL) {
    667         const EC_GROUP *grp;
    668         EVP_PKEY *pk;
    669         pk = EVP_PKEY_CTX_get0_pkey(pctx);
    670         if (!pk)
    671             goto err;
    672         grp = EC_KEY_get0_group(pk->pkey.ec);
    673         ecpeer = EC_KEY_new();
    674         if (ecpeer == NULL)
    675             goto err;
    676         if (!EC_KEY_set_group(ecpeer, grp))
    677             goto err;
    678     } else {
    679         ecpeer = eckey_type2param(atype, aval);
    680         if (!ecpeer)
    681             goto err;
    682     }
    683     /* We have parameters now set public key */
    684     plen = ASN1_STRING_length(pubkey);
    685     p = ASN1_STRING_get0_data(pubkey);
    686     if (!p || !plen)
    687         goto err;
    688     if (!o2i_ECPublicKey(&ecpeer, &p, plen))
    689         goto err;
    690     pkpeer = EVP_PKEY_new();
    691     if (pkpeer == NULL)
    692         goto err;
    693     EVP_PKEY_set1_EC_KEY(pkpeer, ecpeer);
    694     if (EVP_PKEY_derive_set_peer(pctx, pkpeer) > 0)
    695         rv = 1;
    696  err:
    697     EC_KEY_free(ecpeer);
    698     EVP_PKEY_free(pkpeer);
    699     return rv;
    700 }
    701 
    702 /* Set KDF parameters based on KDF NID */
    703 static int ecdh_cms_set_kdf_param(EVP_PKEY_CTX *pctx, int eckdf_nid)
    704 {
    705     int kdf_nid, kdfmd_nid, cofactor;
    706     const EVP_MD *kdf_md;
    707     if (eckdf_nid == NID_undef)
    708         return 0;
    709 
    710     /* Lookup KDF type, cofactor mode and digest */
    711     if (!OBJ_find_sigid_algs(eckdf_nid, &kdfmd_nid, &kdf_nid))
    712         return 0;
    713 
    714     if (kdf_nid == NID_dh_std_kdf)
    715         cofactor = 0;
    716     else if (kdf_nid == NID_dh_cofactor_kdf)
    717         cofactor = 1;
    718     else
    719         return 0;
    720 
    721     if (EVP_PKEY_CTX_set_ecdh_cofactor_mode(pctx, cofactor) <= 0)
    722         return 0;
    723 
    724     if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, EVP_PKEY_ECDH_KDF_X9_63) <= 0)
    725         return 0;
    726 
    727     kdf_md = EVP_get_digestbynid(kdfmd_nid);
    728     if (!kdf_md)
    729         return 0;
    730 
    731     if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
    732         return 0;
    733     return 1;
    734 }
    735 
    736 static int ecdh_cms_set_shared_info(EVP_PKEY_CTX *pctx, CMS_RecipientInfo *ri)
    737 {
    738     int rv = 0;
    739 
    740     X509_ALGOR *alg, *kekalg = NULL;
    741     ASN1_OCTET_STRING *ukm;
    742     const unsigned char *p;
    743     unsigned char *der = NULL;
    744     int plen, keylen;
    745     const EVP_CIPHER *kekcipher;
    746     EVP_CIPHER_CTX *kekctx;
    747 
    748     if (!CMS_RecipientInfo_kari_get0_alg(ri, &alg, &ukm))
    749         return 0;
    750 
    751     if (!ecdh_cms_set_kdf_param(pctx, OBJ_obj2nid(alg->algorithm))) {
    752         ECerr(EC_F_ECDH_CMS_SET_SHARED_INFO, EC_R_KDF_PARAMETER_ERROR);
    753         return 0;
    754     }
    755 
    756     if (alg->parameter->type != V_ASN1_SEQUENCE)
    757         return 0;
    758 
    759     p = alg->parameter->value.sequence->data;
    760     plen = alg->parameter->value.sequence->length;
    761     kekalg = d2i_X509_ALGOR(NULL, &p, plen);
    762     if (!kekalg)
    763         goto err;
    764     kekctx = CMS_RecipientInfo_kari_get0_ctx(ri);
    765     if (!kekctx)
    766         goto err;
    767     kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
    768     if (!kekcipher || EVP_CIPHER_mode(kekcipher) != EVP_CIPH_WRAP_MODE)
    769         goto err;
    770     if (!EVP_EncryptInit_ex(kekctx, kekcipher, NULL, NULL, NULL))
    771         goto err;
    772     if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0)
    773         goto err;
    774 
    775     keylen = EVP_CIPHER_CTX_key_length(kekctx);
    776     if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
    777         goto err;
    778 
    779     plen = CMS_SharedInfo_encode(&der, kekalg, ukm, keylen);
    780 
    781     if (!plen)
    782         goto err;
    783 
    784     if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, der, plen) <= 0)
    785         goto err;
    786     der = NULL;
    787 
    788     rv = 1;
    789  err:
    790     X509_ALGOR_free(kekalg);
    791     OPENSSL_free(der);
    792     return rv;
    793 }
    794 
    795 static int ecdh_cms_decrypt(CMS_RecipientInfo *ri)
    796 {
    797     EVP_PKEY_CTX *pctx;
    798     pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
    799     if (!pctx)
    800         return 0;
    801     /* See if we need to set peer key */
    802     if (!EVP_PKEY_CTX_get0_peerkey(pctx)) {
    803         X509_ALGOR *alg;
    804         ASN1_BIT_STRING *pubkey;
    805         if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &alg, &pubkey,
    806                                                  NULL, NULL, NULL))
    807             return 0;
    808         if (!alg || !pubkey)
    809             return 0;
    810         if (!ecdh_cms_set_peerkey(pctx, alg, pubkey)) {
    811             ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_PEER_KEY_ERROR);
    812             return 0;
    813         }
    814     }
    815     /* Set ECDH derivation parameters and initialise unwrap context */
    816     if (!ecdh_cms_set_shared_info(pctx, ri)) {
    817         ECerr(EC_F_ECDH_CMS_DECRYPT, EC_R_SHARED_INFO_ERROR);
    818         return 0;
    819     }
    820     return 1;
    821 }
    822 
    823 static int ecdh_cms_encrypt(CMS_RecipientInfo *ri)
    824 {
    825     EVP_PKEY_CTX *pctx;
    826     EVP_PKEY *pkey;
    827     EVP_CIPHER_CTX *ctx;
    828     int keylen;
    829     X509_ALGOR *talg, *wrap_alg = NULL;
    830     const ASN1_OBJECT *aoid;
    831     ASN1_BIT_STRING *pubkey;
    832     ASN1_STRING *wrap_str;
    833     ASN1_OCTET_STRING *ukm;
    834     unsigned char *penc = NULL;
    835     int penclen;
    836     int rv = 0;
    837     int ecdh_nid, kdf_type, kdf_nid, wrap_nid;
    838     const EVP_MD *kdf_md;
    839     pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
    840     if (!pctx)
    841         return 0;
    842     /* Get ephemeral key */
    843     pkey = EVP_PKEY_CTX_get0_pkey(pctx);
    844     if (!CMS_RecipientInfo_kari_get0_orig_id(ri, &talg, &pubkey,
    845                                              NULL, NULL, NULL))
    846         goto err;
    847     X509_ALGOR_get0(&aoid, NULL, NULL, talg);
    848     /* Is everything uninitialised? */
    849     if (aoid == OBJ_nid2obj(NID_undef)) {
    850 
    851         EC_KEY *eckey = pkey->pkey.ec;
    852         /* Set the key */
    853         unsigned char *p;
    854 
    855         penclen = i2o_ECPublicKey(eckey, NULL);
    856         if (penclen <= 0)
    857             goto err;
    858         penc = OPENSSL_malloc(penclen);
    859         if (penc == NULL)
    860             goto err;
    861         p = penc;
    862         penclen = i2o_ECPublicKey(eckey, &p);
    863         if (penclen <= 0)
    864             goto err;
    865         ASN1_STRING_set0(pubkey, penc, penclen);
    866         pubkey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
    867         pubkey->flags |= ASN1_STRING_FLAG_BITS_LEFT;
    868 
    869         penc = NULL;
    870         X509_ALGOR_set0(talg, OBJ_nid2obj(NID_X9_62_id_ecPublicKey),
    871                         V_ASN1_UNDEF, NULL);
    872     }
    873 
    874     /* See if custom parameters set */
    875     kdf_type = EVP_PKEY_CTX_get_ecdh_kdf_type(pctx);
    876     if (kdf_type <= 0)
    877         goto err;
    878     if (!EVP_PKEY_CTX_get_ecdh_kdf_md(pctx, &kdf_md))
    879         goto err;
    880     ecdh_nid = EVP_PKEY_CTX_get_ecdh_cofactor_mode(pctx);
    881     if (ecdh_nid < 0)
    882         goto err;
    883     else if (ecdh_nid == 0)
    884         ecdh_nid = NID_dh_std_kdf;
    885     else if (ecdh_nid == 1)
    886         ecdh_nid = NID_dh_cofactor_kdf;
    887 
    888     if (kdf_type == EVP_PKEY_ECDH_KDF_NONE) {
    889         kdf_type = EVP_PKEY_ECDH_KDF_X9_63;
    890         if (EVP_PKEY_CTX_set_ecdh_kdf_type(pctx, kdf_type) <= 0)
    891             goto err;
    892     } else
    893         /* Unknown KDF */
    894         goto err;
    895     if (kdf_md == NULL) {
    896         /* Fixme later for better MD */
    897         kdf_md = EVP_sha1();
    898         if (EVP_PKEY_CTX_set_ecdh_kdf_md(pctx, kdf_md) <= 0)
    899             goto err;
    900     }
    901 
    902     if (!CMS_RecipientInfo_kari_get0_alg(ri, &talg, &ukm))
    903         goto err;
    904 
    905     /* Lookup NID for KDF+cofactor+digest */
    906 
    907     if (!OBJ_find_sigid_by_algs(&kdf_nid, EVP_MD_type(kdf_md), ecdh_nid))
    908         goto err;
    909     /* Get wrap NID */
    910     ctx = CMS_RecipientInfo_kari_get0_ctx(ri);
    911     wrap_nid = EVP_CIPHER_CTX_type(ctx);
    912     keylen = EVP_CIPHER_CTX_key_length(ctx);
    913 
    914     /* Package wrap algorithm in an AlgorithmIdentifier */
    915 
    916     wrap_alg = X509_ALGOR_new();
    917     if (wrap_alg == NULL)
    918         goto err;
    919     wrap_alg->algorithm = OBJ_nid2obj(wrap_nid);
    920     wrap_alg->parameter = ASN1_TYPE_new();
    921     if (wrap_alg->parameter == NULL)
    922         goto err;
    923     if (EVP_CIPHER_param_to_asn1(ctx, wrap_alg->parameter) <= 0)
    924         goto err;
    925     if (ASN1_TYPE_get(wrap_alg->parameter) == NID_undef) {
    926         ASN1_TYPE_free(wrap_alg->parameter);
    927         wrap_alg->parameter = NULL;
    928     }
    929 
    930     if (EVP_PKEY_CTX_set_ecdh_kdf_outlen(pctx, keylen) <= 0)
    931         goto err;
    932 
    933     penclen = CMS_SharedInfo_encode(&penc, wrap_alg, ukm, keylen);
    934 
    935     if (!penclen)
    936         goto err;
    937 
    938     if (EVP_PKEY_CTX_set0_ecdh_kdf_ukm(pctx, penc, penclen) <= 0)
    939         goto err;
    940     penc = NULL;
    941 
    942     /*
    943      * Now need to wrap encoding of wrap AlgorithmIdentifier into parameter
    944      * of another AlgorithmIdentifier.
    945      */
    946     penclen = i2d_X509_ALGOR(wrap_alg, &penc);
    947     if (!penc || !penclen)
    948         goto err;
    949     wrap_str = ASN1_STRING_new();
    950     if (wrap_str == NULL)
    951         goto err;
    952     ASN1_STRING_set0(wrap_str, penc, penclen);
    953     penc = NULL;
    954     X509_ALGOR_set0(talg, OBJ_nid2obj(kdf_nid), V_ASN1_SEQUENCE, wrap_str);
    955 
    956     rv = 1;
    957 
    958  err:
    959     OPENSSL_free(penc);
    960     X509_ALGOR_free(wrap_alg);
    961     return rv;
    962 }
    963 
    964 #endif
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_asn1.c

    r91772 r94082  
    22 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
    77 * https://www.openssl.org/source/license.html
    88 */
     9
     10/*
     11 * ECDSA low level APIs are deprecated for public use, but still ok for
     12 * internal use.
     13 */
     14#include "internal/deprecated.h"
    915
    1016#include <string.h>
     
    1420#include <openssl/objects.h>
    1521#include "internal/nelem.h"
    16 
    17 int EC_GROUP_get_basis_type(const EC_GROUP *group)
    18 {
    19     int i;
    20 
    21     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
    22         NID_X9_62_characteristic_two_field)
    23         /* everything else is currently not supported */
    24         return 0;
    25 
    26     /* Find the last non-zero element of group->poly[] */
    27     for (i = 0;
    28          i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
    29          i++)
    30         continue;
    31 
    32     if (i == 4)
    33         return NID_X9_62_ppBasis;
    34     else if (i == 2)
    35         return NID_X9_62_tpBasis;
    36     else
    37         /* everything else is currently not supported */
    38         return 0;
    39 }
    40 
    41 #ifndef OPENSSL_NO_EC2M
    42 int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
    43 {
    44     if (group == NULL)
    45         return 0;
    46 
    47     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
    48         NID_X9_62_characteristic_two_field
    49         || !((group->poly[0] != 0) && (group->poly[1] != 0)
    50              && (group->poly[2] == 0))) {
    51         ECerr(EC_F_EC_GROUP_GET_TRINOMIAL_BASIS,
    52               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    53         return 0;
    54     }
    55 
    56     if (k)
    57         *k = group->poly[1];
    58 
    59     return 1;
    60 }
    61 
    62 int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
    63                                    unsigned int *k2, unsigned int *k3)
    64 {
    65     if (group == NULL)
    66         return 0;
    67 
    68     if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) !=
    69         NID_X9_62_characteristic_two_field
    70         || !((group->poly[0] != 0) && (group->poly[1] != 0)
    71              && (group->poly[2] != 0) && (group->poly[3] != 0)
    72              && (group->poly[4] == 0))) {
    73         ECerr(EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS,
    74               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    75         return 0;
    76     }
    77 
    78     if (k1)
    79         *k1 = group->poly[3];
    80     if (k2)
    81         *k2 = group->poly[2];
    82     if (k3)
    83         *k3 = group->poly[1];
    84 
    85     return 1;
    86 }
    87 #endif
     22#include "crypto/asn1_dsa.h"
     23
     24#ifndef FIPS_MODULE
    8825
    8926/* some structures needed for the asn1 encoding */
     
    224161} ASN1_CHOICE_END(ECPKPARAMETERS)
    225162
    226 DECLARE_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
    227 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
    228 IMPLEMENT_ASN1_FUNCTIONS_const(ECPKPARAMETERS)
     163DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS)
     164DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS)
     165IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS)
    229166
    230167ASN1_SEQUENCE(EC_PRIVATEKEY) = {
     
    235172} static_ASN1_SEQUENCE_END(EC_PRIVATEKEY)
    236173
    237 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
    238 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
    239 IMPLEMENT_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
     174DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY)
     175DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY)
     176IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY)
    240177
    241178/* some declarations of internal function */
     
    260197    ASN1_TYPE_free(field->p.other);
    261198
    262     nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
     199    nid = EC_GROUP_get_field_type(group);
    263200    /* set OID for the field */
    264201    if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) {
    265         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
     202        ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB);
    266203        goto err;
    267204    }
     
    269206    if (nid == NID_X9_62_prime_field) {
    270207        if ((tmp = BN_new()) == NULL) {
    271             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
     208            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    272209            goto err;
    273210        }
    274211        /* the parameters are specified by the prime number p */
    275212        if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) {
    276             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
     213            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    277214            goto err;
    278215        }
     
    280217        field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL);
    281218        if (field->p.prime == NULL) {
    282             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
     219            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    283220            goto err;
    284221        }
     
    286223#ifdef OPENSSL_NO_EC2M
    287224    {
    288         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_GF2M_NOT_SUPPORTED);
     225        ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
    289226        goto err;
    290227    }
     
    298235
    299236        if (char_two == NULL) {
    300             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
     237            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    301238            goto err;
    302239        }
     
    307244
    308245        if (field_type == 0) {
    309             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_EC_LIB);
     246            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    310247            goto err;
    311248        }
    312249        /* set base type OID */
    313250        if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) {
    314             ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_OBJ_LIB);
     251            ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB);
    315252            goto err;
    316253        }
     
    324261            char_two->p.tpBasis = ASN1_INTEGER_new();
    325262            if (char_two->p.tpBasis == NULL) {
    326                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
     263                ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    327264                goto err;
    328265            }
    329266            if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) {
    330                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_ASN1_LIB);
     267                ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    331268                goto err;
    332269            }
     
    339276            char_two->p.ppBasis = X9_62_PENTANOMIAL_new();
    340277            if (char_two->p.ppBasis == NULL) {
    341                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
     278                ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    342279                goto err;
    343280            }
     
    352289            char_two->p.onBasis = ASN1_NULL_new();
    353290            if (char_two->p.onBasis == NULL) {
    354                 ECerr(EC_F_EC_ASN1_GROUP2FIELDID, ERR_R_MALLOC_FAILURE);
     291                ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    355292                goto err;
    356293            }
     
    359296#endif
    360297    else {
    361         ECerr(EC_F_EC_ASN1_GROUP2FIELDID, EC_R_UNSUPPORTED_FIELD);
     298        ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
    362299        goto err;
    363300    }
     
    381318
    382319    if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) {
    383         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
     320        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    384321        goto err;
    385322    }
     
    387324    /* get a and b */
    388325    if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) {
    389         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_EC_LIB);
     326        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    390327        goto err;
    391328    }
     
    399336    if ((a_buf = OPENSSL_malloc(len)) == NULL
    400337        || (b_buf = OPENSSL_malloc(len)) == NULL) {
    401         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
     338        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    402339        goto err;
    403340    }
    404341    if (BN_bn2binpad(tmp_1, a_buf, len) < 0
    405342        || BN_bn2binpad(tmp_2, b_buf, len) < 0) {
    406         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_BN_LIB);
     343        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    407344        goto err;
    408345    }
     
    411348    if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len)
    412349        || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) {
    413         ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
     350        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    414351        goto err;
    415352    }
     
    419356        if (!curve->seed)
    420357            if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) {
    421                 ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_MALLOC_FAILURE);
     358                ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    422359                goto err;
    423360            }
     
    426363        if (!ASN1_BIT_STRING_set(curve->seed, group->seed,
    427364                                 (int)group->seed_len)) {
    428             ECerr(EC_F_EC_ASN1_GROUP2CURVE, ERR_R_ASN1_LIB);
     365            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    429366            goto err;
    430367        }
     
    445382
    446383ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
    447                                                ECPARAMETERS *params)
     384                                        ECPARAMETERS *params)
    448385{
    449386    size_t len = 0;
     
    457394    if (params == NULL) {
    458395        if ((ret = ECPARAMETERS_new()) == NULL) {
    459             ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
     396            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    460397            goto err;
    461398        }
     
    468405    /* set the fieldID */
    469406    if (!ec_asn1_group2fieldid(group, ret->fieldID)) {
    470         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
     407        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    471408        goto err;
    472409    }
     
    474411    /* set the curve */
    475412    if (!ec_asn1_group2curve(group, ret->curve)) {
    476         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
     413        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    477414        goto err;
    478415    }
     
    480417    /* set the base point */
    481418    if ((point = EC_GROUP_get0_generator(group)) == NULL) {
    482         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, EC_R_UNDEFINED_GENERATOR);
     419        ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
    483420        goto err;
    484421    }
     
    488425    len = EC_POINT_point2buf(group, point, form, &buffer, NULL);
    489426    if (len == 0) {
    490         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
     427        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    491428        goto err;
    492429    }
    493430    if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) {
    494431        OPENSSL_free(buffer);
    495         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
     432        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    496433        goto err;
    497434    }
     
    501438    tmp = EC_GROUP_get0_order(group);
    502439    if (tmp == NULL) {
    503         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_EC_LIB);
     440        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    504441        goto err;
    505442    }
     
    507444    if (ret->order == NULL) {
    508445        ret->order = orig;
    509         ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
     446        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    510447        goto err;
    511448    }
     
    517454        if (ret->cofactor == NULL) {
    518455            ret->cofactor = orig;
    519             ECerr(EC_F_EC_GROUP_GET_ECPARAMETERS, ERR_R_ASN1_LIB);
     456            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    520457            goto err;
    521458        }
     
    538475    if (ret == NULL) {
    539476        if ((ret = ECPKPARAMETERS_new()) == NULL) {
    540             ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, ERR_R_MALLOC_FAILURE);
     477            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    541478            return NULL;
    542479        }
     
    549486    }
    550487
    551     if (EC_GROUP_get_asn1_flag(group)) {
     488    if (EC_GROUP_get_asn1_flag(group) == OPENSSL_EC_NAMED_CURVE) {
    552489        /*
    553490         * use the asn1 OID to describe the elliptic curve parameters
     
    559496            if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
    560497                ASN1_OBJECT_free(asn1obj);
    561                 ECerr(EC_F_EC_GROUP_GET_ECPKPARAMETERS, EC_R_MISSING_OID);
     498                ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID);
    562499                ok = 0;
    563500            } else {
     
    593530    BN_CTX *ctx = NULL;
    594531
    595     if (!params->fieldID || !params->fieldID->fieldType ||
    596         !params->fieldID->p.ptr) {
    597         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
     532    if (params->fieldID == NULL
     533            || params->fieldID->fieldType == NULL
     534            || params->fieldID->p.ptr == NULL) {
     535        ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
    598536        goto err;
    599537    }
     
    605543     * compatibility.
    606544     */
    607     if (!params->curve || !params->curve->a ||
    608         !params->curve->a->data || !params->curve->b ||
    609         !params->curve->b->data) {
    610         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
     545    if (params->curve == NULL
     546            || params->curve->a == NULL || params->curve->a->data == NULL
     547            || params->curve->b == NULL || params->curve->b->data == NULL) {
     548        ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
    611549        goto err;
    612550    }
    613551    a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL);
    614552    if (a == NULL) {
    615         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
     553        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    616554        goto err;
    617555    }
    618556    b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL);
    619557    if (b == NULL) {
    620         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
     558        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    621559        goto err;
    622560    }
     
    627565#ifdef OPENSSL_NO_EC2M
    628566    {
    629         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_GF2M_NOT_SUPPORTED);
     567        ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
    630568        goto err;
    631569    }
     
    638576        field_bits = char_two->m;
    639577        if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
    640             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
     578            ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
    641579            goto err;
    642580        }
    643581
    644582        if ((p = BN_new()) == NULL) {
    645             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
     583            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    646584            goto err;
    647585        }
     
    654592
    655593            if (!char_two->p.tpBasis) {
    656                 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
     594                ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
    657595                goto err;
    658596            }
     
    661599
    662600            if (!(char_two->m > tmp_long && tmp_long > 0)) {
    663                 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
    664                       EC_R_INVALID_TRINOMIAL_BASIS);
     601                ERR_raise(ERR_LIB_EC, EC_R_INVALID_TRINOMIAL_BASIS);
    665602                goto err;
    666603            }
     
    677614
    678615            penta = char_two->p.ppBasis;
    679             if (!penta) {
    680                 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
     616            if (penta == NULL) {
     617                ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
    681618                goto err;
    682619            }
     
    685622                (char_two->m > penta->k3 && penta->k3 > penta->k2
    686623                 && penta->k2 > penta->k1 && penta->k1 > 0)) {
    687                 ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS,
    688                       EC_R_INVALID_PENTANOMIAL_BASIS);
     624                ERR_raise(ERR_LIB_EC, EC_R_INVALID_PENTANOMIAL_BASIS);
    689625                goto err;
    690626            }
     
    702638                goto err;
    703639        } else if (tmp == NID_X9_62_onBasis) {
    704             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_NOT_IMPLEMENTED);
     640            ERR_raise(ERR_LIB_EC, EC_R_NOT_IMPLEMENTED);
    705641            goto err;
    706642        } else {                /* error */
    707643
    708             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
     644            ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
    709645            goto err;
    710646        }
     
    717653        /* we have a curve over a prime field */
    718654        /* extract the prime number */
    719         if (!params->fieldID->p.prime) {
    720             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
     655        if (params->fieldID->p.prime == NULL) {
     656            ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
    721657            goto err;
    722658        }
    723659        p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL);
    724660        if (p == NULL) {
    725             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
     661            ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    726662            goto err;
    727663        }
    728664
    729665        if (BN_is_negative(p) || BN_is_zero(p)) {
    730             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
     666            ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
    731667            goto err;
    732668        }
     
    734670        field_bits = BN_num_bits(p);
    735671        if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
    736             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_FIELD_TOO_LARGE);
     672            ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
    737673            goto err;
    738674        }
     
    741677        ret = EC_GROUP_new_curve_GFp(p, a, b, NULL);
    742678    } else {
    743         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_FIELD);
     679        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
    744680        goto err;
    745681    }
    746682
    747683    if (ret == NULL) {
    748         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
     684        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    749685        goto err;
    750686    }
     
    754690        OPENSSL_free(ret->seed);
    755691        if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) {
    756             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
     692            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    757693            goto err;
    758694        }
     
    766702            || params->base->data == NULL
    767703            || params->base->length == 0) {
    768         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_ASN1_ERROR);
     704        ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
    769705        goto err;
    770706    }
     
    780716    if (!EC_POINT_oct2point(ret, point, params->base->data,
    781717                            params->base->length, NULL)) {
    782         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
     718        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    783719        goto err;
    784720    }
     
    786722    /* extract the order */
    787723    if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) {
    788         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
     724        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    789725        goto err;
    790726    }
    791727    if (BN_is_negative(a) || BN_is_zero(a)) {
    792         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
     728        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
    793729        goto err;
    794730    }
    795731    if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */
    796         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, EC_R_INVALID_GROUP_ORDER);
     732        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
    797733        goto err;
    798734    }
     
    803739        b = NULL;
    804740    } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) {
    805         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_ASN1_LIB);
     741        ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB);
    806742        goto err;
    807743    }
    808744    /* set the generator, order and cofactor (if present) */
    809745    if (!EC_GROUP_set_generator(ret, point, a, b)) {
    810         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
     746        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    811747        goto err;
    812748    }
     
    825761     */
    826762    if ((ctx = BN_CTX_new()) == NULL) {
    827         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_BN_LIB);
     763        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    828764        goto err;
    829765    }
     
    831767            || EC_GROUP_set_seed(dup, NULL, 0) != 1
    832768            || !EC_GROUP_set_generator(dup, point, a, NULL)) {
    833         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
    834         goto err;
    835     }
    836     if ((curve_name = ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
     769        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
     770        goto err;
     771    }
     772    if ((curve_name = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
    837773        /*
    838774         * The input explicit parameters successfully matched one of the
     
    856792
    857793        if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) {
    858             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, ERR_R_EC_LIB);
     794            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    859795            goto err;
    860796        }
     
    908844
    909845    if (params == NULL) {
    910         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_MISSING_PARAMETERS);
     846        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
    911847        return NULL;
    912848    }
     
    916852        tmp = OBJ_obj2nid(params->value.named_curve);
    917853        if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) {
    918             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS,
    919                   EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
     854            ERR_raise(ERR_LIB_EC, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE);
    920855            return NULL;
    921856        }
     
    925860        ret = EC_GROUP_new_from_ecparameters(params->value.parameters);
    926861        if (!ret) {
    927             ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, ERR_R_EC_LIB);
     862            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    928863            return NULL;
    929864        }
     
    933868        return NULL;
    934869    } else {
    935         ECerr(EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, EC_R_ASN1_ERROR);
     870        ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR);
    936871        return NULL;
    937872    }
     
    949884
    950885    if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) {
    951         ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
    952886        ECPKPARAMETERS_free(params);
    953887        return NULL;
     
    955889
    956890    if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) {
    957         ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
    958891        ECPKPARAMETERS_free(params);
    959892        return NULL;
     
    978911    ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL);
    979912    if (tmp == NULL) {
    980         ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_GROUP2PKPARAMETERS_FAILURE);
     913        ERR_raise(ERR_LIB_EC, EC_R_GROUP2PKPARAMETERS_FAILURE);
    981914        return 0;
    982915    }
    983916    if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) {
    984         ECerr(EC_F_I2D_ECPKPARAMETERS, EC_R_I2D_ECPKPARAMETERS_FAILURE);
     917        ERR_raise(ERR_LIB_EC, EC_R_I2D_ECPKPARAMETERS_FAILURE);
    985918        ECPKPARAMETERS_free(tmp);
    986919        return 0;
     
    998931    const unsigned char *p = *in;
    999932
    1000     if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) {
    1001         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
     933    if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL)
    1002934        return NULL;
    1003     }
    1004935
    1005936    if (a == NULL || *a == NULL) {
    1006937        if ((ret = EC_KEY_new()) == NULL) {
    1007             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
     938            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    1008939            goto err;
    1009940        }
     
    1020951
    1021952    if (ret->group == NULL) {
    1022         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
     953        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    1023954        goto err;
    1024955    }
     
    1032963            goto err;
    1033964    } else {
    1034         ECerr(EC_F_D2I_ECPRIVATEKEY, EC_R_MISSING_PRIVATE_KEY);
    1035         goto err;
    1036     }
     965        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
     966        goto err;
     967    }
     968
     969    if (EC_GROUP_get_curve_name(ret->group) == NID_sm2)
     970        EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE);
    1037971
    1038972    EC_POINT_clear_free(ret->pub_key);
    1039973    ret->pub_key = EC_POINT_new(ret->group);
    1040974    if (ret->pub_key == NULL) {
    1041         ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
     975        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    1042976        goto err;
    1043977    }
     
    1050984        pub_oct_len = ASN1_STRING_length(priv_key->publicKey);
    1051985        if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) {
    1052             ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_EC_LIB);
     986            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    1053987            goto err;
    1054988        }
     
    1065999    EC_PRIVATEKEY_free(priv_key);
    10661000    *in = p;
     1001    ret->dirty_cnt++;
    10671002    return ret;
    10681003
     
    10741009}
    10751010
    1076 int i2d_ECPrivateKey(EC_KEY *a, unsigned char **out)
     1011int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out)
    10771012{
    10781013    int ret = 0, ok = 0;
     
    10841019    if (a == NULL || a->group == NULL ||
    10851020        (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) {
    1086         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_PASSED_NULL_PARAMETER);
     1021        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    10871022        goto err;
    10881023    }
    10891024
    10901025    if ((priv_key = EC_PRIVATEKEY_new()) == NULL) {
    1091         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
     1026        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    10921027        goto err;
    10931028    }
     
    10981033
    10991034    if (privlen == 0) {
    1100         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
     1035        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    11011036        goto err;
    11021037    }
     
    11091044             EC_GROUP_get_ecpkparameters(a->group,
    11101045                                        priv_key->parameters)) == NULL) {
    1111             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
     1046            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    11121047            goto err;
    11131048        }
     
    11171052        priv_key->publicKey = ASN1_BIT_STRING_new();
    11181053        if (priv_key->publicKey == NULL) {
    1119             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
     1054            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    11201055            goto err;
    11211056        }
     
    11241059
    11251060        if (publen == 0) {
    1126             ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
     1061            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    11271062            goto err;
    11281063        }
     
    11351070
    11361071    if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) {
    1137         ECerr(EC_F_I2D_ECPRIVATEKEY, ERR_R_EC_LIB);
     1072        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    11381073        goto err;
    11391074    }
     
    11461081}
    11471082
    1148 int i2d_ECParameters(EC_KEY *a, unsigned char **out)
     1083int i2d_ECParameters(const EC_KEY *a, unsigned char **out)
    11491084{
    11501085    if (a == NULL) {
    1151         ECerr(EC_F_I2D_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
     1086        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    11521087        return 0;
    11531088    }
     
    11601095
    11611096    if (in == NULL || *in == NULL) {
    1162         ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_PASSED_NULL_PARAMETER);
     1097        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    11631098        return NULL;
    11641099    }
     
    11661101    if (a == NULL || *a == NULL) {
    11671102        if ((ret = EC_KEY_new()) == NULL) {
    1168             ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
     1103            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    11691104            return NULL;
    11701105        }
     
    11731108
    11741109    if (!d2i_ECPKParameters(&ret->group, in, len)) {
    1175         ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
    11761110        if (a == NULL || *a != ret)
    11771111             EC_KEY_free(ret);
     1112        else
     1113            ret->dirty_cnt++;
    11781114        return NULL;
    11791115    }
     1116
     1117    if (EC_GROUP_get_curve_name(ret->group) == NID_sm2)
     1118        EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE);
     1119
     1120    ret->dirty_cnt++;
    11801121
    11811122    if (a)
     
    11931134         * sorry, but a EC_GROUP-structure is necessary to set the public key
    11941135         */
    1195         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
     1136        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    11961137        return 0;
    11971138    }
    11981139    ret = *a;
     1140    /* EC_KEY_opt2key updates dirty_cnt */
    11991141    if (!EC_KEY_oct2key(ret, *in, len, NULL)) {
    1200         ECerr(EC_F_O2I_ECPUBLICKEY, ERR_R_EC_LIB);
     1142        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    12011143        return 0;
    12021144    }
     
    12111153
    12121154    if (a == NULL) {
    1213         ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_PASSED_NULL_PARAMETER);
     1155        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    12141156        return 0;
    12151157    }
     
    12241166    if (*out == NULL) {
    12251167        if ((*out = OPENSSL_malloc(buf_len)) == NULL) {
    1226             ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_MALLOC_FAILURE);
     1168            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    12271169            return 0;
    12281170        }
     
    12311173    if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form,
    12321174                            *out, buf_len, NULL)) {
    1233         ECerr(EC_F_I2O_ECPUBLICKEY, ERR_R_EC_LIB);
     1175        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    12341176        if (new_buffer) {
    12351177            OPENSSL_free(*out);
     
    12431185}
    12441186
    1245 ASN1_SEQUENCE(ECDSA_SIG) = {
    1246         ASN1_SIMPLE(ECDSA_SIG, r, CBIGNUM),
    1247         ASN1_SIMPLE(ECDSA_SIG, s, CBIGNUM)
    1248 } static_ASN1_SEQUENCE_END(ECDSA_SIG)
    1249 
    1250 DECLARE_ASN1_FUNCTIONS_const(ECDSA_SIG)
    1251 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECDSA_SIG, ECDSA_SIG)
    1252 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ECDSA_SIG, ECDSA_SIG, ECDSA_SIG)
     1187DECLARE_ASN1_FUNCTIONS(ECDSA_SIG)
     1188DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG)
     1189
     1190#endif /* FIPS_MODULE */
    12531191
    12541192ECDSA_SIG *ECDSA_SIG_new(void)
     
    12561194    ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
    12571195    if (sig == NULL)
    1258         ECerr(EC_F_ECDSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
     1196        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    12591197    return sig;
    12601198}
     
    12671205    BN_clear_free(sig->s);
    12681206    OPENSSL_free(sig);
     1207}
     1208
     1209ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len)
     1210{
     1211    ECDSA_SIG *sig;
     1212
     1213    if (len < 0)
     1214        return NULL;
     1215    if (psig != NULL && *psig != NULL) {
     1216        sig = *psig;
     1217    } else {
     1218        sig = ECDSA_SIG_new();
     1219        if (sig == NULL)
     1220            return NULL;
     1221    }
     1222    if (sig->r == NULL)
     1223        sig->r = BN_new();
     1224    if (sig->s == NULL)
     1225        sig->s = BN_new();
     1226    if (sig->r == NULL || sig->s == NULL
     1227        || ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
     1228        if (psig == NULL || *psig == NULL)
     1229            ECDSA_SIG_free(sig);
     1230        return NULL;
     1231    }
     1232    if (psig != NULL && *psig == NULL)
     1233        *psig = sig;
     1234    return sig;
     1235}
     1236
     1237int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout)
     1238{
     1239    BUF_MEM *buf = NULL;
     1240    size_t encoded_len;
     1241    WPACKET pkt;
     1242
     1243    if (ppout == NULL) {
     1244        if (!WPACKET_init_null(&pkt, 0))
     1245            return -1;
     1246    } else if (*ppout == NULL) {
     1247        if ((buf = BUF_MEM_new()) == NULL
     1248                || !WPACKET_init_len(&pkt, buf, 0)) {
     1249            BUF_MEM_free(buf);
     1250            return -1;
     1251        }
     1252    } else {
     1253        if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0))
     1254            return -1;
     1255    }
     1256
     1257    if (!ossl_encode_der_dsa_sig(&pkt, sig->r, sig->s)
     1258            || !WPACKET_get_total_written(&pkt, &encoded_len)
     1259            || !WPACKET_finish(&pkt)) {
     1260        BUF_MEM_free(buf);
     1261        WPACKET_cleanup(&pkt);
     1262        return -1;
     1263    }
     1264
     1265    if (ppout != NULL) {
     1266        if (*ppout == NULL) {
     1267            *ppout = (unsigned char *)buf->data;
     1268            buf->data = NULL;
     1269            BUF_MEM_free(buf);
     1270        } else {
     1271            *ppout += encoded_len;
     1272        }
     1273    }
     1274
     1275    return (int)encoded_len;
    12691276}
    12701277
     
    12981305}
    12991306
    1300 int ECDSA_size(const EC_KEY *r)
    1301 {
    1302     int ret, i;
    1303     ASN1_INTEGER bs;
    1304     unsigned char buf[4];
     1307int ECDSA_size(const EC_KEY *ec)
     1308{
     1309    int ret;
     1310    ECDSA_SIG sig;
    13051311    const EC_GROUP *group;
    1306 
    1307     if (r == NULL)
     1312    const BIGNUM *bn;
     1313
     1314    if (ec == NULL)
    13081315        return 0;
    1309     group = EC_KEY_get0_group(r);
     1316    group = EC_KEY_get0_group(ec);
    13101317    if (group == NULL)
    13111318        return 0;
    13121319
    1313     i = EC_GROUP_order_bits(group);
    1314     if (i == 0)
     1320    bn = EC_GROUP_get0_order(group);
     1321    if (bn == NULL)
    13151322        return 0;
    1316     bs.length = (i + 7) / 8;
    1317     bs.data = buf;
    1318     bs.type = V_ASN1_INTEGER;
    1319     /* If the top bit is set the asn1 encoding is 1 larger. */
    1320     buf[0] = 0xff;
    1321 
    1322     i = i2d_ASN1_INTEGER(&bs, NULL);
    1323     i += i;                     /* r and s */
    1324     ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
     1323
     1324    sig.r = sig.s = (BIGNUM *)bn;
     1325    ret = i2d_ECDSA_SIG(&sig, NULL);
     1326
    13251327    if (ret < 0)
    1326         return 0;
     1328        ret = 0;
    13271329    return ret;
    13281330}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_check.c

    r91772 r94082  
    11/*
    2  * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    88 */
    99
     10/*
     11 * ECDSA low level APIs are deprecated for public use, but still ok for
     12 * internal use.
     13 */
     14#include "internal/deprecated.h"
     15
    1016#include "ec_local.h"
    1117#include <openssl/err.h>
    1218
     19int EC_GROUP_check_named_curve(const EC_GROUP *group, int nist_only,
     20                               BN_CTX *ctx)
     21{
     22    int nid;
     23    BN_CTX *new_ctx = NULL;
     24
     25    if (group == NULL) {
     26        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
     27        return NID_undef;
     28    }
     29
     30    if (ctx == NULL) {
     31        ctx = new_ctx = BN_CTX_new_ex(NULL);
     32        if (ctx == NULL) {
     33            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     34            return NID_undef;
     35        }
     36    }
     37
     38    nid = ossl_ec_curve_nid_from_params(group, ctx);
     39    if (nid > 0 && nist_only && EC_curve_nid2nist(nid) == NULL)
     40        nid = NID_undef;
     41
     42    BN_CTX_free(new_ctx);
     43    return nid;
     44}
     45
    1346int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
    1447{
     48#ifdef FIPS_MODULE
     49    /*
     50    * ECC domain parameter validation.
     51    * See SP800-56A R3 5.5.2 "Assurances of Domain-Parameter Validity" Part 1b.
     52    */
     53    return EC_GROUP_check_named_curve(group, 1, ctx) >= 0 ? 1 : 0;
     54#else
    1555    int ret = 0;
    1656    const BIGNUM *order;
    1757    BN_CTX *new_ctx = NULL;
    1858    EC_POINT *point = NULL;
     59
     60    if (group == NULL || group->meth == NULL) {
     61        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
     62        return 0;
     63    }
    1964
    2065    /* Custom curves assumed to be correct */
     
    2570        ctx = new_ctx = BN_CTX_new();
    2671        if (ctx == NULL) {
    27             ECerr(EC_F_EC_GROUP_CHECK, ERR_R_MALLOC_FAILURE);
     72            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    2873            goto err;
    2974        }
     
    3277    /* check the discriminant */
    3378    if (!EC_GROUP_check_discriminant(group, ctx)) {
    34         ECerr(EC_F_EC_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
     79        ERR_raise(ERR_LIB_EC, EC_R_DISCRIMINANT_IS_ZERO);
    3580        goto err;
    3681    }
     
    3883    /* check the generator */
    3984    if (group->generator == NULL) {
    40         ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
     85        ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
    4186        goto err;
    4287    }
    4388    if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) {
    44         ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
     89        ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
    4590        goto err;
    4691    }
     
    5398        goto err;
    5499    if (BN_is_zero(order)) {
    55         ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
     100        ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_ORDER);
    56101        goto err;
    57102    }
     
    60105        goto err;
    61106    if (!EC_POINT_is_at_infinity(group, point)) {
    62         ECerr(EC_F_EC_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);
     107        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
    63108        goto err;
    64109    }
     
    70115    EC_POINT_free(point);
    71116    return ret;
     117#endif /* FIPS_MODULE */
    72118}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_curve.c

    r91772 r94082  
    11/*
    2  * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    99 */
    1010
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
     16
    1117#include <string.h>
    1218#include "ec_local.h"
    1319#include <openssl/err.h>
    1420#include <openssl/obj_mac.h>
     21#include <openssl/objects.h>
    1522#include <openssl/opensslconf.h>
    1623#include "internal/nelem.h"
     
    194201};
    195202
     203# ifndef FIPS_MODULE
    196204/* the x9.62 prime curves (minus the nist prime curves) */
    197205static const struct {
     
    372380    }
    373381};
     382#endif /* FIPS_MODULE */
    374383
    375384static const struct {
     
    411420};
    412421
     422#ifndef FIPS_MODULE
    413423/* the secg prime curves (minus the nist and x9.62 prime curves) */
    414424static const struct {
     
    832842    }
    833843};
     844#endif /* FIPS_MODULE */
    834845
    835846#ifndef OPENSSL_NO_EC2M
    836847
    837848/* characteristic two curves */
     849
     850# ifndef FIPS_MODULE
    838851static const struct {
    839852    EC_CURVE_DATA h;
     
    963976    }
    964977};
     978# endif /* FIPS_MODULE */
    965979
    966980static const struct {
     
    9941008};
    9951009
     1010# ifndef FIPS_MODULE
    9961011static const struct {
    9971012    EC_CURVE_DATA h;
     
    10221037    }
    10231038};
     1039# endif /* FIPS_MODULE */
    10241040
    10251041static const struct {
     
    10521068};
    10531069
     1070# ifndef FIPS_MODULE
    10541071static const struct {
    10551072    EC_CURVE_DATA h;
     
    11271144    }
    11281145};
     1146# endif /* FIPS_MODULE */
    11291147
    11301148static const struct {
     
    11371155    {
    11381156        /* no seed */
     1157        /* p */
    11391158        0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    11401159        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
     
    12011220};
    12021221
     1222#ifndef FIPS_MODULE
    12031223static const struct {
    12041224    EC_CURVE_DATA h;
     
    12101230    {
    12111231        /* no seed */
     1232        /* p */
    12121233        0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
    12131234        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     
    12351256    }
    12361257};
     1258# endif /* FIPS_MODULE */
    12371259
    12381260static const struct {
     
    12451267    {
    12461268        /* no seed */
     1269        /* p */
    12471270        0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    12481271        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     
    12791302    },
    12801303    {
    1281         /* no seed */
     1304        /* seed */
    12821305        0x77, 0xE2, 0xB0, 0x73, 0x70, 0xEB, 0x0F, 0x83, 0x2A, 0x6D, 0xD5, 0xB6,
    12831306        0x2D, 0xFC, 0x88, 0xCD, 0x06, 0xBB, 0x84, 0xBE,
     
    15171540};
    15181541
     1542# ifndef FIPS_MODULE
    15191543static const struct {
    15201544    EC_CURVE_DATA h;
     
    22012225    }
    22022226};
    2203 
    2204 #endif
     2227# endif /* FIPS_MODULE */
     2228#endif /* OPENSSL_NO_EC2M */
    22052229
    22062230/*
     
    22122236 */
    22132237
     2238#ifndef FIPS_MODULE
    22142239static const struct {
    22152240    EC_CURVE_DATA h;
     
    27512776    }
    27522777};
    2753 
    2754 #ifndef OPENSSL_NO_SM2
     2778#endif /* FIPS_MODULE */
     2779
     2780#if !defined(OPENSSL_NO_SM2) && !defined(FIPS_MODULE)
    27552781static const struct {
    27562782    EC_CURVE_DATA h;
     
    27972823    const char *comment;
    27982824} ec_list_element;
     2825
     2826#ifdef FIPS_MODULE
     2827static const ec_list_element curve_list[] = {
     2828    /* prime field curves */
     2829    /* secg curves */
     2830    {NID_secp224r1, &_EC_NIST_PRIME_224.h,
     2831# if !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
     2832     EC_GFp_nistp224_method,
     2833# else
     2834     0,
     2835# endif
     2836     "NIST/SECG curve over a 224 bit prime field"},
     2837    /* SECG secp256r1 is the same as X9.62 prime256v1 and hence omitted */
     2838    {NID_secp384r1, &_EC_NIST_PRIME_384.h,
     2839# if defined(S390X_EC_ASM)
     2840     EC_GFp_s390x_nistp384_method,
     2841# else
     2842     0,
     2843# endif
     2844     "NIST/SECG curve over a 384 bit prime field"},
     2845
     2846    {NID_secp521r1, &_EC_NIST_PRIME_521.h,
     2847# if defined(S390X_EC_ASM)
     2848     EC_GFp_s390x_nistp521_method,
     2849# elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
     2850     EC_GFp_nistp521_method,
     2851# else
     2852     0,
     2853# endif
     2854     "NIST/SECG curve over a 521 bit prime field"},
     2855
     2856    /* X9.62 curves */
     2857    {NID_X9_62_prime192v1, &_EC_NIST_PRIME_192.h, 0,
     2858     "NIST/X9.62/SECG curve over a 192 bit prime field"},
     2859    {NID_X9_62_prime256v1, &_EC_X9_62_PRIME_256V1.h,
     2860# if defined(ECP_NISTZ256_ASM)
     2861     EC_GFp_nistz256_method,
     2862# elif defined(S390X_EC_ASM)
     2863     EC_GFp_s390x_nistp256_method,
     2864# elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
     2865     EC_GFp_nistp256_method,
     2866# else
     2867     0,
     2868# endif
     2869     "X9.62/SECG curve over a 256 bit prime field"},
     2870
     2871# ifndef OPENSSL_NO_EC2M
     2872    /* characteristic two field curves */
     2873    /* NIST/SECG curves */
     2874    {NID_sect163k1, &_EC_NIST_CHAR2_163K.h, 0,
     2875     "NIST/SECG/WTLS curve over a 163 bit binary field"},
     2876    {NID_sect163r2, &_EC_NIST_CHAR2_163B.h, 0,
     2877     "NIST/SECG curve over a 163 bit binary field"},
     2878    {NID_sect233k1, &_EC_NIST_CHAR2_233K.h, 0,
     2879     "NIST/SECG/WTLS curve over a 233 bit binary field"},
     2880    {NID_sect233r1, &_EC_NIST_CHAR2_233B.h, 0,
     2881     "NIST/SECG/WTLS curve over a 233 bit binary field"},
     2882    {NID_sect283k1, &_EC_NIST_CHAR2_283K.h, 0,
     2883     "NIST/SECG curve over a 283 bit binary field"},
     2884    {NID_sect283r1, &_EC_NIST_CHAR2_283B.h, 0,
     2885     "NIST/SECG curve over a 283 bit binary field"},
     2886    {NID_sect409k1, &_EC_NIST_CHAR2_409K.h, 0,
     2887     "NIST/SECG curve over a 409 bit binary field"},
     2888    {NID_sect409r1, &_EC_NIST_CHAR2_409B.h, 0,
     2889     "NIST/SECG curve over a 409 bit binary field"},
     2890    {NID_sect571k1, &_EC_NIST_CHAR2_571K.h, 0,
     2891     "NIST/SECG curve over a 571 bit binary field"},
     2892    {NID_sect571r1, &_EC_NIST_CHAR2_571B.h, 0,
     2893     "NIST/SECG curve over a 571 bit binary field"},
     2894# endif
     2895};
     2896
     2897#else
    27992898
    28002899static const ec_list_element curve_list[] = {
     
    28202919    {NID_secp224k1, &_EC_SECG_PRIME_224K1.h, 0,
    28212920     "SECG curve over a 224 bit prime field"},
    2822 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
     2921# ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
    28232922    {NID_secp224r1, &_EC_NIST_PRIME_224.h, EC_GFp_nistp224_method,
    28242923     "NIST/SECG curve over a 224 bit prime field"},
    2825 #else
     2924# else
    28262925    {NID_secp224r1, &_EC_NIST_PRIME_224.h, 0,
    28272926     "NIST/SECG curve over a 224 bit prime field"},
    2828 #endif
     2927# endif
    28292928    {NID_secp256k1, &_EC_SECG_PRIME_256K1.h, 0,
    28302929     "SECG curve over a 256 bit prime field"},
    28312930    /* SECG secp256r1 is the same as X9.62 prime256v1 and hence omitted */
    2832     {NID_secp384r1, &_EC_NIST_PRIME_384.h, 0,
     2931    {NID_secp384r1, &_EC_NIST_PRIME_384.h,
     2932# if defined(S390X_EC_ASM)
     2933     EC_GFp_s390x_nistp384_method,
     2934# else
     2935     0,
     2936# endif
    28332937     "NIST/SECG curve over a 384 bit prime field"},
    2834 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
    2835     {NID_secp521r1, &_EC_NIST_PRIME_521.h, EC_GFp_nistp521_method,
     2938    {NID_secp521r1, &_EC_NIST_PRIME_521.h,
     2939# if defined(S390X_EC_ASM)
     2940     EC_GFp_s390x_nistp521_method,
     2941# elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
     2942     EC_GFp_nistp521_method,
     2943# else
     2944     0,
     2945# endif
    28362946     "NIST/SECG curve over a 521 bit prime field"},
    2837 #else
    2838     {NID_secp521r1, &_EC_NIST_PRIME_521.h, 0,
    2839      "NIST/SECG curve over a 521 bit prime field"},
    2840 #endif
    28412947    /* X9.62 curves */
    28422948    {NID_X9_62_prime192v1, &_EC_NIST_PRIME_192.h, 0,
     
    28532959     "X9.62 curve over a 239 bit prime field"},
    28542960    {NID_X9_62_prime256v1, &_EC_X9_62_PRIME_256V1.h,
    2855 #if defined(ECP_NISTZ256_ASM)
     2961# if defined(ECP_NISTZ256_ASM)
    28562962     EC_GFp_nistz256_method,
    2857 #elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
     2963# elif defined(S390X_EC_ASM)
     2964     EC_GFp_s390x_nistp256_method,
     2965# elif !defined(OPENSSL_NO_EC_NISTP_64_GCC_128)
    28582966     EC_GFp_nistp256_method,
    2859 #else
     2967# else
    28602968     0,
    2861 #endif
     2969# endif
    28622970     "X9.62/SECG curve over a 256 bit prime field"},
    2863 #ifndef OPENSSL_NO_EC2M
     2971# ifndef OPENSSL_NO_EC2M
    28642972    /* characteristic two field curves */
    28652973    /* NIST/SECG curves */
     
    29453053    {NID_wap_wsg_idm_ecid_wtls5, &_EC_X9_62_CHAR2_163V1.h, 0,
    29463054     "X9.62 curve over a 163 bit binary field"},
    2947 #endif
     3055# endif
    29483056    {NID_wap_wsg_idm_ecid_wtls6, &_EC_SECG_PRIME_112R1.h, 0,
    29493057     "SECG/WTLS curve over a 112 bit prime field"},
     
    29543062    {NID_wap_wsg_idm_ecid_wtls9, &_EC_WTLS_9.h, 0,
    29553063     "WTLS curve over a 160 bit prime field"},
    2956 #ifndef OPENSSL_NO_EC2M
     3064# ifndef OPENSSL_NO_EC2M
    29573065    {NID_wap_wsg_idm_ecid_wtls10, &_EC_NIST_CHAR2_233K.h, 0,
    29583066     "NIST/SECG/WTLS curve over a 233 bit binary field"},
    29593067    {NID_wap_wsg_idm_ecid_wtls11, &_EC_NIST_CHAR2_233B.h, 0,
    29603068     "NIST/SECG/WTLS curve over a 233 bit binary field"},
    2961 #endif
     3069# endif
    29623070    {NID_wap_wsg_idm_ecid_wtls12, &_EC_WTLS_12.h, 0,
    29633071     "WTLS curve over a 224 bit prime field"},
    2964 #ifndef OPENSSL_NO_EC2M
     3072# ifndef OPENSSL_NO_EC2M
    29653073    /* IPSec curves */
    29663074    {NID_ipsec3, &_EC_IPSEC_155_ID3.h, 0,
     
    29703078     "\n\tIPSec/IKE/Oakley curve #4 over a 185 bit binary field.\n"
    29713079     "\tNot suitable for ECDSA.\n\tQuestionable extension field!"},
    2972 #endif
     3080# endif
    29733081    /* brainpool curves */
    29743082    {NID_brainpoolP160r1, &_EC_brainpoolP160r1.h, 0,
     
    30003108    {NID_brainpoolP512t1, &_EC_brainpoolP512t1.h, 0,
    30013109     "RFC 5639 curve over a 512 bit prime field"},
    3002 #ifndef OPENSSL_NO_SM2
     3110# ifndef OPENSSL_NO_SM2
    30033111    {NID_sm2, &_EC_sm2p256v1.h, 0,
    30043112     "SM2 curve over a 256 bit prime field"},
    3005 #endif
    3006 };
     3113# endif
     3114};
     3115#endif /* FIPS_MODULE */
    30073116
    30083117#define curve_list_length OSSL_NELEM(curve_list)
    30093118
    3010 static EC_GROUP *ec_group_new_from_data(const ec_list_element curve)
     3119static const ec_list_element *ec_curve_nid2curve(int nid)
     3120{
     3121    size_t i;
     3122
     3123    if (nid <= 0)
     3124        return NULL;
     3125
     3126    for (i = 0; i < curve_list_length; i++) {
     3127        if (curve_list[i].nid == nid)
     3128            return &curve_list[i];
     3129    }
     3130    return NULL;
     3131}
     3132
     3133static EC_GROUP *ec_group_new_from_data(OSSL_LIB_CTX *libctx,
     3134                                        const char *propq,
     3135                                        const ec_list_element curve)
    30113136{
    30123137    EC_GROUP *group = NULL;
     
    30233148    /* If no curve data curve method must handle everything */
    30243149    if (curve.data == NULL)
    3025         return EC_GROUP_new(curve.meth != NULL ? curve.meth() : NULL);
    3026 
    3027     if ((ctx = BN_CTX_new()) == NULL) {
    3028         ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_MALLOC_FAILURE);
     3150        return ossl_ec_group_new_ex(libctx, propq,
     3151                                    curve.meth != NULL ? curve.meth() : NULL);
     3152
     3153    if ((ctx = BN_CTX_new_ex(libctx)) == NULL) {
     3154        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    30293155        goto err;
    30303156    }
     
    30393165        || (a = BN_bin2bn(params + 1 * param_len, param_len, NULL)) == NULL
    30403166        || (b = BN_bin2bn(params + 2 * param_len, param_len, NULL)) == NULL) {
    3041         ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
     3167        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    30423168        goto err;
    30433169    }
     
    30453171    if (curve.meth != 0) {
    30463172        meth = curve.meth();
    3047         if (((group = EC_GROUP_new(meth)) == NULL) ||
     3173        if (((group = ossl_ec_group_new_ex(libctx, propq, meth)) == NULL) ||
    30483174            (!(group->meth->group_set_curve(group, p, a, b, ctx)))) {
    3049             ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
     3175            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    30503176            goto err;
    30513177        }
    30523178    } else if (data->field_type == NID_X9_62_prime_field) {
    30533179        if ((group = EC_GROUP_new_curve_GFp(p, a, b, ctx)) == NULL) {
    3054             ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
     3180            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    30553181            goto err;
    30563182        }
     
    30613187
    30623188        if ((group = EC_GROUP_new_curve_GF2m(p, a, b, ctx)) == NULL) {
    3063             ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
     3189            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    30643190            goto err;
    30653191        }
     
    30703196
    30713197    if ((P = EC_POINT_new(group)) == NULL) {
    3072         ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
     3198        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    30733199        goto err;
    30743200    }
     
    30763202    if ((x = BN_bin2bn(params + 3 * param_len, param_len, NULL)) == NULL
    30773203        || (y = BN_bin2bn(params + 4 * param_len, param_len, NULL)) == NULL) {
    3078         ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
     3204        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    30793205        goto err;
    30803206    }
    30813207    if (!EC_POINT_set_affine_coordinates(group, P, x, y, ctx)) {
    3082         ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
     3208        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    30833209        goto err;
    30843210    }
    30853211    if ((order = BN_bin2bn(params + 5 * param_len, param_len, NULL)) == NULL
    30863212        || !BN_set_word(x, (BN_ULONG)data->cofactor)) {
    3087         ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_BN_LIB);
     3213        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    30883214        goto err;
    30893215    }
    30903216    if (!EC_GROUP_set_generator(group, P, order, x)) {
    3091         ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
     3217        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    30923218        goto err;
    30933219    }
    30943220    if (seed_len) {
    30953221        if (!EC_GROUP_set_seed(group, params - seed_len, seed_len)) {
    3096             ECerr(EC_F_EC_GROUP_NEW_FROM_DATA, ERR_R_EC_LIB);
     3222            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    30973223            goto err;
    30983224        }
    30993225    }
     3226
     3227#ifndef FIPS_MODULE
     3228    if (EC_GROUP_get_asn1_flag(group) == OPENSSL_EC_NAMED_CURVE) {
     3229        /*
     3230         * Some curves don't have an associated OID: for those we should not
     3231         * default to `OPENSSL_EC_NAMED_CURVE` encoding of parameters and
     3232         * instead set the ASN1 flag to `OPENSSL_EC_EXPLICIT_CURVE`.
     3233         *
     3234         * Note that `OPENSSL_EC_NAMED_CURVE` is set as the default ASN1 flag on
     3235         * `EC_GROUP_new()`, when we don't have enough elements to determine if
     3236         * an OID for the curve name actually exists.
     3237         * We could implement this check on `EC_GROUP_set_curve_name()` but
     3238         * overloading the simple setter with this lookup could have a negative
     3239         * performance impact and unexpected consequences.
     3240         */
     3241        ASN1_OBJECT *asn1obj = OBJ_nid2obj(curve.nid);
     3242
     3243        if (asn1obj == NULL) {
     3244            ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB);
     3245            goto err;
     3246        }
     3247        if (OBJ_length(asn1obj) == 0)
     3248            EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
     3249
     3250        ASN1_OBJECT_free(asn1obj);
     3251    }
     3252#else
     3253    /*
     3254     * Inside the FIPS module we do not support explicit curves anyway
     3255     * so the above check is not necessary.
     3256     *
     3257     * Skipping it is also necessary because `OBJ_length()` and
     3258     * `ASN1_OBJECT_free()` are not available within the FIPS module
     3259     * boundaries.
     3260     */
     3261#endif
     3262
    31003263    ok = 1;
    31013264 err:
     
    31153278}
    31163279
     3280EC_GROUP *EC_GROUP_new_by_curve_name_ex(OSSL_LIB_CTX *libctx, const char *propq,
     3281                                        int nid)
     3282{
     3283    EC_GROUP *ret = NULL;
     3284    const ec_list_element *curve;
     3285
     3286    if ((curve = ec_curve_nid2curve(nid)) == NULL
     3287        || (ret = ec_group_new_from_data(libctx, propq, *curve)) == NULL) {
     3288#ifndef FIPS_MODULE
     3289        ERR_raise_data(ERR_LIB_EC, EC_R_UNKNOWN_GROUP,
     3290                       "name=%s", OBJ_nid2sn(nid));
     3291#else
     3292        ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_GROUP);
     3293#endif
     3294        return NULL;
     3295    }
     3296
     3297    return ret;
     3298}
     3299
     3300#ifndef FIPS_MODULE
    31173301EC_GROUP *EC_GROUP_new_by_curve_name(int nid)
    31183302{
    3119     size_t i;
    3120     EC_GROUP *ret = NULL;
    3121 
    3122     if (nid <= 0)
    3123         return NULL;
    3124 
    3125     for (i = 0; i < curve_list_length; i++)
    3126         if (curve_list[i].nid == nid) {
    3127             ret = ec_group_new_from_data(curve_list[i]);
    3128             break;
    3129         }
    3130 
    3131     if (ret == NULL) {
    3132         ECerr(EC_F_EC_GROUP_NEW_BY_CURVE_NAME, EC_R_UNKNOWN_GROUP);
    3133         return NULL;
    3134     }
    3135 
    3136     return ret;
     3303    return EC_GROUP_new_by_curve_name_ex(NULL, NULL, nid);
    31373304}
     3305#endif
    31383306
    31393307size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems)
     
    31543322}
    31553323
    3156 /* Functions to translate between common NIST curve names and NIDs */
    3157 
    3158 typedef struct {
    3159     const char *name;           /* NIST Name of curve */
    3160     int nid;                    /* Curve NID */
    3161 } EC_NIST_NAME;
    3162 
    3163 static EC_NIST_NAME nist_curves[] = {
    3164     {"B-163", NID_sect163r2},
    3165     {"B-233", NID_sect233r1},
    3166     {"B-283", NID_sect283r1},
    3167     {"B-409", NID_sect409r1},
    3168     {"B-571", NID_sect571r1},
    3169     {"K-163", NID_sect163k1},
    3170     {"K-233", NID_sect233k1},
    3171     {"K-283", NID_sect283k1},
    3172     {"K-409", NID_sect409k1},
    3173     {"K-571", NID_sect571k1},
    3174     {"P-192", NID_X9_62_prime192v1},
    3175     {"P-224", NID_secp224r1},
    3176     {"P-256", NID_X9_62_prime256v1},
    3177     {"P-384", NID_secp384r1},
    3178     {"P-521", NID_secp521r1}
    3179 };
    3180 
    31813324const char *EC_curve_nid2nist(int nid)
    31823325{
    3183     size_t i;
    3184     for (i = 0; i < OSSL_NELEM(nist_curves); i++) {
    3185         if (nist_curves[i].nid == nid)
    3186             return nist_curves[i].name;
    3187     }
    3188     return NULL;
     3326    return ossl_ec_curve_nid2nist_int(nid);
    31893327}
    31903328
    31913329int EC_curve_nist2nid(const char *name)
    31923330{
    3193     size_t i;
    3194     for (i = 0; i < OSSL_NELEM(nist_curves); i++) {
    3195         if (strcmp(nist_curves[i].name, name) == 0)
    3196             return nist_curves[i].nid;
    3197     }
    3198     return NID_undef;
     3331    return ossl_ec_curve_nist2nid_int(name);
    31993332}
    32003333
     
    32083341 *          if not found. If there was an error it returns -1.
    32093342 */
    3210 int ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx)
     3343int ossl_ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx)
    32113344{
    32123345    int ret = -1, nid, len, field_type, param_len;
     
    32163349    const EC_CURVE_DATA *data;
    32173350    const EC_POINT *generator = NULL;
    3218     const EC_METHOD *meth;
    32193351    const BIGNUM *cofactor = NULL;
    32203352    /* An array of BIGNUMs for (p, a, b, x, y, order) */
    32213353    BIGNUM *bn[NUM_BN_FIELDS] = {NULL, NULL, NULL, NULL, NULL, NULL};
    32223354
    3223     meth = EC_GROUP_method_of(group);
    3224     if (meth == NULL)
    3225         return -1;
    32263355    /* Use the optional named curve nid as a search field */
    32273356    nid = EC_GROUP_get_curve_name(group);
    3228     field_type = EC_METHOD_get_field_type(meth);
     3357    field_type = EC_GROUP_get_field_type(group);
    32293358    seed_len = EC_GROUP_get_seed_len(group);
    32303359    seed = EC_GROUP_get0_seed(group);
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_cvt.c

    r91772 r94082  
    11/*
    2  * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    99 */
    1010
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
     16
    1117#include <openssl/err.h>
     18#include "crypto/bn.h"
    1219#include "ec_local.h"
    1320
     
    4855#endif
    4956
    50     ret = EC_GROUP_new(meth);
     57    ret = ossl_ec_group_new_ex(ossl_bn_get_libctx(ctx), NULL, meth);
    5158    if (ret == NULL)
    5259        return NULL;
    5360
    5461    if (!EC_GROUP_set_curve(ret, p, a, b, ctx)) {
    55         EC_GROUP_clear_free(ret);
     62        EC_GROUP_free(ret);
    5663        return NULL;
    5764    }
     
    6976    meth = EC_GF2m_simple_method();
    7077
    71     ret = EC_GROUP_new(meth);
     78    ret = ossl_ec_group_new_ex(ossl_bn_get_libctx(ctx), NULL, meth);
    7279    if (ret == NULL)
    7380        return NULL;
    7481
    7582    if (!EC_GROUP_set_curve(ret, p, a, b, ctx)) {
    76         EC_GROUP_clear_free(ret);
     83        EC_GROUP_free(ret);
    7784        return NULL;
    7885    }
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_err.c

    r91772 r94082  
    11/*
    22 * Generated by util/mkerr.pl DO NOT EDIT
    3  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
     3 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    1111#include <openssl/err.h>
    1212#include <openssl/ecerr.h>
     13#include "crypto/ecerr.h"
    1314
    14 #ifndef OPENSSL_NO_ERR
     15#ifndef OPENSSL_NO_EC
    1516
    16 static const ERR_STRING_DATA EC_str_functs[] = {
    17     {ERR_PACK(ERR_LIB_EC, EC_F_BN_TO_FELEM, 0), "BN_to_felem"},
    18     {ERR_PACK(ERR_LIB_EC, EC_F_D2I_ECPARAMETERS, 0), "d2i_ECParameters"},
    19     {ERR_PACK(ERR_LIB_EC, EC_F_D2I_ECPKPARAMETERS, 0), "d2i_ECPKParameters"},
    20     {ERR_PACK(ERR_LIB_EC, EC_F_D2I_ECPRIVATEKEY, 0), "d2i_ECPrivateKey"},
    21     {ERR_PACK(ERR_LIB_EC, EC_F_DO_EC_KEY_PRINT, 0), "do_EC_KEY_print"},
    22     {ERR_PACK(ERR_LIB_EC, EC_F_ECDH_CMS_DECRYPT, 0), "ecdh_cms_decrypt"},
    23     {ERR_PACK(ERR_LIB_EC, EC_F_ECDH_CMS_SET_SHARED_INFO, 0),
    24      "ecdh_cms_set_shared_info"},
    25     {ERR_PACK(ERR_LIB_EC, EC_F_ECDH_COMPUTE_KEY, 0), "ECDH_compute_key"},
    26     {ERR_PACK(ERR_LIB_EC, EC_F_ECDH_SIMPLE_COMPUTE_KEY, 0),
    27      "ecdh_simple_compute_key"},
    28     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_DO_SIGN_EX, 0), "ECDSA_do_sign_ex"},
    29     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_DO_VERIFY, 0), "ECDSA_do_verify"},
    30     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_SIGN_EX, 0), "ECDSA_sign_ex"},
    31     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_SIGN_SETUP, 0), "ECDSA_sign_setup"},
    32     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_SIG_NEW, 0), "ECDSA_SIG_new"},
    33     {ERR_PACK(ERR_LIB_EC, EC_F_ECDSA_VERIFY, 0), "ECDSA_verify"},
    34     {ERR_PACK(ERR_LIB_EC, EC_F_ECD_ITEM_VERIFY, 0), "ecd_item_verify"},
    35     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_PARAM2TYPE, 0), "eckey_param2type"},
    36     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_PARAM_DECODE, 0), "eckey_param_decode"},
    37     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_PRIV_DECODE, 0), "eckey_priv_decode"},
    38     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_PRIV_ENCODE, 0), "eckey_priv_encode"},
    39     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_PUB_DECODE, 0), "eckey_pub_decode"},
    40     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_PUB_ENCODE, 0), "eckey_pub_encode"},
    41     {ERR_PACK(ERR_LIB_EC, EC_F_ECKEY_TYPE2PARAM, 0), "eckey_type2param"},
    42     {ERR_PACK(ERR_LIB_EC, EC_F_ECPARAMETERS_PRINT, 0), "ECParameters_print"},
    43     {ERR_PACK(ERR_LIB_EC, EC_F_ECPARAMETERS_PRINT_FP, 0),
    44      "ECParameters_print_fp"},
    45     {ERR_PACK(ERR_LIB_EC, EC_F_ECPKPARAMETERS_PRINT, 0),
    46      "ECPKParameters_print"},
    47     {ERR_PACK(ERR_LIB_EC, EC_F_ECPKPARAMETERS_PRINT_FP, 0),
    48      "ECPKParameters_print_fp"},
    49     {ERR_PACK(ERR_LIB_EC, EC_F_ECP_NISTZ256_GET_AFFINE, 0),
    50      "ecp_nistz256_get_affine"},
    51     {ERR_PACK(ERR_LIB_EC, EC_F_ECP_NISTZ256_INV_MOD_ORD, 0),
    52      "ecp_nistz256_inv_mod_ord"},
    53     {ERR_PACK(ERR_LIB_EC, EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, 0),
    54      "ecp_nistz256_mult_precompute"},
    55     {ERR_PACK(ERR_LIB_EC, EC_F_ECP_NISTZ256_POINTS_MUL, 0),
    56      "ecp_nistz256_points_mul"},
    57     {ERR_PACK(ERR_LIB_EC, EC_F_ECP_NISTZ256_PRE_COMP_NEW, 0),
    58      "ecp_nistz256_pre_comp_new"},
    59     {ERR_PACK(ERR_LIB_EC, EC_F_ECP_NISTZ256_WINDOWED_MUL, 0),
    60      "ecp_nistz256_windowed_mul"},
    61     {ERR_PACK(ERR_LIB_EC, EC_F_ECX_KEY_OP, 0), "ecx_key_op"},
    62     {ERR_PACK(ERR_LIB_EC, EC_F_ECX_PRIV_ENCODE, 0), "ecx_priv_encode"},
    63     {ERR_PACK(ERR_LIB_EC, EC_F_ECX_PUB_ENCODE, 0), "ecx_pub_encode"},
    64     {ERR_PACK(ERR_LIB_EC, EC_F_EC_ASN1_GROUP2CURVE, 0), "ec_asn1_group2curve"},
    65     {ERR_PACK(ERR_LIB_EC, EC_F_EC_ASN1_GROUP2FIELDID, 0),
    66      "ec_asn1_group2fieldid"},
    67     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY, 0),
    68      "ec_GF2m_montgomery_point_multiply"},
    69     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_FIELD_INV, 0),
    70      "ec_GF2m_simple_field_inv"},
    71     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT, 0),
    72      "ec_GF2m_simple_group_check_discriminant"},
    73     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE, 0),
    74      "ec_GF2m_simple_group_set_curve"},
    75     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_LADDER_POST, 0),
    76      "ec_GF2m_simple_ladder_post"},
    77     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_LADDER_PRE, 0),
    78      "ec_GF2m_simple_ladder_pre"},
    79     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_OCT2POINT, 0),
    80      "ec_GF2m_simple_oct2point"},
    81     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_POINT2OCT, 0),
    82      "ec_GF2m_simple_point2oct"},
    83     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_POINTS_MUL, 0),
    84      "ec_GF2m_simple_points_mul"},
    85     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES, 0),
    86      "ec_GF2m_simple_point_get_affine_coordinates"},
    87     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES, 0),
    88      "ec_GF2m_simple_point_set_affine_coordinates"},
    89     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, 0),
    90      "ec_GF2m_simple_set_compressed_coordinates"},
    91     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_MONT_FIELD_DECODE, 0),
    92      "ec_GFp_mont_field_decode"},
    93     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_MONT_FIELD_ENCODE, 0),
    94      "ec_GFp_mont_field_encode"},
    95     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_MONT_FIELD_INV, 0),
    96      "ec_GFp_mont_field_inv"},
    97     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_MONT_FIELD_MUL, 0),
    98      "ec_GFp_mont_field_mul"},
    99     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, 0),
    100      "ec_GFp_mont_field_set_to_one"},
    101     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_MONT_FIELD_SQR, 0),
    102      "ec_GFp_mont_field_sqr"},
    103     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_MONT_GROUP_SET_CURVE, 0),
    104      "ec_GFp_mont_group_set_curve"},
    105     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP224_GROUP_SET_CURVE, 0),
    106      "ec_GFp_nistp224_group_set_curve"},
    107     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP224_POINTS_MUL, 0),
    108      "ec_GFp_nistp224_points_mul"},
    109     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES, 0),
    110      "ec_GFp_nistp224_point_get_affine_coordinates"},
    111     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP256_GROUP_SET_CURVE, 0),
    112      "ec_GFp_nistp256_group_set_curve"},
    113     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP256_POINTS_MUL, 0),
    114      "ec_GFp_nistp256_points_mul"},
    115     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES, 0),
    116      "ec_GFp_nistp256_point_get_affine_coordinates"},
    117     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP521_GROUP_SET_CURVE, 0),
    118      "ec_GFp_nistp521_group_set_curve"},
    119     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP521_POINTS_MUL, 0),
    120      "ec_GFp_nistp521_points_mul"},
    121     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES, 0),
    122      "ec_GFp_nistp521_point_get_affine_coordinates"},
    123     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NIST_FIELD_MUL, 0),
    124      "ec_GFp_nist_field_mul"},
    125     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NIST_FIELD_SQR, 0),
    126      "ec_GFp_nist_field_sqr"},
    127     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_NIST_GROUP_SET_CURVE, 0),
    128      "ec_GFp_nist_group_set_curve"},
    129     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, 0),
    130      "ec_GFp_simple_blind_coordinates"},
    131     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_FIELD_INV, 0),
    132      "ec_GFp_simple_field_inv"},
    133     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT, 0),
    134      "ec_GFp_simple_group_check_discriminant"},
    135     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, 0),
    136      "ec_GFp_simple_group_set_curve"},
    137     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, 0),
    138      "ec_GFp_simple_make_affine"},
    139     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_OCT2POINT, 0),
    140      "ec_GFp_simple_oct2point"},
    141     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_POINT2OCT, 0),
    142      "ec_GFp_simple_point2oct"},
    143     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, 0),
    144      "ec_GFp_simple_points_make_affine"},
    145     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, 0),
    146      "ec_GFp_simple_point_get_affine_coordinates"},
    147     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES, 0),
    148      "ec_GFp_simple_point_set_affine_coordinates"},
    149     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES, 0),
    150      "ec_GFp_simple_set_compressed_coordinates"},
    151     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_CHECK, 0), "EC_GROUP_check"},
    152     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_CHECK_DISCRIMINANT, 0),
    153      "EC_GROUP_check_discriminant"},
    154     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_COPY, 0), "EC_GROUP_copy"},
    155     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_GET_CURVE, 0), "EC_GROUP_get_curve"},
    156     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_GET_CURVE_GF2M, 0),
    157      "EC_GROUP_get_curve_GF2m"},
    158     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_GET_CURVE_GFP, 0),
    159      "EC_GROUP_get_curve_GFp"},
    160     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_GET_DEGREE, 0), "EC_GROUP_get_degree"},
    161     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_GET_ECPARAMETERS, 0),
    162      "EC_GROUP_get_ecparameters"},
    163     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_GET_ECPKPARAMETERS, 0),
    164      "EC_GROUP_get_ecpkparameters"},
    165     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS, 0),
    166      "EC_GROUP_get_pentanomial_basis"},
    167     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_GET_TRINOMIAL_BASIS, 0),
    168      "EC_GROUP_get_trinomial_basis"},
    169     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_NEW, 0), "EC_GROUP_new"},
    170     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_NEW_BY_CURVE_NAME, 0),
    171      "EC_GROUP_new_by_curve_name"},
    172     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_NEW_FROM_DATA, 0),
    173      "ec_group_new_from_data"},
    174     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_NEW_FROM_ECPARAMETERS, 0),
    175      "EC_GROUP_new_from_ecparameters"},
    176     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_NEW_FROM_ECPKPARAMETERS, 0),
    177      "EC_GROUP_new_from_ecpkparameters"},
    178     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_SET_CURVE, 0), "EC_GROUP_set_curve"},
    179     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_SET_CURVE_GF2M, 0),
    180      "EC_GROUP_set_curve_GF2m"},
    181     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_SET_CURVE_GFP, 0),
    182      "EC_GROUP_set_curve_GFp"},
    183     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_SET_GENERATOR, 0),
    184      "EC_GROUP_set_generator"},
    185     {ERR_PACK(ERR_LIB_EC, EC_F_EC_GROUP_SET_SEED, 0), "EC_GROUP_set_seed"},
    186     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_CHECK_KEY, 0), "EC_KEY_check_key"},
    187     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_COPY, 0), "EC_KEY_copy"},
    188     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_GENERATE_KEY, 0), "EC_KEY_generate_key"},
    189     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_NEW, 0), "EC_KEY_new"},
    190     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_NEW_METHOD, 0), "EC_KEY_new_method"},
    191     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_OCT2PRIV, 0), "EC_KEY_oct2priv"},
    192     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_PRINT, 0), "EC_KEY_print"},
    193     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_PRINT_FP, 0), "EC_KEY_print_fp"},
    194     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_PRIV2BUF, 0), "EC_KEY_priv2buf"},
    195     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_PRIV2OCT, 0), "EC_KEY_priv2oct"},
    196     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES, 0),
    197      "EC_KEY_set_public_key_affine_coordinates"},
    198     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_SIMPLE_CHECK_KEY, 0),
    199      "ec_key_simple_check_key"},
    200     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_SIMPLE_OCT2PRIV, 0),
    201      "ec_key_simple_oct2priv"},
    202     {ERR_PACK(ERR_LIB_EC, EC_F_EC_KEY_SIMPLE_PRIV2OCT, 0),
    203      "ec_key_simple_priv2oct"},
    204     {ERR_PACK(ERR_LIB_EC, EC_F_EC_PKEY_CHECK, 0), "ec_pkey_check"},
    205     {ERR_PACK(ERR_LIB_EC, EC_F_EC_PKEY_PARAM_CHECK, 0), "ec_pkey_param_check"},
    206     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINTS_MAKE_AFFINE, 0),
    207      "EC_POINTs_make_affine"},
    208     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINTS_MUL, 0), "EC_POINTs_mul"},
    209     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_ADD, 0), "EC_POINT_add"},
    210     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_BN2POINT, 0), "EC_POINT_bn2point"},
    211     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_CMP, 0), "EC_POINT_cmp"},
    212     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_COPY, 0), "EC_POINT_copy"},
    213     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_DBL, 0), "EC_POINT_dbl"},
    214     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_GET_AFFINE_COORDINATES, 0),
    215      "EC_POINT_get_affine_coordinates"},
    216     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, 0),
    217      "EC_POINT_get_affine_coordinates_GF2m"},
    218     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, 0),
    219      "EC_POINT_get_affine_coordinates_GFp"},
    220     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, 0),
    221      "EC_POINT_get_Jprojective_coordinates_GFp"},
    222     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_INVERT, 0), "EC_POINT_invert"},
    223     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_IS_AT_INFINITY, 0),
    224      "EC_POINT_is_at_infinity"},
    225     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_IS_ON_CURVE, 0),
    226      "EC_POINT_is_on_curve"},
    227     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_MAKE_AFFINE, 0),
    228      "EC_POINT_make_affine"},
    229     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_NEW, 0), "EC_POINT_new"},
    230     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_OCT2POINT, 0), "EC_POINT_oct2point"},
    231     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_POINT2BUF, 0), "EC_POINT_point2buf"},
    232     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_POINT2OCT, 0), "EC_POINT_point2oct"},
    233     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_SET_AFFINE_COORDINATES, 0),
    234      "EC_POINT_set_affine_coordinates"},
    235     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, 0),
    236      "EC_POINT_set_affine_coordinates_GF2m"},
    237     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, 0),
    238      "EC_POINT_set_affine_coordinates_GFp"},
    239     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_SET_COMPRESSED_COORDINATES, 0),
    240      "EC_POINT_set_compressed_coordinates"},
    241     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M, 0),
    242      "EC_POINT_set_compressed_coordinates_GF2m"},
    243     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP, 0),
    244      "EC_POINT_set_compressed_coordinates_GFp"},
    245     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, 0),
    246      "EC_POINT_set_Jprojective_coordinates_GFp"},
    247     {ERR_PACK(ERR_LIB_EC, EC_F_EC_POINT_SET_TO_INFINITY, 0),
    248      "EC_POINT_set_to_infinity"},
    249     {ERR_PACK(ERR_LIB_EC, EC_F_EC_PRE_COMP_NEW, 0), "ec_pre_comp_new"},
    250     {ERR_PACK(ERR_LIB_EC, EC_F_EC_SCALAR_MUL_LADDER, 0),
    251      "ec_scalar_mul_ladder"},
    252     {ERR_PACK(ERR_LIB_EC, EC_F_EC_WNAF_MUL, 0), "ec_wNAF_mul"},
    253     {ERR_PACK(ERR_LIB_EC, EC_F_EC_WNAF_PRECOMPUTE_MULT, 0),
    254      "ec_wNAF_precompute_mult"},
    255     {ERR_PACK(ERR_LIB_EC, EC_F_I2D_ECPARAMETERS, 0), "i2d_ECParameters"},
    256     {ERR_PACK(ERR_LIB_EC, EC_F_I2D_ECPKPARAMETERS, 0), "i2d_ECPKParameters"},
    257     {ERR_PACK(ERR_LIB_EC, EC_F_I2D_ECPRIVATEKEY, 0), "i2d_ECPrivateKey"},
    258     {ERR_PACK(ERR_LIB_EC, EC_F_I2O_ECPUBLICKEY, 0), "i2o_ECPublicKey"},
    259     {ERR_PACK(ERR_LIB_EC, EC_F_NISTP224_PRE_COMP_NEW, 0),
    260      "nistp224_pre_comp_new"},
    261     {ERR_PACK(ERR_LIB_EC, EC_F_NISTP256_PRE_COMP_NEW, 0),
    262      "nistp256_pre_comp_new"},
    263     {ERR_PACK(ERR_LIB_EC, EC_F_NISTP521_PRE_COMP_NEW, 0),
    264      "nistp521_pre_comp_new"},
    265     {ERR_PACK(ERR_LIB_EC, EC_F_O2I_ECPUBLICKEY, 0), "o2i_ECPublicKey"},
    266     {ERR_PACK(ERR_LIB_EC, EC_F_OLD_EC_PRIV_DECODE, 0), "old_ec_priv_decode"},
    267     {ERR_PACK(ERR_LIB_EC, EC_F_OSSL_ECDH_COMPUTE_KEY, 0),
    268      "ossl_ecdh_compute_key"},
    269     {ERR_PACK(ERR_LIB_EC, EC_F_OSSL_ECDSA_SIGN_SIG, 0), "ossl_ecdsa_sign_sig"},
    270     {ERR_PACK(ERR_LIB_EC, EC_F_OSSL_ECDSA_VERIFY_SIG, 0),
    271      "ossl_ecdsa_verify_sig"},
    272     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_ECD_CTRL, 0), "pkey_ecd_ctrl"},
    273     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_ECD_DIGESTSIGN, 0), "pkey_ecd_digestsign"},
    274     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_ECD_DIGESTSIGN25519, 0),
    275      "pkey_ecd_digestsign25519"},
    276     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_ECD_DIGESTSIGN448, 0),
    277      "pkey_ecd_digestsign448"},
    278     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_ECX_DERIVE, 0), "pkey_ecx_derive"},
    279     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_CTRL, 0), "pkey_ec_ctrl"},
    280     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_CTRL_STR, 0), "pkey_ec_ctrl_str"},
    281     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_DERIVE, 0), "pkey_ec_derive"},
    282     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_INIT, 0), "pkey_ec_init"},
    283     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_KDF_DERIVE, 0), "pkey_ec_kdf_derive"},
    284     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_KEYGEN, 0), "pkey_ec_keygen"},
    285     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_PARAMGEN, 0), "pkey_ec_paramgen"},
    286     {ERR_PACK(ERR_LIB_EC, EC_F_PKEY_EC_SIGN, 0), "pkey_ec_sign"},
    287     {ERR_PACK(ERR_LIB_EC, EC_F_VALIDATE_ECX_DERIVE, 0), "validate_ecx_derive"},
    288     {0, NULL}
    289 };
     17# ifndef OPENSSL_NO_ERR
    29018
    29119static const ERR_STRING_DATA EC_str_reasons[] = {
     
    29927    {ERR_PACK(ERR_LIB_EC, 0, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH),
    30028    "curve does not support ecdh"},
     29    {ERR_PACK(ERR_LIB_EC, 0, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA),
     30    "curve does not support ecdsa"},
    30131    {ERR_PACK(ERR_LIB_EC, 0, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING),
    30232    "curve does not support signing"},
    303     {ERR_PACK(ERR_LIB_EC, 0, EC_R_D2I_ECPKPARAMETERS_FAILURE),
    304     "d2i ecpkparameters failure"},
    30533    {ERR_PACK(ERR_LIB_EC, 0, EC_R_DECODE_ERROR), "decode error"},
    30634    {ERR_PACK(ERR_LIB_EC, 0, EC_R_DISCRIMINANT_IS_ZERO),
     
    30836    {ERR_PACK(ERR_LIB_EC, 0, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE),
    30937    "ec group new by name failure"},
     38    {ERR_PACK(ERR_LIB_EC, 0, EC_R_FAILED_MAKING_PUBLIC_KEY),
     39    "failed making public key"},
    31040    {ERR_PACK(ERR_LIB_EC, 0, EC_R_FIELD_TOO_LARGE), "field too large"},
    31141    {ERR_PACK(ERR_LIB_EC, 0, EC_R_GF2M_NOT_SUPPORTED), "gf2m not supported"},
     
    31646    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INCOMPATIBLE_OBJECTS),
    31747    "incompatible objects"},
     48    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_A), "invalid a"},
    31849    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_ARGUMENT), "invalid argument"},
     50    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_B), "invalid b"},
     51    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_COFACTOR), "invalid cofactor"},
    31952    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_COMPRESSED_POINT),
    32053    "invalid compressed point"},
     
    32760    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_FIELD), "invalid field"},
    32861    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_FORM), "invalid form"},
     62    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_GENERATOR), "invalid generator"},
    32963    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_GROUP_ORDER), "invalid group order"},
    33064    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_KEY), "invalid key"},
     65    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_LENGTH), "invalid length"},
     66    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_NAMED_GROUP_CONVERSION),
     67    "invalid named group conversion"},
    33168    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_OUTPUT_LENGTH),
    33269    "invalid output length"},
     70    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_P), "invalid p"},
    33371    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_PEER_KEY), "invalid peer key"},
    33472    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_PENTANOMIAL_BASIS),
    33573    "invalid pentanomial basis"},
    33674    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_PRIVATE_KEY), "invalid private key"},
     75    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_SEED), "invalid seed"},
    33776    {ERR_PACK(ERR_LIB_EC, 0, EC_R_INVALID_TRINOMIAL_BASIS),
    33877    "invalid trinomial basis"},
     
    35796    "passed null parameter"},
    35897    {ERR_PACK(ERR_LIB_EC, 0, EC_R_PEER_KEY_ERROR), "peer key error"},
    359     {ERR_PACK(ERR_LIB_EC, 0, EC_R_PKPARAMETERS2GROUP_FAILURE),
    360     "pkparameters2group failure"},
    36198    {ERR_PACK(ERR_LIB_EC, 0, EC_R_POINT_ARITHMETIC_FAILURE),
    36299    "point arithmetic failure"},
     
    382119};
    383120
    384 #endif
     121# endif
    385122
    386 int ERR_load_EC_strings(void)
     123int ossl_err_load_EC_strings(void)
    387124{
    388 #ifndef OPENSSL_NO_ERR
    389     if (ERR_func_error_string(EC_str_functs[0].error) == NULL) {
    390         ERR_load_strings_const(EC_str_functs);
     125# ifndef OPENSSL_NO_ERR
     126    if (ERR_reason_error_string(EC_str_reasons[0].error) == NULL)
    391127        ERR_load_strings_const(EC_str_reasons);
    392     }
    393 #endif
     128# endif
    394129    return 1;
    395130}
     131#else
     132NON_EMPTY_TRANSLATION_UNIT
     133#endif
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_key.c

    r91772 r94082  
    11/*
    2  * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
    88 * https://www.openssl.org/source/license.html
    99 */
     10
     11/*
     12 * EC_KEY low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
    1016
    1117#include "internal/cryptlib.h"
     
    1420#include "internal/refcount.h"
    1521#include <openssl/err.h>
    16 #include <openssl/engine.h>
     22#ifndef FIPS_MODULE
     23# include <openssl/engine.h>
     24#endif
     25#include <openssl/self_test.h>
     26#include "prov/providercommon.h"
    1727#include "crypto/bn.h"
    1828
     29static int ecdsa_keygen_pairwise_test(EC_KEY *eckey, OSSL_CALLBACK *cb,
     30                                      void *cbarg);
     31
     32#ifndef FIPS_MODULE
    1933EC_KEY *EC_KEY_new(void)
    2034{
    21     return EC_KEY_new_method(NULL);
    22 }
    23 
    24 EC_KEY *EC_KEY_new_by_curve_name(int nid)
    25 {
    26     EC_KEY *ret = EC_KEY_new();
     35    return ossl_ec_key_new_method_int(NULL, NULL, NULL);
     36}
     37#endif
     38
     39EC_KEY *EC_KEY_new_ex(OSSL_LIB_CTX *ctx, const char *propq)
     40{
     41    return ossl_ec_key_new_method_int(ctx, propq, NULL);
     42}
     43
     44EC_KEY *EC_KEY_new_by_curve_name_ex(OSSL_LIB_CTX *ctx, const char *propq,
     45                                    int nid)
     46{
     47    EC_KEY *ret = EC_KEY_new_ex(ctx, propq);
    2748    if (ret == NULL)
    2849        return NULL;
    29     ret->group = EC_GROUP_new_by_curve_name(nid);
     50    ret->group = EC_GROUP_new_by_curve_name_ex(ctx, propq, nid);
    3051    if (ret->group == NULL) {
    3152        EC_KEY_free(ret);
     
    3960    return ret;
    4061}
     62
     63#ifndef FIPS_MODULE
     64EC_KEY *EC_KEY_new_by_curve_name(int nid)
     65{
     66    return EC_KEY_new_by_curve_name_ex(NULL, NULL, nid);
     67}
     68#endif
    4169
    4270void EC_KEY_free(EC_KEY *r)
     
    5684        r->meth->finish(r);
    5785
    58 #ifndef OPENSSL_NO_ENGINE
     86#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
    5987    ENGINE_finish(r->engine);
    6088#endif
     
    6391        r->group->meth->keyfinish(r);
    6492
     93#ifndef FIPS_MODULE
    6594    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EC_KEY, r, &r->ex_data);
     95#endif
    6696    CRYPTO_THREAD_lock_free(r->lock);
    6797    EC_GROUP_free(r->group);
    6898    EC_POINT_free(r->pub_key);
    6999    BN_clear_free(r->priv_key);
     100    OPENSSL_free(r->propq);
    70101
    71102    OPENSSL_clear_free((void *)r, sizeof(EC_KEY));
     
    75106{
    76107    if (dest == NULL || src == NULL) {
    77         ECerr(EC_F_EC_KEY_COPY, ERR_R_PASSED_NULL_PARAMETER);
     108        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    78109        return NULL;
    79110    }
     
    83114        if (dest->group && dest->group->meth->keyfinish)
    84115            dest->group->meth->keyfinish(dest);
    85 #ifndef OPENSSL_NO_ENGINE
     116#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
    86117        if (ENGINE_finish(dest->engine) == 0)
    87118            return 0;
     
    89120#endif
    90121    }
     122    dest->libctx = src->libctx;
    91123    /* copy the parameters */
    92124    if (src->group != NULL) {
    93         const EC_METHOD *meth = EC_GROUP_method_of(src->group);
    94125        /* clear the old group */
    95126        EC_GROUP_free(dest->group);
    96         dest->group = EC_GROUP_new(meth);
     127        dest->group = ossl_ec_group_new_ex(src->libctx, src->propq,
     128                                           src->group->meth);
    97129        if (dest->group == NULL)
    98130            return NULL;
     
    130162    dest->version = src->version;
    131163    dest->flags = src->flags;
     164#ifndef FIPS_MODULE
    132165    if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EC_KEY,
    133166                            &dest->ex_data, &src->ex_data))
    134167        return NULL;
     168#endif
    135169
    136170    if (src->meth != dest->meth) {
    137 #ifndef OPENSSL_NO_ENGINE
     171#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
    138172        if (src->engine != NULL && ENGINE_init(src->engine) == 0)
    139173            return NULL;
     
    146180        return NULL;
    147181
     182    dest->dirty_cnt++;
     183
    148184    return dest;
    149185}
     
    151187EC_KEY *EC_KEY_dup(const EC_KEY *ec_key)
    152188{
    153     EC_KEY *ret = EC_KEY_new_method(ec_key->engine);
    154 
    155     if (ret == NULL)
    156         return NULL;
    157 
    158     if (EC_KEY_copy(ret, ec_key) == NULL) {
    159         EC_KEY_free(ret);
    160         return NULL;
    161     }
    162     return ret;
     189    return ossl_ec_key_dup(ec_key, OSSL_KEYMGMT_SELECT_ALL);
    163190}
    164191
     
    183210{
    184211    if (eckey == NULL || eckey->group == NULL) {
    185         ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
    186         return 0;
    187     }
    188     if (eckey->meth->keygen != NULL)
    189         return eckey->meth->keygen(eckey);
    190     ECerr(EC_F_EC_KEY_GENERATE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
     212        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
     213        return 0;
     214    }
     215    if (eckey->meth->keygen != NULL) {
     216        int ret;
     217
     218        ret = eckey->meth->keygen(eckey);
     219        if (ret == 1)
     220            eckey->dirty_cnt++;
     221
     222        return ret;
     223    }
     224    ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
    191225    return 0;
    192226}
     
    194228int ossl_ec_key_gen(EC_KEY *eckey)
    195229{
    196     return eckey->group->meth->keygen(eckey);
    197 }
    198 
    199 int ec_key_simple_generate_key(EC_KEY *eckey)
     230    int ret;
     231
     232    ret = eckey->group->meth->keygen(eckey);
     233
     234    if (ret == 1)
     235        eckey->dirty_cnt++;
     236    return ret;
     237}
     238
     239/*
     240 * ECC Key generation.
     241 * See SP800-56AR3 5.6.1.2.2 "Key Pair Generation by Testing Candidates"
     242 *
     243 * Params:
     244 *     libctx A context containing an optional self test callback.
     245 *     eckey An EC key object that contains domain params. The generated keypair
     246 *           is stored in this object.
     247 *     pairwise_test Set to non zero to perform a pairwise test. If the test
     248 *                   fails then the keypair is not generated,
     249 * Returns 1 if the keypair was generated or 0 otherwise.
     250 */
     251static int ec_generate_key(EC_KEY *eckey, int pairwise_test)
    200252{
    201253    int ok = 0;
    202     BN_CTX *ctx = NULL;
    203254    BIGNUM *priv_key = NULL;
    204     const BIGNUM *order = NULL;
     255    const BIGNUM *tmp = NULL;
     256    BIGNUM *order = NULL;
    205257    EC_POINT *pub_key = NULL;
    206 
    207     if ((ctx = BN_CTX_new()) == NULL)
     258    const EC_GROUP *group = eckey->group;
     259    BN_CTX *ctx = BN_CTX_secure_new_ex(eckey->libctx);
     260    int sm2 = EC_KEY_get_flags(eckey) & EC_FLAG_SM2_RANGE ? 1 : 0;
     261
     262    if (ctx == NULL)
    208263        goto err;
    209264
    210265    if (eckey->priv_key == NULL) {
    211         priv_key = BN_new();
     266        priv_key = BN_secure_new();
    212267        if (priv_key == NULL)
    213268            goto err;
     
    215270        priv_key = eckey->priv_key;
    216271
    217     order = EC_GROUP_get0_order(eckey->group);
    218     if (order == NULL)
    219         goto err;
     272    /*
     273     * Steps (1-2): Check domain parameters and security strength.
     274     * These steps must be done by the user. This would need to be
     275     * stated in the security policy.
     276     */
     277
     278    tmp = EC_GROUP_get0_order(group);
     279    if (tmp == NULL)
     280        goto err;
     281
     282    /*
     283     * Steps (3-7): priv_key = DRBG_RAND(order_n_bits) (range [1, n-1]).
     284     * Although this is slightly different from the standard, it is effectively
     285     * equivalent as it gives an unbiased result ranging from 1..n-1. It is also
     286     * faster as the standard needs to retry more often. Also doing
     287     * 1 + rand[0..n-2] would effect the way that tests feed dummy entropy into
     288     * rand so the simpler backward compatible method has been used here.
     289     */
     290
     291    /* range of SM2 private key is [1, n-1) */
     292    if (sm2) {
     293        order = BN_new();
     294        if (order == NULL || !BN_sub(order, tmp, BN_value_one()))
     295            goto err;
     296    } else {
     297        order = BN_dup(tmp);
     298        if (order == NULL)
     299            goto err;
     300    }
    220301
    221302    do
    222         if (!BN_priv_rand_range(priv_key, order))
     303        if (!BN_priv_rand_range_ex(priv_key, order, 0, ctx))
    223304            goto err;
    224305    while (BN_is_zero(priv_key)) ;
    225306
    226307    if (eckey->pub_key == NULL) {
    227         pub_key = EC_POINT_new(eckey->group);
     308        pub_key = EC_POINT_new(group);
    228309        if (pub_key == NULL)
    229310            goto err;
     
    231312        pub_key = eckey->pub_key;
    232313
    233     if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx))
     314    /* Step (8) : pub_key = priv_key * G (where G is a point on the curve) */
     315    if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
    234316        goto err;
    235317
    236318    eckey->priv_key = priv_key;
    237319    eckey->pub_key = pub_key;
     320    priv_key = NULL;
     321    pub_key = NULL;
     322
     323    eckey->dirty_cnt++;
     324
     325#ifdef FIPS_MODULE
     326    pairwise_test = 1;
     327#endif /* FIPS_MODULE */
    238328
    239329    ok = 1;
    240 
    241  err:
    242     if (eckey->pub_key == NULL)
    243         EC_POINT_free(pub_key);
    244     if (eckey->priv_key != priv_key)
    245         BN_free(priv_key);
     330    if (pairwise_test) {
     331        OSSL_CALLBACK *cb = NULL;
     332        void *cbarg = NULL;
     333
     334        OSSL_SELF_TEST_get_callback(eckey->libctx, &cb, &cbarg);
     335        ok = ecdsa_keygen_pairwise_test(eckey, cb, cbarg);
     336    }
     337err:
     338    /* Step (9): If there is an error return an invalid keypair. */
     339    if (!ok) {
     340        ossl_set_error_state(OSSL_SELF_TEST_TYPE_PCT);
     341        BN_clear(eckey->priv_key);
     342        if (eckey->pub_key != NULL)
     343            EC_POINT_set_to_infinity(group, eckey->pub_key);
     344    }
     345
     346    EC_POINT_free(pub_key);
     347    BN_clear_free(priv_key);
    246348    BN_CTX_free(ctx);
     349    BN_free(order);
    247350    return ok;
    248351}
    249352
    250 int ec_key_simple_generate_public_key(EC_KEY *eckey)
    251 {
    252     return EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
    253                         NULL, NULL);
     353int ossl_ec_key_simple_generate_key(EC_KEY *eckey)
     354{
     355    return ec_generate_key(eckey, 0);
     356}
     357
     358int ossl_ec_key_simple_generate_public_key(EC_KEY *eckey)
     359{
     360    int ret;
     361    BN_CTX *ctx = BN_CTX_new_ex(eckey->libctx);
     362
     363    if (ctx == NULL)
     364        return 0;
     365
     366    /*
     367     * See SP800-56AR3 5.6.1.2.2: Step (8)
     368     * pub_key = priv_key * G (where G is a point on the curve)
     369     */
     370    ret = EC_POINT_mul(eckey->group, eckey->pub_key, eckey->priv_key, NULL,
     371                       NULL, ctx);
     372
     373    BN_CTX_free(ctx);
     374    if (ret == 1)
     375        eckey->dirty_cnt++;
     376
     377    return ret;
    254378}
    255379
     
    257381{
    258382    if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
    259         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
     383        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    260384        return 0;
    261385    }
    262386
    263387    if (eckey->group->meth->keycheck == NULL) {
    264         ECerr(EC_F_EC_KEY_CHECK_KEY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     388        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    265389        return 0;
    266390    }
     
    269393}
    270394
    271 int ec_key_simple_check_key(const EC_KEY *eckey)
     395/*
     396 * Check the range of the EC public key.
     397 * See SP800-56A R3 Section 5.6.2.3.3 (Part 2)
     398 * i.e.
     399 *  - If q = odd prime p: Verify that xQ and yQ are integers in the
     400 *    interval[0, p - 1], OR
     401 *  - If q = 2m: Verify that xQ and yQ are bit strings of length m bits.
     402 * Returns 1 if the public key has a valid range, otherwise it returns 0.
     403 */
     404static int ec_key_public_range_check(BN_CTX *ctx, const EC_KEY *key)
     405{
     406    int ret = 0;
     407    BIGNUM *x, *y;
     408
     409    BN_CTX_start(ctx);
     410    x = BN_CTX_get(ctx);
     411    y = BN_CTX_get(ctx);
     412    if (y == NULL)
     413        goto err;
     414
     415    if (!EC_POINT_get_affine_coordinates(key->group, key->pub_key, x, y, ctx))
     416        goto err;
     417
     418    if (EC_GROUP_get_field_type(key->group) == NID_X9_62_prime_field) {
     419        if (BN_is_negative(x)
     420            || BN_cmp(x, key->group->field) >= 0
     421            || BN_is_negative(y)
     422            || BN_cmp(y, key->group->field) >= 0) {
     423            goto err;
     424        }
     425    } else {
     426        int m = EC_GROUP_get_degree(key->group);
     427        if (BN_num_bits(x) > m || BN_num_bits(y) > m) {
     428            goto err;
     429        }
     430    }
     431    ret = 1;
     432err:
     433    BN_CTX_end(ctx);
     434    return ret;
     435}
     436
     437/*
     438 * ECC Partial Public-Key Validation as specified in SP800-56A R3
     439 * Section 5.6.2.3.4 ECC Partial Public-Key Validation Routine.
     440 */
     441int ossl_ec_key_public_check_quick(const EC_KEY *eckey, BN_CTX *ctx)
     442{
     443    if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
     444        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
     445        return 0;
     446    }
     447
     448    /* 5.6.2.3.3 (Step 1): Q != infinity */
     449    if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
     450        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
     451        return 0;
     452    }
     453
     454    /* 5.6.2.3.3 (Step 2) Test if the public key is in range */
     455    if (!ec_key_public_range_check(ctx, eckey)) {
     456        ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
     457        return 0;
     458    }
     459
     460    /* 5.6.2.3.3 (Step 3) is the pub_key on the elliptic curve */
     461    if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
     462        ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
     463        return 0;
     464    }
     465    return 1;
     466}
     467
     468/*
     469 * ECC Key validation as specified in SP800-56A R3.
     470 * Section 5.6.2.3.3 ECC Full Public-Key Validation Routine.
     471 */
     472int ossl_ec_key_public_check(const EC_KEY *eckey, BN_CTX *ctx)
     473{
     474    int ret = 0;
     475    EC_POINT *point = NULL;
     476    const BIGNUM *order = NULL;
     477
     478    if (!ossl_ec_key_public_check_quick(eckey, ctx))
     479        return 0;
     480
     481    point = EC_POINT_new(eckey->group);
     482    if (point == NULL)
     483        return 0;
     484
     485    order = eckey->group->order;
     486    if (BN_is_zero(order)) {
     487        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
     488        goto err;
     489    }
     490    /* 5.6.2.3.3 (Step 4) : pub_key * order is the point at infinity. */
     491    if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
     492        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
     493        goto err;
     494    }
     495    if (!EC_POINT_is_at_infinity(eckey->group, point)) {
     496        ERR_raise(ERR_LIB_EC, EC_R_WRONG_ORDER);
     497        goto err;
     498    }
     499    ret = 1;
     500err:
     501    EC_POINT_free(point);
     502    return ret;
     503}
     504
     505/*
     506 * ECC Key validation as specified in SP800-56A R3.
     507 * Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
     508 * The private key is in the range [1, order-1]
     509 */
     510int ossl_ec_key_private_check(const EC_KEY *eckey)
     511{
     512    if (eckey == NULL || eckey->group == NULL || eckey->priv_key == NULL) {
     513        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
     514        return 0;
     515    }
     516    if (BN_cmp(eckey->priv_key, BN_value_one()) < 0
     517        || BN_cmp(eckey->priv_key, eckey->group->order) >= 0) {
     518        ERR_raise(ERR_LIB_EC, EC_R_INVALID_PRIVATE_KEY);
     519        return 0;
     520    }
     521    return 1;
     522}
     523
     524/*
     525 * ECC Key validation as specified in SP800-56A R3.
     526 * Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency (b)
     527 * Check if generator * priv_key = pub_key
     528 */
     529int ossl_ec_key_pairwise_check(const EC_KEY *eckey, BN_CTX *ctx)
     530{
     531    int ret = 0;
     532    EC_POINT *point = NULL;
     533
     534    if (eckey == NULL
     535       || eckey->group == NULL
     536       || eckey->pub_key == NULL
     537       || eckey->priv_key == NULL) {
     538        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
     539        return 0;
     540    }
     541
     542    point = EC_POINT_new(eckey->group);
     543    if (point == NULL)
     544        goto err;
     545
     546
     547    if (!EC_POINT_mul(eckey->group, point, eckey->priv_key, NULL, NULL, ctx)) {
     548        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
     549        goto err;
     550    }
     551    if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
     552        ERR_raise(ERR_LIB_EC, EC_R_INVALID_PRIVATE_KEY);
     553        goto err;
     554    }
     555    ret = 1;
     556err:
     557    EC_POINT_free(point);
     558    return ret;
     559}
     560
     561
     562/*
     563 * ECC Key validation as specified in SP800-56A R3.
     564 *    Section 5.6.2.3.3 ECC Full Public-Key Validation
     565 *    Section 5.6.2.1.2 Owner Assurance of Private-Key Validity
     566 *    Section 5.6.2.1.4 Owner Assurance of Pair-wise Consistency
     567 * NOTES:
     568 *    Before calling this method in fips mode, there should be an assurance that
     569 *    an approved elliptic-curve group is used.
     570 * Returns 1 if the key is valid, otherwise it returns 0.
     571 */
     572int ossl_ec_key_simple_check_key(const EC_KEY *eckey)
    272573{
    273574    int ok = 0;
    274575    BN_CTX *ctx = NULL;
    275     const BIGNUM *order = NULL;
    276     EC_POINT *point = NULL;
    277 
    278     if (eckey == NULL || eckey->group == NULL || eckey->pub_key == NULL) {
    279         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_PASSED_NULL_PARAMETER);
    280         return 0;
    281     }
    282 
    283     if (EC_POINT_is_at_infinity(eckey->group, eckey->pub_key)) {
    284         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_AT_INFINITY);
    285         goto err;
    286     }
    287 
    288     if ((ctx = BN_CTX_new()) == NULL)
    289         goto err;
    290     if ((point = EC_POINT_new(eckey->group)) == NULL)
    291         goto err;
    292 
    293     /* testing whether the pub_key is on the elliptic curve */
    294     if (EC_POINT_is_on_curve(eckey->group, eckey->pub_key, ctx) <= 0) {
    295         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_POINT_IS_NOT_ON_CURVE);
    296         goto err;
    297     }
    298     /* testing whether pub_key * order is the point at infinity */
    299     order = eckey->group->order;
    300     if (BN_is_zero(order)) {
    301         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_GROUP_ORDER);
    302         goto err;
    303     }
    304     if (!EC_POINT_mul(eckey->group, point, NULL, eckey->pub_key, order, ctx)) {
    305         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
    306         goto err;
    307     }
    308     if (!EC_POINT_is_at_infinity(eckey->group, point)) {
    309         ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
    310         goto err;
    311     }
    312     /*
    313      * in case the priv_key is present : check if generator * priv_key ==
    314      * pub_key
    315      */
     576
     577    if (eckey == NULL) {
     578        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
     579        return 0;
     580    }
     581    if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL)
     582        return 0;
     583
     584    if (!ossl_ec_key_public_check(eckey, ctx))
     585        goto err;
     586
    316587    if (eckey->priv_key != NULL) {
    317         if (BN_cmp(eckey->priv_key, order) >= 0) {
    318             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_WRONG_ORDER);
     588        if (!ossl_ec_key_private_check(eckey)
     589            || !ossl_ec_key_pairwise_check(eckey, ctx))
    319590            goto err;
    320         }
    321         if (!EC_POINT_mul(eckey->group, point, eckey->priv_key,
    322                           NULL, NULL, ctx)) {
    323             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, ERR_R_EC_LIB);
    324             goto err;
    325         }
    326         if (EC_POINT_cmp(eckey->group, point, eckey->pub_key, ctx) != 0) {
    327             ECerr(EC_F_EC_KEY_SIMPLE_CHECK_KEY, EC_R_INVALID_PRIVATE_KEY);
    328             goto err;
    329         }
    330591    }
    331592    ok = 1;
    332  err:
     593err:
    333594    BN_CTX_free(ctx);
    334     EC_POINT_free(point);
    335595    return ok;
    336596}
     
    345605
    346606    if (key == NULL || key->group == NULL || x == NULL || y == NULL) {
    347         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
    348               ERR_R_PASSED_NULL_PARAMETER);
    349         return 0;
    350     }
    351     ctx = BN_CTX_new();
     607        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
     608        return 0;
     609    }
     610    ctx = BN_CTX_new_ex(key->libctx);
    352611    if (ctx == NULL)
    353612        return 0;
     
    370629
    371630    /*
    372      * Check if retrieved coordinates match originals and are less than field
    373      * order: if not values are out of range.
     631     * Check if retrieved coordinates match originals. The range check is done
     632     * inside EC_KEY_check_key().
    374633     */
    375     if (BN_cmp(x, tx) || BN_cmp(y, ty)
    376         || (BN_cmp(x, key->group->field) >= 0)
    377         || (BN_cmp(y, key->group->field) >= 0)) {
    378         ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES,
    379               EC_R_COORDINATES_OUT_OF_RANGE);
    380         goto err;
    381     }
    382 
     634    if (BN_cmp(x, tx) || BN_cmp(y, ty)) {
     635        ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
     636        goto err;
     637    }
     638
     639    /* EC_KEY_set_public_key updates dirty_cnt */
    383640    if (!EC_KEY_set_public_key(key, point))
    384641        goto err;
     
    397654}
    398655
     656OSSL_LIB_CTX *ossl_ec_key_get_libctx(const EC_KEY *key)
     657{
     658    return key->libctx;
     659}
     660
     661const char *ossl_ec_key_get0_propq(const EC_KEY *key)
     662{
     663    return key->propq;
     664}
     665
     666void ossl_ec_key_set0_libctx(EC_KEY *key, OSSL_LIB_CTX *libctx)
     667{
     668    key->libctx = libctx;
     669    /* Do we need to propagate this to the group? */
     670}
     671
    399672const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key)
    400673{
     
    408681    EC_GROUP_free(key->group);
    409682    key->group = EC_GROUP_dup(group);
     683    if (key->group != NULL && EC_GROUP_get_curve_name(key->group) == NID_sm2)
     684        EC_KEY_set_flags(key, EC_FLAG_SM2_RANGE);
     685
     686    key->dirty_cnt++;
    410687    return (key->group == NULL) ? 0 : 1;
    411688}
     
    496773    BN_clear_free(key->priv_key);
    497774    key->priv_key = tmp_key;
     775    key->dirty_cnt++;
    498776
    499777    return 1;
     
    512790    EC_POINT_free(key->pub_key);
    513791    key->pub_key = EC_POINT_dup(pub_key, key->group);
     792    key->dirty_cnt++;
    514793    return (key->pub_key == NULL) ? 0 : 1;
    515794}
     
    543822}
    544823
     824#ifndef OPENSSL_NO_DEPRECATED_3_0
    545825int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx)
    546826{
     
    549829    return EC_GROUP_precompute_mult(key->group, ctx);
    550830}
     831#endif
    551832
    552833int EC_KEY_get_flags(const EC_KEY *key)
     
    558839{
    559840    key->flags |= flags;
     841    key->dirty_cnt++;
    560842}
    561843
     
    563845{
    564846    key->flags &= ~flags;
     847    key->dirty_cnt++;
    565848}
    566849
     
    591874    if (EC_POINT_oct2point(key->group, key->pub_key, buf, len, ctx) == 0)
    592875        return 0;
     876    key->dirty_cnt++;
    593877    /*
    594878     * Save the point conversion form.
     
    609893        return 0;
    610894    if (eckey->group->meth->priv2oct == NULL) {
    611         ECerr(EC_F_EC_KEY_PRIV2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     895        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    612896        return 0;
    613897    }
     
    616900}
    617901
    618 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
    619                               unsigned char *buf, size_t len)
     902size_t ossl_ec_key_simple_priv2oct(const EC_KEY *eckey,
     903                                   unsigned char *buf, size_t len)
    620904{
    621905    size_t buf_len;
     
    632916
    633917    if (BN_bn2binpad(eckey->priv_key, buf, buf_len) == -1) {
    634         ECerr(EC_F_EC_KEY_SIMPLE_PRIV2OCT, EC_R_BUFFER_TOO_SMALL);
     918        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
    635919        return 0;
    636920    }
     
    641925int EC_KEY_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
    642926{
     927    int ret;
     928
    643929    if (eckey->group == NULL || eckey->group->meth == NULL)
    644930        return 0;
    645931    if (eckey->group->meth->oct2priv == NULL) {
    646         ECerr(EC_F_EC_KEY_OCT2PRIV, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    647         return 0;
    648     }
    649     return eckey->group->meth->oct2priv(eckey, buf, len);
    650 }
    651 
    652 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len)
     932        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     933        return 0;
     934    }
     935    ret = eckey->group->meth->oct2priv(eckey, buf, len);
     936    if (ret == 1)
     937        eckey->dirty_cnt++;
     938    return ret;
     939}
     940
     941int ossl_ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf,
     942                                size_t len)
    653943{
    654944    if (eckey->priv_key == NULL)
    655945        eckey->priv_key = BN_secure_new();
    656946    if (eckey->priv_key == NULL) {
    657         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_MALLOC_FAILURE);
     947        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    658948        return 0;
    659949    }
    660950    eckey->priv_key = BN_bin2bn(buf, len, eckey->priv_key);
    661951    if (eckey->priv_key == NULL) {
    662         ECerr(EC_F_EC_KEY_SIMPLE_OCT2PRIV, ERR_R_BN_LIB);
    663         return 0;
    664     }
     952        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
     953        return 0;
     954    }
     955    eckey->dirty_cnt++;
    665956    return 1;
    666957}
     
    675966        return 0;
    676967    if ((buf = OPENSSL_malloc(len)) == NULL) {
    677         ECerr(EC_F_EC_KEY_PRIV2BUF, ERR_R_MALLOC_FAILURE);
     968        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    678969        return 0;
    679970    }
     
    694985    return 1;
    695986}
     987
     988/*
     989 * FIPS 140-2 IG 9.9 AS09.33
     990 * Perform a sign/verify operation.
     991 *
     992 * NOTE: When generating keys for key-agreement schemes - FIPS 140-2 IG 9.9
     993 * states that no additional pairwise tests are required (apart from the tests
     994 * specified in SP800-56A) when generating keys. Hence pairwise ECDH tests are
     995 * omitted here.
     996 */
     997static int ecdsa_keygen_pairwise_test(EC_KEY *eckey, OSSL_CALLBACK *cb,
     998                                      void *cbarg)
     999{
     1000    int ret = 0;
     1001    unsigned char dgst[16] = {0};
     1002    int dgst_len = (int)sizeof(dgst);
     1003    ECDSA_SIG *sig = NULL;
     1004    OSSL_SELF_TEST *st = NULL;
     1005
     1006    st = OSSL_SELF_TEST_new(cb, cbarg);
     1007    if (st == NULL)
     1008        return 0;
     1009
     1010    OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
     1011                           OSSL_SELF_TEST_DESC_PCT_ECDSA);
     1012
     1013    sig = ECDSA_do_sign(dgst, dgst_len, eckey);
     1014    if (sig == NULL)
     1015        goto err;
     1016
     1017    OSSL_SELF_TEST_oncorrupt_byte(st, dgst);
     1018
     1019    if (ECDSA_do_verify(dgst, dgst_len, sig, eckey) != 1)
     1020        goto err;
     1021
     1022    ret = 1;
     1023err:
     1024    OSSL_SELF_TEST_onend(st, ret);
     1025    OSSL_SELF_TEST_free(st);
     1026    ECDSA_SIG_free(sig);
     1027    return ret;
     1028}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_kmeth.c

    r91772 r94082  
    11/*
    2  * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    88 */
    99
     10/*
     11 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
     12 * for internal use.
     13 */
     14#include "internal/deprecated.h"
     15
    1016#include <string.h>
    1117#include <openssl/ec.h>
    12 #include <openssl/engine.h>
     18#ifndef FIPS_MODULE
     19# include <openssl/engine.h>
     20#endif
    1321#include <openssl/err.h>
    1422#include "ec_local.h"
     
    6068        finish(key);
    6169
    62 #ifndef OPENSSL_NO_ENGINE
     70#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
    6371    ENGINE_finish(key->engine);
    6472    key->engine = NULL;
     
    7179}
    7280
    73 EC_KEY *EC_KEY_new_method(ENGINE *engine)
     81EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
     82                                   ENGINE *engine)
    7483{
    7584    EC_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
    7685
    7786    if (ret == NULL) {
    78         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
     87        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    7988        return NULL;
     89    }
     90
     91    ret->libctx = libctx;
     92    if (propq != NULL) {
     93        ret->propq = OPENSSL_strdup(propq);
     94        if (ret->propq == NULL) {
     95            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     96            goto err;
     97        }
    8098    }
    8199
     
    83101    ret->lock = CRYPTO_THREAD_lock_new();
    84102    if (ret->lock == NULL) {
    85         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_MALLOC_FAILURE);
    86         OPENSSL_free(ret);
    87         return NULL;
     103        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     104        goto err;
    88105    }
    89106
    90107    ret->meth = EC_KEY_get_default_method();
    91 #ifndef OPENSSL_NO_ENGINE
     108#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
    92109    if (engine != NULL) {
    93110        if (!ENGINE_init(engine)) {
    94             ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
     111            ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
    95112            goto err;
    96113        }
     
    101118        ret->meth = ENGINE_get_EC(ret->engine);
    102119        if (ret->meth == NULL) {
    103             ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_ENGINE_LIB);
     120            ERR_raise(ERR_LIB_EC, ERR_R_ENGINE_LIB);
    104121            goto err;
    105122        }
     
    110127    ret->conv_form = POINT_CONVERSION_UNCOMPRESSED;
    111128
     129/* No ex_data inside the FIPS provider */
     130#ifndef FIPS_MODULE
    112131    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EC_KEY, ret, &ret->ex_data)) {
    113132        goto err;
    114133    }
     134#endif
    115135
    116136    if (ret->meth->init != NULL && ret->meth->init(ret) == 0) {
    117         ECerr(EC_F_EC_KEY_NEW_METHOD, ERR_R_INIT_FAIL);
     137        ERR_raise(ERR_LIB_EC, ERR_R_INIT_FAIL);
    118138        goto err;
    119139    }
     
    124144    return NULL;
    125145}
     146
     147#ifndef FIPS_MODULE
     148EC_KEY *EC_KEY_new_method(ENGINE *engine)
     149{
     150    return ossl_ec_key_new_method_int(NULL, NULL, engine);
     151}
     152#endif
    126153
    127154int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
     
    133160    size_t seclen;
    134161    if (eckey->meth->compute_key == NULL) {
    135         ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_OPERATION_NOT_SUPPORTED);
     162        ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
    136163        return 0;
    137164    }
    138165    if (outlen > INT_MAX) {
    139         ECerr(EC_F_ECDH_COMPUTE_KEY, EC_R_INVALID_OUTPUT_LENGTH);
     166        ERR_raise(ERR_LIB_EC, EC_R_INVALID_OUTPUT_LENGTH);
    140167        return 0;
    141168    }
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_lib.c

    r91772 r94082  
    11/*
    2  * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    99 */
    1010
     11/*
     12 * EC_GROUP low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
     16
    1117#include <string.h>
    12 
     18#include <openssl/params.h>
     19#include <openssl/core_names.h>
    1320#include <openssl/err.h>
    1421#include <openssl/opensslv.h>
    15 
     22#include "crypto/ec.h"
     23#include "internal/nelem.h"
    1624#include "ec_local.h"
     25#include "e_os.h" /* strcasecmp */
    1726
    1827/* functions for EC_GROUP objects */
    1928
    20 EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
     29EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
     30                               const EC_METHOD *meth)
    2131{
    2232    EC_GROUP *ret;
    2333
    2434    if (meth == NULL) {
    25         ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
     35        ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);
    2636        return NULL;
    2737    }
    2838    if (meth->group_init == 0) {
    29         ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     39        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    3040        return NULL;
    3141    }
     
    3343    ret = OPENSSL_zalloc(sizeof(*ret));
    3444    if (ret == NULL) {
    35         ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
     45        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    3646        return NULL;
    3747    }
    3848
     49    ret->libctx = libctx;
     50    if (propq != NULL) {
     51        ret->propq = OPENSSL_strdup(propq);
     52        if (ret->propq == NULL) {
     53            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     54            goto err;
     55        }
     56    }
    3957    ret->meth = meth;
    4058    if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) {
     
    4664            goto err;
    4765    }
    48     ret->asn1_flag = OPENSSL_EC_NAMED_CURVE;
     66    ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;
    4967    ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
    5068    if (!meth->group_init(ret))
     
    5573    BN_free(ret->order);
    5674    BN_free(ret->cofactor);
     75    OPENSSL_free(ret->propq);
    5776    OPENSSL_free(ret);
    5877    return NULL;
    5978}
     79
     80#ifndef OPENSSL_NO_DEPRECATED_3_0
     81# ifndef FIPS_MODULE
     82EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
     83{
     84    return ossl_ec_group_new_ex(NULL, NULL, meth);
     85}
     86# endif
     87#endif
    6088
    6189void EC_pre_comp_free(EC_GROUP *group)
     
    106134    BN_free(group->cofactor);
    107135    OPENSSL_free(group->seed);
     136    OPENSSL_free(group->propq);
    108137    OPENSSL_free(group);
    109138}
    110139
     140#ifndef OPENSSL_NO_DEPRECATED_3_0
    111141void EC_GROUP_clear_free(EC_GROUP *group)
    112142{
     
    127157    OPENSSL_clear_free(group, sizeof(*group));
    128158}
     159#endif
    129160
    130161int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
    131162{
    132163    if (dest->meth->group_copy == 0) {
    133         ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     164        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    134165        return 0;
    135166    }
    136167    if (dest->meth != src->meth) {
    137         ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
     168        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    138169        return 0;
    139170    }
     
    141172        return 1;
    142173
     174    dest->libctx = src->libctx;
    143175    dest->curve_name = src->curve_name;
    144176
     
    217249        OPENSSL_free(dest->seed);
    218250        if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL) {
    219             ECerr(EC_F_EC_GROUP_COPY, ERR_R_MALLOC_FAILURE);
     251            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    220252            return 0;
    221253        }
     
    240272        return NULL;
    241273
    242     if ((t = EC_GROUP_new(a->meth)) == NULL)
     274    if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)
    243275        return NULL;
    244276    if (!EC_GROUP_copy(t, a))
     
    255287}
    256288
     289#ifndef OPENSSL_NO_DEPRECATED_3_0
    257290const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)
    258291{
     
    264297    return meth->field_type;
    265298}
     299#endif
    266300
    267301static int ec_precompute_mont_data(EC_GROUP *);
     
    296330    }
    297331
    298     if ((ctx = BN_CTX_new()) == NULL)
     332    if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)
    299333        return 0;
    300334
     
    332366{
    333367    if (generator == NULL) {
    334         ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
     368        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    335369        return 0;
    336370    }
     
    339373    if (group->field == NULL || BN_is_zero(group->field)
    340374        || BN_is_negative(group->field)) {
    341         ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_FIELD);
     375        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
    342376        return 0;
    343377    }
     
    350384    if (order == NULL || BN_is_zero(order) || BN_is_negative(order)
    351385        || BN_num_bits(order) > BN_num_bits(group->field) + 1) {
    352         ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_INVALID_GROUP_ORDER);
     386        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
    353387        return 0;
    354388    }
     
    360394     */
    361395    if (cofactor != NULL && BN_is_negative(cofactor)) {
    362         ECerr(EC_F_EC_GROUP_SET_GENERATOR, EC_R_UNKNOWN_COFACTOR);
     396        ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
    363397        return 0;
    364398    }
     
    448482{
    449483    group->curve_name = nid;
     484    group->asn1_flag =
     485        (nid != NID_undef)
     486        ? OPENSSL_EC_NAMED_CURVE
     487        : OPENSSL_EC_EXPLICIT_CURVE;
    450488}
    451489
     
    453491{
    454492    return group->curve_name;
     493}
     494
     495const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)
     496{
     497    return group->field;
     498}
     499
     500int EC_GROUP_get_field_type(const EC_GROUP *group)
     501{
     502    return group->meth->field_type;
    455503}
    456504
     
    487535
    488536    if ((group->seed = OPENSSL_malloc(len)) == NULL) {
    489         ECerr(EC_F_EC_GROUP_SET_SEED, ERR_R_MALLOC_FAILURE);
     537        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    490538        return 0;
    491539    }
     
    510558{
    511559    if (group->meth->group_set_curve == 0) {
    512         ECerr(EC_F_EC_GROUP_SET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     560        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    513561        return 0;
    514562    }
     
    520568{
    521569    if (group->meth->group_get_curve == NULL) {
    522         ECerr(EC_F_EC_GROUP_GET_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     570        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    523571        return 0;
    524572    }
     
    526574}
    527575
    528 #if OPENSSL_API_COMPAT < 0x10200000L
     576#ifndef OPENSSL_NO_DEPRECATED_3_0
    529577int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
    530578                           const BIGNUM *b, BN_CTX *ctx)
     
    557605{
    558606    if (group->meth->group_get_degree == 0) {
    559         ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     607        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    560608        return 0;
    561609    }
     
    566614{
    567615    if (group->meth->group_check_discriminant == 0) {
    568         ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT,
    569               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     616        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    570617        return 0;
    571618    }
     
    577624    int r = 0;
    578625    BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
     626#ifndef FIPS_MODULE
    579627    BN_CTX *ctx_new = NULL;
     628#endif
    580629
    581630    /* compare the field types */
    582     if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
    583         EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
     631    if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))
    584632        return 1;
    585633    /* compare the curve name (if present in both) */
     
    590638        return 0;
    591639
     640#ifndef FIPS_MODULE
    592641    if (ctx == NULL)
    593642        ctx_new = ctx = BN_CTX_new();
     643#endif
    594644    if (ctx == NULL)
    595645        return -1;
     
    604654    if (b3 == NULL) {
    605655        BN_CTX_end(ctx);
     656#ifndef FIPS_MODULE
    606657        BN_CTX_free(ctx_new);
     658#endif
    607659        return -1;
    608660    }
     
    616668        r = 1;
    617669
    618     if (r || BN_cmp(a1, b1) || BN_cmp(a2, b2) || BN_cmp(a3, b3))
     670    /* return 1 if the curve parameters are different */
     671    if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)
    619672        r = 1;
    620673
    621674    /* XXX EC_POINT_cmp() assumes that the methods are equal */
     675    /* return 1 if the generators are different */
    622676    if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
    623                           EC_GROUP_get0_generator(b), ctx))
     677                          EC_GROUP_get0_generator(b), ctx) != 0)
    624678        r = 1;
    625679
    626680    if (!r) {
    627681        const BIGNUM *ao, *bo, *ac, *bc;
    628         /* compare the order and cofactor */
     682        /* compare the orders */
    629683        ao = EC_GROUP_get0_order(a);
    630684        bo = EC_GROUP_get0_order(b);
     685        if (ao == NULL || bo == NULL) {
     686            /* return an error if either order is NULL */
     687            r = -1;
     688            goto end;
     689        }
     690        if (BN_cmp(ao, bo) != 0) {
     691            /* return 1 if orders are different */
     692            r = 1;
     693            goto end;
     694        }
     695        /*
     696         * It gets here if the curve parameters and generator matched.
     697         * Now check the optional cofactors (if both are present).
     698         */
    631699        ac = EC_GROUP_get0_cofactor(a);
    632700        bc = EC_GROUP_get0_cofactor(b);
    633         if (ao == NULL || bo == NULL) {
    634             BN_CTX_end(ctx);
    635             BN_CTX_free(ctx_new);
    636             return -1;
    637         }
    638         if (BN_cmp(ao, bo) || BN_cmp(ac, bc))
     701        /* Returns 1 (mismatch) if both cofactors are specified and different */
     702        if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)
    639703            r = 1;
    640     }
    641 
     704        /* Returns 0 if the parameters matched */
     705    }
     706end:
    642707    BN_CTX_end(ctx);
     708#ifndef FIPS_MODULE
    643709    BN_CTX_free(ctx_new);
    644 
     710#endif
    645711    return r;
    646712}
     
    653719
    654720    if (group == NULL) {
    655         ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
     721        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    656722        return NULL;
    657723    }
    658724    if (group->meth->point_init == NULL) {
    659         ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     725        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    660726        return NULL;
    661727    }
     
    663729    ret = OPENSSL_zalloc(sizeof(*ret));
    664730    if (ret == NULL) {
    665         ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
     731        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    666732        return NULL;
    667733    }
     
    680746void EC_POINT_free(EC_POINT *point)
    681747{
    682     if (!point)
     748    if (point == NULL)
    683749        return;
    684750
     
    690756void EC_POINT_clear_free(EC_POINT *point)
    691757{
    692     if (!point)
     758    if (point == NULL)
    693759        return;
    694760
     
    703769{
    704770    if (dest->meth->point_copy == 0) {
    705         ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     771        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    706772        return 0;
    707773    }
    708774    if (dest->meth != src->meth
    709775            || (dest->curve_name != src->curve_name
    710                 && dest->curve_name != 0
    711                 && src->curve_name != 0)) {
    712         ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
     776                 && dest->curve_name != 0
     777                 && src->curve_name != 0)) {
     778        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    713779        return 0;
    714780    }
     
    737803}
    738804
     805#ifndef OPENSSL_NO_DEPRECATED_3_0
    739806const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)
    740807{
    741808    return point->meth;
    742809}
     810#endif
    743811
    744812int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
    745813{
    746814    if (group->meth->point_set_to_infinity == 0) {
    747         ECerr(EC_F_EC_POINT_SET_TO_INFINITY,
    748               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     815        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    749816        return 0;
    750817    }
    751818    if (group->meth != point->meth) {
    752         ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
     819        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    753820        return 0;
    754821    }
     
    756823}
    757824
     825#ifndef OPENSSL_NO_DEPRECATED_3_0
    758826int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
    759827                                             EC_POINT *point, const BIGNUM *x,
     
    761829                                             BN_CTX *ctx)
    762830{
    763     if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
    764         ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
    765               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     831    if (group->meth->field_type != NID_X9_62_prime_field) {
     832        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    766833        return 0;
    767834    }
    768835    if (!ec_point_is_compat(point, group)) {
    769         ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP,
    770               EC_R_INCOMPATIBLE_OBJECTS);
    771         return 0;
    772     }
    773     return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x,
    774                                                               y, z, ctx);
     836        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
     837        return 0;
     838    }
     839    return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,
     840                                                              x, y, z, ctx);
    775841}
    776842
     
    780846                                             BN_CTX *ctx)
    781847{
    782     if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
    783         ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
    784               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     848    if (group->meth->field_type != NID_X9_62_prime_field) {
     849        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    785850        return 0;
    786851    }
    787852    if (!ec_point_is_compat(point, group)) {
    788         ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP,
    789               EC_R_INCOMPATIBLE_OBJECTS);
    790         return 0;
    791     }
    792     return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x,
    793                                                               y, z, ctx);
    794 }
     853        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
     854        return 0;
     855    }
     856    return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,
     857                                                              x, y, z, ctx);
     858}
     859#endif
    795860
    796861int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
     
    799864{
    800865    if (group->meth->point_set_affine_coordinates == NULL) {
    801         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES,
    802               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     866        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    803867        return 0;
    804868    }
    805869    if (!ec_point_is_compat(point, group)) {
    806         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
     870        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    807871        return 0;
    808872    }
     
    811875
    812876    if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
    813         ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES, EC_R_POINT_IS_NOT_ON_CURVE);
     877        ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);
    814878        return 0;
    815879    }
     
    817881}
    818882
    819 #if OPENSSL_API_COMPAT < 0x10200000L
     883#ifndef OPENSSL_NO_DEPRECATED_3_0
    820884int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
    821885                                        EC_POINT *point, const BIGNUM *x,
     
    840904{
    841905    if (group->meth->point_get_affine_coordinates == NULL) {
    842         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES,
    843               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     906        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    844907        return 0;
    845908    }
    846909    if (!ec_point_is_compat(point, group)) {
    847         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_INCOMPATIBLE_OBJECTS);
     910        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    848911        return 0;
    849912    }
    850913    if (EC_POINT_is_at_infinity(group, point)) {
    851         ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
     914        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
    852915        return 0;
    853916    }
     
    855918}
    856919
    857 #if OPENSSL_API_COMPAT < 0x10200000L
     920#ifndef OPENSSL_NO_DEPRECATED_3_0
    858921int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
    859922                                        const EC_POINT *point, BIGNUM *x,
     
    877940{
    878941    if (group->meth->add == 0) {
    879         ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     942        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    880943        return 0;
    881944    }
    882945    if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)
    883946        || !ec_point_is_compat(b, group)) {
    884         ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
     947        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    885948        return 0;
    886949    }
     
    892955{
    893956    if (group->meth->dbl == 0) {
    894         ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     957        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    895958        return 0;
    896959    }
    897960    if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) {
    898         ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
     961        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    899962        return 0;
    900963    }
     
    905968{
    906969    if (group->meth->invert == 0) {
    907         ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     970        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    908971        return 0;
    909972    }
    910973    if (!ec_point_is_compat(a, group)) {
    911         ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
     974        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    912975        return 0;
    913976    }
     
    918981{
    919982    if (group->meth->is_at_infinity == 0) {
    920         ECerr(EC_F_EC_POINT_IS_AT_INFINITY,
    921               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     983        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    922984        return 0;
    923985    }
    924986    if (!ec_point_is_compat(point, group)) {
    925         ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
     987        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    926988        return 0;
    927989    }
     
    9401002{
    9411003    if (group->meth->is_on_curve == 0) {
    942         ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     1004        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    9431005        return 0;
    9441006    }
    9451007    if (!ec_point_is_compat(point, group)) {
    946         ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
     1008        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    9471009        return 0;
    9481010    }
     
    9541016{
    9551017    if (group->meth->point_cmp == 0) {
    956         ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     1018        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    9571019        return -1;
    9581020    }
    9591021    if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) {
    960         ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
     1022        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    9611023        return -1;
    9621024    }
     
    9641026}
    9651027
     1028#ifndef OPENSSL_NO_DEPRECATED_3_0
    9661029int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
    9671030{
    9681031    if (group->meth->make_affine == 0) {
    969         ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     1032        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    9701033        return 0;
    9711034    }
    9721035    if (!ec_point_is_compat(point, group)) {
    973         ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
     1036        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    9741037        return 0;
    9751038    }
     
    9831046
    9841047    if (group->meth->points_make_affine == 0) {
    985         ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     1048        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    9861049        return 0;
    9871050    }
    9881051    for (i = 0; i < num; i++) {
    9891052        if (!ec_point_is_compat(points[i], group)) {
    990             ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
     1053            ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    9911054            return 0;
    9921055        }
     
    9941057    return group->meth->points_make_affine(group, num, points, ctx);
    9951058}
     1059#endif
    9961060
    9971061/*
     
    10011065 */
    10021066
     1067#ifndef OPENSSL_NO_DEPRECATED_3_0
    10031068int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
    10041069                  size_t num, const EC_POINT *points[],
     
    10071072    int ret = 0;
    10081073    size_t i = 0;
     1074#ifndef FIPS_MODULE
    10091075    BN_CTX *new_ctx = NULL;
     1076#endif
    10101077
    10111078    if (!ec_point_is_compat(r, group)) {
    1012         ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
     1079        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    10131080        return 0;
    10141081    }
     
    10191086    for (i = 0; i < num; i++) {
    10201087        if (!ec_point_is_compat(points[i], group)) {
    1021             ECerr(EC_F_EC_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
     1088            ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    10221089            return 0;
    10231090        }
    10241091    }
    10251092
    1026     if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL) {
    1027         ECerr(EC_F_EC_POINTS_MUL, ERR_R_INTERNAL_ERROR);
     1093#ifndef FIPS_MODULE
     1094    if (ctx == NULL)
     1095        ctx = new_ctx = BN_CTX_secure_new();
     1096#endif
     1097    if (ctx == NULL) {
     1098        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    10281099        return 0;
    10291100    }
     
    10331104    else
    10341105        /* use default */
    1035         ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
    1036 
     1106        ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
     1107
     1108#ifndef FIPS_MODULE
    10371109    BN_CTX_free(new_ctx);
     1110#endif
    10381111    return ret;
    10391112}
     1113#endif
    10401114
    10411115int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
    10421116                 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
    10431117{
    1044     /* just a convenient interface to EC_POINTs_mul() */
    1045 
    1046     const EC_POINT *points[1];
    1047     const BIGNUM *scalars[1];
    1048 
    1049     points[0] = point;
    1050     scalars[0] = p_scalar;
    1051 
    1052     return EC_POINTs_mul(group, r, g_scalar,
    1053                          (point != NULL
    1054                           && p_scalar != NULL), points, scalars, ctx);
    1055 }
    1056 
     1118    int ret = 0;
     1119    size_t num;
     1120#ifndef FIPS_MODULE
     1121    BN_CTX *new_ctx = NULL;
     1122#endif
     1123
     1124    if (!ec_point_is_compat(r, group)
     1125        || (point != NULL && !ec_point_is_compat(point, group))) {
     1126        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
     1127        return 0;
     1128    }
     1129
     1130    if (g_scalar == NULL && p_scalar == NULL)
     1131        return EC_POINT_set_to_infinity(group, r);
     1132
     1133#ifndef FIPS_MODULE
     1134    if (ctx == NULL)
     1135        ctx = new_ctx = BN_CTX_secure_new();
     1136#endif
     1137    if (ctx == NULL) {
     1138        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
     1139        return 0;
     1140    }
     1141
     1142    num = (point != NULL && p_scalar != NULL) ? 1 : 0;
     1143    if (group->meth->mul != NULL)
     1144        ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
     1145    else
     1146        /* use default */
     1147        ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);
     1148
     1149#ifndef FIPS_MODULE
     1150    BN_CTX_free(new_ctx);
     1151#endif
     1152    return ret;
     1153}
     1154
     1155#ifndef OPENSSL_NO_DEPRECATED_3_0
    10571156int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
    10581157{
    10591158    if (group->meth->mul == 0)
    10601159        /* use default */
    1061         return ec_wNAF_precompute_mult(group, ctx);
     1160        return ossl_ec_wNAF_precompute_mult(group, ctx);
    10621161
    10631162    if (group->meth->precompute_mult != 0)
     
    10711170    if (group->meth->mul == 0)
    10721171        /* use default */
    1073         return ec_wNAF_have_precompute_mult(group);
     1172        return ossl_ec_wNAF_have_precompute_mult(group);
    10741173
    10751174    if (group->meth->have_precompute_mult != 0)
     
    10791178                                 * been performed */
    10801179}
     1180#endif
    10811181
    10821182/*
     
    10861186static int ec_precompute_mont_data(EC_GROUP *group)
    10871187{
    1088     BN_CTX *ctx = BN_CTX_new();
     1188    BN_CTX *ctx = BN_CTX_new_ex(group->libctx);
    10891189    int ret = 0;
    10901190
     
    11131213}
    11141214
     1215#ifndef FIPS_MODULE
    11151216int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)
    11161217{
     
    11221223    return CRYPTO_get_ex_data(&key->ex_data, idx);
    11231224}
    1124 
    1125 int ec_group_simple_order_bits(const EC_GROUP *group)
     1225#endif
     1226
     1227int ossl_ec_group_simple_order_bits(const EC_GROUP *group)
    11261228{
    11271229    if (group->order == NULL)
     
    11341236{
    11351237    BIGNUM *e = NULL;
     1238    int ret = 0;
     1239#ifndef FIPS_MODULE
    11361240    BN_CTX *new_ctx = NULL;
    1137     int ret = 0;
     1241#endif
    11381242
    11391243    if (group->mont_data == NULL)
    11401244        return 0;
    11411245
    1142     if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
     1246#ifndef FIPS_MODULE
     1247    if (ctx == NULL)
     1248        ctx = new_ctx = BN_CTX_secure_new();
     1249#endif
     1250    if (ctx == NULL)
    11431251        return 0;
    11441252
     
    11661274 err:
    11671275    BN_CTX_end(ctx);
     1276#ifndef FIPS_MODULE
    11681277    BN_CTX_free(new_ctx);
     1278#endif
    11691279    return ret;
    11701280}
     
    11831293 * other functionality.
    11841294 */
    1185 int ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
    1186                             const BIGNUM *x, BN_CTX *ctx)
     1295int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,
     1296                                 const BIGNUM *x, BN_CTX *ctx)
    11871297{
    11881298    if (group->meth->field_inverse_mod_ord != NULL)
     
    12021312 * support coordinate blinding.
    12031313 */
    1204 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx)
     1314int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
     1315                                    BN_CTX *ctx)
    12051316{
    12061317    if (group->meth->blind_coordinates == NULL)
     
    12091320    return group->meth->blind_coordinates(group, p, ctx);
    12101321}
     1322
     1323int EC_GROUP_get_basis_type(const EC_GROUP *group)
     1324{
     1325    int i;
     1326
     1327    if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)
     1328        /* everything else is currently not supported */
     1329        return 0;
     1330
     1331    /* Find the last non-zero element of group->poly[] */
     1332    for (i = 0;
     1333         i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;
     1334         i++)
     1335        continue;
     1336
     1337    if (i == 4)
     1338        return NID_X9_62_ppBasis;
     1339    else if (i == 2)
     1340        return NID_X9_62_tpBasis;
     1341    else
     1342        /* everything else is currently not supported */
     1343        return 0;
     1344}
     1345
     1346#ifndef OPENSSL_NO_EC2M
     1347int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)
     1348{
     1349    if (group == NULL)
     1350        return 0;
     1351
     1352    if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
     1353        || !((group->poly[0] != 0) && (group->poly[1] != 0)
     1354             && (group->poly[2] == 0))) {
     1355        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     1356        return 0;
     1357    }
     1358
     1359    if (k)
     1360        *k = group->poly[1];
     1361
     1362    return 1;
     1363}
     1364
     1365int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,
     1366                                   unsigned int *k2, unsigned int *k3)
     1367{
     1368    if (group == NULL)
     1369        return 0;
     1370
     1371    if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field
     1372        || !((group->poly[0] != 0) && (group->poly[1] != 0)
     1373             && (group->poly[2] != 0) && (group->poly[3] != 0)
     1374             && (group->poly[4] == 0))) {
     1375        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     1376        return 0;
     1377    }
     1378
     1379    if (k1)
     1380        *k1 = group->poly[3];
     1381    if (k2)
     1382        *k2 = group->poly[2];
     1383    if (k3)
     1384        *k3 = group->poly[1];
     1385
     1386    return 1;
     1387}
     1388#endif
     1389
     1390/*
     1391 * Check if the explicit parameters group matches any built-in curves.
     1392 *
     1393 * We create a copy of the group just built, so that we can remove optional
     1394 * fields for the lookup: we do this to avoid the possibility that one of
     1395 * the optional parameters is used to force the library into using a less
     1396 * performant and less secure EC_METHOD instead of the specialized one.
     1397 * In any case, `seed` is not really used in any computation, while a
     1398 * cofactor different from the one in the built-in table is just
     1399 * mathematically wrong anyway and should not be used.
     1400 */
     1401static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,
     1402                                            OSSL_LIB_CTX *libctx,
     1403                                            const char *propq,
     1404                                            BN_CTX *ctx)
     1405{
     1406    EC_GROUP *ret_group = NULL, *dup = NULL;
     1407    int curve_name_nid;
     1408
     1409    const EC_POINT *point = EC_GROUP_get0_generator(group);
     1410    const BIGNUM *order = EC_GROUP_get0_order(group);
     1411    int no_seed = (EC_GROUP_get0_seed(group) == NULL);
     1412
     1413    if ((dup = EC_GROUP_dup(group)) == NULL
     1414            || EC_GROUP_set_seed(dup, NULL, 0) != 1
     1415            || !EC_GROUP_set_generator(dup, point, order, NULL))
     1416        goto err;
     1417    if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) {
     1418        /*
     1419         * The input explicit parameters successfully matched one of the
     1420         * built-in curves: often for built-in curves we have specialized
     1421         * methods with better performance and hardening.
     1422         *
     1423         * In this case we replace the `EC_GROUP` created through explicit
     1424         * parameters with one created from a named group.
     1425         */
     1426
     1427#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
     1428        /*
     1429         * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for
     1430         * the same curve, we prefer the SECP nid when matching explicit
     1431         * parameters as that is associated with a specialized EC_METHOD.
     1432         */
     1433        if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)
     1434            curve_name_nid = NID_secp224r1;
     1435#endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
     1436
     1437        ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);
     1438        if (ret_group == NULL)
     1439            goto err;
     1440
     1441        /*
     1442         * Set the flag so that EC_GROUPs created from explicit parameters are
     1443         * serialized using explicit parameters by default.
     1444         */
     1445        EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);
     1446
     1447        /*
     1448         * If the input params do not contain the optional seed field we make
     1449         * sure it is not added to the returned group.
     1450         *
     1451         * The seed field is not really used inside libcrypto anyway, and
     1452         * adding it to parsed explicit parameter keys would alter their DER
     1453         * encoding output (because of the extra field) which could impact
     1454         * applications fingerprinting keys by their DER encoding.
     1455         */
     1456        if (no_seed) {
     1457            if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)
     1458                goto err;
     1459        }
     1460    } else {
     1461        ret_group = (EC_GROUP *)group;
     1462    }
     1463    EC_GROUP_free(dup);
     1464    return ret_group;
     1465err:
     1466    EC_GROUP_free(dup);
     1467    EC_GROUP_free(ret_group);
     1468    return NULL;
     1469}
     1470
     1471static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,
     1472                                     OSSL_LIB_CTX *libctx, const char *propq)
     1473{
     1474    int ok = 0, nid;
     1475    const char *curve_name = NULL;
     1476
     1477    switch (p->data_type) {
     1478    case OSSL_PARAM_UTF8_STRING:
     1479        /* The OSSL_PARAM functions have no support for this */
     1480        curve_name = p->data;
     1481        ok = (curve_name != NULL);
     1482        break;
     1483    case OSSL_PARAM_UTF8_PTR:
     1484        ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
     1485        break;
     1486    }
     1487
     1488    if (ok) {
     1489        nid = ossl_ec_curve_name2nid(curve_name);
     1490        if (nid == NID_undef) {
     1491            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
     1492            return NULL;
     1493        } else {
     1494            return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);
     1495        }
     1496    }
     1497    return NULL;
     1498}
     1499
     1500/* These parameters can be set directly into an EC_GROUP */
     1501int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])
     1502{
     1503    int encoding_flag = -1, format = -1;
     1504    const OSSL_PARAM *p;
     1505
     1506    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);
     1507    if (p != NULL) {
     1508        if (!ossl_ec_pt_format_param2id(p, &format)) {
     1509            ECerr(0, EC_R_INVALID_FORM);
     1510            return 0;
     1511        }
     1512        EC_GROUP_set_point_conversion_form(group, format);
     1513    }
     1514
     1515    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
     1516    if (p != NULL) {
     1517        if (!ossl_ec_encoding_param2id(p, &encoding_flag)) {
     1518            ECerr(0, EC_R_INVALID_FORM);
     1519            return 0;
     1520        }
     1521        EC_GROUP_set_asn1_flag(group, encoding_flag);
     1522    }
     1523    /* Optional seed */
     1524    p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
     1525    if (p != NULL) {
     1526        /* The seed is allowed to be NULL */
     1527        if (p->data_type != OSSL_PARAM_OCTET_STRING
     1528            || !EC_GROUP_set_seed(group, p->data, p->data_size)) {
     1529            ECerr(0, EC_R_INVALID_SEED);
     1530            return 0;
     1531        }
     1532    }
     1533    return 1;
     1534}
     1535
     1536EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],
     1537                                   OSSL_LIB_CTX *libctx, const char *propq)
     1538{
     1539    const OSSL_PARAM *ptmp, *pa, *pb;
     1540    int ok = 0;
     1541    EC_GROUP *group = NULL, *named_group = NULL;
     1542    BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;
     1543    EC_POINT *point = NULL;
     1544    int field_bits = 0;
     1545    int is_prime_field = 1;
     1546    BN_CTX *bnctx = NULL;
     1547    const unsigned char *buf = NULL;
     1548    int encoding_flag = -1;
     1549
     1550    /* This is the simple named group case */
     1551    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
     1552    if (ptmp != NULL) {
     1553        group = group_new_from_name(ptmp, libctx, propq);
     1554        if (group != NULL) {
     1555            if (!ossl_ec_group_set_params(group, params)) {
     1556                EC_GROUP_free(group);
     1557                group = NULL;
     1558            }
     1559        }
     1560        return group;
     1561    }
     1562    /* If it gets here then we are trying explicit parameters */
     1563    bnctx = BN_CTX_new_ex(libctx);
     1564    if (bnctx == NULL) {
     1565        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     1566        return 0;
     1567    }
     1568    BN_CTX_start(bnctx);
     1569
     1570    p = BN_CTX_get(bnctx);
     1571    a = BN_CTX_get(bnctx);
     1572    b = BN_CTX_get(bnctx);
     1573    order = BN_CTX_get(bnctx);
     1574    if (order == NULL) {
     1575        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     1576        goto err;
     1577    }
     1578
     1579    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);
     1580    if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) {
     1581        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
     1582        goto err;
     1583    }
     1584    if (strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) {
     1585        is_prime_field = 1;
     1586    } else if (strcasecmp(ptmp->data, SN_X9_62_characteristic_two_field) == 0) {
     1587        is_prime_field = 0;
     1588    } else {
     1589        /* Invalid field */
     1590        ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);
     1591        goto err;
     1592    }
     1593
     1594    pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);
     1595    if (!OSSL_PARAM_get_BN(pa, &a)) {
     1596        ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);
     1597        goto err;
     1598    }
     1599    pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);
     1600    if (!OSSL_PARAM_get_BN(pb, &b)) {
     1601        ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);
     1602        goto err;
     1603    }
     1604
     1605    /* extract the prime number or irreducible polynomial */
     1606    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);
     1607    if (!OSSL_PARAM_get_BN(ptmp, &p)) {
     1608        ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
     1609        goto err;
     1610    }
     1611
     1612    if (is_prime_field) {
     1613        if (BN_is_negative(p) || BN_is_zero(p)) {
     1614            ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);
     1615            goto err;
     1616        }
     1617        field_bits = BN_num_bits(p);
     1618        if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
     1619            ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
     1620            goto err;
     1621        }
     1622
     1623        /* create the EC_GROUP structure */
     1624        group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);
     1625    } else {
     1626#ifdef OPENSSL_NO_EC2M
     1627        ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
     1628        goto err;
     1629#else
     1630        /* create the EC_GROUP structure */
     1631        group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);
     1632        if (group != NULL) {
     1633            field_bits = EC_GROUP_get_degree(group);
     1634            if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) {
     1635                ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);
     1636                goto err;
     1637            }
     1638        }
     1639#endif /* OPENSSL_NO_EC2M */
     1640    }
     1641
     1642    if (group == NULL) {
     1643        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
     1644        goto err;
     1645    }
     1646
     1647    /* Optional seed */
     1648    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);
     1649    if (ptmp != NULL) {
     1650        if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
     1651            ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);
     1652            goto err;
     1653        }
     1654        if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))
     1655            goto err;
     1656    }
     1657
     1658    /* generator base point */
     1659    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);
     1660    if (ptmp == NULL
     1661        || ptmp->data_type != OSSL_PARAM_OCTET_STRING) {
     1662        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
     1663        goto err;
     1664    }
     1665    buf = (const unsigned char *)(ptmp->data);
     1666    if ((point = EC_POINT_new(group)) == NULL)
     1667        goto err;
     1668    EC_GROUP_set_point_conversion_form(group,
     1669                                       (point_conversion_form_t)buf[0] & ~0x01);
     1670    if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) {
     1671        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
     1672        goto err;
     1673    }
     1674
     1675    /* order */
     1676    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);
     1677    if (!OSSL_PARAM_get_BN(ptmp, &order)
     1678        || (BN_is_negative(order) || BN_is_zero(order))
     1679        || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */
     1680        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);
     1681        goto err;
     1682    }
     1683
     1684    /* Optional cofactor */
     1685    ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);
     1686    if (ptmp != NULL) {
     1687        cofactor = BN_CTX_get(bnctx);
     1688        if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) {
     1689            ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);
     1690            goto err;
     1691        }
     1692    }
     1693
     1694    /* set the generator, order and cofactor (if present) */
     1695    if (!EC_GROUP_set_generator(group, point, order, cofactor)) {
     1696        ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);
     1697        goto err;
     1698    }
     1699
     1700    named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);
     1701    if (named_group == NULL) {
     1702        ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);
     1703        goto err;
     1704    }
     1705    if (named_group == group) {
     1706        /*
     1707         * If we did not find a named group then the encoding should be explicit
     1708         * if it was specified
     1709         */
     1710        ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);
     1711        if (ptmp != NULL
     1712            && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) {
     1713            ECerr(0, EC_R_INVALID_ENCODING);
     1714            return 0;
     1715        }
     1716        if (encoding_flag == OPENSSL_EC_NAMED_CURVE) {
     1717            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
     1718            goto err;
     1719        }
     1720        EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
     1721    } else {
     1722        EC_GROUP_free(group);
     1723        group = named_group;
     1724    }
     1725    ok = 1;
     1726 err:
     1727    if (!ok) {
     1728        EC_GROUP_free(group);
     1729        group = NULL;
     1730    }
     1731    EC_POINT_free(point);
     1732    BN_CTX_end(bnctx);
     1733    BN_CTX_free(bnctx);
     1734
     1735    return group;
     1736}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_local.h

    r91772 r94082  
    11/*
    2  * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    3131/* Curve does not support signing operations */
    3232#define EC_FLAGS_NO_SIGN        0x4
     33
     34#ifdef OPENSSL_NO_DEPRECATED_3_0
     35typedef struct ec_method_st EC_METHOD;
     36#endif
    3337
    3438/*
     
    7781     */
    7882    int (*point_set_to_infinity) (const EC_GROUP *, EC_POINT *);
    79     int (*point_set_Jprojective_coordinates_GFp) (const EC_GROUP *,
    80                                                   EC_POINT *, const BIGNUM *x,
    81                                                   const BIGNUM *y,
    82                                                   const BIGNUM *z, BN_CTX *);
    83     int (*point_get_Jprojective_coordinates_GFp) (const EC_GROUP *,
    84                                                   const EC_POINT *, BIGNUM *x,
    85                                                   BIGNUM *y, BIGNUM *z,
    86                                                   BN_CTX *);
    8783    int (*point_set_affine_coordinates) (const EC_GROUP *, EC_POINT *,
    8884                                         const BIGNUM *x, const BIGNUM *y,
     
    180176    int (*ecdh_compute_key)(unsigned char **pout, size_t *poutlen,
    181177                            const EC_POINT *pub_key, const EC_KEY *ecdh);
     178    /* custom ECDSA */
     179    int (*ecdsa_sign_setup)(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinvp,
     180                            BIGNUM **rp);
     181    ECDSA_SIG *(*ecdsa_sign_sig)(const unsigned char *dgst, int dgstlen,
     182                                 const BIGNUM *kinv, const BIGNUM *r,
     183                                 EC_KEY *eckey);
     184    int (*ecdsa_verify_sig)(const unsigned char *dgst, int dgstlen,
     185                            const ECDSA_SIG *sig, EC_KEY *eckey);
    182186    /* Inverse modulo order */
    183187    int (*field_inverse_mod_ord)(const EC_GROUP *, BIGNUM *r,
     
    271275        EC_PRE_COMP *ec;
    272276    } pre_comp;
     277
     278    OSSL_LIB_CTX *libctx;
     279    char *propq;
    273280};
    274281
     
    289296    CRYPTO_REF_COUNT references;
    290297    int flags;
     298#ifndef FIPS_MODULE
    291299    CRYPTO_EX_DATA ex_data;
     300#endif
    292301    CRYPTO_RWLOCK *lock;
     302    OSSL_LIB_CTX *libctx;
     303    char *propq;
     304
     305    /* Provider data */
     306    size_t dirty_cnt; /* If any key material changes, increment this */
    293307};
    294308
     
    312326                                          const EC_GROUP *group)
    313327{
    314     if (group->meth != point->meth
    315         || (group->curve_name != 0
    316             && point->curve_name != 0
    317             && group->curve_name != point->curve_name))
    318         return 0;
    319 
    320     return 1;
     328    return group->meth == point->meth
     329           && (group->curve_name == 0
     330               || point->curve_name == 0
     331               || group->curve_name == point->curve_name);
    321332}
    322333
     
    339350 * group->method->mul is 0)
    340351 */
    341 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
    342                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
    343                 BN_CTX *);
    344 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
    345 int ec_wNAF_have_precompute_mult(const EC_GROUP *group);
     352int ossl_ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
     353                     size_t num, const EC_POINT *points[],
     354                     const BIGNUM *scalars[], BN_CTX *);
     355int ossl_ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *);
     356int ossl_ec_wNAF_have_precompute_mult(const EC_GROUP *group);
    346357
    347358/* method functions in ecp_smpl.c */
    348 int ec_GFp_simple_group_init(EC_GROUP *);
    349 void ec_GFp_simple_group_finish(EC_GROUP *);
    350 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
    351 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
    352 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
    353                                   const BIGNUM *a, const BIGNUM *b, BN_CTX *);
    354 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
    355                                   BIGNUM *b, BN_CTX *);
    356 int ec_GFp_simple_group_get_degree(const EC_GROUP *);
    357 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
    358 int ec_GFp_simple_point_init(EC_POINT *);
    359 void ec_GFp_simple_point_finish(EC_POINT *);
    360 void ec_GFp_simple_point_clear_finish(EC_POINT *);
    361 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
    362 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
    363 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
    364                                                   EC_POINT *, const BIGNUM *x,
    365                                                   const BIGNUM *y,
    366                                                   const BIGNUM *z, BN_CTX *);
    367 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
    368                                                   const EC_POINT *, BIGNUM *x,
    369                                                   BIGNUM *y, BIGNUM *z,
     359int ossl_ec_GFp_simple_group_init(EC_GROUP *);
     360void ossl_ec_GFp_simple_group_finish(EC_GROUP *);
     361void ossl_ec_GFp_simple_group_clear_finish(EC_GROUP *);
     362int ossl_ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
     363int ossl_ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
     364                                       const BIGNUM *a, const BIGNUM *b,
     365                                       BN_CTX *);
     366int ossl_ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
     367                                       BIGNUM *b, BN_CTX *);
     368int ossl_ec_GFp_simple_group_get_degree(const EC_GROUP *);
     369int ossl_ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
     370int ossl_ec_GFp_simple_point_init(EC_POINT *);
     371void ossl_ec_GFp_simple_point_finish(EC_POINT *);
     372void ossl_ec_GFp_simple_point_clear_finish(EC_POINT *);
     373int ossl_ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
     374int ossl_ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
     375int ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *,
     376                                                       EC_POINT *,
     377                                                       const BIGNUM *x,
     378                                                       const BIGNUM *y,
     379                                                       const BIGNUM *z,
     380                                                       BN_CTX *);
     381int ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
     382                                                       const EC_POINT *,
     383                                                       BIGNUM *x,
     384                                                       BIGNUM *y, BIGNUM *z,
     385                                                       BN_CTX *);
     386int ossl_ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
     387                                                    const BIGNUM *x,
     388                                                    const BIGNUM *y, BN_CTX *);
     389int ossl_ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
     390                                                    const EC_POINT *, BIGNUM *x,
     391                                                    BIGNUM *y, BN_CTX *);
     392int ossl_ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
     393                                                  const BIGNUM *x, int y_bit,
    370394                                                  BN_CTX *);
    371 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
    372                                                const BIGNUM *x,
    373                                                const BIGNUM *y, BN_CTX *);
    374 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
    375                                                const EC_POINT *, BIGNUM *x,
    376                                                BIGNUM *y, BN_CTX *);
    377 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
    378                                              const BIGNUM *x, int y_bit,
    379                                              BN_CTX *);
    380 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
    381                                point_conversion_form_t form,
    382                                unsigned char *buf, size_t len, BN_CTX *);
    383 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
    384                             const unsigned char *buf, size_t len, BN_CTX *);
    385 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
    386                       const EC_POINT *b, BN_CTX *);
    387 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
    388                       BN_CTX *);
    389 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
    390 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
    391 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
    392 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
    393                       BN_CTX *);
    394 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
    395 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
    396                                      EC_POINT *[], BN_CTX *);
    397 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    398                             const BIGNUM *b, BN_CTX *);
    399 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     395size_t ossl_ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *,
     396                                    point_conversion_form_t form,
     397                                    unsigned char *buf, size_t len, BN_CTX *);
     398int ossl_ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
     399                                 const unsigned char *buf, size_t len, BN_CTX *);
     400int ossl_ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
     401                           const EC_POINT *b, BN_CTX *);
     402int ossl_ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
     403                           BN_CTX *);
     404int ossl_ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
     405int ossl_ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
     406int ossl_ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
     407int ossl_ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a,
     408                           const EC_POINT *b, BN_CTX *);
     409int ossl_ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
     410int ossl_ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
     411                                          EC_POINT *[], BN_CTX *);
     412int ossl_ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     413                                 const BIGNUM *b, BN_CTX *);
     414int ossl_ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     415                                 BN_CTX *);
     416int ossl_ec_GFp_simple_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     417                                 BN_CTX *);
     418int ossl_ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
     419                                         BN_CTX *ctx);
     420int ossl_ec_GFp_simple_ladder_pre(const EC_GROUP *group,
     421                                  EC_POINT *r, EC_POINT *s,
     422                                  EC_POINT *p, BN_CTX *ctx);
     423int ossl_ec_GFp_simple_ladder_step(const EC_GROUP *group,
     424                                   EC_POINT *r, EC_POINT *s,
     425                                   EC_POINT *p, BN_CTX *ctx);
     426int ossl_ec_GFp_simple_ladder_post(const EC_GROUP *group,
     427                                   EC_POINT *r, EC_POINT *s,
     428                                   EC_POINT *p, BN_CTX *ctx);
     429
     430/* method functions in ecp_mont.c */
     431int ossl_ec_GFp_mont_group_init(EC_GROUP *);
     432int ossl_ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p,
     433                                     const BIGNUM *a,
     434                                     const BIGNUM *b, BN_CTX *);
     435void ossl_ec_GFp_mont_group_finish(EC_GROUP *);
     436void ossl_ec_GFp_mont_group_clear_finish(EC_GROUP *);
     437int ossl_ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
     438int ossl_ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     439                               const BIGNUM *b, BN_CTX *);
     440int ossl_ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     441                               BN_CTX *);
     442int ossl_ec_GFp_mont_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     443                               BN_CTX *);
     444int ossl_ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     445                                  BN_CTX *);
     446int ossl_ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     447                                  BN_CTX *);
     448int ossl_ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
     449
     450/* method functions in ecp_nist.c */
     451int ossl_ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
     452int ossl_ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p,
     453                                     const BIGNUM *a, const BIGNUM *b, BN_CTX *);
     454int ossl_ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     455                              const BIGNUM *b, BN_CTX *);
     456int ossl_ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     457                               BN_CTX *);
     458
     459/* method functions in ec2_smpl.c */
     460int ossl_ec_GF2m_simple_group_init(EC_GROUP *);
     461void ossl_ec_GF2m_simple_group_finish(EC_GROUP *);
     462void ossl_ec_GF2m_simple_group_clear_finish(EC_GROUP *);
     463int ossl_ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
     464int ossl_ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
     465                                        const BIGNUM *a, const BIGNUM *b,
     466                                        BN_CTX *);
     467int ossl_ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
     468                                        BIGNUM *b, BN_CTX *);
     469int ossl_ec_GF2m_simple_group_get_degree(const EC_GROUP *);
     470int ossl_ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
     471int ossl_ec_GF2m_simple_point_init(EC_POINT *);
     472void ossl_ec_GF2m_simple_point_finish(EC_POINT *);
     473void ossl_ec_GF2m_simple_point_clear_finish(EC_POINT *);
     474int ossl_ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
     475int ossl_ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
     476int ossl_ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *,
     477                                                     EC_POINT *,
     478                                                     const BIGNUM *x,
     479                                                     const BIGNUM *y, BN_CTX *);
     480int ossl_ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
     481                                                     const EC_POINT *, BIGNUM *x,
     482                                                     BIGNUM *y, BN_CTX *);
     483int ossl_ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
     484                                                   const BIGNUM *x, int y_bit,
     485                                                   BN_CTX *);
     486size_t ossl_ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
     487                                     point_conversion_form_t form,
     488                                     unsigned char *buf, size_t len, BN_CTX *);
     489int ossl_ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
     490                                  const unsigned char *buf, size_t len, BN_CTX *);
     491int ossl_ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
     492                            const EC_POINT *b, BN_CTX *);
     493int ossl_ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
    400494                            BN_CTX *);
    401 int ec_GFp_simple_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    402                             BN_CTX *);
    403 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
    404                                     BN_CTX *ctx);
    405 int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
    406                              EC_POINT *r, EC_POINT *s,
    407                              EC_POINT *p, BN_CTX *ctx);
    408 int ec_GFp_simple_ladder_step(const EC_GROUP *group,
    409                               EC_POINT *r, EC_POINT *s,
    410                               EC_POINT *p, BN_CTX *ctx);
    411 int ec_GFp_simple_ladder_post(const EC_GROUP *group,
    412                               EC_POINT *r, EC_POINT *s,
    413                               EC_POINT *p, BN_CTX *ctx);
    414 
    415 /* method functions in ecp_mont.c */
    416 int ec_GFp_mont_group_init(EC_GROUP *);
    417 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
    418                                 const BIGNUM *b, BN_CTX *);
    419 void ec_GFp_mont_group_finish(EC_GROUP *);
    420 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
    421 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
    422 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    423                           const BIGNUM *b, BN_CTX *);
    424 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    425                           BN_CTX *);
    426 int ec_GFp_mont_field_inv(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    427                           BN_CTX *);
    428 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     495int ossl_ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
     496int ossl_ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
     497int ossl_ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
     498int ossl_ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a,
     499                            const EC_POINT *b, BN_CTX *);
     500int ossl_ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
     501int ossl_ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
     502                                           EC_POINT *[], BN_CTX *);
     503int ossl_ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     504                                  const BIGNUM *b, BN_CTX *);
     505int ossl_ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     506                                  BN_CTX *);
     507int ossl_ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     508                                 const BIGNUM *b, BN_CTX *);
     509
     510#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
     511# ifdef B_ENDIAN
     512#  error "Can not enable ec_nistp_64_gcc_128 on big-endian systems"
     513# endif
     514
     515/* method functions in ecp_nistp224.c */
     516int ossl_ec_GFp_nistp224_group_init(EC_GROUP *group);
     517int ossl_ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
     518                                         const BIGNUM *a, const BIGNUM *n,
     519                                         BN_CTX *);
     520int ossl_ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
     521                                                      const EC_POINT *point,
     522                                                      BIGNUM *x, BIGNUM *y,
     523                                                      BN_CTX *ctx);
     524int ossl_ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
     525                             const BIGNUM *scalar, size_t num,
     526                             const EC_POINT *points[], const BIGNUM *scalars[],
    429527                             BN_CTX *);
    430 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     528int ossl_ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
     529                                    const BIGNUM *scalar, size_t num,
     530                                    const EC_POINT *points[],
     531                                    const BIGNUM *scalars[], BN_CTX *ctx);
     532int ossl_ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
     533int ossl_ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
     534
     535/* method functions in ecp_nistp256.c */
     536int ossl_ec_GFp_nistp256_group_init(EC_GROUP *group);
     537int ossl_ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
     538                                         const BIGNUM *a, const BIGNUM *n,
     539                                         BN_CTX *);
     540int ossl_ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
     541                                                      const EC_POINT *point,
     542                                                      BIGNUM *x, BIGNUM *y,
     543                                                      BN_CTX *ctx);
     544int ossl_ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
     545                             const BIGNUM *scalar, size_t num,
     546                             const EC_POINT *points[], const BIGNUM *scalars[],
    431547                             BN_CTX *);
    432 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
    433 
    434 /* method functions in ecp_nist.c */
    435 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src);
    436 int ec_GFp_nist_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
    437                                 const BIGNUM *b, BN_CTX *);
    438 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    439                           const BIGNUM *b, BN_CTX *);
    440 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    441                           BN_CTX *);
    442 
    443 /* method functions in ec2_smpl.c */
    444 int ec_GF2m_simple_group_init(EC_GROUP *);
    445 void ec_GF2m_simple_group_finish(EC_GROUP *);
    446 void ec_GF2m_simple_group_clear_finish(EC_GROUP *);
    447 int ec_GF2m_simple_group_copy(EC_GROUP *, const EC_GROUP *);
    448 int ec_GF2m_simple_group_set_curve(EC_GROUP *, const BIGNUM *p,
    449                                    const BIGNUM *a, const BIGNUM *b,
    450                                    BN_CTX *);
    451 int ec_GF2m_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
    452                                    BIGNUM *b, BN_CTX *);
    453 int ec_GF2m_simple_group_get_degree(const EC_GROUP *);
    454 int ec_GF2m_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
    455 int ec_GF2m_simple_point_init(EC_POINT *);
    456 void ec_GF2m_simple_point_finish(EC_POINT *);
    457 void ec_GF2m_simple_point_clear_finish(EC_POINT *);
    458 int ec_GF2m_simple_point_copy(EC_POINT *, const EC_POINT *);
    459 int ec_GF2m_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
    460 int ec_GF2m_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
    461                                                 const BIGNUM *x,
    462                                                 const BIGNUM *y, BN_CTX *);
    463 int ec_GF2m_simple_point_get_affine_coordinates(const EC_GROUP *,
    464                                                 const EC_POINT *, BIGNUM *x,
    465                                                 BIGNUM *y, BN_CTX *);
    466 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
    467                                               const BIGNUM *x, int y_bit,
    468                                               BN_CTX *);
    469 size_t ec_GF2m_simple_point2oct(const EC_GROUP *, const EC_POINT *,
    470                                 point_conversion_form_t form,
    471                                 unsigned char *buf, size_t len, BN_CTX *);
    472 int ec_GF2m_simple_oct2point(const EC_GROUP *, EC_POINT *,
    473                              const unsigned char *buf, size_t len, BN_CTX *);
    474 int ec_GF2m_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
    475                        const EC_POINT *b, BN_CTX *);
    476 int ec_GF2m_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
    477                        BN_CTX *);
    478 int ec_GF2m_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
    479 int ec_GF2m_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
    480 int ec_GF2m_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
    481 int ec_GF2m_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
    482                        BN_CTX *);
    483 int ec_GF2m_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
    484 int ec_GF2m_simple_points_make_affine(const EC_GROUP *, size_t num,
    485                                       EC_POINT *[], BN_CTX *);
    486 int ec_GF2m_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    487                              const BIGNUM *b, BN_CTX *);
    488 int ec_GF2m_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
     548int ossl_ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
     549                                    const BIGNUM *scalar, size_t num,
     550                                    const EC_POINT *points[],
     551                                    const BIGNUM *scalars[], BN_CTX *ctx);
     552int ossl_ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
     553int ossl_ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
     554
     555/* method functions in ecp_nistp521.c */
     556int ossl_ec_GFp_nistp521_group_init(EC_GROUP *group);
     557int ossl_ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
     558                                         const BIGNUM *a, const BIGNUM *n,
     559                                         BN_CTX *);
     560int ossl_ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
     561                                                      const EC_POINT *point,
     562                                                      BIGNUM *x, BIGNUM *y,
     563                                                      BN_CTX *ctx);
     564int ossl_ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
     565                             const BIGNUM *scalar, size_t num,
     566                             const EC_POINT *points[], const BIGNUM *scalars[],
    489567                             BN_CTX *);
    490 int ec_GF2m_simple_field_div(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
    491                              const BIGNUM *b, BN_CTX *);
    492 
    493 #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
    494 /* method functions in ecp_nistp224.c */
    495 int ec_GFp_nistp224_group_init(EC_GROUP *group);
    496 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
    497                                     const BIGNUM *a, const BIGNUM *n,
    498                                     BN_CTX *);
    499 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
    500                                                  const EC_POINT *point,
    501                                                  BIGNUM *x, BIGNUM *y,
    502                                                  BN_CTX *ctx);
    503 int ec_GFp_nistp224_mul(const EC_GROUP *group, EC_POINT *r,
    504                         const BIGNUM *scalar, size_t num,
    505                         const EC_POINT *points[], const BIGNUM *scalars[],
    506                         BN_CTX *);
    507 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
    508                                const BIGNUM *scalar, size_t num,
    509                                const EC_POINT *points[],
    510                                const BIGNUM *scalars[], BN_CTX *ctx);
    511 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
    512 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group);
    513 
    514 /* method functions in ecp_nistp256.c */
    515 int ec_GFp_nistp256_group_init(EC_GROUP *group);
    516 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
    517                                     const BIGNUM *a, const BIGNUM *n,
    518                                     BN_CTX *);
    519 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
    520                                                  const EC_POINT *point,
    521                                                  BIGNUM *x, BIGNUM *y,
    522                                                  BN_CTX *ctx);
    523 int ec_GFp_nistp256_mul(const EC_GROUP *group, EC_POINT *r,
    524                         const BIGNUM *scalar, size_t num,
    525                         const EC_POINT *points[], const BIGNUM *scalars[],
    526                         BN_CTX *);
    527 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
    528                                const BIGNUM *scalar, size_t num,
    529                                const EC_POINT *points[],
    530                                const BIGNUM *scalars[], BN_CTX *ctx);
    531 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
    532 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group);
    533 
    534 /* method functions in ecp_nistp521.c */
    535 int ec_GFp_nistp521_group_init(EC_GROUP *group);
    536 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
    537                                     const BIGNUM *a, const BIGNUM *n,
    538                                     BN_CTX *);
    539 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
    540                                                  const EC_POINT *point,
    541                                                  BIGNUM *x, BIGNUM *y,
    542                                                  BN_CTX *ctx);
    543 int ec_GFp_nistp521_mul(const EC_GROUP *group, EC_POINT *r,
    544                         const BIGNUM *scalar, size_t num,
    545                         const EC_POINT *points[], const BIGNUM *scalars[],
    546                         BN_CTX *);
    547 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
    548                                const BIGNUM *scalar, size_t num,
    549                                const EC_POINT *points[],
    550                                const BIGNUM *scalars[], BN_CTX *ctx);
    551 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
    552 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
     568int ossl_ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
     569                                    const BIGNUM *scalar, size_t num,
     570                                    const EC_POINT *points[],
     571                                    const BIGNUM *scalars[], BN_CTX *ctx);
     572int ossl_ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
     573int ossl_ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group);
    553574
    554575/* utility functions in ecp_nistputil.c */
    555 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
    556                                               size_t felem_size,
    557                                               void *tmp_felems,
    558                                               void (*felem_one) (void *out),
    559                                               int (*felem_is_zero) (const void
    560                                                                     *in),
    561                                               void (*felem_assign) (void *out,
    562                                                                     const void
    563                                                                     *in),
    564                                               void (*felem_square) (void *out,
    565                                                                     const void
    566                                                                     *in),
    567                                               void (*felem_mul) (void *out,
    568                                                                  const void
    569                                                                  *in1,
    570                                                                  const void
    571                                                                  *in2),
    572                                               void (*felem_inv) (void *out,
    573                                                                  const void
    574                                                                  *in),
    575                                               void (*felem_contract) (void
    576                                                                       *out,
    577                                                                       const
    578                                                                       void
    579                                                                       *in));
    580 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
    581                                      unsigned char *digit, unsigned char in);
     576void ossl_ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
     577                                                   size_t felem_size,
     578                                                   void *tmp_felems,
     579                                                   void (*felem_one) (void *out),
     580                                                   int (*felem_is_zero)
     581                                                       (const void *in),
     582                                                   void (*felem_assign)
     583                                                       (void *out, const void *in),
     584                                                   void (*felem_square)
     585                                                       (void *out, const void *in),
     586                                                   void (*felem_mul)
     587                                                       (void *out,
     588                                                        const void *in1,
     589                                                        const void *in2),
     590                                                   void (*felem_inv)
     591                                                       (void *out, const void *in),
     592                                                   void (*felem_contract)
     593                                                       (void *out, const void *in));
     594void ossl_ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
     595                                          unsigned char *digit,
     596                                          unsigned char in);
    582597#endif
    583 int ec_group_simple_order_bits(const EC_GROUP *group);
     598int ossl_ec_group_simple_order_bits(const EC_GROUP *group);
     599
     600/**
     601 *  Creates a new EC_GROUP object
     602 *  \param   libctx The associated library context or NULL for the default
     603 *                  library context
     604 *  \param   propq  Any property query string
     605 *  \param   meth   EC_METHOD to use
     606 *  \return  newly created EC_GROUP object or NULL in case of an error.
     607 */
     608EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
     609                               const EC_METHOD *meth);
    584610
    585611#ifdef ECP_NISTZ256_ASM
     
    590616const EC_METHOD *EC_GFp_nistz256_method(void);
    591617#endif
    592 
    593 size_t ec_key_simple_priv2oct(const EC_KEY *eckey,
    594                               unsigned char *buf, size_t len);
    595 int ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf, size_t len);
    596 int ec_key_simple_generate_key(EC_KEY *eckey);
    597 int ec_key_simple_generate_public_key(EC_KEY *eckey);
    598 int ec_key_simple_check_key(const EC_KEY *eckey);
    599 
    600 int ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx);
     618#ifdef S390X_EC_ASM
     619const EC_METHOD *EC_GFp_s390x_nistp256_method(void);
     620const EC_METHOD *EC_GFp_s390x_nistp384_method(void);
     621const EC_METHOD *EC_GFp_s390x_nistp521_method(void);
     622#endif
     623
     624size_t ossl_ec_key_simple_priv2oct(const EC_KEY *eckey,
     625                                   unsigned char *buf, size_t len);
     626int ossl_ec_key_simple_oct2priv(EC_KEY *eckey, const unsigned char *buf,
     627                                size_t len);
     628int ossl_ec_key_simple_generate_key(EC_KEY *eckey);
     629int ossl_ec_key_simple_generate_public_key(EC_KEY *eckey);
     630int ossl_ec_key_simple_check_key(const EC_KEY *eckey);
     631
     632int ossl_ec_curve_nid_from_params(const EC_GROUP *group, BN_CTX *ctx);
    601633
    602634/* EC_METHOD definitions */
     
    631663#define EC_KEY_METHOD_DYNAMIC   1
    632664
     665EC_KEY *ossl_ec_key_new_method_int(OSSL_LIB_CTX *libctx, const char *propq,
     666                                   ENGINE *engine);
     667
    633668int ossl_ec_key_gen(EC_KEY *eckey);
    634669int ossl_ecdh_compute_key(unsigned char **pout, size_t *poutlen,
    635670                          const EC_POINT *pub_key, const EC_KEY *ecdh);
    636 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
    637                             const EC_POINT *pub_key, const EC_KEY *ecdh);
     671int ossl_ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
     672                                 const EC_POINT *pub_key, const EC_KEY *ecdh);
    638673
    639674struct ECDSA_SIG_st {
     
    654689int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
    655690                          const ECDSA_SIG *sig, EC_KEY *eckey);
    656 
    657 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
    658                  const uint8_t public_key[32], const uint8_t private_key[32]);
    659 int ED25519_verify(const uint8_t *message, size_t message_len,
    660                    const uint8_t signature[64], const uint8_t public_key[32]);
    661 void ED25519_public_from_private(uint8_t out_public_key[32],
    662                                  const uint8_t private_key[32]);
    663 
    664 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
    665            const uint8_t peer_public_value[32]);
    666 void X25519_public_from_private(uint8_t out_public_value[32],
    667                                 const uint8_t private_key[32]);
     691int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
     692                                 BIGNUM **rp);
     693ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
     694                                      const BIGNUM *in_kinv, const BIGNUM *in_r,
     695                                      EC_KEY *eckey);
     696int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
     697                                 const ECDSA_SIG *sig, EC_KEY *eckey);
     698
    668699
    669700/*-
     
    696727 * Returns 1 on success, 0 otherwise.
    697728 */
    698 int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
    699                          const BIGNUM *scalar, const EC_POINT *point,
    700                          BN_CTX *ctx);
    701 
    702 int ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx);
     729int ossl_ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
     730                              const BIGNUM *scalar, const EC_POINT *point,
     731                              BN_CTX *ctx);
     732
     733int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
     734                                    BN_CTX *ctx);
    703735
    704736static ossl_inline int ec_point_ladder_pre(const EC_GROUP *group,
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_mult.c

    r91772 r94082  
    11/*
    2  * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
    88 * https://www.openssl.org/source/license.html
    99 */
     10
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
    1016
    1117#include <string.h>
     
    5258    ret = OPENSSL_zalloc(sizeof(*ret));
    5359    if (ret == NULL) {
    54         ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     60        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    5561        return ret;
    5662    }
     
    6369    ret->lock = CRYPTO_THREAD_lock_new();
    6470    if (ret->lock == NULL) {
    65         ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     71        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    6672        OPENSSL_free(ret);
    6773        return NULL;
     
    137143 * Returns 1 on success, 0 otherwise.
    138144 */
    139 int ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
    140                          const BIGNUM *scalar, const EC_POINT *point,
    141                          BN_CTX *ctx)
     145int ossl_ec_scalar_mul_ladder(const EC_GROUP *group, EC_POINT *r,
     146                              const BIGNUM *scalar, const EC_POINT *point,
     147                              BN_CTX *ctx)
    142148{
    143149    int i, cardinality_bits, group_top, kbit, pbit, Z_is_one;
     
    154160
    155161    if (BN_is_zero(group->order)) {
    156         ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_UNKNOWN_ORDER);
     162        ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_ORDER);
    157163        return 0;
    158164    }
    159165    if (BN_is_zero(group->cofactor)) {
    160         ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_UNKNOWN_COFACTOR);
     166        ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);
    161167        return 0;
    162168    }
     
    166172    if (((p = EC_POINT_new(group)) == NULL)
    167173        || ((s = EC_POINT_new(group)) == NULL)) {
    168         ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_MALLOC_FAILURE);
     174        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    169175        goto err;
    170176    }
     
    172178    if (point == NULL) {
    173179        if (!EC_POINT_copy(p, group->generator)) {
    174             ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);
     180            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    175181            goto err;
    176182        }
    177183    } else {
    178184        if (!EC_POINT_copy(p, point)) {
    179             ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);
     185            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    180186            goto err;
    181187        }
     
    190196    k = BN_CTX_get(ctx);
    191197    if (k == NULL) {
    192         ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_MALLOC_FAILURE);
     198        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    193199        goto err;
    194200    }
    195201
    196202    if (!BN_mul(cardinality, group->order, group->cofactor, ctx)) {
    197         ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);
     203        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    198204        goto err;
    199205    }
     
    209215    if ((bn_wexpand(k, group_top + 2) == NULL)
    210216        || (bn_wexpand(lambda, group_top + 2) == NULL)) {
    211         ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);
     217        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    212218        goto err;
    213219    }
    214220
    215221    if (!BN_copy(k, scalar)) {
    216         ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);
     222        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    217223        goto err;
    218224    }
     
    226232         */
    227233        if (!BN_nnmod(k, k, cardinality, ctx)) {
    228             ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);
     234            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    229235            goto err;
    230236        }
     
    232238
    233239    if (!BN_add(lambda, k, cardinality)) {
    234         ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);
     240        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    235241        goto err;
    236242    }
    237243    BN_set_flags(lambda, BN_FLG_CONSTTIME);
    238244    if (!BN_add(k, lambda, cardinality)) {
    239         ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);
     245        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    240246        goto err;
    241247    }
     
    257263        || (bn_wexpand(p->Y, group_top) == NULL)
    258264        || (bn_wexpand(p->Z, group_top) == NULL)) {
    259         ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_BN_LIB);
     265        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    260266        goto err;
    261267    }
    262268
    263269    /* ensure input point is in affine coords for ladder step efficiency */
    264     if (!p->Z_is_one && !EC_POINT_make_affine(group, p, ctx)) {
    265             ECerr(EC_F_EC_SCALAR_MUL_LADDER, ERR_R_EC_LIB);
     270    if (!p->Z_is_one && (group->meth->make_affine == NULL
     271                         || !group->meth->make_affine(group, p, ctx))) {
     272            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    266273            goto err;
    267274    }
     
    269276    /* Initialize the Montgomery ladder */
    270277    if (!ec_point_ladder_pre(group, r, s, p, ctx)) {
    271         ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_PRE_FAILURE);
     278        ERR_raise(ERR_LIB_EC, EC_R_LADDER_PRE_FAILURE);
    272279        goto err;
    273280    }
     
    349356        /* Perform a single step of the Montgomery ladder */
    350357        if (!ec_point_ladder_step(group, r, s, p, ctx)) {
    351             ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_STEP_FAILURE);
     358            ERR_raise(ERR_LIB_EC, EC_R_LADDER_STEP_FAILURE);
    352359            goto err;
    353360        }
     
    364371    /* Finalize ladder (and recover full point coordinates) */
    365372    if (!ec_point_ladder_post(group, r, s, p, ctx)) {
    366         ECerr(EC_F_EC_SCALAR_MUL_LADDER, EC_R_LADDER_POST_FAILURE);
     373        ERR_raise(ERR_LIB_EC, EC_R_LADDER_POST_FAILURE);
    367374        goto err;
    368375    }
     
    381388
    382389/*
    383  * TODO: table should be optimised for the wNAF-based implementation,
     390 * Table could be optimised for the wNAF-based implementation,
    384391 * sometimes smaller windows will give better performance (thus the
    385392 * boundaries should be increased)
     
    401408 * in the addition if scalar != NULL
    402409 */
    403 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
    404                 size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
    405                 BN_CTX *ctx)
     410int ossl_ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
     411                     size_t num, const EC_POINT *points[],
     412                     const BIGNUM *scalars[], BN_CTX *ctx)
    406413{
    407414    const EC_POINT *generator = NULL;
     
    444451             * always call the ladder version.
    445452             */
    446             return ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);
     453            return ossl_ec_scalar_mul_ladder(group, r, scalar, NULL, ctx);
    447454        }
    448455        if ((scalar == NULL) && (num == 1) && (scalars[0] != group->order)) {
     
    454461             * actually set and we always call the ladder version.
    455462             */
    456             return ec_scalar_mul_ladder(group, r, scalars[0], points[0], ctx);
     463            return ossl_ec_scalar_mul_ladder(group, r, scalars[0], points[0],
     464                                             ctx);
    457465        }
    458466    }
     
    461469        generator = EC_GROUP_get0_generator(group);
    462470        if (generator == NULL) {
    463             ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
     471            ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
    464472            goto err;
    465473        }
     
    489497            /* check that pre_comp looks sane */
    490498            if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
    491                 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
     499                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    492500                goto err;
    493501            }
     
    514522
    515523    if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
    516         ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
     524        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    517525        goto err;
    518526    }
     
    544552        if (pre_comp == NULL) {
    545553            if (num_scalar != 1) {
    546                 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
     554                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    547555                goto err;
    548556            }
     
    553561
    554562            if (num_scalar != 0) {
    555                 ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
     563                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    556564                goto err;
    557565            }
     
    596604                    numblocks = (tmp_len + blocksize - 1) / blocksize;
    597605                    if (numblocks > pre_comp->numblocks) {
    598                         ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
     606                        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    599607                        OPENSSL_free(tmp_wNAF);
    600608                        goto err;
     
    611619                        wNAF_len[i] = blocksize;
    612620                        if (tmp_len < blocksize) {
    613                             ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
     621                            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    614622                            OPENSSL_free(tmp_wNAF);
    615623                            goto err;
     
    626634                    wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
    627635                    if (wNAF[i] == NULL) {
    628                         ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
     636                        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    629637                        OPENSSL_free(tmp_wNAF);
    630638                        goto err;
     
    635643
    636644                    if (*tmp_points == NULL) {
    637                         ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
     645                        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    638646                        OPENSSL_free(tmp_wNAF);
    639647                        goto err;
     
    655663    val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
    656664    if (val == NULL) {
    657         ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
     665        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    658666        goto err;
    659667    }
     
    672680    }
    673681    if (!(v == val + num_val)) {
    674         ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
     682        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    675683        goto err;
    676684    }
     
    706714    }
    707715
    708     if (!EC_POINTs_make_affine(group, num_val, val, ctx))
     716    if (group->meth->points_make_affine == NULL
     717        || !group->meth->points_make_affine(group, num_val, val, ctx))
    709718        goto err;
    710719
     
    746755                         *
    747756                         * The underlying EC_METHOD can optionally implement this function:
    748                          * ec_point_blind_coordinates() returns 0 in case of errors or 1 on
     757                         * ossl_ec_point_blind_coordinates() returns 0 in case of errors or 1 on
    749758                         * success or if coordinate blinding is not implemented for this
    750759                         * group.
    751760                         */
    752                         if (!ec_point_blind_coordinates(group, r, ctx)) {
    753                             ECerr(EC_F_EC_WNAF_MUL, EC_R_POINT_COORDINATES_BLIND_FAILURE);
     761                        if (!ossl_ec_point_blind_coordinates(group, r, ctx)) {
     762                            ERR_raise(ERR_LIB_EC, EC_R_POINT_COORDINATES_BLIND_FAILURE);
    754763                            goto err;
    755764                        }
     
    800809
    801810/*-
    802  * ec_wNAF_precompute_mult()
     811 * ossl_ec_wNAF_precompute_mult()
    803812 * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
    804  * for use with wNAF splitting as implemented in ec_wNAF_mul().
     813 * for use with wNAF splitting as implemented in ossl_ec_wNAF_mul().
    805814 *
    806815 * 'pre_comp->points' is an array of multiples of the generator
     
    819828 * points[2^(w-1)*numblocks]       = NULL
    820829 */
    821 int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
     830int ossl_ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
    822831{
    823832    const EC_POINT *generator;
    824833    EC_POINT *tmp_point = NULL, *base = NULL, **var;
    825     BN_CTX *new_ctx = NULL;
    826834    const BIGNUM *order;
    827835    size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
     
    829837    EC_PRE_COMP *pre_comp;
    830838    int ret = 0;
     839    int used_ctx = 0;
     840#ifndef FIPS_MODULE
     841    BN_CTX *new_ctx = NULL;
     842#endif
    831843
    832844    /* if there is an old EC_PRE_COMP object, throw it away */
     
    837849    generator = EC_GROUP_get0_generator(group);
    838850    if (generator == NULL) {
    839         ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNDEFINED_GENERATOR);
    840         goto err;
    841     }
    842 
    843     if (ctx == NULL) {
     851        ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
     852        goto err;
     853    }
     854
     855#ifndef FIPS_MODULE
     856    if (ctx == NULL)
    844857        ctx = new_ctx = BN_CTX_new();
    845         if (ctx == NULL)
    846             goto err;
    847     }
     858#endif
     859    if (ctx == NULL)
     860        goto err;
    848861
    849862    BN_CTX_start(ctx);
     863    used_ctx = 1;
    850864
    851865    order = EC_GROUP_get0_order(group);
     
    853867        goto err;
    854868    if (BN_is_zero(order)) {
    855         ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, EC_R_UNKNOWN_ORDER);
     869        ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_ORDER);
    856870        goto err;
    857871    }
     
    881895    points = OPENSSL_malloc(sizeof(*points) * (num + 1));
    882896    if (points == NULL) {
    883         ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
     897        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    884898        goto err;
    885899    }
     
    889903    for (i = 0; i < num; i++) {
    890904        if ((var[i] = EC_POINT_new(group)) == NULL) {
    891             ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
     905            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    892906            goto err;
    893907        }
     
    896910    if ((tmp_point = EC_POINT_new(group)) == NULL
    897911        || (base = EC_POINT_new(group)) == NULL) {
    898         ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_MALLOC_FAILURE);
     912        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    899913        goto err;
    900914    }
     
    928942
    929943            if (blocksize <= 2) {
    930                 ECerr(EC_F_EC_WNAF_PRECOMPUTE_MULT, ERR_R_INTERNAL_ERROR);
     944                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    931945                goto err;
    932946            }
     
    941955    }
    942956
    943     if (!EC_POINTs_make_affine(group, num, points, ctx))
     957    if (group->meth->points_make_affine == NULL
     958        || !group->meth->points_make_affine(group, num, points, ctx))
    944959        goto err;
    945960
     
    956971
    957972 err:
    958     BN_CTX_end(ctx);
     973    if (used_ctx)
     974        BN_CTX_end(ctx);
     975#ifndef FIPS_MODULE
    959976    BN_CTX_free(new_ctx);
     977#endif
    960978    EC_ec_pre_comp_free(pre_comp);
    961979    if (points) {
     
    971989}
    972990
    973 int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
     991int ossl_ec_wNAF_have_precompute_mult(const EC_GROUP *group)
    974992{
    975993    return HAVEPRECOMP(group, ec);
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_oct.c

    r91772 r94082  
    11/*
    2  * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
    88 * https://www.openssl.org/source/license.html
    99 */
     10
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
    1016
    1117#include <string.h>
     
    2127    if (group->meth->point_set_compressed_coordinates == NULL
    2228        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
    23         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
    24               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     29        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    2530        return 0;
    2631    }
    2732    if (!ec_point_is_compat(point, group)) {
    28         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
    29               EC_R_INCOMPATIBLE_OBJECTS);
     33        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    3034        return 0;
    3135    }
    3236    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
    3337        if (group->meth->field_type == NID_X9_62_prime_field)
    34             return ec_GFp_simple_set_compressed_coordinates(group, point, x,
    35                                                             y_bit, ctx);
     38            return ossl_ec_GFp_simple_set_compressed_coordinates(group, point, x,
     39                                                                 y_bit, ctx);
    3640        else
    3741#ifdef OPENSSL_NO_EC2M
    3842        {
    39             ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
    40                   EC_R_GF2M_NOT_SUPPORTED);
     43            ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
    4144            return 0;
    4245        }
    4346#else
    44             return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
    45                                                              y_bit, ctx);
     47            return ossl_ec_GF2m_simple_set_compressed_coordinates(group, point,
     48                                                                  x, y_bit, ctx);
    4649#endif
    4750    }
     
    5053}
    5154
    52 #if OPENSSL_API_COMPAT < 0x10200000L
     55#ifndef OPENSSL_NO_DEPRECATED_3_0
    5356int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
    5457                                            EC_POINT *point, const BIGNUM *x,
     
    7477    if (group->meth->point2oct == 0
    7578        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
    76         ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     79        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    7780        return 0;
    7881    }
    7982    if (!ec_point_is_compat(point, group)) {
    80         ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
     83        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    8184        return 0;
    8285    }
    8386    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
    8487        if (group->meth->field_type == NID_X9_62_prime_field)
    85             return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
     88            return ossl_ec_GFp_simple_point2oct(group, point, form, buf, len,
     89                                                ctx);
    8690        else
    8791#ifdef OPENSSL_NO_EC2M
    8892        {
    89             ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
     93            ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
    9094            return 0;
    9195        }
    9296#else
    93             return ec_GF2m_simple_point2oct(group, point,
    94                                             form, buf, len, ctx);
     97            return ossl_ec_GF2m_simple_point2oct(group, point,
     98                                                 form, buf, len, ctx);
    9599#endif
    96100    }
     
    104108    if (group->meth->oct2point == 0
    105109        && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
    106         ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
     110        ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    107111        return 0;
    108112    }
    109113    if (!ec_point_is_compat(point, group)) {
    110         ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
     114        ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
    111115        return 0;
    112116    }
    113117    if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
    114118        if (group->meth->field_type == NID_X9_62_prime_field)
    115             return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
     119            return ossl_ec_GFp_simple_oct2point(group, point, buf, len, ctx);
    116120        else
    117121#ifdef OPENSSL_NO_EC2M
    118122        {
    119             ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
     123            ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
    120124            return 0;
    121125        }
    122126#else
    123             return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
     127            return ossl_ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
    124128#endif
    125129    }
     
    138142        return 0;
    139143    if ((buf = OPENSSL_malloc(len)) == NULL) {
    140         ECerr(EC_F_EC_POINT_POINT2BUF, ERR_R_MALLOC_FAILURE);
     144        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    141145        return 0;
    142146    }
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_pmeth.c

    r91772 r94082  
    11/*
    2  * Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
    77 * https://www.openssl.org/source/license.html
    88 */
     9
     10/*
     11 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
     12 * for internal use.
     13 */
     14#include "internal/deprecated.h"
    915
    1016#include <stdio.h>
     
    4450
    4551    if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL) {
    46         ECerr(EC_F_PKEY_EC_INIT, ERR_R_MALLOC_FAILURE);
     52        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    4753        return 0;
    4854    }
     
    5460}
    5561
    56 static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
     62static int pkey_ec_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
    5763{
    5864    EC_PKEY_CTX *dctx, *sctx;
     
    104110    unsigned int sltmp;
    105111    EC_PKEY_CTX *dctx = ctx->data;
    106     EC_KEY *ec = ctx->pkey->pkey.ec;
     112    /*
     113     * Discard const. Its marked as const because this may be a cached copy of
     114     * the "real" key. These calls don't make any modifications that need to
     115     * be reflected back in the "original" key.
     116     */
     117    EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
    107118    const int sig_sz = ECDSA_size(ec);
    108119
     
    117128
    118129    if (*siglen < (size_t)sig_sz) {
    119         ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
    120         return 0;
    121     }
    122 
    123     type = (dctx->md != NULL) ? EVP_MD_type(dctx->md) : NID_sha1;
     130        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
     131        return 0;
     132    }
     133
     134    type = (dctx->md != NULL) ? EVP_MD_get_type(dctx->md) : NID_sha1;
    124135
    125136    ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
     
    137148    int ret, type;
    138149    EC_PKEY_CTX *dctx = ctx->data;
    139     EC_KEY *ec = ctx->pkey->pkey.ec;
     150    /*
     151     * Discard const. Its marked as const because this may be a cached copy of
     152     * the "real" key. These calls don't make any modifications that need to
     153     * be reflected back in the "original" key.
     154     */
     155    EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
    140156
    141157    if (dctx->md)
    142         type = EVP_MD_type(dctx->md);
     158        type = EVP_MD_get_type(dctx->md);
    143159    else
    144160        type = NID_sha1;
     
    156172    const EC_POINT *pubkey = NULL;
    157173    EC_KEY *eckey;
    158     EC_PKEY_CTX *dctx = ctx->data;
    159     if (!ctx->pkey || !ctx->peerkey) {
    160         ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
    161         return 0;
    162     }
    163 
    164     eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
     174    const EC_KEY *eckeypub;
     175    EC_PKEY_CTX *dctx = ctx->data;
     176
     177    if (ctx->pkey == NULL || ctx->peerkey == NULL) {
     178        ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
     179        return 0;
     180    }
     181    eckeypub = EVP_PKEY_get0_EC_KEY(ctx->peerkey);
     182    if (eckeypub == NULL) {
     183        ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
     184        return 0;
     185    }
     186
     187    eckey = dctx->co_key ? dctx->co_key
     188                         : (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
    165189
    166190    if (!key) {
    167191        const EC_GROUP *group;
    168192        group = EC_KEY_get0_group(eckey);
     193
     194        if (group == NULL)
     195            return 0;
    169196        *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
    170197        return 1;
    171198    }
    172     pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
     199    pubkey = EC_KEY_get0_public_key(eckeypub);
    173200
    174201    /*
     
    204231        return 0;
    205232    if ((ktmp = OPENSSL_malloc(ktmplen)) == NULL) {
    206         ECerr(EC_F_PKEY_EC_KDF_DERIVE, ERR_R_MALLOC_FAILURE);
     233        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    207234        return 0;
    208235    }
     
    210237        goto err;
    211238    /* Do KDF stuff */
    212     if (!ecdh_KDF_X9_63(key, *keylen, ktmp, ktmplen,
    213                         dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
     239    if (!ossl_ecdh_kdf_X9_63(key, *keylen, ktmp, ktmplen,
     240                             dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md,
     241                             ctx->libctx, ctx->propquery))
    214242        goto err;
    215243    rv = 1;
     
    229257        group = EC_GROUP_new_by_curve_name(p1);
    230258        if (group == NULL) {
    231             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
     259            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
    232260            return 0;
    233261        }
     
    238266    case EVP_PKEY_CTRL_EC_PARAM_ENC:
    239267        if (!dctx->gen_group) {
    240             ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
     268            ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
    241269            return 0;
    242270        }
     
    250278                return dctx->cofactor_mode;
    251279            else {
    252                 EC_KEY *ec_key = ctx->pkey->pkey.ec;
     280                const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ctx->pkey);
    253281                return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
    254282            }
     
    257285        dctx->cofactor_mode = p1;
    258286        if (p1 != -1) {
    259             EC_KEY *ec_key = ctx->pkey->pkey.ec;
     287            EC_KEY *ec_key = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
     288
     289            /*
     290             * We discarded the "const" above. This will only work if the key is
     291             * a "real" legacy key, and not a cached copy of a provided key
     292             */
     293            if (evp_pkey_is_provided(ctx->pkey)) {
     294                ERR_raise(ERR_LIB_EC, ERR_R_UNSUPPORTED);
     295                return 0;
     296            }
    260297            if (!ec_key->group)
    261298                return -2;
     
    319356
    320357    case EVP_PKEY_CTRL_MD:
    321         if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
    322             EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
    323             EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
    324             EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
    325             EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
    326             EVP_MD_type((const EVP_MD *)p2) != NID_sha512 &&
    327             EVP_MD_type((const EVP_MD *)p2) != NID_sha3_224 &&
    328             EVP_MD_type((const EVP_MD *)p2) != NID_sha3_256 &&
    329             EVP_MD_type((const EVP_MD *)p2) != NID_sha3_384 &&
    330             EVP_MD_type((const EVP_MD *)p2) != NID_sha3_512) {
    331             ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
     358        if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
     359            EVP_MD_get_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
     360            EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
     361            EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
     362            EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
     363            EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
     364            EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
     365            EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
     366            EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
     367            EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512 &&
     368            EVP_MD_get_type((const EVP_MD *)p2) != NID_sm3) {
     369            ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST_TYPE);
    332370            return 0;
    333371        }
     
    363401            nid = OBJ_ln2nid(value);
    364402        if (nid == NID_undef) {
    365             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
     403            ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
    366404            return 0;
    367405        }
     
    379417        const EVP_MD *md;
    380418        if ((md = EVP_get_digestbyname(value)) == NULL) {
    381             ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
     419            ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST);
    382420            return 0;
    383421        }
     
    399437
    400438    if (dctx->gen_group == NULL) {
    401         ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
     439        ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
    402440        return 0;
    403441    }
     
    418456
    419457    if (ctx->pkey == NULL && dctx->gen_group == NULL) {
    420         ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
     458        ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
    421459        return 0;
    422460    }
     
    437475}
    438476
    439 const EVP_PKEY_METHOD ec_pkey_meth = {
     477static const EVP_PKEY_METHOD ec_pkey_meth = {
    440478    EVP_PKEY_EC,
    441479    0,
     
    475513    pkey_ec_ctrl_str
    476514};
     515
     516const EVP_PKEY_METHOD *ossl_ec_pkey_method(void)
     517{
     518    return &ec_pkey_meth;
     519}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ec_print.c

    r91772 r94082  
    11/*
    2  * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    88 */
    99
     10#include <string.h> /* strlen */
    1011#include <openssl/crypto.h>
    11 #include <openssl/err.h>
    1212#include "ec_local.h"
    13 
    14 BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
    15                           const EC_POINT *point,
    16                           point_conversion_form_t form,
    17                           BIGNUM *ret, BN_CTX *ctx)
    18 {
    19     size_t buf_len = 0;
    20     unsigned char *buf;
    21 
    22     buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
    23 
    24     if (buf_len == 0)
    25         return NULL;
    26 
    27     ret = BN_bin2bn(buf, buf_len, ret);
    28 
    29     OPENSSL_free(buf);
    30 
    31     return ret;
    32 }
    33 
    34 EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
    35                             const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
    36 {
    37     size_t buf_len = 0;
    38     unsigned char *buf;
    39     EC_POINT *ret;
    40 
    41     if ((buf_len = BN_num_bytes(bn)) == 0)
    42         buf_len = 1;
    43     if ((buf = OPENSSL_malloc(buf_len)) == NULL) {
    44         ECerr(EC_F_EC_POINT_BN2POINT, ERR_R_MALLOC_FAILURE);
    45         return NULL;
    46     }
    47 
    48     if (!BN_bn2binpad(bn, buf, buf_len)) {
    49         OPENSSL_free(buf);
    50         return NULL;
    51     }
    52 
    53     if (point == NULL) {
    54         if ((ret = EC_POINT_new(group)) == NULL) {
    55             OPENSSL_free(buf);
    56             return NULL;
    57         }
    58     } else
    59         ret = point;
    60 
    61     if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
    62         if (ret != point)
    63             EC_POINT_clear_free(ret);
    64         OPENSSL_free(buf);
    65         return NULL;
    66     }
    67 
    68     OPENSSL_free(buf);
    69     return ret;
    70 }
    7113
    7214static const char *HEX_DIGITS = "0123456789ABCDEF";
     
    10648
    10749EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
    108                              const char *buf, EC_POINT *point, BN_CTX *ctx)
     50                             const char *hex, EC_POINT *point, BN_CTX *ctx)
    10951{
    110     EC_POINT *ret = NULL;
    111     BIGNUM *tmp_bn = NULL;
     52    int ok = 0;
     53    unsigned char *oct_buf = NULL;
     54    size_t len, oct_buf_len = 0;
     55    EC_POINT *pt = NULL;
    11256
    113     if (!BN_hex2bn(&tmp_bn, buf))
     57    if (group == NULL || hex == NULL)
    11458        return NULL;
    11559
    116     ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
     60    if (point == NULL) {
     61        pt = EC_POINT_new(group);
     62        if (pt == NULL)
     63            goto err;
     64    } else {
     65        pt = point;
     66    }
    11767
    118     BN_clear_free(tmp_bn);
     68    len = strlen(hex) / 2;
     69    oct_buf = OPENSSL_malloc(len);
     70    if (oct_buf == NULL)
     71        return NULL;
    11972
    120     return ret;
     73    if (!OPENSSL_hexstr2buf_ex(oct_buf, len, &oct_buf_len, hex, '\0')
     74        || !EC_POINT_oct2point(group, pt, oct_buf, oct_buf_len, ctx))
     75        goto err;
     76    ok = 1;
     77err:
     78    OPENSSL_clear_free(oct_buf, oct_buf_len);
     79    if (!ok) {
     80        if (pt != point)
     81            EC_POINT_clear_free(pt);
     82        pt = NULL;
     83    }
     84    return pt;
    12185}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecdh_kdf.c

    r91772 r94082  
    11/*
    2  * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2015-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    88 */
    99
     10/*
     11 * ECDH low level APIs are deprecated for public use, but still ok for
     12 * internal use.
     13 */
     14#include "internal/deprecated.h"
     15
    1016#include <string.h>
     17#include <openssl/core_names.h>
    1118#include <openssl/ec.h>
    1219#include <openssl/evp.h>
     20#include <openssl/kdf.h>
    1321#include "ec_local.h"
    1422
    1523/* Key derivation function from X9.63/SECG */
    16 /* Way more than we will ever need */
    17 #define ECDH_KDF_MAX    (1 << 30)
     24int ossl_ecdh_kdf_X9_63(unsigned char *out, size_t outlen,
     25                        const unsigned char *Z, size_t Zlen,
     26                        const unsigned char *sinfo, size_t sinfolen,
     27                        const EVP_MD *md,
     28                        OSSL_LIB_CTX *libctx, const char *propq)
     29{
     30    int ret = 0;
     31    EVP_KDF_CTX *kctx = NULL;
     32    OSSL_PARAM params[4], *p = params;
     33    const char *mdname = EVP_MD_get0_name(md);
     34    EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_X963KDF, propq);
    1835
    19 int ecdh_KDF_X9_63(unsigned char *out, size_t outlen,
    20                    const unsigned char *Z, size_t Zlen,
    21                    const unsigned char *sinfo, size_t sinfolen,
    22                    const EVP_MD *md)
    23 {
    24     EVP_MD_CTX *mctx = NULL;
    25     int rv = 0;
    26     unsigned int i;
    27     size_t mdlen;
    28     unsigned char ctr[4];
    29     if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX
    30         || Zlen > ECDH_KDF_MAX)
    31         return 0;
    32     mctx = EVP_MD_CTX_new();
    33     if (mctx == NULL)
    34         return 0;
    35     mdlen = EVP_MD_size(md);
    36     for (i = 1;; i++) {
    37         unsigned char mtmp[EVP_MAX_MD_SIZE];
    38         if (!EVP_DigestInit_ex(mctx, md, NULL))
    39             goto err;
    40         ctr[3] = i & 0xFF;
    41         ctr[2] = (i >> 8) & 0xFF;
    42         ctr[1] = (i >> 16) & 0xFF;
    43         ctr[0] = (i >> 24) & 0xFF;
    44         if (!EVP_DigestUpdate(mctx, Z, Zlen))
    45             goto err;
    46         if (!EVP_DigestUpdate(mctx, ctr, sizeof(ctr)))
    47             goto err;
    48         if (!EVP_DigestUpdate(mctx, sinfo, sinfolen))
    49             goto err;
    50         if (outlen >= mdlen) {
    51             if (!EVP_DigestFinal(mctx, out, NULL))
    52                 goto err;
    53             outlen -= mdlen;
    54             if (outlen == 0)
    55                 break;
    56             out += mdlen;
    57         } else {
    58             if (!EVP_DigestFinal(mctx, mtmp, NULL))
    59                 goto err;
    60             memcpy(out, mtmp, outlen);
    61             OPENSSL_cleanse(mtmp, mdlen);
    62             break;
    63         }
     36    if ((kctx = EVP_KDF_CTX_new(kdf)) != NULL) {
     37        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
     38                                                (char *)mdname, 0);
     39        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
     40                                                 (void *)Z, Zlen);
     41        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
     42                                                 (void *)sinfo, sinfolen);
     43        *p = OSSL_PARAM_construct_end();
     44
     45        ret = EVP_KDF_derive(kctx, out, outlen, params) > 0;
     46        EVP_KDF_CTX_free(kctx);
    6447    }
    65     rv = 1;
    66  err:
    67     EVP_MD_CTX_free(mctx);
    68     return rv;
     48    EVP_KDF_free(kdf);
     49    return ret;
    6950}
    7051
     
    7354 * Retained for ABI compatibility
    7455 */
     56#ifndef OPENSSL_NO_DEPRECATED_3_0
    7557int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
    7658                   const unsigned char *Z, size_t Zlen,
     
    7860                   const EVP_MD *md)
    7961{
    80     return ecdh_KDF_X9_63(out, outlen, Z, Zlen, sinfo, sinfolen, md);
     62    return ossl_ecdh_kdf_X9_63(out, outlen, Z, Zlen, sinfo, sinfolen, md, NULL,
     63                               NULL);
    8164}
     65#endif
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecdh_ossl.c

    r91772 r94082  
    11/*
    2  * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
    88 * https://www.openssl.org/source/license.html
    99 */
     10
     11/*
     12 * ECDH low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
    1016
    1117#include <string.h>
     
    2430{
    2531    if (ecdh->group->meth->ecdh_compute_key == NULL) {
    26         ECerr(EC_F_OSSL_ECDH_COMPUTE_KEY, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH);
     32        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDH);
    2733        return 0;
    2834    }
     
    3238
    3339/*-
    34  * This implementation is based on the following primitives in the IEEE 1363 standard:
     40 * This implementation is based on the following primitives in the
     41 * IEEE 1363 standard:
    3542 *  - ECKAS-DH1
    3643 *  - ECSVDP-DH
     44 *
     45 * It also conforms to SP800-56A r3
     46 * See Section 5.7.1.2 "Elliptic Curve Cryptography Cofactor Diffie-Hellman
     47 * (ECC CDH) Primitive:". The steps listed below refer to SP800-56A.
    3748 */
    38 int ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
    39                             const EC_POINT *pub_key, const EC_KEY *ecdh)
     49int ossl_ecdh_simple_compute_key(unsigned char **pout, size_t *poutlen,
     50                                 const EC_POINT *pub_key, const EC_KEY *ecdh)
    4051{
    4152    BN_CTX *ctx;
     
    4859    unsigned char *buf = NULL;
    4960
    50     if ((ctx = BN_CTX_new()) == NULL)
     61    if ((ctx = BN_CTX_new_ex(ecdh->libctx)) == NULL)
    5162        goto err;
    5263    BN_CTX_start(ctx);
    5364    x = BN_CTX_get(ctx);
    5465    if (x == NULL) {
    55         ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
     66        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    5667        goto err;
    5768    }
     
    5970    priv_key = EC_KEY_get0_private_key(ecdh);
    6071    if (priv_key == NULL) {
    61         ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_MISSING_PRIVATE_KEY);
     72        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
    6273        goto err;
    6374    }
     
    6576    group = EC_KEY_get0_group(ecdh);
    6677
     78    /*
     79     * Step(1) - Compute the point tmp = cofactor * owners_private_key
     80     *                                   * peer_public_key.
     81     */
    6782    if (EC_KEY_get_flags(ecdh) & EC_FLAG_COFACTOR_ECDH) {
    6883        if (!EC_GROUP_get_cofactor(group, x, NULL) ||
    6984            !BN_mul(x, x, priv_key, ctx)) {
    70             ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
     85            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    7186            goto err;
    7287        }
     
    7590
    7691    if ((tmp = EC_POINT_new(group)) == NULL) {
    77         ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
     92        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    7893        goto err;
    7994    }
    8095
    8196    if (!EC_POINT_mul(group, tmp, NULL, pub_key, priv_key, ctx)) {
    82         ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
     97        ERR_raise(ERR_LIB_EC, EC_R_POINT_ARITHMETIC_FAILURE);
    8398        goto err;
    8499    }
    85100
     101    /*
     102     * Step(2) : If point tmp is at infinity then clear intermediate values and
     103     * exit. Note: getting affine coordinates returns 0 if point is at infinity.
     104     * Step(3a) : Get x-coordinate of point x = tmp.x
     105     */
    86106    if (!EC_POINT_get_affine_coordinates(group, tmp, x, NULL, ctx)) {
    87         ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, EC_R_POINT_ARITHMETIC_FAILURE);
     107        ERR_raise(ERR_LIB_EC, EC_R_POINT_ARITHMETIC_FAILURE);
    88108        goto err;
    89109    }
    90110
     111    /*
     112     * Step(3b) : convert x to a byte string, using the field-element-to-byte
     113     * string conversion routine defined in Appendix C.2
     114     */
    91115    buflen = (EC_GROUP_get_degree(group) + 7) / 8;
    92116    len = BN_num_bytes(x);
    93117    if (len > buflen) {
    94         ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
     118        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    95119        goto err;
    96120    }
    97121    if ((buf = OPENSSL_malloc(buflen)) == NULL) {
    98         ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
     122        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    99123        goto err;
    100124    }
     
    102126    memset(buf, 0, buflen - len);
    103127    if (len != (size_t)BN_bn2bin(x, buf + buflen - len)) {
    104         ECerr(EC_F_ECDH_SIMPLE_COMPUTE_KEY, ERR_R_BN_LIB);
     128        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    105129        goto err;
    106130    }
     
    113137
    114138 err:
     139    /* Step(4) : Destroy all intermediate calculations */
     140    BN_clear(x);
    115141    EC_POINT_clear_free(tmp);
    116142    BN_CTX_end(ctx);
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecdsa_ossl.c

    r91772 r94082  
    11/*
    2  * Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
    77 * https://www.openssl.org/source/license.html
    88 */
     9
     10/*
     11 * ECDSA low level APIs are deprecated for public use, but still ok for
     12 * internal use.
     13 */
     14#include "internal/deprecated.h"
    915
    1016#include <string.h>
     
    1420#include "crypto/bn.h"
    1521#include "ec_local.h"
     22
     23int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
     24                          BIGNUM **rp)
     25{
     26    if (eckey->group->meth->ecdsa_sign_setup == NULL) {
     27        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
     28        return 0;
     29    }
     30
     31    return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
     32}
     33
     34ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
     35                               const BIGNUM *in_kinv, const BIGNUM *in_r,
     36                               EC_KEY *eckey)
     37{
     38    if (eckey->group->meth->ecdsa_sign_sig == NULL) {
     39        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
     40        return NULL;
     41    }
     42
     43    return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
     44                                              in_kinv, in_r, eckey);
     45}
     46
     47int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
     48                          const ECDSA_SIG *sig, EC_KEY *eckey)
     49{
     50    if (eckey->group->meth->ecdsa_verify_sig == NULL) {
     51        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
     52        return 0;
     53    }
     54
     55    return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
     56}
    1657
    1758int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
     
    4586
    4687    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
    47         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
     88        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    4889        return 0;
    4990    }
    5091    if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
    51         ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_MISSING_PRIVATE_KEY);
     92        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
    5293        return 0;
    5394    }
    5495
    5596    if (!EC_KEY_can_sign(eckey)) {
    56         ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
     97        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
    5798        return 0;
    5899    }
    59100
    60101    if ((ctx = ctx_in) == NULL) {
    61         if ((ctx = BN_CTX_new()) == NULL) {
    62             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
     102        if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
     103            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    63104            return 0;
    64105        }
    65106    }
    66107
    67     k = BN_new();               /* this value is later returned in *kinvp */
     108    k = BN_secure_new();        /* this value is later returned in *kinvp */
    68109    r = BN_new();               /* this value is later returned in *rp */
    69110    X = BN_new();
    70111    if (k == NULL || r == NULL || X == NULL) {
    71         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
     112        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    72113        goto err;
    73114    }
    74115    if ((tmp_point = EC_POINT_new(group)) == NULL) {
    75         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
     116        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    76117        goto err;
    77118    }
     
    91132                if (!BN_generate_dsa_nonce(k, order, priv_key,
    92133                                           dgst, dlen, ctx)) {
    93                     ECerr(EC_F_ECDSA_SIGN_SETUP,
    94                           EC_R_RANDOM_NUMBER_GENERATION_FAILED);
     134                    ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
    95135                    goto err;
    96136                }
    97137            } else {
    98                 if (!BN_priv_rand_range(k, order)) {
    99                     ECerr(EC_F_ECDSA_SIGN_SETUP,
    100                           EC_R_RANDOM_NUMBER_GENERATION_FAILED);
     138                if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
     139                    ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
    101140                    goto err;
    102141                }
     
    106145        /* compute r the x-coordinate of generator * k */
    107146        if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
    108             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
     147            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    109148            goto err;
    110149        }
    111150
    112151        if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
    113             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
     152            ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    114153            goto err;
    115154        }
    116155
    117156        if (!BN_nnmod(r, X, order, ctx)) {
    118             ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
     157            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    119158            goto err;
    120159        }
     
    122161
    123162    /* compute the inverse of k */
    124     if (!ec_group_do_inverse_ord(group, k, k, ctx)) {
    125         ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
     163    if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
     164        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    126165        goto err;
    127166    }
     
    146185}
    147186
    148 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
    149                           BIGNUM **rp)
     187int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
     188                                 BIGNUM **rp)
    150189{
    151190    return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
    152191}
    153192
    154 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
    155                                const BIGNUM *in_kinv, const BIGNUM *in_r,
    156                                EC_KEY *eckey)
     193ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
     194                                      const BIGNUM *in_kinv, const BIGNUM *in_r,
     195                                      EC_KEY *eckey)
    157196{
    158197    int ok = 0, i;
     
    168207
    169208    if (group == NULL) {
    170         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
     209        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    171210        return NULL;
    172211    }
    173212    if (priv_key == NULL) {
    174         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_MISSING_PRIVATE_KEY);
     213        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
    175214        return NULL;
    176215    }
    177216
    178217    if (!EC_KEY_can_sign(eckey)) {
    179         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
     218        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
    180219        return NULL;
    181220    }
     
    183222    ret = ECDSA_SIG_new();
    184223    if (ret == NULL) {
    185         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
     224        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    186225        return NULL;
    187226    }
     
    189228    ret->s = BN_new();
    190229    if (ret->r == NULL || ret->s == NULL) {
    191         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
     230        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    192231        goto err;
    193232    }
    194233    s = ret->s;
    195234
    196     if ((ctx = BN_CTX_new()) == NULL
     235    if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
    197236        || (m = BN_new()) == NULL) {
    198         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
     237        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    199238        goto err;
    200239    }
     
    208247        dgst_len = (i + 7) / 8;
    209248    if (!BN_bin2bn(dgst, dgst_len, m)) {
    210         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
     249        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    211250        goto err;
    212251    }
    213252    /* If still too long, truncate remaining bits with a shift */
    214253    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
    215         ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
     254        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    216255        goto err;
    217256    }
     
    219258        if (in_kinv == NULL || in_r == NULL) {
    220259            if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
    221                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
     260                ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
    222261                goto err;
    223262            }
     
    226265            ckinv = in_kinv;
    227266            if (BN_copy(ret->r, in_r) == NULL) {
    228                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
     267                ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    229268                goto err;
    230269            }
     
    240279        if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
    241280            || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
    242             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
     281            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    243282            goto err;
    244283        }
    245284        if (!bn_mod_add_fixed_top(s, s, m, order)) {
    246             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
     285            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    247286            goto err;
    248287        }
     
    253292        if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
    254293            || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
    255             ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
     294            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    256295            goto err;
    257296        }
     
    263302             */
    264303            if (in_kinv != NULL && in_r != NULL) {
    265                 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
     304                ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
    266305                goto err;
    267306            }
     
    315354}
    316355
    317 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
    318                           const ECDSA_SIG *sig, EC_KEY *eckey)
     356int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
     357                                 const ECDSA_SIG *sig, EC_KEY *eckey)
    319358{
    320359    int ret = -1, i;
     
    329368    if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
    330369        (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
    331         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
     370        ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
    332371        return -1;
    333372    }
    334373
    335374    if (!EC_KEY_can_sign(eckey)) {
    336         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
     375        ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
    337376        return -1;
    338377    }
    339378
    340     ctx = BN_CTX_new();
     379    ctx = BN_CTX_new_ex(eckey->libctx);
    341380    if (ctx == NULL) {
    342         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
     381        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    343382        return -1;
    344383    }
     
    349388    X = BN_CTX_get(ctx);
    350389    if (X == NULL) {
    351         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
     390        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    352391        goto err;
    353392    }
     
    355394    order = EC_GROUP_get0_order(group);
    356395    if (order == NULL) {
    357         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
     396        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    358397        goto err;
    359398    }
     
    362401        BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
    363402        BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
    364         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
     403        ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
    365404        ret = 0;                /* signature is invalid */
    366405        goto err;
    367406    }
    368407    /* calculate tmp1 = inv(S) mod order */
    369     if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
    370         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
     408    if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
     409        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    371410        goto err;
    372411    }
     
    379418        dgst_len = (i + 7) / 8;
    380419    if (!BN_bin2bn(dgst, dgst_len, m)) {
    381         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
     420        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    382421        goto err;
    383422    }
    384423    /* If still too long truncate remaining bits with a shift */
    385424    if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
    386         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
     425        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    387426        goto err;
    388427    }
    389428    /* u1 = m * tmp mod order */
    390429    if (!BN_mod_mul(u1, m, u2, order, ctx)) {
    391         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
     430        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    392431        goto err;
    393432    }
    394433    /* u2 = r * w mod q */
    395434    if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
    396         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
     435        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    397436        goto err;
    398437    }
    399438
    400439    if ((point = EC_POINT_new(group)) == NULL) {
    401         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
     440        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    402441        goto err;
    403442    }
    404443    if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
    405         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
     444        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    406445        goto err;
    407446    }
    408447
    409448    if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
    410         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
     449        ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
    411450        goto err;
    412451    }
    413452
    414453    if (!BN_nnmod(u1, X, order, ctx)) {
    415         ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
     454        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    416455        goto err;
    417456    }
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecdsa_sign.c

    r91772 r94082  
    11/*
    2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2015-2020 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
    77 * https://www.openssl.org/source/license.html
    88 */
     9
     10/*
     11 * ECDSA low level APIs are deprecated for public use, but still ok for
     12 * internal use.
     13 */
     14#include "internal/deprecated.h"
    915
    1016#include <openssl/ec.h>
     
    2329    if (eckey->meth->sign_sig != NULL)
    2430        return eckey->meth->sign_sig(dgst, dlen, kinv, rp, eckey);
    25     ECerr(EC_F_ECDSA_DO_SIGN_EX, EC_R_OPERATION_NOT_SUPPORTED);
     31    ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
    2632    return NULL;
    2733}
     
    3945    if (eckey->meth->sign != NULL)
    4046        return eckey->meth->sign(type, dgst, dlen, sig, siglen, kinv, r, eckey);
    41     ECerr(EC_F_ECDSA_SIGN_EX, EC_R_OPERATION_NOT_SUPPORTED);
     47    ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
    4248    return 0;
    4349}
     
    4854    if (eckey->meth->sign_setup != NULL)
    4955        return eckey->meth->sign_setup(eckey, ctx_in, kinvp, rp);
    50     ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_OPERATION_NOT_SUPPORTED);
     56    ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
    5157    return 0;
    5258}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecdsa_vrf.c

    r91772 r94082  
    22 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
    77 * https://www.openssl.org/source/license.html
    88 */
     9
     10/*
     11 * ECDSA low level APIs are deprecated for public use, but still ok for
     12 * internal use.
     13 */
     14#include "internal/deprecated.h"
    915
    1016#include <openssl/ec.h>
     
    2329    if (eckey->meth->verify_sig != NULL)
    2430        return eckey->meth->verify_sig(dgst, dgst_len, sig, eckey);
    25     ECerr(EC_F_ECDSA_DO_VERIFY, EC_R_OPERATION_NOT_SUPPORTED);
     31    ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
    2632    return -1;
    2733}
     
    3945        return eckey->meth->verify(type, dgst, dgst_len, sigbuf, sig_len,
    4046                                   eckey);
    41     ECerr(EC_F_ECDSA_VERIFY, EC_R_OPERATION_NOT_SUPPORTED);
     47    ERR_raise(ERR_LIB_EC, EC_R_OPERATION_NOT_SUPPORTED);
    4248    return -1;
    4349}
  • trunk/src/libs/openssl-3.0.1/crypto/ec/eck_prn.c

    r91772 r94082  
    11/*
    2  * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
    88 * https://www.openssl.org/source/license.html
    99 */
     10
     11#include "internal/deprecated.h"
    1012
    1113#include <stdio.h>
     
    1517#include <openssl/bn.h>
    1618
    17 #ifndef OPENSSL_NO_STDIO
     19#ifndef OPENSSL_NO_DEPRECATED_3_0
     20# ifndef OPENSSL_NO_STDIO
    1821int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
    1922{
     
    2225
    2326    if ((b = BIO_new(BIO_s_file())) == NULL) {
    24         ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB);
     27        ERR_raise(ERR_LIB_EC, ERR_R_BUF_LIB);
    2528        return 0;
    2629    }
     
    3740
    3841    if ((b = BIO_new(BIO_s_file())) == NULL) {
    39         ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
     42        ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB);
    4043        return 0;
    4144    }
     
    5255
    5356    if ((b = BIO_new(BIO_s_file())) == NULL) {
    54         ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
     57        ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB);
    5558        return 0;
    5659    }
     
    6063    return ret;
    6164}
    62 #endif
     65#endif /* OPENSSL_NO_STDIO */
    6366
    6467static int print_bin(BIO *fp, const char *str, const unsigned char *num,
     
    7073    BN_CTX *ctx = NULL;
    7174    const EC_POINT *point = NULL;
    72     BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL;
     75    BIGNUM *p = NULL, *a = NULL, *b = NULL;
     76    unsigned char *gen_buf = NULL;
    7377    const BIGNUM *order = NULL, *cofactor = NULL;
    7478    const unsigned char *seed;
    75     size_t seed_len = 0;
     79    size_t seed_len = 0, gen_buf_len = 0;
    7680
    7781    static const char *gen_compressed = "Generator (compressed):";
     
    113117        }
    114118    } else {
     119        const char *form_str;
    115120        /* explicit parameters */
    116121        int is_char_two = 0;
    117122        point_conversion_form_t form;
    118         int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
     123        int tmp_nid = EC_GROUP_get_field_type(x);
    119124
    120125        if (tmp_nid == NID_X9_62_characteristic_two_field)
     
    145150        form = EC_GROUP_get_point_conversion_form(x);
    146151
    147         if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) {
     152        gen_buf_len = EC_POINT_point2buf(x, point, form, &gen_buf, ctx);
     153        if (gen_buf_len == 0) {
    148154            reason = ERR_R_EC_LIB;
    149155            goto err;
     
    186192        if ((b != NULL) && !ASN1_bn_print(bp, "B:   ", b, NULL, off))
    187193            goto err;
    188         if (form == POINT_CONVERSION_COMPRESSED) {
    189             if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
    190                                                 NULL, off))
    191                 goto err;
    192         } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
    193             if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
    194                                                 NULL, off))
    195                 goto err;
    196         } else {                /* form == POINT_CONVERSION_HYBRID */
    197 
    198             if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
    199                                                 NULL, off))
    200                 goto err;
    201         }
    202         if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
    203                                               NULL, off))
     194
     195        if (form == POINT_CONVERSION_COMPRESSED)
     196            form_str = gen_compressed;
     197        else if (form == POINT_CONVERSION_UNCOMPRESSED)
     198            form_str = gen_uncompressed;
     199        else
     200            form_str = gen_hybrid;
     201        if (gen_buf != NULL
     202            && !print_bin(bp, form_str, gen_buf, gen_buf_len, off))
     203            goto err;
     204
     205        if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, NULL, off))
    204206            goto err;
    205207        if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
     
    212214 err:
    213215    if (!ret)
    214         ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
     216        ERR_raise(ERR_LIB_EC, reason);
    215217    BN_free(p);
    216218    BN_free(a);
    217219    BN_free(b);
    218     BN_free(gen);
     220    OPENSSL_clear_free(gen_buf, gen_buf_len);
    219221    BN_CTX_free(ctx);
    220222    return ret;
     
    258260    return 1;
    259261}
     262#endif /* OPENSSL_NO_DEPRECATED_3_0 */
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_mont.c

    r91772 r94082  
    11/*
    2  * Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    99 */
    1010
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
     16
    1117#include <openssl/err.h>
    1218
     
    1824        EC_FLAGS_DEFAULT_OCT,
    1925        NID_X9_62_prime_field,
    20         ec_GFp_mont_group_init,
    21         ec_GFp_mont_group_finish,
    22         ec_GFp_mont_group_clear_finish,
    23         ec_GFp_mont_group_copy,
    24         ec_GFp_mont_group_set_curve,
    25         ec_GFp_simple_group_get_curve,
    26         ec_GFp_simple_group_get_degree,
    27         ec_group_simple_order_bits,
    28         ec_GFp_simple_group_check_discriminant,
    29         ec_GFp_simple_point_init,
    30         ec_GFp_simple_point_finish,
    31         ec_GFp_simple_point_clear_finish,
    32         ec_GFp_simple_point_copy,
    33         ec_GFp_simple_point_set_to_infinity,
    34         ec_GFp_simple_set_Jprojective_coordinates_GFp,
    35         ec_GFp_simple_get_Jprojective_coordinates_GFp,
    36         ec_GFp_simple_point_set_affine_coordinates,
    37         ec_GFp_simple_point_get_affine_coordinates,
     26        ossl_ec_GFp_mont_group_init,
     27        ossl_ec_GFp_mont_group_finish,
     28        ossl_ec_GFp_mont_group_clear_finish,
     29        ossl_ec_GFp_mont_group_copy,
     30        ossl_ec_GFp_mont_group_set_curve,
     31        ossl_ec_GFp_simple_group_get_curve,
     32        ossl_ec_GFp_simple_group_get_degree,
     33        ossl_ec_group_simple_order_bits,
     34        ossl_ec_GFp_simple_group_check_discriminant,
     35        ossl_ec_GFp_simple_point_init,
     36        ossl_ec_GFp_simple_point_finish,
     37        ossl_ec_GFp_simple_point_clear_finish,
     38        ossl_ec_GFp_simple_point_copy,
     39        ossl_ec_GFp_simple_point_set_to_infinity,
     40        ossl_ec_GFp_simple_point_set_affine_coordinates,
     41        ossl_ec_GFp_simple_point_get_affine_coordinates,
    3842        0, 0, 0,
    39         ec_GFp_simple_add,
    40         ec_GFp_simple_dbl,
    41         ec_GFp_simple_invert,
    42         ec_GFp_simple_is_at_infinity,
    43         ec_GFp_simple_is_on_curve,
    44         ec_GFp_simple_cmp,
    45         ec_GFp_simple_make_affine,
    46         ec_GFp_simple_points_make_affine,
     43        ossl_ec_GFp_simple_add,
     44        ossl_ec_GFp_simple_dbl,
     45        ossl_ec_GFp_simple_invert,
     46        ossl_ec_GFp_simple_is_at_infinity,
     47        ossl_ec_GFp_simple_is_on_curve,
     48        ossl_ec_GFp_simple_cmp,
     49        ossl_ec_GFp_simple_make_affine,
     50        ossl_ec_GFp_simple_points_make_affine,
    4751        0 /* mul */ ,
    4852        0 /* precompute_mult */ ,
    4953        0 /* have_precompute_mult */ ,
    50         ec_GFp_mont_field_mul,
    51         ec_GFp_mont_field_sqr,
     54        ossl_ec_GFp_mont_field_mul,
     55        ossl_ec_GFp_mont_field_sqr,
    5256        0 /* field_div */ ,
    53         ec_GFp_mont_field_inv,
    54         ec_GFp_mont_field_encode,
    55         ec_GFp_mont_field_decode,
    56         ec_GFp_mont_field_set_to_one,
    57         ec_key_simple_priv2oct,
    58         ec_key_simple_oct2priv,
     57        ossl_ec_GFp_mont_field_inv,
     58        ossl_ec_GFp_mont_field_encode,
     59        ossl_ec_GFp_mont_field_decode,
     60        ossl_ec_GFp_mont_field_set_to_one,
     61        ossl_ec_key_simple_priv2oct,
     62        ossl_ec_key_simple_oct2priv,
    5963        0, /* set private */
    60         ec_key_simple_generate_key,
    61         ec_key_simple_check_key,
    62         ec_key_simple_generate_public_key,
     64        ossl_ec_key_simple_generate_key,
     65        ossl_ec_key_simple_check_key,
     66        ossl_ec_key_simple_generate_public_key,
    6367        0, /* keycopy */
    6468        0, /* keyfinish */
    65         ecdh_simple_compute_key,
     69        ossl_ecdh_simple_compute_key,
     70        ossl_ecdsa_simple_sign_setup,
     71        ossl_ecdsa_simple_sign_sig,
     72        ossl_ecdsa_simple_verify_sig,
    6673        0, /* field_inverse_mod_ord */
    67         ec_GFp_simple_blind_coordinates,
    68         ec_GFp_simple_ladder_pre,
    69         ec_GFp_simple_ladder_step,
    70         ec_GFp_simple_ladder_post
     74        ossl_ec_GFp_simple_blind_coordinates,
     75        ossl_ec_GFp_simple_ladder_pre,
     76        ossl_ec_GFp_simple_ladder_step,
     77        ossl_ec_GFp_simple_ladder_post
    7178    };
    7279
     
    7481}
    7582
    76 int ec_GFp_mont_group_init(EC_GROUP *group)
     83int ossl_ec_GFp_mont_group_init(EC_GROUP *group)
    7784{
    7885    int ok;
    7986
    80     ok = ec_GFp_simple_group_init(group);
     87    ok = ossl_ec_GFp_simple_group_init(group);
    8188    group->field_data1 = NULL;
    8289    group->field_data2 = NULL;
     
    8491}
    8592
    86 void ec_GFp_mont_group_finish(EC_GROUP *group)
     93void ossl_ec_GFp_mont_group_finish(EC_GROUP *group)
    8794{
    8895    BN_MONT_CTX_free(group->field_data1);
     
    9097    BN_free(group->field_data2);
    9198    group->field_data2 = NULL;
    92     ec_GFp_simple_group_finish(group);
    93 }
    94 
    95 void ec_GFp_mont_group_clear_finish(EC_GROUP *group)
     99    ossl_ec_GFp_simple_group_finish(group);
     100}
     101
     102void ossl_ec_GFp_mont_group_clear_finish(EC_GROUP *group)
    96103{
    97104    BN_MONT_CTX_free(group->field_data1);
     
    99106    BN_clear_free(group->field_data2);
    100107    group->field_data2 = NULL;
    101     ec_GFp_simple_group_clear_finish(group);
    102 }
    103 
    104 int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
     108    ossl_ec_GFp_simple_group_clear_finish(group);
     109}
     110
     111int ossl_ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src)
    105112{
    106113    BN_MONT_CTX_free(dest->field_data1);
     
    109116    dest->field_data2 = NULL;
    110117
    111     if (!ec_GFp_simple_group_copy(dest, src))
     118    if (!ossl_ec_GFp_simple_group_copy(dest, src))
    112119        return 0;
    113120
     
    133140}
    134141
    135 int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
    136                                 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
     142int ossl_ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p,
     143                                     const BIGNUM *a, const BIGNUM *b,
     144                                     BN_CTX *ctx)
    137145{
    138146    BN_CTX *new_ctx = NULL;
     
    147155
    148156    if (ctx == NULL) {
    149         ctx = new_ctx = BN_CTX_new();
     157        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    150158        if (ctx == NULL)
    151159            return 0;
     
    156164        goto err;
    157165    if (!BN_MONT_CTX_set(mont, p, ctx)) {
    158         ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
     166        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    159167        goto err;
    160168    }
     
    170178    one = NULL;
    171179
    172     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
     180    ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
    173181
    174182    if (!ret) {
     
    186194}
    187195
    188 int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
    189                           const BIGNUM *b, BN_CTX *ctx)
     196int ossl_ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
     197                               const BIGNUM *b, BN_CTX *ctx)
    190198{
    191199    if (group->field_data1 == NULL) {
    192         ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
     200        ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
    193201        return 0;
    194202    }
     
    197205}
    198206
    199 int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
    200                           BN_CTX *ctx)
     207int ossl_ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
     208                               BN_CTX *ctx)
    201209{
    202210    if (group->field_data1 == NULL) {
    203         ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
     211        ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
    204212        return 0;
    205213    }
     
    213221 * We have a Mont structure, so SCA hardening is FLT inversion.
    214222 */
    215 int ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
    216                             BN_CTX *ctx)
     223int ossl_ec_GFp_mont_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
     224                               BN_CTX *ctx)
    217225{
    218226    BIGNUM *e = NULL;
     
    223231        return 0;
    224232
    225     if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
     233    if (ctx == NULL
     234            && (ctx = new_ctx = BN_CTX_secure_new_ex(group->libctx)) == NULL)
    226235        return 0;
    227236
     
    244253    /* throw an error on zero */
    245254    if (BN_is_zero(r)) {
    246         ECerr(EC_F_EC_GFP_MONT_FIELD_INV, EC_R_CANNOT_INVERT);
     255        ERR_raise(ERR_LIB_EC, EC_R_CANNOT_INVERT);
    247256        goto err;
    248257    }
     
    256265}
    257266
    258 int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
    259                              const BIGNUM *a, BN_CTX *ctx)
     267int ossl_ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r,
     268                                  const BIGNUM *a, BN_CTX *ctx)
    260269{
    261270    if (group->field_data1 == NULL) {
    262         ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
     271        ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
    263272        return 0;
    264273    }
     
    267276}
    268277
    269 int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
    270                              const BIGNUM *a, BN_CTX *ctx)
     278int ossl_ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r,
     279                                  const BIGNUM *a, BN_CTX *ctx)
    271280{
    272281    if (group->field_data1 == NULL) {
    273         ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
     282        ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
    274283        return 0;
    275284    }
     
    278287}
    279288
    280 int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
    281                                  BN_CTX *ctx)
     289int ossl_ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r,
     290                                      BN_CTX *ctx)
    282291{
    283292    if (group->field_data2 == NULL) {
    284         ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
     293        ERR_raise(ERR_LIB_EC, EC_R_NOT_INITIALIZED);
    285294        return 0;
    286295    }
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_nist.c

    r91772 r94082  
    11/*
    2  * Copyright 2001-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
    88 * https://www.openssl.org/source/license.html
    99 */
     10
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
    1016
    1117#include <limits.h>
     
    2026        EC_FLAGS_DEFAULT_OCT,
    2127        NID_X9_62_prime_field,
    22         ec_GFp_simple_group_init,
    23         ec_GFp_simple_group_finish,
    24         ec_GFp_simple_group_clear_finish,
    25         ec_GFp_nist_group_copy,
    26         ec_GFp_nist_group_set_curve,
    27         ec_GFp_simple_group_get_curve,
    28         ec_GFp_simple_group_get_degree,
    29         ec_group_simple_order_bits,
    30         ec_GFp_simple_group_check_discriminant,
    31         ec_GFp_simple_point_init,
    32         ec_GFp_simple_point_finish,
    33         ec_GFp_simple_point_clear_finish,
    34         ec_GFp_simple_point_copy,
    35         ec_GFp_simple_point_set_to_infinity,
    36         ec_GFp_simple_set_Jprojective_coordinates_GFp,
    37         ec_GFp_simple_get_Jprojective_coordinates_GFp,
    38         ec_GFp_simple_point_set_affine_coordinates,
    39         ec_GFp_simple_point_get_affine_coordinates,
     28        ossl_ec_GFp_simple_group_init,
     29        ossl_ec_GFp_simple_group_finish,
     30        ossl_ec_GFp_simple_group_clear_finish,
     31        ossl_ec_GFp_nist_group_copy,
     32        ossl_ec_GFp_nist_group_set_curve,
     33        ossl_ec_GFp_simple_group_get_curve,
     34        ossl_ec_GFp_simple_group_get_degree,
     35        ossl_ec_group_simple_order_bits,
     36        ossl_ec_GFp_simple_group_check_discriminant,
     37        ossl_ec_GFp_simple_point_init,
     38        ossl_ec_GFp_simple_point_finish,
     39        ossl_ec_GFp_simple_point_clear_finish,
     40        ossl_ec_GFp_simple_point_copy,
     41        ossl_ec_GFp_simple_point_set_to_infinity,
     42        ossl_ec_GFp_simple_point_set_affine_coordinates,
     43        ossl_ec_GFp_simple_point_get_affine_coordinates,
    4044        0, 0, 0,
    41         ec_GFp_simple_add,
    42         ec_GFp_simple_dbl,
    43         ec_GFp_simple_invert,
    44         ec_GFp_simple_is_at_infinity,
    45         ec_GFp_simple_is_on_curve,
    46         ec_GFp_simple_cmp,
    47         ec_GFp_simple_make_affine,
    48         ec_GFp_simple_points_make_affine,
     45        ossl_ec_GFp_simple_add,
     46        ossl_ec_GFp_simple_dbl,
     47        ossl_ec_GFp_simple_invert,
     48        ossl_ec_GFp_simple_is_at_infinity,
     49        ossl_ec_GFp_simple_is_on_curve,
     50        ossl_ec_GFp_simple_cmp,
     51        ossl_ec_GFp_simple_make_affine,
     52        ossl_ec_GFp_simple_points_make_affine,
    4953        0 /* mul */ ,
    5054        0 /* precompute_mult */ ,
    5155        0 /* have_precompute_mult */ ,
    52         ec_GFp_nist_field_mul,
    53         ec_GFp_nist_field_sqr,
     56        ossl_ec_GFp_nist_field_mul,
     57        ossl_ec_GFp_nist_field_sqr,
    5458        0 /* field_div */ ,
    55         ec_GFp_simple_field_inv,
     59        ossl_ec_GFp_simple_field_inv,
    5660        0 /* field_encode */ ,
    5761        0 /* field_decode */ ,
    5862        0,                      /* field_set_to_one */
    59         ec_key_simple_priv2oct,
    60         ec_key_simple_oct2priv,
     63        ossl_ec_key_simple_priv2oct,
     64        ossl_ec_key_simple_oct2priv,
    6165        0, /* set private */
    62         ec_key_simple_generate_key,
    63         ec_key_simple_check_key,
    64         ec_key_simple_generate_public_key,
     66        ossl_ec_key_simple_generate_key,
     67        ossl_ec_key_simple_check_key,
     68        ossl_ec_key_simple_generate_public_key,
    6569        0, /* keycopy */
    6670        0, /* keyfinish */
    67         ecdh_simple_compute_key,
     71        ossl_ecdh_simple_compute_key,
     72        ossl_ecdsa_simple_sign_setup,
     73        ossl_ecdsa_simple_sign_sig,
     74        ossl_ecdsa_simple_verify_sig,
    6875        0, /* field_inverse_mod_ord */
    69         ec_GFp_simple_blind_coordinates,
    70         ec_GFp_simple_ladder_pre,
    71         ec_GFp_simple_ladder_step,
    72         ec_GFp_simple_ladder_post
     76        ossl_ec_GFp_simple_blind_coordinates,
     77        ossl_ec_GFp_simple_ladder_pre,
     78        ossl_ec_GFp_simple_ladder_step,
     79        ossl_ec_GFp_simple_ladder_post
    7380    };
    7481
     
    7683}
    7784
    78 int ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
     85int ossl_ec_GFp_nist_group_copy(EC_GROUP *dest, const EC_GROUP *src)
    7986{
    8087    dest->field_mod_func = src->field_mod_func;
    8188
    82     return ec_GFp_simple_group_copy(dest, src);
     89    return ossl_ec_GFp_simple_group_copy(dest, src);
    8390}
    8491
    85 int ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
    86                                 const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
     92int ossl_ec_GFp_nist_group_set_curve(EC_GROUP *group, const BIGNUM *p,
     93                                     const BIGNUM *a, const BIGNUM *b,
     94                                     BN_CTX *ctx)
    8795{
    8896    int ret = 0;
     
    9098
    9199    if (ctx == NULL)
    92         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
     100        if ((ctx = new_ctx = BN_CTX_new_ex(group->libctx)) == NULL)
    93101            return 0;
    94102
     
    106114        group->field_mod_func = BN_nist_mod_521;
    107115    else {
    108         ECerr(EC_F_EC_GFP_NIST_GROUP_SET_CURVE, EC_R_NOT_A_NIST_PRIME);
     116        ERR_raise(ERR_LIB_EC, EC_R_NOT_A_NIST_PRIME);
    109117        goto err;
    110118    }
    111119
    112     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
     120    ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
    113121
    114122 err:
     
    118126}
    119127
    120 int ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
    121                           const BIGNUM *b, BN_CTX *ctx)
     128int ossl_ec_GFp_nist_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
     129                               const BIGNUM *b, BN_CTX *ctx)
    122130{
    123131    int ret = 0;
     
    125133
    126134    if (!group || !r || !a || !b) {
    127         ECerr(EC_F_EC_GFP_NIST_FIELD_MUL, ERR_R_PASSED_NULL_PARAMETER);
     135        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    128136        goto err;
    129137    }
    130138    if (!ctx)
    131         if ((ctx_new = ctx = BN_CTX_new()) == NULL)
     139        if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
    132140            goto err;
    133141
     
    143151}
    144152
    145 int ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
    146                           BN_CTX *ctx)
     153int ossl_ec_GFp_nist_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
     154                               BN_CTX *ctx)
    147155{
    148156    int ret = 0;
     
    150158
    151159    if (!group || !r || !a) {
    152         ECerr(EC_F_EC_GFP_NIST_FIELD_SQR, EC_R_PASSED_NULL_PARAMETER);
     160        ERR_raise(ERR_LIB_EC, EC_R_PASSED_NULL_PARAMETER);
    153161        goto err;
    154162    }
    155163    if (!ctx)
    156         if ((ctx_new = ctx = BN_CTX_new()) == NULL)
     164        if ((ctx_new = ctx = BN_CTX_new_ex(group->libctx)) == NULL)
    157165            goto err;
    158166
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_nistp224.c

    r91772 r94082  
    11/*
    2  * Copyright 2010-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2010-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    2525
    2626/*
     27 * ECDSA low level APIs are deprecated for public use, but still ok for
     28 * internal use.
     29 */
     30#include "internal/deprecated.h"
     31
     32/*
    2733 * A 64-bit implementation of the NIST P-224 elliptic curve point multiplication
    2834 *
     
    3238
    3339#include <openssl/opensslconf.h>
    34 #ifdef OPENSSL_NO_EC_NISTP_64_GCC_128
    35 NON_EMPTY_TRANSLATION_UNIT
    36 #else
    37 
    38 # include <stdint.h>
    39 # include <string.h>
    40 # include <openssl/err.h>
    41 # include "ec_local.h"
    42 
    43 # if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16
    44   /* even with gcc, the typedef won't work for 32-bit platforms */
    45 typedef __uint128_t uint128_t;  /* nonstandard; implemented by gcc on 64-bit
    46                                  * platforms */
    47 # else
    48 #  error "Your compiler doesn't appear to support 128-bit integer types"
    49 # endif
     40
     41#include <stdint.h>
     42#include <string.h>
     43#include <openssl/err.h>
     44#include "ec_local.h"
     45
     46#include "internal/numbers.h"
     47
     48#ifndef INT128_MAX
     49# error "Your compiler doesn't appear to support 128-bit integer types"
     50#endif
    5051
    5152typedef uint8_t u8;
     
    246247        EC_FLAGS_DEFAULT_OCT,
    247248        NID_X9_62_prime_field,
    248         ec_GFp_nistp224_group_init,
    249         ec_GFp_simple_group_finish,
    250         ec_GFp_simple_group_clear_finish,
    251         ec_GFp_nist_group_copy,
    252         ec_GFp_nistp224_group_set_curve,
    253         ec_GFp_simple_group_get_curve,
    254         ec_GFp_simple_group_get_degree,
    255         ec_group_simple_order_bits,
    256         ec_GFp_simple_group_check_discriminant,
    257         ec_GFp_simple_point_init,
    258         ec_GFp_simple_point_finish,
    259         ec_GFp_simple_point_clear_finish,
    260         ec_GFp_simple_point_copy,
    261         ec_GFp_simple_point_set_to_infinity,
    262         ec_GFp_simple_set_Jprojective_coordinates_GFp,
    263         ec_GFp_simple_get_Jprojective_coordinates_GFp,
    264         ec_GFp_simple_point_set_affine_coordinates,
    265         ec_GFp_nistp224_point_get_affine_coordinates,
     249        ossl_ec_GFp_nistp224_group_init,
     250        ossl_ec_GFp_simple_group_finish,
     251        ossl_ec_GFp_simple_group_clear_finish,
     252        ossl_ec_GFp_nist_group_copy,
     253        ossl_ec_GFp_nistp224_group_set_curve,
     254        ossl_ec_GFp_simple_group_get_curve,
     255        ossl_ec_GFp_simple_group_get_degree,
     256        ossl_ec_group_simple_order_bits,
     257        ossl_ec_GFp_simple_group_check_discriminant,
     258        ossl_ec_GFp_simple_point_init,
     259        ossl_ec_GFp_simple_point_finish,
     260        ossl_ec_GFp_simple_point_clear_finish,
     261        ossl_ec_GFp_simple_point_copy,
     262        ossl_ec_GFp_simple_point_set_to_infinity,
     263        ossl_ec_GFp_simple_point_set_affine_coordinates,
     264        ossl_ec_GFp_nistp224_point_get_affine_coordinates,
    266265        0 /* point_set_compressed_coordinates */ ,
    267266        0 /* point2oct */ ,
    268267        0 /* oct2point */ ,
    269         ec_GFp_simple_add,
    270         ec_GFp_simple_dbl,
    271         ec_GFp_simple_invert,
    272         ec_GFp_simple_is_at_infinity,
    273         ec_GFp_simple_is_on_curve,
    274         ec_GFp_simple_cmp,
    275         ec_GFp_simple_make_affine,
    276         ec_GFp_simple_points_make_affine,
    277         ec_GFp_nistp224_points_mul,
    278         ec_GFp_nistp224_precompute_mult,
    279         ec_GFp_nistp224_have_precompute_mult,
    280         ec_GFp_nist_field_mul,
    281         ec_GFp_nist_field_sqr,
     268        ossl_ec_GFp_simple_add,
     269        ossl_ec_GFp_simple_dbl,
     270        ossl_ec_GFp_simple_invert,
     271        ossl_ec_GFp_simple_is_at_infinity,
     272        ossl_ec_GFp_simple_is_on_curve,
     273        ossl_ec_GFp_simple_cmp,
     274        ossl_ec_GFp_simple_make_affine,
     275        ossl_ec_GFp_simple_points_make_affine,
     276        ossl_ec_GFp_nistp224_points_mul,
     277        ossl_ec_GFp_nistp224_precompute_mult,
     278        ossl_ec_GFp_nistp224_have_precompute_mult,
     279        ossl_ec_GFp_nist_field_mul,
     280        ossl_ec_GFp_nist_field_sqr,
    282281        0 /* field_div */ ,
    283         ec_GFp_simple_field_inv,
     282        ossl_ec_GFp_simple_field_inv,
    284283        0 /* field_encode */ ,
    285284        0 /* field_decode */ ,
    286285        0,                      /* field_set_to_one */
    287         ec_key_simple_priv2oct,
    288         ec_key_simple_oct2priv,
     286        ossl_ec_key_simple_priv2oct,
     287        ossl_ec_key_simple_oct2priv,
    289288        0, /* set private */
    290         ec_key_simple_generate_key,
    291         ec_key_simple_check_key,
    292         ec_key_simple_generate_public_key,
     289        ossl_ec_key_simple_generate_key,
     290        ossl_ec_key_simple_check_key,
     291        ossl_ec_key_simple_generate_public_key,
    293292        0, /* keycopy */
    294293        0, /* keyfinish */
    295         ecdh_simple_compute_key,
     294        ossl_ecdh_simple_compute_key,
     295        ossl_ecdsa_simple_sign_setup,
     296        ossl_ecdsa_simple_sign_sig,
     297        ossl_ecdsa_simple_verify_sig,
    296298        0, /* field_inverse_mod_ord */
    297299        0, /* blind_coordinates */
     
    333335
    334336    if (BN_is_negative(bn)) {
    335         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
     337        ERR_raise(ERR_LIB_EC, EC_R_BIGNUM_OUT_OF_RANGE);
    336338        return 0;
    337339    }
    338340    num_bytes = BN_bn2lebinpad(bn, b_out, sizeof(b_out));
    339341    if (num_bytes < 0) {
    340         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
     342        ERR_raise(ERR_LIB_EC, EC_R_BIGNUM_OUT_OF_RANGE);
    341343        return 0;
    342344    }
     
    664666static void felem_neg(felem out, const felem in)
    665667{
    666     widefelem tmp = {0};
     668    widefelem tmp;
     669
     670    memset(tmp, 0, sizeof(tmp));
    667671    felem_diff_128_64(tmp, in);
    668672    felem_reduce(out, tmp);
     
    12021206                bits |= get_bit(scalars[num], i) << 1;
    12031207                bits |= get_bit(scalars[num], i - 1);
    1204                 ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
     1208                ossl_ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
    12051209
    12061210                /* select the point to add or subtract */
     
    12361240
    12371241    if (!ret) {
    1238         ECerr(EC_F_NISTP224_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     1242        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    12391243        return ret;
    12401244    }
     
    12441248    ret->lock = CRYPTO_THREAD_lock_new();
    12451249    if (ret->lock == NULL) {
    1246         ECerr(EC_F_NISTP224_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     1250        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    12471251        OPENSSL_free(ret);
    12481252        return NULL;
     
    12671271
    12681272    CRYPTO_DOWN_REF(&p->references, &i, p->lock);
    1269     REF_PRINT_COUNT("EC_nistp224", x);
     1273    REF_PRINT_COUNT("EC_nistp224", p);
    12701274    if (i > 0)
    12711275        return;
     
    12811285 */
    12821286
    1283 int ec_GFp_nistp224_group_init(EC_GROUP *group)
     1287int ossl_ec_GFp_nistp224_group_init(EC_GROUP *group)
    12841288{
    12851289    int ret;
    1286     ret = ec_GFp_simple_group_init(group);
     1290    ret = ossl_ec_GFp_simple_group_init(group);
    12871291    group->a_is_minus3 = 1;
    12881292    return ret;
    12891293}
    12901294
    1291 int ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
    1292                                     const BIGNUM *a, const BIGNUM *b,
    1293                                     BN_CTX *ctx)
     1295int ossl_ec_GFp_nistp224_group_set_curve(EC_GROUP *group, const BIGNUM *p,
     1296                                         const BIGNUM *a, const BIGNUM *b,
     1297                                         BN_CTX *ctx)
    12941298{
    12951299    int ret = 0;
     1300    BIGNUM *curve_p, *curve_a, *curve_b;
     1301#ifndef FIPS_MODULE
    12961302    BN_CTX *new_ctx = NULL;
    1297     BIGNUM *curve_p, *curve_a, *curve_b;
    12981303
    12991304    if (ctx == NULL)
    1300         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
    1301             return 0;
     1305        ctx = new_ctx = BN_CTX_new();
     1306#endif
     1307    if (ctx == NULL)
     1308        return 0;
     1309
    13021310    BN_CTX_start(ctx);
    13031311    curve_p = BN_CTX_get(ctx);
     
    13101318    BN_bin2bn(nistp224_curve_params[2], sizeof(felem_bytearray), curve_b);
    13111319    if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
    1312         ECerr(EC_F_EC_GFP_NISTP224_GROUP_SET_CURVE,
    1313               EC_R_WRONG_CURVE_PARAMETERS);
     1320        ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
    13141321        goto err;
    13151322    }
    13161323    group->field_mod_func = BN_nist_mod_224;
    1317     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
     1324    ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
    13181325 err:
    13191326    BN_CTX_end(ctx);
     1327#ifndef FIPS_MODULE
    13201328    BN_CTX_free(new_ctx);
     1329#endif
    13211330    return ret;
    13221331}
     
    13261335 * (X/Z^2, Y/Z^3)
    13271336 */
    1328 int ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
    1329                                                  const EC_POINT *point,
    1330                                                  BIGNUM *x, BIGNUM *y,
    1331                                                  BN_CTX *ctx)
     1337int ossl_ec_GFp_nistp224_point_get_affine_coordinates(const EC_GROUP *group,
     1338                                                      const EC_POINT *point,
     1339                                                      BIGNUM *x, BIGNUM *y,
     1340                                                      BN_CTX *ctx)
    13321341{
    13331342    felem z1, z2, x_in, y_in, x_out, y_out;
     
    13351344
    13361345    if (EC_POINT_is_at_infinity(group, point)) {
    1337         ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
    1338               EC_R_POINT_AT_INFINITY);
     1346        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
    13391347        return 0;
    13401348    }
     
    13501358    if (x != NULL) {
    13511359        if (!felem_to_BN(x, x_out)) {
    1352             ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
    1353                   ERR_R_BN_LIB);
     1360            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    13541361            return 0;
    13551362        }
     
    13621369    if (y != NULL) {
    13631370        if (!felem_to_BN(y, y_out)) {
    1364             ECerr(EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES,
    1365                   ERR_R_BN_LIB);
     1371            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    13661372            return 0;
    13671373        }
     
    13771383     * normally shouldn't happen).
    13781384     */
    1379     ec_GFp_nistp_points_make_affine_internal(num,
    1380                                              points,
    1381                                              sizeof(felem),
    1382                                              tmp_felems,
    1383                                              (void (*)(void *))felem_one,
    1384                                              felem_is_zero_int,
    1385                                              (void (*)(void *, const void *))
    1386                                              felem_assign,
    1387                                              (void (*)(void *, const void *))
    1388                                              felem_square_reduce, (void (*)
    1389                                                                    (void *,
    1390                                                                     const void
    1391                                                                     *,
    1392                                                                     const void
    1393                                                                     *))
    1394                                              felem_mul_reduce,
    1395                                              (void (*)(void *, const void *))
    1396                                              felem_inv,
    1397                                              (void (*)(void *, const void *))
    1398                                              felem_contract);
     1385    ossl_ec_GFp_nistp_points_make_affine_internal(num,
     1386                                                  points,
     1387                                                  sizeof(felem),
     1388                                                  tmp_felems,
     1389                                                  (void (*)(void *))felem_one,
     1390                                                  felem_is_zero_int,
     1391                                                  (void (*)(void *, const void *))
     1392                                                  felem_assign,
     1393                                                  (void (*)(void *, const void *))
     1394                                                  felem_square_reduce, (void (*)
     1395                                                                        (void *,
     1396                                                                         const void
     1397                                                                         *,
     1398                                                                         const void
     1399                                                                         *))
     1400                                                  felem_mul_reduce,
     1401                                                  (void (*)(void *, const void *))
     1402                                                  felem_inv,
     1403                                                  (void (*)(void *, const void *))
     1404                                                  felem_contract);
    13991405}
    14001406
     
    14031409 * values Result is stored in r (r can equal one of the inputs).
    14041410 */
    1405 int ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
    1406                                const BIGNUM *scalar, size_t num,
    1407                                const EC_POINT *points[],
    1408                                const BIGNUM *scalars[], BN_CTX *ctx)
     1411int ossl_ec_GFp_nistp224_points_mul(const EC_GROUP *group, EC_POINT *r,
     1412                                    const BIGNUM *scalar, size_t num,
     1413                                    const EC_POINT *points[],
     1414                                    const BIGNUM *scalars[], BN_CTX *ctx)
    14091415{
    14101416    int ret = 0;
     
    14501456            !felem_to_BN(y, g_pre_comp[0][1][1]) ||
    14511457            !felem_to_BN(z, g_pre_comp[0][1][2])) {
    1452             ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
     1458            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    14531459            goto err;
    14541460        }
    1455         if (!EC_POINT_set_Jprojective_coordinates_GFp(group,
    1456                                                       generator, x, y, z,
    1457                                                       ctx))
     1461        if (!ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group,
     1462                                                                generator,
     1463                                                                x, y, z, ctx))
    14581464            goto err;
    14591465        if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
     
    14831489        if ((secrets == NULL) || (pre_comp == NULL)
    14841490            || (mixed && (tmp_felems == NULL))) {
    1485             ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_MALLOC_FAILURE);
     1491            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    14861492            goto err;
    14871493        }
     
    15101516                     */
    15111517                    if (!BN_nnmod(tmp_scalar, p_scalar, group->order, ctx)) {
    1512                         ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
     1518                        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    15131519                        goto err;
    15141520                    }
     
    15201526                }
    15211527                if (num_bytes < 0) {
    1522                     ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
     1528                    ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    15231529                    goto err;
    15241530                }
     
    15621568             */
    15631569            if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
    1564                 ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
     1570                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    15651571                goto err;
    15661572            }
     
    15861592    if ((!felem_to_BN(x, x_in)) || (!felem_to_BN(y, y_in)) ||
    15871593        (!felem_to_BN(z, z_in))) {
    1588         ECerr(EC_F_EC_GFP_NISTP224_POINTS_MUL, ERR_R_BN_LIB);
     1594        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    15891595        goto err;
    15901596    }
    1591     ret = EC_POINT_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
     1597    ret = ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, r, x, y, z,
     1598                                                             ctx);
    15921599
    15931600 err:
     
    16001607}
    16011608
    1602 int ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
     1609int ossl_ec_GFp_nistp224_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
    16031610{
    16041611    int ret = 0;
    16051612    NISTP224_PRE_COMP *pre = NULL;
    16061613    int i, j;
    1607     BN_CTX *new_ctx = NULL;
    16081614    BIGNUM *x, *y;
    16091615    EC_POINT *generator = NULL;
    16101616    felem tmp_felems[32];
     1617#ifndef FIPS_MODULE
     1618    BN_CTX *new_ctx = NULL;
     1619#endif
    16111620
    16121621    /* throw away old precomputation */
    16131622    EC_pre_comp_free(group);
     1623
     1624#ifndef FIPS_MODULE
    16141625    if (ctx == NULL)
    1615         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
    1616             return 0;
     1626        ctx = new_ctx = BN_CTX_new();
     1627#endif
     1628    if (ctx == NULL)
     1629        return 0;
     1630
    16171631    BN_CTX_start(ctx);
    16181632    x = BN_CTX_get(ctx);
     
    17221736    BN_CTX_end(ctx);
    17231737    EC_POINT_free(generator);
     1738#ifndef FIPS_MODULE
    17241739    BN_CTX_free(new_ctx);
     1740#endif
    17251741    EC_nistp224_pre_comp_free(pre);
    17261742    return ret;
    17271743}
    17281744
    1729 int ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group)
     1745int ossl_ec_GFp_nistp224_have_precompute_mult(const EC_GROUP *group)
    17301746{
    17311747    return HAVEPRECOMP(group, nistp224);
    17321748}
    1733 
    1734 #endif
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_nistp256.c

    r91772 r94082  
    11/*
    2  * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    2525
    2626/*
     27 * ECDSA low level APIs are deprecated for public use, but still ok for
     28 * internal use.
     29 */
     30#include "internal/deprecated.h"
     31
     32/*
    2733 * A 64-bit implementation of the NIST P-256 elliptic curve point multiplication
    2834 *
     
    3339
    3440#include <openssl/opensslconf.h>
    35 #ifdef OPENSSL_NO_EC_NISTP_64_GCC_128
    36 NON_EMPTY_TRANSLATION_UNIT
    37 #else
    38 
    39 # include <stdint.h>
    40 # include <string.h>
    41 # include <openssl/err.h>
    42 # include "ec_local.h"
    43 
    44 # if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16
    45   /* even with gcc, the typedef won't work for 32-bit platforms */
    46 typedef __uint128_t uint128_t;  /* nonstandard; implemented by gcc on 64-bit
    47                                  * platforms */
    48 typedef __int128_t int128_t;
    49 # else
    50 #  error "Your compiler doesn't appear to support 128-bit integer types"
    51 # endif
     41
     42#include <stdint.h>
     43#include <string.h>
     44#include <openssl/err.h>
     45#include "ec_local.h"
     46
     47#include "internal/numbers.h"
     48
     49#ifndef INT128_MAX
     50# error "Your compiler doesn't appear to support 128-bit integer types"
     51#endif
    5252
    5353typedef uint8_t u8;
     
    5757/*
    5858 * The underlying field. P256 operates over GF(2^256-2^224+2^192+2^96-1). We
    59  * can serialise an element of this field into 32 bytes. We call this an
     59 * can serialize an element of this field into 32 bytes. We call this an
    6060 * felem_bytearray.
    6161 */
     
    111111 */
    112112
    113 # define NLIMBS 4
     113#define NLIMBS 4
    114114
    115115typedef uint128_t limb;
     
    136136
    137137/*
    138  * smallfelem_to_bin32 takes a smallfelem and serialises into a little
     138 * smallfelem_to_bin32 takes a smallfelem and serializes into a little
    139139 * endian, 32 byte array. This assumes that the CPU is little-endian.
    140140 */
     
    154154
    155155    if (BN_is_negative(bn)) {
    156         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
     156        ERR_raise(ERR_LIB_EC, EC_R_BIGNUM_OUT_OF_RANGE);
    157157        return 0;
    158158    }
    159159    num_bytes = BN_bn2lebinpad(bn, b_out, sizeof(b_out));
    160160    if (num_bytes < 0) {
    161         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
     161        ERR_raise(ERR_LIB_EC, EC_R_BIGNUM_OUT_OF_RANGE);
    162162        return 0;
    163163    }
     
    243243}
    244244
    245 # define two105m41m9 (((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9)
    246 # define two105 (((limb)1) << 105)
    247 # define two105m41p9 (((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9)
     245#define two105m41m9 (((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9)
     246#define two105 (((limb)1) << 105)
     247#define two105m41p9 (((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9)
    248248
    249249/* zero105 is 0 mod p */
     
    288288}
    289289
    290 # define two107m43m11 (((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11)
    291 # define two107 (((limb)1) << 107)
    292 # define two107m43p11 (((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11)
     290#define two107m43m11 (((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11)
     291#define two107 (((limb)1) << 107)
     292#define two107m43p11 (((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11)
    293293
    294294/* zero107 is 0 mod p */
     
    359359}
    360360
    361 # define two64m0 (((limb)1) << 64) - 1
    362 # define two110p32m0 (((limb)1) << 110) + (((limb)1) << 32) - 1
    363 # define two64m46 (((limb)1) << 64) - (((limb)1) << 46)
    364 # define two64m32 (((limb)1) << 64) - (((limb)1) << 32)
     361#define two64m0 (((limb)1) << 64) - 1
     362#define two110p32m0 (((limb)1) << 110) + (((limb)1) << 32) - 1
     363#define two64m46 (((limb)1) << 64) - (((limb)1) << 46)
     364#define two64m32 (((limb)1) << 64) - (((limb)1) << 32)
    365365
    366366/* zero110 is 0 mod p */
     
    712712}
    713713
    714 # define two100m36m4 (((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4)
    715 # define two100 (((limb)1) << 100)
    716 # define two100m36p4 (((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4)
     714#define two100m36m4 (((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4)
     715#define two100 (((limb)1) << 100)
     716#define two100m36p4 (((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4)
    717717/* zero100 is 0 mod p */
    718718static const felem zero100 =
     
    17411741                bits |= get_bit(scalars[num], i) << 1;
    17421742                bits |= get_bit(scalars[num], i - 1);
    1743                 ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
     1743                ossl_ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
    17441744
    17451745                /*
     
    17821782        EC_FLAGS_DEFAULT_OCT,
    17831783        NID_X9_62_prime_field,
    1784         ec_GFp_nistp256_group_init,
    1785         ec_GFp_simple_group_finish,
    1786         ec_GFp_simple_group_clear_finish,
    1787         ec_GFp_nist_group_copy,
    1788         ec_GFp_nistp256_group_set_curve,
    1789         ec_GFp_simple_group_get_curve,
    1790         ec_GFp_simple_group_get_degree,
    1791         ec_group_simple_order_bits,
    1792         ec_GFp_simple_group_check_discriminant,
    1793         ec_GFp_simple_point_init,
    1794         ec_GFp_simple_point_finish,
    1795         ec_GFp_simple_point_clear_finish,
    1796         ec_GFp_simple_point_copy,
    1797         ec_GFp_simple_point_set_to_infinity,
    1798         ec_GFp_simple_set_Jprojective_coordinates_GFp,
    1799         ec_GFp_simple_get_Jprojective_coordinates_GFp,
    1800         ec_GFp_simple_point_set_affine_coordinates,
    1801         ec_GFp_nistp256_point_get_affine_coordinates,
     1784        ossl_ec_GFp_nistp256_group_init,
     1785        ossl_ec_GFp_simple_group_finish,
     1786        ossl_ec_GFp_simple_group_clear_finish,
     1787        ossl_ec_GFp_nist_group_copy,
     1788        ossl_ec_GFp_nistp256_group_set_curve,
     1789        ossl_ec_GFp_simple_group_get_curve,
     1790        ossl_ec_GFp_simple_group_get_degree,
     1791        ossl_ec_group_simple_order_bits,
     1792        ossl_ec_GFp_simple_group_check_discriminant,
     1793        ossl_ec_GFp_simple_point_init,
     1794        ossl_ec_GFp_simple_point_finish,
     1795        ossl_ec_GFp_simple_point_clear_finish,
     1796        ossl_ec_GFp_simple_point_copy,
     1797        ossl_ec_GFp_simple_point_set_to_infinity,
     1798        ossl_ec_GFp_simple_point_set_affine_coordinates,
     1799        ossl_ec_GFp_nistp256_point_get_affine_coordinates,
    18021800        0 /* point_set_compressed_coordinates */ ,
    18031801        0 /* point2oct */ ,
    18041802        0 /* oct2point */ ,
    1805         ec_GFp_simple_add,
    1806         ec_GFp_simple_dbl,
    1807         ec_GFp_simple_invert,
    1808         ec_GFp_simple_is_at_infinity,
    1809         ec_GFp_simple_is_on_curve,
    1810         ec_GFp_simple_cmp,
    1811         ec_GFp_simple_make_affine,
    1812         ec_GFp_simple_points_make_affine,
    1813         ec_GFp_nistp256_points_mul,
    1814         ec_GFp_nistp256_precompute_mult,
    1815         ec_GFp_nistp256_have_precompute_mult,
    1816         ec_GFp_nist_field_mul,
    1817         ec_GFp_nist_field_sqr,
     1803        ossl_ec_GFp_simple_add,
     1804        ossl_ec_GFp_simple_dbl,
     1805        ossl_ec_GFp_simple_invert,
     1806        ossl_ec_GFp_simple_is_at_infinity,
     1807        ossl_ec_GFp_simple_is_on_curve,
     1808        ossl_ec_GFp_simple_cmp,
     1809        ossl_ec_GFp_simple_make_affine,
     1810        ossl_ec_GFp_simple_points_make_affine,
     1811        ossl_ec_GFp_nistp256_points_mul,
     1812        ossl_ec_GFp_nistp256_precompute_mult,
     1813        ossl_ec_GFp_nistp256_have_precompute_mult,
     1814        ossl_ec_GFp_nist_field_mul,
     1815        ossl_ec_GFp_nist_field_sqr,
    18181816        0 /* field_div */ ,
    1819         ec_GFp_simple_field_inv,
     1817        ossl_ec_GFp_simple_field_inv,
    18201818        0 /* field_encode */ ,
    18211819        0 /* field_decode */ ,
    18221820        0,                      /* field_set_to_one */
    1823         ec_key_simple_priv2oct,
    1824         ec_key_simple_oct2priv,
     1821        ossl_ec_key_simple_priv2oct,
     1822        ossl_ec_key_simple_oct2priv,
    18251823        0, /* set private */
    1826         ec_key_simple_generate_key,
    1827         ec_key_simple_check_key,
    1828         ec_key_simple_generate_public_key,
     1824        ossl_ec_key_simple_generate_key,
     1825        ossl_ec_key_simple_check_key,
     1826        ossl_ec_key_simple_generate_public_key,
    18291827        0, /* keycopy */
    18301828        0, /* keyfinish */
    1831         ecdh_simple_compute_key,
     1829        ossl_ecdh_simple_compute_key,
     1830        ossl_ecdsa_simple_sign_setup,
     1831        ossl_ecdsa_simple_sign_sig,
     1832        ossl_ecdsa_simple_verify_sig,
    18321833        0, /* field_inverse_mod_ord */
    18331834        0, /* blind_coordinates */
     
    18501851
    18511852    if (ret == NULL) {
    1852         ECerr(EC_F_NISTP256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     1853        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    18531854        return ret;
    18541855    }
     
    18581859    ret->lock = CRYPTO_THREAD_lock_new();
    18591860    if (ret->lock == NULL) {
    1860         ECerr(EC_F_NISTP256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     1861        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    18611862        OPENSSL_free(ret);
    18621863        return NULL;
     
    18811882
    18821883    CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
    1883     REF_PRINT_COUNT("EC_nistp256", x);
     1884    REF_PRINT_COUNT("EC_nistp256", pre);
    18841885    if (i > 0)
    18851886        return;
     
    18951896 */
    18961897
    1897 int ec_GFp_nistp256_group_init(EC_GROUP *group)
     1898int ossl_ec_GFp_nistp256_group_init(EC_GROUP *group)
    18981899{
    18991900    int ret;
    1900     ret = ec_GFp_simple_group_init(group);
     1901    ret = ossl_ec_GFp_simple_group_init(group);
    19011902    group->a_is_minus3 = 1;
    19021903    return ret;
    19031904}
    19041905
    1905 int ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
    1906                                     const BIGNUM *a, const BIGNUM *b,
    1907                                     BN_CTX *ctx)
     1906int ossl_ec_GFp_nistp256_group_set_curve(EC_GROUP *group, const BIGNUM *p,
     1907                                         const BIGNUM *a, const BIGNUM *b,
     1908                                         BN_CTX *ctx)
    19081909{
    19091910    int ret = 0;
     1911    BIGNUM *curve_p, *curve_a, *curve_b;
     1912#ifndef FIPS_MODULE
    19101913    BN_CTX *new_ctx = NULL;
    1911     BIGNUM *curve_p, *curve_a, *curve_b;
    19121914
    19131915    if (ctx == NULL)
    1914         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
    1915             return 0;
     1916        ctx = new_ctx = BN_CTX_new();
     1917#endif
     1918    if (ctx == NULL)
     1919        return 0;
     1920
    19161921    BN_CTX_start(ctx);
    19171922    curve_p = BN_CTX_get(ctx);
     
    19241929    BN_bin2bn(nistp256_curve_params[2], sizeof(felem_bytearray), curve_b);
    19251930    if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
    1926         ECerr(EC_F_EC_GFP_NISTP256_GROUP_SET_CURVE,
    1927               EC_R_WRONG_CURVE_PARAMETERS);
     1931        ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
    19281932        goto err;
    19291933    }
    19301934    group->field_mod_func = BN_nist_mod_256;
    1931     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
     1935    ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
    19321936 err:
    19331937    BN_CTX_end(ctx);
     1938#ifndef FIPS_MODULE
    19341939    BN_CTX_free(new_ctx);
     1940#endif
    19351941    return ret;
    19361942}
     
    19401946 * (X/Z^2, Y/Z^3)
    19411947 */
    1942 int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
    1943                                                  const EC_POINT *point,
    1944                                                  BIGNUM *x, BIGNUM *y,
    1945                                                  BN_CTX *ctx)
     1948int ossl_ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
     1949                                                      const EC_POINT *point,
     1950                                                      BIGNUM *x, BIGNUM *y,
     1951                                                      BN_CTX *ctx)
    19461952{
    19471953    felem z1, z2, x_in, y_in;
     
    19501956
    19511957    if (EC_POINT_is_at_infinity(group, point)) {
    1952         ECerr(EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES,
    1953               EC_R_POINT_AT_INFINITY);
     1958        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
    19541959        return 0;
    19551960    }
     
    19651970    if (x != NULL) {
    19661971        if (!smallfelem_to_BN(x, x_out)) {
    1967             ECerr(EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES,
    1968                   ERR_R_BN_LIB);
     1972            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    19691973            return 0;
    19701974        }
     
    19771981    if (y != NULL) {
    19781982        if (!smallfelem_to_BN(y, y_out)) {
    1979             ECerr(EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES,
    1980                   ERR_R_BN_LIB);
     1983            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    19811984            return 0;
    19821985        }
     
    19931996     * normally shouldn't happen).
    19941997     */
    1995     ec_GFp_nistp_points_make_affine_internal(num,
    1996                                              points,
    1997                                              sizeof(smallfelem),
    1998                                              tmp_smallfelems,
    1999                                              (void (*)(void *))smallfelem_one,
    2000                                              smallfelem_is_zero_int,
    2001                                              (void (*)(void *, const void *))
    2002                                              smallfelem_assign,
    2003                                              (void (*)(void *, const void *))
    2004                                              smallfelem_square_contract,
    2005                                              (void (*)
    2006                                               (void *, const void *,
    2007                                                const void *))
    2008                                              smallfelem_mul_contract,
    2009                                              (void (*)(void *, const void *))
    2010                                              smallfelem_inv_contract,
    2011                                              /* nothing to contract */
    2012                                              (void (*)(void *, const void *))
    2013                                              smallfelem_assign);
     1998    ossl_ec_GFp_nistp_points_make_affine_internal(num,
     1999                                                  points,
     2000                                                  sizeof(smallfelem),
     2001                                                  tmp_smallfelems,
     2002                                                  (void (*)(void *))smallfelem_one,
     2003                                                  smallfelem_is_zero_int,
     2004                                                  (void (*)(void *, const void *))
     2005                                                  smallfelem_assign,
     2006                                                  (void (*)(void *, const void *))
     2007                                                  smallfelem_square_contract,
     2008                                                  (void (*)
     2009                                                   (void *, const void *,
     2010                                                    const void *))
     2011                                                  smallfelem_mul_contract,
     2012                                                  (void (*)(void *, const void *))
     2013                                                  smallfelem_inv_contract,
     2014                                                  /* nothing to contract */
     2015                                                  (void (*)(void *, const void *))
     2016                                                  smallfelem_assign);
    20142017}
    20152018
     
    20182021 * values Result is stored in r (r can equal one of the inputs).
    20192022 */
    2020 int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
    2021                                const BIGNUM *scalar, size_t num,
    2022                                const EC_POINT *points[],
    2023                                const BIGNUM *scalars[], BN_CTX *ctx)
     2023int ossl_ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
     2024                                    const BIGNUM *scalar, size_t num,
     2025                                    const EC_POINT *points[],
     2026                                    const BIGNUM *scalars[], BN_CTX *ctx)
    20242027{
    20252028    int ret = 0;
     
    20662069            !smallfelem_to_BN(y, g_pre_comp[0][1][1]) ||
    20672070            !smallfelem_to_BN(z, g_pre_comp[0][1][2])) {
    2068             ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
     2071            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    20692072            goto err;
    20702073        }
    2071         if (!EC_POINT_set_Jprojective_coordinates_GFp(group,
    2072                                                       generator, x, y, z,
    2073                                                       ctx))
     2074        if (!ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group,
     2075                                                                generator,
     2076                                                                x, y, z, ctx))
    20742077            goto err;
    20752078        if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
     
    20982101        if ((secrets == NULL) || (pre_comp == NULL)
    20992102            || (mixed && (tmp_smallfelems == NULL))) {
    2100             ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
     2103            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    21012104            goto err;
    21022105        }
     
    21302133                     */
    21312134                    if (!BN_nnmod(tmp_scalar, p_scalar, group->order, ctx)) {
    2132                         ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
     2135                        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    21332136                        goto err;
    21342137                    }
     
    21402143                }
    21412144                if (num_bytes < 0) {
    2142                     ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
     2145                    ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    21432146                    goto err;
    21442147                }
     
    21842187             */
    21852188            if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
    2186                 ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
     2189                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    21872190                goto err;
    21882191            }
     
    22082211    if ((!smallfelem_to_BN(x, x_in)) || (!smallfelem_to_BN(y, y_in)) ||
    22092212        (!smallfelem_to_BN(z, z_in))) {
    2210         ECerr(EC_F_EC_GFP_NISTP256_POINTS_MUL, ERR_R_BN_LIB);
     2213        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    22112214        goto err;
    22122215    }
    2213     ret = EC_POINT_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
     2216    ret = ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, r, x, y, z,
     2217                                                             ctx);
    22142218
    22152219 err:
     
    22222226}
    22232227
    2224 int ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
     2228int ossl_ec_GFp_nistp256_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
    22252229{
    22262230    int ret = 0;
    22272231    NISTP256_PRE_COMP *pre = NULL;
    22282232    int i, j;
    2229     BN_CTX *new_ctx = NULL;
    22302233    BIGNUM *x, *y;
    22312234    EC_POINT *generator = NULL;
    22322235    smallfelem tmp_smallfelems[32];
    22332236    felem x_tmp, y_tmp, z_tmp;
     2237#ifndef FIPS_MODULE
     2238    BN_CTX *new_ctx = NULL;
     2239#endif
    22342240
    22352241    /* throw away old precomputation */
    22362242    EC_pre_comp_free(group);
     2243
     2244#ifndef FIPS_MODULE
    22372245    if (ctx == NULL)
    2238         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
    2239             return 0;
     2246        ctx = new_ctx = BN_CTX_new();
     2247#endif
     2248    if (ctx == NULL)
     2249        return 0;
     2250
    22402251    BN_CTX_start(ctx);
    22412252    x = BN_CTX_get(ctx);
     
    23552366    BN_CTX_end(ctx);
    23562367    EC_POINT_free(generator);
     2368#ifndef FIPS_MODULE
    23572369    BN_CTX_free(new_ctx);
     2370#endif
    23582371    EC_nistp256_pre_comp_free(pre);
    23592372    return ret;
    23602373}
    23612374
    2362 int ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group)
     2375int ossl_ec_GFp_nistp256_have_precompute_mult(const EC_GROUP *group)
    23632376{
    23642377    return HAVEPRECOMP(group, nistp256);
    23652378}
    2366 #endif
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_nistp521.c

    r91772 r94082  
    11/*
    2  * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    2525
    2626/*
     27 * ECDSA low level APIs are deprecated for public use, but still ok for
     28 * internal use.
     29 */
     30#include "internal/deprecated.h"
     31
     32/*
    2733 * A 64-bit implementation of the NIST P-521 elliptic curve point multiplication
    2834 *
     
    3339
    3440#include <openssl/e_os2.h>
    35 #ifdef OPENSSL_NO_EC_NISTP_64_GCC_128
    36 NON_EMPTY_TRANSLATION_UNIT
    37 #else
    38 
    39 # include <string.h>
    40 # include <openssl/err.h>
    41 # include "ec_local.h"
    42 
    43 # if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16
    44   /* even with gcc, the typedef won't work for 32-bit platforms */
    45 typedef __uint128_t uint128_t;  /* nonstandard; implemented by gcc on 64-bit
    46                                  * platforms */
    47 # else
    48 #  error "Your compiler doesn't appear to support 128-bit integer types"
    49 # endif
     41
     42#include <string.h>
     43#include <openssl/err.h>
     44#include "ec_local.h"
     45
     46#include "internal/numbers.h"
     47
     48#ifndef INT128_MAX
     49# error "Your compiler doesn't appear to support 128-bit integer types"
     50#endif
    5051
    5152typedef uint8_t u8;
     
    5354
    5455/*
    55  * The underlying field. P521 operates over GF(2^521-1). We can serialise an
     56 * The underlying field. P521 operates over GF(2^521-1). We can serialize an
    5657 * element of this field into 66 bytes where the most significant byte
    5758 * contains only a single bit. We call this an felem_bytearray.
     
    126127 * 'largefelem' */
    127128
    128 # define NLIMBS 9
     129#define NLIMBS 9
    129130
    130131typedef uint64_t limb;
     
    154155
    155156/*
    156  * felem_to_bin66 takes an felem and serialises into a little endian, 66 byte
     157 * felem_to_bin66 takes an felem and serializes into a little endian, 66 byte
    157158 * array. This assumes that the CPU is little-endian.
    158159 */
     
    178179
    179180    if (BN_is_negative(bn)) {
    180         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
     181        ERR_raise(ERR_LIB_EC, EC_R_BIGNUM_OUT_OF_RANGE);
    181182        return 0;
    182183    }
    183184    num_bytes = BN_bn2lebinpad(bn, b_out, sizeof(b_out));
    184185    if (num_bytes < 0) {
    185         ECerr(EC_F_BN_TO_FELEM, EC_R_BIGNUM_OUT_OF_RANGE);
     186        ERR_raise(ERR_LIB_EC, EC_R_BIGNUM_OUT_OF_RANGE);
    186187        return 0;
    187188    }
     
    401402 *   out[i] < 17 * max(in[i]) * max(in[i])
    402403 */
    403 static void felem_square(largefelem out, const felem in)
     404static void felem_square_ref(largefelem out, const felem in)
    404405{
    405406    felem inx2, inx4;
     
    485486 *   out[i] < 17 * max(in1[i]) * max(in2[i])
    486487 */
    487 static void felem_mul(largefelem out, const felem in1, const felem in2)
     488static void felem_mul_ref(largefelem out, const felem in1, const felem in2)
    488489{
    489490    felem in2x2;
     
    674675     */
    675676}
     677
     678#if defined(ECP_NISTP521_ASM)
     679void felem_square_wrapper(largefelem out, const felem in);
     680void felem_mul_wrapper(largefelem out, const felem in1, const felem in2);
     681
     682static void (*felem_square_p)(largefelem out, const felem in) =
     683    felem_square_wrapper;
     684static void (*felem_mul_p)(largefelem out, const felem in1, const felem in2) =
     685    felem_mul_wrapper;
     686
     687void p521_felem_square(largefelem out, const felem in);
     688void p521_felem_mul(largefelem out, const felem in1, const felem in2);
     689
     690# if defined(_ARCH_PPC64)
     691#  include "crypto/ppc_arch.h"
     692# endif
     693
     694void felem_select(void)
     695{
     696# if defined(_ARCH_PPC64)
     697    if ((OPENSSL_ppccap_P & PPC_MADD300) && (OPENSSL_ppccap_P & PPC_ALTIVEC)) {
     698        felem_square_p = p521_felem_square;
     699        felem_mul_p = p521_felem_mul;
     700
     701        return;
     702    }
     703# endif
     704
     705    /* Default */
     706    felem_square_p = felem_square_ref;
     707    felem_mul_p = felem_mul_ref;
     708}
     709
     710void felem_square_wrapper(largefelem out, const felem in)
     711{
     712    felem_select();
     713    felem_square_p(out, in);
     714}
     715
     716void felem_mul_wrapper(largefelem out, const felem in1, const felem in2)
     717{
     718    felem_select();
     719    felem_mul_p(out, in1, in2);
     720}
     721
     722# define felem_square felem_square_p
     723# define felem_mul felem_mul_p
     724#else
     725# define felem_square felem_square_ref
     726# define felem_mul felem_mul_ref
     727#endif
    676728
    677729static void felem_square_reduce(felem out, const felem in)
     
    12671319         * for ECDH / ECDSA. The case where it can happen is during scalar-mult
    12681320         * where the intermediate value gets very close to the group order.
    1269          * Since |ec_GFp_nistp_recode_scalar_bits| produces signed digits for
    1270          * the scalar, it's possible for the intermediate value to be a small
     1321         * Since |ossl_ec_GFp_nistp_recode_scalar_bits| produces signed digits
     1322         * for the scalar, it's possible for the intermediate value to be a small
    12711323         * negative multiple of the base point, and for the final signed digit
    12721324         * to be the same value. We believe that this only occurs for the scalar
     
    15851637                bits |= get_bit(scalars[num], i) << 1;
    15861638                bits |= get_bit(scalars[num], i - 1);
    1587                 ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
     1639                ossl_ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
    15881640
    15891641                /*
     
    16231675        EC_FLAGS_DEFAULT_OCT,
    16241676        NID_X9_62_prime_field,
    1625         ec_GFp_nistp521_group_init,
    1626         ec_GFp_simple_group_finish,
    1627         ec_GFp_simple_group_clear_finish,
    1628         ec_GFp_nist_group_copy,
    1629         ec_GFp_nistp521_group_set_curve,
    1630         ec_GFp_simple_group_get_curve,
    1631         ec_GFp_simple_group_get_degree,
    1632         ec_group_simple_order_bits,
    1633         ec_GFp_simple_group_check_discriminant,
    1634         ec_GFp_simple_point_init,
    1635         ec_GFp_simple_point_finish,
    1636         ec_GFp_simple_point_clear_finish,
    1637         ec_GFp_simple_point_copy,
    1638         ec_GFp_simple_point_set_to_infinity,
    1639         ec_GFp_simple_set_Jprojective_coordinates_GFp,
    1640         ec_GFp_simple_get_Jprojective_coordinates_GFp,
    1641         ec_GFp_simple_point_set_affine_coordinates,
    1642         ec_GFp_nistp521_point_get_affine_coordinates,
     1677        ossl_ec_GFp_nistp521_group_init,
     1678        ossl_ec_GFp_simple_group_finish,
     1679        ossl_ec_GFp_simple_group_clear_finish,
     1680        ossl_ec_GFp_nist_group_copy,
     1681        ossl_ec_GFp_nistp521_group_set_curve,
     1682        ossl_ec_GFp_simple_group_get_curve,
     1683        ossl_ec_GFp_simple_group_get_degree,
     1684        ossl_ec_group_simple_order_bits,
     1685        ossl_ec_GFp_simple_group_check_discriminant,
     1686        ossl_ec_GFp_simple_point_init,
     1687        ossl_ec_GFp_simple_point_finish,
     1688        ossl_ec_GFp_simple_point_clear_finish,
     1689        ossl_ec_GFp_simple_point_copy,
     1690        ossl_ec_GFp_simple_point_set_to_infinity,
     1691        ossl_ec_GFp_simple_point_set_affine_coordinates,
     1692        ossl_ec_GFp_nistp521_point_get_affine_coordinates,
    16431693        0 /* point_set_compressed_coordinates */ ,
    16441694        0 /* point2oct */ ,
    16451695        0 /* oct2point */ ,
    1646         ec_GFp_simple_add,
    1647         ec_GFp_simple_dbl,
    1648         ec_GFp_simple_invert,
    1649         ec_GFp_simple_is_at_infinity,
    1650         ec_GFp_simple_is_on_curve,
    1651         ec_GFp_simple_cmp,
    1652         ec_GFp_simple_make_affine,
    1653         ec_GFp_simple_points_make_affine,
    1654         ec_GFp_nistp521_points_mul,
    1655         ec_GFp_nistp521_precompute_mult,
    1656         ec_GFp_nistp521_have_precompute_mult,
    1657         ec_GFp_nist_field_mul,
    1658         ec_GFp_nist_field_sqr,
     1696        ossl_ec_GFp_simple_add,
     1697        ossl_ec_GFp_simple_dbl,
     1698        ossl_ec_GFp_simple_invert,
     1699        ossl_ec_GFp_simple_is_at_infinity,
     1700        ossl_ec_GFp_simple_is_on_curve,
     1701        ossl_ec_GFp_simple_cmp,
     1702        ossl_ec_GFp_simple_make_affine,
     1703        ossl_ec_GFp_simple_points_make_affine,
     1704        ossl_ec_GFp_nistp521_points_mul,
     1705        ossl_ec_GFp_nistp521_precompute_mult,
     1706        ossl_ec_GFp_nistp521_have_precompute_mult,
     1707        ossl_ec_GFp_nist_field_mul,
     1708        ossl_ec_GFp_nist_field_sqr,
    16591709        0 /* field_div */ ,
    1660         ec_GFp_simple_field_inv,
     1710        ossl_ec_GFp_simple_field_inv,
    16611711        0 /* field_encode */ ,
    16621712        0 /* field_decode */ ,
    16631713        0,                      /* field_set_to_one */
    1664         ec_key_simple_priv2oct,
    1665         ec_key_simple_oct2priv,
     1714        ossl_ec_key_simple_priv2oct,
     1715        ossl_ec_key_simple_oct2priv,
    16661716        0, /* set private */
    1667         ec_key_simple_generate_key,
    1668         ec_key_simple_check_key,
    1669         ec_key_simple_generate_public_key,
     1717        ossl_ec_key_simple_generate_key,
     1718        ossl_ec_key_simple_check_key,
     1719        ossl_ec_key_simple_generate_public_key,
    16701720        0, /* keycopy */
    16711721        0, /* keyfinish */
    1672         ecdh_simple_compute_key,
     1722        ossl_ecdh_simple_compute_key,
     1723        ossl_ecdsa_simple_sign_setup,
     1724        ossl_ecdsa_simple_sign_sig,
     1725        ossl_ecdsa_simple_verify_sig,
    16731726        0, /* field_inverse_mod_ord */
    16741727        0, /* blind_coordinates */
     
    16911744
    16921745    if (ret == NULL) {
    1693         ECerr(EC_F_NISTP521_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     1746        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    16941747        return ret;
    16951748    }
     
    16991752    ret->lock = CRYPTO_THREAD_lock_new();
    17001753    if (ret->lock == NULL) {
    1701         ECerr(EC_F_NISTP521_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     1754        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    17021755        OPENSSL_free(ret);
    17031756        return NULL;
     
    17221775
    17231776    CRYPTO_DOWN_REF(&p->references, &i, p->lock);
    1724     REF_PRINT_COUNT("EC_nistp521", x);
     1777    REF_PRINT_COUNT("EC_nistp521", p);
    17251778    if (i > 0)
    17261779        return;
     
    17361789 */
    17371790
    1738 int ec_GFp_nistp521_group_init(EC_GROUP *group)
     1791int ossl_ec_GFp_nistp521_group_init(EC_GROUP *group)
    17391792{
    17401793    int ret;
    1741     ret = ec_GFp_simple_group_init(group);
     1794    ret = ossl_ec_GFp_simple_group_init(group);
    17421795    group->a_is_minus3 = 1;
    17431796    return ret;
    17441797}
    17451798
    1746 int ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
    1747                                     const BIGNUM *a, const BIGNUM *b,
    1748                                     BN_CTX *ctx)
     1799int ossl_ec_GFp_nistp521_group_set_curve(EC_GROUP *group, const BIGNUM *p,
     1800                                         const BIGNUM *a, const BIGNUM *b,
     1801                                         BN_CTX *ctx)
    17491802{
    17501803    int ret = 0;
     1804    BIGNUM *curve_p, *curve_a, *curve_b;
     1805#ifndef FIPS_MODULE
    17511806    BN_CTX *new_ctx = NULL;
    1752     BIGNUM *curve_p, *curve_a, *curve_b;
    17531807
    17541808    if (ctx == NULL)
    1755         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
    1756             return 0;
     1809        ctx = new_ctx = BN_CTX_new();
     1810#endif
     1811    if (ctx == NULL)
     1812        return 0;
     1813
    17571814    BN_CTX_start(ctx);
    17581815    curve_p = BN_CTX_get(ctx);
     
    17651822    BN_bin2bn(nistp521_curve_params[2], sizeof(felem_bytearray), curve_b);
    17661823    if ((BN_cmp(curve_p, p)) || (BN_cmp(curve_a, a)) || (BN_cmp(curve_b, b))) {
    1767         ECerr(EC_F_EC_GFP_NISTP521_GROUP_SET_CURVE,
    1768               EC_R_WRONG_CURVE_PARAMETERS);
     1824        ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);
    17691825        goto err;
    17701826    }
    17711827    group->field_mod_func = BN_nist_mod_521;
    1772     ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
     1828    ret = ossl_ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
    17731829 err:
    17741830    BN_CTX_end(ctx);
     1831#ifndef FIPS_MODULE
    17751832    BN_CTX_free(new_ctx);
     1833#endif
    17761834    return ret;
    17771835}
     
    17811839 * (X/Z^2, Y/Z^3)
    17821840 */
    1783 int ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
    1784                                                  const EC_POINT *point,
    1785                                                  BIGNUM *x, BIGNUM *y,
    1786                                                  BN_CTX *ctx)
     1841int ossl_ec_GFp_nistp521_point_get_affine_coordinates(const EC_GROUP *group,
     1842                                                      const EC_POINT *point,
     1843                                                      BIGNUM *x, BIGNUM *y,
     1844                                                      BN_CTX *ctx)
    17871845{
    17881846    felem z1, z2, x_in, y_in, x_out, y_out;
     
    17901848
    17911849    if (EC_POINT_is_at_infinity(group, point)) {
    1792         ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
    1793               EC_R_POINT_AT_INFINITY);
     1850        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
    17941851        return 0;
    17951852    }
     
    18051862    if (x != NULL) {
    18061863        if (!felem_to_BN(x, x_out)) {
    1807             ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
    1808                   ERR_R_BN_LIB);
     1864            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    18091865            return 0;
    18101866        }
     
    18171873    if (y != NULL) {
    18181874        if (!felem_to_BN(y, y_out)) {
    1819             ECerr(EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES,
    1820                   ERR_R_BN_LIB);
     1875            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    18211876            return 0;
    18221877        }
     
    18331888     * normally shouldn't happen).
    18341889     */
    1835     ec_GFp_nistp_points_make_affine_internal(num,
    1836                                              points,
    1837                                              sizeof(felem),
    1838                                              tmp_felems,
    1839                                              (void (*)(void *))felem_one,
    1840                                              felem_is_zero_int,
    1841                                              (void (*)(void *, const void *))
    1842                                              felem_assign,
    1843                                              (void (*)(void *, const void *))
    1844                                              felem_square_reduce, (void (*)
    1845                                                                    (void *,
    1846                                                                     const void
    1847                                                                     *,
    1848                                                                     const void
    1849                                                                     *))
    1850                                              felem_mul_reduce,
    1851                                              (void (*)(void *, const void *))
    1852                                              felem_inv,
    1853                                              (void (*)(void *, const void *))
    1854                                              felem_contract);
     1890    ossl_ec_GFp_nistp_points_make_affine_internal(num,
     1891                                                  points,
     1892                                                  sizeof(felem),
     1893                                                  tmp_felems,
     1894                                                  (void (*)(void *))felem_one,
     1895                                                  felem_is_zero_int,
     1896                                                  (void (*)(void *, const void *))
     1897                                                  felem_assign,
     1898                                                  (void (*)(void *, const void *))
     1899                                                  felem_square_reduce, (void (*)
     1900                                                                        (void *,
     1901                                                                         const void
     1902                                                                         *,
     1903                                                                         const void
     1904                                                                         *))
     1905                                                  felem_mul_reduce,
     1906                                                  (void (*)(void *, const void *))
     1907                                                  felem_inv,
     1908                                                  (void (*)(void *, const void *))
     1909                                                  felem_contract);
    18551910}
    18561911
     
    18591914 * values Result is stored in r (r can equal one of the inputs).
    18601915 */
    1861 int ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
    1862                                const BIGNUM *scalar, size_t num,
    1863                                const EC_POINT *points[],
    1864                                const BIGNUM *scalars[], BN_CTX *ctx)
     1916int ossl_ec_GFp_nistp521_points_mul(const EC_GROUP *group, EC_POINT *r,
     1917                                    const BIGNUM *scalar, size_t num,
     1918                                    const EC_POINT *points[],
     1919                                    const BIGNUM *scalars[], BN_CTX *ctx)
    18651920{
    18661921    int ret = 0;
     
    19061961            !felem_to_BN(y, g_pre_comp[1][1]) ||
    19071962            !felem_to_BN(z, g_pre_comp[1][2])) {
    1908             ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
     1963            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    19091964            goto err;
    19101965        }
    1911         if (!EC_POINT_set_Jprojective_coordinates_GFp(group,
    1912                                                       generator, x, y, z,
    1913                                                       ctx))
     1966        if (!ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group,
     1967                                                                generator,
     1968                                                                x, y, z, ctx))
    19141969            goto err;
    19151970        if (0 == EC_POINT_cmp(group, generator, group->generator, ctx))
     
    19391994        if ((secrets == NULL) || (pre_comp == NULL)
    19401995            || (mixed && (tmp_felems == NULL))) {
    1941             ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_MALLOC_FAILURE);
     1996            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    19421997            goto err;
    19431998        }
     
    19692024                     */
    19702025                    if (!BN_nnmod(tmp_scalar, p_scalar, group->order, ctx)) {
    1971                         ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
     2026                        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    19722027                        goto err;
    19732028                    }
     
    19792034                }
    19802035                if (num_bytes < 0) {
    1981                     ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
     2036                    ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    19822037                    goto err;
    19832038                }
     
    20212076             */
    20222077            if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
    2023                 ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
     2078                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    20242079                goto err;
    20252080            }
     
    20462101    if ((!felem_to_BN(x, x_in)) || (!felem_to_BN(y, y_in)) ||
    20472102        (!felem_to_BN(z, z_in))) {
    2048         ECerr(EC_F_EC_GFP_NISTP521_POINTS_MUL, ERR_R_BN_LIB);
     2103        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    20492104        goto err;
    20502105    }
    2051     ret = EC_POINT_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
     2106    ret = ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, r, x, y, z,
     2107                                                             ctx);
    20522108
    20532109 err:
     
    20602116}
    20612117
    2062 int ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
     2118int ossl_ec_GFp_nistp521_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
    20632119{
    20642120    int ret = 0;
    20652121    NISTP521_PRE_COMP *pre = NULL;
    20662122    int i, j;
    2067     BN_CTX *new_ctx = NULL;
    20682123    BIGNUM *x, *y;
    20692124    EC_POINT *generator = NULL;
    20702125    felem tmp_felems[16];
     2126#ifndef FIPS_MODULE
     2127    BN_CTX *new_ctx = NULL;
     2128#endif
    20712129
    20722130    /* throw away old precomputation */
    20732131    EC_pre_comp_free(group);
     2132
     2133#ifndef FIPS_MODULE
    20742134    if (ctx == NULL)
    2075         if ((ctx = new_ctx = BN_CTX_new()) == NULL)
    2076             return 0;
     2135        ctx = new_ctx = BN_CTX_new();
     2136#endif
     2137    if (ctx == NULL)
     2138        return 0;
     2139
    20772140    BN_CTX_start(ctx);
    20782141    x = BN_CTX_get(ctx);
     
    21622225    BN_CTX_end(ctx);
    21632226    EC_POINT_free(generator);
     2227#ifndef FIPS_MODULE
    21642228    BN_CTX_free(new_ctx);
     2229#endif
    21652230    EC_nistp521_pre_comp_free(pre);
    21662231    return ret;
    21672232}
    21682233
    2169 int ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group)
     2234int ossl_ec_GFp_nistp521_have_precompute_mult(const EC_GROUP *group)
    21702235{
    21712236    return HAVEPRECOMP(group, nistp521);
    21722237}
    2173 
    2174 #endif
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_nistputil.c

    r91772 r94082  
    11/*
    2  * Copyright 2011-2019 The OpenSSL Project Authors. All Rights Reserved.
    3  *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     2 * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
     3 *
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    2424 */
    2525
     26/*
     27 * ECDSA low level APIs are deprecated for public use, but still ok for
     28 * internal use.
     29 */
     30#include "internal/deprecated.h"
     31
    2632#include <openssl/opensslconf.h>
    27 #ifdef OPENSSL_NO_EC_NISTP_64_GCC_128
    28 NON_EMPTY_TRANSLATION_UNIT
    29 #else
    3033
    3134/*
     
    3336 */
    3437
    35 # include <stddef.h>
    36 # include "ec_local.h"
     38#include <stddef.h>
     39#include "ec_local.h"
    3740
    3841/*
     
    4750 * 'num'+1 field elements for storage of intermediate values.
    4851 */
    49 void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
     52void
     53ossl_ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
    5054                                              size_t felem_size,
    5155                                              void *tmp_felems,
     
    7579    int i = 0;
    7680
    77 # define tmp_felem(I) (&((char *)tmp_felems)[(I) * felem_size])
    78 # define X(I) (&((char *)point_array)[3*(I) * felem_size])
    79 # define Y(I) (&((char *)point_array)[(3*(I) + 1) * felem_size])
    80 # define Z(I) (&((char *)point_array)[(3*(I) + 2) * felem_size])
     81#define tmp_felem(I) (&((char *)tmp_felems)[(I) * felem_size])
     82#define X(I) (&((char *)point_array)[3*(I) * felem_size])
     83#define Y(I) (&((char *)point_array)[(3*(I) + 1) * felem_size])
     84#define Z(I) (&((char *)point_array)[(3*(I) + 2) * felem_size])
    8185
    8286    if (!felem_is_zero(Z(0)))
     
    207211 *
    208212 */
    209 void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
    210                                      unsigned char *digit, unsigned char in)
     213void ossl_ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
     214                                          unsigned char *digit, unsigned char in)
    211215{
    212216    unsigned char s, d;
     
    221225    *digit = d;
    222226}
    223 #endif
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_nistz256.c

    r91772 r94082  
    11/*
    2  * Copyright 2014-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2014, Intel Corporation. All Rights Reserved.
    44 * Copyright (c) 2015, CloudFlare, Inc.
    55 *
    6  * Licensed under the OpenSSL license (the "License").  You may not use
     6 * Licensed under the Apache License 2.0 (the "License").  You may not use
    77 * this file except in compliance with the License.  You can obtain a copy
    88 * in the file LICENSE in the source distribution or at
     
    1818 *                          256 Bit Primes"
    1919 */
     20
     21/*
     22 * ECDSA low level APIs are deprecated for public use, but still ok for
     23 * internal use.
     24 */
     25#include "internal/deprecated.h"
    2026
    2127#include <string.h>
     
    632638            OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL
    633639        || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
    634         ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_MALLOC_FAILURE);
     640        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    635641        goto err;
    636642    }
     
    649655                goto err;
    650656            if (!BN_nnmod(mod, scalar[i], group->order, ctx)) {
    651                 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);
     657                ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    652658                goto err;
    653659            }
     
    677683            || !ecp_nistz256_bignum_to_field_elem(temp[0].Y, point[i]->Y)
    678684            || !ecp_nistz256_bignum_to_field_elem(temp[0].Z, point[i]->Z)) {
    679             ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL,
    680                   EC_R_COORDINATES_OUT_OF_RANGE);
     685            ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
    681686            goto err;
    682687        }
     
    829834    generator = EC_GROUP_get0_generator(group);
    830835    if (generator == NULL) {
    831         ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNDEFINED_GENERATOR);
     836        ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
    832837        return 0;
    833838    }
     
    845850
    846851    if (ctx == NULL) {
    847         ctx = new_ctx = BN_CTX_new();
     852        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    848853        if (ctx == NULL)
    849854            goto err;
     
    857862
    858863    if (BN_is_zero(order)) {
    859         ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNKNOWN_ORDER);
     864        ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_ORDER);
    860865        goto err;
    861866    }
     
    865870    if ((precomp_storage =
    866871         OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) {
    867         ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, ERR_R_MALLOC_FAILURE);
     872        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    868873        goto err;
    869874    }
     
    892897             * make multiple points affine at the same time.
    893898             */
    894             if (!EC_POINT_make_affine(group, P, ctx))
     899            if (group->meth->make_affine == NULL
     900                || !group->meth->make_affine(group, P, ctx))
    895901                goto err;
    896902            if (!ecp_nistz256_bignum_to_field_elem(temp.X, P->X) ||
    897903                !ecp_nistz256_bignum_to_field_elem(temp.Y, P->Y)) {
    898                 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE,
    899                       EC_R_COORDINATES_OUT_OF_RANGE);
     904                ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
    900905                goto err;
    901906            }
     
    970975
    971976    if ((num + 1) == 0 || (num + 1) > OPENSSL_MALLOC_MAX_NELEMS(void *)) {
    972         ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
     977        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    973978        return 0;
    974979    }
     
    979984        generator = EC_GROUP_get0_generator(group);
    980985        if (generator == NULL) {
    981             ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
     986            ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR);
    982987            goto err;
    983988        }
     
    10271032
    10281033                if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
    1029                     ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);
     1034                    ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    10301035                    goto err;
    10311036                }
     
    11231128        new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));
    11241129        if (new_scalars == NULL) {
    1125             ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
     1130            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    11261131            goto err;
    11271132        }
     
    11291134        new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));
    11301135        if (new_points == NULL) {
    1131             ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
     1136            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    11321137            goto err;
    11331138        }
     
    11841189
    11851190    if (EC_POINT_is_at_infinity(group, point)) {
    1186         ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_POINT_AT_INFINITY);
     1191        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
    11871192        return 0;
    11881193    }
     
    11911196        !ecp_nistz256_bignum_to_field_elem(point_y, point->Y) ||
    11921197        !ecp_nistz256_bignum_to_field_elem(point_z, point->Z)) {
    1193         ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_COORDINATES_OUT_OF_RANGE);
     1198        ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
    11941199        return 0;
    11951200    }
     
    12261231
    12271232    if (ret == NULL) {
    1228         ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     1233        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    12291234        return ret;
    12301235    }
     
    12361241    ret->lock = CRYPTO_THREAD_lock_new();
    12371242    if (ret->lock == NULL) {
    1238         ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
     1243        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    12391244        OPENSSL_free(ret);
    12401245        return NULL;
     
    12591264
    12601265    CRYPTO_DOWN_REF(&pre->references, &i, pre->lock);
    1261     REF_PRINT_COUNT("EC_nistz256", x);
     1266    REF_PRINT_COUNT("EC_nistz256", pre);
    12621267    if (i > 0)
    12631268        return;
     
    12951300void ecp_nistz256_ord_sqr_mont(BN_ULONG res[P256_LIMBS],
    12961301                               const BN_ULONG a[P256_LIMBS],
    1297                                int rep);
     1302                               BN_ULONG rep);
    12981303
    12991304static int ecp_nistz256_inv_mod_ord(const EC_GROUP *group, BIGNUM *r,
     
    13251330     */
    13261331    if (bn_wexpand(r, P256_LIMBS) == NULL) {
    1327         ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, ERR_R_BN_LIB);
     1332        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    13281333        goto err;
    13291334    }
     
    13341339        if ((tmp = BN_CTX_get(ctx)) == NULL
    13351340            || !BN_nnmod(tmp, x, group->order, ctx)) {
    1336             ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, ERR_R_BN_LIB);
     1341            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    13371342            goto err;
    13381343        }
     
    13411346
    13421347    if (!ecp_nistz256_bignum_to_field_elem(t, x)) {
    1343         ECerr(EC_F_ECP_NISTZ256_INV_MOD_ORD, EC_R_COORDINATES_OUT_OF_RANGE);
     1348        ERR_raise(ERR_LIB_EC, EC_R_COORDINATES_OUT_OF_RANGE);
    13441349        goto err;
    13451350    }
     
    14711476        EC_FLAGS_DEFAULT_OCT,
    14721477        NID_X9_62_prime_field,
    1473         ec_GFp_mont_group_init,
    1474         ec_GFp_mont_group_finish,
    1475         ec_GFp_mont_group_clear_finish,
    1476         ec_GFp_mont_group_copy,
    1477         ec_GFp_mont_group_set_curve,
    1478         ec_GFp_simple_group_get_curve,
    1479         ec_GFp_simple_group_get_degree,
    1480         ec_group_simple_order_bits,
    1481         ec_GFp_simple_group_check_discriminant,
    1482         ec_GFp_simple_point_init,
    1483         ec_GFp_simple_point_finish,
    1484         ec_GFp_simple_point_clear_finish,
    1485         ec_GFp_simple_point_copy,
    1486         ec_GFp_simple_point_set_to_infinity,
    1487         ec_GFp_simple_set_Jprojective_coordinates_GFp,
    1488         ec_GFp_simple_get_Jprojective_coordinates_GFp,
    1489         ec_GFp_simple_point_set_affine_coordinates,
     1478        ossl_ec_GFp_mont_group_init,
     1479        ossl_ec_GFp_mont_group_finish,
     1480        ossl_ec_GFp_mont_group_clear_finish,
     1481        ossl_ec_GFp_mont_group_copy,
     1482        ossl_ec_GFp_mont_group_set_curve,
     1483        ossl_ec_GFp_simple_group_get_curve,
     1484        ossl_ec_GFp_simple_group_get_degree,
     1485        ossl_ec_group_simple_order_bits,
     1486        ossl_ec_GFp_simple_group_check_discriminant,
     1487        ossl_ec_GFp_simple_point_init,
     1488        ossl_ec_GFp_simple_point_finish,
     1489        ossl_ec_GFp_simple_point_clear_finish,
     1490        ossl_ec_GFp_simple_point_copy,
     1491        ossl_ec_GFp_simple_point_set_to_infinity,
     1492        ossl_ec_GFp_simple_point_set_affine_coordinates,
    14901493        ecp_nistz256_get_affine,
    14911494        0, 0, 0,
    1492         ec_GFp_simple_add,
    1493         ec_GFp_simple_dbl,
    1494         ec_GFp_simple_invert,
    1495         ec_GFp_simple_is_at_infinity,
    1496         ec_GFp_simple_is_on_curve,
    1497         ec_GFp_simple_cmp,
    1498         ec_GFp_simple_make_affine,
    1499         ec_GFp_simple_points_make_affine,
     1495        ossl_ec_GFp_simple_add,
     1496        ossl_ec_GFp_simple_dbl,
     1497        ossl_ec_GFp_simple_invert,
     1498        ossl_ec_GFp_simple_is_at_infinity,
     1499        ossl_ec_GFp_simple_is_on_curve,
     1500        ossl_ec_GFp_simple_cmp,
     1501        ossl_ec_GFp_simple_make_affine,
     1502        ossl_ec_GFp_simple_points_make_affine,
    15001503        ecp_nistz256_points_mul,                    /* mul */
    15011504        ecp_nistz256_mult_precompute,               /* precompute_mult */
    15021505        ecp_nistz256_window_have_precompute_mult,   /* have_precompute_mult */
    1503         ec_GFp_mont_field_mul,
    1504         ec_GFp_mont_field_sqr,
     1506        ossl_ec_GFp_mont_field_mul,
     1507        ossl_ec_GFp_mont_field_sqr,
    15051508        0,                                          /* field_div */
    1506         ec_GFp_mont_field_inv,
    1507         ec_GFp_mont_field_encode,
    1508         ec_GFp_mont_field_decode,
    1509         ec_GFp_mont_field_set_to_one,
    1510         ec_key_simple_priv2oct,
    1511         ec_key_simple_oct2priv,
     1509        ossl_ec_GFp_mont_field_inv,
     1510        ossl_ec_GFp_mont_field_encode,
     1511        ossl_ec_GFp_mont_field_decode,
     1512        ossl_ec_GFp_mont_field_set_to_one,
     1513        ossl_ec_key_simple_priv2oct,
     1514        ossl_ec_key_simple_oct2priv,
    15121515        0, /* set private */
    1513         ec_key_simple_generate_key,
    1514         ec_key_simple_check_key,
    1515         ec_key_simple_generate_public_key,
     1516        ossl_ec_key_simple_generate_key,
     1517        ossl_ec_key_simple_check_key,
     1518        ossl_ec_key_simple_generate_public_key,
    15161519        0, /* keycopy */
    15171520        0, /* keyfinish */
    1518         ecdh_simple_compute_key,
     1521        ossl_ecdh_simple_compute_key,
     1522        ossl_ecdsa_simple_sign_setup,
     1523        ossl_ecdsa_simple_sign_sig,
     1524        ossl_ecdsa_simple_verify_sig,
    15191525        ecp_nistz256_inv_mod_ord,                   /* can be #define-d NULL */
    15201526        0,                                          /* blind_coordinates */
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_nistz256_table.c

    r91772 r94082  
    22 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_oct.c

    r91772 r94082  
    11/*
    2  * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    99 */
    1010
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
     16
    1117#include <openssl/err.h>
    1218#include <openssl/symhacks.h>
     
    1420#include "ec_local.h"
    1521
    16 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
    17                                              EC_POINT *point,
    18                                              const BIGNUM *x_, int y_bit,
    19                                              BN_CTX *ctx)
     22int ossl_ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
     23                                                  EC_POINT *point,
     24                                                  const BIGNUM *x_, int y_bit,
     25                                                  BN_CTX *ctx)
    2026{
    2127    BN_CTX *new_ctx = NULL;
     
    2329    int ret = 0;
    2430
    25     /* clear error queue */
    26     ERR_clear_error();
    27 
    2831    if (ctx == NULL) {
    29         ctx = new_ctx = BN_CTX_new();
     32        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    3033        if (ctx == NULL)
    3134            return 0;
     
    99102    }
    100103
     104    ERR_set_mark();
    101105    if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
     106#ifndef FIPS_MODULE
    102107        unsigned long err = ERR_peek_last_error();
    103108
    104109        if (ERR_GET_LIB(err) == ERR_LIB_BN
    105110            && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
    106             ERR_clear_error();
    107             ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
    108                   EC_R_INVALID_COMPRESSED_POINT);
     111            ERR_pop_to_mark();
     112            ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
    109113        } else
    110             ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
    111                   ERR_R_BN_LIB);
    112         goto err;
    113     }
     114#endif
     115        {
     116            ERR_clear_last_mark();
     117            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
     118        }
     119        goto err;
     120    }
     121    ERR_clear_last_mark();
    114122
    115123    if (y_bit != BN_is_odd(y)) {
     
    122130
    123131            if (kron == 1)
    124                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
    125                       EC_R_INVALID_COMPRESSION_BIT);
     132                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSION_BIT);
    126133            else
    127134                /*
    128135                 * BN_mod_sqrt() should have caught this error (not a square)
    129136                 */
    130                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
    131                       EC_R_INVALID_COMPRESSED_POINT);
     137                ERR_raise(ERR_LIB_EC, EC_R_INVALID_COMPRESSED_POINT);
    132138            goto err;
    133139        }
     
    136142    }
    137143    if (y_bit != BN_is_odd(y)) {
    138         ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
    139               ERR_R_INTERNAL_ERROR);
     144        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    140145        goto err;
    141146    }
     
    152157}
    153158
    154 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
    155                                point_conversion_form_t form,
    156                                unsigned char *buf, size_t len, BN_CTX *ctx)
     159size_t ossl_ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
     160                                    point_conversion_form_t form,
     161                                    unsigned char *buf, size_t len, BN_CTX *ctx)
    157162{
    158163    size_t ret;
     
    165170        && (form != POINT_CONVERSION_UNCOMPRESSED)
    166171        && (form != POINT_CONVERSION_HYBRID)) {
    167         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
     172        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);
    168173        goto err;
    169174    }
     
    173178        if (buf != NULL) {
    174179            if (len < 1) {
    175                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
     180                ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
    176181                return 0;
    177182            }
     
    190195    if (buf != NULL) {
    191196        if (len < ret) {
    192             ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
     197            ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
    193198            goto err;
    194199        }
    195200
    196201        if (ctx == NULL) {
    197             ctx = new_ctx = BN_CTX_new();
     202            ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    198203            if (ctx == NULL)
    199204                return 0;
     
    220225        skip = field_len - BN_num_bytes(x);
    221226        if (skip > field_len) {
    222             ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
     227            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    223228            goto err;
    224229        }
     
    230235        i += skip;
    231236        if (i != 1 + field_len) {
    232             ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
     237            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    233238            goto err;
    234239        }
     
    238243            skip = field_len - BN_num_bytes(y);
    239244            if (skip > field_len) {
    240                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
     245                ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    241246                goto err;
    242247            }
     
    250255
    251256        if (i != ret) {
    252             ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
     257            ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    253258            goto err;
    254259        }
     
    267272}
    268273
    269 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
    270                             const unsigned char *buf, size_t len, BN_CTX *ctx)
     274int ossl_ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
     275                                 const unsigned char *buf, size_t len,
     276                                 BN_CTX *ctx)
    271277{
    272278    point_conversion_form_t form;
     
    278284
    279285    if (len == 0) {
    280         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
     286        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
    281287        return 0;
    282288    }
     
    287293        && (form != POINT_CONVERSION_UNCOMPRESSED)
    288294        && (form != POINT_CONVERSION_HYBRID)) {
    289         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     295        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    290296        return 0;
    291297    }
    292298    if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
    293         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     299        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    294300        return 0;
    295301    }
     
    297303    if (form == 0) {
    298304        if (len != 1) {
    299             ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     305            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    300306            return 0;
    301307        }
     
    310316
    311317    if (len != enc_len) {
    312         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     318        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    313319        return 0;
    314320    }
    315321
    316322    if (ctx == NULL) {
    317         ctx = new_ctx = BN_CTX_new();
     323        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    318324        if (ctx == NULL)
    319325            return 0;
     
    329335        goto err;
    330336    if (BN_ucmp(x, group->field) >= 0) {
    331         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     337        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    332338        goto err;
    333339    }
     
    340346            goto err;
    341347        if (BN_ucmp(y, group->field) >= 0) {
    342             ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     348            ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    343349            goto err;
    344350        }
    345351        if (form == POINT_CONVERSION_HYBRID) {
    346352            if (y_bit != BN_is_odd(y)) {
    347                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
     353                ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    348354                goto err;
    349355            }
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecp_smpl.c

    r91772 r94082  
    11/*
    2  * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
    44 *
    5  * Licensed under the OpenSSL license (the "License").  You may not use
     5 * Licensed under the Apache License 2.0 (the "License").  You may not use
    66 * this file except in compliance with the License.  You can obtain a copy
    77 * in the file LICENSE in the source distribution or at
     
    99 */
    1010
     11/*
     12 * ECDSA low level APIs are deprecated for public use, but still ok for
     13 * internal use.
     14 */
     15#include "internal/deprecated.h"
     16
    1117#include <openssl/err.h>
    1218#include <openssl/symhacks.h>
     
    1925        EC_FLAGS_DEFAULT_OCT,
    2026        NID_X9_62_prime_field,
    21         ec_GFp_simple_group_init,
    22         ec_GFp_simple_group_finish,
    23         ec_GFp_simple_group_clear_finish,
    24         ec_GFp_simple_group_copy,
    25         ec_GFp_simple_group_set_curve,
    26         ec_GFp_simple_group_get_curve,
    27         ec_GFp_simple_group_get_degree,
    28         ec_group_simple_order_bits,
    29         ec_GFp_simple_group_check_discriminant,
    30         ec_GFp_simple_point_init,
    31         ec_GFp_simple_point_finish,
    32         ec_GFp_simple_point_clear_finish,
    33         ec_GFp_simple_point_copy,
    34         ec_GFp_simple_point_set_to_infinity,
    35         ec_GFp_simple_set_Jprojective_coordinates_GFp,
    36         ec_GFp_simple_get_Jprojective_coordinates_GFp,
    37         ec_GFp_simple_point_set_affine_coordinates,
    38         ec_GFp_simple_point_get_affine_coordinates,
     27        ossl_ec_GFp_simple_group_init,
     28        ossl_ec_GFp_simple_group_finish,
     29        ossl_ec_GFp_simple_group_clear_finish,
     30        ossl_ec_GFp_simple_group_copy,
     31        ossl_ec_GFp_simple_group_set_curve,
     32        ossl_ec_GFp_simple_group_get_curve,
     33        ossl_ec_GFp_simple_group_get_degree,
     34        ossl_ec_group_simple_order_bits,
     35        ossl_ec_GFp_simple_group_check_discriminant,
     36        ossl_ec_GFp_simple_point_init,
     37        ossl_ec_GFp_simple_point_finish,
     38        ossl_ec_GFp_simple_point_clear_finish,
     39        ossl_ec_GFp_simple_point_copy,
     40        ossl_ec_GFp_simple_point_set_to_infinity,
     41        ossl_ec_GFp_simple_point_set_affine_coordinates,
     42        ossl_ec_GFp_simple_point_get_affine_coordinates,
    3943        0, 0, 0,
    40         ec_GFp_simple_add,
    41         ec_GFp_simple_dbl,
    42         ec_GFp_simple_invert,
    43         ec_GFp_simple_is_at_infinity,
    44         ec_GFp_simple_is_on_curve,
    45         ec_GFp_simple_cmp,
    46         ec_GFp_simple_make_affine,
    47         ec_GFp_simple_points_make_affine,
     44        ossl_ec_GFp_simple_add,
     45        ossl_ec_GFp_simple_dbl,
     46        ossl_ec_GFp_simple_invert,
     47        ossl_ec_GFp_simple_is_at_infinity,
     48        ossl_ec_GFp_simple_is_on_curve,
     49        ossl_ec_GFp_simple_cmp,
     50        ossl_ec_GFp_simple_make_affine,
     51        ossl_ec_GFp_simple_points_make_affine,
    4852        0 /* mul */ ,
    4953        0 /* precompute_mult */ ,
    5054        0 /* have_precompute_mult */ ,
    51         ec_GFp_simple_field_mul,
    52         ec_GFp_simple_field_sqr,
     55        ossl_ec_GFp_simple_field_mul,
     56        ossl_ec_GFp_simple_field_sqr,
    5357        0 /* field_div */ ,
    54         ec_GFp_simple_field_inv,
     58        ossl_ec_GFp_simple_field_inv,
    5559        0 /* field_encode */ ,
    5660        0 /* field_decode */ ,
    5761        0,                      /* field_set_to_one */
    58         ec_key_simple_priv2oct,
    59         ec_key_simple_oct2priv,
     62        ossl_ec_key_simple_priv2oct,
     63        ossl_ec_key_simple_oct2priv,
    6064        0, /* set private */
    61         ec_key_simple_generate_key,
    62         ec_key_simple_check_key,
    63         ec_key_simple_generate_public_key,
     65        ossl_ec_key_simple_generate_key,
     66        ossl_ec_key_simple_check_key,
     67        ossl_ec_key_simple_generate_public_key,
    6468        0, /* keycopy */
    6569        0, /* keyfinish */
    66         ecdh_simple_compute_key,
     70        ossl_ecdh_simple_compute_key,
     71        ossl_ecdsa_simple_sign_setup,
     72        ossl_ecdsa_simple_sign_sig,
     73        ossl_ecdsa_simple_verify_sig,
    6774        0, /* field_inverse_mod_ord */
    68         ec_GFp_simple_blind_coordinates,
    69         ec_GFp_simple_ladder_pre,
    70         ec_GFp_simple_ladder_step,
    71         ec_GFp_simple_ladder_post
     75        ossl_ec_GFp_simple_blind_coordinates,
     76        ossl_ec_GFp_simple_ladder_pre,
     77        ossl_ec_GFp_simple_ladder_step,
     78        ossl_ec_GFp_simple_ladder_post
    7279    };
    7380
     
    8996 */
    9097
    91 int ec_GFp_simple_group_init(EC_GROUP *group)
     98int ossl_ec_GFp_simple_group_init(EC_GROUP *group)
    9299{
    93100    group->field = BN_new();
     
    104111}
    105112
    106 void ec_GFp_simple_group_finish(EC_GROUP *group)
     113void ossl_ec_GFp_simple_group_finish(EC_GROUP *group)
    107114{
    108115    BN_free(group->field);
     
    111118}
    112119
    113 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
     120void ossl_ec_GFp_simple_group_clear_finish(EC_GROUP *group)
    114121{
    115122    BN_clear_free(group->field);
     
    118125}
    119126
    120 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
     127int ossl_ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
    121128{
    122129    if (!BN_copy(dest->field, src->field))
     
    132139}
    133140
    134 int ec_GFp_simple_group_set_curve(EC_GROUP *group,
    135                                   const BIGNUM *p, const BIGNUM *a,
    136                                   const BIGNUM *b, BN_CTX *ctx)
     141int ossl_ec_GFp_simple_group_set_curve(EC_GROUP *group,
     142                                       const BIGNUM *p, const BIGNUM *a,
     143                                       const BIGNUM *b, BN_CTX *ctx)
    137144{
    138145    int ret = 0;
     
    142149    /* p must be a prime > 3 */
    143150    if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
    144         ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
     151        ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);
    145152        return 0;
    146153    }
    147154
    148155    if (ctx == NULL) {
    149         ctx = new_ctx = BN_CTX_new();
     156        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    150157        if (ctx == NULL)
    151158            return 0;
     
    191198}
    192199
    193 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
    194                                   BIGNUM *b, BN_CTX *ctx)
     200int ossl_ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p,
     201                                       BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
    195202{
    196203    int ret = 0;
     
    205212        if (group->meth->field_decode) {
    206213            if (ctx == NULL) {
    207                 ctx = new_ctx = BN_CTX_new();
     214                ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    208215                if (ctx == NULL)
    209216                    return 0;
     
    236243}
    237244
    238 int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
     245int ossl_ec_GFp_simple_group_get_degree(const EC_GROUP *group)
    239246{
    240247    return BN_num_bits(group->field);
    241248}
    242249
    243 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
     250int ossl_ec_GFp_simple_group_check_discriminant(const EC_GROUP *group,
     251                                                BN_CTX *ctx)
    244252{
    245253    int ret = 0;
     
    249257
    250258    if (ctx == NULL) {
    251         ctx = new_ctx = BN_CTX_new();
     259        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    252260        if (ctx == NULL) {
    253             ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT,
    254                   ERR_R_MALLOC_FAILURE);
     261            ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    255262            goto err;
    256263        }
     
    313320}
    314321
    315 int ec_GFp_simple_point_init(EC_POINT *point)
     322int ossl_ec_GFp_simple_point_init(EC_POINT *point)
    316323{
    317324    point->X = BN_new();
     
    329336}
    330337
    331 void ec_GFp_simple_point_finish(EC_POINT *point)
     338void ossl_ec_GFp_simple_point_finish(EC_POINT *point)
    332339{
    333340    BN_free(point->X);
     
    336343}
    337344
    338 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
     345void ossl_ec_GFp_simple_point_clear_finish(EC_POINT *point)
    339346{
    340347    BN_clear_free(point->X);
     
    344351}
    345352
    346 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
     353int ossl_ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
    347354{
    348355    if (!BN_copy(dest->X, src->X))
     
    358365}
    359366
    360 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
    361                                         EC_POINT *point)
     367int ossl_ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
     368                                             EC_POINT *point)
    362369{
    363370    point->Z_is_one = 0;
     
    366373}
    367374
    368 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
    369                                                   EC_POINT *point,
    370                                                   const BIGNUM *x,
    371                                                   const BIGNUM *y,
    372                                                   const BIGNUM *z,
    373                                                   BN_CTX *ctx)
     375int ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
     376                                                       EC_POINT *point,
     377                                                       const BIGNUM *x,
     378                                                       const BIGNUM *y,
     379                                                       const BIGNUM *z,
     380                                                       BN_CTX *ctx)
    374381{
    375382    BN_CTX *new_ctx = NULL;
     
    377384
    378385    if (ctx == NULL) {
    379         ctx = new_ctx = BN_CTX_new();
     386        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    380387        if (ctx == NULL)
    381388            return 0;
     
    426433}
    427434
    428 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
    429                                                   const EC_POINT *point,
    430                                                   BIGNUM *x, BIGNUM *y,
    431                                                   BIGNUM *z, BN_CTX *ctx)
     435int ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
     436                                                       const EC_POINT *point,
     437                                                       BIGNUM *x, BIGNUM *y,
     438                                                       BIGNUM *z, BN_CTX *ctx)
    432439{
    433440    BN_CTX *new_ctx = NULL;
     
    436443    if (group->meth->field_decode != 0) {
    437444        if (ctx == NULL) {
    438             ctx = new_ctx = BN_CTX_new();
     445            ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    439446            if (ctx == NULL)
    440447                return 0;
     
    475482}
    476483
    477 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
    478                                                EC_POINT *point,
    479                                                const BIGNUM *x,
    480                                                const BIGNUM *y, BN_CTX *ctx)
     484int ossl_ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
     485                                                    EC_POINT *point,
     486                                                    const BIGNUM *x,
     487                                                    const BIGNUM *y, BN_CTX *ctx)
    481488{
    482489    if (x == NULL || y == NULL) {
     
    484491         * unlike for projective coordinates, we do not tolerate this
    485492         */
    486         ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES,
    487               ERR_R_PASSED_NULL_PARAMETER);
     493        ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
    488494        return 0;
    489495    }
     
    493499}
    494500
    495 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
    496                                                const EC_POINT *point,
    497                                                BIGNUM *x, BIGNUM *y,
    498                                                BN_CTX *ctx)
     501int ossl_ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group,
     502                                                    const EC_POINT *point,
     503                                                    BIGNUM *x, BIGNUM *y,
     504                                                    BN_CTX *ctx)
    499505{
    500506    BN_CTX *new_ctx = NULL;
     
    504510
    505511    if (EC_POINT_is_at_infinity(group, point)) {
    506         ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
    507               EC_R_POINT_AT_INFINITY);
     512        ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);
    508513        return 0;
    509514    }
    510515
    511516    if (ctx == NULL) {
    512         ctx = new_ctx = BN_CTX_new();
     517        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    513518        if (ctx == NULL)
    514519            return 0;
     
    555560    } else {
    556561        if (!group->meth->field_inv(group, Z_1, Z_, ctx)) {
    557             ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES,
    558                   ERR_R_BN_LIB);
     562            ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    559563            goto err;
    560564        }
     
    607611}
    608612
    609 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
    610                       const EC_POINT *b, BN_CTX *ctx)
     613int ossl_ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
     614                           const EC_POINT *b, BN_CTX *ctx)
    611615{
    612616    int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
     
    630634
    631635    if (ctx == NULL) {
    632         ctx = new_ctx = BN_CTX_new();
     636        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    633637        if (ctx == NULL)
    634638            return 0;
     
    792796}
    793797
    794 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
    795                       BN_CTX *ctx)
     798int ossl_ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
     799                           BN_CTX *ctx)
    796800{
    797801    int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
     
    814818
    815819    if (ctx == NULL) {
    816         ctx = new_ctx = BN_CTX_new();
     820        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    817821        if (ctx == NULL)
    818822            return 0;
     
    934938}
    935939
    936 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
     940int ossl_ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point,
     941                              BN_CTX *ctx)
    937942{
    938943    if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y))
     
    943948}
    944949
    945 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
     950int ossl_ec_GFp_simple_is_at_infinity(const EC_GROUP *group,
     951                                      const EC_POINT *point)
    946952{
    947953    return BN_is_zero(point->Z);
    948954}
    949955
    950 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
    951                               BN_CTX *ctx)
     956int ossl_ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
     957                                   BN_CTX *ctx)
    952958{
    953959    int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *,
     
    967973
    968974    if (ctx == NULL) {
    969         ctx = new_ctx = BN_CTX_new();
     975        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    970976        if (ctx == NULL)
    971977            return -1;
     
    10511057}
    10521058
    1053 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
    1054                       const EC_POINT *b, BN_CTX *ctx)
     1059int ossl_ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a,
     1060                           const EC_POINT *b, BN_CTX *ctx)
    10551061{
    10561062    /*-
     
    10841090
    10851091    if (ctx == NULL) {
    1086         ctx = new_ctx = BN_CTX_new();
     1092        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    10871093        if (ctx == NULL)
    10881094            return -1;
     
    11591165}
    11601166
    1161 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
    1162                               BN_CTX *ctx)
     1167int ossl_ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point,
     1168                                   BN_CTX *ctx)
    11631169{
    11641170    BN_CTX *new_ctx = NULL;
     
    11701176
    11711177    if (ctx == NULL) {
    1172         ctx = new_ctx = BN_CTX_new();
     1178        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    11731179        if (ctx == NULL)
    11741180            return 0;
     
    11861192        goto err;
    11871193    if (!point->Z_is_one) {
    1188         ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
     1194        ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);
    11891195        goto err;
    11901196    }
     
    11981204}
    11991205
    1200 int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
    1201                                      EC_POINT *points[], BN_CTX *ctx)
     1206int ossl_ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num,
     1207                                          EC_POINT *points[], BN_CTX *ctx)
    12021208{
    12031209    BN_CTX *new_ctx = NULL;
     
    12111217
    12121218    if (ctx == NULL) {
    1213         ctx = new_ctx = BN_CTX_new();
     1219        ctx = new_ctx = BN_CTX_new_ex(group->libctx);
    12141220        if (ctx == NULL)
    12151221            return 0;
     
    12671273
    12681274    if (!group->meth->field_inv(group, tmp, prod_Z[num - 1], ctx)) {
    1269         ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
     1275        ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
    12701276        goto err;
    12711277    }
     
    13571363}
    13581364
    1359 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
    1360                             const BIGNUM *b, BN_CTX *ctx)
     1365int ossl_ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
     1366                                 const BIGNUM *b, BN_CTX *ctx)
    13611367{
    13621368    return BN_mod_mul(r, a, b, group->field, ctx);
    13631369}
    13641370
    1365 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
    1366                             BN_CTX *ctx)
     1371int ossl_ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
     1372                                 BN_CTX *ctx)
    13671373{
    13681374    return BN_mod_sqr(r, a, group->field, ctx);
     
    13751381 * NB: "a" must be in _decoded_ form. (i.e. field_decode must precede.)
    13761382 */
    1377 int ec_GFp_simple_field_inv(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
    1378                             BN_CTX *ctx)
     1383int ossl_ec_GFp_simple_field_inv(const EC_GROUP *group, BIGNUM *r,
     1384                                 const BIGNUM *a, BN_CTX *ctx)
    13791385{
    13801386    BIGNUM *e = NULL;
     
    13821388    int ret = 0;
    13831389
    1384     if (ctx == NULL && (ctx = new_ctx = BN_CTX_secure_new()) == NULL)
     1390    if (ctx == NULL
     1391            && (ctx = new_ctx = BN_CTX_secure_new_ex(group->libctx)) == NULL)
    13851392        return 0;
    13861393
     
    13901397
    13911398    do {
    1392         if (!BN_priv_rand_range(e, group->field))
     1399        if (!BN_priv_rand_range_ex(e, group->field, 0, ctx))
    13931400        goto err;
    13941401    } while (BN_is_zero(e));
     
    13991406    /* r := 1/(a * e) */
    14001407    if (!BN_mod_inverse(r, r, group->field, ctx)) {
    1401         ECerr(EC_F_EC_GFP_SIMPLE_FIELD_INV, EC_R_CANNOT_INVERT);
     1408        ERR_raise(ERR_LIB_EC, EC_R_CANNOT_INVERT);
    14021409        goto err;
    14031410    }
     
    14211428 *
    14221429 */
    1423 int ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
    1424                                     BN_CTX *ctx)
     1430int ossl_ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p,
     1431                                         BN_CTX *ctx)
    14251432{
    14261433    int ret = 0;
     
    14321439    temp = BN_CTX_get(ctx);
    14331440    if (temp == NULL) {
    1434         ECerr(EC_F_EC_GFP_SIMPLE_BLIND_COORDINATES, ERR_R_MALLOC_FAILURE);
     1441        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    14351442        goto end;
    14361443    }
     
    14431450    do {
    14441451        ERR_set_mark();
    1445         ret = BN_priv_rand_range(lambda, group->field);
     1452        ret = BN_priv_rand_range_ex(lambda, group->field, 0, ctx);
    14461453        ERR_pop_to_mark();
    14471454        if (ret == 0) {
     
    14841491 * for any non-zero \lambda that holds for projective (homogeneous) coords.
    14851492 */
    1486 int ec_GFp_simple_ladder_pre(const EC_GROUP *group,
    1487                              EC_POINT *r, EC_POINT *s,
    1488                              EC_POINT *p, BN_CTX *ctx)
     1493int ossl_ec_GFp_simple_ladder_pre(const EC_GROUP *group,
     1494                                  EC_POINT *r, EC_POINT *s,
     1495                                  EC_POINT *p, BN_CTX *ctx)
    14891496{
    14901497    BIGNUM *t1, *t2, *t3, *t4, *t5 = NULL;
     
    15131520    /* make sure lambda (r->Y here for storage) is not zero */
    15141521    do {
    1515         if (!BN_priv_rand_range(r->Y, group->field))
     1522        if (!BN_priv_rand_range_ex(r->Y, group->field, 0, ctx))
    15161523            return 0;
    15171524    } while (BN_is_zero(r->Y));
     
    15191526    /* make sure lambda (s->Z here for storage) is not zero */
    15201527    do {
    1521         if (!BN_priv_rand_range(s->Z, group->field))
     1528        if (!BN_priv_rand_range_ex(s->Z, group->field, 0, ctx))
    15221529            return 0;
    15231530    } while (BN_is_zero(s->Z));
     
    15541561 * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#ladder-mladd-2002-it-4
    15551562 */
    1556 int ec_GFp_simple_ladder_step(const EC_GROUP *group,
    1557                               EC_POINT *r, EC_POINT *s,
    1558                               EC_POINT *p, BN_CTX *ctx)
     1563int ossl_ec_GFp_simple_ladder_step(const EC_GROUP *group,
     1564                                   EC_POINT *r, EC_POINT *s,
     1565                                   EC_POINT *p, BN_CTX *ctx)
    15591566{
    15601567    int ret = 0;
     
    16421649 *    one of the BN_is_zero(...) branches.
    16431650 */
    1644 int ec_GFp_simple_ladder_post(const EC_GROUP *group,
    1645                               EC_POINT *r, EC_POINT *s,
    1646                               EC_POINT *p, BN_CTX *ctx)
     1651int ossl_ec_GFp_simple_ladder_post(const EC_GROUP *group,
     1652                                   EC_POINT *r, EC_POINT *s,
     1653                                   EC_POINT *p, BN_CTX *ctx)
    16471654{
    16481655    int ret = 0;
  • trunk/src/libs/openssl-3.0.1/crypto/ec/ecx_meth.c

    r91772 r94082  
    11/*
    2  * Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    88 */
    99
     10/*
     11 * ECDSA low level APIs are deprecated for public use, but still ok for
     12 * internal use.
     13 */
     14#include "internal/deprecated.h"
     15
    1016#include <stdio.h>
    11 #include "internal/cryptlib.h"
    1217#include <openssl/x509.h>
    1318#include <openssl/ec.h>
     19#include <openssl/core_names.h>
     20#include <openssl/param_build.h>
    1421#include <openssl/rand.h>
     22#include "internal/cryptlib.h"
     23#include "internal/provider.h"
    1524#include "crypto/asn1.h"
    1625#include "crypto/evp.h"
     26#include "crypto/ecx.h"
    1727#include "ec_local.h"
    1828#include "curve448/curve448_local.h"
    19 
    20 #define X25519_BITS          253
    21 #define X25519_SECURITY_BITS 128
    22 
    23 #define ED25519_SIGSIZE      64
    24 
    25 #define X448_BITS            448
    26 #define ED448_BITS           456
    27 #define X448_SECURITY_BITS   224
    28 
    29 #define ED448_SIGSIZE        114
    30 
    31 #define ISX448(id)      ((id) == EVP_PKEY_X448)
    32 #define IS25519(id)     ((id) == EVP_PKEY_X25519 || (id) == EVP_PKEY_ED25519)
    33 #define KEYLENID(id)    (IS25519(id) ? X25519_KEYLEN \
    34                                      : ((id) == EVP_PKEY_X448 ? X448_KEYLEN \
    35                                                               : ED448_KEYLEN))
    36 #define KEYLEN(p)       KEYLENID((p)->ameth->pkey_id)
    37 
    38 
    39 typedef enum {
    40     KEY_OP_PUBLIC,
    41     KEY_OP_PRIVATE,
    42     KEY_OP_KEYGEN
    43 } ecx_key_op_t;
    44 
    45 /* Setup EVP_PKEY using public, private or generation */
    46 static int ecx_key_op(EVP_PKEY *pkey, int id, const X509_ALGOR *palg,
    47                       const unsigned char *p, int plen, ecx_key_op_t op)
    48 {
    49     ECX_KEY *key = NULL;
    50     unsigned char *privkey, *pubkey;
    51 
    52     if (op != KEY_OP_KEYGEN) {
    53         if (palg != NULL) {
    54             int ptype;
    55 
    56             /* Algorithm parameters must be absent */
    57             X509_ALGOR_get0(NULL, &ptype, NULL, palg);
    58             if (ptype != V_ASN1_UNDEF) {
    59                 ECerr(EC_F_ECX_KEY_OP, EC_R_INVALID_ENCODING);
    60                 return 0;
    61             }
    62         }
    63 
    64         if (p == NULL || plen != KEYLENID(id)) {
    65             ECerr(EC_F_ECX_KEY_OP, EC_R_INVALID_ENCODING);
    66             return 0;
    67         }
    68     }
    69 
    70     key = OPENSSL_zalloc(sizeof(*key));
    71     if (key == NULL) {
    72         ECerr(EC_F_ECX_KEY_OP, ERR_R_MALLOC_FAILURE);
    73         return 0;
    74     }
    75     pubkey = key->pubkey;
    76 
    77     if (op == KEY_OP_PUBLIC) {
    78         memcpy(pubkey, p, plen);
    79     } else {
    80         privkey = key->privkey = OPENSSL_secure_malloc(KEYLENID(id));
    81         if (privkey == NULL) {
    82             ECerr(EC_F_ECX_KEY_OP, ERR_R_MALLOC_FAILURE);
    83             goto err;
    84         }
    85         if (op == KEY_OP_KEYGEN) {
    86             if (RAND_priv_bytes(privkey, KEYLENID(id)) <= 0) {
    87                 OPENSSL_secure_free(privkey);
    88                 key->privkey = NULL;
    89                 goto err;
    90             }
    91             if (id == EVP_PKEY_X25519) {
    92                 privkey[0] &= 248;
    93                 privkey[X25519_KEYLEN - 1] &= 127;
    94                 privkey[X25519_KEYLEN - 1] |= 64;
    95             } else if (id == EVP_PKEY_X448) {
    96                 privkey[0] &= 252;
    97                 privkey[X448_KEYLEN - 1] |= 128;
    98             }
    99         } else {
    100             memcpy(privkey, p, KEYLENID(id));
    101         }
    102         switch (id) {
    103         case EVP_PKEY_X25519:
    104             X25519_public_from_private(pubkey, privkey);
    105             break;
    106         case EVP_PKEY_ED25519:
    107             ED25519_public_from_private(pubkey, privkey);
    108             break;
    109         case EVP_PKEY_X448:
    110             X448_public_from_private(pubkey, privkey);
    111             break;
    112         case EVP_PKEY_ED448:
    113             ED448_public_from_private(pubkey, privkey);
    114             break;
    115         }
    116     }
    117 
    118     EVP_PKEY_assign(pkey, id, key);
    119     return 1;
    120  err:
    121     OPENSSL_free(key);
    122     return 0;
    123 }
     29#include "ecx_backend.h"
    12430
    12531static int ecx_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
     
    12935
    13036    if (ecxkey == NULL) {
    131         ECerr(EC_F_ECX_PUB_ENCODE, EC_R_INVALID_KEY);
     37        ERR_raise(ERR_LIB_EC, EC_R_INVALID_KEY);
    13238        return 0;
    13339    }
     
    13541    penc = OPENSSL_memdup(ecxkey->pubkey, KEYLEN(pkey));
    13642    if (penc == NULL) {
    137         ECerr(EC_F_ECX_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
     43        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    13844        return 0;
    13945    }
     
    14248                                V_ASN1_UNDEF, NULL, penc, KEYLEN(pkey))) {
    14349        OPENSSL_free(penc);
    144         ECerr(EC_F_ECX_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
    145         return 0;
    146     }
    147     return 1;
    148 }
    149 
    150 static int ecx_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
     50        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     51        return 0;
     52    }
     53    return 1;
     54}
     55
     56static int ecx_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
    15157{
    15258    const unsigned char *p;
    15359    int pklen;
    15460    X509_ALGOR *palg;
     61    ECX_KEY *ecx;
     62    int ret = 0;
    15563
    15664    if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
    15765        return 0;
    158     return ecx_key_op(pkey, pkey->ameth->pkey_id, palg, p, pklen,
    159                       KEY_OP_PUBLIC);
     66    ecx = ossl_ecx_key_op(palg, p, pklen, pkey->ameth->pkey_id,
     67                          KEY_OP_PUBLIC, NULL, NULL);
     68    if (ecx != NULL) {
     69        ret = 1;
     70        EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
     71    }
     72    return ret;
    16073}
    16174
     
    17184}
    17285
    173 static int ecx_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
    174 {
    175     const unsigned char *p;
    176     int plen;
    177     ASN1_OCTET_STRING *oct = NULL;
    178     const X509_ALGOR *palg;
    179     int rv;
    180 
    181     if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8))
    182         return 0;
    183 
    184     oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
    185     if (oct == NULL) {
    186         p = NULL;
    187         plen = 0;
    188     } else {
    189         p = ASN1_STRING_get0_data(oct);
    190         plen = ASN1_STRING_length(oct);
    191     }
    192 
    193     rv = ecx_key_op(pkey, pkey->ameth->pkey_id, palg, p, plen, KEY_OP_PRIVATE);
    194     ASN1_STRING_clear_free(oct);
    195     return rv;
     86static int ecx_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8,
     87                              OSSL_LIB_CTX *libctx, const char *propq)
     88{
     89    int ret = 0;
     90    ECX_KEY *ecx = ossl_ecx_key_from_pkcs8(p8, libctx, propq);
     91
     92    if (ecx != NULL) {
     93        ret = 1;
     94        EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
     95    }
     96
     97    return ret;
    19698}
    19799
     
    204106
    205107    if (ecxkey == NULL || ecxkey->privkey == NULL) {
    206         ECerr(EC_F_ECX_PRIV_ENCODE, EC_R_INVALID_PRIVATE_KEY);
     108        ERR_raise(ERR_LIB_EC, EC_R_INVALID_PRIVATE_KEY);
    207109        return 0;
    208110    }
     
    214116    penclen = i2d_ASN1_OCTET_STRING(&oct, &penc);
    215117    if (penclen < 0) {
    216         ECerr(EC_F_ECX_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
     118        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    217119        return 0;
    218120    }
     
    221123                         V_ASN1_UNDEF, NULL, penc, penclen)) {
    222124        OPENSSL_clear_free(penc, penclen);
    223         ECerr(EC_F_ECX_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
     125        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
    224126        return 0;
    225127    }
     
    255157static void ecx_free(EVP_PKEY *pkey)
    256158{
    257     if (pkey->pkey.ecx != NULL)
    258         OPENSSL_secure_clear_free(pkey->pkey.ecx->privkey, KEYLEN(pkey));
    259     OPENSSL_free(pkey->pkey.ecx);
     159    ossl_ecx_key_free(pkey->pkey.ecx);
    260160}
    261161
     
    319219    switch (op) {
    320220
    321     case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
    322         return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, arg2, arg1,
    323                           KEY_OP_PUBLIC);
    324 
     221    case ASN1_PKEY_CTRL_SET1_TLS_ENCPT: {
     222        ECX_KEY *ecx = ossl_ecx_key_op(NULL, arg2, arg1, pkey->ameth->pkey_id,
     223                                       KEY_OP_PUBLIC, NULL, NULL);
     224
     225        if (ecx != NULL) {
     226            EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
     227            return 1;
     228        }
     229        return 0;
     230    }
    325231    case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
    326232        if (pkey->pkey.ecx != NULL) {
     
    356262                            size_t len)
    357263{
    358     return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, priv, len,
    359                        KEY_OP_PRIVATE);
     264    OSSL_LIB_CTX *libctx = NULL;
     265    ECX_KEY *ecx = NULL;
     266
     267    if (pkey->keymgmt != NULL)
     268        libctx = ossl_provider_libctx(EVP_KEYMGMT_get0_provider(pkey->keymgmt));
     269
     270    ecx = ossl_ecx_key_op(NULL, priv, len, pkey->ameth->pkey_id,
     271                          KEY_OP_PRIVATE, libctx, NULL);
     272
     273    if (ecx != NULL) {
     274        EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
     275        return 1;
     276    }
     277    return 0;
    360278}
    361279
    362280static int ecx_set_pub_key(EVP_PKEY *pkey, const unsigned char *pub, size_t len)
    363281{
    364     return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, pub, len,
    365                       KEY_OP_PUBLIC);
     282    OSSL_LIB_CTX *libctx = NULL;
     283    ECX_KEY *ecx = NULL;
     284
     285    if (pkey->keymgmt != NULL)
     286        libctx = ossl_provider_libctx(EVP_KEYMGMT_get0_provider(pkey->keymgmt));
     287
     288    ecx = ossl_ecx_key_op(NULL, pub, len, pkey->ameth->pkey_id,
     289                          KEY_OP_PUBLIC, libctx, NULL);
     290
     291    if (ecx != NULL) {
     292        EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, ecx);
     293        return 1;
     294    }
     295    return 0;
    366296}
    367297
     
    407337}
    408338
    409 const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth = {
     339static size_t ecx_pkey_dirty_cnt(const EVP_PKEY *pkey)
     340{
     341    /*
     342     * We provide no mechanism to "update" an ECX key once it has been set,
     343     * therefore we do not have to maintain a dirty count.
     344     */
     345    return 1;
     346}
     347
     348static int ecx_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
     349                              OSSL_FUNC_keymgmt_import_fn *importer,
     350                              OSSL_LIB_CTX *libctx, const char *propq)
     351{
     352    const ECX_KEY *key = from->pkey.ecx;
     353    OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
     354    OSSL_PARAM *params = NULL;
     355    int selection = 0;
     356    int rv = 0;
     357
     358    if (tmpl == NULL)
     359        return 0;
     360
     361    /* A key must at least have a public part */
     362    if (!OSSL_PARAM_BLD_push_octet_string(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
     363                                          key->pubkey, key->keylen))
     364        goto err;
     365    selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
     366
     367    if (key->privkey != NULL) {
     368        if (!OSSL_PARAM_BLD_push_octet_string(tmpl,
     369                                              OSSL_PKEY_PARAM_PRIV_KEY,
     370                                              key->privkey, key->keylen))
     371            goto err;
     372        selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
     373    }
     374
     375    params = OSSL_PARAM_BLD_to_param(tmpl);
     376
     377    /* We export, the provider imports */
     378    rv = importer(to_keydata, selection, params);
     379
     380 err:
     381    OSSL_PARAM_BLD_free(tmpl);
     382    OSSL_PARAM_free(params);
     383    return rv;
     384}
     385
     386static int ecx_generic_import_from(const OSSL_PARAM params[], void *vpctx,
     387                                   int keytype)
     388{
     389    EVP_PKEY_CTX *pctx = vpctx;
     390    EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
     391    ECX_KEY *ecx = ossl_ecx_key_new(pctx->libctx, KEYNID2TYPE(keytype), 0,
     392                                    pctx->propquery);
     393
     394    if (ecx == NULL) {
     395        ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
     396        return 0;
     397    }
     398
     399    if (!ossl_ecx_key_fromdata(ecx, params, 1)
     400        || !EVP_PKEY_assign(pkey, keytype, ecx)) {
     401        ossl_ecx_key_free(ecx);
     402        return 0;
     403    }
     404    return 1;
     405}
     406
     407static int ecx_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
     408{
     409    ECX_KEY *ecx = from->pkey.ecx, *dupkey = NULL;
     410    int ret;
     411
     412    if (ecx != NULL) {
     413        dupkey = ossl_ecx_key_dup(ecx, OSSL_KEYMGMT_SELECT_ALL);
     414        if (dupkey == NULL)
     415            return 0;
     416    }
     417
     418    ret = EVP_PKEY_assign(to, from->type, dupkey);
     419    if (!ret)
     420        ossl_ecx_key_free(dupkey);
     421    return ret;
     422}
     423
     424static int x25519_import_from(const OSSL_PARAM params[], void *vpctx)
     425{
     426    return ecx_generic_import_from(params, vpctx, EVP_PKEY_X25519);
     427}
     428
     429const EVP_PKEY_ASN1_METHOD ossl_ecx25519_asn1_meth = {
    410430    EVP_PKEY_X25519,
    411431    EVP_PKEY_X25519,
     
    419439    ecx_pub_print,
    420440
    421     ecx_priv_decode,
     441    NULL,
    422442    ecx_priv_encode,
    423443    ecx_priv_print,
     
    448468    ecx_get_priv_key,
    449469    ecx_get_pub_key,
     470    ecx_pkey_dirty_cnt,
     471    ecx_pkey_export_to,
     472    x25519_import_from,
     473    ecx_pkey_copy,
     474
     475    ecx_priv_decode_ex
    450476};
    451477
    452 const EVP_PKEY_ASN1_METHOD ecx448_asn1_meth = {
     478static int x448_import_from(const OSSL_PARAM params[], void *vpctx)
     479{
     480    return ecx_generic_import_from(params, vpctx, EVP_PKEY_X448);
     481}
     482
     483const EVP_PKEY_ASN1_METHOD ossl_ecx448_asn1_meth = {
    453484    EVP_PKEY_X448,
    454485    EVP_PKEY_X448,
     
    462493    ecx_pub_print,
    463494
    464     ecx_priv_decode,
     495    NULL,
    465496    ecx_priv_encode,
    466497    ecx_priv_print,
     
    491522    ecx_get_priv_key,
    492523    ecx_get_pub_key,
     524    ecx_pkey_dirty_cnt,
     525    ecx_pkey_export_to,
     526    x448_import_from,
     527    ecx_pkey_copy,
     528
     529    ecx_priv_decode_ex
    493530};
    494531
     
    503540}
    504541
    505 static int ecd_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
    506                            X509_ALGOR *sigalg, ASN1_BIT_STRING *str,
    507                            EVP_PKEY *pkey)
     542static int ecd_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
     543                           const void *asn, const X509_ALGOR *sigalg,
     544                           const ASN1_BIT_STRING *str, EVP_PKEY *pkey)
    508545{
    509546    const ASN1_OBJECT *obj;
     
    515552    nid = OBJ_obj2nid(obj);
    516553    if ((nid != NID_ED25519 && nid != NID_ED448) || ptype != V_ASN1_UNDEF) {
    517         ECerr(EC_F_ECD_ITEM_VERIFY, EC_R_INVALID_ENCODING);
     554        ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
    518555        return 0;
    519556    }
     
    525562}
    526563
    527 static int ecd_item_sign25519(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
     564static int ecd_item_sign25519(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
     565                              const void *asn,
    528566                              X509_ALGOR *alg1, X509_ALGOR *alg2,
    529567                              ASN1_BIT_STRING *str)
     
    545583}
    546584
    547 static int ecd_item_sign448(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
     585static int ecd_item_sign448(EVP_MD_CTX *ctx, const ASN1_ITEM *it,
     586                            const void *asn,
    548587                            X509_ALGOR *alg1, X509_ALGOR *alg2,
    549588                            ASN1_BIT_STRING *str)
     
    565604}
    566605
    567 
    568 const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
     606static int ed25519_import_from(const OSSL_PARAM params[], void *vpctx)
     607{
     608    return ecx_generic_import_from(params, vpctx, EVP_PKEY_ED25519);
     609}
     610
     611const EVP_PKEY_ASN1_METHOD ossl_ed25519_asn1_meth = {
    569612    EVP_PKEY_ED25519,
    570613    EVP_PKEY_ED25519,
     
    578621    ecx_pub_print,
    579622
    580     ecx_priv_decode,
     623    NULL,
    581624    ecx_priv_encode,
    582625    ecx_priv_print,
     
    606649    ecx_get_priv_key,
    607650    ecx_get_pub_key,
     651    ecx_pkey_dirty_cnt,
     652    ecx_pkey_export_to,
     653    ed25519_import_from,
     654    ecx_pkey_copy,
     655
     656    ecx_priv_decode_ex
    608657};
    609658
    610 const EVP_PKEY_ASN1_METHOD ed448_asn1_meth = {
     659static int ed448_import_from(const OSSL_PARAM params[], void *vpctx)
     660{
     661    return ecx_generic_import_from(params, vpctx, EVP_PKEY_ED448);
     662}
     663
     664const EVP_PKEY_ASN1_METHOD ossl_ed448_asn1_meth = {
    611665    EVP_PKEY_ED448,
    612666    EVP_PKEY_ED448,
     
    620674    ecx_pub_print,
    621675
    622     ecx_priv_decode,
     676    NULL,
    623677    ecx_priv_encode,
    624678    ecx_priv_print,
     
    648702    ecx_get_priv_key,
    649703    ecx_get_pub_key,
     704    ecx_pkey_dirty_cnt,
     705    ecx_pkey_export_to,
     706    ed448_import_from,
     707    ecx_pkey_copy,
     708
     709    ecx_priv_decode_ex
    650710};
    651711
    652712static int pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
    653713{
    654     return ecx_key_op(pkey, ctx->pmeth->pkey_id, NULL, NULL, 0, KEY_OP_KEYGEN);
     714    ECX_KEY *ecx = ossl_ecx_key_op(NULL, NULL, 0, ctx->pmeth->pkey_id,
     715                                   KEY_OP_PUBLIC, NULL, NULL);
     716
     717    if (ecx != NULL) {
     718        EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, ecx);
     719        return 1;
     720    }
     721    return 0;
    655722}
    656723
     
    663730
    664731    if (ctx->pkey == NULL || ctx->peerkey == NULL) {
    665         ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_KEYS_NOT_SET);
    666         return 0;
    667     }
    668     ecxkey = ctx->pkey->pkey.ecx;
    669     peerkey = ctx->peerkey->pkey.ecx;
     732        ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
     733        return 0;
     734    }
     735    ecxkey = evp_pkey_get_legacy(ctx->pkey);
     736    peerkey = evp_pkey_get_legacy(ctx->peerkey);
    670737    if (ecxkey == NULL || ecxkey->privkey == NULL) {
    671         ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_INVALID_PRIVATE_KEY);
     738        ERR_raise(ERR_LIB_EC, EC_R_INVALID_PRIVATE_KEY);
    672739        return 0;
    673740    }
    674741    if (peerkey == NULL) {
    675         ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_INVALID_PEER_KEY);
     742        ERR_raise(ERR_LIB_EC, EC_R_INVALID_PEER_KEY);
    676743        return 0;
    677744    }
     
    689756    if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
    690757            || (key != NULL
    691                 && X25519(key, privkey, pubkey) == 0))
     758                && ossl_x25519(key, privkey, pubkey) == 0))
    692759        return 0;
    693760    *keylen = X25519_KEYLEN;
     
    702769    if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
    703770            || (key != NULL
    704                 && X448(key, privkey, pubkey) == 0))
     771                && ossl_x448(key, privkey, pubkey) == 0))
    705772        return 0;
    706773    *keylen = X448_KEYLEN;
     
    716783}
    717784
    718 const EVP_PKEY_METHOD ecx25519_pkey_meth = {
     785static const EVP_PKEY_METHOD ecx25519_pkey_meth = {
    719786    EVP_PKEY_X25519,
    720787    0, 0, 0, 0, 0, 0, 0,
     
    726793};
    727794
    728 const EVP_PKEY_METHOD ecx448_pkey_meth = {
     795static const EVP_PKEY_METHOD ecx448_pkey_meth = {
    729796    EVP_PKEY_X448,
    730797    0, 0, 0, 0, 0, 0, 0,
     
    740807                                    size_t tbslen)
    741808{
    742     const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
     809    const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
    743810
    744811    if (sig == NULL) {
     
    747814    }
    748815    if (*siglen < ED25519_SIGSIZE) {
    749         ECerr(EC_F_PKEY_ECD_DIGESTSIGN25519, EC_R_BUFFER_TOO_SMALL);
    750         return 0;
    751     }
    752 
    753     if (ED25519_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey) == 0)
     816        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
     817        return 0;
     818    }
     819
     820    if (ossl_ed25519_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey, NULL,
     821                          NULL) == 0)
    754822        return 0;
    755823    *siglen = ED25519_SIGSIZE;
     
    761829                                  size_t tbslen)
    762830{
    763     const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
     831    const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
    764832
    765833    if (sig == NULL) {
     
    768836    }
    769837    if (*siglen < ED448_SIGSIZE) {
    770         ECerr(EC_F_PKEY_ECD_DIGESTSIGN448, EC_R_BUFFER_TOO_SMALL);
    771         return 0;
    772     }
    773 
    774     if (ED448_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey, NULL,
    775                    0) == 0)
     838        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
     839        return 0;
     840    }
     841
     842    if (ossl_ed448_sign(edkey->libctx, sig, tbs, tbslen, edkey->pubkey,
     843                        edkey->privkey, NULL, 0, edkey->propq) == 0)
    776844        return 0;
    777845    *siglen = ED448_SIGSIZE;
     
    783851                                      size_t tbslen)
    784852{
    785     const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
     853    const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
    786854
    787855    if (siglen != ED25519_SIGSIZE)
    788856        return 0;
    789857
    790     return ED25519_verify(tbs, tbslen, sig, edkey->pubkey);
     858    return ossl_ed25519_verify(tbs, tbslen, sig, edkey->pubkey,
     859                               edkey->libctx, edkey->propq);
    791860}
    792861
     
    795864                                    size_t tbslen)
    796865{
    797     const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
     866    const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
    798867
    799868    if (siglen != ED448_SIGSIZE)
    800869        return 0;
    801870
    802     return ED448_verify(tbs, tbslen, sig, edkey->pubkey, NULL, 0);
     871    return ossl_ed448_verify(edkey->libctx, tbs, tbslen, sig, edkey->pubkey,
     872                             NULL, 0, edkey->propq);
    803873}
    804874
     
    810880        if (p2 == NULL || (const EVP_MD *)p2 == EVP_md_null())
    811881            return 1;
    812         ECerr(EC_F_PKEY_ECD_CTRL, EC_R_INVALID_DIGEST_TYPE);
     882        ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST_TYPE);
    813883        return 0;
    814884
     
    819889}
    820890
    821 const EVP_PKEY_METHOD ed25519_pkey_meth = {
     891static const EVP_PKEY_METHOD ed25519_pkey_meth = {
    822892    EVP_PKEY_ED25519, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
    823893    0, 0, 0, 0, 0, 0,
     
    830900};
    831901
    832 const EVP_PKEY_METHOD ed448_pkey_meth = {
     902static const EVP_PKEY_METHOD ed448_pkey_meth = {
    833903    EVP_PKEY_ED448, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
    834904    0, 0, 0, 0, 0, 0,
     
    840910    pkey_ecd_digestverify448
    841911};
     912
     913#ifdef S390X_EC_ASM
     914# include "s390x_arch.h"
     915
     916static int s390x_pkey_ecx_keygen25519(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
     917{
     918    static const unsigned char generator[] = {
     919        0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     920        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     921        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
     922    };
     923    ECX_KEY *key = ossl_ecx_key_new(ctx->libctx, ECX_KEY_TYPE_X25519, 1,
     924                                    ctx->propquery);
     925    unsigned char *privkey = NULL, *pubkey;
     926
     927    if (key == NULL) {
     928        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     929        goto err;
     930    }
     931
     932    pubkey = key->pubkey;
     933
     934    privkey = ossl_ecx_key_allocate_privkey(key);
     935    if (privkey == NULL) {
     936        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     937        goto err;
     938    }
     939
     940    if (RAND_priv_bytes_ex(ctx->libctx, privkey, X25519_KEYLEN, 0) <= 0)
     941        goto err;
     942
     943    privkey[0] &= 248;
     944    privkey[31] &= 127;
     945    privkey[31] |= 64;
     946
     947    if (s390x_x25519_mul(pubkey, generator, privkey) != 1)
     948        goto err;
     949
     950    EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
     951    return 1;
     952 err:
     953    ossl_ecx_key_free(key);
     954    return 0;
     955}
     956
     957static int s390x_pkey_ecx_keygen448(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
     958{
     959    static const unsigned char generator[] = {
     960        0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     961        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     962        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     963        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     964        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
     965    };
     966    ECX_KEY *key = ossl_ecx_key_new(ctx->libctx, ECX_KEY_TYPE_X448, 1,
     967                                    ctx->propquery);
     968    unsigned char *privkey = NULL, *pubkey;
     969
     970    if (key == NULL) {
     971        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     972        goto err;
     973    }
     974
     975    pubkey = key->pubkey;
     976
     977    privkey = ossl_ecx_key_allocate_privkey(key);
     978    if (privkey == NULL) {
     979        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     980        goto err;
     981    }
     982
     983    if (RAND_priv_bytes_ex(ctx->libctx, privkey, X448_KEYLEN, 0) <= 0)
     984        goto err;
     985
     986    privkey[0] &= 252;
     987    privkey[55] |= 128;
     988
     989    if (s390x_x448_mul(pubkey, generator, privkey) != 1)
     990        goto err;
     991
     992    EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
     993    return 1;
     994 err:
     995    ossl_ecx_key_free(key);
     996    return 0;
     997}
     998
     999static int s390x_pkey_ecd_keygen25519(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
     1000{
     1001    static const unsigned char generator_x[] = {
     1002        0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9, 0xb2, 0xa7, 0x25, 0x95,
     1003        0x60, 0xc7, 0x2c, 0x69, 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0,
     1004        0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21
     1005    };
     1006    static const unsigned char generator_y[] = {
     1007        0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
     1008        0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
     1009        0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
     1010    };
     1011    unsigned char x_dst[32], buff[SHA512_DIGEST_LENGTH];
     1012    ECX_KEY *key = ossl_ecx_key_new(ctx->libctx, ECX_KEY_TYPE_ED25519, 1,
     1013                                    ctx->propquery);
     1014    unsigned char *privkey = NULL, *pubkey;
     1015    unsigned int sz;
     1016    EVP_MD *md = NULL;
     1017    int rv;
     1018
     1019    if (key == NULL) {
     1020        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     1021        goto err;
     1022    }
     1023
     1024    pubkey = key->pubkey;
     1025
     1026    privkey = ossl_ecx_key_allocate_privkey(key);
     1027    if (privkey == NULL) {
     1028        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     1029        goto err;
     1030    }
     1031
     1032    if (RAND_priv_bytes_ex(ctx->libctx, privkey, ED25519_KEYLEN, 0) <= 0)
     1033        goto err;
     1034
     1035    md = EVP_MD_fetch(ctx->libctx, "SHA512", ctx->propquery);
     1036    if (md == NULL)
     1037        goto err;
     1038
     1039    rv = EVP_Digest(privkey, 32, buff, &sz, md, NULL);
     1040    EVP_MD_free(md);
     1041    if (!rv)
     1042        goto err;
     1043
     1044    buff[0] &= 248;
     1045    buff[31] &= 63;
     1046    buff[31] |= 64;
     1047
     1048    if (s390x_ed25519_mul(x_dst, pubkey,
     1049                          generator_x, generator_y, buff) != 1)
     1050        goto err;
     1051
     1052    pubkey[31] |= ((x_dst[0] & 0x01) << 7);
     1053
     1054    EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
     1055    return 1;
     1056 err:
     1057    ossl_ecx_key_free(key);
     1058    return 0;
     1059}
     1060
     1061static int s390x_pkey_ecd_keygen448(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
     1062{
     1063    static const unsigned char generator_x[] = {
     1064        0x5e, 0xc0, 0x0c, 0xc7, 0x2b, 0xa8, 0x26, 0x26, 0x8e, 0x93, 0x00, 0x8b,
     1065        0xe1, 0x80, 0x3b, 0x43, 0x11, 0x65, 0xb6, 0x2a, 0xf7, 0x1a, 0xae, 0x12,
     1066        0x64, 0xa4, 0xd3, 0xa3, 0x24, 0xe3, 0x6d, 0xea, 0x67, 0x17, 0x0f, 0x47,
     1067        0x70, 0x65, 0x14, 0x9e, 0xda, 0x36, 0xbf, 0x22, 0xa6, 0x15, 0x1d, 0x22,
     1068        0xed, 0x0d, 0xed, 0x6b, 0xc6, 0x70, 0x19, 0x4f, 0x00
     1069    };
     1070    static const unsigned char generator_y[] = {
     1071        0x14, 0xfa, 0x30, 0xf2, 0x5b, 0x79, 0x08, 0x98, 0xad, 0xc8, 0xd7, 0x4e,
     1072        0x2c, 0x13, 0xbd, 0xfd, 0xc4, 0x39, 0x7c, 0xe6, 0x1c, 0xff, 0xd3, 0x3a,
     1073        0xd7, 0xc2, 0xa0, 0x05, 0x1e, 0x9c, 0x78, 0x87, 0x40, 0x98, 0xa3, 0x6c,
     1074        0x73, 0x73, 0xea, 0x4b, 0x62, 0xc7, 0xc9, 0x56, 0x37, 0x20, 0x76, 0x88,
     1075        0x24, 0xbc, 0xb6, 0x6e, 0x71, 0x46, 0x3f, 0x69, 0x00
     1076    };
     1077    unsigned char x_dst[57], buff[114];
     1078    ECX_KEY *key = ossl_ecx_key_new(ctx->libctx, ECX_KEY_TYPE_ED448, 1,
     1079                                    ctx->propquery);
     1080    unsigned char *privkey = NULL, *pubkey;
     1081    EVP_MD_CTX *hashctx = NULL;
     1082    EVP_MD *md = NULL;
     1083    int rv;
     1084
     1085    if (key == NULL) {
     1086        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     1087        goto err;
     1088    }
     1089
     1090    pubkey = key->pubkey;
     1091
     1092    privkey = ossl_ecx_key_allocate_privkey(key);
     1093    if (privkey == NULL) {
     1094        ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
     1095        goto err;
     1096    }
     1097
     1098    if (RAND_priv_bytes_ex(ctx->libctx, privkey, ED448_KEYLEN, 0) <= 0)
     1099        goto err;
     1100
     1101    hashctx = EVP_MD_CTX_new();
     1102    if (hashctx == NULL)
     1103        goto err;
     1104
     1105    md = EVP_MD_fetch(ctx->libctx, "SHAKE256", ctx->propquery);
     1106    if (md == NULL)
     1107        goto err;
     1108
     1109    rv = EVP_DigestInit_ex(hashctx, md, NULL);
     1110    EVP_MD_free(md);
     1111    if (rv != 1)
     1112        goto err;
     1113
     1114    if (EVP_DigestUpdate(hashctx, privkey, 57) != 1)
     1115        goto err;
     1116    if (EVP_DigestFinalXOF(hashctx, buff, sizeof(buff)) != 1)
     1117        goto err;
     1118
     1119    buff[0] &= -4;
     1120    buff[55] |= 0x80;
     1121    buff[56] = 0;
     1122
     1123    if (s390x_ed448_mul(x_dst, pubkey,
     1124                        generator_x, generator_y, buff) != 1)
     1125        goto err;
     1126
     1127    pubkey[56] |= ((x_dst[0] & 0x01) << 7);
     1128
     1129    EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, key);
     1130    EVP_MD_CTX_free(hashctx);
     1131    return 1;
     1132 err:
     1133    ossl_ecx_key_free(key);
     1134    EVP_MD_CTX_free(hashctx);
     1135    return 0;
     1136}
     1137
     1138static int s390x_pkey_ecx_derive25519(EVP_PKEY_CTX *ctx, unsigned char *key,
     1139                                      size_t *keylen)
     1140{
     1141    const unsigned char *privkey, *pubkey;
     1142
     1143    if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey))
     1144        return 0;
     1145
     1146    if (key != NULL)
     1147        return s390x_x25519_mul(key, pubkey, privkey);
     1148
     1149    *keylen = X25519_KEYLEN;
     1150    return 1;
     1151}
     1152
     1153static int s390x_pkey_ecx_derive448(EVP_PKEY_CTX *ctx, unsigned char *key,
     1154                                      size_t *keylen)
     1155{
     1156    const unsigned char *privkey, *pubkey;
     1157
     1158    if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey))
     1159        return 0;
     1160
     1161    if (key != NULL)
     1162        return s390x_x448_mul(key, pubkey, privkey);
     1163
     1164    *keylen = X448_KEYLEN;
     1165    return 1;
     1166}
     1167
     1168static int s390x_pkey_ecd_digestsign25519(EVP_MD_CTX *ctx,
     1169                                          unsigned char *sig, size_t *siglen,
     1170                                          const unsigned char *tbs,
     1171                                          size_t tbslen)
     1172{
     1173    union {
     1174        struct {
     1175            unsigned char sig[64];
     1176            unsigned char priv[32];
     1177        } ed25519;
     1178        unsigned long long buff[512];
     1179    } param;
     1180    const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
     1181    int rc;
     1182
     1183    if (sig == NULL) {
     1184        *siglen = ED25519_SIGSIZE;
     1185        return 1;
     1186    }
     1187
     1188    if (*siglen < ED25519_SIGSIZE) {
     1189        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
     1190        return 0;
     1191    }
     1192
     1193    memset(&param, 0, sizeof(param));
     1194    memcpy(param.ed25519.priv, edkey->privkey, sizeof(param.ed25519.priv));
     1195
     1196    rc = s390x_kdsa(S390X_EDDSA_SIGN_ED25519, &param.ed25519, tbs, tbslen);
     1197    OPENSSL_cleanse(param.ed25519.priv, sizeof(param.ed25519.priv));
     1198    if (rc != 0)
     1199        return 0;
     1200
     1201    s390x_flip_endian32(sig, param.ed25519.sig);
     1202    s390x_flip_endian32(sig + 32, param.ed25519.sig + 32);
     1203
     1204    *siglen = ED25519_SIGSIZE;
     1205    return 1;
     1206}
     1207
     1208static int s390x_pkey_ecd_digestsign448(EVP_MD_CTX *ctx,
     1209                                        unsigned char *sig, size_t *siglen,
     1210                                        const unsigned char *tbs,
     1211                                        size_t tbslen)
     1212{
     1213    union {
     1214        struct {
     1215            unsigned char sig[128];
     1216            unsigned char priv[64];
     1217        } ed448;
     1218        unsigned long long buff[512];
     1219    } param;
     1220    const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
     1221    int rc;
     1222
     1223    if (sig == NULL) {
     1224        *siglen = ED448_SIGSIZE;
     1225        return 1;
     1226    }
     1227
     1228    if (*siglen < ED448_SIGSIZE) {
     1229        ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
     1230        return 0;
     1231    }
     1232
     1233    memset(&param, 0, sizeof(param));
     1234    memcpy(param.ed448.priv + 64 - 57, edkey->privkey, 57);
     1235
     1236    rc = s390x_kdsa(S390X_EDDSA_SIGN_ED448, &param.ed448, tbs, tbslen);
     1237    OPENSSL_cleanse(param.ed448.priv, sizeof(param.ed448.priv));
     1238    if (rc != 0)
     1239        return 0;
     1240
     1241    s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
     1242    s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
     1243    memcpy(sig, param.ed448.sig, 57);
     1244    memcpy(sig + 57, param.ed448.sig + 64, 57);
     1245
     1246    *siglen = ED448_SIGSIZE;
     1247    return 1;
     1248}
     1249
     1250static int s390x_pkey_ecd_digestverify25519(EVP_MD_CTX *ctx,
     1251                                            const unsigned char *sig,
     1252                                            size_t siglen,
     1253                                            const unsigned char *tbs,
     1254                                            size_t tbslen)
     1255{
     1256    union {
     1257        struct {
     1258            unsigned char sig[64];
     1259            unsigned char pub[32];
     1260        } ed25519;
     1261        unsigned long long buff[512];
     1262    } param;
     1263    const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
     1264
     1265    if (siglen != ED25519_SIGSIZE)
     1266        return 0;
     1267
     1268    memset(&param, 0, sizeof(param));
     1269    s390x_flip_endian32(param.ed25519.sig, sig);
     1270    s390x_flip_endian32(param.ed25519.sig + 32, sig + 32);
     1271    s390x_flip_endian32(param.ed25519.pub, edkey->pubkey);
     1272
     1273    return s390x_kdsa(S390X_EDDSA_VERIFY_ED25519,
     1274                      &param.ed25519, tbs, tbslen) == 0 ? 1 : 0;
     1275}
     1276
     1277static int s390x_pkey_ecd_digestverify448(EVP_MD_CTX *ctx,
     1278                                          const unsigned char *sig,
     1279                                          size_t siglen,
     1280                                          const unsigned char *tbs,
     1281                                          size_t tbslen)
     1282{
     1283    union {
     1284        struct {
     1285            unsigned char sig[128];
     1286            unsigned char pub[64];
     1287        } ed448;
     1288        unsigned long long buff[512];
     1289    } param;
     1290    const ECX_KEY *edkey = evp_pkey_get_legacy(EVP_MD_CTX_get_pkey_ctx(ctx)->pkey);
     1291
     1292    if (siglen != ED448_SIGSIZE)
     1293        return 0;
     1294
     1295    memset(&param, 0, sizeof(param));
     1296    memcpy(param.ed448.sig, sig, 57);
     1297    s390x_flip_endian64(param.ed448.sig, param.ed448.sig);
     1298    memcpy(param.ed448.sig + 64, sig + 57, 57);
     1299    s390x_flip_endian64(param.ed448.sig + 64, param.ed448.sig + 64);
     1300    memcpy(param.ed448.pub, edkey->pubkey, 57);
     1301    s390x_flip_endian64(param.ed448.pub, param.ed448.pub);
     1302
     1303    return s390x_kdsa(S390X_EDDSA_VERIFY_ED448,
     1304                      &param.ed448, tbs, tbslen) == 0 ? 1 : 0;
     1305}
     1306
     1307static const EVP_PKEY_METHOD ecx25519_s390x_pkey_meth = {
     1308    EVP_PKEY_X25519,
     1309    0, 0, 0, 0, 0, 0, 0,
     1310    s390x_pkey_ecx_keygen25519,
     1311    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     1312    s390x_pkey_ecx_derive25519,
     1313    pkey_ecx_ctrl,
     1314    0
     1315};
     1316
     1317static const EVP_PKEY_METHOD ecx448_s390x_pkey_meth = {
     1318    EVP_PKEY_X448,
     1319    0, 0, 0, 0, 0, 0, 0,
     1320    s390x_pkey_ecx_keygen448,
     1321    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     1322    s390x_pkey_ecx_derive448,
     1323    pkey_ecx_ctrl,
     1324    0
     1325};
     1326static const EVP_PKEY_METHOD ed25519_s390x_pkey_meth = {
     1327    EVP_PKEY_ED25519, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
     1328    0, 0, 0, 0, 0, 0,
     1329    s390x_pkey_ecd_keygen25519,
     1330    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     1331    pkey_ecd_ctrl,
     1332    0,
     1333    s390x_pkey_ecd_digestsign25519,
     1334    s390x_pkey_ecd_digestverify25519
     1335};
     1336
     1337static const EVP_PKEY_METHOD ed448_s390x_pkey_meth = {
     1338    EVP_PKEY_ED448, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
     1339    0, 0, 0, 0, 0, 0,
     1340    s390x_pkey_ecd_keygen448,
     1341    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     1342    pkey_ecd_ctrl,
     1343    0,
     1344    s390x_pkey_ecd_digestsign448,
     1345    s390x_pkey_ecd_digestverify448
     1346};
     1347#endif
     1348
     1349const EVP_PKEY_METHOD *ossl_ecx25519_pkey_method(void)
     1350{
     1351#ifdef S390X_EC_ASM
     1352    if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519))
     1353        return &ecx25519_s390x_pkey_meth;
     1354#endif
     1355    return &ecx25519_pkey_meth;
     1356}
     1357
     1358const EVP_PKEY_METHOD *ossl_ecx448_pkey_method(void)
     1359{
     1360#ifdef S390X_EC_ASM
     1361    if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448))
     1362        return &ecx448_s390x_pkey_meth;
     1363#endif
     1364    return &ecx448_pkey_meth;
     1365}
     1366
     1367const EVP_PKEY_METHOD *ossl_ed25519_pkey_method(void)
     1368{
     1369#ifdef S390X_EC_ASM
     1370    if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED25519)
     1371        && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED25519)
     1372        && OPENSSL_s390xcap_P.kdsa[0]
     1373            & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED25519))
     1374        return &ed25519_s390x_pkey_meth;
     1375#endif
     1376    return &ed25519_pkey_meth;
     1377}
     1378
     1379const EVP_PKEY_METHOD *ossl_ed448_pkey_method(void)
     1380{
     1381#ifdef S390X_EC_ASM
     1382    if (OPENSSL_s390xcap_P.pcc[1] & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_ED448)
     1383        && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_SIGN_ED448)
     1384        && OPENSSL_s390xcap_P.kdsa[0] & S390X_CAPBIT(S390X_EDDSA_VERIFY_ED448))
     1385        return &ed448_s390x_pkey_meth;
     1386#endif
     1387    return &ed448_pkey_meth;
     1388}
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette