Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:15

0001 /* ocb.h
0002 
0003    OCB AEAD mode, RFC 7253
0004 
0005    Copyright (C) 2021 Niels Möller
0006 
0007    This file is part of GNU Nettle.
0008 
0009    GNU Nettle is free software: you can redistribute it and/or
0010    modify it under the terms of either:
0011 
0012      * the GNU Lesser General Public License as published by the Free
0013        Software Foundation; either version 3 of the License, or (at your
0014        option) any later version.
0015 
0016    or
0017 
0018      * the GNU General Public License as published by the Free
0019        Software Foundation; either version 2 of the License, or (at your
0020        option) any later version.
0021 
0022    or both in parallel, as here.
0023 
0024    GNU Nettle is distributed in the hope that it will be useful,
0025    but WITHOUT ANY WARRANTY; without even the implied warranty of
0026    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0027    General Public License for more details.
0028 
0029    You should have received copies of the GNU General Public License and
0030    the GNU Lesser General Public License along with this program.  If
0031    not, see http://www.gnu.org/licenses/.
0032 */
0033 
0034 #ifndef NETTLE_OCB_H_INCLUDED
0035 #define NETTLE_OCB_H_INCLUDED
0036 
0037 #include "nettle-types.h"
0038 #include "aes.h"
0039 
0040 #ifdef __cplusplus
0041 extern "C" {
0042 #endif
0043 
0044 /* Name mangling */
0045 #define ocb_set_key nettle_ocb_set_key
0046 #define ocb_set_nonce nettle_ocb_set_nonce
0047 #define ocb_update nettle_ocb_update
0048 #define ocb_encrypt nettle_ocb_encrypt
0049 #define ocb_decrypt nettle_ocb_decrypt
0050 #define ocb_digest nettle_ocb_digest
0051 #define ocb_encrypt_message nettle_ocb_encrypt_message
0052 #define ocb_decrypt_message nettle_ocb_decrypt_message
0053 #define ocb_aes128_set_encrypt_key nettle_ocb_aes128_set_encrypt_key
0054 #define ocb_aes128_set_decrypt_key nettle_ocb_aes128_set_decrypt_key
0055 #define ocb_aes128_set_nonce nettle_ocb_aes128_set_nonce
0056 #define ocb_aes128_update nettle_ocb_aes128_update
0057 #define ocb_aes128_encrypt nettle_ocb_aes128_encrypt
0058 #define ocb_aes128_decrypt nettle_ocb_aes128_decrypt
0059 #define ocb_aes128_digest nettle_ocb_aes128_digest
0060 #define ocb_aes128_encrypt_message nettle_ocb_aes128_encrypt_message
0061 #define ocb_aes128_decrypt_message nettle_ocb_aes128_decrypt_message
0062 
0063 #define OCB_BLOCK_SIZE 16
0064 #define OCB_DIGEST_SIZE 16
0065 #define OCB_MAX_NONCE_SIZE 15
0066 
0067 struct ocb_key {
0068   /* L_*, L_$ and L_0, and one reserved entry */
0069   union nettle_block16 L[4];
0070 };
0071 
0072 struct ocb_ctx {
0073   /* Initial offset, Offset_0 in the spec. */
0074   union nettle_block16 initial;
0075   /* Offset, updated per block. */
0076   union nettle_block16 offset;
0077   /* Authentication for the associated data */
0078   union nettle_block16 sum;
0079   /* Authentication for the message */
0080   union nettle_block16 checksum;
0081   /* Count of processed blocks. */
0082   size_t data_count;
0083   size_t message_count;
0084 };
0085 
0086 void
0087 ocb_set_key (struct ocb_key *key, const void *cipher, nettle_cipher_func *f);
0088 
0089 void
0090 ocb_set_nonce (struct ocb_ctx *ctx,
0091            const void *cipher, nettle_cipher_func *f,
0092            size_t tag_length, size_t nonce_length, const uint8_t *nonce);
0093 
0094 void
0095 ocb_update (struct ocb_ctx *ctx, const struct ocb_key *key,
0096         const void *cipher, nettle_cipher_func *f,
0097         size_t length, const uint8_t *data);
0098 
0099 void
0100 ocb_encrypt (struct ocb_ctx *ctx, const struct ocb_key *key,
0101          const void *cipher, nettle_cipher_func *f,
0102          size_t length, uint8_t *dst, const uint8_t *src);
0103 
0104 void
0105 ocb_decrypt (struct ocb_ctx *ctx, const struct ocb_key *key,
0106          const void *encrypt_ctx, nettle_cipher_func *encrypt,
0107          const void *decrypt_ctx, nettle_cipher_func *decrypt,
0108          size_t length, uint8_t *dst, const uint8_t *src);
0109 
0110 void
0111 ocb_digest (const struct ocb_ctx *ctx, const struct ocb_key *key,
0112         const void *cipher, nettle_cipher_func *f,
0113         size_t length, uint8_t *digest);
0114 
0115 
0116 void
0117 ocb_encrypt_message (const struct ocb_key *ocb_key,
0118              const void *cipher, nettle_cipher_func *f,
0119              size_t nlength, const uint8_t *nonce,
0120              size_t alength, const uint8_t *adata,
0121              size_t tlength,
0122              size_t clength, uint8_t *dst, const uint8_t *src);
0123 
0124 int
0125 ocb_decrypt_message (const struct ocb_key *ocb_key,
0126              const void *encrypt_ctx, nettle_cipher_func *encrypt,
0127              const void *decrypt_ctx, nettle_cipher_func *decrypt,
0128              size_t nlength, const uint8_t *nonce,
0129              size_t alength, const uint8_t *adata,
0130              size_t tlength,
0131              size_t mlength, uint8_t *dst, const uint8_t *src);
0132 
0133 /* OCB-AES */
0134 /* This struct represents an expanded key for ocb-aes encryption. For
0135    decryption, a separate decryption context is needed as well. */
0136 struct ocb_aes128_encrypt_key
0137 {
0138   struct ocb_key ocb;
0139   struct aes128_ctx encrypt;
0140 };
0141 
0142 void
0143 ocb_aes128_set_encrypt_key (struct ocb_aes128_encrypt_key *ocb, const uint8_t *key);
0144 
0145 void
0146 ocb_aes128_set_decrypt_key (struct ocb_aes128_encrypt_key *ocb, struct aes128_ctx *decrypt,
0147                 const uint8_t *key);
0148 
0149 void
0150 ocb_aes128_set_nonce (struct ocb_ctx *ctx, const struct ocb_aes128_encrypt_key *key,
0151               size_t tag_length, size_t nonce_length, const uint8_t *nonce);
0152 
0153 void
0154 ocb_aes128_update (struct ocb_ctx *ctx, const struct ocb_aes128_encrypt_key *key,
0155            size_t length, const uint8_t *data);
0156 
0157 void
0158 ocb_aes128_encrypt(struct ocb_ctx *ctx, const struct ocb_aes128_encrypt_key *key,
0159            size_t length, uint8_t *dst, const uint8_t *src);
0160 
0161 void
0162 ocb_aes128_decrypt(struct ocb_ctx *ctx, const struct ocb_aes128_encrypt_key *key,
0163            const struct aes128_ctx *decrypt,
0164            size_t length, uint8_t *dst, const uint8_t *src);
0165 
0166 void
0167 ocb_aes128_digest(struct ocb_ctx *ctx, const struct ocb_aes128_encrypt_key *key,
0168           size_t length, uint8_t *digest);
0169 
0170 void
0171 ocb_aes128_encrypt_message (const struct ocb_aes128_encrypt_key *key,
0172                 size_t nlength, const uint8_t *nonce,
0173                 size_t alength, const uint8_t *adata,
0174                 size_t tlength,
0175                 size_t clength, uint8_t *dst, const uint8_t *src);
0176 
0177 int
0178 ocb_aes128_decrypt_message (const struct ocb_aes128_encrypt_key *key,
0179                 const struct aes128_ctx *decrypt,
0180                 size_t nlength, const uint8_t *nonce,
0181                 size_t alength, const uint8_t *adata,
0182                 size_t tlength,
0183                 size_t mlength, uint8_t *dst, const uint8_t *src);
0184 
0185 #ifdef __cplusplus
0186 }
0187 #endif
0188 
0189 #endif /* NETTLE_OCB_H_INCLUDED */