File: crypto_aesctr_shared.c

package info (click to toggle)
python-scrypt 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 832 kB
  • sloc: ansic: 6,290; python: 733; sh: 99; makefile: 5
file content (103 lines) | stat: -rw-r--r-- 2,858 bytes parent folder | download | duplicates (4)
1
2
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
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
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * This code is shared between crypto_aesctr*.c files, and should not be
 * compiled as a separate translation unit.  For details, see the comments in
 * those files.
 */

/* AES-CTR state. */
struct crypto_aesctr {
	const struct crypto_aes_key * key;
	uint64_t bytectr;
	uint8_t buf[16];
	uint8_t pblk[16];
};

/* Generate a block of cipherstream. */
static inline void
crypto_aesctr_stream_cipherblock_generate(struct crypto_aesctr * stream)
{

	/* Sanity check. */
	assert(stream->bytectr % 16 == 0);

	/* Prepare counter. */
	stream->pblk[15]++;
	if (stream->pblk[15] == 0) {
		/*
		 * If incrementing the least significant byte resulted in it
		 * wrapping, re-encode the complete 64-bit value.
		 */
		be64enc(stream->pblk + 8, stream->bytectr / 16);
	}

	/* Encrypt the cipherblock. */
	crypto_aes_encrypt_block(stream->pblk, stream->buf, stream->key);
}

/* Encrypt ${nbytes} bytes, then update ${inbuf}, ${outbuf}, and ${buflen}. */
static inline void
crypto_aesctr_stream_cipherblock_use(struct crypto_aesctr * stream,
    const uint8_t ** inbuf, uint8_t ** outbuf, size_t * buflen, size_t nbytes,
    size_t bytemod)
{
	size_t i;

	/* Encrypt the byte(s). */
	for (i = 0; i < nbytes; i++)
		(*outbuf)[i] = (*inbuf)[i] ^ stream->buf[bytemod + i];

	/* Move to the next byte(s) of cipherstream. */
	stream->bytectr += nbytes;

	/* Update the positions. */
	*inbuf += nbytes;
	*outbuf += nbytes;
	*buflen -= nbytes;
}

/*
 * Process any bytes before we can process a whole block.  Return 1 if there
 * are no bytes left to process after calling this function.
 */
static inline int
crypto_aesctr_stream_pre_wholeblock(struct crypto_aesctr * stream,
    const uint8_t ** inbuf, uint8_t ** outbuf, size_t * buflen_p)
{
	size_t bytemod;

	/* Do we have any bytes left in the current cipherblock? */
	bytemod = stream->bytectr % 16;
	if (bytemod != 0) {
		/* Do we have enough to complete the request? */
		if (bytemod + *buflen_p <= 16) {
			/* Process only buflen bytes, then return. */
			crypto_aesctr_stream_cipherblock_use(stream, inbuf,
			    outbuf, buflen_p, *buflen_p, bytemod);
			return (1);
		}

		/* Encrypt the byte(s) and update the positions. */
		crypto_aesctr_stream_cipherblock_use(stream, inbuf, outbuf,
		    buflen_p, 16 - bytemod, bytemod);
	}

	/* Success! */
	return (0);
}

/* Process any final bytes after finishing all whole blocks. */
static inline void
crypto_aesctr_stream_post_wholeblock(struct crypto_aesctr * stream,
    const uint8_t ** inbuf, uint8_t ** outbuf, size_t * buflen_p)
{

	/* Process any final bytes; we need a new cipherblock. */
	if (*buflen_p > 0) {
		/* Generate a block of cipherstream. */
		crypto_aesctr_stream_cipherblock_generate(stream);

		/* Encrypt the byte(s) and update the positions. */
		crypto_aesctr_stream_cipherblock_use(stream, inbuf, outbuf,
		    buflen_p, *buflen_p, 0);
	}
}