cryptoengine.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Motenpoche
  2. *
  3. * (c) 2023 Daniele Lacamera <root@danielinux.net>
  4. *
  5. *
  6. * Motenpoche is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Motenpoche is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  19. *
  20. */
  21. #ifndef CRYPTOENGINE_H
  22. #define CRYPTOENGINE_H
  23. #include <wolfssl/wolfcrypt/ecc.h>
  24. #include <wolfssl/wolfcrypt/sha512.h>
  25. #define CRYPTO_KEY_SIZE 32
  26. #define PK_SIGNATURE_SIZE 64
  27. #define SEED_LEN 24
  28. #define SALT_LEN 64
  29. /* Use lower half of flash */
  30. #define VAULT_FLASH_OFFSET 0x00000000
  31. #define VAULT_FLASH_SIZE (512 * 1024)
  32. #define VAULT_DIGEST_SIZE WC_SHA512_DIGEST_SIZE
  33. #define SHA_PAYLOAD_SIZE (sizeof(struct vault_header) - (PK_SIGNATURE_SIZE + VAULT_DIGEST_SIZE))
  34. #define VAULT_MAGIC 0x5AFECA5E
  35. int cryptoengine_verify_passphrase(const char *passphrase, int *res);
  36. int cryptoengine_check_vault(void);
  37. struct __attribute__((packed)) vault_header {
  38. uint32_t magic; /* 0x5afeca5e */
  39. uint32_t size; /* Including magic and size */
  40. uint32_t id[2];
  41. uint8_t auth_pubkey[2 * CRYPTO_KEY_SIZE]; /* pc-signP, used to authenticate header on rekey/changes */
  42. uint8_t host_salt[SALT_LEN]; /* current PBKDF2 salt. vS = PBKDF2(passphrase, SALT) */
  43. uint8_t host_seed[SEED_LEN]; /* current 'seed' used for IV */
  44. uint8_t digest[VAULT_DIGEST_SIZE]; /* SHA digest of this header */
  45. uint8_t signature[PK_SIGNATURE_SIZE]; /* Signature of the digest */
  46. };
  47. struct __attribute__((packed)) vault_status {
  48. uint32_t id[2];
  49. uint8_t salt[SALT_LEN];
  50. uint8_t seed[SEED_LEN];
  51. uint16_t state;
  52. uint16_t services_active;
  53. uint16_t first_avail;
  54. };
  55. struct __attribute__((packed)) cdc_packet_hdr
  56. {
  57. uint32_t magic; /* 0x5AFECA5E */
  58. uint16_t cmd;
  59. uint16_t len;
  60. };
  61. #define CDC_STATUS 0x0000
  62. #define CDC_TOFU_INIT 0x0001
  63. #define CDC_CHALLENGE 0x0002
  64. #define CDC_REKEY 0x0003
  65. #define CDC_ADDSERV 0x0004
  66. #define CDC_DELSERV 0x0005
  67. #define CDC_OK 0x0100
  68. #define CDC_FAIL 0x0800
  69. #define MAX_CDC_CMD 512
  70. #define SETTINGS_FLASH_OFFSET 0x300
  71. #define SVC_FLASH_OFFSET 0x400
  72. #define SVC_NAME_SIZE_LIMIT 16
  73. #define SVC_USER_NAME_MAX 32
  74. #define SVC_PASSWORD_MAX 64
  75. #define SVC_FLAG_UNUSED 0xFFFFFFFF
  76. #define SVC_FLAG_ERASED 0xDEADC0DE
  77. #define SVC_FLAG_ACTIVE 0xAAAAAAAA
  78. struct __attribute__((packed)) vault_service
  79. {
  80. uint32_t flags;
  81. uint32_t reserved;
  82. char name[24];
  83. char user[32];
  84. char pass[64];
  85. uint8_t dig[64];
  86. uint8_t sig[64];
  87. };
  88. #define SVC_ENC_SIZE (256 - 8)
  89. #define SVC_ENC_OFF (8)
  90. #define SVC_SIZE (256)
  91. int cryptoengine_hdr_sha_check(const struct vault_header *vh);
  92. int cryptoengine_svc_sha_check(const struct vault_service *vs);
  93. int cryptoengine_svc_sig_verify(const struct vault_service *vs, int *res);
  94. int cryptoengine_import_service(struct vault_service *vs, uint32_t idx);
  95. int cryptoengine_service_count(uint16_t *n_srv, uint16_t *first_avail);
  96. int cryptoengine_fill_vault_status(struct vault_status *vst);
  97. int flash_decrypt_read_svc(struct vault_service *out, uint32_t addr);
  98. #endif