ccm.h 9.4 KB
Newer Older
Owen Kirby's avatar
Owen Kirby committed
1 2
/* ccm.h

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
   Counter with CBC-MAC mode, specified by NIST,
   http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf

   Copyright (C) 2014 Exegin Technologies Limited
   Copyright (C) 2014 Owen Kirby

   Contributed to GNU Nettle by Owen Kirby

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/

/* NIST SP800-38C doesn't specify the particular formatting and
 * counter generation algorithm for CCM, but it does include an
 * example algorithm. This example has become the de-factor standard,
 * and has been adopted by both the IETF and IEEE across a wide
 * variety of protocols.
Owen Kirby's avatar
Owen Kirby committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
 */

#ifndef NETTLE_CCM_H_INCLUDED
#define NETTLE_CCM_H_INCLUDED

#include "aes.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Name mangling */
#define ccm_set_nonce nettle_ccm_set_nonce
#define ccm_update nettle_ccm_update
#define ccm_encrypt nettle_ccm_encrypt
#define ccm_decrypt nettle_ccm_decrypt
#define ccm_digest nettle_ccm_digest
#define ccm_encrypt_message nettle_ccm_encrypt_message
#define ccm_decrypt_message nettle_ccm_decrypt_message

#define ccm_aes128_set_key nettle_ccm_aes128_set_key
#define ccm_aes128_set_nonce nettle_ccm_aes128_set_nonce
#define ccm_aes128_update nettle_ccm_aes128_update
#define ccm_aes128_encrypt nettle_ccm_aes128_encrypt
#define ccm_aes128_decrypt nettle_ccm_aes128_decrypt
#define ccm_aes128_digest nettle_ccm_aes128_digest
#define ccm_aes128_encrypt_message nettle_ccm_aes128_encrypt_message
#define ccm_aes128_decrypt_message nettle_ccm_aes128_decrypt_message

#define ccm_aes192_set_key nettle_ccm_aes192_set_key
#define ccm_aes192_set_nonce nettle_ccm_aes192_set_nonce
#define ccm_aes192_update nettle_ccm_aes192_update
#define ccm_aes192_encrypt nettle_ccm_aes192_encrypt
#define ccm_aes192_decrypt nettle_ccm_aes192_decrypt
#define ccm_aes192_digest nettle_ccm_aes192_digest
#define ccm_aes192_encrypt_message nettle_ccm_aes192_encrypt_message
#define ccm_aes192_decrypt_message nettle_ccm_aes192_decrypt_message

#define ccm_aes256_set_key nettle_ccm_aes256_set_key
#define ccm_aes256_set_nonce nettle_ccm_aes256_set_nonce
#define ccm_aes256_update nettle_ccm_aes256_update
#define ccm_aes256_encrypt nettle_ccm_aes256_encrypt
#define ccm_aes256_decrypt nettle_ccm_aes256_decrypt
#define ccm_aes256_digest nettle_ccm_aes256_digest
#define ccm_aes256_encrypt_message nettle_ccm_aes256_encrypt_message
#define ccm_aes256_decrypt_message nettle_ccm_aes256_decrypt_message

/* For CCM, the block size of the block cipher shall be 128 bits. */
#define CCM_BLOCK_SIZE  16
92
#define CCM_DIGEST_SIZE 16
93 94 95 96 97 98 99 100 101 102
#define CCM_MIN_NONCE_SIZE 7
#define CCM_MAX_NONCE_SIZE 14

/* Maximum cleartext message size, as a function of the nonce size N.
   The length field is L octets, with L = 15 - N, and then the maximum
   size M = 2^{8L} - 1. */
#define CCM_MAX_MSG_SIZE(N)			\
  ((sizeof(size_t) + (N) <= 15)			\
   ? ~(size_t) 0				\
   : ((size_t) 1 << (8*(15 - N))) - 1)
Owen Kirby's avatar
Owen Kirby committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

/* Per-message state */
struct ccm_ctx {
  union nettle_block16 ctr;     /* Counter for CTR encryption. */
  union nettle_block16 tag;     /* CBC-MAC message tag. */
  /* Length of data processed by the CBC-MAC modulus the block size */
  unsigned int blength;
};

/*
 * CCM mode requires the adata and message lengths when building the IV, which
 * prevents streaming processing and it incompatible with the AEAD API.
 */
void
ccm_set_nonce(struct ccm_ctx *ctx, const void *cipher, nettle_cipher_func *f,
	      size_t noncelen, const uint8_t *nonce,
	      size_t authlen, size_t msglen, size_t taglen);

void
ccm_update(struct ccm_ctx *ctx, const void *cipher, nettle_cipher_func *f,
	    size_t length, const uint8_t *data);

void
ccm_encrypt(struct ccm_ctx *ctx, const void *cipher, nettle_cipher_func *f,
	    size_t length, uint8_t *dst, const uint8_t *src);

void
ccm_decrypt(struct ccm_ctx *ctx, const void *cipher, nettle_cipher_func *f,
	    size_t length, uint8_t *dst, const uint8_t *src);

void
ccm_digest(struct ccm_ctx *ctx, const void *cipher, nettle_cipher_func *f,
	   size_t length, uint8_t *digest);

/*
 * All-in-one encryption and decryption API:
 *  tlength = sizeof(digest)
 *  mlength = sizeof(cleartext)
 *  clength = sizeof(ciphertext) = mlength + tlength
 *
 * The ciphertext will contain the encrypted payload with the message digest
 * appended to the end.
 */
void
ccm_encrypt_message(const void *cipher, nettle_cipher_func *f,
		    size_t nlength, const uint8_t *nonce,
		    size_t alength, const uint8_t *adata,
		    size_t tlength,
		    size_t clength, uint8_t *dst, const uint8_t *src);

/*
 * The decryption function will write the plaintext to dst and parse the digest
 * from the final tlength bytes of the ciphertext. If the digest matched the
 * value computed during decryption then this will return 1, or it will return
 * 0 if the digest was invalid.
 */
int
ccm_decrypt_message(const void *cipher, nettle_cipher_func *f,
		    size_t nlength, const uint8_t *nonce,
		    size_t alength, const uint8_t *adata,
		    size_t tlength,
164
		    size_t mlength, uint8_t *dst, const uint8_t *src);
Owen Kirby's avatar
Owen Kirby committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

/* CCM Mode with AES-128 */
struct ccm_aes128_ctx {
    struct ccm_ctx      ccm;
    struct aes128_ctx   cipher;
};

void
ccm_aes128_set_key(struct ccm_aes128_ctx *ctx, const uint8_t *key);

void
ccm_aes128_set_nonce(struct ccm_aes128_ctx *ctx,
		     size_t length, const uint8_t *nonce,
		     size_t authlen, size_t msglen, size_t taglen);

void
ccm_aes128_update (struct ccm_aes128_ctx *ctx,
		   size_t length, const uint8_t *data);

void
ccm_aes128_encrypt(struct ccm_aes128_ctx *ctx,
		   size_t length, uint8_t *dst, const uint8_t *src);

void
ccm_aes128_decrypt(struct ccm_aes128_ctx *ctx,
		   size_t length, uint8_t *dst, const uint8_t *src);

void
ccm_aes128_digest(struct ccm_aes128_ctx *ctx,
		  size_t length, uint8_t *digest);

void
ccm_aes128_encrypt_message(struct ccm_aes128_ctx *ctx,
			   size_t nlength, const uint8_t *nonce,
			   size_t alength, const uint8_t *adata,
			   size_t tlength,
			   size_t clength, uint8_t *dst, const uint8_t *src);

int
ccm_aes128_decrypt_message(struct ccm_aes128_ctx *ctx,
			   size_t nlength, const uint8_t *nonce,
			   size_t alength, const uint8_t *adata,
			   size_t tlength,
208
			   size_t mlength, uint8_t *dst, const uint8_t *src);
Owen Kirby's avatar
Owen Kirby committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

struct ccm_aes192_ctx {
    struct ccm_ctx      ccm;
    struct aes192_ctx   cipher;
};

/* CCM Mode with AES-192 */
void
ccm_aes192_set_key(struct ccm_aes192_ctx *ctx, const uint8_t *key);

void
ccm_aes192_set_nonce(struct ccm_aes192_ctx *ctx,
		     size_t length, const uint8_t *nonce,
		     size_t authlen, size_t msglen, size_t taglen);

void
ccm_aes192_update(struct ccm_aes192_ctx *ctx,
		  size_t length, const uint8_t *data);

void
ccm_aes192_encrypt(struct ccm_aes192_ctx *ctx,
		   size_t length, uint8_t *dst, const uint8_t *src);

void
ccm_aes192_decrypt(struct ccm_aes192_ctx *ctx,
		   size_t length, uint8_t *dst, const uint8_t *src);

void
ccm_aes192_digest(struct ccm_aes192_ctx *ctx,
		  size_t length, uint8_t *digest);

void
ccm_aes192_encrypt_message(struct ccm_aes192_ctx *ctx,
			   size_t nlength, const uint8_t *nonce,
			   size_t alength, const uint8_t *adata,
			   size_t tlength,
			   size_t clength, uint8_t *dst, const uint8_t *src);

int
ccm_aes192_decrypt_message(struct ccm_aes192_ctx *ctx,
			   size_t nlength, const uint8_t *nonce,
			   size_t alength, const uint8_t *adata,
			   size_t tlength,
252
			   size_t mlength, uint8_t *dst, const uint8_t *src);
Owen Kirby's avatar
Owen Kirby committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

/* CCM Mode with AES-256 */
struct ccm_aes256_ctx {
    struct ccm_ctx      ccm;
    struct aes256_ctx   cipher;
};

void
ccm_aes256_set_key(struct ccm_aes256_ctx *ctx, const uint8_t *key);

void
ccm_aes256_set_nonce(struct ccm_aes256_ctx *ctx,
		     size_t length, const uint8_t *nonce,
		     size_t authlen, size_t msglen, size_t taglen);

void
ccm_aes256_update(struct ccm_aes256_ctx *ctx,
		  size_t length, const uint8_t *data);

void
ccm_aes256_encrypt(struct ccm_aes256_ctx *ctx,
		   size_t length, uint8_t *dst, const uint8_t *src);

void
ccm_aes256_decrypt(struct ccm_aes256_ctx *ctx,
		   size_t length, uint8_t *dst, const uint8_t *src);

void
ccm_aes256_digest(struct ccm_aes256_ctx *ctx,
		  size_t length, uint8_t *digest);

void
ccm_aes256_encrypt_message(struct ccm_aes256_ctx *ctx,
			   size_t nlength, const uint8_t *nonce,
			   size_t alength, const uint8_t *adata,
			   size_t tlength,
			   size_t clength, uint8_t *dst, const uint8_t *src);

int
ccm_aes256_decrypt_message(struct ccm_aes256_ctx *ctx,
			   size_t nlength, const uint8_t *nonce,
			   size_t alength, const uint8_t *adata,
			   size_t tlength,
296
			   size_t mlength, uint8_t *dst, const uint8_t *src);
Owen Kirby's avatar
Owen Kirby committed
297 298 299 300 301 302

#ifdef __cplusplus
}
#endif

#endif /* NETTLE_CCM_H_INCLUDED */