~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/lib/ffi/ffi_block.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* (C) 2015,2017 Jack Lloyd
 
3
*
 
4
* Botan is released under the Simplified BSD License (see license.txt)
 
5
*/
 
6
 
 
7
#include <botan/ffi.h>
 
8
#include <botan/internal/ffi_util.h>
 
9
#include <botan/block_cipher.h>
 
10
 
 
11
extern "C" {
 
12
 
 
13
using namespace Botan_FFI;
 
14
 
 
15
BOTAN_FFI_DECLARE_STRUCT(botan_block_cipher_struct, Botan::BlockCipher, 0x64C29716);
 
16
 
 
17
int botan_block_cipher_init(botan_block_cipher_t* bc, const char* bc_name)
 
18
   {
 
19
   return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
 
20
      if(bc == nullptr || bc_name == nullptr || *bc_name == 0)
 
21
         return BOTAN_FFI_ERROR_NULL_POINTER;
 
22
 
 
23
      *bc = nullptr;
 
24
 
 
25
      std::unique_ptr<Botan::BlockCipher> cipher(Botan::BlockCipher::create(bc_name));
 
26
      if(cipher == nullptr)
 
27
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
 
28
 
 
29
      *bc = new botan_block_cipher_struct(cipher.release());
 
30
      return BOTAN_FFI_SUCCESS;
 
31
      });
 
32
   }
 
33
 
 
34
/**
 
35
* Destroy a block cipher object
 
36
*/
 
37
int botan_block_cipher_destroy(botan_block_cipher_t bc)
 
38
   {
 
39
   return BOTAN_FFI_CHECKED_DELETE(bc);
 
40
   }
 
41
 
 
42
int botan_block_cipher_clear(botan_block_cipher_t bc)
 
43
   {
 
44
   return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.clear(); });
 
45
   }
 
46
 
 
47
/**
 
48
* Set the key for a block cipher instance
 
49
*/
 
50
int botan_block_cipher_set_key(botan_block_cipher_t bc,
 
51
                               const uint8_t key[], size_t len)
 
52
   {
 
53
   return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.set_key(key, len); });
 
54
   }
 
55
 
 
56
/**
 
57
* Return the positive block size of this block cipher, or negative to
 
58
* indicate an error
 
59
*/
 
60
int botan_block_cipher_block_size(botan_block_cipher_t bc)
 
61
   {
 
62
   return BOTAN_FFI_DO(Botan::BlockCipher, bc, b,
 
63
                       { return static_cast<int>(b.block_size()); });
 
64
   }
 
65
 
 
66
int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc,
 
67
                                      const uint8_t in[],
 
68
                                      uint8_t out[],
 
69
                                      size_t blocks)
 
70
   {
 
71
   return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.encrypt_n(in, out, blocks); });
 
72
   }
 
73
 
 
74
int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc,
 
75
                                      const uint8_t in[],
 
76
                                      uint8_t out[],
 
77
                                      size_t blocks)
 
78
   {
 
79
   return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.decrypt_n(in, out, blocks); });
 
80
   }
 
81
 
 
82
}