Initialize Tizen 2.3
[external/nettle.git] / examples / rsa-session.h
1 /* Session key definitions for the rsa-encrypt and rsa-decrypt programs.
2  */
3
4 #ifndef NETTLE_EXAMPLES_RSA_SESSION_H_INCLUDED
5 #define NETTLE_EXAMPLES_RSA_SESSION_H_INCLUDED
6
7 #include "aes.h"
8 #include "cbc.h"
9 #include "hmac.h"
10
11 #define RSA_VERSION 1
12
13 /* Encryption program using the following file format:
14
15      uint32_t version = 1;
16      uint32_t nsize;
17      uint8_t x[nsize];
18      uint8_t encrypted[n];
19      uint8_t hmac[SHA1_DIGEST_SIZE];
20
21    where x is the data
22
23      uint32_t version = 1;
24      uint8_t aes_key[AES_KEY_SIZE];
25      uint8_t iv[AES_BLOCK_SIZE];
26      uint8_t hmac_key[SHA1_DIGEST_SIZE];
27
28    of size (4 + AES_KEY_SIZE + AES_BLOCK_SIZE + SHA1_DIGEST_SIZE) = 72
29    bytes, encrypted using rsa-pkcs1.
30
31    The cleartext input is encrypted using aes-cbc. The final block is
32    padded as
33
34      | data | random octets | padding length |
35
36    where the last octet is the padding length, a number between 1 and
37    AES_BLOCK_SIZE (inclusive).
38 */
39
40 struct rsa_session
41 {
42   struct CBC_CTX(struct aes_ctx, AES_BLOCK_SIZE) aes;
43   struct hmac_sha1_ctx hmac;
44   struct yarrow256_ctx yarrow;
45 };
46
47 struct rsa_session_info
48 {
49   /* Version followed by aes key, iv and mac key */
50   uint8_t key[4 + AES_KEY_SIZE + AES_BLOCK_SIZE + SHA1_DIGEST_SIZE];
51 };
52
53 #define SESSION_VERSION(s) ((s)->key)
54 #define SESSION_AES_KEY(s) ((s)->key + 4)
55 #define SESSION_IV(s) ((s)->key + 4 + AES_KEY_SIZE)
56 #define SESSION_HMAC_KEY(s) ((s)->key + 4 + AES_KEY_SIZE + AES_BLOCK_SIZE)
57
58 void
59 rsa_session_set_encrypt_key(struct rsa_session *ctx,
60                             const struct rsa_session_info *key);
61
62 void
63 rsa_session_set_decrypt_key(struct rsa_session *ctx,
64                             const struct rsa_session_info *key);
65
66 #endif /* NETTLE_EXAMPLES_RSA_SESSION_H_INCLUDED */