]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_gost2015.h
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / gost_gost2015.h
1 /*
2  * Copyright (c) 2020 Dmitry Belyavskiy <beldmit@gmail.com>
3  *
4  * Contents licensed under the terms of the OpenSSL license
5  * See https://www.openssl.org/source/license.html for details
6  */
7 #ifndef GOST_GOST2015_H
8 #define GOST_GOST2015_H
9
10 #include "gost_grasshopper_cipher.h"
11
12 #include <openssl/evp.h>
13 #include <openssl/x509.h>
14 #include <openssl/modes.h>
15
16 #define MAGMA_MAC_MAX_SIZE 8
17 #define KUZNYECHIK_MAC_MAX_SIZE 16
18 #define OID_GOST_CMS_MAC "1.2.643.7.1.0.6.1.1"
19
20 #define SN_magma_mgm            "magma-mgm"
21
22 #define BSWAP64(x) \
23     (((x & 0xFF00000000000000ULL) >> 56) | \
24      ((x & 0x00FF000000000000ULL) >> 40) | \
25      ((x & 0x0000FF0000000000ULL) >> 24) | \
26      ((x & 0x000000FF00000000ULL) >>  8) | \
27      ((x & 0x00000000FF000000ULL) <<  8) | \
28      ((x & 0x0000000000FF0000ULL) << 24) | \
29      ((x & 0x000000000000FF00ULL) << 40) | \
30      ((x & 0x00000000000000FFULL) << 56))
31
32 typedef void (*mul128_f) (uint64_t *result, uint64_t *arg1, uint64_t *arg2);
33
34 typedef struct {
35     union {
36         uint64_t u[2];
37         uint32_t d[4];
38         uint8_t c[16];
39     } nonce, Yi, Zi, EKi, Hi, len, ACi, mul, sum, tag;
40
41     unsigned int mres, ares;
42     block128_f block;
43     mul128_f mul_gf;
44     int blocklen;
45     void *key;
46 } mgm128_context;
47
48 typedef struct {
49     union {
50         struct ossl_gost_cipher_ctx g_ks;
51         gost_grasshopper_cipher_ctx gh_ks;
52     } ks;
53     int key_set;
54     int iv_set;
55     mgm128_context mgm;
56     unsigned char *iv;
57     int ivlen;
58     int taglen;
59     int tlstree_mode;
60 } gost_mgm_ctx;
61
62 int gost2015_final_call(EVP_CIPHER_CTX *ctx, EVP_MD_CTX *omac_ctx, size_t mac_size,
63                         unsigned char *encrypted_mac,
64                         int (*do_cipher) (EVP_CIPHER_CTX *ctx,
65                                 unsigned char *out,
66                                 const unsigned char *in,
67                                 size_t inl));
68
69 /* IV is expected to be 16 bytes*/
70 int gost2015_get_asn1_params(const ASN1_TYPE *params, size_t ukm_size,
71         unsigned char *iv, size_t ukm_offset, unsigned char *kdf_seed);
72
73 int gost2015_set_asn1_params(ASN1_TYPE *params,
74         const unsigned char *iv, size_t iv_size, const unsigned char *kdf_seed);
75
76 int gost2015_process_unprotected_attributes(STACK_OF(X509_ATTRIBUTE) *attrs,
77             int encryption, size_t mac_len, unsigned char *final_tag);
78
79 int gost2015_acpkm_omac_init(int nid, int enc, const unsigned char *inkey,
80                              EVP_MD_CTX *omac_ctx,
81                              unsigned char *outkey, unsigned char *kdf_seed);
82 int init_zero_kdf_seed(unsigned char *kdf_seed);
83
84
85 /* enc/dec mgm mode */
86
87 void gost_mgm128_init(mgm128_context *ctx, void *key, block128_f block,
88                                           mul128_f mul_gf, int blen);
89
90 int gost_mgm128_setiv(mgm128_context *ctx, const unsigned char *iv, size_t len);
91
92 int gost_mgm128_aad(mgm128_context *ctx, const unsigned char *aad, size_t len);
93
94 int gost_mgm128_encrypt(mgm128_context *ctx, const unsigned char *in,
95                           unsigned char *out, size_t len);
96
97 int gost_mgm128_decrypt(mgm128_context *ctx, const unsigned char *in,
98                           unsigned char *out, size_t len);
99
100 int gost_mgm128_finish(mgm128_context *ctx, const unsigned char *tag, size_t len);
101
102 void gost_mgm128_tag(mgm128_context *ctx, unsigned char *tag, size_t len);
103
104 #endif