OpenSSL 1.1.1-pre2
This commit is contained in:
+10
-17
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -17,9 +17,6 @@
|
||||
static int b64_write(BIO *h, const char *buf, int num);
|
||||
static int b64_read(BIO *h, char *buf, int size);
|
||||
static int b64_puts(BIO *h, const char *str);
|
||||
/*
|
||||
* static int b64_gets(BIO *h, char *str, int size);
|
||||
*/
|
||||
static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
||||
static int b64_new(BIO *h);
|
||||
static int b64_free(BIO *data);
|
||||
@@ -49,7 +46,11 @@ typedef struct b64_struct {
|
||||
static const BIO_METHOD methods_b64 = {
|
||||
BIO_TYPE_BASE64,
|
||||
"base64 encoding",
|
||||
/* TODO: Convert to new style write function */
|
||||
bwrite_conv,
|
||||
b64_write,
|
||||
/* TODO: Convert to new style read function */
|
||||
bread_conv,
|
||||
b64_read,
|
||||
b64_puts,
|
||||
NULL, /* b64_gets, */
|
||||
@@ -113,7 +114,7 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||
BIO *next;
|
||||
|
||||
if (out == NULL)
|
||||
return (0);
|
||||
return 0;
|
||||
ctx = (BIO_B64_CTX *)BIO_get_data(b);
|
||||
|
||||
next = BIO_next(b);
|
||||
@@ -288,14 +289,6 @@ static int b64_read(BIO *b, char *out, int outl)
|
||||
(unsigned char *)ctx->tmp, i);
|
||||
ctx->tmp_len = 0;
|
||||
}
|
||||
/*
|
||||
* If eof or an error was signalled, then the condition
|
||||
* 'ctx->cont <= 0' will prevent b64_read() from reading
|
||||
* more data on subsequent calls. This assignment was
|
||||
* deleted accidentally in commit 5562cfaca4f3.
|
||||
*/
|
||||
ctx->cont = i;
|
||||
|
||||
ctx->buf_off = 0;
|
||||
if (i < 0) {
|
||||
ret_code = 0;
|
||||
@@ -354,7 +347,7 @@ static int b64_write(BIO *b, const char *in, int inl)
|
||||
i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
|
||||
if (i <= 0) {
|
||||
BIO_copy_next_retry(b);
|
||||
return (i);
|
||||
return i;
|
||||
}
|
||||
OPENSSL_assert(i <= n);
|
||||
ctx->buf_off += i;
|
||||
@@ -367,7 +360,7 @@ static int b64_write(BIO *b, const char *in, int inl)
|
||||
ctx->buf_len = 0;
|
||||
|
||||
if ((in == NULL) || (inl <= 0))
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
while (inl > 0) {
|
||||
n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
|
||||
@@ -440,7 +433,7 @@ static int b64_write(BIO *b, const char *in, int inl)
|
||||
ctx->buf_len = 0;
|
||||
ctx->buf_off = 0;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||
@@ -542,7 +535,7 @@ static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
|
||||
ret = BIO_callback_ctrl(next, cmd, fp);
|
||||
break;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int b64_puts(BIO *b, const char *str)
|
||||
|
||||
+12
-34
@@ -16,12 +16,6 @@
|
||||
|
||||
static int enc_write(BIO *h, const char *buf, int num);
|
||||
static int enc_read(BIO *h, char *buf, int size);
|
||||
/*
|
||||
* static int enc_puts(BIO *h, const char *str);
|
||||
*/
|
||||
/*
|
||||
* static int enc_gets(BIO *h, char *str, int size);
|
||||
*/
|
||||
static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
||||
static int enc_new(BIO *h);
|
||||
static int enc_free(BIO *data);
|
||||
@@ -48,7 +42,11 @@ typedef struct enc_struct {
|
||||
static const BIO_METHOD methods_enc = {
|
||||
BIO_TYPE_CIPHER,
|
||||
"cipher",
|
||||
/* TODO: Convert to new style write function */
|
||||
bwrite_conv,
|
||||
enc_write,
|
||||
/* TODO: Convert to new style read function */
|
||||
bread_conv,
|
||||
enc_read,
|
||||
NULL, /* enc_puts, */
|
||||
NULL, /* enc_gets, */
|
||||
@@ -60,7 +58,7 @@ static const BIO_METHOD methods_enc = {
|
||||
|
||||
const BIO_METHOD *BIO_f_cipher(void)
|
||||
{
|
||||
return (&methods_enc);
|
||||
return &methods_enc;
|
||||
}
|
||||
|
||||
static int enc_new(BIO *bi)
|
||||
@@ -111,7 +109,7 @@ static int enc_read(BIO *b, char *out, int outl)
|
||||
BIO *next;
|
||||
|
||||
if (out == NULL)
|
||||
return (0);
|
||||
return 0;
|
||||
ctx = BIO_get_data(b);
|
||||
|
||||
next = BIO_next(b);
|
||||
@@ -251,7 +249,7 @@ static int enc_write(BIO *b, const char *in, int inl)
|
||||
i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
|
||||
if (i <= 0) {
|
||||
BIO_copy_next_retry(b);
|
||||
return (i);
|
||||
return i;
|
||||
}
|
||||
ctx->buf_off += i;
|
||||
n -= i;
|
||||
@@ -259,7 +257,7 @@ static int enc_write(BIO *b, const char *in, int inl)
|
||||
/* at this point all pending data has been written */
|
||||
|
||||
if ((in == NULL) || (inl <= 0))
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
ctx->buf_off = 0;
|
||||
while (inl > 0) {
|
||||
@@ -289,7 +287,7 @@ static int enc_write(BIO *b, const char *in, int inl)
|
||||
ctx->buf_off = 0;
|
||||
}
|
||||
BIO_copy_next_retry(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||
@@ -384,7 +382,7 @@ static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||
ret = BIO_ctrl(next, cmd, num, ptr);
|
||||
break;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
|
||||
@@ -393,35 +391,15 @@ static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
|
||||
BIO *next = BIO_next(b);
|
||||
|
||||
if (next == NULL)
|
||||
return (0);
|
||||
return 0;
|
||||
switch (cmd) {
|
||||
default:
|
||||
ret = BIO_callback_ctrl(next, cmd, fp);
|
||||
break;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*-
|
||||
void BIO_set_cipher_ctx(b,c)
|
||||
BIO *b;
|
||||
EVP_CIPHER_ctx *c;
|
||||
{
|
||||
if (b == NULL) return;
|
||||
|
||||
if ((b->callback != NULL) &&
|
||||
(b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
|
||||
return;
|
||||
|
||||
b->init=1;
|
||||
ctx=(BIO_ENC_CTX *)b->ptr;
|
||||
memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
|
||||
|
||||
if (b->callback != NULL)
|
||||
b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
|
||||
}
|
||||
*/
|
||||
|
||||
int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
|
||||
const unsigned char *i, int e)
|
||||
{
|
||||
|
||||
+14
-13
@@ -22,9 +22,6 @@
|
||||
|
||||
static int md_write(BIO *h, char const *buf, int num);
|
||||
static int md_read(BIO *h, char *buf, int size);
|
||||
/*
|
||||
* static int md_puts(BIO *h, const char *str);
|
||||
*/
|
||||
static int md_gets(BIO *h, char *str, int size);
|
||||
static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
||||
static int md_new(BIO *h);
|
||||
@@ -34,7 +31,11 @@ static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
|
||||
static const BIO_METHOD methods_md = {
|
||||
BIO_TYPE_MD,
|
||||
"message digest",
|
||||
/* TODO: Convert to new style write function */
|
||||
bwrite_conv,
|
||||
md_write,
|
||||
/* TODO: Convert to new style read function */
|
||||
bread_conv,
|
||||
md_read,
|
||||
NULL, /* md_puts, */
|
||||
md_gets,
|
||||
@@ -46,7 +47,7 @@ static const BIO_METHOD methods_md = {
|
||||
|
||||
const BIO_METHOD *BIO_f_md(void)
|
||||
{
|
||||
return (&methods_md);
|
||||
return &methods_md;
|
||||
}
|
||||
|
||||
static int md_new(BIO *bi)
|
||||
@@ -55,7 +56,7 @@ static int md_new(BIO *bi)
|
||||
|
||||
ctx = EVP_MD_CTX_new();
|
||||
if (ctx == NULL)
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
BIO_set_init(bi, 1);
|
||||
BIO_set_data(bi, ctx);
|
||||
@@ -66,7 +67,7 @@ static int md_new(BIO *bi)
|
||||
static int md_free(BIO *a)
|
||||
{
|
||||
if (a == NULL)
|
||||
return (0);
|
||||
return 0;
|
||||
EVP_MD_CTX_free(BIO_get_data(a));
|
||||
BIO_set_data(a, NULL);
|
||||
BIO_set_init(a, 0);
|
||||
@@ -81,25 +82,25 @@ static int md_read(BIO *b, char *out, int outl)
|
||||
BIO *next;
|
||||
|
||||
if (out == NULL)
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
ctx = BIO_get_data(b);
|
||||
next = BIO_next(b);
|
||||
|
||||
if ((ctx == NULL) || (next == NULL))
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
ret = BIO_read(next, out, outl);
|
||||
if (BIO_get_init(b)) {
|
||||
if (ret > 0) {
|
||||
if (EVP_DigestUpdate(ctx, (unsigned char *)out,
|
||||
(unsigned int)ret) <= 0)
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
BIO_clear_retry_flags(b);
|
||||
BIO_copy_next_retry(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int md_write(BIO *b, const char *in, int inl)
|
||||
@@ -194,7 +195,7 @@ static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||
ret = BIO_ctrl(next, cmd, num, ptr);
|
||||
break;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
|
||||
@@ -212,7 +213,7 @@ static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
|
||||
ret = BIO_callback_ctrl(next, cmd, fp);
|
||||
break;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int md_gets(BIO *bp, char *buf, int size)
|
||||
@@ -228,5 +229,5 @@ static int md_gets(BIO *bp, char *buf, int size)
|
||||
if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
|
||||
return -1;
|
||||
|
||||
return ((int)ret);
|
||||
return (int)ret;
|
||||
}
|
||||
+10
-6
@@ -8,7 +8,7 @@
|
||||
*/
|
||||
|
||||
/*-
|
||||
From: Arne Ansper <arne@cyber.ee>
|
||||
From: Arne Ansper
|
||||
|
||||
Why BIO_f_reliable?
|
||||
|
||||
@@ -110,7 +110,11 @@ typedef struct ok_struct {
|
||||
static const BIO_METHOD methods_ok = {
|
||||
BIO_TYPE_CIPHER,
|
||||
"reliable",
|
||||
/* TODO: Convert to new style write function */
|
||||
bwrite_conv,
|
||||
ok_write,
|
||||
/* TODO: Convert to new style read function */
|
||||
bread_conv,
|
||||
ok_read,
|
||||
NULL, /* ok_puts, */
|
||||
NULL, /* ok_gets, */
|
||||
@@ -122,7 +126,7 @@ static const BIO_METHOD methods_ok = {
|
||||
|
||||
const BIO_METHOD *BIO_f_reliable(void)
|
||||
{
|
||||
return (&methods_ok);
|
||||
return &methods_ok;
|
||||
}
|
||||
|
||||
static int ok_new(BIO *bi)
|
||||
@@ -263,7 +267,7 @@ static int ok_write(BIO *b, const char *in, int inl)
|
||||
ret = inl;
|
||||
|
||||
if ((ctx == NULL) || (next == NULL) || (BIO_get_init(b) == 0))
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
if (ctx->sigio && !sig_out(b))
|
||||
return 0;
|
||||
@@ -277,7 +281,7 @@ static int ok_write(BIO *b, const char *in, int inl)
|
||||
BIO_copy_next_retry(b);
|
||||
if (!BIO_should_retry(b))
|
||||
ctx->cont = 0;
|
||||
return (i);
|
||||
return i;
|
||||
}
|
||||
ctx->buf_off += i;
|
||||
n -= i;
|
||||
@@ -291,7 +295,7 @@ static int ok_write(BIO *b, const char *in, int inl)
|
||||
}
|
||||
|
||||
if ((in == NULL) || (inl <= 0))
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ?
|
||||
(int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl;
|
||||
@@ -311,7 +315,7 @@ static int ok_write(BIO *b, const char *in, int inl)
|
||||
|
||||
BIO_clear_retry_flags(b);
|
||||
BIO_copy_next_retry(b);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static long ok_ctrl(BIO *b, int cmd, long num, void *ptr)
|
||||
|
||||
@@ -2,14 +2,14 @@ LIBS=../../libcrypto
|
||||
SOURCE[../../libcrypto]=\
|
||||
encode.c digest.c evp_enc.c evp_key.c evp_cnf.c \
|
||||
e_des.c e_bf.c e_idea.c e_des3.c e_camellia.c\
|
||||
e_rc4.c e_aes.c names.c e_seed.c \
|
||||
e_rc4.c e_aes.c names.c e_seed.c e_aria.c e_sm4.c \
|
||||
e_xcbc_d.c e_rc2.c e_cast.c e_rc5.c \
|
||||
m_null.c m_md2.c m_md4.c m_md5.c m_sha1.c m_wp.c \
|
||||
m_md5_sha1.c m_mdc2.c m_ripemd.c \
|
||||
m_md5_sha1.c m_mdc2.c m_ripemd.c m_sha3.c \
|
||||
p_open.c p_seal.c p_sign.c p_verify.c p_lib.c p_enc.c p_dec.c \
|
||||
bio_md.c bio_b64.c bio_enc.c evp_err.c e_null.c \
|
||||
c_allc.c c_alld.c evp_lib.c bio_ok.c \
|
||||
evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c scrypt.c \
|
||||
evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c pbe_scrypt.c \
|
||||
e_old.c pmeth_lib.c pmeth_fn.c pmeth_gn.c m_sigver.c \
|
||||
e_aes_cbc_hmac_sha1.c e_aes_cbc_hmac_sha256.c e_rc4_hmac_md5.c \
|
||||
e_chacha20_poly1305.c cmeth_lib.c
|
||||
@@ -17,6 +17,8 @@ SOURCE[../../libcrypto]=\
|
||||
INCLUDE[e_aes.o]=.. ../modes
|
||||
INCLUDE[e_aes_cbc_hmac_sha1.o]=../modes
|
||||
INCLUDE[e_aes_cbc_hmac_sha256.o]=../modes
|
||||
INCLUDE[e_aria.o]=.. ../modes
|
||||
INCLUDE[e_camellia.o]=.. ../modes
|
||||
INCLUDE[e_sm4.o]=.. ../modes
|
||||
INCLUDE[e_des.o]=..
|
||||
INCLUDE[e_des3.o]=..
|
||||
+48
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <stdio.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include <openssl/evp.h>
|
||||
#include <internal/evp_int.h>
|
||||
#include "internal/evp_int.h"
|
||||
#include <openssl/pkcs12.h>
|
||||
#include <openssl/objects.h>
|
||||
|
||||
@@ -79,6 +79,16 @@ void openssl_add_all_ciphers_int(void)
|
||||
EVP_add_cipher_alias(SN_seed_cbc, "seed");
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_SM4
|
||||
EVP_add_cipher(EVP_sm4_ecb());
|
||||
EVP_add_cipher(EVP_sm4_cbc());
|
||||
EVP_add_cipher(EVP_sm4_cfb());
|
||||
EVP_add_cipher(EVP_sm4_ofb());
|
||||
EVP_add_cipher(EVP_sm4_ctr());
|
||||
EVP_add_cipher_alias(SN_sm4_cbc, "SM4");
|
||||
EVP_add_cipher_alias(SN_sm4_cbc, "sm4");
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_RC2
|
||||
EVP_add_cipher(EVP_rc2_ecb());
|
||||
EVP_add_cipher(EVP_rc2_cfb());
|
||||
@@ -181,6 +191,42 @@ void openssl_add_all_ciphers_int(void)
|
||||
EVP_add_cipher(EVP_aes_128_cbc_hmac_sha256());
|
||||
EVP_add_cipher(EVP_aes_256_cbc_hmac_sha256());
|
||||
|
||||
#ifndef OPENSSL_NO_ARIA
|
||||
EVP_add_cipher(EVP_aria_128_ecb());
|
||||
EVP_add_cipher(EVP_aria_128_cbc());
|
||||
EVP_add_cipher(EVP_aria_128_cfb());
|
||||
EVP_add_cipher(EVP_aria_128_cfb1());
|
||||
EVP_add_cipher(EVP_aria_128_cfb8());
|
||||
EVP_add_cipher(EVP_aria_128_ctr());
|
||||
EVP_add_cipher(EVP_aria_128_ofb());
|
||||
EVP_add_cipher(EVP_aria_128_gcm());
|
||||
EVP_add_cipher(EVP_aria_128_ccm());
|
||||
EVP_add_cipher_alias(SN_aria_128_cbc, "ARIA128");
|
||||
EVP_add_cipher_alias(SN_aria_128_cbc, "aria128");
|
||||
EVP_add_cipher(EVP_aria_192_ecb());
|
||||
EVP_add_cipher(EVP_aria_192_cbc());
|
||||
EVP_add_cipher(EVP_aria_192_cfb());
|
||||
EVP_add_cipher(EVP_aria_192_cfb1());
|
||||
EVP_add_cipher(EVP_aria_192_cfb8());
|
||||
EVP_add_cipher(EVP_aria_192_ctr());
|
||||
EVP_add_cipher(EVP_aria_192_ofb());
|
||||
EVP_add_cipher(EVP_aria_192_gcm());
|
||||
EVP_add_cipher(EVP_aria_192_ccm());
|
||||
EVP_add_cipher_alias(SN_aria_192_cbc, "ARIA192");
|
||||
EVP_add_cipher_alias(SN_aria_192_cbc, "aria192");
|
||||
EVP_add_cipher(EVP_aria_256_ecb());
|
||||
EVP_add_cipher(EVP_aria_256_cbc());
|
||||
EVP_add_cipher(EVP_aria_256_cfb());
|
||||
EVP_add_cipher(EVP_aria_256_cfb1());
|
||||
EVP_add_cipher(EVP_aria_256_cfb8());
|
||||
EVP_add_cipher(EVP_aria_256_ctr());
|
||||
EVP_add_cipher(EVP_aria_256_ofb());
|
||||
EVP_add_cipher(EVP_aria_256_gcm());
|
||||
EVP_add_cipher(EVP_aria_256_ccm());
|
||||
EVP_add_cipher_alias(SN_aria_256_cbc, "ARIA256");
|
||||
EVP_add_cipher_alias(SN_aria_256_cbc, "aria256");
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_CAMELLIA
|
||||
EVP_add_cipher(EVP_camellia_128_ecb());
|
||||
EVP_add_cipher(EVP_camellia_128_cbc());
|
||||
|
||||
+13
-2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -10,7 +10,7 @@
|
||||
#include <stdio.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include <openssl/evp.h>
|
||||
#include <internal/evp_int.h>
|
||||
#include "internal/evp_int.h"
|
||||
#include <openssl/pkcs12.h>
|
||||
#include <openssl/objects.h>
|
||||
|
||||
@@ -39,11 +39,22 @@ void openssl_add_all_digests_int(void)
|
||||
EVP_add_digest(EVP_sha256());
|
||||
EVP_add_digest(EVP_sha384());
|
||||
EVP_add_digest(EVP_sha512());
|
||||
EVP_add_digest(EVP_sha512_224());
|
||||
EVP_add_digest(EVP_sha512_256());
|
||||
#ifndef OPENSSL_NO_WHIRLPOOL
|
||||
EVP_add_digest(EVP_whirlpool());
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_SM3
|
||||
EVP_add_digest(EVP_sm3());
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_BLAKE2
|
||||
EVP_add_digest(EVP_blake2b512());
|
||||
EVP_add_digest(EVP_blake2s256());
|
||||
#endif
|
||||
EVP_add_digest(EVP_sha3_224());
|
||||
EVP_add_digest(EVP_sha3_256());
|
||||
EVP_add_digest(EVP_sha3_384());
|
||||
EVP_add_digest(EVP_sha3_512());
|
||||
EVP_add_digest(EVP_shake128());
|
||||
EVP_add_digest(EVP_shake256());
|
||||
}
|
||||
@@ -174,6 +174,27 @@ int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t size)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (ctx->digest->flags & EVP_MD_FLAG_XOF
|
||||
&& size <= INT_MAX
|
||||
&& ctx->digest->md_ctrl(ctx, EVP_MD_CTRL_XOF_LEN, (int)size, NULL)) {
|
||||
ret = ctx->digest->final(ctx, md);
|
||||
|
||||
if (ctx->digest->cleanup != NULL) {
|
||||
ctx->digest->cleanup(ctx);
|
||||
EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_CLEANED);
|
||||
}
|
||||
OPENSSL_cleanse(ctx->md_data, ctx->digest->ctx_size);
|
||||
} else {
|
||||
EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in)
|
||||
{
|
||||
EVP_MD_CTX_reset(out);
|
||||
|
||||
+1287
-5
@@ -136,10 +136,10 @@ void AES_ctr32_encrypt(const unsigned char *in, unsigned char *out,
|
||||
const unsigned char ivec[AES_BLOCK_SIZE]);
|
||||
#endif
|
||||
#ifdef AES_XTS_ASM
|
||||
void AES_xts_encrypt(const char *inp, char *out, size_t len,
|
||||
void AES_xts_encrypt(const unsigned char *inp, unsigned char *out, size_t len,
|
||||
const AES_KEY *key1, const AES_KEY *key2,
|
||||
const unsigned char iv[16]);
|
||||
void AES_xts_decrypt(const char *inp, char *out, size_t len,
|
||||
void AES_xts_decrypt(const unsigned char *inp, unsigned char *out, size_t len,
|
||||
const AES_KEY *key1, const AES_KEY *key2,
|
||||
const unsigned char iv[16]);
|
||||
#endif
|
||||
@@ -950,6 +950,1287 @@ static const EVP_CIPHER aes_##keylen##_##mode = { \
|
||||
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
|
||||
{ return SPARC_AES_CAPABLE?&aes_t4_##keylen##_##mode:&aes_##keylen##_##mode; }
|
||||
|
||||
#elif defined(OPENSSL_CPUID_OBJ) && defined(__s390__)
|
||||
/*
|
||||
* IBM S390X support
|
||||
*/
|
||||
# include "s390x_arch.h"
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
/*-
|
||||
* KMA-GCM-AES parameter block - begin
|
||||
* (see z/Architecture Principles of Operation >= SA22-7832-11)
|
||||
*/
|
||||
struct {
|
||||
unsigned char reserved[12];
|
||||
union {
|
||||
unsigned int w;
|
||||
unsigned char b[4];
|
||||
} cv;
|
||||
union {
|
||||
unsigned long long g[2];
|
||||
unsigned char b[16];
|
||||
} t;
|
||||
unsigned char h[16];
|
||||
unsigned long long taadl;
|
||||
unsigned long long tpcl;
|
||||
union {
|
||||
unsigned long long g[2];
|
||||
unsigned int w[4];
|
||||
} j0;
|
||||
unsigned char k[32];
|
||||
} param;
|
||||
/* KMA-GCM-AES parameter block - end */
|
||||
} kma;
|
||||
unsigned int fc;
|
||||
int key_set;
|
||||
|
||||
unsigned char *iv;
|
||||
int ivlen;
|
||||
int iv_set;
|
||||
int iv_gen;
|
||||
|
||||
int taglen;
|
||||
|
||||
unsigned char ares[16];
|
||||
unsigned char mres[16];
|
||||
unsigned char kres[16];
|
||||
int areslen;
|
||||
int mreslen;
|
||||
int kreslen;
|
||||
|
||||
int tls_aad_len;
|
||||
} S390X_AES_GCM_CTX;
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
/*-
|
||||
* Padding is chosen so that ccm.kmac_param.k overlaps with key.k and
|
||||
* ccm.fc with key.k.rounds. Remember that on s390x, an AES_KEY's
|
||||
* rounds field is used to store the function code and that the key
|
||||
* schedule is not stored (if aes hardware support is detected).
|
||||
*/
|
||||
struct {
|
||||
unsigned char pad[16];
|
||||
AES_KEY k;
|
||||
} key;
|
||||
|
||||
struct {
|
||||
/*-
|
||||
* KMAC-AES parameter block - begin
|
||||
* (see z/Architecture Principles of Operation >= SA22-7832-08)
|
||||
*/
|
||||
struct {
|
||||
union {
|
||||
unsigned long long g[2];
|
||||
unsigned char b[16];
|
||||
} icv;
|
||||
unsigned char k[32];
|
||||
} kmac_param;
|
||||
/* KMAC-AES paramater block - end */
|
||||
|
||||
union {
|
||||
unsigned long long g[2];
|
||||
unsigned char b[16];
|
||||
} nonce;
|
||||
union {
|
||||
unsigned long long g[2];
|
||||
unsigned char b[16];
|
||||
} buf;
|
||||
|
||||
unsigned long long blocks;
|
||||
int l;
|
||||
int m;
|
||||
int tls_aad_len;
|
||||
int iv_set;
|
||||
int tag_set;
|
||||
int len_set;
|
||||
int key_set;
|
||||
|
||||
unsigned char pad[140];
|
||||
unsigned int fc;
|
||||
} ccm;
|
||||
} aes;
|
||||
} S390X_AES_CCM_CTX;
|
||||
|
||||
# define S390X_aes_128_CAPABLE ((OPENSSL_s390xcap_P.km[0] & \
|
||||
S390X_CAPBIT(S390X_AES_128)) &&\
|
||||
(OPENSSL_s390xcap_P.kmc[0] & \
|
||||
S390X_CAPBIT(S390X_AES_128)))
|
||||
# define S390X_aes_192_CAPABLE ((OPENSSL_s390xcap_P.km[0] & \
|
||||
S390X_CAPBIT(S390X_AES_192)) &&\
|
||||
(OPENSSL_s390xcap_P.kmc[0] & \
|
||||
S390X_CAPBIT(S390X_AES_192)))
|
||||
# define S390X_aes_256_CAPABLE ((OPENSSL_s390xcap_P.km[0] & \
|
||||
S390X_CAPBIT(S390X_AES_256)) &&\
|
||||
(OPENSSL_s390xcap_P.kmc[0] & \
|
||||
S390X_CAPBIT(S390X_AES_256)))
|
||||
|
||||
# define s390x_aes_init_key aes_init_key
|
||||
static int s390x_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc);
|
||||
|
||||
# define S390X_aes_128_cbc_CAPABLE 1 /* checked by callee */
|
||||
# define S390X_aes_192_cbc_CAPABLE 1
|
||||
# define S390X_aes_256_cbc_CAPABLE 1
|
||||
|
||||
# define s390x_aes_cbc_cipher aes_cbc_cipher
|
||||
static int s390x_aes_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
|
||||
# define S390X_aes_128_ecb_CAPABLE 0
|
||||
# define S390X_aes_192_ecb_CAPABLE 0
|
||||
# define S390X_aes_256_ecb_CAPABLE 0
|
||||
|
||||
# define s390x_aes_ecb_cipher aes_ecb_cipher
|
||||
static int s390x_aes_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
|
||||
# define S390X_aes_128_ofb_CAPABLE 0
|
||||
# define S390X_aes_192_ofb_CAPABLE 0
|
||||
# define S390X_aes_256_ofb_CAPABLE 0
|
||||
|
||||
# define s390x_aes_ofb_cipher aes_ofb_cipher
|
||||
static int s390x_aes_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
|
||||
# define S390X_aes_128_cfb_CAPABLE 0
|
||||
# define S390X_aes_192_cfb_CAPABLE 0
|
||||
# define S390X_aes_256_cfb_CAPABLE 0
|
||||
|
||||
# define s390x_aes_cfb_cipher aes_cfb_cipher
|
||||
static int s390x_aes_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
|
||||
# define S390X_aes_128_cfb8_CAPABLE 0
|
||||
# define S390X_aes_192_cfb8_CAPABLE 0
|
||||
# define S390X_aes_256_cfb8_CAPABLE 0
|
||||
|
||||
# define s390x_aes_cfb8_cipher aes_cfb8_cipher
|
||||
static int s390x_aes_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
|
||||
# define S390X_aes_128_cfb1_CAPABLE 0
|
||||
# define S390X_aes_192_cfb1_CAPABLE 0
|
||||
# define S390X_aes_256_cfb1_CAPABLE 0
|
||||
|
||||
# define s390x_aes_cfb1_cipher aes_cfb1_cipher
|
||||
static int s390x_aes_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
|
||||
# define S390X_aes_128_ctr_CAPABLE 1 /* checked by callee */
|
||||
# define S390X_aes_192_ctr_CAPABLE 1
|
||||
# define S390X_aes_256_ctr_CAPABLE 1
|
||||
|
||||
# define s390x_aes_ctr_cipher aes_ctr_cipher
|
||||
static int s390x_aes_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
|
||||
# define S390X_aes_128_gcm_CAPABLE (S390X_aes_128_CAPABLE && \
|
||||
(OPENSSL_s390xcap_P.kma[0] & \
|
||||
S390X_CAPBIT(S390X_AES_128)))
|
||||
# define S390X_aes_192_gcm_CAPABLE (S390X_aes_192_CAPABLE && \
|
||||
(OPENSSL_s390xcap_P.kma[0] & \
|
||||
S390X_CAPBIT(S390X_AES_192)))
|
||||
# define S390X_aes_256_gcm_CAPABLE (S390X_aes_256_CAPABLE && \
|
||||
(OPENSSL_s390xcap_P.kma[0] & \
|
||||
S390X_CAPBIT(S390X_AES_256)))
|
||||
|
||||
/* iv + padding length for iv lenghts != 12 */
|
||||
# define S390X_gcm_ivpadlen(i) ((((i) + 15) >> 4 << 4) + 16)
|
||||
|
||||
/*-
|
||||
* Process additional authenticated data. Returns 0 on success. Code is
|
||||
* big-endian.
|
||||
*/
|
||||
static int s390x_aes_gcm_aad(S390X_AES_GCM_CTX *ctx, const unsigned char *aad,
|
||||
size_t len)
|
||||
{
|
||||
unsigned long long alen;
|
||||
int n, rem;
|
||||
|
||||
if (ctx->kma.param.tpcl)
|
||||
return -2;
|
||||
|
||||
alen = ctx->kma.param.taadl + len;
|
||||
if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
|
||||
return -1;
|
||||
ctx->kma.param.taadl = alen;
|
||||
|
||||
n = ctx->areslen;
|
||||
if (n) {
|
||||
while (n && len) {
|
||||
ctx->ares[n] = *aad;
|
||||
n = (n + 1) & 0xf;
|
||||
++aad;
|
||||
--len;
|
||||
}
|
||||
/* ctx->ares contains a complete block if offset has wrapped around */
|
||||
if (!n) {
|
||||
s390x_kma(ctx->ares, 16, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
|
||||
ctx->fc |= S390X_KMA_HS;
|
||||
}
|
||||
ctx->areslen = n;
|
||||
}
|
||||
|
||||
rem = len & 0xf;
|
||||
|
||||
len &= ~0xf;
|
||||
if (len) {
|
||||
s390x_kma(aad, len, NULL, 0, NULL, ctx->fc, &ctx->kma.param);
|
||||
aad += len;
|
||||
ctx->fc |= S390X_KMA_HS;
|
||||
}
|
||||
|
||||
if (rem) {
|
||||
ctx->areslen = rem;
|
||||
|
||||
do {
|
||||
--rem;
|
||||
ctx->ares[rem] = aad[rem];
|
||||
} while (rem);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-
|
||||
* En/de-crypt plain/cipher-text and authenticate ciphertext. Returns 0 for
|
||||
* success. Code is big-endian.
|
||||
*/
|
||||
static int s390x_aes_gcm(S390X_AES_GCM_CTX *ctx, const unsigned char *in,
|
||||
unsigned char *out, size_t len)
|
||||
{
|
||||
const unsigned char *inptr;
|
||||
unsigned long long mlen;
|
||||
union {
|
||||
unsigned int w[4];
|
||||
unsigned char b[16];
|
||||
} buf;
|
||||
size_t inlen;
|
||||
int n, rem, i;
|
||||
|
||||
mlen = ctx->kma.param.tpcl + len;
|
||||
if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
|
||||
return -1;
|
||||
ctx->kma.param.tpcl = mlen;
|
||||
|
||||
n = ctx->mreslen;
|
||||
if (n) {
|
||||
inptr = in;
|
||||
inlen = len;
|
||||
while (n && inlen) {
|
||||
ctx->mres[n] = *inptr;
|
||||
n = (n + 1) & 0xf;
|
||||
++inptr;
|
||||
--inlen;
|
||||
}
|
||||
/* ctx->mres contains a complete block if offset has wrapped around */
|
||||
if (!n) {
|
||||
s390x_kma(ctx->ares, ctx->areslen, ctx->mres, 16, buf.b,
|
||||
ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
|
||||
ctx->fc |= S390X_KMA_HS;
|
||||
ctx->areslen = 0;
|
||||
|
||||
/* previous call already encrypted/decrypted its remainder,
|
||||
* see comment below */
|
||||
n = ctx->mreslen;
|
||||
while (n) {
|
||||
*out = buf.b[n];
|
||||
n = (n + 1) & 0xf;
|
||||
++out;
|
||||
++in;
|
||||
--len;
|
||||
}
|
||||
ctx->mreslen = 0;
|
||||
}
|
||||
}
|
||||
|
||||
rem = len & 0xf;
|
||||
|
||||
len &= ~0xf;
|
||||
if (len) {
|
||||
s390x_kma(ctx->ares, ctx->areslen, in, len, out,
|
||||
ctx->fc | S390X_KMA_LAAD, &ctx->kma.param);
|
||||
in += len;
|
||||
out += len;
|
||||
ctx->fc |= S390X_KMA_HS;
|
||||
ctx->areslen = 0;
|
||||
}
|
||||
|
||||
/*-
|
||||
* If there is a remainder, it has to be saved such that it can be
|
||||
* processed by kma later. However, we also have to do the for-now
|
||||
* unauthenticated encryption/decryption part here and now...
|
||||
*/
|
||||
if (rem) {
|
||||
if (!ctx->mreslen) {
|
||||
buf.w[0] = ctx->kma.param.j0.w[0];
|
||||
buf.w[1] = ctx->kma.param.j0.w[1];
|
||||
buf.w[2] = ctx->kma.param.j0.w[2];
|
||||
buf.w[3] = ctx->kma.param.cv.w + 1;
|
||||
s390x_km(buf.b, 16, ctx->kres, ctx->fc & 0x1f, &ctx->kma.param.k);
|
||||
}
|
||||
|
||||
n = ctx->mreslen;
|
||||
for (i = 0; i < rem; i++) {
|
||||
ctx->mres[n + i] = in[i];
|
||||
out[i] = in[i] ^ ctx->kres[n + i];
|
||||
}
|
||||
|
||||
ctx->mreslen += rem;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-
|
||||
* Initialize context structure. Code is big-endian.
|
||||
*/
|
||||
static void s390x_aes_gcm_setiv(S390X_AES_GCM_CTX *ctx,
|
||||
const unsigned char *iv)
|
||||
{
|
||||
ctx->kma.param.t.g[0] = 0;
|
||||
ctx->kma.param.t.g[1] = 0;
|
||||
ctx->kma.param.tpcl = 0;
|
||||
ctx->kma.param.taadl = 0;
|
||||
ctx->mreslen = 0;
|
||||
ctx->areslen = 0;
|
||||
ctx->kreslen = 0;
|
||||
|
||||
if (ctx->ivlen == 12) {
|
||||
memcpy(&ctx->kma.param.j0, iv, ctx->ivlen);
|
||||
ctx->kma.param.j0.w[3] = 1;
|
||||
ctx->kma.param.cv.w = 1;
|
||||
} else {
|
||||
/* ctx->iv has the right size and is already padded. */
|
||||
memcpy(ctx->iv, iv, ctx->ivlen);
|
||||
s390x_kma(ctx->iv, S390X_gcm_ivpadlen(ctx->ivlen), NULL, 0, NULL,
|
||||
ctx->fc, &ctx->kma.param);
|
||||
ctx->fc |= S390X_KMA_HS;
|
||||
|
||||
ctx->kma.param.j0.g[0] = ctx->kma.param.t.g[0];
|
||||
ctx->kma.param.j0.g[1] = ctx->kma.param.t.g[1];
|
||||
ctx->kma.param.cv.w = ctx->kma.param.j0.w[3];
|
||||
ctx->kma.param.t.g[0] = 0;
|
||||
ctx->kma.param.t.g[1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* Performs various operations on the context structure depending on control
|
||||
* type. Returns 1 for success, 0 for failure and -1 for unknown control type.
|
||||
* Code is big-endian.
|
||||
*/
|
||||
static int s390x_aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
{
|
||||
S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
|
||||
S390X_AES_GCM_CTX *gctx_out;
|
||||
EVP_CIPHER_CTX *out;
|
||||
unsigned char *buf, *iv;
|
||||
int ivlen, enc, len;
|
||||
|
||||
switch (type) {
|
||||
case EVP_CTRL_INIT:
|
||||
ivlen = EVP_CIPHER_CTX_iv_length(c);
|
||||
iv = EVP_CIPHER_CTX_iv_noconst(c);
|
||||
gctx->key_set = 0;
|
||||
gctx->iv_set = 0;
|
||||
gctx->ivlen = ivlen;
|
||||
gctx->iv = iv;
|
||||
gctx->taglen = -1;
|
||||
gctx->iv_gen = 0;
|
||||
gctx->tls_aad_len = -1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_IVLEN:
|
||||
if (arg <= 0)
|
||||
return 0;
|
||||
|
||||
if (arg != 12) {
|
||||
iv = EVP_CIPHER_CTX_iv_noconst(c);
|
||||
len = S390X_gcm_ivpadlen(arg);
|
||||
|
||||
/* Allocate memory for iv if needed. */
|
||||
if (gctx->ivlen == 12 || len > S390X_gcm_ivpadlen(gctx->ivlen)) {
|
||||
if (gctx->iv != iv)
|
||||
OPENSSL_free(gctx->iv);
|
||||
|
||||
gctx->iv = OPENSSL_malloc(len);
|
||||
if (gctx->iv == NULL)
|
||||
return 0;
|
||||
}
|
||||
/* Add padding. */
|
||||
memset(gctx->iv + arg, 0, len - arg - 8);
|
||||
*((unsigned long long *)(gctx->iv + len - 8)) = arg << 3;
|
||||
}
|
||||
gctx->ivlen = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_TAG:
|
||||
buf = EVP_CIPHER_CTX_buf_noconst(c);
|
||||
enc = EVP_CIPHER_CTX_encrypting(c);
|
||||
if (arg <= 0 || arg > 16 || enc)
|
||||
return 0;
|
||||
|
||||
memcpy(buf, ptr, arg);
|
||||
gctx->taglen = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_GET_TAG:
|
||||
enc = EVP_CIPHER_CTX_encrypting(c);
|
||||
if (arg <= 0 || arg > 16 || !enc || gctx->taglen < 0)
|
||||
return 0;
|
||||
|
||||
memcpy(ptr, gctx->kma.param.t.b, arg);
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_SET_IV_FIXED:
|
||||
/* Special case: -1 length restores whole iv */
|
||||
if (arg == -1) {
|
||||
memcpy(gctx->iv, ptr, gctx->ivlen);
|
||||
gctx->iv_gen = 1;
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
* Fixed field must be at least 4 bytes and invocation field at least
|
||||
* 8.
|
||||
*/
|
||||
if ((arg < 4) || (gctx->ivlen - arg) < 8)
|
||||
return 0;
|
||||
|
||||
if (arg)
|
||||
memcpy(gctx->iv, ptr, arg);
|
||||
|
||||
enc = EVP_CIPHER_CTX_encrypting(c);
|
||||
if (enc && RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
|
||||
return 0;
|
||||
|
||||
gctx->iv_gen = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_IV_GEN:
|
||||
if (gctx->iv_gen == 0 || gctx->key_set == 0)
|
||||
return 0;
|
||||
|
||||
s390x_aes_gcm_setiv(gctx, gctx->iv);
|
||||
|
||||
if (arg <= 0 || arg > gctx->ivlen)
|
||||
arg = gctx->ivlen;
|
||||
|
||||
memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
|
||||
/*
|
||||
* Invocation field will be at least 8 bytes in size and so no need
|
||||
* to check wrap around or increment more than last 8 bytes.
|
||||
*/
|
||||
(*(unsigned long long *)(gctx->iv + gctx->ivlen - 8))++;
|
||||
gctx->iv_set = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_SET_IV_INV:
|
||||
enc = EVP_CIPHER_CTX_encrypting(c);
|
||||
if (gctx->iv_gen == 0 || gctx->key_set == 0 || enc)
|
||||
return 0;
|
||||
|
||||
memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
|
||||
s390x_aes_gcm_setiv(gctx, gctx->iv);
|
||||
gctx->iv_set = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_TLS1_AAD:
|
||||
/* Save the aad for later use. */
|
||||
if (arg != EVP_AEAD_TLS1_AAD_LEN)
|
||||
return 0;
|
||||
|
||||
buf = EVP_CIPHER_CTX_buf_noconst(c);
|
||||
memcpy(buf, ptr, arg);
|
||||
gctx->tls_aad_len = arg;
|
||||
|
||||
len = buf[arg - 2] << 8 | buf[arg - 1];
|
||||
/* Correct length for explicit iv. */
|
||||
if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
|
||||
return 0;
|
||||
len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
|
||||
/* If decrypting correct for tag too. */
|
||||
enc = EVP_CIPHER_CTX_encrypting(c);
|
||||
if (!enc) {
|
||||
if (len < EVP_GCM_TLS_TAG_LEN)
|
||||
return 0;
|
||||
len -= EVP_GCM_TLS_TAG_LEN;
|
||||
}
|
||||
buf[arg - 2] = len >> 8;
|
||||
buf[arg - 1] = len & 0xff;
|
||||
/* Extra padding: tag appended to record. */
|
||||
return EVP_GCM_TLS_TAG_LEN;
|
||||
|
||||
case EVP_CTRL_COPY:
|
||||
out = ptr;
|
||||
gctx_out = EVP_C_DATA(S390X_AES_GCM_CTX, out);
|
||||
iv = EVP_CIPHER_CTX_iv_noconst(c);
|
||||
|
||||
if (gctx->iv == iv) {
|
||||
gctx_out->iv = EVP_CIPHER_CTX_iv_noconst(out);
|
||||
} else {
|
||||
len = S390X_gcm_ivpadlen(gctx->ivlen);
|
||||
|
||||
gctx_out->iv = OPENSSL_malloc(len);
|
||||
if (gctx_out->iv == NULL)
|
||||
return 0;
|
||||
|
||||
memcpy(gctx_out->iv, gctx->iv, len);
|
||||
}
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* Set key and/or iv. Returns 1 on success. Otherwise 0 is returned.
|
||||
*/
|
||||
static int s390x_aes_gcm_init_key(EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
|
||||
int keylen;
|
||||
|
||||
if (iv == NULL && key == NULL)
|
||||
return 1;
|
||||
|
||||
if (key != NULL) {
|
||||
keylen = EVP_CIPHER_CTX_key_length(ctx);
|
||||
memcpy(&gctx->kma.param.k, key, keylen);
|
||||
|
||||
/* Convert key size to function code. */
|
||||
gctx->fc = S390X_AES_128 + (((keylen << 3) - 128) >> 6);
|
||||
if (!enc)
|
||||
gctx->fc |= S390X_DECRYPT;
|
||||
|
||||
if (iv == NULL && gctx->iv_set)
|
||||
iv = gctx->iv;
|
||||
|
||||
if (iv != NULL) {
|
||||
s390x_aes_gcm_setiv(gctx, iv);
|
||||
gctx->iv_set = 1;
|
||||
}
|
||||
gctx->key_set = 1;
|
||||
} else {
|
||||
if (gctx->key_set)
|
||||
s390x_aes_gcm_setiv(gctx, iv);
|
||||
else
|
||||
memcpy(gctx->iv, iv, gctx->ivlen);
|
||||
|
||||
gctx->iv_set = 1;
|
||||
gctx->iv_gen = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-
|
||||
* En/de-crypt and authenticate TLS packet. Returns the number of bytes written
|
||||
* if successful. Otherwise -1 is returned. Code is big-endian.
|
||||
*/
|
||||
static int s390x_aes_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
|
||||
const unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
|
||||
const int enc = EVP_CIPHER_CTX_encrypting(ctx);
|
||||
int rv = -1;
|
||||
|
||||
if (out != in || len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
|
||||
return -1;
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, enc ? EVP_CTRL_GCM_IV_GEN
|
||||
: EVP_CTRL_GCM_SET_IV_INV,
|
||||
EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
|
||||
goto err;
|
||||
|
||||
in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
|
||||
|
||||
gctx->kma.param.taadl = gctx->tls_aad_len << 3;
|
||||
gctx->kma.param.tpcl = len << 3;
|
||||
s390x_kma(buf, gctx->tls_aad_len, in, len, out,
|
||||
gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
|
||||
|
||||
if (enc) {
|
||||
memcpy(out + len, gctx->kma.param.t.b, EVP_GCM_TLS_TAG_LEN);
|
||||
rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
|
||||
} else {
|
||||
if (CRYPTO_memcmp(gctx->kma.param.t.b, in + len,
|
||||
EVP_GCM_TLS_TAG_LEN)) {
|
||||
OPENSSL_cleanse(out, len);
|
||||
goto err;
|
||||
}
|
||||
rv = len;
|
||||
}
|
||||
err:
|
||||
gctx->iv_set = 0;
|
||||
gctx->tls_aad_len = -1;
|
||||
return rv;
|
||||
}
|
||||
|
||||
/*-
|
||||
* Called from EVP layer to initialize context, process additional
|
||||
* authenticated data, en/de-crypt plain/cipher-text and authenticate
|
||||
* ciphertext or process a TLS packet, depending on context. Returns bytes
|
||||
* written on success. Otherwise -1 is returned. Code is big-endian.
|
||||
*/
|
||||
static int s390x_aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, ctx);
|
||||
unsigned char *buf, tmp[16];
|
||||
int enc;
|
||||
|
||||
if (!gctx->key_set)
|
||||
return -1;
|
||||
|
||||
if (gctx->tls_aad_len >= 0)
|
||||
return s390x_aes_gcm_tls_cipher(ctx, out, in, len);
|
||||
|
||||
if (!gctx->iv_set)
|
||||
return -1;
|
||||
|
||||
if (in != NULL) {
|
||||
if (out == NULL) {
|
||||
if (s390x_aes_gcm_aad(gctx, in, len))
|
||||
return -1;
|
||||
} else {
|
||||
if (s390x_aes_gcm(gctx, in, out, len))
|
||||
return -1;
|
||||
}
|
||||
return len;
|
||||
} else {
|
||||
gctx->kma.param.taadl <<= 3;
|
||||
gctx->kma.param.tpcl <<= 3;
|
||||
s390x_kma(gctx->ares, gctx->areslen, gctx->mres, gctx->mreslen, tmp,
|
||||
gctx->fc | S390X_KMA_LAAD | S390X_KMA_LPC, &gctx->kma.param);
|
||||
/* recall that we already did en-/decrypt gctx->mres
|
||||
* and returned it to caller... */
|
||||
OPENSSL_cleanse(tmp, gctx->mreslen);
|
||||
gctx->iv_set = 0;
|
||||
|
||||
enc = EVP_CIPHER_CTX_encrypting(ctx);
|
||||
if (enc) {
|
||||
gctx->taglen = 16;
|
||||
} else {
|
||||
if (gctx->taglen < 0)
|
||||
return -1;
|
||||
|
||||
buf = EVP_CIPHER_CTX_buf_noconst(ctx);
|
||||
if (CRYPTO_memcmp(buf, gctx->kma.param.t.b, gctx->taglen))
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int s390x_aes_gcm_cleanup(EVP_CIPHER_CTX *c)
|
||||
{
|
||||
S390X_AES_GCM_CTX *gctx = EVP_C_DATA(S390X_AES_GCM_CTX, c);
|
||||
const unsigned char *iv;
|
||||
|
||||
if (gctx == NULL)
|
||||
return 0;
|
||||
|
||||
iv = EVP_CIPHER_CTX_iv(c);
|
||||
if (iv != gctx->iv)
|
||||
OPENSSL_free(gctx->iv);
|
||||
|
||||
OPENSSL_cleanse(gctx, sizeof(*gctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
# define S390X_AES_XTS_CTX EVP_AES_XTS_CTX
|
||||
# define S390X_aes_128_xts_CAPABLE 1 /* checked by callee */
|
||||
# define S390X_aes_256_xts_CAPABLE 1
|
||||
|
||||
# define s390x_aes_xts_init_key aes_xts_init_key
|
||||
static int s390x_aes_xts_init_key(EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv, int enc);
|
||||
# define s390x_aes_xts_cipher aes_xts_cipher
|
||||
static int s390x_aes_xts_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
# define s390x_aes_xts_ctrl aes_xts_ctrl
|
||||
static int s390x_aes_xts_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
|
||||
# define s390x_aes_xts_cleanup aes_xts_cleanup
|
||||
|
||||
# define S390X_aes_128_ccm_CAPABLE (S390X_aes_128_CAPABLE && \
|
||||
(OPENSSL_s390xcap_P.kmac[0] & \
|
||||
S390X_CAPBIT(S390X_AES_128)))
|
||||
# define S390X_aes_192_ccm_CAPABLE (S390X_aes_192_CAPABLE && \
|
||||
(OPENSSL_s390xcap_P.kmac[0] & \
|
||||
S390X_CAPBIT(S390X_AES_192)))
|
||||
# define S390X_aes_256_ccm_CAPABLE (S390X_aes_256_CAPABLE && \
|
||||
(OPENSSL_s390xcap_P.kmac[0] & \
|
||||
S390X_CAPBIT(S390X_AES_256)))
|
||||
|
||||
# define S390X_CCM_AAD_FLAG 0x40
|
||||
|
||||
/*-
|
||||
* Set nonce and length fields. Code is big-endian.
|
||||
*/
|
||||
static inline void s390x_aes_ccm_setiv(S390X_AES_CCM_CTX *ctx,
|
||||
const unsigned char *nonce,
|
||||
size_t mlen)
|
||||
{
|
||||
ctx->aes.ccm.nonce.b[0] &= ~S390X_CCM_AAD_FLAG;
|
||||
ctx->aes.ccm.nonce.g[1] = mlen;
|
||||
memcpy(ctx->aes.ccm.nonce.b + 1, nonce, 15 - ctx->aes.ccm.l);
|
||||
}
|
||||
|
||||
/*-
|
||||
* Process additional authenticated data. Code is big-endian.
|
||||
*/
|
||||
static void s390x_aes_ccm_aad(S390X_AES_CCM_CTX *ctx, const unsigned char *aad,
|
||||
size_t alen)
|
||||
{
|
||||
unsigned char *ptr;
|
||||
int i, rem;
|
||||
|
||||
if (!alen)
|
||||
return;
|
||||
|
||||
ctx->aes.ccm.nonce.b[0] |= S390X_CCM_AAD_FLAG;
|
||||
|
||||
/* Suppress 'type-punned pointer dereference' warning. */
|
||||
ptr = ctx->aes.ccm.buf.b;
|
||||
|
||||
if (alen < ((1 << 16) - (1 << 8))) {
|
||||
*(uint16_t *)ptr = alen;
|
||||
i = 2;
|
||||
} else if (sizeof(alen) == 8
|
||||
&& alen >= (size_t)1 << (32 % (sizeof(alen) * 8))) {
|
||||
*(uint16_t *)ptr = 0xffff;
|
||||
*(uint64_t *)(ptr + 2) = alen;
|
||||
i = 10;
|
||||
} else {
|
||||
*(uint16_t *)ptr = 0xfffe;
|
||||
*(uint32_t *)(ptr + 2) = alen;
|
||||
i = 6;
|
||||
}
|
||||
|
||||
while (i < 16 && alen) {
|
||||
ctx->aes.ccm.buf.b[i] = *aad;
|
||||
++aad;
|
||||
--alen;
|
||||
++i;
|
||||
}
|
||||
while (i < 16) {
|
||||
ctx->aes.ccm.buf.b[i] = 0;
|
||||
++i;
|
||||
}
|
||||
|
||||
ctx->aes.ccm.kmac_param.icv.g[0] = 0;
|
||||
ctx->aes.ccm.kmac_param.icv.g[1] = 0;
|
||||
s390x_kmac(ctx->aes.ccm.nonce.b, 32, ctx->aes.ccm.fc,
|
||||
&ctx->aes.ccm.kmac_param);
|
||||
ctx->aes.ccm.blocks += 2;
|
||||
|
||||
rem = alen & 0xf;
|
||||
alen &= ~0xf;
|
||||
if (alen) {
|
||||
s390x_kmac(aad, alen, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
|
||||
ctx->aes.ccm.blocks += alen >> 4;
|
||||
aad += alen;
|
||||
}
|
||||
if (rem) {
|
||||
for (i = 0; i < rem; i++)
|
||||
ctx->aes.ccm.kmac_param.icv.b[i] ^= aad[i];
|
||||
|
||||
s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
|
||||
ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
|
||||
ctx->aes.ccm.kmac_param.k);
|
||||
ctx->aes.ccm.blocks++;
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* En/de-crypt plain/cipher-text. Compute tag from plaintext. Returns 0 for
|
||||
* success.
|
||||
*/
|
||||
static int s390x_aes_ccm(S390X_AES_CCM_CTX *ctx, const unsigned char *in,
|
||||
unsigned char *out, size_t len, int enc)
|
||||
{
|
||||
size_t n, rem;
|
||||
unsigned int i, l, num;
|
||||
unsigned char flags;
|
||||
|
||||
flags = ctx->aes.ccm.nonce.b[0];
|
||||
if (!(flags & S390X_CCM_AAD_FLAG)) {
|
||||
s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.kmac_param.icv.b,
|
||||
ctx->aes.ccm.fc, ctx->aes.ccm.kmac_param.k);
|
||||
ctx->aes.ccm.blocks++;
|
||||
}
|
||||
l = flags & 0x7;
|
||||
ctx->aes.ccm.nonce.b[0] = l;
|
||||
|
||||
/*-
|
||||
* Reconstruct length from encoded length field
|
||||
* and initialize it with counter value.
|
||||
*/
|
||||
n = 0;
|
||||
for (i = 15 - l; i < 15; i++) {
|
||||
n |= ctx->aes.ccm.nonce.b[i];
|
||||
ctx->aes.ccm.nonce.b[i] = 0;
|
||||
n <<= 8;
|
||||
}
|
||||
n |= ctx->aes.ccm.nonce.b[15];
|
||||
ctx->aes.ccm.nonce.b[15] = 1;
|
||||
|
||||
if (n != len)
|
||||
return -1; /* length mismatch */
|
||||
|
||||
if (enc) {
|
||||
/* Two operations per block plus one for tag encryption */
|
||||
ctx->aes.ccm.blocks += (((len + 15) >> 4) << 1) + 1;
|
||||
if (ctx->aes.ccm.blocks > (1ULL << 61))
|
||||
return -2; /* too much data */
|
||||
}
|
||||
|
||||
num = 0;
|
||||
rem = len & 0xf;
|
||||
len &= ~0xf;
|
||||
|
||||
if (enc) {
|
||||
/* mac-then-encrypt */
|
||||
if (len)
|
||||
s390x_kmac(in, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
|
||||
if (rem) {
|
||||
for (i = 0; i < rem; i++)
|
||||
ctx->aes.ccm.kmac_param.icv.b[i] ^= in[len + i];
|
||||
|
||||
s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
|
||||
ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
|
||||
ctx->aes.ccm.kmac_param.k);
|
||||
}
|
||||
|
||||
CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k,
|
||||
ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b,
|
||||
&num, (ctr128_f)AES_ctr32_encrypt);
|
||||
} else {
|
||||
/* decrypt-then-mac */
|
||||
CRYPTO_ctr128_encrypt_ctr32(in, out, len + rem, &ctx->aes.key.k,
|
||||
ctx->aes.ccm.nonce.b, ctx->aes.ccm.buf.b,
|
||||
&num, (ctr128_f)AES_ctr32_encrypt);
|
||||
|
||||
if (len)
|
||||
s390x_kmac(out, len, ctx->aes.ccm.fc, &ctx->aes.ccm.kmac_param);
|
||||
if (rem) {
|
||||
for (i = 0; i < rem; i++)
|
||||
ctx->aes.ccm.kmac_param.icv.b[i] ^= out[len + i];
|
||||
|
||||
s390x_km(ctx->aes.ccm.kmac_param.icv.b, 16,
|
||||
ctx->aes.ccm.kmac_param.icv.b, ctx->aes.ccm.fc,
|
||||
ctx->aes.ccm.kmac_param.k);
|
||||
}
|
||||
}
|
||||
/* encrypt tag */
|
||||
for (i = 15 - l; i < 16; i++)
|
||||
ctx->aes.ccm.nonce.b[i] = 0;
|
||||
|
||||
s390x_km(ctx->aes.ccm.nonce.b, 16, ctx->aes.ccm.buf.b, ctx->aes.ccm.fc,
|
||||
ctx->aes.ccm.kmac_param.k);
|
||||
ctx->aes.ccm.kmac_param.icv.g[0] ^= ctx->aes.ccm.buf.g[0];
|
||||
ctx->aes.ccm.kmac_param.icv.g[1] ^= ctx->aes.ccm.buf.g[1];
|
||||
|
||||
ctx->aes.ccm.nonce.b[0] = flags; /* restore flags field */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-
|
||||
* En/de-crypt and authenticate TLS packet. Returns the number of bytes written
|
||||
* if successful. Otherwise -1 is returned.
|
||||
*/
|
||||
static int s390x_aes_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
|
||||
unsigned char *ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
|
||||
unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
|
||||
const int enc = EVP_CIPHER_CTX_encrypting(ctx);
|
||||
|
||||
if (out != in
|
||||
|| len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->aes.ccm.m))
|
||||
return -1;
|
||||
|
||||
if (enc) {
|
||||
/* Set explicit iv (sequence number). */
|
||||
memcpy(out, buf, EVP_CCM_TLS_EXPLICIT_IV_LEN);
|
||||
}
|
||||
|
||||
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m;
|
||||
/*-
|
||||
* Get explicit iv (sequence number). We already have fixed iv
|
||||
* (server/client_write_iv) here.
|
||||
*/
|
||||
memcpy(ivec + EVP_CCM_TLS_FIXED_IV_LEN, in, EVP_CCM_TLS_EXPLICIT_IV_LEN);
|
||||
s390x_aes_ccm_setiv(cctx, ivec, len);
|
||||
|
||||
/* Process aad (sequence number|type|version|length) */
|
||||
s390x_aes_ccm_aad(cctx, buf, cctx->aes.ccm.tls_aad_len);
|
||||
|
||||
in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
|
||||
if (enc) {
|
||||
if (s390x_aes_ccm(cctx, in, out, len, enc))
|
||||
return -1;
|
||||
|
||||
memcpy(out + len, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
|
||||
return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->aes.ccm.m;
|
||||
} else {
|
||||
if (!s390x_aes_ccm(cctx, in, out, len, enc)) {
|
||||
if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, in + len,
|
||||
cctx->aes.ccm.m))
|
||||
return len;
|
||||
}
|
||||
|
||||
OPENSSL_cleanse(out, len);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* Set key and flag field and/or iv. Returns 1 if successful. Otherwise 0 is
|
||||
* returned.
|
||||
*/
|
||||
static int s390x_aes_ccm_init_key(EVP_CIPHER_CTX *ctx,
|
||||
const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
|
||||
unsigned char *ivec;
|
||||
int keylen;
|
||||
|
||||
if (iv == NULL && key == NULL)
|
||||
return 1;
|
||||
|
||||
if (key != NULL) {
|
||||
keylen = EVP_CIPHER_CTX_key_length(ctx);
|
||||
/* Convert key size to function code. */
|
||||
cctx->aes.ccm.fc = S390X_AES_128 + (((keylen << 3) - 128) >> 6);
|
||||
memcpy(cctx->aes.ccm.kmac_param.k, key, keylen);
|
||||
|
||||
/* Store encoded m and l. */
|
||||
cctx->aes.ccm.nonce.b[0] = ((cctx->aes.ccm.l - 1) & 0x7)
|
||||
| (((cctx->aes.ccm.m - 2) >> 1) & 0x7) << 3;
|
||||
memset(cctx->aes.ccm.nonce.b + 1, 0,
|
||||
sizeof(cctx->aes.ccm.nonce.b));
|
||||
cctx->aes.ccm.blocks = 0;
|
||||
|
||||
cctx->aes.ccm.key_set = 1;
|
||||
}
|
||||
|
||||
if (iv != NULL) {
|
||||
ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
|
||||
memcpy(ivec, iv, 15 - cctx->aes.ccm.l);
|
||||
|
||||
cctx->aes.ccm.iv_set = 1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*-
|
||||
* Called from EVP layer to initialize context, process additional
|
||||
* authenticated data, en/de-crypt plain/cipher-text and authenticate
|
||||
* plaintext or process a TLS packet, depending on context. Returns bytes
|
||||
* written on success. Otherwise -1 is returned.
|
||||
*/
|
||||
static int s390x_aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, ctx);
|
||||
const int enc = EVP_CIPHER_CTX_encrypting(ctx);
|
||||
int rv;
|
||||
unsigned char *buf, *ivec;
|
||||
|
||||
if (!cctx->aes.ccm.key_set)
|
||||
return -1;
|
||||
|
||||
if (cctx->aes.ccm.tls_aad_len >= 0)
|
||||
return s390x_aes_ccm_tls_cipher(ctx, out, in, len);
|
||||
|
||||
/*-
|
||||
* Final(): Does not return any data. Recall that ccm is mac-then-encrypt
|
||||
* so integrity must be checked already at Update() i.e., before
|
||||
* potentially corrupted data is output.
|
||||
*/
|
||||
if (in == NULL && out != NULL)
|
||||
return 0;
|
||||
|
||||
if (!cctx->aes.ccm.iv_set)
|
||||
return -1;
|
||||
|
||||
if (!enc && !cctx->aes.ccm.tag_set)
|
||||
return -1;
|
||||
|
||||
if (out == NULL) {
|
||||
/* Update(): Pass message length. */
|
||||
if (in == NULL) {
|
||||
ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
|
||||
s390x_aes_ccm_setiv(cctx, ivec, len);
|
||||
|
||||
cctx->aes.ccm.len_set = 1;
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Update(): Process aad. */
|
||||
if (!cctx->aes.ccm.len_set && len)
|
||||
return -1;
|
||||
|
||||
s390x_aes_ccm_aad(cctx, in, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Update(): Process message. */
|
||||
|
||||
if (!cctx->aes.ccm.len_set) {
|
||||
/*-
|
||||
* In case message length was not previously set explicitely via
|
||||
* Update(), set it now.
|
||||
*/
|
||||
ivec = EVP_CIPHER_CTX_iv_noconst(ctx);
|
||||
s390x_aes_ccm_setiv(cctx, ivec, len);
|
||||
|
||||
cctx->aes.ccm.len_set = 1;
|
||||
}
|
||||
|
||||
if (enc) {
|
||||
if (s390x_aes_ccm(cctx, in, out, len, enc))
|
||||
return -1;
|
||||
|
||||
cctx->aes.ccm.tag_set = 1;
|
||||
return len;
|
||||
} else {
|
||||
rv = -1;
|
||||
|
||||
if (!s390x_aes_ccm(cctx, in, out, len, enc)) {
|
||||
buf = EVP_CIPHER_CTX_buf_noconst(ctx);
|
||||
if (!CRYPTO_memcmp(cctx->aes.ccm.kmac_param.icv.b, buf,
|
||||
cctx->aes.ccm.m))
|
||||
rv = len;
|
||||
}
|
||||
|
||||
if (rv == -1)
|
||||
OPENSSL_cleanse(out, len);
|
||||
|
||||
cctx->aes.ccm.iv_set = 0;
|
||||
cctx->aes.ccm.tag_set = 0;
|
||||
cctx->aes.ccm.len_set = 0;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
/*-
|
||||
* Performs various operations on the context structure depending on control
|
||||
* type. Returns 1 for success, 0 for failure and -1 for unknown control type.
|
||||
* Code is big-endian.
|
||||
*/
|
||||
static int s390x_aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
{
|
||||
S390X_AES_CCM_CTX *cctx = EVP_C_DATA(S390X_AES_CCM_CTX, c);
|
||||
unsigned char *buf, *iv;
|
||||
int enc, len;
|
||||
|
||||
switch (type) {
|
||||
case EVP_CTRL_INIT:
|
||||
cctx->aes.ccm.key_set = 0;
|
||||
cctx->aes.ccm.iv_set = 0;
|
||||
cctx->aes.ccm.l = 8;
|
||||
cctx->aes.ccm.m = 12;
|
||||
cctx->aes.ccm.tag_set = 0;
|
||||
cctx->aes.ccm.len_set = 0;
|
||||
cctx->aes.ccm.tls_aad_len = -1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_TLS1_AAD:
|
||||
if (arg != EVP_AEAD_TLS1_AAD_LEN)
|
||||
return 0;
|
||||
|
||||
/* Save the aad for later use. */
|
||||
buf = EVP_CIPHER_CTX_buf_noconst(c);
|
||||
memcpy(buf, ptr, arg);
|
||||
cctx->aes.ccm.tls_aad_len = arg;
|
||||
|
||||
len = *(uint16_t *)(buf + arg - 2);
|
||||
if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
|
||||
return 0;
|
||||
|
||||
/* Correct length for explicit iv. */
|
||||
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
|
||||
enc = EVP_CIPHER_CTX_encrypting(c);
|
||||
if (!enc) {
|
||||
if (len < cctx->aes.ccm.m)
|
||||
return 0;
|
||||
|
||||
/* Correct length for tag. */
|
||||
len -= cctx->aes.ccm.m;
|
||||
}
|
||||
|
||||
*(uint16_t *)(buf + arg - 2) = len;
|
||||
/* Extra padding: tag appended to record. */
|
||||
return cctx->aes.ccm.m;
|
||||
|
||||
case EVP_CTRL_CCM_SET_IV_FIXED:
|
||||
if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
|
||||
return 0;
|
||||
|
||||
/* Copy to first part of the iv. */
|
||||
iv = EVP_CIPHER_CTX_iv_noconst(c);
|
||||
memcpy(iv, ptr, arg);
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_IVLEN:
|
||||
arg = 15 - arg;
|
||||
/* fall-through */
|
||||
|
||||
case EVP_CTRL_CCM_SET_L:
|
||||
if (arg < 2 || arg > 8)
|
||||
return 0;
|
||||
|
||||
cctx->aes.ccm.l = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_TAG:
|
||||
if ((arg & 1) || arg < 4 || arg > 16)
|
||||
return 0;
|
||||
|
||||
enc = EVP_CIPHER_CTX_encrypting(c);
|
||||
if (enc && ptr)
|
||||
return 0;
|
||||
|
||||
if (ptr) {
|
||||
cctx->aes.ccm.tag_set = 1;
|
||||
buf = EVP_CIPHER_CTX_buf_noconst(c);
|
||||
memcpy(buf, ptr, arg);
|
||||
}
|
||||
|
||||
cctx->aes.ccm.m = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_GET_TAG:
|
||||
enc = EVP_CIPHER_CTX_encrypting(c);
|
||||
if (!enc || !cctx->aes.ccm.tag_set)
|
||||
return 0;
|
||||
|
||||
if(arg < cctx->aes.ccm.m)
|
||||
return 0;
|
||||
|
||||
memcpy(ptr, cctx->aes.ccm.kmac_param.icv.b, cctx->aes.ccm.m);
|
||||
cctx->aes.ccm.tag_set = 0;
|
||||
cctx->aes.ccm.iv_set = 0;
|
||||
cctx->aes.ccm.len_set = 0;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_COPY:
|
||||
return 1;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
# define s390x_aes_ccm_cleanup aes_ccm_cleanup
|
||||
|
||||
# ifndef OPENSSL_NO_OCB
|
||||
# define S390X_AES_OCB_CTX EVP_AES_OCB_CTX
|
||||
# define S390X_aes_128_ocb_CAPABLE 0
|
||||
# define S390X_aes_192_ocb_CAPABLE 0
|
||||
# define S390X_aes_256_ocb_CAPABLE 0
|
||||
|
||||
# define s390x_aes_ocb_init_key aes_ocb_init_key
|
||||
static int s390x_aes_ocb_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc);
|
||||
# define s390x_aes_ocb_cipher aes_ocb_cipher
|
||||
static int s390x_aes_ocb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len);
|
||||
# define s390x_aes_ocb_cleanup aes_ocb_cleanup
|
||||
static int s390x_aes_ocb_cleanup(EVP_CIPHER_CTX *);
|
||||
# define s390x_aes_ocb_ctrl aes_ocb_ctrl
|
||||
static int s390x_aes_ocb_ctrl(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
|
||||
# endif
|
||||
|
||||
# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode, \
|
||||
MODE,flags) \
|
||||
static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \
|
||||
nid##_##keylen##_##nmode,blocksize, \
|
||||
keylen / 8, \
|
||||
ivlen, \
|
||||
flags | EVP_CIPH_##MODE##_MODE, \
|
||||
s390x_aes_init_key, \
|
||||
s390x_aes_##mode##_cipher, \
|
||||
NULL, \
|
||||
sizeof(EVP_AES_KEY), \
|
||||
NULL, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
NULL \
|
||||
}; \
|
||||
static const EVP_CIPHER aes_##keylen##_##mode = { \
|
||||
nid##_##keylen##_##nmode, \
|
||||
blocksize, \
|
||||
keylen / 8, \
|
||||
ivlen, \
|
||||
flags | EVP_CIPH_##MODE##_MODE, \
|
||||
aes_init_key, \
|
||||
aes_##mode##_cipher, \
|
||||
NULL, \
|
||||
sizeof(EVP_AES_KEY), \
|
||||
NULL,NULL,NULL,NULL \
|
||||
}; \
|
||||
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
|
||||
{ \
|
||||
return S390X_aes_##keylen##_##mode##_CAPABLE ? \
|
||||
&s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \
|
||||
}
|
||||
|
||||
# define BLOCK_CIPHER_custom(nid,keylen,blocksize,ivlen,mode,MODE,flags)\
|
||||
static const EVP_CIPHER s390x_aes_##keylen##_##mode = { \
|
||||
nid##_##keylen##_##mode, \
|
||||
blocksize, \
|
||||
(EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8, \
|
||||
ivlen, \
|
||||
flags | EVP_CIPH_##MODE##_MODE, \
|
||||
s390x_aes_##mode##_init_key, \
|
||||
s390x_aes_##mode##_cipher, \
|
||||
s390x_aes_##mode##_cleanup, \
|
||||
sizeof(S390X_AES_##MODE##_CTX), \
|
||||
NULL, \
|
||||
NULL, \
|
||||
s390x_aes_##mode##_ctrl, \
|
||||
NULL \
|
||||
}; \
|
||||
static const EVP_CIPHER aes_##keylen##_##mode = { \
|
||||
nid##_##keylen##_##mode,blocksize, \
|
||||
(EVP_CIPH_##MODE##_MODE == EVP_CIPH_XTS_MODE ? 2 : 1) * keylen / 8, \
|
||||
ivlen, \
|
||||
flags | EVP_CIPH_##MODE##_MODE, \
|
||||
aes_##mode##_init_key, \
|
||||
aes_##mode##_cipher, \
|
||||
aes_##mode##_cleanup, \
|
||||
sizeof(EVP_AES_##MODE##_CTX), \
|
||||
NULL, \
|
||||
NULL, \
|
||||
aes_##mode##_ctrl, \
|
||||
NULL \
|
||||
}; \
|
||||
const EVP_CIPHER *EVP_aes_##keylen##_##mode(void) \
|
||||
{ \
|
||||
return S390X_aes_##keylen##_##mode##_CAPABLE ? \
|
||||
&s390x_aes_##keylen##_##mode : &aes_##keylen##_##mode; \
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
|
||||
@@ -2132,6 +3413,10 @@ static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
if (cctx->tls_aad_len >= 0)
|
||||
return aes_ccm_tls_cipher(ctx, out, in, len);
|
||||
|
||||
/* EVP_*Final() doesn't return any data */
|
||||
if (in == NULL && out != NULL)
|
||||
return 0;
|
||||
|
||||
if (!cctx->iv_set)
|
||||
return -1;
|
||||
|
||||
@@ -2151,9 +3436,6 @@ static int aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
CRYPTO_ccm128_aad(ccm, in, len);
|
||||
return len;
|
||||
}
|
||||
/* EVP_*Final() doesn't return any data */
|
||||
if (!in)
|
||||
return 0;
|
||||
/* If not set length yet do it */
|
||||
if (!cctx->len_set) {
|
||||
if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
|
||||
@@ -570,7 +570,7 @@ static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
}
|
||||
# endif
|
||||
|
||||
# if 1
|
||||
# if 1 /* see original reference version in #else */
|
||||
len -= SHA_DIGEST_LENGTH; /* amend mac */
|
||||
if (len >= (256 + SHA_CBLOCK)) {
|
||||
j = (len - (256 + SHA_CBLOCK)) & (0 - SHA_CBLOCK);
|
||||
@@ -664,7 +664,7 @@ static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
}
|
||||
# endif
|
||||
len += SHA_DIGEST_LENGTH;
|
||||
# else
|
||||
# else /* pre-lucky-13 reference version of above */
|
||||
SHA1_Update(&key->md, out, inp_len);
|
||||
res = key->md.num;
|
||||
SHA1_Final(pmac->c, &key->md);
|
||||
@@ -691,7 +691,7 @@ static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
/* verify HMAC */
|
||||
out += inp_len;
|
||||
len -= inp_len;
|
||||
# if 1
|
||||
# if 1 /* see original reference version in #else */
|
||||
{
|
||||
unsigned char *p = out + len - 1 - maxpad - SHA_DIGEST_LENGTH;
|
||||
size_t off = out - p;
|
||||
@@ -713,7 +713,7 @@ static int aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
|
||||
ret &= (int)~res;
|
||||
}
|
||||
# else
|
||||
# else /* pre-lucky-13 reference version of above */
|
||||
for (res = 0, i = 0; i < SHA_DIGEST_LENGTH; i++)
|
||||
res |= out[i] ^ pmac->c[i];
|
||||
res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
|
||||
|
||||
@@ -559,7 +559,7 @@ static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
|
||||
key->md = key->head;
|
||||
SHA256_Update(&key->md, key->aux.tls_aad, plen);
|
||||
|
||||
# if 1
|
||||
# if 1 /* see original reference version in #else */
|
||||
len -= SHA256_DIGEST_LENGTH; /* amend mac */
|
||||
if (len >= (256 + SHA256_CBLOCK)) {
|
||||
j = (len - (256 + SHA256_CBLOCK)) & (0 - SHA256_CBLOCK);
|
||||
@@ -687,7 +687,7 @@ static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
|
||||
for (; inp_blocks < pad_blocks; inp_blocks++)
|
||||
sha1_block_data_order(&key->md, data, 1);
|
||||
}
|
||||
# endif
|
||||
# endif /* pre-lucky-13 reference version of above */
|
||||
key->md = key->tail;
|
||||
SHA256_Update(&key->md, pmac->c, SHA256_DIGEST_LENGTH);
|
||||
SHA256_Final(pmac->c, &key->md);
|
||||
@@ -695,7 +695,7 @@ static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
|
||||
/* verify HMAC */
|
||||
out += inp_len;
|
||||
len -= inp_len;
|
||||
# if 1
|
||||
# if 1 /* see original reference version in #else */
|
||||
{
|
||||
unsigned char *p =
|
||||
out + len - 1 - maxpad - SHA256_DIGEST_LENGTH;
|
||||
@@ -718,7 +718,7 @@ static int aesni_cbc_hmac_sha256_cipher(EVP_CIPHER_CTX *ctx,
|
||||
res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
|
||||
ret &= (int)~res;
|
||||
}
|
||||
# else
|
||||
# else /* pre-lucky-13 reference version of above */
|
||||
for (res = 0, i = 0; i < SHA256_DIGEST_LENGTH; i++)
|
||||
res |= out[i] ^ pmac->c[i];
|
||||
res = 0 - ((0 - res) >> (sizeof(res) * 8 - 1));
|
||||
|
||||
@@ -0,0 +1,753 @@
|
||||
/*
|
||||
* Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
* in the file LICENSE in the source distribution or at
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
#include "internal/cryptlib.h"
|
||||
#ifndef OPENSSL_NO_ARIA
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/modes.h>
|
||||
# include <openssl/rand.h>
|
||||
# include "internal/aria.h"
|
||||
# include "internal/evp_int.h"
|
||||
# include "modes_lcl.h"
|
||||
# include "evp_locl.h"
|
||||
|
||||
/* ARIA subkey Structure */
|
||||
typedef struct {
|
||||
ARIA_KEY ks;
|
||||
} EVP_ARIA_KEY;
|
||||
|
||||
/* ARIA GCM context */
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
ARIA_KEY ks;
|
||||
} ks; /* ARIA subkey to use */
|
||||
int key_set; /* Set if key initialised */
|
||||
int iv_set; /* Set if an iv is set */
|
||||
GCM128_CONTEXT gcm;
|
||||
unsigned char *iv; /* Temporary IV store */
|
||||
int ivlen; /* IV length */
|
||||
int taglen;
|
||||
int iv_gen; /* It is OK to generate IVs */
|
||||
int tls_aad_len; /* TLS AAD length */
|
||||
} EVP_ARIA_GCM_CTX;
|
||||
|
||||
/* ARIA CCM context */
|
||||
typedef struct {
|
||||
union {
|
||||
double align;
|
||||
ARIA_KEY ks;
|
||||
} ks; /* ARIA key schedule to use */
|
||||
int key_set; /* Set if key initialised */
|
||||
int iv_set; /* Set if an iv is set */
|
||||
int tag_set; /* Set if tag is valid */
|
||||
int len_set; /* Set if message length set */
|
||||
int L, M; /* L and M parameters from RFC3610 */
|
||||
int tls_aad_len; /* TLS AAD length */
|
||||
CCM128_CONTEXT ccm;
|
||||
ccm128_f str;
|
||||
} EVP_ARIA_CCM_CTX;
|
||||
|
||||
/* The subkey for ARIA is generated. */
|
||||
static int aria_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
int ret;
|
||||
int mode = EVP_CIPHER_CTX_mode(ctx);
|
||||
|
||||
if (enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE))
|
||||
ret = aria_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
|
||||
EVP_CIPHER_CTX_get_cipher_data(ctx));
|
||||
else
|
||||
ret = aria_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
|
||||
EVP_CIPHER_CTX_get_cipher_data(ctx));
|
||||
if (ret < 0) {
|
||||
EVPerr(EVP_F_ARIA_INIT_KEY,EVP_R_ARIA_KEY_SETUP_FAILED);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void aria_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
||||
size_t len, const ARIA_KEY *key,
|
||||
unsigned char *ivec, const int enc)
|
||||
{
|
||||
|
||||
if (enc)
|
||||
CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
|
||||
(block128_f) aria_encrypt);
|
||||
else
|
||||
CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
|
||||
(block128_f) aria_encrypt);
|
||||
}
|
||||
|
||||
static void aria_cfb128_encrypt(const unsigned char *in, unsigned char *out,
|
||||
size_t length, const ARIA_KEY *key,
|
||||
unsigned char *ivec, int *num, const int enc)
|
||||
{
|
||||
|
||||
CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
|
||||
(block128_f) aria_encrypt);
|
||||
}
|
||||
|
||||
static void aria_cfb1_encrypt(const unsigned char *in, unsigned char *out,
|
||||
size_t length, const ARIA_KEY *key,
|
||||
unsigned char *ivec, int *num, const int enc)
|
||||
{
|
||||
CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc,
|
||||
(block128_f) aria_encrypt);
|
||||
}
|
||||
|
||||
static void aria_cfb8_encrypt(const unsigned char *in, unsigned char *out,
|
||||
size_t length, const ARIA_KEY *key,
|
||||
unsigned char *ivec, int *num, const int enc)
|
||||
{
|
||||
CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc,
|
||||
(block128_f) aria_encrypt);
|
||||
}
|
||||
|
||||
static void aria_ecb_encrypt(const unsigned char *in, unsigned char *out,
|
||||
const ARIA_KEY *key, const int enc)
|
||||
{
|
||||
aria_encrypt(in, out, key);
|
||||
}
|
||||
|
||||
static void aria_ofb128_encrypt(const unsigned char *in, unsigned char *out,
|
||||
size_t length, const ARIA_KEY *key,
|
||||
unsigned char *ivec, int *num)
|
||||
{
|
||||
CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
|
||||
(block128_f) aria_encrypt);
|
||||
}
|
||||
|
||||
IMPLEMENT_BLOCK_CIPHER(aria_128, ks, aria, EVP_ARIA_KEY,
|
||||
NID_aria_128, 16, 16, 16, 128,
|
||||
0, aria_init_key, NULL,
|
||||
EVP_CIPHER_set_asn1_iv,
|
||||
EVP_CIPHER_get_asn1_iv,
|
||||
NULL)
|
||||
IMPLEMENT_BLOCK_CIPHER(aria_192, ks, aria, EVP_ARIA_KEY,
|
||||
NID_aria_192, 16, 24, 16, 128,
|
||||
0, aria_init_key, NULL,
|
||||
EVP_CIPHER_set_asn1_iv,
|
||||
EVP_CIPHER_get_asn1_iv,
|
||||
NULL)
|
||||
IMPLEMENT_BLOCK_CIPHER(aria_256, ks, aria, EVP_ARIA_KEY,
|
||||
NID_aria_256, 16, 32, 16, 128,
|
||||
0, aria_init_key, NULL,
|
||||
EVP_CIPHER_set_asn1_iv,
|
||||
EVP_CIPHER_get_asn1_iv,
|
||||
NULL)
|
||||
|
||||
# define IMPLEMENT_ARIA_CFBR(ksize,cbits) \
|
||||
IMPLEMENT_CFBR(aria,aria,EVP_ARIA_KEY,ks,ksize,cbits,16,0)
|
||||
IMPLEMENT_ARIA_CFBR(128,1)
|
||||
IMPLEMENT_ARIA_CFBR(192,1)
|
||||
IMPLEMENT_ARIA_CFBR(256,1)
|
||||
IMPLEMENT_ARIA_CFBR(128,8)
|
||||
IMPLEMENT_ARIA_CFBR(192,8)
|
||||
IMPLEMENT_ARIA_CFBR(256,8)
|
||||
|
||||
# define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
|
||||
static const EVP_CIPHER aria_##keylen##_##mode = { \
|
||||
nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
|
||||
flags|EVP_CIPH_##MODE##_MODE, \
|
||||
aria_init_key, \
|
||||
aria_##mode##_cipher, \
|
||||
NULL, \
|
||||
sizeof(EVP_ARIA_KEY), \
|
||||
NULL,NULL,NULL,NULL }; \
|
||||
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
|
||||
{ return &aria_##keylen##_##mode; }
|
||||
|
||||
static int aria_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
unsigned int num = EVP_CIPHER_CTX_num(ctx);
|
||||
EVP_ARIA_KEY *dat = EVP_C_DATA(EVP_ARIA_KEY,ctx);
|
||||
|
||||
CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
EVP_CIPHER_CTX_buf_noconst(ctx), &num,
|
||||
(block128_f) aria_encrypt);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
return 1;
|
||||
}
|
||||
|
||||
BLOCK_CIPHER_generic(NID_aria, 128, 1, 16, ctr, ctr, CTR, 0)
|
||||
BLOCK_CIPHER_generic(NID_aria, 192, 1, 16, ctr, ctr, CTR, 0)
|
||||
BLOCK_CIPHER_generic(NID_aria, 256, 1, 16, ctr, ctr, CTR, 0)
|
||||
|
||||
/* Authenticated cipher modes (GCM/CCM) */
|
||||
|
||||
/* increment counter (64-bit int) by 1 */
|
||||
static void ctr64_inc(unsigned char *counter)
|
||||
{
|
||||
int n = 8;
|
||||
unsigned char c;
|
||||
|
||||
do {
|
||||
--n;
|
||||
c = counter[n];
|
||||
++c;
|
||||
counter[n] = c;
|
||||
if (c)
|
||||
return;
|
||||
} while (n);
|
||||
}
|
||||
|
||||
static int aria_gcm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
int ret;
|
||||
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx);
|
||||
|
||||
if (!iv && !key)
|
||||
return 1;
|
||||
if (key) {
|
||||
ret = aria_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
|
||||
&gctx->ks.ks);
|
||||
CRYPTO_gcm128_init(&gctx->gcm, &gctx->ks,
|
||||
(block128_f) aria_encrypt);
|
||||
if (ret < 0) {
|
||||
EVPerr(EVP_F_ARIA_GCM_INIT_KEY,EVP_R_ARIA_KEY_SETUP_FAILED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we have an iv can set it directly, otherwise use saved IV.
|
||||
*/
|
||||
if (iv == NULL && gctx->iv_set)
|
||||
iv = gctx->iv;
|
||||
if (iv) {
|
||||
CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
|
||||
gctx->iv_set = 1;
|
||||
}
|
||||
gctx->key_set = 1;
|
||||
} else {
|
||||
/* If key set use IV, otherwise copy */
|
||||
if (gctx->key_set)
|
||||
CRYPTO_gcm128_setiv(&gctx->gcm, iv, gctx->ivlen);
|
||||
else
|
||||
memcpy(gctx->iv, iv, gctx->ivlen);
|
||||
gctx->iv_set = 1;
|
||||
gctx->iv_gen = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aria_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
{
|
||||
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,c);
|
||||
|
||||
switch (type) {
|
||||
case EVP_CTRL_INIT:
|
||||
gctx->key_set = 0;
|
||||
gctx->iv_set = 0;
|
||||
gctx->ivlen = EVP_CIPHER_CTX_iv_length(c);
|
||||
gctx->iv = EVP_CIPHER_CTX_iv_noconst(c);
|
||||
gctx->taglen = -1;
|
||||
gctx->iv_gen = 0;
|
||||
gctx->tls_aad_len = -1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_IVLEN:
|
||||
if (arg <= 0)
|
||||
return 0;
|
||||
/* Allocate memory for IV if needed */
|
||||
if ((arg > EVP_MAX_IV_LENGTH) && (arg > gctx->ivlen)) {
|
||||
if (gctx->iv != EVP_CIPHER_CTX_iv_noconst(c))
|
||||
OPENSSL_free(gctx->iv);
|
||||
gctx->iv = OPENSSL_malloc(arg);
|
||||
if (gctx->iv == NULL)
|
||||
return 0;
|
||||
}
|
||||
gctx->ivlen = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_TAG:
|
||||
if (arg <= 0 || arg > 16 || EVP_CIPHER_CTX_encrypting(c))
|
||||
return 0;
|
||||
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
|
||||
gctx->taglen = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_GET_TAG:
|
||||
if (arg <= 0 || arg > 16 || !EVP_CIPHER_CTX_encrypting(c)
|
||||
|| gctx->taglen < 0)
|
||||
return 0;
|
||||
memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(c), arg);
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_SET_IV_FIXED:
|
||||
/* Special case: -1 length restores whole IV */
|
||||
if (arg == -1) {
|
||||
memcpy(gctx->iv, ptr, gctx->ivlen);
|
||||
gctx->iv_gen = 1;
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
* Fixed field must be at least 4 bytes and invocation field at least
|
||||
* 8.
|
||||
*/
|
||||
if ((arg < 4) || (gctx->ivlen - arg) < 8)
|
||||
return 0;
|
||||
if (arg)
|
||||
memcpy(gctx->iv, ptr, arg);
|
||||
if (EVP_CIPHER_CTX_encrypting(c)
|
||||
&& RAND_bytes(gctx->iv + arg, gctx->ivlen - arg) <= 0)
|
||||
return 0;
|
||||
gctx->iv_gen = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_IV_GEN:
|
||||
if (gctx->iv_gen == 0 || gctx->key_set == 0)
|
||||
return 0;
|
||||
CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
|
||||
if (arg <= 0 || arg > gctx->ivlen)
|
||||
arg = gctx->ivlen;
|
||||
memcpy(ptr, gctx->iv + gctx->ivlen - arg, arg);
|
||||
/*
|
||||
* Invocation field will be at least 8 bytes in size and so no need
|
||||
* to check wrap around or increment more than last 8 bytes.
|
||||
*/
|
||||
ctr64_inc(gctx->iv + gctx->ivlen - 8);
|
||||
gctx->iv_set = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_GCM_SET_IV_INV:
|
||||
if (gctx->iv_gen == 0 || gctx->key_set == 0
|
||||
|| EVP_CIPHER_CTX_encrypting(c))
|
||||
return 0;
|
||||
memcpy(gctx->iv + gctx->ivlen - arg, ptr, arg);
|
||||
CRYPTO_gcm128_setiv(&gctx->gcm, gctx->iv, gctx->ivlen);
|
||||
gctx->iv_set = 1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_TLS1_AAD:
|
||||
/* Save the AAD for later use */
|
||||
if (arg != EVP_AEAD_TLS1_AAD_LEN)
|
||||
return 0;
|
||||
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
|
||||
gctx->tls_aad_len = arg;
|
||||
{
|
||||
unsigned int len =
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
|
||||
| EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
|
||||
/* Correct length for explicit IV */
|
||||
if (len < EVP_GCM_TLS_EXPLICIT_IV_LEN)
|
||||
return 0;
|
||||
len -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
/* If decrypting correct for tag too */
|
||||
if (!EVP_CIPHER_CTX_encrypting(c)) {
|
||||
if (len < EVP_GCM_TLS_TAG_LEN)
|
||||
return 0;
|
||||
len -= EVP_GCM_TLS_TAG_LEN;
|
||||
}
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
|
||||
}
|
||||
/* Extra padding: tag appended to record */
|
||||
return EVP_GCM_TLS_TAG_LEN;
|
||||
|
||||
case EVP_CTRL_COPY:
|
||||
{
|
||||
EVP_CIPHER_CTX *out = ptr;
|
||||
EVP_ARIA_GCM_CTX *gctx_out = EVP_C_DATA(EVP_ARIA_GCM_CTX,out);
|
||||
if (gctx->gcm.key) {
|
||||
if (gctx->gcm.key != &gctx->ks)
|
||||
return 0;
|
||||
gctx_out->gcm.key = &gctx_out->ks;
|
||||
}
|
||||
if (gctx->iv == EVP_CIPHER_CTX_iv_noconst(c))
|
||||
gctx_out->iv = EVP_CIPHER_CTX_iv_noconst(out);
|
||||
else {
|
||||
gctx_out->iv = OPENSSL_malloc(gctx->ivlen);
|
||||
if (gctx_out->iv == NULL)
|
||||
return 0;
|
||||
memcpy(gctx_out->iv, gctx->iv, gctx->ivlen);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
default:
|
||||
return -1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int aria_gcm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx);
|
||||
int rv = -1;
|
||||
|
||||
/* Encrypt/decrypt must be performed in place */
|
||||
if (out != in
|
||||
|| len < (EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN))
|
||||
return -1;
|
||||
/*
|
||||
* Set IV from start of buffer or generate IV and write to start of
|
||||
* buffer.
|
||||
*/
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CIPHER_CTX_encrypting(ctx) ?
|
||||
EVP_CTRL_GCM_IV_GEN : EVP_CTRL_GCM_SET_IV_INV,
|
||||
EVP_GCM_TLS_EXPLICIT_IV_LEN, out) <= 0)
|
||||
goto err;
|
||||
/* Use saved AAD */
|
||||
if (CRYPTO_gcm128_aad(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
gctx->tls_aad_len))
|
||||
goto err;
|
||||
/* Fix buffer and length to point to payload */
|
||||
in += EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
out += EVP_GCM_TLS_EXPLICIT_IV_LEN;
|
||||
len -= EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
/* Encrypt payload */
|
||||
if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
|
||||
goto err;
|
||||
out += len;
|
||||
/* Finally write tag */
|
||||
CRYPTO_gcm128_tag(&gctx->gcm, out, EVP_GCM_TLS_TAG_LEN);
|
||||
rv = len + EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_TAG_LEN;
|
||||
} else {
|
||||
/* Decrypt */
|
||||
if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
|
||||
goto err;
|
||||
/* Retrieve tag */
|
||||
CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
EVP_GCM_TLS_TAG_LEN);
|
||||
/* If tag mismatch wipe buffer */
|
||||
if (CRYPTO_memcmp(EVP_CIPHER_CTX_buf_noconst(ctx), in + len,
|
||||
EVP_GCM_TLS_TAG_LEN)) {
|
||||
OPENSSL_cleanse(out, len);
|
||||
goto err;
|
||||
}
|
||||
rv = len;
|
||||
}
|
||||
|
||||
err:
|
||||
gctx->iv_set = 0;
|
||||
gctx->tls_aad_len = -1;
|
||||
return rv;
|
||||
}
|
||||
|
||||
static int aria_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_ARIA_GCM_CTX *gctx = EVP_C_DATA(EVP_ARIA_GCM_CTX,ctx);
|
||||
|
||||
/* If not set up, return error */
|
||||
if (!gctx->key_set)
|
||||
return -1;
|
||||
|
||||
if (gctx->tls_aad_len >= 0)
|
||||
return aria_gcm_tls_cipher(ctx, out, in, len);
|
||||
|
||||
if (!gctx->iv_set)
|
||||
return -1;
|
||||
if (in) {
|
||||
if (out == NULL) {
|
||||
if (CRYPTO_gcm128_aad(&gctx->gcm, in, len))
|
||||
return -1;
|
||||
} else if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (CRYPTO_gcm128_encrypt(&gctx->gcm, in, out, len))
|
||||
return -1;
|
||||
} else {
|
||||
if (CRYPTO_gcm128_decrypt(&gctx->gcm, in, out, len))
|
||||
return -1;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
if (!EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (gctx->taglen < 0)
|
||||
return -1;
|
||||
if (CRYPTO_gcm128_finish(&gctx->gcm,
|
||||
EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
gctx->taglen) != 0)
|
||||
return -1;
|
||||
gctx->iv_set = 0;
|
||||
return 0;
|
||||
}
|
||||
CRYPTO_gcm128_tag(&gctx->gcm, EVP_CIPHER_CTX_buf_noconst(ctx), 16);
|
||||
gctx->taglen = 16;
|
||||
/* Don't reuse the IV */
|
||||
gctx->iv_set = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int aria_ccm_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
int ret;
|
||||
EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx);
|
||||
|
||||
if (!iv && !key)
|
||||
return 1;
|
||||
|
||||
if (key) {
|
||||
ret = aria_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
|
||||
&cctx->ks.ks);
|
||||
CRYPTO_ccm128_init(&cctx->ccm, cctx->M, cctx->L,
|
||||
&cctx->ks, (block128_f) aria_encrypt);
|
||||
if (ret < 0) {
|
||||
EVPerr(EVP_F_ARIA_CCM_INIT_KEY,EVP_R_ARIA_KEY_SETUP_FAILED);
|
||||
return 0;
|
||||
}
|
||||
cctx->str = NULL;
|
||||
cctx->key_set = 1;
|
||||
}
|
||||
if (iv) {
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 15 - cctx->L);
|
||||
cctx->iv_set = 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int aria_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
{
|
||||
EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,c);
|
||||
|
||||
switch (type) {
|
||||
case EVP_CTRL_INIT:
|
||||
cctx->key_set = 0;
|
||||
cctx->iv_set = 0;
|
||||
cctx->L = 8;
|
||||
cctx->M = 12;
|
||||
cctx->tag_set = 0;
|
||||
cctx->len_set = 0;
|
||||
cctx->tls_aad_len = -1;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_TLS1_AAD:
|
||||
/* Save the AAD for later use */
|
||||
if (arg != EVP_AEAD_TLS1_AAD_LEN)
|
||||
return 0;
|
||||
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
|
||||
cctx->tls_aad_len = arg;
|
||||
{
|
||||
uint16_t len =
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] << 8
|
||||
| EVP_CIPHER_CTX_buf_noconst(c)[arg - 1];
|
||||
/* Correct length for explicit IV */
|
||||
if (len < EVP_CCM_TLS_EXPLICIT_IV_LEN)
|
||||
return 0;
|
||||
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
/* If decrypting correct for tag too */
|
||||
if (!EVP_CIPHER_CTX_encrypting(c)) {
|
||||
if (len < cctx->M)
|
||||
return 0;
|
||||
len -= cctx->M;
|
||||
}
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 2] = len >> 8;
|
||||
EVP_CIPHER_CTX_buf_noconst(c)[arg - 1] = len & 0xff;
|
||||
}
|
||||
/* Extra padding: tag appended to record */
|
||||
return cctx->M;
|
||||
|
||||
case EVP_CTRL_CCM_SET_IV_FIXED:
|
||||
/* Sanity check length */
|
||||
if (arg != EVP_CCM_TLS_FIXED_IV_LEN)
|
||||
return 0;
|
||||
/* Just copy to first part of IV */
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(c), ptr, arg);
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_SET_IVLEN:
|
||||
arg = 15 - arg;
|
||||
/* fall thru */
|
||||
case EVP_CTRL_CCM_SET_L:
|
||||
if (arg < 2 || arg > 8)
|
||||
return 0;
|
||||
cctx->L = arg;
|
||||
return 1;
|
||||
case EVP_CTRL_AEAD_SET_TAG:
|
||||
if ((arg & 1) || arg < 4 || arg > 16)
|
||||
return 0;
|
||||
if (EVP_CIPHER_CTX_encrypting(c) && ptr)
|
||||
return 0;
|
||||
if (ptr) {
|
||||
cctx->tag_set = 1;
|
||||
memcpy(EVP_CIPHER_CTX_buf_noconst(c), ptr, arg);
|
||||
}
|
||||
cctx->M = arg;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_AEAD_GET_TAG:
|
||||
if (!EVP_CIPHER_CTX_encrypting(c) || !cctx->tag_set)
|
||||
return 0;
|
||||
if (!CRYPTO_ccm128_tag(&cctx->ccm, ptr, (size_t)arg))
|
||||
return 0;
|
||||
cctx->tag_set = 0;
|
||||
cctx->iv_set = 0;
|
||||
cctx->len_set = 0;
|
||||
return 1;
|
||||
|
||||
case EVP_CTRL_COPY:
|
||||
{
|
||||
EVP_CIPHER_CTX *out = ptr;
|
||||
EVP_ARIA_CCM_CTX *cctx_out = EVP_C_DATA(EVP_ARIA_CCM_CTX,out);
|
||||
if (cctx->ccm.key) {
|
||||
if (cctx->ccm.key != &cctx->ks)
|
||||
return 0;
|
||||
cctx_out->ccm.key = &cctx_out->ks;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int aria_ccm_tls_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx);
|
||||
CCM128_CONTEXT *ccm = &cctx->ccm;
|
||||
|
||||
/* Encrypt/decrypt must be performed in place */
|
||||
if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)cctx->M))
|
||||
return -1;
|
||||
/* If encrypting set explicit IV from sequence number (start of AAD) */
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx))
|
||||
memcpy(out, EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
EVP_CCM_TLS_EXPLICIT_IV_LEN);
|
||||
/* Get rest of IV from explicit IV */
|
||||
memcpy(EVP_CIPHER_CTX_iv_noconst(ctx) + EVP_CCM_TLS_FIXED_IV_LEN, in,
|
||||
EVP_CCM_TLS_EXPLICIT_IV_LEN);
|
||||
/* Correct length value */
|
||||
len -= EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
|
||||
if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx), 15 - cctx->L,
|
||||
len))
|
||||
return -1;
|
||||
/* Use saved AAD */
|
||||
CRYPTO_ccm128_aad(ccm, EVP_CIPHER_CTX_buf_noconst(ctx), cctx->tls_aad_len);
|
||||
/* Fix buffer to point to payload */
|
||||
in += EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
out += EVP_CCM_TLS_EXPLICIT_IV_LEN;
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str)
|
||||
: CRYPTO_ccm128_encrypt(ccm, in, out, len))
|
||||
return -1;
|
||||
if (!CRYPTO_ccm128_tag(ccm, out + len, cctx->M))
|
||||
return -1;
|
||||
return len + EVP_CCM_TLS_EXPLICIT_IV_LEN + cctx->M;
|
||||
} else {
|
||||
if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len, cctx->str)
|
||||
: !CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
|
||||
unsigned char tag[16];
|
||||
if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
|
||||
if (!CRYPTO_memcmp(tag, in + len, cctx->M))
|
||||
return len;
|
||||
}
|
||||
}
|
||||
OPENSSL_cleanse(out, len);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int aria_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
EVP_ARIA_CCM_CTX *cctx = EVP_C_DATA(EVP_ARIA_CCM_CTX,ctx);
|
||||
CCM128_CONTEXT *ccm = &cctx->ccm;
|
||||
|
||||
/* If not set up, return error */
|
||||
if (!cctx->key_set)
|
||||
return -1;
|
||||
|
||||
if (cctx->tls_aad_len >= 0)
|
||||
return aria_ccm_tls_cipher(ctx, out, in, len);
|
||||
|
||||
/* EVP_*Final() doesn't return any data */
|
||||
if (in == NULL && out != NULL)
|
||||
return 0;
|
||||
|
||||
if (!cctx->iv_set)
|
||||
return -1;
|
||||
|
||||
if (!EVP_CIPHER_CTX_encrypting(ctx) && !cctx->tag_set)
|
||||
return -1;
|
||||
if (!out) {
|
||||
if (!in) {
|
||||
if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
15 - cctx->L, len))
|
||||
return -1;
|
||||
cctx->len_set = 1;
|
||||
return len;
|
||||
}
|
||||
/* If have AAD need message length */
|
||||
if (!cctx->len_set && len)
|
||||
return -1;
|
||||
CRYPTO_ccm128_aad(ccm, in, len);
|
||||
return len;
|
||||
}
|
||||
/* If not set length yet do it */
|
||||
if (!cctx->len_set) {
|
||||
if (CRYPTO_ccm128_setiv(ccm, EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
15 - cctx->L, len))
|
||||
return -1;
|
||||
cctx->len_set = 1;
|
||||
}
|
||||
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
||||
if (cctx->str ? CRYPTO_ccm128_encrypt_ccm64(ccm, in, out, len, cctx->str)
|
||||
: CRYPTO_ccm128_encrypt(ccm, in, out, len))
|
||||
return -1;
|
||||
cctx->tag_set = 1;
|
||||
return len;
|
||||
} else {
|
||||
int rv = -1;
|
||||
if (cctx->str ? !CRYPTO_ccm128_decrypt_ccm64(ccm, in, out, len,
|
||||
cctx->str) :
|
||||
!CRYPTO_ccm128_decrypt(ccm, in, out, len)) {
|
||||
unsigned char tag[16];
|
||||
if (CRYPTO_ccm128_tag(ccm, tag, cctx->M)) {
|
||||
if (!CRYPTO_memcmp(tag, EVP_CIPHER_CTX_buf_noconst(ctx),
|
||||
cctx->M))
|
||||
rv = len;
|
||||
}
|
||||
}
|
||||
if (rv == -1)
|
||||
OPENSSL_cleanse(out, len);
|
||||
cctx->iv_set = 0;
|
||||
cctx->tag_set = 0;
|
||||
cctx->len_set = 0;
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
#define ARIA_AUTH_FLAGS (EVP_CIPH_FLAG_DEFAULT_ASN1 \
|
||||
| EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER \
|
||||
| EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CTRL_INIT \
|
||||
| EVP_CIPH_CUSTOM_COPY | EVP_CIPH_FLAG_AEAD_CIPHER)
|
||||
|
||||
#define BLOCK_CIPHER_aead(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
|
||||
static const EVP_CIPHER aria_##keylen##_##mode = { \
|
||||
nid##_##keylen##_##nmode, \
|
||||
blocksize, keylen/8, ivlen, \
|
||||
ARIA_AUTH_FLAGS|EVP_CIPH_##MODE##_MODE, \
|
||||
aria_##mode##_init_key, \
|
||||
aria_##mode##_cipher, \
|
||||
NULL, \
|
||||
sizeof(EVP_ARIA_##MODE##_CTX), \
|
||||
NULL,NULL,aria_##mode##_ctrl,NULL }; \
|
||||
const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
|
||||
{ return (EVP_CIPHER*)&aria_##keylen##_##mode; }
|
||||
|
||||
BLOCK_CIPHER_aead(NID_aria, 128, 1, 12, gcm, gcm, GCM, 0)
|
||||
BLOCK_CIPHER_aead(NID_aria, 192, 1, 12, gcm, gcm, GCM, 0)
|
||||
BLOCK_CIPHER_aead(NID_aria, 256, 1, 12, gcm, gcm, GCM, 0)
|
||||
|
||||
BLOCK_CIPHER_aead(NID_aria, 128, 1, 12, ccm, ccm, CCM, 0)
|
||||
BLOCK_CIPHER_aead(NID_aria, 192, 1, 12, ccm, ccm, CCM, 0)
|
||||
BLOCK_CIPHER_aead(NID_aria, 256, 1, 12, ccm, ccm, CCM, 0)
|
||||
|
||||
#endif
|
||||
@@ -140,7 +140,7 @@ static const EVP_CIPHER chacha20 = {
|
||||
|
||||
const EVP_CIPHER *EVP_chacha20(void)
|
||||
{
|
||||
return (&chacha20);
|
||||
return &chacha20;
|
||||
}
|
||||
|
||||
# ifndef OPENSSL_NO_POLY1305
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ static const EVP_CIPHER n_cipher = {
|
||||
|
||||
const EVP_CIPHER *EVP_enc_null(void)
|
||||
{
|
||||
return (&n_cipher);
|
||||
return &n_cipher;
|
||||
}
|
||||
|
||||
static int null_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
|
||||
+8
-8
@@ -72,12 +72,12 @@ static const EVP_CIPHER r2_40_cbc_cipher = {
|
||||
|
||||
const EVP_CIPHER *EVP_rc2_64_cbc(void)
|
||||
{
|
||||
return (&r2_64_cbc_cipher);
|
||||
return &r2_64_cbc_cipher;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *EVP_rc2_40_cbc(void)
|
||||
{
|
||||
return (&r2_40_cbc_cipher);
|
||||
return &r2_40_cbc_cipher;
|
||||
}
|
||||
|
||||
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
@@ -94,13 +94,13 @@ static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
|
||||
|
||||
EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i);
|
||||
if (i == 128)
|
||||
return (RC2_128_MAGIC);
|
||||
return RC2_128_MAGIC;
|
||||
else if (i == 64)
|
||||
return (RC2_64_MAGIC);
|
||||
return RC2_64_MAGIC;
|
||||
else if (i == 40)
|
||||
return (RC2_40_MAGIC);
|
||||
return RC2_40_MAGIC;
|
||||
else
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rc2_magic_to_meth(int i)
|
||||
@@ -113,7 +113,7 @@ static int rc2_magic_to_meth(int i)
|
||||
return 40;
|
||||
else {
|
||||
EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
|
||||
(unsigned char *)EVP_CIPHER_CTX_original_iv(c),
|
||||
j);
|
||||
}
|
||||
return (i);
|
||||
return i;
|
||||
}
|
||||
|
||||
static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
|
||||
|
||||
+2
-2
@@ -58,12 +58,12 @@ static const EVP_CIPHER r4_40_cipher = {
|
||||
|
||||
const EVP_CIPHER *EVP_rc4(void)
|
||||
{
|
||||
return (&r4_cipher);
|
||||
return &r4_cipher;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *EVP_rc4_40(void)
|
||||
{
|
||||
return (&r4_40_cipher);
|
||||
return &r4_40_cipher;
|
||||
}
|
||||
|
||||
static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
|
||||
@@ -257,6 +257,6 @@ static EVP_CIPHER r4_hmac_md5_cipher = {
|
||||
|
||||
const EVP_CIPHER *EVP_rc4_hmac_md5(void)
|
||||
{
|
||||
return (&r4_hmac_md5_cipher);
|
||||
return &r4_hmac_md5_cipher;
|
||||
}
|
||||
#endif
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
#ifndef OPENSSL_NO_RC5
|
||||
|
||||
# include <openssl/evp.h>
|
||||
# include <internal/evp_int.h>
|
||||
# include "internal/evp_int.h"
|
||||
# include <openssl/objects.h>
|
||||
# include "evp_locl.h"
|
||||
# include <openssl/rc5.h>
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2017 Ribose Inc. All Rights Reserved.
|
||||
* Ported from Ribose contributions from Botan.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
* in the file LICENSE in the source distribution or at
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
#include "internal/cryptlib.h"
|
||||
#ifndef OPENSSL_NO_SM4
|
||||
# include <openssl/evp.h>
|
||||
# include <openssl/modes.h>
|
||||
# include "internal/sm4.h"
|
||||
# include "internal/evp_int.h"
|
||||
|
||||
typedef struct {
|
||||
SM4_KEY ks;
|
||||
} EVP_SM4_KEY;
|
||||
|
||||
static int sm4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
const unsigned char *iv, int enc)
|
||||
{
|
||||
SM4_set_key(key, EVP_CIPHER_CTX_get_cipher_data(ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void sm4_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
||||
size_t len, const SM4_KEY *key,
|
||||
unsigned char *ivec, const int enc)
|
||||
{
|
||||
if (enc)
|
||||
CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
|
||||
(block128_f)SM4_encrypt);
|
||||
else
|
||||
CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
|
||||
(block128_f)SM4_decrypt);
|
||||
}
|
||||
|
||||
static void sm4_cfb128_encrypt(const unsigned char *in, unsigned char *out,
|
||||
size_t length, const SM4_KEY *key,
|
||||
unsigned char *ivec, int *num, const int enc)
|
||||
{
|
||||
CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
|
||||
(block128_f)SM4_encrypt);
|
||||
}
|
||||
|
||||
static void sm4_ecb_encrypt(const unsigned char *in, unsigned char *out,
|
||||
const SM4_KEY *key, const int enc)
|
||||
{
|
||||
if (enc)
|
||||
SM4_encrypt(in, out, key);
|
||||
else
|
||||
SM4_decrypt(in, out, key);
|
||||
}
|
||||
|
||||
static void sm4_ofb128_encrypt(const unsigned char *in, unsigned char *out,
|
||||
size_t length, const SM4_KEY *key,
|
||||
unsigned char *ivec, int *num)
|
||||
{
|
||||
CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
|
||||
(block128_f)SM4_encrypt);
|
||||
}
|
||||
|
||||
IMPLEMENT_BLOCK_CIPHER(sm4, ks, sm4, EVP_SM4_KEY, NID_sm4,
|
||||
16, 16, 16, 128, EVP_CIPH_FLAG_DEFAULT_ASN1,
|
||||
sm4_init_key, 0, 0, 0, 0)
|
||||
|
||||
static int sm4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
unsigned int num = EVP_CIPHER_CTX_num(ctx);
|
||||
EVP_SM4_KEY *dat = EVP_C_DATA(EVP_SM4_KEY, ctx);
|
||||
|
||||
CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
|
||||
EVP_CIPHER_CTX_iv_noconst(ctx),
|
||||
EVP_CIPHER_CTX_buf_noconst(ctx), &num,
|
||||
(block128_f)SM4_encrypt);
|
||||
EVP_CIPHER_CTX_set_num(ctx, num);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const EVP_CIPHER sm4_ctr_mode = {
|
||||
NID_sm4_ctr, 1, 16, 16,
|
||||
EVP_CIPH_CTR_MODE,
|
||||
sm4_init_key,
|
||||
sm4_ctr_cipher,
|
||||
NULL,
|
||||
sizeof(EVP_SM4_KEY),
|
||||
NULL, NULL, NULL, NULL
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_sm4_ctr(void)
|
||||
{
|
||||
return &sm4_ctr_mode;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -46,7 +46,7 @@ static const EVP_CIPHER d_xcbc_cipher = {
|
||||
|
||||
const EVP_CIPHER *EVP_desx_cbc(void)
|
||||
{
|
||||
return (&d_xcbc_cipher);
|
||||
return &d_xcbc_cipher;
|
||||
}
|
||||
|
||||
static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
|
||||
+8
-8
@@ -212,7 +212,7 @@ int EVP_EncodeBlock(unsigned char *t, const unsigned char *f, int dlen)
|
||||
}
|
||||
|
||||
*t = '\0';
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void EVP_DecodeInit(EVP_ENCODE_CTX *ctx)
|
||||
@@ -345,7 +345,7 @@ end:
|
||||
/* Legacy behaviour. This should probably rather be zeroed on error. */
|
||||
*outl = ret;
|
||||
ctx->num = n;
|
||||
return (rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
|
||||
@@ -367,7 +367,7 @@ int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
|
||||
n--;
|
||||
|
||||
if (n % 4 != 0)
|
||||
return (-1);
|
||||
return -1;
|
||||
|
||||
for (i = 0; i < n; i += 4) {
|
||||
a = conv_ascii2bin(*(f++));
|
||||
@@ -375,7 +375,7 @@ int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
|
||||
c = conv_ascii2bin(*(f++));
|
||||
d = conv_ascii2bin(*(f++));
|
||||
if ((a & 0x80) || (b & 0x80) || (c & 0x80) || (d & 0x80))
|
||||
return (-1);
|
||||
return -1;
|
||||
l = ((((unsigned long)a) << 18L) |
|
||||
(((unsigned long)b) << 12L) |
|
||||
(((unsigned long)c) << 6L) | (((unsigned long)d)));
|
||||
@@ -384,7 +384,7 @@ int EVP_DecodeBlock(unsigned char *t, const unsigned char *f, int n)
|
||||
*(t++) = (unsigned char)(l) & 0xff;
|
||||
ret += 3;
|
||||
}
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
|
||||
@@ -395,10 +395,10 @@ int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl)
|
||||
if (ctx->num != 0) {
|
||||
i = EVP_DecodeBlock(out, ctx->enc_data, ctx->num);
|
||||
if (i < 0)
|
||||
return (-1);
|
||||
return -1;
|
||||
ctx->num = 0;
|
||||
*outl = i;
|
||||
return (1);
|
||||
return 1;
|
||||
} else
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
+1
-10
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2012-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -8,7 +8,6 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <openssl/crypto.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include <openssl/conf.h>
|
||||
@@ -38,16 +37,8 @@ static int alg_module_init(CONF_IMODULE *md, const CONF *cnf)
|
||||
return 0;
|
||||
}
|
||||
if (m > 0) {
|
||||
#ifdef OPENSSL_FIPS
|
||||
if (!FIPS_mode() && !FIPS_mode_set(1)) {
|
||||
EVPerr(EVP_F_ALG_MODULE_INIT,
|
||||
EVP_R_ERROR_SETTING_FIPS_MODE);
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
EVPerr(EVP_F_ALG_MODULE_INIT, EVP_R_FIPS_MODE_NOT_SUPPORTED);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
EVPerr(EVP_F_ALG_MODULE_INIT, EVP_R_UNKNOWN_OPTION);
|
||||
|
||||
@@ -523,7 +523,7 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
||||
if (b > 1) {
|
||||
if (ctx->buf_len || !ctx->final_used) {
|
||||
EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
OPENSSL_assert(b <= sizeof(ctx->final));
|
||||
|
||||
@@ -534,12 +534,12 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
||||
n = ctx->final[b - 1];
|
||||
if (n == 0 || n > (int)b) {
|
||||
EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < n; i++) {
|
||||
if (ctx->final[--b] != n) {
|
||||
EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
n = ctx->cipher->block_size - n;
|
||||
@@ -548,7 +548,7 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
||||
*outl = n;
|
||||
} else
|
||||
*outl = 0;
|
||||
return (1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
|
||||
|
||||
+222
-154
@@ -8,166 +8,235 @@
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/evperr.h>
|
||||
|
||||
/* BEGIN ERROR CODES */
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
# define ERR_FUNC(func) ERR_PACK(ERR_LIB_EVP,func,0)
|
||||
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_EVP,0,reason)
|
||||
|
||||
static ERR_STRING_DATA EVP_str_functs[] = {
|
||||
{ERR_FUNC(EVP_F_AESNI_INIT_KEY), "aesni_init_key"},
|
||||
{ERR_FUNC(EVP_F_AES_INIT_KEY), "aes_init_key"},
|
||||
{ERR_FUNC(EVP_F_AES_OCB_CIPHER), "aes_ocb_cipher"},
|
||||
{ERR_FUNC(EVP_F_AES_T4_INIT_KEY), "aes_t4_init_key"},
|
||||
{ERR_FUNC(EVP_F_AES_WRAP_CIPHER), "aes_wrap_cipher"},
|
||||
{ERR_FUNC(EVP_F_ALG_MODULE_INIT), "alg_module_init"},
|
||||
{ERR_FUNC(EVP_F_CAMELLIA_INIT_KEY), "camellia_init_key"},
|
||||
{ERR_FUNC(EVP_F_CHACHA20_POLY1305_CTRL), "chacha20_poly1305_ctrl"},
|
||||
{ERR_FUNC(EVP_F_CMLL_T4_INIT_KEY), "cmll_t4_init_key"},
|
||||
{ERR_FUNC(EVP_F_DES_EDE3_WRAP_CIPHER), "des_ede3_wrap_cipher"},
|
||||
{ERR_FUNC(EVP_F_DO_SIGVER_INIT), "do_sigver_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_CIPHERINIT_EX), "EVP_CipherInit_ex"},
|
||||
{ERR_FUNC(EVP_F_EVP_CIPHER_CTX_COPY), "EVP_CIPHER_CTX_copy"},
|
||||
{ERR_FUNC(EVP_F_EVP_CIPHER_CTX_CTRL), "EVP_CIPHER_CTX_ctrl"},
|
||||
{ERR_FUNC(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH),
|
||||
static const ERR_STRING_DATA EVP_str_functs[] = {
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_AESNI_INIT_KEY, 0), "aesni_init_key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_AES_INIT_KEY, 0), "aes_init_key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_AES_OCB_CIPHER, 0), "aes_ocb_cipher"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_AES_T4_INIT_KEY, 0), "aes_t4_init_key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_AES_WRAP_CIPHER, 0), "aes_wrap_cipher"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_ALG_MODULE_INIT, 0), "alg_module_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_ARIA_CCM_INIT_KEY, 0), "aria_ccm_init_key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_ARIA_GCM_INIT_KEY, 0), "aria_gcm_init_key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_ARIA_INIT_KEY, 0), "aria_init_key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_CAMELLIA_INIT_KEY, 0), "camellia_init_key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_CHACHA20_POLY1305_CTRL, 0),
|
||||
"chacha20_poly1305_ctrl"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_CMLL_T4_INIT_KEY, 0), "cmll_t4_init_key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_DES_EDE3_WRAP_CIPHER, 0),
|
||||
"des_ede3_wrap_cipher"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_DO_SIGVER_INIT, 0), "do_sigver_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_CIPHERINIT_EX, 0), "EVP_CipherInit_ex"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_CIPHER_CTX_COPY, 0),
|
||||
"EVP_CIPHER_CTX_copy"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_CIPHER_CTX_CTRL, 0),
|
||||
"EVP_CIPHER_CTX_ctrl"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, 0),
|
||||
"EVP_CIPHER_CTX_set_key_length"},
|
||||
{ERR_FUNC(EVP_F_EVP_DECRYPTFINAL_EX), "EVP_DecryptFinal_ex"},
|
||||
{ERR_FUNC(EVP_F_EVP_DECRYPTUPDATE), "EVP_DecryptUpdate"},
|
||||
{ERR_FUNC(EVP_F_EVP_DIGESTINIT_EX), "EVP_DigestInit_ex"},
|
||||
{ERR_FUNC(EVP_F_EVP_ENCRYPTFINAL_EX), "EVP_EncryptFinal_ex"},
|
||||
{ERR_FUNC(EVP_F_EVP_ENCRYPTUPDATE), "EVP_EncryptUpdate"},
|
||||
{ERR_FUNC(EVP_F_EVP_MD_CTX_COPY_EX), "EVP_MD_CTX_copy_ex"},
|
||||
{ERR_FUNC(EVP_F_EVP_MD_SIZE), "EVP_MD_size"},
|
||||
{ERR_FUNC(EVP_F_EVP_OPENINIT), "EVP_OpenInit"},
|
||||
{ERR_FUNC(EVP_F_EVP_PBE_ALG_ADD), "EVP_PBE_alg_add"},
|
||||
{ERR_FUNC(EVP_F_EVP_PBE_ALG_ADD_TYPE), "EVP_PBE_alg_add_type"},
|
||||
{ERR_FUNC(EVP_F_EVP_PBE_CIPHERINIT), "EVP_PBE_CipherInit"},
|
||||
{ERR_FUNC(EVP_F_EVP_PBE_SCRYPT), "EVP_PBE_scrypt"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKCS82PKEY), "EVP_PKCS82PKEY"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY2PKCS8), "EVP_PKEY2PKCS8"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_ASN1_ADD0), "EVP_PKEY_asn1_add0"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_COPY_PARAMETERS), "EVP_PKEY_copy_parameters"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_CTX_CTRL), "EVP_PKEY_CTX_ctrl"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_CTX_CTRL_STR), "EVP_PKEY_CTX_ctrl_str"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_CTX_DUP), "EVP_PKEY_CTX_dup"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_DECRYPT), "EVP_PKEY_decrypt"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_DECRYPT_INIT), "EVP_PKEY_decrypt_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_DECRYPT_OLD), "EVP_PKEY_decrypt_old"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_DERIVE), "EVP_PKEY_derive"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_DERIVE_INIT), "EVP_PKEY_derive_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_DERIVE_SET_PEER), "EVP_PKEY_derive_set_peer"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_ENCRYPT), "EVP_PKEY_encrypt"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_ENCRYPT_INIT), "EVP_PKEY_encrypt_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_ENCRYPT_OLD), "EVP_PKEY_encrypt_old"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_DH), "EVP_PKEY_get0_DH"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_DSA), "EVP_PKEY_get0_DSA"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_EC_KEY), "EVP_PKEY_get0_EC_KEY"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_HMAC), "EVP_PKEY_get0_hmac"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_GET0_RSA), "EVP_PKEY_get0_RSA"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_KEYGEN), "EVP_PKEY_keygen"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_KEYGEN_INIT), "EVP_PKEY_keygen_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_NEW), "EVP_PKEY_new"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_PARAMGEN), "EVP_PKEY_paramgen"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_PARAMGEN_INIT), "EVP_PKEY_paramgen_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_SET1_ENGINE), "EVP_PKEY_set1_engine"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_SIGN), "EVP_PKEY_sign"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_SIGN_INIT), "EVP_PKEY_sign_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_VERIFY), "EVP_PKEY_verify"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_VERIFY_INIT), "EVP_PKEY_verify_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_VERIFY_RECOVER), "EVP_PKEY_verify_recover"},
|
||||
{ERR_FUNC(EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT),
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, 0),
|
||||
"EVP_DecryptFinal_ex"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTUPDATE, 0), "EVP_DecryptUpdate"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DIGESTFINALXOF, 0), "EVP_DigestFinalXOF"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DIGESTINIT_EX, 0), "EVP_DigestInit_ex"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_ENCRYPTFINAL_EX, 0),
|
||||
"EVP_EncryptFinal_ex"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_ENCRYPTUPDATE, 0), "EVP_EncryptUpdate"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MD_CTX_COPY_EX, 0), "EVP_MD_CTX_copy_ex"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_MD_SIZE, 0), "EVP_MD_size"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_OPENINIT, 0), "EVP_OpenInit"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PBE_ALG_ADD, 0), "EVP_PBE_alg_add"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PBE_ALG_ADD_TYPE, 0),
|
||||
"EVP_PBE_alg_add_type"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PBE_CIPHERINIT, 0), "EVP_PBE_CipherInit"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PBE_SCRYPT, 0), "EVP_PBE_scrypt"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKCS82PKEY, 0), "EVP_PKCS82PKEY"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY2PKCS8, 0), "EVP_PKEY2PKCS8"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_ASN1_ADD0, 0), "EVP_PKEY_asn1_add0"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CHECK, 0), "EVP_PKEY_check"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_COPY_PARAMETERS, 0),
|
||||
"EVP_PKEY_copy_parameters"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, 0), "EVP_PKEY_CTX_ctrl"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL_STR, 0),
|
||||
"EVP_PKEY_CTX_ctrl_str"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_DUP, 0), "EVP_PKEY_CTX_dup"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_MD, 0), "EVP_PKEY_CTX_md"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_DECRYPT, 0), "EVP_PKEY_decrypt"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_DECRYPT_INIT, 0),
|
||||
"EVP_PKEY_decrypt_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_DECRYPT_OLD, 0),
|
||||
"EVP_PKEY_decrypt_old"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_DERIVE, 0), "EVP_PKEY_derive"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_DERIVE_INIT, 0),
|
||||
"EVP_PKEY_derive_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_DERIVE_SET_PEER, 0),
|
||||
"EVP_PKEY_derive_set_peer"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_ENCRYPT, 0), "EVP_PKEY_encrypt"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_ENCRYPT_INIT, 0),
|
||||
"EVP_PKEY_encrypt_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_ENCRYPT_OLD, 0),
|
||||
"EVP_PKEY_encrypt_old"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_GET0_DH, 0), "EVP_PKEY_get0_DH"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_GET0_DSA, 0), "EVP_PKEY_get0_DSA"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_GET0_EC_KEY, 0),
|
||||
"EVP_PKEY_get0_EC_KEY"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_GET0_HMAC, 0), "EVP_PKEY_get0_hmac"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_GET0_POLY1305, 0),
|
||||
"EVP_PKEY_get0_poly1305"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_GET0_RSA, 0), "EVP_PKEY_get0_RSA"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_GET0_SIPHASH, 0),
|
||||
"EVP_PKEY_get0_siphash"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_KEYGEN, 0), "EVP_PKEY_keygen"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_KEYGEN_INIT, 0),
|
||||
"EVP_PKEY_keygen_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_NEW, 0), "EVP_PKEY_new"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_PARAMGEN, 0), "EVP_PKEY_paramgen"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_PARAMGEN_INIT, 0),
|
||||
"EVP_PKEY_paramgen_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_PARAM_CHECK, 0),
|
||||
"EVP_PKEY_param_check"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_PUBLIC_CHECK, 0),
|
||||
"EVP_PKEY_public_check"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_SET1_ENGINE, 0),
|
||||
"EVP_PKEY_set1_engine"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_SIGN, 0), "EVP_PKEY_sign"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_SIGN_INIT, 0), "EVP_PKEY_sign_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_VERIFY, 0), "EVP_PKEY_verify"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_VERIFY_INIT, 0),
|
||||
"EVP_PKEY_verify_init"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_VERIFY_RECOVER, 0),
|
||||
"EVP_PKEY_verify_recover"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_VERIFY_RECOVER_INIT, 0),
|
||||
"EVP_PKEY_verify_recover_init"},
|
||||
{ERR_FUNC(EVP_F_EVP_SIGNFINAL), "EVP_SignFinal"},
|
||||
{ERR_FUNC(EVP_F_EVP_VERIFYFINAL), "EVP_VerifyFinal"},
|
||||
{ERR_FUNC(EVP_F_INT_CTX_NEW), "int_ctx_new"},
|
||||
{ERR_FUNC(EVP_F_PKCS5_PBE_KEYIVGEN), "PKCS5_PBE_keyivgen"},
|
||||
{ERR_FUNC(EVP_F_PKCS5_V2_PBE_KEYIVGEN), "PKCS5_v2_PBE_keyivgen"},
|
||||
{ERR_FUNC(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN), "PKCS5_v2_PBKDF2_keyivgen"},
|
||||
{ERR_FUNC(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN), "PKCS5_v2_scrypt_keyivgen"},
|
||||
{ERR_FUNC(EVP_F_PKEY_SET_TYPE), "pkey_set_type"},
|
||||
{ERR_FUNC(EVP_F_RC2_MAGIC_TO_METH), "rc2_magic_to_meth"},
|
||||
{ERR_FUNC(EVP_F_RC5_CTRL), "rc5_ctrl"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_SIGNFINAL, 0), "EVP_SignFinal"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_VERIFYFINAL, 0), "EVP_VerifyFinal"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_INT_CTX_NEW, 0), "int_ctx_new"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKCS5_PBE_KEYIVGEN, 0), "PKCS5_PBE_keyivgen"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKCS5_V2_PBE_KEYIVGEN, 0),
|
||||
"PKCS5_v2_PBE_keyivgen"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, 0),
|
||||
"PKCS5_v2_PBKDF2_keyivgen"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, 0),
|
||||
"PKCS5_v2_scrypt_keyivgen"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_PKEY_SET_TYPE, 0), "pkey_set_type"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_RC2_MAGIC_TO_METH, 0), "rc2_magic_to_meth"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_RC5_CTRL, 0), "rc5_ctrl"},
|
||||
{ERR_PACK(ERR_LIB_EVP, EVP_F_UPDATE, 0), "update"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
static ERR_STRING_DATA EVP_str_reasons[] = {
|
||||
{ERR_REASON(EVP_R_AES_KEY_SETUP_FAILED), "aes key setup failed"},
|
||||
{ERR_REASON(EVP_R_BAD_DECRYPT), "bad decrypt"},
|
||||
{ERR_REASON(EVP_R_BUFFER_TOO_SMALL), "buffer too small"},
|
||||
{ERR_REASON(EVP_R_CAMELLIA_KEY_SETUP_FAILED),
|
||||
"camellia key setup failed"},
|
||||
{ERR_REASON(EVP_R_CIPHER_PARAMETER_ERROR), "cipher parameter error"},
|
||||
{ERR_REASON(EVP_R_COMMAND_NOT_SUPPORTED), "command not supported"},
|
||||
{ERR_REASON(EVP_R_COPY_ERROR), "copy error"},
|
||||
{ERR_REASON(EVP_R_CTRL_NOT_IMPLEMENTED), "ctrl not implemented"},
|
||||
{ERR_REASON(EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED),
|
||||
"ctrl operation not implemented"},
|
||||
{ERR_REASON(EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH),
|
||||
"data not multiple of block length"},
|
||||
{ERR_REASON(EVP_R_DECODE_ERROR), "decode error"},
|
||||
{ERR_REASON(EVP_R_DIFFERENT_KEY_TYPES), "different key types"},
|
||||
{ERR_REASON(EVP_R_DIFFERENT_PARAMETERS), "different parameters"},
|
||||
{ERR_REASON(EVP_R_ERROR_LOADING_SECTION), "error loading section"},
|
||||
{ERR_REASON(EVP_R_ERROR_SETTING_FIPS_MODE), "error setting fips mode"},
|
||||
{ERR_REASON(EVP_R_EXPECTING_AN_HMAC_KEY), "expecting an hmac key"},
|
||||
{ERR_REASON(EVP_R_EXPECTING_AN_RSA_KEY), "expecting an rsa key"},
|
||||
{ERR_REASON(EVP_R_EXPECTING_A_DH_KEY), "expecting a dh key"},
|
||||
{ERR_REASON(EVP_R_EXPECTING_A_DSA_KEY), "expecting a dsa key"},
|
||||
{ERR_REASON(EVP_R_EXPECTING_A_EC_KEY), "expecting a ec key"},
|
||||
{ERR_REASON(EVP_R_FIPS_MODE_NOT_SUPPORTED), "fips mode not supported"},
|
||||
{ERR_REASON(EVP_R_ILLEGAL_SCRYPT_PARAMETERS),
|
||||
"illegal scrypt parameters"},
|
||||
{ERR_REASON(EVP_R_INITIALIZATION_ERROR), "initialization error"},
|
||||
{ERR_REASON(EVP_R_INPUT_NOT_INITIALIZED), "input not initialized"},
|
||||
{ERR_REASON(EVP_R_INVALID_DIGEST), "invalid digest"},
|
||||
{ERR_REASON(EVP_R_INVALID_FIPS_MODE), "invalid fips mode"},
|
||||
{ERR_REASON(EVP_R_INVALID_KEY), "invalid key"},
|
||||
{ERR_REASON(EVP_R_INVALID_KEY_LENGTH), "invalid key length"},
|
||||
{ERR_REASON(EVP_R_INVALID_OPERATION), "invalid operation"},
|
||||
{ERR_REASON(EVP_R_KEYGEN_FAILURE), "keygen failure"},
|
||||
{ERR_REASON(EVP_R_MEMORY_LIMIT_EXCEEDED), "memory limit exceeded"},
|
||||
{ERR_REASON(EVP_R_MESSAGE_DIGEST_IS_NULL), "message digest is null"},
|
||||
{ERR_REASON(EVP_R_METHOD_NOT_SUPPORTED), "method not supported"},
|
||||
{ERR_REASON(EVP_R_MISSING_PARAMETERS), "missing parameters"},
|
||||
{ERR_REASON(EVP_R_NO_CIPHER_SET), "no cipher set"},
|
||||
{ERR_REASON(EVP_R_NO_DEFAULT_DIGEST), "no default digest"},
|
||||
{ERR_REASON(EVP_R_NO_DIGEST_SET), "no digest set"},
|
||||
{ERR_REASON(EVP_R_NO_KEY_SET), "no key set"},
|
||||
{ERR_REASON(EVP_R_NO_OPERATION_SET), "no operation set"},
|
||||
{ERR_REASON(EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE),
|
||||
"operation not supported for this keytype"},
|
||||
{ERR_REASON(EVP_R_OPERATON_NOT_INITIALIZED), "operaton not initialized"},
|
||||
{ERR_REASON(EVP_R_PARTIALLY_OVERLAPPING),
|
||||
"partially overlapping buffers"},
|
||||
{ERR_REASON(EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED),
|
||||
"pkey application asn1 method already registered"},
|
||||
{ERR_REASON(EVP_R_PKEY_ASN1_METHOD_ALREADY_REGISTERED),
|
||||
"pkey asn1 method already registered"},
|
||||
{ERR_REASON(EVP_R_PRIVATE_KEY_DECODE_ERROR), "private key decode error"},
|
||||
{ERR_REASON(EVP_R_PRIVATE_KEY_ENCODE_ERROR), "private key encode error"},
|
||||
{ERR_REASON(EVP_R_PUBLIC_KEY_NOT_RSA), "public key not rsa"},
|
||||
{ERR_REASON(EVP_R_UNKNOWN_CIPHER), "unknown cipher"},
|
||||
{ERR_REASON(EVP_R_UNKNOWN_DIGEST), "unknown digest"},
|
||||
{ERR_REASON(EVP_R_UNKNOWN_OPTION), "unknown option"},
|
||||
{ERR_REASON(EVP_R_UNKNOWN_PBE_ALGORITHM), "unknown pbe algorithm"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_ALGORITHM), "unsupported algorithm"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_CIPHER), "unsupported cipher"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_KEYLENGTH), "unsupported keylength"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION),
|
||||
"unsupported key derivation function"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_KEY_SIZE), "unsupported key size"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_NUMBER_OF_ROUNDS),
|
||||
"unsupported number of rounds"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_PRF), "unsupported prf"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM),
|
||||
"unsupported private key algorithm"},
|
||||
{ERR_REASON(EVP_R_UNSUPPORTED_SALT_TYPE), "unsupported salt type"},
|
||||
{ERR_REASON(EVP_R_WRAP_MODE_NOT_ALLOWED), "wrap mode not allowed"},
|
||||
{ERR_REASON(EVP_R_WRONG_FINAL_BLOCK_LENGTH), "wrong final block length"},
|
||||
static const ERR_STRING_DATA EVP_str_reasons[] = {
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_AES_KEY_SETUP_FAILED),
|
||||
"aes key setup failed"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_ARIA_KEY_SETUP_FAILED),
|
||||
"aria key setup failed"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_BAD_DECRYPT), "bad decrypt"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_BUFFER_TOO_SMALL), "buffer too small"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_CAMELLIA_KEY_SETUP_FAILED),
|
||||
"camellia key setup failed"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_CIPHER_PARAMETER_ERROR),
|
||||
"cipher parameter error"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_COMMAND_NOT_SUPPORTED),
|
||||
"command not supported"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_COPY_ERROR), "copy error"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_CTRL_NOT_IMPLEMENTED),
|
||||
"ctrl not implemented"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED),
|
||||
"ctrl operation not implemented"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH),
|
||||
"data not multiple of block length"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_DECODE_ERROR), "decode error"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_DIFFERENT_KEY_TYPES),
|
||||
"different key types"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_DIFFERENT_PARAMETERS),
|
||||
"different parameters"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_ERROR_LOADING_SECTION),
|
||||
"error loading section"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_ERROR_SETTING_FIPS_MODE),
|
||||
"error setting fips mode"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_AN_HMAC_KEY),
|
||||
"expecting an hmac key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_AN_RSA_KEY),
|
||||
"expecting an rsa key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_A_DH_KEY), "expecting a dh key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_A_DSA_KEY),
|
||||
"expecting a dsa key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_A_EC_KEY), "expecting a ec key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_A_POLY1305_KEY),
|
||||
"expecting a poly1305 key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_EXPECTING_A_SIPHASH_KEY),
|
||||
"expecting a siphash key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_FIPS_MODE_NOT_SUPPORTED),
|
||||
"fips mode not supported"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_ILLEGAL_SCRYPT_PARAMETERS),
|
||||
"illegal scrypt parameters"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INITIALIZATION_ERROR),
|
||||
"initialization error"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INPUT_NOT_INITIALIZED),
|
||||
"input not initialized"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INVALID_DIGEST), "invalid digest"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INVALID_FIPS_MODE), "invalid fips mode"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INVALID_KEY), "invalid key"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INVALID_KEY_LENGTH), "invalid key length"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_INVALID_OPERATION), "invalid operation"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_KEYGEN_FAILURE), "keygen failure"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_MEMORY_LIMIT_EXCEEDED),
|
||||
"memory limit exceeded"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_MESSAGE_DIGEST_IS_NULL),
|
||||
"message digest is null"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_METHOD_NOT_SUPPORTED),
|
||||
"method not supported"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_MISSING_PARAMETERS), "missing parameters"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NOT_XOF_OR_INVALID_LENGTH),
|
||||
"not XOF or invalid length"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_CIPHER_SET), "no cipher set"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_DEFAULT_DIGEST), "no default digest"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_DIGEST_SET), "no digest set"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_KEY_SET), "no key set"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_NO_OPERATION_SET), "no operation set"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_ONLY_ONESHOT_SUPPORTED),
|
||||
"only oneshot supported"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE),
|
||||
"operation not supported for this keytype"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_OPERATON_NOT_INITIALIZED),
|
||||
"operaton not initialized"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_PARTIALLY_OVERLAPPING),
|
||||
"partially overlapping buffers"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED),
|
||||
"pkey application asn1 method already registered"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_PRIVATE_KEY_DECODE_ERROR),
|
||||
"private key decode error"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_PRIVATE_KEY_ENCODE_ERROR),
|
||||
"private key encode error"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_PUBLIC_KEY_NOT_RSA), "public key not rsa"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_CIPHER), "unknown cipher"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_DIGEST), "unknown digest"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_OPTION), "unknown option"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNKNOWN_PBE_ALGORITHM),
|
||||
"unknown pbe algorithm"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_ALGORITHM),
|
||||
"unsupported algorithm"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_CIPHER), "unsupported cipher"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_KEYLENGTH),
|
||||
"unsupported keylength"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION),
|
||||
"unsupported key derivation function"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_KEY_SIZE),
|
||||
"unsupported key size"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_NUMBER_OF_ROUNDS),
|
||||
"unsupported number of rounds"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_PRF), "unsupported prf"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM),
|
||||
"unsupported private key algorithm"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_UNSUPPORTED_SALT_TYPE),
|
||||
"unsupported salt type"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_WRAP_MODE_NOT_ALLOWED),
|
||||
"wrap mode not allowed"},
|
||||
{ERR_PACK(ERR_LIB_EVP, 0, EVP_R_WRONG_FINAL_BLOCK_LENGTH),
|
||||
"wrong final block length"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
@@ -176,10 +245,9 @@ static ERR_STRING_DATA EVP_str_reasons[] = {
|
||||
int ERR_load_EVP_strings(void)
|
||||
{
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
if (ERR_func_error_string(EVP_str_functs[0].error) == NULL) {
|
||||
ERR_load_strings(0, EVP_str_functs);
|
||||
ERR_load_strings(0, EVP_str_reasons);
|
||||
ERR_load_strings_const(EVP_str_functs);
|
||||
ERR_load_strings_const(EVP_str_reasons);
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/ui.h>
|
||||
|
||||
#ifndef OPENSSL_NO_UI
|
||||
/* should be init to zeros. */
|
||||
static char prompt_string[80];
|
||||
|
||||
@@ -31,9 +30,9 @@ void EVP_set_pw_prompt(const char *prompt)
|
||||
char *EVP_get_pw_prompt(void)
|
||||
{
|
||||
if (prompt_string[0] == '\0')
|
||||
return (NULL);
|
||||
return NULL;
|
||||
else
|
||||
return (prompt_string);
|
||||
return prompt_string;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -71,7 +70,6 @@ int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
|
||||
UI_free(ui);
|
||||
return ret;
|
||||
}
|
||||
#endif /* OPENSSL_NO_UI */
|
||||
|
||||
int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
|
||||
const unsigned char *salt, const unsigned char *data,
|
||||
@@ -89,7 +87,7 @@ int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
|
||||
OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);
|
||||
|
||||
if (data == NULL)
|
||||
return (nkey);
|
||||
return nkey;
|
||||
|
||||
c = EVP_MD_CTX_new();
|
||||
if (c == NULL)
|
||||
|
||||
@@ -40,7 +40,7 @@ int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
|
||||
}
|
||||
} else
|
||||
ret = -1;
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
|
||||
@@ -69,7 +69,7 @@ int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
|
||||
}
|
||||
} else
|
||||
ret = -1;
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
|
||||
@@ -82,11 +82,11 @@ int EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
|
||||
OPENSSL_assert(l <= sizeof(c->iv));
|
||||
i = ASN1_TYPE_get_octetstring(type, c->oiv, l);
|
||||
if (i != (int)l)
|
||||
return (-1);
|
||||
return -1;
|
||||
else if (i > 0)
|
||||
memcpy(c->iv, c->oiv, l);
|
||||
}
|
||||
return (i);
|
||||
return i;
|
||||
}
|
||||
|
||||
int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
|
||||
@@ -99,7 +99,7 @@ int EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
|
||||
OPENSSL_assert(j <= sizeof(c->iv));
|
||||
i = ASN1_TYPE_set_octetstring(type, c->oiv, j);
|
||||
}
|
||||
return (i);
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Convert the various cipher NIDs and dummies to a proper OID NID */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -61,6 +61,8 @@ static const EVP_PBE_CTL builtin_pbe[] = {
|
||||
NID_des_cbc, NID_sha1, PKCS5_PBE_keyivgen},
|
||||
|
||||
{EVP_PBE_TYPE_PRF, NID_hmacWithSHA1, -1, NID_sha1, 0},
|
||||
{EVP_PBE_TYPE_PRF, NID_hmac_md5, -1, NID_md5, 0},
|
||||
{EVP_PBE_TYPE_PRF, NID_hmac_sha1, -1, NID_sha1, 0},
|
||||
{EVP_PBE_TYPE_PRF, NID_hmacWithMD5, -1, NID_md5, 0},
|
||||
{EVP_PBE_TYPE_PRF, NID_hmacWithSHA224, -1, NID_sha224, 0},
|
||||
{EVP_PBE_TYPE_PRF, NID_hmacWithSHA256, -1, NID_sha256, 0},
|
||||
|
||||
@@ -80,7 +80,6 @@ PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
|
||||
EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
|
||||
goto error;
|
||||
}
|
||||
RAND_add(p8->pkey->data, p8->pkey->length, 0.0);
|
||||
return p8;
|
||||
error:
|
||||
PKCS8_PRIV_KEY_INFO_free(p8);
|
||||
|
||||
+1
-1
@@ -50,6 +50,6 @@ static const EVP_MD md4_md = {
|
||||
|
||||
const EVP_MD *EVP_md4(void)
|
||||
{
|
||||
return (&md4_md);
|
||||
return &md4_md;
|
||||
}
|
||||
#endif
|
||||
+1
-1
@@ -50,6 +50,6 @@ static const EVP_MD md5_md = {
|
||||
|
||||
const EVP_MD *EVP_md5(void)
|
||||
{
|
||||
return (&md5_md);
|
||||
return &md5_md;
|
||||
}
|
||||
#endif
|
||||
+1
-1
@@ -50,6 +50,6 @@ static const EVP_MD mdc2_md = {
|
||||
|
||||
const EVP_MD *EVP_mdc2(void)
|
||||
{
|
||||
return (&mdc2_md);
|
||||
return &mdc2_md;
|
||||
}
|
||||
#endif
|
||||
+1
-1
@@ -45,5 +45,5 @@ static const EVP_MD null_md = {
|
||||
|
||||
const EVP_MD *EVP_md_null(void)
|
||||
{
|
||||
return (&null_md);
|
||||
return &null_md;
|
||||
}
|
||||
@@ -50,6 +50,6 @@ static const EVP_MD ripemd160_md = {
|
||||
|
||||
const EVP_MD *EVP_ripemd160(void)
|
||||
{
|
||||
return (&ripemd160_md);
|
||||
return &ripemd160_md;
|
||||
}
|
||||
#endif
|
||||
+54
-5
@@ -15,6 +15,7 @@
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include "internal/evp_int.h"
|
||||
#include "internal/sha.h"
|
||||
|
||||
static int init(EVP_MD_CTX *ctx)
|
||||
{
|
||||
@@ -107,7 +108,7 @@ static const EVP_MD sha1_md = {
|
||||
|
||||
const EVP_MD *EVP_sha1(void)
|
||||
{
|
||||
return (&sha1_md);
|
||||
return &sha1_md;
|
||||
}
|
||||
|
||||
static int init224(EVP_MD_CTX *ctx)
|
||||
@@ -156,7 +157,7 @@ static const EVP_MD sha224_md = {
|
||||
|
||||
const EVP_MD *EVP_sha224(void)
|
||||
{
|
||||
return (&sha224_md);
|
||||
return &sha224_md;
|
||||
}
|
||||
|
||||
static const EVP_MD sha256_md = {
|
||||
@@ -175,7 +176,17 @@ static const EVP_MD sha256_md = {
|
||||
|
||||
const EVP_MD *EVP_sha256(void)
|
||||
{
|
||||
return (&sha256_md);
|
||||
return &sha256_md;
|
||||
}
|
||||
|
||||
static int init512_224(EVP_MD_CTX *ctx)
|
||||
{
|
||||
return sha512_224_init(EVP_MD_CTX_md_data(ctx));
|
||||
}
|
||||
|
||||
static int init512_256(EVP_MD_CTX *ctx)
|
||||
{
|
||||
return sha512_256_init(EVP_MD_CTX_md_data(ctx));
|
||||
}
|
||||
|
||||
static int init384(EVP_MD_CTX *ctx)
|
||||
@@ -209,6 +220,44 @@ static int final512(EVP_MD_CTX *ctx, unsigned char *md)
|
||||
return SHA512_Final(md, EVP_MD_CTX_md_data(ctx));
|
||||
}
|
||||
|
||||
static const EVP_MD sha512_224_md = {
|
||||
NID_sha512_224,
|
||||
NID_sha512_224WithRSAEncryption,
|
||||
SHA224_DIGEST_LENGTH,
|
||||
EVP_MD_FLAG_DIGALGID_ABSENT,
|
||||
init512_224,
|
||||
update512,
|
||||
final512,
|
||||
NULL,
|
||||
NULL,
|
||||
SHA512_CBLOCK,
|
||||
sizeof(EVP_MD *) + sizeof(SHA512_CTX),
|
||||
};
|
||||
|
||||
const EVP_MD *EVP_sha512_224(void)
|
||||
{
|
||||
return &sha512_224_md;
|
||||
}
|
||||
|
||||
static const EVP_MD sha512_256_md = {
|
||||
NID_sha512_256,
|
||||
NID_sha512_256WithRSAEncryption,
|
||||
SHA256_DIGEST_LENGTH,
|
||||
EVP_MD_FLAG_DIGALGID_ABSENT,
|
||||
init512_256,
|
||||
update512,
|
||||
final512,
|
||||
NULL,
|
||||
NULL,
|
||||
SHA512_CBLOCK,
|
||||
sizeof(EVP_MD *) + sizeof(SHA512_CTX),
|
||||
};
|
||||
|
||||
const EVP_MD *EVP_sha512_256(void)
|
||||
{
|
||||
return &sha512_256_md;
|
||||
}
|
||||
|
||||
static const EVP_MD sha384_md = {
|
||||
NID_sha384,
|
||||
NID_sha384WithRSAEncryption,
|
||||
@@ -225,7 +274,7 @@ static const EVP_MD sha384_md = {
|
||||
|
||||
const EVP_MD *EVP_sha384(void)
|
||||
{
|
||||
return (&sha384_md);
|
||||
return &sha384_md;
|
||||
}
|
||||
|
||||
static const EVP_MD sha512_md = {
|
||||
@@ -244,5 +293,5 @@ static const EVP_MD sha512_md = {
|
||||
|
||||
const EVP_MD *EVP_sha512(void)
|
||||
{
|
||||
return (&sha512_md);
|
||||
return &sha512_md;
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
* in the file LICENSE in the source distribution or at
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/objects.h>
|
||||
#include "internal/evp_int.h"
|
||||
#include "evp_locl.h"
|
||||
|
||||
size_t SHA3_absorb(uint64_t A[5][5], const unsigned char *inp, size_t len,
|
||||
size_t r);
|
||||
void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r);
|
||||
|
||||
#define KECCAK1600_WIDTH 1600
|
||||
|
||||
typedef struct {
|
||||
uint64_t A[5][5];
|
||||
size_t block_size; /* cached ctx->digest->block_size */
|
||||
size_t md_size; /* output length, variable in XOF */
|
||||
size_t num; /* used bytes in below buffer */
|
||||
unsigned char buf[KECCAK1600_WIDTH / 8 - 32];
|
||||
unsigned char pad;
|
||||
} KECCAK1600_CTX;
|
||||
|
||||
static int init(EVP_MD_CTX *evp_ctx, unsigned char pad)
|
||||
{
|
||||
KECCAK1600_CTX *ctx = evp_ctx->md_data;
|
||||
size_t bsz = evp_ctx->digest->block_size;
|
||||
|
||||
if (bsz <= sizeof(ctx->buf)) {
|
||||
memset(ctx->A, 0, sizeof(ctx->A));
|
||||
|
||||
ctx->num = 0;
|
||||
ctx->block_size = bsz;
|
||||
ctx->md_size = evp_ctx->digest->md_size;
|
||||
ctx->pad = pad;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sha3_init(EVP_MD_CTX *evp_ctx)
|
||||
{
|
||||
return init(evp_ctx, '\x06');
|
||||
}
|
||||
|
||||
static int shake_init(EVP_MD_CTX *evp_ctx)
|
||||
{
|
||||
return init(evp_ctx, '\x1f');
|
||||
}
|
||||
|
||||
static int sha3_update(EVP_MD_CTX *evp_ctx, const void *_inp, size_t len)
|
||||
{
|
||||
KECCAK1600_CTX *ctx = evp_ctx->md_data;
|
||||
const unsigned char *inp = _inp;
|
||||
size_t bsz = ctx->block_size;
|
||||
size_t num, rem;
|
||||
|
||||
if ((num = ctx->num) != 0) { /* process intermediate buffer? */
|
||||
rem = bsz - num;
|
||||
|
||||
if (len < rem) {
|
||||
memcpy(ctx->buf + num, inp, len);
|
||||
ctx->num += len;
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
* We have enough data to fill or overflow the intermediate
|
||||
* buffer. So we append |rem| bytes and process the block,
|
||||
* leaving the rest for later processing...
|
||||
*/
|
||||
memcpy(ctx->buf + num, inp, rem);
|
||||
inp += rem, len -= rem;
|
||||
(void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
|
||||
ctx->num = 0;
|
||||
/* ctx->buf is processed, ctx->num is guaranteed to be zero */
|
||||
}
|
||||
|
||||
if (len >= bsz)
|
||||
rem = SHA3_absorb(ctx->A, inp, len, bsz);
|
||||
else
|
||||
rem = len;
|
||||
|
||||
if (rem) {
|
||||
memcpy(ctx->buf, inp + len - rem, rem);
|
||||
ctx->num = rem;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int sha3_final(EVP_MD_CTX *evp_ctx, unsigned char *md)
|
||||
{
|
||||
KECCAK1600_CTX *ctx = evp_ctx->md_data;
|
||||
size_t bsz = ctx->block_size;
|
||||
size_t num = ctx->num;
|
||||
|
||||
/*
|
||||
* Pad the data with 10*1. Note that |num| can be |bsz - 1|
|
||||
* in which case both byte operations below are performed on
|
||||
* same byte...
|
||||
*/
|
||||
memset(ctx->buf + num, 0, bsz - num);
|
||||
ctx->buf[num] = ctx->pad;
|
||||
ctx->buf[bsz - 1] |= 0x80;
|
||||
|
||||
(void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz);
|
||||
|
||||
SHA3_squeeze(ctx->A, md, ctx->md_size, bsz);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int shake_ctrl(EVP_MD_CTX *evp_ctx, int cmd, int p1, void *p2)
|
||||
{
|
||||
KECCAK1600_CTX *ctx = evp_ctx->md_data;
|
||||
|
||||
switch (cmd) {
|
||||
case EVP_MD_CTRL_XOF_LEN:
|
||||
ctx->md_size = p1;
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#define EVP_MD_SHA3(bitlen) \
|
||||
const EVP_MD *EVP_sha3_##bitlen(void) \
|
||||
{ \
|
||||
static const EVP_MD sha3_##bitlen##_md = { \
|
||||
NID_sha3_##bitlen, \
|
||||
NID_RSA_SHA3_##bitlen, \
|
||||
bitlen / 8, \
|
||||
EVP_MD_FLAG_DIGALGID_ABSENT, \
|
||||
sha3_init, \
|
||||
sha3_update, \
|
||||
sha3_final, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
(KECCAK1600_WIDTH - bitlen * 2) / 8, \
|
||||
sizeof(KECCAK1600_CTX), \
|
||||
}; \
|
||||
return &sha3_##bitlen##_md; \
|
||||
}
|
||||
|
||||
EVP_MD_SHA3(224)
|
||||
EVP_MD_SHA3(256)
|
||||
EVP_MD_SHA3(384)
|
||||
EVP_MD_SHA3(512)
|
||||
|
||||
#define EVP_MD_SHAKE(bitlen) \
|
||||
const EVP_MD *EVP_shake##bitlen(void) \
|
||||
{ \
|
||||
static const EVP_MD shake##bitlen##_md = { \
|
||||
NID_shake##bitlen, \
|
||||
0, \
|
||||
bitlen / 8, \
|
||||
EVP_MD_FLAG_XOF, \
|
||||
shake_init, \
|
||||
sha3_update, \
|
||||
sha3_final, \
|
||||
NULL, \
|
||||
NULL, \
|
||||
(KECCAK1600_WIDTH - bitlen * 2) / 8, \
|
||||
sizeof(KECCAK1600_CTX), \
|
||||
shake_ctrl \
|
||||
}; \
|
||||
return &shake##bitlen##_md; \
|
||||
}
|
||||
|
||||
EVP_MD_SHAKE(128)
|
||||
EVP_MD_SHAKE(256)
|
||||
+36
-2
@@ -15,6 +15,12 @@
|
||||
#include "internal/evp_int.h"
|
||||
#include "evp_locl.h"
|
||||
|
||||
static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
|
||||
{
|
||||
EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey,
|
||||
int ver)
|
||||
@@ -43,15 +49,23 @@ static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
|
||||
if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
|
||||
return 0;
|
||||
ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
|
||||
} else if (EVP_PKEY_verify_init(ctx->pctx) <= 0)
|
||||
} else if (ctx->pctx->pmeth->digestverify != 0) {
|
||||
ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
|
||||
ctx->update = update;
|
||||
} else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (ctx->pctx->pmeth->signctx_init) {
|
||||
if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
|
||||
return 0;
|
||||
ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
|
||||
} else if (EVP_PKEY_sign_init(ctx->pctx) <= 0)
|
||||
} else if (ctx->pctx->pmeth->digestsign != 0) {
|
||||
ctx->pctx->operation = EVP_PKEY_OP_SIGN;
|
||||
ctx->update = update;
|
||||
} else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
|
||||
return 0;
|
||||
@@ -139,6 +153,16 @@ int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
|
||||
return 1;
|
||||
}
|
||||
|
||||
int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
|
||||
const unsigned char *tbs, size_t tbslen)
|
||||
{
|
||||
if (ctx->pctx->pmeth->digestsign != NULL)
|
||||
return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
|
||||
if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
|
||||
return 0;
|
||||
return EVP_DigestSignFinal(ctx, sigret, siglen);
|
||||
}
|
||||
|
||||
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
|
||||
size_t siglen)
|
||||
{
|
||||
@@ -175,3 +199,13 @@ int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
|
||||
return r;
|
||||
return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
|
||||
}
|
||||
|
||||
int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
|
||||
size_t siglen, const unsigned char *tbs, size_t tbslen)
|
||||
{
|
||||
if (ctx->pctx->pmeth->digestverify != NULL)
|
||||
return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
|
||||
if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
|
||||
return -1;
|
||||
return EVP_DigestVerifyFinal(ctx, sigret, siglen);
|
||||
}
|
||||
+1
-1
@@ -49,6 +49,6 @@ static const EVP_MD whirlpool_md = {
|
||||
|
||||
const EVP_MD *EVP_whirlpool(void)
|
||||
{
|
||||
return (&whirlpool_md);
|
||||
return &whirlpool_md;
|
||||
}
|
||||
#endif
|
||||
+11
-9
@@ -10,7 +10,7 @@
|
||||
#include <stdio.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include <openssl/evp.h>
|
||||
#include <internal/objects.h>
|
||||
#include "internal/objects.h"
|
||||
#include <openssl/x509.h>
|
||||
#include "internal/evp_int.h"
|
||||
|
||||
@@ -24,10 +24,10 @@ int EVP_add_cipher(const EVP_CIPHER *c)
|
||||
r = OBJ_NAME_add(OBJ_nid2sn(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
|
||||
(const char *)c);
|
||||
if (r == 0)
|
||||
return (0);
|
||||
return 0;
|
||||
r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
|
||||
(const char *)c);
|
||||
return (r);
|
||||
return r;
|
||||
}
|
||||
|
||||
int EVP_add_digest(const EVP_MD *md)
|
||||
@@ -38,21 +38,21 @@ int EVP_add_digest(const EVP_MD *md)
|
||||
name = OBJ_nid2sn(md->type);
|
||||
r = OBJ_NAME_add(name, OBJ_NAME_TYPE_MD_METH, (const char *)md);
|
||||
if (r == 0)
|
||||
return (0);
|
||||
return 0;
|
||||
r = OBJ_NAME_add(OBJ_nid2ln(md->type), OBJ_NAME_TYPE_MD_METH,
|
||||
(const char *)md);
|
||||
if (r == 0)
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
if (md->pkey_type && md->type != md->pkey_type) {
|
||||
r = OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),
|
||||
OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
|
||||
if (r == 0)
|
||||
return (0);
|
||||
return 0;
|
||||
r = OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),
|
||||
OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
|
||||
}
|
||||
return (r);
|
||||
return r;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
|
||||
@@ -63,7 +63,7 @@ const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
|
||||
return NULL;
|
||||
|
||||
cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
|
||||
return (cp);
|
||||
return cp;
|
||||
}
|
||||
|
||||
const EVP_MD *EVP_get_digestbyname(const char *name)
|
||||
@@ -74,7 +74,7 @@ const EVP_MD *EVP_get_digestbyname(const char *name)
|
||||
return NULL;
|
||||
|
||||
cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
|
||||
return (cp);
|
||||
return cp;
|
||||
}
|
||||
|
||||
void evp_cleanup_int(void)
|
||||
@@ -90,6 +90,8 @@ void evp_cleanup_int(void)
|
||||
|
||||
EVP_PBE_cleanup();
|
||||
OBJ_sigid_free();
|
||||
|
||||
evp_app_cleanup_int();
|
||||
}
|
||||
|
||||
struct doall_cipher {
|
||||
|
||||
+1
-16
@@ -25,8 +25,7 @@ static void h__dump(const unsigned char *p, int len);
|
||||
/*
|
||||
* This is an implementation of PKCS#5 v2.0 password based encryption key
|
||||
* derivation function PBKDF2. SHA1 version verified against test vectors
|
||||
* posted by Peter Gutmann <pgut001@cs.auckland.ac.nz> to the PKCS-TNG
|
||||
* <pkcs-tng@rsa.com> mailing list.
|
||||
* posted by Peter Gutmann to the PKCS-TNG mailing list.
|
||||
*/
|
||||
|
||||
int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
|
||||
@@ -88,7 +87,6 @@ int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
|
||||
HMAC_CTX_free(hctx_tpl);
|
||||
return 0;
|
||||
}
|
||||
HMAC_CTX_reset(hctx);
|
||||
memcpy(p, digtmp, cplen);
|
||||
for (j = 1; j < iter; j++) {
|
||||
if (!HMAC_CTX_copy(hctx, hctx_tpl)) {
|
||||
@@ -102,7 +100,6 @@ int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
|
||||
HMAC_CTX_free(hctx_tpl);
|
||||
return 0;
|
||||
}
|
||||
HMAC_CTX_reset(hctx);
|
||||
for (k = 0; k < cplen; k++)
|
||||
p[k] ^= digtmp[k];
|
||||
}
|
||||
@@ -132,18 +129,6 @@ int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
|
||||
keylen, out);
|
||||
}
|
||||
|
||||
# ifdef DO_TEST
|
||||
main()
|
||||
{
|
||||
unsigned char out[4];
|
||||
unsigned char salt[] = { 0x12, 0x34, 0x56, 0x78 };
|
||||
PKCS5_PBKDF2_HMAC_SHA1("password", -1, salt, 4, 5, 4, out);
|
||||
fprintf(stderr, "Out %02X %02X %02X %02X\n",
|
||||
out[0], out[1], out[2], out[3]);
|
||||
}
|
||||
|
||||
# endif
|
||||
|
||||
/*
|
||||
* Now the key derivation function itself. This is a bit evil because it has
|
||||
* to check the ASN1 parameters are valid: and there are quite a few of
|
||||
|
||||
+1
-1
@@ -32,5 +32,5 @@ int EVP_PKEY_decrypt_old(unsigned char *key, const unsigned char *ek, int ekl,
|
||||
RSA_PKCS1_PADDING);
|
||||
err:
|
||||
#endif
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
+1
-1
@@ -31,5 +31,5 @@ int EVP_PKEY_encrypt_old(unsigned char *ek, const unsigned char *key,
|
||||
RSA_PKCS1_PADDING);
|
||||
err:
|
||||
#endif
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
+36
-6
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include "internal/cryptlib.h"
|
||||
#include "internal/refcount.h"
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/objects.h>
|
||||
@@ -55,7 +56,7 @@ int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
|
||||
|
||||
if (mode >= 0)
|
||||
pkey->save_parameters = mode;
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_EC
|
||||
@@ -64,10 +65,10 @@ int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
|
||||
|
||||
if (mode >= 0)
|
||||
pkey->save_parameters = mode;
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
|
||||
@@ -160,7 +161,7 @@ int EVP_PKEY_up_ref(EVP_PKEY *pkey)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (CRYPTO_atomic_add(&pkey->references, 1, &i, pkey->lock) <= 0)
|
||||
if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
|
||||
return 0;
|
||||
|
||||
REF_PRINT_COUNT("EVP_PKEY", pkey);
|
||||
@@ -269,6 +270,35 @@ const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
|
||||
return os->data;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_POLY1305
|
||||
const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
|
||||
{
|
||||
ASN1_OCTET_STRING *os = NULL;
|
||||
if (pkey->type != EVP_PKEY_POLY1305) {
|
||||
EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
|
||||
return NULL;
|
||||
}
|
||||
os = EVP_PKEY_get0(pkey);
|
||||
*len = os->length;
|
||||
return os->data;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_SIPHASH
|
||||
const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
|
||||
{
|
||||
ASN1_OCTET_STRING *os = NULL;
|
||||
|
||||
if (pkey->type != EVP_PKEY_SIPHASH) {
|
||||
EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
|
||||
return NULL;
|
||||
}
|
||||
os = EVP_PKEY_get0(pkey);
|
||||
*len = os->length;
|
||||
return os->data;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef OPENSSL_NO_RSA
|
||||
int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
|
||||
{
|
||||
@@ -412,7 +442,7 @@ void EVP_PKEY_free(EVP_PKEY *x)
|
||||
if (x == NULL)
|
||||
return;
|
||||
|
||||
CRYPTO_atomic_add(&x->references, -1, &i, x->lock);
|
||||
CRYPTO_DOWN_REF(&x->references, &i, x->lock);
|
||||
REF_PRINT_COUNT("EVP_PKEY", x);
|
||||
if (i > 0)
|
||||
return;
|
||||
|
||||
+2
-2
@@ -58,7 +58,7 @@ int EVP_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
|
||||
ret = 1;
|
||||
err:
|
||||
OPENSSL_clear_free(key, size);
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
||||
@@ -68,6 +68,6 @@ int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
||||
i = EVP_DecryptFinal_ex(ctx, out, outl);
|
||||
if (i)
|
||||
i = EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);
|
||||
return (i);
|
||||
return i;
|
||||
}
|
||||
#endif
|
||||
+2
-14
@@ -43,23 +43,11 @@ int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
|
||||
EVP_PKEY_encrypt_old(ek[i], key, EVP_CIPHER_CTX_key_length(ctx),
|
||||
pubk[i]);
|
||||
if (ekl[i] <= 0)
|
||||
return (-1);
|
||||
return -1;
|
||||
}
|
||||
return (npubk);
|
||||
return npubk;
|
||||
}
|
||||
|
||||
/*- MACRO
|
||||
void EVP_SealUpdate(ctx,out,outl,in,inl)
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
unsigned char *out;
|
||||
int *outl;
|
||||
unsigned char *in;
|
||||
int inl;
|
||||
{
|
||||
EVP_EncryptUpdate(ctx,out,outl,in,inl);
|
||||
}
|
||||
*/
|
||||
|
||||
int EVP_SealFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
#include <internal/numbers.h>
|
||||
#include "internal/numbers.h"
|
||||
|
||||
#ifndef OPENSSL_NO_SCRYPT
|
||||
|
||||
@@ -164,7 +164,6 @@ int EVP_PBE_scrypt(const char *pass, size_t passlen,
|
||||
unsigned char *B;
|
||||
uint32_t *X, *V, *T;
|
||||
uint64_t i, Blen, Vlen;
|
||||
size_t allocsize;
|
||||
|
||||
/* Sanity check parameters */
|
||||
/* initial check, r,p must be non zero, N >= 2 and a power of 2 */
|
||||
@@ -192,10 +191,17 @@ int EVP_PBE_scrypt(const char *pass, size_t passlen,
|
||||
* p * r < SCRYPT_PR_MAX
|
||||
*/
|
||||
Blen = p * 128 * r;
|
||||
/*
|
||||
* Yet we pass it as integer to PKCS5_PBKDF2_HMAC... [This would
|
||||
* have to be revised when/if PKCS5_PBKDF2_HMAC accepts size_t.]
|
||||
*/
|
||||
if (Blen > INT_MAX) {
|
||||
EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check 32 * r * (N + 2) * sizeof(uint32_t) fits in
|
||||
* uint64_t and also size_t (their sizes are unrelated).
|
||||
* Check 32 * r * (N + 2) * sizeof(uint32_t) fits in uint64_t
|
||||
* This is combined size V, X and T (section 4)
|
||||
*/
|
||||
i = UINT64_MAX / (32 * sizeof(uint32_t));
|
||||
@@ -206,16 +212,15 @@ int EVP_PBE_scrypt(const char *pass, size_t passlen,
|
||||
/* check total allocated size fits in uint64_t */
|
||||
if (Blen > UINT64_MAX - Vlen)
|
||||
return 0;
|
||||
/* check total allocated size fits in size_t */
|
||||
if (Blen > SIZE_MAX - Vlen)
|
||||
return 0;
|
||||
|
||||
allocsize = (size_t)(Blen + Vlen);
|
||||
|
||||
if (maxmem == 0)
|
||||
maxmem = SCRYPT_MAX_MEM;
|
||||
|
||||
if (allocsize > maxmem) {
|
||||
/* Check that the maximum memory doesn't exceed a size_t limits */
|
||||
if (maxmem > SIZE_MAX)
|
||||
maxmem = SIZE_MAX;
|
||||
|
||||
if (Blen + Vlen > maxmem) {
|
||||
EVPerr(EVP_F_EVP_PBE_SCRYPT, EVP_R_MEMORY_LIMIT_EXCEEDED);
|
||||
return 0;
|
||||
}
|
||||
@@ -224,25 +229,25 @@ int EVP_PBE_scrypt(const char *pass, size_t passlen,
|
||||
if (key == NULL)
|
||||
return 1;
|
||||
|
||||
B = OPENSSL_malloc(allocsize);
|
||||
B = OPENSSL_malloc((size_t)(Blen + Vlen));
|
||||
if (B == NULL)
|
||||
return 0;
|
||||
X = (uint32_t *)(B + Blen);
|
||||
T = X + 32 * r;
|
||||
V = T + 32 * r;
|
||||
if (PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, 1, EVP_sha256(),
|
||||
Blen, B) == 0)
|
||||
(int)Blen, B) == 0)
|
||||
goto err;
|
||||
|
||||
for (i = 0; i < p; i++)
|
||||
scryptROMix(B + 128 * r * i, r, N, X, T, V);
|
||||
|
||||
if (PKCS5_PBKDF2_HMAC(pass, passlen, B, Blen, 1, EVP_sha256(),
|
||||
if (PKCS5_PBKDF2_HMAC(pass, passlen, B, (int)Blen, 1, EVP_sha256(),
|
||||
keylen, key) == 0)
|
||||
goto err;
|
||||
rv = 1;
|
||||
err:
|
||||
OPENSSL_clear_free(B, allocsize);
|
||||
OPENSSL_clear_free(B, (size_t)(Blen + Vlen));
|
||||
return rv;
|
||||
}
|
||||
#endif
|
||||
@@ -255,7 +255,7 @@ int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer)
|
||||
}
|
||||
|
||||
/*
|
||||
* ran@cryptocom.ru: For clarity. The error is if parameters in peer are
|
||||
* For clarity. The error is if parameters in peer are
|
||||
* present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
|
||||
* 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
|
||||
* (different key types) is impossible here because it is checked earlier.
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/evp.h>
|
||||
#include "internal/bn_int.h"
|
||||
#include "internal/asn1_int.h"
|
||||
#include "internal/evp_int.h"
|
||||
|
||||
int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
|
||||
@@ -167,3 +168,72 @@ EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
|
||||
EVP_PKEY_CTX_free(mac_ctx);
|
||||
return mac_key;
|
||||
}
|
||||
|
||||
int EVP_PKEY_check(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
EVP_PKEY *pkey = ctx->pkey;
|
||||
|
||||
if (pkey == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* call customized check function first */
|
||||
if (ctx->pmeth->check != NULL)
|
||||
return ctx->pmeth->check(pkey);
|
||||
|
||||
/* use default check function in ameth */
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_CHECK,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
return pkey->ameth->pkey_check(pkey);
|
||||
}
|
||||
|
||||
int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
EVP_PKEY *pkey = ctx->pkey;
|
||||
|
||||
if (pkey == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* call customized public key check function first */
|
||||
if (ctx->pmeth->public_check != NULL)
|
||||
return ctx->pmeth->public_check(pkey);
|
||||
|
||||
/* use default public key check function in ameth */
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
return pkey->ameth->pkey_public_check(pkey);
|
||||
}
|
||||
|
||||
int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
EVP_PKEY *pkey = ctx->pkey;
|
||||
|
||||
if (pkey == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* call customized param check function first */
|
||||
if (ctx->pmeth->param_check != NULL)
|
||||
return ctx->pmeth->param_check(pkey);
|
||||
|
||||
/* use default param check function in ameth */
|
||||
if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK,
|
||||
EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
||||
return -2;
|
||||
}
|
||||
|
||||
return pkey->ameth->pkey_param_check(pkey);
|
||||
}
|
||||
+116
-10
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the OpenSSL license (the "License"). You may not use
|
||||
* this file except in compliance with the License. You can obtain a copy
|
||||
@@ -21,6 +21,7 @@ typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
|
||||
|
||||
static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
|
||||
|
||||
/* This array needs to be in order of NIDs */
|
||||
static const EVP_PKEY_METHOD *standard_methods[] = {
|
||||
#ifndef OPENSSL_NO_RSA
|
||||
&rsa_pkey_meth,
|
||||
@@ -38,14 +39,29 @@ static const EVP_PKEY_METHOD *standard_methods[] = {
|
||||
#ifndef OPENSSL_NO_CMAC
|
||||
&cmac_pkey_meth,
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_RSA
|
||||
&rsa_pss_pkey_meth,
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_DH
|
||||
&dhx_pkey_meth,
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_SCRYPT
|
||||
&scrypt_pkey_meth,
|
||||
#endif
|
||||
&tls1_prf_pkey_meth,
|
||||
#ifndef OPENSSL_NO_EC
|
||||
&ecx25519_pkey_meth,
|
||||
#endif
|
||||
&hkdf_pkey_meth
|
||||
&hkdf_pkey_meth,
|
||||
#ifndef OPENSSL_NO_POLY1305
|
||||
&poly1305_pkey_meth,
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_SIPHASH
|
||||
&siphash_pkey_meth,
|
||||
#endif
|
||||
#ifndef OPENSSL_NO_EC
|
||||
&ed25519_pkey_meth,
|
||||
#endif
|
||||
};
|
||||
|
||||
DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
|
||||
@@ -207,6 +223,8 @@ void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
|
||||
|
||||
dst->ctrl = src->ctrl;
|
||||
dst->ctrl_str = src->ctrl_str;
|
||||
|
||||
dst->check = src->check;
|
||||
}
|
||||
|
||||
void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
|
||||
@@ -282,6 +300,42 @@ int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
|
||||
return 1;
|
||||
}
|
||||
|
||||
void evp_app_cleanup_int(void)
|
||||
{
|
||||
if (app_pkey_methods != NULL)
|
||||
sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
|
||||
}
|
||||
|
||||
int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
|
||||
{
|
||||
const EVP_PKEY_METHOD *ret;
|
||||
|
||||
ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
|
||||
|
||||
return ret == NULL ? 0 : 1;
|
||||
}
|
||||
|
||||
size_t EVP_PKEY_meth_get_count(void)
|
||||
{
|
||||
size_t rv = OSSL_NELEM(standard_methods);
|
||||
|
||||
if (app_pkey_methods)
|
||||
rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
|
||||
return rv;
|
||||
}
|
||||
|
||||
const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
|
||||
{
|
||||
if (idx < OSSL_NELEM(standard_methods))
|
||||
return standard_methods[idx];
|
||||
if (app_pkey_methods == NULL)
|
||||
return NULL;
|
||||
idx -= OSSL_NELEM(standard_methods);
|
||||
if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
|
||||
return NULL;
|
||||
return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
|
||||
}
|
||||
|
||||
void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
if (ctx == NULL)
|
||||
@@ -326,6 +380,12 @@ int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
|
||||
|
||||
}
|
||||
|
||||
int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
|
||||
int cmd, uint64_t value)
|
||||
{
|
||||
return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
|
||||
}
|
||||
|
||||
int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
|
||||
const char *name, const char *value)
|
||||
{
|
||||
@@ -333,14 +393,9 @@ int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
|
||||
EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
|
||||
return -2;
|
||||
}
|
||||
if (strcmp(name, "digest") == 0) {
|
||||
const EVP_MD *md;
|
||||
if (value == NULL || (md = EVP_get_digestbyname(value)) == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);
|
||||
return 0;
|
||||
}
|
||||
return EVP_PKEY_CTX_set_signature_md(ctx, md);
|
||||
}
|
||||
if (strcmp(name, "digest") == 0)
|
||||
return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
|
||||
value);
|
||||
return ctx->pmeth->ctrl_str(ctx, name, value);
|
||||
}
|
||||
|
||||
@@ -371,6 +426,18 @@ int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* Pass a message digest to a ctrl */
|
||||
int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
|
||||
{
|
||||
const EVP_MD *m;
|
||||
|
||||
if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
|
||||
EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
|
||||
return 0;
|
||||
}
|
||||
return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
|
||||
}
|
||||
|
||||
int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
return ctx->operation;
|
||||
@@ -557,6 +624,24 @@ void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
|
||||
pmeth->ctrl_str = ctrl_str;
|
||||
}
|
||||
|
||||
void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
|
||||
int (*check) (EVP_PKEY *pkey))
|
||||
{
|
||||
pmeth->check = check;
|
||||
}
|
||||
|
||||
void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
|
||||
int (*check) (EVP_PKEY *pkey))
|
||||
{
|
||||
pmeth->public_check = check;
|
||||
}
|
||||
|
||||
void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
|
||||
int (*check) (EVP_PKEY *pkey))
|
||||
{
|
||||
pmeth->param_check = check;
|
||||
}
|
||||
|
||||
void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
|
||||
int (**pinit) (EVP_PKEY_CTX *ctx))
|
||||
{
|
||||
@@ -723,3 +808,24 @@ void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
|
||||
if (pctrl_str)
|
||||
*pctrl_str = pmeth->ctrl_str;
|
||||
}
|
||||
|
||||
void EVP_PKEY_meth_get_check(EVP_PKEY_METHOD *pmeth,
|
||||
int (**pcheck) (EVP_PKEY *pkey))
|
||||
{
|
||||
if (*pcheck)
|
||||
*pcheck = pmeth->check;
|
||||
}
|
||||
|
||||
void EVP_PKEY_meth_get_public_check(EVP_PKEY_METHOD *pmeth,
|
||||
int (**pcheck) (EVP_PKEY *pkey))
|
||||
{
|
||||
if (*pcheck)
|
||||
*pcheck = pmeth->public_check;
|
||||
}
|
||||
|
||||
void EVP_PKEY_meth_get_param_check(EVP_PKEY_METHOD *pmeth,
|
||||
int (**pcheck) (EVP_PKEY *pkey))
|
||||
{
|
||||
if (*pcheck)
|
||||
*pcheck = pmeth->param_check;
|
||||
}
|
||||
Reference in New Issue
Block a user